Sessiond timer thread
[lttng-tools.git] / src / bin / lttng-sessiond / rotate.c
CommitLineData
db66e574
JD
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 "session.h"
36#include "rotate.h"
37#include "rotation-thread.h"
38#include "lttng-sessiond.h"
39#include "health-sessiond.h"
40#include "cmd.h"
41#include "utils.h"
42
43#include <urcu.h>
44#include <urcu/list.h>
45#include <urcu/rculfhash.h>
46
47unsigned long hash_channel_key(struct rotation_channel_key *key)
48{
49 return hash_key_u64(&key->key, lttng_ht_seed) ^ hash_key_ulong(
50 (void *) (unsigned long) key->domain, lttng_ht_seed);
51}
52
53/* The session's lock must be held by the caller. */
54static
55int session_rename_chunk(struct ltt_session *session, char *current_path,
56 char *new_path)
57{
58 int ret;
59 struct consumer_socket *socket;
60 struct consumer_output *output;
61 struct lttng_ht_iter iter;
62 uid_t uid;
63 gid_t gid;
64
65 DBG("Renaming session chunk path of session \"%s\" from %s to %s",
66 session->name, current_path, new_path);
67
68 /*
69 * Either one of the sessions is enough to find the consumer_output
70 * and uid/gid.
71 */
72 if (session->kernel_session) {
73 output = session->kernel_session->consumer;
74 uid = session->kernel_session->uid;
75 gid = session->kernel_session->gid;
76 } else if (session->ust_session) {
77 output = session->ust_session->consumer;
78 uid = session->ust_session->uid;
79 gid = session->ust_session->gid;
80 } else {
81 assert(0);
82 }
83
84 if (!output || !output->socks) {
85 ERR("No consumer output found for session \"%s\"",
86 session->name);
87 ret = -1;
88 goto end;
89 }
90
91 rcu_read_lock();
92 /*
93 * We have to iterate to find a socket, but we only need to send the
94 * rename command to one consumer, so we break after the first one.
95 */
96 cds_lfht_for_each_entry(output->socks->ht, &iter.iter, socket, node.node) {
97 pthread_mutex_lock(socket->lock);
98 ret = consumer_rotate_rename(socket, session->id, output,
99 current_path, new_path, uid, gid);
100 pthread_mutex_unlock(socket->lock);
101 if (ret) {
102 ret = -1;
103 goto end_unlock;
104 }
105 break;
106 }
107
108 ret = 0;
109
110end_unlock:
111 rcu_read_unlock();
112end:
113 return ret;
114}
115
116/* The session's lock must be held by the caller. */
117static
118int rename_first_chunk(struct ltt_session *session,
119 struct consumer_output *consumer, char *new_path)
120{
121 int ret;
122 char current_full_path[LTTNG_PATH_MAX], new_full_path[LTTNG_PATH_MAX];
123
124 /* Current domain path: <session>/kernel */
125 if (session->net_handle > 0) {
126 ret = snprintf(current_full_path, sizeof(current_full_path), "%s/%s",
127 consumer->dst.net.base_dir, consumer->subdir);
128 if (ret < 0 || ret >= sizeof(current_full_path)) {
129 ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"",
130 session->name);
131 ret = -1;
132 goto error;
133 }
134 } else {
135 ret = snprintf(current_full_path, sizeof(current_full_path), "%s/%s",
136 consumer->dst.session_root_path, consumer->subdir);
137 if (ret < 0 || ret >= sizeof(current_full_path)) {
138 ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"",
139 session->name);
140 ret = -1;
141 goto error;
142 }
143 }
144 /* New domain path: <session>/<start-date>-<end-date>-<rotate-count>/kernel */
145 ret = snprintf(new_full_path, sizeof(new_full_path), "%s/%s",
146 new_path, consumer->subdir);
147 if (ret < 0 || ret >= sizeof(new_full_path)) {
148 ERR("Failed to initialize new full path while renaming first rotation chunk of session \"%s\"",
149 session->name);
150 ret = -1;
151 goto error;
152 }
153 /*
154 * Move the per-domain fcurrenter inside the first rotation
155 * fcurrenter.
156 */
157 ret = session_rename_chunk(session, current_full_path, new_full_path);
158 if (ret < 0) {
159 ret = -LTTNG_ERR_UNK;
160 goto error;
161 }
162
163 ret = 0;
164
165error:
166 return ret;
167}
168
169/*
170 * Rename a chunk folder after a rotation is complete.
171 * session_lock_list and session lock must be held.
172 *
173 * Returns 0 on success, a negative value on error.
174 */
175int rename_complete_chunk(struct ltt_session *session, time_t ts)
176{
177 struct tm *timeinfo;
178 char datetime[16], start_datetime[16];
179 char new_path[LTTNG_PATH_MAX];
180 int ret;
181 size_t strf_ret;
182
183 DBG("Renaming completed chunk for session %s", session->name);
184 timeinfo = localtime(&ts);
185 if (!timeinfo) {
186 ERR("Failed to retrieve local time while renaming completed chunk");
187 ret = -1;
188 goto end;
189 }
190 strf_ret = strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S",
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->rotate_count == 1) {
199 char start_time[16];
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%d-%H%M%S", 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->rotate_count);
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), "%Y%m%d-%H%M%S", timeinfo);
272 if (!strf_ret) {
273 ERR("Failed to format timestamp while renaming completed session chunk");
274 ret = -1;
275 goto end;
276 }
277 ret = snprintf(new_path, sizeof(new_path), "%s/%s-%s-%" PRIu64,
278 session_get_base_path(session),
279 start_datetime,
280 datetime, session->rotate_count);
281 if (ret < 0 || ret >= sizeof(new_path)) {
282 ERR("Failed to format new chunk path while renaming chunk of session \"%s\"",
283 session->name);
284 ret = -1;
285 goto error;
286 }
287 ret = session_rename_chunk(session,
288 session->rotation_chunk.current_rotate_path,
289 new_path);
290 if (ret) {
291 ERR("Failed to rename session trace folder from %s to %s",
292 session->rotation_chunk.current_rotate_path,
293 new_path);
294 ret = 0;
295 goto error;
296 }
297 }
298
299 /*
300 * Store the path where the readable chunk is. This path is valid
301 * and can be queried by the client with rotate_pending until the next
302 * rotation is started.
303 */
304 ret = lttng_strncpy(session->rotation_chunk.current_rotate_path,
305 new_path,
306 sizeof(session->rotation_chunk.current_rotate_path));
307 if (ret) {
308 ERR("Failed the current chunk's path of session \"%s\"",
309 session->name);
310 ret = -1;
311 goto error;
312 }
313
314 goto end;
315
316error:
317 session->rotation_status = LTTNG_ROTATION_STATUS_ERROR;
318end:
319 return ret;
320}
This page took 0.053818 seconds and 5 git commands to generate.