[for-next][PATCH 10/10] rtla: Refactor save_trace_to_file

Steven Rostedt
2025-03-06T18:18:25Z

From: Costa Shulyupin <costa.shul@redhat.com>

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.

Cc: Daniel Bristot de Oliveira <bristot@kernel.org>
Cc: John Kacur <jkacur@redhat.com>
Cc: "Luis Claudio R. Goncalves" <lgoncalv@redhat.com>
Cc: Eder Zulian <ezulian@redhat.com>
Cc: Dan Carpenter <dan.carpenter@linaro.org>
Cc: Gabriele Monaco <gmonaco@redhat.com>
Link: https://lore.kernel.org/20250219115138.406075-1-costa.shul@redhat.com
Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>
Reviewed-by: Tomas Glozar <tglozar@redhat.com>
Tested-by: Tomas Glozar <tglozar@redhat.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
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(-)

diff --git a/tools/tracing/rtla/src/osnoise_hist.c b/tools/tracing/rtla/src/osnoise_hist.c
index b4930b835b0a..7c6ef67ef3e6 100644
--- a/tools/tracing/rtla/src/osnoise_hist.c
+++ b/tools/tracing/rtla/src/osnoise_hist.c
@@ -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:
diff --git a/tools/tracing/rtla/src/osnoise_top.c b/tools/tracing/rtla/src/osnoise_top.c
index 4772677ac762..0eeefbbbf317 100644
--- a/tools/tracing/rtla/src/osnoise_top.c
+++ b/tools/tracing/rtla/src/osnoise_top.c
@@ -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:
diff --git a/tools/tracing/rtla/src/timerlat_hist.c b/tools/tracing/rtla/src/timerlat_hist.c
index 202d99a598ba..93d0c9e45020 100644
--- a/tools/tracing/rtla/src/timerlat_hist.c
+++ b/tools/tracing/rtla/src/timerlat_hist.c
@@ -1473,10 +1473,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:
diff --git a/tools/tracing/rtla/src/timerlat_top.c b/tools/tracing/rtla/src/timerlat_top.c
index 1b6455b9e093..3894ac37d81c 100644
--- a/tools/tracing/rtla/src/timerlat_top.c
+++ b/tools/tracing/rtla/src/timerlat_top.c
@@ -1295,10 +1295,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
diff --git a/tools/tracing/rtla/src/trace.c b/tools/tracing/rtla/src/trace.c
index 728f5029d533..74ed2f6208ba 100644
--- a/tools/tracing/rtla/src/trace.c
+++ b/tools/tracing/rtla/src/trace.c
@@ -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.47.2