Fix: lttng-live: 100ms active poll delay
[babeltrace.git] / formats / lttng-live / lttng-live-comm.c
index a53f6c3a65c7f6d6603d9ed799ffa52c2caf3d23..568a4a1e806d64db8ad6191d4f0fcce25670ea21 100644 (file)
@@ -33,6 +33,7 @@
 #include <inttypes.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <poll.h>
 
 #include <babeltrace/ctf/ctf-index.h>
 
@@ -54,6 +55,8 @@
 #include "lttng-live.h"
 #include "lttng-viewer-abi.h"
 
+#define ACTIVE_POLL_DELAY      100     /* ms */
+
 /*
  * Memory allocation zeroed
  */
@@ -72,6 +75,38 @@ static int get_new_metadata(struct lttng_live_ctx *ctx,
                struct lttng_live_viewer_stream *viewer_stream,
                char **metadata_buf);
 
+static
+ssize_t lttng_live_recv(int fd, void *buf, size_t len)
+{
+       ssize_t ret;
+       size_t copied = 0, to_copy = len;
+
+       do {
+               ret = recv(fd, buf + copied, to_copy, 0);
+               if (ret > 0) {
+                       assert(ret <= to_copy);
+                       copied += ret;
+                       to_copy -= ret;
+               }
+       } while ((ret > 0 && to_copy > 0)
+               || (ret < 0 && errno == EINTR));
+       if (ret > 0)
+               ret = copied;
+       /* ret = 0 means orderly shutdown, ret < 0 is error. */
+       return ret;
+}
+
+static
+ssize_t lttng_live_send(int fd, const void *buf, size_t len)
+{
+       ssize_t ret;
+
+       do {
+               ret = send(fd, buf, len, MSG_NOSIGNAL);
+       } while (ret < 0 && errno == EINTR);
+       return ret;
+}
+
 int lttng_live_connect_viewer(struct lttng_live_ctx *ctx)
 {
        struct hostent *host;
@@ -124,9 +159,7 @@ int lttng_live_establish_connection(struct lttng_live_ctx *ctx)
        connect.minor = htobe32(LTTNG_LIVE_MINOR);
        connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);
 
-       do {
-               ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
        if (ret_len < 0) {
                perror("[error] Error sending cmd");
                ret = ret_len;
@@ -134,9 +167,7 @@ int lttng_live_establish_connection(struct lttng_live_ctx *ctx)
        }
        assert(ret_len == sizeof(cmd));
 
-       do {
-               ret_len = send(ctx->control_sock, &connect, sizeof(connect), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &connect, sizeof(connect));
        if (ret_len < 0) {
                perror("[error] Error sending version");
                ret = ret_len;
@@ -144,9 +175,7 @@ int lttng_live_establish_connection(struct lttng_live_ctx *ctx)
        }
        assert(ret_len == sizeof(connect));
 
-       do {
-               ret_len = recv(ctx->control_sock, &connect, sizeof(connect), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock, &connect, sizeof(connect));
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -251,9 +280,7 @@ int lttng_live_list_sessions(struct lttng_live_ctx *ctx, const char *path)
        cmd.data_size = 0;
        cmd.cmd_version = 0;
 
-       do {
-               ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
        if (ret_len < 0) {
                perror("[error] Error sending cmd");
                ret = ret_len;
@@ -261,9 +288,7 @@ int lttng_live_list_sessions(struct lttng_live_ctx *ctx, const char *path)
        }
        assert(ret_len == sizeof(cmd));
 
-       do {
-               ret_len = recv(ctx->control_sock, &list, sizeof(list), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock, &list, sizeof(list));
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -278,9 +303,7 @@ int lttng_live_list_sessions(struct lttng_live_ctx *ctx, const char *path)
 
        sessions_count = be32toh(list.sessions_count);
        for (i = 0; i < sessions_count; i++) {
-               do {
-                       ret_len = recv(ctx->control_sock, &lsession, sizeof(lsession), 0);
-               } while (ret_len < 0 && errno == EINTR);
+               ret_len = lttng_live_recv(ctx->control_sock, &lsession, sizeof(lsession));
                if (ret_len == 0) {
                        fprintf(stderr, "[error] Remote side has closed connection\n");
                        ret = -1;
@@ -386,9 +409,7 @@ int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
        // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
        rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST);
 
-       do {
-               ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
        if (ret_len < 0) {
                perror("[error] Error sending cmd");
                ret = ret_len;
@@ -396,9 +417,7 @@ int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
        }
        assert(ret_len == sizeof(cmd));
 
-       do {
-               ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
        if (ret_len < 0) {
                perror("[error] Error sending attach request");
                ret = ret_len;
@@ -406,9 +425,7 @@ int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
        }
        assert(ret_len == sizeof(rq));
 
-       do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -465,9 +482,7 @@ int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
        ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
                        ctx->session->stream_count);
        for (i = 0; i < be32toh(rp.streams_count); i++) {
-               do {
-                       ret_len = recv(ctx->control_sock, &stream, sizeof(stream), 0);
-               } while (ret_len < 0 && errno == EINTR);
+               ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
                if (ret_len == 0) {
                        fprintf(stderr, "[error] Remote side has closed connection\n");
                        ret = -1;
@@ -606,9 +621,7 @@ retry:
        rq.offset = offset;
        rq.len = htobe32(len);
 
-       do {
-               ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
        if (ret_len < 0) {
                perror("[error] Error sending cmd");
                ret = ret_len;
@@ -616,9 +629,7 @@ retry:
        }
        assert(ret_len == sizeof(cmd));
 
-       do {
-               ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
        if (ret_len < 0) {
                perror("[error] Error sending get_data_packet request");
                ret = ret_len;
@@ -626,9 +637,7 @@ retry:
        }
        assert(ret_len == sizeof(rq));
 
-       do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -721,11 +730,8 @@ retry:
                                stream->mmap_size);
        }
 
-       do {
-               ret_len = recv(ctx->control_sock,
-                       mmap_align_addr(pos->base_mma), len,
-                       MSG_WAITALL);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock,
+                       mmap_align_addr(pos->base_mma), len);
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -760,9 +766,7 @@ int get_one_metadata_packet(struct lttng_live_ctx *ctx,
        cmd.data_size = sizeof(rq);
        cmd.cmd_version = 0;
 
-       do {
-               ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
        if (ret_len < 0) {
                perror("[error] Error sending cmd");
                ret = ret_len;
@@ -770,9 +774,7 @@ int get_one_metadata_packet(struct lttng_live_ctx *ctx,
        }
        assert(ret_len == sizeof(cmd));
 
-       do {
-               ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
        if (ret_len < 0) {
                perror("[error] Error sending get_metadata request");
                ret = ret_len;
@@ -780,9 +782,7 @@ int get_one_metadata_packet(struct lttng_live_ctx *ctx,
        }
        assert(ret_len == sizeof(rq));
 
-       do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -826,9 +826,7 @@ int get_one_metadata_packet(struct lttng_live_ctx *ctx,
                ret = -1;
                goto error;
        }
-       do {
-               ret_len = recv(ctx->control_sock, data, len, MSG_WAITALL);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock, data, len);
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -874,7 +872,7 @@ int get_new_metadata(struct lttng_live_ctx *ctx,
 {
        int ret = 0;
        struct lttng_live_viewer_stream *metadata_stream;
-       size_t size;
+       size_t size, len_read = 0;;
 
        metadata_stream = viewer_stream->ctf_trace->metadata_stream;
        if (!metadata_stream) {
@@ -895,9 +893,16 @@ int get_new_metadata(struct lttng_live_ctx *ctx,
                 * negative value on error.
                 */
                ret = get_one_metadata_packet(ctx, metadata_stream);
-       } while (ret > 0);
+               if (ret > 0) {
+                       len_read += ret;
+               }
+               if (!len_read) {
+                       (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
+               }
+       } while (ret > 0 || !len_read);
 
-       fclose(metadata_stream->metadata_fp_write);
+       if (fclose(metadata_stream->metadata_fp_write))
+               perror("fclose");
        metadata_stream->metadata_fp_write = NULL;
 
 error:
@@ -928,9 +933,7 @@ int get_next_index(struct lttng_live_ctx *ctx,
        rq.stream_id = htobe64(viewer_stream->id);
 
 retry:
-       do {
-               ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
        if (ret_len < 0) {
                perror("[error] Error sending cmd");
                ret = ret_len;
@@ -938,9 +941,7 @@ retry:
        }
        assert(ret_len == sizeof(cmd));
 
-       do {
-               ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
        if (ret_len < 0) {
                perror("[error] Error sending get_next_index request");
                ret = ret_len;
@@ -948,9 +949,7 @@ retry:
        }
        assert(ret_len == sizeof(rq));
 
-       do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -996,7 +995,7 @@ retry:
                break;
        case LTTNG_VIEWER_INDEX_RETRY:
                printf_verbose("get_next_index: retry\n");
-               sleep(1);
+               (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
                goto retry;
        case LTTNG_VIEWER_INDEX_HUP:
                printf_verbose("get_next_index: stream hung up\n");
@@ -1165,9 +1164,7 @@ int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx)
        cmd.data_size = 0;
        cmd.cmd_version = 0;
 
-       do {
-               ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
        if (ret_len < 0) {
                perror("[error] Error sending cmd");
                ret = ret_len;
@@ -1175,9 +1172,7 @@ int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx)
        }
        assert(ret_len == sizeof(cmd));
 
-       do {
-               ret_len = recv(ctx->control_sock, &resp, sizeof(resp), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock, &resp, sizeof(resp));
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -1329,9 +1324,7 @@ int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
        memset(&rq, 0, sizeof(rq));
        rq.session_id = htobe64(id);
 
-       do {
-               ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
        if (ret_len < 0) {
                perror("[error] Error sending cmd");
                ret = ret_len;
@@ -1339,9 +1332,7 @@ int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
        }
        assert(ret_len == sizeof(cmd));
 
-       do {
-               ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
        if (ret_len < 0) {
                perror("[error] Error sending get_new_streams request");
                ret = ret_len;
@@ -1349,9 +1340,7 @@ int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
        }
        assert(ret_len == sizeof(rq));
 
-       do {
-               ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0);
-       } while (ret_len < 0 && errno == EINTR);
+       ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
        if (ret_len == 0) {
                fprintf(stderr, "[error] Remote side has closed connection\n");
                ret = -1;
@@ -1400,9 +1389,7 @@ int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
        ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
                        ctx->session->stream_count);
        for (i = 0; i < stream_count; i++) {
-               do {
-                       ret_len = recv(ctx->control_sock, &stream, sizeof(stream), 0);
-               } while (ret_len < 0 && errno == EINTR);
+               ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
                if (ret_len == 0) {
                        fprintf(stderr, "[error] Remote side has closed connection\n");
                        ret = -1;
@@ -1507,6 +1494,8 @@ void lttng_live_read(struct lttng_live_ctx *ctx)
                        ret = ask_new_streams(ctx);
                        if (ret < 0)
                                goto end_free;
+                       if (!ctx->session->stream_count)
+                               (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
                }
 
                g_hash_table_foreach(ctx->session->ctf_traces, add_traces,
This page took 0.034172 seconds and 4 git commands to generate.