File: rotate-client-example.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 (354 lines) | stat: -rw-r--r-- 8,885 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
/*
 * Session rotation example control application
 *
 * Copyright 2017, Julien Desfossez <jdesfossez@efficios.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * Compile with:
 *     gcc -o rotate-client rotate-client-example.c -llttng-ctl
 *
 * Run with the following command to rotate the session every second and
 * compress the chunk, until ctrl-c:
 *     ./rotate-client mysession 1 -1 ./rotate-client-compress.sh
 */

#include <lttng/lttng.h>

#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define DEFAULT_DATA_AVAILABILITY_WAIT_TIME 200000 /* usec */

static volatile int quit = 0;

static void sighandler(int signal __attribute__((unused)))
{
	const char msg[] = "Signal caught, exiting\n";
	const int ret = write(STDOUT_FILENO, msg, sizeof(msg));

	assert(ret == 0); /* NOLINT assert is not async signal safe */
	quit = 1;
}

static int setup_session(const char *session_name, const char *path)
{
	int ret;
	struct lttng_domain dom;
	struct lttng_event ev;
	struct lttng_handle *chan_handle = NULL;

	printf("Creating session %s\n", session_name);
	ret = lttng_create_session(session_name, path);
	if (ret) {
		fprintf(stderr, "Failed to create session, ret = %d\n", ret);
		goto end;
	}

	dom.type = LTTNG_DOMAIN_KERNEL;
	dom.buf_type = LTTNG_BUFFER_GLOBAL;

	chan_handle = lttng_create_handle(session_name, &dom);
	if (chan_handle == NULL) {
		ret = -1;
		goto end;
	}

	memset(&ev, 0, sizeof(ev));
	ev.type = LTTNG_EVENT_SYSCALL;
	strcpy(ev.name, "*");
	ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;

	ret = lttng_enable_event_with_exclusions(chan_handle, &ev, "mychan", NULL, 0, NULL);
	if (ret < 0) {
		fprintf(stderr, "Failed to enable events (ret = %i)\n", ret);
		goto end;
	}
	printf("Enabled all system call kernel events\n");

	ret = lttng_start_tracing(session_name);
	if (ret < 0) {
		fprintf(stderr, "Failed to start tracing\n");
		goto end;
	}

	ret = 0;

end:
	lttng_destroy_handle(chan_handle);
	return ret;
}

static int cleanup_session(const char *session_name)
{
	int ret;

	printf("Stopping session %s", session_name);
	ret = lttng_stop_tracing_no_wait(session_name);
	if (ret) {
		fprintf(stderr, "Failed to stop tracing\n");
		goto end;
	}

	fflush(stdout);
	do {
		ret = lttng_data_pending(session_name);
		if (ret < 0) {
			/* Return the data available call error. */
			goto end;
		}

		/*
		 * Data sleep time before retrying (in usec). Don't sleep if the
		 * call returned value indicates availability.
		 */
		if (ret) {
			usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
			printf(".");
			fflush(stdout);
		}
	} while (ret != 0);
	printf("\n");

	printf("Destroying session %s\n", session_name);
	ret = lttng_destroy_session(session_name);
	if (ret) {
		fprintf(stderr, "Failed to destroy the session\n");
		goto end;
	}

	ret = 0;

end:
	return ret;
}

static int rotate_session(const char *session_name, const char *ext_program)
{
	int ret;
	struct lttng_rotation_handle *handle = NULL;
	enum lttng_rotation_status rotation_status;
	enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING;
	char cmd[PATH_MAX];

	printf("Rotating the output files of session %s", session_name);

	ret = lttng_rotate_session(session_name, NULL, &handle);
	if (ret < 0) {
		fprintf(stderr, "Failed to rotate session, %s\n", lttng_strerror(ret));
		goto end;
	}

	fflush(stdout);

	do {
		rotation_status = lttng_rotation_handle_get_state(handle, &rotation_state);
		if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
			ret = -1;
			fprintf(stderr, "Failed to get the current rotation's state\n");
			goto end;
		}

		/*
		 * Data sleep time before retrying (in usec). Don't
		 * sleep if the call returned value indicates
		 * availability.
		 */
		if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
			usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
			printf(".");
			fflush(stdout);
		}
	} while (rotation_state == LTTNG_ROTATION_STATE_ONGOING);
	printf("\n");

	switch (rotation_state) {
	case LTTNG_ROTATION_STATE_COMPLETED:
	{
		const struct lttng_trace_archive_location *location;
		const char *absolute_path;
		enum lttng_trace_archive_location_status location_status;

		rotation_status = lttng_rotation_handle_get_archive_location(handle, &location);
		if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
			fprintf(stderr,
				"Failed to retrieve the rotation's completed chunk archive location\n");
			ret = -1;
			goto end;
		}

		location_status = lttng_trace_archive_location_local_get_absolute_path(
			location, &absolute_path);
		if (location_status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
			fprintf(stderr, "Failed to get absolute path of completed chunk archive");
			ret = -1;
			goto end;
		}

		printf("Output files of session %s rotated to %s\n", session_name, absolute_path);
		ret = snprintf(cmd, PATH_MAX, "%s %s", ext_program, absolute_path);
		if (ret < 0) {
			fprintf(stderr, "Failed to prepare command string\n");
			goto end;
		}
		ret = system(cmd);
		goto end;
	}
	case LTTNG_ROTATION_STATE_EXPIRED:
		printf("Output files of session %s rotated, but the handle expired\n",
		       session_name);
		ret = 0;
		goto end;
	case LTTNG_ROTATION_STATE_ERROR:
		fprintf(stderr,
			"An error occurred with the rotation of session %s\n",
			session_name);
		ret = -1;
		goto end;
	case LTTNG_ROTATION_STATE_ONGOING:
		abort();
		goto end;
	case LTTNG_ROTATION_STATE_NO_ROTATION:
		fprintf(stderr,
			"No rotation was performed on rotation request for session %s\n",
			session_name);
		ret = -1;
		goto end;
	}

end:
	lttng_rotation_handle_destroy(handle);
	return ret;
}

static int cleanup_dir(const char *path)
{
	char cmd[PATH_MAX];
	int ret;

	ret = snprintf(cmd, PATH_MAX, "rm -rf %s", path);
	if (ret < 0) {
		fprintf(stderr, "Failed to prepare rm -rf command string\n");
		goto end;
	}
	ret = system(cmd);

end:
	return ret;
}

static void usage(const char *prog_name)
{
	fprintf(stderr, "Usage: %s <session-name> <delay-sec> <nr-rotate> <program>\n", prog_name);
	fprintf(stderr, "  <session-name>: the name of the session you want to create\n");
	fprintf(stderr, "  <delay-sec>: the delay in seconds between each rotation\n");
	fprintf(stderr,
		"  <nr-rotate>: the number of rotation you want to perform, "
		"-1 for infinite until ctrl-c\n");
	fprintf(stderr,
		"  <program>: program to run on each chunk, it must be "
		"executable, and expect a trace folder as only argument\n");
	fprintf(stderr, "\nThe trace folder is deleted when this program completes.\n");
}

int main(int argc, char **argv)
{
	int ret;
	char tmppath[] = "/tmp/lttng-rotate-XXXXXX";
	char *session_name, *path, *ext_program;
	int delay, nr;

	if (argc != 5) {
		usage(argv[0]);
		ret = -1;
		goto end;
	}

	session_name = argv[1];
	delay = atoi(argv[2]);
	nr = atoi(argv[3]);
	ext_program = argv[4];

	if (delay < 0) {
		fprintf(stderr, "delay-sec must be a positive values\n");
		ret = -1;
		goto end;
	}

	if (signal(SIGINT, sighandler) == SIG_ERR) {
		ret = -1;
		perror("signal handler");
		goto end;
	}

	path = mkdtemp(tmppath);
	if (!path) {
		fprintf(stderr, "Failed to create temporary path\n");
	}

	printf("Output directory: %s\n", path);

	ret = setup_session(session_name, path);
	if (ret) {
		goto end_cleanup_dir;
	}

	if (nr > 0) {
		unsigned int sleep_time;
		int i;

		for (i = 0; i < nr; i++) {
			ret = rotate_session(session_name, ext_program);
			if (ret) {
				goto end_cleanup;
			}
			sleep_time = delay;
			while (sleep_time > 0) {
				sleep_time = sleep(sleep_time);
			}
		}
	} else {
		for (;;) {
			if (quit) {
				break;
			}
			ret = rotate_session(session_name, ext_program);
			if (ret) {
				goto end_cleanup;
			}
			sleep(delay);
		}
	}

end_cleanup:
	ret = cleanup_session(session_name);
	if (ret) {
		goto end;
	}
end_cleanup_dir:
	ret = cleanup_dir(path);
end:
	return ret;
}