Commit | Line | Data |
---|---|---|
db66e574 JD |
1 | /* |
2 | * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com> | |
92816cc3 | 3 | * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
db66e574 JD |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License, version 2 only, as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
16 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
19 | #define _LGPL_SOURCE | |
20 | #include <lttng/trigger/trigger.h> | |
21 | #include <common/error.h> | |
22 | #include <common/config/session-config.h> | |
23 | #include <common/defaults.h> | |
24 | #include <common/utils.h> | |
25 | #include <common/futex.h> | |
26 | #include <common/align.h> | |
27 | #include <common/time.h> | |
28 | #include <common/hashtable/utils.h> | |
29 | #include <common/kernel-ctl/kernel-ctl.h> | |
30 | #include <sys/eventfd.h> | |
31 | #include <sys/stat.h> | |
32 | #include <time.h> | |
33 | #include <signal.h> | |
34 | #include <inttypes.h> | |
35 | ||
90936dcf | 36 | #include <lttng/notification/channel-internal.h> |
d88744a4 JD |
37 | #include <lttng/rotate-internal.h> |
38 | ||
db66e574 JD |
39 | #include "session.h" |
40 | #include "rotate.h" | |
41 | #include "rotation-thread.h" | |
42 | #include "lttng-sessiond.h" | |
43 | #include "health-sessiond.h" | |
44 | #include "cmd.h" | |
45 | #include "utils.h" | |
90936dcf | 46 | #include "notification-thread-commands.h" |
db66e574 JD |
47 | |
48 | #include <urcu.h> | |
49 | #include <urcu/list.h> | |
50 | #include <urcu/rculfhash.h> | |
51 | ||
db66e574 JD |
52 | /* The session's lock must be held by the caller. */ |
53 | static | |
54 | int session_rename_chunk(struct ltt_session *session, char *current_path, | |
55 | char *new_path) | |
56 | { | |
57 | int ret; | |
58 | struct consumer_socket *socket; | |
59 | struct consumer_output *output; | |
60 | struct lttng_ht_iter iter; | |
61 | uid_t uid; | |
62 | gid_t gid; | |
63 | ||
64 | DBG("Renaming session chunk path of session \"%s\" from %s to %s", | |
65 | session->name, current_path, new_path); | |
66 | ||
67 | /* | |
68 | * Either one of the sessions is enough to find the consumer_output | |
69 | * and uid/gid. | |
70 | */ | |
71 | if (session->kernel_session) { | |
72 | output = session->kernel_session->consumer; | |
73 | uid = session->kernel_session->uid; | |
74 | gid = session->kernel_session->gid; | |
75 | } else if (session->ust_session) { | |
76 | output = session->ust_session->consumer; | |
77 | uid = session->ust_session->uid; | |
78 | gid = session->ust_session->gid; | |
79 | } else { | |
80 | assert(0); | |
81 | } | |
82 | ||
83 | if (!output || !output->socks) { | |
84 | ERR("No consumer output found for session \"%s\"", | |
85 | session->name); | |
86 | ret = -1; | |
87 | goto end; | |
88 | } | |
89 | ||
90 | rcu_read_lock(); | |
91 | /* | |
92 | * We have to iterate to find a socket, but we only need to send the | |
93 | * rename command to one consumer, so we break after the first one. | |
94 | */ | |
95 | cds_lfht_for_each_entry(output->socks->ht, &iter.iter, socket, node.node) { | |
96 | pthread_mutex_lock(socket->lock); | |
97 | ret = consumer_rotate_rename(socket, session->id, output, | |
98 | current_path, new_path, uid, gid); | |
99 | pthread_mutex_unlock(socket->lock); | |
100 | if (ret) { | |
101 | ret = -1; | |
102 | goto end_unlock; | |
103 | } | |
104 | break; | |
105 | } | |
106 | ||
107 | ret = 0; | |
108 | ||
109 | end_unlock: | |
110 | rcu_read_unlock(); | |
111 | end: | |
112 | return ret; | |
113 | } | |
114 | ||
115 | /* The session's lock must be held by the caller. */ | |
116 | static | |
117 | int rename_first_chunk(struct ltt_session *session, | |
118 | struct consumer_output *consumer, char *new_path) | |
119 | { | |
120 | int ret; | |
121 | char current_full_path[LTTNG_PATH_MAX], new_full_path[LTTNG_PATH_MAX]; | |
122 | ||
123 | /* Current domain path: <session>/kernel */ | |
124 | if (session->net_handle > 0) { | |
125 | ret = snprintf(current_full_path, sizeof(current_full_path), "%s/%s", | |
126 | consumer->dst.net.base_dir, consumer->subdir); | |
127 | if (ret < 0 || ret >= sizeof(current_full_path)) { | |
128 | ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"", | |
129 | session->name); | |
130 | ret = -1; | |
131 | goto error; | |
132 | } | |
133 | } else { | |
134 | ret = snprintf(current_full_path, sizeof(current_full_path), "%s/%s", | |
135 | consumer->dst.session_root_path, consumer->subdir); | |
136 | if (ret < 0 || ret >= sizeof(current_full_path)) { | |
137 | ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"", | |
138 | session->name); | |
139 | ret = -1; | |
140 | goto error; | |
141 | } | |
142 | } | |
143 | /* New domain path: <session>/<start-date>-<end-date>-<rotate-count>/kernel */ | |
144 | ret = snprintf(new_full_path, sizeof(new_full_path), "%s/%s", | |
145 | new_path, consumer->subdir); | |
146 | if (ret < 0 || ret >= sizeof(new_full_path)) { | |
147 | ERR("Failed to initialize new full path while renaming first rotation chunk of session \"%s\"", | |
148 | session->name); | |
149 | ret = -1; | |
150 | goto error; | |
151 | } | |
152 | /* | |
153 | * Move the per-domain fcurrenter inside the first rotation | |
154 | * fcurrenter. | |
155 | */ | |
156 | ret = session_rename_chunk(session, current_full_path, new_full_path); | |
157 | if (ret < 0) { | |
158 | ret = -LTTNG_ERR_UNK; | |
159 | goto error; | |
160 | } | |
161 | ||
162 | ret = 0; | |
163 | ||
164 | error: | |
165 | return ret; | |
166 | } | |
167 | ||
168 | /* | |
169 | * Rename a chunk folder after a rotation is complete. | |
170 | * session_lock_list and session lock must be held. | |
171 | * | |
172 | * Returns 0 on success, a negative value on error. | |
173 | */ | |
92816cc3 | 174 | int rename_completed_chunk(struct ltt_session *session, time_t ts) |
db66e574 JD |
175 | { |
176 | struct tm *timeinfo; | |
db66e574 | 177 | char new_path[LTTNG_PATH_MAX]; |
fc58be13 | 178 | char datetime[21], start_datetime[21]; |
db66e574 JD |
179 | int ret; |
180 | size_t strf_ret; | |
181 | ||
182 | DBG("Renaming completed chunk for session %s", session->name); | |
183 | timeinfo = localtime(&ts); | |
184 | if (!timeinfo) { | |
185 | ERR("Failed to retrieve local time while renaming completed chunk"); | |
186 | ret = -1; | |
187 | goto end; | |
188 | } | |
fc58be13 JD |
189 | |
190 | strf_ret = strftime(datetime, sizeof(datetime), "%Y%m%dT%H%M%S%z", | |
db66e574 JD |
191 | timeinfo); |
192 | if (strf_ret == 0) { | |
193 | ERR("Failed to format timestamp while renaming completed session chunk"); | |
194 | ret = -1; | |
195 | goto end; | |
196 | } | |
197 | ||
ad9f5c17 | 198 | if (session->current_archive_id == 1) { |
fc58be13 | 199 | char start_time[21]; |
db66e574 JD |
200 | |
201 | timeinfo = localtime(&session->last_chunk_start_ts); | |
202 | if (!timeinfo) { | |
203 | ERR("Failed to retrieve local time while renaming completed chunk"); | |
204 | ret = -1; | |
205 | goto end; | |
206 | } | |
207 | ||
208 | strf_ret = strftime(start_time, sizeof(start_time), | |
fc58be13 | 209 | "%Y%m%dT%H%M%S%z", timeinfo); |
db66e574 JD |
210 | if (strf_ret == 0) { |
211 | ERR("Failed to format timestamp while renaming completed session chunk"); | |
212 | ret = -1; | |
213 | goto end; | |
214 | } | |
215 | ||
216 | /* | |
217 | * On the first rotation, the current_rotate_path is the | |
218 | * session_root_path, so we need to create the chunk folder | |
219 | * and move the domain-specific folders inside it. | |
220 | */ | |
221 | ret = snprintf(new_path, sizeof(new_path), "%s/%s-%s-%" PRIu64, | |
222 | session->rotation_chunk.current_rotate_path, | |
223 | start_time, | |
ad9f5c17 | 224 | datetime, session->current_archive_id); |
db66e574 JD |
225 | if (ret < 0 || ret >= sizeof(new_path)) { |
226 | ERR("Failed to format new chunk path while renaming session \"%s\"'s first chunk", | |
227 | session->name); | |
228 | ret = -1; | |
229 | goto end; | |
230 | } | |
231 | ||
232 | if (session->kernel_session) { | |
233 | ret = rename_first_chunk(session, | |
234 | session->kernel_session->consumer, | |
235 | new_path); | |
236 | if (ret) { | |
237 | ERR("Failed to rename kernel session trace folder to %s", new_path); | |
238 | /* | |
239 | * This is not a fatal error for the rotation | |
240 | * thread, we just need to inform the client | |
241 | * that a problem occurred with the rotation. | |
242 | * Returning 0, same for the other errors | |
243 | * below. | |
244 | */ | |
245 | ret = 0; | |
246 | goto error; | |
247 | } | |
248 | } | |
249 | if (session->ust_session) { | |
250 | ret = rename_first_chunk(session, | |
251 | session->ust_session->consumer, | |
252 | new_path); | |
253 | if (ret) { | |
254 | ERR("Failed to rename userspace session trace folder to %s", new_path); | |
255 | ret = 0; | |
256 | goto error; | |
257 | } | |
258 | } | |
259 | } else { | |
260 | /* | |
261 | * After the first rotation, all the trace data is already in | |
262 | * its own chunk folder, we just need to append the suffix. | |
263 | */ | |
264 | /* Recreate the session->rotation_chunk.current_rotate_path */ | |
265 | timeinfo = localtime(&session->last_chunk_start_ts); | |
266 | if (!timeinfo) { | |
267 | ERR("Failed to retrieve local time while renaming completed chunk"); | |
268 | ret = -1; | |
269 | goto end; | |
270 | } | |
fc58be13 JD |
271 | strf_ret = strftime(start_datetime, sizeof(start_datetime), |
272 | "%Y%m%dT%H%M%S%z", timeinfo); | |
db66e574 JD |
273 | if (!strf_ret) { |
274 | ERR("Failed to format timestamp while renaming completed session chunk"); | |
275 | ret = -1; | |
276 | goto end; | |
277 | } | |
278 | ret = snprintf(new_path, sizeof(new_path), "%s/%s-%s-%" PRIu64, | |
279 | session_get_base_path(session), | |
280 | start_datetime, | |
ad9f5c17 | 281 | datetime, session->current_archive_id); |
db66e574 JD |
282 | if (ret < 0 || ret >= sizeof(new_path)) { |
283 | ERR("Failed to format new chunk path while renaming chunk of session \"%s\"", | |
284 | session->name); | |
285 | ret = -1; | |
286 | goto error; | |
287 | } | |
288 | ret = session_rename_chunk(session, | |
289 | session->rotation_chunk.current_rotate_path, | |
290 | new_path); | |
291 | if (ret) { | |
292 | ERR("Failed to rename session trace folder from %s to %s", | |
293 | session->rotation_chunk.current_rotate_path, | |
294 | new_path); | |
295 | ret = 0; | |
296 | goto error; | |
297 | } | |
298 | } | |
299 | ||
300 | /* | |
301 | * Store the path where the readable chunk is. This path is valid | |
302 | * and can be queried by the client with rotate_pending until the next | |
303 | * rotation is started. | |
304 | */ | |
305 | ret = lttng_strncpy(session->rotation_chunk.current_rotate_path, | |
306 | new_path, | |
307 | sizeof(session->rotation_chunk.current_rotate_path)); | |
308 | if (ret) { | |
309 | ERR("Failed the current chunk's path of session \"%s\"", | |
310 | session->name); | |
311 | ret = -1; | |
312 | goto error; | |
313 | } | |
314 | ||
315 | goto end; | |
316 | ||
317 | error: | |
d68c9a04 | 318 | session->rotation_state = LTTNG_ROTATION_STATE_ERROR; |
db66e574 JD |
319 | end: |
320 | return ret; | |
321 | } | |
d88744a4 | 322 | |
9402c5b9 JG |
323 | int rename_active_chunk(struct ltt_session *session) |
324 | { | |
325 | int ret; | |
326 | ||
327 | session->current_archive_id++; | |
328 | ||
329 | /* | |
330 | * The currently active tracing path is now the folder we | |
331 | * want to rename. | |
332 | */ | |
333 | ret = lttng_strncpy(session->rotation_chunk.current_rotate_path, | |
334 | session->rotation_chunk.active_tracing_path, | |
335 | sizeof(session->rotation_chunk.current_rotate_path)); | |
336 | if (ret) { | |
337 | ERR("Failed to copy active tracing path"); | |
338 | goto end; | |
339 | } | |
340 | ||
341 | ret = rename_completed_chunk(session, time(NULL)); | |
342 | if (ret < 0) { | |
343 | ERR("Failed to rename current rotation's path"); | |
344 | goto end; | |
345 | } | |
346 | ||
347 | /* | |
348 | * We just renamed, the folder, we didn't do an actual rotation, so | |
349 | * the active tracing path is now the renamed folder and we have to | |
350 | * restore the rotate count. | |
351 | */ | |
352 | ret = lttng_strncpy(session->rotation_chunk.active_tracing_path, | |
353 | session->rotation_chunk.current_rotate_path, | |
354 | sizeof(session->rotation_chunk.active_tracing_path)); | |
355 | if (ret) { | |
356 | ERR("Failed to rename active session chunk tracing path"); | |
357 | goto end; | |
358 | } | |
359 | end: | |
360 | session->current_archive_id--; | |
361 | return ret; | |
362 | } | |
363 | ||
90936dcf JD |
364 | int subscribe_session_consumed_size_rotation(struct ltt_session *session, uint64_t size, |
365 | struct notification_thread_handle *notification_thread_handle) | |
366 | { | |
367 | int ret; | |
368 | enum lttng_condition_status condition_status; | |
369 | enum lttng_notification_channel_status nc_status; | |
370 | struct lttng_action *action; | |
371 | ||
372 | session->rotate_condition = lttng_condition_session_consumed_size_create(); | |
373 | if (!session->rotate_condition) { | |
374 | ERR("Failed to create session consumed size condition object"); | |
375 | ret = -1; | |
376 | goto end; | |
377 | } | |
378 | ||
379 | condition_status = lttng_condition_session_consumed_size_set_threshold( | |
380 | session->rotate_condition, size); | |
381 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
382 | ERR("Could not set session consumed size condition threshold (size = %" PRIu64 ")", | |
383 | size); | |
384 | ret = -1; | |
385 | goto end; | |
386 | } | |
387 | ||
388 | condition_status = | |
389 | lttng_condition_session_consumed_size_set_session_name( | |
390 | session->rotate_condition, session->name); | |
391 | if (condition_status != LTTNG_CONDITION_STATUS_OK) { | |
392 | ERR("Could not set session consumed size condition session name (name = %s)", | |
393 | session->name); | |
394 | ret = -1; | |
395 | goto end; | |
396 | } | |
397 | ||
398 | action = lttng_action_notify_create(); | |
399 | if (!action) { | |
400 | ERR("Could not create notify action"); | |
401 | ret = -1; | |
402 | goto end; | |
403 | } | |
404 | ||
405 | session->rotate_trigger = lttng_trigger_create(session->rotate_condition, | |
406 | action); | |
407 | if (!session->rotate_trigger) { | |
408 | ERR("Could not create size-based rotation trigger"); | |
409 | ret = -1; | |
410 | goto end; | |
411 | } | |
412 | ||
413 | nc_status = lttng_notification_channel_subscribe( | |
414 | rotate_notification_channel, session->rotate_condition); | |
415 | if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) { | |
416 | ERR("Could not subscribe to session consumed size notification"); | |
417 | ret = -1; | |
418 | goto end; | |
419 | } | |
420 | ||
421 | ret = notification_thread_command_register_trigger( | |
422 | notification_thread_handle, session->rotate_trigger); | |
423 | if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) { | |
424 | ERR("Register trigger, %s", lttng_strerror(ret)); | |
425 | ret = -1; | |
426 | goto end; | |
427 | } | |
428 | ||
429 | ret = 0; | |
430 | ||
431 | end: | |
432 | return ret; | |
433 | } | |
434 | ||
435 | int unsubscribe_session_consumed_size_rotation(struct ltt_session *session, | |
436 | struct notification_thread_handle *notification_thread_handle) | |
437 | { | |
438 | int ret = 0; | |
439 | enum lttng_notification_channel_status status; | |
440 | ||
441 | status = lttng_notification_channel_unsubscribe( | |
442 | rotate_notification_channel, | |
443 | session->rotate_condition); | |
444 | if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) { | |
445 | ERR("Session unsubscribe error: %d", (int) status); | |
446 | ret = -1; | |
447 | goto end; | |
448 | } | |
449 | ||
450 | ret = notification_thread_command_unregister_trigger( | |
451 | notification_thread_handle, session->rotate_trigger); | |
452 | if (ret != LTTNG_OK) { | |
453 | ERR("Session unregister trigger error: %d", ret); | |
454 | goto end; | |
455 | } | |
456 | ||
457 | ret = 0; | |
458 | end: | |
459 | return ret; | |
460 | } |