Skip to contentSkip to conversation

perf stat: Use perf_evsel__is_clocki() for clock events

PATCH Applied Ravi Bangoria 4 participants 13 messages Nov 15, 2018 – Dec 18, 2018 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 perf stat: Use perf_evsel__is_clocki() for clock events

We already have function to check if a given event is either SW_CPU_CLOCK or SW_TASK_CLOCK. Utilize it.

---
 tools/perf/util/stat-shadow.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/perf/util/stat-shadow.c +1 -2
index 8ad32763cfff..f0a8cec55c47 100644
@@ -212,8 +212,7 @@ void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
 
 	count *= counter->scale;
 
-	if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
-	    perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
+	if (perf_evsel__is_clock(counter))
 		update_runtime_stat(st, STAT_NSECS, 0, cpu, count);
 	else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
 		update_runtime_stat(st, STAT_CYCLES, ctx, cpu, count);
Signature · 1 line
2.17.1
#1 lore raw

RFC 2/2 perf stat: Fix shadow stats for clock events

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.

Fixes 0aa802a79469 ("perf stat: Get rid of extra clock display function")
---
 tools/perf/util/stat-shadow.c | 13 +++++++------
 tools/perf/util/stat.h        |  2 +-
 2 files changed, 8 insertions(+), 7 deletions(-)
tools/perf/util/stat-shadow.c +7 -6
index f0a8cec55c47..5b28b278a24e 100644
@@ -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';
tools/perf/util/stat.h +1 -1
index 2f9c9159a364..941ad4b0836c 100644
@@ -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,
Signature · 1 line
2.17.1
#2 lore raw

Re: [RFC 2/2] perf stat: Fix shadow stats for clock events

Quoted 20 lines · Ravi Bangoria

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
Quoted 4 lines

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.

yea, could we just leave the NSEC instead? like below

jirka

---

tools/perf/util/stat-shadow.c +2 -1
index f4bad808bdd9..da8857df238e 100644
@@ -209,11 +209,12 @@ void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
 				    int cpu, struct runtime_stat *st)
 {
 	int ctx = evsel_context(counter);
+	u64 count_ns = count;
 
 	count *= counter->scale;
 
 	if (perf_evsel__is_clock(counter))
-		update_runtime_stat(st, STAT_NSECS, 0, cpu, count);
+		update_runtime_stat(st, STAT_NSECS, 0, cpu, count_ns);
 	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))
#3 lore raw

PATCH 1/1 perf stat: Fix shadow stats for clock events

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.60 msec task-clock:u    #    0.877 CPUs utilized
         2,430,564      cycles:u        # 1215282.000 GHz

Fix this by saving original nsec values for clock events in perf_stat__update_shadow_stats(). After patch:

  # ./perf stat -e task-clock,cycles ls
    <SNIP>
              3.14 msec task-clock:u    #    0.839 CPUs utilized
         3,094,528      cycles:u        #    0.985 GHz
Fixes 0aa802a79469 ("perf stat: Get rid of extra clock display function")
---
 tools/perf/util/stat-shadow.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/stat-shadow.c +2 -1
index f0a8cec55c47..3c22c58b3e90 100644
@@ -209,11 +209,12 @@ void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
 				    int cpu, struct runtime_stat *st)
 {
 	int ctx = evsel_context(counter);
+	u64 count_ns = count;
 
 	count *= counter->scale;
 
 	if (perf_evsel__is_clock(counter))
-		update_runtime_stat(st, STAT_NSECS, 0, cpu, count);
+		update_runtime_stat(st, STAT_NSECS, 0, cpu, count_ns);
 	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))
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#4 lore raw

Re: [PATCH] perf stat: Fix shadow stats for clock events

Quoted 21 lines · Ravi Bangoria

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.60 msec task-clock:u    #    0.877 CPUs utilized
         2,430,564      cycles:u        # 1215282.000 GHz

Fix this by saving original nsec values for clock events in perf_stat__update_shadow_stats(). After patch:

  # ./perf stat -e task-clock,cycles ls
    <SNIP>
              3.14 msec task-clock:u    #    0.839 CPUs utilized
         3,094,528      cycles:u        #    0.985 GHz
Quoted 4 lines
Fixes 0aa802a79469 ("perf stat: Get rid of extra clock display function")

thanks,
jirka

Quoted 25 lines
---
 tools/perf/util/stat-shadow.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/stat-shadow.c +2 -1
index f0a8cec55c47..3c22c58b3e90 100644
@@ -209,11 +209,12 @@ void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
 				    int cpu, struct runtime_stat *st)
 {
 	int ctx = evsel_context(counter);
+	u64 count_ns = count;
 
 	count *= counter->scale;
 
 	if (perf_evsel__is_clock(counter))
-		update_runtime_stat(st, STAT_NSECS, 0, cpu, count);
+		update_runtime_stat(st, STAT_NSECS, 0, cpu, count_ns);
 	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))
Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH] perf stat: Fix shadow stats for clock events

Quoted 22 lines · Jiri Olsa
Quoted 21 lines · Ravi Bangoria

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.60 msec task-clock:u    #    0.877 CPUs utilized
         2,430,564      cycles:u        # 1215282.000 GHz

Fix this by saving original nsec values for clock events in perf_stat__update_shadow_stats(). After patch:

  # ./perf stat -e task-clock,cycles ls
    <SNIP>
              3.14 msec task-clock:u    #    0.839 CPUs utilized
         3,094,528      cycles:u        #    0.985 GHz
Quoted 6 lines
Quoted 4 lines
Fixes 0aa802a79469 ("perf stat: Get rid of extra clock display function")

Hi Arnaldo, Please pull this.

Thanks.

#6 lore raw

Re: [PATCH] perf stat: Fix shadow stats for clock events

Em Tue, Nov 27, 2018 at 01:50:44PM +0530, Ravi Bangoria escreveu:

Quoted 25 lines · Jiri Olsa
Quoted 22 lines · Ravi Bangoria
Quoted 21 lines

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.60 msec task-clock:u    #    0.877 CPUs utilized
         2,430,564      cycles:u        # 1215282.000 GHz

Fix this by saving original nsec values for clock events in perf_stat__update_shadow_stats(). After patch:

  # ./perf stat -e task-clock,cycles ls
    <SNIP>
              3.14 msec task-clock:u    #    0.839 CPUs utilized
         3,094,528      cycles:u        #    0.985 GHz
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Fixes 0aa802a79469 ("perf stat: Get rid of extra clock display function")

Hi Arnaldo, Please pull this.

I did it yesterday, its just that I installed a new notebook and the MTA was down :-\ All should be fixed by now and the patch should go Ingo's way soon.

- Arnaldo

maintainer Applied
#7 lore raw

Re: [PATCH] perf stat: Fix shadow stats for clock events

In reply to Jiri Olsa

Em Fri, Nov 16, 2018 at 02:35:55PM +0100, Jiri Olsa escreveu:

Quoted 22 lines · Ravi Bangoria
Quoted 21 lines

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.60 msec task-clock:u    #    0.877 CPUs utilized
         2,430,564      cycles:u        # 1215282.000 GHz

Fix this by saving original nsec values for clock events in perf_stat__update_shadow_stats(). After patch:

  # ./perf stat -e task-clock,cycles ls
    <SNIP>
              3.14 msec task-clock:u    #    0.839 CPUs utilized
         3,094,528      cycles:u        #    0.985 GHz
Quoted 6 lines
Quoted 4 lines
Fixes 0aa802a79469 ("perf stat: Get rid of extra clock display function")

Thanks, applied.

- Arnaldo

#8 lore raw

[tip:perf/core] perf stat: Fix shadow stats for clock events

In reply to Ravi Bangoria
perf stat: Fix shadow stats for clock events
Commit-ID:  6e269c85dcea8a41faac44dbd5130843080f0576
Gitweb:     https://git.kernel.org/tip/6e269c85dcea8a41faac44dbd5130843080f0576
Author:     Ravi Bangoria <[email protected]>
AuthorDate: Fri, 16 Nov 2018 09:58:43 +0530
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 29 Nov 2018 20:42:46 -0300

perf stat: Fix shadow stats for clock events

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.60 msec task-clock:u    #    0.877 CPUs utilized
         2,430,564      cycles:u        # 1215282.000 GHz

Fix this by saving original nsec values for clock events in perf_stat__update_shadow_stats(). After patch:

  # ./perf stat -e task-clock,cycles ls
    <SNIP>
              3.14 msec task-clock:u    #    0.839 CPUs utilized
         3,094,528      cycles:u        #    0.985 GHz
Cc Alexander Shishkin <[email protected]>
Cc Jin Yao <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Thomas Richter <[email protected]>
Fixes 0aa802a79469 ("perf stat: Get rid of extra clock display function")
---
 tools/perf/util/stat-shadow.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/stat-shadow.c +2 -1
index f0a8cec55c47..3c22c58b3e90 100644
@@ -209,11 +209,12 @@ void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
 				    int cpu, struct runtime_stat *st)
 {
 	int ctx = evsel_context(counter);
+	u64 count_ns = count;
 
 	count *= counter->scale;
 
 	if (perf_evsel__is_clock(counter))
-		update_runtime_stat(st, STAT_NSECS, 0, cpu, count);
+		update_runtime_stat(st, STAT_NSECS, 0, cpu, count_ns);
 	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))
#9 lore raw

[tip:perf/core] perf stat: Fix shadow stats for clock events

In reply to Ravi Bangoria
perf stat: Fix shadow stats for clock events
Commit-ID:  57ddf09173c1e7d0511ead8924675c7198e56545
Gitweb:     https://git.kernel.org/tip/57ddf09173c1e7d0511ead8924675c7198e56545
Author:     Ravi Bangoria <[email protected]>
AuthorDate: Fri, 16 Nov 2018 09:58:43 +0530
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Mon, 17 Dec 2018 14:53:30 -0300

perf stat: Fix shadow stats for clock events

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.60 msec task-clock:u    #    0.877 CPUs utilized
         2,430,564      cycles:u        # 1215282.000 GHz

Fix this by saving original nsec values for clock events in perf_stat__update_shadow_stats(). After patch:

  # ./perf stat -e task-clock,cycles ls
    <SNIP>
              3.14 msec task-clock:u    #    0.839 CPUs utilized
         3,094,528      cycles:u        #    0.985 GHz
Cc Alexander Shishkin <[email protected]>
Cc Jin Yao <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Thomas Richter <[email protected]>
Fixes 0aa802a79469 ("perf stat: Get rid of extra clock display function")
---
 tools/perf/util/stat-shadow.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/stat-shadow.c +2 -1
index f0a8cec55c47..3c22c58b3e90 100644
@@ -209,11 +209,12 @@ void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
 				    int cpu, struct runtime_stat *st)
 {
 	int ctx = evsel_context(counter);
+	u64 count_ns = count;
 
 	count *= counter->scale;
 
 	if (perf_evsel__is_clock(counter))
-		update_runtime_stat(st, STAT_NSECS, 0, cpu, count);
+		update_runtime_stat(st, STAT_NSECS, 0, cpu, count_ns);
 	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))
#12 lore raw
In reply to Ravi Bangoria
perf stat: Use perf_evsel__is_clocki() for clock events
Commit-ID:  eb08d006054e7e374592068919e32579988602d4
Gitweb:     https://git.kernel.org/tip/eb08d006054e7e374592068919e32579988602d4
Author:     Ravi Bangoria <[email protected]>
AuthorDate: Thu, 15 Nov 2018 15:25:32 +0530
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 21 Nov 2018 22:39:57 -0300

perf stat: Use perf_evsel__is_clocki() for clock events

We already have function to check if a given event is either SW_CPU_CLOCK or SW_TASK_CLOCK. Utilize it.

---
 tools/perf/util/stat-shadow.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/perf/util/stat-shadow.c +1 -2
index 8ad32763cfff..f0a8cec55c47 100644
@@ -212,8 +212,7 @@ void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
 
 	count *= counter->scale;
 
-	if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
-	    perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
+	if (perf_evsel__is_clock(counter))
 		update_runtime_stat(st, STAT_NSECS, 0, cpu, count);
 	else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
 		update_runtime_stat(st, STAT_CYCLES, ctx, cpu, count);