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