X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Frotate.c;h=f4127c968b558bdbd8c41bb2367fb7f6a3f770c6;hb=f48ccdb2a224a5c2c9d2dae3f8799865cf937fc5;hp=49ccea94e381fd6ee520162b8c362f310efe7e9e;hpb=5c408ad8ef08a226c018702aca969536f36ac4e5;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/rotate.c b/src/bin/lttng-sessiond/rotate.c index 49ccea94e..f4127c968 100644 --- a/src/bin/lttng-sessiond/rotate.c +++ b/src/bin/lttng-sessiond/rotate.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2017 - Julien Desfossez + * Copyright (C) 2018 - Jérémie Galarneau * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License, version 2 only, as @@ -32,6 +33,9 @@ #include #include +#include +#include + #include "session.h" #include "rotate.h" #include "rotation-thread.h" @@ -39,49 +43,12 @@ #include "health-sessiond.h" #include "cmd.h" #include "utils.h" +#include "notification-thread-commands.h" #include #include #include -unsigned long hash_channel_key(struct rotation_channel_key *key) -{ - return hash_key_u64(&key->key, lttng_ht_seed) ^ hash_key_ulong( - (void *) (unsigned long) key->domain, lttng_ht_seed); -} - -int rotate_add_channel_pending(uint64_t key, enum lttng_domain_type domain, - struct ltt_session *session) -{ - int ret; - struct rotation_channel_info *new_info; - struct rotation_channel_key channel_key = { .key = key, - .domain = domain }; - - new_info = zmalloc(sizeof(struct rotation_channel_info)); - if (!new_info) { - goto error; - } - - new_info->channel_key.key = key; - new_info->channel_key.domain = domain; - new_info->session_id = session->id; - cds_lfht_node_init(&new_info->rotate_channels_ht_node); - - session->nr_chan_rotate_pending++; - cds_lfht_add(channel_pending_rotate_ht, - hash_channel_key(&channel_key), - &new_info->rotate_channels_ht_node); - - ret = 0; - goto end; - -error: - ret = -1; -end: - return ret; -} - /* The session's lock must be held by the caller. */ static int session_rename_chunk(struct ltt_session *session, char *current_path, @@ -153,39 +120,50 @@ int rename_first_chunk(struct ltt_session *session, int ret; char current_full_path[LTTNG_PATH_MAX], new_full_path[LTTNG_PATH_MAX]; - /* Current domain path: /kernel */ if (session->net_handle > 0) { - ret = snprintf(current_full_path, sizeof(current_full_path), "%s/%s", - consumer->dst.net.base_dir, consumer->subdir); + /* + * Current domain path: + * HOSTNAME/{SESSION-[TIMESTAMP], USER_DIRECTORY}/DOMAIN + */ + ret = snprintf(current_full_path, sizeof(current_full_path), + "%s%s", + consumer->dst.net.base_dir, + consumer->domain_subdir); if (ret < 0 || ret >= sizeof(current_full_path)) { ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"", session->name); - ret = -1; + ret = -LTTNG_ERR_UNK; goto error; } } else { - ret = snprintf(current_full_path, sizeof(current_full_path), "%s/%s", - consumer->dst.session_root_path, consumer->subdir); + /* + * Current domain path: + * SESSION_OUTPUT_PATH/DOMAIN + */ + ret = snprintf(current_full_path, sizeof(current_full_path), + "%s/%s", + consumer->dst.session_root_path, + consumer->domain_subdir); if (ret < 0 || ret >= sizeof(current_full_path)) { ERR("Failed to initialize current full path while renaming first rotation chunk of session \"%s\"", session->name); - ret = -1; + ret = -LTTNG_ERR_UNK; goto error; } } - /* New domain path: /--/kernel */ + /* + * New domain path: + * SESSION_BASE_PATH/_-INDEX/DOMAIN + */ ret = snprintf(new_full_path, sizeof(new_full_path), "%s/%s", - new_path, consumer->subdir); + new_path, consumer->domain_subdir); if (ret < 0 || ret >= sizeof(new_full_path)) { ERR("Failed to initialize new full path while renaming first rotation chunk of session \"%s\"", session->name); - ret = -1; + ret = -LTTNG_ERR_UNK; goto error; } - /* - * Move the per-domain fcurrenter inside the first rotation - * fcurrenter. - */ + /* Move the per-domain inside the first rotation chunk path. */ ret = session_rename_chunk(session, current_full_path, new_full_path); if (ret < 0) { ret = -LTTNG_ERR_UNK; @@ -204,69 +182,70 @@ error: * * Returns 0 on success, a negative value on error. */ -int rename_complete_chunk(struct ltt_session *session, time_t ts) +int rename_completed_chunk(struct ltt_session *session, time_t end_ts) { - struct tm *timeinfo; - char datetime[16], start_datetime[16]; - char new_path[LTTNG_PATH_MAX]; int ret; size_t strf_ret; + struct tm *timeinfo; + char new_path[LTTNG_PATH_MAX]; + char start_datetime[21], end_datetime[21]; DBG("Renaming completed chunk for session %s", session->name); - timeinfo = localtime(&ts); + + /* Format chunk start time. */ + timeinfo = localtime(&session->last_chunk_start_ts); if (!timeinfo) { - ERR("Failed to retrieve local time while renaming completed chunk"); + ERR("Failed to separate local time while renaming completed chunk"); ret = -1; goto end; } - strf_ret = strftime(datetime, sizeof(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 == 0) { ERR("Failed to format timestamp while renaming completed session chunk"); ret = -1; goto end; } - if (session->rotate_count == 1) { - char start_time[16]; - - timeinfo = localtime(&session->last_chunk_start_ts); - if (!timeinfo) { - ERR("Failed to retrieve local time while renaming completed chunk"); - ret = -1; - goto end; - } + /* Format chunk end time. */ + timeinfo = localtime(&end_ts); + if (!timeinfo) { + ERR("Failed to parse time while renaming completed chunk"); + ret = -1; + goto end; + } + strf_ret = strftime(end_datetime, sizeof(end_datetime), + "%Y%m%dT%H%M%S%z", timeinfo); + if (strf_ret == 0) { + ERR("Failed to format timestamp while renaming completed session chunk"); + ret = -1; + goto end; + } - strf_ret = strftime(start_time, sizeof(start_time), - "%Y%m%d-%H%M%S", timeinfo); - if (strf_ret == 0) { - ERR("Failed to format timestamp while renaming completed session chunk"); - ret = -1; - goto end; - } + /* Format completed chunk's path. */ + ret = snprintf(new_path, sizeof(new_path), "%s/archives/%s-%s-%" PRIu64, + session_get_base_path(session), + start_datetime, end_datetime, + session->current_archive_id); + if (ret < 0 || ret >= sizeof(new_path)) { + ERR("Failed to format new chunk path while renaming chunk of session \"%s\"", + session->name); + ret = -1; + goto error; + } + if (session->current_archive_id == 1) { /* * On the first rotation, the current_rotate_path is the * session_root_path, so we need to create the chunk folder * and move the domain-specific folders inside it. */ - ret = snprintf(new_path, sizeof(new_path), "%s/%s-%s-%" PRIu64, - session->rotation_chunk.current_rotate_path, - start_time, - datetime, session->rotate_count); - if (ret < 0 || ret >= sizeof(new_path)) { - ERR("Failed to format new chunk path while renaming session \"%s\"'s first chunk", - session->name); - ret = -1; - goto end; - } - if (session->kernel_session) { ret = rename_first_chunk(session, session->kernel_session->consumer, new_path); if (ret) { - ERR("Failed to rename kernel session trace folder to %s", new_path); + ERR("Failed to rename kernel session trace folder to \"%s\"", new_path); /* * This is not a fatal error for the rotation * thread, we just need to inform the client @@ -283,7 +262,7 @@ int rename_complete_chunk(struct ltt_session *session, time_t ts) session->ust_session->consumer, new_path); if (ret) { - ERR("Failed to rename userspace session trace folder to %s", new_path); + ERR("Failed to rename userspace session trace folder to \"%s\"", new_path); ret = 0; goto error; } @@ -293,34 +272,11 @@ int rename_complete_chunk(struct ltt_session *session, time_t ts) * After the first rotation, all the trace data is already in * its own chunk folder, we just need to append the suffix. */ - /* Recreate the session->rotation_chunk.current_rotate_path */ - timeinfo = localtime(&session->last_chunk_start_ts); - if (!timeinfo) { - ERR("Failed to retrieve local time while renaming completed chunk"); - ret = -1; - goto end; - } - strf_ret = strftime(start_datetime, sizeof(start_datetime), "%Y%m%d-%H%M%S", timeinfo); - if (!strf_ret) { - ERR("Failed to format timestamp while renaming completed session chunk"); - ret = -1; - goto end; - } - ret = snprintf(new_path, sizeof(new_path), "%s/%s-%s-%" PRIu64, - session_get_base_path(session), - start_datetime, - datetime, session->rotate_count); - if (ret < 0 || ret >= sizeof(new_path)) { - ERR("Failed to format new chunk path while renaming chunk of session \"%s\"", - session->name); - ret = -1; - goto error; - } ret = session_rename_chunk(session, session->rotation_chunk.current_rotate_path, new_path); if (ret) { - ERR("Failed to rename session trace folder from %s to %s", + ERR("Failed to rename session trace folder from \"%s\" to \"%s\"", session->rotation_chunk.current_rotate_path, new_path); ret = 0; @@ -346,7 +302,146 @@ int rename_complete_chunk(struct ltt_session *session, time_t ts) goto end; error: - session->rotation_status = LTTNG_ROTATION_STATUS_ERROR; + session->rotation_state = LTTNG_ROTATION_STATE_ERROR; +end: + return ret; +} + +int rename_active_chunk(struct ltt_session *session) +{ + int ret; + + session->current_archive_id++; + + /* + * The currently active tracing path is now the folder we + * want to rename. + */ + ret = lttng_strncpy(session->rotation_chunk.current_rotate_path, + session->rotation_chunk.active_tracing_path, + sizeof(session->rotation_chunk.current_rotate_path)); + if (ret) { + ERR("Failed to copy active tracing path"); + goto end; + } + + ret = rename_completed_chunk(session, time(NULL)); + if (ret < 0) { + ERR("Failed to rename current rotation's path"); + goto end; + } + + /* + * We just renamed, the folder, we didn't do an actual rotation, so + * the active tracing path is now the renamed folder and we have to + * restore the rotate count. + */ + ret = lttng_strncpy(session->rotation_chunk.active_tracing_path, + session->rotation_chunk.current_rotate_path, + sizeof(session->rotation_chunk.active_tracing_path)); + if (ret) { + ERR("Failed to rename active session chunk tracing path"); + goto end; + } +end: + session->current_archive_id--; + 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; }