Skip to contentSkip to conversation

perf stat: Improve runtime stat for interval mode

PATCH Applied Jin Yao 5 participants 8 messages Apr 20, 2020 – May 8, 2020 perf events

lore
Share & tools
lore

Jin Yao

author
#0 lore raw

PATCH v2 1/1 perf stat: Improve runtime stat for interval mode

For interval mode, the metric is printed after # if it exists. But it's not calculated by the counts generated in this interval. See following examples,

 root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
 #           time             counts unit events
      1.000422803            764,809      inst_retired.any          #      2.9 CPI
      1.000422803          2,234,932      cycles
      2.001464585          1,960,061      inst_retired.any          #      1.6 CPI
      2.001464585          4,022,591      cycles

The second CPI should not be 1.6 (4,022,591/1,960,061 is 2.1)

 root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
 #           time             counts unit events
      1.000429493          2,869,311      cycles
      1.000429493            816,875      instructions              #    0.28  insn per cycle
      2.001516426          9,260,973      cycles
      2.001516426          5,250,634      instructions              #    0.87  insn per cycle

The second 'insn per cycle' should not be 0.87 (5,250,634/9,260,973 is 0.57).

The current code uses a global variable rt_stat for tracking and updating the std dev of runtime stat. Unlike the counts, rt_stat is not reset for interval. While the counts are reset for interval.

perf_stat_process_counter()

{
        if (config->interval)
                init_stats(ps->res_stats);
}

So for interval, the rt_stat should be reset either.

This patch resets rt_stat before read_counters, so the runtime stat is only calculated by the counts generated in this interval.

With this patch,

 root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
 #           time             counts unit events
      1.000420924          2,408,818      inst_retired.any          #      2.1 CPI
      1.000420924          5,010,111      cycles
      2.001448579          2,798,407      inst_retired.any          #      1.6 CPI
      2.001448579          4,599,861      cycles
 root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
 #           time             counts unit events
      1.000428555          2,769,714      cycles
      1.000428555            774,462      instructions              #    0.28  insn per cycle
      2.001471562          3,595,904      cycles
      2.001471562          1,243,703      instructions              #    0.35  insn per cycle

Now the second 'insn per cycle' and CPI are calculated by the counts generated in this interval.

v2:
---
Use just existing perf_stat__reset_shadow_per_stat(&rt_stat). We don't need to define new function perf_stat__reset_rt_stat.

---
 tools/perf/Documentation/perf-stat.txt | 2 ++
 tools/perf/builtin-stat.c              | 1 +
 2 files changed, 3 insertions(+)
tools/perf/Documentation/perf-stat.txt +2 -0
index 4d56586b2fb9..3fb5028aef08 100644
@@ -176,6 +176,8 @@ Print count deltas every N milliseconds (minimum: 1ms)
 The overhead percentage could be high in some cases, for instance with small, sub 100ms intervals.  Use with caution.
 	example: 'perf stat -I 1000 -e cycles -a sleep 5'
 
+If the metric exists, it is calculated by the counts generated in this interval and the metric is printed after #.
+
 --interval-count times::
 Print count deltas for fixed number of times.
 This option should be used together with "-I" option.
tools/perf/builtin-stat.c +1 -0
index 9207b6c45475..3f050d85c277 100644
@@ -359,6 +359,7 @@ static void process_interval(void)
 	clock_gettime(CLOCK_MONOTONIC, &ts);
 	diff_timespec(&rs, &ts, &ref_time);
 
+	perf_stat__reset_shadow_per_stat(&rt_stat);
 	read_counters(&rs);
 
 	if (STAT_RECORD) {
Signature · 1 line
2.17.1
Tested-by on v2 1/1
#1 lore raw
Quoted 85 lines · Jin Yao

For interval mode, the metric is printed after # if it exists. But it's not calculated by the counts generated in this interval. See following examples,

 root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
 #           time             counts unit events
      1.000422803            764,809      inst_retired.any          #      2.9 CPI
      1.000422803          2,234,932      cycles
      2.001464585          1,960,061      inst_retired.any          #      1.6 CPI
      2.001464585          4,022,591      cycles

The second CPI should not be 1.6 (4,022,591/1,960,061 is 2.1)

 root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
 #           time             counts unit events
      1.000429493          2,869,311      cycles
      1.000429493            816,875      instructions              #    0.28  insn per cycle
      2.001516426          9,260,973      cycles
      2.001516426          5,250,634      instructions              #    0.87  insn per cycle

The second 'insn per cycle' should not be 0.87 (5,250,634/9,260,973 is 0.57).

The current code uses a global variable rt_stat for tracking and updating the std dev of runtime stat. Unlike the counts, rt_stat is not reset for interval. While the counts are reset for interval.

perf_stat_process_counter()

{
        if (config->interval)
                init_stats(ps->res_stats);
}

So for interval, the rt_stat should be reset either.

This patch resets rt_stat before read_counters, so the runtime stat is only calculated by the counts generated in this interval.

With this patch,

 root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
 #           time             counts unit events
      1.000420924          2,408,818      inst_retired.any          #      2.1 CPI
      1.000420924          5,010,111      cycles
      2.001448579          2,798,407      inst_retired.any          #      1.6 CPI
      2.001448579          4,599,861      cycles
 root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
 #           time             counts unit events
      1.000428555          2,769,714      cycles
      1.000428555            774,462      instructions              #    0.28  insn per cycle
      2.001471562          3,595,904      cycles
      2.001471562          1,243,703      instructions              #    0.35  insn per cycle

Now the second 'insn per cycle' and CPI are calculated by the counts generated in this interval.

v2:
---
Use just existing perf_stat__reset_shadow_per_stat(&rt_stat). We don't need to define new function perf_stat__reset_rt_stat.

---
 tools/perf/Documentation/perf-stat.txt | 2 ++
 tools/perf/builtin-stat.c              | 1 +
 2 files changed, 3 insertions(+)
tools/perf/Documentation/perf-stat.txt +2 -0
index 4d56586b2fb9..3fb5028aef08 100644
@@ -176,6 +176,8 @@ Print count deltas every N milliseconds (minimum: 1ms)
 The overhead percentage could be high in some cases, for instance with small, sub 100ms intervals.  Use with caution.
 	example: 'perf stat -I 1000 -e cycles -a sleep 5'
 
+If the metric exists, it is calculated by the counts generated in this interval and the metric is printed after #.
+
 --interval-count times::
 Print count deltas for fixed number of times.
 This option should be used together with "-I" option.
tools/perf/builtin-stat.c +0 -0
index 9207b6c45475..3f050d85c277 100644

tools/perf/builtin-stat.c static void process_interval(void) · line 362

Quoted 4 lines
tools/perf/builtin-stat.c +1 -0
@@ -359,6 +359,7 @@ static void process_interval(void)
 	clock_gettime(CLOCK_MONOTONIC, &ts);
 	diff_timespec(&rs, &ts, &ref_time);
 
+	perf_stat__reset_shadow_per_stat(&rt_stat);

Thanks,
Kajol

Quoted 4 lines
tools/perf/builtin-stat.c +0 -0
 	read_counters(&rs);
 
 	if (STAT_RECORD) {
#2 lore raw
In reply to Jin Yao

Em Mon, Apr 20, 2020 at 10:54:17PM +0800, Jin Yao escreveu:

Quoted 26 lines

For interval mode, the metric is printed after # if it exists. But it's not calculated by the counts generated in this interval. See following examples,

 root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
 #           time             counts unit events
      1.000422803            764,809      inst_retired.any          #      2.9 CPI
      1.000422803          2,234,932      cycles
      2.001464585          1,960,061      inst_retired.any          #      1.6 CPI
      2.001464585          4,022,591      cycles

The second CPI should not be 1.6 (4,022,591/1,960,061 is 2.1)

 root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
 #           time             counts unit events
      1.000429493          2,869,311      cycles
      1.000429493            816,875      instructions              #    0.28  insn per cycle
      2.001516426          9,260,973      cycles
      2.001516426          5,250,634      instructions              #    0.87  insn per cycle

The second 'insn per cycle' should not be 0.87 (5,250,634/9,260,973 is 0.57).

The current code uses a global variable rt_stat for tracking and updating the std dev of runtime stat. Unlike the counts, rt_stat is not reset for interval. While the counts are reset for interval.

Quoted 7 lines

perf_stat_process_counter()

{
        if (config->interval)
                init_stats(ps->res_stats);
}

So for interval, the rt_stat should be reset either.

s/either/too/g right?

And please try and find what was the cset that introduced the problem, so that we can have a Fixes: line and the stable series can pick it, ok?

- Arnaldo

Quoted 61 lines

This patch resets rt_stat before read_counters, so the runtime stat is only calculated by the counts generated in this interval.

With this patch,

 root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
 #           time             counts unit events
      1.000420924          2,408,818      inst_retired.any          #      2.1 CPI
      1.000420924          5,010,111      cycles
      2.001448579          2,798,407      inst_retired.any          #      1.6 CPI
      2.001448579          4,599,861      cycles
 root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
 #           time             counts unit events
      1.000428555          2,769,714      cycles
      1.000428555            774,462      instructions              #    0.28  insn per cycle
      2.001471562          3,595,904      cycles
      2.001471562          1,243,703      instructions              #    0.35  insn per cycle

Now the second 'insn per cycle' and CPI are calculated by the counts generated in this interval.

v2:
---
Use just existing perf_stat__reset_shadow_per_stat(&rt_stat). We don't need to define new function perf_stat__reset_rt_stat.

---
 tools/perf/Documentation/perf-stat.txt | 2 ++
 tools/perf/builtin-stat.c              | 1 +
 2 files changed, 3 insertions(+)
tools/perf/Documentation/perf-stat.txt +2 -0
index 4d56586b2fb9..3fb5028aef08 100644
@@ -176,6 +176,8 @@ Print count deltas every N milliseconds (minimum: 1ms)
 The overhead percentage could be high in some cases, for instance with small, sub 100ms intervals.  Use with caution.
 	example: 'perf stat -I 1000 -e cycles -a sleep 5'
 
+If the metric exists, it is calculated by the counts generated in this interval and the metric is printed after #.
+
 --interval-count times::
 Print count deltas for fixed number of times.
 This option should be used together with "-I" option.
tools/perf/builtin-stat.c +1 -0
index 9207b6c45475..3f050d85c277 100644
@@ -359,6 +359,7 @@ static void process_interval(void)
 	clock_gettime(CLOCK_MONOTONIC, &ts);
 	diff_timespec(&rs, &ts, &ref_time);
 
+	perf_stat__reset_shadow_per_stat(&rt_stat);
 	read_counters(&rs);
 
 	if (STAT_RECORD) {
Signature · 1 line
2.17.1
Signature · 1 line
- Arnaldo
author
#3 lore raw

Hi Arnaldo,

Quoted 33 lines · Arnaldo Carvalho de Melo

Em Mon, Apr 20, 2020 at 10:54:17PM +0800, Jin Yao escreveu:

Quoted 32 lines

For interval mode, the metric is printed after # if it exists. But it's not calculated by the counts generated in this interval. See following examples,

  root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
  #           time             counts unit events
       1.000422803            764,809      inst_retired.any          #      2.9 CPI
       1.000422803          2,234,932      cycles
       2.001464585          1,960,061      inst_retired.any          #      1.6 CPI
       2.001464585          4,022,591      cycles

The second CPI should not be 1.6 (4,022,591/1,960,061 is 2.1)

  root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
  #           time             counts unit events
       1.000429493          2,869,311      cycles
       1.000429493            816,875      instructions              #    0.28  insn per cycle
       2.001516426          9,260,973      cycles
       2.001516426          5,250,634      instructions              #    0.87  insn per cycle

The second 'insn per cycle' should not be 0.87 (5,250,634/9,260,973 is 0.57).

The current code uses a global variable rt_stat for tracking and updating the std dev of runtime stat. Unlike the counts, rt_stat is not reset for interval. While the counts are reset for interval.

perf_stat_process_counter()

{
         if (config->interval)
                 init_stats(ps->res_stats);
}
Quoted 4 lines
Quoted 1 line

So for interval, the rt_stat should be reset either.

s/either/too/g right?

Yes, should use "too" here. :)

Quoted 5 lines

And please try and find what was the cset that introduced the problem, so that we can have a Fixes: line and the stable series can pick it, ok?

- Arnaldo

I have tried to find the patch which introduced this issue.

51fd2df1e882 ("perf stat: Fix interval output values").

This patch zeros stats for interval mode. I just think it should reset rt_stat too.

But I really don't know if it's fair to this patch so I don't add it in my patch description.

Thanks
Jin Yao

Quoted 62 lines
Quoted 61 lines

This patch resets rt_stat before read_counters, so the runtime stat is only calculated by the counts generated in this interval.

With this patch,

  root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
  #           time             counts unit events
       1.000420924          2,408,818      inst_retired.any          #      2.1 CPI
       1.000420924          5,010,111      cycles
       2.001448579          2,798,407      inst_retired.any          #      1.6 CPI
       2.001448579          4,599,861      cycles
  root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
  #           time             counts unit events
       1.000428555          2,769,714      cycles
       1.000428555            774,462      instructions              #    0.28  insn per cycle
       2.001471562          3,595,904      cycles
       2.001471562          1,243,703      instructions              #    0.35  insn per cycle

Now the second 'insn per cycle' and CPI are calculated by the counts generated in this interval.

v2:
---
Use just existing perf_stat__reset_shadow_per_stat(&rt_stat). We don't need to define new function perf_stat__reset_rt_stat.

---
  tools/perf/Documentation/perf-stat.txt | 2 ++
  tools/perf/builtin-stat.c              | 1 +
  2 files changed, 3 insertions(+)
tools/perf/Documentation/perf-stat.txt +2 -0
index 4d56586b2fb9..3fb5028aef08 100644
@@ -176,6 +176,8 @@ Print count deltas every N milliseconds (minimum: 1ms)
  The overhead percentage could be high in some cases, for instance with small, sub 100ms intervals.  Use with caution.
  	example: 'perf stat -I 1000 -e cycles -a sleep 5'
 
+If the metric exists, it is calculated by the counts generated in this interval and the metric is printed after #.
+
  --interval-count times::
  Print count deltas for fixed number of times.
  This option should be used together with "-I" option.
tools/perf/builtin-stat.c +1 -0
index 9207b6c45475..3f050d85c277 100644
@@ -359,6 +359,7 @@ static void process_interval(void)
  	clock_gettime(CLOCK_MONOTONIC, &ts);
  	diff_timespec(&rs, &ts, &ref_time);
 
+	perf_stat__reset_shadow_per_stat(&rt_stat);
  	read_counters(&rs);
 
  	if (STAT_RECORD) {
Signature · 1 line
2.17.1
#4 lore raw
Quoted 42 lines · Yao

Hi Arnaldo,

Quoted 8 lines · Arnaldo Carvalho de Melo

Em Mon, Apr 20, 2020 at 10:54:17PM +0800, Jin Yao escreveu:

Quoted 7 lines

For interval mode, the metric is printed after # if it exists. But it's not calculated by the counts generated in this interval. See following examples,

  root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
  #           time             counts unit events
       1.000422803            764,809      inst_retired.any         

# 2.9 CPI

Quoted 2 lines
Quoted 2 lines
       1.000422803          2,234,932      cycles
       2.001464585          1,960,061      inst_retired.any         

# 1.6 CPI

Quoted 5 lines
Quoted 5 lines
       2.001464585          4,022,591      cycles

The second CPI should not be 1.6 (4,022,591/1,960,061 is 2.1)

  root@kbl-ppc:~# perf stat -e cycles,instructions -I1000

--interval-count 2

Quoted 3 lines
Quoted 3 lines

# time counts unit events

       1.000429493          2,869,311      cycles
       1.000429493            816,875      instructions             

# 0.28 insn per cycle

Quoted 2 lines
Quoted 2 lines
       2.001516426          9,260,973      cycles
       2.001516426          5,250,634      instructions             

# 0.87 insn per cycle

Quoted 2 lines
Quoted 2 lines

The second 'insn per cycle' should not be 0.87 (5,250,634/9,260,973

is 0.57).

Quoted 11 lines
Quoted 11 lines

The current code uses a global variable rt_stat for tracking and updating the std dev of runtime stat. Unlike the counts, rt_stat is not reset for interval. While the counts are reset for interval.

perf_stat_process_counter()

{
         if (config->interval)
                 init_stats(ps->res_stats);
}
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

So for interval, the rt_stat should be reset either.

s/either/too/g right?

Yes, should use "too" here. :)

Ok

Quoted 11 lines
Quoted 2 lines

And please try and find what was the cset that introduced the

problem,

Quoted 1 line

so that we can have a Fixes: line and the stable series can pick it,

ok?

Quoted 3 lines

- Arnaldo

I have tried to find the patch which introduced this issue.

Quoted 8 lines

51fd2df1e882 ("perf stat: Fix interval output values").

This patch zeros stats for interval mode. I just think it should reset rt_stat too.

But I really don't know if it's fair to this patch so I don't add it in

my patch description.

That's ok, this just speeds up the process, I'll check it :-)

- Arnaldo

Quoted 75 lines

Thanks
Jin Yao

Quoted 8 lines
Quoted 8 lines

This patch resets rt_stat before read_counters, so the runtime stat is only calculated by the counts generated in this interval.

With this patch,

  root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
  #           time             counts unit events
       1.000420924          2,408,818      inst_retired.any         

# 2.1 CPI

Quoted 2 lines
Quoted 2 lines
       1.000420924          5,010,111      cycles
       2.001448579          2,798,407      inst_retired.any         

# 1.6 CPI

Quoted 3 lines
Quoted 3 lines
       2.001448579          4,599,861      cycles
  root@kbl-ppc:~# perf stat -e cycles,instructions -I1000

--interval-count 2

Quoted 3 lines
Quoted 3 lines

# time counts unit events

       1.000428555          2,769,714      cycles
       1.000428555            774,462      instructions             

# 0.28 insn per cycle

Quoted 2 lines
Quoted 2 lines
       2.001471562          3,595,904      cycles
       2.001471562          1,243,703      instructions             

# 0.35 insn per cycle

Quoted 9 lines
Quoted 9 lines

Now the second 'insn per cycle' and CPI are calculated by the counts generated in this interval.

v2:
---
Use just existing perf_stat__reset_shadow_per_stat(&rt_stat). We don't need to define new function perf_stat__reset_rt_stat.

Quoted 7 lines

Review

Quoted 7 lines
---
  tools/perf/Documentation/perf-stat.txt | 2 ++
  tools/perf/builtin-stat.c              | 1 +
  2 files changed, 3 insertions(+)
(quoted diff) +0 -0

b/tools/perf/Documentation/perf-stat.txt

Quoted 4 lines

tools/perf/Documentation/perf-stat.txt Print count deltas every N milliseconds

Quoted 4 lines

index 4d56586b2fb9..3fb5028aef08 100644

tools/perf/Documentation/perf-stat.txt +0 -0
@@ -176,6 +176,8 @@ Print count deltas every N milliseconds

(minimum: 1ms)

Quoted 1 line

tools/perf/Documentation/perf-stat.txt Print count deltas every N milliseconds · line 176

Quoted 1 line
tools/perf/Documentation/perf-stat.txt +0 -0
  The overhead percentage could be high in some cases, for instance

with small, sub 100ms intervals. Use with caution.

Quoted 3 lines

tools/perf/Documentation/perf-stat.txt Print count deltas every N milliseconds (minimum: 1ms)

Quoted 3 lines
tools/perf/Documentation/perf-stat.txt +1 -0
  	example: 'perf stat -I 1000 -e cycles -a sleep 5'
 
+If the metric exists, it is calculated by the counts generated in

this interval and the metric is printed after #.

Quoted 20 lines
Quoted 19 lines
tools/perf/Documentation/perf-stat.txt +1 -0
+
  --interval-count times::
  Print count deltas for fixed number of times.
  This option should be used together with "-I" option.
tools/perf/builtin-stat.c +1 -0
index 9207b6c45475..3f050d85c277 100644
@@ -359,6 +359,7 @@ static void process_interval(void)
  	clock_gettime(CLOCK_MONOTONIC, &ts);
  	diff_timespec(&rs, &ts, &ref_time);
 
+	perf_stat__reset_shadow_per_stat(&rt_stat);
  	read_counters(&rs);
 
  	if (STAT_RECORD) {
Signature · 1 line
2.17.1
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
#5 lore raw
In reply to Jin, Yao

Em Wed, Apr 22, 2020 at 08:53:41AM +0800, Jin, Yao escreveu:

Quoted 51 lines

Hi Arnaldo,

Quoted 33 lines · Arnaldo Carvalho de Melo

Em Mon, Apr 20, 2020 at 10:54:17PM +0800, Jin Yao escreveu:

Quoted 32 lines

For interval mode, the metric is printed after # if it exists. But it's not calculated by the counts generated in this interval. See following examples,

  root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
  #           time             counts unit events
       1.000422803            764,809      inst_retired.any          #      2.9 CPI
       1.000422803          2,234,932      cycles
       2.001464585          1,960,061      inst_retired.any          #      1.6 CPI
       2.001464585          4,022,591      cycles

The second CPI should not be 1.6 (4,022,591/1,960,061 is 2.1)

  root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
  #           time             counts unit events
       1.000429493          2,869,311      cycles
       1.000429493            816,875      instructions              #    0.28  insn per cycle
       2.001516426          9,260,973      cycles
       2.001516426          5,250,634      instructions              #    0.87  insn per cycle

The second 'insn per cycle' should not be 0.87 (5,250,634/9,260,973 is 0.57).

The current code uses a global variable rt_stat for tracking and updating the std dev of runtime stat. Unlike the counts, rt_stat is not reset for interval. While the counts are reset for interval.

perf_stat_process_counter()

{
         if (config->interval)
                 init_stats(ps->res_stats);
}
Quoted 4 lines
Quoted 1 line

So for interval, the rt_stat should be reset either.

s/either/too/g right?

Yes, should use "too" here. :)

Quoted 5 lines

And please try and find what was the cset that introduced the problem, so that we can have a Fixes: line and the stable series can pick it, ok?

- Arnaldo

I have tried to find the patch which introduced this issue.

Quoted 7 lines

51fd2df1e882 ("perf stat: Fix interval output values").

This patch zeros stats for interval mode. I just think it should reset rt_stat too.

But I really don't know if it's fair to this patch so I don't add it in my patch description.

I think you are right that 51fd2df1e882 patch missed the fix you're providing now, so I think it is fair to say that your current patch fixes 51fd2df1e882, so I added it in the Fixes tag for the patch discussed in this message, thanks.

- Arnaldo

Quoted 62 lines

Thanks
Jin Yao

Quoted 59 lines
Quoted 58 lines

This patch resets rt_stat before read_counters, so the runtime stat is only calculated by the counts generated in this interval.

With this patch,

  root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
  #           time             counts unit events
       1.000420924          2,408,818      inst_retired.any          #      2.1 CPI
       1.000420924          5,010,111      cycles
       2.001448579          2,798,407      inst_retired.any          #      1.6 CPI
       2.001448579          4,599,861      cycles
  root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
  #           time             counts unit events
       1.000428555          2,769,714      cycles
       1.000428555            774,462      instructions              #    0.28  insn per cycle
       2.001471562          3,595,904      cycles
       2.001471562          1,243,703      instructions              #    0.35  insn per cycle

Now the second 'insn per cycle' and CPI are calculated by the counts generated in this interval.

v2:
---
Use just existing perf_stat__reset_shadow_per_stat(&rt_stat). We don't need to define new function perf_stat__reset_rt_stat.

---
  tools/perf/Documentation/perf-stat.txt | 2 ++
  tools/perf/builtin-stat.c              | 1 +
  2 files changed, 3 insertions(+)
tools/perf/Documentation/perf-stat.txt +2 -0
index 4d56586b2fb9..3fb5028aef08 100644
@@ -176,6 +176,8 @@ Print count deltas every N milliseconds (minimum: 1ms)
  The overhead percentage could be high in some cases, for instance with small, sub 100ms intervals.  Use with caution.
  	example: 'perf stat -I 1000 -e cycles -a sleep 5'
+If the metric exists, it is calculated by the counts generated in this interval and the metric is printed after #.
+
  --interval-count times::
  Print count deltas for fixed number of times.
  This option should be used together with "-I" option.
tools/perf/builtin-stat.c +1 -0
index 9207b6c45475..3f050d85c277 100644
@@ -359,6 +359,7 @@ static void process_interval(void)
  	clock_gettime(CLOCK_MONOTONIC, &ts);
  	diff_timespec(&rs, &ts, &ref_time);
+	perf_stat__reset_shadow_per_stat(&rt_stat);
  	read_counters(&rs);
  	if (STAT_RECORD) {
Signature · 1 line
2.17.1
Signature · 1 line
- Arnaldo
Acked-by on v2 1/1
#6 lore raw
In reply to Jin Yao
Quoted 53 lines · Jin Yao

For interval mode, the metric is printed after # if it exists. But it's not calculated by the counts generated in this interval. See following examples,

 root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
 #           time             counts unit events
      1.000422803            764,809      inst_retired.any          #      2.9 CPI
      1.000422803          2,234,932      cycles
      2.001464585          1,960,061      inst_retired.any          #      1.6 CPI
      2.001464585          4,022,591      cycles

The second CPI should not be 1.6 (4,022,591/1,960,061 is 2.1)

 root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
 #           time             counts unit events
      1.000429493          2,869,311      cycles
      1.000429493            816,875      instructions              #    0.28  insn per cycle
      2.001516426          9,260,973      cycles
      2.001516426          5,250,634      instructions              #    0.87  insn per cycle

The second 'insn per cycle' should not be 0.87 (5,250,634/9,260,973 is 0.57).

The current code uses a global variable rt_stat for tracking and updating the std dev of runtime stat. Unlike the counts, rt_stat is not reset for interval. While the counts are reset for interval.

perf_stat_process_counter()

{
        if (config->interval)
                init_stats(ps->res_stats);
}

So for interval, the rt_stat should be reset either.

This patch resets rt_stat before read_counters, so the runtime stat is only calculated by the counts generated in this interval.

With this patch,

 root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
 #           time             counts unit events
      1.000420924          2,408,818      inst_retired.any          #      2.1 CPI
      1.000420924          5,010,111      cycles
      2.001448579          2,798,407      inst_retired.any          #      1.6 CPI
      2.001448579          4,599,861      cycles
 root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
 #           time             counts unit events
      1.000428555          2,769,714      cycles
      1.000428555            774,462      instructions              #    0.28  insn per cycle
      2.001471562          3,595,904      cycles
      2.001471562          1,243,703      instructions              #    0.35  insn per cycle
Quoted 7 lines

Now the second 'insn per cycle' and CPI are calculated by the counts generated in this interval.

v2:
---
Use just existing perf_stat__reset_shadow_per_stat(&rt_stat). We don't need to define new function perf_stat__reset_rt_stat.

thanks,
jirka

merge bot Applied
#7 lore raw
In reply to Jin Yao
The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     197ba86fdc888dc0d3d6b89b402c9c6851d4c6fb
Gitweb:        https://git.kernel.org/tip/197ba86fdc888dc0d3d6b89b402c9c6851d4c6fb
Author:        Jin Yao <[email protected]>
AuthorDate:    Mon, 20 Apr 2020 22:54:17 +08:00
Committer:     Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Thu, 23 Apr 2020 11:03:46 -03:00

perf stat: Improve runtime stat for interval mode

For interval mode, the metric is printed after the '#' character if it exists. But it's not calculated by the counts generated in this interval.

See the following examples:

  root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
  #           time             counts unit events
       1.000422803            764,809      inst_retired.any          #      2.9 CPI
       1.000422803          2,234,932      cycles
       2.001464585          1,960,061      inst_retired.any          #      1.6 CPI
       2.001464585          4,022,591      cycles

The second CPI should not be 1.6 (4,022,591/1,960,061 is 2.1)

  root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
  #           time             counts unit events
       1.000429493          2,869,311      cycles
       1.000429493            816,875      instructions              #    0.28  insn per cycle
       2.001516426          9,260,973      cycles
       2.001516426          5,250,634      instructions              #    0.87  insn per cycle

The second 'insn per cycle' should not be 0.87 (5,250,634/9,260,973 is 0.57).

The current code uses a global variable 'rt_stat' for tracking and updating the std dev of runtime stat. Unlike the counts, 'rt_stat' is not reset for interval. While the counts are reset for interval.

  perf_stat_process_counter()
  {
          if (config->interval)
                  init_stats(ps->res_stats);
  }

So for interval mode, the 'rt_stat' variable should be reset too.

This patch resets 'rt_stat' before read_counters(), so the runtime stat is only calculated by the counts generated in this interval.

With this patch:

  root@kbl-ppc:~# perf stat -M CPI -I1000 --interval-count 2
  #           time             counts unit events
       1.000420924          2,408,818      inst_retired.any          #      2.1 CPI
       1.000420924          5,010,111      cycles
       2.001448579          2,798,407      inst_retired.any          #      1.6 CPI
       2.001448579          4,599,861      cycles
  root@kbl-ppc:~# perf stat -e cycles,instructions -I1000 --interval-count 2
  #           time             counts unit events
       1.000428555          2,769,714      cycles
       1.000428555            774,462      instructions              #    0.28  insn per cycle
       2.001471562          3,595,904      cycles
       2.001471562          1,243,703      instructions              #    0.35  insn per cycle

Now the second 'insn per cycle' and CPI are calculated by the counts generated in this interval.

---
 tools/perf/Documentation/perf-stat.txt | 2 ++
 tools/perf/builtin-stat.c              | 1 +
 2 files changed, 3 insertions(+)
tools/perf/Documentation/perf-stat.txt +2 -0
index 4d56586..3fb5028 100644
@@ -176,6 +176,8 @@ Print count deltas every N milliseconds (minimum: 1ms)
 The overhead percentage could be high in some cases, for instance with small, sub 100ms intervals.  Use with caution.
 	example: 'perf stat -I 1000 -e cycles -a sleep 5'
 
+If the metric exists, it is calculated by the counts generated in this interval and the metric is printed after #.
+
 --interval-count times::
 Print count deltas for fixed number of times.
 This option should be used together with "-I" option.
tools/perf/builtin-stat.c +1 -0
index 9207b6c..3f050d8 100644
@@ -359,6 +359,7 @@ static void process_interval(void)
 	clock_gettime(CLOCK_MONOTONIC, &ts);
 	diff_timespec(&rs, &ts, &ref_time);
 
+	perf_stat__reset_shadow_per_stat(&rt_stat);
 	read_counters(&rs);
 
 	if (STAT_RECORD) {