X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-relayd%2Flive.c;h=9029a5955a4ad2a4f7551fb033e371914b905bee;hb=b6025e9476332b75eb8184345c3eb3e924780088;hp=2c3a10574b5499c6a4a8585f3a22e5eac05562bc;hpb=1a1a34b40ab10a195633b1ed5e2e9b42fdae0a78;p=lttng-tools.git diff --git a/src/bin/lttng-relayd/live.c b/src/bin/lttng-relayd/live.c index 2c3a10574..9029a5955 100644 --- a/src/bin/lttng-relayd/live.c +++ b/src/bin/lttng-relayd/live.c @@ -654,12 +654,16 @@ void *thread_dispatcher(void *data) health_code_update(); - while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) { + for (;;) { health_code_update(); /* Atomically prepare the queue futex */ futex_nto1_prepare(&viewer_conn_queue.futex); + if (CMM_LOAD_SHARED(live_dispatch_thread_exit)) { + break; + } + do { health_code_update(); @@ -1122,8 +1126,8 @@ error: /* * Open the index file if needed for the given vstream. * - * If an index file is successfully opened, the vstream index_fd set with - * it. + * If an index file is successfully opened, the vstream will set it as its + * current index file. * * Return 0 on success, a negative value on error (-ENOENT if not ready yet). * @@ -1134,7 +1138,7 @@ static int try_open_index(struct relay_viewer_stream *vstream, { int ret = 0; - if (vstream->index_fd) { + if (vstream->index_file) { goto end; } @@ -1145,20 +1149,12 @@ static int try_open_index(struct relay_viewer_stream *vstream, ret = -ENOENT; goto end; } - ret = index_open(vstream->path_name, vstream->channel_name, + vstream->index_file = lttng_index_file_open(vstream->path_name, + vstream->channel_name, vstream->stream->tracefile_count, vstream->current_tracefile_id); - if (ret >= 0) { - vstream->index_fd = stream_fd_create(ret); - if (!vstream->index_fd) { - if (close(ret)) { - PERROR("close"); - } - ret = -1; - } else { - ret = 0; - } - goto end; + if (!vstream->index_file) { + ret = -1; } end: @@ -1182,10 +1178,13 @@ static int check_index_status(struct relay_viewer_stream *vstream, { int ret; - if (trace->session->connection_closed + if ((trace->session->connection_closed || rstream->closed) && rstream->index_received_seqcount == vstream->index_sent_seqcount) { - /* Last index sent and session connection is closed. */ + /* + * Last index sent and session connection or relay + * stream are closed. + */ index->status = htobe32(LTTNG_VIEWER_INDEX_HUP); goto hup; } else if (rstream->beacon_ts_end != -1ULL && @@ -1277,7 +1276,6 @@ static int viewer_get_next_index(struct relay_connection *conn) { int ret; - ssize_t read_ret; struct lttng_viewer_get_next_index request_index; struct lttng_viewer_index viewer_index; struct ctf_packet_index packet_index; @@ -1400,11 +1398,10 @@ int viewer_get_next_index(struct relay_connection *conn) viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM; } - read_ret = lttng_read(vstream->index_fd->fd, &packet_index, - sizeof(packet_index)); - if (read_ret < sizeof(packet_index)) { - ERR("Relay reading index file %d returned %zd", - vstream->index_fd->fd, read_ret); + ret = lttng_index_file_read(vstream->index_file, &packet_index); + if (ret) { + ERR("Relay error reading index file %d", + vstream->index_file->fd); viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); goto send_reply; } else { @@ -1485,13 +1482,15 @@ error_put: static int viewer_get_packet(struct relay_connection *conn) { - int ret, send_data = 0; + int ret; char *data = NULL; - uint32_t len = 0; - ssize_t read_len; struct lttng_viewer_get_packet get_packet_info; struct lttng_viewer_trace_packet reply; struct relay_viewer_stream *vstream = NULL; + bool skip_send_data = false; + uint32_t send_len = sizeof(reply); + uint32_t packet_data_len = 0; + ssize_t read_len; DBG2("Relay get data packet"); @@ -1509,21 +1508,26 @@ int viewer_get_packet(struct relay_connection *conn) vstream = viewer_stream_get_by_id(be64toh(get_packet_info.stream_id)); if (!vstream) { + skip_send_data = true; DBG("Client requested packet of unknown stream id %" PRIu64, be64toh(get_packet_info.stream_id)); reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR); - goto send_reply_nolock; + } else { + packet_data_len = be32toh(get_packet_info.len); + send_len += packet_data_len; } - pthread_mutex_lock(&vstream->stream->lock); - - len = be32toh(get_packet_info.len); - data = zmalloc(len); + data = zmalloc(send_len); if (!data) { PERROR("relay data zmalloc"); goto error; } + if (skip_send_data) { + goto send_reply_nolock; + } + + pthread_mutex_lock(&vstream->stream->lock); ret = lseek(vstream->stream_fd->fd, be64toh(get_packet_info.offset), SEEK_SET); if (ret < 0) { @@ -1531,16 +1535,17 @@ int viewer_get_packet(struct relay_connection *conn) be64toh(get_packet_info.offset)); goto error; } - read_len = lttng_read(vstream->stream_fd->fd, data, len); - if (read_len < len) { + read_len = lttng_read(vstream->stream_fd->fd, + data + sizeof(reply), + packet_data_len); + if (read_len < packet_data_len) { PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64, vstream->stream_fd->fd, be64toh(get_packet_info.offset)); goto error; } reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK); - reply.len = htobe32(len); - send_data = 1; + reply.len = htobe32(packet_data_len); goto send_reply; error: @@ -1551,26 +1556,19 @@ send_reply: pthread_mutex_unlock(&vstream->stream->lock); } send_reply_nolock: - reply.flags = htobe32(reply.flags); health_code_update(); - ret = send_response(conn->sock, &reply, sizeof(reply)); + memcpy(data, &reply, sizeof(reply)); + health_code_update(); + ret = send_response(conn->sock, data, send_len); + health_code_update(); if (ret < 0) { + PERROR("sendmsg of packet data failed"); goto end_free; } - health_code_update(); - - if (send_data) { - health_code_update(); - ret = send_response(conn->sock, data, len); - if (ret < 0) { - goto end_free; - } - health_code_update(); - } - DBG("Sent %u bytes for stream %" PRIu64, len, + DBG("Sent %u bytes for stream %" PRIu64, send_len, be64toh(get_packet_info.stream_id)); end_free: