mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Markus Elfring <Markus.Elfring@web.de>
To: linux-perf-users@vger.kernel.org,
	kernel-janitors@vger.kernel.org,
	Adrian Hunter <adrian.hunter@intel.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ian Rogers <irogers@google.com>, Ingo Molnar <mingo@redhat.com>,
	Jiri Olsa <jolsa@kernel.org>,
	Kan Liang <kan.liang@linux.intel.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Julia Lawall <julia.lawall@inria.fr>
Subject: [PATCH] perf: Optimise format string selections in three functions
Date: Fri, 19 Jul 2024 15:38:14 +0200	[thread overview]
Message-ID: <fcc9ebe9-e4a0-4395-b3f0-8c6d5fba56f2@web.de> (raw)

From: Markus Elfring <elfring@users.sourceforge.net>
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@users.sourceforge.net>
---
 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


                 reply	other threads:[~2024-07-19 13:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fcc9ebe9-e4a0-4395-b3f0-8c6d5fba56f2@web.de \
    --to=markus.elfring@web.de \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=julia.lawall@inria.fr \
    --cc=kan.liang@linux.intel.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®