All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-trace-devel@vger.kernel.org
Cc: "Steven Rostedt (Google)" <rostedt@goodmis.org>
Subject: [PATCH 26/26] trace-cmd: Have the host agent proxy control the time synchronization
Date: Fri, 13 May 2022 22:47:56 -0400	[thread overview]
Message-ID: <20220514024756.1319681-27-rostedt@goodmis.org> (raw)
In-Reply-To: <20220514024756.1319681-1-rostedt@goodmis.org>

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

The time synchronization logic between agent and recorder made the
assumption that the host is running the recorder and the guest is
running the agent. This is very limiting, especially for environments
where you have a streamlined host that has no disk space, and a guest
that is "privileged".

Have the agent proxy handle the time synchronization for the host, and
the trace-cmd record running on the guest retrieve this information from
the host.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 tracecmd/include/trace-local.h |  5 ++--
 tracecmd/trace-agent.c         | 17 +++++++++---
 tracecmd/trace-record.c        | 47 ++++++++++++++++++++++++++--------
 tracecmd/trace-tsync.c         | 12 +++++----
 4 files changed, 60 insertions(+), 21 deletions(-)

diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h
index a86d22ca9ad8..3d025f86fb19 100644
--- a/tracecmd/include/trace-local.h
+++ b/tracecmd/include/trace-local.h
@@ -121,8 +121,9 @@ void trace_convert(int argc, char **argv);
 
 int trace_record_agent(struct tracecmd_msg_handle *msg_handle,
 		       int cpus, int *fds,
-		       int argc, char **argv, bool use_fifos,
-		       unsigned long long trace_id, const char *host);
+		       int argc, char **argv,
+		       bool use_fifos, struct tracecmd_time_sync *tsync,
+		       unsigned long long trace_id, int rcid, const char *host);
 
 struct hook_list;
 
diff --git a/tracecmd/trace-agent.c b/tracecmd/trace-agent.c
index 3098a1ea2b8b..ea41ad540701 100644
--- a/tracecmd/trace-agent.c
+++ b/tracecmd/trace-agent.c
@@ -238,17 +238,26 @@ static void agent_handle(int sd, int nr_cpus, int page_size,
 
 	if (tsync_proto) {
 		fd = wait_for_connection(fd);
-		tsync = trace_tsync_as_guest(fd, tsync_proto, get_clock(argc, argv),
-					     remote_id, local_id);
+
+		if (rcid >= 0) {
+			tsync = trace_tsync_as_host(fd, trace_id, 0, rcid,
+						    client_cpus, tsync_proto,
+						    get_clock(argc, argv));
+		} else {
+			tsync = trace_tsync_as_guest(fd, tsync_proto,
+						     get_clock(argc, argv),
+						     remote_id, local_id);
+		}
 		if (!tsync)
 			close(fd);
 	}
 
 	trace_record_agent(msg_handle, nr_cpus, fds, argc, argv,
-			   use_fifos, trace_id, network);
+			   use_fifos, tsync, trace_id, rcid, network);
 
 	if (tsync) {
-		tracecmd_tsync_with_host_stop(tsync);
+		if (rcid < 0)
+			tracecmd_tsync_with_host_stop(tsync);
 		tracecmd_tsync_free(tsync);
 	}
 
diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index e0d2f2b0f554..541bce7298ce 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -686,6 +686,12 @@ add_tsc2nsec(struct tracecmd_output *handle, struct tsc_nsec *tsc2nsec)
 	tracecmd_add_option_v(handle, TRACECMD_OPTION_TSC2NSEC, vector, 3);
 }
 
+static void guest_tsync_complete(struct buffer_instance *instance)
+{
+	tracecmd_tsync_with_host_stop(instance->tsync);
+	tracecmd_tsync_free(instance->tsync);
+}
+
 static void host_tsync_complete(struct common_record_context *ctx,
 				struct buffer_instance *instance)
 {
@@ -732,8 +738,12 @@ static void tell_guests_to_stop(struct common_record_context *ctx)
 	}
 
 	for_all_instances(instance) {
-		if (is_guest(instance))
-			host_tsync_complete(ctx, instance);
+		if (is_guest(instance)) {
+			if (is_proxy(instance))
+				guest_tsync_complete(instance);
+			else
+				host_tsync_complete(ctx, instance);
+		}
 	}
 
 	/* Wait for guests to acknowledge */
@@ -3851,11 +3861,15 @@ static int host_tsync(struct common_record_context *ctx,
 		fd = trace_vsock_open(instance->cid, tsync_port);
 	}
 
-	instance->tsync = trace_tsync_as_host(fd, top_instance.trace_id,
-					      instance->tsync_loop_interval,
-					      guest_id, instance->cpu_count,
-					      proto, ctx->clock);
-
+	if (is_proxy(instance)) {
+		instance->tsync = trace_tsync_as_guest(fd, proto, ctx->clock,
+						       guest_id, -1);
+	} else {
+		instance->tsync = trace_tsync_as_host(fd, top_instance.trace_id,
+						      instance->tsync_loop_interval,
+						      guest_id, instance->cpu_count,
+						      proto, ctx->clock);
+	}
 	return instance->tsync ? 0 : -1;
 }
 
@@ -4267,6 +4281,7 @@ enum {
 	DATA_FL_DATE		= 1,
 	DATA_FL_OFFSET		= 2,
 	DATA_FL_GUEST		= 4,
+	DATA_FL_PROXY		= 8,
 };
 
 static void add_options(struct tracecmd_output *handle, struct common_record_context *ctx)
@@ -6235,6 +6250,11 @@ static void parse_record_options(int argc,
 			ctx->instance->name = name;
 			add_instance(ctx->instance, 0);
 			ctx->data_flags |= DATA_FL_GUEST;
+
+			/* Do not send a clock to a proxy */
+			if (is_proxy)
+				ctx->instance->flags |= BUFFER_FL_HAS_CLOCK;
+
 			break;
 		}
 		case 'F':
@@ -6706,6 +6726,8 @@ static void finalize_record_trace(struct common_record_context *ctx)
 			tracecmd_msg_wait(ctx->instance->msg_handle);
 			if (ctx->tsc2nsec.mult)
 				add_tsc2nsec(ctx->instance->network_handle, &ctx->tsc2nsec);
+			tracecmd_write_guest_time_shift(ctx->instance->network_handle,
+							ctx->instance->tsync);
 			tracecmd_msg_send_options(ctx->instance->msg_handle,
 						  ctx->instance->network_handle);
 		}
@@ -6821,7 +6843,7 @@ static void record_trace(int argc, char **argv,
 	if (!ctx->output)
 		ctx->output = DEFAULT_INPUT_FILE;
 
-	if (ctx->data_flags & DATA_FL_GUEST)
+	if (ctx->data_flags & (DATA_FL_GUEST | DATA_FL_PROXY))
 		set_tsync_params(ctx);
 
 	make_instances();
@@ -7129,8 +7151,9 @@ void trace_record(int argc, char **argv)
  */
 int trace_record_agent(struct tracecmd_msg_handle *msg_handle,
 		       int cpus, int *fds,
-		       int argc, char **argv, bool use_fifos,
-		       unsigned long long trace_id, const char *host)
+		       int argc, char **argv,
+		       bool use_fifos, struct tracecmd_time_sync *tsync,
+		       unsigned long long trace_id, int rcid, const char *host)
 {
 	struct common_record_context ctx;
 	char **argv_plus;
@@ -7158,8 +7181,12 @@ int trace_record_agent(struct tracecmd_msg_handle *msg_handle,
 	ctx.instance->fds = fds;
 	ctx.instance->use_fifos = use_fifos;
 	ctx.instance->flags |= BUFFER_FL_AGENT;
+	if (rcid >= 0)
+		ctx.data_flags |= DATA_FL_PROXY;
 	ctx.instance->msg_handle = msg_handle;
 	ctx.instance->host = host;
+	ctx.instance->tsync = tsync;
+	ctx.instance->cid = rcid;
 	msg_handle->version = V3_PROTOCOL;
 	top_instance.trace_id = trace_id;
 	record_trace(argc, argv, &ctx);
diff --git a/tracecmd/trace-tsync.c b/tracecmd/trace-tsync.c
index 45ebb9c66a19..a96b4e43e0d9 100644
--- a/tracecmd/trace-tsync.c
+++ b/tracecmd/trace-tsync.c
@@ -228,15 +228,17 @@ struct tracecmd_time_sync *
 trace_tsync_as_guest(int fd, const char *tsync_proto, const char *clock,
 	       unsigned int remote_id, unsigned int local_id)
 {
-	struct tracecmd_time_sync *tsync;
+	struct tracecmd_time_sync *tsync = NULL;
 
 	if (fd < 0)
-		return NULL;
+		 return NULL;
 
-	tsync = tracecmd_tsync_with_host(fd, tsync_proto, clock,
-					 remote_id, local_id);
-	if (!tsync)
+	tsync = tracecmd_tsync_with_host(fd, tsync_proto,
+					 clock, remote_id, local_id);
+	if (!tsync) {
 		warning("Failed to negotiate timestamps synchronization with the host");
+		return NULL;
+	}
 
 	return tsync;
 }
-- 
2.35.1


      parent reply	other threads:[~2022-05-14  2:55 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-14  2:47 [PATCH 00/26] trace-cmd: Add agent proxy (agent on the host) Steven Rostedt
2022-05-14  2:47 ` [PATCH 01/26] trace-cmd record: Make start_threads() static Steven Rostedt
2022-05-14  2:47 ` [PATCH 02/26] trace-cmd: Move add_guest_info() into trace-vm.c Steven Rostedt
2022-05-14  2:47 ` [PATCH 03/26] trace-cmd: Simplify add_guest() Steven Rostedt
2022-05-14  2:47 ` [PATCH 04/26] trace-cmd: Move find_pid_by_cid() into add_guest() Steven Rostedt
2022-05-14  2:47 ` [PATCH 05/26] trace-cmd: Move find_tasks() " Steven Rostedt
2022-05-14  2:47 ` [PATCH 06/26] trace-cmd: Move trace_msg cache file to memfd Steven Rostedt
2022-05-14  2:47 ` [PATCH 07/26] trace-cmd Makefile: Change test-build to link as well Steven Rostedt
2022-05-14  2:47 ` [PATCH 08/26] trace-cmd agent: Test if memfd_create() is available Steven Rostedt
2022-05-14  2:47 ` [PATCH 09/26] trace-cmd: Add kernel-doc to trace_record_agent() Steven Rostedt
2022-05-14  2:47 ` [PATCH 10/26] trace-cmd: Move selecting tsync protocol out of tracecmd_tsync_with_host() Steven Rostedt
2022-05-14  2:47 ` [PATCH 11/26] trace-cmd: Move accepting tsync connection " Steven Rostedt
2022-05-14  2:47 ` [PATCH 12/26] trace-cmd: Have get_vsocket_params() cid and rcid parameters be optional Steven Rostedt
2022-05-14  2:47 ` [PATCH 13/26] trace-cmd agent: Add trace_tsync_as_guest() helper function Steven Rostedt
2022-05-14  2:47 ` [PATCH 14/26] trace-cmd record: Pass cpu_count instead of an instance to stop_mapping_vcpus() Steven Rostedt
2022-05-14  2:47 ` [PATCH 15/26] trace-cmd record: Add trace_tsync_as_host() helper Steven Rostedt
2022-05-14  2:47 ` [PATCH 16/26] trace-cmd: Move tsync as guest and host helpers into trace-tsync.c Steven Rostedt
2022-05-14  2:47 ` [PATCH 17/26] trace-cmd msg: Add PROXY communication Steven Rostedt
2022-05-14  2:47 ` [PATCH 18/26] trace-cmd: Add agent proxy communications between record and agent Steven Rostedt
2022-05-14  2:47 ` [PATCH 19/26] trace-cmd msg: Keep track of offset of flushed cache Steven Rostedt
2022-05-14  2:47 ` [PATCH 20/26] trace-cmd library: Add tracecmd_prepare_options() Steven Rostedt
2022-05-14  2:47 ` [PATCH 21/26] trace-cmd library: Add tracecmd_msg_flush_data() Steven Rostedt
2022-05-14  2:47 ` [PATCH 22/26] trace-cmd agent proxy: Allow agent to send more meta data after trace Steven Rostedt
2022-05-14  2:47 ` [PATCH 23/26] trace-cmd agent proxy: Add the remote guest cid to guest list Steven Rostedt
2022-05-14  2:47 ` [PATCH 24/26] trace-cmd agent-proxy: Send options at the end of the trace Steven Rostedt
2022-05-14  2:47 ` [PATCH 25/26] trace-cmd: Have the guest structure hold guest trace_id Steven Rostedt
2022-05-14  2:47 ` Steven Rostedt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220514024756.1319681-27-rostedt@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.