2 * Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #include <lttng/trigger/trigger.h>
11 #include <common/error.h>
12 #include <common/config/session-config.h>
13 #include <common/defaults.h>
14 #include <common/utils.h>
15 #include <common/futex.h>
16 #include <common/align.h>
17 #include <common/time.h>
18 #include <common/hashtable/utils.h>
24 #include <common/kernel-ctl/kernel-ctl.h>
25 #include <lttng/notification/channel-internal.h>
26 #include <lttng/rotate-internal.h>
27 #include <lttng/location-internal.h>
28 #include <lttng/condition/condition-internal.h>
30 #include "rotation-thread.h"
31 #include "lttng-sessiond.h"
32 #include "health-sessiond.h"
37 #include "notification-thread-commands.h"
42 #include <urcu/list.h>
44 struct lttng_notification_channel
*rotate_notification_channel
= NULL
;
46 struct rotation_thread
{
47 struct lttng_poll_event events
;
50 struct rotation_thread_job
{
51 enum rotation_thread_job_type type
;
52 struct ltt_session
*session
;
53 /* List member in struct rotation_thread_timer_queue. */
54 struct cds_list_head head
;
58 * The timer thread enqueues jobs and wakes up the rotation thread.
59 * When the rotation thread wakes up, it empties the queue.
61 struct rotation_thread_timer_queue
{
62 struct lttng_pipe
*event_pipe
;
63 struct cds_list_head list
;
67 struct rotation_thread_handle
{
68 struct rotation_thread_timer_queue
*rotation_timer_queue
;
69 /* Access to the notification thread cmd_queue */
70 struct notification_thread_handle
*notification_thread_handle
;
71 /* Thread-specific quit pipe. */
72 struct lttng_pipe
*quit_pipe
;
76 const char *get_job_type_str(enum rotation_thread_job_type job_type
)
79 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION
:
80 return "CHECK_PENDING_ROTATION";
81 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION
:
82 return "SCHEDULED_ROTATION";
88 struct rotation_thread_timer_queue
*rotation_thread_timer_queue_create(void)
90 struct rotation_thread_timer_queue
*queue
= NULL
;
92 queue
= (rotation_thread_timer_queue
*) zmalloc(sizeof(*queue
));
94 PERROR("Failed to allocate timer rotate queue");
98 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
99 CDS_INIT_LIST_HEAD(&queue
->list
);
100 pthread_mutex_init(&queue
->lock
, NULL
);
105 void rotation_thread_timer_queue_destroy(
106 struct rotation_thread_timer_queue
*queue
)
112 lttng_pipe_destroy(queue
->event_pipe
);
114 pthread_mutex_lock(&queue
->lock
);
115 LTTNG_ASSERT(cds_list_empty(&queue
->list
));
116 pthread_mutex_unlock(&queue
->lock
);
117 pthread_mutex_destroy(&queue
->lock
);
122 * Destroy the thread data previously created by the init function.
124 void rotation_thread_handle_destroy(
125 struct rotation_thread_handle
*handle
)
127 lttng_pipe_destroy(handle
->quit_pipe
);
131 struct rotation_thread_handle
*rotation_thread_handle_create(
132 struct rotation_thread_timer_queue
*rotation_timer_queue
,
133 struct notification_thread_handle
*notification_thread_handle
)
135 struct rotation_thread_handle
*handle
;
137 handle
= (rotation_thread_handle
*) zmalloc(sizeof(*handle
));
142 handle
->rotation_timer_queue
= rotation_timer_queue
;
143 handle
->notification_thread_handle
= notification_thread_handle
;
144 handle
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
145 if (!handle
->quit_pipe
) {
152 rotation_thread_handle_destroy(handle
);
157 * Called with the rotation_thread_timer_queue lock held.
158 * Return true if the same timer job already exists in the queue, false if not.
161 bool timer_job_exists(const struct rotation_thread_timer_queue
*queue
,
162 enum rotation_thread_job_type job_type
,
163 struct ltt_session
*session
)
166 struct rotation_thread_job
*job
;
168 cds_list_for_each_entry(job
, &queue
->list
, head
) {
169 if (job
->session
== session
&& job
->type
== job_type
) {
178 void rotation_thread_enqueue_job(struct rotation_thread_timer_queue
*queue
,
179 enum rotation_thread_job_type job_type
,
180 struct ltt_session
*session
)
183 const char dummy
= '!';
184 struct rotation_thread_job
*job
= NULL
;
185 const char *job_type_str
= get_job_type_str(job_type
);
187 pthread_mutex_lock(&queue
->lock
);
188 if (timer_job_exists(queue
, job_type
, session
)) {
190 * This timer job is already pending, we don't need to add
196 job
= (rotation_thread_job
*) zmalloc(sizeof(struct rotation_thread_job
));
198 PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"",
199 job_type_str
, session
->name
);
202 /* No reason for this to fail as the caller must hold a reference. */
203 (void) session_get(session
);
205 job
->session
= session
;
206 job
->type
= job_type
;
207 cds_list_add_tail(&job
->head
, &queue
->list
);
209 ret
= lttng_write(lttng_pipe_get_writefd(queue
->event_pipe
), &dummy
,
213 * We do not want to block in the timer handler, the job has
214 * been enqueued in the list, the wakeup pipe is probably full,
215 * the job will be processed when the rotation_thread catches
219 DIAGNOSTIC_IGNORE_LOGICAL_OP
220 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
223 * Not an error, but would be surprising and indicate
224 * that the rotation thread can't keep up with the
227 DBG("Wake-up pipe of rotation thread job queue is full");
230 PERROR("Failed to wake-up the rotation thread after pushing a job of type \"%s\" for session \"%s\"",
231 job_type_str
, session
->name
);
236 pthread_mutex_unlock(&queue
->lock
);
240 int init_poll_set(struct lttng_poll_event
*poll_set
,
241 struct rotation_thread_handle
*handle
)
246 * Create pollset with size 3:
247 * - rotation thread quit pipe,
248 * - rotation thread timer queue pipe,
249 * - notification channel sock,
251 ret
= lttng_poll_create(poll_set
, 5, LTTNG_CLOEXEC
);
256 ret
= lttng_poll_add(poll_set
,
257 lttng_pipe_get_readfd(handle
->quit_pipe
),
260 ERR("Failed to add quit pipe read fd to poll set");
264 ret
= lttng_poll_add(poll_set
,
265 lttng_pipe_get_readfd(handle
->rotation_timer_queue
->event_pipe
),
268 ERR("Failed to add rotate_pending fd to poll set");
274 lttng_poll_clean(poll_set
);
279 void fini_thread_state(struct rotation_thread
*state
)
281 lttng_poll_clean(&state
->events
);
282 if (rotate_notification_channel
) {
283 lttng_notification_channel_destroy(rotate_notification_channel
);
288 int init_thread_state(struct rotation_thread_handle
*handle
,
289 struct rotation_thread
*state
)
293 memset(state
, 0, sizeof(*state
));
294 lttng_poll_init(&state
->events
);
296 ret
= init_poll_set(&state
->events
, handle
);
298 ERR("Failed to initialize rotation thread poll set");
302 rotate_notification_channel
= lttng_notification_channel_create(
303 lttng_session_daemon_notification_endpoint
);
304 if (!rotate_notification_channel
) {
305 ERR("Could not create notification channel");
309 ret
= lttng_poll_add(&state
->events
, rotate_notification_channel
->socket
,
312 ERR("Failed to add notification fd to pollset");
321 void check_session_rotation_pending_on_consumers(struct ltt_session
*session
,
322 bool *_rotation_completed
)
325 struct consumer_socket
*socket
;
326 struct cds_lfht_iter iter
;
327 enum consumer_trace_chunk_exists_status exists_status
;
329 bool chunk_exists_on_peer
= false;
330 enum lttng_trace_chunk_status chunk_status
;
332 LTTNG_ASSERT(session
->chunk_being_archived
);
335 * Check for a local pending rotation on all consumers (32-bit
336 * user space, 64-bit user space, and kernel).
339 if (!session
->ust_session
) {
342 cds_lfht_for_each_entry(session
->ust_session
->consumer
->socks
->ht
,
343 &iter
, socket
, node
.node
) {
344 relayd_id
= session
->ust_session
->consumer
->type
== CONSUMER_DST_LOCAL
?
346 session
->ust_session
->consumer
->net_seq_index
;
348 pthread_mutex_lock(socket
->lock
);
349 ret
= consumer_trace_chunk_exists(socket
,
351 session
->id
, session
->chunk_being_archived
,
354 pthread_mutex_unlock(socket
->lock
);
355 ERR("Error occurred while checking rotation status on consumer daemon");
359 if (exists_status
!= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
) {
360 pthread_mutex_unlock(socket
->lock
);
361 chunk_exists_on_peer
= true;
364 pthread_mutex_unlock(socket
->lock
);
368 if (!session
->kernel_session
) {
371 cds_lfht_for_each_entry(session
->kernel_session
->consumer
->socks
->ht
,
372 &iter
, socket
, node
.node
) {
373 pthread_mutex_lock(socket
->lock
);
374 relayd_id
= session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
?
376 session
->kernel_session
->consumer
->net_seq_index
;
378 ret
= consumer_trace_chunk_exists(socket
,
380 session
->id
, session
->chunk_being_archived
,
383 pthread_mutex_unlock(socket
->lock
);
384 ERR("Error occurred while checking rotation status on consumer daemon");
388 if (exists_status
!= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
) {
389 pthread_mutex_unlock(socket
->lock
);
390 chunk_exists_on_peer
= true;
393 pthread_mutex_unlock(socket
->lock
);
399 if (!chunk_exists_on_peer
) {
400 uint64_t chunk_being_archived_id
;
402 chunk_status
= lttng_trace_chunk_get_id(
403 session
->chunk_being_archived
,
404 &chunk_being_archived_id
);
405 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
406 DBG("Rotation of trace archive %" PRIu64
" of session \"%s\" is complete on all consumers",
407 chunk_being_archived_id
,
410 *_rotation_completed
= !chunk_exists_on_peer
;
412 ret
= session_reset_rotation_state(session
,
413 LTTNG_ROTATION_STATE_ERROR
);
415 ERR("Failed to reset rotation state of session \"%s\"",
422 * Check if the last rotation was completed, called with session lock held.
423 * Should only return non-zero in the event of a fatal error. Doing so will
424 * shutdown the thread.
427 int check_session_rotation_pending(struct ltt_session
*session
,
428 struct notification_thread_handle
*notification_thread_handle
)
431 struct lttng_trace_archive_location
*location
;
432 enum lttng_trace_chunk_status chunk_status
;
433 bool rotation_completed
= false;
434 const char *archived_chunk_name
;
435 uint64_t chunk_being_archived_id
;
437 if (!session
->chunk_being_archived
) {
442 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
,
443 &chunk_being_archived_id
);
444 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
446 DBG("Checking for pending rotation on session \"%s\", trace archive %" PRIu64
,
447 session
->name
, chunk_being_archived_id
);
450 * The rotation-pending check timer of a session is launched in
451 * one-shot mode. If the rotation is incomplete, the rotation
452 * thread will re-enable the pending-check timer.
454 * The timer thread can't stop the timer itself since it is involved
455 * in the check for the timer's quiescence.
457 ret
= timer_session_rotation_pending_check_stop(session
);
459 goto check_ongoing_rotation
;
462 check_session_rotation_pending_on_consumers(session
,
463 &rotation_completed
);
464 if (!rotation_completed
||
465 session
->rotation_state
== LTTNG_ROTATION_STATE_ERROR
) {
466 goto check_ongoing_rotation
;
470 * Now we can clear the "ONGOING" state in the session. New
471 * rotations can start now.
473 chunk_status
= lttng_trace_chunk_get_name(session
->chunk_being_archived
,
474 &archived_chunk_name
, NULL
);
475 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
476 free(session
->last_archived_chunk_name
);
477 session
->last_archived_chunk_name
= strdup(archived_chunk_name
);
478 if (!session
->last_archived_chunk_name
) {
479 PERROR("Failed to duplicate archived chunk name");
481 session_reset_rotation_state(session
, LTTNG_ROTATION_STATE_COMPLETED
);
483 if (!session
->quiet_rotation
) {
484 location
= session_get_trace_archive_location(session
);
485 ret
= notification_thread_command_session_rotation_completed(
486 notification_thread_handle
,
490 session
->last_archived_chunk_id
.value
,
492 lttng_trace_archive_location_put(location
);
493 if (ret
!= LTTNG_OK
) {
494 ERR("Failed to notify notification thread of completed rotation for session %s",
500 check_ongoing_rotation
:
501 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
502 chunk_status
= lttng_trace_chunk_get_id(
503 session
->chunk_being_archived
,
504 &chunk_being_archived_id
);
505 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
507 DBG("Rotation of trace archive %" PRIu64
" is still pending for session %s",
508 chunk_being_archived_id
, session
->name
);
509 ret
= timer_session_rotation_pending_check_start(session
,
510 DEFAULT_ROTATE_PENDING_TIMER
);
512 ERR("Failed to re-enable rotation pending timer");
522 /* Call with the session and session_list locks held. */
524 int launch_session_rotation(struct ltt_session
*session
)
527 struct lttng_rotate_session_return rotation_return
;
529 DBG("Launching scheduled time-based rotation on session \"%s\"",
532 ret
= cmd_rotate_session(session
, &rotation_return
, false,
533 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
534 if (ret
== LTTNG_OK
) {
535 DBG("Scheduled time-based rotation successfully launched on session \"%s\"",
538 /* Don't consider errors as fatal. */
539 DBG("Scheduled time-based rotation aborted for session %s: %s",
540 session
->name
, lttng_strerror(ret
));
546 int run_job(struct rotation_thread_job
*job
, struct ltt_session
*session
,
547 struct notification_thread_handle
*notification_thread_handle
)
552 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION
:
553 ret
= launch_session_rotation(session
);
555 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION
:
556 ret
= check_session_rotation_pending(session
,
557 notification_thread_handle
);
566 int handle_job_queue(struct rotation_thread_handle
*handle
,
567 struct rotation_thread
*state
__attribute__((unused
)),
568 struct rotation_thread_timer_queue
*queue
)
573 struct ltt_session
*session
;
574 struct rotation_thread_job
*job
;
576 /* Take the queue lock only to pop an element from the list. */
577 pthread_mutex_lock(&queue
->lock
);
578 if (cds_list_empty(&queue
->list
)) {
579 pthread_mutex_unlock(&queue
->lock
);
582 job
= cds_list_first_entry(&queue
->list
,
584 cds_list_del(&job
->head
);
585 pthread_mutex_unlock(&queue
->lock
);
588 session
= job
->session
;
590 DBG("Session \"%s\" not found",
591 session
->name
!= NULL
? session
->name
: "");
593 * This is a non-fatal error, and we cannot report it to
594 * the user (timer), so just print the error and
595 * continue the processing.
597 * While the timer thread will purge pending signals for
598 * a session on the session's destruction, it is
599 * possible for a job targeting that session to have
600 * already been queued before it was destroyed.
603 session_put(session
);
604 session_unlock_list();
608 session_lock(session
);
609 ret
= run_job(job
, session
, handle
->notification_thread_handle
);
610 session_unlock(session
);
611 /* Release reference held by the job. */
612 session_put(session
);
613 session_unlock_list();
627 int handle_condition(const struct lttng_condition
*condition
,
628 const struct lttng_evaluation
*evaluation
,
629 struct notification_thread_handle
*notification_thread_handle
)
632 const char *condition_session_name
= NULL
;
633 enum lttng_condition_type condition_type
;
634 enum lttng_condition_status condition_status
;
635 enum lttng_evaluation_status evaluation_status
;
637 struct ltt_session
*session
;
639 condition_type
= lttng_condition_get_type(condition
);
641 if (condition_type
!= LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
) {
643 ERR("Condition type and session usage type are not the same");
647 /* Fetch info to test */
648 condition_status
= lttng_condition_session_consumed_size_get_session_name(
649 condition
, &condition_session_name
);
650 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
651 ERR("Session name could not be fetched");
655 evaluation_status
= lttng_evaluation_session_consumed_size_get_consumed_size(evaluation
,
657 if (evaluation_status
!= LTTNG_EVALUATION_STATUS_OK
) {
658 ERR("Failed to get evaluation");
664 session
= session_find_by_name(condition_session_name
);
666 DBG("Failed to find session while handling notification: notification type = %s, session name = `%s`",
667 lttng_condition_type_str(condition_type
),
668 condition_session_name
);
670 * Not a fatal error: a session can be destroyed before we get
671 * the chance to handle the notification.
674 session_unlock_list();
677 session_lock(session
);
679 ret
= unsubscribe_session_consumed_size_rotation(session
,
680 notification_thread_handle
);
685 ret
= cmd_rotate_session(session
, NULL
, false,
686 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
687 if (ret
== -LTTNG_ERR_ROTATION_PENDING
) {
688 DBG("Rotate already pending, subscribe to the next threshold value");
689 } else if (ret
!= LTTNG_OK
) {
690 ERR("Failed to rotate on size notification with error: %s",
691 lttng_strerror(ret
));
695 ret
= subscribe_session_consumed_size_rotation(session
,
696 consumed
+ session
->rotate_size
,
697 notification_thread_handle
);
699 ERR("Failed to subscribe to session consumed size condition");
705 session_unlock(session
);
706 session_put(session
);
707 session_unlock_list();
713 int handle_notification_channel(int fd
__attribute__((unused
)),
714 struct rotation_thread_handle
*handle
,
715 struct rotation_thread
*state
__attribute__((unused
)))
718 bool notification_pending
;
719 struct lttng_notification
*notification
= NULL
;
720 enum lttng_notification_channel_status status
;
721 const struct lttng_evaluation
*notification_evaluation
;
722 const struct lttng_condition
*notification_condition
;
724 status
= lttng_notification_channel_has_pending_notification(
725 rotate_notification_channel
, ¬ification_pending
);
726 if (status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
727 ERR("Error occurred while checking for pending notification");
732 if (!notification_pending
) {
737 /* Receive the next notification. */
738 status
= lttng_notification_channel_get_next_notification(
739 rotate_notification_channel
,
743 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
745 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
746 /* Not an error, we will wait for the next one */
749 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
750 ERR("Notification channel was closed");
754 /* Unhandled conditions / errors. */
755 ERR("Unknown notification channel status");
760 notification_condition
= lttng_notification_get_condition(notification
);
761 notification_evaluation
= lttng_notification_get_evaluation(notification
);
763 ret
= handle_condition(notification_condition
, notification_evaluation
,
764 handle
->notification_thread_handle
);
767 lttng_notification_destroy(notification
);
772 void *thread_rotation(void *data
)
775 struct rotation_thread_handle
*handle
= (rotation_thread_handle
*) data
;
776 struct rotation_thread thread
;
779 DBG("Started rotation thread");
780 rcu_register_thread();
782 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_ROTATION
);
783 health_code_update();
786 ERR("Invalid thread context provided");
790 queue_pipe_fd
= lttng_pipe_get_readfd(
791 handle
->rotation_timer_queue
->event_pipe
);
794 ret
= init_thread_state(handle
, &thread
);
803 DBG("Entering poll wait");
804 ret
= lttng_poll_wait(&thread
.events
, -1);
805 DBG("Poll wait returned (%i)", ret
);
809 * Restart interrupted system call.
811 if (errno
== EINTR
) {
814 ERR("Error encountered during lttng_poll_wait (%i)", ret
);
819 for (i
= 0; i
< fd_count
; i
++) {
820 int fd
= LTTNG_POLL_GETFD(&thread
.events
, i
);
821 uint32_t revents
= LTTNG_POLL_GETEV(&thread
.events
, i
);
823 DBG("Handling fd (%i) activity (%u)",
826 if (revents
& LPOLLERR
) {
827 ERR("Polling returned an error on fd %i", fd
);
831 if (fd
== rotate_notification_channel
->socket
) {
832 ret
= handle_notification_channel(fd
, handle
,
835 ERR("Error occurred while handling activity on notification channel socket");
839 /* Job queue or quit pipe activity. */
842 * The job queue is serviced if there is
843 * activity on the quit pipe to ensure it is
844 * flushed and all references held in the queue
847 ret
= handle_job_queue(handle
, &thread
,
848 handle
->rotation_timer_queue
);
850 ERR("Failed to handle rotation timer pipe event");
854 if (fd
== queue_pipe_fd
) {
857 ret
= lttng_read(fd
, &buf
, 1);
859 ERR("Failed to read from wakeup pipe (fd = %i)", fd
);
863 DBG("Quit pipe activity");
872 fini_thread_state(&thread
);
874 health_unregister(the_health_sessiond
);
875 rcu_thread_offline();
876 rcu_unregister_thread();
881 bool shutdown_rotation_thread(void *thread_data
)
883 struct rotation_thread_handle
*handle
= (rotation_thread_handle
*) thread_data
;
884 const int write_fd
= lttng_pipe_get_writefd(handle
->quit_pipe
);
886 return notify_thread_pipe(write_fd
) == 1;
889 bool launch_rotation_thread(struct rotation_thread_handle
*handle
)
891 struct lttng_thread
*thread
;
893 thread
= lttng_thread_create("Rotation",
895 shutdown_rotation_thread
,
901 lttng_thread_put(thread
);