rtla/timerlat: Use pretty formatting only on interactive tty
authorDaniel Bristot de Oliveira <bristot@kernel.org>
Wed, 24 Apr 2024 14:36:52 +0000 (16:36 +0200)
committerDaniel Bristot de Oliveira <bristot@kernel.org>
Wed, 15 May 2024 13:13:57 +0000 (15:13 +0200)
timerlat top does some background/font color formatting. While useful
on terminal, it breaks the output on other formats. For example, when
piping the output for pastebin tools, the format strings are printed
as characters. For instance:

  [2;37;40m                                     Timer Latency                                              [0;0;0m
    0 00:00:01   |          IRQ Timer Latency (us)        |         Thread Timer Latency (us)
  [2;30;47mCPU COUNT      |      cur       min       avg       max |      cur       min       avg       max[0;0;0m
    0 #1013      |        1         0         1        54 |        5         2         4        57
    1 #1013      |        3         0         1        10 |        6         2         4        15

To avoid this problem, do the formatting only if running on a tty,
and in !quiet mode.

Link: https://lkml.kernel.org/r/8288e1544ceab21557d5dda93a0f00339497c649.1713968967.git.bristot@kernel.org
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Juri Lelli <juri.lelli@redhat.com>
Signed-off-by: Daniel Bristot de Oliveira <bristot@kernel.org>
tools/tracing/rtla/src/timerlat_top.c

index 2665e0bb5f1eefb378ca103b8b6e0d905b104e08..c9cf90ed4e6dfd11a3568e24fe7fde3ea8003338 100644 (file)
@@ -44,6 +44,7 @@ struct timerlat_top_params {
        int                     hk_cpus;
        int                     user_top;
        int                     user_workload;
+       int                     pretty_output;
        cpu_set_t               hk_cpu_set;
        struct sched_attr       sched_param;
        struct trace_events     *events;
@@ -179,19 +180,22 @@ timerlat_top_handler(struct trace_seq *s, struct tep_record *record,
 /*
  * timerlat_top_header - print the header of the tool output
  */
-static void timerlat_top_header(struct osnoise_tool *top)
+static void timerlat_top_header(struct timerlat_top_params *params, struct osnoise_tool *top)
 {
-       struct timerlat_top_params *params = top->params;
        struct trace_seq *s = top->trace.seq;
        char duration[26];
 
        get_duration(top->start_time, duration, sizeof(duration));
 
-       trace_seq_printf(s, "\033[2;37;40m");
+       if (params->pretty_output)
+               trace_seq_printf(s, "\033[2;37;40m");
+
        trace_seq_printf(s, "                                     Timer Latency                                              ");
        if (params->user_top)
                trace_seq_printf(s, "                                         ");
-       trace_seq_printf(s, "\033[0;0;0m");
+
+       if (params->pretty_output)
+               trace_seq_printf(s, "\033[0;0;0m");
        trace_seq_printf(s, "\n");
 
        trace_seq_printf(s, "%-6s   |          IRQ Timer Latency (%s)        |         Thread Timer Latency (%s)", duration,
@@ -204,11 +208,15 @@ static void timerlat_top_header(struct osnoise_tool *top)
        }
 
        trace_seq_printf(s, "\n");
-       trace_seq_printf(s, "\033[2;30;47m");
+       if (params->pretty_output)
+               trace_seq_printf(s, "\033[2;30;47m");
+
        trace_seq_printf(s, "CPU COUNT      |      cur       min       avg       max |      cur       min       avg       max");
        if (params->user_top)
                trace_seq_printf(s, " |      cur       min       avg       max");
-       trace_seq_printf(s, "\033[0;0;0m");
+
+       if (params->pretty_output)
+               trace_seq_printf(s, "\033[0;0;0m");
        trace_seq_printf(s, "\n");
 }
 
@@ -305,7 +313,7 @@ timerlat_print_stats(struct timerlat_top_params *params, struct osnoise_tool *to
        if (!params->quiet)
                clear_terminal(trace->seq);
 
-       timerlat_top_header(top);
+       timerlat_top_header(params, top);
 
        for (i = 0; i < nr_cpus; i++) {
                if (params->cpus && !CPU_ISSET(i, &params->monitored_cpus))
@@ -693,6 +701,9 @@ timerlat_top_apply_config(struct osnoise_tool *top, struct timerlat_top_params *
                }
        }
 
+       if (isatty(1) && !params->quiet)
+               params->pretty_output = 1;
+
        return 0;
 
 out_err: