Fix: kernel/ust snapshot backward compat for 2.10 relay
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
index e3118f1910048fefd890742eb3fa44c0491ef062..53072f97187654dc37db1b6042f8100b375cbb70 100644 (file)
@@ -24,6 +24,7 @@
 #include <inttypes.h>
 
 #include <common/common.h>
+#include <common/trace-chunk.h>
 #include <common/kernel-ctl/kernel-ctl.h>
 #include <common/kernel-ctl/kernel-ioctl.h>
 #include <common/sessiond-comm/sessiond-comm.h>
@@ -130,6 +131,7 @@ int kernel_create_session(struct ltt_session *session, int tracer_fd)
 error:
        if (lks) {
                trace_kernel_destroy_session(lks);
+               trace_kernel_free_session(lks);
        }
        return ret;
 }
@@ -1155,16 +1157,19 @@ end_boot_id:
 }
 
 /*
- * Complete teardown of a kernel session.
+ * Teardown of a kernel session, keeping data required by destroy notifiers.
  */
 void kernel_destroy_session(struct ltt_kernel_session *ksess)
 {
+       struct lttng_trace_chunk *trace_chunk;
+
        if (ksess == NULL) {
                DBG3("No kernel session when tearing down session");
                return;
        }
 
        DBG("Tearing down kernel session");
+       trace_chunk = ksess->current_trace_chunk;
 
        /*
         * Destroy channels on the consumer if at least one FD has been sent and we
@@ -1198,6 +1203,16 @@ void kernel_destroy_session(struct ltt_kernel_session *ksess)
        consumer_output_send_destroy_relayd(ksess->consumer);
 
        trace_kernel_destroy_session(ksess);
+       lttng_trace_chunk_put(trace_chunk);
+}
+
+/* Teardown of data required by destroy notifiers. */
+void kernel_free_session(struct ltt_kernel_session *ksess)
+{
+       if (ksess == NULL) {
+               return;
+       }
+       trace_kernel_free_session(ksess);
 }
 
 /*
@@ -1235,8 +1250,9 @@ void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
  *
  * Return LTTNG_OK on success or else return a LTTNG_ERR code.
  */
-enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
-               struct snapshot_output *output, int wait,
+enum lttng_error_code kernel_snapshot_record(
+               struct ltt_kernel_session *ksess,
+               const struct consumer_output *output, int wait,
                uint64_t nb_packets_per_stream)
 {
        int err, ret, saved_metadata_fd;
@@ -1244,8 +1260,7 @@ enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
        struct consumer_socket *socket;
        struct lttng_ht_iter iter;
        struct ltt_kernel_metadata *saved_metadata;
-       struct ltt_session *session = NULL;
-       uint64_t trace_archive_id;
+       char *trace_path = NULL;
 
        assert(ksess);
        assert(ksess->consumer);
@@ -1253,12 +1268,6 @@ enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
 
        DBG("Kernel snapshot record started");
 
-       session = session_find_by_id(ksess->id);
-       assert(session);
-       assert(pthread_mutex_trylock(&session->lock));
-       assert(session_trylock_list());
-       trace_archive_id = session->current_archive_id;
-
        /* Save current metadata since the following calls will change it. */
        saved_metadata = ksess->metadata;
        saved_metadata_fd = ksess->metadata_stream_fd;
@@ -1277,25 +1286,21 @@ enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
                goto error_open_stream;
        }
 
+       trace_path = setup_channel_trace_path(ksess->consumer,
+                       DEFAULT_KERNEL_TRACE_DIR);
+       if (!trace_path) {
+               status = LTTNG_ERR_INVALID;
+               goto error;
+       }
        /* Send metadata to consumer and snapshot everything. */
-       cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
+       cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
                        socket, node.node) {
-               struct consumer_output *saved_output;
                struct ltt_kernel_channel *chan;
 
-               /*
-                * Temporarly switch consumer output for our snapshot output. As long
-                * as the session lock is taken, this is safe.
-                */
-               saved_output = ksess->consumer;
-               ksess->consumer = output->consumer;
-
                pthread_mutex_lock(socket->lock);
                /* This stream must not be monitored by the consumer. */
                ret = kernel_consumer_add_metadata(socket, ksess, 0);
                pthread_mutex_unlock(socket->lock);
-               /* Put back the saved consumer output into the session. */
-               ksess->consumer = saved_output;
                if (ret < 0) {
                        status = LTTNG_ERR_KERN_META_FAIL;
                        goto error_consumer;
@@ -1305,9 +1310,8 @@ enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
                cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
                        status = consumer_snapshot_channel(socket, chan->key, output, 0,
                                        ksess->uid, ksess->gid,
-                                       DEFAULT_KERNEL_TRACE_DIR, wait,
-                                       nb_packets_per_stream,
-                                       trace_archive_id);
+                                       trace_path, wait,
+                                       nb_packets_per_stream);
                        if (status != LTTNG_OK) {
                                (void) kernel_consumer_destroy_metadata(socket,
                                                ksess->metadata);
@@ -1317,9 +1321,7 @@ enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
 
                /* Snapshot metadata, */
                status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
-                               1, ksess->uid, ksess->gid,
-                               DEFAULT_KERNEL_TRACE_DIR, wait, 0,
-                               trace_archive_id);
+                               1, ksess->uid, ksess->gid, trace_path, wait, 0);
                if (status != LTTNG_OK) {
                        goto error_consumer;
                }
@@ -1344,10 +1346,8 @@ error:
        /* Restore metadata state.*/
        ksess->metadata = saved_metadata;
        ksess->metadata_stream_fd = saved_metadata_fd;
-       if (session) {
-               session_put(session);
-       }
        rcu_read_unlock();
+       free(trace_path);
        return status;
 }
 
@@ -1426,15 +1426,13 @@ enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
                        socket, node.node) {
                struct ltt_kernel_channel *chan;
 
-               /* For each channel, ask the consumer to rotate it. */
+                /* For each channel, ask the consumer to rotate it. */
                cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
                        DBG("Rotate kernel channel %" PRIu64 ", session %s",
                                        chan->key, session->name);
                        ret = consumer_rotate_channel(socket, chan->key,
                                        ksess->uid, ksess->gid, ksess->consumer,
-                                       ksess->consumer->subdir,
-                                       /* is_metadata_channel */ false,
-                                       session->current_archive_id);
+                                       /* is_metadata_channel */ false);
                        if (ret < 0) {
                                status = LTTNG_ERR_KERN_CONSUMER_FAIL;
                                goto error;
@@ -1446,9 +1444,7 @@ enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
                 */
                ret = consumer_rotate_channel(socket, ksess->metadata->key,
                                ksess->uid, ksess->gid, ksess->consumer,
-                               ksess->consumer->subdir,
-                               /* is_metadata_channel */ true,
-                               session->current_archive_id);
+                               /* is_metadata_channel */ true);
                if (ret < 0) {
                        status = LTTNG_ERR_KERN_CONSUMER_FAIL;
                        goto error;
@@ -1459,3 +1455,28 @@ error:
        rcu_read_unlock();
        return status;
 }
+
+enum lttng_error_code kernel_create_channel_subdirectories(
+               const struct ltt_kernel_session *ksess)
+{
+       enum lttng_error_code ret = LTTNG_OK;
+       enum lttng_trace_chunk_status chunk_status;
+
+       rcu_read_lock();
+       assert(ksess->current_trace_chunk);
+
+       /*
+        * Create the index subdirectory which will take care
+        * of implicitly creating the channel's path.
+        */
+       chunk_status = lttng_trace_chunk_create_subdirectory(
+                       ksess->current_trace_chunk,
+                       DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
+       if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+               ret = LTTNG_ERR_CREATE_DIR_FAIL;
+               goto error;
+       }
+error:
+       rcu_read_unlock();
+       return ret;
+}
This page took 0.025822 seconds and 4 git commands to generate.