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
12 #include <lttng/action/action.h>
13 #include <lttng/action/list.h>
14 #include <lttng/action/notify.h>
15 #include <lttng/condition/buffer-usage.h>
16 #include <lttng/condition/condition.h>
17 #include <lttng/condition/evaluation.h>
18 #include <lttng/domain.h>
19 #include <lttng/endpoint.h>
20 #include <lttng/lttng-error.h>
21 #include <lttng/notification/channel.h>
22 #include <lttng/notification/notification.h>
23 #include <lttng/trigger/trigger.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
;
42 int handle_condition(const struct lttng_condition
*condition
,
43 const struct lttng_evaluation
*condition_evaluation
);
45 static int parse_arguments(char **argv
)
48 const char *domain_type_string
= NULL
;
49 const char *buffer_usage_type_string
= NULL
;
50 const char *buffer_usage_threshold_type
= NULL
;
51 const char *buffer_usage_threshold_value
= NULL
;
52 const char *nr_expected_notifications_string
= NULL
;
53 const char *use_action_list_value
= NULL
;
55 session_name
= argv
[1];
56 channel_name
= argv
[2];
57 domain_type_string
= argv
[3];
58 buffer_usage_type_string
= argv
[4];
59 buffer_usage_threshold_type
= argv
[5];
60 buffer_usage_threshold_value
= argv
[6];
61 nr_expected_notifications_string
= argv
[7];
62 use_action_list_value
= argv
[8];
66 if (!strcasecmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
67 domain_type
= LTTNG_DOMAIN_UST
;
69 if (!strcasecmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
70 domain_type
= LTTNG_DOMAIN_KERNEL
;
72 if (domain_type
== LTTNG_DOMAIN_NONE
) {
73 printf("error: Unknown domain type\n");
77 /* Buffer usage condition type */
78 if (!strcasecmp("low", buffer_usage_type_string
)) {
79 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
;
81 if (!strcasecmp("high", buffer_usage_type_string
)) {
82 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
84 if (buffer_usage_type
== LTTNG_CONDITION_TYPE_UNKNOWN
) {
85 printf("error: Unknown condition type\n");
89 /* Ratio or bytes ? */
90 if (!strcasecmp("bytes", buffer_usage_threshold_type
)) {
91 is_threshold_ratio
= false;
92 sscanf_ret
= sscanf(buffer_usage_threshold_value
, "%" SCNu64
, &threshold_bytes
);
93 if (sscanf_ret
!= 1) {
94 printf("error: Invalid buffer usage threshold value bytes (integer), sscanf returned %d\n",
100 if (!strcasecmp("ratio", buffer_usage_threshold_type
)) {
101 is_threshold_ratio
= true;
102 sscanf_ret
= sscanf(buffer_usage_threshold_value
, "%lf", &threshold_ratio
);
103 if (sscanf_ret
!= 1) {
104 printf("error: Invalid buffer usage threshold value ratio (float), sscanf returned %d\n",
110 /* Number of notification to expect */
111 sscanf_ret
= sscanf(nr_expected_notifications_string
, "%d", &nr_expected_notifications
);
112 if (sscanf_ret
!= 1) {
113 printf("error: Invalid nr_expected_notifications, sscanf returned %d\n",
118 /* Put notify action in a group. */
119 if (!strcasecmp("1", use_action_list_value
)) {
120 use_action_list
= true;
128 int main(int argc
, char **argv
)
131 enum lttng_condition_status condition_status
;
132 enum lttng_action_status action_status
;
133 enum lttng_notification_channel_status nc_status
;
134 struct lttng_notification_channel
*notification_channel
= NULL
;
135 struct lttng_condition
*condition
= NULL
;
136 struct lttng_action
*action
= NULL
;
137 struct lttng_trigger
*trigger
= NULL
;
138 enum lttng_error_code ret_code
;
141 * Disable buffering on stdout.
142 * Safety measure to prevent hang on the validation side since
143 * stdout is used for outside synchronization.
145 setbuf(stdout
, NULL
);
148 printf("error: Missing arguments for tests\n");
153 ret
= parse_arguments(argv
);
155 printf("error: Could not parse arguments\n");
160 notification_channel
=
161 lttng_notification_channel_create(lttng_session_daemon_notification_endpoint
);
162 if (!notification_channel
) {
163 printf("error: Could not create notification channel\n");
168 switch (buffer_usage_type
) {
169 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
170 condition
= lttng_condition_buffer_usage_low_create();
172 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
173 condition
= lttng_condition_buffer_usage_high_create();
176 printf("error: Invalid buffer_usage_type\n");
182 printf("error: Could not create condition object\n");
187 if (is_threshold_ratio
) {
188 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
189 condition
, threshold_ratio
);
192 lttng_condition_buffer_usage_set_threshold(condition
, threshold_bytes
);
195 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
196 printf("error: Could not set threshold\n");
201 condition_status
= lttng_condition_buffer_usage_set_session_name(condition
, session_name
);
202 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
203 printf("error: Could not set session name\n");
207 condition_status
= lttng_condition_buffer_usage_set_channel_name(condition
, channel_name
);
208 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
209 printf("error: Could not set channel name\n");
213 condition_status
= lttng_condition_buffer_usage_set_domain_type(condition
, domain_type
);
214 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
215 printf("error: Could not set domain type\n");
220 if (use_action_list
) {
221 struct lttng_action
*notify
, *group
;
223 group
= lttng_action_list_create();
225 printf("error: Could not create action list\n");
230 notify
= lttng_action_notify_create();
232 lttng_action_destroy(group
);
233 printf("error: Could not create action notify\n");
238 action_status
= lttng_action_list_add_action(group
, notify
);
239 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
240 printf("error: Could not add action notify to action list\n");
241 lttng_action_destroy(group
);
242 lttng_action_destroy(notify
);
249 action
= lttng_action_notify_create();
251 printf("error: Could not create action notify\n");
257 trigger
= lttng_trigger_create(condition
, action
);
259 printf("error: Could not create trigger\n");
264 ret_code
= lttng_register_trigger_with_automatic_name(trigger
);
267 * An equivalent trigger might already be registered if an other app
268 * registered an equivalent trigger.
270 if (ret_code
!= LTTNG_OK
&& ret_code
!= LTTNG_ERR_TRIGGER_EXISTS
) {
271 printf("error: %s\n", lttng_strerror(-ret_code
));
276 nc_status
= lttng_notification_channel_subscribe(notification_channel
, condition
);
277 if (nc_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
278 printf("error: Could not subscribe\n");
283 /* Tell outside process that the client is ready */
284 printf("sync: ready\n");
287 struct lttng_notification
*notification
;
288 enum lttng_notification_channel_status status
;
289 const struct lttng_evaluation
*notification_evaluation
;
290 const struct lttng_condition
*notification_condition
;
292 if (nr_notifications
== nr_expected_notifications
) {
296 /* Receive the next notification. */
297 status
= lttng_notification_channel_get_next_notification(notification_channel
,
301 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
303 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
305 printf("error: No drop should be observed during this test app\n");
307 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
309 * The notification channel has been closed by the
310 * session daemon. This is typically caused by a session
311 * daemon shutting down (cleanly or because of a crash).
313 printf("error: Notification channel was closed\n");
317 /* Unhandled conditions / errors. */
318 printf("error: Unknown notification channel status (%d) \n", status
);
323 notification_condition
= lttng_notification_get_condition(notification
);
324 notification_evaluation
= lttng_notification_get_evaluation(notification
);
326 ret
= handle_condition(notification_condition
, notification_evaluation
);
329 lttng_notification_destroy(notification
);
336 lttng_unregister_trigger(trigger
);
338 if (lttng_notification_channel_unsubscribe(notification_channel
, condition
)) {
339 printf("error: channel unsubscribe error\n");
341 lttng_trigger_destroy(trigger
);
342 lttng_condition_destroy(condition
);
343 lttng_action_destroy(action
);
344 lttng_notification_channel_destroy(notification_channel
);
345 printf("exit: %d\n", ret
);
349 int handle_condition(const struct lttng_condition
*condition
,
350 const struct lttng_evaluation
*evaluation
)
353 const char *string_low
= "low";
354 const char *string_high
= "high";
355 const char *string_condition_type
= NULL
;
356 const char *condition_session_name
= NULL
;
357 const char *condition_channel_name
= NULL
;
358 enum lttng_condition_type condition_type
;
359 enum lttng_domain_type condition_domain_type
;
360 double buffer_usage_ratio
;
361 uint64_t buffer_usage_bytes
;
363 condition_type
= lttng_condition_get_type(condition
);
365 if (condition_type
!= buffer_usage_type
) {
367 printf("error: condition type and buffer usage type are not the same\n");
371 /* Fetch info to test */
372 ret
= lttng_condition_buffer_usage_get_session_name(condition
, &condition_session_name
);
374 printf("error: session name could not be fetched\n");
378 ret
= lttng_condition_buffer_usage_get_channel_name(condition
, &condition_channel_name
);
380 printf("error: channel name could not be fetched\n");
384 ret
= lttng_condition_buffer_usage_get_domain_type(condition
, &condition_domain_type
);
386 printf("error: domain type could not be fetched\n");
391 if (strcmp(condition_session_name
, session_name
) != 0) {
392 printf("error: session name differs\n");
397 if (strcmp(condition_channel_name
, channel_name
) != 0) {
398 printf("error: channel name differs\n");
403 if (condition_domain_type
!= domain_type
) {
404 printf("error: domain type differs\n");
409 if (is_threshold_ratio
) {
410 lttng_evaluation_buffer_usage_get_usage_ratio(evaluation
, &buffer_usage_ratio
);
411 switch (condition_type
) {
412 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
413 if (buffer_usage_ratio
> threshold_ratio
) {
414 printf("error: buffer usage ratio is bigger than set threshold ratio\n");
419 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
420 if (buffer_usage_ratio
< threshold_ratio
) {
421 printf("error: buffer usage ratio is lower than set threshold ratio\n");
427 printf("error: Unknown condition type\n");
432 lttng_evaluation_buffer_usage_get_usage(evaluation
, &buffer_usage_bytes
);
433 switch (condition_type
) {
434 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
435 if (buffer_usage_bytes
> threshold_bytes
) {
436 printf("error: buffer usage ratio is bigger than set threshold bytes\n");
441 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
442 if (buffer_usage_bytes
< threshold_bytes
) {
443 printf("error: buffer usage ratio is lower than set threshold bytes\n");
449 printf("error: Unknown condition type\n");
455 switch (condition_type
) {
456 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
457 string_condition_type
= string_low
;
459 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
460 string_condition_type
= string_high
;
463 printf("error: Unknown condition type\n");
468 printf("notification: %s %d\n", string_condition_type
, nr_notifications
);