1e21c79978c02917106a86d63cc4802e57e6cb56
[lttng-tools.git] / tests / regression / tools / trigger / utils / register-some-triggers.cpp
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.hpp>
11 #include <common/macros.hpp>
12
13 #include <lttng/lttng.h>
14
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_error_code ret;
24
25 trigger = lttng_trigger_create(condition, action);
26 ret = lttng_register_trigger_with_name(trigger, trigger_name);
27 LTTNG_ASSERT(ret == LTTNG_OK);
28 lttng_trigger_destroy(trigger);
29 lttng_condition_destroy(condition);
30 lttng_action_destroy(action);
31 }
32
33 /*
34 * Register a trigger with the given condition and an action list containing a
35 * single notify action.
36 */
37 static void register_trigger_action_list_notify(const char *trigger_name,
38 struct lttng_condition *condition)
39 {
40 struct lttng_action *action_notify;
41 struct lttng_action *action_list;
42 enum lttng_action_status action_status;
43
44 action_list = lttng_action_list_create();
45 action_notify = lttng_action_notify_create();
46 action_status = lttng_action_list_add_action(action_list, action_notify);
47 LTTNG_ASSERT(action_status == LTTNG_ACTION_STATUS_OK);
48 lttng_action_destroy(action_notify);
49
50 register_trigger(trigger_name, condition, action_list);
51 }
52
53 static struct lttng_condition *create_session_consumed_size_condition(const char *session_name,
54 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(condition, session_name);
62 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
63 condition_status =
64 lttng_condition_session_consumed_size_set_threshold(condition, threshold);
65 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
66
67 return condition;
68 }
69
70 static void test_session_consumed_size_condition(void)
71 {
72 register_trigger_action_list_notify(
73 "trigger-with-session-consumed-size-condition",
74 create_session_consumed_size_condition("the-session-name", 1234));
75 }
76
77 static void fill_buffer_usage_condition(struct lttng_condition *condition,
78 const char *session_name,
79 const char *channel_name,
80 enum lttng_domain_type domain_type)
81 {
82 enum lttng_condition_status condition_status;
83
84 condition_status = lttng_condition_buffer_usage_set_session_name(condition, session_name);
85 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
86 condition_status = lttng_condition_buffer_usage_set_channel_name(condition, channel_name);
87 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
88 condition_status = lttng_condition_buffer_usage_set_domain_type(condition, domain_type);
89 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
90 }
91
92 static void fill_buffer_usage_bytes_condition(struct lttng_condition *condition,
93 const char *session_name,
94 const char *channel_name,
95 enum lttng_domain_type domain_type,
96 uint64_t threshold)
97 {
98 enum lttng_condition_status condition_status;
99
100 fill_buffer_usage_condition(condition, session_name, channel_name, domain_type);
101 condition_status = lttng_condition_buffer_usage_set_threshold(condition, threshold);
102 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
103 }
104
105 static void fill_buffer_usage_ratio_condition(struct lttng_condition *condition,
106 const char *session_name,
107 const char *channel_name,
108 enum lttng_domain_type domain_type,
109 double ratio)
110 {
111 enum lttng_condition_status condition_status;
112
113 fill_buffer_usage_condition(condition, session_name, channel_name, domain_type);
114 condition_status = lttng_condition_buffer_usage_set_threshold_ratio(condition, ratio);
115 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
116 }
117
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)
123 {
124 struct lttng_condition *condition;
125
126 condition = lttng_condition_buffer_usage_high_create();
127 fill_buffer_usage_bytes_condition(
128 condition, session_name, channel_name, domain_type, threshold);
129
130 return condition;
131 }
132
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)
138 {
139 struct lttng_condition *condition;
140
141 condition = lttng_condition_buffer_usage_low_create();
142 fill_buffer_usage_bytes_condition(
143 condition, session_name, channel_name, domain_type, threshold);
144
145 return condition;
146 }
147
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)
153 {
154 struct lttng_condition *condition;
155
156 condition = lttng_condition_buffer_usage_high_create();
157 fill_buffer_usage_ratio_condition(
158 condition, session_name, channel_name, domain_type, ratio);
159
160 return condition;
161 }
162
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)
168 {
169 struct lttng_condition *condition;
170
171 condition = lttng_condition_buffer_usage_low_create();
172 fill_buffer_usage_ratio_condition(
173 condition, session_name, channel_name, domain_type, ratio);
174
175 return condition;
176 }
177
178 static void test_buffer_usage_conditions(void)
179 {
180 register_trigger_action_list_notify(
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));
184
185 register_trigger_action_list_notify(
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));
189
190 register_trigger_action_list_notify(
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));
194
195 register_trigger_action_list_notify(
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));
199 }
200
201 static void fill_session_rotation_condition(struct lttng_condition *condition,
202 const char *session_name)
203 {
204 enum lttng_condition_status condition_status;
205
206 condition_status =
207 lttng_condition_session_rotation_set_session_name(condition, session_name);
208 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
209 }
210
211 static struct lttng_condition *create_session_rotation_ongoing_condition(const char *session_name)
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
222 static struct lttng_condition *create_session_rotation_completed_condition(const char *session_name)
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
233 static void test_session_rotation_conditions(void)
234 {
235 register_trigger_action_list_notify(
236 "trigger-with-session-rotation-ongoing-condition",
237 create_session_rotation_ongoing_condition("the-session-name"));
238
239 register_trigger_action_list_notify(
240 "trigger-with-session-rotation-completed-condition",
241 create_session_rotation_completed_condition("the-session-name"));
242 }
243
244 static struct {
245 const char *name;
246 void (*callback)(void);
247 } tests[] = {
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 },
254 };
255
256 static void show_known_tests(void)
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 }
This page took 0.035567 seconds and 4 git commands to generate.