perf: Optimise format string selections in three functions
Subsystems (2)
PATCH 1/1 perf: Optimise format string selections in three functions
From: Markus Elfring <[email protected]>
Date: Fri, 19 Jul 2024 15:30:26 +0200
Adjust source code in three function implementations so that duplicate code can be avoided for a few format string selections.
This issue was transformed by using the Coccinelle software.
Signed-off-by Markus Elfring <[email protected]>
--- tools/perf/builtin-script.c | 29 +++++++++++++---------------- tools/perf/ui/stdio/hist.c | 11 +++-------- 2 files changed, 16 insertions(+), 24 deletions(-)
tools/perf/builtin-script.c +13 -16
@@ -796,16 +796,16 @@ static int perf_sample__fprintf_start(struct perf_script *script,if (PRINT_FIELD (VCPU ) && sample->machine_pid) printed +=fprintf (fp,"VCPU:%03d " , sample->vcpu); -if (PRINT_FIELD (COMM )) { -const char *comm = thread ?thread__comm_str (thread) :":-1" ; - -if (latency_format) - printed +=fprintf (fp,"%8.8s " , comm); -else if (PRINT_FIELD (IP ) &&evsel__has_callchain (evsel) && symbol_conf.use_callchain) - printed +=fprintf (fp,"%s " , comm); -else - printed +=fprintf (fp,"%16s " , comm); - } +if (PRINT_FIELD (COMM )) + printed +=fprintf (fp, + (latency_format + ?"%8.8s " + : ((PRINT_FIELD (IP ) && +evsel__has_callchain (evsel) && + symbol_conf.use_callchain) + ?"%s " + :"%16s " )), + (thread ?thread__comm_str (thread) :":-1" ));if (PRINT_FIELD (PID ) &&PRINT_FIELD (TID )) printed +=fprintf (fp,"%7d/%-7d " , sample->pid, sample->tid);
@@ -814,12 +814,9 @@ static int perf_sample__fprintf_start(struct perf_script *script,else if (PRINT_FIELD (TID )) printed +=fprintf (fp,"%7d " , sample->tid); -if (PRINT_FIELD (CPU )) { -if (latency_format) - printed +=fprintf (fp,"%3d " , sample->cpu); -else - printed +=fprintf (fp,"[%03d] " , sample->cpu); - } +if (PRINT_FIELD (CPU )) + printed +=fprintf (fp, (latency_format ?"%3d " :"[%03d] " ), + sample->cpu);if (PRINT_FIELD (MISC )) {int ret =0 ;
tools/perf/ui/stdio/hist.c +3 -8
@@ -37,10 +37,7 @@ static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,size_t ret =callchain__fprintf_left_margin (fp, left_margin);for (i =0 ; i < depth; i++) -if (depth_mask & (1 << i)) - ret +=fprintf (fp,"| " ); -else - ret +=fprintf (fp," " ); + ret +=fprintf (fp, ((depth_mask & (1 << i)) ?"| " :" " )); ret +=fprintf (fp,"\n" );
@@ -60,10 +57,8 @@ static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node, ret +=callchain__fprintf_left_margin (fp, left_margin);for (i =0 ; i < depth; i++) { -if (depth_mask & (1 << i)) - ret +=fprintf (fp,"|" ); -else - ret +=fprintf (fp," " ); + ret +=fprintf (fp, ((depth_mask & (1 << i)) ?"|" :" " )); +if (!period && i == depth -1 ) { ret +=fprintf (fp,"--" ); ret +=callchain_node__fprintf_value (node, fp, total_samples);
--
2.45.2