mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
To: acme@kernel.org, jolsa@redhat.com
Cc: alexander.shishkin@linux.intel.com, namhyung@kernel.org,
	yao.jin@linux.intel.com, linux-kernel@vger.kernel.org,
	yuzhoujian@didichuxing.com, tmricht@linux.vnet.ibm.com,
	anton@samba.org, Ravi Bangoria <ravi.bangoria@linux.ibm.com>
Subject: [RFC 2/2] perf stat: Fix shadow stats for clock events
Date: Thu, 15 Nov 2018 15:25:33 +0530	[thread overview]
Message-ID: <20181115095533.16930-2-ravi.bangoria@linux.ibm.com> (raw)
In-Reply-To: <20181115095533.16930-1-ravi.bangoria@linux.ibm.com>

Commit 0aa802a79469 ("perf stat: Get rid of extra clock display
function") introduced scale and unit for clock events. Thus,
perf_stat__update_shadow_stats() now saves scaled values of
clock events in msecs, instead of original nsecs. But while
calculating values of shadow stats we still consider clock
event values in nsecs. This results in a wrong shadow stat
values. Ex,

  # ./perf stat -e task-clock,cycles ls
    <SNIP>
              2.62 msec task-clock:u    #    0.624 CPUs utilized
         2,501,536      cycles:u        # 1250768.000 GHz

Fix this by considering clock events's saved stats in msecs:

  # ./perf stat -e task-clock,cycles ls
    <SNIP>
              2.42 msec task-clock:u    #    0.754 CPUs utilized
         2,338,747      cycles:u        #    1.169 GHz

Note:
The problem with this approach is, we are losing fractional part
while converting nsecs to msecs. This results in a sightly different
values of shadow stats.

Reported-by: Anton Blanchard <anton@samba.org>
Fixes: 0aa802a79469 ("perf stat: Get rid of extra clock display function")
Signed-off-by: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
---
 tools/perf/util/stat-shadow.c | 13 +++++++------
 tools/perf/util/stat.h        |  2 +-
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index f0a8cec55c47..5b28b278a24e 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <stdio.h>
+#include <linux/time64.h>
 #include "evsel.h"
 #include "stat.h"
 #include "color.h"
@@ -213,7 +214,7 @@ void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
 	count *= counter->scale;
 
 	if (perf_evsel__is_clock(counter))
-		update_runtime_stat(st, STAT_NSECS, 0, cpu, count);
+		update_runtime_stat(st, STAT_MSECS, 0, cpu, count);
 	else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
 		update_runtime_stat(st, STAT_CYCLES, ctx, cpu, count);
 	else if (perf_stat_evsel__is(counter, CYCLES_IN_TX))
@@ -873,10 +874,10 @@ void perf_stat__print_shadow_stats(struct perf_stat_config *config,
 	} else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
 		print_stalled_cycles_backend(config, cpu, evsel, avg, out, st);
 	} else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
-		total = runtime_stat_avg(st, STAT_NSECS, 0, cpu);
+		total = runtime_stat_avg(st, STAT_MSECS, 0, cpu);
 
 		if (total) {
-			ratio = avg / total;
+			ratio = avg / (total * NSEC_PER_MSEC);
 			print_metric(config, ctxp, NULL, "%8.3f", "GHz", ratio);
 		} else {
 			print_metric(config, ctxp, NULL, NULL, "Ghz", 0);
@@ -972,14 +973,14 @@ void perf_stat__print_shadow_stats(struct perf_stat_config *config,
 	} else if (evsel->metric_expr) {
 		generic_metric(config, evsel->metric_expr, evsel->metric_events, evsel->name,
 				evsel->metric_name, avg, cpu, out, st);
-	} else if (runtime_stat_n(st, STAT_NSECS, 0, cpu) != 0) {
+	} else if (runtime_stat_n(st, STAT_MSECS, 0, cpu) != 0) {
 		char unit = 'M';
 		char unit_buf[10];
 
-		total = runtime_stat_avg(st, STAT_NSECS, 0, cpu);
+		total = runtime_stat_avg(st, STAT_MSECS, 0, cpu);
 
 		if (total)
-			ratio = 1000.0 * avg / total;
+			ratio = 1000.0 * avg / (total * NSEC_PER_MSEC);
 		if (ratio < 0.001) {
 			ratio *= 1000;
 			unit = 'K';
diff --git a/tools/perf/util/stat.h b/tools/perf/util/stat.h
index 2f9c9159a364..941ad4b0836c 100644
--- a/tools/perf/util/stat.h
+++ b/tools/perf/util/stat.h
@@ -62,7 +62,7 @@ enum {
 
 enum stat_type {
 	STAT_NONE = 0,
-	STAT_NSECS,
+	STAT_MSECS,			/* Milliseconds */
 	STAT_CYCLES,
 	STAT_STALLED_CYCLES_FRONT,
 	STAT_STALLED_CYCLES_BACK,
-- 
2.17.1


  reply	other threads:[~2018-11-15  9:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-15  9:55 [PATCH 1/2] perf stat: Use perf_evsel__is_clocki() " Ravi Bangoria
2018-11-15  9:55 ` Ravi Bangoria [this message]
2018-11-15 14:17   ` [RFC 2/2] perf stat: Fix shadow stats " Jiri Olsa
2018-11-16  4:28     ` [PATCH] " Ravi Bangoria
2018-11-16 13:35       ` Jiri Olsa
2018-11-26 18:45         ` Arnaldo Carvalho de Melo
2018-11-27  8:20         ` Ravi Bangoria
2018-11-27 12:37           ` Arnaldo Carvalho de Melo
2018-12-14 20:19       ` [tip:perf/core] " tip-bot for Ravi Bangoria
2018-12-18 13:46       ` tip-bot for Ravi Bangoria
2018-11-15 13:55 ` [PATCH 1/2] perf stat: Use perf_evsel__is_clocki() " Jiri Olsa
2018-11-15 19:22   ` Arnaldo Carvalho de Melo
2018-11-22  7:13 ` [tip:perf/core] " tip-bot for Ravi Bangoria

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=20181115095533.16930-2-ravi.bangoria@linux.ibm.com \
    --to=ravi.bangoria@linux.ibm.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=anton@samba.org \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namhyung@kernel.org \
    --cc=tmricht@linux.vnet.ibm.com \
    --cc=yao.jin@linux.intel.com \
    --cc=yuzhoujian@didichuxing.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

all inboxes | Powered by JetHome®