2 * Session rotation example control application
4 * Copyright 2017, Julien Desfossez <jdesfossez@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
27 * gcc -o rotate-client rotate-client-example.c -llttng-ctl
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
39 #include <lttng/lttng.h>
41 #define DEFAULT_DATA_AVAILABILITY_WAIT_TIME 200000 /* usec */
43 /* Uncomment to enable debug output. */
46 #define printf(fmt, ...) (0)
49 static volatile int quit
= 0;
52 void sighandler(int signal
)
54 printf("Signal caught, exiting\n");
59 int setup_session(const char *session_name
, const char *path
)
62 struct lttng_domain dom
;
63 struct lttng_event ev
;
64 struct lttng_handle
*chan_handle
;
66 printf("Creating session %s\n", session_name
);
67 ret
= lttng_create_session(session_name
, path
);
69 fprintf(stderr
, "Failed to create session, ret = %d\n", ret
);
73 dom
.type
= LTTNG_DOMAIN_KERNEL
;
74 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
76 chan_handle
= lttng_create_handle(session_name
, &dom
);
77 if (chan_handle
== NULL
) {
82 memset(&ev
, 0, sizeof(ev
));
83 ev
.type
= LTTNG_EVENT_SYSCALL
;
85 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
87 ret
= lttng_enable_event_with_exclusions(chan_handle
, &ev
, "mychan", NULL
,
90 fprintf(stderr
, "Failed to enable events\n");
93 printf("Enabled all system call kernel events\n");
95 ret
= lttng_start_tracing(session_name
);
97 fprintf(stderr
, "Failed to start tracing\n");
101 lttng_destroy_handle(chan_handle
);
110 int cleanup_session(const char *session_name
)
114 printf("Stopping session %s", session_name
);
115 ret
= lttng_stop_tracing_no_wait(session_name
);
117 fprintf(stderr
, "Failed to stop tracing\n");
123 ret
= lttng_data_pending(session_name
);
125 /* Return the data available call error. */
130 * Data sleep time before retrying (in usec). Don't sleep if the call
131 * returned value indicates availability.
134 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
141 printf("Destroying session %s\n", session_name
);
142 ret
= lttng_destroy_session(session_name
);
144 fprintf(stderr
, "Failed to destroy the session\n");
155 int rotate_session(const char *session_name
, const char *ext_program
)
159 struct lttng_rotation_manual_attr
*attr
= NULL
;
160 struct lttng_rotation_handle
*handle
= NULL
;
161 enum lttng_rotation_status rotation_status
;
164 attr
= lttng_rotation_manual_attr_create();
166 fprintf(stderr
, "Failed to create rotate attr\n");
171 ret
= lttng_rotation_manual_attr_set_session_name(attr
, session_name
);
173 fprintf(stderr
, "Failed to set rotate attr session name\n");
177 printf("Rotating the output files of session %s", session_name
);
179 ret
= lttng_rotate_session(attr
, &handle
);
181 fprintf(stderr
, "Failed to rotate session, %s\n", lttng_strerror(ret
));
187 ret
= lttng_rotation_is_pending(handle
);
189 fprintf(stderr
, "Rotate pending failed\n");
194 * Data sleep time before retrying (in usec). Don't sleep if the call
195 * returned value indicates availability.
198 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
205 rotation_status
= lttng_rotation_handle_get_status(handle
);
206 switch(rotation_status
) {
207 case LTTNG_ROTATION_STATUS_COMPLETED
:
208 lttng_rotation_handle_get_output_path(handle
, &path
);
209 printf("Output files of session %s rotated to %s\n", session_name
, path
);
210 ret
= snprintf(cmd
, PATH_MAX
, "%s %s", ext_program
, path
);
212 fprintf(stderr
, "Failed to prepare command string\n");
217 case LTTNG_ROTATION_STATUS_STARTED
:
218 /* Should not happen after a rotate_pending. */
219 printf("Rotation started for session %s\n", session_name
);
222 case LTTNG_ROTATION_STATUS_EXPIRED
:
223 printf("Output files of session %s rotated, but handle expired", session_name
);
226 case LTTNG_ROTATION_STATUS_ERROR
:
227 fprintf(stderr
, "An error occurred with the rotation of session %s", session_name
);
233 lttng_rotation_handle_destroy(handle
);
234 lttng_rotation_manual_attr_destroy(attr
);
239 int cleanup_dir(const char *path
)
244 ret
= snprintf(cmd
, PATH_MAX
, "rm -rf %s", path
);
246 fprintf(stderr
, "Failed to prepare rm -rf command string\n");
255 void usage(const char *prog_name
)
257 fprintf(stderr
, "Usage: %s <session-name> <delay-sec> <nr-rotate> <program>\n",
259 fprintf(stderr
, " <session-name>: the name of the session you want to create\n");
260 fprintf(stderr
, " <delay-sec>: the delay in seconds between each rotation\n");
261 fprintf(stderr
, " <nr-rotate>: the number of rotation you want to perform, "
262 "-1 for infinite until ctrl-c\n");
263 fprintf(stderr
, " <program>: program to run on each chunk, it must be "
264 "executable, and expect a trace folder as only argument\n");
265 fprintf(stderr
, "\nThe trace folder is deleted when this program completes.\n");
268 int main(int argc
, char **argv
)
271 char tmppath
[] = "/tmp/lttng-rotate-XXXXXX";
272 char *session_name
, *path
, *ext_program
;
281 session_name
= argv
[1];
282 delay
= atoi(argv
[2]);
284 ext_program
= argv
[4];
287 fprintf(stderr
, "delay-sec must be a positive values\n");
292 if (signal(SIGINT
, sighandler
) == SIG_ERR
) {
293 perror("signal handler");
297 path
= mkdtemp(tmppath
);
299 fprintf(stderr
, "Failed to create temporary path\n");
302 printf("Output directory: %s\n", path
);
304 ret
= setup_session(session_name
, path
);
306 goto end_cleanup_dir
;
310 unsigned int sleep_time
;
313 for (i
= 0; i
< nr
; i
++) {
314 ret
= rotate_session(session_name
, ext_program
);
319 while (sleep_time
> 0) {
320 sleep_time
= sleep(sleep_time
);
328 ret
= rotate_session(session_name
, ext_program
);
337 ret
= cleanup_session(session_name
);
342 ret
= cleanup_dir(path
);