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>
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
) {
62 (void) poll(NULL
, 0, 10); /* 10 ms delay */
65 break; /* File does not exist */
75 int write_pipe(const char *path
, uint8_t data
)
80 fd
= open(path
, O_WRONLY
| O_NONBLOCK
);
82 perror("Could not open consumer control named pipe");
86 ret
= write(fd
, &data
, sizeof(data
));
88 perror("Named pipe write failed");
90 perror("Named pipe close failed");
98 perror("Name pipe closing failed");
106 int stop_consumer(const char **argv
)
110 for (i
= named_pipe_args_start
; i
< nb_args
; i
++) {
111 ret
= write_pipe(argv
[i
], 49);
116 int resume_consumer(const char **argv
)
120 for (i
= named_pipe_args_start
; i
< nb_args
; i
++) {
121 ret
= write_pipe(argv
[i
], 0);
126 int suspend_application()
131 if (!stat(app_state_file
, &buf
)) {
132 fail("App is already in a suspended state.");
138 * Send SIGUSR1 to application instructing it to bypass tracepoint.
140 ret
= kill(app_pid
, SIGUSR1
);
142 fail("SIGUSR1 failed. errno %d", errno
);
147 wait_on_file(app_state_file
, true);
154 int resume_application()
159 ret
= stat(app_state_file
, &buf
);
160 if (ret
== -1 && errno
== ENOENT
) {
161 fail("State file does not exist");
169 ret
= kill(app_pid
, SIGUSR1
);
171 fail("SIGUSR1 failed. errno %d", errno
);
176 wait_on_file(app_state_file
, false);
184 void test_triggers_buffer_usage_condition(const char *session_name
,
185 const char *channel_name
,
186 enum lttng_domain_type domain_type
,
187 enum lttng_condition_type condition_type
)
189 unsigned int test_vector_size
= 5, i
;
190 enum lttng_condition_status condition_status
;
191 struct lttng_action
*action
;
194 action
= lttng_action_notify_create();
196 fail("Setup error on action creation");
200 /* Test lttng_register_trigger with null value */
201 ok(lttng_register_trigger(NULL
) == -LTTNG_ERR_INVALID
, "Registering a NULL trigger fails as expected");
203 /* Test: register a trigger */
205 for (i
= 0; i
< pow(2,test_vector_size
); i
++) {
207 char *test_tuple_string
= NULL
;
208 unsigned int mask_position
= 0;
209 bool session_name_set
= false;
210 bool channel_name_set
= false;
211 bool threshold_ratio_set
= false;
212 bool threshold_byte_set
= false;
213 bool domain_type_set
= false;
215 struct lttng_trigger
*trigger
= NULL
;
216 struct lttng_condition
*condition
= NULL
;
218 /* Create base condition */
219 switch (condition_type
) {
220 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
221 condition
= lttng_condition_buffer_usage_low_create();
223 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
224 condition
= lttng_condition_buffer_usage_high_create();
237 /* Prepare the condition for trigger registration test */
239 /* Set session name */
240 if ((1 << mask_position
) & i
) {
241 condition_status
= lttng_condition_buffer_usage_set_session_name(
242 condition
, session_name
);
243 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
247 session_name_set
= true;
251 /* Set channel name */
252 if ((1 << mask_position
) & i
) {
253 condition_status
= lttng_condition_buffer_usage_set_channel_name(
254 condition
, channel_name
);
255 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
259 channel_name_set
= true;
263 /* Set threshold ratio */
264 if ((1 << mask_position
) & i
) {
265 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
267 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
271 threshold_ratio_set
= true;
275 /* Set threshold byte */
276 if ((1 << mask_position
) & i
) {
277 condition_status
= lttng_condition_buffer_usage_set_threshold(
279 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
283 threshold_byte_set
= true;
287 /* Set domain type */
288 if ((1 << mask_position
) & i
) {
289 condition_status
= lttng_condition_buffer_usage_set_domain_type(
290 condition
, LTTNG_DOMAIN_UST
);
291 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
295 domain_type_set
= true;
299 if (mask_position
!= test_vector_size
-1) {
300 assert("Logic error for test vector generation");
303 loop_ret
= asprintf(&test_tuple_string
, "session name %s, channel name %s, threshold ratio %s, threshold byte %s, domain type %s",
304 session_name_set
? "set" : "unset",
305 channel_name_set
? "set" : "unset",
306 threshold_ratio_set
? "set" : "unset",
307 threshold_byte_set
? "set" : "unset",
308 domain_type_set
? "set" : "unset");
309 if (!test_tuple_string
|| loop_ret
< 0) {
315 trigger
= lttng_trigger_create(condition
, action
);
321 loop_ret
= lttng_register_trigger(trigger
);
325 fail("Setup error occurred for tuple: %s", test_tuple_string
);
329 /* This combination happens three times */
330 if (session_name_set
&& channel_name_set
331 && (threshold_ratio_set
|| threshold_byte_set
)
332 && domain_type_set
) {
333 ok(loop_ret
== 0, "Trigger is registered: %s", test_tuple_string
);
336 * Test that a trigger cannot be registered
339 loop_ret
= lttng_register_trigger(trigger
);
340 ok(loop_ret
== -LTTNG_ERR_TRIGGER_EXISTS
, "Re-register trigger fails as expected: %s", test_tuple_string
);
342 /* Test that a trigger can be unregistered */
343 loop_ret
= lttng_unregister_trigger(trigger
);
344 ok(loop_ret
== 0, "Unregister trigger: %s", test_tuple_string
);
347 * Test that unregistration of a non-previously
348 * registered trigger fail.
350 loop_ret
= lttng_unregister_trigger(trigger
);
351 ok(loop_ret
== -LTTNG_ERR_TRIGGER_NOT_FOUND
, "Unregister of a non-registerd trigger fails as expected: %s", test_tuple_string
);
353 ok(loop_ret
== -LTTNG_ERR_INVALID_TRIGGER
, "Trigger is invalid as expected and cannot be registered: %s", test_tuple_string
);
357 free(test_tuple_string
);
358 lttng_trigger_destroy(trigger
);
359 lttng_condition_destroy(condition
);
363 lttng_action_destroy(action
);
367 void wait_data_pending(const char *session_name
)
372 ret
= lttng_data_pending(session_name
);
377 void test_notification_channel(const char *session_name
, const char *channel_name
, const enum lttng_domain_type domain_type
, const char **argv
)
380 enum lttng_condition_status condition_status
;
381 enum lttng_notification_channel_status nc_status
;
383 struct lttng_action
*action
= NULL
;
384 struct lttng_notification
*notification
= NULL
;
385 struct lttng_notification_channel
*notification_channel
= NULL
;
386 struct lttng_trigger
*trigger
= NULL
;
388 struct lttng_condition
*low_condition
= NULL
;
389 struct lttng_condition
*high_condition
= NULL
;
390 struct lttng_condition
*dummy_invalid_condition
= NULL
;
391 struct lttng_condition
*dummy_condition
= NULL
;
393 double low_ratio
= 0.0;
394 double high_ratio
= 0.99;
397 action
= lttng_action_notify_create();
399 fail("Setup error on action creation");
403 /* Create a dummy, empty condition for later test */
404 dummy_invalid_condition
= lttng_condition_buffer_usage_low_create();
405 if (!dummy_invalid_condition
) {
406 fail("Setup error on condition creation");
410 /* Create a valid dummy condition with a ratio of 0.5 */
411 dummy_condition
= lttng_condition_buffer_usage_low_create();
412 if (!dummy_condition
) {
413 fail("Setup error on dummy_condition creation");
417 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
418 dummy_condition
, 0.5);
419 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
420 fail("Setup error on condition creation");
424 condition_status
= lttng_condition_buffer_usage_set_session_name(
425 dummy_condition
, session_name
);
426 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
427 fail("Setup error on dummy_condition creation");
430 condition_status
= lttng_condition_buffer_usage_set_channel_name(
431 dummy_condition
, channel_name
);
432 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
433 fail("Setup error on dummy_condition creation");
436 condition_status
= lttng_condition_buffer_usage_set_domain_type(
437 dummy_condition
, domain_type
);
438 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
439 fail("Setup error on dummy_condition creation");
443 /* Register a low condition with a ratio */
444 low_condition
= lttng_condition_buffer_usage_low_create();
445 if (!low_condition
) {
446 fail("Setup error on low_condition creation");
449 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
450 low_condition
, low_ratio
);
451 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
452 fail("Setup error on low_condition creation");
456 condition_status
= lttng_condition_buffer_usage_set_session_name(
457 low_condition
, session_name
);
458 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
459 fail("Setup error on low_condition creation");
462 condition_status
= lttng_condition_buffer_usage_set_channel_name(
463 low_condition
, channel_name
);
464 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
465 fail("Setup error on low_condition creation");
468 condition_status
= lttng_condition_buffer_usage_set_domain_type(
469 low_condition
, domain_type
);
470 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
471 fail("Setup error on low_condition creation");
476 /* Register a high condition with a ratio */
477 high_condition
= lttng_condition_buffer_usage_high_create();
478 if (!high_condition
) {
479 fail("Setup error on high_condition creation");
483 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
484 high_condition
, high_ratio
);
485 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
486 fail("Setup error on high_condition creation");
490 condition_status
= lttng_condition_buffer_usage_set_session_name(
491 high_condition
, session_name
);
492 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
493 fail("Setup error on high_condition creation");
496 condition_status
= lttng_condition_buffer_usage_set_channel_name(
497 high_condition
, channel_name
);
498 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
499 fail("Setup error on high_condition creation");
502 condition_status
= lttng_condition_buffer_usage_set_domain_type(
503 high_condition
, domain_type
);
504 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
505 fail("Setup error on high_condition creation");
509 /* Register the triggers for low and high condition */
510 trigger
= lttng_trigger_create(low_condition
, action
);
512 fail("Setup error on low trigger creation");
516 ret
= lttng_register_trigger(trigger
);
518 fail("Setup error on low trigger registration");
522 lttng_trigger_destroy(trigger
);
525 trigger
= lttng_trigger_create(high_condition
, action
);
527 fail("Setup error on high trigger creation");
531 ret
= lttng_register_trigger(trigger
);
533 fail("Setup error on high trigger registration");
538 notification_channel
= lttng_notification_channel_create(lttng_session_daemon_notification_endpoint
);
539 ok(notification_channel
, "Notification channel object creation");
540 if (!notification_channel
) {
544 /* Basic error path check */
545 nc_status
= lttng_notification_channel_subscribe(NULL
, NULL
);
546 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Notification channel subscription is invalid: NULL, NULL");
548 nc_status
= lttng_notification_channel_subscribe(notification_channel
, NULL
);
549 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Notification channel subscription is invalid: NON-NULL, NULL");
551 nc_status
= lttng_notification_channel_subscribe(NULL
, low_condition
);
552 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Notification channel subscription is invalid: NULL, NON-NULL");
554 nc_status
= lttng_notification_channel_subscribe(notification_channel
, dummy_invalid_condition
);
555 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Subscribing to an invalid condition");
557 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, dummy_invalid_condition
);
558 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Unsubscribing from an invalid condition");
560 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, dummy_condition
);
561 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
, "Unsubscribing from a valid unknown condition");
563 /* Subscribe a valid low condition */
564 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
565 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Subscribe to condition");
567 /* Subscribe a valid high condition */
568 nc_status
= lttng_notification_channel_subscribe(notification_channel
, high_condition
);
569 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Subscribe to condition");
571 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
572 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
, "Subscribe to a condition for which subscription was already done");
574 nc_status
= lttng_notification_channel_subscribe(notification_channel
, high_condition
);
575 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
, "Subscribe to a condition for which subscription was already done");
577 /* Wait for notification to happen */
579 lttng_start_tracing(session_name
);
581 /* Wait for high notification */
582 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
583 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
585 && lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
586 "High notification received after intermediary communication");
587 lttng_notification_destroy(notification
);
590 suspend_application();
591 lttng_stop_tracing_no_wait(session_name
);
592 resume_consumer(argv
);
593 wait_data_pending(session_name
);
596 * Test that communication still work even if there is notification
597 * waiting for consumption.
600 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, low_condition
);
601 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe with pending notification");
603 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
604 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "subscribe with pending notification");
606 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
607 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
609 && lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
,
610 "Low notification received after intermediary communication");
611 lttng_notification_destroy(notification
);
614 /* Stop consumer to force a high notification */
616 resume_application();
617 lttng_start_tracing(session_name
);
619 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
620 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
621 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
622 "High notification received after intermediary communication");
623 lttng_notification_destroy(notification
);
626 suspend_application();
627 lttng_stop_tracing_no_wait(session_name
);
628 resume_consumer(argv
);
629 wait_data_pending(session_name
);
631 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
632 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
633 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
,
634 "Low notification received after re-subscription");
635 lttng_notification_destroy(notification
);
639 resume_application();
640 /* Stop consumer to force a high notification */
641 lttng_start_tracing(session_name
);
643 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
644 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
645 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
646 "High notification");
647 lttng_notification_destroy(notification
);
650 /* Resume consumer to allow event consumption */
651 suspend_application();
652 lttng_stop_tracing_no_wait(session_name
);
653 resume_consumer(argv
);
654 wait_data_pending(session_name
);
656 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, low_condition
);
657 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe low condition with pending notification");
658 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, high_condition
);
659 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe high condition with pending notification");
662 lttng_notification_channel_destroy(notification_channel
);
663 lttng_trigger_destroy(trigger
);
664 lttng_action_destroy(action
);
665 lttng_condition_destroy(low_condition
);
666 lttng_condition_destroy(high_condition
);
667 lttng_condition_destroy(dummy_invalid_condition
);
668 lttng_condition_destroy(dummy_condition
);
671 int main(int argc
, const char *argv
[])
673 const char *session_name
= NULL
;
674 const char *channel_name
= NULL
;
675 const char *domain_type_string
= NULL
;
676 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
678 plan_tests(NUM_TESTS
);
680 /* Argument 6 and upward are named pipe location for consumerd control */
681 named_pipe_args_start
= 6;
684 fail("Missing parameter for tests to run %d", argc
);
690 domain_type_string
= argv
[1];
691 session_name
= argv
[2];
692 channel_name
= argv
[3];
693 app_pid
= (pid_t
) atoi(argv
[4]);
694 app_state_file
= argv
[5];
696 if (!strcmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
697 domain_type
= LTTNG_DOMAIN_UST
;
699 if (!strcmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
700 domain_type
= LTTNG_DOMAIN_KERNEL
;
702 if (domain_type
== LTTNG_DOMAIN_NONE
) {
703 fail("Unknown domain type");
707 diag("Test trigger for domain %s with buffer_usage_low condition", domain_type_string
);
708 test_triggers_buffer_usage_condition(session_name
, channel_name
, domain_type
, LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
);
709 diag("Test trigger for domain %s with buffer_usage_high condition", domain_type_string
);
710 test_triggers_buffer_usage_condition(session_name
, channel_name
, domain_type
, LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
712 diag("Test notification channel api for domain %s", domain_type_string
);
713 test_notification_channel(session_name
, channel_name
, domain_type
, argv
);
715 return exit_status();