4 * Tests suite for LTTng notification API
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
8 * SPDX-License-Identifier: MIT
20 #include <sys/types.h>
26 #include <common/compat/errno.h>
27 #include <lttng/action/action.h>
28 #include <lttng/action/notify.h>
29 #include <lttng/condition/buffer-usage.h>
30 #include <lttng/condition/condition.h>
31 #include <lttng/condition/evaluation.h>
32 #include <lttng/domain.h>
33 #include <lttng/endpoint.h>
34 #include <lttng/lttng-error.h>
35 #include <lttng/notification/channel.h>
36 #include <lttng/notification/notification.h>
37 #include <lttng/trigger/trigger.h>
38 #include <lttng/lttng.h>
45 int named_pipe_args_start
= 0;
47 const char *app_state_file
= NULL
;
50 void wait_on_file(const char *path
, bool file_exist
)
59 ret
= stat(path
, &buf
);
60 if (ret
== -1 && errno
== ENOENT
) {
63 * The file does not exist. wait a bit and
64 * continue looping until it does.
66 (void) poll(NULL
, 0, 10);
71 * File does not exist and the exit condition we want.
72 * Break from the loop and return.
81 * stat() returned 0, so the file exists. break now only if
82 * that's the exit condition we want.
91 int write_pipe(const char *path
, uint8_t data
)
96 fd
= open(path
, O_WRONLY
| O_NONBLOCK
);
98 perror("Could not open consumer control named pipe");
102 ret
= write(fd
, &data
, sizeof(data
));
104 perror("Named pipe write failed");
106 perror("Named pipe close failed");
114 perror("Name pipe closing failed");
123 int stop_consumer(const char **argv
)
127 for (i
= named_pipe_args_start
; i
< nb_args
; i
++) {
128 ret
= write_pipe(argv
[i
], 49);
134 int resume_consumer(const char **argv
)
138 for (i
= named_pipe_args_start
; i
< nb_args
; i
++) {
139 ret
= write_pipe(argv
[i
], 0);
145 int suspend_application(void)
150 if (!stat(app_state_file
, &buf
)) {
151 fail("App is already in a suspended state.");
157 * Send SIGUSR1 to application instructing it to bypass tracepoint.
159 ret
= kill(app_pid
, SIGUSR1
);
161 fail("SIGUSR1 failed. errno %d", errno
);
166 wait_on_file(app_state_file
, true);
174 int resume_application()
179 ret
= stat(app_state_file
, &buf
);
180 if (ret
== -1 && errno
== ENOENT
) {
181 fail("State file does not exist");
189 ret
= kill(app_pid
, SIGUSR1
);
191 fail("SIGUSR1 failed. errno %d", errno
);
196 wait_on_file(app_state_file
, false);
205 void test_triggers_buffer_usage_condition(const char *session_name
,
206 const char *channel_name
,
207 enum lttng_domain_type domain_type
,
208 enum lttng_condition_type condition_type
)
210 unsigned int test_vector_size
= 5, i
;
211 enum lttng_condition_status condition_status
;
212 struct lttng_action
*action
;
215 action
= lttng_action_notify_create();
217 fail("Setup error on action creation");
221 /* Test lttng_register_trigger with null value */
222 ok(lttng_register_trigger(NULL
) == -LTTNG_ERR_INVALID
, "Registering a NULL trigger fails as expected");
224 /* Test: register a trigger */
226 for (i
= 0; i
< pow(2,test_vector_size
); i
++) {
228 char *test_tuple_string
= NULL
;
229 unsigned int mask_position
= 0;
230 bool session_name_set
= false;
231 bool channel_name_set
= false;
232 bool threshold_ratio_set
= false;
233 bool threshold_byte_set
= false;
234 bool domain_type_set
= false;
236 struct lttng_trigger
*trigger
= NULL
;
237 struct lttng_condition
*condition
= NULL
;
239 /* Create base condition */
240 switch (condition_type
) {
241 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
242 condition
= lttng_condition_buffer_usage_low_create();
244 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
245 condition
= lttng_condition_buffer_usage_high_create();
258 /* Prepare the condition for trigger registration test */
260 /* Set session name */
261 if ((1 << mask_position
) & i
) {
262 condition_status
= lttng_condition_buffer_usage_set_session_name(
263 condition
, session_name
);
264 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
268 session_name_set
= true;
272 /* Set channel name */
273 if ((1 << mask_position
) & i
) {
274 condition_status
= lttng_condition_buffer_usage_set_channel_name(
275 condition
, channel_name
);
276 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
280 channel_name_set
= true;
284 /* Set threshold ratio */
285 if ((1 << mask_position
) & i
) {
286 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
288 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
292 threshold_ratio_set
= true;
296 /* Set threshold byte */
297 if ((1 << mask_position
) & i
) {
298 condition_status
= lttng_condition_buffer_usage_set_threshold(
300 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
304 threshold_byte_set
= true;
308 /* Set domain type */
309 if ((1 << mask_position
) & i
) {
310 condition_status
= lttng_condition_buffer_usage_set_domain_type(
311 condition
, LTTNG_DOMAIN_UST
);
312 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
316 domain_type_set
= true;
320 if (mask_position
!= test_vector_size
-1) {
321 assert("Logic error for test vector generation");
324 loop_ret
= asprintf(&test_tuple_string
, "session name %s, channel name %s, threshold ratio %s, threshold byte %s, domain type %s",
325 session_name_set
? "set" : "unset",
326 channel_name_set
? "set" : "unset",
327 threshold_ratio_set
? "set" : "unset",
328 threshold_byte_set
? "set" : "unset",
329 domain_type_set
? "set" : "unset");
330 if (!test_tuple_string
|| loop_ret
< 0) {
336 trigger
= lttng_trigger_create(condition
, action
);
342 loop_ret
= lttng_register_trigger(trigger
);
346 fail("Setup error occurred for tuple: %s", test_tuple_string
);
350 /* This combination happens three times */
351 if (session_name_set
&& channel_name_set
352 && (threshold_ratio_set
|| threshold_byte_set
)
353 && domain_type_set
) {
354 ok(loop_ret
== 0, "Trigger is registered: %s", test_tuple_string
);
357 * Test that a trigger cannot be registered
360 loop_ret
= lttng_register_trigger(trigger
);
361 ok(loop_ret
== -LTTNG_ERR_TRIGGER_EXISTS
, "Re-register trigger fails as expected: %s", test_tuple_string
);
363 /* Test that a trigger can be unregistered */
364 loop_ret
= lttng_unregister_trigger(trigger
);
365 ok(loop_ret
== 0, "Unregister trigger: %s", test_tuple_string
);
368 * Test that unregistration of a non-previously
369 * registered trigger fail.
371 loop_ret
= lttng_unregister_trigger(trigger
);
372 ok(loop_ret
== -LTTNG_ERR_TRIGGER_NOT_FOUND
, "Unregister of a non-registered trigger fails as expected: %s", test_tuple_string
);
374 ok(loop_ret
== -LTTNG_ERR_INVALID_TRIGGER
, "Trigger is invalid as expected and cannot be registered: %s", test_tuple_string
);
378 free(test_tuple_string
);
379 lttng_trigger_destroy(trigger
);
380 lttng_condition_destroy(condition
);
384 lttng_action_destroy(action
);
388 void wait_data_pending(const char *session_name
)
393 ret
= lttng_data_pending(session_name
);
399 void test_notification_channel(const char *session_name
, const char *channel_name
, const enum lttng_domain_type domain_type
, const char **argv
)
402 enum lttng_condition_status condition_status
;
403 enum lttng_notification_channel_status nc_status
;
405 struct lttng_action
*action
= NULL
;
406 struct lttng_notification
*notification
= NULL
;
407 struct lttng_notification_channel
*notification_channel
= NULL
;
408 struct lttng_trigger
*trigger
= NULL
;
410 struct lttng_condition
*low_condition
= NULL
;
411 struct lttng_condition
*high_condition
= NULL
;
412 struct lttng_condition
*dummy_invalid_condition
= NULL
;
413 struct lttng_condition
*dummy_condition
= NULL
;
415 double low_ratio
= 0.0;
416 double high_ratio
= 0.90;
419 action
= lttng_action_notify_create();
421 fail("Setup error on action creation");
425 /* Create a dummy, empty condition for later test */
426 dummy_invalid_condition
= lttng_condition_buffer_usage_low_create();
427 if (!dummy_invalid_condition
) {
428 fail("Setup error on condition creation");
432 /* Create a valid dummy condition with a ratio of 0.5 */
433 dummy_condition
= lttng_condition_buffer_usage_low_create();
434 if (!dummy_condition
) {
435 fail("Setup error on dummy_condition creation");
439 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
440 dummy_condition
, 0.5);
441 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
442 fail("Setup error on condition creation");
446 condition_status
= lttng_condition_buffer_usage_set_session_name(
447 dummy_condition
, session_name
);
448 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
449 fail("Setup error on dummy_condition creation");
452 condition_status
= lttng_condition_buffer_usage_set_channel_name(
453 dummy_condition
, channel_name
);
454 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
455 fail("Setup error on dummy_condition creation");
458 condition_status
= lttng_condition_buffer_usage_set_domain_type(
459 dummy_condition
, domain_type
);
460 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
461 fail("Setup error on dummy_condition creation");
465 /* Register a low condition with a ratio */
466 low_condition
= lttng_condition_buffer_usage_low_create();
467 if (!low_condition
) {
468 fail("Setup error on low_condition creation");
471 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
472 low_condition
, low_ratio
);
473 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
474 fail("Setup error on low_condition creation");
478 condition_status
= lttng_condition_buffer_usage_set_session_name(
479 low_condition
, session_name
);
480 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
481 fail("Setup error on low_condition creation");
484 condition_status
= lttng_condition_buffer_usage_set_channel_name(
485 low_condition
, channel_name
);
486 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
487 fail("Setup error on low_condition creation");
490 condition_status
= lttng_condition_buffer_usage_set_domain_type(
491 low_condition
, domain_type
);
492 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
493 fail("Setup error on low_condition creation");
498 /* Register a high condition with a ratio */
499 high_condition
= lttng_condition_buffer_usage_high_create();
500 if (!high_condition
) {
501 fail("Setup error on high_condition creation");
505 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
506 high_condition
, high_ratio
);
507 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
508 fail("Setup error on high_condition creation");
512 condition_status
= lttng_condition_buffer_usage_set_session_name(
513 high_condition
, session_name
);
514 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
515 fail("Setup error on high_condition creation");
518 condition_status
= lttng_condition_buffer_usage_set_channel_name(
519 high_condition
, channel_name
);
520 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
521 fail("Setup error on high_condition creation");
524 condition_status
= lttng_condition_buffer_usage_set_domain_type(
525 high_condition
, domain_type
);
526 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
527 fail("Setup error on high_condition creation");
531 /* Register the triggers for low and high condition */
532 trigger
= lttng_trigger_create(low_condition
, action
);
534 fail("Setup error on low trigger creation");
538 ret
= lttng_register_trigger(trigger
);
540 fail("Setup error on low trigger registration");
544 lttng_trigger_destroy(trigger
);
547 trigger
= lttng_trigger_create(high_condition
, action
);
549 fail("Setup error on high trigger creation");
553 ret
= lttng_register_trigger(trigger
);
555 fail("Setup error on high trigger registration");
560 notification_channel
= lttng_notification_channel_create(lttng_session_daemon_notification_endpoint
);
561 ok(notification_channel
, "Notification channel object creation");
562 if (!notification_channel
) {
566 /* Basic error path check */
567 nc_status
= lttng_notification_channel_subscribe(NULL
, NULL
);
568 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Notification channel subscription is invalid: NULL, NULL");
570 nc_status
= lttng_notification_channel_subscribe(notification_channel
, NULL
);
571 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Notification channel subscription is invalid: NON-NULL, NULL");
573 nc_status
= lttng_notification_channel_subscribe(NULL
, low_condition
);
574 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Notification channel subscription is invalid: NULL, NON-NULL");
576 nc_status
= lttng_notification_channel_subscribe(notification_channel
, dummy_invalid_condition
);
577 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Subscribing to an invalid condition");
579 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, dummy_invalid_condition
);
580 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Unsubscribing from an invalid condition");
582 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, dummy_condition
);
583 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
, "Unsubscribing from a valid unknown condition");
585 /* Subscribe a valid low condition */
586 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
587 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Subscribe to condition");
589 /* Subscribe a valid high condition */
590 nc_status
= lttng_notification_channel_subscribe(notification_channel
, high_condition
);
591 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Subscribe to condition");
593 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
594 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
, "Subscribe to a condition for which subscription was already done");
596 nc_status
= lttng_notification_channel_subscribe(notification_channel
, high_condition
);
597 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
, "Subscribe to a condition for which subscription was already done");
599 /* Wait for notification to happen */
601 lttng_start_tracing(session_name
);
603 /* Wait for high notification */
605 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
606 } while (nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED
);
607 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
609 && lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
610 "High notification received after intermediary communication");
611 lttng_notification_destroy(notification
);
614 suspend_application();
615 lttng_stop_tracing_no_wait(session_name
);
616 resume_consumer(argv
);
617 wait_data_pending(session_name
);
620 * Test that communication still work even if there is notification
621 * waiting for consumption.
624 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, low_condition
);
625 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe with pending notification");
627 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
628 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "subscribe with pending notification");
631 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
632 } while (nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED
);
633 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
635 && lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
,
636 "Low notification received after intermediary communication");
637 lttng_notification_destroy(notification
);
640 /* Stop consumer to force a high notification */
642 resume_application();
643 lttng_start_tracing(session_name
);
646 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
647 } while (nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED
);
648 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
649 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
650 "High notification received after intermediary communication");
651 lttng_notification_destroy(notification
);
654 suspend_application();
655 lttng_stop_tracing_no_wait(session_name
);
656 resume_consumer(argv
);
657 wait_data_pending(session_name
);
660 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
661 } while (nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED
);
662 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
663 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
,
664 "Low notification received after re-subscription");
665 lttng_notification_destroy(notification
);
669 resume_application();
670 /* Stop consumer to force a high notification */
671 lttng_start_tracing(session_name
);
674 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
675 } while (nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED
);
676 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
677 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
678 "High notification");
679 lttng_notification_destroy(notification
);
682 /* Resume consumer to allow event consumption */
683 suspend_application();
684 lttng_stop_tracing_no_wait(session_name
);
685 resume_consumer(argv
);
686 wait_data_pending(session_name
);
688 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, low_condition
);
689 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe low condition with pending notification");
690 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, high_condition
);
691 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe high condition with pending notification");
694 lttng_notification_channel_destroy(notification_channel
);
695 lttng_trigger_destroy(trigger
);
696 lttng_action_destroy(action
);
697 lttng_condition_destroy(low_condition
);
698 lttng_condition_destroy(high_condition
);
699 lttng_condition_destroy(dummy_invalid_condition
);
700 lttng_condition_destroy(dummy_condition
);
703 int main(int argc
, const char *argv
[])
705 const char *session_name
= NULL
;
706 const char *channel_name
= NULL
;
707 const char *domain_type_string
= NULL
;
708 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
710 plan_tests(NUM_TESTS
);
712 /* Argument 6 and upward are named pipe location for consumerd control */
713 named_pipe_args_start
= 6;
716 fail("Missing parameter for tests to run %d", argc
);
722 domain_type_string
= argv
[1];
723 session_name
= argv
[2];
724 channel_name
= argv
[3];
725 app_pid
= (pid_t
) atoi(argv
[4]);
726 app_state_file
= argv
[5];
728 if (!strcmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
729 domain_type
= LTTNG_DOMAIN_UST
;
731 if (!strcmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
732 domain_type
= LTTNG_DOMAIN_KERNEL
;
734 if (domain_type
== LTTNG_DOMAIN_NONE
) {
735 fail("Unknown domain type");
739 diag("Test trigger for domain %s with buffer_usage_low condition", domain_type_string
);
740 test_triggers_buffer_usage_condition(session_name
, channel_name
, domain_type
, LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
);
741 diag("Test trigger for domain %s with buffer_usage_high condition", domain_type_string
);
742 test_triggers_buffer_usage_condition(session_name
, channel_name
, domain_type
, LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
744 diag("Test notification channel api for domain %s", domain_type_string
);
745 test_notification_channel(session_name
, channel_name
, domain_type
, argv
);
747 return exit_status();