Move to kernel style SPDX license identifiers
[babeltrace.git] / src / plugins / ctf / lttng-live / metadata.c
index 787fc0b322da0c39d68494b636c852a6e6ec8876..859a36de954e31f491c4e4762076a553a5fa184b 100644 (file)
@@ -1,27 +1,9 @@
 /*
- * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com>
- * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
- * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
+ * SPDX-License-Identifier: MIT
  *
- * Some functions are based on older functions written by Mathieu Desnoyers.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
+ * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
  */
 
 #define BT_COMP_LOG_SELF_COMP self_comp
@@ -132,6 +114,8 @@ enum lttng_live_iterator_status lttng_live_metadata_update(
        bt_self_component *self_comp = trace->self_comp;
        enum lttng_live_get_one_metadata_status metadata_status;
 
+       BT_COMP_LOGD("Updating metadata for trace: trace-id=%"PRIu64, trace->id);
+
        /* No metadata stream yet. */
        if (!metadata) {
                if (session->new_streams_needed) {
@@ -143,19 +127,19 @@ enum lttng_live_iterator_status lttng_live_metadata_update(
                goto end;
        }
 
-       if (!metadata->trace) {
-               trace->new_metadata_needed = false;
-       }
-
-       if (!trace->new_metadata_needed) {
+       if (trace->metadata_stream_state != LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) {
                goto end;
        }
 
-       /* Open for writing */
+       /*
+        * Open a new write only file handle to populate the `metadata_buf`
+        * memory buffer so we can write in loop in it easily.
+        */
        fp = bt_open_memstream(&metadata_buf, &size);
        if (!fp) {
                if (errno == EINTR &&
                                lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
+                       session->lttng_live_msg_iter->was_interrupted = true;
                        status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
                } else {
                        BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
@@ -192,27 +176,31 @@ enum lttng_live_iterator_status lttng_live_metadata_update(
                        keep_receiving = false;
                        break;
                case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED:
+                       BT_COMP_LOGD("Metadata stream was closed by the Relay, the trace is no longer active: "
+                               "trace-id=%"PRIu64", metadata-stream-id=%"PRIu64,
+                               trace->id, metadata->stream_id);
+                       /*
+                        * The stream was closed and we received everything
+                        * there was to receive for this metadata stream.
+                        * We go on with the decoding of what we received. So
+                        * that data stream can be decoded.
+                        */
                        keep_receiving = false;
+                       trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED;
                        break;
                case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR:
+                       BT_COMP_LOGE_APPEND_CAUSE(self_comp,
+                               "Error getting one trace metadata packet: "
+                               "trace-id=%"PRIu64, trace->id);
                        goto error;
                default:
                        bt_common_abort();
                }
        }
 
-       /*
-        * A closed metadata stream means the trace is no longer active. Return
-        * _END so that the caller can remove the trace from its list.
-        */
-       if (metadata_status == LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED) {
-               status = LTTNG_LIVE_ITERATOR_STATUS_END;
-               goto end;
-       }
-
+       /* The memory buffer `metadata_buf` contains all the metadata. */
        if (bt_close_memstream(&metadata_buf, &size, fp)) {
-               BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
-                       "Metadata bt_close_memstream", ".");
+               BT_COMP_LOGW_ERRNO("Metadata bt_close_memstream", ".");
        }
 
        fp = NULL;
@@ -222,14 +210,21 @@ enum lttng_live_iterator_status lttng_live_metadata_update(
                        status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
                        goto end;
                }
-               trace->new_metadata_needed = false;
+
+               /* The relay sent zero bytes of metdata. */
+               trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
                goto end;
        }
 
+       /*
+        * Open a new reading file handle on the `metadata_buf` and pass it to
+        * the metadata decoder.
+        */
        fp = bt_fmemopen(metadata_buf, len_read, "rb");
        if (!fp) {
                if (errno == EINTR &&
                                lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) {
+                       session->lttng_live_msg_iter->was_interrupted = true;
                        status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
                } else {
                        BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
@@ -243,6 +238,7 @@ enum lttng_live_iterator_status lttng_live_metadata_update(
         * The call to ctf_metadata_decoder_append_content() will append
         * new metadata to our current trace class.
         */
+       BT_COMP_LOGD("Appending new metadata to the ctf_trace class");
        decoder_status = ctf_metadata_decoder_append_content(
                metadata->decoder, fp);
        switch (decoder_status) {
@@ -276,7 +272,9 @@ enum lttng_live_iterator_status lttng_live_metadata_update(
                        trace->clock_class =
                                borrow_any_clock_class(trace->trace_class);
                }
-               trace->new_metadata_needed = false;
+
+               /* The metadata was updated succesfully. */
+               trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED;
 
                break;
        case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
@@ -296,8 +294,7 @@ end:
 
                closeret = fclose(fp);
                if (closeret) {
-                       BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
-                               "Error on fclose", ".");
+                       BT_COMP_LOGW_ERRNO("Error on fclose", ".");
                }
        }
        free(metadata_buf);
@@ -335,13 +332,13 @@ int lttng_live_metadata_create_stream(struct lttng_live_session *session,
                        "Failed to create CTF metadata decoder");
                goto error;
        }
-       trace = lttng_live_borrow_trace(session, ctf_trace_id);
+       trace = lttng_live_session_borrow_or_create_trace_by_id(session,
+               ctf_trace_id);
        if (!trace) {
                BT_COMP_LOGE_APPEND_CAUSE(self_comp,
                        "Failed to borrow trace");
                goto error;
        }
-       metadata->trace = trace;
        trace->metadata = metadata;
        return 0;
 
This page took 0.026295 seconds and 4 git commands to generate.