X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=formats%2Flttng-live%2Flttng-live-comm.c;h=3970ac22ee6ed43fa33dae4664061cc478811836;hb=85bb466398bbf32d8334066011bfe7be7ba71d8a;hp=842eae0840661f1b5ebf972a728e355db49a7e38;hpb=7def295f1625bf793b85fb032f63f8a2925b8307;p=babeltrace.git diff --git a/formats/lttng-live/lttng-live-comm.c b/formats/lttng-live/lttng-live-comm.c index 842eae08..3970ac22 100644 --- a/formats/lttng-live/lttng-live-comm.c +++ b/formats/lttng-live/lttng-live-comm.c @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -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; @@ -574,10 +589,12 @@ int append_metadata(struct lttng_live_ctx *ctx, ret = ctf_append_trace_metadata( viewer_stream->ctf_trace->handle->td, metadata->ctf_trace->metadata_fp); - if (ret != 0) { + /* We accept empty metadata packets */ + if (ret != 0 && ret != -ENOENT) { fprintf(stderr, "[error] Appending metadata\n"); goto error; } + ret = 0; error: return ret; @@ -606,9 +623,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 +631,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 +639,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; @@ -665,14 +676,18 @@ retry: ret = append_metadata(ctx, stream); if (ret) goto error; - goto retry; } if (rp.flags & LTTNG_VIEWER_FLAG_NEW_STREAM) { + printf_verbose("get_data_packet: new streams needed\n"); ret = ask_new_streams(ctx); if (ret < 0) goto error; - g_hash_table_foreach(ctx->session->ctf_traces, add_traces, - ctx->bt_ctx); + g_hash_table_foreach(ctx->session->ctf_traces, + add_traces, ctx->bt_ctx); + } + if (rp.flags & (LTTNG_VIEWER_FLAG_NEW_METADATA + | LTTNG_VIEWER_FLAG_NEW_STREAM)) { + goto retry; } fprintf(stderr, "[error] get_data_packet: error\n"); ret = -1; @@ -721,11 +736,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; @@ -737,7 +749,7 @@ retry: goto error; } assert(ret_len == len); - + ret = 0; end: error: return ret; @@ -760,9 +772,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 +780,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 +788,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 +832,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 +878,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,7 +899,13 @@ 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); if (fclose(metadata_stream->metadata_fp_write)) perror("fclose"); @@ -929,9 +939,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; @@ -939,9 +947,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; @@ -949,9 +955,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; @@ -991,13 +995,13 @@ retry: ret = ask_new_streams(ctx); if (ret < 0) goto error; - g_hash_table_foreach(ctx->session->ctf_traces, add_traces, - ctx->bt_ctx); + g_hash_table_foreach(ctx->session->ctf_traces, + add_traces, ctx->bt_ctx); } 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"); @@ -1166,9 +1170,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; @@ -1176,9 +1178,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; @@ -1330,9 +1330,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; @@ -1340,9 +1338,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; @@ -1350,9 +1346,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; @@ -1401,9 +1395,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; @@ -1475,8 +1467,9 @@ void lttng_live_read(struct lttng_live_ctx *ctx) sout = container_of(td_write, struct ctf_text_stream_pos, trace_descriptor); - if (!sout->parent.event_cb) + if (!sout->parent.event_cb) { goto end_free; + } ret = lttng_live_create_viewer_session(ctx); if (ret < 0) { @@ -1503,11 +1496,16 @@ void lttng_live_read(struct lttng_live_ctx *ctx) int flags; while (!ctx->session->stream_count) { - if (ctx->session_ids->len == 0) + if (ctx->session_ids->len == 0) { goto end_free; + } ret = ask_new_streams(ctx); - if (ret < 0) + 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,