[ltt-dev] [UST PATCH] Separate create trace and setup trace

Nils Carlson nils.carlson at ericsson.com
Mon Nov 8 10:31:15 EST 2010


In order to allow multiple traces to co-exist markers need to be
enabled per-trace. This requires first creating a trace struct
with corresponding hashes and then activating markers.
This patch just separates out the trace_struct allocation for the
trace and makes the corresponding necessary changes to ustctl,
ustcmd, tracectl and test-cases.

Signed-off-by: Nils Carlson <nils.carlson at ericsson.com>
---
 libust/tracectl.c                                  |   31 +++---
 libust/tracer.c                                    |  108 ++++++++++++++++----
 libust/tracer.h                                    |    2 +-
 libust/tracercore.c                                |    1 +
 libust/tracercore.h                                |    1 +
 libustcmd/ustcmd.c                                 |   11 ++
 tests/manual_mode_tracing.sh                       |    3 +-
 .../ustcmd_function_tests/ustcmd_function_tests.c  |    6 +-
 ustctl/ustctl.c                                    |   12 ++
 9 files changed, 137 insertions(+), 38 deletions(-)

diff --git a/libust/tracectl.c b/libust/tracectl.c
index d5ca7d2..692364d 100644
--- a/libust/tracectl.c
+++ b/libust/tracectl.c
@@ -185,7 +185,7 @@ static void request_buffer_consumer(int sock,
  * produced by this pid.
  *
  * The trace must be at least allocated. (It can also be started.)
- * This is because _ltt_trace_find is used.
+ * This is because _ltt_trace_find_alloc is used.
  */
 
 static void inform_consumer_daemon(const char *trace_name)
@@ -203,7 +203,7 @@ static void inform_consumer_daemon(const char *trace_name)
 
 	ltt_lock_traces();
 
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 	if (trace == NULL) {
 		WARN("inform_consumer_daemon: could not find trace \"%s\"; it is probably already destroyed", trace_name);
 		goto unlock_traces;
@@ -255,7 +255,7 @@ static int get_buffer_shmid_pipe_fd(const char *trace_name, const char *ch_name,
 	DBG("get_buffer_shmid_pipe_fd");
 
 	ltt_lock_traces();
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 	ltt_unlock_traces();
 
 	if (trace == NULL) {
@@ -287,7 +287,7 @@ static int get_subbuf_num_size(const char *trace_name, const char *ch_name,
 	DBG("get_subbuf_size");
 
 	ltt_lock_traces();
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 	ltt_unlock_traces();
 
 	if (!trace) {
@@ -410,7 +410,7 @@ static int get_subbuffer(const char *trace_name, const char *ch_name,
 	*consumed_old = 0;
 
 	ltt_lock_traces();
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 
 	if (!trace) {
 		DBG("Cannot find trace. It was likely destroyed by the user.");
@@ -451,7 +451,7 @@ static int notify_buffer_mapped(const char *trace_name,
 	DBG("get_buffer_fd");
 
 	ltt_lock_traces();
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 
 	if (!trace) {
 		retval = -ENODATA;
@@ -498,7 +498,7 @@ static int put_subbuffer(const char *trace_name, const char *ch_name,
 	DBG("put_subbuf");
 
 	ltt_lock_traces();
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 
 	if (!trace) {
 		retval = -ENODATA;
@@ -644,15 +644,9 @@ static int process_trace_cmd(int command, char *trace_name)
 	case CREATE_TRACE:
 		DBG("trace create");
 
-		result = ltt_trace_setup(trace_name);
-		if (result < 0) {
-			ERR("ltt_trace_setup failed");
-			return result;
-		}
-
-		result = ltt_trace_set_type(trace_name, trace_type);
+		result = ltt_trace_create(trace_name);
 		if (result < 0) {
-			ERR("ltt_trace_set_type failed");
+			ERR("ltt_trace_create failed");
 			return result;
 		}
 
@@ -1381,6 +1375,12 @@ static void __attribute__((constructor)) init()
 		 */
 		ltt_channels_register("ust");
 
+		result = ltt_trace_create(trace_name);
+		if (result < 0) {
+			ERR("ltt_trace_create failed");
+			return;
+		}
+
 		result = ltt_trace_setup(trace_name);
 		if (result < 0) {
 			ERR("ltt_trace_setup failed");
@@ -1613,6 +1613,7 @@ static void ust_fork(void)
 		return;
 	}
 	create_listener();
+	ltt_trace_create("auto");
 	ltt_trace_setup("auto");
 	result = ltt_trace_set_type("auto", "ustrelay");
 	if (result < 0) {
diff --git a/libust/tracer.c b/libust/tracer.c
index c0d7a1a..73cd981 100644
--- a/libust/tracer.c
+++ b/libust/tracer.c
@@ -288,12 +288,29 @@ static void trace_async_wakeup(struct ust_trace *trace)
 //ust// }
 
 /**
- * _ltt_trace_find - find a trace by given name.
+ * _ltt_trace_find_created - find a created trace by given name.
  * trace_name: trace name
  *
  * Returns a pointer to the trace structure, NULL if not found.
  */
-struct ust_trace *_ltt_trace_find(const char *trace_name)
+struct ust_trace *_ltt_trace_find_created(const char *trace_name)
+{
+	struct ust_trace *trace;
+
+	list_for_each_entry(trace, &ltt_traces.created_head, list)
+		if (!strncmp(trace->trace_name, trace_name, NAME_MAX))
+			return trace;
+
+	return NULL;
+}
+
+/**
+ * _ltt_trace_find_alloc - find an allocated trace by given name.
+ * trace_name: trace name
+ *
+ * Returns a pointer to the trace structure, NULL if not found.
+ */
+struct ust_trace *_ltt_trace_find_alloc(const char *trace_name)
 {
 	struct ust_trace *trace;
 
@@ -320,6 +337,30 @@ struct ust_trace *_ltt_trace_find_setup(const char *trace_name)
 	return NULL;
 }
 
+/* _ltt_trace_find :
+ * find a trace by a given name.
+ *
+ * Returns a pointer to the trace structure, NULL if not found.
+ */
+struct ust_trace *_ltt_trace_find(const char *trace_name)
+{
+	struct ust_trace *trace;
+
+	trace = _ltt_trace_find_alloc(trace_name);
+	if (trace)
+		return trace;
+
+	trace = _ltt_trace_find_setup(trace_name);
+	if (trace)
+		return trace;
+
+	trace = _ltt_trace_find_created(trace_name);
+	if (trace)
+		return trace;
+
+	return NULL;
+}
+
 /**
  * ltt_release_transport - Release an LTT transport
  * @kref : reference count on the transport
@@ -358,19 +399,10 @@ static inline void prepare_chan_size_num(unsigned int *subbuf_size,
 	WARN_ON(hweight32(*n_subbufs) != 1);
 }
 
-int _ltt_trace_setup(const char *trace_name)
+static int _ltt_trace_create(const char *trace_name)
 {
 	int err = 0;
 	struct ust_trace *new_trace = NULL;
-	int metadata_index;
-	unsigned int chan;
-	enum ltt_channels chantype;
-
-	if (_ltt_trace_find_setup(trace_name)) {
-		ERR("Trace name %s already used", trace_name);
-		err = -EEXIST;
-		goto traces_error;
-	}
 
 	if (_ltt_trace_find(trace_name)) {
 		ERR("Trace name %s already used", trace_name);
@@ -385,13 +417,40 @@ int _ltt_trace_setup(const char *trace_name)
 		goto traces_error;
 	}
 	strncpy(new_trace->trace_name, trace_name, NAME_MAX);
+
+	list_add(&new_trace->list, &ltt_traces.created_head);
+
+	return 0;
+
+trace_free:
+	free(new_trace);
+traces_error:
+	return err;
+
+}
+
+int _ltt_trace_setup(const char *trace_name)
+{
+	int err = 0;
+	struct ust_trace *new_trace = NULL;
+	int metadata_index;
+	unsigned int chan;
+	enum ltt_channels chantype;
+
+	new_trace = _ltt_trace_find_created(trace_name);
+	if (!new_trace) {
+		ERR("Trace not found %s", trace_name);
+		err = -ENOENT;
+		goto traces_error;
+	}
+
 	new_trace->channels = ltt_channels_trace_alloc(&new_trace->nr_channels,
 				ust_channels_overwrite_by_default,
 				ust_channels_request_collection_by_default, 1);
 	if (!new_trace->channels) {
 		ERR("Unable to allocate memory for chaninfo  %s\n", trace_name);
 		err = -ENOMEM;
-		goto trace_free;
+		goto traces_error;
 	}
 
 	/*
@@ -417,16 +476,25 @@ int _ltt_trace_setup(const char *trace_name)
 			chan_infos[chantype].def_subbufcount;
 	}
 
+	list_del(&new_trace->list);
 	list_add(&new_trace->list, &ltt_traces.setup_head);
 	return 0;
 
-trace_free:
-	free(new_trace);
 traces_error:
 	return err;
 }
 
 
+int ltt_trace_create(const char *trace_name)
+{
+	int ret;
+	ltt_lock_traces();
+	ret = _ltt_trace_create(trace_name);
+	ltt_unlock_traces();
+	return ret;
+}
+
+
 int ltt_trace_setup(const char *trace_name)
 {
 	int ret;
@@ -628,7 +696,7 @@ int ltt_trace_alloc(const char *trace_name)
 
 	ltt_lock_traces();
 
-	if (_ltt_trace_find(trace_name)) { /* Trace already allocated */
+	if (_ltt_trace_find_alloc(trace_name)) { /* Trace already allocated */
 		err = 1;
 		goto traces_error;
 	}
@@ -831,7 +899,7 @@ int ltt_trace_destroy(const char *trace_name, int drop)
 
 	ltt_lock_traces();
 
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 	if (trace) {
 		err = _ltt_trace_destroy(trace);
 		if (err)
@@ -894,7 +962,7 @@ int ltt_trace_start(const char *trace_name)
 
 	ltt_lock_traces();
 
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 	err = _ltt_trace_start(trace);
 	if (err)
 		goto no_trace;
@@ -958,7 +1026,7 @@ int ltt_trace_stop(const char *trace_name)
 	struct ust_trace *trace;
 
 	ltt_lock_traces();
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 	err = _ltt_trace_stop(trace);
 	ltt_unlock_traces();
 	return err;
@@ -976,7 +1044,7 @@ int ltt_filter_control(enum ltt_filter_control_msg msg, const char *trace_name)
 
 	DBG("ltt_filter_control : trace %s", trace_name);
 	ltt_lock_traces();
-	trace = _ltt_trace_find(trace_name);
+	trace = _ltt_trace_find_alloc(trace_name);
 	if (trace == NULL) {
 		ERR("Trace does not exist. Cannot proxy control request");
 		err = -ENOENT;
diff --git a/libust/tracer.h b/libust/tracer.h
index c5df6ec..7d4ec38 100644
--- a/libust/tracer.h
+++ b/libust/tracer.h
@@ -435,6 +435,6 @@ extern void ltt_dump_marker_state(struct ust_trace *trace);
 extern void ltt_lock_traces(void);
 extern void ltt_unlock_traces(void);
 
-extern struct ust_trace *_ltt_trace_find(const char *trace_name);
+extern struct ust_trace *_ltt_trace_find_alloc(const char *trace_name);
 
 #endif /* _LTT_TRACER_H */
diff --git a/libust/tracercore.c b/libust/tracercore.c
index bbc8691..36656f9 100644
--- a/libust/tracercore.c
+++ b/libust/tracercore.c
@@ -22,6 +22,7 @@
 
 /* Traces structures */
 struct ltt_traces ltt_traces = {
+	.created_head = LIST_HEAD_INIT(ltt_traces.created_head),
 	.setup_head = LIST_HEAD_INIT(ltt_traces.setup_head),
 	.head = LIST_HEAD_INIT(ltt_traces.head),
 };
diff --git a/libust/tracercore.h b/libust/tracercore.h
index 5c396f4..857930e 100644
--- a/libust/tracercore.h
+++ b/libust/tracercore.h
@@ -32,6 +32,7 @@
  * list.
  */
 struct ltt_traces {
+	struct list_head created_head;	/* Created traces list */
 	struct list_head setup_head;	/* Pre-allocated traces list */
 	struct list_head head;		/* Allocated Traces list */
 	unsigned int num_active_traces;	/* Number of active traces */
diff --git a/libustcmd/ustcmd.c b/libustcmd/ustcmd.c
index bffd3c2..aada64b 100644
--- a/libustcmd/ustcmd.c
+++ b/libustcmd/ustcmd.c
@@ -353,6 +353,17 @@ int ustcmd_create_trace(const char *trace, pid_t pid)
 }
 
 /**
+ * Sets up an UST trace according to a PID.
+ *
+ * @param pid	Traced process ID
+ * @return	0 if successful, or error USTCMD_ERR_GEN
+ */
+int ustcmd_setup_trace(const char *trace, pid_t pid)
+{
+	return do_trace_cmd(trace, pid, SETUP_TRACE);
+}
+
+/**
  * Starts an UST trace according to a PID.
  *
  * @param pid	Traced process ID
diff --git a/tests/manual_mode_tracing.sh b/tests/manual_mode_tracing.sh
index 3f203b1..ec6d508 100755
--- a/tests/manual_mode_tracing.sh
+++ b/tests/manual_mode_tracing.sh
@@ -28,7 +28,7 @@ source $TESTDIR/tap.sh
 
 starttest "Manual mode tracing"
 
-plan_tests 9
+plan_tests 10
 
 TRACE_DIR="/tmp/ust-testsuite-manual-trace"
 rm -rf "$TRACE_DIR"
@@ -47,6 +47,7 @@ okx ustctl --list-markers "$PID"
 okx ustctl --enable-marker ust/malloc $PID
 okx ustctl --enable-marker ust/free $PID
 okx ustctl --create-trace $PID
+okx ustctl --setup-trace $PID
 okx ustctl --alloc-trace $PID
 okx ustctl --start-trace $PID
 sleep 0.5
diff --git a/tests/ustcmd_function_tests/ustcmd_function_tests.c b/tests/ustcmd_function_tests/ustcmd_function_tests.c
index d44dafc..3209964 100644
--- a/tests/ustcmd_function_tests/ustcmd_function_tests.c
+++ b/tests/ustcmd_function_tests/ustcmd_function_tests.c
@@ -82,6 +82,8 @@ static void ustcmd_function_tests(pid_t pid)
 	/* Create and allocate a trace */
 	tap_ok(!ustcmd_create_trace(trace, pid), "ustcmd_create_trace");
 
+	tap_ok(!ustcmd_setup_trace(trace, pid), "ustcmd_setup_trace");
+
 	tap_ok(!ustcmd_alloc_trace(trace, pid), "ustcmd_alloc_trace");
 
 	/* Get subbuf size and number */
@@ -105,6 +107,8 @@ static void ustcmd_function_tests(pid_t pid)
 	/* Create a new trace */
 	tap_ok(!ustcmd_create_trace(trace, pid), "ustcmd_create_trace - create a new trace");
 
+	tap_ok(!ustcmd_setup_trace(trace, pid), "ustcmd_setup_trace - setup a new trace");
+
 	printf("Setting new subbufer number and sizes (doubling)\n");
 	new_subbuf_num = 2 * subbuf_num;
 	new_subbuf_size = 2 * subbuf_size;
@@ -162,7 +166,7 @@ int main()
 	pid_t parent_pid, child_pid;
 	FILE *pipe_file;
 
-	tap_plan(27);
+	tap_plan(29);
 
 	printf("Function tests for ustcmd\n");
 
diff --git a/ustctl/ustctl.c b/ustctl/ustctl.c
index aad3834..1a029ac 100644
--- a/ustctl/ustctl.c
+++ b/ustctl/ustctl.c
@@ -27,6 +27,7 @@
 
 enum command {
 	CREATE_TRACE=1000,
+	SETUP_TRACE,
 	ALLOC_TRACE,
 	START_TRACE,
 	STOP_TRACE,
@@ -61,6 +62,7 @@ void usage(void)
 \n\
 Commands:\n\
     --create-trace\t\t\tCreate trace\n\
+    --setup-trace\t\t\tSetup trace\n\
     --alloc-trace\t\t\tAlloc trace\n\
     --start-trace\t\t\tStart tracing\n\
     --stop-trace\t\t\tStop tracing\n\
@@ -91,6 +93,7 @@ int parse_opts_long(int argc, char **argv, struct ust_opts *opts)
 		int option_index = 0;
 		static struct option long_options[] = {
 			{ "create-trace", 0, 0, CREATE_TRACE },
+			{ "setup-trace", 0, 0, SETUP_TRACE },
 			{ "alloc-trace", 0, 0, ALLOC_TRACE },
 			{ "start-trace", 0, 0, START_TRACE },
 			{ "stop-trace", 0, 0, STOP_TRACE },
@@ -282,6 +285,15 @@ int main(int argc, char *argv[])
 				}
 				break;
 
+			case SETUP_TRACE:
+				result = ustcmd_setup_trace(trace, *pidit);
+				if (result) {
+					ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit);
+					retval = EXIT_FAILURE;
+					break;
+				}
+				break;
+
 			case START_TRACE:
 				result = ustcmd_start_trace(trace, *pidit);
 				if (result) {
-- 
1.7.1





More information about the lttng-dev mailing list