Commit | Line | Data |
---|---|---|
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 | ||
95ce9d29 | 34 | #include <lttng/lttng.h> |
28f23191 JG |
35 | |
36 | #include <assert.h> | |
95ce9d29 | 37 | #include <signal.h> |
7779bc96 JD |
38 | #include <stdio.h> |
39 | #include <stdlib.h> | |
40 | #include <string.h> | |
41 | #include <unistd.h> | |
7779bc96 | 42 | |
95ce9d29 | 43 | #define DEFAULT_DATA_AVAILABILITY_WAIT_TIME 200000 /* usec */ |
7779bc96 JD |
44 | |
45 | static volatile int quit = 0; | |
46 | ||
f46376a1 | 47 | static void sighandler(int signal __attribute__((unused))) |
7779bc96 | 48 | { |
5c7248cd | 49 | const char msg[] = "Signal caught, exiting\n"; |
28f23191 | 50 | const int ret = write(STDOUT_FILENO, msg, sizeof(msg)); |
5c7248cd JG |
51 | |
52 | assert(ret == 0); /* NOLINT assert is not async signal safe */ | |
7779bc96 JD |
53 | quit = 1; |
54 | } | |
55 | ||
95ce9d29 | 56 | static int setup_session(const char *session_name, const char *path) |
7779bc96 JD |
57 | { |
58 | int ret; | |
59 | struct lttng_domain dom; | |
60 | struct lttng_event ev; | |
878fea90 | 61 | struct lttng_handle *chan_handle = NULL; |
7779bc96 JD |
62 | |
63 | printf("Creating session %s\n", session_name); | |
64 | ret = lttng_create_session(session_name, path); | |
65 | if (ret) { | |
66 | fprintf(stderr, "Failed to create session, ret = %d\n", ret); | |
67 | goto end; | |
68 | } | |
69 | ||
70 | dom.type = LTTNG_DOMAIN_KERNEL; | |
71 | dom.buf_type = LTTNG_BUFFER_GLOBAL; | |
72 | ||
73 | chan_handle = lttng_create_handle(session_name, &dom); | |
74 | if (chan_handle == NULL) { | |
75 | ret = -1; | |
76 | goto end; | |
77 | } | |
78 | ||
79 | memset(&ev, 0, sizeof(ev)); | |
80 | ev.type = LTTNG_EVENT_SYSCALL; | |
81 | strcpy(ev.name, "*"); | |
82 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
83 | ||
28f23191 | 84 | ret = lttng_enable_event_with_exclusions(chan_handle, &ev, "mychan", NULL, 0, NULL); |
7779bc96 | 85 | if (ret < 0) { |
61e4fc8e | 86 | fprintf(stderr, "Failed to enable events (ret = %i)\n", ret); |
7779bc96 JD |
87 | goto end; |
88 | } | |
89 | printf("Enabled all system call kernel events\n"); | |
90 | ||
91 | ret = lttng_start_tracing(session_name); | |
92 | if (ret < 0) { | |
93 | fprintf(stderr, "Failed to start tracing\n"); | |
94 | goto end; | |
95 | } | |
96 | ||
7779bc96 JD |
97 | ret = 0; |
98 | ||
99 | end: | |
878fea90 | 100 | lttng_destroy_handle(chan_handle); |
7779bc96 JD |
101 | return ret; |
102 | } | |
103 | ||
95ce9d29 | 104 | static int cleanup_session(const char *session_name) |
7779bc96 JD |
105 | { |
106 | int ret; | |
107 | ||
108 | printf("Stopping session %s", session_name); | |
109 | ret = lttng_stop_tracing_no_wait(session_name); | |
110 | if (ret) { | |
111 | fprintf(stderr, "Failed to stop tracing\n"); | |
112 | goto end; | |
113 | } | |
114 | ||
115 | fflush(stdout); | |
116 | do { | |
117 | ret = lttng_data_pending(session_name); | |
118 | if (ret < 0) { | |
119 | /* Return the data available call error. */ | |
120 | goto end; | |
121 | } | |
122 | ||
123 | /* | |
95ce9d29 JR |
124 | * Data sleep time before retrying (in usec). Don't sleep if the |
125 | * call returned value indicates availability. | |
7779bc96 JD |
126 | */ |
127 | if (ret) { | |
128 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); | |
129 | printf("."); | |
130 | fflush(stdout); | |
131 | } | |
132 | } while (ret != 0); | |
133 | printf("\n"); | |
134 | ||
135 | printf("Destroying session %s\n", session_name); | |
136 | ret = lttng_destroy_session(session_name); | |
137 | if (ret) { | |
138 | fprintf(stderr, "Failed to destroy the session\n"); | |
139 | goto end; | |
140 | } | |
141 | ||
142 | ret = 0; | |
143 | ||
144 | end: | |
145 | return ret; | |
146 | } | |
147 | ||
95ce9d29 | 148 | static int rotate_session(const char *session_name, const char *ext_program) |
7779bc96 JD |
149 | { |
150 | int ret; | |
7779bc96 JD |
151 | struct lttng_rotation_handle *handle = NULL; |
152 | enum lttng_rotation_status rotation_status; | |
61e4fc8e | 153 | enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING; |
7779bc96 JD |
154 | char cmd[PATH_MAX]; |
155 | ||
7779bc96 JD |
156 | printf("Rotating the output files of session %s", session_name); |
157 | ||
61e4fc8e | 158 | ret = lttng_rotate_session(session_name, NULL, &handle); |
7779bc96 | 159 | if (ret < 0) { |
28f23191 | 160 | fprintf(stderr, "Failed to rotate session, %s\n", lttng_strerror(ret)); |
7779bc96 JD |
161 | goto end; |
162 | } | |
163 | ||
164 | fflush(stdout); | |
61e4fc8e | 165 | |
7779bc96 | 166 | do { |
28f23191 | 167 | rotation_status = lttng_rotation_handle_get_state(handle, &rotation_state); |
61e4fc8e JG |
168 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { |
169 | ret = -1; | |
170 | fprintf(stderr, "Failed to get the current rotation's state\n"); | |
7779bc96 JD |
171 | goto end; |
172 | } | |
173 | ||
174 | /* | |
61e4fc8e JG |
175 | * Data sleep time before retrying (in usec). Don't |
176 | * sleep if the call returned value indicates | |
177 | * availability. | |
7779bc96 | 178 | */ |
61e4fc8e | 179 | if (rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
7779bc96 JD |
180 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); |
181 | printf("."); | |
182 | fflush(stdout); | |
183 | } | |
61e4fc8e | 184 | } while (rotation_state == LTTNG_ROTATION_STATE_ONGOING); |
7779bc96 JD |
185 | printf("\n"); |
186 | ||
61e4fc8e JG |
187 | switch (rotation_state) { |
188 | case LTTNG_ROTATION_STATE_COMPLETED: | |
189 | { | |
190 | const struct lttng_trace_archive_location *location; | |
191 | const char *absolute_path; | |
192 | enum lttng_trace_archive_location_status location_status; | |
193 | ||
28f23191 | 194 | rotation_status = lttng_rotation_handle_get_archive_location(handle, &location); |
61e4fc8e | 195 | if (rotation_status != LTTNG_ROTATION_STATUS_OK) { |
28f23191 JG |
196 | fprintf(stderr, |
197 | "Failed to retrieve the rotation's completed chunk archive location\n"); | |
61e4fc8e JG |
198 | ret = -1; |
199 | goto end; | |
200 | } | |
201 | ||
28f23191 JG |
202 | location_status = lttng_trace_archive_location_local_get_absolute_path( |
203 | location, &absolute_path); | |
61e4fc8e JG |
204 | if (location_status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) { |
205 | fprintf(stderr, "Failed to get absolute path of completed chunk archive"); | |
206 | ret = -1; | |
207 | goto end; | |
208 | } | |
209 | ||
28f23191 JG |
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); | |
7779bc96 JD |
212 | if (ret < 0) { |
213 | fprintf(stderr, "Failed to prepare command string\n"); | |
214 | goto end; | |
215 | } | |
216 | ret = system(cmd); | |
217 | goto end; | |
61e4fc8e JG |
218 | } |
219 | case LTTNG_ROTATION_STATE_EXPIRED: | |
95ce9d29 | 220 | printf("Output files of session %s rotated, but the handle expired\n", |
28f23191 | 221 | session_name); |
7779bc96 JD |
222 | ret = 0; |
223 | goto end; | |
61e4fc8e | 224 | case LTTNG_ROTATION_STATE_ERROR: |
28f23191 JG |
225 | fprintf(stderr, |
226 | "An error occurred with the rotation of session %s\n", | |
227 | session_name); | |
95ce9d29 JR |
228 | ret = -1; |
229 | goto end; | |
230 | case LTTNG_ROTATION_STATE_ONGOING: | |
231 | abort(); | |
232 | goto end; | |
233 | case LTTNG_ROTATION_STATE_NO_ROTATION: | |
28f23191 JG |
234 | fprintf(stderr, |
235 | "No rotation was performed on rotation request for session %s\n", | |
236 | session_name); | |
7779bc96 JD |
237 | ret = -1; |
238 | goto end; | |
239 | } | |
240 | ||
241 | end: | |
242 | lttng_rotation_handle_destroy(handle); | |
7779bc96 JD |
243 | return ret; |
244 | } | |
245 | ||
95ce9d29 | 246 | static int cleanup_dir(const char *path) |
7779bc96 JD |
247 | { |
248 | char cmd[PATH_MAX]; | |
249 | int ret; | |
250 | ||
251 | ret = snprintf(cmd, PATH_MAX, "rm -rf %s", path); | |
252 | if (ret < 0) { | |
253 | fprintf(stderr, "Failed to prepare rm -rf command string\n"); | |
254 | goto end; | |
255 | } | |
256 | ret = system(cmd); | |
257 | ||
258 | end: | |
259 | return ret; | |
260 | } | |
261 | ||
95ce9d29 | 262 | static void usage(const char *prog_name) |
7779bc96 | 263 | { |
28f23191 | 264 | fprintf(stderr, "Usage: %s <session-name> <delay-sec> <nr-rotate> <program>\n", prog_name); |
7779bc96 JD |
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"); | |
95ce9d29 | 267 | fprintf(stderr, |
28f23191 JG |
268 | " <nr-rotate>: the number of rotation you want to perform, " |
269 | "-1 for infinite until ctrl-c\n"); | |
95ce9d29 | 270 | fprintf(stderr, |
28f23191 JG |
271 | " <program>: program to run on each chunk, it must be " |
272 | "executable, and expect a trace folder as only argument\n"); | |
7779bc96 JD |
273 | fprintf(stderr, "\nThe trace folder is deleted when this program completes.\n"); |
274 | } | |
275 | ||
276 | int main(int argc, char **argv) | |
277 | { | |
278 | int ret; | |
279 | char tmppath[] = "/tmp/lttng-rotate-XXXXXX"; | |
280 | char *session_name, *path, *ext_program; | |
281 | int delay, nr; | |
282 | ||
283 | if (argc != 5) { | |
284 | usage(argv[0]); | |
285 | ret = -1; | |
286 | goto end; | |
287 | } | |
288 | ||
289 | session_name = argv[1]; | |
290 | delay = atoi(argv[2]); | |
291 | nr = atoi(argv[3]); | |
292 | ext_program = argv[4]; | |
293 | ||
294 | if (delay < 0) { | |
295 | fprintf(stderr, "delay-sec must be a positive values\n"); | |
296 | ret = -1; | |
297 | goto end; | |
298 | } | |
299 | ||
95ce9d29 JR |
300 | if (signal(SIGINT, sighandler) == SIG_ERR) { |
301 | ret = -1; | |
7779bc96 JD |
302 | perror("signal handler"); |
303 | goto end; | |
304 | } | |
305 | ||
306 | path = mkdtemp(tmppath); | |
307 | if (!path) { | |
308 | fprintf(stderr, "Failed to create temporary path\n"); | |
309 | } | |
310 | ||
311 | printf("Output directory: %s\n", path); | |
312 | ||
313 | ret = setup_session(session_name, path); | |
314 | if (ret) { | |
315 | goto end_cleanup_dir; | |
316 | } | |
317 | ||
318 | if (nr > 0) { | |
319 | unsigned int sleep_time; | |
320 | int i; | |
321 | ||
322 | for (i = 0; i < nr; i++) { | |
323 | ret = rotate_session(session_name, ext_program); | |
324 | if (ret) { | |
325 | goto end_cleanup; | |
326 | } | |
327 | sleep_time = delay; | |
328 | while (sleep_time > 0) { | |
329 | sleep_time = sleep(sleep_time); | |
330 | } | |
331 | } | |
332 | } else { | |
95ce9d29 | 333 | for (;;) { |
7779bc96 JD |
334 | if (quit) { |
335 | break; | |
336 | } | |
337 | ret = rotate_session(session_name, ext_program); | |
338 | if (ret) { | |
339 | goto end_cleanup; | |
340 | } | |
341 | sleep(delay); | |
342 | } | |
343 | } | |
344 | ||
345 | end_cleanup: | |
346 | ret = cleanup_session(session_name); | |
347 | if (ret) { | |
348 | goto end; | |
349 | } | |
350 | end_cleanup_dir: | |
351 | ret = cleanup_dir(path); | |
352 | end: | |
353 | return ret; | |
354 | } |