remove debug
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
index 1ec3891b8d0244c3f66a1fb997146dd8f34bfa88..0eba587c133adb20841343840d6902aa075a21bc 100644 (file)
@@ -53,6 +53,7 @@
 #include "notification-thread-commands.h"
 #include "rotate.h"
 #include "rotation-thread.h"
+#include "sessiond-timer.h"
 
 #include "cmd.h"
 
@@ -2402,8 +2403,8 @@ int cmd_start_trace(struct ltt_session *session)
         * an eventual session rotation call.
         */
        if (!session->has_been_started) {
-               session->session_start_ts = time(NULL);
-               if (session->session_start_ts == (time_t) -1) {
+               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;
@@ -2437,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:
@@ -2766,6 +2777,14 @@ int cmd_destroy_session(struct ltt_session *session, int wpipe)
                }
        }
 
+       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);
 
@@ -3164,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) {
@@ -4133,54 +4160,21 @@ int cmd_set_session_shm_path(struct ltt_session *session,
 }
 
 static
-int rename_first_chunk(struct ltt_session *session,
-               struct consumer_output *consumer, char *datetime)
+const char *get_base_path(struct ltt_session *session,
+               struct consumer_output *consumer)
 {
-       int ret;
-       char *tmppath = NULL, *tmppath2 = NULL;
-
-       tmppath = zmalloc(PATH_MAX * sizeof(char));
-       if (!tmppath) {
-               ret = -LTTNG_ERR_NOMEM;
-               goto error;
-       }
-       tmppath2 = zmalloc(PATH_MAX * sizeof(char));
-       if (!tmppath2) {
-               ret = -LTTNG_ERR_NOMEM;
-               goto error;
-       }
-
-       /* Current domain path: <session>/kernel */
-       snprintf(tmppath, PATH_MAX, "%s/%s",
-                       consumer->dst.session_root_path, consumer->subdir);
-       /* New domain path: <session>/<start-date>-/kernel */
-       snprintf(tmppath2, PATH_MAX, "%s/%s-/%s",
-                       consumer->dst.session_root_path, datetime,
-                       consumer->subdir);
-       /*
-        * Move the per-domain folder inside the first rotation
-        * folder.
-        */
-       ret = session_rename_chunk(session, tmppath, tmppath2, 1);
-       if (ret < 0) {
-               PERROR("Rename first trace directory");
-               ret = -LTTNG_ERR_ROTATE_NO_DATA;
-               goto error;
+       if (session->net_handle > 0) {
+               return consumer->dst.net.base_dir;
+       } else {
+               return consumer->dst.session_root_path;
        }
-
-       ret = 0;
-
-error:
-       free(tmppath);
-       free(tmppath2);
-
-       return ret;
 }
 
 /*
  * 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.
  */
@@ -4194,10 +4188,12 @@ int cmd_rotate_session(struct ltt_session *session,
 
        assert(session);
 
-       *rotate_return = zmalloc(sizeof(struct lttng_rotate_session_return));
-       if (!*rotate_return) {
-               ret = -ENOMEM;
-               goto end;
+       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 ||
@@ -4206,59 +4202,27 @@ int cmd_rotate_session(struct ltt_session *session,
                goto error;
        }
 
-       if (session->rotate_pending) {
+       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) {
-               timeinfo = localtime(&session->session_start_ts);
-               strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
+               const char *base_path = NULL;
+
                /* Either one of the two sessions is enough to get the root path. */
                if (session->kernel_session) {
-                       snprintf(session->rotation_chunk.current_rotate_path,
-                                       PATH_MAX, "%s/%s-",
-                                       session->kernel_session->consumer->dst.session_root_path,
-                                       datetime);
+                       base_path = get_base_path(session, session->kernel_session->consumer);
                } else if (session->ust_session) {
-                       snprintf(session->rotation_chunk.current_rotate_path,
-                                       PATH_MAX, "%s/%s-",
-                                       session->ust_session->consumer->dst.session_root_path,
-                                       datetime);
+                       base_path = get_base_path(session, session->ust_session->consumer);
                } else {
                        assert(0);
                }
-
-               /*
-                * Create the first rotation folder to move the existing
-                * kernel/ust folders into.
-                */
-               ret = run_as_mkdir_recursive(session->rotation_chunk.current_rotate_path,
-                               S_IRWXU | S_IRWXG, session->uid, session->gid);
-               if (ret < 0) {
-                       if (errno != EEXIST) {
-                               ERR("Trace directory creation error");
-                               ret = -LTTNG_ERR_ROTATE_NOT_AVAILABLE;
-                               goto error;
-                       }
-               }
-               if (session->kernel_session) {
-                       ret = rename_first_chunk(session,
-                                       session->kernel_session->consumer,
-                                       datetime);
-                       if (ret < 0) {
-                               goto error;
-                       }
-               }
-               if (session->ust_session) {
-                       ret = rename_first_chunk(session,
-                                       session->ust_session->consumer,
-                                       datetime);
-                       if (ret < 0) {
-                               goto error;
-                       }
-               }
+               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
@@ -4270,7 +4234,9 @@ int cmd_rotate_session(struct ltt_session *session,
        }
 
        session->rotate_count++;
-       session->rotate_pending = 1;
+       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.
@@ -4280,18 +4246,27 @@ int cmd_rotate_session(struct ltt_session *session,
                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. */
+               /*
+                * 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-",
-                               session->kernel_session->consumer->dst.session_root_path,
-                               datetime);
-               /* The sub-directory for the consumer. */
+                               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-/%s/", datetime,
+                               PATH_MAX, "/%s-%" PRIu64 "%s", datetime,
+                               session->rotate_count + 1,
                                session->kernel_session->consumer->subdir);
                ret = kernel_rotate_session(session);
                if (ret != LTTNG_OK) {
@@ -4300,19 +4275,22 @@ int cmd_rotate_session(struct ltt_session *session,
        }
        if (session->ust_session) {
                snprintf(session->rotation_chunk.active_tracing_path,
-                               PATH_MAX, "%s/%s-",
-                               session->ust_session->consumer->dst.session_root_path,
-                               datetime);
+                               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-/", datetime);
+                               PATH_MAX, "/%s-%" PRIu64, datetime,
+                               session->rotate_count + 1);
                ret = ust_app_rotate_session(session);
                if (ret != LTTNG_OK) {
                        goto error;
                }
        }
 
-       (*rotate_return)->rotate_id = session->rotate_count;
-       (*rotate_return)->status = LTTNG_ROTATE_STARTED;
+       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);
@@ -4321,7 +4299,9 @@ int cmd_rotate_session(struct ltt_session *session,
        goto end;
 
 error:
-       (*rotate_return)->status = LTTNG_ROTATE_ERROR;
+       if (rotate_return) {
+               (*rotate_return)->status = LTTNG_ROTATE_ERROR;
+       }
 end:
        return ret;
 }
@@ -4356,7 +4336,18 @@ int cmd_rotate_pending(struct ltt_session *session,
                goto end;
        }
 
-       if (session->rotate_pending) {
+       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;
@@ -4376,6 +4367,59 @@ 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.031051 seconds and 5 git commands to generate.