perf diff: Report noisy for cycles diff
PATCH v3 1/1 perf diff: Report noisy for cycles diff
This patch prints the stddev and hist for the cycles diff of program block. It can help us to understand if the cycles diff is noisy or not.
This patch is inspired by Andi Kleen's patch
https://lwn.net/Articles/600471/
We create new option '-n or --noisy'.
Example:
perf record -b ./div
perf record -b ./div
perf diff -c cycles
# Baseline [Program Block Range] Cycles Diff Shared Object Symbol
# ........ ...................................................................... ................. ............................
#
46.72% [div.c:40 -> div.c:40] 0 div [.] main
46.72% [div.c:42 -> div.c:44] 0 div [.] main
46.72% [div.c:42 -> div.c:39] 0 div [.] main
20.54% [random_r.c:357 -> random_r.c:394] 1 libc-2.27.so [.] __random_r
20.54% [random_r.c:357 -> random_r.c:380] 0 libc-2.27.so [.] __random_r
20.54% [random_r.c:388 -> random_r.c:388] 0 libc-2.27.so [.] __random_r
20.54% [random_r.c:388 -> random_r.c:391] 0 libc-2.27.so [.] __random_r
17.04% [random.c:288 -> random.c:291] 0 libc-2.27.so [.] __random
17.04% [random.c:291 -> random.c:291] 0 libc-2.27.so [.] __random
17.04% [random.c:293 -> random.c:293] 0 libc-2.27.so [.] __random
17.04% [random.c:295 -> random.c:295] 0 libc-2.27.so [.] __random
17.04% [random.c:295 -> random.c:295] 0 libc-2.27.so [.] __random
17.04% [random.c:298 -> random.c:298] 0 libc-2.27.so [.] __random
8.40% [div.c:22 -> div.c:25] 0 div [.] compute_flag
8.40% [div.c:27 -> div.c:28] 0 div [.] compute_flag
5.14% [rand.c:26 -> rand.c:27] 0 libc-2.27.so [.] rand
5.14% [rand.c:28 -> rand.c:28] 0 libc-2.27.so [.] rand
2.15% [rand@plt+0 -> rand@plt+0] 0 div [.] rand@plt
0.00% [kernel.kallsyms] [k] __x86_indirect_thunk_rax
0.00% [do_mmap+714 -> do_mmap+732] -10 [kernel.kallsyms] [k] do_mmap
0.00% [do_mmap+737 -> do_mmap+765] 1 [kernel.kallsyms] [k] do_mmap
0.00% [do_mmap+262 -> do_mmap+299] 0 [kernel.kallsyms] [k] do_mmap
0.00% [__x86_indirect_thunk_r15+0 -> __x86_indirect_thunk_r15+0] 7 [kernel.kallsyms] [k] __x86_indirect_thunk_r15
0.00% [native_sched_clock+0 -> native_sched_clock+119] -1 [kernel.kallsyms] [k] native_sched_clock
0.00% [native_write_msr+0 -> native_write_msr+16] -13 [kernel.kallsyms] [k] native_write_msrWhen we enable the option '-n' or '--noisy', the output is
perf diff -c cycles -n or perf diff -c cycles --noisy
# Baseline [Program Block Range] Cycles Diff stddev/Hist Shared Object Symbol
# ........ ...................................................................... ................. ................. ............................
#
46.72% [div.c:40 -> div.c:40] 0 ± 37.8% ▁█▁▁██▁█ div [.] main
46.72% [div.c:42 -> div.c:44] 0 ± 49.4% ▁▁▂█▂▂▂▂ div [.] main
46.72% [div.c:42 -> div.c:39] 0 ± 24.1% ▃█▂▄▁▃▂▁ div [.] main
20.54% [random_r.c:357 -> random_r.c:394] 1 ± 33.5% ▅▂▁█▃▁▂▁ libc-2.27.so [.] __random_r
20.54% [random_r.c:357 -> random_r.c:380] 0 ± 39.4% ▁▁█▁██▅▁ libc-2.27.so [.] __random_r
20.54% [random_r.c:388 -> random_r.c:388] 0 libc-2.27.so [.] __random_r
20.54% [random_r.c:388 -> random_r.c:391] 0 ± 41.2% ▁▃▁▂█▄▃▁ libc-2.27.so [.] __random_r
17.04% [random.c:288 -> random.c:291] 0 ± 48.8% ▁▁▁▁███▁ libc-2.27.so [.] __random
17.04% [random.c:291 -> random.c:291] 0 ±100.0% ▁█▁▁▁▁▁▁ libc-2.27.so [.] __random
17.04% [random.c:293 -> random.c:293] 0 ±100.0% ▁█▁▁▁▁▁▁ libc-2.27.so [.] __random
17.04% [random.c:295 -> random.c:295] 0 ±100.0% ▁█▁▁▁▁▁▁ libc-2.27.so [.] __random
17.04% [random.c:295 -> random.c:295] 0 libc-2.27.so [.] __random
17.04% [random.c:298 -> random.c:298] 0 ± 75.6% ▃█▁▁▁▁▁▁ libc-2.27.so [.] __random
8.40% [div.c:22 -> div.c:25] 0 ± 42.1% ▁▃▁▁███▁ div [.] compute_flag
8.40% [div.c:27 -> div.c:28] 0 ± 41.8% ██▁▁▄▁▁▄ div [.] compute_flag
5.14% [rand.c:26 -> rand.c:27] 0 ± 37.8% ▁▁▁████▁ libc-2.27.so [.] rand
5.14% [rand.c:28 -> rand.c:28] 0 libc-2.27.so [.] rand
2.15% [rand@plt+0 -> rand@plt+0] 0 div [.] rand@plt
0.00% [kernel.kallsyms] [k] __x86_indirect_thunk_rax
0.00% [do_mmap+714 -> do_mmap+732] -10 [kernel.kallsyms] [k] do_mmap
0.00% [do_mmap+737 -> do_mmap+765] 1 [kernel.kallsyms] [k] do_mmap
0.00% [do_mmap+262 -> do_mmap+299] 0 [kernel.kallsyms] [k] do_mmap
0.00% [__x86_indirect_thunk_r15+0 -> __x86_indirect_thunk_r15+0] 7 [kernel.kallsyms] [k] __x86_indirect_thunk_r15
0.00% [native_sched_clock+0 -> native_sched_clock+119] -1 ± 38.5% ▄█▁ [kernel.kallsyms] [k] native_sched_clock
0.00% [native_write_msr+0 -> native_write_msr+16] -13 ± 47.1% ▁█▇▃▁▁ [kernel.kallsyms] [k] native_write_msr v3:
---
1. Move the histogram to a separate column
2. Move the svals[] out of struct stats
v2:
---
Jiri got a compile error,
CC builtin-diff.obuiltin-diff.c: In function ‘compute_cycles_diff’:
builtin-diff.c:712:10: error: taking the absolute value of unsigned type ‘u64’ {aka ‘long unsigned int’} has no effect [-Werror=absolute-value]
712 | labs(pair->block_info->cycles_spark[i] -
| ^~~~Because the result of u64 - u64 is still u64. Now we change the type of cycles_spark[] to s64.
--- tools/perf/Documentation/perf-diff.txt | 5 + tools/perf/builtin-diff.c | 150 ++++++++++++++++++++++++- tools/perf/util/Build | 1 + tools/perf/util/annotate.c | 4 + tools/perf/util/annotate.h | 2 + tools/perf/util/sort.h | 4 + tools/perf/util/spark.c | 34 ++++++ tools/perf/util/spark.h | 8 ++ tools/perf/util/symbol.h | 2 + 9 files changed, 208 insertions(+), 2 deletions(-) create mode 100644 tools/perf/util/spark.c create mode 100644 tools/perf/util/spark.h
tools/perf/Documentation/perf-diff.txt +5 -0
@@ -95,6 +95,11 @@ OPTIONS diff.compute config option. See COMPARISON METHODS section for more info. +-n:: +--noisy:: + Show cycles noisy data, such as stddev and hists. Should use + with '-c cycles'. + -p:: --period:: Show period values for both compared hist entries.
tools/perf/builtin-diff.c +148 -2
222 lines not on this page · raw message
tools/perf/util/Build +1 -0
@@ -89,6 +89,7 @@ perf-y += cloexec.o perf-y += call-path.o perf-y += rwsem.o perf-y += thread-stack.o +perf-y += spark.o perf-$(CONFIG_AUXTRACE) += auxtrace.o perf-$(CONFIG_AUXTRACE) += intel-pt-decoder/ perf-$(CONFIG_AUXTRACE) += intel-pt.o
tools/perf/util/annotate.c +4 -0
@@ -847,6 +847,10 @@ static int __symbol__account_cycles(struct cyc_hist *ch, ch[offset].start < start)return 0 ; } + +if (ch[offset].num <NUM_SPARKS ) + ch[offset].cycles_spark[ch[offset].num] = cycles; + ch[offset].have_start = have_start; ch[offset].start = start; ch[offset].cycles += cycles;
tools/perf/util/annotate.h +2 -0
@@ -11,6 +11,7 @@#include <pthread.h> #include <asm/bug.h> #include "symbol_conf.h" +#include "spark.h" struct hist_browser_timer ;struct hist_entry ;
@@ -235,6 +236,7 @@ struct cyc_hist {u64 cycles_aggr;u64 cycles_max;u64 cycles_min; +s64 cycles_spark[NUM_SPARKS ];u32 num;u32 num_aggr;u8 have_start;
tools/perf/util/sort.h +4 -0
@@ -23,6 +23,8 @@#include "parse-events.h" #include "hist.h" #include "srcline.h" +#include "stat.h" +#include "spark.h" struct thread ;
@@ -83,6 +85,8 @@ struct hist_entry_diff {/* PERF_HPP_DIFF__CYCLES */ s64 cycles; }; +struct stats stats; +unsigned long svals[NUM_SPARKS ]; };struct hist_entry_ops {
tools/perf/util/spark.c new file +34 -0
@@ -0,0 +1,34 @@ +#include <stdio.h> +#include <limits.h> +#include <string.h> +#include <stdlib.h> +#include "spark.h" +#include "stat.h" + +#define SPARK_SHIFT 8 + +/* Print spark lines on outf for numval values in val. */ +int print_spark (char *bf,int size,unsigned long *val,int numval) +{ +static const char *ticks[NUM_SPARKS ] = { +"▁" ,"▂" ,"▃" ,"▄" ,"▅" ,"▆" ,"▇" ,"█" + }; +int i, printed =0 ; +unsigned long min =ULONG_MAX , max =0 , f; + +for (i =0 ; i < numval; i++) { +if (val[i] < min) + min = val[i]; +if (val[i] > max) + max = val[i]; + } + f = ((max - min) <<SPARK_SHIFT ) / (NUM_SPARKS -1 ); +if (f <1 ) + f =1 ; +for (i =0 ; i < numval; i++) { + printed +=scnprintf (bf + printed, size - printed,"%s" , + ticks[((val[i] - min) <<SPARK_SHIFT ) / f]); + } + +return printed; +}
tools/perf/util/spark.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef SPARK_H +#define SPARK_H 1 + +#define NUM_SPARKS 8 + +int print_spark (char *bf,int size,unsigned long *val,int numval); + +#endif
tools/perf/util/symbol.h +2 -0
@@ -12,6 +12,7 @@#include "branch.h" #include "path.h" #include "symbol_conf.h" +#include "spark.h" #ifdef HAVE_LIBELF_SUPPORT #include <libelf.h>
@@ -137,6 +138,7 @@ struct block_info {u64 end;u64 cycles;u64 cycles_aggr; +s64 cycles_spark[NUM_SPARKS ];int num;int num_aggr;refcount_t refcnt;
Signature · 1 line
2.17.1
On Sat, Aug 10, 2019 at 07:30:29AM +0800, Jin Yao wrote:
SNIP
Quoted 24 lines
static int
process_block_per_sym(struct hist_entry *he)tools/perf/builtin-diff.c +15
-0@@ -684,6 +694,21 @@ static struct hist_entry *get_block_pair(struct hist_entry *he,return NULL ; } +static void init_spark_values (unsigned long *svals,int num) +{ +for (int i =0 ; i < num; i++) + svals[i] =0 ; +} + +static void update_spark_value (unsigned long *svals,int num, +struct stats *stats,u64 val) +{ +int n = stats->n; + +if (n < num) + svals[n] = val; +} +static void compute_cycles_diff (struct hist_entry *he,struct hist_entry *pair) {
tools/perf/builtin-diff.c static void compute_cycles_diff(struct hist_entry *he, · line 720
Quoted 4 lines
tools/perf/builtin-diff.c +1
-0@@ -692,6 +717,23 @@ static void compute_cycles_diff(struct hist_entry *he, pair->diff.cycles = pair->block_info->cycles_aggr / pair->block_info->num_aggr - he->block_info->cycles_aggr / he->block_info->num_aggr; +
should below code be executed only for show_noisy?
jirka
Quoted 18 lines
tools/perf/builtin-diff.c +16
-0+init_stats (&pair->diff.stats); +init_spark_values (pair->diff.svals,NUM_SPARKS ); + +for (int i =0 ; i < pair->block_info->num; i++) { +u64 val; + +if (i >= he->block_info->num || i >=NUM_SPARKS ) +break ; + + val =labs (pair->block_info->cycles_spark[i] - + he->block_info->cycles_spark[i]); + +update_spark_value (pair->diff.svals,NUM_SPARKS , + &pair->diff.stats, val); +update_stats (&pair->diff.stats, val); + } } }
SNIP
Quoted 35 lines · Jiri Olsa
On Sat, Aug 10, 2019 at 07:30:29AM +0800, Jin Yao wrote:
SNIP
Quoted 24 lines
static int
process_block_per_sym(struct hist_entry *he)tools/perf/builtin-diff.c +15
-0@@ -684,6 +694,21 @@ static struct hist_entry *get_block_pair(struct hist_entry *he,return NULL ; } +static void init_spark_values (unsigned long *svals,int num) +{ +for (int i =0 ; i < num; i++) + svals[i] =0 ; +} + +static void update_spark_value (unsigned long *svals,int num, +struct stats *stats,u64 val) +{ +int n = stats->n; + +if (n < num) + svals[n] = val; +} +static void compute_cycles_diff (struct hist_entry *he,struct hist_entry *pair) {tools/perf/builtin-diff.c static void compute_cycles_diff(struct hist_entry *he, · line 720
Quoted 4 lines
tools/perf/builtin-diff.c +1
-0@@ -692,6 +717,23 @@ static void compute_cycles_diff(struct hist_entry *he, pair->diff.cycles = pair->block_info->cycles_aggr / pair->block_info->num_aggr - he->block_info->cycles_aggr / he->block_info->num_aggr; +should below code be executed only for
show_noisy?
Quoted 2 lines
jirka
Oh, yes, following code should be executed only when show_noisy is enabled. Thanks for pointing out this issue. I will refine the code.
Thanks
Jin Yao
Quoted 21 lines
Quoted 18 lines
tools/perf/builtin-diff.c +16
-0+init_stats (&pair->diff.stats); +init_spark_values (pair->diff.svals,NUM_SPARKS ); + +for (int i =0 ; i < pair->block_info->num; i++) { +u64 val; + +if (i >= he->block_info->num || i >=NUM_SPARKS ) +break ; + + val =labs (pair->block_info->cycles_spark[i] - + he->block_info->cycles_spark[i]); + +update_spark_value (pair->diff.svals,NUM_SPARKS , + &pair->diff.stats, val); +update_stats (&pair->diff.stats, val); + } } }SNIP
On Sat, Aug 10, 2019 at 07:30:29AM +0800, Jin Yao wrote:
SNIP
Quoted 21 lines
tools/perf/builtin-diff.c +17
-0+ +init_stats (&pair->diff.stats); +init_spark_values (pair->diff.svals,NUM_SPARKS ); + +for (int i =0 ; i < pair->block_info->num; i++) { +u64 val; + +if (i >= he->block_info->num || i >=NUM_SPARKS ) +break ; + + val =labs (pair->block_info->cycles_spark[i] - + he->block_info->cycles_spark[i]); + +update_spark_value (pair->diff.svals,NUM_SPARKS , + &pair->diff.stats, val); +update_stats (&pair->diff.stats, val); + } } }
tools/perf/builtin-diff.c static const struct option options[] = { · line 1296
Quoted 5 lines
tools/perf/builtin-diff.c +2
-0@@ -1250,6 +1292,8 @@ static const struct option options[] = { "Show period values."),OPT_BOOLEAN ('F' ,"formula" , &show_formula,"Show formula." ), +OPT_BOOLEAN ('n' ,"noisy" , &show_noisy, + "Show cycles noisy - WARNING: use only with -c cycles."),
this should be more 'cycles' specific like --cycles-noisy or --cycles-hist?
also I dont think we should use 'n' for this, just the long option
jirka
Quoted 33 lines · Jiri Olsa
On Sat, Aug 10, 2019 at 07:30:29AM +0800, Jin Yao wrote:
SNIP
Quoted 21 lines
tools/perf/builtin-diff.c +17
-0+ +init_stats (&pair->diff.stats); +init_spark_values (pair->diff.svals,NUM_SPARKS ); + +for (int i =0 ; i < pair->block_info->num; i++) { +u64 val; + +if (i >= he->block_info->num || i >=NUM_SPARKS ) +break ; + + val =labs (pair->block_info->cycles_spark[i] - + he->block_info->cycles_spark[i]); + +update_spark_value (pair->diff.svals,NUM_SPARKS , + &pair->diff.stats, val); +update_stats (&pair->diff.stats, val); + } } }tools/perf/builtin-diff.c static const struct option options[] = { · line 1296
Quoted 5 lines
tools/perf/builtin-diff.c +2
-0@@ -1250,6 +1292,8 @@ static const struct option options[] = { "Show period values."),OPT_BOOLEAN ('F' ,"formula" , &show_formula,"Show formula." ), +OPT_BOOLEAN ('n' ,"noisy" , &show_noisy, + "Show cycles noisy - WARNING: use only with -c cycles."),this should be more 'cycles' specific like --cycles-noisy or --cycles-hist?
Quoted 4 lines
also I dont think we should use 'n' for this, just the long option
jirka
Yes, it's cycles specific. Maybe --cycles-hist is better and the long option should be enough. We can reserve -n for future feature. :)
Thanks
Jin Yao
On Sat, Aug 10, 2019 at 07:30:29AM +0800, Jin Yao wrote:
SNIP
Quoted 12 lines
tools/perf/builtin-diff.c +12
-0+if (vals[i] !=0 ) +return 0 ; +return 1 ; +} + +static int print_cycles_spark (char *bf,int size,unsigned long *svals,u64 n) +{ +int len = n, printed; + +if (len <=1 ) +return 0 ; +
tools/perf/builtin-diff.c static int hpp__color_cycles(struct perf_hpp_fmt *fmt, · line 1530
Quoted 10 lines
tools/perf/builtin-diff.c +10
-0+if (len >NUM_SPARKS ) + len =NUM_SPARKS ; +if (all_zero (svals, len)) +return 0 ; + + printed =print_spark (bf, size, svals, len); + printed +=scnprintf (bf + printed, size - printed," " ); + +if (n >NUM_SPARKS ) + printed +=scnprintf (bf + printed, size - printed,".." );
will this '..' ever be printed? I can't see that even if I enlarge the column width..
jirka
Quoted 30 lines · Jiri Olsa
On Sat, Aug 10, 2019 at 07:30:29AM +0800, Jin Yao wrote:
SNIP
Quoted 12 lines
tools/perf/builtin-diff.c +12
-0+if (vals[i] !=0 ) +return 0 ; +return 1 ; +} + +static int print_cycles_spark (char *bf,int size,unsigned long *svals,u64 n) +{ +int len = n, printed; + +if (len <=1 ) +return 0 ; +tools/perf/builtin-diff.c static int hpp__color_cycles(struct perf_hpp_fmt *fmt, · line 1530
Quoted 10 lines
tools/perf/builtin-diff.c +10
-0+if (len >NUM_SPARKS ) + len =NUM_SPARKS ; +if (all_zero (svals, len)) +return 0 ; + + printed =print_spark (bf, size, svals, len); + printed +=scnprintf (bf + printed, size - printed," " ); + +if (n >NUM_SPARKS ) + printed +=scnprintf (bf + printed, size - printed,".." );will this '..' ever be printed? I can't see that even if I enlarge the column width..
Quoted 2 lines
jirka
@@ -83,6 +85,8 @@ struct hist_entry_diff {
/* PERF_HPP_DIFF__CYCLES */
s64 cycles;
};
+ struct stats stats;
+ unsigned long svals[NUM_SPARKS];};
Now we only save 8 items in svals[] (NUM_PARKS = 8). So '..' will not be printed. The code of printing '..' is for future possible case.
If you think it's not necessary, I would remove this.
Thanks
Jin Yao
Quoted 47 lines · Yao
Quoted 32 lines · Jiri Olsa
On Sat, Aug 10, 2019 at 07:30:29AM +0800, Jin Yao wrote:
SNIP
Quoted 12 lines
tools/perf/builtin-diff.c +12
-0+if (vals[i] !=0 ) +return 0 ; +return 1 ; +} + +static int print_cycles_spark (char *bf,int size,unsigned long *svals,u64 n) +{ +int len = n, printed; + +if (len <=1 ) +return 0 ; +tools/perf/builtin-diff.c static int hpp__color_cycles(struct perf_hpp_fmt *fmt, · line 1530
Quoted 10 lines
tools/perf/builtin-diff.c +10
-0+if (len >NUM_SPARKS ) + len =NUM_SPARKS ; +if (all_zero (svals, len)) +return 0 ; + + printed =print_spark (bf, size, svals, len); + printed +=scnprintf (bf + printed, size - printed," " ); + +if (n >NUM_SPARKS ) + printed +=scnprintf (bf + printed, size - printed,".." );will this '..' ever be printed? I can't see that even if I enlarge the column width..
jirka
tools/perf/util/sort.h +2
-0@@ -83,6 +85,8 @@ struct hist_entry_diff {/* PERF_HPP_DIFF__CYCLES */ s64 cycles; }; +struct stats stats; +unsigned long svals[NUM_SPARKS ]; };Now we only save 8 items in svals[] (
NUM_PARKS= 8). So '..' will not be printed. The code of printing '..' is for future possible case.
Quoted 1 line
If you think it's not necessary, I would remove this.
please remove it then
thanks,
jirka