lttng-sessiond(8): sort the option list by long option name
[lttng-tools.git] / doc / examples / rotation / rotate-client-example.c
CommitLineData
7779bc96
JD
1/*
2 * Session rotation example control application
3 *
4 * Copyright 2017, Julien Desfossez <jdesfossez@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25/*
26 * Compile with:
27 * gcc -o rotate-client rotate-client-example.c -llttng-ctl
28 *
29 * Run with the following command to rotate the session every second and
30 * compress the chunk, until ctrl-c:
31 * ./rotate-client mysession 1 -1 ./rotate-client-compress.sh
32 */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38#include <signal.h>
39#include <lttng/lttng.h>
40
41#define DEFAULT_DATA_AVAILABILITY_WAIT_TIME 200000 /* usec */
42
43/* Uncomment to enable debug output. */
44//#define DEBUG
45#ifndef DEBUG
46#define printf(fmt, ...) (0)
47#endif
48
49static volatile int quit = 0;
50
51static
52void sighandler(int signal)
53{
54 printf("Signal caught, exiting\n");
55 quit = 1;
56}
57
58static
59int setup_session(const char *session_name, const char *path)
60{
61 int ret;
62 struct lttng_domain dom;
63 struct lttng_event ev;
64 struct lttng_handle *chan_handle;
65
66 printf("Creating session %s\n", session_name);
67 ret = lttng_create_session(session_name, path);
68 if (ret) {
69 fprintf(stderr, "Failed to create session, ret = %d\n", ret);
70 goto end;
71 }
72
73 dom.type = LTTNG_DOMAIN_KERNEL;
74 dom.buf_type = LTTNG_BUFFER_GLOBAL;
75
76 chan_handle = lttng_create_handle(session_name, &dom);
77 if (chan_handle == NULL) {
78 ret = -1;
79 goto end;
80 }
81
82 memset(&ev, 0, sizeof(ev));
83 ev.type = LTTNG_EVENT_SYSCALL;
84 strcpy(ev.name, "*");
85 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
86
87 ret = lttng_enable_event_with_exclusions(chan_handle, &ev, "mychan", NULL,
88 0, NULL);
89 if (ret < 0) {
61e4fc8e 90 fprintf(stderr, "Failed to enable events (ret = %i)\n", ret);
7779bc96
JD
91 goto end;
92 }
93 printf("Enabled all system call kernel events\n");
94
95 ret = lttng_start_tracing(session_name);
96 if (ret < 0) {
97 fprintf(stderr, "Failed to start tracing\n");
98 goto end;
99 }
100
101 lttng_destroy_handle(chan_handle);
102
103 ret = 0;
104
105end:
106 return ret;
107}
108
109static
110int cleanup_session(const char *session_name)
111{
112 int ret;
113
114 printf("Stopping session %s", session_name);
115 ret = lttng_stop_tracing_no_wait(session_name);
116 if (ret) {
117 fprintf(stderr, "Failed to stop tracing\n");
118 goto end;
119 }
120
121 fflush(stdout);
122 do {
123 ret = lttng_data_pending(session_name);
124 if (ret < 0) {
125 /* Return the data available call error. */
126 goto end;
127 }
128
129 /*
130 * Data sleep time before retrying (in usec). Don't sleep if the call
131 * returned value indicates availability.
132 */
133 if (ret) {
134 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
135 printf(".");
136 fflush(stdout);
137 }
138 } while (ret != 0);
139 printf("\n");
140
141 printf("Destroying session %s\n", session_name);
142 ret = lttng_destroy_session(session_name);
143 if (ret) {
144 fprintf(stderr, "Failed to destroy the session\n");
145 goto end;
146 }
147
148 ret = 0;
149
150end:
151 return ret;
152}
153
154static
155int rotate_session(const char *session_name, const char *ext_program)
156{
157 int ret;
7779bc96
JD
158 struct lttng_rotation_handle *handle = NULL;
159 enum lttng_rotation_status rotation_status;
61e4fc8e 160 enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING;
7779bc96
JD
161 char cmd[PATH_MAX];
162
7779bc96
JD
163 printf("Rotating the output files of session %s", session_name);
164
61e4fc8e 165 ret = lttng_rotate_session(session_name, NULL, &handle);
7779bc96
JD
166 if (ret < 0) {
167 fprintf(stderr, "Failed to rotate session, %s\n", lttng_strerror(ret));
168 goto end;
169 }
170
171 fflush(stdout);
61e4fc8e 172
7779bc96 173 do {
61e4fc8e
JG
174 rotation_status = lttng_rotation_handle_get_state(handle,
175 &rotation_state);
176 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
177 ret = -1;
178 fprintf(stderr, "Failed to get the current rotation's state\n");
7779bc96
JD
179 goto end;
180 }
181
182 /*
61e4fc8e
JG
183 * Data sleep time before retrying (in usec). Don't
184 * sleep if the call returned value indicates
185 * availability.
7779bc96 186 */
61e4fc8e 187 if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
7779bc96
JD
188 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
189 printf(".");
190 fflush(stdout);
191 }
61e4fc8e 192 } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING);
7779bc96
JD
193 printf("\n");
194
61e4fc8e
JG
195 switch (rotation_state) {
196 case LTTNG_ROTATION_STATE_COMPLETED:
197 {
198 const struct lttng_trace_archive_location *location;
199 const char *absolute_path;
200 enum lttng_trace_archive_location_status location_status;
201
202 rotation_status = lttng_rotation_handle_get_archive_location(
203 handle, &location);
204 if (rotation_status != LTTNG_ROTATION_STATUS_OK) {
205 fprintf(stderr, "Failed to retrieve the rotation's completed chunk archive location\n");
206 ret = -1;
207 goto end;
208 }
209
210 location_status = lttng_trace_archive_location_local_get_absolute_path(
211 location, &absolute_path);
212 if (location_status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
213 fprintf(stderr, "Failed to get absolute path of completed chunk archive");
214 ret = -1;
215 goto end;
216 }
217
218 printf("Output files of session %s rotated to %s\n",
219 session_name, absolute_path);
220 ret = snprintf(cmd, PATH_MAX, "%s %s", ext_program, absolute_path);
7779bc96
JD
221 if (ret < 0) {
222 fprintf(stderr, "Failed to prepare command string\n");
223 goto end;
224 }
225 ret = system(cmd);
226 goto end;
61e4fc8e
JG
227 }
228 case LTTNG_ROTATION_STATE_EXPIRED:
229 printf("Output files of session %s rotated, but the handle expired\n", session_name);
7779bc96
JD
230 ret = 0;
231 goto end;
61e4fc8e
JG
232 case LTTNG_ROTATION_STATE_ERROR:
233 fprintf(stderr, "An error occurred with the rotation of session %s\n", session_name);
7779bc96
JD
234 ret = -1;
235 goto end;
236 }
237
238end:
239 lttng_rotation_handle_destroy(handle);
7779bc96
JD
240 return ret;
241}
242
243static
244int cleanup_dir(const char *path)
245{
246 char cmd[PATH_MAX];
247 int ret;
248
249 ret = snprintf(cmd, PATH_MAX, "rm -rf %s", path);
250 if (ret < 0) {
251 fprintf(stderr, "Failed to prepare rm -rf command string\n");
252 goto end;
253 }
254 ret = system(cmd);
255
256end:
257 return ret;
258}
259
260void usage(const char *prog_name)
261{
262 fprintf(stderr, "Usage: %s <session-name> <delay-sec> <nr-rotate> <program>\n",
263 prog_name);
264 fprintf(stderr, " <session-name>: the name of the session you want to create\n");
265 fprintf(stderr, " <delay-sec>: the delay in seconds between each rotation\n");
266 fprintf(stderr, " <nr-rotate>: the number of rotation you want to perform, "
267 "-1 for infinite until ctrl-c\n");
268 fprintf(stderr, " <program>: program to run on each chunk, it must be "
269 "executable, and expect a trace folder as only argument\n");
270 fprintf(stderr, "\nThe trace folder is deleted when this program completes.\n");
271}
272
273int main(int argc, char **argv)
274{
275 int ret;
276 char tmppath[] = "/tmp/lttng-rotate-XXXXXX";
277 char *session_name, *path, *ext_program;
278 int delay, nr;
279
280 if (argc != 5) {
281 usage(argv[0]);
282 ret = -1;
283 goto end;
284 }
285
286 session_name = argv[1];
287 delay = atoi(argv[2]);
288 nr = atoi(argv[3]);
289 ext_program = argv[4];
290
291 if (delay < 0) {
292 fprintf(stderr, "delay-sec must be a positive values\n");
293 ret = -1;
294 goto end;
295 }
296
297 if (signal(SIGINT, sighandler) == SIG_ERR) {
298 perror("signal handler");
299 goto end;
300 }
301
302 path = mkdtemp(tmppath);
303 if (!path) {
304 fprintf(stderr, "Failed to create temporary path\n");
305 }
306
307 printf("Output directory: %s\n", path);
308
309 ret = setup_session(session_name, path);
310 if (ret) {
311 goto end_cleanup_dir;
312 }
313
314 if (nr > 0) {
315 unsigned int sleep_time;
316 int i;
317
318 for (i = 0; i < nr; i++) {
319 ret = rotate_session(session_name, ext_program);
320 if (ret) {
321 goto end_cleanup;
322 }
323 sleep_time = delay;
324 while (sleep_time > 0) {
325 sleep_time = sleep(sleep_time);
326 }
327 }
328 } else {
329 for(;;) {
330 if (quit) {
331 break;
332 }
333 ret = rotate_session(session_name, ext_program);
334 if (ret) {
335 goto end_cleanup;
336 }
337 sleep(delay);
338 }
339 }
340
341end_cleanup:
342 ret = cleanup_session(session_name);
343 if (ret) {
344 goto end;
345 }
346end_cleanup_dir:
347 ret = cleanup_dir(path);
348end:
349 return ret;
350}
This page took 0.058106 seconds and 4 git commands to generate.