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