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
)
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
;
54 session_name
= argv
[1];
55 channel_name
= argv
[2];
56 domain_type_string
= argv
[3];
57 buffer_usage_type_string
= argv
[4];
58 buffer_usage_threshold_type
= argv
[5];
59 buffer_usage_threshold_value
= argv
[6];
60 nr_expected_notifications_string
= argv
[7];
64 if (!strcasecmp("LTTNG_DOMAIN_UST", domain_type_string
)) {
65 domain_type
= LTTNG_DOMAIN_UST
;
67 if (!strcasecmp("LTTNG_DOMAIN_KERNEL", domain_type_string
)) {
68 domain_type
= LTTNG_DOMAIN_KERNEL
;
70 if (domain_type
== LTTNG_DOMAIN_NONE
) {
71 printf("error: Unknown domain type\n");
75 /* Buffer usage condition type */
76 if (!strcasecmp("low", buffer_usage_type_string
)) {
77 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
;
79 if (!strcasecmp("high", buffer_usage_type_string
)) {
80 buffer_usage_type
= LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
82 if (buffer_usage_type
== LTTNG_CONDITION_TYPE_UNKNOWN
) {
83 printf("error: Unknown condition type\n");
87 /* Ratio or bytes ? */
88 if (!strcasecmp("bytes", buffer_usage_threshold_type
)) {
89 is_threshold_ratio
= false;
90 sscanf(buffer_usage_threshold_value
, "%" SCNu64
, &threshold_bytes
);
93 if (!strcasecmp("ratio", buffer_usage_threshold_type
)) {
94 is_threshold_ratio
= true;
95 sscanf(buffer_usage_threshold_value
, "%lf", &threshold_ratio
);
98 /* Number of notification to expect */
99 sscanf(nr_expected_notifications_string
, "%d", &nr_expected_notifications
);
106 int main(int argc
, char **argv
)
109 enum lttng_condition_status condition_status
;
110 enum lttng_notification_channel_status nc_status
;
111 struct lttng_notification_channel
*notification_channel
= NULL
;
112 struct lttng_condition
*condition
= NULL
;
113 struct lttng_action
*action
= NULL
;
114 struct lttng_trigger
*trigger
= NULL
;
117 * Disable buffering on stdout.
118 * Safety measure to prevent hang on the validation side since
119 * stdout is used for outside synchronization.
121 setbuf(stdout
, NULL
);
124 printf("error: Missing arguments for tests\n");
129 ret
= parse_arguments(argv
);
131 printf("error: Could not parse arguments\n");
136 notification_channel
= lttng_notification_channel_create(
137 lttng_session_daemon_notification_endpoint
);
138 if (!notification_channel
) {
139 printf("error: Could not create notification channel\n");
144 switch (buffer_usage_type
) {
145 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
146 condition
= lttng_condition_buffer_usage_low_create();
148 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
149 condition
= lttng_condition_buffer_usage_high_create();
152 printf("error: Invalid buffer_usage_type\n");
158 printf("error: Could not create condition object\n");
163 if (is_threshold_ratio
) {
164 condition_status
= lttng_condition_buffer_usage_set_threshold_ratio(
165 condition
, threshold_ratio
);
167 condition_status
= lttng_condition_buffer_usage_set_threshold(
168 condition
, threshold_bytes
);
171 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
172 printf("error: Could not set threshold\n");
177 condition_status
= lttng_condition_buffer_usage_set_session_name(
178 condition
, session_name
);
179 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
180 printf("error: Could not set session name\n");
184 condition_status
= lttng_condition_buffer_usage_set_channel_name(
185 condition
, channel_name
);
186 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
187 printf("error: Could not set channel name\n");
191 condition_status
= lttng_condition_buffer_usage_set_domain_type(
192 condition
, domain_type
);
193 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
194 printf("error: Could not set domain type\n");
199 action
= lttng_action_notify_create();
201 printf("error: Could not create action notify\n");
206 trigger
= lttng_trigger_create(condition
, action
);
208 printf("error: Could not create trigger\n");
213 ret
= lttng_register_trigger(trigger
);
216 * An equivalent trigger might already be registered if an other app
217 * registered an equivalent trigger.
219 if (ret
< 0 && ret
!= -LTTNG_ERR_TRIGGER_EXISTS
) {
220 printf("error: %s\n", lttng_strerror(ret
));
225 nc_status
= lttng_notification_channel_subscribe(notification_channel
, condition
);
226 if (nc_status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
227 printf("error: Could not subscribe\n");
232 /* Tell outside process that the client is ready */
233 printf("sync: ready\n");
236 struct lttng_notification
*notification
;
237 enum lttng_notification_channel_status status
;
238 const struct lttng_evaluation
*notification_evaluation
;
239 const struct lttng_condition
*notification_condition
;
241 if (nr_notifications
== nr_expected_notifications
) {
245 /* Receive the next notification. */
246 status
= lttng_notification_channel_get_next_notification(
247 notification_channel
,
251 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
253 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
255 printf("error: No drop should be observed during this test app\n");
257 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
259 * The notification channel has been closed by the
260 * session daemon. This is typically caused by a session
261 * daemon shutting down (cleanly or because of a crash).
263 printf("error: Notification channel was closed\n");
267 /* Unhandled conditions / errors. */
268 printf("error: Unknown notification channel status\n");
273 notification_condition
= lttng_notification_get_condition(notification
);
274 notification_evaluation
= lttng_notification_get_evaluation(notification
);
276 ret
= handle_condition(notification_condition
, notification_evaluation
);
279 lttng_notification_destroy(notification
);
286 lttng_unregister_trigger(trigger
);
288 if (lttng_notification_channel_unsubscribe(notification_channel
, condition
)) {
289 printf("error: channel unsubscribe error\n");
291 lttng_trigger_destroy(trigger
);
292 lttng_condition_destroy(condition
);
293 lttng_action_destroy(action
);
294 lttng_notification_channel_destroy(notification_channel
);
295 printf("exit: %d\n", ret
);
299 int handle_condition(
300 const struct lttng_condition
*condition
,
301 const struct lttng_evaluation
*evaluation
)
304 const char *string_low
= "low";
305 const char *string_high
= "high";
306 const char *string_condition_type
= NULL
;
307 const char *condition_session_name
= NULL
;
308 const char *condition_channel_name
= NULL
;
309 enum lttng_condition_type condition_type
;
310 enum lttng_domain_type condition_domain_type
;
311 double buffer_usage_ratio
;
312 uint64_t buffer_usage_bytes
;
314 condition_type
= lttng_condition_get_type(condition
);
316 if (condition_type
!= buffer_usage_type
) {
318 printf("error: condition type and buffer usage type are not the same\n");
322 /* Fetch info to test */
323 ret
= lttng_condition_buffer_usage_get_session_name(condition
,
324 &condition_session_name
);
326 printf("error: session name could not be fetched\n");
330 ret
= lttng_condition_buffer_usage_get_channel_name(condition
,
331 &condition_channel_name
);
333 printf("error: channel name could not be fetched\n");
337 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
338 &condition_domain_type
);
340 printf("error: domain type could not be fetched\n");
345 if (strcmp(condition_session_name
, session_name
) != 0) {
346 printf("error: session name differs\n");
351 if (strcmp(condition_channel_name
, channel_name
) != 0) {
352 printf("error: channel name differs\n");
357 if (condition_domain_type
!= domain_type
) {
358 printf("error: domain type differs\n");
363 if (is_threshold_ratio
) {
364 lttng_evaluation_buffer_usage_get_usage_ratio(
365 evaluation
, &buffer_usage_ratio
);
366 switch (condition_type
) {
367 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
368 if (buffer_usage_ratio
> threshold_ratio
) {
369 printf("error: buffer usage ratio is bigger than set threshold ratio\n");
374 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
375 if (buffer_usage_ratio
< threshold_ratio
) {
376 printf("error: buffer usage ratio is lower than set threshold ratio\n");
382 printf("error: Unknown condition type\n");
387 lttng_evaluation_buffer_usage_get_usage(
388 evaluation
, &buffer_usage_bytes
);
389 switch (condition_type
) {
390 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
391 if (buffer_usage_bytes
> threshold_bytes
) {
392 printf("error: buffer usage ratio is bigger than set threshold bytes\n");
397 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
398 if (buffer_usage_bytes
< threshold_bytes
) {
399 printf("error: buffer usage ratio is lower than set threshold bytes\n");
405 printf("error: Unknown condition type\n");
411 switch (condition_type
) {
412 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
413 string_condition_type
= string_low
;
415 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
416 string_condition_type
= string_high
;
419 printf("error: Unknown condition type\n");
424 printf("notification: %s %d\n", string_condition_type
, nr_notifications
);