4 * Tests suite for LTTng notification API
6 * Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
35 #include <sys/types.h>
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>
53 #include <lttng/lttng.h>
60 int named_pipe_args_start
= 0;
62 const char *app_state_file
= NULL
;
65 void wait_on_file(const char *path
, bool file_exist
)
74 ret
= stat(path
, &buf
);
75 if (ret
== -1 && errno
== ENOENT
) {
77 (void) poll(NULL
, 0, 10); /* 10 ms delay */
80 break; /* File does not exist */
90 int write_pipe(const char *path
, uint8_t data
)
95 fd
= open(path
, O_WRONLY
| O_NONBLOCK
);
97 perror("Could not open consumer control named pipe");
101 ret
= write(fd
, &data
, sizeof(data
));
103 perror("Named pipe write failed");
105 perror("Named pipe close failed");
113 perror("Name pipe closing failed");
121 int stop_consumer(const char **argv
)
124 for (int i
= named_pipe_args_start
; i
< nb_args
; i
++) {
125 ret
= write_pipe(argv
[i
], 49);
130 int resume_consumer(const char **argv
)
133 for (int i
= named_pipe_args_start
; i
< nb_args
; i
++) {
134 ret
= write_pipe(argv
[i
], 0);
139 int suspend_application()
144 if (!stat(app_state_file
, &buf
)) {
145 fail("App is already in a suspended state.");
151 * Send SIGUSR1 to application instructing it to bypass tracepoint.
153 ret
= kill(app_pid
, SIGUSR1
);
155 fail("SIGUSR1 failed. errno %d", errno
);
160 wait_on_file(app_state_file
, true);
167 int resume_application()
172 ret
= stat(app_state_file
, &buf
);
173 if (ret
== -1 && errno
== ENOENT
) {
174 fail("State file does not exist");
182 ret
= kill(app_pid
, SIGUSR1
);
184 fail("SIGUSR1 failed. errno %d", errno
);
189 wait_on_file(app_state_file
, false);
197 void test_triggers_buffer_usage_condition(const char *session_name
,
198 const char *channel_name
,
199 enum lttng_domain_type domain_type
,
200 enum lttng_condition_type condition_type
)
202 enum lttng_condition_status condition_status
;
203 struct lttng_action
*action
;
206 action
= lttng_action_notify_create();
208 fail("Setup error on action creation");
212 /* Test lttng_register_trigger with null value */
213 ok(lttng_register_trigger(NULL
) == -LTTNG_ERR_INVALID
, "Registering a NULL trigger fails as expected");
215 /* Test: register a trigger */
216 unsigned int test_vector_size
= 5;
217 for (unsigned int i
= 0; i
< pow(2,test_vector_size
); i
++) {
219 char *test_tuple_string
= NULL
;
220 unsigned int mask_position
= 0;
221 bool session_name_set
= false;
222 bool channel_name_set
= false;
223 bool threshold_ratio_set
= false;
224 bool threshold_byte_set
= false;
225 bool domain_type_set
= false;
227 struct lttng_trigger
*trigger
= NULL
;
228 struct lttng_condition
*condition
= NULL
;
230 /* Create base condition */
231 switch (condition_type
) {
232 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
233 condition
= lttng_condition_buffer_usage_low_create();
235 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
236 condition
= lttng_condition_buffer_usage_high_create();
249 /* Prepare the condition for trigger registration test */
251 /* Set session name */
252 if ((1 << mask_position
) & i
) {
253 condition_status
= lttng_condition_buffer_usage_set_session_name(
254 condition
, session_name
);
255 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
259 session_name_set
= true;
263 /* Set channel name */
264 if ((1 << mask_position
) & i
) {
265 condition_status
= lttng_condition_buffer_usage_set_channel_name(
266 condition
, channel_name
);
267 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
271 channel_name_set
= true;
275 /* Set threshold ratio */
276 if ((1 << mask_position
) & i
) {
277 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
279 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
283 threshold_ratio_set
= true;
287 /* Set threshold byte */
288 if ((1 << mask_position
) & i
) {
289 condition_status
= lttng_condition_buffer_usage_set_threshold(
291 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
295 threshold_byte_set
= true;
299 /* Set domain type */
300 if ((1 << mask_position
) & i
) {
301 condition_status
= lttng_condition_buffer_usage_set_domain_type(
302 condition
, LTTNG_DOMAIN_UST
);
303 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
307 domain_type_set
= true;
311 if (mask_position
!= test_vector_size
-1) {
312 assert("Logic error for test vector generation");
315 loop_ret
= asprintf(&test_tuple_string
, "session name %s, channel name %s, threshold ratio %s, threshold byte %s, domain type %s",
316 session_name_set
? "set" : "unset",
317 channel_name_set
? "set" : "unset",
318 threshold_ratio_set
? "set" : "unset",
319 threshold_byte_set
? "set" : "unset",
320 domain_type_set
? "set" : "unset");
321 if (!test_tuple_string
|| loop_ret
< 0) {
327 trigger
= lttng_trigger_create(condition
, action
);
333 loop_ret
= lttng_register_trigger(trigger
);
337 fail("Setup error occurred for tuple: %s", test_tuple_string
);
341 /* This combination happens three times */
342 if (session_name_set
&& channel_name_set
343 && (threshold_ratio_set
|| threshold_byte_set
)
344 && domain_type_set
) {
345 ok(loop_ret
== 0, "Trigger is registered: %s", test_tuple_string
);
348 * Test that a trigger cannot be registered
351 loop_ret
= lttng_register_trigger(trigger
);
352 ok(loop_ret
== -LTTNG_ERR_TRIGGER_EXISTS
, "Re-register trigger fails as expected: %s", test_tuple_string
);
354 /* Test that a trigger can be unregistered */
355 loop_ret
= lttng_unregister_trigger(trigger
);
356 ok(loop_ret
== 0, "Unregister trigger: %s", test_tuple_string
);
359 * Test that unregistration of a non-previously
360 * registered trigger fail.
362 loop_ret
= lttng_unregister_trigger(trigger
);
363 ok(loop_ret
== -LTTNG_ERR_TRIGGER_NOT_FOUND
, "Unregister of a non-registered trigger fails as expected: %s", test_tuple_string
);
365 ok(loop_ret
== -LTTNG_ERR_INVALID_TRIGGER
, "Trigger is invalid as expected and cannot be registered: %s", test_tuple_string
);
369 free(test_tuple_string
);
370 lttng_trigger_destroy(trigger
);
371 lttng_condition_destroy(condition
);
375 lttng_action_destroy(action
);
379 void wait_data_pending(const char *session_name
)
384 ret
= lttng_data_pending(session_name
);
389 void test_notification_channel(const char *session_name
, const char *channel_name
, const enum lttng_domain_type domain_type
, const char **argv
)
392 enum lttng_condition_status condition_status
;
393 enum lttng_notification_channel_status nc_status
;
395 struct lttng_action
*action
= NULL
;
396 struct lttng_notification
*notification
= NULL
;
397 struct lttng_notification_channel
*notification_channel
= NULL
;
398 struct lttng_trigger
*trigger
= NULL
;
400 struct lttng_condition
*low_condition
= NULL
;
401 struct lttng_condition
*high_condition
= NULL
;
402 struct lttng_condition
*dummy_invalid_condition
= NULL
;
403 struct lttng_condition
*dummy_condition
= NULL
;
405 double low_ratio
= 0.0;
406 double high_ratio
= 0.99;
409 action
= lttng_action_notify_create();
411 fail("Setup error on action creation");
415 /* Create a dummy, empty condition for later test */
416 dummy_invalid_condition
= lttng_condition_buffer_usage_low_create();
417 if (!dummy_invalid_condition
) {
418 fail("Setup error on condition creation");
422 /* Create a valid dummy condition with a ratio of 0.5 */
423 dummy_condition
= lttng_condition_buffer_usage_low_create();
424 if (!dummy_condition
) {
425 fail("Setup error on dummy_condition creation");
429 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
430 dummy_condition
, 0.5);
431 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
432 fail("Setup error on condition creation");
436 condition_status
= lttng_condition_buffer_usage_set_session_name(
437 dummy_condition
, session_name
);
438 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
439 fail("Setup error on dummy_condition creation");
442 condition_status
= lttng_condition_buffer_usage_set_channel_name(
443 dummy_condition
, channel_name
);
444 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
445 fail("Setup error on dummy_condition creation");
448 condition_status
= lttng_condition_buffer_usage_set_domain_type(
449 dummy_condition
, domain_type
);
450 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
451 fail("Setup error on dummy_condition creation");
455 /* Register a low condition with a ratio */
456 low_condition
= lttng_condition_buffer_usage_low_create();
457 if (!low_condition
) {
458 fail("Setup error on low_condition creation");
461 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
462 low_condition
, low_ratio
);
463 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
464 fail("Setup error on low_condition creation");
468 condition_status
= lttng_condition_buffer_usage_set_session_name(
469 low_condition
, session_name
);
470 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
471 fail("Setup error on low_condition creation");
474 condition_status
= lttng_condition_buffer_usage_set_channel_name(
475 low_condition
, channel_name
);
476 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
477 fail("Setup error on low_condition creation");
480 condition_status
= lttng_condition_buffer_usage_set_domain_type(
481 low_condition
, domain_type
);
482 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
483 fail("Setup error on low_condition creation");
488 /* Register a high condition with a ratio */
489 high_condition
= lttng_condition_buffer_usage_high_create();
490 if (!high_condition
) {
491 fail("Setup error on high_condition creation");
495 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
496 high_condition
, high_ratio
);
497 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
498 fail("Setup error on high_condition creation");
502 condition_status
= lttng_condition_buffer_usage_set_session_name(
503 high_condition
, session_name
);
504 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
505 fail("Setup error on high_condition creation");
508 condition_status
= lttng_condition_buffer_usage_set_channel_name(
509 high_condition
, channel_name
);
510 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
511 fail("Setup error on high_condition creation");
514 condition_status
= lttng_condition_buffer_usage_set_domain_type(
515 high_condition
, domain_type
);
516 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
517 fail("Setup error on high_condition creation");
521 /* Register the triggers for low and high condition */
522 trigger
= lttng_trigger_create(low_condition
, action
);
524 fail("Setup error on low trigger creation");
528 ret
= lttng_register_trigger(trigger
);
530 fail("Setup error on low trigger registration");
534 lttng_trigger_destroy(trigger
);
537 trigger
= lttng_trigger_create(high_condition
, action
);
539 fail("Setup error on high trigger creation");
543 ret
= lttng_register_trigger(trigger
);
545 fail("Setup error on high trigger registration");
550 notification_channel
= lttng_notification_channel_create(lttng_session_daemon_notification_endpoint
);
551 ok(notification_channel
, "Notification channel object creation");
552 if (!notification_channel
) {
556 /* Basic error path check */
557 nc_status
= lttng_notification_channel_subscribe(NULL
, NULL
);
558 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Notification channel subscription is invalid: NULL, NULL");
560 nc_status
= lttng_notification_channel_subscribe(notification_channel
, NULL
);
561 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Notification channel subscription is invalid: NON-NULL, NULL");
563 nc_status
= lttng_notification_channel_subscribe(NULL
, low_condition
);
564 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Notification channel subscription is invalid: NULL, NON-NULL");
566 nc_status
= lttng_notification_channel_subscribe(notification_channel
, dummy_invalid_condition
);
567 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Subscribing to an invalid condition");
569 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, dummy_invalid_condition
);
570 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID
, "Unsubscribing from an invalid condition");
572 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, dummy_condition
);
573 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
, "Unsubscribing from a valid unknown condition");
575 /* Subscribe a valid low condition */
576 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
577 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Subscribe to condition");
579 /* Subscribe a valid high condition */
580 nc_status
= lttng_notification_channel_subscribe(notification_channel
, high_condition
);
581 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Subscribe to condition");
583 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
584 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
, "Subscribe to a condition for which subscription was already done");
586 nc_status
= lttng_notification_channel_subscribe(notification_channel
, high_condition
);
587 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
, "Subscribe to a condition for which subscription was already done");
589 /* Wait for notification to happen */
591 lttng_start_tracing(session_name
);
593 /* Wait for high notification */
594 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
595 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
597 && lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
598 "High notification received after intermediary communication");
599 lttng_notification_destroy(notification
);
602 suspend_application();
603 lttng_stop_tracing_no_wait(session_name
);
604 resume_consumer(argv
);
605 wait_data_pending(session_name
);
608 * Test that communication still work even if there is notification
609 * waiting for consumption.
612 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, low_condition
);
613 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe with pending notification");
615 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
616 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "subscribe with pending notification");
618 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
619 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
621 && lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
,
622 "Low notification received after intermediary communication");
623 lttng_notification_destroy(notification
);
626 /* Stop consumer to force a high notification */
628 resume_application();
629 lttng_start_tracing(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_HIGH
,
634 "High notification received after intermediary communication");
635 lttng_notification_destroy(notification
);
638 suspend_application();
639 lttng_stop_tracing_no_wait(session_name
);
640 resume_consumer(argv
);
641 wait_data_pending(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_LOW
,
646 "Low notification received after re-subscription");
647 lttng_notification_destroy(notification
);
651 resume_application();
652 /* Stop consumer to force a high notification */
653 lttng_start_tracing(session_name
);
655 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
656 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
657 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
658 "High notification");
659 lttng_notification_destroy(notification
);
662 /* Resume consumer to allow event consumption */
663 suspend_application();
664 lttng_stop_tracing_no_wait(session_name
);
665 resume_consumer(argv
);
666 wait_data_pending(session_name
);
668 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, low_condition
);
669 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe low condition with pending notification");
670 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, high_condition
);
671 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe high condition with pending notification");
674 lttng_notification_channel_destroy(notification_channel
);
675 lttng_trigger_destroy(trigger
);
676 lttng_action_destroy(action
);
677 lttng_condition_destroy(low_condition
);
678 lttng_condition_destroy(high_condition
);
679 lttng_condition_destroy(dummy_invalid_condition
);
680 lttng_condition_destroy(dummy_condition
);
683 int main(int argc
, const char *argv
[])
685 const char *session_name
= NULL
;
686 const char *channel_name
= NULL
;
687 const char *domain_type_string
= NULL
;
688 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
690 plan_tests(NUM_TESTS
);
692 /* Argument 6 and upward are named pipe location for consumerd control */
693 named_pipe_args_start
= 6;
696 fail("Missing parameter for tests to run %d", argc
);
702 domain_type_string
= argv
[1];
703 session_name
= argv
[2];
704 channel_name
= argv
[3];
705 app_pid
= (pid_t
) atoi(argv
[4]);
706 app_state_file
= argv
[5];
708 if (!strcmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
709 domain_type
= LTTNG_DOMAIN_UST
;
711 if (!strcmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
712 domain_type
= LTTNG_DOMAIN_KERNEL
;
714 if (domain_type
== LTTNG_DOMAIN_NONE
) {
715 fail("Unknown domain type");
719 diag("Test trigger for domain %s with buffer_usage_low condition", domain_type_string
);
720 test_triggers_buffer_usage_condition(session_name
, channel_name
, domain_type
, LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
);
721 diag("Test trigger for domain %s with buffer_usage_high condition", domain_type_string
);
722 test_triggers_buffer_usage_condition(session_name
, channel_name
, domain_type
, LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
724 diag("Test notification channel api for domain %s", domain_type_string
);
725 test_notification_channel(session_name
, channel_name
, domain_type
, argv
);
727 return exit_status();