X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fconsumer.c;h=53806b08d8ee4a0dcd54b80ee71edaca221fd784;hp=16a6c47f4269e4ad9155ba5d242a5e99ed0c8805;hb=633d0084183d5b0aed953ce267e0e57e5ef29bd5;hpb=1d4dfdefb6efe31cd6436931f893caf7b05d0223 diff --git a/src/common/consumer.c b/src/common/consumer.c index 16a6c47f4..53806b08d 100644 --- a/src/common/consumer.c +++ b/src/common/consumer.c @@ -175,7 +175,7 @@ static void consumer_rcu_free_relayd(struct rcu_head *head) * * This function MUST be called with the consumer_data lock acquired. */ -void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd) +static void destroy_relayd(struct consumer_relayd_sock_pair *relayd) { int ret; struct lttng_ht_iter iter; @@ -218,7 +218,7 @@ void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd) /* Destroy the relayd if refcount is 0 */ if (uatomic_read(&relayd->refcount) == 0) { - consumer_destroy_relayd(relayd); + destroy_relayd(relayd); } } @@ -314,23 +314,25 @@ void consumer_del_stream(struct lttng_consumer_stream *stream) /* Both conditions are met, we destroy the relayd. */ if (uatomic_read(&relayd->refcount) == 0 && uatomic_read(&relayd->destroy_flag)) { - consumer_destroy_relayd(relayd); + destroy_relayd(relayd); } } rcu_read_unlock(); - if (!--stream->chan->refcount) { + uatomic_dec(&stream->chan->refcount); + if (!uatomic_read(&stream->chan->refcount) + && !uatomic_read(&stream->chan->nb_init_streams)) { free_chan = stream->chan; } - call_rcu(&stream->node.head, consumer_free_stream); end: consumer_data.need_update = 1; pthread_mutex_unlock(&consumer_data.lock); - if (free_chan) + if (free_chan) { consumer_del_channel(free_chan); + } } struct lttng_consumer_stream *consumer_allocate_stream( @@ -343,7 +345,8 @@ struct lttng_consumer_stream *consumer_allocate_stream( uid_t uid, gid_t gid, int net_index, - int metadata_flag) + int metadata_flag, + int *alloc_ret) { struct lttng_consumer_stream *stream; int ret; @@ -351,12 +354,13 @@ struct lttng_consumer_stream *consumer_allocate_stream( stream = zmalloc(sizeof(*stream)); if (stream == NULL) { perror("malloc struct lttng_consumer_stream"); - goto end; + *alloc_ret = -ENOMEM; + return NULL; } stream->chan = consumer_find_channel(channel_key); if (!stream->chan) { - perror("Unable to find channel key"); - goto end; + *alloc_ret = -ENOENT; + goto error; } stream->chan->refcount++; stream->key = stream_key; @@ -385,24 +389,37 @@ struct lttng_consumer_stream *consumer_allocate_stream( stream->cpu = stream->chan->cpucount++; ret = lttng_ustconsumer_allocate_stream(stream); if (ret) { - free(stream); - return NULL; + *alloc_ret = -EINVAL; + goto error; } break; default: ERR("Unknown consumer_data type"); - assert(0); - goto end; + *alloc_ret = -EINVAL; + goto error; } - DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)", - stream->path_name, stream->key, - stream->shm_fd, - stream->wait_fd, - (unsigned long long) stream->mmap_len, - stream->out_fd, + + /* + * When nb_init_streams reaches 0, we don't need to trigger any action in + * terms of destroying the associated channel, because the action that + * causes the count to become 0 also causes a stream to be added. The + * channel deletion will thus be triggered by the following removal of this + * stream. + */ + if (uatomic_read(&stream->chan->nb_init_streams) > 0) { + uatomic_dec(&stream->chan->nb_init_streams); + } + + DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu," + " out_fd %d, net_seq_idx %d)", stream->path_name, stream->key, + stream->shm_fd, stream->wait_fd, + (unsigned long long) stream->mmap_len, stream->out_fd, stream->net_seq_idx); -end: return stream; + +error: + free(stream); + return NULL; } /* @@ -452,8 +469,7 @@ end: * Add relayd socket to global consumer data hashtable. RCU read side lock MUST * be acquired before calling this. */ - -int consumer_add_relayd(struct consumer_relayd_sock_pair *relayd) +static int add_relayd(struct consumer_relayd_sock_pair *relayd) { int ret = 0; struct lttng_ht_node_ulong *node; @@ -672,7 +688,8 @@ struct lttng_consumer_channel *consumer_allocate_channel( int channel_key, int shm_fd, int wait_fd, uint64_t mmap_len, - uint64_t max_sb_size) + uint64_t max_sb_size, + unsigned int nb_init_streams) { struct lttng_consumer_channel *channel; int ret; @@ -688,6 +705,7 @@ struct lttng_consumer_channel *consumer_allocate_channel( channel->mmap_len = mmap_len; channel->max_sb_size = max_sb_size; channel->refcount = 0; + channel->nb_init_streams = nb_init_streams; lttng_ht_node_init_ulong(&channel->node, channel->key); switch (consumer_data.type) { @@ -1503,12 +1521,14 @@ static void destroy_stream_ht(struct lttng_ht *ht) return; } + rcu_read_lock(); cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) { ret = lttng_ht_del(ht, &iter); assert(!ret); free(stream); } + rcu_read_unlock(); lttng_ht_destroy(ht); } @@ -1519,7 +1539,6 @@ static void destroy_stream_ht(struct lttng_ht *ht) static void consumer_del_metadata_stream(struct lttng_consumer_stream *stream) { int ret; - struct lttng_consumer_channel *free_chan = NULL; struct consumer_relayd_sock_pair *relayd; assert(stream); @@ -1594,19 +1613,17 @@ static void consumer_del_metadata_stream(struct lttng_consumer_stream *stream) /* Both conditions are met, we destroy the relayd. */ if (uatomic_read(&relayd->refcount) == 0 && uatomic_read(&relayd->destroy_flag)) { - consumer_destroy_relayd(relayd); + destroy_relayd(relayd); } } rcu_read_unlock(); /* Atomically decrement channel refcount since other threads can use it. */ uatomic_dec(&stream->chan->refcount); - if (!uatomic_read(&stream->chan->refcount)) { - free_chan = stream->chan; - } - - if (free_chan) { - consumer_del_channel(free_chan); + if (!uatomic_read(&stream->chan->refcount) + && !uatomic_read(&stream->chan->nb_init_streams)) { + /* Go for channel deletion! */ + consumer_del_channel(stream->chan); } free(stream); @@ -1696,7 +1713,7 @@ restart: /* Check the metadata pipe for incoming metadata. */ if (pollfd == ctx->consumer_metadata_pipe[0]) { - if (revents & (LPOLLERR | LPOLLHUP | LPOLLNVAL)) { + if (revents & (LPOLLERR | LPOLLHUP )) { DBG("Metadata thread pipe hung up"); /* * Remove the pipe from the poll set and continue the loop @@ -1706,20 +1723,13 @@ restart: close(ctx->consumer_metadata_pipe[0]); continue; } else if (revents & LPOLLIN) { - stream = zmalloc(sizeof(struct lttng_consumer_stream)); - if (stream == NULL) { - PERROR("zmalloc metadata consumer stream"); - goto error; - } - do { - /* Get the stream and add it to the local hash table */ - ret = read(pollfd, stream, - sizeof(struct lttng_consumer_stream)); + /* Get the stream pointer received */ + ret = read(pollfd, &stream, sizeof(stream)); } while (ret < 0 && errno == EINTR); - if (ret < 0 || ret < sizeof(struct lttng_consumer_stream)) { + if (ret < 0 || + ret < sizeof(struct lttng_consumer_stream *)) { PERROR("read metadata stream"); - free(stream); /* * Let's continue here and hope we can still work * without stopping the consumer. XXX: Should we? @@ -1730,9 +1740,11 @@ restart: DBG("Adding metadata stream %d to poll set", stream->wait_fd); + rcu_read_lock(); /* The node should be init at this point */ lttng_ht_add_unique_ulong(metadata_ht, &stream->waitfd_node); + rcu_read_unlock(); /* Add metadata stream to the global poll events list */ lttng_poll_add(&events, stream->wait_fd, @@ -1747,11 +1759,13 @@ restart: /* From here, the event is a metadata wait fd */ + rcu_read_lock(); lttng_ht_lookup(metadata_ht, (void *)((unsigned long) pollfd), &iter); node = lttng_ht_iter_get_node_ulong(&iter); if (node == NULL) { /* FD not found, continue loop */ + rcu_read_unlock(); continue; } @@ -1766,6 +1780,7 @@ restart: len = ctx->on_buffer_ready(stream, ctx); /* It's ok to have an unavailable sub-buffer */ if (len < 0 && len != -EAGAIN) { + rcu_read_unlock(); goto end; } else if (len > 0) { stream->data_read = 1; @@ -1776,7 +1791,7 @@ restart: * Remove the stream from the hash table since there is no data * left on the fd because we previously did a read on the buffer. */ - if (revents & (LPOLLERR | LPOLLHUP | LPOLLNVAL)) { + if (revents & (LPOLLERR | LPOLLHUP)) { DBG("Metadata fd %d is hup|err|nval.", pollfd); if (!stream->hangup_flush_done && (consumer_data.type == LTTNG_CONSUMER32_UST @@ -1788,15 +1803,18 @@ restart: len = ctx->on_buffer_ready(stream, ctx); /* It's ok to have an unavailable sub-buffer */ if (len < 0 && len != -EAGAIN) { + rcu_read_unlock(); goto end; } } /* Removing it from hash table, poll set and free memory */ lttng_ht_del(metadata_ht, &iter); + lttng_poll_del(&events, stream->wait_fd); consumer_del_metadata_stream(stream); } + rcu_read_unlock(); } } @@ -2290,7 +2308,7 @@ int consumer_add_relayd_socket(int net_seq_idx, int sock_type, * Add relayd socket pair to consumer data hashtable. If object already * exists or on error, the function gracefully returns. */ - consumer_add_relayd(relayd); + add_relayd(relayd); /* All good! */ ret = 0;