Fix: rotation may never complete in per-PID buffering mode
[lttng-tools.git] / src / bin / lttng-sessiond / rotate.c
1 /*
2 * Copyright (C) 2017 - Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
36 #include <lttng/notification/channel-internal.h>
37 #include <lttng/rotate-internal.h>
38
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"
46 #include "notification-thread-commands.h"
47
48 #include <urcu.h>
49 #include <urcu/list.h>
50 #include <urcu/rculfhash.h>
51
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 */
174 int rename_completed_chunk(struct ltt_session *session, time_t ts)
175 {
176 struct tm *timeinfo;
177 char new_path[LTTNG_PATH_MAX];
178 char datetime[21], start_datetime[21];
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 }
189
190 strf_ret = strftime(datetime, sizeof(datetime), "%Y%m%dT%H%M%S%z",
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
198 if (session->current_archive_id == 1) {
199 char start_time[21];
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),
209 "%Y%m%dT%H%M%S%z", timeinfo);
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,
224 datetime, session->current_archive_id);
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 }
271 strf_ret = strftime(start_datetime, sizeof(start_datetime),
272 "%Y%m%dT%H%M%S%z", timeinfo);
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,
281 datetime, session->current_archive_id);
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:
318 session->rotation_state = LTTNG_ROTATION_STATE_ERROR;
319 end:
320 return ret;
321 }
322
323 int subscribe_session_consumed_size_rotation(struct ltt_session *session, uint64_t size,
324 struct notification_thread_handle *notification_thread_handle)
325 {
326 int ret;
327 enum lttng_condition_status condition_status;
328 enum lttng_notification_channel_status nc_status;
329 struct lttng_action *action;
330
331 session->rotate_condition = lttng_condition_session_consumed_size_create();
332 if (!session->rotate_condition) {
333 ERR("Failed to create session consumed size condition object");
334 ret = -1;
335 goto end;
336 }
337
338 condition_status = lttng_condition_session_consumed_size_set_threshold(
339 session->rotate_condition, size);
340 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
341 ERR("Could not set session consumed size condition threshold (size = %" PRIu64 ")",
342 size);
343 ret = -1;
344 goto end;
345 }
346
347 condition_status =
348 lttng_condition_session_consumed_size_set_session_name(
349 session->rotate_condition, session->name);
350 if (condition_status != LTTNG_CONDITION_STATUS_OK) {
351 ERR("Could not set session consumed size condition session name (name = %s)",
352 session->name);
353 ret = -1;
354 goto end;
355 }
356
357 action = lttng_action_notify_create();
358 if (!action) {
359 ERR("Could not create notify action");
360 ret = -1;
361 goto end;
362 }
363
364 session->rotate_trigger = lttng_trigger_create(session->rotate_condition,
365 action);
366 if (!session->rotate_trigger) {
367 ERR("Could not create size-based rotation trigger");
368 ret = -1;
369 goto end;
370 }
371
372 nc_status = lttng_notification_channel_subscribe(
373 rotate_notification_channel, session->rotate_condition);
374 if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
375 ERR("Could not subscribe to session consumed size notification");
376 ret = -1;
377 goto end;
378 }
379
380 ret = notification_thread_command_register_trigger(
381 notification_thread_handle, session->rotate_trigger);
382 if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) {
383 ERR("Register trigger, %s", lttng_strerror(ret));
384 ret = -1;
385 goto end;
386 }
387
388 ret = 0;
389
390 end:
391 return ret;
392 }
393
394 int unsubscribe_session_consumed_size_rotation(struct ltt_session *session,
395 struct notification_thread_handle *notification_thread_handle)
396 {
397 int ret = 0;
398 enum lttng_notification_channel_status status;
399
400 status = lttng_notification_channel_unsubscribe(
401 rotate_notification_channel,
402 session->rotate_condition);
403 if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
404 ERR("Session unsubscribe error: %d", (int) status);
405 ret = -1;
406 goto end;
407 }
408
409 ret = notification_thread_command_unregister_trigger(
410 notification_thread_handle, session->rotate_trigger);
411 if (ret != LTTNG_OK) {
412 ERR("Session unregister trigger error: %d", ret);
413 goto end;
414 }
415
416 ret = 0;
417 end:
418 return ret;
419 }
This page took 0.038433 seconds and 5 git commands to generate.