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