Build fix: missing stdio.h include in signal-helper.hpp
[lttng-tools.git] / tests / regression / tools / trigger / utils / register-some-triggers.cpp
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
c9e313bc
SM
10#include <common/filter/filter-ast.hpp>
11#include <common/macros.hpp>
19904669
SM
12#include <lttng/lttng.h>
13
19904669
SM
14#include <stdlib.h>
15#include <string.h>
16
17static 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);
06f325ca
JG
27 lttng_trigger_destroy(trigger);
28 lttng_condition_destroy(condition);
29 lttng_action_destroy(action);
19904669
SM
30}
31
32/*
702f26c8 33 * Register a trigger with the given condition and an action list containing a
19904669
SM
34 * single notify action.
35 */
702f26c8 36static void register_trigger_action_list_notify(
19904669
SM
37 const char *trigger_name, struct lttng_condition *condition)
38{
39 struct lttng_action *action_notify;
702f26c8 40 struct lttng_action *action_list;
19904669
SM
41 enum lttng_action_status action_status;
42
702f26c8 43 action_list = lttng_action_list_create();
19904669 44 action_notify = lttng_action_notify_create();
702f26c8
JR
45 action_status = lttng_action_list_add_action(
46 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
53static 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);
a0377dfe 63 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
19904669
SM
64 condition_status = lttng_condition_session_consumed_size_set_threshold(
65 condition, threshold);
a0377dfe 66 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
19904669
SM
67
68 return condition;
69}
70
71static void test_session_consumed_size_condition(void)
72{
702f26c8 73 register_trigger_action_list_notify(
19904669
SM
74 "trigger-with-session-consumed-size-condition",
75 create_session_consumed_size_condition(
76 "the-session-name", 1234));
77}
78
79static 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);
a0377dfe 88 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
19904669
SM
89 condition_status = lttng_condition_buffer_usage_set_channel_name(
90 condition, channel_name);
a0377dfe 91 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
19904669
SM
92 condition_status = lttng_condition_buffer_usage_set_domain_type(
93 condition, domain_type);
a0377dfe 94 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
19904669
SM
95}
96
97static 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);
a0377dfe 109 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
19904669
SM
110}
111
112static 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);
a0377dfe 124 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
19904669
SM
125}
126
127static 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
142static 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
157static 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
172static 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
187static void test_buffer_usage_conditions(void)
188{
702f26c8 189 register_trigger_action_list_notify(
19904669
SM
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
702f26c8 195 register_trigger_action_list_notify(
19904669
SM
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
702f26c8 201 register_trigger_action_list_notify(
19904669
SM
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
702f26c8 207 register_trigger_action_list_notify(
19904669
SM
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
214static 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);
a0377dfe 221 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
19904669
SM
222}
223
224static 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
236static 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
248static void test_session_rotation_conditions(void)
249{
702f26c8 250 register_trigger_action_list_notify(
19904669
SM
251 "trigger-with-session-rotation-ongoing-condition",
252 create_session_rotation_ongoing_condition(
253 "the-session-name"));
254
702f26c8 255 register_trigger_action_list_notify(
19904669
SM
256 "trigger-with-session-rotation-completed-condition",
257 create_session_rotation_completed_condition(
258 "the-session-name"));
259}
260
261static 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
274static 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
283int 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
318error:
319 ret = 1;
320
321end:
322 return ret;
323}
This page took 0.045563 seconds and 4 git commands to generate.