Commit | Line | Data |
---|---|---|
19904669 SM |
1 | /* |
2 | * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0-only | |
5 | * | |
6 | */ | |
7 | ||
8 | /* Utility to register some triggers, for test purposes. */ | |
9 | ||
10 | #include <common/filter/filter-ast.h> | |
11 | #include <common/macros.h> | |
12 | #include <lttng/lttng.h> | |
13 | ||
14 | #include <assert.h> | |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | ||
18 | static void register_trigger(const char *trigger_name, | |
19 | struct lttng_condition *condition, | |
20 | struct lttng_action *action) | |
21 | { | |
22 | struct lttng_trigger *trigger; | |
a5c2d2a7 | 23 | enum lttng_error_code ret; |
19904669 SM |
24 | |
25 | trigger = lttng_trigger_create(condition, action); | |
a5c2d2a7 JG |
26 | ret = lttng_register_trigger_with_name(trigger, trigger_name); |
27 | assert(ret == LTTNG_OK); | |
0915ddc8 JG |
28 | lttng_trigger_destroy(trigger); |
29 | lttng_condition_destroy(condition); | |
30 | lttng_action_destroy(action); | |
19904669 SM |
31 | } |
32 | ||
33 | /* | |
702f26c8 | 34 | * Register a trigger with the given condition and an action list containing a |
19904669 SM |
35 | * single notify action. |
36 | */ | |
702f26c8 | 37 | static void register_trigger_action_list_notify( |
19904669 SM |
38 | const char *trigger_name, struct lttng_condition *condition) |
39 | { | |
40 | struct lttng_action *action_notify; | |
702f26c8 | 41 | struct lttng_action *action_list; |
19904669 SM |
42 | enum lttng_action_status action_status; |
43 | ||
702f26c8 | 44 | action_list = lttng_action_list_create(); |
19904669 | 45 | action_notify = lttng_action_notify_create(); |
702f26c8 JR |
46 | action_status = lttng_action_list_add_action( |
47 | action_list, action_notify); | |
19904669 | 48 | assert(action_status == LTTNG_ACTION_STATUS_OK); |
0915ddc8 | 49 | lttng_action_destroy(action_notify); |
19904669 | 50 | |
702f26c8 | 51 | register_trigger(trigger_name, condition, action_list); |
19904669 SM |
52 | } |
53 | ||
54 | static struct lttng_condition *create_session_consumed_size_condition( | |
55 | const char *session_name, uint64_t threshold) | |
56 | { | |
57 | struct lttng_condition *condition; | |
58 | enum lttng_condition_status condition_status; | |
59 | ||
60 | condition = lttng_condition_session_consumed_size_create(); | |
61 | condition_status = | |
62 | lttng_condition_session_consumed_size_set_session_name( | |
63 | condition, session_name); | |
64 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
65 | condition_status = lttng_condition_session_consumed_size_set_threshold( | |
66 | condition, threshold); | |
67 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
68 | ||
69 | return condition; | |
70 | } | |
71 | ||
72 | static void test_session_consumed_size_condition(void) | |
73 | { | |
702f26c8 | 74 | register_trigger_action_list_notify( |
19904669 SM |
75 | "trigger-with-session-consumed-size-condition", |
76 | create_session_consumed_size_condition( | |
77 | "the-session-name", 1234)); | |
78 | } | |
79 | ||
80 | static void fill_buffer_usage_condition(struct lttng_condition *condition, | |
81 | const char *session_name, | |
82 | const char *channel_name, | |
83 | enum lttng_domain_type domain_type) | |
84 | { | |
85 | enum lttng_condition_status condition_status; | |
86 | ||
87 | condition_status = lttng_condition_buffer_usage_set_session_name( | |
88 | condition, session_name); | |
89 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
90 | condition_status = lttng_condition_buffer_usage_set_channel_name( | |
91 | condition, channel_name); | |
92 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
93 | condition_status = lttng_condition_buffer_usage_set_domain_type( | |
94 | condition, domain_type); | |
95 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
96 | } | |
97 | ||
98 | static void fill_buffer_usage_bytes_condition(struct lttng_condition *condition, | |
99 | const char *session_name, | |
100 | const char *channel_name, | |
101 | enum lttng_domain_type domain_type, | |
102 | uint64_t threshold) | |
103 | { | |
104 | enum lttng_condition_status condition_status; | |
105 | ||
106 | fill_buffer_usage_condition( | |
107 | condition, session_name, channel_name, domain_type); | |
108 | condition_status = lttng_condition_buffer_usage_set_threshold( | |
109 | condition, threshold); | |
110 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
111 | } | |
112 | ||
113 | static void fill_buffer_usage_ratio_condition(struct lttng_condition *condition, | |
114 | const char *session_name, | |
115 | const char *channel_name, | |
116 | enum lttng_domain_type domain_type, | |
117 | double ratio) | |
118 | { | |
119 | enum lttng_condition_status condition_status; | |
120 | ||
121 | fill_buffer_usage_condition( | |
122 | condition, session_name, channel_name, domain_type); | |
123 | condition_status = lttng_condition_buffer_usage_set_threshold_ratio( | |
124 | condition, ratio); | |
125 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
126 | } | |
127 | ||
128 | static struct lttng_condition *create_buffer_usage_high_bytes_condition( | |
129 | const char *session_name, | |
130 | const char *channel_name, | |
131 | enum lttng_domain_type domain_type, | |
132 | uint64_t threshold) | |
133 | { | |
134 | struct lttng_condition *condition; | |
135 | ||
136 | condition = lttng_condition_buffer_usage_high_create(); | |
137 | fill_buffer_usage_bytes_condition(condition, session_name, channel_name, | |
138 | domain_type, threshold); | |
139 | ||
140 | return condition; | |
141 | } | |
142 | ||
143 | static struct lttng_condition *create_buffer_usage_low_bytes_condition( | |
144 | const char *session_name, | |
145 | const char *channel_name, | |
146 | enum lttng_domain_type domain_type, | |
147 | uint64_t threshold) | |
148 | { | |
149 | struct lttng_condition *condition; | |
150 | ||
151 | condition = lttng_condition_buffer_usage_low_create(); | |
152 | fill_buffer_usage_bytes_condition(condition, session_name, channel_name, | |
153 | domain_type, threshold); | |
154 | ||
155 | return condition; | |
156 | } | |
157 | ||
158 | static struct lttng_condition *create_buffer_usage_high_ratio_condition( | |
159 | const char *session_name, | |
160 | const char *channel_name, | |
161 | enum lttng_domain_type domain_type, | |
162 | double ratio) | |
163 | { | |
164 | struct lttng_condition *condition; | |
165 | ||
166 | condition = lttng_condition_buffer_usage_high_create(); | |
167 | fill_buffer_usage_ratio_condition(condition, session_name, channel_name, | |
168 | domain_type, ratio); | |
169 | ||
170 | return condition; | |
171 | } | |
172 | ||
173 | static struct lttng_condition *create_buffer_usage_low_ratio_condition( | |
174 | const char *session_name, | |
175 | const char *channel_name, | |
176 | enum lttng_domain_type domain_type, | |
177 | double ratio) | |
178 | { | |
179 | struct lttng_condition *condition; | |
180 | ||
181 | condition = lttng_condition_buffer_usage_low_create(); | |
182 | fill_buffer_usage_ratio_condition(condition, session_name, channel_name, | |
183 | domain_type, ratio); | |
184 | ||
185 | return condition; | |
186 | } | |
187 | ||
188 | static void test_buffer_usage_conditions(void) | |
189 | { | |
702f26c8 | 190 | register_trigger_action_list_notify( |
19904669 SM |
191 | "trigger-with-buffer-usage-high-bytes-condition", |
192 | create_buffer_usage_high_bytes_condition( | |
193 | "the-session-name", "the-channel-name", | |
194 | LTTNG_DOMAIN_UST, 1234)); | |
195 | ||
702f26c8 | 196 | register_trigger_action_list_notify( |
19904669 SM |
197 | "trigger-with-buffer-usage-low-bytes-condition", |
198 | create_buffer_usage_low_bytes_condition( | |
199 | "the-session-name", "the-channel-name", | |
200 | LTTNG_DOMAIN_UST, 2345)); | |
201 | ||
702f26c8 | 202 | register_trigger_action_list_notify( |
19904669 SM |
203 | "trigger-with-buffer-usage-high-ratio-condition", |
204 | create_buffer_usage_high_ratio_condition( | |
205 | "the-session-name", "the-channel-name", | |
206 | LTTNG_DOMAIN_UST, 0.25)); | |
207 | ||
702f26c8 | 208 | register_trigger_action_list_notify( |
19904669 SM |
209 | "trigger-with-buffer-usage-low-ratio-condition", |
210 | create_buffer_usage_low_ratio_condition( | |
211 | "the-session-name", "the-channel-name", | |
212 | LTTNG_DOMAIN_UST, 0.4)); | |
213 | } | |
214 | ||
215 | static void fill_session_rotation_condition( | |
216 | struct lttng_condition *condition, const char *session_name) | |
217 | { | |
218 | enum lttng_condition_status condition_status; | |
219 | ||
220 | condition_status = lttng_condition_session_rotation_set_session_name( | |
221 | condition, session_name); | |
222 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); | |
223 | } | |
224 | ||
225 | static struct lttng_condition *create_session_rotation_ongoing_condition( | |
226 | const char *session_name) | |
227 | { | |
228 | struct lttng_condition *condition; | |
229 | ||
230 | condition = lttng_condition_session_rotation_ongoing_create(); | |
231 | ||
232 | fill_session_rotation_condition(condition, session_name); | |
233 | ||
234 | return condition; | |
235 | } | |
236 | ||
237 | static struct lttng_condition *create_session_rotation_completed_condition( | |
238 | const char *session_name) | |
239 | { | |
240 | struct lttng_condition *condition; | |
241 | ||
242 | condition = lttng_condition_session_rotation_completed_create(); | |
243 | ||
244 | fill_session_rotation_condition(condition, session_name); | |
245 | ||
246 | return condition; | |
247 | } | |
248 | ||
249 | static void test_session_rotation_conditions(void) | |
250 | { | |
702f26c8 | 251 | register_trigger_action_list_notify( |
19904669 SM |
252 | "trigger-with-session-rotation-ongoing-condition", |
253 | create_session_rotation_ongoing_condition( | |
254 | "the-session-name")); | |
255 | ||
702f26c8 | 256 | register_trigger_action_list_notify( |
19904669 SM |
257 | "trigger-with-session-rotation-completed-condition", |
258 | create_session_rotation_completed_condition( | |
259 | "the-session-name")); | |
260 | } | |
261 | ||
262 | static struct { | |
263 | const char *name; | |
264 | void (*callback)(void); | |
265 | } tests[] = { | |
266 | { | |
267 | "test_session_consumed_size_condition", | |
268 | test_session_consumed_size_condition, | |
269 | }, | |
270 | {"test_buffer_usage_conditions", test_buffer_usage_conditions}, | |
271 | {"test_session_rotation_conditions", | |
272 | test_session_rotation_conditions}, | |
273 | }; | |
274 | ||
275 | static void show_known_tests(void) | |
276 | { | |
277 | size_t i; | |
278 | ||
279 | for (i = 0; i < ARRAY_SIZE(tests); i++) { | |
280 | fprintf(stderr, " - %s\n", tests[i].name); | |
281 | } | |
282 | } | |
283 | ||
284 | int main(int argc, char **argv) | |
285 | { | |
286 | const char *test; | |
287 | size_t i; | |
288 | int ret; | |
289 | ||
290 | if (argc != 2) { | |
291 | fprintf(stderr, "Usage: %s <test>\n", argv[0]); | |
292 | fprintf(stderr, "\n"); | |
293 | fprintf(stderr, "Test must be one of:\n"); | |
294 | show_known_tests(); | |
295 | goto error; | |
296 | } | |
297 | ||
298 | test = argv[1]; | |
299 | ||
300 | for (i = 0; i < ARRAY_SIZE(tests); i++) { | |
301 | if (strcmp(tests[i].name, test) == 0) { | |
302 | break; | |
303 | } | |
304 | } | |
305 | ||
306 | if (i == ARRAY_SIZE(tests)) { | |
307 | fprintf(stderr, "Unrecognized test `%s`\n", test); | |
308 | fprintf(stderr, "\n"); | |
309 | fprintf(stderr, "Known tests:\n"); | |
310 | show_known_tests(); | |
311 | goto error; | |
312 | } | |
313 | ||
314 | tests[i].callback(); | |
315 | ||
316 | ret = 0; | |
317 | goto end; | |
318 | ||
319 | error: | |
320 | ret = 1; | |
321 | ||
322 | end: | |
323 | return ret; | |
324 | } |