diff mbox series

[v1] rtla: Refactor save_trace_to_file

Message ID 20250219115138.406075-1-costa.shul@redhat.com (mailing list archive)
State New
Headers show
Series [v1] rtla: Refactor save_trace_to_file | expand

Commit Message

Costa Shulyupin Feb. 19, 2025, 11:51 a.m. UTC
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.

Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>
---
 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(-)

Comments

Tomas Glozar Feb. 19, 2025, 12:49 p.m. UTC | #1
st 19. 2. 2025 v 12:52 odesílatel Costa Shulyupin
<costa.shul@redhat.com> napsal:
>
> 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/20250218145859.27762-1-tglozar@redhat.com/

> Refactor by consolidating the duplicate lines into the
> save_trace_to_file() function.
>
> Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>

Thank you for the cleanup patch.

Reviewed-by: Tomas Glozar <tglozar@redhat.com>
Tested-by: Tomas Glozar <tglozar@redhat.com>

> ---
>  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 b4930b835b0a1..7c6ef67ef3e6c 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 4772677ac762c..0eeefbbbf3173 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 6d7d0a2d45b47..5f54b268a1033 100644
> --- a/tools/tracing/rtla/src/timerlat_hist.c
> +++ b/tools/tracing/rtla/src/timerlat_hist.c
> @@ -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:
> diff --git a/tools/tracing/rtla/src/timerlat_top.c b/tools/tracing/rtla/src/timerlat_top.c
> index 05a9403b01d26..1e64e6216bc2e 100644
> --- a/tools/tracing/rtla/src/timerlat_top.c
> +++ b/tools/tracing/rtla/src/timerlat_top.c
> @@ -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
> diff --git a/tools/tracing/rtla/src/trace.c b/tools/tracing/rtla/src/trace.c
> index 728f5029d5335..74ed2f6208baa 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.48.1
>
diff mbox series

Patch

diff --git a/tools/tracing/rtla/src/osnoise_hist.c b/tools/tracing/rtla/src/osnoise_hist.c
index b4930b835b0a1..7c6ef67ef3e6c 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 4772677ac762c..0eeefbbbf3173 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 6d7d0a2d45b47..5f54b268a1033 100644
--- a/tools/tracing/rtla/src/timerlat_hist.c
+++ b/tools/tracing/rtla/src/timerlat_hist.c
@@ -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:
diff --git a/tools/tracing/rtla/src/timerlat_top.c b/tools/tracing/rtla/src/timerlat_top.c
index 05a9403b01d26..1e64e6216bc2e 100644
--- a/tools/tracing/rtla/src/timerlat_top.c
+++ b/tools/tracing/rtla/src/timerlat_top.c
@@ -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
diff --git a/tools/tracing/rtla/src/trace.c b/tools/tracing/rtla/src/trace.c
index 728f5029d5335..74ed2f6208baa 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);