File: base_client.c

package info (click to toggle)
ltt-control 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,860 kB
  • sloc: cpp: 192,012; sh: 28,777; ansic: 10,960; python: 7,108; makefile: 3,520; java: 109; xml: 46
file content (471 lines) | stat: -rw-r--r-- 13,058 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * base_client.c
 *
 * Base client application for testing of LTTng notification API
 *
 * SPDX-FileCopyrightText: 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include <lttng/action/action.h>
#include <lttng/action/list.h>
#include <lttng/action/notify.h>
#include <lttng/condition/buffer-usage.h>
#include <lttng/condition/condition.h>
#include <lttng/condition/evaluation.h>
#include <lttng/domain.h>
#include <lttng/endpoint.h>
#include <lttng/lttng-error.h>
#include <lttng/notification/channel.h>
#include <lttng/notification/notification.h>
#include <lttng/trigger/trigger.h>

#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

static unsigned int nr_notifications = 0;
static unsigned int nr_expected_notifications = 0;
static const char *session_name = NULL;
static const char *channel_name = NULL;
static double threshold_ratio = 0.0;
static uint64_t threshold_bytes = 0;
static bool is_threshold_ratio = false;
static bool use_action_list = false;
static enum lttng_condition_type buffer_usage_type = LTTNG_CONDITION_TYPE_UNKNOWN;
static enum lttng_domain_type domain_type = LTTNG_DOMAIN_NONE;

int handle_condition(const struct lttng_condition *condition,
		     const struct lttng_evaluation *condition_evaluation);

static int parse_arguments(char **argv)
{
	int sscanf_ret;
	const char *domain_type_string = NULL;
	const char *buffer_usage_type_string = NULL;
	const char *buffer_usage_threshold_type = NULL;
	const char *buffer_usage_threshold_value = NULL;
	const char *nr_expected_notifications_string = NULL;
	const char *use_action_list_value = NULL;

	session_name = argv[1];
	channel_name = argv[2];
	domain_type_string = argv[3];
	buffer_usage_type_string = argv[4];
	buffer_usage_threshold_type = argv[5];
	buffer_usage_threshold_value = argv[6];
	nr_expected_notifications_string = argv[7];
	use_action_list_value = argv[8];

	/* Parse arguments */
	/* Domain type */
	if (!strcasecmp("LTTNG_DOMAIN_UST", domain_type_string)) {
		domain_type = LTTNG_DOMAIN_UST;
	}
	if (!strcasecmp("LTTNG_DOMAIN_KERNEL", domain_type_string)) {
		domain_type = LTTNG_DOMAIN_KERNEL;
	}
	if (domain_type == LTTNG_DOMAIN_NONE) {
		printf("error: Unknown domain type\n");
		goto error;
	}

	/* Buffer usage condition type */
	if (!strcasecmp("low", buffer_usage_type_string)) {
		buffer_usage_type = LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW;
	}
	if (!strcasecmp("high", buffer_usage_type_string)) {
		buffer_usage_type = LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH;
	}
	if (buffer_usage_type == LTTNG_CONDITION_TYPE_UNKNOWN) {
		printf("error: Unknown condition type\n");
		goto error;
	}

	/* Ratio or bytes ? */
	if (!strcasecmp("bytes", buffer_usage_threshold_type)) {
		is_threshold_ratio = false;
		sscanf_ret = sscanf(buffer_usage_threshold_value, "%" SCNu64, &threshold_bytes);
		if (sscanf_ret != 1) {
			printf("error: Invalid buffer usage threshold value bytes (integer), sscanf returned %d\n",
			       sscanf_ret);
			goto error;
		}
	}

	if (!strcasecmp("ratio", buffer_usage_threshold_type)) {
		is_threshold_ratio = true;
		sscanf_ret = sscanf(buffer_usage_threshold_value, "%lf", &threshold_ratio);
		if (sscanf_ret != 1) {
			printf("error: Invalid buffer usage threshold value ratio (float), sscanf returned %d\n",
			       sscanf_ret);
			goto error;
		}
	}

	/* Number of notification to expect */
	sscanf_ret = sscanf(nr_expected_notifications_string, "%d", &nr_expected_notifications);
	if (sscanf_ret != 1) {
		printf("error: Invalid nr_expected_notifications, sscanf returned %d\n",
		       sscanf_ret);
		goto error;
	}

	/* Put notify action in a group. */
	if (!strcasecmp("1", use_action_list_value)) {
		use_action_list = true;
	}

	return 0;
error:
	return 1;
}

int main(int argc, char **argv)
{
	int ret = 0;
	enum lttng_condition_status condition_status;
	enum lttng_action_status action_status;
	enum lttng_notification_channel_status nc_status;
	struct lttng_notification_channel *notification_channel = NULL;
	struct lttng_condition *condition = NULL;
	struct lttng_action *action = NULL;
	struct lttng_trigger *trigger = NULL;
	enum lttng_error_code ret_code;

	/*
	 * Disable buffering on stdout.
	 * Safety measure to prevent hang on the validation side since
	 * stdout is used for outside synchronization.
	 */
	setbuf(stdout, NULL);

	if (argc < 9) {
		printf("error: Missing arguments for tests\n");
		ret = 1;
		goto end;
	}

	ret = parse_arguments(argv);
	if (ret) {
		printf("error: Could not parse arguments\n");
		goto end;
	}

	/* Setup */
	notification_channel =
		lttng_notification_channel_create(lttng_session_daemon_notification_endpoint);
	if (!notification_channel) {
		printf("error: Could not create notification channel\n");
		ret = 1;
		goto end;
	}

	switch (buffer_usage_type) {
	case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
		condition = lttng_condition_buffer_usage_low_create();
		break;
	case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
		condition = lttng_condition_buffer_usage_high_create();
		break;
	default:
		printf("error: Invalid buffer_usage_type\n");
		ret = 1;
		goto end;
	}

	if (!condition) {
		printf("error: Could not create condition object\n");
		ret = 1;
		goto end;
	}

	if (is_threshold_ratio) {
		condition_status = lttng_condition_buffer_usage_set_threshold_ratio(
			condition, threshold_ratio);
	} else {
		condition_status =
			lttng_condition_buffer_usage_set_threshold(condition, threshold_bytes);
	}

	if (condition_status != LTTNG_CONDITION_STATUS_OK) {
		printf("error: Could not set threshold\n");
		ret = 1;
		goto end;
	}

	condition_status = lttng_condition_buffer_usage_set_session_name(condition, session_name);
	if (condition_status != LTTNG_CONDITION_STATUS_OK) {
		printf("error: Could not set session name\n");
		ret = 1;
		goto end;
	}
	condition_status = lttng_condition_buffer_usage_set_channel_name(condition, channel_name);
	if (condition_status != LTTNG_CONDITION_STATUS_OK) {
		printf("error: Could not set channel name\n");
		ret = 1;
		goto end;
	}
	condition_status = lttng_condition_buffer_usage_set_domain_type(condition, domain_type);
	if (condition_status != LTTNG_CONDITION_STATUS_OK) {
		printf("error: Could not set domain type\n");
		ret = 1;
		goto end;
	}

	if (use_action_list) {
		struct lttng_action *notify, *group;

		group = lttng_action_list_create();
		if (!group) {
			printf("error: Could not create action list\n");
			ret = 1;
			goto end;
		}

		notify = lttng_action_notify_create();
		if (!notify) {
			lttng_action_destroy(group);
			printf("error: Could not create action notify\n");
			ret = 1;
			goto end;
		}

		action_status = lttng_action_list_add_action(group, notify);
		if (action_status != LTTNG_ACTION_STATUS_OK) {
			printf("error: Could not add action notify to action list\n");
			lttng_action_destroy(group);
			lttng_action_destroy(notify);
			ret = 1;
			goto end;
		}

		action = group;
	} else {
		action = lttng_action_notify_create();
		if (!action) {
			printf("error: Could not create action notify\n");
			ret = 1;
			goto end;
		}
	}

	trigger = lttng_trigger_create(condition, action);
	if (!trigger) {
		printf("error: Could not create trigger\n");
		ret = 1;
		goto end;
	}

	ret_code = lttng_register_trigger_with_automatic_name(trigger);

	/*
	 * An equivalent trigger might already be registered if an other app
	 * registered an equivalent trigger.
	 */
	if (ret_code != LTTNG_OK && ret_code != LTTNG_ERR_TRIGGER_EXISTS) {
		printf("error: %s\n", lttng_strerror(-ret_code));
		ret = 1;
		goto end;
	}

	nc_status = lttng_notification_channel_subscribe(notification_channel, condition);
	if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
		printf("error: Could not subscribe\n");
		ret = 1;
		goto end;
	}

	/* Tell outside process that the client is ready */
	printf("sync: ready\n");

	for (;;) {
		struct lttng_notification *notification;
		enum lttng_notification_channel_status status;
		const struct lttng_evaluation *notification_evaluation;
		const struct lttng_condition *notification_condition;

		if (nr_notifications == nr_expected_notifications) {
			ret = 0;
			goto end;
		}
		/* Receive the next notification. */
		status = lttng_notification_channel_get_next_notification(notification_channel,
									  &notification);

		switch (status) {
		case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK:
			break;
		case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED:
			ret = 1;
			printf("error: No drop should be observed during this test app\n");
			goto end;
		case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED:
			/*
			 * The notification channel has been closed by the
			 * session daemon. This is typically caused by a session
			 * daemon shutting down (cleanly or because of a crash).
			 */
			printf("error: Notification channel was closed\n");
			ret = 1;
			goto end;
		default:
			/* Unhandled conditions / errors. */
			printf("error: Unknown notification channel status (%d) \n", status);
			ret = 1;
			goto end;
		}

		notification_condition = lttng_notification_get_condition(notification);
		notification_evaluation = lttng_notification_get_evaluation(notification);

		ret = handle_condition(notification_condition, notification_evaluation);
		nr_notifications++;

		lttng_notification_destroy(notification);
		if (ret != 0) {
			goto end;
		}
	}
end:
	if (trigger) {
		lttng_unregister_trigger(trigger);
	}
	if (lttng_notification_channel_unsubscribe(notification_channel, condition)) {
		printf("error: channel unsubscribe error\n");
	}
	lttng_trigger_destroy(trigger);
	lttng_condition_destroy(condition);
	lttng_action_destroy(action);
	lttng_notification_channel_destroy(notification_channel);
	printf("exit: %d\n", ret);
	return ret;
}

int handle_condition(const struct lttng_condition *condition,
		     const struct lttng_evaluation *evaluation)
{
	int ret = 0;
	const char *string_low = "low";
	const char *string_high = "high";
	const char *string_condition_type = NULL;
	const char *condition_session_name = NULL;
	const char *condition_channel_name = NULL;
	enum lttng_condition_type condition_type;
	enum lttng_domain_type condition_domain_type;
	double buffer_usage_ratio;
	uint64_t buffer_usage_bytes;

	condition_type = lttng_condition_get_type(condition);

	if (condition_type != buffer_usage_type) {
		ret = 1;
		printf("error: condition type and buffer usage type are not the same\n");
		goto end;
	}

	/* Fetch info to test */
	ret = lttng_condition_buffer_usage_get_session_name(condition, &condition_session_name);
	if (ret) {
		printf("error: session name could not be fetched\n");
		ret = 1;
		goto end;
	}
	ret = lttng_condition_buffer_usage_get_channel_name(condition, &condition_channel_name);
	if (ret) {
		printf("error: channel name could not be fetched\n");
		ret = 1;
		goto end;
	}
	ret = lttng_condition_buffer_usage_get_domain_type(condition, &condition_domain_type);
	if (ret) {
		printf("error: domain type could not be fetched\n");
		ret = 1;
		goto end;
	}

	if (strcmp(condition_session_name, session_name) != 0) {
		printf("error: session name differs\n");
		ret = 1;
		goto end;
	}

	if (strcmp(condition_channel_name, channel_name) != 0) {
		printf("error: channel name differs\n");
		ret = 1;
		goto end;
	}

	if (condition_domain_type != domain_type) {
		printf("error: domain type differs\n");
		ret = 1;
		goto end;
	}

	if (is_threshold_ratio) {
		lttng_evaluation_buffer_usage_get_usage_ratio(evaluation, &buffer_usage_ratio);
		switch (condition_type) {
		case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
			if (buffer_usage_ratio > threshold_ratio) {
				printf("error: buffer usage ratio is bigger than set threshold ratio\n");
				ret = 1;
				goto end;
			}
			break;
		case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
			if (buffer_usage_ratio < threshold_ratio) {
				printf("error: buffer usage ratio is lower than set threshold ratio\n");
				ret = 1;
				goto end;
			}
			break;
		default:
			printf("error: Unknown condition type\n");
			ret = 1;
			goto end;
		}
	} else {
		lttng_evaluation_buffer_usage_get_usage(evaluation, &buffer_usage_bytes);
		switch (condition_type) {
		case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
			if (buffer_usage_bytes > threshold_bytes) {
				printf("error: buffer usage ratio is bigger than set threshold bytes\n");
				ret = 1;
				goto end;
			}
			break;
		case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
			if (buffer_usage_bytes < threshold_bytes) {
				printf("error: buffer usage ratio is lower than set threshold bytes\n");
				ret = 1;
				goto end;
			}
			break;
		default:
			printf("error: Unknown condition type\n");
			ret = 1;
			goto end;
		}
	}

	switch (condition_type) {
	case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
		string_condition_type = string_low;
		break;
	case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
		string_condition_type = string_high;
		break;
	default:
		printf("error: Unknown condition type\n");
		ret = 1;
		goto end;
	}

	printf("notification: %s %d\n", string_condition_type, nr_notifications);
end:
	return ret;
}