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
= 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 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
= 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
= 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
218 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
220 * Not an error, but would be surprising and indicate
221 * that the rotation thread can't keep up with the
224 DBG("Wake-up pipe of rotation thread job queue is full");
227 PERROR("Failed to wake-up the rotation thread after pushing a job of type \"%s\" for session \"%s\"",
228 job_type_str
, session
->name
);
233 pthread_mutex_unlock(&queue
->lock
);
237 int init_poll_set(struct lttng_poll_event
*poll_set
,
238 struct rotation_thread_handle
*handle
)
243 * Create pollset with size 3:
244 * - rotation thread quit pipe,
245 * - rotation thread timer queue pipe,
246 * - notification channel sock,
248 ret
= lttng_poll_create(poll_set
, 5, LTTNG_CLOEXEC
);
253 ret
= lttng_poll_add(poll_set
,
254 lttng_pipe_get_readfd(handle
->quit_pipe
),
257 ERR("Failed to add quit pipe read fd to poll set");
261 ret
= lttng_poll_add(poll_set
,
262 lttng_pipe_get_readfd(handle
->rotation_timer_queue
->event_pipe
),
265 ERR("Failed to add rotate_pending fd to poll set");
271 lttng_poll_clean(poll_set
);
276 void fini_thread_state(struct rotation_thread
*state
)
278 lttng_poll_clean(&state
->events
);
279 if (rotate_notification_channel
) {
280 lttng_notification_channel_destroy(rotate_notification_channel
);
285 int init_thread_state(struct rotation_thread_handle
*handle
,
286 struct rotation_thread
*state
)
290 memset(state
, 0, sizeof(*state
));
291 lttng_poll_init(&state
->events
);
293 ret
= init_poll_set(&state
->events
, handle
);
295 ERR("Failed to initialize rotation thread poll set");
299 rotate_notification_channel
= lttng_notification_channel_create(
300 lttng_session_daemon_notification_endpoint
);
301 if (!rotate_notification_channel
) {
302 ERR("Could not create notification channel");
306 ret
= lttng_poll_add(&state
->events
, rotate_notification_channel
->socket
,
309 ERR("Failed to add notification fd to pollset");
318 void check_session_rotation_pending_on_consumers(struct ltt_session
*session
,
319 bool *_rotation_completed
)
322 struct consumer_socket
*socket
;
323 struct cds_lfht_iter iter
;
324 enum consumer_trace_chunk_exists_status exists_status
;
326 bool chunk_exists_on_peer
= false;
327 enum lttng_trace_chunk_status chunk_status
;
329 assert(session
->chunk_being_archived
);
332 * Check for a local pending rotation on all consumers (32-bit
333 * user space, 64-bit user space, and kernel).
336 if (!session
->ust_session
) {
339 cds_lfht_for_each_entry(session
->ust_session
->consumer
->socks
->ht
,
340 &iter
, socket
, node
.node
) {
341 relayd_id
= session
->ust_session
->consumer
->type
== CONSUMER_DST_LOCAL
?
343 session
->ust_session
->consumer
->net_seq_index
;
345 pthread_mutex_lock(socket
->lock
);
346 ret
= consumer_trace_chunk_exists(socket
,
348 session
->id
, session
->chunk_being_archived
,
351 pthread_mutex_unlock(socket
->lock
);
352 ERR("Error occurred while checking rotation status on consumer daemon");
356 if (exists_status
!= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
) {
357 pthread_mutex_unlock(socket
->lock
);
358 chunk_exists_on_peer
= true;
361 pthread_mutex_unlock(socket
->lock
);
365 if (!session
->kernel_session
) {
368 cds_lfht_for_each_entry(session
->kernel_session
->consumer
->socks
->ht
,
369 &iter
, socket
, node
.node
) {
370 pthread_mutex_lock(socket
->lock
);
371 relayd_id
= session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
?
373 session
->kernel_session
->consumer
->net_seq_index
;
375 ret
= consumer_trace_chunk_exists(socket
,
377 session
->id
, session
->chunk_being_archived
,
380 pthread_mutex_unlock(socket
->lock
);
381 ERR("Error occurred while checking rotation status on consumer daemon");
385 if (exists_status
!= CONSUMER_TRACE_CHUNK_EXISTS_STATUS_UNKNOWN_CHUNK
) {
386 pthread_mutex_unlock(socket
->lock
);
387 chunk_exists_on_peer
= true;
390 pthread_mutex_unlock(socket
->lock
);
396 if (!chunk_exists_on_peer
) {
397 uint64_t chunk_being_archived_id
;
399 chunk_status
= lttng_trace_chunk_get_id(
400 session
->chunk_being_archived
,
401 &chunk_being_archived_id
);
402 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
403 DBG("Rotation of trace archive %" PRIu64
" of session \"%s\" is complete on all consumers",
404 chunk_being_archived_id
,
407 *_rotation_completed
= !chunk_exists_on_peer
;
409 ret
= session_reset_rotation_state(session
,
410 LTTNG_ROTATION_STATE_ERROR
);
412 ERR("Failed to reset rotation state of session \"%s\"",
419 * Check if the last rotation was completed, called with session lock held.
420 * Should only return non-zero in the event of a fatal error. Doing so will
421 * shutdown the thread.
424 int check_session_rotation_pending(struct ltt_session
*session
,
425 struct notification_thread_handle
*notification_thread_handle
)
428 struct lttng_trace_archive_location
*location
;
429 enum lttng_trace_chunk_status chunk_status
;
430 bool rotation_completed
= false;
431 const char *archived_chunk_name
;
432 uint64_t chunk_being_archived_id
;
434 if (!session
->chunk_being_archived
) {
439 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
,
440 &chunk_being_archived_id
);
441 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
443 DBG("Checking for pending rotation on session \"%s\", trace archive %" PRIu64
,
444 session
->name
, chunk_being_archived_id
);
447 * The rotation-pending check timer of a session is launched in
448 * one-shot mode. If the rotation is incomplete, the rotation
449 * thread will re-enable the pending-check timer.
451 * The timer thread can't stop the timer itself since it is involved
452 * in the check for the timer's quiescence.
454 ret
= timer_session_rotation_pending_check_stop(session
);
456 goto check_ongoing_rotation
;
459 check_session_rotation_pending_on_consumers(session
,
460 &rotation_completed
);
461 if (!rotation_completed
||
462 session
->rotation_state
== LTTNG_ROTATION_STATE_ERROR
) {
463 goto check_ongoing_rotation
;
467 * Now we can clear the "ONGOING" state in the session. New
468 * rotations can start now.
470 chunk_status
= lttng_trace_chunk_get_name(session
->chunk_being_archived
,
471 &archived_chunk_name
, NULL
);
472 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
473 free(session
->last_archived_chunk_name
);
474 session
->last_archived_chunk_name
= strdup(archived_chunk_name
);
475 if (!session
->last_archived_chunk_name
) {
476 PERROR("Failed to duplicate archived chunk name");
478 session_reset_rotation_state(session
, LTTNG_ROTATION_STATE_COMPLETED
);
480 if (!session
->quiet_rotation
) {
481 location
= session_get_trace_archive_location(session
);
482 ret
= notification_thread_command_session_rotation_completed(
483 notification_thread_handle
,
487 session
->last_archived_chunk_id
.value
,
489 lttng_trace_archive_location_put(location
);
490 if (ret
!= LTTNG_OK
) {
491 ERR("Failed to notify notification thread of completed rotation for session %s",
497 check_ongoing_rotation
:
498 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
499 chunk_status
= lttng_trace_chunk_get_id(
500 session
->chunk_being_archived
,
501 &chunk_being_archived_id
);
502 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
504 DBG("Rotation of trace archive %" PRIu64
" is still pending for session %s",
505 chunk_being_archived_id
, session
->name
);
506 ret
= timer_session_rotation_pending_check_start(session
,
507 DEFAULT_ROTATE_PENDING_TIMER
);
509 ERR("Failed to re-enable rotation pending timer");
519 /* Call with the session and session_list locks held. */
521 int launch_session_rotation(struct ltt_session
*session
)
524 struct lttng_rotate_session_return rotation_return
;
526 DBG("Launching scheduled time-based rotation on session \"%s\"",
529 ret
= cmd_rotate_session(session
, &rotation_return
, false,
530 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
531 if (ret
== LTTNG_OK
) {
532 DBG("Scheduled time-based rotation successfully launched on session \"%s\"",
535 /* Don't consider errors as fatal. */
536 DBG("Scheduled time-based rotation aborted for session %s: %s",
537 session
->name
, lttng_strerror(ret
));
543 int run_job(struct rotation_thread_job
*job
, struct ltt_session
*session
,
544 struct notification_thread_handle
*notification_thread_handle
)
549 case ROTATION_THREAD_JOB_TYPE_SCHEDULED_ROTATION
:
550 ret
= launch_session_rotation(session
);
552 case ROTATION_THREAD_JOB_TYPE_CHECK_PENDING_ROTATION
:
553 ret
= check_session_rotation_pending(session
,
554 notification_thread_handle
);
563 int handle_job_queue(struct rotation_thread_handle
*handle
,
564 struct rotation_thread
*state
,
565 struct rotation_thread_timer_queue
*queue
)
570 struct ltt_session
*session
;
571 struct rotation_thread_job
*job
;
573 /* Take the queue lock only to pop an element from the list. */
574 pthread_mutex_lock(&queue
->lock
);
575 if (cds_list_empty(&queue
->list
)) {
576 pthread_mutex_unlock(&queue
->lock
);
579 job
= cds_list_first_entry(&queue
->list
,
581 cds_list_del(&job
->head
);
582 pthread_mutex_unlock(&queue
->lock
);
585 session
= job
->session
;
588 * This is a non-fatal error, and we cannot report it to
589 * the user (timer), so just print the error and
590 * continue the processing.
592 * While the timer thread will purge pending signals for
593 * a session on the session's destruction, it is
594 * possible for a job targeting that session to have
595 * already been queued before it was destroyed.
598 session_put(session
);
599 session_unlock_list();
603 session_lock(session
);
604 ret
= run_job(job
, session
, handle
->notification_thread_handle
);
605 session_unlock(session
);
606 /* Release reference held by the job. */
607 session_put(session
);
608 session_unlock_list();
622 int handle_condition(const struct lttng_condition
*condition
,
623 const struct lttng_evaluation
*evaluation
,
624 struct notification_thread_handle
*notification_thread_handle
)
627 const char *condition_session_name
= NULL
;
628 enum lttng_condition_type condition_type
;
629 enum lttng_condition_status condition_status
;
630 enum lttng_evaluation_status evaluation_status
;
632 struct ltt_session
*session
;
634 condition_type
= lttng_condition_get_type(condition
);
636 if (condition_type
!= LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
) {
638 ERR("Condition type and session usage type are not the same");
642 /* Fetch info to test */
643 condition_status
= lttng_condition_session_consumed_size_get_session_name(
644 condition
, &condition_session_name
);
645 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
646 ERR("Session name could not be fetched");
650 evaluation_status
= lttng_evaluation_session_consumed_size_get_consumed_size(evaluation
,
652 if (evaluation_status
!= LTTNG_EVALUATION_STATUS_OK
) {
653 ERR("Failed to get evaluation");
659 session
= session_find_by_name(condition_session_name
);
661 DBG("Failed to find session while handling notification: notification type = %s, session name = `%s`",
662 lttng_condition_type_str(condition_type
),
663 condition_session_name
);
665 * Not a fatal error: a session can be destroyed before we get
666 * the chance to handle the notification.
669 session_unlock_list();
672 session_lock(session
);
674 ret
= unsubscribe_session_consumed_size_rotation(session
,
675 notification_thread_handle
);
680 ret
= cmd_rotate_session(session
, NULL
, false,
681 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
682 if (ret
== -LTTNG_ERR_ROTATION_PENDING
) {
683 DBG("Rotate already pending, subscribe to the next threshold value");
684 } else if (ret
!= LTTNG_OK
) {
685 ERR("Failed to rotate on size notification with error: %s",
686 lttng_strerror(ret
));
690 ret
= subscribe_session_consumed_size_rotation(session
,
691 consumed
+ session
->rotate_size
,
692 notification_thread_handle
);
694 ERR("Failed to subscribe to session consumed size condition");
700 session_unlock(session
);
701 session_put(session
);
702 session_unlock_list();
708 int handle_notification_channel(int fd
,
709 struct rotation_thread_handle
*handle
,
710 struct rotation_thread
*state
)
713 bool notification_pending
;
714 struct lttng_notification
*notification
= NULL
;
715 enum lttng_notification_channel_status status
;
716 const struct lttng_evaluation
*notification_evaluation
;
717 const struct lttng_condition
*notification_condition
;
719 status
= lttng_notification_channel_has_pending_notification(
720 rotate_notification_channel
, ¬ification_pending
);
721 if (status
!= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
) {
722 ERR("Error occurred while checking for pending notification");
727 if (!notification_pending
) {
732 /* Receive the next notification. */
733 status
= lttng_notification_channel_get_next_notification(
734 rotate_notification_channel
,
738 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
740 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
741 /* Not an error, we will wait for the next one */
744 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
745 ERR("Notification channel was closed");
749 /* Unhandled conditions / errors. */
750 ERR("Unknown notification channel status");
755 notification_condition
= lttng_notification_get_condition(notification
);
756 notification_evaluation
= lttng_notification_get_evaluation(notification
);
758 ret
= handle_condition(notification_condition
, notification_evaluation
,
759 handle
->notification_thread_handle
);
762 lttng_notification_destroy(notification
);
767 void *thread_rotation(void *data
)
770 struct rotation_thread_handle
*handle
= data
;
771 struct rotation_thread thread
;
774 DBG("Started rotation thread");
775 rcu_register_thread();
777 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_ROTATION
);
778 health_code_update();
781 ERR("Invalid thread context provided");
785 queue_pipe_fd
= lttng_pipe_get_readfd(
786 handle
->rotation_timer_queue
->event_pipe
);
789 ret
= init_thread_state(handle
, &thread
);
798 DBG("Entering poll wait");
799 ret
= lttng_poll_wait(&thread
.events
, -1);
800 DBG("Poll wait returned (%i)", ret
);
804 * Restart interrupted system call.
806 if (errno
== EINTR
) {
809 ERR("Error encountered during lttng_poll_wait (%i)", ret
);
814 for (i
= 0; i
< fd_count
; i
++) {
815 int fd
= LTTNG_POLL_GETFD(&thread
.events
, i
);
816 uint32_t revents
= LTTNG_POLL_GETEV(&thread
.events
, i
);
818 DBG("Handling fd (%i) activity (%u)",
821 if (revents
& LPOLLERR
) {
822 ERR("Polling returned an error on fd %i", fd
);
826 if (fd
== rotate_notification_channel
->socket
) {
827 ret
= handle_notification_channel(fd
, handle
,
830 ERR("Error occurred while handling activity on notification channel socket");
834 /* Job queue or quit pipe activity. */
837 * The job queue is serviced if there is
838 * activity on the quit pipe to ensure it is
839 * flushed and all references held in the queue
842 ret
= handle_job_queue(handle
, &thread
,
843 handle
->rotation_timer_queue
);
845 ERR("Failed to handle rotation timer pipe event");
849 if (fd
== queue_pipe_fd
) {
852 ret
= lttng_read(fd
, &buf
, 1);
854 ERR("Failed to read from wakeup pipe (fd = %i)", fd
);
858 DBG("Quit pipe activity");
867 fini_thread_state(&thread
);
869 health_unregister(the_health_sessiond
);
870 rcu_thread_offline();
871 rcu_unregister_thread();
876 bool shutdown_rotation_thread(void *thread_data
)
878 struct rotation_thread_handle
*handle
= thread_data
;
879 const int write_fd
= lttng_pipe_get_writefd(handle
->quit_pipe
);
881 return notify_thread_pipe(write_fd
) == 1;
884 bool launch_rotation_thread(struct rotation_thread_handle
*handle
)
886 struct lttng_thread
*thread
;
888 thread
= lttng_thread_create("Rotation",
890 shutdown_rotation_thread
,
896 lttng_thread_put(thread
);