Skip to contentSkip to conversation

Fix: grab more than one packet for snapshots

PATCH New Mathieu Desnoyers 2 participants 2 messages Jan 15, 2015 – Jan 22, 2015 1 message 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 2/2 Fix: grab more than one packet for snapshots

There are a few issues with snapshot size: when taking a snapshot without specifying any "max size" (should be unlimited), only a single packet from each stream is saved. We expect all stream available content to be saved. There is a similar issue when a max size is specified.

Also, trying to make all streams save as much data has unexpected
corner-cases: for instance, if we have this configuration:
- kernel channels: 2 subbuffers of 1MB x 8 CPUs
- per-PID UST channels: 16 subbuffers of 4kB x 8 CPUs x 100 apps

would require the user to have a very large max size, since it would try to fit (8 + (100 * 8)) * 1MB = 808MB of sub-buffers, else it would fail. This issue here is using the largest subbuffer size as the criterion applied to all channels.

We fix those issues by simplifying the algorithm used to calculate how much data to grab. Rather than calculating the size to grab from each stream, we calculate a number of packets to grab. It fails if we cannot grab at least one packet from each stream in the session. Then checks if it can grab 2 packets from each stream, and so on, until there is no more space available (based on max size). This is not a perfect solution, but has the merit of being simple to understand, and has no (or few) unexpected corner-cases.

Fixes #860

---
 doc/man/lttng.1                              |   4 -
 src/bin/lttng-sessiond/buffer-registry.h     |   2 +
 src/bin/lttng-sessiond/cmd.c                 | 179 ++++++++++++++-------------
 src/bin/lttng-sessiond/consumer.c            |   4 +-
 src/bin/lttng-sessiond/consumer.h            |   2 +-
 src/bin/lttng-sessiond/kernel.c              |   7 +-
 src/bin/lttng-sessiond/kernel.h              |   3 +-
 src/bin/lttng-sessiond/snapshot.c            |   5 +-
 src/bin/lttng-sessiond/ust-app.c             |  40 ++++--
 src/bin/lttng-sessiond/ust-app.h             |   6 +-
 src/bin/lttng/commands/snapshot.c            |   4 +-
 src/common/align.h                           |   2 +-
 src/common/consumer.c                        |  30 +++--
 src/common/consumer.h                        |   5 +-
 src/common/kernel-consumer/kernel-consumer.c |  15 +--
 src/common/sessiond-comm/sessiond-comm.h     |   2 +-
 src/common/ust-consumer/ust-consumer.c       |  11 +-
 17 files changed, 170 insertions(+), 151 deletions(-)
doc/man/lttng.1 +0 -4
index dda9095..24dcf88 100644
@@ -935,10 +935,6 @@ Name of the snapshot's output.
 Maximum size in bytes of the snapshot. The maximum size does not include the
 metadata file. Human readable format is accepted: {+k,+M,+G}. For instance,
 \-\-max-size 5M
-
-The minimum size of a snapshot is computed by multiplying the total amount of
-streams in the session by the largest subbuffer size. This is to ensure
-fairness between channels when extracting data.
 .TP
 .BR "\-C, \-\-ctrl-url URL"
 Set control path URL. (Must use -D also)
src/bin/lttng-sessiond/buffer-registry.h +2 -0
index c2099b6..656fc91 100644
@@ -51,6 +51,8 @@ struct buffer_reg_channel {
 	struct lttng_ht_node_u64 node;
 	/* Size of subbuffers in this channel. */
 	size_t subbuf_size;
+	/* Number of subbuffers per stream. */
+	size_t num_subbuf;
 	union {
 		/* Original object data that MUST be copied over. */
 		struct lttng_ust_object_data *ust;
src/bin/lttng-sessiond/cmd.c +93 -86

304 lines not on this page · raw message

src/bin/lttng-sessiond/consumer.c +2 -2
index 42738bc..9b8a0a7 100644
@@ -1277,7 +1277,7 @@ end:
  */
 int consumer_snapshot_channel(struct consumer_socket *socket, uint64_t key,
 		struct snapshot_output *output, int metadata, uid_t uid, gid_t gid,
-		const char *session_path, int wait, int max_stream_size)
+		const char *session_path, int wait, uint64_t nb_packets_per_stream)
 {
 	int ret;
 	struct lttcomm_consumer_msg msg;
@@ -1291,7 +1291,7 @@ int consumer_snapshot_channel(struct consumer_socket *socket, uint64_t key,
 	memset(&msg, 0, sizeof(msg));
 	msg.cmd_type = LTTNG_CONSUMER_SNAPSHOT_CHANNEL;
 	msg.u.snapshot_channel.key = key;
-	msg.u.snapshot_channel.max_stream_size = max_stream_size;
+	msg.u.snapshot_channel.nb_packets_per_stream = nb_packets_per_stream;
 	msg.u.snapshot_channel.metadata = metadata;
 
 	if (output->consumer->type == CONSUMER_DST_NET) {
src/bin/lttng-sessiond/consumer.h +1 -1
index 3601ed9..a9266d5 100644
@@ -279,6 +279,6 @@ int consumer_flush_channel(struct consumer_socket *socket, uint64_t key);
 /* Snapshot command. */
 int consumer_snapshot_channel(struct consumer_socket *socket, uint64_t key,
 		struct snapshot_output *output, int metadata, uid_t uid, gid_t gid,
-		const char *session_path, int wait, int max_size_per_stream);
+		const char *session_path, int wait, uint64_t nb_packets_per_stream);
 
 #endif /* _CONSUMER_H */
src/bin/lttng-sessiond/kernel.c +4 -3
index 6b404c0..9464a84 100644
@@ -853,7 +853,8 @@ void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
  * Return 0 on success or else return a LTTNG_ERR code.
  */
 int kernel_snapshot_record(struct ltt_kernel_session *ksess,
-		struct snapshot_output *output, int wait, uint64_t max_size_per_stream)
+		struct snapshot_output *output, int wait,
+		uint64_t nb_packets_per_stream)
 {
 	int err, ret, saved_metadata_fd;
 	struct consumer_socket *socket;
@@ -914,7 +915,7 @@ int kernel_snapshot_record(struct ltt_kernel_session *ksess,
 			ret = consumer_snapshot_channel(socket, chan->fd, output, 0,
 					ksess->uid, ksess->gid,
 					DEFAULT_KERNEL_TRACE_DIR, wait,
-					max_size_per_stream);
+					nb_packets_per_stream);
 			pthread_mutex_unlock(socket->lock);
 			if (ret < 0) {
 				ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
@@ -928,7 +929,7 @@ int kernel_snapshot_record(struct ltt_kernel_session *ksess,
 		pthread_mutex_lock(socket->lock);
 		ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output,
 				1, ksess->uid, ksess->gid,
-				DEFAULT_KERNEL_TRACE_DIR, wait, max_size_per_stream);
+				DEFAULT_KERNEL_TRACE_DIR, wait, 0);
 		pthread_mutex_unlock(socket->lock);
 		if (ret < 0) {
 			ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
src/bin/lttng-sessiond/kernel.h +2 -1
index d6332e2..09c3ed9 100644
@@ -59,7 +59,8 @@ int kernel_validate_version(int tracer_fd);
 void kernel_destroy_session(struct ltt_kernel_session *ksess);
 void kernel_destroy_channel(struct ltt_kernel_channel *kchan);
 int kernel_snapshot_record(struct ltt_kernel_session *ksess,
-		struct snapshot_output *output, int wait, uint64_t max_stream_size);
+		struct snapshot_output *output, int wait,
+		uint64_t nb_packets_per_stream);
 int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits);
 
 int init_kernel_workarounds(void);
src/bin/lttng-sessiond/snapshot.c +3 -2
index 1060269..f9366b3 100644
@@ -47,10 +47,11 @@ static int output_init(uint64_t max_size, const char *name,
 {
 	int ret = 0, i;
 
-	assert(output);
-
 	memset(output, 0, sizeof(struct snapshot_output));
 
+	/*
+	 * max_size of -1ULL means unset. Set to default (unlimited).
+	 */
 	if (max_size == (uint64_t) -1ULL) {
 		max_size = 0;
 	}
src/bin/lttng-sessiond/ust-app.c +28 -12
index 0c4045c..41a6d39 100644
@@ -2395,6 +2395,7 @@ static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
 	assert(reg_chan);
 	reg_chan->consumer_key = ua_chan->key;
 	reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
+	reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
 
 	/* Create and add a channel registry to session. */
 	ret = ust_registry_channel_add(reg_sess->reg.ust,
@@ -5083,7 +5084,8 @@ void ust_app_destroy(struct ust_app *app)
  * Return 0 on success or else a negative value.
  */
 int ust_app_snapshot_record(struct ltt_ust_session *usess,
-		struct snapshot_output *output, int wait, uint64_t max_stream_size)
+		struct snapshot_output *output, int wait,
+		uint64_t nb_packets_per_stream)
 {
 	int ret = 0;
 	unsigned int snapshot_done = 0;
@@ -5127,14 +5129,14 @@ int ust_app_snapshot_record(struct ltt_ust_session *usess,
 					reg_chan, node.node) {
 				ret = consumer_snapshot_channel(socket, reg_chan->consumer_key,
 						output, 0, usess->uid, usess->gid, pathname, wait,
-						max_stream_size);
+						nb_packets_per_stream);
 				if (ret < 0) {
 					goto error;
 				}
 			}
 			ret = consumer_snapshot_channel(socket,
 					reg->registry->reg.ust->metadata_key, output, 1,
-					usess->uid, usess->gid, pathname, wait, max_stream_size);
+					usess->uid, usess->gid, pathname, wait, 0);
 			if (ret < 0) {
 				goto error;
 			}
@@ -5178,7 +5180,7 @@ int ust_app_snapshot_record(struct ltt_ust_session *usess,
 					ua_chan, node.node) {
 				ret = consumer_snapshot_channel(socket, ua_chan->key, output,
 						0, ua_sess->euid, ua_sess->egid, pathname, wait,
-						max_stream_size);
+						nb_packets_per_stream);
 				if (ret < 0) {
 					goto error;
 				}
@@ -5187,8 +5189,7 @@ int ust_app_snapshot_record(struct ltt_ust_session *usess,
 			registry = get_session_registry(ua_sess);
 			assert(registry);
 			ret = consumer_snapshot_channel(socket, registry->metadata_key, output,
-					1, ua_sess->euid, ua_sess->egid, pathname, wait,
-					max_stream_size);
+					1, ua_sess->euid, ua_sess->egid, pathname, wait, 0);
 			if (ret < 0) {
 				goto error;
 			}
@@ -5216,11 +5217,12 @@ error:
 }
 
 /*
- * Return the number of streams for a UST session.
+ * Return the size taken by one more packet per stream.
  */
-unsigned int ust_app_get_nb_stream(struct ltt_ust_session *usess)
+uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session *usess,
+		uint64_t cur_nr_packets)
 {
-	unsigned int ret = 0;
+	uint64_t tot_size = 0;
 	struct ust_app *app;
 	struct lttng_ht_iter iter;
 
@@ -5237,7 +5239,14 @@ unsigned int ust_app_get_nb_stream(struct ltt_ust_session *usess)
 			rcu_read_lock();
 			cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
 					reg_chan, node.node) {
-				ret += reg_chan->stream_count;
+				if (cur_nr_packets >= reg_chan->num_subbuf) {
+					/*
+					 * Don't take channel into account if we
+					 * already grab all its packets.
+					 */
+					continue;
+				}
+				tot_size += reg_chan->subbuf_size * reg_chan->stream_count;
 			}
 			rcu_read_unlock();
 		}
@@ -5259,7 +5268,14 @@ unsigned int ust_app_get_nb_stream(struct ltt_ust_session *usess)
 
 			cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
 					ua_chan, node.node) {
-				ret += ua_chan->streams.count;
+				if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
+					/*
+					 * Don't take channel into account if we
+					 * already grab all its packets.
+					 */
+					continue;
+				}
+				tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
 			}
 		}
 		rcu_read_unlock();
@@ -5270,5 +5286,5 @@ unsigned int ust_app_get_nb_stream(struct ltt_ust_session *usess)
 		break;
 	}
 
-	return ret;
+	return tot_size;
 }
src/bin/lttng-sessiond/ust-app.h +4 -2
index dc63677..7a35ef2 100644
@@ -326,8 +326,10 @@ ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
 		struct consumer_socket *socket, int send_zero_data);
 void ust_app_destroy(struct ust_app *app);
 int ust_app_snapshot_record(struct ltt_ust_session *usess,
-		struct snapshot_output *output, int wait, uint64_t max_stream_size);
-unsigned int ust_app_get_nb_stream(struct ltt_ust_session *usess);
+		struct snapshot_output *output, int wait,
+		uint64_t nb_packets_per_stream);
+uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session *usess,
+		uint64_t cur_nr_packets);
 struct ust_app *ust_app_find_by_sock(int sock);
 
 static inline
src/bin/lttng/commands/snapshot.c +1 -3
index 4b38edf..846e9ba 100644
@@ -568,9 +568,7 @@ static int record(const char *url)
 	ret = lttng_snapshot_record(current_session_name, output, 0);
 	if (ret < 0) {
 		if (ret == -LTTNG_ERR_MAX_SIZE_INVALID) {
-			ERR("The minimum size of a snapshot is computed by multiplying "
-					"the total amount of streams with the largest subbuffer "
-					"in the session.");
+			ERR("Invalid snapshot size. Cannot fit at least one packet per stream.");
 		}
 		goto error;
 	}
src/common/align.h +1 -1
index fe32673..928c5b6 100644
@@ -58,7 +58,7 @@
 	({								       \
 		LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0		       \
 				   || ((alignment) & ((alignment) - 1)));      \
-		(((align_drift) - (alignment)) & ((alignment) - 1);	       \
+		(((align_drift) - (alignment)) & ((alignment) - 1));	       \
 	})
 
 #endif /* _LTTNG_ALIGN_H */
src/common/consumer.c +14 -16
index b0b926b..0af994f 100644
@@ -48,6 +48,7 @@
 #include "consumer.h"
 #include "consumer-stream.h"
 #include "consumer-testpoint.h"
+#include "align.h"
 
 struct lttng_consumer_global_data consumer_data = {
 	.stream_count = 0,
@@ -3651,22 +3652,19 @@ int consumer_send_status_channel(int sock,
 	return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
 }
 
-/*
- * Using a maximum stream size with the produced and consumed position of a
- * stream, computes the new consumed position to be as close as possible to the
- * maximum possible stream size.
- *
- * If maximum stream size is lower than the possible buffer size (produced -
- * consumed), the consumed_pos given is returned untouched else the new value
- * is returned.
- */
-unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos,
-		unsigned long produced_pos, uint64_t max_stream_size)
+unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos,
+		unsigned long produced_pos, uint64_t nb_packets_per_stream,
+		uint64_t max_sb_size)
 {
-	if (max_stream_size && max_stream_size < (produced_pos - consumed_pos)) {
-		/* Offset from the produced position to get the latest buffers. */
-		return produced_pos - max_stream_size;
-	}
+	unsigned long start_pos;
 
-	return consumed_pos;
+	if (!nb_packets_per_stream) {
+		return consumed_pos;	/* Grab everything */
+	}
+	start_pos = produced_pos - offset_align_floor(produced_pos, max_sb_size);
+	start_pos -= max_sb_size * nb_packets_per_stream;
+	if ((long) (start_pos - consumed_pos) < 0) {
+		return consumed_pos;	/* Grab everything */
+	}
+	return start_pos;
 }
src/common/consumer.h +3 -2
index 1e378f0..790cb6b 100644
@@ -666,8 +666,9 @@ int consumer_send_status_channel(int sock,
 void notify_thread_del_channel(struct lttng_consumer_local_data *ctx,
 		uint64_t key);
 void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd);
-unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos,
-		unsigned long produced_pos, uint64_t max_stream_size);
+unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos,
+		unsigned long produced_pos, uint64_t nb_packets_per_stream,
+		uint64_t max_sb_size);
 int consumer_add_data_stream(struct lttng_consumer_stream *stream);
 void consumer_del_stream_for_data(struct lttng_consumer_stream *stream);
 int consumer_add_metadata_stream(struct lttng_consumer_stream *stream);
src/common/kernel-consumer/kernel-consumer.c +5 -10
index 161293d..7622a22 100644
@@ -115,7 +115,7 @@ int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
  * Returns 0 on success, < 0 on error
  */
 int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
-		uint64_t relayd_id, uint64_t max_stream_size,
+		uint64_t relayd_id, uint64_t nb_packets_per_stream,
 		struct lttng_consumer_local_data *ctx)
 {
 	int ret;
@@ -221,14 +221,9 @@ int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
 			}
 		}
 
-		/*
-		 * The original value is sent back if max stream size is larger than
-		 * the possible size of the snapshot. Also, we asume that the session
-		 * daemon should never send a maximum stream size that is lower than
-		 * subbuffer size.
-		 */
-		consumed_pos = consumer_get_consumed_maxsize(consumed_pos,
-				produced_pos, max_stream_size);
+		consumed_pos = consumer_get_consume_start_pos(consumed_pos,
+				produced_pos, nb_packets_per_stream,
+				stream->max_sb_size);
 
 		while (consumed_pos < produced_pos) {
 			ssize_t read_len;
@@ -886,7 +881,7 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
 			ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
 					msg.u.snapshot_channel.pathname,
 					msg.u.snapshot_channel.relayd_id,
-					msg.u.snapshot_channel.max_stream_size,
+					msg.u.snapshot_channel.nb_packets_per_stream,
 					ctx);
 			if (ret < 0) {
 				ERR("Snapshot channel failed");
src/common/sessiond-comm/sessiond-comm.h +1 -1
index 9842214..1426fcf 100644
@@ -452,7 +452,7 @@ struct lttcomm_consumer_msg {
 			uint32_t metadata;		/* This a metadata snapshot. */
 			uint64_t relayd_id;		/* Relayd id if apply. */
 			uint64_t key;
-			uint64_t max_stream_size;
+			uint64_t nb_packets_per_stream;
 		} LTTNG_PACKED snapshot_channel;
 		struct {
 			uint64_t channel_key;
src/common/ust-consumer/ust-consumer.c +6 -5
index 38cdf70..bf0208f 100644
@@ -860,7 +860,7 @@ error:
  * Returns 0 on success, < 0 on error
  */
 static int snapshot_channel(uint64_t key, char *path, uint64_t relayd_id,
-		uint64_t max_stream_size, struct lttng_consumer_local_data *ctx)
+		uint64_t nb_packets_per_stream, struct lttng_consumer_local_data *ctx)
 {
 	int ret;
 	unsigned use_relayd = 0;
@@ -942,12 +942,13 @@ static int snapshot_channel(uint64_t key, char *path, uint64_t relayd_id,
 
 		/*
 		 * The original value is sent back if max stream size is larger than
-		 * the possible size of the snapshot. Also, we asume that the session
+		 * the possible size of the snapshot. Also, we assume that the session
 		 * daemon should never send a maximum stream size that is lower than
 		 * subbuffer size.
 		 */
-		consumed_pos = consumer_get_consumed_maxsize(consumed_pos,
-				produced_pos, max_stream_size);
+		consumed_pos = consumer_get_consume_start_pos(consumed_pos,
+				produced_pos, nb_packets_per_stream,
+				stream->max_sb_size);
 
 		while (consumed_pos < produced_pos) {
 			ssize_t read_len;
@@ -1490,7 +1491,7 @@ int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
 			ret = snapshot_channel(msg.u.snapshot_channel.key,
 					msg.u.snapshot_channel.pathname,
 					msg.u.snapshot_channel.relayd_id,
-					msg.u.snapshot_channel.max_stream_size,
+					msg.u.snapshot_channel.nb_packets_per_stream,
 					ctx);
 			if (ret < 0) {
 				ERR("Snapshot channel failed");
Signature · 1 line
2.1.1
#2 lore raw

Re: [PATCH lttng-tools 1/2] Fix: per-uid flush and ust registry locking

In reply to a message not in the archive

Both patches were merged in master, 2.6, 2.5 and 2.4.

Thanks!
Jérémie

Quoted 459 lines · Mathieu Desnoyers

Commit c4b88406 "Fix: ust-app: per-PID app unregister vs tracing stop races" introduces a regression for per-UID flush. It can be triggered by the test_high_throughput_limits (root regression) test. For per-UID tracing, we need to use the registry channel ID, not the per-application channel ID, when asking the consumer daemon to flush.

When doing this fix, we notice that the locking rules of push_metadata() are weird. A per-ust app session lock is protecting registry data, which makes it impossible to call push_metadata from a ust session level (for the entire session) in the case of per-UID tracing. Moreover, it's unclear how holding a per-application lock can protect a registry shared across applications in per-UID tracing. Therefore, we move all accesses to the registry metadata_key and metadata_closed fields into the registry lock critical section. We now only rely on RCU to ensure existance of registry across push_metadata(), rather than relying on the per-application session lock.

It also takes care of a documentation vs code mismatch: push_metadata() documents that "The session lock MUST be acquired here before calling this.", but in reality, it's the application session lock which is held across those calls. Removing this requirement, and relying on RCU instead, fixes this mismatch.

---
 src/bin/lttng-sessiond/ust-app.c | 243 +++++++++++++++++++++------------------
 1 file changed, 132 insertions(+), 111 deletions(-)
src/bin/lttng-sessiond/ust-app.c +132 -112
index dcb6e7c..0c4045c 100644
@@ -442,21 +442,28 @@ ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
        assert(registry);
        assert(socket);
 
+       pthread_mutex_lock(&registry->lock);
+
+       /*
+        * Means that no metadata was assigned to the session. This can happens if
+        * no start has been done previously.
+        */
+       if (!registry->metadata_key) {
+               pthread_mutex_unlock(&registry->lock);
+               return 0;
+       }
+
        /*
         * On a push metadata error either the consumer is dead or the metadata
         * channel has been destroyed because its endpoint might have died (e.g:
         * relayd). If so, the metadata closed flag is set to 1 so we deny pushing
         * metadata again which is not valid anymore on the consumer side.
-        *
-        * The ust app session mutex locked allows us to make this check without
-        * the registry lock.
         */
        if (registry->metadata_closed) {
+               pthread_mutex_unlock(&registry->lock);
                return -EPIPE;
        }
 
-       pthread_mutex_lock(&registry->lock);
-
        offset = registry->metadata_len_sent;
        len = registry->metadata_len - registry->metadata_len_sent;
        if (len == 0) {
@@ -514,6 +521,14 @@ push_data:
 
 end:
 error:
+       if (ret_val) {
+               /*
+                * On error, flag the registry that the metadata is closed. We were unable
+                * to push anything and this means that either the consumer is not
+                * responding or the metadata cache has been destroyed on the consumer.
+                */
+               registry->metadata_closed = 1;
+       }
        pthread_mutex_unlock(&registry->lock);
 error_push:
        free(metadata_str);
@@ -521,11 +536,12 @@ error_push:
 }
 
 /*
- * For a given application and session, push metadata to consumer. The session
- * lock MUST be acquired here before calling this.
+ * For a given application and session, push metadata to consumer.
  * Either sock or consumer is required : if sock is NULL, the default
  * socket to send the metadata is retrieved from consumer, if sock
  * is not NULL we use it to send the metadata.
+ * RCU read-side lock must be held while calling this function,
+ * therefore ensuring existance of registry.
  *
  * Return 0 on success else a negative error.
  */
@@ -539,23 +555,20 @@ static int push_metadata(struct ust_registry_session *registry,
        assert(registry);
        assert(consumer);
 
-       rcu_read_lock();
+       pthread_mutex_lock(&registry->lock);
 
-       /*
-        * Means that no metadata was assigned to the session. This can happens if
-        * no start has been done previously.
-        */
-       if (!registry->metadata_key) {
-               ret_val = 0;
-               goto end_rcu_unlock;
+       if (registry->metadata_closed) {
+               pthread_mutex_unlock(&registry->lock);
+               return -EPIPE;
        }
 
        /* Get consumer socket to use to push the metadata.*/
        socket = consumer_find_socket_by_bitness(registry->bits_per_long,
                        consumer);
+       pthread_mutex_unlock(&registry->lock);
        if (!socket) {
                ret_val = -1;
-               goto error_rcu_unlock;
+               goto error;
        }
 
        /*
@@ -573,21 +586,13 @@ static int push_metadata(struct ust_registry_session *registry,
        pthread_mutex_unlock(socket->lock);
        if (ret < 0) {
                ret_val = ret;
-               goto error_rcu_unlock;
+               goto error;
        }
 
-       rcu_read_unlock();
        return 0;
 
-error_rcu_unlock:
-       /*
-        * On error, flag the registry that the metadata is closed. We were unable
-        * to push anything and this means that either the consumer is not
-        * responding or the metadata cache has been destroyed on the consumer.
-        */
-       registry->metadata_closed = 1;
-end_rcu_unlock:
-       rcu_read_unlock();
+error:
+end:
        return ret_val;
 }
 
@@ -610,6 +615,8 @@ static int close_metadata(struct ust_registry_session *registry,
 
        rcu_read_lock();
 
+       pthread_mutex_lock(&registry->lock);
+
        if (!registry->metadata_key || registry->metadata_closed) {
                ret = 0;
                goto end;
@@ -636,6 +643,7 @@ error:
         */
        registry->metadata_closed = 1;
 end:
+       pthread_mutex_unlock(&registry->lock);
        rcu_read_unlock();
        return ret;
 }
@@ -674,7 +682,7 @@ void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
        pthread_mutex_lock(&ua_sess->lock);
 
        registry = get_session_registry(ua_sess);
-       if (registry && !registry->metadata_closed) {
+       if (registry) {
                /* Push metadata for application before freeing the application. */
                (void) push_metadata(registry, ua_sess->consumer);
 
@@ -684,8 +692,7 @@ void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
                 * previous push metadata could have flag the metadata registry to
                 * close so don't send a close command if closed.
                 */
-               if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID &&
-                               !registry->metadata_closed) {
+               if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
                        /* And ask to close it for this session registry. */
                        (void) close_metadata(registry, ua_sess->consumer);
                }
@@ -2841,6 +2848,8 @@ static int create_ust_app_metadata(struct ust_app_session *ua_sess,
        registry = get_session_registry(ua_sess);
        assert(registry);
 
+       pthread_mutex_lock(&registry->lock);
+
        /* Metadata already exists for this registry or it was closed previously */
        if (registry->metadata_key || registry->metadata_closed) {
                ret = 0;
@@ -2913,6 +2922,7 @@ error_consumer:
        lttng_fd_put(LTTNG_FD_APPS, 1);
        delete_ust_app_channel(-1, metadata, app);
 error:
+       pthread_mutex_unlock(&registry->lock);
        return ret;
 }
 
@@ -3098,9 +3108,9 @@ void ust_app_unregister(int sock)
        DBG("PID %d unregistering with sock %d", lta->pid, sock);
 
        /*
-        * Perform "push metadata" and flush all application streams
-        * before removing app from hash tables, ensuring proper
-        * behavior of data_pending check.
+        * For per-PID buffers, perform "push metadata" and flush all
+        * application streams before removing app from hash tables,
+        * ensuring proper behavior of data_pending check.
         * Remove sessions so they are not visible during deletion.
         */
        cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
@@ -3113,7 +3123,9 @@ void ust_app_unregister(int sock)
                        continue;
                }
 
-               (void) ust_app_flush_app_session(lta, ua_sess);
+               if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
+                       (void) ust_app_flush_app_session(lta, ua_sess);
+               }
 
                /*
                 * Add session to list for teardown. This is safe since at this point we
@@ -3133,7 +3145,7 @@ void ust_app_unregister(int sock)
                 * session so the delete session will NOT push/close a second time.
                 */
                registry = get_session_registry(ua_sess);
-               if (registry && !registry->metadata_closed) {
+               if (registry) {
                        /* Push metadata for application before freeing the application. */
                        (void) push_metadata(registry, ua_sess->consumer);
 
@@ -3143,8 +3155,7 @@ void ust_app_unregister(int sock)
                         * previous push metadata could have flag the metadata registry to
                         * close so don't send a close command if closed.
                         */
-                       if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID &&
-                                       !registry->metadata_closed) {
+                       if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
                                /* And ask to close it for this session registry. */
                                (void) close_metadata(registry, ua_sess->consumer);
                        }
@@ -4039,10 +4050,8 @@ int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
        registry = get_session_registry(ua_sess);
        assert(registry);
 
-       if (!registry->metadata_closed) {
-               /* Push metadata for application before freeing the application. */
-               (void) push_metadata(registry, ua_sess->consumer);
-       }
+       /* Push metadata for application before freeing the application. */
+       (void) push_metadata(registry, ua_sess->consumer);
 
 end_unlock:
        pthread_mutex_unlock(&ua_sess->lock);
@@ -4082,21 +4091,32 @@ int ust_app_flush_app_session(struct ust_app *app,
        /* Flushing buffers */
        socket = consumer_find_socket_by_bitness(app->bits_per_long,
                        ua_sess->consumer);
-       cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
-                       node.node) {
-               health_code_update();
-               assert(ua_chan->is_sent);
-               ret = consumer_flush_channel(socket, ua_chan->key);
-               if (ret) {
-                       ERR("Error flushing consumer channel");
-                       retval = -1;
-                       continue;
+
+       /* Flush buffers and push metadata. */
+       switch (ua_sess->buffer_type) {
+       case LTTNG_BUFFER_PER_PID:
+               cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
+                               node.node) {
+                       health_code_update();
+                       assert(ua_chan->is_sent);
+                       ret = consumer_flush_channel(socket, ua_chan->key);
+                       if (ret) {
+                               ERR("Error flushing consumer channel");
+                               retval = -1;
+                               continue;
+                       }
                }
+               break;
+       case LTTNG_BUFFER_PER_UID:
+       default:
+               assert(0);
+               break;
        }
 
        health_code_update();
 
        pthread_mutex_unlock(&ua_sess->lock);
+
 end_not_compatible:
        rcu_read_unlock();
        health_code_update();
@@ -4104,25 +4124,76 @@ end_not_compatible:
 }
 
 /*
- * Flush buffers for a specific UST session and app.
+ * Flush buffers for all applications for a specific UST session.
+ * Called with UST session lock held.
  */
 static
-int ust_app_flush_session(struct ust_app *app, struct ltt_ust_session *usess)
+int ust_app_flush_session(struct ltt_ust_session *usess)
 
 {
        int ret;
-       struct ust_app_session *ua_sess;
 
-       DBG("Flushing session buffers for ust app pid %d", app->pid);
+       DBG("Flushing session buffers for all ust apps");
 
        rcu_read_lock();
 
-       ua_sess = lookup_session_by_app(usess, app);
-       if (ua_sess == NULL) {
-               ret = -1;
-               goto end_no_session;
+       /* Flush buffers and push metadata. */
+       switch (usess->buffer_type) {
+       case LTTNG_BUFFER_PER_UID:
+       {
+               struct buffer_reg_uid *reg;
+               struct lttng_ht_iter iter;
+
+               /* Flush all per UID buffers associated to that session. */
+               cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
+                       struct ust_registry_session *ust_session_reg;
+                       struct buffer_reg_channel *reg_chan;
+                       struct consumer_socket *socket;
+
+                       /* Get consumer socket to use to push the metadata.*/
+                       socket = consumer_find_socket_by_bitness(reg->bits_per_long,
+                                       usess->consumer);
+                       if (!socket) {
+                               /* Ignore request if no consumer is found for the session. */
+                               continue;
+                       }
+
+                       cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
+                                       reg_chan, node.node) {
+                               /*
+                                * The following call will print error values so the return
+                                * code is of little importance because whatever happens, we
+                                * have to try them all.
+                                */
+                               (void) consumer_flush_channel(socket, reg_chan->consumer_key);
+                       }
+
+                       ust_session_reg = reg->registry->reg.ust;
+                       /* Push metadata. */
+                       (void) push_metadata(ust_session_reg, usess->consumer);
+               }
+               ret = 0;
+               break;
+       }
+       case LTTNG_BUFFER_PER_PID:
+       {
+               struct ust_app_session *ua_sess;
+               struct lttng_ht_iter iter;
+               struct ust_app *app;
+
+               cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
+                       ua_sess = lookup_session_by_app(usess, app);
+                       if (ua_sess == NULL) {
+                               continue;
+                       }
+                       (void) ust_app_flush_app_session(app, ua_sess);
+               }
+               break;
+       }
+       default:
+               assert(0);
+               break;
        }
-       ret = ust_app_flush_app_session(app, ua_sess);
 
 end_no_session:
        rcu_read_unlock();
@@ -4201,6 +4272,7 @@ int ust_app_start_trace_all(struct ltt_ust_session *usess)
 
 /*
  * Start tracing for the UST session.
+ * Called with UST session lock held.
  */
 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
 {
@@ -4220,58 +4292,7 @@ int ust_app_stop_trace_all(struct ltt_ust_session *usess)
                }
        }
 
-       /* Flush buffers and push metadata (for UID buffers). */
-       switch (usess->buffer_type) {
-       case LTTNG_BUFFER_PER_UID:
-       {
-               struct buffer_reg_uid *reg;
-
-               /* Flush all per UID buffers associated to that session. */
-               cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
-                       struct ust_registry_session *ust_session_reg;
-                       struct buffer_reg_channel *reg_chan;
-                       struct consumer_socket *socket;
-
-                       /* Get consumer socket to use to push the metadata.*/
-                       socket = consumer_find_socket_by_bitness(reg->bits_per_long,
-                                       usess->consumer);
-                       if (!socket) {
-                               /* Ignore request if no consumer is found for the session. */
-                               continue;
-                       }
-
-                       cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
-                                       reg_chan, node.node) {
-                               /*
-                                * The following call will print error values so the return
-                                * code is of little importance because whatever happens, we
-                                * have to try them all.
-                                */
-                               (void) consumer_flush_channel(socket, reg_chan->consumer_key);
-                       }
-
-                       ust_session_reg = reg->registry->reg.ust;
-                       if (!ust_session_reg->metadata_closed) {
-                               /* Push metadata. */
-                               (void) push_metadata(ust_session_reg, usess->consumer);
-                       }
-               }
-
-               break;
-       }
-       case LTTNG_BUFFER_PER_PID:
-               cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
-                       ret = ust_app_flush_session(app, usess);
-                       if (ret < 0) {
-                               /* Continue to next apps even on error */
-                               continue;
-                       }
-               }
-               break;
-       default:
-               assert(0);
-               break;
-       }
+       (void) ust_app_flush_session(usess);
 
        rcu_read_unlock();
 
--

2.1.1

Signature · 8 lines
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

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