Skip to contentSkip to conversation

trace-cmd: support -m parameter for virt instances

PATCH In review Marcelo Tosatti 3 participants 4 messages Mar 7, 2024 – May 13, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trace-cmd: support -m parameter for virt instances

Take into consideration the value passed in with "-m", the max size in kilobytes that a per cpu buffer should be, when reading trace data from guests.

This allows one to prevent running out of diskspace on long runs.

lib/trace-cmd/include/private/trace-cmd-private.h +1 -1
index f2cf8dc8..2d14ed7a 100644
@@ -370,7 +370,7 @@ enum {
 void tracecmd_free_recorder(struct tracecmd_recorder *recorder);
 struct tracecmd_recorder *tracecmd_create_recorder(const char *file, int cpu, unsigned flags);
 struct tracecmd_recorder *tracecmd_create_recorder_fd(int fd, int cpu, unsigned flags);
-struct tracecmd_recorder *tracecmd_create_recorder_virt(const char *file, int cpu, unsigned flags, int trace_fd);
+struct tracecmd_recorder *tracecmd_create_recorder_virt(const char *file, int cpu, unsigned flags, int trace_fd, int maxkb);
 struct tracecmd_recorder *tracecmd_create_recorder_maxkb(const char *file, int cpu, unsigned flags, int maxkb);
 struct tracecmd_recorder *tracecmd_create_buffer_recorder_fd(int fd, int cpu, unsigned flags, struct tracefs_instance *instance);
 struct tracecmd_recorder *tracecmd_create_buffer_recorder(const char *file, int cpu, unsigned flags, struct tracefs_instance *instance);
lib/trace-cmd/trace-recorder.c +34 -7

69 lines not on this page · raw message

tracecmd/trace-record.c +1 -1
index 91cc90d4..69ea0cbd 100644
@@ -3584,7 +3584,7 @@ create_recorder_instance(struct buffer_instance *instance, const char *file, int
 			flags |= TRACECMD_RECORD_NOBRASS;
 		else if (!trace_vsock_can_splice_read())
 			flags |= TRACECMD_RECORD_NOSPLICE;
-		return tracecmd_create_recorder_virt(file, cpu, flags, fd);
+		return tracecmd_create_recorder_virt(file, cpu, flags, fd, max_kb);
 	}
 
 	if (brass)
#1 lore raw

Test by: Junyao Zhao <[email protected]>

Successfully collected 12h test data from the guest.

Quoted 114 lines · Marcelo Tosatti

Take into consideration the value passed in with "-m", the max size in kilobytes that a per cpu buffer should be, when reading trace data from guests.

This allows one to prevent running out of diskspace on long runs.

lib/trace-cmd/include/private/trace-cmd-private.h +1 -1
index f2cf8dc8..2d14ed7a 100644
@@ -370,7 +370,7 @@ enum {
 void tracecmd_free_recorder(struct tracecmd_recorder *recorder);
 struct tracecmd_recorder *tracecmd_create_recorder(const char *file, int cpu, unsigned flags);
 struct tracecmd_recorder *tracecmd_create_recorder_fd(int fd, int cpu, unsigned flags);
-struct tracecmd_recorder *tracecmd_create_recorder_virt(const char *file, int cpu, unsigned flags, int trace_fd);
+struct tracecmd_recorder *tracecmd_create_recorder_virt(const char *file, int cpu, unsigned flags, int trace_fd, int maxkb);
 struct tracecmd_recorder *tracecmd_create_recorder_maxkb(const char *file, int cpu, unsigned flags, int maxkb);
 struct tracecmd_recorder *tracecmd_create_buffer_recorder_fd(int fd, int cpu, unsigned flags, struct tracefs_instance *instance);
 struct tracecmd_recorder *tracecmd_create_buffer_recorder(const char *file, int cpu, unsigned flags, struct tracefs_instance *instance);
lib/trace-cmd/trace-recorder.c +34 -7
index 0633edf5..44f245d5 100644
@@ -175,19 +175,46 @@ tracecmd_create_buffer_recorder_fd(int fd, int cpu, unsigned flags, struct trace
 
 static struct tracecmd_recorder *
 __tracecmd_create_buffer_recorder(const char *file, int cpu, unsigned flags,
-                                 struct tracefs_instance *instance, int tfd)
+                                 struct tracefs_instance *instance, int tfd, int maxkb)
 {
        struct tracecmd_recorder *recorder;
-       int fd;
+       int fd, fd2 = -1;
+       char *file2;
 
-       fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
+       fd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
        if (fd < 0)
                return NULL;
 
-       recorder = create_buffer_recorder_fd2(fd, -1, cpu, flags, instance, 0, tfd);
+       if (maxkb) {
+               int len = strlen(file);
+
+               file2 = malloc(len + 3);
+               if (!file2)
+                       return NULL;
+
+               sprintf(file2, "%s.1", file);
+
+               fd2 = open(file2, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
+               if (fd2 < 0) {
+                       close(fd);
+                       unlink(file);
+                       free(file2);
+                       return NULL;
+               }
+       }
+
+       recorder = create_buffer_recorder_fd2(fd, fd2, cpu, flags, instance, maxkb, tfd);
        if (!recorder) {
                close(fd);
                unlink(file);
+               if (fd2 != -1)
+                       close(fd2);
+       }
+
+       if (fd2 != -1) {
+               /* Unlink file2, we need to add everything to file at the end */
+               unlink(file2);
+               free(file2);
        }
 
        return recorder;
@@ -242,7 +269,7 @@ struct tracecmd_recorder *
 tracecmd_create_buffer_recorder(const char *file, int cpu, unsigned flags,
                                struct tracefs_instance *instance)
 {
-       return __tracecmd_create_buffer_recorder(file, cpu, flags, instance, -1);
+       return __tracecmd_create_buffer_recorder(file, cpu, flags, instance, -1, 0);
 }
 
 /**
@@ -255,9 +282,9 @@ tracecmd_create_buffer_recorder(const char *file, int cpu, unsigned flags,
  */
 struct tracecmd_recorder *
 tracecmd_create_recorder_virt(const char *file, int cpu, unsigned flags,
-                             int trace_fd)
+                             int trace_fd, int maxkb)
 {
-       return __tracecmd_create_buffer_recorder(file, cpu, flags, NULL, trace_fd);
+       return __tracecmd_create_buffer_recorder(file, cpu, flags, NULL, trace_fd, maxkb);
 }
 
 struct tracecmd_recorder *tracecmd_create_recorder_fd(int fd, int cpu, unsigned flags)
tracecmd/trace-record.c +1 -1
index 91cc90d4..69ea0cbd 100644
@@ -3584,7 +3584,7 @@ create_recorder_instance(struct buffer_instance *instance, const char *file, int
                        flags |= TRACECMD_RECORD_NOBRASS;
                else if (!trace_vsock_can_splice_read())
                        flags |= TRACECMD_RECORD_NOSPLICE;
-               return tracecmd_create_recorder_virt(file, cpu, flags, fd);
+               return tracecmd_create_recorder_virt(file, cpu, flags, fd, max_kb);
        }
 
        if (brass)
#2 lore raw
In reply to Marcelo Tosatti

Ping?

Quoted 113 lines · Marcelo Tosatti

Take into consideration the value passed in with "-m", the max size in kilobytes that a per cpu buffer should be, when reading trace data from guests.

This allows one to prevent running out of diskspace on long runs.

lib/trace-cmd/include/private/trace-cmd-private.h +1 -1
index f2cf8dc8..2d14ed7a 100644
@@ -370,7 +370,7 @@ enum {
 void tracecmd_free_recorder(struct tracecmd_recorder *recorder);
 struct tracecmd_recorder *tracecmd_create_recorder(const char *file, int cpu, unsigned flags);
 struct tracecmd_recorder *tracecmd_create_recorder_fd(int fd, int cpu, unsigned flags);
-struct tracecmd_recorder *tracecmd_create_recorder_virt(const char *file, int cpu, unsigned flags, int trace_fd);
+struct tracecmd_recorder *tracecmd_create_recorder_virt(const char *file, int cpu, unsigned flags, int trace_fd, int maxkb);
 struct tracecmd_recorder *tracecmd_create_recorder_maxkb(const char *file, int cpu, unsigned flags, int maxkb);
 struct tracecmd_recorder *tracecmd_create_buffer_recorder_fd(int fd, int cpu, unsigned flags, struct tracefs_instance *instance);
 struct tracecmd_recorder *tracecmd_create_buffer_recorder(const char *file, int cpu, unsigned flags, struct tracefs_instance *instance);
lib/trace-cmd/trace-recorder.c +34 -7
index 0633edf5..44f245d5 100644
@@ -175,19 +175,46 @@ tracecmd_create_buffer_recorder_fd(int fd, int cpu, unsigned flags, struct trace
 
 static struct tracecmd_recorder *
 __tracecmd_create_buffer_recorder(const char *file, int cpu, unsigned flags,
-				  struct tracefs_instance *instance, int tfd)
+				  struct tracefs_instance *instance, int tfd, int maxkb)
 {
 	struct tracecmd_recorder *recorder;
-	int fd;
+	int fd, fd2 = -1;
+	char *file2;
 
-	fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
+	fd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
 	if (fd < 0)
 		return NULL;
 
-	recorder = create_buffer_recorder_fd2(fd, -1, cpu, flags, instance, 0, tfd);
+	if (maxkb) {
+		int len = strlen(file);
+
+		file2 = malloc(len + 3);
+		if (!file2)
+			return NULL;
+
+		sprintf(file2, "%s.1", file);
+
+		fd2 = open(file2, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
+		if (fd2 < 0) {
+			close(fd);
+			unlink(file);
+			free(file2);
+			return NULL;
+		}
+	}
+
+	recorder = create_buffer_recorder_fd2(fd, fd2, cpu, flags, instance, maxkb, tfd);
 	if (!recorder) {
 		close(fd);
 		unlink(file);
+		if (fd2 != -1)
+			close(fd2);
+	}
+
+	if (fd2 != -1) {
+		/* Unlink file2, we need to add everything to file at the end */
+		unlink(file2);
+		free(file2);
 	}
 
 	return recorder;
@@ -242,7 +269,7 @@ struct tracecmd_recorder *
 tracecmd_create_buffer_recorder(const char *file, int cpu, unsigned flags,
 				struct tracefs_instance *instance)
 {
-	return __tracecmd_create_buffer_recorder(file, cpu, flags, instance, -1);
+	return __tracecmd_create_buffer_recorder(file, cpu, flags, instance, -1, 0);
 }
 
 /**
@@ -255,9 +282,9 @@ tracecmd_create_buffer_recorder(const char *file, int cpu, unsigned flags,
  */
 struct tracecmd_recorder *
 tracecmd_create_recorder_virt(const char *file, int cpu, unsigned flags,
-			      int trace_fd)
+			      int trace_fd, int maxkb)
 {
-	return __tracecmd_create_buffer_recorder(file, cpu, flags, NULL, trace_fd);
+	return __tracecmd_create_buffer_recorder(file, cpu, flags, NULL, trace_fd, maxkb);
 }
 
 struct tracecmd_recorder *tracecmd_create_recorder_fd(int fd, int cpu, unsigned flags)
tracecmd/trace-record.c +1 -1
index 91cc90d4..69ea0cbd 100644
@@ -3584,7 +3584,7 @@ create_recorder_instance(struct buffer_instance *instance, const char *file, int
 			flags |= TRACECMD_RECORD_NOBRASS;
 		else if (!trace_vsock_can_splice_read())
 			flags |= TRACECMD_RECORD_NOSPLICE;
-		return tracecmd_create_recorder_virt(file, cpu, flags, fd);
+		return tracecmd_create_recorder_virt(file, cpu, flags, fd, max_kb);
 	}
 
 	if (brass)
#3 lore raw
Quoted 1 line · On Mon, 13 May 2024 14:46:31 -0300 Marcelo Tosatti <[email protected]> wrote:

Ping?

Apologies, I got totally swamped. But the priority of updating user space tooling is getting higher, so hopefully not too long.

Thanks for you patience.

-- Steve