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
11 #include "health-sessiond.hpp"
12 #include "lttng-sessiond.hpp"
13 #include "notification-thread-commands.hpp"
15 #include "rotation-thread.hpp"
16 #include "session.hpp"
21 #include <common/align.hpp>
22 #include <common/config/session-config.hpp>
23 #include <common/defaults.hpp>
24 #include <common/error.hpp>
25 #include <common/futex.hpp>
26 #include <common/hashtable/utils.hpp>
27 #include <common/kernel-ctl/kernel-ctl.hpp>
28 #include <common/time.hpp>
29 #include <common/urcu.hpp>
30 #include <common/utils.hpp>
32 #include <lttng/condition/condition-internal.hpp>
33 #include <lttng/location-internal.hpp>
34 #include <lttng/notification/channel-internal.hpp>
35 #include <lttng/notification/notification-internal.hpp>
36 #include <lttng/rotate-internal.hpp>
37 #include <lttng/trigger/trigger.h>
44 #include <urcu/list.h>
46 struct lttng_notification_channel
*rotate_notification_channel
= nullptr;
48 struct rotation_thread
{
49 struct lttng_poll_event events
;
53 * The timer thread enqueues jobs and wakes up the rotation thread.
54 * When the rotation thread wakes up, it empties the queue.
56 struct rotation_thread_timer_queue
{
57 struct lttng_pipe
*event_pipe
;
58 struct cds_list_head list
;
62 struct rotation_thread_handle
{
63 struct rotation_thread_timer_queue
*rotation_timer_queue
;
64 /* Access to the notification thread cmd_queue */
65 struct notification_thread_handle
*notification_thread_handle
;
66 /* Thread-specific quit pipe. */
67 struct lttng_pipe
*quit_pipe
;
71 struct rotation_thread_job
{
72 enum rotation_thread_job_type type
;
73 struct ltt_session
*session
;
74 /* List member in struct rotation_thread_timer_queue. */
75 struct cds_list_head head
;
79 static const char *get_job_type_str(enum rotation_thread_job_type job_type
)
82 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION
:
83 return "CHECK_PENDING_ROTATION";
84 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION
:
85 return "SCHEDULED_ROTATION";
91 struct rotation_thread_timer_queue
*rotation_thread_timer_queue_create()
93 struct rotation_thread_timer_queue
*queue
= nullptr;
95 queue
= zmalloc
<rotation_thread_timer_queue
>();
97 PERROR("Failed to allocate timer rotate queue");
101 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
102 CDS_INIT_LIST_HEAD(&queue
->list
);
103 pthread_mutex_init(&queue
->lock
, nullptr);
108 void rotation_thread_timer_queue_destroy(struct rotation_thread_timer_queue
*queue
)
114 lttng_pipe_destroy(queue
->event_pipe
);
116 pthread_mutex_lock(&queue
->lock
);
117 LTTNG_ASSERT(cds_list_empty(&queue
->list
));
118 pthread_mutex_unlock(&queue
->lock
);
119 pthread_mutex_destroy(&queue
->lock
);
124 * Destroy the thread data previously created by the init function.
126 void rotation_thread_handle_destroy(struct rotation_thread_handle
*handle
)
128 lttng_pipe_destroy(handle
->quit_pipe
);
132 struct rotation_thread_handle
*
133 rotation_thread_handle_create(struct rotation_thread_timer_queue
*rotation_timer_queue
,
134 struct notification_thread_handle
*notification_thread_handle
)
136 struct rotation_thread_handle
*handle
;
138 handle
= zmalloc
<rotation_thread_handle
>();
143 handle
->rotation_timer_queue
= rotation_timer_queue
;
144 handle
->notification_thread_handle
= notification_thread_handle
;
145 handle
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
146 if (!handle
->quit_pipe
) {
153 rotation_thread_handle_destroy(handle
);
158 * Called with the rotation_thread_timer_queue lock held.
159 * Return true if the same timer job already exists in the queue, false if not.
161 static 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
= nullptr;
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
= zmalloc
<rotation_thread_job
>();
198 PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"",
203 /* No reason for this to fail as the caller must hold a reference. */
204 (void) session_get(session
);
206 job
->session
= session
;
207 job
->type
= job_type
;
208 cds_list_add_tail(&job
->head
, &queue
->list
);
210 ret
= lttng_write(lttng_pipe_get_writefd(queue
->event_pipe
), &dummy
, sizeof(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\"",
237 pthread_mutex_unlock(&queue
->lock
);
240 static int init_poll_set(struct lttng_poll_event
*poll_set
, struct rotation_thread_handle
*handle
)
245 * Create pollset with size 3:
246 * - rotation thread quit pipe,
247 * - rotation thread timer queue pipe,
248 * - notification channel sock,
250 ret
= lttng_poll_create(poll_set
, 5, LTTNG_CLOEXEC
);
255 ret
= lttng_poll_add(poll_set
, lttng_pipe_get_readfd(handle
->quit_pipe
), LPOLLIN
);
257 ERR("Failed to add quit pipe read fd to poll set");
261 ret
= lttng_poll_add(
262 poll_set
, lttng_pipe_get_readfd(handle
->rotation_timer_queue
->event_pipe
), LPOLLIN
);
264 ERR("Failed to add rotate_pending fd to poll set");
270 lttng_poll_clean(poll_set
);
274 static void fini_thread_state(struct rotation_thread
*state
)
276 lttng_poll_clean(&state
->events
);
277 if (rotate_notification_channel
) {
278 lttng_notification_channel_destroy(rotate_notification_channel
);
282 static int init_thread_state(struct rotation_thread_handle
*handle
, struct rotation_thread
*state
)
286 memset(state
, 0, sizeof(*state
));
287 lttng_poll_init(&state
->events
);
289 ret
= init_poll_set(&state
->events
, handle
);
291 ERR("Failed to initialize rotation thread poll set");
295 rotate_notification_channel
=
296 lttng_notification_channel_create(lttng_session_daemon_notification_endpoint
);
297 if (!rotate_notification_channel
) {
298 ERR("Could not create notification channel");
302 ret
= lttng_poll_add(&state
->events
, rotate_notification_channel
->socket
, LPOLLIN
);
304 ERR("Failed to add notification fd to pollset");
312 static void check_session_rotation_pending_on_consumers(struct ltt_session
*session
,
313 bool *_rotation_completed
)
316 struct consumer_socket
*socket
;
317 struct cds_lfht_iter iter
;
318 enum consumer_trace_chunk_exists_status exists_status
;
320 bool chunk_exists_on_peer
= false;
321 enum lttng_trace_chunk_status chunk_status
;
322 lttng::urcu::read_lock_guard read_lock
;
324 LTTNG_ASSERT(session
->chunk_being_archived
);
327 * Check for a local pending rotation on all consumers (32-bit
328 * user space, 64-bit user space, and kernel).
330 if (!session
->ust_session
) {
334 cds_lfht_for_each_entry (
335 session
->ust_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
336 relayd_id
= session
->ust_session
->consumer
->type
== CONSUMER_DST_LOCAL
?
338 session
->ust_session
->consumer
->net_seq_index
;
340 pthread_mutex_lock(socket
->lock
);
341 ret
= consumer_trace_chunk_exists(socket
,
344 session
->chunk_being_archived
,
347 pthread_mutex_unlock(socket
->lock
);
348 ERR("Error occurred while checking rotation status on consumer daemon");
352 if (exists_status
!= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
) {
353 pthread_mutex_unlock(socket
->lock
);
354 chunk_exists_on_peer
= true;
357 pthread_mutex_unlock(socket
->lock
);
361 if (!session
->kernel_session
) {
364 cds_lfht_for_each_entry (
365 session
->kernel_session
->consumer
->socks
->ht
, &iter
, socket
, node
.node
) {
366 pthread_mutex_lock(socket
->lock
);
367 relayd_id
= session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
?
369 session
->kernel_session
->consumer
->net_seq_index
;
371 ret
= consumer_trace_chunk_exists(socket
,
374 session
->chunk_being_archived
,
377 pthread_mutex_unlock(socket
->lock
);
378 ERR("Error occurred while checking rotation status on consumer daemon");
382 if (exists_status
!= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
) {
383 pthread_mutex_unlock(socket
->lock
);
384 chunk_exists_on_peer
= true;
387 pthread_mutex_unlock(socket
->lock
);
392 if (!chunk_exists_on_peer
) {
393 uint64_t chunk_being_archived_id
;
395 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
,
396 &chunk_being_archived_id
);
397 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
398 DBG("Rotation of trace archive %" PRIu64
399 " of session \"%s\" is complete on all consumers",
400 chunk_being_archived_id
,
403 *_rotation_completed
= !chunk_exists_on_peer
;
405 ret
= session_reset_rotation_state(session
, LTTNG_ROTATION_STATE_ERROR
);
407 ERR("Failed to reset rotation state of session \"%s\"", session
->name
);
413 * Check if the last rotation was completed, called with session lock held.
414 * Should only return non-zero in the event of a fatal error. Doing so will
415 * shutdown the thread.
418 check_session_rotation_pending(struct ltt_session
*session
,
419 struct notification_thread_handle
*notification_thread_handle
)
422 struct lttng_trace_archive_location
*location
;
423 enum lttng_trace_chunk_status chunk_status
;
424 bool rotation_completed
= false;
425 const char *archived_chunk_name
;
426 uint64_t chunk_being_archived_id
;
428 if (!session
->chunk_being_archived
) {
434 lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_being_archived_id
);
435 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
437 DBG("Checking for pending rotation on session \"%s\", trace archive %" PRIu64
,
439 chunk_being_archived_id
);
442 * The rotation-pending check timer of a session is launched in
443 * one-shot mode. If the rotation is incomplete, the rotation
444 * thread will re-enable the pending-check timer.
446 * The timer thread can't stop the timer itself since it is involved
447 * in the check for the timer's quiescence.
449 ret
= timer_session_rotation_pending_check_stop(session
);
451 goto check_ongoing_rotation
;
454 check_session_rotation_pending_on_consumers(session
, &rotation_completed
);
455 if (!rotation_completed
|| session
->rotation_state
== LTTNG_ROTATION_STATE_ERROR
) {
456 goto check_ongoing_rotation
;
460 * Now we can clear the "ONGOING" state in the session. New
461 * rotations can start now.
463 chunk_status
= lttng_trace_chunk_get_name(
464 session
->chunk_being_archived
, &archived_chunk_name
, nullptr);
465 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
466 free(session
->last_archived_chunk_name
);
467 session
->last_archived_chunk_name
= strdup(archived_chunk_name
);
468 if (!session
->last_archived_chunk_name
) {
469 PERROR("Failed to duplicate archived chunk name");
471 session_reset_rotation_state(session
, LTTNG_ROTATION_STATE_COMPLETED
);
473 if (!session
->quiet_rotation
) {
474 location
= session_get_trace_archive_location(session
);
475 ret
= notification_thread_command_session_rotation_completed(
476 notification_thread_handle
,
478 session
->last_archived_chunk_id
.value
,
480 lttng_trace_archive_location_put(location
);
481 if (ret
!= LTTNG_OK
) {
482 ERR("Failed to notify notification thread of completed rotation for session %s",
488 check_ongoing_rotation
:
489 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
490 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
,
491 &chunk_being_archived_id
);
492 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
494 DBG("Rotation of trace archive %" PRIu64
" is still pending for session %s",
495 chunk_being_archived_id
,
497 ret
= timer_session_rotation_pending_check_start(session
,
498 DEFAULT_ROTATE_PENDING_TIMER
);
500 ERR("Failed to re-enable rotation pending timer");
510 /* Call with the session and session_list locks held. */
511 static int launch_session_rotation(struct ltt_session
*session
)
514 struct lttng_rotate_session_return rotation_return
;
516 DBG("Launching scheduled time-based rotation on session \"%s\"", session
->name
);
518 ret
= cmd_rotate_session(
519 session
, &rotation_return
, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
520 if (ret
== LTTNG_OK
) {
521 DBG("Scheduled time-based rotation successfully launched on session \"%s\"",
524 /* Don't consider errors as fatal. */
525 DBG("Scheduled time-based rotation aborted for session %s: %s",
527 lttng_strerror(ret
));
532 static int run_job(struct rotation_thread_job
*job
,
533 struct ltt_session
*session
,
534 struct notification_thread_handle
*notification_thread_handle
)
539 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION
:
540 ret
= launch_session_rotation(session
);
542 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION
:
543 ret
= check_session_rotation_pending(session
, notification_thread_handle
);
551 static int handle_job_queue(struct rotation_thread_handle
*handle
,
552 struct rotation_thread
*state
__attribute__((unused
)),
553 struct rotation_thread_timer_queue
*queue
)
558 struct ltt_session
*session
;
559 struct rotation_thread_job
*job
;
561 /* Take the queue lock only to pop an element from the list. */
562 pthread_mutex_lock(&queue
->lock
);
563 if (cds_list_empty(&queue
->list
)) {
564 pthread_mutex_unlock(&queue
->lock
);
567 job
= cds_list_first_entry(&queue
->list
, typeof(*job
), head
);
568 cds_list_del(&job
->head
);
569 pthread_mutex_unlock(&queue
->lock
);
572 session
= job
->session
;
574 DBG("Session \"%s\" not found", session
->name
!= NULL
? session
->name
: "");
576 * This is a non-fatal error, and we cannot report it to
577 * the user (timer), so just print the error and
578 * continue the processing.
580 * While the timer thread will purge pending signals for
581 * a session on the session's destruction, it is
582 * possible for a job targeting that session to have
583 * already been queued before it was destroyed.
586 session_put(session
);
587 session_unlock_list();
591 session_lock(session
);
592 ret
= run_job(job
, session
, handle
->notification_thread_handle
);
593 session_unlock(session
);
594 /* Release reference held by the job. */
595 session_put(session
);
596 session_unlock_list();
609 static int handle_condition(const struct lttng_notification
*notification
,
610 struct notification_thread_handle
*notification_thread_handle
)
613 const char *condition_session_name
= nullptr;
614 enum lttng_condition_type condition_type
;
615 enum lttng_condition_status condition_status
;
616 enum lttng_evaluation_status evaluation_status
;
618 struct ltt_session
*session
;
619 const struct lttng_condition
*condition
=
620 lttng_notification_get_const_condition(notification
);
621 const struct lttng_evaluation
*evaluation
=
622 lttng_notification_get_const_evaluation(notification
);
624 condition_type
= lttng_condition_get_type(condition
);
626 if (condition_type
!= LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
) {
628 ERR("Condition type and session usage type are not the same");
632 /* Fetch info to test */
633 condition_status
= lttng_condition_session_consumed_size_get_session_name(
634 condition
, &condition_session_name
);
635 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
636 ERR("Session name could not be fetched");
641 lttng_evaluation_session_consumed_size_get_consumed_size(evaluation
, &consumed
);
642 if (evaluation_status
!= LTTNG_EVALUATION_STATUS_OK
) {
643 ERR("Failed to get evaluation");
649 session
= session_find_by_name(condition_session_name
);
651 DBG("Failed to find session while handling notification: notification type = %s, session name = `%s`",
652 lttng_condition_type_str(condition_type
),
653 condition_session_name
);
655 * Not a fatal error: a session can be destroyed before we get
656 * the chance to handle the notification.
659 session_unlock_list();
662 session_lock(session
);
664 if (!lttng_trigger_is_equal(session
->rotate_trigger
,
665 lttng_notification_get_const_trigger(notification
))) {
666 /* Notification does not originate from our rotation trigger. */
671 ret
= unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
676 ret
= cmd_rotate_session(
677 session
, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
681 case -LTTNG_ERR_ROTATION_PENDING
:
682 DBG("Rotate already pending, subscribe to the next threshold value");
684 case -LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
:
685 DBG("Rotation already happened since last stop, subscribe to the next threshold value");
687 case -LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
:
688 DBG("Rotation already happened since last stop and clear, subscribe to the next threshold value");
691 ERR("Failed to rotate on size notification with error: %s", lttng_strerror(ret
));
696 ret
= subscribe_session_consumed_size_rotation(
697 session
, consumed
+ session
->rotate_size
, 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();
712 static int handle_notification_channel(int fd
__attribute__((unused
)),
713 struct rotation_thread_handle
*handle
,
714 struct rotation_thread
*state
__attribute__((unused
)))
717 bool notification_pending
;
718 struct lttng_notification
*notification
= nullptr;
719 enum lttng_notification_channel_status status
;
721 status
= lttng_notification_channel_has_pending_notification(rotate_notification_channel
,
722 ¬ification_pending
);
723 if (status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
724 ERR("Error occurred while checking for pending notification");
729 if (!notification_pending
) {
734 /* Receive the next notification. */
735 status
= lttng_notification_channel_get_next_notification(rotate_notification_channel
,
739 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
741 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
742 /* Not an error, we will wait for the next one */
746 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
747 ERR("Notification channel was closed");
751 /* Unhandled conditions / errors. */
752 ERR("Unknown notification channel status");
757 ret
= handle_condition(notification
, handle
->notification_thread_handle
);
760 lttng_notification_destroy(notification
);
764 static void *thread_rotation(void *data
)
767 struct rotation_thread_handle
*handle
= (rotation_thread_handle
*) data
;
768 struct rotation_thread thread
;
771 DBG("Started rotation thread");
772 rcu_register_thread();
774 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_ROTATION
);
775 health_code_update();
778 ERR("Invalid thread context provided");
782 queue_pipe_fd
= lttng_pipe_get_readfd(handle
->rotation_timer_queue
->event_pipe
);
784 ret
= init_thread_state(handle
, &thread
);
793 DBG("Entering poll wait");
794 ret
= lttng_poll_wait(&thread
.events
, -1);
795 DBG("Poll wait returned (%i)", ret
);
799 * Restart interrupted system call.
801 if (errno
== EINTR
) {
804 ERR("Error encountered during lttng_poll_wait (%i)", ret
);
809 for (i
= 0; i
< fd_count
; i
++) {
810 int fd
= LTTNG_POLL_GETFD(&thread
.events
, i
);
811 uint32_t revents
= LTTNG_POLL_GETEV(&thread
.events
, i
);
813 DBG("Handling fd (%i) activity (%u)", fd
, revents
);
815 if (revents
& LPOLLERR
) {
816 ERR("Polling returned an error on fd %i", fd
);
820 if (fd
== rotate_notification_channel
->socket
) {
821 ret
= handle_notification_channel(fd
, handle
, &thread
);
823 ERR("Error occurred while handling activity on notification channel socket");
827 /* Job queue or quit pipe activity. */
830 * The job queue is serviced if there is
831 * activity on the quit pipe to ensure it is
832 * flushed and all references held in the queue
835 ret
= handle_job_queue(
836 handle
, &thread
, handle
->rotation_timer_queue
);
838 ERR("Failed to handle rotation timer pipe event");
842 if (fd
== queue_pipe_fd
) {
845 ret
= lttng_read(fd
, &buf
, 1);
847 ERR("Failed to read from wakeup pipe (fd = %i)",
852 DBG("Quit pipe activity");
861 fini_thread_state(&thread
);
863 health_unregister(the_health_sessiond
);
864 rcu_thread_offline();
865 rcu_unregister_thread();
869 static bool shutdown_rotation_thread(void *thread_data
)
871 struct rotation_thread_handle
*handle
= (rotation_thread_handle
*) thread_data
;
872 const int write_fd
= lttng_pipe_get_writefd(handle
->quit_pipe
);
874 return notify_thread_pipe(write_fd
) == 1;
877 bool launch_rotation_thread(struct rotation_thread_handle
*handle
)
879 struct lttng_thread
*thread
;
881 thread
= lttng_thread_create(
882 "Rotation", thread_rotation
, shutdown_rotation_thread
, nullptr, handle
);
886 lttng_thread_put(thread
);