X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fconsumer.c;h=2604f3d060574c1c7677047fcc16581a5c54818d;hp=d7b319452a87088eab5d6cd648fd7515e5b387f5;hb=509bb1cf6c7fc3de758b0a58d3610a653235788b;hpb=702b1ea4d37c122c2622004fd4cf53175e7c7754 diff --git a/src/common/consumer.c b/src/common/consumer.c index d7b319452..2604f3d06 100644 --- a/src/common/consumer.c +++ b/src/common/consumer.c @@ -2,24 +2,22 @@ * Copyright (C) 2011 - Julien Desfossez * Mathieu Desnoyers * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; only version 2 - * of the License. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2 only, + * as published by the Free Software Foundation. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _GNU_SOURCE #include -#include #include #include #include @@ -86,9 +84,18 @@ static void consumer_steal_stream_key(int key) { struct lttng_consumer_stream *stream; + rcu_read_lock(); stream = consumer_find_stream(key); - if (stream) + if (stream) { stream->key = -1; + /* + * We don't want the lookup to match, but we still need + * to iterate on this stream when iterating over the hash table. Just + * change the node key. + */ + stream->node.key = -1; + } + rcu_read_unlock(); } static struct lttng_consumer_channel *consumer_find_channel(int key) @@ -119,9 +126,18 @@ static void consumer_steal_channel_key(int key) { struct lttng_consumer_channel *channel; + rcu_read_lock(); channel = consumer_find_channel(key); - if (channel) + if (channel) { channel->key = -1; + /* + * We don't want the lookup to match, but we still need + * to iterate on this channel when iterating over the hash table. Just + * change the node key. + */ + channel->node.key = -1; + } + rcu_read_unlock(); } static @@ -167,15 +183,9 @@ void consumer_del_stream(struct lttng_consumer_stream *stream) } rcu_read_lock(); - - /* Get stream node from hash table */ - lttng_ht_lookup(consumer_data.stream_ht, - (void *)((unsigned long) stream->key), &iter); - /* - * Remove stream node from hash table. It can fail if it's been - * replaced due to key reuse. - */ - (void) lttng_ht_del(consumer_data.stream_ht, &iter); + iter.iter.node = &stream->node.node; + ret = lttng_ht_del(consumer_data.stream_ht, &iter); + assert(!ret); rcu_read_unlock(); @@ -288,17 +298,24 @@ end: int consumer_add_stream(struct lttng_consumer_stream *stream) { int ret = 0; + struct lttng_ht_node_ulong *node; + struct lttng_ht_iter iter; pthread_mutex_lock(&consumer_data.lock); /* Steal stream identifier, for UST */ consumer_steal_stream_key(stream->key); rcu_read_lock(); - /* - * We simply remove the old channel from the hash table. It's - * ok, since we know for sure the sessiond wants to replace it - * with the new version, because the key has been reused. - */ - (void) lttng_ht_add_replace_ulong(consumer_data.stream_ht, &stream->node); + + lttng_ht_lookup(consumer_data.stream_ht, + (void *)((unsigned long) stream->key), &iter); + node = lttng_ht_iter_get_node_ulong(&iter); + if (node != NULL) { + rcu_read_unlock(); + /* Stream already exist. Ignore the insertion */ + goto end; + } + + lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node); rcu_read_unlock(); consumer_data.stream_count++; consumer_data.need_update = 1; @@ -376,16 +393,9 @@ void consumer_del_channel(struct lttng_consumer_channel *channel) } rcu_read_lock(); - - lttng_ht_lookup(consumer_data.channel_ht, - (void *)((unsigned long) channel->key), &iter); - - /* - * Remove channel node from hash table. It can fail if it's been - * replaced due to key reuse. - */ - (void) lttng_ht_del(consumer_data.channel_ht, &iter); - + iter.iter.node = &channel->node.node; + ret = lttng_ht_del(consumer_data.channel_ht, &iter); + assert(!ret); rcu_read_unlock(); if (channel->mmap_base != NULL) { @@ -468,16 +478,25 @@ end: */ int consumer_add_channel(struct lttng_consumer_channel *channel) { + struct lttng_ht_node_ulong *node; + struct lttng_ht_iter iter; + pthread_mutex_lock(&consumer_data.lock); /* Steal channel identifier, for UST */ consumer_steal_channel_key(channel->key); rcu_read_lock(); - /* - * We simply remove the old channel from the hash table. It's - * ok, since we know for sure the sessiond wants to replace it - * with the new version, because the key has been reused. - */ - (void) lttng_ht_add_replace_ulong(consumer_data.channel_ht, &channel->node); + + lttng_ht_lookup(consumer_data.channel_ht, + (void *)((unsigned long) channel->key), &iter); + node = lttng_ht_iter_get_node_ulong(&iter); + if (node != NULL) { + /* Channel already exist. Ignore the insertion */ + goto end; + } + + lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node); + +end: rcu_read_unlock(); pthread_mutex_unlock(&consumer_data.lock); @@ -500,6 +519,7 @@ int consumer_update_poll_array( struct lttng_consumer_stream *stream; DBG("Updating poll fd array"); + rcu_read_lock(); cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream, node.node) { if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) { @@ -511,13 +531,14 @@ int consumer_update_poll_array( local_stream[i] = stream; i++; } + rcu_read_unlock(); /* * Insert the consumer_poll_pipe at the end of the array and don't * increment i so nb_fd is the number of real FD. */ (*pollfd)[i].fd = ctx->consumer_poll_pipe[0]; - (*pollfd)[i].events = POLLIN; + (*pollfd)[i].events = POLLIN | POLLPRI; return i; } @@ -541,7 +562,7 @@ restart: perror("Poll error"); goto exit; } - if (consumer_sockpoll[0].revents == POLLIN) { + if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) { DBG("consumer_should_quit wake up"); goto exit; } @@ -644,7 +665,7 @@ void lttng_consumer_sync_trace_file( if (orig_offset < stream->chan->max_sb_size) { return; } - sync_file_range(outfd, orig_offset - stream->chan->max_sb_size, + lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size, stream->chan->max_sb_size, SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE @@ -812,6 +833,8 @@ ssize_t lttng_consumer_on_read_subbuffer_mmap( ERR("Unknown consumer_data type"); assert(0); } + + return 0; } /* @@ -995,7 +1018,7 @@ void *lttng_consumer_thread_poll_fds(void *data) * array. We want to prioritize array update over * low-priority reads. */ - if (pollfd[nb_fd].revents & POLLIN) { + if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) { DBG("consumer_poll_pipe wake up"); tmp2 = read(ctx->consumer_poll_pipe[0], &tmp, 1); if (tmp2 < 0) { @@ -1035,8 +1058,6 @@ void *lttng_consumer_thread_poll_fds(void *data) local_stream[i]->hangup_flush_done) { ssize_t len; - assert(!(pollfd[i].revents & POLLERR)); - assert(!(pollfd[i].revents & POLLNVAL)); DBG("Normal read on fd %d", pollfd[i].fd); len = ctx->on_buffer_ready(local_stream[i], ctx); /* it's ok to have an unavailable sub-buffer */