Add lttng_notification_channel_has_pending_notification()
[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>
5c408ad8 36#include <lttng/rotate-internal.h>
db66e574
JD
37
38#include "rotation-thread.h"
39#include "lttng-sessiond.h"
40#include "health-sessiond.h"
41#include "rotate.h"
42#include "cmd.h"
43#include "session.h"
d086f507 44#include "sessiond-timer.h"
db66e574
JD
45
46#include <urcu.h>
47#include <urcu/list.h>
48#include <urcu/rculfhash.h>
49
50/*
51 * Store a struct rotation_channel_info for each channel that is currently
52 * being rotated by the consumer.
53 */
54struct cds_lfht *channel_pending_rotate_ht;
55
56struct rotation_thread_state {
57 struct lttng_poll_event events;
58};
59
60static
61void channel_rotation_info_destroy(struct rotation_channel_info *channel_info)
62{
63 assert(channel_info);
64 free(channel_info);
65}
66
67static
68int match_channel_info(struct cds_lfht_node *node, const void *key)
69{
70 struct rotation_channel_key *channel_key = (struct rotation_channel_key *) key;
71 struct rotation_channel_info *channel_info;
72
73 channel_info = caa_container_of(node, struct rotation_channel_info,
74 rotate_channels_ht_node);
75
76 return !!((channel_key->key == channel_info->channel_key.key) &&
77 (channel_key->domain == channel_info->channel_key.domain));
78}
79
80static
81struct rotation_channel_info *lookup_channel_pending(uint64_t key,
82 enum lttng_domain_type domain)
83{
84 struct cds_lfht_iter iter;
85 struct cds_lfht_node *node;
86 struct rotation_channel_info *channel_info = NULL;
87 struct rotation_channel_key channel_key = { .key = key,
88 .domain = domain };
89
90 cds_lfht_lookup(channel_pending_rotate_ht,
91 hash_channel_key(&channel_key),
92 match_channel_info,
93 &channel_key, &iter);
94 node = cds_lfht_iter_get_node(&iter);
95 if (!node) {
96 goto end;
97 }
98
99 channel_info = caa_container_of(node, struct rotation_channel_info,
100 rotate_channels_ht_node);
101 cds_lfht_del(channel_pending_rotate_ht, node);
102end:
103 return channel_info;
104}
105
106/*
107 * Destroy the thread data previously created by the init function.
108 */
109void rotation_thread_handle_destroy(
110 struct rotation_thread_handle *handle)
111{
112 int ret;
113
114 if (!handle) {
115 goto end;
116 }
117
118 if (handle->ust32_consumer >= 0) {
119 ret = close(handle->ust32_consumer);
120 if (ret) {
121 PERROR("close 32-bit consumer channel rotation pipe");
122 }
123 }
124 if (handle->ust64_consumer >= 0) {
125 ret = close(handle->ust64_consumer);
126 if (ret) {
127 PERROR("close 64-bit consumer channel rotation pipe");
128 }
129 }
130 if (handle->kernel_consumer >= 0) {
131 ret = close(handle->kernel_consumer);
132 if (ret) {
133 PERROR("close kernel consumer channel rotation pipe");
134 }
135 }
136
137end:
138 free(handle);
139}
140
141struct rotation_thread_handle *rotation_thread_handle_create(
142 struct lttng_pipe *ust32_channel_rotate_pipe,
143 struct lttng_pipe *ust64_channel_rotate_pipe,
144 struct lttng_pipe *kernel_channel_rotate_pipe,
d086f507
JD
145 int thread_quit_pipe,
146 struct rotation_thread_timer_queue *rotation_timer_queue)
db66e574
JD
147{
148 struct rotation_thread_handle *handle;
149
150 handle = zmalloc(sizeof(*handle));
151 if (!handle) {
152 goto end;
153 }
154
155 if (ust32_channel_rotate_pipe) {
156 handle->ust32_consumer =
157 lttng_pipe_release_readfd(
158 ust32_channel_rotate_pipe);
159 if (handle->ust32_consumer < 0) {
160 goto error;
161 }
162 } else {
163 handle->ust32_consumer = -1;
164 }
165 if (ust64_channel_rotate_pipe) {
166 handle->ust64_consumer =
167 lttng_pipe_release_readfd(
168 ust64_channel_rotate_pipe);
169 if (handle->ust64_consumer < 0) {
170 goto error;
171 }
172 } else {
173 handle->ust64_consumer = -1;
174 }
175 if (kernel_channel_rotate_pipe) {
176 handle->kernel_consumer =
177 lttng_pipe_release_readfd(
178 kernel_channel_rotate_pipe);
179 if (handle->kernel_consumer < 0) {
180 goto error;
181 }
182 } else {
183 handle->kernel_consumer = -1;
184 }
185 handle->thread_quit_pipe = thread_quit_pipe;
d086f507 186 handle->rotation_timer_queue = rotation_timer_queue;
db66e574
JD
187
188end:
189 return handle;
190error:
191 rotation_thread_handle_destroy(handle);
192 return NULL;
193}
194
195static
196int init_poll_set(struct lttng_poll_event *poll_set,
197 struct rotation_thread_handle *handle)
198{
199 int ret;
200
201 /*
d086f507 202 * Create pollset with size 5:
db66e574 203 * - sessiond quit pipe
d086f507 204 * - sessiond timer pipe,
db66e574
JD
205 * - consumerd (32-bit user space) channel rotate pipe,
206 * - consumerd (64-bit user space) channel rotate pipe,
207 * - consumerd (kernel) channel rotate pipe,
208 */
d086f507 209 ret = lttng_poll_create(poll_set, 5, LTTNG_CLOEXEC);
db66e574
JD
210 if (ret < 0) {
211 goto end;
212 }
213
214 ret = lttng_poll_add(poll_set, handle->thread_quit_pipe,
215 LPOLLIN | LPOLLERR);
216 if (ret < 0) {
217 ERR("[rotation-thread] Failed to add thread_quit_pipe fd to pollset");
218 goto error;
219 }
d086f507
JD
220 ret = lttng_poll_add(poll_set,
221 lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe),
222 LPOLLIN | LPOLLERR);
223 if (ret < 0) {
224 ERR("[rotation-thread] Failed to add rotate_pending fd to pollset");
225 goto error;
226 }
db66e574
JD
227 ret = lttng_poll_add(poll_set, handle->ust32_consumer,
228 LPOLLIN | LPOLLERR);
229 if (ret < 0) {
230 ERR("[rotation-thread] Failed to add ust-32 channel rotation pipe fd to pollset");
231 goto error;
232 }
233 ret = lttng_poll_add(poll_set, handle->ust64_consumer,
234 LPOLLIN | LPOLLERR);
235 if (ret < 0) {
236 ERR("[rotation-thread] Failed to add ust-64 channel rotation pipe fd to pollset");
237 goto error;
238 }
239 if (handle->kernel_consumer >= 0) {
240 ret = lttng_poll_add(poll_set, handle->kernel_consumer,
241 LPOLLIN | LPOLLERR);
242 if (ret < 0) {
243 ERR("[rotation-thread] Failed to add kernel channel rotation pipe fd to pollset");
244 goto error;
245 }
246 }
247
248end:
249 return ret;
250error:
251 lttng_poll_clean(poll_set);
252 return ret;
253}
254
255static
256void fini_thread_state(struct rotation_thread_state *state)
257{
258 lttng_poll_clean(&state->events);
259 cds_lfht_destroy(channel_pending_rotate_ht, NULL);
260}
261
262static
263int init_thread_state(struct rotation_thread_handle *handle,
264 struct rotation_thread_state *state)
265{
266 int ret;
267
268 memset(state, 0, sizeof(*state));
269 lttng_poll_init(&state->events);
270
271 ret = init_poll_set(&state->events, handle);
272 if (ret) {
273 ERR("[rotation-thread] Failed to initialize rotation thread poll set");
274 goto end;
275 }
276
277 channel_pending_rotate_ht = cds_lfht_new(DEFAULT_HT_SIZE,
278 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
279 if (!channel_pending_rotate_ht) {
280 ERR("[rotation-thread] Failed to create channel pending rotation hash table");
281 ret = -1;
282 goto end;
283 }
284
285end:
286 return ret;
287}
288
289static
290int handle_channel_rotation_pipe(int fd, uint32_t revents,
291 struct rotation_thread_handle *handle,
292 struct rotation_thread_state *state)
293{
294 int ret = 0;
295 enum lttng_domain_type domain;
296 struct rotation_channel_info *channel_info;
297 struct ltt_session *session = NULL;
298 uint64_t key;
299
300 if (fd == handle->ust32_consumer ||
301 fd == handle->ust64_consumer) {
302 domain = LTTNG_DOMAIN_UST;
303 } else if (fd == handle->kernel_consumer) {
304 domain = LTTNG_DOMAIN_KERNEL;
305 } else {
306 ERR("[rotation-thread] Unknown channel rotation pipe fd %d",
307 fd);
308 abort();
309 }
310
311 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
312 ret = lttng_poll_del(&state->events, fd);
313 if (ret) {
314 ERR("[rotation-thread] Failed to remove consumer "
315 "rotation pipe from poll set");
316 }
317 goto end;
318 }
319
320 do {
321 ret = read(fd, &key, sizeof(key));
322 } while (ret == -1 && errno == EINTR);
323 if (ret != sizeof(key)) {
324 ERR("[rotation-thread] Failed to read from pipe (fd = %i)",
325 fd);
326 ret = -1;
327 goto end;
328 }
329
330 DBG("[rotation-thread] Received notification for chan %" PRIu64
331 ", domain %d\n", key, domain);
332
333 channel_info = lookup_channel_pending(key, domain);
334 if (!channel_info) {
335 ERR("[rotation-thread] Failed to find channel_info (key = %"
336 PRIu64 ")", key);
337 ret = -1;
338 goto end;
339 }
340 rcu_read_lock();
341 session_lock_list();
342 session = session_find_by_id(channel_info->session_id);
343 if (!session) {
344 /*
345 * The session may have been destroyed before we had a chance to
346 * perform this action, return gracefully.
347 */
348 DBG("[rotation-thread] Session %" PRIu64 " not found",
349 channel_info->session_id);
350 ret = 0;
351 goto end_unlock_session_list;
352 }
353
354 session_lock(session);
355 if (--session->nr_chan_rotate_pending == 0) {
356 time_t now = time(NULL);
357
358 if (now == (time_t) -1) {
d68c9a04 359 session->rotation_state = LTTNG_ROTATION_STATE_ERROR;
db66e574
JD
360 ret = LTTNG_ERR_UNK;
361 goto end_unlock_session;
362 }
363
364 ret = rename_complete_chunk(session, now);
365 if (ret < 0) {
366 ERR("Failed to rename completed rotation chunk");
367 goto end_unlock_session;
368 }
369 session->rotate_pending = false;
d68c9a04 370 session->rotation_state = LTTNG_ROTATION_STATE_COMPLETED;
db66e574 371 session->last_chunk_start_ts = session->current_chunk_start_ts;
d88744a4
JD
372 if (session->rotate_pending_relay) {
373 ret = sessiond_timer_rotate_pending_start(
374 session,
375 DEFAULT_ROTATE_PENDING_RELAY_TIMER);
376 if (ret) {
377 ERR("Failed to enable rotate pending timer");
378 ret = -1;
379 goto end_unlock_session;
380 }
381 }
db66e574
JD
382 DBG("Rotation completed for session %s", session->name);
383 }
384
385 ret = 0;
386
387end_unlock_session:
388 channel_rotation_info_destroy(channel_info);
389 session_unlock(session);
390end_unlock_session_list:
391 session_unlock_list();
392 rcu_read_unlock();
393end:
394 return ret;
395}
396
d88744a4
JD
397/*
398 * Process the rotate_pending check, called with session lock held.
399 */
400static
401int rotate_pending_relay_timer(struct ltt_session *session)
402{
403 int ret;
404
405 DBG("[rotation-thread] Check rotate pending on session %" PRIu64,
406 session->id);
407 ret = relay_rotate_pending(session, session->rotate_count - 1);
408 if (ret < 0) {
409 ERR("[rotation-thread] Check relay rotate pending");
410 goto end;
411 }
412 if (ret == 0) {
413 DBG("[rotation-thread] Rotation completed on the relay for "
414 "session %" PRIu64, session->id);
415 /*
416 * Now we can clear the pending flag in the session. New
417 * rotations can start now.
418 */
419 session->rotate_pending_relay = false;
420 } else if (ret == 1) {
421 DBG("[rotation-thread] Rotation still pending on the relay for "
422 "session %" PRIu64, session->id);
423 ret = sessiond_timer_rotate_pending_start(session,
424 DEFAULT_ROTATE_PENDING_RELAY_TIMER);
425 if (ret) {
426 ERR("Re-enabling rotate pending timer");
427 ret = -1;
428 goto end;
429 }
430 }
431
432 ret = 0;
433
434end:
435 return ret;
436}
437
259c2674
JD
438/*
439 * Process the rotate_timer, called with session lock held.
440 */
441static
442int rotate_timer(struct ltt_session *session)
443{
444 int ret;
445
446 /*
447 * Complete _at most_ one scheduled rotation on a stopped session.
448 */
449 if (!session->active && session->rotate_timer_enabled &&
450 session->rotated_after_last_stop) {
451 ret = 0;
452 goto end;
453 }
454
455 /* Ignore this timer if a rotation is already in progress. */
456 if (session->rotate_pending || session->rotate_pending_relay) {
457 ret = 0;
458 goto end;
459 }
460
461 DBG("[rotation-thread] Rotate timer on session %s", session->name);
462
463 ret = cmd_rotate_session(session, NULL);
464 if (ret == -LTTNG_ERR_ROTATION_PENDING) {
465 DBG("Scheduled rotation aborted since a rotation is already in progress");
466 ret = 0;
467 goto end;
468 } else if (ret != LTTNG_OK) {
469 ERR("[rotation-thread] Automatic time-triggered rotation failed with error code %i", ret);
470 ret = -1;
471 goto end;
472 }
473
474 ret = 0;
475
476end:
477 return ret;
478}
479
d88744a4
JD
480static
481int handle_rotate_timer_pipe(uint32_t revents,
482 struct rotation_thread_handle *handle,
483 struct rotation_thread_state *state,
484 struct rotation_thread_timer_queue *queue)
485{
486 int ret = 0;
487 int fd = lttng_pipe_get_readfd(queue->event_pipe);
488 struct ltt_session *session;
489 char buf[1];
490
491 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
492 ret = lttng_poll_del(&state->events, fd);
493 if (ret) {
494 ERR("[rotation-thread] Failed to remove consumer "
495 "rotate pending pipe from poll set");
496 }
497 goto end;
498 }
499
500 ret = lttng_read(fd, buf, 1);
501 if (ret != 1) {
502 ERR("[rotation-thread] Failed to read from wakeup pipe (fd = %i)", fd);
503 ret = -1;
504 goto end;
505 }
506
507 for (;;) {
508 struct sessiond_rotation_timer *timer_data;
509
510 /*
511 * Take the queue lock only to pop elements from the list.
512 */
513 pthread_mutex_lock(&queue->lock);
514 if (cds_list_empty(&queue->list)) {
515 pthread_mutex_unlock(&queue->lock);
516 break;
517 }
518 timer_data = cds_list_first_entry(&queue->list,
519 struct sessiond_rotation_timer, head);
520 cds_list_del(&timer_data->head);
521 pthread_mutex_unlock(&queue->lock);
522
523 /*
524 * session lock to lookup the session ID.
525 */
526 session_lock_list();
527 session = session_find_by_id(timer_data->session_id);
528 if (!session) {
529 DBG("[rotation-thread] Session %" PRIu64 " not found",
530 timer_data->session_id);
531 /*
532 * This is a non-fatal error, and we cannot report it to the
533 * user (timer), so just print the error and continue the
534 * processing.
535 */
536 session_unlock_list();
537 free(timer_data);
538 continue;
539 }
540
541 /*
542 * Take the session lock and release the session_list lock.
543 */
544 session_lock(session);
545 session_unlock_list();
546
547 if (timer_data->signal == LTTNG_SESSIOND_SIG_ROTATE_PENDING) {
548 ret = rotate_pending_relay_timer(session);
259c2674
JD
549 } else if (timer_data->signal == LTTNG_SESSIOND_SIG_ROTATE_TIMER) {
550 ret = rotate_timer(session);
d88744a4
JD
551 } else {
552 ERR("Unknown signal in rotate timer %d", timer_data->signal);
553 ret = -1;
554 }
555 session_unlock(session);
556 free(timer_data);
557 if (ret) {
558 ERR("Error processing timer");
559 goto end;
560 }
561 }
562
563 ret = 0;
564
565end:
566 return ret;
567}
568
db66e574
JD
569void *thread_rotation(void *data)
570{
571 int ret;
572 struct rotation_thread_handle *handle = data;
573 struct rotation_thread_state state;
574
575 DBG("[rotation-thread] Started rotation thread");
576
577 if (!handle) {
578 ERR("[rotation-thread] Invalid thread context provided");
579 goto end;
580 }
581
582 rcu_register_thread();
583 rcu_thread_online();
584
585 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_ROTATION);
586 health_code_update();
587
588 ret = init_thread_state(handle, &state);
589 if (ret) {
590 goto end;
591 }
592
593 /* Ready to handle client connections. */
594 sessiond_notify_ready();
595
596 while (true) {
597 int fd_count, i;
598
599 health_poll_entry();
600 DBG("[rotation-thread] Entering poll wait");
601 ret = lttng_poll_wait(&state.events, -1);
602 DBG("[rotation-thread] Poll wait returned (%i)", ret);
603 health_poll_exit();
604 if (ret < 0) {
605 /*
606 * Restart interrupted system call.
607 */
608 if (errno == EINTR) {
609 continue;
610 }
611 ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret);
612 goto error;
613 }
614
615 fd_count = ret;
616 for (i = 0; i < fd_count; i++) {
617 int fd = LTTNG_POLL_GETFD(&state.events, i);
618 uint32_t revents = LTTNG_POLL_GETEV(&state.events, i);
619
620 DBG("[rotation-thread] Handling fd (%i) activity (%u)",
621 fd, revents);
622
623 if (fd == handle->thread_quit_pipe) {
624 DBG("[rotation-thread] Quit pipe activity");
625 goto exit;
d88744a4
JD
626 } else if (fd == lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe)) {
627 ret = handle_rotate_timer_pipe(revents,
628 handle, &state, handle->rotation_timer_queue);
629 if (ret) {
630 ERR("[rotation-thread] Failed to handle rotation timer pipe event");
631 goto error;
632 }
db66e574
JD
633 } else if (fd == handle->ust32_consumer ||
634 fd == handle->ust64_consumer ||
635 fd == handle->kernel_consumer) {
636 ret = handle_channel_rotation_pipe(fd,
637 revents, handle, &state);
638 if (ret) {
639 ERR("[rotation-thread] Handle channel rotation pipe");
640 goto error;
641 }
642 }
643 }
644 }
645exit:
646error:
647 DBG("[rotation-thread] Exit");
648 fini_thread_state(&state);
649 health_unregister(health_sessiond);
650 rcu_thread_offline();
651 rcu_unregister_thread();
652end:
653 return NULL;
654}
This page took 0.047729 seconds and 4 git commands to generate.