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_group
= 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
)
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_group_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_group_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(buffer_usage_threshold_value
, "%" SCNu64
, &threshold_bytes
);
97 if (!strcasecmp("ratio", buffer_usage_threshold_type
)) {
98 is_threshold_ratio
= true;
99 sscanf(buffer_usage_threshold_value
, "%lf", &threshold_ratio
);
102 /* Number of notification to expect */
103 sscanf(nr_expected_notifications_string
, "%d", &nr_expected_notifications
);
105 /* Put notify action in a group. */
106 if (!strcasecmp("1", use_action_group_value
)) {
107 use_action_group
= true;
115 int main(int argc
, char **argv
)
118 enum lttng_condition_status condition_status
;
119 enum lttng_action_status action_status
;
120 enum lttng_notification_channel_status nc_status
;
121 struct lttng_notification_channel
*notification_channel
= NULL
;
122 struct lttng_condition
*condition
= NULL
;
123 struct lttng_action
*action
= NULL
;
124 struct lttng_trigger
*trigger
= NULL
;
127 * Disable buffering on stdout.
128 * Safety measure to prevent hang on the validation side since
129 * stdout is used for outside synchronization.
131 setbuf(stdout
, NULL
);
134 printf("error: Missing arguments for tests\n");
139 ret
= parse_arguments(argv
);
141 printf("error: Could not parse arguments\n");
146 notification_channel
= lttng_notification_channel_create(
147 lttng_session_daemon_notification_endpoint
);
148 if (!notification_channel
) {
149 printf("error: Could not create notification channel\n");
154 switch (buffer_usage_type
) {
155 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
156 condition
= lttng_condition_buffer_usage_low_create();
158 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
159 condition
= lttng_condition_buffer_usage_high_create();
162 printf("error: Invalid buffer_usage_type\n");
168 printf("error: Could not create condition object\n");
173 if (is_threshold_ratio
) {
174 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
175 condition
, threshold_ratio
);
177 condition_status
= lttng_condition_buffer_usage_set_threshold(
178 condition
, threshold_bytes
);
181 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
182 printf("error: Could not set threshold\n");
187 condition_status
= lttng_condition_buffer_usage_set_session_name(
188 condition
, session_name
);
189 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
190 printf("error: Could not set session name\n");
194 condition_status
= lttng_condition_buffer_usage_set_channel_name(
195 condition
, channel_name
);
196 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
197 printf("error: Could not set channel name\n");
201 condition_status
= lttng_condition_buffer_usage_set_domain_type(
202 condition
, domain_type
);
203 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
204 printf("error: Could not set domain type\n");
209 if (use_action_group
) {
210 struct lttng_action
*notify
, *group
;
212 group
= lttng_action_group_create();
214 printf("error: Could not create action group\n");
218 notify
= lttng_action_notify_create();
220 lttng_action_destroy(group
);
221 printf("error: Could not create action notify\n");
225 action_status
= lttng_action_group_add_action(group
, notify
);
226 if (action_status
!= LTTNG_ACTION_STATUS_OK
) {
227 printf("error: Could not add action notify to action group\n");
228 lttng_action_destroy(group
);
229 lttng_action_destroy(notify
);
236 action
= lttng_action_notify_create();
238 printf("error: Could not create action notify\n");
244 trigger
= lttng_trigger_create(condition
, action
);
246 printf("error: Could not create trigger\n");
251 ret
= lttng_register_trigger(trigger
);
254 * An equivalent trigger might already be registered if an other app
255 * registered an equivalent trigger.
257 if (ret
< 0 && ret
!= -LTTNG_ERR_TRIGGER_EXISTS
) {
258 printf("error: %s\n", lttng_strerror(ret
));
263 nc_status
= lttng_notification_channel_subscribe(notification_channel
, condition
);
264 if (nc_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
265 printf("error: Could not subscribe\n");
270 /* Tell outside process that the client is ready */
271 printf("sync: ready\n");
274 struct lttng_notification
*notification
;
275 enum lttng_notification_channel_status status
;
276 const struct lttng_evaluation
*notification_evaluation
;
277 const struct lttng_condition
*notification_condition
;
279 if (nr_notifications
== nr_expected_notifications
) {
283 /* Receive the next notification. */
284 status
= lttng_notification_channel_get_next_notification(
285 notification_channel
,
289 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
291 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
293 printf("error: No drop should be observed during this test app\n");
295 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
297 * The notification channel has been closed by the
298 * session daemon. This is typically caused by a session
299 * daemon shutting down (cleanly or because of a crash).
301 printf("error: Notification channel was closed\n");
305 /* Unhandled conditions / errors. */
306 printf("error: Unknown notification channel status (%d) \n", status
);
311 notification_condition
= lttng_notification_get_condition(notification
);
312 notification_evaluation
= lttng_notification_get_evaluation(notification
);
314 ret
= handle_condition(notification_condition
, notification_evaluation
);
317 lttng_notification_destroy(notification
);
324 lttng_unregister_trigger(trigger
);
326 if (lttng_notification_channel_unsubscribe(notification_channel
, condition
)) {
327 printf("error: channel unsubscribe error\n");
329 lttng_trigger_destroy(trigger
);
330 lttng_condition_destroy(condition
);
331 lttng_action_destroy(action
);
332 lttng_notification_channel_destroy(notification_channel
);
333 printf("exit: %d\n", ret
);
337 int handle_condition(
338 const struct lttng_condition
*condition
,
339 const struct lttng_evaluation
*evaluation
)
342 const char *string_low
= "low";
343 const char *string_high
= "high";
344 const char *string_condition_type
= NULL
;
345 const char *condition_session_name
= NULL
;
346 const char *condition_channel_name
= NULL
;
347 enum lttng_condition_type condition_type
;
348 enum lttng_domain_type condition_domain_type
;
349 double buffer_usage_ratio
;
350 uint64_t buffer_usage_bytes
;
352 condition_type
= lttng_condition_get_type(condition
);
354 if (condition_type
!= buffer_usage_type
) {
356 printf("error: condition type and buffer usage type are not the same\n");
360 /* Fetch info to test */
361 ret
= lttng_condition_buffer_usage_get_session_name(condition
,
362 &condition_session_name
);
364 printf("error: session name could not be fetched\n");
368 ret
= lttng_condition_buffer_usage_get_channel_name(condition
,
369 &condition_channel_name
);
371 printf("error: channel name could not be fetched\n");
375 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
376 &condition_domain_type
);
378 printf("error: domain type could not be fetched\n");
383 if (strcmp(condition_session_name
, session_name
) != 0) {
384 printf("error: session name differs\n");
389 if (strcmp(condition_channel_name
, channel_name
) != 0) {
390 printf("error: channel name differs\n");
395 if (condition_domain_type
!= domain_type
) {
396 printf("error: domain type differs\n");
401 if (is_threshold_ratio
) {
402 lttng_evaluation_buffer_usage_get_usage_ratio(
403 evaluation
, &buffer_usage_ratio
);
404 switch (condition_type
) {
405 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
406 if (buffer_usage_ratio
> threshold_ratio
) {
407 printf("error: buffer usage ratio is bigger than set threshold ratio\n");
412 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
413 if (buffer_usage_ratio
< threshold_ratio
) {
414 printf("error: buffer usage ratio is lower than set threshold ratio\n");
420 printf("error: Unknown condition type\n");
425 lttng_evaluation_buffer_usage_get_usage(
426 evaluation
, &buffer_usage_bytes
);
427 switch (condition_type
) {
428 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
429 if (buffer_usage_bytes
> threshold_bytes
) {
430 printf("error: buffer usage ratio is bigger than set threshold bytes\n");
435 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
436 if (buffer_usage_bytes
< threshold_bytes
) {
437 printf("error: buffer usage ratio is lower than set threshold bytes\n");
443 printf("error: Unknown condition type\n");
449 switch (condition_type
) {
450 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
451 string_condition_type
= string_low
;
453 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
454 string_condition_type
= string_high
;
457 printf("error: Unknown condition type\n");
462 printf("notification: %s %d\n", string_condition_type
, nr_notifications
);