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