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
19 #include <lttng/action/action.h>
20 #include <lttng/action/group.h>
21 #include <lttng/action/notify.h>
22 #include <lttng/condition/buffer-usage.h>
23 #include <lttng/condition/condition.h>
24 #include <lttng/condition/evaluation.h>
25 #include <lttng/domain.h>
26 #include <lttng/endpoint.h>
27 #include <lttng/notification/channel.h>
28 #include <lttng/notification/notification.h>
29 #include <lttng/trigger/trigger.h>
30 #include <lttng/lttng-error.h>
32 static unsigned int nr_notifications
= 0;
33 static unsigned int nr_expected_notifications
= 0;
34 static const char *session_name
= NULL
;
35 static const char *channel_name
= NULL
;
36 static double threshold_ratio
= 0.0;
37 static uint64_t threshold_bytes
= 0;
38 static bool is_threshold_ratio
= false;
39 static bool use_action_list
= false;
40 static enum lttng_condition_type buffer_usage_type
= LTTNG_CONDITION_TYPE_UNKNOWN
;
41 static enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
44 const struct lttng_condition
*condition
,
45 const struct lttng_evaluation
*condition_evaluation
);
48 int parse_arguments(char **argv
)
51 const char *domain_type_string
= NULL
;
52 const char *buffer_usage_type_string
= NULL
;
53 const char *buffer_usage_threshold_type
= NULL
;
54 const char *buffer_usage_threshold_value
= NULL
;
55 const char *nr_expected_notifications_string
= NULL
;
56 const char *use_action_list_value
= NULL
;
58 session_name
= argv
[1];
59 channel_name
= argv
[2];
60 domain_type_string
= argv
[3];
61 buffer_usage_type_string
= argv
[4];
62 buffer_usage_threshold_type
= argv
[5];
63 buffer_usage_threshold_value
= argv
[6];
64 nr_expected_notifications_string
= argv
[7];
65 use_action_list_value
= argv
[8];
69 if (!strcasecmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
70 domain_type
= LTTNG_DOMAIN_UST
;
72 if (!strcasecmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
73 domain_type
= LTTNG_DOMAIN_KERNEL
;
75 if (domain_type
== LTTNG_DOMAIN_NONE
) {
76 printf("error: Unknown domain type\n");
80 /* Buffer usage condition type */
81 if (!strcasecmp("low", buffer_usage_type_string
)) {
82 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
;
84 if (!strcasecmp("high", buffer_usage_type_string
)) {
85 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
87 if (buffer_usage_type
== LTTNG_CONDITION_TYPE_UNKNOWN
) {
88 printf("error: Unknown condition type\n");
92 /* Ratio or bytes ? */
93 if (!strcasecmp("bytes", buffer_usage_threshold_type
)) {
94 is_threshold_ratio
= false;
95 sscanf(buffer_usage_threshold_value
, "%" SCNu64
, &threshold_bytes
);
98 if (!strcasecmp("ratio", buffer_usage_threshold_type
)) {
99 is_threshold_ratio
= true;
100 sscanf(buffer_usage_threshold_value
, "%lf", &threshold_ratio
);
103 /* Number of notification to expect */
104 sscanf_ret
= sscanf(nr_expected_notifications_string
, "%d",
105 &nr_expected_notifications
);
106 if (sscanf_ret
!= 1) {
107 printf("error: Invalid nr_expected_notifications, sscanf returned %d\n",
112 /* Put notify action in a group. */
113 if (!strcasecmp("1", use_action_list_value
)) {
114 use_action_list
= true;
122 int main(int argc
, char **argv
)
125 enum lttng_condition_status condition_status
;
126 enum lttng_action_status action_status
;
127 enum lttng_notification_channel_status nc_status
;
128 struct lttng_notification_channel
*notification_channel
= NULL
;
129 struct lttng_condition
*condition
= NULL
;
130 struct lttng_action
*action
= NULL
;
131 struct lttng_trigger
*trigger
= NULL
;
134 * Disable buffering on stdout.
135 * Safety measure to prevent hang on the validation side since
136 * stdout is used for outside synchronization.
138 setbuf(stdout
, NULL
);
141 printf("error: Missing arguments for tests\n");
146 ret
= parse_arguments(argv
);
148 printf("error: Could not parse arguments\n");
153 notification_channel
= lttng_notification_channel_create(
154 lttng_session_daemon_notification_endpoint
);
155 if (!notification_channel
) {
156 printf("error: Could not create notification channel\n");
161 switch (buffer_usage_type
) {
162 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
163 condition
= lttng_condition_buffer_usage_low_create();
165 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
166 condition
= lttng_condition_buffer_usage_high_create();
169 printf("error: Invalid buffer_usage_type\n");
175 printf("error: Could not create condition object\n");
180 if (is_threshold_ratio
) {
181 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
182 condition
, threshold_ratio
);
184 condition_status
= lttng_condition_buffer_usage_set_threshold(
185 condition
, threshold_bytes
);
188 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
189 printf("error: Could not set threshold\n");
194 condition_status
= lttng_condition_buffer_usage_set_session_name(
195 condition
, session_name
);
196 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
197 printf("error: Could not set session name\n");
201 condition_status
= lttng_condition_buffer_usage_set_channel_name(
202 condition
, channel_name
);
203 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
204 printf("error: Could not set channel name\n");
208 condition_status
= lttng_condition_buffer_usage_set_domain_type(
209 condition
, domain_type
);
210 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
211 printf("error: Could not set domain type\n");
216 if (use_action_list
) {
217 struct lttng_action
*notify
, *group
;
219 group
= lttng_action_list_create();
221 printf("error: Could not create action list\n");
226 notify
= lttng_action_notify_create();
228 lttng_action_destroy(group
);
229 printf("error: Could not create action notify\n");
234 action_status
= lttng_action_list_add_action(group
, notify
);
235 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
236 printf("error: Could not add action notify to action list\n");
237 lttng_action_destroy(group
);
238 lttng_action_destroy(notify
);
245 action
= lttng_action_notify_create();
247 printf("error: Could not create action notify\n");
253 trigger
= lttng_trigger_create(condition
, action
);
255 printf("error: Could not create trigger\n");
260 ret
= lttng_register_trigger(trigger
);
263 * An equivalent trigger might already be registered if an other app
264 * registered an equivalent trigger.
266 if (ret
< 0 && ret
!= -LTTNG_ERR_TRIGGER_EXISTS
) {
267 printf("error: %s\n", lttng_strerror(ret
));
272 nc_status
= lttng_notification_channel_subscribe(notification_channel
, condition
);
273 if (nc_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
274 printf("error: Could not subscribe\n");
279 /* Tell outside process that the client is ready */
280 printf("sync: ready\n");
283 struct lttng_notification
*notification
;
284 enum lttng_notification_channel_status status
;
285 const struct lttng_evaluation
*notification_evaluation
;
286 const struct lttng_condition
*notification_condition
;
288 if (nr_notifications
== nr_expected_notifications
) {
292 /* Receive the next notification. */
293 status
= lttng_notification_channel_get_next_notification(
294 notification_channel
,
298 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
300 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
302 printf("error: No drop should be observed during this test app\n");
304 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
306 * The notification channel has been closed by the
307 * session daemon. This is typically caused by a session
308 * daemon shutting down (cleanly or because of a crash).
310 printf("error: Notification channel was closed\n");
314 /* Unhandled conditions / errors. */
315 printf("error: Unknown notification channel status (%d) \n", status
);
320 notification_condition
= lttng_notification_get_condition(notification
);
321 notification_evaluation
= lttng_notification_get_evaluation(notification
);
323 ret
= handle_condition(notification_condition
, notification_evaluation
);
326 lttng_notification_destroy(notification
);
333 lttng_unregister_trigger(trigger
);
335 if (lttng_notification_channel_unsubscribe(notification_channel
, condition
)) {
336 printf("error: channel unsubscribe error\n");
338 lttng_trigger_destroy(trigger
);
339 lttng_condition_destroy(condition
);
340 lttng_action_destroy(action
);
341 lttng_notification_channel_destroy(notification_channel
);
342 printf("exit: %d\n", ret
);
346 int handle_condition(
347 const struct lttng_condition
*condition
,
348 const struct lttng_evaluation
*evaluation
)
351 const char *string_low
= "low";
352 const char *string_high
= "high";
353 const char *string_condition_type
= NULL
;
354 const char *condition_session_name
= NULL
;
355 const char *condition_channel_name
= NULL
;
356 enum lttng_condition_type condition_type
;
357 enum lttng_domain_type condition_domain_type
;
358 double buffer_usage_ratio
;
359 uint64_t buffer_usage_bytes
;
361 condition_type
= lttng_condition_get_type(condition
);
363 if (condition_type
!= buffer_usage_type
) {
365 printf("error: condition type and buffer usage type are not the same\n");
369 /* Fetch info to test */
370 ret
= lttng_condition_buffer_usage_get_session_name(condition
,
371 &condition_session_name
);
373 printf("error: session name could not be fetched\n");
377 ret
= lttng_condition_buffer_usage_get_channel_name(condition
,
378 &condition_channel_name
);
380 printf("error: channel name could not be fetched\n");
384 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
385 &condition_domain_type
);
387 printf("error: domain type could not be fetched\n");
392 if (strcmp(condition_session_name
, session_name
) != 0) {
393 printf("error: session name differs\n");
398 if (strcmp(condition_channel_name
, channel_name
) != 0) {
399 printf("error: channel name differs\n");
404 if (condition_domain_type
!= domain_type
) {
405 printf("error: domain type differs\n");
410 if (is_threshold_ratio
) {
411 lttng_evaluation_buffer_usage_get_usage_ratio(
412 evaluation
, &buffer_usage_ratio
);
413 switch (condition_type
) {
414 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
415 if (buffer_usage_ratio
> threshold_ratio
) {
416 printf("error: buffer usage ratio is bigger than set threshold ratio\n");
421 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
422 if (buffer_usage_ratio
< threshold_ratio
) {
423 printf("error: buffer usage ratio is lower than set threshold ratio\n");
429 printf("error: Unknown condition type\n");
434 lttng_evaluation_buffer_usage_get_usage(
435 evaluation
, &buffer_usage_bytes
);
436 switch (condition_type
) {
437 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
438 if (buffer_usage_bytes
> threshold_bytes
) {
439 printf("error: buffer usage ratio is bigger than set threshold bytes\n");
444 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
445 if (buffer_usage_bytes
< threshold_bytes
) {
446 printf("error: buffer usage ratio is lower than set threshold bytes\n");
452 printf("error: Unknown condition type\n");
458 switch (condition_type
) {
459 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
460 string_condition_type
= string_low
;
462 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
463 string_condition_type
= string_high
;
466 printf("error: Unknown condition type\n");
471 printf("notification: %s %d\n", string_condition_type
, nr_notifications
);