Skip to contentSkip to conversation

rtla: Refactor save_trace_to_file

PATCH In review Costa Shulyupin 2 participants 2 messages Feb 19, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtla: Refactor save_trace_to_file

The functions osnoise_hist_main(), osnoise_top_main(), timerlat_hist_main(), and timerlat_top_main() are lengthy and contain duplicated code.

Refactor by consolidating the duplicate lines into the save_trace_to_file() function.

---
 tools/tracing/rtla/src/osnoise_hist.c  | 5 +----
 tools/tracing/rtla/src/osnoise_top.c   | 5 +----
 tools/tracing/rtla/src/timerlat_hist.c | 5 +----
 tools/tracing/rtla/src/timerlat_top.c  | 5 +----
 tools/tracing/rtla/src/trace.c         | 4 ++++
 5 files changed, 8 insertions(+), 16 deletions(-)
tools/tracing/rtla/src/osnoise_hist.c +1 -4
index b4930b835b0a1..7c6ef67ef3e6c 100644
@@ -983,10 +983,7 @@ int osnoise_hist_main(int argc, char *argv[])
 
 	if (osnoise_trace_is_off(tool, record)) {
 		printf("rtla osnoise hit stop tracing\n");
-		if (params->trace_output) {
-			printf("  Saving trace to %s\n", params->trace_output);
-			save_trace_to_file(record->trace.inst, params->trace_output);
-		}
+		save_trace_to_file(record->trace.inst, params->trace_output);
 	}
 
 out_hist:
tools/tracing/rtla/src/osnoise_top.c +1 -4
index 4772677ac762c..0eeefbbbf3173 100644
@@ -813,10 +813,7 @@ int osnoise_top_main(int argc, char **argv)
 
 	if (osnoise_trace_is_off(tool, record)) {
 		printf("osnoise hit stop tracing\n");
-		if (params->trace_output) {
-			printf("  Saving trace to %s\n", params->trace_output);
-			save_trace_to_file(record->trace.inst, params->trace_output);
-		}
+		save_trace_to_file(record->trace.inst, params->trace_output);
 	}
 
 out_top:
tools/tracing/rtla/src/timerlat_hist.c +1 -4
index 6d7d0a2d45b47..5f54b268a1033 100644
@@ -1390,10 +1390,7 @@ int timerlat_hist_main(int argc, char *argv[])
 		if (!params->no_aa)
 			timerlat_auto_analysis(params->stop_us, params->stop_total_us);
 
-		if (params->trace_output) {
-			printf("  Saving trace to %s\n", params->trace_output);
-			save_trace_to_file(record->trace.inst, params->trace_output);
-		}
+		save_trace_to_file(record->trace.inst, params->trace_output);
 	}
 
 out_hist:
tools/tracing/rtla/src/timerlat_top.c +1 -4
index 05a9403b01d26..1e64e6216bc2e 100644
@@ -1159,10 +1159,7 @@ int timerlat_top_main(int argc, char *argv[])
 		if (!params->no_aa)
 			timerlat_auto_analysis(params->stop_us, params->stop_total_us);
 
-		if (params->trace_output) {
-			printf("  Saving trace to %s\n", params->trace_output);
-			save_trace_to_file(record->trace.inst, params->trace_output);
-		}
+		save_trace_to_file(record->trace.inst, params->trace_output);
 	} else if (params->aa_only) {
 		/*
 		 * If the trace did not stop with --aa-only, at least print the
tools/tracing/rtla/src/trace.c +4 -0
index 728f5029d5335..74ed2f6208baa 100644
@@ -75,12 +75,16 @@ int save_trace_to_file(struct tracefs_instance *inst, const char *filename)
 	int out_fd, in_fd;
 	int retval = -1;
 
+	if (!filename)
+		return 0;
+
 	in_fd = tracefs_instance_file_open(inst, file, O_RDONLY);
 	if (in_fd < 0) {
 		err_msg("Failed to open trace file\n");
 		return -1;
 	}
 
+	printf("  Saving trace to %s\n", filename);
 	out_fd = creat(filename, mode);
 	if (out_fd < 0) {
 		err_msg("Failed to create output file %s\n", filename);
Signature · 1 line
2.48.1
maintainer Reviewed-by Tested-by on v1 1/1
#1 lore raw

st 19. 2. 2025 v 12:52 odesílatel Costa Shulyupin
<[email protected]> napsal:

Quoted 5 lines

The functions osnoise_hist_main(), osnoise_top_main(), timerlat_hist_main(), and timerlat_top_main() are lengthy and contain duplicated code.

There is definitely a bunch of code that needs to be unified in rtla. My idea is to eventually merge top and hist, see my note to the cover letter to the BPF patchset [1].

[1] https://lore.kernel.org/linux-trace-kernel/[email protected]/

Quoted 4 lines

Refactor by consolidating the duplicate lines into the save_trace_to_file() function.

Thank you for the cleanup patch.

Quoted 96 lines
---
 tools/tracing/rtla/src/osnoise_hist.c  | 5 +----
 tools/tracing/rtla/src/osnoise_top.c   | 5 +----
 tools/tracing/rtla/src/timerlat_hist.c | 5 +----
 tools/tracing/rtla/src/timerlat_top.c  | 5 +----
 tools/tracing/rtla/src/trace.c         | 4 ++++
 5 files changed, 8 insertions(+), 16 deletions(-)
tools/tracing/rtla/src/osnoise_hist.c +1 -4
index b4930b835b0a1..7c6ef67ef3e6c 100644
@@ -983,10 +983,7 @@ int osnoise_hist_main(int argc, char *argv[])
 
        if (osnoise_trace_is_off(tool, record)) {
                printf("rtla osnoise hit stop tracing\n");
-               if (params->trace_output) {
-                       printf("  Saving trace to %s\n", params->trace_output);
-                       save_trace_to_file(record->trace.inst, params->trace_output);
-               }
+               save_trace_to_file(record->trace.inst, params->trace_output);
        }
 
 out_hist:
tools/tracing/rtla/src/osnoise_top.c +1 -4
index 4772677ac762c..0eeefbbbf3173 100644
@@ -813,10 +813,7 @@ int osnoise_top_main(int argc, char **argv)
 
        if (osnoise_trace_is_off(tool, record)) {
                printf("osnoise hit stop tracing\n");
-               if (params->trace_output) {
-                       printf("  Saving trace to %s\n", params->trace_output);
-                       save_trace_to_file(record->trace.inst, params->trace_output);
-               }
+               save_trace_to_file(record->trace.inst, params->trace_output);
        }
 
 out_top:
tools/tracing/rtla/src/timerlat_hist.c +1 -4
index 6d7d0a2d45b47..5f54b268a1033 100644
@@ -1390,10 +1390,7 @@ int timerlat_hist_main(int argc, char *argv[])
                if (!params->no_aa)
                        timerlat_auto_analysis(params->stop_us, params->stop_total_us);
 
-               if (params->trace_output) {
-                       printf("  Saving trace to %s\n", params->trace_output);
-                       save_trace_to_file(record->trace.inst, params->trace_output);
-               }
+               save_trace_to_file(record->trace.inst, params->trace_output);
        }
 
 out_hist:
tools/tracing/rtla/src/timerlat_top.c +1 -4
index 05a9403b01d26..1e64e6216bc2e 100644
@@ -1159,10 +1159,7 @@ int timerlat_top_main(int argc, char *argv[])
                if (!params->no_aa)
                        timerlat_auto_analysis(params->stop_us, params->stop_total_us);
 
-               if (params->trace_output) {
-                       printf("  Saving trace to %s\n", params->trace_output);
-                       save_trace_to_file(record->trace.inst, params->trace_output);
-               }
+               save_trace_to_file(record->trace.inst, params->trace_output);
        } else if (params->aa_only) {
                /*
                 * If the trace did not stop with --aa-only, at least print the
tools/tracing/rtla/src/trace.c +4 -1
index 728f5029d5335..74ed2f6208baa 100644
@@ -75,12 +75,16 @@ int save_trace_to_file(struct tracefs_instance *inst, const char *filename)
        int out_fd, in_fd;
        int retval = -1;
 
+       if (!filename)
+               return 0;
+
        in_fd = tracefs_instance_file_open(inst, file, O_RDONLY);
        if (in_fd < 0) {
                err_msg("Failed to open trace file\n");
                return -1;
        }
 
+       printf("  Saving trace to %s\n", filename);
        out_fd = creat(filename, mode);
        if (out_fd < 0) {
                err_msg("Failed to create output file %s\n", filename);
--

2.48.1