remove debug
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
index a295059fbf487ef237e97d31ffccbd9fb248fdd1..0eba587c133adb20841343840d6902aa075a21bc 100644 (file)
@@ -51,6 +51,9 @@
 #include "buffer-registry.h"
 #include "notification-thread.h"
 #include "notification-thread-commands.h"
+#include "rotate.h"
+#include "rotation-thread.h"
+#include "sessiond-timer.h"
 
 #include "cmd.h"
 
@@ -784,17 +787,17 @@ static int add_uri_to_consumer(struct consumer_output *consumer,
                break;
        case LTTNG_DST_PATH:
                DBG2("Setting trace directory path from URI to %s", uri->dst.path);
-               memset(consumer->dst.trace_path, 0,
-                               sizeof(consumer->dst.trace_path));
+               memset(consumer->dst.session_root_path, 0,
+                               sizeof(consumer->dst.session_root_path));
                /* Explicit length checks for strcpy and strcat. */
                if (strlen(uri->dst.path) + strlen(default_trace_dir)
-                               >= sizeof(consumer->dst.trace_path)) {
+                               >= sizeof(consumer->dst.session_root_path)) {
                        ret = LTTNG_ERR_FATAL;
                        goto error;
                }
-               strcpy(consumer->dst.trace_path, uri->dst.path);
+               strcpy(consumer->dst.session_root_path, uri->dst.path);
                /* Append default trace dir */
-               strcat(consumer->dst.trace_path, default_trace_dir);
+               strcat(consumer->dst.session_root_path, default_trace_dir);
                /* Flag consumer as local. */
                consumer->type = CONSUMER_DST_LOCAL;
                break;
@@ -2395,6 +2398,19 @@ int cmd_start_trace(struct ltt_session *session)
                goto error;
        }
 
+       /*
+        * Record the timestamp of the first time the session is started for
+        * an eventual session rotation call.
+        */
+       if (!session->has_been_started) {
+               session->current_chunk_start_ts = time(NULL);
+               if (session->current_chunk_start_ts == (time_t) -1) {
+                       PERROR("Get start time");
+                       ret = LTTNG_ERR_FATAL;
+                       goto error;
+               }
+       }
+
        /* Kernel tracing */
        if (ksession != NULL) {
                ret = start_kernel_session(ksession, kernel_tracer_fd);
@@ -2422,6 +2438,16 @@ int cmd_start_trace(struct ltt_session *session)
        session->has_been_started = 1;
        session->active = 1;
 
+       if (session->rotate_timer_period) {
+               ret = sessiond_rotate_timer_start(session,
+                               session->rotate_timer_period);
+               if (ret < 0) {
+                       ERR("Failed to enable rotate timer");
+                       ret = LTTNG_ERR_UNK;
+                       goto error;
+               }
+       }
+
        ret = LTTNG_OK;
 
 error:
@@ -2495,6 +2521,8 @@ int cmd_stop_trace(struct ltt_session *session)
                }
        }
 
+       session->session_last_stop_ts = time(NULL);
+
        /* Flag inactive after a successful stop. */
        session->active = 0;
        ret = LTTNG_OK;
@@ -2733,6 +2761,30 @@ int cmd_destroy_session(struct ltt_session *session, int wpipe)
        usess = session->ust_session;
        ksess = session->kernel_session;
 
+       if (session->rotate_count > 0) {
+               session->rotate_count++;
+               /*
+                * The currently active tracing path is now the folder we
+                * want to rename.
+                */
+               snprintf(session->rotation_chunk.current_rotate_path,
+                               PATH_MAX, "%s",
+                               session->rotation_chunk.active_tracing_path);
+               ret = rename_complete_chunk(session,
+                               session->session_last_stop_ts);
+               if (ret < 0) {
+                       ERR("Renaming session on destroy");
+               }
+       }
+
+       if (session->rotate_relay_pending_timer_enabled) {
+               sessiond_timer_rotate_pending_stop(session);
+       }
+
+       if (session->rotate_timer_enabled) {
+               sessiond_rotate_timer_stop(session);
+       }
+
        /* Clean kernel session teardown */
        kernel_destroy_session(ksess);
 
@@ -3082,7 +3134,7 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
                                        sizeof(sessions[i].path), session);
                } else {
                        ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
-                                       session->consumer->dst.trace_path);
+                                       session->consumer->dst.session_root_path);
                }
                if (ret < 0) {
                        PERROR("snprintf session path");
@@ -3131,6 +3183,14 @@ int cmd_data_pending(struct ltt_session *session)
                }
        }
 
+       /*
+        * A rotation is still pending, we have to wait.
+        */
+       if (session->rotate_pending) {
+               ret = 1;
+               goto error;
+       }
+
        if (ksess && ksess->consumer) {
                ret = consumer_is_data_pending(ksess->id, ksess->consumer);
                if (ret == 1) {
@@ -3319,7 +3379,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
                }
                if (output->consumer->type == CONSUMER_DST_LOCAL) {
                        if (lttng_strncpy(list[idx].ctrl_url,
-                                       output->consumer->dst.trace_path,
+                                       output->consumer->dst.session_root_path,
                                        sizeof(list[idx].ctrl_url))) {
                                ret = -LTTNG_ERR_INVALID;
                                goto error;
@@ -4099,6 +4159,267 @@ int cmd_set_session_shm_path(struct ltt_session *session,
        return 0;
 }
 
+static
+const char *get_base_path(struct ltt_session *session,
+               struct consumer_output *consumer)
+{
+       if (session->net_handle > 0) {
+               return consumer->dst.net.base_dir;
+       } else {
+               return consumer->dst.session_root_path;
+       }
+}
+
+/*
+ * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
+ *
+ * Ask the consumer to rotate the session output directory.
+ * The session lock must be held.
+ *
+ * Return 0 on success or else a LTTNG_ERR code.
+ */
+int cmd_rotate_session(struct ltt_session *session,
+               struct lttng_rotate_session_return **rotate_return)
+{
+       int ret;
+       struct tm *timeinfo;
+       char datetime[16];
+       time_t now;
+
+       assert(session);
+
+       if (rotate_return) {
+               *rotate_return = zmalloc(sizeof(struct lttng_rotate_session_return));
+               if (!*rotate_return) {
+                       ret = -ENOMEM;
+                       goto end;
+               }
+       }
+
+       if (session->live_timer || session->snapshot_mode ||
+                       !session->output_traces) {
+               ret = -LTTNG_ERR_ROTATE_NOT_AVAILABLE;
+               goto error;
+       }
+
+       if (session->rotate_pending || session->rotate_pending_relay) {
+               ret = -LTTNG_ERR_ROTATE_PENDING;
+               goto error;
+       }
+
+       /* Special case for the first rotation. */
+       if (session->rotate_count == 0) {
+               const char *base_path = NULL;
+
+               /* Either one of the two sessions is enough to get the root path. */
+               if (session->kernel_session) {
+                       base_path = get_base_path(session, session->kernel_session->consumer);
+               } else if (session->ust_session) {
+                       base_path = get_base_path(session, session->ust_session->consumer);
+               } else {
+                       assert(0);
+               }
+               assert(base_path);
+               snprintf(session->rotation_chunk.current_rotate_path,
+                               PATH_MAX, "%s",
+                               base_path);
+       } else {
+               /*
+                * The currently active tracing path is now the folder we
+                * want to rotate.
+                */
+               snprintf(session->rotation_chunk.current_rotate_path,
+                               PATH_MAX, "%s",
+                               session->rotation_chunk.active_tracing_path);
+       }
+
+       session->rotate_count++;
+       session->rotate_pending = true;
+       session->rotate_pending_relay_check_in_progress = false;
+       session->rotate_status = LTTNG_ROTATE_STARTED;
+
+       /*
+        * Create the path name for the next chunk.
+        */
+       now = time(NULL);
+       if (now == (time_t) -1) {
+               ret = -LTTNG_ERR_ROTATE_NOT_AVAILABLE;
+               goto error;
+       }
+       session->last_chunk_start_ts = session->current_chunk_start_ts;
+       session->current_chunk_start_ts = now;
+
+       timeinfo = localtime(&now);
+       strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
+       if (session->kernel_session) {
+               /*
+                * The active path for the next rotation/destroy.
+                * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
+                */
+               snprintf(session->rotation_chunk.active_tracing_path,
+                               PATH_MAX, "%s/%s-%" PRIu64,
+                               get_base_path(session, session->kernel_session->consumer),
+                               datetime, session->rotate_count + 1);
+               /*
+                * The sub-directory for the consumer
+                * Ex: /20170922-111754-42/kernel
+                */
+               snprintf(session->kernel_session->consumer->chunk_path,
+                               PATH_MAX, "/%s-%" PRIu64 "%s", datetime,
+                               session->rotate_count + 1,
+                               session->kernel_session->consumer->subdir);
+               ret = kernel_rotate_session(session);
+               if (ret != LTTNG_OK) {
+                       goto error;
+               }
+       }
+       if (session->ust_session) {
+               snprintf(session->rotation_chunk.active_tracing_path,
+                               PATH_MAX, "%s/%s-%" PRIu64,
+                               get_base_path(session, session->ust_session->consumer),
+                               datetime, session->rotate_count + 1);
+               snprintf(session->ust_session->consumer->chunk_path,
+                               PATH_MAX, "/%s-%" PRIu64, datetime,
+                               session->rotate_count + 1);
+               ret = ust_app_rotate_session(session);
+               if (ret != LTTNG_OK) {
+                       goto error;
+               }
+       }
+
+       if (rotate_return) {
+               (*rotate_return)->rotate_id = session->rotate_count;
+               (*rotate_return)->status = LTTNG_ROTATE_STARTED;
+       }
+
+       DBG("Cmd rotate session %s, rotate_id %" PRIu64, session->name,
+                       session->rotate_count);
+       ret = LTTNG_OK;
+
+       goto end;
+
+error:
+       if (rotate_return) {
+               (*rotate_return)->status = LTTNG_ROTATE_ERROR;
+       }
+end:
+       return ret;
+}
+
+/*
+ * Command LTTNG_ROTATE_PENDING from the lttng-ctl library.
+ *
+ * Check if the session has finished its rotation.
+ *
+ * Return 0 on success or else a LTTNG_ERR code.
+ */
+int cmd_rotate_pending(struct ltt_session *session,
+               struct lttng_rotate_pending_return **pending_return,
+               uint64_t rotate_id)
+{
+       int ret;
+
+       assert(session);
+
+       DBG("Cmd rotate pending session %s, rotate_id %" PRIu64, session->name,
+                       session->rotate_count);
+
+       *pending_return = zmalloc(sizeof(struct lttng_rotate_pending_return));
+       if (!*pending_return) {
+               ret = -ENOMEM;
+               goto end;
+       }
+
+       if (session->rotate_count != rotate_id) {
+               (*pending_return)->status = LTTNG_ROTATE_EXPIRED;
+               ret = LTTNG_OK;
+               goto end;
+       }
+
+       if (session->rotate_status == LTTNG_ROTATE_ERROR) {
+               DBG("An error occurred during rotation");
+               (*pending_return)->status = LTTNG_ROTATE_ERROR;
+       } else if (session->rotate_status == LTTNG_ROTATE_EMPTY) {
+               DBG("Nothing to rotate");
+               (*pending_return)->status = LTTNG_ROTATE_EMPTY;
+       /* Rotate with a relay */
+       } else if (session->rotate_pending_relay) {
+               DBG("Session %s, rotate_id %" PRIu64 " still pending",
+                               session->name, session->rotate_count);
+               (*pending_return)->status = LTTNG_ROTATE_STARTED;
+       } else if (session->rotate_pending) {
+               DBG("Session %s, rotate_id %" PRIu64 " still pending",
+                               session->name, session->rotate_count);
+               (*pending_return)->status = LTTNG_ROTATE_STARTED;
+       } else {
+               DBG("Session %s, rotate_id %" PRIu64 " finished",
+                               session->name, session->rotate_count);
+               (*pending_return)->status = LTTNG_ROTATE_COMPLETED;
+               snprintf((*pending_return)->output_path, PATH_MAX, "%s",
+                               session->rotation_chunk.current_rotate_path);
+       }
+
+       ret = LTTNG_OK;
+
+       goto end;
+
+end:
+       return ret;
+}
+
+/*
+ * Command LTTNG_ROTATE_SETUP from the lttng-ctl library.
+ *
+ * Configure the automatic rotation parameters.
+ *
+ * Return 0 on success or else a LTTNG_ERR code.
+ */
+int cmd_rotate_setup(struct ltt_session *session,
+               uint64_t timer_us, uint64_t size)
+{
+       int ret;
+
+       assert(session);
+
+       DBG("Cmd rotate setup session %s", session->name);
+
+       if (timer_us && session->rotate_timer_period) {
+               ret = LTTNG_ERR_ROTATE_TIMER_EXISTS;
+               goto end;
+       }
+
+       if (size && session->rotate_size) {
+               ret = LTTNG_ERR_ROTATE_SIZE_EXISTS;
+               goto end;
+       }
+
+       if (timer_us && !session->rotate_timer_period) {
+               session->rotate_timer_period = timer_us;
+               /*
+                * Only start the timer if the session is active, otherwise
+                * it will be started when the session starts.
+                */
+               if (session->active) {
+                       ret = sessiond_rotate_timer_start(session, timer_us);
+                       if (ret) {
+                               ERR("Failed to enable rotate timer");
+                               ret = LTTNG_ERR_UNK;
+                               goto end;
+                       }
+               }
+       } else if (timer_us == -1ULL && session->rotate_timer_period > 0) {
+               sessiond_rotate_timer_stop(session);
+       }
+       session->rotate_size = size;
+
+       ret = LTTNG_OK;
+
+       goto end;
+
+end:
+       return ret;
+}
+
 /*
  * Init command subsystem.
  */
This page took 0.030462 seconds and 5 git commands to generate.