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
);
45 static int parse_arguments(char **argv
)
47 const char *domain_type_string
= NULL
;
48 const char *buffer_usage_type_string
= NULL
;
49 const char *buffer_usage_threshold_type
= NULL
;
50 const char *buffer_usage_threshold_value
= NULL
;
51 const char *nr_expected_notifications_string
= NULL
;
53 session_name
= argv
[1];
54 channel_name
= argv
[2];
55 domain_type_string
= argv
[3];
56 buffer_usage_type_string
= argv
[4];
57 buffer_usage_threshold_type
= argv
[5];
58 buffer_usage_threshold_value
= argv
[6];
59 nr_expected_notifications_string
= argv
[7];
63 if (!strcasecmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
64 domain_type
= LTTNG_DOMAIN_UST
;
66 if (!strcasecmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
67 domain_type
= LTTNG_DOMAIN_KERNEL
;
69 if (domain_type
== LTTNG_DOMAIN_NONE
) {
70 printf("error: Unknown domain type\n");
74 /* Buffer usage condition type */
75 if (!strcasecmp("low", buffer_usage_type_string
)) {
76 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
;
78 if (!strcasecmp("high", buffer_usage_type_string
)) {
79 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
81 if (buffer_usage_type
== LTTNG_CONDITION_TYPE_UNKNOWN
) {
82 printf("error: Unknown condition type\n");
86 /* Ratio or bytes ? */
87 if (!strcasecmp("bytes", buffer_usage_threshold_type
)) {
88 is_threshold_ratio
= false;
89 sscanf(buffer_usage_threshold_value
, "%" SCNu64
, &threshold_bytes
);
92 if (!strcasecmp("ratio", buffer_usage_threshold_type
)) {
93 is_threshold_ratio
= true;
94 sscanf(buffer_usage_threshold_value
, "%lf", &threshold_ratio
);
97 /* Number of notification to expect */
98 sscanf(nr_expected_notifications_string
, "%d", &nr_expected_notifications
);
105 int main(int argc
, char **argv
)
108 enum lttng_condition_status condition_status
;
109 enum lttng_notification_channel_status nc_status
;
110 struct lttng_notification_channel
*notification_channel
= NULL
;
111 struct lttng_condition
*condition
= NULL
;
112 struct lttng_action
*action
= NULL
;
113 struct lttng_trigger
*trigger
= NULL
;
116 * Disable buffering on stdout.
117 * Safety measure to prevent hang on the validation side since
118 * stdout is used for outside synchronization.
120 setbuf(stdout
, NULL
);
123 printf("error: Missing arguments for tests\n");
128 ret
= parse_arguments(argv
);
130 printf("error: Could not parse arguments\n");
135 notification_channel
= lttng_notification_channel_create(
136 lttng_session_daemon_notification_endpoint
);
137 if (!notification_channel
) {
138 printf("error: Could not create notification channel\n");
143 switch (buffer_usage_type
) {
144 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
145 condition
= lttng_condition_buffer_usage_low_create();
147 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
148 condition
= lttng_condition_buffer_usage_high_create();
151 printf("error: Invalid buffer_usage_type\n");
157 printf("error: Could not create condition object\n");
162 if (is_threshold_ratio
) {
163 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
164 condition
, threshold_ratio
);
166 condition_status
= lttng_condition_buffer_usage_set_threshold(
167 condition
, threshold_bytes
);
170 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
171 printf("error: Could not set threshold\n");
176 condition_status
= lttng_condition_buffer_usage_set_session_name(
177 condition
, session_name
);
178 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
179 printf("error: Could not set session name\n");
183 condition_status
= lttng_condition_buffer_usage_set_channel_name(
184 condition
, channel_name
);
185 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
186 printf("error: Could not set channel name\n");
190 condition_status
= lttng_condition_buffer_usage_set_domain_type(
191 condition
, domain_type
);
192 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
193 printf("error: Could not set domain type\n");
198 action
= lttng_action_notify_create();
200 printf("error: Could not create action notify\n");
205 trigger
= lttng_trigger_create(condition
, action
);
207 printf("error: Could not create trigger\n");
212 ret
= lttng_register_trigger(trigger
);
215 * An equivalent trigger might already be registered if an other app
216 * registered an equivalent trigger.
218 if (ret
< 0 && ret
!= -LTTNG_ERR_TRIGGER_EXISTS
) {
219 printf("error: %s\n", lttng_strerror(ret
));
224 nc_status
= lttng_notification_channel_subscribe(notification_channel
, condition
);
225 if (nc_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
226 printf("error: Could not subscribe\n");
231 /* Tell outside process that the client is ready */
232 printf("sync: ready\n");
235 struct lttng_notification
*notification
;
236 enum lttng_notification_channel_status status
;
237 const struct lttng_evaluation
*notification_evaluation
;
238 const struct lttng_condition
*notification_condition
;
240 if (nr_notifications
== nr_expected_notifications
) {
244 /* Receive the next notification. */
245 status
= lttng_notification_channel_get_next_notification(
246 notification_channel
,
250 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
252 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
254 printf("error: No drop should be observed during this test app\n");
256 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
258 * The notification channel has been closed by the
259 * session daemon. This is typically caused by a session
260 * daemon shutting down (cleanly or because of a crash).
262 printf("error: Notification channel was closed\n");
266 /* Unhandled conditions / errors. */
267 printf("error: Unknown notification channel status\n");
272 notification_condition
= lttng_notification_get_condition(notification
);
273 notification_evaluation
= lttng_notification_get_evaluation(notification
);
275 ret
= handle_condition(notification_condition
, notification_evaluation
);
278 lttng_notification_destroy(notification
);
285 lttng_unregister_trigger(trigger
);
287 if (lttng_notification_channel_unsubscribe(notification_channel
, condition
)) {
288 printf("error: channel unsubscribe error\n");
290 lttng_trigger_destroy(trigger
);
291 lttng_condition_destroy(condition
);
292 lttng_action_destroy(action
);
293 lttng_notification_channel_destroy(notification_channel
);
294 printf("exit: %d\n", ret
);
298 int handle_condition(
299 const struct lttng_condition
*condition
,
300 const struct lttng_evaluation
*evaluation
)
303 const char *string_low
= "low";
304 const char *string_high
= "high";
305 const char *string_condition_type
= NULL
;
306 const char *condition_session_name
= NULL
;
307 const char *condition_channel_name
= NULL
;
308 enum lttng_condition_type condition_type
;
309 enum lttng_domain_type condition_domain_type
;
310 double buffer_usage_ratio
;
311 uint64_t buffer_usage_bytes
;
313 condition_type
= lttng_condition_get_type(condition
);
315 if (condition_type
!= buffer_usage_type
) {
317 printf("error: condition type and buffer usage type are not the same\n");
321 /* Fetch info to test */
322 ret
= lttng_condition_buffer_usage_get_session_name(condition
,
323 &condition_session_name
);
325 printf("error: session name could not be fetched\n");
329 ret
= lttng_condition_buffer_usage_get_channel_name(condition
,
330 &condition_channel_name
);
332 printf("error: channel name could not be fetched\n");
336 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
337 &condition_domain_type
);
339 printf("error: domain type could not be fetched\n");
344 if (strcmp(condition_session_name
, session_name
) != 0) {
345 printf("error: session name differs\n");
350 if (strcmp(condition_channel_name
, channel_name
) != 0) {
351 printf("error: channel name differs\n");
356 if (condition_domain_type
!= domain_type
) {
357 printf("error: domain type differs\n");
362 if (is_threshold_ratio
) {
363 lttng_evaluation_buffer_usage_get_usage_ratio(
364 evaluation
, &buffer_usage_ratio
);
365 switch (condition_type
) {
366 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
367 if (buffer_usage_ratio
> threshold_ratio
) {
368 printf("error: buffer usage ratio is bigger than set threshold ratio\n");
373 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
374 if (buffer_usage_ratio
< threshold_ratio
) {
375 printf("error: buffer usage ratio is lower than set threshold ratio\n");
381 printf("error: Unknown condition type\n");
386 lttng_evaluation_buffer_usage_get_usage(
387 evaluation
, &buffer_usage_bytes
);
388 switch (condition_type
) {
389 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
390 if (buffer_usage_bytes
> threshold_bytes
) {
391 printf("error: buffer usage ratio is bigger than set threshold bytes\n");
396 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
397 if (buffer_usage_bytes
< threshold_bytes
) {
398 printf("error: buffer usage ratio is lower than set threshold bytes\n");
404 printf("error: Unknown condition type\n");
410 switch (condition_type
) {
411 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
412 string_condition_type
= string_low
;
414 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
415 string_condition_type
= string_high
;
418 printf("error: Unknown condition type\n");
423 printf("notification: %s %d\n", string_condition_type
, nr_notifications
);