Size-based rotation
[lttng-tools.git] / src / bin / lttng-sessiond / rotate.c
index 237723d3da8c7bc3e4f8963fcf73e829310bdd72..bd6dbddf95492492eb02a5dd87d138cfffe6c108 100644 (file)
@@ -32,6 +32,7 @@
 #include <signal.h>
 #include <inttypes.h>
 
+#include <lttng/notification/channel-internal.h>
 #include <lttng/rotate-internal.h>
 
 #include "session.h"
@@ -41,6 +42,7 @@
 #include "health-sessiond.h"
 #include "cmd.h"
 #include "utils.h"
+#include "notification-thread-commands.h"
 
 #include <urcu.h>
 #include <urcu/list.h>
@@ -209,8 +211,8 @@ error:
 int rename_complete_chunk(struct ltt_session *session, time_t ts)
 {
        struct tm *timeinfo;
-       char datetime[16], start_datetime[16];
        char new_path[LTTNG_PATH_MAX];
+       char datetime[21], start_datetime[21];
        int ret;
        size_t strf_ret;
 
@@ -221,7 +223,8 @@ int rename_complete_chunk(struct ltt_session *session, time_t ts)
                ret = -1;
                goto end;
        }
-       strf_ret = strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S",
+
+       strf_ret = strftime(datetime, sizeof(datetime), "%Y%m%dT%H%M%S%z",
                        timeinfo);
        if (strf_ret == 0) {
                ERR("Failed to format timestamp while renaming completed session chunk");
@@ -230,7 +233,7 @@ int rename_complete_chunk(struct ltt_session *session, time_t ts)
        }
 
        if (session->rotate_count == 1) {
-               char start_time[16];
+               char start_time[21];
 
                timeinfo = localtime(&session->last_chunk_start_ts);
                if (!timeinfo) {
@@ -240,7 +243,7 @@ int rename_complete_chunk(struct ltt_session *session, time_t ts)
                }
 
                strf_ret = strftime(start_time, sizeof(start_time),
-                               "%Y%m%d-%H%M%S", timeinfo);
+                               "%Y%m%dT%H%M%S%z", timeinfo);
                if (strf_ret == 0) {
                        ERR("Failed to format timestamp while renaming completed session chunk");
                        ret = -1;
@@ -302,7 +305,8 @@ int rename_complete_chunk(struct ltt_session *session, time_t ts)
                        ret = -1;
                        goto end;
                }
-               strf_ret = strftime(start_datetime, sizeof(start_datetime), "%Y%m%d-%H%M%S", timeinfo);
+               strf_ret = strftime(start_datetime, sizeof(start_datetime),
+                               "%Y%m%dT%H%M%S%z", timeinfo);
                if (!strf_ret) {
                        ERR("Failed to format timestamp while renaming completed session chunk");
                        ret = -1;
@@ -399,3 +403,101 @@ int relay_rotate_pending(struct ltt_session *session, uint64_t chunk_id)
 end:
        return ret;
 }
+
+int subscribe_session_consumed_size_rotation(struct ltt_session *session, uint64_t size,
+               struct notification_thread_handle *notification_thread_handle)
+{
+       int ret;
+       enum lttng_condition_status condition_status;
+       enum lttng_notification_channel_status nc_status;
+       struct lttng_action *action;
+
+       session->rotate_condition = lttng_condition_session_consumed_size_create();
+       if (!session->rotate_condition) {
+               ERR("Failed to create session consumed size condition object");
+               ret = -1;
+               goto end;
+       }
+
+       condition_status = lttng_condition_session_consumed_size_set_threshold(
+                       session->rotate_condition, size);
+       if (condition_status != LTTNG_CONDITION_STATUS_OK) {
+               ERR("Could not set session consumed size condition threshold (size = %" PRIu64 ")",
+                               size);
+               ret = -1;
+               goto end;
+       }
+
+       condition_status =
+                       lttng_condition_session_consumed_size_set_session_name(
+                               session->rotate_condition, session->name);
+       if (condition_status != LTTNG_CONDITION_STATUS_OK) {
+               ERR("Could not set session consumed size condition session name (name = %s)",
+                               session->name);
+               ret = -1;
+               goto end;
+       }
+
+       action = lttng_action_notify_create();
+       if (!action) {
+               ERR("Could not create notify action");
+               ret = -1;
+               goto end;
+       }
+
+       session->rotate_trigger = lttng_trigger_create(session->rotate_condition,
+                       action);
+       if (!session->rotate_trigger) {
+               ERR("Could not create size-based rotation trigger");
+               ret = -1;
+               goto end;
+       }
+
+       nc_status = lttng_notification_channel_subscribe(
+                       rotate_notification_channel, session->rotate_condition);
+       if (nc_status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
+               ERR("Could not subscribe to session consumed size notification");
+               ret = -1;
+               goto end;
+       }
+
+       ret = notification_thread_command_register_trigger(
+                       notification_thread_handle, session->rotate_trigger);
+       if (ret < 0 && ret != -LTTNG_ERR_TRIGGER_EXISTS) {
+               ERR("Register trigger, %s", lttng_strerror(ret));
+               ret = -1;
+               goto end;
+       }
+
+       ret = 0;
+
+end:
+       return ret;
+}
+
+int unsubscribe_session_consumed_size_rotation(struct ltt_session *session,
+               struct notification_thread_handle *notification_thread_handle)
+{
+       int ret = 0;
+       enum lttng_notification_channel_status status;
+
+       status = lttng_notification_channel_unsubscribe(
+                       rotate_notification_channel,
+                       session->rotate_condition);
+       if (status != LTTNG_NOTIFICATION_CHANNEL_STATUS_OK) {
+               ERR("Session unsubscribe error: %d", (int) status);
+               ret = -1;
+               goto end;
+       }
+
+       ret = notification_thread_command_unregister_trigger(
+                       notification_thread_handle, session->rotate_trigger);
+       if (ret != LTTNG_OK) {
+               ERR("Session unregister trigger error: %d", ret);
+               goto end;
+       }
+
+       ret = 0;
+end:
+       return ret;
+}
This page took 0.025486 seconds and 4 git commands to generate.