Sessiond timer thread
[lttng-tools.git] / src / bin / lttng-sessiond / rotation-thread.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 <sys/eventfd.h>
29#include <sys/stat.h>
30#include <time.h>
31#include <signal.h>
32#include <inttypes.h>
33
34#include <common/kernel-ctl/kernel-ctl.h>
35#include <lttng/notification/channel-internal.h>
36
37#include "rotation-thread.h"
38#include "lttng-sessiond.h"
39#include "health-sessiond.h"
40#include "rotate.h"
41#include "cmd.h"
42#include "session.h"
d086f507 43#include "sessiond-timer.h"
db66e574
JD
44
45#include <urcu.h>
46#include <urcu/list.h>
47#include <urcu/rculfhash.h>
48
49/*
50 * Store a struct rotation_channel_info for each channel that is currently
51 * being rotated by the consumer.
52 */
53struct cds_lfht *channel_pending_rotate_ht;
54
55struct rotation_thread_state {
56 struct lttng_poll_event events;
57};
58
59static
60void channel_rotation_info_destroy(struct rotation_channel_info *channel_info)
61{
62 assert(channel_info);
63 free(channel_info);
64}
65
66static
67int match_channel_info(struct cds_lfht_node *node, const void *key)
68{
69 struct rotation_channel_key *channel_key = (struct rotation_channel_key *) key;
70 struct rotation_channel_info *channel_info;
71
72 channel_info = caa_container_of(node, struct rotation_channel_info,
73 rotate_channels_ht_node);
74
75 return !!((channel_key->key == channel_info->channel_key.key) &&
76 (channel_key->domain == channel_info->channel_key.domain));
77}
78
79static
80struct rotation_channel_info *lookup_channel_pending(uint64_t key,
81 enum lttng_domain_type domain)
82{
83 struct cds_lfht_iter iter;
84 struct cds_lfht_node *node;
85 struct rotation_channel_info *channel_info = NULL;
86 struct rotation_channel_key channel_key = { .key = key,
87 .domain = domain };
88
89 cds_lfht_lookup(channel_pending_rotate_ht,
90 hash_channel_key(&channel_key),
91 match_channel_info,
92 &channel_key, &iter);
93 node = cds_lfht_iter_get_node(&iter);
94 if (!node) {
95 goto end;
96 }
97
98 channel_info = caa_container_of(node, struct rotation_channel_info,
99 rotate_channels_ht_node);
100 cds_lfht_del(channel_pending_rotate_ht, node);
101end:
102 return channel_info;
103}
104
105/*
106 * Destroy the thread data previously created by the init function.
107 */
108void rotation_thread_handle_destroy(
109 struct rotation_thread_handle *handle)
110{
111 int ret;
112
113 if (!handle) {
114 goto end;
115 }
116
117 if (handle->ust32_consumer >= 0) {
118 ret = close(handle->ust32_consumer);
119 if (ret) {
120 PERROR("close 32-bit consumer channel rotation pipe");
121 }
122 }
123 if (handle->ust64_consumer >= 0) {
124 ret = close(handle->ust64_consumer);
125 if (ret) {
126 PERROR("close 64-bit consumer channel rotation pipe");
127 }
128 }
129 if (handle->kernel_consumer >= 0) {
130 ret = close(handle->kernel_consumer);
131 if (ret) {
132 PERROR("close kernel consumer channel rotation pipe");
133 }
134 }
135
136end:
137 free(handle);
138}
139
140struct rotation_thread_handle *rotation_thread_handle_create(
141 struct lttng_pipe *ust32_channel_rotate_pipe,
142 struct lttng_pipe *ust64_channel_rotate_pipe,
143 struct lttng_pipe *kernel_channel_rotate_pipe,
d086f507
JD
144 int thread_quit_pipe,
145 struct rotation_thread_timer_queue *rotation_timer_queue)
db66e574
JD
146{
147 struct rotation_thread_handle *handle;
148
149 handle = zmalloc(sizeof(*handle));
150 if (!handle) {
151 goto end;
152 }
153
154 if (ust32_channel_rotate_pipe) {
155 handle->ust32_consumer =
156 lttng_pipe_release_readfd(
157 ust32_channel_rotate_pipe);
158 if (handle->ust32_consumer < 0) {
159 goto error;
160 }
161 } else {
162 handle->ust32_consumer = -1;
163 }
164 if (ust64_channel_rotate_pipe) {
165 handle->ust64_consumer =
166 lttng_pipe_release_readfd(
167 ust64_channel_rotate_pipe);
168 if (handle->ust64_consumer < 0) {
169 goto error;
170 }
171 } else {
172 handle->ust64_consumer = -1;
173 }
174 if (kernel_channel_rotate_pipe) {
175 handle->kernel_consumer =
176 lttng_pipe_release_readfd(
177 kernel_channel_rotate_pipe);
178 if (handle->kernel_consumer < 0) {
179 goto error;
180 }
181 } else {
182 handle->kernel_consumer = -1;
183 }
184 handle->thread_quit_pipe = thread_quit_pipe;
d086f507 185 handle->rotation_timer_queue = rotation_timer_queue;
db66e574
JD
186
187end:
188 return handle;
189error:
190 rotation_thread_handle_destroy(handle);
191 return NULL;
192}
193
194static
195int init_poll_set(struct lttng_poll_event *poll_set,
196 struct rotation_thread_handle *handle)
197{
198 int ret;
199
200 /*
d086f507 201 * Create pollset with size 5:
db66e574 202 * - sessiond quit pipe
d086f507 203 * - sessiond timer pipe,
db66e574
JD
204 * - consumerd (32-bit user space) channel rotate pipe,
205 * - consumerd (64-bit user space) channel rotate pipe,
206 * - consumerd (kernel) channel rotate pipe,
207 */
d086f507 208 ret = lttng_poll_create(poll_set, 5, LTTNG_CLOEXEC);
db66e574
JD
209 if (ret < 0) {
210 goto end;
211 }
212
213 ret = lttng_poll_add(poll_set, handle->thread_quit_pipe,
214 LPOLLIN | LPOLLERR);
215 if (ret < 0) {
216 ERR("[rotation-thread] Failed to add thread_quit_pipe fd to pollset");
217 goto error;
218 }
d086f507
JD
219 ret = lttng_poll_add(poll_set,
220 lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe),
221 LPOLLIN | LPOLLERR);
222 if (ret < 0) {
223 ERR("[rotation-thread] Failed to add rotate_pending fd to pollset");
224 goto error;
225 }
db66e574
JD
226 ret = lttng_poll_add(poll_set, handle->ust32_consumer,
227 LPOLLIN | LPOLLERR);
228 if (ret < 0) {
229 ERR("[rotation-thread] Failed to add ust-32 channel rotation pipe fd to pollset");
230 goto error;
231 }
232 ret = lttng_poll_add(poll_set, handle->ust64_consumer,
233 LPOLLIN | LPOLLERR);
234 if (ret < 0) {
235 ERR("[rotation-thread] Failed to add ust-64 channel rotation pipe fd to pollset");
236 goto error;
237 }
238 if (handle->kernel_consumer >= 0) {
239 ret = lttng_poll_add(poll_set, handle->kernel_consumer,
240 LPOLLIN | LPOLLERR);
241 if (ret < 0) {
242 ERR("[rotation-thread] Failed to add kernel channel rotation pipe fd to pollset");
243 goto error;
244 }
245 }
246
247end:
248 return ret;
249error:
250 lttng_poll_clean(poll_set);
251 return ret;
252}
253
254static
255void fini_thread_state(struct rotation_thread_state *state)
256{
257 lttng_poll_clean(&state->events);
258 cds_lfht_destroy(channel_pending_rotate_ht, NULL);
259}
260
261static
262int init_thread_state(struct rotation_thread_handle *handle,
263 struct rotation_thread_state *state)
264{
265 int ret;
266
267 memset(state, 0, sizeof(*state));
268 lttng_poll_init(&state->events);
269
270 ret = init_poll_set(&state->events, handle);
271 if (ret) {
272 ERR("[rotation-thread] Failed to initialize rotation thread poll set");
273 goto end;
274 }
275
276 channel_pending_rotate_ht = cds_lfht_new(DEFAULT_HT_SIZE,
277 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
278 if (!channel_pending_rotate_ht) {
279 ERR("[rotation-thread] Failed to create channel pending rotation hash table");
280 ret = -1;
281 goto end;
282 }
283
284end:
285 return ret;
286}
287
288static
289int handle_channel_rotation_pipe(int fd, uint32_t revents,
290 struct rotation_thread_handle *handle,
291 struct rotation_thread_state *state)
292{
293 int ret = 0;
294 enum lttng_domain_type domain;
295 struct rotation_channel_info *channel_info;
296 struct ltt_session *session = NULL;
297 uint64_t key;
298
299 if (fd == handle->ust32_consumer ||
300 fd == handle->ust64_consumer) {
301 domain = LTTNG_DOMAIN_UST;
302 } else if (fd == handle->kernel_consumer) {
303 domain = LTTNG_DOMAIN_KERNEL;
304 } else {
305 ERR("[rotation-thread] Unknown channel rotation pipe fd %d",
306 fd);
307 abort();
308 }
309
310 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
311 ret = lttng_poll_del(&state->events, fd);
312 if (ret) {
313 ERR("[rotation-thread] Failed to remove consumer "
314 "rotation pipe from poll set");
315 }
316 goto end;
317 }
318
319 do {
320 ret = read(fd, &key, sizeof(key));
321 } while (ret == -1 && errno == EINTR);
322 if (ret != sizeof(key)) {
323 ERR("[rotation-thread] Failed to read from pipe (fd = %i)",
324 fd);
325 ret = -1;
326 goto end;
327 }
328
329 DBG("[rotation-thread] Received notification for chan %" PRIu64
330 ", domain %d\n", key, domain);
331
332 channel_info = lookup_channel_pending(key, domain);
333 if (!channel_info) {
334 ERR("[rotation-thread] Failed to find channel_info (key = %"
335 PRIu64 ")", key);
336 ret = -1;
337 goto end;
338 }
339 rcu_read_lock();
340 session_lock_list();
341 session = session_find_by_id(channel_info->session_id);
342 if (!session) {
343 /*
344 * The session may have been destroyed before we had a chance to
345 * perform this action, return gracefully.
346 */
347 DBG("[rotation-thread] Session %" PRIu64 " not found",
348 channel_info->session_id);
349 ret = 0;
350 goto end_unlock_session_list;
351 }
352
353 session_lock(session);
354 if (--session->nr_chan_rotate_pending == 0) {
355 time_t now = time(NULL);
356
357 if (now == (time_t) -1) {
358 session->rotation_status = LTTNG_ROTATION_STATUS_ERROR;
359 ret = LTTNG_ERR_UNK;
360 goto end_unlock_session;
361 }
362
363 ret = rename_complete_chunk(session, now);
364 if (ret < 0) {
365 ERR("Failed to rename completed rotation chunk");
366 goto end_unlock_session;
367 }
368 session->rotate_pending = false;
369 session->rotation_status = LTTNG_ROTATION_STATUS_COMPLETED;
370 session->last_chunk_start_ts = session->current_chunk_start_ts;
371 DBG("Rotation completed for session %s", session->name);
372 }
373
374 ret = 0;
375
376end_unlock_session:
377 channel_rotation_info_destroy(channel_info);
378 session_unlock(session);
379end_unlock_session_list:
380 session_unlock_list();
381 rcu_read_unlock();
382end:
383 return ret;
384}
385
386void *thread_rotation(void *data)
387{
388 int ret;
389 struct rotation_thread_handle *handle = data;
390 struct rotation_thread_state state;
391
392 DBG("[rotation-thread] Started rotation thread");
393
394 if (!handle) {
395 ERR("[rotation-thread] Invalid thread context provided");
396 goto end;
397 }
398
399 rcu_register_thread();
400 rcu_thread_online();
401
402 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION);
403 health_code_update();
404
405 ret = init_thread_state(handle, &state);
406 if (ret) {
407 goto end;
408 }
409
410 /* Ready to handle client connections. */
411 sessiond_notify_ready();
412
413 while (true) {
414 int fd_count, i;
415
416 health_poll_entry();
417 DBG("[rotation-thread] Entering poll wait");
418 ret = lttng_poll_wait(&state.events, -1);
419 DBG("[rotation-thread] Poll wait returned (%i)", ret);
420 health_poll_exit();
421 if (ret < 0) {
422 /*
423 * Restart interrupted system call.
424 */
425 if (errno == EINTR) {
426 continue;
427 }
428 ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret);
429 goto error;
430 }
431
432 fd_count = ret;
433 for (i = 0; i < fd_count; i++) {
434 int fd = LTTNG_POLL_GETFD(&state.events, i);
435 uint32_t revents = LTTNG_POLL_GETEV(&state.events, i);
436
437 DBG("[rotation-thread] Handling fd (%i) activity (%u)",
438 fd, revents);
439
440 if (fd == handle->thread_quit_pipe) {
441 DBG("[rotation-thread] Quit pipe activity");
442 goto exit;
443 } else if (fd == handle->ust32_consumer ||
444 fd == handle->ust64_consumer ||
445 fd == handle->kernel_consumer) {
446 ret = handle_channel_rotation_pipe(fd,
447 revents, handle, &state);
448 if (ret) {
449 ERR("[rotation-thread] Handle channel rotation pipe");
450 goto error;
451 }
452 }
453 }
454 }
455exit:
456error:
457 DBG("[rotation-thread] Exit");
458 fini_thread_state(&state);
459 health_unregister(health_sessiond);
460 rcu_thread_offline();
461 rcu_unregister_thread();
462end:
463 return NULL;
464}
This page took 0.040816 seconds and 5 git commands to generate.