diff mbox series

[15/26] trace-cmd record: Add trace_tsync_as_host() helper

Message ID 20220514024756.1319681-16-rostedt@goodmis.org (mailing list archive)
State Accepted
Commit aa12b16f256a78c565f55ff19b74c7e6de8673c9
Headers show
Series trace-cmd: Add agent proxy (agent on the host) | expand

Commit Message

Steven Rostedt May 14, 2022, 2:47 a.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

In the process of adding an agent proxy, it will be useful to have a
function that will allow the agent to synchronize as the host. Move the
code to do so into a helper function that can be used by the agent. This
will be later moved to a separate file.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 tracecmd/trace-record.c | 57 +++++++++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index f05ad09c9723..2c595a7918f0 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -3979,12 +3979,42 @@  static void stop_mapping_vcpus(int cpu_count, struct trace_guest *guest)
 	tracefs_instance_free(guest->instance);
 }
 
+static struct tracecmd_time_sync *
+trace_tsync_as_host(int fd, unsigned long long trace_id,
+		    int loop_interval, int guest_id,
+		    int guest_cpus, const char *proto_name,
+		    const char *clock)
+{
+	struct tracecmd_time_sync *tsync;
+	struct trace_guest *guest;
+	int guest_pid = -1;
+
+	if (fd < 0)
+		return NULL;
+
+	if (guest_id >= 0) {
+		guest = trace_get_guest(guest_id, NULL);
+		if (guest == NULL)
+			return NULL;
+		guest_pid = guest->pid;
+		start_mapping_vcpus(guest);
+	}
+
+	tsync = tracecmd_tsync_with_guest(trace_id, loop_interval, fd,
+					  guest_pid, guest_cpus, proto_name,
+					  clock);
+
+	if (guest_id >= 0)
+		stop_mapping_vcpus(guest_cpus, guest);
+
+	return tsync;
+}
+
 static int host_tsync(struct common_record_context *ctx,
 		      struct buffer_instance *instance,
 		      unsigned int tsync_port, char *proto)
 {
-	struct trace_guest *guest;
-	int guest_pid = -1;
+	int guest_id = -1;
 	int fd;
 
 	if (!proto)
@@ -3994,27 +4024,16 @@  static int host_tsync(struct common_record_context *ctx,
 		fd = connect_port(instance->name, tsync_port,
 				  instance->port_type);
 	} else {
-		guest = trace_get_guest(instance->cid, NULL);
-		if (guest == NULL)
-			return -1;
-
-		guest_pid = guest->pid;
-		start_mapping_vcpus(guest);
+		guest_id = instance->cid;
 		fd = trace_vsock_open(instance->cid, tsync_port);
 	}
 
-	instance->tsync = tracecmd_tsync_with_guest(top_instance.trace_id,
-						    instance->tsync_loop_interval,
-						    fd, guest_pid,
-						    instance->cpu_count,
-						    proto, ctx->clock);
-	if (!is_network(instance))
-		stop_mapping_vcpus(instance->cpu_count, guest);
+	instance->tsync = trace_tsync_as_host(fd, top_instance.trace_id,
+					      instance->tsync_loop_interval,
+					      guest_id, instance->cpu_count,
+					      proto, ctx->clock);
 
-	if (!instance->tsync)
-		return -1;
-
-	return 0;
+	return instance->tsync ? 0 : -1;
 }
 
 static void connect_to_agent(struct common_record_context *ctx,