Commit | Line | Data |
---|---|---|
434f8068 JR |
1 | /* |
2 | * notification.c | |
3 | * | |
4 | * Tests suite for LTTng notification API | |
5 | * | |
6 | * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
27 | #include <assert.h> | |
28 | #include <math.h> | |
29 | #include <stdbool.h> | |
30 | #include <stdio.h> | |
31 | #include <stdlib.h> | |
32 | #include <string.h> | |
33 | #include <unistd.h> | |
34 | #include <inttypes.h> | |
35 | #include <sys/types.h> | |
36 | #include <sys/stat.h> | |
37 | #include <fcntl.h> | |
854382b8 JR |
38 | #include <signal.h> |
39 | #include <errno.h> | |
40 | #include <poll.h> | |
434f8068 JR |
41 | |
42 | #include <lttng/action/action.h> | |
43 | #include <lttng/action/notify.h> | |
44 | #include <lttng/condition/buffer-usage.h> | |
45 | #include <lttng/condition/condition.h> | |
46 | #include <lttng/condition/evaluation.h> | |
47 | #include <lttng/domain.h> | |
48 | #include <lttng/endpoint.h> | |
49 | #include <lttng/lttng-error.h> | |
50 | #include <lttng/notification/channel.h> | |
51 | #include <lttng/notification/notification.h> | |
52 | #include <lttng/trigger/trigger.h> | |
25040abc | 53 | #include <lttng/lttng.h> |
434f8068 JR |
54 | |
55 | #include <tap/tap.h> | |
56 | ||
57 | #define NUM_TESTS 104 | |
854382b8 | 58 | |
434f8068 JR |
59 | int nb_args = 0; |
60 | int named_pipe_args_start = 0; | |
854382b8 JR |
61 | pid_t app_pid = -1; |
62 | const char *app_state_file = NULL; | |
63 | ||
64 | static | |
65 | void wait_on_file(const char *path, bool file_exist) | |
66 | { | |
67 | if (!path) { | |
68 | return; | |
69 | } | |
70 | for (;;) { | |
71 | int ret; | |
72 | struct stat buf; | |
73 | ||
74 | ret = stat(path, &buf); | |
75 | if (ret == -1 && errno == ENOENT) { | |
76 | if (file_exist) { | |
9090be90 FD |
77 | /* |
78 | * The file does not exist. wait a bit and | |
79 | * continue looping until it does. | |
80 | */ | |
81 | (void) poll(NULL, 0, 10); | |
82 | continue; | |
854382b8 | 83 | } |
9090be90 FD |
84 | |
85 | /* | |
86 | * File does not exist and the exit condition we want. | |
87 | * Break from the loop and return. | |
88 | */ | |
89 | break; | |
854382b8 JR |
90 | } |
91 | if (ret) { | |
92 | perror("stat"); | |
93 | exit(EXIT_FAILURE); | |
94 | } | |
9090be90 FD |
95 | /* |
96 | * stat() returned 0, so the file exists. break now only if | |
97 | * that's the exit condition we want. | |
98 | */ | |
99 | if (file_exist) { | |
100 | break; | |
101 | } | |
854382b8 JR |
102 | } |
103 | } | |
434f8068 JR |
104 | |
105 | int write_pipe(const char *path, uint8_t data) | |
106 | { | |
107 | int ret = 0; | |
108 | int fd = 0; | |
109 | ||
110 | fd = open(path, O_WRONLY | O_NONBLOCK); | |
111 | if (fd < 0) { | |
112 | perror("Could not open consumer control named pipe"); | |
113 | goto end; | |
114 | } | |
115 | ||
116 | ret = write(fd, &data , sizeof(data)); | |
117 | if (ret < 1) { | |
118 | perror("Named pipe write failed"); | |
eff748d0 JR |
119 | if (close(fd)) { |
120 | perror("Named pipe close failed"); | |
121 | } | |
434f8068 JR |
122 | ret = -1; |
123 | goto end; | |
124 | } | |
125 | ||
126 | ret = close(fd); | |
127 | if (ret < 0) { | |
128 | perror("Name pipe closing failed"); | |
129 | ret = -1; | |
130 | goto end; | |
131 | } | |
132 | end: | |
133 | return ret; | |
134 | } | |
135 | ||
136 | int stop_consumer(const char **argv) | |
137 | { | |
138 | int ret = 0; | |
139 | for (int i = named_pipe_args_start; i < nb_args; i++) { | |
140 | ret = write_pipe(argv[i], 49); | |
141 | } | |
142 | return ret; | |
143 | } | |
144 | ||
145 | int resume_consumer(const char **argv) | |
146 | { | |
147 | int ret = 0; | |
148 | for (int i = named_pipe_args_start; i < nb_args; i++) { | |
149 | ret = write_pipe(argv[i], 0); | |
150 | } | |
151 | return ret; | |
152 | } | |
153 | ||
854382b8 JR |
154 | int suspend_application() |
155 | { | |
156 | int ret; | |
157 | struct stat buf; | |
158 | ||
159 | if (!stat(app_state_file, &buf)) { | |
160 | fail("App is already in a suspended state."); | |
161 | ret = -1; | |
162 | goto error; | |
163 | } | |
164 | ||
165 | /* | |
166 | * Send SIGUSR1 to application instructing it to bypass tracepoint. | |
167 | */ | |
168 | ret = kill(app_pid, SIGUSR1); | |
169 | if (ret) { | |
170 | fail("SIGUSR1 failed. errno %d", errno); | |
171 | ret = -1; | |
172 | goto error; | |
173 | } | |
174 | ||
175 | wait_on_file(app_state_file, true); | |
176 | ||
177 | error: | |
178 | return ret; | |
179 | ||
180 | } | |
181 | ||
182 | int resume_application() | |
183 | { | |
184 | int ret; | |
185 | struct stat buf; | |
186 | ||
187 | ret = stat(app_state_file, &buf); | |
188 | if (ret == -1 && errno == ENOENT) { | |
189 | fail("State file does not exist"); | |
190 | goto error; | |
191 | } | |
192 | if (ret) { | |
193 | perror("stat"); | |
194 | goto error; | |
195 | } | |
196 | ||
197 | ret = kill(app_pid, SIGUSR1); | |
198 | if (ret) { | |
199 | fail("SIGUSR1 failed. errno %d", errno); | |
200 | ret = -1; | |
201 | goto error; | |
202 | } | |
203 | ||
204 | wait_on_file(app_state_file, false); | |
205 | ||
206 | error: | |
207 | return ret; | |
208 | ||
209 | } | |
210 | ||
211 | ||
434f8068 JR |
212 | void test_triggers_buffer_usage_condition(const char *session_name, |
213 | const char *channel_name, | |
214 | enum lttng_domain_type domain_type, | |
215 | enum lttng_condition_type condition_type) | |
216 | { | |
217 | enum lttng_condition_status condition_status; | |
218 | struct lttng_action *action; | |
219 | ||
220 | /* Set-up */ | |
221 | action = lttng_action_notify_create(); | |
222 | if (!action) { | |
223 | fail("Setup error on action creation"); | |
224 | goto end; | |
225 | } | |
226 | ||
227 | /* Test lttng_register_trigger with null value */ | |
228 | ok(lttng_register_trigger(NULL) == -LTTNG_ERR_INVALID, "Registering a NULL trigger fails as expected"); | |
229 | ||
230 | /* Test: register a trigger */ | |
231 | unsigned int test_vector_size = 5; | |
232 | for (unsigned int i = 0; i < pow(2,test_vector_size); i++) { | |
233 | int loop_ret = 0; | |
234 | char *test_tuple_string = NULL; | |
235 | unsigned int mask_position = 0; | |
236 | bool session_name_set = false; | |
237 | bool channel_name_set = false; | |
238 | bool threshold_ratio_set = false; | |
239 | bool threshold_byte_set = false; | |
240 | bool domain_type_set = false; | |
241 | ||
242 | struct lttng_trigger *trigger = NULL; | |
243 | struct lttng_condition *condition = NULL; | |
244 | ||
245 | /* Create base condition */ | |
246 | switch (condition_type) { | |
247 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
248 | condition = lttng_condition_buffer_usage_low_create(); | |
249 | break; | |
250 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
251 | condition = lttng_condition_buffer_usage_high_create(); | |
252 | break; | |
253 | default: | |
254 | loop_ret = 1; | |
255 | goto loop_end; | |
256 | } | |
257 | ||
258 | if (!condition) { | |
259 | loop_ret = 1; | |
260 | goto loop_end; | |
261 | ||
262 | } | |
263 | ||
264 | /* Prepare the condition for trigger registration test */ | |
265 | ||
266 | /* Set session name */ | |
267 | if ((1 << mask_position) & i) { | |
268 | condition_status = lttng_condition_buffer_usage_set_session_name( | |
269 | condition, session_name); | |
270 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
271 | loop_ret = 1; | |
272 | goto loop_end; | |
273 | } | |
274 | session_name_set = true; | |
275 | } | |
276 | mask_position++; | |
277 | ||
278 | /* Set channel name */ | |
279 | if ((1 << mask_position) & i) { | |
280 | condition_status = lttng_condition_buffer_usage_set_channel_name( | |
281 | condition, channel_name); | |
282 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
283 | loop_ret = 1; | |
284 | goto loop_end; | |
285 | } | |
286 | channel_name_set = true; | |
287 | } | |
288 | mask_position++; | |
289 | ||
290 | /* Set threshold ratio */ | |
291 | if ((1 << mask_position) & i) { | |
292 | condition_status = lttng_condition_buffer_usage_set_threshold_ratio( | |
293 | condition, 0.0); | |
294 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
295 | loop_ret = 1; | |
296 | goto loop_end; | |
297 | } | |
298 | threshold_ratio_set = true; | |
299 | } | |
300 | mask_position++; | |
301 | ||
302 | /* Set threshold byte */ | |
303 | if ((1 << mask_position) & i) { | |
304 | condition_status = lttng_condition_buffer_usage_set_threshold( | |
305 | condition, 0); | |
306 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
307 | loop_ret = 1; | |
308 | goto loop_end; | |
309 | } | |
310 | threshold_byte_set = true; | |
311 | } | |
312 | mask_position++; | |
313 | ||
314 | /* Set domain type */ | |
315 | if ((1 << mask_position) & i) { | |
316 | condition_status = lttng_condition_buffer_usage_set_domain_type( | |
317 | condition, LTTNG_DOMAIN_UST); | |
318 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
319 | loop_ret = 1; | |
320 | goto loop_end; | |
321 | } | |
322 | domain_type_set = true; | |
323 | } | |
324 | ||
325 | /* Safety check */ | |
326 | if (mask_position != test_vector_size -1) { | |
327 | assert("Logic error for test vector generation"); | |
328 | } | |
329 | ||
330 | loop_ret = asprintf(&test_tuple_string, "session name %s, channel name %s, threshold ratio %s, threshold byte %s, domain type %s", | |
331 | session_name_set ? "set" : "unset", | |
332 | channel_name_set ? "set" : "unset", | |
333 | threshold_ratio_set ? "set" : "unset", | |
334 | threshold_byte_set ? "set" : "unset", | |
335 | domain_type_set? "set" : "unset"); | |
336 | if (!test_tuple_string || loop_ret < 0) { | |
337 | loop_ret = 1; | |
338 | goto loop_end; | |
339 | } | |
340 | ||
341 | /* Create trigger */ | |
342 | trigger = lttng_trigger_create(condition, action); | |
343 | if (!trigger) { | |
344 | loop_ret = 1; | |
345 | goto loop_end; | |
346 | } | |
347 | ||
348 | loop_ret = lttng_register_trigger(trigger); | |
349 | ||
350 | loop_end: | |
351 | if (loop_ret == 1) { | |
352 | fail("Setup error occurred for tuple: %s", test_tuple_string); | |
353 | goto loop_cleanup; | |
354 | } | |
355 | ||
356 | /* This combination happens three times */ | |
357 | if (session_name_set && channel_name_set | |
358 | && (threshold_ratio_set || threshold_byte_set) | |
359 | && domain_type_set) { | |
360 | ok(loop_ret == 0, "Trigger is registered: %s", test_tuple_string); | |
361 | ||
362 | /* | |
363 | * Test that a trigger cannot be registered | |
364 | * multiple time. | |
365 | */ | |
366 | loop_ret = lttng_register_trigger(trigger); | |
367 | ok(loop_ret == -LTTNG_ERR_TRIGGER_EXISTS, "Re-register trigger fails as expected: %s", test_tuple_string); | |
368 | ||
369 | /* Test that a trigger can be unregistered */ | |
370 | loop_ret = lttng_unregister_trigger(trigger); | |
371 | ok(loop_ret == 0, "Unregister trigger: %s", test_tuple_string); | |
372 | ||
373 | /* | |
374 | * Test that unregistration of a non-previously | |
375 | * registered trigger fail. | |
376 | */ | |
377 | loop_ret = lttng_unregister_trigger(trigger); | |
30d8bd89 | 378 | ok(loop_ret == -LTTNG_ERR_TRIGGER_NOT_FOUND, "Unregister of a non-registered trigger fails as expected: %s", test_tuple_string); |
434f8068 JR |
379 | } else { |
380 | ok(loop_ret == -LTTNG_ERR_INVALID_TRIGGER, "Trigger is invalid as expected and cannot be registered: %s", test_tuple_string); | |
381 | } | |
382 | ||
383 | loop_cleanup: | |
384 | free(test_tuple_string); | |
385 | lttng_trigger_destroy(trigger); | |
386 | lttng_condition_destroy(condition); | |
387 | } | |
388 | ||
389 | end: | |
390 | lttng_action_destroy(action); | |
391 | } | |
392 | ||
ff2b03c8 JG |
393 | static |
394 | void wait_data_pending(const char *session_name) | |
395 | { | |
396 | int ret; | |
397 | ||
398 | do { | |
399 | ret = lttng_data_pending(session_name); | |
400 | assert(ret >= 0); | |
401 | } while (ret != 0); | |
402 | } | |
403 | ||
854382b8 | 404 | void test_notification_channel(const char *session_name, const char *channel_name, const enum lttng_domain_type domain_type, const char **argv) |
434f8068 JR |
405 | { |
406 | int ret = 0; | |
407 | enum lttng_condition_status condition_status; | |
408 | enum lttng_notification_channel_status nc_status; | |
409 | ||
410 | struct lttng_action *action = NULL; | |
411 | struct lttng_notification *notification = NULL; | |
412 | struct lttng_notification_channel *notification_channel = NULL; | |
413 | struct lttng_trigger *trigger = NULL; | |
414 | ||
415 | struct lttng_condition *low_condition = NULL; | |
416 | struct lttng_condition *high_condition = NULL; | |
417 | struct lttng_condition *dummy_invalid_condition = NULL; | |
418 | struct lttng_condition *dummy_condition = NULL; | |
419 | ||
420 | double low_ratio = 0.0; | |
421 | double high_ratio = 0.99; | |
422 | ||
423 | /* Set-up */ | |
424 | action = lttng_action_notify_create(); | |
425 | if (!action) { | |
426 | fail("Setup error on action creation"); | |
427 | goto end; | |
428 | } | |
429 | ||
430 | /* Create a dummy, empty condition for later test */ | |
431 | dummy_invalid_condition = lttng_condition_buffer_usage_low_create(); | |
432 | if (!dummy_invalid_condition) { | |
433 | fail("Setup error on condition creation"); | |
434 | goto end; | |
435 | } | |
436 | ||
437 | /* Create a valid dummy condition with a ratio of 0.5 */ | |
438 | dummy_condition = lttng_condition_buffer_usage_low_create(); | |
439 | if (!dummy_condition) { | |
440 | fail("Setup error on dummy_condition creation"); | |
441 | goto end; | |
442 | ||
443 | } | |
444 | condition_status = lttng_condition_buffer_usage_set_threshold_ratio( | |
445 | dummy_condition, 0.5); | |
446 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
447 | fail("Setup error on condition creation"); | |
448 | goto end; | |
449 | } | |
450 | ||
451 | condition_status = lttng_condition_buffer_usage_set_session_name( | |
452 | dummy_condition, session_name); | |
453 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
454 | fail("Setup error on dummy_condition creation"); | |
455 | goto end; | |
456 | } | |
457 | condition_status = lttng_condition_buffer_usage_set_channel_name( | |
458 | dummy_condition, channel_name); | |
459 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
460 | fail("Setup error on dummy_condition creation"); | |
461 | goto end; | |
462 | } | |
463 | condition_status = lttng_condition_buffer_usage_set_domain_type( | |
464 | dummy_condition, domain_type); | |
465 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
466 | fail("Setup error on dummy_condition creation"); | |
467 | goto end; | |
468 | } | |
469 | ||
470 | /* Register a low condition with a ratio */ | |
471 | low_condition = lttng_condition_buffer_usage_low_create(); | |
472 | if (!low_condition) { | |
473 | fail("Setup error on low_condition creation"); | |
474 | goto end; | |
475 | } | |
476 | condition_status = lttng_condition_buffer_usage_set_threshold_ratio( | |
477 | low_condition, low_ratio); | |
478 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
479 | fail("Setup error on low_condition creation"); | |
480 | goto end; | |
481 | } | |
482 | ||
483 | condition_status = lttng_condition_buffer_usage_set_session_name( | |
484 | low_condition, session_name); | |
485 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
486 | fail("Setup error on low_condition creation"); | |
487 | goto end; | |
488 | } | |
489 | condition_status = lttng_condition_buffer_usage_set_channel_name( | |
490 | low_condition, channel_name); | |
491 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
492 | fail("Setup error on low_condition creation"); | |
493 | goto end; | |
494 | } | |
495 | condition_status = lttng_condition_buffer_usage_set_domain_type( | |
496 | low_condition, domain_type); | |
497 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
498 | fail("Setup error on low_condition creation"); | |
499 | goto end; | |
500 | ||
501 | } | |
502 | ||
503 | /* Register a high condition with a ratio */ | |
504 | high_condition = lttng_condition_buffer_usage_high_create(); | |
505 | if (!high_condition) { | |
506 | fail("Setup error on high_condition creation"); | |
507 | goto end; | |
508 | } | |
509 | ||
510 | condition_status = lttng_condition_buffer_usage_set_threshold_ratio( | |
511 | high_condition, high_ratio); | |
512 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
513 | fail("Setup error on high_condition creation"); | |
514 | goto end; | |
515 | } | |
516 | ||
517 | condition_status = lttng_condition_buffer_usage_set_session_name( | |
518 | high_condition, session_name); | |
519 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
520 | fail("Setup error on high_condition creation"); | |
521 | goto end; | |
522 | } | |
523 | condition_status = lttng_condition_buffer_usage_set_channel_name( | |
524 | high_condition, channel_name); | |
525 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
526 | fail("Setup error on high_condition creation"); | |
527 | goto end; | |
528 | } | |
529 | condition_status = lttng_condition_buffer_usage_set_domain_type( | |
530 | high_condition, domain_type); | |
531 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
532 | fail("Setup error on high_condition creation"); | |
533 | goto end; | |
534 | } | |
535 | ||
536 | /* Register the triggers for low and high condition */ | |
537 | trigger = lttng_trigger_create(low_condition, action); | |
538 | if (!trigger) { | |
539 | fail("Setup error on low trigger creation"); | |
540 | goto end; | |
541 | } | |
542 | ||
543 | ret = lttng_register_trigger(trigger); | |
544 | if (ret) { | |
545 | fail("Setup error on low trigger registration"); | |
546 | goto end; | |
547 | } | |
548 | ||
549 | lttng_trigger_destroy(trigger); | |
550 | trigger = NULL; | |
551 | ||
552 | trigger = lttng_trigger_create(high_condition, action); | |
553 | if (!trigger) { | |
554 | fail("Setup error on high trigger creation"); | |
555 | goto end; | |
556 | } | |
557 | ||
558 | ret = lttng_register_trigger(trigger); | |
559 | if (ret) { | |
560 | fail("Setup error on high trigger registration"); | |
561 | goto end; | |
562 | } | |
563 | ||
564 | /* Begin testing */ | |
565 | notification_channel = lttng_notification_channel_create(lttng_session_daemon_notification_endpoint); | |
566 | ok(notification_channel, "Notification channel object creation"); | |
567 | if (!notification_channel) { | |
568 | goto end; | |
569 | } | |
570 | ||
571 | /* Basic error path check */ | |
572 | nc_status = lttng_notification_channel_subscribe(NULL, NULL); | |
573 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID, "Notification channel subscription is invalid: NULL, NULL"); | |
574 | ||
575 | nc_status = lttng_notification_channel_subscribe(notification_channel, NULL); | |
576 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID, "Notification channel subscription is invalid: NON-NULL, NULL"); | |
577 | ||
578 | nc_status = lttng_notification_channel_subscribe(NULL, low_condition); | |
579 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID, "Notification channel subscription is invalid: NULL, NON-NULL"); | |
580 | ||
581 | nc_status = lttng_notification_channel_subscribe(notification_channel, dummy_invalid_condition); | |
582 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID, "Subscribing to an invalid condition"); | |
583 | ||
584 | nc_status = lttng_notification_channel_unsubscribe(notification_channel, dummy_invalid_condition); | |
a5fcfec4 | 585 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID, "Unsubscribing from an invalid condition"); |
434f8068 JR |
586 | |
587 | nc_status = lttng_notification_channel_unsubscribe(notification_channel, dummy_condition); | |
a5fcfec4 | 588 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION, "Unsubscribing from a valid unknown condition"); |
434f8068 JR |
589 | |
590 | /* Subscribe a valid low condition */ | |
591 | nc_status = lttng_notification_channel_subscribe(notification_channel, low_condition); | |
592 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK, "Subscribe to condition"); | |
593 | ||
594 | /* Subscribe a valid high condition */ | |
595 | nc_status = lttng_notification_channel_subscribe(notification_channel, high_condition); | |
596 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK, "Subscribe to condition"); | |
597 | ||
598 | nc_status = lttng_notification_channel_subscribe(notification_channel, low_condition); | |
599 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED, "Subscribe to a condition for which subscription was already done"); | |
600 | ||
601 | nc_status = lttng_notification_channel_subscribe(notification_channel, high_condition); | |
602 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED, "Subscribe to a condition for which subscription was already done"); | |
603 | ||
604 | /* Wait for notification to happen */ | |
434f8068 | 605 | stop_consumer(argv); |
ff2b03c8 | 606 | lttng_start_tracing(session_name); |
434f8068 JR |
607 | |
608 | /* Wait for high notification */ | |
609 | nc_status = lttng_notification_channel_get_next_notification(notification_channel, ¬ification); | |
610 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK | |
611 | && notification | |
612 | && lttng_condition_get_type(lttng_notification_get_condition(notification)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, | |
613 | "High notification received after intermediary communication"); | |
614 | lttng_notification_destroy(notification); | |
615 | notification = NULL; | |
616 | ||
854382b8 | 617 | suspend_application(); |
ff2b03c8 | 618 | lttng_stop_tracing_no_wait(session_name); |
434f8068 | 619 | resume_consumer(argv); |
ff2b03c8 | 620 | wait_data_pending(session_name); |
434f8068 JR |
621 | |
622 | /* | |
623 | * Test that communication still work even if there is notification | |
624 | * waiting for consumption. | |
625 | */ | |
626 | ||
627 | nc_status = lttng_notification_channel_unsubscribe(notification_channel, low_condition); | |
628 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK, "Unsubscribe with pending notification"); | |
629 | ||
630 | nc_status = lttng_notification_channel_subscribe(notification_channel, low_condition); | |
631 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK, "subscribe with pending notification"); | |
632 | ||
633 | nc_status = lttng_notification_channel_get_next_notification(notification_channel, ¬ification); | |
634 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK | |
635 | && notification | |
636 | && lttng_condition_get_type(lttng_notification_get_condition(notification)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW, | |
637 | "Low notification received after intermediary communication"); | |
638 | lttng_notification_destroy(notification); | |
639 | notification = NULL; | |
640 | ||
641 | /* Stop consumer to force a high notification */ | |
ff2b03c8 | 642 | stop_consumer(argv); |
854382b8 | 643 | resume_application(); |
434f8068 | 644 | lttng_start_tracing(session_name); |
434f8068 JR |
645 | |
646 | nc_status = lttng_notification_channel_get_next_notification(notification_channel, ¬ification); | |
647 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK && notification && | |
648 | lttng_condition_get_type(lttng_notification_get_condition(notification)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, | |
649 | "High notification received after intermediary communication"); | |
650 | lttng_notification_destroy(notification); | |
651 | notification = NULL; | |
652 | ||
854382b8 | 653 | suspend_application(); |
ff2b03c8 | 654 | lttng_stop_tracing_no_wait(session_name); |
434f8068 | 655 | resume_consumer(argv); |
ff2b03c8 | 656 | wait_data_pending(session_name); |
434f8068 JR |
657 | |
658 | nc_status = lttng_notification_channel_get_next_notification(notification_channel, ¬ification); | |
659 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK && notification && | |
660 | lttng_condition_get_type(lttng_notification_get_condition(notification)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW, | |
661 | "Low notification received after re-subscription"); | |
662 | lttng_notification_destroy(notification); | |
663 | notification = NULL; | |
664 | ||
ff2b03c8 | 665 | stop_consumer(argv); |
854382b8 | 666 | resume_application(); |
434f8068 JR |
667 | /* Stop consumer to force a high notification */ |
668 | lttng_start_tracing(session_name); | |
434f8068 JR |
669 | |
670 | nc_status = lttng_notification_channel_get_next_notification(notification_channel, ¬ification); | |
671 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK && notification && | |
672 | lttng_condition_get_type(lttng_notification_get_condition(notification)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, | |
673 | "High notification"); | |
674 | lttng_notification_destroy(notification); | |
675 | notification = NULL; | |
676 | ||
677 | /* Resume consumer to allow event consumption */ | |
ff2b03c8 JG |
678 | suspend_application(); |
679 | lttng_stop_tracing_no_wait(session_name); | |
434f8068 | 680 | resume_consumer(argv); |
ff2b03c8 | 681 | wait_data_pending(session_name); |
434f8068 JR |
682 | |
683 | nc_status = lttng_notification_channel_unsubscribe(notification_channel, low_condition); | |
684 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK, "Unsubscribe low condition with pending notification"); | |
685 | nc_status = lttng_notification_channel_unsubscribe(notification_channel, high_condition); | |
686 | ok(nc_status == LTTNG_NOTIFICATION_CHANNEL_STATUS_OK, "Unsubscribe high condition with pending notification"); | |
687 | ||
688 | end: | |
689 | lttng_notification_channel_destroy(notification_channel); | |
690 | lttng_trigger_destroy(trigger); | |
691 | lttng_action_destroy(action); | |
692 | lttng_condition_destroy(low_condition); | |
eff748d0 | 693 | lttng_condition_destroy(high_condition); |
434f8068 JR |
694 | lttng_condition_destroy(dummy_invalid_condition); |
695 | lttng_condition_destroy(dummy_condition); | |
696 | } | |
697 | ||
698 | int main(int argc, const char *argv[]) | |
699 | { | |
700 | const char *session_name = NULL; | |
701 | const char *channel_name = NULL; | |
702 | const char *domain_type_string = NULL; | |
703 | enum lttng_domain_type domain_type = LTTNG_DOMAIN_NONE; | |
704 | ||
705 | plan_tests(NUM_TESTS); | |
706 | ||
854382b8 JR |
707 | /* Argument 6 and upward are named pipe location for consumerd control */ |
708 | named_pipe_args_start = 6; | |
434f8068 | 709 | |
854382b8 | 710 | if (argc < 7) { |
434f8068 JR |
711 | fail("Missing parameter for tests to run %d", argc); |
712 | goto error; | |
713 | } | |
714 | ||
715 | nb_args = argc; | |
716 | ||
854382b8 JR |
717 | domain_type_string = argv[1]; |
718 | session_name = argv[2]; | |
719 | channel_name = argv[3]; | |
720 | app_pid = (pid_t) atoi(argv[4]); | |
721 | app_state_file = argv[5]; | |
434f8068 JR |
722 | |
723 | if (!strcmp("LTTNG_DOMAIN_UST", domain_type_string)) { | |
724 | domain_type = LTTNG_DOMAIN_UST; | |
725 | } | |
726 | if (!strcmp("LTTNG_DOMAIN_KERNEL", domain_type_string)) { | |
727 | domain_type = LTTNG_DOMAIN_KERNEL; | |
728 | } | |
729 | if (domain_type == LTTNG_DOMAIN_NONE) { | |
730 | fail("Unknown domain type"); | |
731 | goto error; | |
732 | } | |
733 | ||
734 | diag("Test trigger for domain %s with buffer_usage_low condition", domain_type_string); | |
735 | test_triggers_buffer_usage_condition(session_name, channel_name, domain_type, LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW); | |
736 | diag("Test trigger for domain %s with buffer_usage_high condition", domain_type_string); | |
737 | test_triggers_buffer_usage_condition(session_name, channel_name, domain_type, LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH); | |
738 | ||
739 | diag("Test notification channel api for domain %s", domain_type_string); | |
740 | test_notification_channel(session_name, channel_name, domain_type, argv); | |
741 | error: | |
742 | return exit_status(); | |
743 | } | |
744 |