Fix: kernel consumer: get next subbuffer EAGAIN handling
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
index 4ace64920109dd38e58e75dad6452a78518b47b8..5464822aaa58678377869a30d7c8bd9dbae1c6ba 100644 (file)
@@ -403,16 +403,12 @@ static int lttng_kconsumer_snapshot_metadata(
 
                ret_read = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
                if (ret_read < 0) {
-                       if (ret_read != -EAGAIN) {
-                               ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
-                                               ret_read);
-                               ret = ret_read;
-                               goto error_snapshot;
-                       }
-                       /* ret_read is negative at this point so we will exit the loop. */
-                       continue;
+                       ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
+                                       ret_read);
+                       ret = ret_read;
+                       goto error_snapshot;
                }
-       } while (ret_read >= 0);
+       } while (ret_read > 0);
 
        if (use_relayd) {
                close_relayd_stream(metadata_stream);
@@ -494,7 +490,7 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
        case LTTNG_CONSUMER_ADD_CHANNEL:
        {
                struct lttng_consumer_channel *new_channel;
-               int ret_send_status, ret_add_channel;
+               int ret_send_status, ret_add_channel = 0;
                const uint64_t chunk_id = msg.u.channel.chunk_id.value;
 
                health_code_update();
@@ -1594,6 +1590,17 @@ int get_subbuffer_common(struct lttng_consumer_stream *stream,
 
        ret = kernctl_get_next_subbuf(stream->wait_fd);
        if (ret) {
+               /*
+                * The caller only expects -ENODATA when there is no data to
+                * read, but the kernel tracer returns -EAGAIN when there is
+                * currently no data for a non-finalized stream, and -ENODATA
+                * when there is no data for a finalized stream. Those can be
+                * combined into a -ENODATA return value.
+                */
+               if (ret == -EAGAIN) {
+                       ret = -ENODATA;
+               }
+
                goto end;
        }
 
@@ -1675,6 +1682,16 @@ int get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream,
                        subbuffer->info.metadata.padded_subbuf_size,
                        coherent ? "true" : "false");
 end:
+       /*
+        * The caller only expects -ENODATA when there is no data to read, but
+        * the kernel tracer returns -EAGAIN when there is currently no data
+        * for a non-finalized stream, and -ENODATA when there is no data for a
+        * finalized stream. Those can be combined into a -ENODATA return value.
+        */
+       if (ret == -EAGAIN) {
+               ret = -ENODATA;
+       }
+
        return ret;
 }
 
@@ -1699,8 +1716,23 @@ int put_next_subbuffer(struct lttng_consumer_stream *stream,
 static
 bool is_get_next_check_metadata_available(int tracer_fd)
 {
-       return kernctl_get_next_subbuf_metadata_check(tracer_fd, NULL) !=
-                       -ENOTTY;
+       const int ret = kernctl_get_next_subbuf_metadata_check(tracer_fd, NULL);
+       const bool available = ret != -ENOTTY;
+
+       if (ret == 0) {
+               /* get succeeded, make sure to put the subbuffer. */
+               kernctl_put_subbuf(tracer_fd);
+       }
+
+       return available;
+}
+
+static
+int signal_metadata(struct lttng_consumer_stream *stream,
+               struct lttng_consumer_local_data *ctx)
+{
+       ASSERT_LOCKED(stream->metadata_rdv_lock);
+       return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
 }
 
 static
@@ -1733,6 +1765,8 @@ int lttng_kconsumer_set_stream_ops(
                        metadata_bucket_destroy(stream->metadata_bucket);
                        stream->metadata_bucket = NULL;
                }
+
+               stream->read_subbuffer_ops.on_sleep = signal_metadata;
        }
 
        if (!stream->read_subbuffer_ops.get_next_subbuffer) {
This page took 0.025233 seconds and 5 git commands to generate.