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