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>
19 #include <sys/eventfd.h>
25 #include <common/kernel-ctl/kernel-ctl.h>
26 #include <lttng/notification/channel-internal.h>
27 #include <lttng/rotate-internal.h>
28 #include <lttng/location-internal.h>
29 #include <lttng/condition/condition-internal.h>
30 #include <lttng/notification/notification-internal.h>
32 #include "rotation-thread.h"
33 #include "lttng-sessiond.h"
34 #include "health-sessiond.h"
39 #include "notification-thread-commands.h"
44 #include <urcu/list.h>
46 struct lttng_notification_channel
*rotate_notification_channel
= NULL
;
48 struct rotation_thread
{
49 struct lttng_poll_event events
;
52 struct rotation_thread_job
{
53 enum rotation_thread_job_type type
;
54 struct ltt_session
*session
;
55 /* List member in struct rotation_thread_timer_queue. */
56 struct cds_list_head head
;
60 * The timer thread enqueues jobs and wakes up the rotation thread.
61 * When the rotation thread wakes up, it empties the queue.
63 struct rotation_thread_timer_queue
{
64 struct lttng_pipe
*event_pipe
;
65 struct cds_list_head list
;
69 struct rotation_thread_handle
{
70 struct rotation_thread_timer_queue
*rotation_timer_queue
;
71 /* Access to the notification thread cmd_queue */
72 struct notification_thread_handle
*notification_thread_handle
;
73 /* Thread-specific quit pipe. */
74 struct lttng_pipe
*quit_pipe
;
78 const char *get_job_type_str(enum rotation_thread_job_type job_type
)
81 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION
:
82 return "CHECK_PENDING_ROTATION";
83 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION
:
84 return "SCHEDULED_ROTATION";
90 struct rotation_thread_timer_queue
*rotation_thread_timer_queue_create(void)
92 struct rotation_thread_timer_queue
*queue
= NULL
;
94 queue
= zmalloc(sizeof(*queue
));
96 PERROR("Failed to allocate timer rotate queue");
100 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
101 CDS_INIT_LIST_HEAD(&queue
->list
);
102 pthread_mutex_init(&queue
->lock
, NULL
);
107 void rotation_thread_timer_queue_destroy(
108 struct rotation_thread_timer_queue
*queue
)
114 lttng_pipe_destroy(queue
->event_pipe
);
116 pthread_mutex_lock(&queue
->lock
);
117 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(
127 struct rotation_thread_handle
*handle
)
129 lttng_pipe_destroy(handle
->quit_pipe
);
133 struct rotation_thread_handle
*rotation_thread_handle_create(
134 struct rotation_thread_timer_queue
*rotation_timer_queue
,
135 struct notification_thread_handle
*notification_thread_handle
)
137 struct rotation_thread_handle
*handle
;
139 handle
= zmalloc(sizeof(*handle
));
144 handle
->rotation_timer_queue
= rotation_timer_queue
;
145 handle
->notification_thread_handle
= notification_thread_handle
;
146 handle
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
147 if (!handle
->quit_pipe
) {
154 rotation_thread_handle_destroy(handle
);
159 * Called with the rotation_thread_timer_queue lock held.
160 * Return true if the same timer job already exists in the queue, false if not.
163 bool timer_job_exists(const struct rotation_thread_timer_queue
*queue
,
164 enum rotation_thread_job_type job_type
,
165 struct ltt_session
*session
)
168 struct rotation_thread_job
*job
;
170 cds_list_for_each_entry(job
, &queue
->list
, head
) {
171 if (job
->session
== session
&& job
->type
== job_type
) {
180 void rotation_thread_enqueue_job(struct rotation_thread_timer_queue
*queue
,
181 enum rotation_thread_job_type job_type
,
182 struct ltt_session
*session
)
185 const char dummy
= '!';
186 struct rotation_thread_job
*job
= NULL
;
187 const char *job_type_str
= get_job_type_str(job_type
);
189 pthread_mutex_lock(&queue
->lock
);
190 if (timer_job_exists(queue
, job_type
, session
)) {
192 * This timer job is already pending, we don't need to add
198 job
= zmalloc(sizeof(struct rotation_thread_job
));
200 PERROR("Failed to allocate rotation thread job of type \"%s\" for session \"%s\"",
201 job_type_str
, session
->name
);
204 /* No reason for this to fail as the caller must hold a reference. */
205 (void) session_get(session
);
207 job
->session
= session
;
208 job
->type
= job_type
;
209 cds_list_add_tail(&job
->head
, &queue
->list
);
211 ret
= lttng_write(lttng_pipe_get_writefd(queue
->event_pipe
), &dummy
,
215 * We do not want to block in the timer handler, the job has
216 * been enqueued in the list, the wakeup pipe is probably full,
217 * the job will be processed when the rotation_thread catches
220 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
222 * Not an error, but would be surprising and indicate
223 * that the rotation thread can't keep up with the
226 DBG("Wake-up pipe of rotation thread job queue is full");
229 PERROR("Failed to wake-up the rotation thread after pushing a job of type \"%s\" for session \"%s\"",
230 job_type_str
, session
->name
);
235 pthread_mutex_unlock(&queue
->lock
);
239 int init_poll_set(struct lttng_poll_event
*poll_set
,
240 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
,
256 lttng_pipe_get_readfd(handle
->quit_pipe
),
259 ERR("[rotation-thread] Failed to add quit pipe read fd to poll set");
263 ret
= lttng_poll_add(poll_set
,
264 lttng_pipe_get_readfd(handle
->rotation_timer_queue
->event_pipe
),
267 ERR("[rotation-thread] Failed to add rotate_pending fd to poll set");
273 lttng_poll_clean(poll_set
);
278 void fini_thread_state(struct rotation_thread
*state
)
280 lttng_poll_clean(&state
->events
);
281 if (rotate_notification_channel
) {
282 lttng_notification_channel_destroy(rotate_notification_channel
);
287 int init_thread_state(struct rotation_thread_handle
*handle
,
288 struct rotation_thread
*state
)
292 memset(state
, 0, sizeof(*state
));
293 lttng_poll_init(&state
->events
);
295 ret
= init_poll_set(&state
->events
, handle
);
297 ERR("[rotation-thread] Failed to initialize rotation thread poll set");
301 rotate_notification_channel
= lttng_notification_channel_create(
302 lttng_session_daemon_notification_endpoint
);
303 if (!rotate_notification_channel
) {
304 ERR("[rotation-thread] Could not create notification channel");
308 ret
= lttng_poll_add(&state
->events
, rotate_notification_channel
->socket
,
311 ERR("[rotation-thread] Failed to add notification fd to pollset");
320 void check_session_rotation_pending_on_consumers(struct ltt_session
*session
,
321 bool *_rotation_completed
)
324 struct consumer_socket
*socket
;
325 struct cds_lfht_iter iter
;
326 enum consumer_trace_chunk_exists_status exists_status
;
328 bool chunk_exists_on_peer
= false;
329 enum lttng_trace_chunk_status chunk_status
;
331 assert(session
->chunk_being_archived
);
334 * Check for a local pending rotation on all consumers (32-bit
335 * user space, 64-bit user space, and kernel).
338 if (!session
->ust_session
) {
341 cds_lfht_for_each_entry(session
->ust_session
->consumer
->socks
->ht
,
342 &iter
, socket
, node
.node
) {
343 relayd_id
= session
->ust_session
->consumer
->type
== CONSUMER_DST_LOCAL
?
345 session
->ust_session
->consumer
->net_seq_index
;
347 pthread_mutex_lock(socket
->lock
);
348 ret
= consumer_trace_chunk_exists(socket
,
350 session
->id
, session
->chunk_being_archived
,
353 pthread_mutex_unlock(socket
->lock
);
354 ERR("Error occurred while checking rotation status on consumer daemon");
358 if (exists_status
!= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
) {
359 pthread_mutex_unlock(socket
->lock
);
360 chunk_exists_on_peer
= true;
363 pthread_mutex_unlock(socket
->lock
);
367 if (!session
->kernel_session
) {
370 cds_lfht_for_each_entry(session
->kernel_session
->consumer
->socks
->ht
,
371 &iter
, socket
, node
.node
) {
372 pthread_mutex_lock(socket
->lock
);
373 relayd_id
= session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
?
375 session
->kernel_session
->consumer
->net_seq_index
;
377 ret
= consumer_trace_chunk_exists(socket
,
379 session
->id
, session
->chunk_being_archived
,
382 pthread_mutex_unlock(socket
->lock
);
383 ERR("Error occurred while checking rotation status on consumer daemon");
387 if (exists_status
!= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
) {
388 pthread_mutex_unlock(socket
->lock
);
389 chunk_exists_on_peer
= true;
392 pthread_mutex_unlock(socket
->lock
);
398 if (!chunk_exists_on_peer
) {
399 uint64_t chunk_being_archived_id
;
401 chunk_status
= lttng_trace_chunk_get_id(
402 session
->chunk_being_archived
,
403 &chunk_being_archived_id
);
404 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
405 DBG("[rotation-thread] Rotation of trace archive %" PRIu64
" of session \"%s\" is complete on all consumers",
406 chunk_being_archived_id
,
409 *_rotation_completed
= !chunk_exists_on_peer
;
411 ret
= session_reset_rotation_state(session
,
412 LTTNG_ROTATION_STATE_ERROR
);
414 ERR("Failed to reset rotation state of session \"%s\"",
421 * Check if the last rotation was completed, called with session lock held.
422 * Should only return non-zero in the event of a fatal error. Doing so will
423 * shutdown the thread.
426 int check_session_rotation_pending(struct ltt_session
*session
,
427 struct notification_thread_handle
*notification_thread_handle
)
430 struct lttng_trace_archive_location
*location
;
431 enum lttng_trace_chunk_status chunk_status
;
432 bool rotation_completed
= false;
433 const char *archived_chunk_name
;
434 uint64_t chunk_being_archived_id
;
436 if (!session
->chunk_being_archived
) {
441 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
,
442 &chunk_being_archived_id
);
443 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
445 DBG("[rotation-thread] Checking for pending rotation on session \"%s\", trace archive %" PRIu64
,
446 session
->name
, chunk_being_archived_id
);
449 * The rotation-pending check timer of a session is launched in
450 * one-shot mode. If the rotation is incomplete, the rotation
451 * thread will re-enable the pending-check timer.
453 * The timer thread can't stop the timer itself since it is involved
454 * in the check for the timer's quiescence.
456 ret
= timer_session_rotation_pending_check_stop(session
);
458 goto check_ongoing_rotation
;
461 check_session_rotation_pending_on_consumers(session
,
462 &rotation_completed
);
463 if (!rotation_completed
||
464 session
->rotation_state
== LTTNG_ROTATION_STATE_ERROR
) {
465 goto check_ongoing_rotation
;
469 * Now we can clear the "ONGOING" state in the session. New
470 * rotations can start now.
472 chunk_status
= lttng_trace_chunk_get_name(session
->chunk_being_archived
,
473 &archived_chunk_name
, NULL
);
474 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
475 free(session
->last_archived_chunk_name
);
476 session
->last_archived_chunk_name
= strdup(archived_chunk_name
);
477 if (!session
->last_archived_chunk_name
) {
478 PERROR("Failed to duplicate archived chunk name");
480 session_reset_rotation_state(session
, LTTNG_ROTATION_STATE_COMPLETED
);
482 if (!session
->quiet_rotation
) {
483 location
= session_get_trace_archive_location(session
);
484 ret
= notification_thread_command_session_rotation_completed(
485 notification_thread_handle
,
489 session
->last_archived_chunk_id
.value
,
491 lttng_trace_archive_location_put(location
);
492 if (ret
!= LTTNG_OK
) {
493 ERR("[rotation-thread] Failed to notify notification thread of completed rotation for session %s",
499 check_ongoing_rotation
:
500 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
501 uint64_t chunk_being_archived_id
;
503 chunk_status
= lttng_trace_chunk_get_id(
504 session
->chunk_being_archived
,
505 &chunk_being_archived_id
);
506 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
508 DBG("[rotation-thread] Rotation of trace archive %" PRIu64
" is still pending for session %s",
509 chunk_being_archived_id
, session
->name
);
510 ret
= timer_session_rotation_pending_check_start(session
,
511 DEFAULT_ROTATE_PENDING_TIMER
);
513 ERR("Failed to re-enable rotation pending timer");
523 /* Call with the session and session_list locks held. */
525 int launch_session_rotation(struct ltt_session
*session
)
528 struct lttng_rotate_session_return rotation_return
;
530 DBG("[rotation-thread] Launching scheduled time-based rotation on session \"%s\"",
533 ret
= cmd_rotate_session(session
, &rotation_return
, false,
534 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
535 if (ret
== LTTNG_OK
) {
536 DBG("[rotation-thread] Scheduled time-based rotation successfully launched on session \"%s\"",
539 /* Don't consider errors as fatal. */
540 DBG("[rotation-thread] Scheduled time-based rotation aborted for session %s: %s",
541 session
->name
, lttng_strerror(ret
));
547 int run_job(struct rotation_thread_job
*job
, struct ltt_session
*session
,
548 struct notification_thread_handle
*notification_thread_handle
)
553 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION
:
554 ret
= launch_session_rotation(session
);
556 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION
:
557 ret
= check_session_rotation_pending(session
,
558 notification_thread_handle
);
567 int handle_job_queue(struct rotation_thread_handle
*handle
,
568 struct rotation_thread
*state
,
569 struct rotation_thread_timer_queue
*queue
)
574 struct ltt_session
*session
;
575 struct rotation_thread_job
*job
;
577 /* Take the queue lock only to pop an element from the list. */
578 pthread_mutex_lock(&queue
->lock
);
579 if (cds_list_empty(&queue
->list
)) {
580 pthread_mutex_unlock(&queue
->lock
);
583 job
= cds_list_first_entry(&queue
->list
,
585 cds_list_del(&job
->head
);
586 pthread_mutex_unlock(&queue
->lock
);
589 session
= job
->session
;
591 DBG("[rotation-thread] Session not found");
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_unlock_list();
607 session_lock(session
);
608 ret
= run_job(job
, session
, handle
->notification_thread_handle
);
609 session_unlock(session
);
610 /* Release reference held by the job. */
611 session_put(session
);
612 session_unlock_list();
626 int handle_condition(const struct lttng_notification
*notification
,
627 struct notification_thread_handle
*notification_thread_handle
)
630 const char *condition_session_name
= NULL
;
631 enum lttng_condition_type condition_type
;
632 enum lttng_condition_status condition_status
;
633 enum lttng_evaluation_status evaluation_status
;
635 struct ltt_session
*session
;
636 const struct lttng_condition
*condition
=
637 lttng_notification_get_const_condition(notification
);
638 const struct lttng_evaluation
*evaluation
=
639 lttng_notification_get_const_evaluation(notification
);
641 condition_type
= lttng_condition_get_type(condition
);
643 if (condition_type
!= LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
) {
645 ERR("[rotation-thread] Condition type and session usage type are not the same");
649 /* Fetch info to test */
650 condition_status
= lttng_condition_session_consumed_size_get_session_name(
651 condition
, &condition_session_name
);
652 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
653 ERR("[rotation-thread] Session name could not be fetched");
657 evaluation_status
= lttng_evaluation_session_consumed_size_get_consumed_size(evaluation
,
659 if (evaluation_status
!= LTTNG_EVALUATION_STATUS_OK
) {
660 ERR("[rotation-thread] Failed to get evaluation");
666 session
= session_find_by_name(condition_session_name
);
668 DBG("[rotation-thread] Failed to find session while handling notification: session name = `%s`",
669 condition_session_name
);
671 * Not a fatal error: a session can be destroyed before we get
672 * the chance to handle the notification.
675 session_unlock_list();
678 session_lock(session
);
680 if (!lttng_trigger_is_equal(session
->rotate_trigger
,
681 lttng_notification_get_const_trigger(notification
))) {
682 /* Notification does not originate from our rotation trigger. */
687 ret
= unsubscribe_session_consumed_size_rotation(session
,
688 notification_thread_handle
);
693 ret
= cmd_rotate_session(session
, NULL
, false,
694 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
695 if (ret
== -LTTNG_ERR_ROTATION_PENDING
) {
696 DBG("Rotate already pending, subscribe to the next threshold value");
697 } else if (ret
!= LTTNG_OK
) {
698 ERR("[rotation-thread] Failed to rotate on size notification with error: %s",
699 lttng_strerror(ret
));
703 ret
= subscribe_session_consumed_size_rotation(session
,
704 consumed
+ session
->rotate_size
,
705 notification_thread_handle
);
707 ERR("[rotation-thread] Failed to subscribe to session consumed size condition");
713 session_unlock(session
);
714 session_put(session
);
715 session_unlock_list();
721 int handle_notification_channel(int fd
,
722 struct rotation_thread_handle
*handle
,
723 struct rotation_thread
*state
)
726 bool notification_pending
;
727 struct lttng_notification
*notification
= NULL
;
728 enum lttng_notification_channel_status status
;
730 status
= lttng_notification_channel_has_pending_notification(
731 rotate_notification_channel
, ¬ification_pending
);
732 if (status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
733 ERR("[rotation-thread ]Error occurred while checking for pending notification");
738 if (!notification_pending
) {
743 /* Receive the next notification. */
744 status
= lttng_notification_channel_get_next_notification(
745 rotate_notification_channel
,
749 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
751 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
752 /* Not an error, we will wait for the next one */
755 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
756 ERR("Notification channel was closed");
760 /* Unhandled conditions / errors. */
761 ERR("Unknown notification channel status");
766 ret
= handle_condition(notification
,
767 handle
->notification_thread_handle
);
770 lttng_notification_destroy(notification
);
775 void *thread_rotation(void *data
)
778 struct rotation_thread_handle
*handle
= data
;
779 struct rotation_thread thread
;
782 DBG("[rotation-thread] Started rotation thread");
783 rcu_register_thread();
785 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_ROTATION
);
786 health_code_update();
789 ERR("[rotation-thread] Invalid thread context provided");
793 queue_pipe_fd
= lttng_pipe_get_readfd(
794 handle
->rotation_timer_queue
->event_pipe
);
797 ret
= init_thread_state(handle
, &thread
);
806 DBG("[rotation-thread] Entering poll wait");
807 ret
= lttng_poll_wait(&thread
.events
, -1);
808 DBG("[rotation-thread] Poll wait returned (%i)", ret
);
812 * Restart interrupted system call.
814 if (errno
== EINTR
) {
817 ERR("[rotation-thread] Error encountered during lttng_poll_wait (%i)", ret
);
822 for (i
= 0; i
< fd_count
; i
++) {
823 int fd
= LTTNG_POLL_GETFD(&thread
.events
, i
);
824 uint32_t revents
= LTTNG_POLL_GETEV(&thread
.events
, i
);
826 DBG("[rotation-thread] Handling fd (%i) activity (%u)",
829 if (revents
& LPOLLERR
) {
830 ERR("[rotation-thread] Polling returned an error on fd %i", fd
);
834 if (fd
== rotate_notification_channel
->socket
) {
835 ret
= handle_notification_channel(fd
, handle
,
838 ERR("[rotation-thread] Error occurred while handling activity on notification channel socket");
842 /* Job queue or quit pipe activity. */
845 * The job queue is serviced if there is
846 * activity on the quit pipe to ensure it is
847 * flushed and all references held in the queue
850 ret
= handle_job_queue(handle
, &thread
,
851 handle
->rotation_timer_queue
);
853 ERR("[rotation-thread] Failed to handle rotation timer pipe event");
857 if (fd
== queue_pipe_fd
) {
860 ret
= lttng_read(fd
, &buf
, 1);
862 ERR("[rotation-thread] Failed to read from wakeup pipe (fd = %i)", fd
);
866 DBG("[rotation-thread] Quit pipe activity");
874 DBG("[rotation-thread] Exit");
875 fini_thread_state(&thread
);
877 health_unregister(health_sessiond
);
878 rcu_thread_offline();
879 rcu_unregister_thread();
884 bool shutdown_rotation_thread(void *thread_data
)
886 struct rotation_thread_handle
*handle
= thread_data
;
887 const int write_fd
= lttng_pipe_get_writefd(handle
->quit_pipe
);
889 return notify_thread_pipe(write_fd
) == 1;
892 bool launch_rotation_thread(struct rotation_thread_handle
*handle
)
894 struct lttng_thread
*thread
;
896 thread
= lttng_thread_create("Rotation",
898 shutdown_rotation_thread
,
904 lttng_thread_put(thread
);