linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>, Thomas Gleixner <tglx@linutronix.de>
Cc: Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	Clark Williams <williams@redhat.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	Jin Yao <yao.jin@linux.intel.com>,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Andi Kleen <ak@linux.intel.com>, Jin Yao <yao.jin@intel.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	Peter Zijlstra <peterz@infradead.org>
Subject: [PATCH 19/19] perf block-info: Support color ops to print block percents in color
Date: Tue, 10 Mar 2020 08:15:51 -0300	[thread overview]
Message-ID: <20200310111551.25160-20-acme@kernel.org> (raw)
In-Reply-To: <20200310111551.25160-1-acme@kernel.org>

From: Jin Yao <yao.jin@linux.intel.com>

It would be nice to print the block percents with colors.

This patch supports the 'Sampled Cycles%' and 'Avg Cycles%' printed in
colors.

For example,

perf record -b ...
perf report --total-cycles or perf report --total-cycles --stdio

percent > 5%, colored in red
percent > 0.5%, colored in green
percent < 0.5%, default color

Signed-off-by: Jin Yao <yao.jin@linux.intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Jin Yao <yao.jin@intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20200202141655.32053-5-yao.jin@linux.intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/block-info.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/tools/perf/util/block-info.c b/tools/perf/util/block-info.c
index debb8b12cf51..423ec69bda6c 100644
--- a/tools/perf/util/block-info.c
+++ b/tools/perf/util/block-info.c
@@ -181,6 +181,17 @@ static int block_column_width(struct perf_hpp_fmt *fmt,
 	return block_fmt->width;
 }
 
+static int color_pct(struct perf_hpp *hpp, int width, double pct)
+{
+#ifdef HAVE_SLANG_SUPPORT
+	if (use_browser) {
+		return __hpp__slsmg_color_printf(hpp, "%*.2f%%",
+						 width - 1, pct);
+	}
+#endif
+	return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, pct);
+}
+
 static int block_total_cycles_pct_entry(struct perf_hpp_fmt *fmt,
 					struct perf_hpp *hpp,
 					struct hist_entry *he)
@@ -188,14 +199,11 @@ static int block_total_cycles_pct_entry(struct perf_hpp_fmt *fmt,
 	struct block_fmt *block_fmt = container_of(fmt, struct block_fmt, fmt);
 	struct block_info *bi = he->block_info;
 	double ratio = 0.0;
-	char buf[16];
 
 	if (block_fmt->total_cycles)
 		ratio = (double)bi->cycles / (double)block_fmt->total_cycles;
 
-	sprintf(buf, "%.2f%%", 100.0 * ratio);
-
-	return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
+	return color_pct(hpp, block_fmt->width, 100.0 * ratio);
 }
 
 static int64_t block_total_cycles_pct_sort(struct perf_hpp_fmt *fmt,
@@ -248,16 +256,13 @@ static int block_cycles_pct_entry(struct perf_hpp_fmt *fmt,
 	struct block_info *bi = he->block_info;
 	double ratio = 0.0;
 	u64 avg;
-	char buf[16];
 
 	if (block_fmt->block_cycles && bi->num_aggr) {
 		avg = bi->cycles_aggr / bi->num_aggr;
 		ratio = (double)avg / (double)block_fmt->block_cycles;
 	}
 
-	sprintf(buf, "%.2f%%", 100.0 * ratio);
-
-	return scnprintf(hpp->buf, hpp->size, "%*s", block_fmt->width, buf);
+	return color_pct(hpp, block_fmt->width, 100.0 * ratio);
 }
 
 static int block_avg_cycles_entry(struct perf_hpp_fmt *fmt,
@@ -345,7 +350,7 @@ static void hpp_register(struct block_fmt *block_fmt, int idx,
 
 	switch (idx) {
 	case PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT:
-		fmt->entry = block_total_cycles_pct_entry;
+		fmt->color = block_total_cycles_pct_entry;
 		fmt->cmp = block_info__cmp;
 		fmt->sort = block_total_cycles_pct_sort;
 		break;
@@ -353,7 +358,7 @@ static void hpp_register(struct block_fmt *block_fmt, int idx,
 		fmt->entry = block_cycles_lbr_entry;
 		break;
 	case PERF_HPP_REPORT__BLOCK_CYCLES_PCT:
-		fmt->entry = block_cycles_pct_entry;
+		fmt->color = block_cycles_pct_entry;
 		break;
 	case PERF_HPP_REPORT__BLOCK_AVG_CYCLES:
 		fmt->entry = block_avg_cycles_entry;
-- 
2.21.1

      parent reply	other threads:[~2020-03-10 11:15 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-10 11:15 [GIT PULL] perf/core improvements and fixes Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 01/19] tools lib api fs: Move cgroupsfs_find_mountpoint() Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 02/19] perf stat: Show percore counts in per CPU output Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 03/19] perf llvm: Add debug hint message about missing kernel-devel package Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 04/19] perf annotate: Get rid of annotation->nr_jumps Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 05/19] libperf: Add counting example Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 06/19] tools lib traceevent: Remove extra '\n' in print_event_time() Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 07/19] tools headers UAPI: Update tools's copy of linux/perf_event.h Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 08/19] perf tools: Add hw_idx in struct branch_stack Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 09/19] perf evsel: Support PERF_SAMPLE_BRANCH_HW_INDEX Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 10/19] perf header: Add check for unexpected use of reserved membrs in event attr Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 11/19] perf expr: Add expr.c object Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 12/19] perf expr: Move expr lexer to flex Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 13/19] perf expr: Increase EXPR_MAX_OTHER to support metrics with more than 15 variables Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 14/19] perf expr: Straighten expr__parse()/expr__find_other() interface Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 15/19] perf expr: Make expr__parse() return -1 on error Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 16/19] perf block-info: Fix wrong block address comparison in block_info__cmp() Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 17/19] perf diff: Use __block_info__cmp() to replace block_pair_cmp() Arnaldo Carvalho de Melo
2020-03-10 11:15 ` [PATCH 18/19] perf block-info: Allow selecting which columns to report and its order Arnaldo Carvalho de Melo
2020-03-10 11:15 ` Arnaldo Carvalho de Melo [this message]

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=20200310111551.25160-20-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=williams@redhat.com \
    --cc=yao.jin@intel.com \
    --cc=yao.jin@linux.intel.com \
    /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;
as well as URLs for NNTP newsgroup(s).