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>
59 int named_pipe_args_start
= 0;
61 const char *app_state_file
= NULL
;
64 void wait_on_file(const char *path
, bool file_exist
)
73 ret
= stat(path
, &buf
);
74 if (ret
== -1 && errno
== ENOENT
) {
76 (void) poll(NULL
, 0, 10); /* 10 ms delay */
79 break; /* File does not exist */
89 int write_pipe(const char *path
, uint8_t data
)
94 fd
= open(path
, O_WRONLY
| O_NONBLOCK
);
96 perror("Could not open consumer control named pipe");
100 ret
= write(fd
, &data
, sizeof(data
));
102 perror("Named pipe write failed");
104 perror("Named pipe close failed");
112 perror("Name pipe closing failed");
120 int stop_consumer(const char **argv
)
123 for (int i
= named_pipe_args_start
; i
< nb_args
; i
++) {
124 ret
= write_pipe(argv
[i
], 49);
129 int resume_consumer(const char **argv
)
132 for (int i
= named_pipe_args_start
; i
< nb_args
; i
++) {
133 ret
= write_pipe(argv
[i
], 0);
138 int suspend_application()
143 if (!stat(app_state_file
, &buf
)) {
144 fail("App is already in a suspended state.");
150 * Send SIGUSR1 to application instructing it to bypass tracepoint.
152 ret
= kill(app_pid
, SIGUSR1
);
154 fail("SIGUSR1 failed. errno %d", errno
);
159 wait_on_file(app_state_file
, true);
166 int resume_application()
171 ret
= stat(app_state_file
, &buf
);
172 if (ret
== -1 && errno
== ENOENT
) {
173 fail("State file does not exist");
181 ret
= kill(app_pid
, SIGUSR1
);
183 fail("SIGUSR1 failed. errno %d", errno
);
188 wait_on_file(app_state_file
, false);
196 void test_triggers_buffer_usage_condition(const char *session_name
,
197 const char *channel_name
,
198 enum lttng_domain_type domain_type
,
199 enum lttng_condition_type condition_type
)
201 enum lttng_condition_status condition_status
;
202 struct lttng_action
*action
;
205 action
= lttng_action_notify_create();
207 fail("Setup error on action creation");
211 /* Test lttng_register_trigger with null value */
212 ok(lttng_register_trigger(NULL
) == -LTTNG_ERR_INVALID
, "Registering a NULL trigger fails as expected");
214 /* Test: register a trigger */
215 unsigned int test_vector_size
= 5;
216 for (unsigned int i
= 0; i
< pow(2,test_vector_size
); i
++) {
218 char *test_tuple_string
= NULL
;
219 unsigned int mask_position
= 0;
220 bool session_name_set
= false;
221 bool channel_name_set
= false;
222 bool threshold_ratio_set
= false;
223 bool threshold_byte_set
= false;
224 bool domain_type_set
= false;
226 struct lttng_trigger
*trigger
= NULL
;
227 struct lttng_condition
*condition
= NULL
;
229 /* Create base condition */
230 switch (condition_type
) {
231 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
232 condition
= lttng_condition_buffer_usage_low_create();
234 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
235 condition
= lttng_condition_buffer_usage_high_create();
248 /* Prepare the condition for trigger registration test */
250 /* Set session name */
251 if ((1 << mask_position
) & i
) {
252 condition_status
= lttng_condition_buffer_usage_set_session_name(
253 condition
, session_name
);
254 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
258 session_name_set
= true;
262 /* Set channel name */
263 if ((1 << mask_position
) & i
) {
264 condition_status
= lttng_condition_buffer_usage_set_channel_name(
265 condition
, channel_name
);
266 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
270 channel_name_set
= true;
274 /* Set threshold ratio */
275 if ((1 << mask_position
) & i
) {
276 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
278 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
282 threshold_ratio_set
= true;
286 /* Set threshold byte */
287 if ((1 << mask_position
) & i
) {
288 condition_status
= lttng_condition_buffer_usage_set_threshold(
290 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
294 threshold_byte_set
= true;
298 /* Set domain type */
299 if ((1 << mask_position
) & i
) {
300 condition_status
= lttng_condition_buffer_usage_set_domain_type(
301 condition
, LTTNG_DOMAIN_UST
);
302 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
306 domain_type_set
= true;
310 if (mask_position
!= test_vector_size
-1) {
311 assert("Logic error for test vector generation");
314 loop_ret
= asprintf(&test_tuple_string
, "session name %s, channel name %s, threshold ratio %s, threshold byte %s, domain type %s",
315 session_name_set
? "set" : "unset",
316 channel_name_set
? "set" : "unset",
317 threshold_ratio_set
? "set" : "unset",
318 threshold_byte_set
? "set" : "unset",
319 domain_type_set
? "set" : "unset");
320 if (!test_tuple_string
|| loop_ret
< 0) {
326 trigger
= lttng_trigger_create(condition
, action
);
332 loop_ret
= lttng_register_trigger(trigger
);
336 fail("Setup error occurred for tuple: %s", test_tuple_string
);
340 /* This combination happens three times */
341 if (session_name_set
&& channel_name_set
342 && (threshold_ratio_set
|| threshold_byte_set
)
343 && domain_type_set
) {
344 ok(loop_ret
== 0, "Trigger is registered: %s", test_tuple_string
);
347 * Test that a trigger cannot be registered
350 loop_ret
= lttng_register_trigger(trigger
);
351 ok(loop_ret
== -LTTNG_ERR_TRIGGER_EXISTS
, "Re-register trigger fails as expected: %s", test_tuple_string
);
353 /* Test that a trigger can be unregistered */
354 loop_ret
= lttng_unregister_trigger(trigger
);
355 ok(loop_ret
== 0, "Unregister trigger: %s", test_tuple_string
);
358 * Test that unregistration of a non-previously
359 * registered trigger fail.
361 loop_ret
= lttng_unregister_trigger(trigger
);
362 ok(loop_ret
== -LTTNG_ERR_TRIGGER_NOT_FOUND
, "Unregister of a non-registerd trigger fails as expected: %s", test_tuple_string
);
364 ok(loop_ret
== -LTTNG_ERR_INVALID_TRIGGER
, "Trigger is invalid as expected and cannot be registered: %s", test_tuple_string
);
368 free(test_tuple_string
);
369 lttng_trigger_destroy(trigger
);
370 lttng_condition_destroy(condition
);
374 lttng_action_destroy(action
);
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 to 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 to an 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 */
578 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 resume_consumer(argv
);
592 lttng_stop_tracing(session_name
);
595 * Test that communication still work even if there is notification
596 * waiting for consumption.
599 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, low_condition
);
600 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe with pending notification");
602 nc_status
= lttng_notification_channel_subscribe(notification_channel
, low_condition
);
603 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "subscribe with pending notification");
605 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
606 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
608 && lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
,
609 "Low notification received after intermediary communication");
610 lttng_notification_destroy(notification
);
613 /* Stop consumer to force a high notification */
614 resume_application();
615 lttng_start_tracing(session_name
);
618 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
619 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
620 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
621 "High notification received after intermediary communication");
622 lttng_notification_destroy(notification
);
625 suspend_application();
626 /* Resume consumer to allow event consumption */
627 resume_consumer(argv
);
628 lttng_stop_tracing(session_name
);
630 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
631 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
632 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
,
633 "Low notification received after re-subscription");
634 lttng_notification_destroy(notification
);
637 resume_application();
638 /* Stop consumer to force a high notification */
639 lttng_start_tracing(session_name
);
642 nc_status
= lttng_notification_channel_get_next_notification(notification_channel
, ¬ification
);
643 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
&& notification
&&
644 lttng_condition_get_type(lttng_notification_get_condition(notification
)) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
,
645 "High notification");
646 lttng_notification_destroy(notification
);
649 /* Resume consumer to allow event consumption */
650 resume_consumer(argv
);
651 lttng_stop_tracing(session_name
);
653 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, low_condition
);
654 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe low condition with pending notification");
655 nc_status
= lttng_notification_channel_unsubscribe(notification_channel
, high_condition
);
656 ok(nc_status
== LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
, "Unsubscribe high condition with pending notification");
659 lttng_notification_channel_destroy(notification_channel
);
660 lttng_trigger_destroy(trigger
);
661 lttng_action_destroy(action
);
662 lttng_condition_destroy(low_condition
);
663 lttng_condition_destroy(high_condition
);
664 lttng_condition_destroy(dummy_invalid_condition
);
665 lttng_condition_destroy(dummy_condition
);
668 int main(int argc
, const char *argv
[])
670 const char *session_name
= NULL
;
671 const char *channel_name
= NULL
;
672 const char *domain_type_string
= NULL
;
673 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
675 plan_tests(NUM_TESTS
);
677 /* Argument 6 and upward are named pipe location for consumerd control */
678 named_pipe_args_start
= 6;
681 fail("Missing parameter for tests to run %d", argc
);
687 domain_type_string
= argv
[1];
688 session_name
= argv
[2];
689 channel_name
= argv
[3];
690 app_pid
= (pid_t
) atoi(argv
[4]);
691 app_state_file
= argv
[5];
693 if (!strcmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
694 domain_type
= LTTNG_DOMAIN_UST
;
696 if (!strcmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
697 domain_type
= LTTNG_DOMAIN_KERNEL
;
699 if (domain_type
== LTTNG_DOMAIN_NONE
) {
700 fail("Unknown domain type");
704 diag("Test trigger for domain %s with buffer_usage_low condition", domain_type_string
);
705 test_triggers_buffer_usage_condition(session_name
, channel_name
, domain_type
, LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
);
706 diag("Test trigger for domain %s with buffer_usage_high condition", domain_type_string
);
707 test_triggers_buffer_usage_condition(session_name
, channel_name
, domain_type
, LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
709 diag("Test notification channel api for domain %s", domain_type_string
);
710 test_notification_channel(session_name
, channel_name
, domain_type
, argv
);
712 return exit_status();