4 * Base client application for testing of LTTng notification API
6 * Copyright 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
8 * SPDX-License-Identifier: MIT
18 #include <lttng/action/action.h>
19 #include <lttng/action/list.h>
20 #include <lttng/action/notify.h>
21 #include <lttng/condition/buffer-usage.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/condition/evaluation.h>
24 #include <lttng/domain.h>
25 #include <lttng/endpoint.h>
26 #include <lttng/notification/channel.h>
27 #include <lttng/notification/notification.h>
28 #include <lttng/trigger/trigger.h>
29 #include <lttng/lttng-error.h>
31 static unsigned int nr_notifications
= 0;
32 static unsigned int nr_expected_notifications
= 0;
33 static const char *session_name
= NULL
;
34 static const char *channel_name
= NULL
;
35 static double threshold_ratio
= 0.0;
36 static uint64_t threshold_bytes
= 0;
37 static bool is_threshold_ratio
= false;
38 static bool use_action_list
= false;
39 static enum lttng_condition_type buffer_usage_type
= LTTNG_CONDITION_TYPE_UNKNOWN
;
40 static enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
43 const struct lttng_condition
*condition
,
44 const struct lttng_evaluation
*condition_evaluation
);
47 int parse_arguments(char **argv
)
50 const char *domain_type_string
= NULL
;
51 const char *buffer_usage_type_string
= NULL
;
52 const char *buffer_usage_threshold_type
= NULL
;
53 const char *buffer_usage_threshold_value
= NULL
;
54 const char *nr_expected_notifications_string
= NULL
;
55 const char *use_action_list_value
= NULL
;
57 session_name
= argv
[1];
58 channel_name
= argv
[2];
59 domain_type_string
= argv
[3];
60 buffer_usage_type_string
= argv
[4];
61 buffer_usage_threshold_type
= argv
[5];
62 buffer_usage_threshold_value
= argv
[6];
63 nr_expected_notifications_string
= argv
[7];
64 use_action_list_value
= argv
[8];
68 if (!strcasecmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
69 domain_type
= LTTNG_DOMAIN_UST
;
71 if (!strcasecmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
72 domain_type
= LTTNG_DOMAIN_KERNEL
;
74 if (domain_type
== LTTNG_DOMAIN_NONE
) {
75 printf("error: Unknown domain type\n");
79 /* Buffer usage condition type */
80 if (!strcasecmp("low", buffer_usage_type_string
)) {
81 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
;
83 if (!strcasecmp("high", buffer_usage_type_string
)) {
84 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
86 if (buffer_usage_type
== LTTNG_CONDITION_TYPE_UNKNOWN
) {
87 printf("error: Unknown condition type\n");
91 /* Ratio or bytes ? */
92 if (!strcasecmp("bytes", buffer_usage_threshold_type
)) {
93 is_threshold_ratio
= false;
94 sscanf_ret
= sscanf(buffer_usage_threshold_value
, "%" SCNu64
,
96 if (sscanf_ret
!= 1) {
97 printf("error: Invalid buffer usage threshold value bytes (integer), sscanf returned %d\n",
103 if (!strcasecmp("ratio", buffer_usage_threshold_type
)) {
104 is_threshold_ratio
= true;
105 sscanf_ret
= sscanf(buffer_usage_threshold_value
, "%lf",
107 if (sscanf_ret
!= 1) {
108 printf("error: Invalid buffer usage threshold value ratio (float), sscanf returned %d\n",
114 /* Number of notification to expect */
115 sscanf_ret
= sscanf(nr_expected_notifications_string
, "%d",
116 &nr_expected_notifications
);
117 if (sscanf_ret
!= 1) {
118 printf("error: Invalid nr_expected_notifications, sscanf returned %d\n",
123 /* Put notify action in a group. */
124 if (!strcasecmp("1", use_action_list_value
)) {
125 use_action_list
= true;
133 int main(int argc
, char **argv
)
136 enum lttng_condition_status condition_status
;
137 enum lttng_action_status action_status
;
138 enum lttng_notification_channel_status nc_status
;
139 struct lttng_notification_channel
*notification_channel
= NULL
;
140 struct lttng_condition
*condition
= NULL
;
141 struct lttng_action
*action
= NULL
;
142 struct lttng_trigger
*trigger
= NULL
;
143 enum lttng_error_code ret_code
;
146 * Disable buffering on stdout.
147 * Safety measure to prevent hang on the validation side since
148 * stdout is used for outside synchronization.
150 setbuf(stdout
, NULL
);
153 printf("error: Missing arguments for tests\n");
158 ret
= parse_arguments(argv
);
160 printf("error: Could not parse arguments\n");
165 notification_channel
= lttng_notification_channel_create(
166 lttng_session_daemon_notification_endpoint
);
167 if (!notification_channel
) {
168 printf("error: Could not create notification channel\n");
173 switch (buffer_usage_type
) {
174 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
175 condition
= lttng_condition_buffer_usage_low_create();
177 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
178 condition
= lttng_condition_buffer_usage_high_create();
181 printf("error: Invalid buffer_usage_type\n");
187 printf("error: Could not create condition object\n");
192 if (is_threshold_ratio
) {
193 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
194 condition
, threshold_ratio
);
196 condition_status
= lttng_condition_buffer_usage_set_threshold(
197 condition
, threshold_bytes
);
200 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
201 printf("error: Could not set threshold\n");
206 condition_status
= lttng_condition_buffer_usage_set_session_name(
207 condition
, session_name
);
208 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
209 printf("error: Could not set session name\n");
213 condition_status
= lttng_condition_buffer_usage_set_channel_name(
214 condition
, channel_name
);
215 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
216 printf("error: Could not set channel name\n");
220 condition_status
= lttng_condition_buffer_usage_set_domain_type(
221 condition
, domain_type
);
222 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
223 printf("error: Could not set domain type\n");
228 if (use_action_list
) {
229 struct lttng_action
*notify
, *group
;
231 group
= lttng_action_list_create();
233 printf("error: Could not create action list\n");
238 notify
= lttng_action_notify_create();
240 lttng_action_destroy(group
);
241 printf("error: Could not create action notify\n");
246 action_status
= lttng_action_list_add_action(group
, notify
);
247 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
248 printf("error: Could not add action notify to action list\n");
249 lttng_action_destroy(group
);
250 lttng_action_destroy(notify
);
257 action
= lttng_action_notify_create();
259 printf("error: Could not create action notify\n");
265 trigger
= lttng_trigger_create(condition
, action
);
267 printf("error: Could not create trigger\n");
272 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
275 * An equivalent trigger might already be registered if an other app
276 * registered an equivalent trigger.
278 if (ret_code
!= LTTNG_OK
&& ret_code
!= LTTNG_ERR_TRIGGER_EXISTS
) {
279 printf("error: %s\n", lttng_strerror(-ret_code
));
284 nc_status
= lttng_notification_channel_subscribe(notification_channel
, condition
);
285 if (nc_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
286 printf("error: Could not subscribe\n");
291 /* Tell outside process that the client is ready */
292 printf("sync: ready\n");
295 struct lttng_notification
*notification
;
296 enum lttng_notification_channel_status status
;
297 const struct lttng_evaluation
*notification_evaluation
;
298 const struct lttng_condition
*notification_condition
;
300 if (nr_notifications
== nr_expected_notifications
) {
304 /* Receive the next notification. */
305 status
= lttng_notification_channel_get_next_notification(
306 notification_channel
,
310 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
312 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
314 printf("error: No drop should be observed during this test app\n");
316 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
318 * The notification channel has been closed by the
319 * session daemon. This is typically caused by a session
320 * daemon shutting down (cleanly or because of a crash).
322 printf("error: Notification channel was closed\n");
326 /* Unhandled conditions / errors. */
327 printf("error: Unknown notification channel status (%d) \n", status
);
332 notification_condition
= lttng_notification_get_condition(notification
);
333 notification_evaluation
= lttng_notification_get_evaluation(notification
);
335 ret
= handle_condition(notification_condition
, notification_evaluation
);
338 lttng_notification_destroy(notification
);
345 lttng_unregister_trigger(trigger
);
347 if (lttng_notification_channel_unsubscribe(notification_channel
, condition
)) {
348 printf("error: channel unsubscribe error\n");
350 lttng_trigger_destroy(trigger
);
351 lttng_condition_destroy(condition
);
352 lttng_action_destroy(action
);
353 lttng_notification_channel_destroy(notification_channel
);
354 printf("exit: %d\n", ret
);
358 int handle_condition(
359 const struct lttng_condition
*condition
,
360 const struct lttng_evaluation
*evaluation
)
363 const char *string_low
= "low";
364 const char *string_high
= "high";
365 const char *string_condition_type
= NULL
;
366 const char *condition_session_name
= NULL
;
367 const char *condition_channel_name
= NULL
;
368 enum lttng_condition_type condition_type
;
369 enum lttng_domain_type condition_domain_type
;
370 double buffer_usage_ratio
;
371 uint64_t buffer_usage_bytes
;
373 condition_type
= lttng_condition_get_type(condition
);
375 if (condition_type
!= buffer_usage_type
) {
377 printf("error: condition type and buffer usage type are not the same\n");
381 /* Fetch info to test */
382 ret
= lttng_condition_buffer_usage_get_session_name(condition
,
383 &condition_session_name
);
385 printf("error: session name could not be fetched\n");
389 ret
= lttng_condition_buffer_usage_get_channel_name(condition
,
390 &condition_channel_name
);
392 printf("error: channel name could not be fetched\n");
396 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
397 &condition_domain_type
);
399 printf("error: domain type could not be fetched\n");
404 if (strcmp(condition_session_name
, session_name
) != 0) {
405 printf("error: session name differs\n");
410 if (strcmp(condition_channel_name
, channel_name
) != 0) {
411 printf("error: channel name differs\n");
416 if (condition_domain_type
!= domain_type
) {
417 printf("error: domain type differs\n");
422 if (is_threshold_ratio
) {
423 lttng_evaluation_buffer_usage_get_usage_ratio(
424 evaluation
, &buffer_usage_ratio
);
425 switch (condition_type
) {
426 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
427 if (buffer_usage_ratio
> threshold_ratio
) {
428 printf("error: buffer usage ratio is bigger than set threshold ratio\n");
433 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
434 if (buffer_usage_ratio
< threshold_ratio
) {
435 printf("error: buffer usage ratio is lower than set threshold ratio\n");
441 printf("error: Unknown condition type\n");
446 lttng_evaluation_buffer_usage_get_usage(
447 evaluation
, &buffer_usage_bytes
);
448 switch (condition_type
) {
449 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
450 if (buffer_usage_bytes
> threshold_bytes
) {
451 printf("error: buffer usage ratio is bigger than set threshold bytes\n");
456 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
457 if (buffer_usage_bytes
< threshold_bytes
) {
458 printf("error: buffer usage ratio is lower than set threshold bytes\n");
464 printf("error: Unknown condition type\n");
470 switch (condition_type
) {
471 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
472 string_condition_type
= string_low
;
474 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
475 string_condition_type
= string_high
;
478 printf("error: Unknown condition type\n");
483 printf("notification: %s %d\n", string_condition_type
, nr_notifications
);