Rotate timer
[lttng-tools.git] / src / bin / lttng-sessiond / rotation-thread.c
index b245f75adaa89f372b66bb83dc16769b5a0e7c23..ecccc7bb01ab00bbbba7dd71675b88a7f4b08e3d 100644 (file)
@@ -33,6 +33,7 @@
 
 #include <common/kernel-ctl/kernel-ctl.h>
 #include <lttng/notification/channel-internal.h>
+#include <lttng/rotate-internal.h>
 
 #include "rotation-thread.h"
 #include "lttng-sessiond.h"
@@ -40,6 +41,7 @@
 #include "rotate.h"
 #include "cmd.h"
 #include "session.h"
+#include "sessiond-timer.h"
 
 #include <urcu.h>
 #include <urcu/list.h>
@@ -140,7 +142,8 @@ struct rotation_thread_handle *rotation_thread_handle_create(
                struct lttng_pipe *ust32_channel_rotate_pipe,
                struct lttng_pipe *ust64_channel_rotate_pipe,
                struct lttng_pipe *kernel_channel_rotate_pipe,
-               int thread_quit_pipe)
+               int thread_quit_pipe,
+               struct rotation_thread_timer_queue *rotation_timer_queue)
 {
        struct rotation_thread_handle *handle;
 
@@ -180,6 +183,7 @@ struct rotation_thread_handle *rotation_thread_handle_create(
                handle->kernel_consumer = -1;
        }
        handle->thread_quit_pipe = thread_quit_pipe;
+       handle->rotation_timer_queue = rotation_timer_queue;
 
 end:
        return handle;
@@ -195,13 +199,14 @@ int init_poll_set(struct lttng_poll_event *poll_set,
        int ret;
 
        /*
-        * Create pollset with size 4:
+        * Create pollset with size 5:
         *      - sessiond quit pipe
+        *      - sessiond timer pipe,
         *      - consumerd (32-bit user space) channel rotate pipe,
         *      - consumerd (64-bit user space) channel rotate pipe,
         *      - consumerd (kernel) channel rotate pipe,
         */
-       ret = lttng_poll_create(poll_set, 4, LTTNG_CLOEXEC);
+       ret = lttng_poll_create(poll_set, 5, LTTNG_CLOEXEC);
        if (ret < 0) {
                goto end;
        }
@@ -212,6 +217,13 @@ int init_poll_set(struct lttng_poll_event *poll_set,
                ERR("[rotation-thread] Failed to add thread_quit_pipe fd to pollset");
                goto error;
        }
+       ret = lttng_poll_add(poll_set,
+                       lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe),
+                       LPOLLIN | LPOLLERR);
+       if (ret < 0) {
+               ERR("[rotation-thread] Failed to add rotate_pending fd to pollset");
+               goto error;
+       }
        ret = lttng_poll_add(poll_set, handle->ust32_consumer,
                        LPOLLIN | LPOLLERR);
        if (ret < 0) {
@@ -344,7 +356,7 @@ int handle_channel_rotation_pipe(int fd, uint32_t revents,
                time_t now = time(NULL);
 
                if (now == (time_t) -1) {
-                       session->rotation_status = LTTNG_ROTATION_STATUS_ERROR;
+                       session->rotation_state = LTTNG_ROTATION_STATE_ERROR;
                        ret = LTTNG_ERR_UNK;
                        goto end_unlock_session;
                }
@@ -355,8 +367,18 @@ int handle_channel_rotation_pipe(int fd, uint32_t revents,
                        goto end_unlock_session;
                }
                session->rotate_pending = false;
-               session->rotation_status = LTTNG_ROTATION_STATUS_COMPLETED;
+               session->rotation_state = LTTNG_ROTATION_STATE_COMPLETED;
                session->last_chunk_start_ts = session->current_chunk_start_ts;
+               if (session->rotate_pending_relay) {
+                       ret = sessiond_timer_rotate_pending_start(
+                                       session,
+                                       DEFAULT_ROTATE_PENDING_RELAY_TIMER);
+                       if (ret) {
+                               ERR("Failed to enable rotate pending timer");
+                               ret = -1;
+                               goto end_unlock_session;
+                       }
+               }
                DBG("Rotation completed for session %s", session->name);
        }
 
@@ -372,6 +394,178 @@ end:
        return ret;
 }
 
+/*
+ * Process the rotate_pending check, called with session lock held.
+ */
+static
+int rotate_pending_relay_timer(struct ltt_session *session)
+{
+       int ret;
+
+       DBG("[rotation-thread] Check rotate pending on session %" PRIu64,
+                       session->id);
+       ret = relay_rotate_pending(session, session->rotate_count - 1);
+       if (ret < 0) {
+               ERR("[rotation-thread] Check relay rotate pending");
+               goto end;
+       }
+       if (ret == 0) {
+               DBG("[rotation-thread] Rotation completed on the relay for "
+                               "session %" PRIu64, session->id);
+               /*
+                * Now we can clear the pending flag in the session. New
+                * rotations can start now.
+                */
+               session->rotate_pending_relay = false;
+       } else if (ret == 1) {
+               DBG("[rotation-thread] Rotation still pending on the relay for "
+                               "session %" PRIu64, session->id);
+               ret = sessiond_timer_rotate_pending_start(session,
+                               DEFAULT_ROTATE_PENDING_RELAY_TIMER);
+               if (ret) {
+                       ERR("Re-enabling rotate pending timer");
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       ret = 0;
+
+end:
+       return ret;
+}
+
+/*
+ * Process the rotate_timer, called with session lock held.
+ */
+static
+int rotate_timer(struct ltt_session *session)
+{
+       int ret;
+
+       /*
+        * Complete _at most_ one scheduled rotation on a stopped session.
+        */
+       if (!session->active && session->rotate_timer_enabled &&
+                       session->rotated_after_last_stop) {
+               ret = 0;
+               goto end;
+       }
+
+       /* Ignore this timer if a rotation is already in progress. */
+       if (session->rotate_pending || session->rotate_pending_relay) {
+               ret = 0;
+               goto end;
+       }
+
+       DBG("[rotation-thread] Rotate timer on session %s", session->name);
+
+       ret = cmd_rotate_session(session, NULL);
+       if (ret == -LTTNG_ERR_ROTATION_PENDING) {
+               DBG("Scheduled rotation aborted since a rotation is already in progress");
+               ret = 0;
+               goto end;
+       } else if (ret != LTTNG_OK) {
+               ERR("[rotation-thread] Automatic time-triggered rotation failed with error code %i", ret);
+               ret = -1;
+               goto end;
+       }
+
+       ret = 0;
+
+end:
+       return ret;
+}
+
+static
+int handle_rotate_timer_pipe(uint32_t revents,
+               struct rotation_thread_handle *handle,
+               struct rotation_thread_state *state,
+               struct rotation_thread_timer_queue *queue)
+{
+       int ret = 0;
+       int fd = lttng_pipe_get_readfd(queue->event_pipe);
+       struct ltt_session *session;
+       char buf[1];
+
+       if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+               ret = lttng_poll_del(&state->events, fd);
+               if (ret) {
+                       ERR("[rotation-thread] Failed to remove consumer "
+                                       "rotate pending pipe from poll set");
+               }
+               goto end;
+       }
+
+       ret = lttng_read(fd, buf, 1);
+       if (ret != 1) {
+               ERR("[rotation-thread] Failed to read from wakeup pipe (fd = %i)", fd);
+               ret = -1;
+               goto end;
+       }
+
+       for (;;) {
+               struct sessiond_rotation_timer *timer_data;
+
+               /*
+                * Take the queue lock only to pop elements from the list.
+                */
+               pthread_mutex_lock(&queue->lock);
+               if (cds_list_empty(&queue->list)) {
+                       pthread_mutex_unlock(&queue->lock);
+                       break;
+               }
+               timer_data = cds_list_first_entry(&queue->list,
+                               struct sessiond_rotation_timer, head);
+               cds_list_del(&timer_data->head);
+               pthread_mutex_unlock(&queue->lock);
+
+               /*
+                * session lock to lookup the session ID.
+                */
+               session_lock_list();
+               session = session_find_by_id(timer_data->session_id);
+               if (!session) {
+                       DBG("[rotation-thread] Session %" PRIu64 " not found",
+                                       timer_data->session_id);
+                       /*
+                        * This is a non-fatal error, and we cannot report it to the
+                        * user (timer), so just print the error and continue the
+                        * processing.
+                        */
+                       session_unlock_list();
+                       free(timer_data);
+                       continue;
+               }
+
+               /*
+                * Take the session lock and release the session_list lock.
+                */
+               session_lock(session);
+               session_unlock_list();
+
+               if (timer_data->signal == LTTNG_SESSIOND_SIG_ROTATE_PENDING) {
+                       ret = rotate_pending_relay_timer(session);
+               } else if (timer_data->signal == LTTNG_SESSIOND_SIG_ROTATE_TIMER) {
+                       ret = rotate_timer(session);
+               } else {
+                       ERR("Unknown signal in rotate timer %d", timer_data->signal);
+                       ret = -1;
+               }
+               session_unlock(session);
+               free(timer_data);
+               if (ret) {
+                       ERR("Error processing timer");
+                       goto end;
+               }
+       }
+
+       ret = 0;
+
+end:
+       return ret;
+}
+
 void *thread_rotation(void *data)
 {
        int ret;
@@ -429,6 +623,13 @@ void *thread_rotation(void *data)
                        if (fd == handle->thread_quit_pipe) {
                                DBG("[rotation-thread] Quit pipe activity");
                                goto exit;
+                       } else if (fd == lttng_pipe_get_readfd(handle->rotation_timer_queue->event_pipe)) {
+                               ret = handle_rotate_timer_pipe(revents,
+                                               handle, &state, handle->rotation_timer_queue);
+                               if (ret) {
+                                       ERR("[rotation-thread] Failed to handle rotation timer pipe event");
+                                       goto error;
+                               }
                        } else if (fd == handle->ust32_consumer ||
                                        fd == handle->ust64_consumer ||
                                        fd == handle->kernel_consumer) {
This page took 0.027469 seconds and 4 git commands to generate.