[PATCH] perf: Optimise format string selections in three functions

From: Markus Elfring
Date: Fri Jul 19 2024 - 09:39:02 EST


From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
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 <elfring@xxxxxxxxxxxxxxxxxxxxx>
---
tools/perf/builtin-script.c | 29 +++++++++++++----------------
tools/perf/ui/stdio/hist.c | 11 +++--------
2 files changed, 16 insertions(+), 24 deletions(-)

diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index c16224b1fef3..ec6807f00c54 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -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;
diff --git a/tools/perf/ui/stdio/hist.c b/tools/perf/ui/stdio/hist.c
index 9372e8904d22..899019e8aab8 100644
--- a/tools/perf/ui/stdio/hist.c
+++ b/tools/perf/ui/stdio/hist.c
@@ -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