Skip to contentSkip to conversation

Fix: sessiond vs consumerd push/get metadata deadlock

PATCH New Mathieu Desnoyers 1 participant 5 messages Aug 17, 2015 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/4 Fix: sessiond vs consumerd push/get metadata deadlock

We need to unlock the registry while we push metadata to break a circular dependency between the consumerd metadata lock and the sessiond registry lock. Indeed, pushing metadata to the consumerd awaits that it gets pushed all the way to relayd, but doing so requires grabbing the metadata lock. If a concurrent metadata request is being performed by consumerd, this can try to grab the registry lock on the sessiond while holding the metadata lock on the consumer daemon. Those push and pull schemes are performed on two different bidirectionnal communication sockets.

---
 src/bin/lttng-sessiond/ust-app.c       | 100 +++++++++++++++++++++++----------
 src/bin/lttng-sessiond/ust-app.h       |   6 +-
 src/bin/lttng-sessiond/ust-consumer.c  |   2 +-
 src/common/ust-consumer/ust-consumer.c |   9 +++
 4 files changed, 84 insertions(+), 33 deletions(-)
src/bin/lttng-sessiond/ust-app.c +70 -30
index 6f032da..d452986 100644
@@ -436,21 +436,25 @@ void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan,
  * terminated concurrently).
  */
 ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
-		struct consumer_socket *socket, int send_zero_data)
+		struct consumer_socket *socket, int send_zero_data,
+		int peek_consumer)
 {
 	int ret;
 	char *metadata_str = NULL;
 	size_t len, offset;
 	ssize_t ret_val;
+	uint64_t metadata_key;
 
 	assert(registry);
 	assert(socket);
 
+	metadata_key = registry->metadata_key;
+
 	/*
 	 * Means that no metadata was assigned to the session. This can
 	 * happens if no start has been done previously.
 	 */
-	if (!registry->metadata_key) {
+	if (!metadata_key) {
 		return 0;
 	}
 
@@ -466,6 +470,42 @@ ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
 		return -EPIPE;
 	}
 
+	if (peek_consumer) {
+		pthread_mutex_unlock(&registry->lock);
+		/*
+		 * Always need to unlock registry while sending metadata to
+		 * consumer due to deadlock (see below). Try to push empty
+		 * metadata to detect cases where the metadata stream is not
+		 * created on the consumer side. This allows us to assume it is
+		 * there in the following push, which should not fail.
+		 */
+		ret = consumer_push_metadata(socket, metadata_key,
+				NULL, 0, 0);
+		pthread_mutex_lock(&registry->lock);
+		if (ret < 0) {
+			/*
+			 * There is an acceptable race here between the registry
+			 * metadata key assignment and the creation on the
+			 * consumer. The session daemon can concurrently push
+			 * metadata for this registry while being created on the
+			 * consumer since the metadata key of the registry is
+			 * assigned *before* it is setup to avoid the consumer
+			 * to ask for metadata that could possibly be not found
+			 * in the session daemon.
+			 *
+			 * The metadata will get pushed either by the session
+			 * being stopped or the consumer requesting metadata if
+			 * that race is triggered.
+			 */
+			if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
+				ret = 0;
+			}
+
+			ret_val = ret;
+			goto error_push;
+		}
+	}
+
 	offset = registry->metadata_len_sent;
 	len = registry->metadata_len - registry->metadata_len_sent;
 	if (len == 0) {
@@ -488,39 +528,39 @@ ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
 	}
 	/* Copy what we haven't send out. */
 	memcpy(metadata_str, registry->metadata + offset, len);
+	/*
+	 * Metadata may have been concurrently pushed, since we're not
+	 * holding the registry lock while pushing to consumer. This is
+	 * handled by the fact that we send the metadata content, size,
+	 * and the offset at which that metadata belongs. This may
+	 * arrive out of order on the consumer side, and the consumer is
+	 * able to reorder the fragments. The consumer supports
+	 * non-consecutive fragments, but not overlapping fragments.
+	 */
 	registry->metadata_len_sent += len;
 
 push_data:
-	ret = consumer_push_metadata(socket, registry->metadata_key,
+	pthread_mutex_unlock(&registry->lock);
+	/*
+	 * We need to unlock the registry while we push metadata to
+	 * break a circular dependency between the consumerd metadata
+	 * lock and the sessiond registry lock. Indeed, pushing metadata
+	 * to the consumerd awaits that it gets pushed all the way to
+	 * relayd, but doing so requires grabbing the metadata lock. If
+	 * a concurrent metadata request is being performed by
+	 * consumerd, this can try to grab the registry lock on the
+	 * sessiond while holding the metadata lock on the consumer
+	 * daemon. Those push and pull schemes are performed on two
+	 * different bidirectionnal communication sockets.
+	 */
+	ret = consumer_push_metadata(socket, metadata_key,
 			metadata_str, len, offset);
+	pthread_mutex_lock(&registry->lock);
 	if (ret < 0) {
-		/*
-		 * There is an acceptable race here between the registry
-		 * metadata key assignment and the creation on the
-		 * consumer. The session daemon can concurrently push
-		 * metadata for this registry while being created on the
-		 * consumer since the metadata key of the registry is
-		 * assigned *before* it is setup to avoid the consumer
-		 * to ask for metadata that could possibly be not found
-		 * in the session daemon.
-		 *
-		 * The metadata will get pushed either by the session
-		 * being stopped or the consumer requesting metadata if
-		 * that race is triggered.
-		 */
-		if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
-			ret = 0;
-		}
-
-		/*
-		 * Update back the actual metadata len sent since it
-		 * failed here.
-		 */
-		registry->metadata_len_sent -= len;
-		ret_val = ret;
-		goto error_push;
+		ERR("Error pushing metadata to consumer");
+		ret_val = -LTTCOMM_CONSUMERD_CHANNEL_FAIL;
+		goto error;
 	}
-
 	free(metadata_str);
 	return len;
 
@@ -579,7 +619,7 @@ static int push_metadata(struct ust_registry_session *registry,
 		goto error;
 	}
 
-	ret = ust_app_push_metadata(registry, socket, 0);
+	ret = ust_app_push_metadata(registry, socket, 0, 1);
 	if (ret < 0) {
 		ret_val = ret;
 		goto error;
src/bin/lttng-sessiond/ust-app.h +4 -2
index 102b05b..35da66b 100644
@@ -326,7 +326,8 @@ void ust_app_add(struct ust_app *app);
 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock);
 void ust_app_notify_sock_unregister(int sock);
 ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
-		struct consumer_socket *socket, int send_zero_data);
+		struct consumer_socket *socket, int send_zero_data,
+		int peek_consumer);
 void ust_app_destroy(struct ust_app *app);
 int ust_app_snapshot_record(struct ltt_ust_session *usess,
 		struct snapshot_output *output, int wait,
@@ -507,7 +508,8 @@ void ust_app_notify_sock_unregister(int sock)
 }
 static inline
 ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
-		struct consumer_socket *socket, int send_zero_data)
+		struct consumer_socket *socket, int send_zero_data,
+		int peek_consumer)
 {
 	return 0;
 }
src/bin/lttng-sessiond/ust-consumer.c +1 -1
index ad076e3..bacbf92 100644
@@ -509,7 +509,7 @@ int ust_consumer_metadata_request(struct consumer_socket *socket)
 	assert(ust_reg);
 
 	pthread_mutex_lock(&ust_reg->lock);
-	ret_push = ust_app_push_metadata(ust_reg, socket, 1);
+	ret_push = ust_app_push_metadata(ust_reg, socket, 1, 0);
 	pthread_mutex_unlock(&ust_reg->lock);
 	if (ret_push == -EPIPE) {
 		DBG("Application or relay closed while pushing metadata");
src/common/ust-consumer/ust-consumer.c +9 -0
index fbc3bbb..a3daff3 100644
@@ -1578,6 +1578,15 @@ int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
 
 		health_code_update();
 
+		if (!len) {
+			/*
+			 * There is nothing to receive. We have simply
+			 * checked whether the channel can be found.
+			 */
+			ret_code = LTTCOMM_CONSUMERD_SUCCESS;
+			goto end_msg_sessiond;
+		}
+
 		/* Tell session daemon we are ready to receive the metadata. */
 		ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
 		if (ret < 0) {
Signature · 1 line
2.1.4
#2 lore raw

PATCH 2/4 Fix: sessiond ust-app session teardown race

In reply to a message not in the archive

Add a deleted flag within the ust app session which is raised (with ust app session lock held) at delete, and checked within each RCU traversal, again with ust app session lock held.

This takes care of races between teardown of an application (unregister) and execution of commands which are accessing the app session concurrently.

---
 src/bin/lttng-sessiond/ust-app.c | 57 ++++++++++++++++++++++++++++++++++++++++
 src/bin/lttng-sessiond/ust-app.h |  2 ++
 2 files changed, 59 insertions(+)
src/bin/lttng-sessiond/ust-app.c +57 -0
index d452986..369eab9 100644
@@ -717,6 +717,9 @@ void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
 
 	pthread_mutex_lock(&ua_sess->lock);
 
+	assert(!ua_sess->deleted);
+	ua_sess->deleted = true;
+
 	registry = get_session_registry(ua_sess);
 	if (registry) {
 		/* Push metadata for application before freeing the application. */
@@ -3209,6 +3212,11 @@ void ust_app_unregister(int sock)
 		 */
 		pthread_mutex_lock(&ua_sess->lock);
 
+		if (ua_sess->deleted) {
+			pthread_mutex_unlock(&ua_sess->lock);
+			continue;
+		}
+
 		/*
 		 * Normally, this is done in the delete session process which is
 		 * executed in the call rcu below. However, upon registration we can't
@@ -3801,6 +3809,12 @@ int ust_app_create_channel_glb(struct ltt_ust_session *usess,
 		assert(ua_sess);
 
 		pthread_mutex_lock(&ua_sess->lock);
+
+		if (ua_sess->deleted) {
+			pthread_mutex_unlock(&ua_sess->lock);
+			continue;
+		}
+
 		if (!strncmp(uchan->name, DEFAULT_METADATA_NAME,
 					sizeof(uchan->name))) {
 			copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &uchan->attr);
@@ -3870,6 +3884,11 @@ int ust_app_enable_event_glb(struct ltt_ust_session *usess,
 
 		pthread_mutex_lock(&ua_sess->lock);
 
+		if (ua_sess->deleted) {
+			pthread_mutex_unlock(&ua_sess->lock);
+			continue;
+		}
+
 		/* Lookup channel in the ust app session */
 		lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
 		ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
@@ -3936,6 +3955,12 @@ int ust_app_create_event_glb(struct ltt_ust_session *usess,
 		}
 
 		pthread_mutex_lock(&ua_sess->lock);
+
+		if (ua_sess->deleted) {
+			pthread_mutex_unlock(&ua_sess->lock);
+			continue;
+		}
+
 		/* Lookup channel in the ust app session */
 		lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
 		ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
@@ -3987,6 +4012,11 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
 
 	pthread_mutex_lock(&ua_sess->lock);
 
+	if (ua_sess->deleted) {
+		pthread_mutex_unlock(&ua_sess->lock);
+		goto end;
+	}
+
 	/* Upon restart, we skip the setup, already done */
 	if (ua_sess->started) {
 		goto skip_setup;
@@ -4087,6 +4117,11 @@ int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
 
 	pthread_mutex_lock(&ua_sess->lock);
 
+	if (ua_sess->deleted) {
+		pthread_mutex_unlock(&ua_sess->lock);
+		goto end_no_session;
+	}
+
 	/*
 	 * If started = 0, it means that stop trace has been called for a session
 	 * that was never started. It's possible since we can have a fail start
@@ -4167,6 +4202,10 @@ int ust_app_flush_app_session(struct ust_app *app,
 
 	pthread_mutex_lock(&ua_sess->lock);
 
+	if (ua_sess->deleted) {
+		goto end_deleted;
+	}
+
 	health_code_update();
 
 	/* Flushing buffers */
@@ -4196,6 +4235,7 @@ int ust_app_flush_app_session(struct ust_app *app,
 
 	health_code_update();
 
+end_deleted:
 	pthread_mutex_unlock(&ua_sess->lock);
 
 end_not_compatible:
@@ -4429,6 +4469,11 @@ void ust_app_global_create(struct ltt_ust_session *usess, struct ust_app *app)
 
 	pthread_mutex_lock(&ua_sess->lock);
 
+	if (ua_sess->deleted) {
+		pthread_mutex_unlock(&ua_sess->lock);
+		goto end;
+	}
+
 	/*
 	 * We can iterate safely here over all UST app session since the create ust
 	 * app session above made a shadow copy of the UST global domain from the
@@ -4571,6 +4616,12 @@ int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
 		}
 
 		pthread_mutex_lock(&ua_sess->lock);
+
+		if (ua_sess->deleted) {
+			pthread_mutex_unlock(&ua_sess->lock);
+			continue;
+		}
+
 		/* Lookup channel in the ust app session */
 		lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
 		ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
@@ -4629,6 +4680,12 @@ int ust_app_enable_event_pid(struct ltt_ust_session *usess,
 	}
 
 	pthread_mutex_lock(&ua_sess->lock);
+
+	if (ua_sess->deleted) {
+		ret = 0;
+		goto end_unlock;
+	}
+
 	/* Lookup channel in the ust app session */
 	lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
 	ua_chan_node = lttng_ht_iter_get_node_str(&iter);
src/bin/lttng-sessiond/ust-app.h +2 -0
index 35da66b..60569d0 100644
@@ -182,6 +182,8 @@ struct ust_app_session {
 	int started;  /* allows detection of start vs restart. */
 	int handle;   /* used has unique identifier for app session */
 
+	bool deleted;	/* Session deleted flag. Check with lock held. */
+
 	/*
 	 * Tracing session ID. Multiple ust app session can have the same tracing
 	 * session id making this value NOT unique to the object.
Signature · 1 line
2.1.4
#3 lore raw

PATCH 3/4 Fix: sessiond should not error on channel creation vs app exit

In reply to a message not in the archive

We should not report an error when creating a channel if the application is exiting concurrently.

Also, remove an inappropriate assert() in ust_app_create_event_glb: it is possible to have a channel lookup fail if channel/event creation occurs concurrently with an application exit.

---
 src/bin/lttng-sessiond/ust-app.c | 82 +++++++++++++++++++++++++++++-----------
 1 file changed, 59 insertions(+), 23 deletions(-)
src/bin/lttng-sessiond/ust-app.c +59 -23
index 369eab9..db13601 100644
@@ -1416,7 +1416,10 @@ static int send_channel_pid_to_ust(struct ust_app *app,
 
 	/* Send channel to the application. */
 	ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
-	if (ret < 0) {
+	if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
+		ret = -ENOTCONN;	/* Caused by app exiting. */
+		goto error;
+	} else if (ret < 0) {
 		goto error;
 	}
 
@@ -1425,7 +1428,10 @@ static int send_channel_pid_to_ust(struct ust_app *app,
 	/* Send all streams to application. */
 	cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
 		ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
-		if (ret < 0) {
+		if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
+			ret = -ENOTCONN;	/* Caused by app exiting. */
+			goto error;
+		} else if (ret < 0) {
 			goto error;
 		}
 		/* We don't need the stream anymore once sent to the tracer. */
@@ -2557,7 +2563,10 @@ static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
 
 	/* Send channel to the application. */
 	ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
-	if (ret < 0) {
+	if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
+		ret = -ENOTCONN;	/* Caused by app exiting. */
+		goto error;
+	} else if (ret < 0) {
 		goto error;
 	}
 
@@ -2576,6 +2585,12 @@ static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
 		ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
 		if (ret < 0) {
 			(void) release_ust_app_stream(-1, &stream);
+			if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
+				ret = -ENOTCONN; /* Caused by app exiting. */
+				goto error;
+			} else if (ret < 0) {
+				goto error;
+			}
 			goto error_stream_unlock;
 		}
 
@@ -2669,10 +2684,9 @@ static int create_channel_per_uid(struct ust_app *app,
 	/* Send buffers to the application. */
 	ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
 	if (ret < 0) {
-		/*
-		 * Don't report error to the console, since it may be
-		 * caused by application concurrently exiting.
-		 */
+		if (ret != -ENOTCONN) {
+			ERR("Error sending channel to application");
+		}
 		goto error;
 	}
 
@@ -2723,10 +2737,9 @@ static int create_channel_per_pid(struct ust_app *app,
 
 	ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
 	if (ret < 0) {
-		/*
-		 * Don't report error to the console, since it may be
-		 * caused by application concurrently exiting.
-		 */
+		if (ret != -ENOTCONN) {
+			ERR("Error sending channel to application");
+		}
 		goto error;
 	}
 
@@ -2740,7 +2753,8 @@ error:
  * need and send it to the application. This MUST be called with a RCU read
  * side lock acquired.
  *
- * Return 0 on success or else a negative value.
+ * Return 0 on success or else a negative value. Returns -ENOTCONN if
+ * the application exited concurrently.
  */
 static int do_create_channel(struct ust_app *app,
 		struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
@@ -2799,7 +2813,8 @@ error:
  *
  * Called with UST app session lock and RCU read-side lock held.
  *
- * Return 0 on success or else a negative value.
+ * Return 0 on success or else a negative value. Returns -ENOTCONN if
+ * the application exited concurrently.
  */
 static int create_ust_app_channel(struct ust_app_session *ua_sess,
 		struct ltt_ust_channel *uchan, struct ust_app *app,
@@ -3800,6 +3815,7 @@ int ust_app_create_channel_glb(struct ltt_ust_session *usess,
 				 * or a timeout on it. We can't inform the caller that for a
 				 * specific app, the session failed so lets continue here.
 				 */
+				ret = 0;	/* Not an error. */
 				continue;
 			case -ENOMEM:
 			default:
@@ -3826,14 +3842,24 @@ int ust_app_create_channel_glb(struct ltt_ust_session *usess,
 		}
 		pthread_mutex_unlock(&ua_sess->lock);
 		if (ret < 0) {
-			if (ret == -ENOMEM) {
-				/* No more memory is a fatal error. Stop right now. */
-				goto error_rcu_unlock;
-			}
 			/* Cleanup the created session if it's the case. */
 			if (created) {
 				destroy_app_session(app, ua_sess);
 			}
+			switch (ret) {
+			case -ENOTCONN:
+				/*
+				 * The application's socket is not valid. Either a bad socket
+				 * or a timeout on it. We can't inform the caller that for a
+				 * specific app, the session failed so lets continue here.
+				 */
+				ret = 0;	/* Not an error. */
+				continue;
+			case -ENOMEM:
+			default:
+
+				goto error_rcu_unlock;
+			}
 		}
 	}
 
@@ -3964,8 +3990,15 @@ int ust_app_create_event_glb(struct ltt_ust_session *usess,
 		/* Lookup channel in the ust app session */
 		lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
 		ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
-		/* If the channel is not found, there is a code flow error */
-		assert(ua_chan_node);
+		/*
+		 * It is possible that the channel cannot be found is
+		 * the channel/event creation occurs concurrently with
+		 * an application exit.
+		 */
+		if (!ua_chan_node) {
+			pthread_mutex_unlock(&ua_sess->lock);
+			continue;
+		}
 
 		ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
 
@@ -4482,11 +4515,14 @@ void ust_app_global_create(struct ltt_ust_session *usess, struct ust_app *app)
 	cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
 			node.node) {
 		ret = do_create_channel(app, usess, ua_sess, ua_chan);
-		if (ret < 0) {
+		if (ret < 0 && ret != -ENOTCONN) {
 			/*
-			 * Stop everything. On error, the application failed, no more
-			 * file descriptor are available or ENOMEM so stopping here is
-			 * the only thing we can do for now.
+			 * Stop everything. On error, the application
+			 * failed, no more file descriptor are available
+			 * or ENOMEM so stopping here is the only thing
+			 * we can do for now. The only exception is
+			 * -ENOTCONN, which indicates that the application
+			 * has exit.
 			 */
 			goto error_unlock;
 		}
Signature · 1 line
2.1.4
#4 lore raw

PATCH v3 4/4 Fix: Relay daemon ownership and reference counting

In reply to a message not in the archive

The ownership and reference counting of the relay daemon is unclear and buggy in many ways. It is the cause of memory corruptions, double-free, leaks, segmentation faults, observed in various conditions.

Fix this situation by introducing a clear ownership and reference counting scheme for this daemon.

See doc/relayd-architecture.txt for details.

---
 doc/Makefile.am                       |    3 +-
 doc/relayd-architecture.txt           |   96 +++
 src/bin/lttng-relayd/Makefile.am      |    4 +-
 src/bin/lttng-relayd/cmd-2-1.c        |   17 +-
 src/bin/lttng-relayd/cmd-2-1.h        |   10 +-
 src/bin/lttng-relayd/cmd-2-2.c        |   22 +-
 src/bin/lttng-relayd/cmd-2-2.h        |   10 +-
 src/bin/lttng-relayd/cmd-2-4.c        |   19 +-
 src/bin/lttng-relayd/cmd-2-4.h        |   10 +-
 src/bin/lttng-relayd/cmd-generic.c    |    1 +
 src/bin/lttng-relayd/cmd-generic.h    |    7 +-
 src/bin/lttng-relayd/cmd.h            |    7 +-
 src/bin/lttng-relayd/connection.c     |  128 ++--
 src/bin/lttng-relayd/connection.h     |   63 +-
 src/bin/lttng-relayd/ctf-trace.c      |  198 +++---
 src/bin/lttng-relayd/ctf-trace.h      |   66 +-
 src/bin/lttng-relayd/index.c          |  342 ++++++----
 src/bin/lttng-relayd/index.h          |   60 +-
 src/bin/lttng-relayd/live.c           | 1049 ++++++++++++++---------------
 src/bin/lttng-relayd/live.h           |   10 +-
 src/bin/lttng-relayd/lttng-relayd.h   |   23 +-
 src/bin/lttng-relayd/main.c           | 1167 ++++++++++++++-------------------
 src/bin/lttng-relayd/session.c        |  214 +++---
 src/bin/lttng-relayd/session.h        |  116 ++--
 src/bin/lttng-relayd/stream-fd.c      |   58 ++
 src/bin/lttng-relayd/stream-fd.h      |   32 +
 src/bin/lttng-relayd/stream.c         |  343 ++++++++--
 src/bin/lttng-relayd/stream.h         |  130 ++--
 src/bin/lttng-relayd/utils.h          |    7 +-
 src/bin/lttng-relayd/viewer-session.c |  164 +++++
 src/bin/lttng-relayd/viewer-session.h |   53 ++
 src/bin/lttng-relayd/viewer-stream.c  |  325 +++++----
 src/bin/lttng-relayd/viewer-stream.h  |   83 ++-
 src/bin/lttng-sessiond/consumer.c     |    7 +-
 src/bin/lttng-sessiond/ust-consumer.c |    3 +-
 src/common/index/ctf-index.h          |    2 +-
 src/common/index/index.c              |   13 +
 src/common/utils.c                    |  109 ++-
 src/common/utils.h                    |    2 +
 39 files changed, 2909 insertions(+), 2064 deletions(-)
 create mode 100644 doc/relayd-architecture.txt
 create mode 100644 src/bin/lttng-relayd/stream-fd.c
 create mode 100644 src/bin/lttng-relayd/stream-fd.h
 create mode 100644 src/bin/lttng-relayd/viewer-session.c
 create mode 100644 src/bin/lttng-relayd/viewer-session.h
doc/Makefile.am +2 -1
index 4ff71eb..7ae9303 100644
@@ -1,7 +1,8 @@
 SUBDIRS = man
 EXTRA_DIST = quickstart.txt streaming-howto.txt python-howto.txt \
 	snapshot-howto.txt calibrate.txt kernel-CodingStyle.txt \
-	live-reading-howto.txt live-reading-protocol.txt
+	live-reading-howto.txt live-reading-protocol.txt \
+	relayd-architecture.txt
 
 dist_doc_DATA = quickstart.txt streaming-howto.txt python-howto.txt \
 	snapshot-howto.txt calibrate.txt live-reading-howto.txt \
doc/relayd-architecture.txt new file +96 -0

96 lines not on this page · raw message

src/bin/lttng-relayd/Makefile.am +3 -1
index 126cdaf..428f352 100644
@@ -17,7 +17,9 @@ lttng_relayd_SOURCES = main.c lttng-relayd.h utils.h utils.c cmd.h \
                        viewer-stream.h viewer-stream.c \
                        session.c session.h \
                        stream.c stream.h \
-                       connection.c connection.h
+                       stream-fd.c stream-fd.h \
+                       connection.c connection.h \
+                       viewer-session.c viewer-session.h
 
 # link on liblttngctl for check if relayd is already alive.
 lttng_relayd_LDADD = -lrt -lurcu-common -lurcu \
src/bin/lttng-relayd/cmd-2-1.c +9 -8
index 0cd9b5a..a2c340f 100644
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2013 - Julien Desfossez <[email protected]>
  *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
  *
  * 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
@@ -28,30 +29,30 @@
 #include "cmd-2-1.h"
 #include "utils.h"
 
+/*
+ * cmd_recv_stream_2_1 allocates path_name and channel_name.
+ */
 int cmd_recv_stream_2_1(struct relay_connection *conn,
-		struct relay_stream *stream)
+		char **path_name, char **channel_name)
 {
 	int ret;
 	struct lttcomm_relayd_add_stream stream_info;
 
-	assert(conn);
-	assert(stream);
-
 	ret = cmd_recv(conn->sock, &stream_info, sizeof(stream_info));
 	if (ret < 0) {
 		ERR("Unable to recv stream version 2.1");
 		goto error;
 	}
 
-	stream->path_name = create_output_path(stream_info.pathname);
-	if (stream->path_name == NULL) {
+	*path_name = create_output_path(stream_info.pathname);
+	if (*path_name == NULL) {
 		PERROR("Path name allocation");
 		ret = -ENOMEM;
 		goto error;
 	}
 
-	stream->channel_name = strdup(stream_info.channel_name);
-	if (stream->channel_name == NULL) {
+	*channel_name = strdup(stream_info.channel_name);
+	if (*channel_name == NULL) {
 		ret = -errno;
 		PERROR("Path name allocation");
 		goto error;
src/bin/lttng-relayd/cmd-2-1.h +5 -5
index bab8190..46283dc 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_2_1_H
+#define RELAYD_CMD_2_1_H
+
 /*
  * Copyright (C) 2013 - Julien Desfossez <[email protected]>
  *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
  *
  * 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
@@ -16,13 +20,9 @@
  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifndef RELAYD_CMD_2_1_H
-#define RELAYD_CMD_2_1_H
-
 #include "lttng-relayd.h"
-#include "stream.h"
 
 int cmd_recv_stream_2_1(struct relay_connection *conn,
-		struct relay_stream *stream);
+		char **path_name, char **channel_name);
 
 #endif /* RELAYD_CMD_2_1_H */
src/bin/lttng-relayd/cmd-2-2.c +12 -10
index 7dd99ad..1493b73 100644
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2013 - Julien Desfossez <[email protected]>
  *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
  *
  * 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
@@ -30,37 +31,38 @@
 #include "cmd-2-1.h"
 #include "utils.h"
 
+/*
+ * cmd_recv_stream_2_2 allocates path_name and channel_name.
+ */
 int cmd_recv_stream_2_2(struct relay_connection *conn,
-		struct relay_stream *stream)
+		char **path_name, char **channel_name,
+		uint64_t *tracefile_size, uint64_t *tracefile_count)
 {
 	int ret;
 	struct lttcomm_relayd_add_stream_2_2 stream_info;
 
-	assert(conn);
-	assert(stream);
-
 	ret = cmd_recv(conn->sock, &stream_info, sizeof(stream_info));
 	if (ret < 0) {
 		ERR("Unable to recv stream version 2.2");
 		goto error;
 	}
 
-	stream->path_name = create_output_path(stream_info.pathname);
-	if (stream->path_name == NULL) {
+	*path_name = create_output_path(stream_info.pathname);
+	if (*path_name == NULL) {
 		PERROR("Path name allocation");
 		ret = -ENOMEM;
 		goto error;
 	}
 
-	stream->channel_name = strdup(stream_info.channel_name);
-	if (stream->channel_name == NULL) {
+	*channel_name = strdup(stream_info.channel_name);
+	if (*channel_name == NULL) {
 		ret = -errno;
 		PERROR("Path name allocation");
 		goto error;
 	}
 
-	stream->tracefile_size = be64toh(stream_info.tracefile_size);
-	stream->tracefile_count = be64toh(stream_info.tracefile_count);
+	*tracefile_size = be64toh(stream_info.tracefile_size);
+	*tracefile_count = be64toh(stream_info.tracefile_count);
 	ret = 0;
 
 error:
src/bin/lttng-relayd/cmd-2-2.h +6 -4
index bd1cd14..894a63a 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_2_2_H
+#define RELAYD_CMD_2_2_H
+
 /*
  * Copyright (C) 2013 - Julien Desfossez <[email protected]>
  *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
  *
  * 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
@@ -16,12 +20,10 @@
  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifndef RELAYD_CMD_2_2_H
-#define RELAYD_CMD_2_2_H
-
 #include "lttng-relayd.h"
 
 int cmd_recv_stream_2_2(struct relay_connection *conn,
-		struct relay_stream *stream);
+		char **path_name, char **channel_name,
+		uint64_t *tracefile_size, uint64_t *tracefile_count);
 
 #endif /* RELAYD_CMD_2_2_H */
src/bin/lttng-relayd/cmd-2-4.c +9 -10
index d8aa737..a3290cb 100644
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2013 - Julien Desfossez <[email protected]>
  *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
  *
  * 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
@@ -30,26 +31,24 @@
 #include "lttng-relayd.h"
 
 int cmd_create_session_2_4(struct relay_connection *conn,
-		struct relay_session *session)
+		char *session_name, char *hostname,
+		uint32_t *live_timer, bool *snapshot)
 {
 	int ret;
 	struct lttcomm_relayd_create_session_2_4 session_info;
 
-	assert(conn);
-	assert(session);
-
 	ret = cmd_recv(conn->sock, &session_info, sizeof(session_info));
 	if (ret < 0) {
 		ERR("Unable to recv session info version 2.4");
 		goto error;
 	}
 
-	strncpy(session->session_name, session_info.session_name,
-			sizeof(session->session_name));
-	strncpy(session->hostname, session_info.hostname,
-			sizeof(session->hostname));
-	session->live_timer = be32toh(session_info.live_timer);
-	session->snapshot = be32toh(session_info.snapshot);
+	strncpy(session_name, session_info.session_name,
+			sizeof(session_info.session_name));
+	strncpy(hostname, session_info.hostname,
+			sizeof(session_info.hostname));
+	*live_timer = be32toh(session_info.live_timer);
+	*snapshot = be32toh(session_info.snapshot);
 
 	ret = 0;
 
src/bin/lttng-relayd/cmd-2-4.h +6 -4
index aaf572a..ab73478 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_2_4_H
+#define RELAYD_CMD_2_4_H
+
 /*
  * Copyright (C) 2013 - Julien Desfossez <[email protected]>
  *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
  *
  * 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
@@ -16,12 +20,10 @@
  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifndef RELAYD_CMD_2_4_H
-#define RELAYD_CMD_2_4_H
-
 #include "lttng-relayd.h"
 
 int cmd_create_session_2_4(struct relay_connection *conn,
-		struct relay_session *session);
+		char *session_name, char *hostname,
+		uint32_t *live_timer, bool *snapshot);
 
 #endif /* RELAYD_CMD_2_4_H */
src/bin/lttng-relayd/cmd-generic.c +1 -0
index 417d6d3..276e85b 100644
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2013 - Julien Desfossez <[email protected]>
  *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
  *
  * 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
src/bin/lttng-relayd/cmd-generic.h +4 -3
index 640fed7..4551f0a 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_GENERIC_H
+#define RELAYD_CMD_GENERIC_H
+
 /*
  * Copyright (C) 2013 - Julien Desfossez <[email protected]>
  *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
  *
  * 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
@@ -16,9 +20,6 @@
  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifndef RELAYD_CMD_GENERIC_H
-#define RELAYD_CMD_GENERIC_H
-
 #include <common/sessiond-comm/sessiond-comm.h>
 
 #include "connection.h"
src/bin/lttng-relayd/cmd.h +4 -3
index c8b37d5..88db09a 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_H
+#define RELAYD_CMD_H
+
 /*
  * Copyright (C) 2013 - Julien Desfossez <[email protected]>
  *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
  *
  * 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
@@ -16,9 +20,6 @@
  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifndef RELAYD_CMD_H
-#define RELAYD_CMD_H
-
 #include "cmd-generic.h"
 #include "cmd-2-1.h"
 #include "cmd-2-2.h"
src/bin/lttng-relayd/connection.c +85 -43

182 lines not on this page · raw message

src/bin/lttng-relayd/connection.h +39 -24

96 lines not on this page · raw message

src/bin/lttng-relayd/ctf-trace.c +121 -77

275 lines not on this page · raw message

src/bin/lttng-relayd/ctf-trace.h +37 -29

92 lines not on this page · raw message

src/bin/lttng-relayd/index.c +230 -112

426 lines not on this page · raw message

src/bin/lttng-relayd/index.h +39 -21

87 lines not on this page · raw message

src/bin/lttng-relayd/live.c +509 -540

1,469 lines not on this page · raw message

src/bin/lttng-relayd/live.h +5 -5
index 5db940b..2b8a3a0 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/lttng-relayd.h +10 -13
index 245c5fd..889071c 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/main.c +483 -684

1,167 lines not on this page · raw message

src/bin/lttng-relayd/session.c +137 -77

214 lines not on this page · raw message

src/bin/lttng-relayd/session.h +59 -57

116 lines not on this page · raw message

src/bin/lttng-relayd/stream-fd.c new file +58 -0

58 lines not on this page · raw message

src/bin/lttng-relayd/stream-fd.h new file +32 -0
new file mode 100644
index 0000000..64f3b16

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/stream.c +274 -69

343 lines not on this page · raw message

src/bin/lttng-relayd/stream.h +71 -59

130 lines not on this page · raw message

src/bin/lttng-relayd/utils.h +4 -3
index de1521d..4a56980 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/viewer-session.c new file +164 -0

164 lines not on this page · raw message

src/bin/lttng-relayd/viewer-session.h new file +53 -0

53 lines not on this page · raw message

src/bin/lttng-relayd/viewer-stream.c +194 -131

325 lines not on this page · raw message

src/bin/lttng-relayd/viewer-stream.h +38 -45

83 lines not on this page · raw message

src/bin/lttng-sessiond/consumer.c +2 -5
index 87d5f34..0c5ffff 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-sessiond/ust-consumer.c +1 -2
index bacbf92..4eaf341 100644

Lines of this file are not shown. View the raw message.

src/common/index/ctf-index.h +1 -1
index 0efa888..1f38d9a 100644

Lines of this file are not shown. View the raw message.

src/common/index/index.c +13 -0
index 35cff53..3fce35d 100644

Lines of this file are not shown. View the raw message.

src/common/utils.c +91 -18

109 lines not on this page · raw message

src/common/utils.h +2 -0
index 05914cc..8bc5a3f 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 9 lines
2.1.4




_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Part of the diff is not shown here. View the raw message.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

Re: [PATCH v3 lttng-tools 4/4] Fix: Relay daemon ownership and reference counting

This version of the patch has issues with tracefile rotation. I'm working on an updated version.

Thanks,

Mathieu

Quoted 7,917 lines · ----- On Aug 17, 2015, at 11:12 AM, Mathieu Desnoyers…

The ownership and reference counting of the relay daemon is unclear and buggy in many ways. It is the cause of memory corruptions, double-free, leaks, segmentation faults, observed in various conditions.

Fix this situation by introducing a clear ownership and reference counting scheme for this daemon.

See doc/relayd-architecture.txt for details.

---

doc/Makefile.am                       |    3 +-
doc/relayd-architecture.txt           |   96 +++
src/bin/lttng-relayd/Makefile.am      |    4 +-
src/bin/lttng-relayd/cmd-2-1.c        |   17 +-
src/bin/lttng-relayd/cmd-2-1.h        |   10 +-
src/bin/lttng-relayd/cmd-2-2.c        |   22 +-
src/bin/lttng-relayd/cmd-2-2.h        |   10 +-
src/bin/lttng-relayd/cmd-2-4.c        |   19 +-
src/bin/lttng-relayd/cmd-2-4.h        |   10 +-
src/bin/lttng-relayd/cmd-generic.c    |    1 +
src/bin/lttng-relayd/cmd-generic.h    |    7 +-
src/bin/lttng-relayd/cmd.h            |    7 +-
src/bin/lttng-relayd/connection.c     |  128 ++--
src/bin/lttng-relayd/connection.h     |   63 +-
src/bin/lttng-relayd/ctf-trace.c      |  198 +++---
src/bin/lttng-relayd/ctf-trace.h      |   66 +-
src/bin/lttng-relayd/index.c          |  342 ++++++----
src/bin/lttng-relayd/index.h          |   60 +-
src/bin/lttng-relayd/live.c           | 1049 ++++++++++++++---------------
src/bin/lttng-relayd/live.h           |   10 +-
src/bin/lttng-relayd/lttng-relayd.h   |   23 +-
src/bin/lttng-relayd/main.c           | 1167 ++++++++++++++-------------------
src/bin/lttng-relayd/session.c        |  214 +++---
src/bin/lttng-relayd/session.h        |  116 ++--
src/bin/lttng-relayd/stream-fd.c      |   58 ++
src/bin/lttng-relayd/stream-fd.h      |   32 +
src/bin/lttng-relayd/stream.c         |  343 ++++++++--
src/bin/lttng-relayd/stream.h         |  130 ++--
src/bin/lttng-relayd/utils.h          |    7 +-
src/bin/lttng-relayd/viewer-session.c |  164 +++++
src/bin/lttng-relayd/viewer-session.h |   53 ++
src/bin/lttng-relayd/viewer-stream.c  |  325 +++++----
src/bin/lttng-relayd/viewer-stream.h  |   83 ++-
src/bin/lttng-sessiond/consumer.c     |    7 +-
src/bin/lttng-sessiond/ust-consumer.c |    3 +-
src/common/index/ctf-index.h          |    2 +-
src/common/index/index.c              |   13 +
src/common/utils.c                    |  109 ++-
src/common/utils.h                    |    2 +
39 files changed, 2909 insertions(+), 2064 deletions(-)

create mode 100644 doc/relayd-architecture.txt create mode 100644 src/bin/lttng-relayd/stream-fd.c create mode 100644 src/bin/lttng-relayd/stream-fd.h create mode 100644 src/bin/lttng-relayd/viewer-session.c create mode 100644 src/bin/lttng-relayd/viewer-session.h

doc/Makefile.am +0 -0
index 4ff71eb..7ae9303 100644
@@ -1,7 +1,8 @@

SUBDIRS = man

EXTRA_DIST = quickstart.txt streaming-howto.txt python-howto.txt \
	snapshot-howto.txt calibrate.txt kernel-CodingStyle.txt \
doc/Makefile.am +2 -1
-	live-reading-howto.txt live-reading-protocol.txt
+	live-reading-howto.txt live-reading-protocol.txt \
+	relayd-architecture.txt
dist_doc_DATA = quickstart.txt streaming-howto.txt python-howto.txt \
	snapshot-howto.txt calibrate.txt live-reading-howto.txt \
doc/relayd-architecture.txt new file +96 -0
new file mode 100644
index 0000000..c45bd56
@@ -0,0 +1,96 @@
+LTTng Relay Daemon Architecture
+Mathieu Desnoyers, August 2015
+
+This document describes the object model and architecture of the relay
+daemon, after the refactoring done within the commit "Fix: Relay daemon
+ownership and reference counting".
+
+We have the following object composition hierarchy:
+
+relay connection (main.c, for sessiond/consumer)
+  |
+  \-> 0 or 1 session
+       |
+       \-> 0 or many ctf-trace
+                 |
+                 \-> 0 or many stream
+                      |   |
+                      |   \-> 0 or many index
+                      |
+                      \-------> 0 or 1 viewer stream
+
+live connection (live.c, for client)
+  |
+  \-> 1 viewer session
+       |
+       \-> 0 or many session (actually a reference to session as created
+             |                by the relay connection)
+             |
+             \-> ..... (ctf-trace, stream, index, viewer stream)
+
+There are global tables declared in lttng-relayd.h for sessions
+(sessions_ht, indexed by session id), streams (relay_streams_ht, indexed
+by stream handle), and viewer streams (viewer_streams_ht, indexed by
+stream handle). The purpuse of those tables is to allow fast lookup of
+those objects using the IDs received in the communication protocols.
+
+There is also one connection hash table per worker thread. There is one
+worker thread to receive data (main.c), and one worker thread to
+interact with viewer clients (live.c). Those tables are indexed by
+socket file descriptor.
+
+A RCU lookup+refcounting scheme has been introduced for all objects
+(except viewer session which is still an exception at the moment). This
+scheme allows looking up the objects or doing a traversal on the RCU
+linked list or hash table in combination with a getter on the object.
+This getter validables that there is still at least one reference to the
+object, else the lookup acts just as if the object does not exist. This
+scheme is protected by a "reflock" mutex in each object. "reflock"
+mutexes can be nested from the innermost object to the outermost object.
+IOW, the session reflock can nest within the ctf-trace reflock.
+
+There is also a "lock" mutex in each object. Those are used to
+synchronize between threads (currently the main.c relay thread and
+live.c client thread) when objects are shared. Locks can be nested form
+the outmost object to the innermost object. IOW, the ctf-trace lock can
+nest within the session lock.
+
+A "lock" should never nest within a "reflock".
+
+RCU linked lists are used to iterate using RCU, and protected by
+their own mutex for modifications. Iterations should be confirmed using
+the object "getter" to ensure its refcount is not 0 (exept in cases
+where the caller actually owns the objects and therefore can assume its
+refcount is not 0).
+
+RCU hash tables are used to iterate using RCU. Iteration should be
+confirmed using the object "getter" to ensure its refcount is not 0
+(except again if we have ownership and can assume the object refcount is
+not 0).
+
+Object creation has a refcount of 1. Each getter increments the
+refcount, and need to be paired with a "put" to decrement it. A final
+put on "self" (ownership) will allow refcount to reach 0, therefore
+triggering release, and thus free through call_rcu.
+
+In the composition scheme, we find back references from each composite
+to its container. Therefore, each composite holds a reference (refcount)
+on its container. This allows following pointers from e.g. viewer stream
+to stream to ctf-trace to session without performing any validation,
+due to transitive refcounting of those back-references.
+
+In addition to those back references, there are a few key ownership
+references held. The connection in the relay worker thread (main.c)
+holds ownership on the session, and on each stream it contains. The
+connection in the live worker thread (live.c) holds ownership on each
+the viewer stream it creates. The rest is ensured by back references
+from composite to container objects. When a connection is closed, it
+puts all the ownership references it is holding. This will then
+eventually trigger destruction of the session, streams, and viewer
+streams associated with the connection when all the back references
+reach 0.
+
+RCU read-side locks are now only held during iteration on RCU lists and
+hash tables, and within the internals of the get (lookup) and put
+functions. Those functions then use refcounting to ensure existance of
+the object when returned to their caller.
src/bin/lttng-relayd/Makefile.am +3 -1
index 126cdaf..428f352 100644
@@ -17,7 +17,9 @@ lttng_relayd_SOURCES = main.c lttng-relayd.h utils.h utils.c
 cmd.h \
                       viewer-stream.h viewer-stream.c \
                       session.c session.h \
                       stream.c stream.h \
-                       connection.c connection.h
+                       stream-fd.c stream-fd.h \
+                       connection.c connection.h \
+                       viewer-session.c viewer-session.h
 
 # link on liblttngctl for check if relayd is already alive.

lttng_relayd_LDADD = -lrt -lurcu-common -lurcu \

src/bin/lttng-relayd/cmd-2-1.c +9 -8
index 0cd9b5a..a2c340f 100644
@@ -1,6 +1,7 @@
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -28,30 +29,30 @@
 #include "cmd-2-1.h"
 #include "utils.h"
 
+/*
+ * cmd_recv_stream_2_1 allocates path_name and channel_name.
+ */
 int cmd_recv_stream_2_1(struct relay_connection *conn,
-		struct relay_stream *stream)
+		char **path_name, char **channel_name)
 {
 	int ret;
 	struct lttcomm_relayd_add_stream stream_info;
 
-	assert(conn);
-	assert(stream);
-
 	ret = cmd_recv(conn->sock, &stream_info, sizeof(stream_info));
 	if (ret < 0) {
 		ERR("Unable to recv stream version 2.1");
 		goto error;
 	}
 
-	stream->path_name = create_output_path(stream_info.pathname);
-	if (stream->path_name == NULL) {
+	*path_name = create_output_path(stream_info.pathname);
+	if (*path_name == NULL) {
 		PERROR("Path name allocation");
 		ret = -ENOMEM;
 		goto error;
 	}
 
-	stream->channel_name = strdup(stream_info.channel_name);
-	if (stream->channel_name == NULL) {
+	*channel_name = strdup(stream_info.channel_name);
+	if (*channel_name == NULL) {
 		ret = -errno;
 		PERROR("Path name allocation");
 		goto error;
src/bin/lttng-relayd/cmd-2-1.h +5 -5
index bab8190..46283dc 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_2_1_H
+#define RELAYD_CMD_2_1_H
+
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -16,13 +20,9 @@
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef RELAYD_CMD_2_1_H
-#define RELAYD_CMD_2_1_H
-
 #include "lttng-relayd.h"
-#include "stream.h"
 
 int cmd_recv_stream_2_1(struct relay_connection *conn,
-		struct relay_stream *stream);
+		char **path_name, char **channel_name);
 
 #endif /* RELAYD_CMD_2_1_H */
src/bin/lttng-relayd/cmd-2-2.c +12 -10
index 7dd99ad..1493b73 100644
@@ -1,6 +1,7 @@
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -30,37 +31,38 @@
 #include "cmd-2-1.h"
 #include "utils.h"
 
+/*
+ * cmd_recv_stream_2_2 allocates path_name and channel_name.
+ */
 int cmd_recv_stream_2_2(struct relay_connection *conn,
-		struct relay_stream *stream)
+		char **path_name, char **channel_name,
+		uint64_t *tracefile_size, uint64_t *tracefile_count)
 {
 	int ret;
 	struct lttcomm_relayd_add_stream_2_2 stream_info;
 
-	assert(conn);
-	assert(stream);
-
 	ret = cmd_recv(conn->sock, &stream_info, sizeof(stream_info));
 	if (ret < 0) {
 		ERR("Unable to recv stream version 2.2");
 		goto error;
 	}
 
-	stream->path_name = create_output_path(stream_info.pathname);
-	if (stream->path_name == NULL) {
+	*path_name = create_output_path(stream_info.pathname);
+	if (*path_name == NULL) {
 		PERROR("Path name allocation");
 		ret = -ENOMEM;
 		goto error;
 	}
 
-	stream->channel_name = strdup(stream_info.channel_name);
-	if (stream->channel_name == NULL) {
+	*channel_name = strdup(stream_info.channel_name);
+	if (*channel_name == NULL) {
 		ret = -errno;
 		PERROR("Path name allocation");
 		goto error;
 	}
 
-	stream->tracefile_size = be64toh(stream_info.tracefile_size);
-	stream->tracefile_count = be64toh(stream_info.tracefile_count);
+	*tracefile_size = be64toh(stream_info.tracefile_size);
+	*tracefile_count = be64toh(stream_info.tracefile_count);
 	ret = 0;
 

error:

src/bin/lttng-relayd/cmd-2-2.h +6 -4
index bd1cd14..894a63a 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_2_2_H
+#define RELAYD_CMD_2_2_H
+
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -16,12 +20,10 @@
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef RELAYD_CMD_2_2_H
-#define RELAYD_CMD_2_2_H
-
 #include "lttng-relayd.h"
 
 int cmd_recv_stream_2_2(struct relay_connection *conn,
-		struct relay_stream *stream);
+		char **path_name, char **channel_name,
+		uint64_t *tracefile_size, uint64_t *tracefile_count);
 
 #endif /* RELAYD_CMD_2_2_H */
src/bin/lttng-relayd/cmd-2-4.c +9 -10
index d8aa737..a3290cb 100644
@@ -1,6 +1,7 @@
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -30,26 +31,24 @@
 #include "lttng-relayd.h"
 
 int cmd_create_session_2_4(struct relay_connection *conn,
-		struct relay_session *session)
+		char *session_name, char *hostname,
+		uint32_t *live_timer, bool *snapshot)
 {
 	int ret;
 	struct lttcomm_relayd_create_session_2_4 session_info;
 
-	assert(conn);
-	assert(session);
-
 	ret = cmd_recv(conn->sock, &session_info, sizeof(session_info));
 	if (ret < 0) {
 		ERR("Unable to recv session info version 2.4");
 		goto error;
 	}
 
-	strncpy(session->session_name, session_info.session_name,
-			sizeof(session->session_name));
-	strncpy(session->hostname, session_info.hostname,
-			sizeof(session->hostname));
-	session->live_timer = be32toh(session_info.live_timer);
-	session->snapshot = be32toh(session_info.snapshot);
+	strncpy(session_name, session_info.session_name,
+			sizeof(session_info.session_name));
+	strncpy(hostname, session_info.hostname,
+			sizeof(session_info.hostname));
+	*live_timer = be32toh(session_info.live_timer);
+	*snapshot = be32toh(session_info.snapshot);
 
 	ret = 0;
 
src/bin/lttng-relayd/cmd-2-4.h +6 -4
index aaf572a..ab73478 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_2_4_H
+#define RELAYD_CMD_2_4_H
+
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -16,12 +20,10 @@
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef RELAYD_CMD_2_4_H
-#define RELAYD_CMD_2_4_H
-
 #include "lttng-relayd.h"
 
 int cmd_create_session_2_4(struct relay_connection *conn,
-		struct relay_session *session);
+		char *session_name, char *hostname,
+		uint32_t *live_timer, bool *snapshot);
 
 #endif /* RELAYD_CMD_2_4_H */
src/bin/lttng-relayd/cmd-generic.c +1 -0
index 417d6d3..276e85b 100644
@@ -1,6 +1,7 @@
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
src/bin/lttng-relayd/cmd-generic.h +4 -3
index 640fed7..4551f0a 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_GENERIC_H
+#define RELAYD_CMD_GENERIC_H
+
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -16,9 +20,6 @@
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef RELAYD_CMD_GENERIC_H
-#define RELAYD_CMD_GENERIC_H
-
 #include <common/sessiond-comm/sessiond-comm.h>
 
 #include "connection.h"
src/bin/lttng-relayd/cmd.h +4 -3
index c8b37d5..88db09a 100644
@@ -1,6 +1,10 @@
+#ifndef RELAYD_CMD_H
+#define RELAYD_CMD_H
+
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -16,9 +20,6 @@
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef RELAYD_CMD_H
-#define RELAYD_CMD_H
-
 #include "cmd-generic.h"
 #include "cmd-2-1.h"
 #include "cmd-2-2.h"
src/bin/lttng-relayd/connection.c +21 -13
index 76e48a6..b425548 100644
@@ -1,6 +1,7 @@
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -19,91 +20,132 @@
 #define _GNU_SOURCE
 #define _LGPL_SOURCE
 #include <common/common.h>
+#include <urcu/rculist.h>
 
 #include "connection.h"
 #include "stream.h"
+#include "viewer-session.h"
 
-static void rcu_free_connection(struct rcu_head *head)
+bool connection_get(struct relay_connection *conn)
 {
-	struct relay_connection *conn =
-		caa_container_of(head, struct relay_connection, rcu_node);
+	bool has_ref = false;
 
-	lttcomm_destroy_sock(conn->sock);
-	connection_free(conn);
+	pthread_mutex_lock(&conn->reflock);
+	if (conn->ref.refcount != 0) {
+		has_ref = true;
+		urcu_ref_get(&conn->ref);
+	}
+	pthread_mutex_unlock(&conn->reflock);
+
+	return has_ref;
 }
 
-/*
- * Must be called with a read side lock held. The read side lock must be
- * kept until the returned relay_connection is no longer in use.
- */
-struct relay_connection *connection_find_by_sock(struct lttng_ht *ht, int sock)
+struct relay_connection *connection_get_by_sock(struct lttng_ht
 *relay_connections_ht,
+		int sock)
 {
 	struct lttng_ht_node_ulong *node;
 	struct lttng_ht_iter iter;
 	struct relay_connection *conn = NULL;
 
-	assert(ht);
 	assert(sock >= 0);
 
-	lttng_ht_lookup(ht, (void *)((unsigned long) sock), &iter);
+	rcu_read_lock();
+	lttng_ht_lookup(relay_connections_ht, (void *)((unsigned long) sock),
+			&iter);
 	node = lttng_ht_iter_get_node_ulong(&iter);
 	if (!node) {
 		DBG2("Relay connection by sock %d not found", sock);
 		goto end;
 	}
 	conn = caa_container_of(node, struct relay_connection, sock_n);
-
+	if (!connection_get(conn)) {
+		conn = NULL;
+	}

end:

src/bin/lttng-relayd/connection.c +1 -0
+	rcu_read_unlock();
	return conn;
}
src/bin/lttng-relayd/connection.c +2 -1
-void connection_delete(struct lttng_ht *ht, struct relay_connection *conn)
+struct relay_connection *connection_create(struct lttcomm_sock *sock,
+		enum connection_type type)

{

src/bin/lttng-relayd/connection.c +14 -8
-	int ret;
-	struct lttng_ht_iter iter;
-
-	assert(ht);
-	assert(conn);
+	struct relay_connection *conn;
 
-	iter.iter.node = &conn->sock_n.node;
-	ret = lttng_ht_del(ht, &iter);
-	assert(!ret);
+	conn = zmalloc(sizeof(*conn));
+	if (!conn) {
+		PERROR("zmalloc relay connection");
+		goto end;
+	}
+	pthread_mutex_init(&conn->lock, NULL);
+	pthread_mutex_init(&conn->reflock, NULL);
+	urcu_ref_init(&conn->ref);
+	conn->type = type;
+	conn->sock = sock;
+	lttng_ht_node_init_ulong(&conn->sock_n, (unsigned long) conn->sock->fd);
+end:
+	return conn;

}

src/bin/lttng-relayd/connection.c +1 -1
-void connection_destroy(struct relay_connection *conn)
+static void rcu_free_connection(struct rcu_head *head)

{

src/bin/lttng-relayd/connection.c +12 -1
-	assert(conn);
+	struct relay_connection *conn =
+		caa_container_of(head, struct relay_connection, rcu_node);
 
+	lttcomm_destroy_sock(conn->sock);
+	if (conn->viewer_session) {
+		viewer_session_destroy(conn->viewer_session);
+		conn->viewer_session = NULL;
+	}
+	free(conn);
+}
+
+static void destroy_connection(struct relay_connection *conn)
+{
	call_rcu(&conn->rcu_node, rcu_free_connection);
}
src/bin/lttng-relayd/connection.c +1 -1
-struct relay_connection *connection_create(void)
+static void connection_release(struct urcu_ref *ref)

{

src/bin/lttng-relayd/connection.c +9 -5
-	struct relay_connection *conn;
+	struct relay_connection *conn =
+		caa_container_of(ref, struct relay_connection, ref);
 
-	conn = zmalloc(sizeof(*conn));
-	if (!conn) {
-		PERROR("zmalloc relay connection");
-		goto error;
+	if (conn->in_socket_ht) {
+		struct lttng_ht_iter iter;
+		int ret;
+
+		iter.iter.node = &conn->sock_n.node;
+		ret = lttng_ht_del(conn->socket_ht, &iter);
+		assert(!ret);
	}
src/bin/lttng-relayd/connection.c +10 -2
-error:
-	return conn;
+	if (conn->session) {
+		if (session_close(conn->session)) {
+			ERR("session_close");
+		}
+		conn->session = NULL;
+	}
+	if (conn->viewer_session) {
+		viewer_session_close(conn->viewer_session);
+	}
+	destroy_connection(conn);

}

src/bin/lttng-relayd/connection.c +1 -1
-void connection_init(struct relay_connection *conn)
+void connection_put(struct relay_connection *conn)

{

src/bin/lttng-relayd/connection.c +5 -5
-	assert(conn);
-	assert(conn->sock);
-
-	CDS_INIT_LIST_HEAD(&conn->recv_head);
-	lttng_ht_node_init_ulong(&conn->sock_n, (unsigned long) conn->sock->fd);
+	rcu_read_lock();
+	pthread_mutex_lock(&conn->reflock);
+	urcu_ref_put(&conn->ref, connection_release);
+	pthread_mutex_unlock(&conn->reflock);
+	rcu_read_unlock();

}

src/bin/lttng-relayd/connection.c +2 -1
-void connection_free(struct relay_connection *conn)
+void connection_ht_add(struct lttng_ht *relay_connections_ht,
+		struct relay_connection *conn)

{

src/bin/lttng-relayd/connection.c +6 -4
-	assert(conn);
-
-	free(conn->viewer_session);
-	free(conn);
+	pthread_mutex_lock(&conn->lock);
+	assert(!conn->in_socket_ht);
+	lttng_ht_add_unique_ulong(relay_connections_ht, &conn->sock_n);
+	conn->in_socket_ht = 1;
+	conn->socket_ht = relay_connections_ht;
+	pthread_mutex_unlock(&conn->lock);

}

src/bin/lttng-relayd/connection.h +39 -24
index 70fe4ba..1fff515 100644
@@ -1,6 +1,10 @@
+#ifndef _CONNECTION_H
+#define _CONNECTION_H
+
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -16,9 +20,6 @@
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef _CONNECTION_H
-#define _CONNECTION_H
-
 #include <limits.h>
 #include <inttypes.h>
 #include <pthread.h>
@@ -32,6 +33,7 @@
 #include "session.h"
 
 enum connection_type {
+	RELAY_CONNECTION_UNKNOWN    = 0,
 	RELAY_DATA                  = 1,
 	RELAY_CONTROL               = 2,
 	RELAY_VIEWER_COMMAND        = 3,
@@ -44,36 +46,49 @@ enum connection_type {
 */
 struct relay_connection {
 	struct lttcomm_sock *sock;
-	struct relay_session *session;
-	struct relay_viewer_session *viewer_session;
 	struct cds_wfcq_node qnode;
-	struct lttng_ht_node_ulong sock_n;
-	struct rcu_head rcu_node;
+
 	enum connection_type type;
-	/* Protocol version to use for this connection. */
+	/*
+	 * session is only ever set for RELAY_CONTROL connection type.
+	 */
+	struct relay_session *session;
+	/*
+	 * viewer_session is only ever set for RELAY_VIEWER_COMMAND
+	 * connection type.
+	 */
+	struct relay_viewer_session *viewer_session;
+
+	/*
+	 * Protocol version to use for this connection. Only valid for
+	 * RELAY_CONTROL connection type.
+	 */
 	uint32_t major;
 	uint32_t minor;
-	uint64_t session_id;
+
+	struct urcu_ref ref;
+	pthread_mutex_t reflock;
+
+	pthread_mutex_t lock;
+
+	bool version_check_done;
 
 	/*
-	 * This contains streams that are received on that connection. It's used to
-	 * store them until we get the streams sent command where they are removed
-	 * and flagged ready for the viewer. This is ONLY used by the control
-	 * thread thus any action on it should happen in that thread.
+	 * Node member of connection within global socket hash table.
 	 */
-	struct cds_list_head recv_head;
-	unsigned int version_check_done:1;
-
-	/* Pointer to the sessions HT that this connection can use. */
-	struct lttng_ht *sessions_ht;
+	struct lttng_ht_node_ulong sock_n;
+	bool in_socket_ht;
+	struct lttng_ht *socket_ht;	/* HACK: Contained within this hash table. */
+	struct rcu_head rcu_node;	/* For call_rcu teardown. */
 };
 
-struct relay_connection *connection_find_by_sock(struct lttng_ht *ht,
+struct relay_connection *connection_create(struct lttcomm_sock *sock,
+		enum connection_type type);
+struct relay_connection *connection_get_by_sock(struct lttng_ht
 *relay_connections_ht,
 		int sock);
-struct relay_connection *connection_create(void);
-void connection_init(struct relay_connection *conn);
-void connection_delete(struct lttng_ht *ht, struct relay_connection *conn);
-void connection_destroy(struct relay_connection *conn);
-void connection_free(struct relay_connection *conn);
+bool connection_get(struct relay_connection *connection);
+void connection_put(struct relay_connection *connection);
+void connection_ht_add(struct lttng_ht *relay_connections_ht,
+		struct relay_connection *conn);
 

#endif /* _CONNECTION_H */

src/bin/lttng-relayd/ctf-trace.c +68 -49
index 02a8b2b..20e1543 100644
@@ -1,6 +1,7 @@
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -22,148 +23,191 @@
 
 #include <common/common.h>
 #include <common/utils.h>
+#include <urcu/rculist.h>
 
 #include "ctf-trace.h"
 #include "lttng-relayd.h"
 #include "stream.h"
 
 static uint64_t last_relay_ctf_trace_id;
+static pthread_mutex_t last_relay_ctf_trace_id_lock =
 PTHREAD_MUTEX_INITIALIZER;
 
-static void rcu_destroy_ctf_trace(struct rcu_head *head)
+static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
 {
-	struct lttng_ht_node_str *node =
-		caa_container_of(head, struct lttng_ht_node_str, head);
-	struct ctf_trace *trace=
-		caa_container_of(node, struct ctf_trace, node);
+	struct ctf_trace *trace =
+		caa_container_of(rcu_head, struct ctf_trace, rcu_node);
 
 	free(trace);
 }
 
-static void rcu_destroy_stream(struct rcu_head *head)
-{
-	struct relay_stream *stream =
-		caa_container_of(head, struct relay_stream, rcu_node);
-
-	stream_destroy(stream);
-}
-
 /*
 * Destroy a ctf trace and all stream contained in it.
 *
 * MUST be called with the RCU read side lock.
 */
-void ctf_trace_destroy(struct ctf_trace *obj)
+void ctf_trace_destroy(struct ctf_trace *trace)
 {
-	struct relay_stream *stream, *tmp_stream;
-
-	assert(obj);
 	/*
-	 * Getting to this point, every stream referenced to that object have put
-	 * back their ref since the've been closed by the control side.
+	 * Getting to this point, every stream referenced by that traceect
+	 * have put back their ref since the've been closed by the
+	 * control side.
 	 */
-	assert(!obj->refcount);
+	assert(cds_list_empty(&trace->stream_list));
+	session_put(trace->session);
+	trace->session = NULL;
+	call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace);
+}
 
-	cds_list_for_each_entry_safe(stream, tmp_stream, &obj->stream_list,
-			trace_list) {
-		stream_delete(relay_streams_ht, stream);
-		call_rcu(&stream->rcu_node, rcu_destroy_stream);
-	}
+void ctf_trace_release(struct urcu_ref *ref)
+{
+	struct ctf_trace *trace =
+		caa_container_of(ref, struct ctf_trace, ref);
+	int ret;
+	struct lttng_ht_iter iter;
 
-	call_rcu(&obj->node.head, rcu_destroy_ctf_trace);
+	iter.iter.node = &trace->node.node;
+	ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter);
+	assert(!ret);
+	ctf_trace_destroy(trace);
 }
 
-void ctf_trace_try_destroy(struct relay_session *session,
-		struct ctf_trace *ctf_trace)
+/*
+ * Should be called with RCU read-side lock held.
+ */
+bool ctf_trace_get(struct ctf_trace *trace)
 {
-	assert(session);
-	assert(ctf_trace);
+	bool has_ref = false;
 
-	/*
-	 * Considering no viewer attach to the session and the trace having no more
-	 * stream attached, wipe the trace.
-	 */
-	if (uatomic_read(&session->viewer_refcount) == 0 &&
-			uatomic_read(&ctf_trace->refcount) == 0) {
-		ctf_trace_delete(session->ctf_traces_ht, ctf_trace);
-		ctf_trace_destroy(ctf_trace);
+	/* Confirm that the trace refcount has not reached 0. */
+	pthread_mutex_lock(&trace->reflock);
+	if (trace->ref.refcount != 0) {
+		has_ref = true;
+		urcu_ref_get(&trace->ref);
 	}
+	pthread_mutex_unlock(&trace->reflock);
+
+	return has_ref;
 }
 
 /*
- * Create and return an allocated ctf_trace object. NULL on error.
+ * Create and return an allocated ctf_trace. NULL on error.
+ * There is no "open" and "close" for a ctf_trace, but rather just a
+ * create and refcounting. Whenever all the streams belonging to a trace
+ * put their reference, its refcount drops to 0.
 */
-struct ctf_trace *ctf_trace_create(char *path_name)
+static struct ctf_trace *ctf_trace_create(struct relay_session *session,
+		char *path_name)
 {
-	struct ctf_trace *obj;
+	struct ctf_trace *trace;
 
-	assert(path_name);
-
-	obj = zmalloc(sizeof(*obj));
-	if (!obj) {
+	trace = zmalloc(sizeof(*trace));
+	if (!trace) {
 		PERROR("ctf_trace alloc");
 		goto error;
 	}
 
-	CDS_INIT_LIST_HEAD(&obj->stream_list);
+	if (!session_get(session)) {
+		ERR("Cannot get session");
+		free(trace);
+		trace = NULL;
+		goto error;
+	}
+	trace->session = session;
+
+	CDS_INIT_LIST_HEAD(&trace->stream_list);
+
+	pthread_mutex_lock(&last_relay_ctf_trace_id_lock);
+	trace->id = ++last_relay_ctf_trace_id;
+	pthread_mutex_unlock(&last_relay_ctf_trace_id_lock);
 
-	obj->id = ++last_relay_ctf_trace_id;
-	lttng_ht_node_init_str(&obj->node, path_name);
+	lttng_ht_node_init_str(&trace->node, path_name);
+	trace->session = session;
+	urcu_ref_init(&trace->ref);
+	pthread_mutex_init(&trace->lock, NULL);
+	pthread_mutex_init(&trace->reflock, NULL);
+	pthread_mutex_init(&trace->stream_list_lock, NULL);
+	lttng_ht_add_str(session->ctf_traces_ht, &trace->node);
 
-	DBG("Created ctf_trace %" PRIu64 " with path: %s", obj->id, path_name);
+	DBG("Created ctf_trace %" PRIu64 " with path: %s", trace->id, path_name);
 

error:

src/bin/lttng-relayd/ctf-trace.c +1 -1
-	return obj;
+	return trace;

}

/*
- * Return a ctf_trace object if found by id in the given hash table else NULL.
- *
- * Must be called with rcu_read_lock() taken.
+ * Return a ctf_trace if found by id in the given hash table else NULL.
+ * Hold a reference on the ctf_trace, and must be paired with
+ * ctf_trace_put().
*/

src/bin/lttng-relayd/ctf-trace.c +1 -1
-struct ctf_trace *ctf_trace_find_by_path(struct lttng_ht *ht,
+struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session

*session,
char *path_name)

{
	struct lttng_ht_node_str *node;
	struct lttng_ht_iter iter;
	struct ctf_trace *trace = NULL;
src/bin/lttng-relayd/ctf-trace.c +2 -3
-	assert(ht);
-
-	lttng_ht_lookup(ht, (void *) path_name, &iter);
+	rcu_read_lock();
+	lttng_ht_lookup(session->ctf_traces_ht, (void *) path_name, &iter);
	node = lttng_ht_iter_get_node_str(&iter);
	if (!node) {
		DBG("CTF Trace path %s not found", path_name);
		goto end;
	}
	trace = caa_container_of(node, struct ctf_trace, node);
src/bin/lttng-relayd/ctf-trace.c +3 -1
-
+	if (!ctf_trace_get(trace)) {
+		trace = NULL;
+	}

end:

src/bin/lttng-relayd/ctf-trace.c +5 -0
+	rcu_read_unlock();
+	if (!trace) {
+		/* Try to create */
+		trace = ctf_trace_create(session, path_name);
+	}
	return trace;
}
src/bin/lttng-relayd/ctf-trace.c +1 -4
-/*
- * Add stream to a given hash table.
- */
-void ctf_trace_add(struct lttng_ht *ht, struct ctf_trace *trace)
+void ctf_trace_put(struct ctf_trace *trace)

{

src/bin/lttng-relayd/ctf-trace.c +5 -4
-	assert(ht);
-	assert(trace);
-
-	lttng_ht_add_str(ht, &trace->node);
+	rcu_read_lock();
+	pthread_mutex_lock(&trace->reflock);
+	urcu_ref_put(&trace->ref, ctf_trace_release);
+	pthread_mutex_unlock(&trace->reflock);
+	rcu_read_unlock();

}

src/bin/lttng-relayd/ctf-trace.c +1 -4
-/*
- * Delete stream from a given hash table.
- */
-void ctf_trace_delete(struct lttng_ht *ht, struct ctf_trace *trace)
+int ctf_trace_close(struct ctf_trace *trace)

{

src/bin/lttng-relayd/ctf-trace.c +18 -4
-	int ret;
-	struct lttng_ht_iter iter;
+	struct relay_stream *stream;
+
+	rcu_read_lock();
+	cds_list_for_each_entry_rcu(stream, &trace->stream_list,
+			stream_node) {
+		/*
+		 * Close the stream.
+		 */
+		stream_close(stream);
+	}
+	rcu_read_unlock();
+	/*
+	 * Since all references to the trace are held by its streams, we
+	 * don't need to do any self-ref put.
+	 */
+	return 0;
+}
 
-	assert(ht);
-	assert(trace);
+struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct

ctf_trace *trace)

src/bin/lttng-relayd/ctf-trace.c +13 -3
+{
+	struct relay_viewer_stream *vstream;
 
-	iter.iter.node = &trace->node.node;
-	ret = lttng_ht_del(ht, &iter);
-	assert(!ret);
+	rcu_read_lock();
+	vstream = rcu_dereference(trace->viewer_metadata_stream);
+	if (!vstream) {
+		goto end;
+	}
+	if (!viewer_stream_get(vstream)) {
+		vstream = NULL;
+	}
+end:
+	rcu_read_unlock();
+	return vstream;

}

src/bin/lttng-relayd/ctf-trace.h +37 -29
index 489c5f1..d051f80 100644
@@ -1,6 +1,10 @@
+#ifndef _CTF_TRACE_H
+#define _CTF_TRACE_H
+
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -16,49 +20,53 @@
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef _CTF_TRACE_H
-#define _CTF_TRACE_H
-
 #include <inttypes.h>
+#include <urcu/ref.h>
 
 #include <common/hashtable/hashtable.h>
 
 #include "session.h"
 #include "stream.h"
+#include "viewer-stream.h"
 
 struct ctf_trace {
-	int refcount;
-	unsigned int invalid_flag:1;
+	/*
+	 * The ctf_trace reflock nests inside the stream reflock.
+	 */
+	pthread_mutex_t reflock;	/* Protects refcounting */
+	struct urcu_ref ref;		/* Every stream has a ref on the trace. */
+	struct relay_session *session;	/* Back ref to trace session */
+
+	/*
+	 * The ctf_trace lock nests inside the session lock.
+	 */
+	pthread_mutex_t lock;
 	uint64_t id;
-	uint64_t metadata_received;
-	uint64_t metadata_sent;
-	struct relay_stream *metadata_stream;
-	struct relay_viewer_stream *viewer_metadata_stream;
-	/* Node indexed by stream path name in the corresponding session. */
-	struct lttng_ht_node_str node;
+	struct relay_viewer_stream *viewer_metadata_stream;	/* RCU protected */
 
-	/* Relay stream associated with this ctf trace. */
+	/*
+	 * Relay streams associated with this ctf trace.
+	 * Updates are protected by the stream_list lock.
+	 * Traversals are protected by RCU.
+	 */
 	struct cds_list_head stream_list;
+	pthread_mutex_t stream_list_lock;
+
+	/*
+	 * Node within session trace hash table. Node is indexed by
+	 * stream path name.
+	 */
+	struct lttng_ht_node_str node;
+	struct rcu_head rcu_node;	/* For call_rcu teardown. */
 };
 
-static inline void ctf_trace_get_ref(struct ctf_trace *trace)
-{
-	uatomic_inc(&trace->refcount);
-}
+struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session
 *session,
+		char *path_name);
+bool ctf_trace_get(struct ctf_trace *trace);
+void ctf_trace_put(struct ctf_trace *trace);
 
-static inline void ctf_trace_put_ref(struct ctf_trace *trace)
-{
-	uatomic_add(&trace->refcount, -1);
-}
+int ctf_trace_close(struct ctf_trace *trace);
 
-void ctf_trace_assign(struct relay_stream *stream);
-struct ctf_trace *ctf_trace_create(char *path_name);
-void ctf_trace_destroy(struct ctf_trace *obj);
-void ctf_trace_try_destroy(struct relay_session *session,
-		struct ctf_trace *ctf_trace);
-struct ctf_trace *ctf_trace_find_by_path(struct lttng_ht *ht,
-		char *path_name);
-void ctf_trace_add(struct lttng_ht *ht, struct ctf_trace *trace);
-void ctf_trace_delete(struct lttng_ht *ht, struct ctf_trace *trace);
+struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct
 ctf_trace *trace);

#endif /* _CTF_TRACE_H */

src/bin/lttng-relayd/index.c +103 -41
index b7507a0..c092b7f 100644
@@ -1,6 +1,7 @@
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -24,190 +25,307 @@
 #include <common/utils.h>
 
 #include "lttng-relayd.h"
+#include "stream.h"
 #include "index.h"
 
 /*
- * Deferred free of a relay index object. MUST only be called by a call RCU.
+ * Allocate a new relay index object. Pass the stream in which it is
+ * contained as parameter. The sequence number will be used as the hash
+ * table key.
+ *
+ * Called with stream mutex held.
+ * Return allocated object or else NULL on error.
 */
-static void deferred_free_relay_index(struct rcu_head *head)
+static struct relay_index *relay_index_create(struct relay_stream *stream,
+		uint64_t net_seq_num)
 {
-	struct relay_index *index =
-		caa_container_of(head, struct relay_index, rcu_node);
+	struct relay_index *index;
 
-	if (index->to_close_fd >= 0) {
-		int ret;
+	DBG2("Creating relay index for stream id %" PRIu64 " and seqnum %" PRIu64,
+			stream->stream_handle, net_seq_num);
 
-		ret = close(index->to_close_fd);
-		if (ret < 0) {
-			PERROR("Relay index to close fd %d", index->to_close_fd);
-		}
+	index = zmalloc(sizeof(*index));
+	if (!index) {
+		PERROR("Relay index zmalloc");
+		goto end;
+	}
+	if (!stream_get(stream)) {
+		ERR("Cannot get stream");
+		free(index);
+		index = NULL;
+		goto end;
 	}
+	index->stream = stream;
 
-	relay_index_free(index);
+	lttng_ht_node_init_u64(&index->index_n, net_seq_num);
+	pthread_mutex_init(&index->lock, NULL);
+	pthread_mutex_init(&index->reflock, NULL);
+	urcu_ref_init(&index->ref);
+
+end:
+	return index;
 }
 
 /*
- * Allocate a new relay index object using the given stream ID and sequence
- * number as the hash table key.
+ * Add unique relay index to the given hash table. In case of a collision, the
+ * already existing object is put in the given _index variable.
 *
- * Return allocated object or else NULL on error.
+ * RCU read side lock MUST be acquired.
 */
-struct relay_index *relay_index_create(uint64_t stream_id,
-		uint64_t net_seq_num)
+static struct relay_index *relay_index_add_unique(struct relay_stream *stream,
+		struct relay_index *index)
 {
-	struct relay_index *index;
+	struct cds_lfht_node *node_ptr;
+	struct relay_index *_index;
 
-	DBG2("Creating relay index with stream id %" PRIu64 " and seqnum %" PRIu64,
-			stream_id, net_seq_num);
+	DBG2("Adding relay index with stream id %" PRIu64 " and seqnum %" PRIu64,
+			stream->stream_handle, index->index_n.key);
 
-	index = zmalloc(sizeof(*index));
-	if (index == NULL) {
-		PERROR("Relay index zmalloc");
-		goto error;
+	node_ptr = cds_lfht_add_unique(stream->indexes_ht->ht,
+			stream->indexes_ht->hash_fct(&index->index_n, lttng_ht_seed),
+			stream->indexes_ht->match_fct, &index->index_n,
+			&index->index_n.node);
+	if (node_ptr != &index->index_n.node) {
+		_index = caa_container_of(node_ptr, struct relay_index,
+				index_n.node);
+	} else {
+		_index = NULL;
 	}
+	return _index;
+}
+
+/*
+ * Should be called with RCU read-side lock held.
+ */
+static bool relay_index_get(struct relay_index *index)
+{
+	bool has_ref = false;
 
-	index->to_close_fd = -1;
-	lttng_ht_node_init_two_u64(&index->index_n, stream_id, net_seq_num);
+	DBG2("index get for stream id %" PRIu64 " and seqnum %" PRIu64 " refcount %d",
+			index->stream->stream_handle, index->index_n.key,
+			(int) index->ref.refcount);
 
-error:
-	return index;
+	/* Confirm that the index refcount has not reached 0. */
+	pthread_mutex_lock(&index->reflock);
+	if (index->ref.refcount != 0) {
+		has_ref = true;
+		urcu_ref_get(&index->ref);
+	}
+	pthread_mutex_unlock(&index->reflock);
+
+	return has_ref;
 }
 
 /*
- * Find a relayd index in the given hash table.
+ * Get a relayd index in within the given stream, or create it if not
+ * present.
 *
+ * Called with stream mutex held.
 * Return index object or else NULL on error.
 */
-struct relay_index *relay_index_find(uint64_t stream_id, uint64_t net_seq_num)
+struct relay_index *relay_index_get_by_id_or_create(struct relay_stream
 *stream,
+		 uint64_t net_seq_num)
 {
-	struct lttng_ht_node_two_u64 *node;
+	struct lttng_ht_node_u64 *node;
 	struct lttng_ht_iter iter;
-	struct lttng_ht_two_u64 key;
 	struct relay_index *index = NULL;
 
 	DBG3("Finding index for stream id %" PRIu64 " and seq_num %" PRIu64,
-			stream_id, net_seq_num);
-
-	key.key1 = stream_id;
-	key.key2 = net_seq_num;
+			stream->stream_handle, net_seq_num);
 
-	lttng_ht_lookup(indexes_ht, (void *)(&key), &iter);
-	node = lttng_ht_iter_get_node_two_u64(&iter);
-	if (node == NULL) {
-		goto end;
+	rcu_read_lock();
+	lttng_ht_lookup(stream->indexes_ht, &net_seq_num, &iter);
+	node = lttng_ht_iter_get_node_u64(&iter);
+	if (node) {
+		index = caa_container_of(node, struct relay_index, index_n);
+	} else {
+		struct relay_index *oldindex;
+
+		index = relay_index_create(stream, net_seq_num);
+		if (!index) {
+			ERR("Cannot create index for stream id %" PRIu64 " and seq_num %" PRIu64,
+				index->stream->stream_handle, net_seq_num);
+			goto end;
+		}
+		oldindex = relay_index_add_unique(stream, index);
+		if (oldindex) {
+			/* Added concurrently, keep old. */
+			relay_index_put(index);
+			index = oldindex;
+			if (!relay_index_get(index)) {
+				index = NULL;
+			}
+		} else {
+			stream->indexes_in_flight++;
+			index->in_hash_table = true;
+		}
 	}
-	index = caa_container_of(node, struct relay_index, index_n);
-

end:

src/bin/lttng-relayd/index.c +2 -2
-	DBG2("Index %sfound in HT for stream ID %" PRIu64 " and seqnum %" PRIu64,
-			(index == NULL) ? "NOT " : "", stream_id, net_seq_num);
+	rcu_read_unlock();
+	DBG2("Index %sfound or created in HT for stream ID %" PRIu64 " and seqnum %"

PRIu64,

src/bin/lttng-relayd/index.c +1 -0
+			(index == NULL) ? "NOT " : "", index->stream->stream_handle, net_seq_num);
	return index;
}
src/bin/lttng-relayd/index.c +2 -7
-/*
- * Add unique relay index to the given hash table. In case of a collision, the
- * already existing object is put in the given _index variable.
- *
- * RCU read side lock MUST be acquired.
- */
-void relay_index_add(struct relay_index *index, struct relay_index **_index)
+int relay_index_set_fd(struct relay_index *index, struct stream_fd *index_fd,
+		uint64_t data_offset)

{

src/bin/lttng-relayd/index.c +21 -10
-	struct cds_lfht_node *node_ptr;
+	int ret = 0;
 
-	assert(index);
+	pthread_mutex_lock(&index->lock);
+	if (index->index_fd) {
+		ret = -1;
+		goto end;
+	}
+	stream_fd_get(index_fd);
+	index->index_fd = index_fd;
+	index->index_data.offset = data_offset;
+end:
+	pthread_mutex_unlock(&index->lock);
+	return ret;
+}
 
-	DBG2("Adding relay index with stream id %" PRIu64 " and seqnum %" PRIu64,
-			index->index_n.key.key1, index->index_n.key.key2);
+int relay_index_set_data(struct relay_index *index,
+                const struct ctf_packet_index *data)
+{
+	int ret = 0;
 
-	node_ptr = cds_lfht_add_unique(indexes_ht->ht,
-			indexes_ht->hash_fct((void *) &index->index_n.key, lttng_ht_seed),
-			indexes_ht->match_fct, (void *) &index->index_n.key,
-			&index->index_n.node);
-	if (node_ptr != &index->index_n.node) {
-		*_index = caa_container_of(node_ptr, struct relay_index, index_n.node);
+	pthread_mutex_lock(&index->lock);
+	if (index->has_index_data) {
+		ret = -1;
+		goto end;
	}
src/bin/lttng-relayd/index.c +11 -0
+	/* Set everything except data_offset. */
+	index->index_data.packet_size = data->packet_size;
+	index->index_data.content_size = data->content_size;
+	index->index_data.timestamp_begin = data->timestamp_begin;
+	index->index_data.timestamp_end = data->timestamp_end;
+	index->index_data.events_discarded = data->events_discarded;
+	index->index_data.stream_id = data->stream_id;
+	index->has_index_data = true;
+end:
+	pthread_mutex_unlock(&index->lock);
+	return ret;

}

src/bin/lttng-relayd/index.c +1 -9
-/*
- * Write index on disk to the given fd. Once done error or not, it is removed
- * from the hash table and destroy the object.
- *
- * MUST be called with a RCU read side lock held.
- *
- * Return 0 on success else a negative value.
- */
-int relay_index_write(int fd, struct relay_index *index)
+static void index_destroy(struct relay_index *index)

{

src/bin/lttng-relayd/index.c +7 -12
-	int ret;
-	struct lttng_ht_iter iter;
-
-	DBG2("Writing index for stream ID %" PRIu64 " and seq num %" PRIu64
-			" on fd %d", index->index_n.key.key1,
-			index->index_n.key.key2, fd);
+	free(index);
+}
 
-	/* Delete index from hash table. */
-	iter.iter.node = &index->index_n.node;
-	ret = lttng_ht_del(indexes_ht, &iter);
-	assert(!ret);
-	call_rcu(&index->rcu_node, deferred_free_relay_index);
+static void index_destroy_rcu(struct rcu_head *rcu_head)
+{
+	struct relay_index *index =
+		caa_container_of(rcu_head, struct relay_index, rcu_node);
 
-	return index_write(fd, &index->index_data, sizeof(index->index_data));
+	index_destroy(index);

}

src/bin/lttng-relayd/index.c +1 -4
-/*
- * Free the given index.
- */
-void relay_index_free(struct relay_index *index)
+static void index_release(struct urcu_ref *ref)

{

src/bin/lttng-relayd/index.c +21 -1
-	free(index);
+	struct relay_index *index = caa_container_of(ref, struct relay_index, ref);
+	struct relay_stream *stream = index->stream;
+	int ret;
+	struct lttng_ht_iter iter;
+
+	if (index->index_fd) {
+		stream_fd_put(index->index_fd);
+		index->index_fd = NULL;
+	}
+	if (index->in_hash_table) {
+		/* Delete index from hash table. */
+		iter.iter.node = &index->index_n.node;
+		ret = lttng_ht_del(stream->indexes_ht, &iter);
+		assert(!ret);
+		stream->indexes_in_flight--;
+	}
+
+	stream_put(index->stream);
+	index->stream = NULL;
+
+	call_rcu(&index->rcu_node, index_destroy_rcu);

}

/*
- * Safely free the given index using a call RCU.
+ * Called with stream mutex held.
*/

src/bin/lttng-relayd/index.c +1 -1
-void relay_index_free_safe(struct relay_index *index)
+void relay_index_put(struct relay_index *index)

{

src/bin/lttng-relayd/index.c +16 -5
-	if (!index) {
-		return;
-	}
-
-	call_rcu(&index->rcu_node, deferred_free_relay_index);
+	DBG2("index put for stream id %" PRIu64 " and seqnum %" PRIu64 " refcount %d",
+			index->stream->stream_handle, index->index_n.key,
+			(int) index->ref.refcount);
+	/*
+	 * Ensure existance of index->lock for index unlock.
+	 */
+	rcu_read_lock();
+	/*
+	 * Index lock ensures that concurrent test and update of stream
+	 * ref is atomic.
+	 */
+	pthread_mutex_lock(&index->reflock);
+	assert(index->ref.refcount != 0);
+	urcu_ref_put(&index->ref, index_release);
+	pthread_mutex_unlock(&index->reflock);
+	rcu_read_unlock();

}

/*
- * Delete index from the given hash table.
+ * Try to flush index to disk. Releases self-reference to index once
+ * flush succeeds.
*
- * RCU read side lock MUST be acquired.
+ * Return 0 on successful flush, a negative value on error, or positive
+ * value if no flush was performed.
*/

src/bin/lttng-relayd/index.c +1 -1
-void relay_index_delete(struct relay_index *index)
+int relay_index_try_flush(struct relay_index *index)

{

src/bin/lttng-relayd/index.c +30 -9
-	int ret;
-	struct lttng_ht_iter iter;
+	int ret = 1;
+	bool flushed = false;
+	int fd;
 
-	DBG3("Relay index with stream ID %" PRIu64 " and seq num %" PRIu64
-			" deleted.", index->index_n.key.key1,
-			index->index_n.key.key2);
+	pthread_mutex_lock(&index->lock);
+	if (index->flushed) {
+		goto skip;
+	}
+	/* Check if we are ready to flush. */
+	if (!index->has_index_data || !index->index_fd) {
+		goto skip;
+	}
+	fd = index->index_fd->fd;
+	DBG2("Writing index for stream ID %" PRIu64 " and seq num %" PRIu64
+			" on fd %d", index->stream->stream_handle,
+			index->index_n.key, fd);
+	flushed = true;
+	index->flushed = true;
+	ret = index_write(fd, &index->index_data, sizeof(index->index_data));
+	if (ret == sizeof(index->index_data)) {
+		ret = 0;
+	} else {
+		ret = -1;
+	}
+skip:
+	pthread_mutex_unlock(&index->lock);
 
-	/* Delete index from hash table. */
-	iter.iter.node = &index->index_n.node;
-	ret = lttng_ht_del(indexes_ht, &iter);
-	assert(!ret);
+	if (flushed) {
+		/* Put self-ref from index now that it has been flushed. */
+		relay_index_put(index);
+	}
+	return ret;

}

/*
- * Destroy every relay index with the given stream id as part of the key.
+ * Close every relay index within a given stream, without flushing
+ * them.
*/

src/bin/lttng-relayd/index.c +1 -1
-void relay_index_destroy_by_stream_id(uint64_t stream_id)
+void relay_index_close_all(struct relay_stream *stream)
{
	struct lttng_ht_iter iter;
	struct relay_index *index;

	rcu_read_lock();
src/bin/lttng-relayd/index.c +4 -5
-	cds_lfht_for_each_entry(indexes_ht->ht, &iter.iter, index, index_n.node) {
-		if (index->index_n.key.key1 == stream_id) {
-			relay_index_delete(index);
-			relay_index_free_safe(index);
-		}
+	cds_lfht_for_each_entry(stream->indexes_ht->ht, &iter.iter,
+			index, index_n.node) {
+		/* Put self-ref from index. */
+		relay_index_put(index);
	}
	rcu_read_unlock();
}
src/bin/lttng-relayd/index.h +39 -21
index e7f9cdb..e882ed9 100644
@@ -1,6 +1,10 @@
+#ifndef _RELAY_INDEX_H
+#define _RELAY_INDEX_H
+
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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
@@ -16,42 +20,56 @@
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef _RELAY_INDEX_H
-#define _RELAY_INDEX_H
-
 #include <inttypes.h>
 #include <pthread.h>
 
 #include <common/hashtable/hashtable.h>
 #include <common/index/index.h>
 
+#include "stream-fd.h"
+
+struct relay_stream;
+
 struct relay_index {
-	/* FD on which to write the index data. */
-	int fd;
 	/*
-	 * When destroying this object, this fd is checked and if valid, close it
-	 * so this is basically a lazy close of the previous fd corresponding to
-	 * the same stream id. This is used for the rotate file feature.
+	 * index lock nests inside stream lock.
 	 */
-	int to_close_fd;
+	pthread_mutex_t reflock;	/* Protects refcounting. */
+	struct urcu_ref ref;		/* Reference from getters. */
+	struct relay_stream *stream;	/* Back ref to stream */
+
+	pthread_mutex_t lock;
+	/*
+	 * FD on which to write the index data. May differ from
+	 * stream->index_fd due to tracefile rotation.
+	 */
+	struct stream_fd *index_fd;
 
 	/* Index packet data. This is the data that is written on disk. */
 	struct ctf_packet_index index_data;
 
-	/* key1 = stream_id, key2 = net_seq_num */
-	struct lttng_ht_node_two_u64 index_n;
-	struct rcu_head rcu_node;
-	pthread_mutex_t mutex;
+	bool has_index_data;
+	bool flushed;
+	bool in_hash_table;
+
+	/*
+	 * Node within indexes_ht that corresponds to this struct
+	 * relay_index. Indexed by net_seq_num, which is unique for this
+	 * index across the stream.
+	 */
+	struct lttng_ht_node_u64 index_n;
+	struct rcu_head rcu_node;	/* For call_rcu teardown. */
 };
 
-struct relay_index *relay_index_create(uint64_t stream_id,
+struct relay_index *relay_index_get_by_id_or_create(struct relay_stream
 *stream,
 		uint64_t net_seq_num);
-struct relay_index *relay_index_find(uint64_t stream_id, uint64_t net_seq_num);
-void relay_index_add(struct relay_index *index, struct relay_index **_index);
-int relay_index_write(int fd, struct relay_index *index);
-void relay_index_free(struct relay_index *index);
-void relay_index_free_safe(struct relay_index *index);
-void relay_index_delete(struct relay_index *index);
-void relay_index_destroy_by_stream_id(uint64_t stream_id);
+void relay_index_put(struct relay_index *index);
+int relay_index_set_fd(struct relay_index *index, struct stream_fd *index_fd,
+		 uint64_t data_offset);
+int relay_index_set_data(struct relay_index *index,
+                const struct ctf_packet_index *data);
+int relay_index_try_flush(struct relay_index *index);
+
+void relay_index_close_all(struct relay_stream *stream);
 

#endif /* _RELAY_INDEX_H */

src/bin/lttng-relayd/live.c +7 -3
index 562a7fa..64d37fa 100644
@@ -1,6 +1,7 @@
 /*
 * Copyright (C) 2013 - Julien Desfossez <[email protected]>
 *                      David Goulet <[email protected]>
+ *               2015 - Mathieu Desnoyers <[email protected]>
 *
 * 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,
@@ -36,6 +37,7 @@
 #include <inttypes.h>
 #include <urcu/futex.h>
 #include <urcu/uatomic.h>
+#include <urcu/rculist.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <config.h>
@@ -65,6 +67,9 @@
 #include "session.h"
 #include "ctf-trace.h"
 #include "connection.h"
+#include "viewer-session.h"
+
+#define SESSION_BUF_DEFAULT_COUNT	16
 
 static struct lttng_uri *live_uri;
 
@@ -90,6 +95,8 @@ static pthread_t live_worker_thread;
 static struct relay_conn_queue viewer_conn_queue;
 
 static uint64_t last_relay_viewer_session_id;
+static pthread_mutex_t last_relay_viewer_session_id_lock =
+		PTHREAD_MUTEX_INITIALIZER;
 
 /*
 * Cleanup the daemon
@@ -114,9 +121,6 @@ ssize_t recv_request(struct lttcomm_sock *sock, void *buf,
 size_t size)
 {
 	ssize_t ret;
 
-	assert(sock);
-	assert(buf);
-
 	ret = sock->ops->recvmsg(sock, buf, size, 0);
 	if (ret < 0 || ret != size) {
		if (ret == 0) {
src/bin/lttng-relayd/live.c +0 -3
@@ -143,9 +147,6 @@ ssize_t send_response(struct lttcomm_sock *sock, void *buf,
 size_t size)
 {
 	ssize_t ret;
 
-	assert(sock);
-	assert(buf);
-
 	ret = sock->ops->sendmsg(sock, buf, size, 0);
 	if (ret < 0) {
		ERR("Relayd failed to send response.");
src/bin/lttng-relayd/live.c +8 -4
@@ -171,17 +172,22 @@ int check_new_streams(struct relay_connection *conn)
 	if (!conn->viewer_session) {
 		goto end;
 	}
-	cds_list_for_each_entry(session,
-			&conn->viewer_session->sessions_head,
-			viewer_session_list) {
+	rcu_read_lock();
+	cds_list_for_each_entry_rcu(session,
+			&conn->viewer_session->session_list,
+			viewer_session_node) {
+		if (!session_get(session)) {
+			continue;
+		}
 		current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
 		ret = current_val;
+		session_put(session);
 		if (ret == 1) {
 			goto end;
 		}
 	}
-

end:

src/bin/lttng-relayd/live.c +1 -0
+	rcu_read_unlock();
	return ret;
}
src/bin/lttng-relayd/live.c +45 -30
@@ -200,8 +206,6 @@ ssize_t send_viewer_streams(struct lttcomm_sock *sock,
 	struct lttng_ht_iter iter;
 	struct relay_viewer_stream *vstream;
 
-	assert(session);
-
 	rcu_read_lock();
 
 	cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
@@ -210,30 +214,38 @@ ssize_t send_viewer_streams(struct lttcomm_sock *sock,
 
 		health_code_update();
 
+		if (!viewer_stream_get(vstream)) {
+			continue;
+		}
+
+		pthread_mutex_lock(&vstream->lock);
 		/* Ignore if not the same session. */
-		if (vstream->session_id != session->id ||
+		if (vstream->stream->trace->session->id != session->id ||
 				(!ignore_sent_flag && vstream->sent_flag)) {
+			pthread_mutex_unlock(&vstream->lock);
+			viewer_stream_put(vstream);
 			continue;
 		}
 
-		ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
-				vstream->path_name);
-		assert(ctf_trace);
-
-		send_stream.id = htobe64(vstream->stream_handle);
+		ctf_trace = vstream->stream->trace;
+		send_stream.id = htobe64(vstream->stream->stream_handle);
 		send_stream.ctf_trace_id = htobe64(ctf_trace->id);
-		send_stream.metadata_flag = htobe32(vstream->metadata_flag);
+		send_stream.metadata_flag = htobe32(vstream->stream->is_metadata);
 		strncpy(send_stream.path_name, vstream->path_name,
 				sizeof(send_stream.path_name));
 		strncpy(send_stream.channel_name, vstream->channel_name,
 				sizeof(send_stream.channel_name));
 
-		DBG("Sending stream %" PRIu64 " to viewer", vstream->stream_handle);
+		DBG("Sending stream %" PRIu64 " to viewer",
+				vstream->stream->stream_handle);
+		vstream->sent_flag = 1;
+		pthread_mutex_unlock(&vstream->lock);
+
 		ret = send_response(sock, &send_stream, sizeof(send_stream));
 		if (ret < 0) {
 			goto end_unlock;
 		}
-		vstream->sent_flag = 1;
+		viewer_stream_put(vstream);
 	}
 
 	ret = 0;
@@ -263,17 +275,14 @@ int make_viewer_streams(struct relay_session *session,
 	assert(session);
 
 	/*
-	 * This is to make sure we create viewer streams for a full received
-	 * channel. For instance, if we have 8 streams for a channel that are
-	 * concurrently being flagged ready, we can end up creating just a subset
-	 * of the 8 streams (the ones that are flagged). This lock avoids this
-	 * limbo state.
+	 * Hold the session lock to ensure that we see either none or
+	 * all initial streams for a session, but no intermediate state.
 	 */
-	pthread_mutex_lock(&session->viewer_ready_lock);
+	pthread_mutex_lock(&session->lock);
 
 	/*
-	 * Create viewer streams for relay streams that are ready to be used for a
-	 * the given session id only.
+	 * Create viewer streams for relay streams that are ready to be
+	 * used for a the given session id only.
 	 */
 	rcu_read_lock();
 	cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
@@ -282,47 +291,59 @@ int make_viewer_streams(struct relay_session *session,
 
 		health_code_update();
 
-		if (ctf_trace->invalid_flag) {
+		if (!ctf_trace_get(ctf_trace)) {
 			continue;
 		}
 
-		cds_list_for_each_entry(stream, &ctf_trace->stream_list, trace_list) {
+		cds_list_for_each_entry_rcu(stream, &ctf_trace->stream_list, stream_node) {
 			struct relay_viewer_stream *vstream;
 
-			if (!stream->viewer_ready) {
+			if (!stream_get(stream)) {
 				continue;
 			}
-
-			vstream = viewer_stream_find_by_id(stream->stream_handle);
+			/*
+			 * stream published is protected by the session
+			 * lock.
+			 */
+			if (!stream->published) {
+				goto next;
+			}
+			vstream = viewer_stream_get_by_id(stream->stream_handle);
 			if (!vstream) {
-				vstream = viewer_stream_create(stream, seek_t, ctf_trace);
+				vstream = viewer_stream_create(stream, seek_t);
 				if (!vstream) {
 					ret = -1;
+					ctf_trace_put(ctf_trace);
+					stream_put(stream);
 					goto error_unlock;
 				}
-				/* Acquire reference to ctf_trace. */
-				ctf_trace_get_ref(ctf_trace);
 
 				if (nb_created) {
 					/* Update number of created stream counter. */
 					(*nb_created)++;
 				}
-			} else if (!vstream->sent_flag && nb_unsent) {
-				/* Update number of unsent stream counter. */
-				(*nb_unsent)++;
+			} else {
+				if (!vstream->sent_flag && nb_unsent) {
+					/* Update number of unsent stream counter. */
+					(*nb_unsent)++;
+				}
+				viewer_stream_put(vstream);
 			}
 			/* Update number of total stream counter. */
 			if (nb_total) {
 				(*nb_total)++;
 			}
+		next:
+			stream_put(stream);
 		}
+		ctf_trace_put(ctf_trace);
 	}
 
 	ret = 0;
 

error_unlock:

	rcu_read_unlock();
src/bin/lttng-relayd/live.c +1 -1
-	pthread_mutex_unlock(&session->viewer_ready_lock);
+	pthread_mutex_unlock(&session->lock);
	return ret;
}
src/bin/lttng-relayd/live.c +76 -139
@@ -505,22 +526,17 @@ restart:
 				goto error;
 			} else if (revents & LPOLLIN) {
 				/*
-				 * Get allocated in this thread, enqueued to a global queue,
-				 * dequeued and freed in the worker thread.
+				 * Get allocated in this thread,
+				 * enqueued to a global queue, dequeued
+				 * and freed in the worker thread.
 				 */
 				int val = 1;
 				struct relay_connection *new_conn;
 				struct lttcomm_sock *newsock;
 
-				new_conn = connection_create();
-				if (!new_conn) {
-					goto error;
-				}
-
 				newsock = live_control_sock->ops->accept(live_control_sock);
 				if (!newsock) {
 					PERROR("accepting control sock");
-					connection_free(new_conn);
 					goto error;
 				}
 				DBG("Relay viewer connection accepted socket %d", newsock->fd);
@@ -530,18 +546,22 @@ restart:
 				if (ret < 0) {
 					PERROR("setsockopt inet");
 					lttcomm_destroy_sock(newsock);
-					connection_free(new_conn);
 					goto error;
 				}
-				new_conn->sock = newsock;
+				new_conn = connection_create(newsock, RELAY_CONNECTION_UNKNOWN);
+				if (!new_conn) {
+					lttcomm_destroy_sock(newsock);
+					goto error;
+				}
 
 				/* Enqueue request for the dispatcher thread. */
 				cds_wfcq_enqueue(&viewer_conn_queue.head, &viewer_conn_queue.tail,
 						 &new_conn->qnode);
 
 				/*
-				 * Wake the dispatch queue futex. Implicit memory barrier with
-				 * the exchange in cds_wfcq_enqueue.
+				 * Wake the dispatch queue futex.
+				 * Implicit memory barrier with the
+				 * exchange in cds_wfcq_enqueue.
 				 */
 				futex_nto1_wake(&viewer_conn_queue.futex);
 			}
@@ -618,14 +638,15 @@ void *thread_dispatcher(void *data)
 					conn->sock->fd);
 
 			/*
-			 * Inform worker thread of the new request. This call is blocking
-			 * so we can be assured that the data will be read at some point in
-			 * time or wait to the end of the world :)
+			 * Inform worker thread of the new request. This
+			 * call is blocking so we can be assured that
+			 * the data will be read at some point in time
+			 * or wait to the end of the world :)
 			 */
 			ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn));
 			if (ret < 0) {
 				PERROR("write conn pipe");
-				connection_destroy(conn);
+				connection_put(conn);
 				goto error;
 			}
 		} while (node != NULL);
@@ -664,8 +685,6 @@ int viewer_connect(struct relay_connection *conn)
 	int ret;
 	struct lttng_viewer_connect reply, msg;
 
-	assert(conn);
-
 	conn->version_check_done = 1;
 
 	health_code_update();
@@ -713,10 +732,13 @@ int viewer_connect(struct relay_connection *conn)
 	reply.minor = htobe32(reply.minor);
 	if (conn->type == RELAY_VIEWER_COMMAND) {
 		/*
-		 * Increment outside of htobe64 macro, because can be used more than once
-		 * within the macro, and thus the operation may be undefined.
+		 * Increment outside of htobe64 macro, because can be
+		 * used more than once within the macro, and thus the
+		 * operation may be undefined.
 		 */
+		pthread_mutex_lock(&last_relay_viewer_session_id_lock);
 		last_relay_viewer_session_id++;
+		pthread_mutex_unlock(&last_relay_viewer_session_id_lock);
 		reply.viewer_session_id = htobe64(last_relay_viewer_session_id);
 	}
 
@@ -738,6 +760,9 @@ end:
 
 /*
 * Send the viewer the list of current sessions.
+ * We need to create a copy of the hash table content because otherwise
+ * we cannot assume the number of entries stays the same between getting
+ * the number of HT elements and iteration over the HT.
 *
 * Return 0 on success or else a negative value.
 */
@@ -746,156 +771,89 @@ int viewer_list_sessions(struct relay_connection *conn)
 {
 	int ret;
 	struct lttng_viewer_list_sessions session_list;
-	unsigned long count;
-	long approx_before, approx_after;
 	struct lttng_ht_iter iter;
-	struct lttng_viewer_session send_session;
 	struct relay_session *session;
+	struct lttng_viewer_session *send_session_buf = NULL;
+	uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT;
+	uint32_t count = 0;
 
 	DBG("List sessions received");
 
-	rcu_read_lock();
-	cds_lfht_count_nodes(conn->sessions_ht->ht, &approx_before, &count,
-			&approx_after);
-	session_list.sessions_count = htobe32(count);
-
-	health_code_update();
-
-	ret = send_response(conn->sock, &session_list, sizeof(session_list));
-	if (ret < 0) {
-		goto end_unlock;
+	send_session_buf = zmalloc(SESSION_BUF_DEFAULT_COUNT *
 sizeof(*send_session_buf));
+	if (!send_session_buf) {
+		return -1;
 	}
 
-	health_code_update();
-
-	cds_lfht_for_each_entry(conn->sessions_ht->ht, &iter.iter, session,
+	rcu_read_lock();
+	cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
 			session_n.node) {
-		health_code_update();
-
-		strncpy(send_session.session_name, session->session_name,
-				sizeof(send_session.session_name));
-		strncpy(send_session.hostname, session->hostname,
-				sizeof(send_session.hostname));
-		send_session.id = htobe64(session->id);
-		send_session.live_timer = htobe32(session->live_timer);
-		send_session.clients = htobe32(session->viewer_refcount);
-		send_session.streams = htobe32(session->stream_count);
+		struct lttng_viewer_session *send_session;
 
 		health_code_update();
 
-		ret = send_response(conn->sock, &send_session, sizeof(send_session));
-		if (ret < 0) {
-			goto end_unlock;
-		}
-	}
-	health_code_update();
+		if (count >= buf_count) {
+			struct lttng_viewer_session *newbuf;
+			uint32_t new_buf_count = buf_count << 1;
 
-	ret = 0;
-end_unlock:
-	rcu_read_unlock();
-	return ret;
-}
-
-/*
- * Check if a connection is attached to a session.
- * Return 1 if attached, 0 if not attached, a negative value on error.
- */
-static
-int session_attached(struct relay_connection *conn, uint64_t session_id)
-{
-	struct relay_session *session;
-	int found = 0;
-
-	if (!conn->viewer_session) {
-		goto end;
-	}
-	cds_list_for_each_entry(session,
-			&conn->viewer_session->sessions_head,
-			viewer_session_list) {
-		if (session->id == session_id) {
-			found = 1;
-			goto end;
+			newbuf = realloc(send_session_buf,
+				new_buf_count * sizeof(*send_session_buf));
+			if (!new_buf_count) {
+				ret = -1;
+				rcu_read_unlock();
+				goto end_free;
+			}
+			send_session_buf = newbuf;
+			buf_count = new_buf_count;
 		}
-	}
-
-end:
-	return found;
-}
-
-/*
- * Delete all streams for a specific session ID.
- */
-static void destroy_viewer_streams_by_session(struct relay_session *session)
-{
-	struct relay_viewer_stream *stream;
-	struct lttng_ht_iter iter;
-
-	assert(session);
-
-	rcu_read_lock();
-	cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, stream,
-			stream_n.node) {
-		struct ctf_trace *ctf_trace;
-
-		health_code_update();
-		if (stream->session_id != session->id) {
-			continue;
+		send_session = &send_session_buf[count];
+		strncpy(send_session->session_name, session->session_name,
+				sizeof(send_session->session_name));
+		strncpy(send_session->hostname, session->hostname,
+				sizeof(send_session->hostname));
+		send_session->id = htobe64(session->id);
+		send_session->live_timer = htobe32(session->live_timer);
+		if (session->viewer_attached) {
+			send_session->clients = htobe32(1);
+		} else {
+			send_session->clients = htobe32(0);
 		}
+		send_session->streams = htobe32(session->stream_count);
+		count++;
+	}
+	rcu_read_unlock();
 
-		ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
-				stream->path_name);
-		assert(ctf_trace);
-
-		viewer_stream_delete(stream);
+	session_list.sessions_count = htobe32(count);
 
-		if (stream->metadata_flag) {
-			ctf_trace->metadata_sent = 0;
-			ctf_trace->viewer_metadata_stream = NULL;
-		}
+	health_code_update();
 
-		viewer_stream_destroy(ctf_trace, stream);
+	ret = send_response(conn->sock, &session_list, sizeof(session_list));
+	if (ret < 0) {
+		goto end_free;
 	}
-	rcu_read_unlock();
-}
-
-static void try_destroy_streams(struct relay_session *session)
-{
-	struct ctf_trace *ctf_trace;
-	struct lttng_ht_iter iter;
 
-	assert(session);
+	health_code_update();
 
-	cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
-			node.node) {
-		/* Attempt to destroy the ctf trace of that session. */
-		ctf_trace_try_destroy(session, ctf_trace);
+	ret = send_response(conn->sock, send_session_buf,
+			count * sizeof(*send_session_buf));
+	if (ret < 0) {
+		goto end_free;
 	}
-}
+	health_code_update();
 
-/*
- * Cleanup a session.
- */
-static void cleanup_session(struct relay_connection *conn,
-		struct relay_session *session)
-{
-	/*
-	 * Very important that this is done before destroying the session so we
-	 * can put back every viewer stream reference from the ctf_trace.
-	 */
-	destroy_viewer_streams_by_session(session);
-	try_destroy_streams(session);
-	cds_list_del(&session->viewer_session_list);
-	session_viewer_try_destroy(conn->sessions_ht, session);
+	ret = 0;
+end_free:
+	free(send_session_buf);
+	return ret;
 }
 
 /*
- * Send the viewer the list of current sessions.
+ * Send the viewer the list of current streams.
 */

static
int viewer_get_new_streams(struct relay_connection *conn)

{
	int ret, send_streams = 0;
src/bin/lttng-relayd/live.c +1 -1
-	uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0;
+	uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 0;
	struct lttng_viewer_new_streams_request request;
	struct lttng_viewer_new_streams_response response;
	struct relay_session *session;
src/bin/lttng-relayd/live.c +4 -5
@@ -918,15 +876,14 @@ int viewer_get_new_streams(struct relay_connection *conn)
 
 	memset(&response, 0, sizeof(response));
 
-	rcu_read_lock();
-	session = session_find_by_id(conn->sessions_ht, session_id);
+	session = session_get_by_id(session_id);
 	if (!session) {
 		DBG("Relay session %" PRIu64 " not found", session_id);
 		response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
 		goto send_reply;
 	}
 
-	if (!session_attached(conn, session_id)) {
+	if (!viewer_session_is_attached(conn->viewer_session, session)) {
 		send_streams = 0;
 		response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
 		goto send_reply;
@@ -935,10 +892,10 @@ int viewer_get_new_streams(struct relay_connection *conn)
 	send_streams = 1;
 	response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
 
-	ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, NULL, &nb_unsent,
+	ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, &nb_total,
 &nb_unsent,
 			&nb_created);
 	if (ret < 0) {
-		goto end_unlock;
+		goto end_put_session;
 	}
 	/* Only send back the newly created streams with the unsent ones. */
	nb_streams = nb_created + nb_unsent;
src/bin/lttng-relayd/live.c +13 -16
@@ -949,15 +906,9 @@ int viewer_get_new_streams(struct relay_connection *conn)
 	 * it means that the viewer has already received the whole trace
 	 * for this session and should now close it.
 	 */
-	if (nb_streams == 0 && session->close_flag) {
+	if (nb_total == 0 && session->connection_closed) {
 		send_streams = 0;
 		response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
-		/*
-		 * Remove the session from the attached list of the connection
-		 * and try to destroy it.
-		 */
-		cds_list_del(&session->viewer_session_list);
-		cleanup_session(conn, session);
 		goto send_reply;
 	}
 
@@ -965,30 +916,33 @@ send_reply:
 	health_code_update();
 	ret = send_response(conn->sock, &response, sizeof(response));
 	if (ret < 0) {
-		goto end_unlock;
+		goto end_put_session;
 	}
 	health_code_update();
 
 	/*
-	 * Unknown or empty session, just return gracefully, the viewer knows what
-	 * is happening.
+	 * Unknown or empty session, just return gracefully, the viewer
+	 * knows what is happening.
 	 */
 	if (!send_streams || !nb_streams) {
 		ret = 0;
-		goto end_unlock;
+		goto end_put_session;
 	}
 
 	/*
-	 * Send stream and *DON'T* ignore the sent flag so every viewer streams
-	 * that were not sent from that point will be sent to the viewer.
+	 * Send stream and *DON'T* ignore the sent flag so every viewer
+	 * streams that were not sent from that point will be sent to
+	 * the viewer.
 	 */
 	ret = send_viewer_streams(conn->sock, session, 0);
 	if (ret < 0) {
-		goto end_unlock;
+		goto end_put_session;
 	}
 
-end_unlock:
-	rcu_read_unlock();
+end_put_session:
+	if (session) {
+		session_put(session);
+	}

error:

	return ret;
}
src/bin/lttng-relayd/live.c +24 -25
@@ -1005,7 +959,7 @@ int viewer_attach_session(struct relay_connection *conn)
 	enum lttng_viewer_seek seek_type;
 	struct lttng_viewer_attach_session_request request;
 	struct lttng_viewer_attach_session_response response;
-	struct relay_session *session;
+	struct relay_session *session = NULL;
 
 	assert(conn);
 
@@ -1027,37 +981,34 @@ int viewer_attach_session(struct relay_connection *conn)
 		goto send_reply;
 	}
 
-	rcu_read_lock();
-	session = session_find_by_id(conn->sessions_ht,
-			be64toh(request.session_id));
+	session = session_get_by_id(be64toh(request.session_id));
 	if (!session) {
 		DBG("Relay session %" PRIu64 " not found",
 				be64toh(request.session_id));
 		response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
 		goto send_reply;
 	}
-	session_viewer_attach(session);
-	DBG("Attach session ID %" PRIu64 " received", be64toh(request.session_id));
+	DBG("Attach session ID %" PRIu64 " received",
+		be64toh(request.session_id));
 
-	if (uatomic_read(&session->viewer_refcount) > 1) {
-		DBG("Already a viewer attached");
-		response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY);
-		session_viewer_detach(session);
-		goto send_reply;
-	} else if (session->live_timer == 0) {
+	if (session->live_timer == 0) {
 		DBG("Not live session");
 		response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE);
 		goto send_reply;
-	} else {
-		send_streams = 1;
-		response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
-		cds_list_add(&session->viewer_session_list,
-				&conn->viewer_session->sessions_head);
+	}
+
+	send_streams = 1;
+	ret = viewer_session_attach(conn->viewer_session, session);
+	if (ret) {
+		DBG("Already a viewer attached");
+		response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY);
+		goto send_reply;
 	}
 
 	switch (be32toh(request.seek)) {
 	case LTTNG_VIEWER_SEEK_BEGINNING:
 	case LTTNG_VIEWER_SEEK_LAST:
+		response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
 		seek_type = be32toh(request.seek);
 		break;
 	default:
@@ -1069,7 +1020,7 @@ int viewer_attach_session(struct relay_connection *conn)
 
 	ret = make_viewer_streams(session, seek_type, &nb_streams, NULL, NULL);
 	if (ret < 0) {
-		goto end_unlock;
+		goto end_put_session;
 	}
 	response.streams_count = htobe32(nb_streams);
 
@@ -1077,27 +1028,29 @@ send_reply:
 	health_code_update();
 	ret = send_response(conn->sock, &response, sizeof(response));
 	if (ret < 0) {
-		goto end_unlock;
+		goto end_put_session;
 	}
 	health_code_update();
 
 	/*
-	 * Unknown or empty session, just return gracefully, the viewer knows what
-	 * is happening.
+	 * Unknown or empty session, just return gracefully, the viewer
+	 * knows what is happening.
 	 */
 	if (!send_streams || !nb_streams) {
 		ret = 0;
-		goto end_unlock;
+		goto end_put_session;
 	}
 
 	/* Send stream and ignore the sent flag. */
 	ret = send_viewer_streams(conn->sock, session, 1);
 	if (ret < 0) {
-		goto end_unlock;
+		goto end_put_session;
 	}
 
-end_unlock:
-	rcu_read_unlock();
+end_put_session:
+	if (session) {
+		session_put(session);
+	}

error:

	return ret;
}
src/bin/lttng-relayd/live.c +108 -65
@@ -1105,38 +1058,42 @@ error:
 /*
 * Open the index file if needed for the given vstream.
 *
- * If an index file is successfully opened, the index_read_fd of the stream is
- * set with it.
+ * If an index file is successfully opened, the vstream index_fd set with
+ * it.
 *
 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
+ *
+ * Called with both rstream and vstream locks held.
 */
 static int try_open_index(struct relay_viewer_stream *vstream,
 		struct relay_stream *rstream)
 {
 	int ret = 0;
 
-	assert(vstream);
-	assert(rstream);
-
-	if (vstream->index_read_fd >= 0) {
+	if (vstream->index_fd) {
 		goto end;
 	}
 
 	/*
-	 * First time, we open the index file and at least one index is ready.  The
-	 * race between the read and write of the total_index_received is
-	 * acceptable here since the client will be notified to simply come back
-	 * and get the next index.
+	 * First time, we open the index file and at least one index is ready.
 	 */
 	if (rstream->total_index_received <= 0) {
 		ret = -ENOENT;
 		goto end;
 	}
 	ret = index_open(vstream->path_name, vstream->channel_name,
-			vstream->tracefile_count, vstream->tracefile_count_current);
+			vstream->stream->tracefile_count,
+			vstream->current_tracefile_id);
 	if (ret >= 0) {
-		vstream->index_read_fd = ret;
-		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;
 	}
 
@@ -1145,13 +1102,15 @@ end:
 }
 
 /*
- * Check the status of the index for the given stream. This function updates
- * the index structure if needed and can destroy the vstream also for the HUP
- * situation.
+ * Check the status of the index for the given stream. This function
+ * updates the index structure if needed and can put (close) the vstream
+ * in the HUP situation.
+ *
+ * Return 0 means that we can proceed with the index. A value of 1 means
+ * that the index has been updated and is ready to be send to the
+ * client. A negative value indicates an error that can't be handled.
 *
- * Return 0 means that we can proceed with the index. A value of 1 means that
- * the index has been updated and is ready to be send to the client. A negative
- * value indicates an error that can't be handled.
+ * Called with both rstream and vstream locks held.
 */
 static int check_index_status(struct relay_viewer_stream *vstream,
 		struct relay_stream *rstream, struct ctf_trace *trace,
@@ -1159,68 +1118,104 @@ static int check_index_status(struct
 relay_viewer_stream *vstream,
 {
 	int ret;
 
-	assert(vstream);
-	assert(rstream);
-	assert(index);
-	assert(trace);
-
-	if (!rstream->close_flag) {
-		/* Rotate on abort (overwrite). */
-		if (vstream->abort_flag) {
-			DBG("Viewer stream %" PRIu64 " rotate because of overwrite",
-					vstream->stream_handle);
-			ret = viewer_stream_rotate(vstream, rstream);
+	if (trace->session->connection_closed
+			&& rstream->total_index_received
+				== vstream->last_sent_index) {
+		/* Last index sent and session connection is closed. */
+		index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
+		goto hup;
+	} else if (rstream->beacon_ts_end != -1ULL &&
+			rstream->total_index_received
+				== vstream->last_sent_index) {
+		/*
+		 * We've received a synchronization beacon and the last index
+		 * available has been sent, the index for now is inactive.
+		 */
+		index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
+		index->timestamp_end = htobe64(rstream->beacon_ts_end);
+		index->stream_id = htobe64(rstream->ctf_stream_id);
+		goto index_ready;
+	} else if (rstream->total_index_received <= vstream->last_sent_index) {
+		/* This actually checks the case where recv ==  last_sent. */
+		index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
+		goto index_ready;
+	} else if (!viewer_stream_is_tracefile_id_readable(vstream,
+			vstream->current_tracefile_id)) {
+		/*
+		 * The producer has overwritten our current file. We
+		 * need to rotate.
+		 */
+		DBG("Viewer stream %" PRIu64 " rotation due to overwrite",
+				vstream->stream->stream_handle);
+		ret = viewer_stream_rotate(vstream);
+		if (ret < 0) {
+			goto end;
+		} else if (ret == 1) {
+			/* EOF across entire stream. */
+			index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
+			goto hup;
+		} else if (!viewer_stream_is_tracefile_id_readable(vstream,
+				vstream->current_tracefile_id)) {
+			/*
+			 * We rotated into a file being concurrently
+			 * updated, need to retry later.
+			 */
+			index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
+			goto index_ready;
+		}
+		/* ret == 0 means successful so we continue. */
+		ret = 0;
+	} else {
+		ssize_t read_ret;
+		char tmp[1];
+
+		/*
+		 * Use EOF on current index file to find out when we
+		 * need to rotate.
+		 */
+		read_ret = lttng_read(vstream->index_fd->fd, tmp, 1);
+		if (read_ret == 1) {
+			off_t seek_ret;
+
+			/* There is still data to read. Rewind position. */
+			seek_ret = lseek(vstream->index_fd->fd, -1, SEEK_CUR);
+			if (seek_ret < 0) {
+				ret = -1;
+				goto end;
+			}
+			ret = 0;
+		} else if (read_ret == 0) {
+			/* EOF. We need to rotate. */
+			DBG("Viewer stream %" PRIu64 " rotation due to EOF",
+					vstream->stream->stream_handle);
+			ret = viewer_stream_rotate(vstream);
 			if (ret < 0) {
-				goto error;
+				goto end;
 			} else if (ret == 1) {
-				/* EOF */
+				/* EOF across entire stream. */
 				index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
 				goto hup;
-			}
-			/* ret == 0 means successful so we continue. */
-		}
-
-		/* Check if we are in the same trace file at this point. */
-		if (rstream->tracefile_count_current == vstream->tracefile_count_current) {
-			if (rstream->beacon_ts_end != -1ULL &&
-					vstream->last_sent_index == rstream->total_index_received) {
+			} else if (!viewer_stream_is_tracefile_id_readable(vstream,
+					vstream->current_tracefile_id)) {
 				/*
-				 * We've received a synchronization beacon and the last index
-				 * available has been sent, the index for now is inactive.
-				 */
-				index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
-				index->timestamp_end = htobe64(rstream->beacon_ts_end);
-				index->stream_id = htobe64(rstream->ctf_stream_id);
-				goto index_ready;
-			} else if (rstream->total_index_received <= vstream->last_sent_index
-					&& !vstream->close_write_flag) {
-				/*
-				 * Reader and writer are working in the same tracefile, so we care
-				 * about the number of index received and sent. Otherwise, we read
-				 * up to EOF.
+				 * We rotated into a file being concurrently
+				 * updated, need to retry later.
 				 */
 				index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
 				goto index_ready;
 			}
+			/* ret == 0 means successful so we continue. */
+			ret = 0;
+		} else {
+			/* Error reading index. */
+			ret = -1;
 		}
-		/* Nothing to do with the index, continue with it. */
-		ret = 0;
-	} else if (rstream->close_flag && vstream->close_write_flag &&
-			vstream->total_index_received == vstream->last_sent_index) {
-		/* Last index sent and current tracefile closed in write */
-		index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
-		goto hup;
-	} else {
-		vstream->close_write_flag = 1;
-		ret = 0;
 	}
-
-error:
+end:
 	return ret;
 

hup:

src/bin/lttng-relayd/live.c +1 -2
-	viewer_stream_delete(vstream);
-	viewer_stream_destroy(trace, vstream);
+	viewer_stream_put(vstream);

index_ready:

	return 1;
}
src/bin/lttng-relayd/live.c +19 -20
@@ -1238,10 +1233,10 @@ int viewer_get_next_index(struct relay_connection *conn)
 	struct lttng_viewer_get_next_index request_index;
 	struct lttng_viewer_index viewer_index;
 	struct ctf_packet_index packet_index;
-	struct relay_viewer_stream *vstream;
-	struct relay_stream *rstream;
-	struct ctf_trace *ctf_trace;
-	struct relay_session *session;
+	struct relay_viewer_stream *vstream = NULL;
+	struct relay_stream *rstream = NULL;
+	struct ctf_trace *ctf_trace = NULL;
+	struct relay_viewer_stream *metadata_viewer_stream = NULL;
 
 	assert(conn);
 
@@ -1255,42 +1250,41 @@ int viewer_get_next_index(struct relay_connection *conn)
 	}
 	health_code_update();
 
-	rcu_read_lock();
-	vstream = viewer_stream_find_by_id(be64toh(request_index.stream_id));
+	vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id));
 	if (!vstream) {
 		ret = -1;
-		goto end_unlock;
+		goto end;
 	}
 
-	session = session_find_by_id(conn->sessions_ht, vstream->session_id);
-	if (!session) {
-		ret = -1;
-		goto end_unlock;
-	}
+	/* Use back. ref. Protected by refcounts. */
+	rstream = vstream->stream;
+	ctf_trace = rstream->trace;
 
-	ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
 vstream->path_name);
-	assert(ctf_trace);
+	/* metadata_viewer_stream may be NULL. */
+	metadata_viewer_stream =
+		ctf_trace_get_viewer_metadata_stream(ctf_trace);
 
 	memset(&viewer_index, 0, sizeof(viewer_index));
 
+	pthread_mutex_lock(&rstream->lock);
+	pthread_mutex_lock(&vstream->lock);
+
 	/*
 	 * The viewer should not ask for index on metadata stream.
 	 */
-	if (vstream->metadata_flag) {
+	if (rstream->is_metadata) {
 		viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
 		goto send_reply;
 	}
 
-	rstream = stream_find_by_id(relay_streams_ht, vstream->stream_handle);
-	assert(rstream);
-
 	/* Try to open an index if one is needed for that stream. */
 	ret = try_open_index(vstream, rstream);
 	if (ret < 0) {
 		if (ret == -ENOENT) {
 			/*
-			 * The index is created only when the first data packet arrives, it
-			 * might not be ready at the beginning of the session
+			 * The index is created only when the first data
+			 * packet arrives, it might not be ready at the
+			 * beginning of the session
 			 */
 			viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
		} else {
src/bin/lttng-relayd/live.c +49 -55
@@ -1300,80 +1294,71 @@ int viewer_get_next_index(struct relay_connection *conn)
 		goto send_reply;
 	}
 
-	pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
 	ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
-	pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
 	if (ret < 0) {
-		goto end_unlock;
+		goto error_put;
 	} else if (ret == 1) {
 		/*
-		 * This means the viewer index data structure has been populated by the
-		 * check call thus we now send back the reply to the client.
+		 * This means the viewer index data structure has been
+		 * populated by the check call thus we now send back the
+		 * reply to the client.
 		 */
 		goto send_reply;
 	}
 	/* At this point, ret MUST be 0 thus we continue with the get. */
 	assert(!ret);
 
-	if (!ctf_trace->metadata_received ||
-			ctf_trace->metadata_received > ctf_trace->metadata_sent) {
-		viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
+	/*
+	 * vstream->stream_fd may be NULL if it has been closed by
+	 * tracefile rotation, or if we are at the beginning of the
+	 * stream. We open the data stream file here to protect against
+	 * overwrite caused by tracefile rotation (in association with
+	 * unlink performed before overwrite).
+	 */
+	if (!vstream->stream_fd) {
+		char fullpath[PATH_MAX];
+
+		if (vstream->stream->tracefile_count > 0) {
+			ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64,
+					vstream->path_name,
+					vstream->channel_name,
+					vstream->current_tracefile_id);
+		} else {
+			ret = snprintf(fullpath, PATH_MAX, "%s/%s",
+					vstream->path_name,
+					vstream->channel_name);
+		}
+		if (ret < 0) {
+			goto error_put;
+		}
+		ret = open(fullpath, O_RDONLY);
+		if (ret < 0) {
+			PERROR("Relay opening trace file");
+			goto error_put;
+		}
+		vstream->stream_fd = stream_fd_create(ret);
+		if (!vstream->stream_fd) {
+			if (close(ret)) {
+				PERROR("close");
+			}
+			goto error_put;
+		}
 	}
 
 	ret = check_new_streams(conn);
 	if (ret < 0) {
-		goto end_unlock;
+		viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
+		goto send_reply;
 	} else if (ret == 1) {
 		viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
 	}
 
-	pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
-	pthread_mutex_lock(&vstream->overwrite_lock);
-	if (vstream->abort_flag) {
-		/* The file is being overwritten by the writer, we cannot use it. */
-		pthread_mutex_unlock(&vstream->overwrite_lock);
-		ret = viewer_stream_rotate(vstream, rstream);
-		pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
-		if (ret < 0) {
-			goto end_unlock;
-		} else if (ret == 1) {
-			viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
-			viewer_stream_delete(vstream);
-			viewer_stream_destroy(ctf_trace, vstream);
-		} else {
-			viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
-		}
-		goto send_reply;
-	}
-
-	read_ret = lttng_read(vstream->index_read_fd, &packet_index,
+	read_ret = lttng_read(vstream->index_fd->fd, &packet_index,
 			sizeof(packet_index));
-	pthread_mutex_unlock(&vstream->overwrite_lock);
-	pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
-	if (read_ret < 0) {
-		viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
-		viewer_stream_delete(vstream);
-		viewer_stream_destroy(ctf_trace, vstream);
-		goto send_reply;
-	} else if (read_ret < sizeof(packet_index)) {
-		pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
-		if (vstream->close_write_flag) {
-			ret = viewer_stream_rotate(vstream, rstream);
-			if (ret < 0) {
-				pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
-				goto end_unlock;
-			} else if (ret == 1) {
-				viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
-				viewer_stream_delete(vstream);
-				viewer_stream_destroy(ctf_trace, vstream);
-			} else {
-				viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
-			}
-		} else {
-			ERR("Relay reading index file %d", vstream->index_read_fd);
-			viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
-		}
-		pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
+	if (read_ret < sizeof(packet_index)) {
+		ERR("Relay reading index file %d returned %zd",
+			vstream->index_fd->fd, read_ret);
+		viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
 		goto send_reply;
 	} else {
 		viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK);
@@ -1383,6 +1368,9 @@ int viewer_get_next_index(struct relay_connection *conn)
 	/*
 	 * Indexes are stored in big endian, no need to switch before sending.
 	 */
+	DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64,
+		rstream->stream_handle,
+		be64toh(packet_index.offset));
 	viewer_index.offset = packet_index.offset;
 	viewer_index.packet_size = packet_index.packet_size;
 	viewer_index.content_size = packet_index.content_size;
@@ -1392,22 +1380,53 @@ int viewer_get_next_index(struct relay_connection *conn)
 	viewer_index.stream_id = packet_index.stream_id;
 

send_reply:

src/bin/lttng-relayd/live.c +19 -0
+	pthread_mutex_unlock(&vstream->lock);
+	pthread_mutex_unlock(&rstream->lock);
+
+	if (metadata_viewer_stream) {
+		pthread_mutex_lock(&metadata_viewer_stream->stream->lock);
+		pthread_mutex_lock(&metadata_viewer_stream->lock);
+		DBG("get next index metadata check: recv %" PRIu64
+				" sent %" PRIu64,
+			metadata_viewer_stream->stream->metadata_received,
+			metadata_viewer_stream->metadata_sent);
+		if (!metadata_viewer_stream->stream->metadata_received ||
+				metadata_viewer_stream->stream->metadata_received >
+					metadata_viewer_stream->metadata_sent) {
+			viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
+		}
+		pthread_mutex_unlock(&metadata_viewer_stream->lock);
+		pthread_mutex_unlock(&metadata_viewer_stream->stream->lock);
+	}
+
	viewer_index.flags = htobe32(viewer_index.flags);
	health_code_update();

	ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
	if (ret < 0) {
src/bin/lttng-relayd/live.c +1 -1
-		goto end_unlock;
+		goto end;
	}
	health_code_update();

	DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
src/bin/lttng-relayd/live.c +2 -5
-			vstream->last_sent_index, vstream->stream_handle);
-
-end_unlock:
-	rcu_read_unlock();
-
+			vstream->last_sent_index,
+			vstream->stream->stream_handle);

end:

src/bin/lttng-relayd/live.c +15 -0
+	if (metadata_viewer_stream) {
+		viewer_stream_put(metadata_viewer_stream);
+	}
+	if (vstream) {
+		viewer_stream_put(vstream);
+	}
+	return ret;
+
+error_put:
+	pthread_mutex_unlock(&vstream->lock);
+	pthread_mutex_unlock(&rstream->lock);
+	if (metadata_viewer_stream) {
+		viewer_stream_put(metadata_viewer_stream);
+	}
+	viewer_stream_put(vstream);
	return ret;
}
src/bin/lttng-relayd/live.c +49 -71
@@ -1425,17 +1444,16 @@ int viewer_get_packet(struct relay_connection *conn)
 	ssize_t read_len;
 	struct lttng_viewer_get_packet get_packet_info;
 	struct lttng_viewer_trace_packet reply;
-	struct relay_viewer_stream *stream;
-	struct relay_session *session;
+	struct relay_viewer_stream *vstream = NULL;
 	struct ctf_trace *ctf_trace;
-
-	assert(conn);
+	struct relay_viewer_stream *metadata_viewer_stream = NULL;
 
 	DBG2("Relay get data packet");
 
 	health_code_update();
 
-	ret = recv_request(conn->sock, &get_packet_info, sizeof(get_packet_info));
+	ret = recv_request(conn->sock, &get_packet_info,
+		sizeof(get_packet_info));
 	if (ret < 0) {
 		goto end;
 	}
@@ -1444,59 +1462,53 @@ int viewer_get_packet(struct relay_connection *conn)
 	/* From this point on, the error label can be reached. */
 	memset(&reply, 0, sizeof(reply));
 
-	rcu_read_lock();
-	stream = viewer_stream_find_by_id(be64toh(get_packet_info.stream_id));
-	if (!stream) {
+	vstream = viewer_stream_get_by_id(be64toh(get_packet_info.stream_id));
+	if (!vstream) {
 		goto error;
 	}
 
-	session = session_find_by_id(conn->sessions_ht, stream->session_id);
-	if (!session) {
-		ret = -1;
-		goto error;
-	}
+	ctf_trace = vstream->stream->trace;
 
-	ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
-			stream->path_name);
-	assert(ctf_trace);
+	/* metadata_viewer_stream may be NULL. */
+	metadata_viewer_stream =
+		ctf_trace_get_viewer_metadata_stream(ctf_trace);
 
-	/*
-	 * First time we read this stream, we need open the tracefile, we should
-	 * only arrive here if an index has already been sent to the viewer, so the
-	 * tracefile must exist, if it does not it is a fatal error.
-	 */
-	if (stream->read_fd < 0) {
-		char fullpath[PATH_MAX];
+	if (metadata_viewer_stream) {
+		bool get_packet_err = false;
 
-		if (stream->tracefile_count > 0) {
-			ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64, stream->path_name,
-					stream->channel_name,
-					stream->tracefile_count_current);
-		} else {
-			ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
-					stream->channel_name);
+		pthread_mutex_lock(&metadata_viewer_stream->stream->lock);
+		pthread_mutex_lock(&metadata_viewer_stream->lock);
+		DBG("get packet metadata check: recv %" PRIu64 " sent %" PRIu64,
+			metadata_viewer_stream->stream->metadata_received,
+			metadata_viewer_stream->metadata_sent);
+		if (!metadata_viewer_stream->stream->metadata_received ||
+				metadata_viewer_stream->stream->metadata_received >
+					metadata_viewer_stream->metadata_sent) {
+			get_packet_err = true;
 		}
-		if (ret < 0) {
-			goto error;
-		}
-		ret = open(fullpath, O_RDONLY);
-		if (ret < 0) {
-			PERROR("Relay opening trace file");
-			goto error;
+		pthread_mutex_unlock(&metadata_viewer_stream->lock);
+		pthread_mutex_unlock(&metadata_viewer_stream->stream->lock);
+		viewer_stream_put(metadata_viewer_stream);
+		if (get_packet_err) {
+			reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
+			reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
+			goto send_reply_nolock;
 		}
-		stream->read_fd = ret;
-	}
-
-	if (!ctf_trace->metadata_received ||
-			ctf_trace->metadata_received > ctf_trace->metadata_sent) {
-		reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
-		reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
-		goto send_reply;
 	}
 
+	pthread_mutex_lock(&vstream->lock);
+	/*
+	 * The vstream->stream_fd used here has been opened by
+	 * get_next_index. It is opened there because this is what
+	 * allows us to grab a reference to the file with stream lock
+	 * held, thus protecting us against overwrite caused by
+	 * tracefile rotation. Since tracefile rotation unlinks the old
+	 * data file, we are ensured that we won't have our data
+	 * overwritten under us.
+	 */
 	ret = check_new_streams(conn);
 	if (ret < 0) {
-		goto end_unlock;
+		goto end_free;
 	} else if (ret == 1) {
 		reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
 		reply.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
@@ -1510,34 +1522,19 @@ int viewer_get_packet(struct relay_connection *conn)
 		goto error;
 	}
 
-	ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
+	ret = lseek(vstream->stream_fd->fd, be64toh(get_packet_info.offset),
+			SEEK_SET);
 	if (ret < 0) {
-		/*
-		 * If the read fd was closed by the streaming side, the
-		 * abort_flag will be set to 1, otherwise it is an error.
-		 */
-		if (stream->abort_flag == 0) {
-			PERROR("lseek");
-			goto error;
-		}
-		reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
-		goto send_reply;
+		PERROR("lseek fd %d to offset %" PRIu64, vstream->stream_fd->fd,
+			be64toh(get_packet_info.offset));
+		goto error;
 	}
-	read_len = lttng_read(stream->read_fd, data, len);
+	read_len = lttng_read(vstream->stream_fd->fd, data, len);
 	if (read_len < len) {
-		/*
-		 * If the read fd was closed by the streaming side, the
-		 * abort_flag will be set to 1, otherwise it is an error.
-		 */
-		if (stream->abort_flag == 0) {
-			PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
-					stream->read_fd,
-					be64toh(get_packet_info.offset));
-			goto error;
-		} else {
-			reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
-			goto send_reply;
-		}
+		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);
@@ -1548,13 +1545,17 @@ error:
 	reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
 

send_reply:

src/bin/lttng-relayd/live.c +4 -0
+	if (vstream) {
+		pthread_mutex_unlock(&vstream->lock);
+	}
+send_reply_nolock:
	reply.flags = htobe32(reply.flags);

	health_code_update();

	ret = send_response(conn->sock, &reply, sizeof(reply));
	if (ret < 0) {
src/bin/lttng-relayd/live.c +1 -1
-		goto end_unlock;
+		goto end_free;
	}
	health_code_update();
src/bin/lttng-relayd/live.c +2 -4
@@ -1562,7 +1563,7 @@ send_reply:
 		health_code_update();
 		ret = send_response(conn->sock, data, len);
 		if (ret < 0) {
-			goto end_unlock;
+			goto end_free;
 		}
 		health_code_update();
 	}
@@ -1570,11 +1571,12 @@ send_reply:
 	DBG("Sent %u bytes for stream %" PRIu64, len,
 			be64toh(get_packet_info.stream_id));
 
-end_unlock:
+end_free:
 	free(data);
-	rcu_read_unlock();
-

end:

src/bin/lttng-relayd/live.c +3 -0
+	if (vstream) {
+		viewer_stream_put(vstream);
+	}
	return ret;
}
src/bin/lttng-relayd/live.c +28 -23
@@ -1592,9 +1594,7 @@ int viewer_get_metadata(struct relay_connection *conn)
 	char *data = NULL;
 	struct lttng_viewer_get_metadata request;
 	struct lttng_viewer_metadata_packet reply;
-	struct relay_viewer_stream *stream;
-	struct ctf_trace *ctf_trace;
-	struct relay_session *session;
+	struct relay_viewer_stream *vstream = NULL;
 
 	assert(conn);
 
@@ -1610,36 +1610,30 @@ int viewer_get_metadata(struct relay_connection *conn)
 
 	memset(&reply, 0, sizeof(reply));
 
-	rcu_read_lock();
-	stream = viewer_stream_find_by_id(be64toh(request.stream_id));
-	if (!stream || !stream->metadata_flag) {
-		ERR("Invalid metadata stream");
-		goto error;
+	vstream = viewer_stream_get_by_id(be64toh(request.stream_id));
+	if (!vstream) {
+		reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
+		goto send_reply;
 	}
-
-	session = session_find_by_id(conn->sessions_ht, stream->session_id);
-	if (!session) {
-		ret = -1;
+	if (!vstream->stream->is_metadata) {
+		ERR("Invalid metadata stream");
 		goto error;
 	}
 
-	ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
-			stream->path_name);
-	assert(ctf_trace);
-	assert(ctf_trace->metadata_sent <= ctf_trace->metadata_received);
+	assert(vstream->metadata_sent <= vstream->stream->metadata_received);
 
-	len = ctf_trace->metadata_received - ctf_trace->metadata_sent;
+	len = vstream->stream->metadata_received - vstream->metadata_sent;
 	if (len == 0) {
 		reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
 		goto send_reply;
 	}
 
 	/* first time, we open the metadata file */
-	if (stream->read_fd < 0) {
+	if (!vstream->stream_fd) {
 		char fullpath[PATH_MAX];
 
-		ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
-				stream->channel_name);
+		ret = snprintf(fullpath, PATH_MAX, "%s/%s", vstream->path_name,
+				vstream->channel_name);
 		if (ret < 0) {
 			goto error;
 		}
@@ -1648,7 +1642,13 @@ int viewer_get_metadata(struct relay_connection *conn)
 			PERROR("Relay opening metadata file");
 			goto error;
 		}
-		stream->read_fd = ret;
+		vstream->stream_fd = stream_fd_create(ret);
+		if (!vstream->stream_fd) {
+			if (close(ret)) {
+				PERROR("close");
+			}
+			goto error;
+		}
 	}
 
 	reply.len = htobe64(len);
@@ -1658,13 +1658,20 @@ int viewer_get_metadata(struct relay_connection *conn)
 		goto error;
 	}
 
-	read_len = lttng_read(stream->read_fd, data, len);
+	read_len = lttng_read(vstream->stream_fd->fd, data, len);
 	if (read_len < len) {
 		PERROR("Relay reading metadata file");
 		goto error;
 	}
-	ctf_trace->metadata_sent += read_len;
+	vstream->metadata_sent += read_len;
+	if (vstream->metadata_sent == vstream->stream->metadata_received
+			&& vstream->stream->closed) {
+		/* Release ownership for the viewer metadata stream. */
+		viewer_stream_put(vstream);
+	}
+
 	reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
+
 	goto send_reply;
 

error:

src/bin/lttng-relayd/live.c +3 -4
@@ -1674,14 +1681,14 @@ send_reply:
 	health_code_update();
 	ret = send_response(conn->sock, &reply, sizeof(reply));
 	if (ret < 0) {
-		goto end_unlock;
+		goto end_free;
 	}
 	health_code_update();
 
 	if (len > 0) {
 		ret = send_response(conn->sock, data, len);
 		if (ret < 0) {
-			goto end_unlock;
+			goto end_free;
 		}
 	}
 
@@ -1690,10 +1697,12 @@ send_reply:
 
 	DBG("Metadata sent");
 
-end_unlock:
+end_free:
 	free(data);
-	rcu_read_unlock();

end:

src/bin/lttng-relayd/live.c +3 -0
+	if (vstream) {
+		viewer_stream_put(vstream);
+	}
	return ret;
}
src/bin/lttng-relayd/live.c +1 -2
@@ -1712,13 +1721,12 @@ int viewer_create_session(struct relay_connection *conn)
 
 	memset(&resp, 0, sizeof(resp));
 	resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
-	conn->viewer_session = zmalloc(sizeof(*conn->viewer_session));
+	conn->viewer_session = viewer_session_create();
 	if (!conn->viewer_session) {
 		ERR("Allocation viewer session");
 		resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
 		goto send_reply;
 	}
-	CDS_INIT_LIST_HEAD(&conn->viewer_session->sessions_head);
 

send_reply:

	health_code_update();
src/bin/lttng-relayd/live.c +2 -6
@@ -1757,9 +1765,6 @@ int process_control(struct lttng_viewer_cmd *recv_hdr,
 	int ret = 0;
 	uint32_t msg_value;
 
-	assert(recv_hdr);
-	assert(conn);
-
 	msg_value = be32toh(recv_hdr->cmd);
 
 	/*
@@ -1798,7 +1803,8 @@ int process_control(struct lttng_viewer_cmd *recv_hdr,
 		ret = viewer_create_session(conn);
 		break;
 	default:
-		ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
+		ERR("Received unknown viewer command (%u)",
+			be32toh(recv_hdr->cmd));
 		live_relay_unknown_command(conn);
 		ret = -1;
 		goto end;
@@ -1813,8 +1819,6 @@ void cleanup_connection_pollfd(struct lttng_poll_event
 *events, int pollfd)
 {
 	int ret;
 
-	assert(events);
-
 	(void) lttng_poll_del(events, pollfd);
 
	ret = close(pollfd);
src/bin/lttng-relayd/live.c +0 -32
@@ -1824,38 +1828,6 @@ void cleanup_connection_pollfd(struct lttng_poll_event
 *events, int pollfd)
 }
 
 /*
- * Delete and destroy a connection.
- *
- * RCU read side lock MUST be acquired.
- */
-static void destroy_connection(struct lttng_ht *relay_connections_ht,
-		struct relay_connection *conn)
-{
-	struct relay_session *session, *tmp_session;
-
-	assert(relay_connections_ht);
-	assert(conn);
-
-	connection_delete(relay_connections_ht, conn);
-
-	if (!conn->viewer_session) {
-		goto end;
-	}
-
-	rcu_read_lock();
-	cds_list_for_each_entry_safe(session, tmp_session,
-			&conn->viewer_session->sessions_head,
-			viewer_session_list) {
-		DBG("Cleaning connection of session ID %" PRIu64, session->id);
-		cleanup_session(conn, session);
-	}
-	rcu_read_unlock();
-
-end:
-	connection_destroy(conn);
-}
-
-/*
 * This thread does the actual work
 */

static

src/bin/lttng-relayd/live.c +17 -19
@@ -1867,8 +1839,6 @@ void *thread_worker(void *data)
 	struct lttng_ht *relay_connections_ht;
 	struct lttng_ht_iter iter;
 	struct lttng_viewer_cmd recv_hdr;
-	struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
-	struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
 	struct relay_connection *destroy_conn;
 
 	DBG("[thread] Live viewer relay worker started");
@@ -1952,50 +1922,50 @@ restart:
 				} else if (revents & LPOLLIN) {
 					struct relay_connection *conn;
 
-					ret = lttng_read(live_conn_pipe[0], &conn, sizeof(conn));
+					ret = lttng_read(live_conn_pipe[0],
+						&conn, sizeof(conn));
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

error_poll_create:

src/bin/lttng-relayd/live.c +2 -3

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/live.h +5 -5
index 5db940b..2b8a3a0 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/lttng-relayd.h +10 -13
index 245c5fd..889071c 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/main.c +12 -6
index 8e1879f..46a4e55 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	fprintf(stderr, "  -h, --help                Display this usage.\n");
src/bin/lttng-relayd/main.c +5 -7

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

			ret = config_parse_value(entry->value);
src/bin/lttng-relayd/main.c +75 -177

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	case 2:
src/bin/lttng-relayd/main.c +16 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	} else {
src/bin/lttng-relayd/main.c +50 -110

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/main.c +1 -8

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

end_no_session:

	return ret;
src/bin/lttng-relayd/main.c +0 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

/*
* relay_close_stream: close a specific stream
*/

src/bin/lttng-relayd/main.c +1 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

struct relay_connection *conn)

{
	int ret, send_ret;
src/bin/lttng-relayd/main.c +25 -11

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	if (ret < 0) {
src/bin/lttng-relayd/main.c +4 -8

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

		ERR("Metadata sent before version check");
src/bin/lttng-relayd/main.c +14 -15

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

end:

	return ret;
}
src/bin/lttng-relayd/main.c +2 -5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/main.c +5 -5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

			last_net_seq_num);
src/bin/lttng-relayd/main.c +4 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	reply.ret_code = htobe32(ret);
src/bin/lttng-relayd/main.c +15 -19

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
src/bin/lttng-relayd/main.c +23 -20

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	if (!conn->session || conn->version_check_done == 0) {
src/bin/lttng-relayd/main.c +23 -7

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	rcu_read_unlock();
src/bin/lttng-relayd/main.c +41 -52

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	if (ret < 0) {
src/bin/lttng-relayd/main.c +4 -12

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	reply.ret_code = htobe32(LTTNG_OK);
src/bin/lttng-relayd/main.c +101 -126

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

end:

	return ret;
}
src/bin/lttng-relayd/main.c +1 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

pollfd)

{
	int ret;
src/bin/lttng-relayd/main.c +0 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	(void) lttng_poll_del(events, pollfd);

	ret = close(pollfd);
src/bin/lttng-relayd/main.c +22 -13

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	uint32_t nb_fd;
src/bin/lttng-relayd/main.c +22 -40

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

exit:
error:

src/bin/lttng-relayd/main.c +0 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	/* Cleanup reamaining connection object. */
	rcu_read_lock();
	cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter,
			destroy_conn,
			sock_n.node) {
		health_code_update();
src/bin/lttng-relayd/main.c +6 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
	rcu_read_unlock();
src/bin/lttng-relayd/main.c +3 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

error_poll_create:

src/bin/lttng-relayd/main.c +0 -10

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	lttng_ht_destroy(relay_connections_ht);
relay_connections_ht_error:
	/* Close relay conn pipes */
src/bin/lttng-relayd/main.c +4 -12

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

exit_health_app_create:
exit_options:

src/bin/lttng-relayd/main.c +4 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	if (!retval) {
		exit(EXIT_SUCCESS);
src/bin/lttng-relayd/session.c +26 -10
index 46d9cc6..de00fc3 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

error:

	return session;
}
src/bin/lttng-relayd/session.c +15 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

/*
- * Lookup a session within the given hash table and session id. RCU read side
- * lock MUST be acquired before calling this and as long as the caller has a
- * reference to the object.
+ * Lookup a session within the session hash table using the session id
+ * as key. A session reference is taken when a session is returned.
+ * session_put() must be called on that session.
*
* Return session or NULL if not found.
*/

src/bin/lttng-relayd/session.c +1 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{
	struct relay_session *session = NULL;
	struct lttng_ht_node_u64 *node;
	struct lttng_ht_iter iter;
src/bin/lttng-relayd/session.c +2 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	node = lttng_ht_iter_get_node_u64(&iter);
	if (!node) {
		DBG("Session find by ID %" PRIu64 " id NOT found", id);
src/bin/lttng-relayd/session.c +3 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

end:

src/bin/lttng-relayd/session.c +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	return session;
}
src/bin/lttng-relayd/session.c +8 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

/*
* Delete session from the given hash table.
*
* Return lttng ht del error code being 0 on success and 1 on failure. */

src/bin/lttng-relayd/session.c +1 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{
	struct lttng_ht_iter iter;
src/bin/lttng-relayd/session.c +0 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	iter.iter.node = &session->session_n.node;
src/bin/lttng-relayd/session.c +1 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

src/bin/lttng-relayd/session.c +17 -6

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{

src/bin/lttng-relayd/session.c +11 -6

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

src/bin/lttng-relayd/session.c +1 -8

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{
	int ret = 0;
src/bin/lttng-relayd/session.c +23 -11

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

		}
	}
src/bin/lttng-relayd/session.c +12 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

src/bin/lttng-relayd/session.c +1 -7

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{

src/bin/lttng-relayd/session.c +0 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	struct lttng_ht_iter iter;
src/bin/lttng-relayd/session.c +1 -8

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	rcu_read_lock();
src/bin/lttng-relayd/session.c +10 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
	rcu_read_unlock();
src/bin/lttng-relayd/session.c +0 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

src/bin/lttng-relayd/session.h +59 -57
index cb125be..0126e12 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/stream-fd.c new file +58 -0
new file mode 100644
index 0000000..57324d7

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/stream-fd.h new file +32 -0
new file mode 100644
index 0000000..64f3b16

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/stream.c +31 -11
index 17a5bcd..5525806 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

end:

src/bin/lttng-relayd/stream.c +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	return stream;
}

/*
- * Close a given stream. If an assosiated viewer stream exists it is updated.
- *
- * RCU read side lock MUST be acquired.
- *
- * Return 0 if close was successful or 1 if already closed.
+ * We keep ownership of path_name and channel_name.
*/

src/bin/lttng-relayd/stream.c +4 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{

src/bin/lttng-relayd/stream.c +27 -9

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/stream.c +19 -5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

		}
src/bin/lttng-relayd/stream.c +11 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/stream.c +26 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

		}
src/bin/lttng-relayd/stream.c +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/stream.c +21 -13

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/stream.c +12 -10

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	pthread_mutex_unlock(&stream->lock);
src/bin/lttng-relayd/stream.c +0 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

src/bin/lttng-relayd/stream.c +28 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{

src/bin/lttng-relayd/stream.c +11 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	int ret;
	struct lttng_ht_iter iter;
src/bin/lttng-relayd/stream.c +11 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	iter.iter.node = &stream->node.node;
src/bin/lttng-relayd/stream.c +1 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	assert(!ret);
src/bin/lttng-relayd/stream.c +16 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

src/bin/lttng-relayd/stream.c +1 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{

src/bin/lttng-relayd/stream.c +51 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

src/bin/lttng-relayd/stream.h +71 -59
index 4dd2e62..92a809f 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/utils.h +4 -3
index de1521d..4a56980 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/viewer-session.c new file +164 -0
new file mode 100644
index 0000000..ff2e41b

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/viewer-session.h new file +53 -0
new file mode 100644
index 0000000..4013b35

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/viewer-stream.c +46 -49
index 3748629..6ecb83e 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

error:

	if (vstream) {
src/bin/lttng-relayd/viewer-stream.c +1 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
	return NULL;
}
src/bin/lttng-relayd/viewer-stream.c +1 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{
	int ret;
	struct lttng_ht_iter iter;
src/bin/lttng-relayd/viewer-stream.c +1 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	ret = lttng_ht_del(viewer_streams_ht, &iter);
	assert(!ret);
}
src/bin/lttng-relayd/viewer-stream.c +1 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{

src/bin/lttng-relayd/viewer-stream.c +4 -5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/viewer-stream.c +5 -5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/viewer-stream.c +20 -5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/viewer-stream.c +2 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

/*
- * Find viewer stream by id. RCU read side lock MUST be acquired.
+ * Get viewer stream by id.
*
- * Return stream if found else NULL.
+ * Return viewer stream if found else NULL.
*/

src/bin/lttng-relayd/viewer-stream.c +1 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{
	struct lttng_ht_node_u64 *node;
	struct lttng_ht_iter iter;
src/bin/lttng-relayd/viewer-stream.c +2 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	lttng_ht_lookup(viewer_streams_ht, &id, &iter);
	node = lttng_ht_iter_get_node_u64(&iter);
	if (!node) {
		DBG("Relay viewer stream %" PRIu64 " not found", id);
		goto end;
	}
src/bin/lttng-relayd/viewer-stream.c +4 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

end:

src/bin/lttng-relayd/viewer-stream.c +19 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

*vstream,

src/bin/lttng-relayd/viewer-stream.c +23 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

}

/*
* Rotate a stream to the next tracefile.
*
- * Must be called with viewer_stream_rotation_lock held.
+ * Must be called with rstream and vstream locks held.
* Returns 0 on success, 1 on EOF, a negative value on error. */

src/bin/lttng-relayd/viewer-stream.c +1 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

{
	int ret;
src/bin/lttng-relayd/viewer-stream.c +2 -13

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	/* Detect the last tracefile to open. */
src/bin/lttng-relayd/viewer-stream.c +2 -3

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

		ret = 1;
		goto end;
	}
src/bin/lttng-relayd/viewer-stream.c +4 -15

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/viewer-stream.c +9 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/viewer-stream.c +8 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/viewer-stream.c +0 -5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	ret = index_open(vstream->path_name, vstream->channel_name,
src/bin/lttng-relayd/viewer-stream.c +2 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	if (ret < 0) {
src/bin/lttng-relayd/viewer-stream.c +10 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/bin/lttng-relayd/viewer-stream.c +0 -4

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

end:
-error:

	return ret;
}
src/bin/lttng-relayd/viewer-stream.c +23 -0

Lines of this file are not shown. View the raw message.

src/bin/lttng-relayd/viewer-stream.h +38 -45
index 003b119..a19ffe0 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

#endif /* _VIEWER_STREAM_H */

src/bin/lttng-sessiond/consumer.c +2 -5
index 87d5f34..0c5ffff 100644

Lines of this file are not shown. View the raw message.

src/bin/lttng-sessiond/ust-consumer.c +1 -1
index bacbf92..4eaf341 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

error:

src/bin/lttng-sessiond/ust-consumer.c +0 -1

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	return ret;
}
src/common/index/ctf-index.h +1 -1
index 0efa888..1f38d9a 100644

Lines of this file are not shown. View the raw message.

src/common/index/index.c +13 -0
index 35cff53..3fce35d 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	if (ret < 0) {
src/common/utils.c +8 -11
index 766f224..7681367 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	if (ret < 0) {
src/common/utils.c +30 -2

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	flags = O_WRONLY | O_CREAT | O_TRUNC;
src/common/utils.c +1 -5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

error:

	return ret;
}

/*
+ * Unlink the stream tracefile from disk.
+ *
+ * Return 0 on success or else a negative value.
+ */

src/common/utils.c +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

size,

src/common/utils.c +46 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

	}
src/common/utils.h +0 -0
index 05914cc..8bc5a3f 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size,

		uint64_t count, int uid, int gid, char *suffix);

+int utils_unlink_stream_file(const char *path_name, char *file_name, uint64_t size,

src/common/utils.h +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,

		uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count,
		int *stream_fd);

--
2.1.4

Signature · 8 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

Part of the diff is not shown here. View the raw message.