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
34 #include <lttng/lttng.h>
43 #define DEFAULT_DATA_AVAILABILITY_WAIT_TIME 200000 /* usec */
45 static volatile int quit
= 0;
47 static void sighandler(int signal
__attribute__((unused
)))
49 const char msg
[] = "Signal caught, exiting\n";
50 const int ret
= write(STDOUT_FILENO
, msg
, sizeof(msg
));
52 assert(ret
== 0); /* NOLINT assert is not async signal safe */
56 static int setup_session(const char *session_name
, const char *path
)
59 struct lttng_domain dom
;
60 struct lttng_event ev
;
61 struct lttng_handle
*chan_handle
= NULL
;
63 printf("Creating session %s\n", session_name
);
64 ret
= lttng_create_session(session_name
, path
);
66 fprintf(stderr
, "Failed to create session, ret = %d\n", ret
);
70 dom
.type
= LTTNG_DOMAIN_KERNEL
;
71 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
73 chan_handle
= lttng_create_handle(session_name
, &dom
);
74 if (chan_handle
== NULL
) {
79 memset(&ev
, 0, sizeof(ev
));
80 ev
.type
= LTTNG_EVENT_SYSCALL
;
82 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
84 ret
= lttng_enable_event_with_exclusions(chan_handle
, &ev
, "mychan", NULL
, 0, NULL
);
86 fprintf(stderr
, "Failed to enable events (ret = %i)\n", ret
);
89 printf("Enabled all system call kernel events\n");
91 ret
= lttng_start_tracing(session_name
);
93 fprintf(stderr
, "Failed to start tracing\n");
100 lttng_destroy_handle(chan_handle
);
104 static int cleanup_session(const char *session_name
)
108 printf("Stopping session %s", session_name
);
109 ret
= lttng_stop_tracing_no_wait(session_name
);
111 fprintf(stderr
, "Failed to stop tracing\n");
117 ret
= lttng_data_pending(session_name
);
119 /* Return the data available call error. */
124 * Data sleep time before retrying (in usec). Don't sleep if the
125 * call returned value indicates availability.
128 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
135 printf("Destroying session %s\n", session_name
);
136 ret
= lttng_destroy_session(session_name
);
138 fprintf(stderr
, "Failed to destroy the session\n");
148 static int rotate_session(const char *session_name
, const char *ext_program
)
151 struct lttng_rotation_handle
*handle
= NULL
;
152 enum lttng_rotation_status rotation_status
;
153 enum lttng_rotation_state rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
156 printf("Rotating the output files of session %s", session_name
);
158 ret
= lttng_rotate_session(session_name
, NULL
, &handle
);
160 fprintf(stderr
, "Failed to rotate session, %s\n", lttng_strerror(ret
));
167 rotation_status
= lttng_rotation_handle_get_state(handle
, &rotation_state
);
168 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
170 fprintf(stderr
, "Failed to get the current rotation's state\n");
175 * Data sleep time before retrying (in usec). Don't
176 * sleep if the call returned value indicates
179 if (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
180 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME
);
184 } while (rotation_state
== LTTNG_ROTATION_STATE_ONGOING
);
187 switch (rotation_state
) {
188 case LTTNG_ROTATION_STATE_COMPLETED
:
190 const struct lttng_trace_archive_location
*location
;
191 const char *absolute_path
;
192 enum lttng_trace_archive_location_status location_status
;
194 rotation_status
= lttng_rotation_handle_get_archive_location(handle
, &location
);
195 if (rotation_status
!= LTTNG_ROTATION_STATUS_OK
) {
197 "Failed to retrieve the rotation's completed chunk archive location\n");
202 location_status
= lttng_trace_archive_location_local_get_absolute_path(
203 location
, &absolute_path
);
204 if (location_status
!= LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK
) {
205 fprintf(stderr
, "Failed to get absolute path of completed chunk archive");
210 printf("Output files of session %s rotated to %s\n", session_name
, absolute_path
);
211 ret
= snprintf(cmd
, PATH_MAX
, "%s %s", ext_program
, absolute_path
);
213 fprintf(stderr
, "Failed to prepare command string\n");
219 case LTTNG_ROTATION_STATE_EXPIRED
:
220 printf("Output files of session %s rotated, but the handle expired\n",
224 case LTTNG_ROTATION_STATE_ERROR
:
226 "An error occurred with the rotation of session %s\n",
230 case LTTNG_ROTATION_STATE_ONGOING
:
233 case LTTNG_ROTATION_STATE_NO_ROTATION
:
235 "No rotation was performed on rotation request for session %s\n",
242 lttng_rotation_handle_destroy(handle
);
246 static int cleanup_dir(const char *path
)
251 ret
= snprintf(cmd
, PATH_MAX
, "rm -rf %s", path
);
253 fprintf(stderr
, "Failed to prepare rm -rf command string\n");
262 static void usage(const char *prog_name
)
264 fprintf(stderr
, "Usage: %s <session-name> <delay-sec> <nr-rotate> <program>\n", prog_name
);
265 fprintf(stderr
, " <session-name>: the name of the session you want to create\n");
266 fprintf(stderr
, " <delay-sec>: the delay in seconds between each rotation\n");
268 " <nr-rotate>: the number of rotation you want to perform, "
269 "-1 for infinite until ctrl-c\n");
271 " <program>: program to run on each chunk, it must be "
272 "executable, and expect a trace folder as only argument\n");
273 fprintf(stderr
, "\nThe trace folder is deleted when this program completes.\n");
276 int main(int argc
, char **argv
)
279 char tmppath
[] = "/tmp/lttng-rotate-XXXXXX";
280 char *session_name
, *path
, *ext_program
;
289 session_name
= argv
[1];
290 delay
= atoi(argv
[2]);
292 ext_program
= argv
[4];
295 fprintf(stderr
, "delay-sec must be a positive values\n");
300 if (signal(SIGINT
, sighandler
) == SIG_ERR
) {
302 perror("signal handler");
306 path
= mkdtemp(tmppath
);
308 fprintf(stderr
, "Failed to create temporary path\n");
311 printf("Output directory: %s\n", path
);
313 ret
= setup_session(session_name
, path
);
315 goto end_cleanup_dir
;
319 unsigned int sleep_time
;
322 for (i
= 0; i
< nr
; i
++) {
323 ret
= rotate_session(session_name
, ext_program
);
328 while (sleep_time
> 0) {
329 sleep_time
= sleep(sleep_time
);
337 ret
= rotate_session(session_name
, ext_program
);
346 ret
= cleanup_session(session_name
);
351 ret
= cleanup_dir(path
);