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/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 enum lttng_condition_type buffer_usage_type
= LTTNG_CONDITION_TYPE_UNKNOWN
;
39 static enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
42 const struct lttng_condition
*condition
,
43 const struct lttng_evaluation
*condition_evaluation
);
46 int parse_arguments(char **argv
)
49 const char *domain_type_string
= NULL
;
50 const char *buffer_usage_type_string
= NULL
;
51 const char *buffer_usage_threshold_type
= NULL
;
52 const char *buffer_usage_threshold_value
= NULL
;
53 const char *nr_expected_notifications_string
= 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];
65 if (!strcasecmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
66 domain_type
= LTTNG_DOMAIN_UST
;
68 if (!strcasecmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
69 domain_type
= LTTNG_DOMAIN_KERNEL
;
71 if (domain_type
== LTTNG_DOMAIN_NONE
) {
72 printf("error: Unknown domain type\n");
76 /* Buffer usage condition type */
77 if (!strcasecmp("low", buffer_usage_type_string
)) {
78 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
;
80 if (!strcasecmp("high", buffer_usage_type_string
)) {
81 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
83 if (buffer_usage_type
== LTTNG_CONDITION_TYPE_UNKNOWN
) {
84 printf("error: Unknown condition type\n");
88 /* Ratio or bytes ? */
89 if (!strcasecmp("bytes", buffer_usage_threshold_type
)) {
90 is_threshold_ratio
= false;
91 sscanf(buffer_usage_threshold_value
, "%" SCNu64
, &threshold_bytes
);
94 if (!strcasecmp("ratio", buffer_usage_threshold_type
)) {
95 is_threshold_ratio
= true;
96 sscanf(buffer_usage_threshold_value
, "%lf", &threshold_ratio
);
99 /* Number of notification to expect */
100 sscanf_ret
= sscanf(nr_expected_notifications_string
, "%d",
101 &nr_expected_notifications
);
102 if (sscanf_ret
!= 1) {
103 printf("error: Invalid nr_expected_notifications, sscanf returned %d\n",
113 int main(int argc
, char **argv
)
116 enum lttng_condition_status condition_status
;
117 enum lttng_notification_channel_status nc_status
;
118 struct lttng_notification_channel
*notification_channel
= NULL
;
119 struct lttng_condition
*condition
= NULL
;
120 struct lttng_action
*action
= NULL
;
121 struct lttng_trigger
*trigger
= NULL
;
124 * Disable buffering on stdout.
125 * Safety measure to prevent hang on the validation side since
126 * stdout is used for outside synchronization.
128 setbuf(stdout
, NULL
);
131 printf("error: Missing arguments for tests\n");
136 ret
= parse_arguments(argv
);
138 printf("error: Could not parse arguments\n");
143 notification_channel
= lttng_notification_channel_create(
144 lttng_session_daemon_notification_endpoint
);
145 if (!notification_channel
) {
146 printf("error: Could not create notification channel\n");
151 switch (buffer_usage_type
) {
152 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
153 condition
= lttng_condition_buffer_usage_low_create();
155 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
156 condition
= lttng_condition_buffer_usage_high_create();
159 printf("error: Invalid buffer_usage_type\n");
165 printf("error: Could not create condition object\n");
170 if (is_threshold_ratio
) {
171 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
172 condition
, threshold_ratio
);
174 condition_status
= lttng_condition_buffer_usage_set_threshold(
175 condition
, threshold_bytes
);
178 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
179 printf("error: Could not set threshold\n");
184 condition_status
= lttng_condition_buffer_usage_set_session_name(
185 condition
, session_name
);
186 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
187 printf("error: Could not set session name\n");
191 condition_status
= lttng_condition_buffer_usage_set_channel_name(
192 condition
, channel_name
);
193 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
194 printf("error: Could not set channel name\n");
198 condition_status
= lttng_condition_buffer_usage_set_domain_type(
199 condition
, domain_type
);
200 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
201 printf("error: Could not set domain type\n");
206 action
= lttng_action_notify_create();
208 printf("error: Could not create action notify\n");
213 trigger
= lttng_trigger_create(condition
, action
);
215 printf("error: Could not create trigger\n");
220 ret
= lttng_register_trigger(trigger
);
223 * An equivalent trigger might already be registered if an other app
224 * registered an equivalent trigger.
226 if (ret
< 0 && ret
!= -LTTNG_ERR_TRIGGER_EXISTS
) {
227 printf("error: %s\n", lttng_strerror(ret
));
232 nc_status
= lttng_notification_channel_subscribe(notification_channel
, condition
);
233 if (nc_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
234 printf("error: Could not subscribe\n");
239 /* Tell outside process that the client is ready */
240 printf("sync: ready\n");
243 struct lttng_notification
*notification
;
244 enum lttng_notification_channel_status status
;
245 const struct lttng_evaluation
*notification_evaluation
;
246 const struct lttng_condition
*notification_condition
;
248 if (nr_notifications
== nr_expected_notifications
) {
252 /* Receive the next notification. */
253 status
= lttng_notification_channel_get_next_notification(
254 notification_channel
,
258 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
260 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
262 printf("error: No drop should be observed during this test app\n");
264 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
266 * The notification channel has been closed by the
267 * session daemon. This is typically caused by a session
268 * daemon shutting down (cleanly or because of a crash).
270 printf("error: Notification channel was closed\n");
274 /* Unhandled conditions / errors. */
275 printf("error: Unknown notification channel status\n");
280 notification_condition
= lttng_notification_get_condition(notification
);
281 notification_evaluation
= lttng_notification_get_evaluation(notification
);
283 ret
= handle_condition(notification_condition
, notification_evaluation
);
286 lttng_notification_destroy(notification
);
293 lttng_unregister_trigger(trigger
);
295 if (lttng_notification_channel_unsubscribe(notification_channel
, condition
)) {
296 printf("error: channel unsubscribe error\n");
298 lttng_trigger_destroy(trigger
);
299 lttng_condition_destroy(condition
);
300 lttng_action_destroy(action
);
301 lttng_notification_channel_destroy(notification_channel
);
302 printf("exit: %d\n", ret
);
306 int handle_condition(
307 const struct lttng_condition
*condition
,
308 const struct lttng_evaluation
*evaluation
)
311 const char *string_low
= "low";
312 const char *string_high
= "high";
313 const char *string_condition_type
= NULL
;
314 const char *condition_session_name
= NULL
;
315 const char *condition_channel_name
= NULL
;
316 enum lttng_condition_type condition_type
;
317 enum lttng_domain_type condition_domain_type
;
318 double buffer_usage_ratio
;
319 uint64_t buffer_usage_bytes
;
321 condition_type
= lttng_condition_get_type(condition
);
323 if (condition_type
!= buffer_usage_type
) {
325 printf("error: condition type and buffer usage type are not the same\n");
329 /* Fetch info to test */
330 ret
= lttng_condition_buffer_usage_get_session_name(condition
,
331 &condition_session_name
);
333 printf("error: session name could not be fetched\n");
337 ret
= lttng_condition_buffer_usage_get_channel_name(condition
,
338 &condition_channel_name
);
340 printf("error: channel name could not be fetched\n");
344 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
345 &condition_domain_type
);
347 printf("error: domain type could not be fetched\n");
352 if (strcmp(condition_session_name
, session_name
) != 0) {
353 printf("error: session name differs\n");
358 if (strcmp(condition_channel_name
, channel_name
) != 0) {
359 printf("error: channel name differs\n");
364 if (condition_domain_type
!= domain_type
) {
365 printf("error: domain type differs\n");
370 if (is_threshold_ratio
) {
371 lttng_evaluation_buffer_usage_get_usage_ratio(
372 evaluation
, &buffer_usage_ratio
);
373 switch (condition_type
) {
374 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
375 if (buffer_usage_ratio
> threshold_ratio
) {
376 printf("error: buffer usage ratio is bigger than set threshold ratio\n");
381 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
382 if (buffer_usage_ratio
< threshold_ratio
) {
383 printf("error: buffer usage ratio is lower than set threshold ratio\n");
389 printf("error: Unknown condition type\n");
394 lttng_evaluation_buffer_usage_get_usage(
395 evaluation
, &buffer_usage_bytes
);
396 switch (condition_type
) {
397 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
398 if (buffer_usage_bytes
> threshold_bytes
) {
399 printf("error: buffer usage ratio is bigger than set threshold bytes\n");
404 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
405 if (buffer_usage_bytes
< threshold_bytes
) {
406 printf("error: buffer usage ratio is lower than set threshold bytes\n");
412 printf("error: Unknown condition type\n");
418 switch (condition_type
) {
419 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
420 string_condition_type
= string_low
;
422 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
423 string_condition_type
= string_high
;
426 printf("error: Unknown condition type\n");
431 printf("notification: %s %d\n", string_condition_type
, nr_notifications
);