Share events between metrics
Subsystems (5)
Ian Rogers
reviewerv2 cover Share events between metrics
Metric groups contain metrics. Metrics create groups of events to ideally be scheduled together. Often metrics refer to the same events, for example, a cache hit and cache miss rate. Using separate event groups means these metrics are multiplexed at different times and the counts don't sum to 100%. More multiplexing also decreases the accuracy of the measurement.
This change orders metrics from groups or the command line, so that the ones with the most events are set up first. Later metrics see if groups already provide their events, and reuse them if possible. Unnecessary events and groups are eliminated.
RFC because:
- without this change events within a metric may get scheduled together, after they may appear as part of a larger group and be multiplexed at different times, lowering accuracy - however, less multiplexing may compensate for this.
- libbpf's hashmap is used, however, libbpf is an optional requirement for building perf.
- other things I'm not thinking of.
Thanks!
v2. is the entire patch set based on acme's perf/core tree and includes a cherry-picks. Patch 13 was sent for review to the bpf maintainers here:
https://lore.kernel.org/lkml/[email protected]/
v1. was based on the perf metrics fixes and test sent here:
https://lore.kernel.org/lkml/[email protected]/
Andrii Nakryiko (1):
libbpf: Fix memory leak and possible double-free in hashmap__clear
Ian Rogers (22):
perf expr: unlimited escaped characters in a symbol perf metrics: fix parse errors in cascade lake metrics perf metrics: fix parse errors in skylake metrics perf expr: allow ',' to be an other token
perf expr: increase max other
perf expr: parse numbers as doubles
perf expr: debug lex if debugging yacc
perf metrics: fix parse errors in power8 metrics perf metrics: fix parse errors in power9 metrics perf expr: print a debug message for division by zero perf parse-events: expand add PMU error/verbose messages perf test: improve pmu event metric testing lib/bpf hashmap: increase portability
perf expr: fix memory leaks in bison
perf evsel: fix 2 memory leaks
perf expr: migrate expr ids table to libbpf's hashmap perf metricgroup: change evlist_used to a bitmap perf metricgroup: free metric_events on error perf metricgroup: always place duration_time last perf metricgroup: delay events string creation perf metricgroup: order event groups by size perf metricgroup: remove duped metric group events
tools/lib/bpf/hashmap.c | 7 + tools/lib/bpf/hashmap.h | 3 +- tools/perf/arch/x86/util/intel-pt.c | 32 ++- .../arch/powerpc/power8/metrics.json | 2 +- .../arch/powerpc/power9/metrics.json | 2 +- .../arch/x86/cascadelakex/clx-metrics.json | 10 +- .../arch/x86/skylakex/skx-metrics.json | 4 +- tools/perf/tests/builtin-test.c | 5 + tools/perf/tests/expr.c | 33 ++- tools/perf/tests/pmu-events.c | 158 +++++++++++- tools/perf/tests/pmu.c | 4 +- tools/perf/tests/tests.h | 2 + tools/perf/util/evsel.c | 2 + tools/perf/util/expr.c | 126 ++++----- tools/perf/util/expr.h | 22 +- tools/perf/util/expr.l | 16 +- tools/perf/util/expr.y | 41 ++- tools/perf/util/metricgroup.c | 242 +++++++++++------- tools/perf/util/parse-events.c | 29 ++- tools/perf/util/pmu.c | 33 ++- tools/perf/util/pmu.h | 2 +- tools/perf/util/stat-shadow.c | 46 ++-- 22 files changed, 545 insertions(+), 276 deletions(-)
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 1/23 perf expr: unlimited escaped characters in a symbol
Current expression allows 2 escaped '-,=' characters. However, some metrics require more, for example Haswell DRAM_BW_Use.
--- tools/perf/util/expr.l | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/expr.l +1 -1
@@ -86,7 +86,7 @@ number [0-9]+ sch [-,=] spec \\{sch} sym [0-9a-zA-Z_\.:@?]+ -symbol {spec}*{sym}*{spec}*{sym}*{spec}*{sym} +symbol ({spec}|{sym})+ %% struct expr_scanner_ctx *sctx = expr_get_extra(yyscanner);
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 2/23 perf metrics: fix parse errors in cascade lake metrics
In reply to Ian RogersRemove over escaping with \\.
Remove extraneous if 1 if 0 == 1 else 0 else 0.
--- .../pmu-events/arch/x86/cascadelakex/clx-metrics.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tools/perf/pmu-events/arch/x86/cascadelakex/clx-metrics.json +5 -5
@@ -328,31 +328,31 @@ }, { "BriefDescription": "Average latency of data read request to external memory (in nanoseconds). Accounts for demand loads and L1/L2 prefetches", - "MetricExpr": "1000000000 * ( cha@event\\=0x36\\\\\\,umask\\=0x21@ / cha@event\\=0x35\\\\\\,umask\\=0x21@ ) / ( cha_0@event\\=0x0@ / duration_time )", + "MetricExpr": "1000000000 * ( cha@event\\=0x36\\,umask\\=0x21@ / cha@event\\=0x35\\,umask\\=0x21@ ) / ( cha_0@event\\=0x0@ / duration_time )", "MetricGroup": "Memory_Lat", "MetricName": "DRAM_Read_Latency" }, { "BriefDescription": "Average number of parallel data read requests to external memory. Accounts for demand loads and L1/L2 prefetches", - "MetricExpr": "cha@event\\=0x36\\\\\\,umask\\=0x21@ / cha@event\\=0x36\\\\\\,umask\\=0x21\\\\\\,thresh\\=1@", + "MetricExpr": "cha@event\\=0x36\\,umask\\=0x21@ / cha@event\\=0x36\\,umask\\=0x21\\,thresh\\=1@", "MetricGroup": "Memory_BW", "MetricName": "DRAM_Parallel_Reads" }, { "BriefDescription": "Average latency of data read request to external 3D X-Point memory [in nanoseconds]. Accounts for demand loads and L1/L2 data-read prefetches", - "MetricExpr": "( 1000000000 * ( imc@event\\=0xe0\\\\\\,umask\\=0x1@ / imc@event\\=0xe3@ ) / imc_0@event\\=0x0@ ) if 1 if 0 == 1 else 0 else 0", + "MetricExpr": "( 1000000000 * ( imc@event\\=0xe0\\,umask\\=0x1@ / imc@event\\=0xe3@ ) / imc_0@event\\=0x0@ )", "MetricGroup": "Memory_Lat", "MetricName": "MEM_PMM_Read_Latency" }, { "BriefDescription": "Average 3DXP Memory Bandwidth Use for reads [GB / sec]", - "MetricExpr": "( ( 64 * imc@event\\=0xe3@ / 1000000000 ) / duration_time ) if 1 if 0 == 1 else 0 else 0", + "MetricExpr": "( ( 64 * imc@event\\=0xe3@ / 1000000000 ) / duration_time )", "MetricGroup": "Memory_BW", "MetricName": "PMM_Read_BW" }, { "BriefDescription": "Average 3DXP Memory Bandwidth Use for Writes [GB / sec]", - "MetricExpr": "( ( 64 * imc@event\\=0xe7@ / 1000000000 ) / duration_time ) if 1 if 0 == 1 else 0 else 0", + "MetricExpr": "( ( 64 * imc@event\\=0xe7@ / 1000000000 ) / duration_time )", "MetricGroup": "Memory_BW", "MetricName": "PMM_Write_BW" },
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 3/23 perf metrics: fix parse errors in skylake metrics
In reply to Ian RogersRemove over escaping with \\.
--- tools/perf/pmu-events/arch/x86/skylakex/skx-metrics.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/pmu-events/arch/x86/skylakex/skx-metrics.json +2 -2
@@ -328,13 +328,13 @@ }, { "BriefDescription": "Average latency of data read request to external memory (in nanoseconds). Accounts for demand loads and L1/L2 prefetches", - "MetricExpr": "1000000000 * ( cha@event\\=0x36\\\\\\,umask\\=0x21@ / cha@event\\=0x35\\\\\\,umask\\=0x21@ ) / ( cha_0@event\\=0x0@ / duration_time )", + "MetricExpr": "1000000000 * ( cha@event\\=0x36\\,umask\\=0x21@ / cha@event\\=0x35\\,umask\\=0x21@ ) / ( cha_0@event\\=0x0@ / duration_time )", "MetricGroup": "Memory_Lat", "MetricName": "DRAM_Read_Latency" }, { "BriefDescription": "Average number of parallel data read requests to external memory. Accounts for demand loads and L1/L2 prefetches", - "MetricExpr": "cha@event\\=0x36\\\\\\,umask\\=0x21@ / cha@event\\=0x36\\\\\\,umask\\=0x21\\\\\\,thresh\\=1@", + "MetricExpr": "cha@event\\=0x36\\,umask\\=0x21@ / cha@event\\=0x36\\,umask\\=0x21\\,thresh\\=1@", "MetricGroup": "Memory_BW", "MetricName": "DRAM_Parallel_Reads" },
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 4/23 perf expr: allow ',' to be an other token
In reply to Ian RogersCorrects parse errors in expr__find_other of expressions with min.
--- tools/perf/util/expr.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/expr.y +1 -1
@@ -80,7 +80,7 @@ other: ID ctx->ids[ctx->num_ids++].name = $1; } | -MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' +MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ',' all_expr: if_expr { *final_val = $1; }
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 5/23 perf expr: increase max other
In reply to Ian RogersLarge metrics such as Branch_Misprediction_Cost_SMT on x86 broadwell need more space.
--- tools/perf/util/expr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/expr.h +1 -1
@@ -2,7 +2,7 @@#ifndef PARSE_CTX_H #define PARSE_CTX_H 1 -#define EXPR_MAX_OTHER 20 +#define EXPR_MAX_OTHER 64#define MAX_PARSE_ID EXPR_MAX_OTHER struct expr_parse_id {
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 6/23 perf expr: parse numbers as doubles
In reply to Ian RogersThis is expected in expr.y and metrics use floating point values such as x86 broadwell IFetch_Line_Utilization.
--- tools/perf/util/expr.l | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
tools/perf/util/expr.l +7 -7
@@ -10,12 +10,12 @@ char *expr_get_text(yyscan_t yyscanner); YYSTYPE *expr_get_lval(yyscan_t yyscanner); -static int __value(YYSTYPE *yylval, char *str, int base, int token) +static double __value(YYSTYPE *yylval, char *str, int token) { - u64 num; + double num; errno = 0; - num = strtoull(str, NULL, base); + num = strtod(str, NULL); if (errno) return EXPR_ERROR;
@@ -23,12 +23,12 @@ static int __value(YYSTYPE *yylval, char *str, int base, int token) return token; } -static int value(yyscan_t scanner, int base) +static int value(yyscan_t scanner) { YYSTYPE *yylval = expr_get_lval(scanner); char *text = expr_get_text(scanner); - return __value(yylval, text, base, NUMBER); + return __value(yylval, text, NUMBER); } /*
@@ -81,7 +81,7 @@ static int str(yyscan_t scanner, int token, int runtime) } %} -number [0-9]+ +number [0-9]*\.?[0-9]+ sch [-,=] spec \\{sch}
@@ -105,7 +105,7 @@ min { return MIN; } if { return IF; } else { return ELSE; } #smt_on { return SMT_ON; } -{number} { return value(yyscanner, 10); } +{number} { return value(yyscanner); } {symbol} { return str(yyscanner, ID, sctx->runtime); } "|" { return '|'; } "^" { return '^'; }
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 7/23 perf expr: debug lex if debugging yacc
In reply to Ian RogersOnly effects parser debugging (disabled by default). Enables displaying '--accepting rule at line .. ("...").
--- tools/perf/util/expr.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/util/expr.c +1 -0
@@ -45,6 +45,7 @@ __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr,#ifdef PARSER_DEBUG expr_debug =1 ; +expr_set_debug (1 , scanner);#endif ret =expr_parse (val, ctx, scanner);
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 8/23 perf metrics: fix parse errors in power8 metrics
In reply to Ian RogersMismatched parentheses.
--- tools/perf/pmu-events/arch/powerpc/power8/metrics.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/pmu-events/arch/powerpc/power8/metrics.json +1 -1
@@ -169,7 +169,7 @@ }, { "BriefDescription": "Cycles GCT empty where dispatch was held", - "MetricExpr": "(PM_GCT_NOSLOT_DISP_HELD_MAP + PM_GCT_NOSLOT_DISP_HELD_SRQ + PM_GCT_NOSLOT_DISP_HELD_ISSQ + PM_GCT_NOSLOT_DISP_HELD_OTHER) / PM_RUN_INST_CMPL)", + "MetricExpr": "(PM_GCT_NOSLOT_DISP_HELD_MAP + PM_GCT_NOSLOT_DISP_HELD_SRQ + PM_GCT_NOSLOT_DISP_HELD_ISSQ + PM_GCT_NOSLOT_DISP_HELD_OTHER) / PM_RUN_INST_CMPL", "MetricGroup": "cpi_breakdown", "MetricName": "gct_empty_disp_held_cpi" },
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 9/23 perf metrics: fix parse errors in power9 metrics
In reply to Ian RogersMismatched parentheses.
--- tools/perf/pmu-events/arch/powerpc/power9/metrics.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/pmu-events/arch/powerpc/power9/metrics.json +1 -1
@@ -362,7 +362,7 @@ }, { "BriefDescription": "Completion stall for other reasons", - "MetricExpr": "PM_CMPLU_STALL - PM_CMPLU_STALL_NTC_DISP_FIN - PM_CMPLU_STALL_NTC_FLUSH - PM_CMPLU_STALL_LSU - PM_CMPLU_STALL_EXEC_UNIT - PM_CMPLU_STALL_BRU)/PM_RUN_INST_CMPL", + "MetricExpr": "(PM_CMPLU_STALL - PM_CMPLU_STALL_NTC_DISP_FIN - PM_CMPLU_STALL_NTC_FLUSH - PM_CMPLU_STALL_LSU - PM_CMPLU_STALL_EXEC_UNIT - PM_CMPLU_STALL_BRU)/PM_RUN_INST_CMPL", "MetricGroup": "cpi_breakdown", "MetricName": "other_stall_cpi" },
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 10/23 perf expr: print a debug message for division by zero
In reply to Ian RogersIf an expression yields 0 and is then divided-by/modulus-by then the parsing aborts. Add a debug error message to better enable debugging when this happens.
--- tools/perf/util/expr.y | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
tools/perf/util/expr.y +12 -2
@@ -103,8 +103,18 @@ expr: NUMBER | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '*' expr { $$ = $1 * $3; } - | expr '/' expr { if ($3 == 0) YYABORT; $$ = $1 / $3; } - | expr '%' expr { if ((long)$3 == 0) YYABORT; $$ = (long)$1 % (long)$3; } + | expr '/' expr { if ($3 == 0) { + pr_debug("division by zero\n"); + YYABORT; + } + $$ = $1 / $3; + } + | expr '%' expr { if ((long)$3 == 0) { + pr_debug("division by zero\n"); + YYABORT; + } + $$ = (long)$1 % (long)$3; + } | '-' expr %prec NEG { $$ = -$2; } | '(' if_expr ')' { $$ = $2; } | MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 11/23 perf parse-events: expand add PMU error/verbose messages
In reply to Ian RogersOn a CPU like skylakex an uncore_iio_0 PMU may alias with uncore_iio_free_running_0. The latter PMU doesn't support fc_mask as a parameter and so pmu_config_term fails. Typically parse_events_add_pmu is called in a loop where if one alias succeeds errors are ignored, however, if multiple errors occur parse_events__handle_error will currently give a WARN_ONCE.
This change removes the WARN_ONCE in parse_events__handle_error and makes it a pr_debug. It adds verbose messages to parse_events_add_pmu warning that non-fatal errors may occur, while giving details on the pmu and config terms for useful context. pmu_config_term is altered so the failing term and pmu are present in the case of the 'unknown term' error which makes spotting the free_running case more straightforward.
Before:
$ perf --debug verbose=3 stat -M llc_misses.pcie_read sleep 1
Using CPUID GenuineIntel-6-55-4
metric expr unc_iio_data_req_of_cpu.mem_read.part0 + unc_iio_data_req_of_cpu.mem_read.part1 + unc_iio_data_req_of_cpu.mem_read.part2 + unc_iio_data_req_of_cpu.mem_read.part3 for LLC_MISSES.PCIE_READ
found event unc_iio_data_req_of_cpu.mem_read.part0
found event unc_iio_data_req_of_cpu.mem_read.part1
found event unc_iio_data_req_of_cpu.mem_read.part2
found event unc_iio_data_req_of_cpu.mem_read.part3
metric expr unc_iio_data_req_of_cpu.mem_read.part0 + unc_iio_data_req_of_cpu.mem_read.part1 + unc_iio_data_req_of_cpu.mem_read.part2 + unc_iio_data_req_of_cpu.mem_read.part3 for LLC_MISSES.PCIE_READ
found event unc_iio_data_req_of_cpu.mem_read.part0
found event unc_iio_data_req_of_cpu.mem_read.part1
found event unc_iio_data_req_of_cpu.mem_read.part2
found event unc_iio_data_req_of_cpu.mem_read.part3
adding {unc_iio_data_req_of_cpu.mem_read.part0,unc_iio_data_req_of_cpu.mem_read.part1,unc_iio_data_req_of_cpu.mem_read.part2,unc_iio_data_req_of_cpu.mem_read.part3}:W,{unc_iio_data_req_of_cpu.mem_read.part0,unc_iio_data_req_of_cpu.mem_read.part1,unc_iio_data_req_of_cpu.mem_read.part2,unc_iio_data_req_of_cpu.mem_read.part3}:W
intel_pt default config: tsc,mtc,mtc_period=3,psb_period=3,pt,branch
WARNING: multiple event parsing errors
...
Invalid event/parameter 'fc_mask'
...After:
$ perf --debug verbose=3 stat -M llc_misses.pcie_read sleep 1
Using CPUID GenuineIntel-6-55-4
metric expr unc_iio_data_req_of_cpu.mem_read.part0 + unc_iio_data_req_of_cpu.mem_read.part1 + unc_iio_data_req_of_cpu.mem_read.part2 + unc_iio_data_req_of_cpu.mem_read.part3 for LLC_MISSES.PCIE_READ
found event unc_iio_data_req_of_cpu.mem_read.part0
found event unc_iio_data_req_of_cpu.mem_read.part1
found event unc_iio_data_req_of_cpu.mem_read.part2
found event unc_iio_data_req_of_cpu.mem_read.part3
metric expr unc_iio_data_req_of_cpu.mem_read.part0 + unc_iio_data_req_of_cpu.mem_read.part1 + unc_iio_data_req_of_cpu.mem_read.part2 + unc_iio_data_req_of_cpu.mem_read.part3 for LLC_MISSES.PCIE_READ
found event unc_iio_data_req_of_cpu.mem_read.part0
found event unc_iio_data_req_of_cpu.mem_read.part1
found event unc_iio_data_req_of_cpu.mem_read.part2
found event unc_iio_data_req_of_cpu.mem_read.part3
adding {unc_iio_data_req_of_cpu.mem_read.part0,unc_iio_data_req_of_cpu.mem_read.part1,unc_iio_data_req_of_cpu.mem_read.part2,unc_iio_data_req_of_cpu.mem_read.part3}:W,{unc_iio_data_req_of_cpu.mem_read.part0,unc_iio_data_req_of_cpu.mem_read.part1,unc_iio_data_req_of_cpu.mem_read.part2,unc_iio_data_req_of_cpu.mem_read.part3}:W
intel_pt default config: tsc,mtc,mtc_period=3,psb_period=3,pt,branch
Attempting to add event pmu 'uncore_iio_free_running_5' with 'unc_iio_data_req_of_cpu.mem_read.part0,' that may result in non-fatal errors
After aliases, add event pmu 'uncore_iio_free_running_5' with 'fc_mask,ch_mask,umask,event,' that may result in non-fatal errors
Attempting to add event pmu 'uncore_iio_free_running_3' with 'unc_iio_data_req_of_cpu.mem_read.part0,' that may result in non-fatal errors
After aliases, add event pmu 'uncore_iio_free_running_3' with 'fc_mask,ch_mask,umask,event,' that may result in non-fatal errors
Attempting to add event pmu 'uncore_iio_free_running_1' with 'unc_iio_data_req_of_cpu.mem_read.part0,' that may result in non-fatal errors
After aliases, add event pmu 'uncore_iio_free_running_1' with 'fc_mask,ch_mask,umask,event,' that may result in non-fatal errors
Multiple errors dropping message: unknown term 'fc_mask' for pmu 'uncore_iio_free_running_3' (valid terms: event,umask,config,config1,config2,name,period,percore)
...--- tools/perf/arch/x86/util/intel-pt.c | 32 +++++++++++++++++----------- tools/perf/tests/pmu.c | 4 ++-- tools/perf/util/parse-events.c | 29 ++++++++++++++++++++++++- tools/perf/util/pmu.c | 33 ++++++++++++++++++----------- tools/perf/util/pmu.h | 2 +- 5 files changed, 72 insertions(+), 28 deletions(-)
tools/perf/arch/x86/util/intel-pt.c +20 -12
@@ -59,7 +59,8 @@ struct intel_pt_recording {size_t priv_size; }; -static int intel_pt_parse_terms_with_default (struct list_head *formats, +static int intel_pt_parse_terms_with_default (const char *pmu_name, +struct list_head *formats,const char *str,u64 *config) {
@@ -78,7 +79,8 @@ static int intel_pt_parse_terms_with_default(struct list_head *formats, goto out_free; attr.config = *config; - err =perf_pmu__config_terms (formats, &attr, terms, true,NULL ); + err =perf_pmu__config_terms (pmu_name, formats, &attr, terms, true, +NULL );if (err) goto out_free;
@@ -88,11 +90,12 @@ static int intel_pt_parse_terms_with_default(struct list_head *formats,return err; } -static int intel_pt_parse_terms (struct list_head *formats,const char *str, -u64 *config) +static int intel_pt_parse_terms (const char *pmu_name,struct list_head *formats, +const char *str,u64 *config) { *config =0 ; -return intel_pt_parse_terms_with_default (formats, str, config); +return intel_pt_parse_terms_with_default (pmu_name, formats, str, + config); }static u64 intel_pt_masked_bits (u64 mask,u64 bits)
@@ -229,7 +232,8 @@ static u64 intel_pt_default_config(struct perf_pmu *intel_pt_pmu)pr_debug2 ("%s default config: %s\n" , intel_pt_pmu->name, buf); -intel_pt_parse_terms (&intel_pt_pmu->format, buf, &config); +intel_pt_parse_terms (intel_pt_pmu->name, &intel_pt_pmu->format, buf, + &config);return config; }
@@ -337,13 +341,16 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,if (priv_size != ptr->priv_size)return -EINVAL ; -intel_pt_parse_terms (&intel_pt_pmu->format,"tsc" , &tsc_bit); -intel_pt_parse_terms (&intel_pt_pmu->format,"noretcomp" , - &noretcomp_bit); -intel_pt_parse_terms (&intel_pt_pmu->format,"mtc" , &mtc_bit); +intel_pt_parse_terms (intel_pt_pmu->name, &intel_pt_pmu->format, +"tsc" , &tsc_bit); +intel_pt_parse_terms (intel_pt_pmu->name, &intel_pt_pmu->format, +"noretcomp" , &noretcomp_bit); +intel_pt_parse_terms (intel_pt_pmu->name, &intel_pt_pmu->format, +"mtc" , &mtc_bit); mtc_freq_bits =perf_pmu__format_bits (&intel_pt_pmu->format,"mtc_period" ); -intel_pt_parse_terms (&intel_pt_pmu->format,"cyc" , &cyc_bit); +intel_pt_parse_terms (intel_pt_pmu->name, &intel_pt_pmu->format, +"cyc" , &cyc_bit);intel_pt_tsc_ctc_ratio (&tsc_ctc_ratio_n, &tsc_ctc_ratio_d);
@@ -769,7 +776,8 @@ static int intel_pt_recording_options(struct auxtrace_record *itr, } } -intel_pt_parse_terms (&intel_pt_pmu->format,"tsc" , &tsc_bit); +intel_pt_parse_terms (intel_pt_pmu->name, &intel_pt_pmu->format, +"tsc" , &tsc_bit);if (opts->full_auxtrace && (intel_pt_evsel->core.attr.config & tsc_bit)) have_timing_info = true;
tools/perf/tests/pmu.c +2 -2
@@ -156,8 +156,8 @@ int test__pmu(struct test *test __maybe_unused, int subtest __maybe_unused)if (ret)break ; - ret =perf_pmu__config_terms (&formats, &attr, terms, - false,NULL ); + ret =perf_pmu__config_terms ("perf-pmu-test" , &formats, &attr, + terms, false,NULL );if (ret)break ;
tools/perf/util/parse-events.c +28 -1
@@ -204,7 +204,8 @@ void parse_events__handle_error(struct parse_events_error *err, int idx, err->help = help;break ;default : -WARN_ONCE (1 ,"WARNING: multiple event parsing errors\n" ); +pr_debug ("Multiple errors dropping message: %s (%s)\n" , + err->str, err->help);free (err->str); err->str = str;free (err->help);
@@ -1424,6 +1425,19 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,bool use_uncore_alias;LIST_HEAD (config_terms); +if (verbose >1 ) { +fprintf (stderr,"Attempting to add event pmu '%s' with '" , + name); +if (head_config) { +struct parse_events_term *term; + +list_for_each_entry (term, head_config, list) { +fprintf (stderr,"%s," , term->config); + } + } +fprintf (stderr,"' that may result in non-fatal errors\n" ); + } + pmu =perf_pmu__find (name);if (!pmu) {char *err_str;
@@ -1460,6 +1474,19 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,if (perf_pmu__check_alias (pmu, head_config, &info))return -EINVAL ; +if (verbose >1 ) { +fprintf (stderr,"After aliases, add event pmu '%s' with '" , + name); +if (head_config) { +struct parse_events_term *term; + +list_for_each_entry (term, head_config, list) { +fprintf (stderr,"%s," , term->config); + } + } +fprintf (stderr,"' that may result in non-fatal errors\n" ); + } +/* * Configure hardcoded terms first, no need to check * return value when called with fail == 0 ;)
tools/perf/util/pmu.c +21 -12
@@ -1056,7 +1056,8 @@ static char *pmu_formats_string(struct list_head *formats)* Setup one of config[12] attr members based on the * user input data - term parameter. */ -static int pmu_config_term(struct list_head *formats, +static int pmu_config_term(const char *pmu_name, + struct list_head *formats, struct perf_event_attr *attr, struct parse_events_term *term, struct list_head *head_terms,
@@ -1082,16 +1083,24 @@ static int pmu_config_term(struct list_head *formats, format =pmu_find_format (formats, term->config);if (!format) { -if (verbose >0 ) -printf ("Invalid event/parameter '%s'\n" , term->config); +char *pmu_term =pmu_formats_string (formats); +char *unknown_term; +char *help_msg; + +if (asprintf (&unknown_term, +"unknown term '%s' for pmu '%s'" , + term->config, pmu_name) <0 ) + unknown_term =strdup ("unknown term" ); + help_msg =parse_events_formats_error_string (pmu_term);if (err) { -char *pmu_term =pmu_formats_string (formats); -parse_events__handle_error (err, term->err_term, -strdup ("unknown term" ), -parse_events_formats_error_string (pmu_term)); -free (pmu_term); + unknown_term, + help_msg); + }else { +pr_debug ("%s (%s)\n" , unknown_term, help_msg); +free (unknown_term); } +free (pmu_term);return -EINVAL ; }
@@ -1168,7 +1177,7 @@ static int pmu_config_term(struct list_head *formats,return 0 ; } -int perf_pmu__config_terms (struct list_head *formats, +int perf_pmu__config_terms (const char *pmu_name,struct list_head *formats,struct perf_event_attr *attr,struct list_head *head_terms,bool zero,struct parse_events_error *err)
@@ -1176,7 +1185,7 @@ int perf_pmu__config_terms(struct list_head *formats,struct parse_events_term *term;list_for_each_entry (term, head_terms, list) { -if (pmu_config_term (formats, attr, term, head_terms, +if (pmu_config_term (pmu_name, formats, attr, term, head_terms, zero, err))return -EINVAL ; }
@@ -1196,8 +1205,8 @@ int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,bool zero = !!pmu->default_config; attr->type = pmu->type; -return perf_pmu__config_terms (&pmu->format, attr, head_terms, - zero, err); +return perf_pmu__config_terms (pmu->name, &pmu->format, attr, + head_terms, zero, err); } static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
tools/perf/util/pmu.h +1 -1
@@ -76,7 +76,7 @@ struct perf_pmu *perf_pmu__find_by_type(unsigned int type);int perf_pmu__config (struct perf_pmu *pmu,struct perf_event_attr *attr,struct list_head *head_terms,struct parse_events_error *error); -int perf_pmu__config_terms (struct list_head *formats, +int perf_pmu__config_terms (const char *pmu_name,struct list_head *formats,struct perf_event_attr *attr,struct list_head *head_terms,bool zero,struct parse_events_error *error);
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 12/23 perf test: improve pmu event metric testing
In reply to Ian RogersAdd a basic floating point number test to expr.
Break pmu-events test into 2 and add a test to verify that all pmu metric expressions simply parse. Try to parse all metric ids/events, failing if metrics for the current architecture fail to parse.
Tested on skylakex with the patch set in place. May fail on other architectures if metrics are invalid.
--- tools/perf/tests/builtin-test.c | 5 + tools/perf/tests/expr.c | 1 + tools/perf/tests/pmu-events.c | 158 ++++++++++++++++++++++++++++++-- tools/perf/tests/tests.h | 2 + 4 files changed, 160 insertions(+), 6 deletions(-)
tools/perf/tests/builtin-test.c +5 -0
@@ -75,6 +75,11 @@ static struct test generic_tests[] = { { .desc = "PMU events", .func = test__pmu_events, + .subtest = { + .get_nr = test__pmu_events_subtest_get_nr, + .get_desc = test__pmu_events_subtest_get_desc, + }, + }, { .desc = "DSO data read",
tools/perf/tests/expr.c +1 -0
@@ -39,6 +39,7 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) ret |=test (&ctx,"min(1,2) + 1" ,2 ); ret |=test (&ctx,"max(1,2) + 1" ,3 ); ret |=test (&ctx,"1+1 if 3*4 else 0" ,2 ); + ret |=test (&ctx,"1.1 + 2.1" ,3.2 );if (ret)return ret;
tools/perf/tests/pmu-events.c +152 -6
182 lines not on this page · raw message
tools/perf/tests/tests.h +2 -0
@@ -50,6 +50,8 @@ int test__perf_evsel__tp_sched_test(struct test *test, int subtest);int test__syscall_openat_tp_fields (struct test *test,int subtest);int test__pmu (struct test *test,int subtest);int test__pmu_events (struct test *test,int subtest); +const char *test__pmu_events_subtest_get_desc (int subtest); +int test__pmu_events_subtest_get_nr (void );int test__attr (struct test *test,int subtest);int test__dso_data (struct test *test,int subtest);int test__dso_data_cache (struct test *test,int subtest);
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 13/23 lib/bpf hashmap: increase portability
In reply to Ian RogersDon't include libbpf_internal.h as it is unused and has conflicting definitions, for example, with tools/perf/util/debug.h. Fix a non-glibc include path.
--- tools/lib/bpf/hashmap.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tools/lib/bpf/hashmap.h +1 -2
@@ -13,9 +13,8 @@#ifdef __GLIBC__#include <bits/wordsize.h> #else -#include <bits/reg.h> +#include <linux/bitops.h> #endif -#include "libbpf_internal.h" static inline size_t hash_bits (size_t h,int bits) {
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 14/23 libbpf: Fix memory leak and possible double-free in hashmap__clear
In reply to Ian RogersFrom: Andrii Nakryiko <[email protected]>
Fix memory leak in hashmap_clear() not freeing hashmap_entry structs for each of the remaining entries. Also NULL-out bucket list to prevent possible double-free between hashmap__clear() and hashmap__free().
Running test_progs-asan flavor clearly showed this problem.
--- tools/lib/bpf/hashmap.c | 7 +++++++ 1 file changed, 7 insertions(+)
tools/lib/bpf/hashmap.c +7 -0
@@ -59,7 +59,14 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,void hashmap__clear (struct hashmap *map) { +struct hashmap_entry *cur, *tmp; +int bkt; + +hashmap__for_each_entry_safe (map, cur, tmp, bkt) { +free (cur); + }free (map->buckets); + map->buckets =NULL ; map->cap = map->cap_bits = map->sz =0 ; }
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 15/23 perf expr: fix memory leaks in bison
In reply to Ian RogersAdd a destructor for strings to reclaim memory in the event of errors. Free the ID given for a lookup.
--- tools/perf/util/expr.y | 3 +++ 1 file changed, 3 insertions(+)
tools/perf/util/expr.y +3 -0
@@ -27,6 +27,7 @@ %token EXPR_PARSE EXPR_OTHER EXPR_ERROR %token <num> NUMBER %token <str> ID +%destructor { free ($$); } <str> %token MIN MAX IF ELSE SMT_ON %left MIN MAX IF %left '|'
@@ -94,8 +95,10 @@ if_expr: expr: NUMBER | ID { if (lookup_id(ctx, $1, &$$) < 0) { pr_debug("%s not found\n", $1); + free($1); YYABORT; } + free($1); } | expr '|' expr { $$ = (long)$1 | (long)$3; } | expr '&' expr { $$ = (long)$1 & (long)$3; }
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 16/23 perf evsel: fix 2 memory leaks
In reply to Ian RogersIf allocated, perf_pkg_mask and metric_events need freeing.
--- tools/perf/util/evsel.c | 2 ++ 1 file changed, 2 insertions(+)
tools/perf/util/evsel.c +2 -0
@@ -1268,6 +1268,8 @@ void evsel__exit(struct evsel *evsel)zfree (&evsel->group_name);zfree (&evsel->name);zfree (&evsel->pmu_name); +zfree (&evsel->per_pkg_mask); +zfree (&evsel->metric_events); perf_evsel__object.fini (evsel); }
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 17/23 perf expr: migrate expr ids table to libbpf's hashmap
In reply to Ian RogersUse a hashmap between a char* string and a double* value. While bpf's hashmap entries are size_t in size, we can't guarantee sizeof(size_t) >= sizeof(double). Avoid a memory allocation when gathering ids by making 0.0 a special value encoded as NULL.
Suggested by Andi Kleen:
https://lore.kernel.org/lkml/[email protected]/
and seconded by Jiri Olsa:
https://lore.kernel.org/lkml/20200423112915.GH1136647@krava/
--- tools/perf/tests/expr.c | 32 ++++----- tools/perf/tests/pmu-events.c | 22 +++--- tools/perf/util/expr.c | 125 ++++++++++++++++++---------------- tools/perf/util/expr.h | 22 +++--- tools/perf/util/expr.y | 22 +----- tools/perf/util/metricgroup.c | 86 +++++++++++------------ tools/perf/util/stat-shadow.c | 46 ++++++++----- 7 files changed, 176 insertions(+), 179 deletions(-)
tools/perf/tests/expr.c +14 -18
@@ -19,11 +19,9 @@ static int test(struct expr_parse_ctx *ctx, const char *e, double val2)int test__expr (struct test *t__maybe_unused ,int subtest__maybe_unused ) {const char *p; -const char **other; -double val; -int i, ret; +double val, *val_ptr; +int ret;struct expr_parse_ctx ctx; -int num_other;expr__ctx_init (&ctx);expr__add_id (&ctx,"FOO" ,1 );
@@ -52,25 +50,23 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) ret =expr__parse (&val, &ctx, p,1 );TEST_ASSERT_VAL ("missing operand" , ret ==-1 ); +hashmap__clear (&ctx.ids);TEST_ASSERT_VAL ("find other" , -expr__find_other ("FOO + BAR + BAZ + BOZO" ,"FOO" , &other, &num_other,1 ) ==0 ); -TEST_ASSERT_VAL ("find other" , num_other ==3 ); -TEST_ASSERT_VAL ("find other" , !strcmp (other[0 ],"BAR" )); -TEST_ASSERT_VAL ("find other" , !strcmp (other[1 ],"BAZ" )); -TEST_ASSERT_VAL ("find other" , !strcmp (other[2 ],"BOZO" )); -TEST_ASSERT_VAL ("find other" , other[3 ] ==NULL ); +expr__find_other ("FOO + BAR + BAZ + BOZO" ,"FOO" , &ctx,1 ) ==0 ); +TEST_ASSERT_VAL ("find other" ,hashmap__size (&ctx.ids) ==3 ); +TEST_ASSERT_VAL ("find other" ,hashmap__find (&ctx.ids,"BAR" , (void **)&val_ptr)); +TEST_ASSERT_VAL ("find other" ,hashmap__find (&ctx.ids,"BAZ" , (void **)&val_ptr)); +TEST_ASSERT_VAL ("find other" ,hashmap__find (&ctx.ids,"BOZO" , (void **)&val_ptr)); +hashmap__clear (&ctx.ids);TEST_ASSERT_VAL ("find other" ,expr__find_other ("EVENT1\\,param\\=?@ + EVENT2\\,param\\=?@" ,NULL , - &other, &num_other,3 ) ==0 ); -TEST_ASSERT_VAL ("find other" , num_other ==2 ); -TEST_ASSERT_VAL ("find other" , !strcmp (other[0 ],"EVENT1,param=3/" )); -TEST_ASSERT_VAL ("find other" , !strcmp (other[1 ],"EVENT2,param=3/" )); -TEST_ASSERT_VAL ("find other" , other[2 ] ==NULL ); + &ctx,3 ) ==0 ); +TEST_ASSERT_VAL ("find other" ,hashmap__size (&ctx.ids) ==2 ); +TEST_ASSERT_VAL ("find other" ,hashmap__find (&ctx.ids,"EVENT1,param=3/" , (void **)&val_ptr)); +TEST_ASSERT_VAL ("find other" ,hashmap__find (&ctx.ids,"EVENT2,param=3/" , (void **)&val_ptr)); -for (i =0 ; i < num_other; i++) -zfree (&other[i]); -free ((void *)other); +expr__ctx_clear (&ctx);return 0 ; }
tools/perf/tests/pmu-events.c +11 -11
@@ -428,8 +428,6 @@ static int test_parsing(void)struct pmu_events_map *map;struct pmu_event *pe;int i, j, k; -const char **ids; -int idnum;int ret =0 ;struct expr_parse_ctx ctx;double result;
@@ -443,13 +441,16 @@ static int test_parsing(void) } j =0 ;for (;;) { +struct hashmap_entry *cur; +size_t bkt; + pe = &map->table[j++];if (!pe->name && !pe->metric_group && !pe->metric_name)break ;if (!pe->metric_expr)continue ; -if (expr__find_other (pe->metric_expr,NULL , - &ids, &idnum,0 ) <0 ) { +if (expr__find_other (pe->metric_expr,NULL , &ctx,0 ) + <0 ) {pr_debug ("Parse other failed for map %s %s %s\n" , map->cpuid, map->version, map->type);pr_debug ("On metric %s\n" , pe->metric_name);
@@ -464,11 +465,12 @@ static int test_parsing(void)* trigger divide by zero when subtracted and so try to * make them unique. */ -for (k =0 ; k < idnum; k++) -expr__add_id (&ctx, ids[k], k +1 ); + k =1 ; +hashmap__for_each_entry ((&ctx.ids), cur, bkt) +expr__add_id (&ctx, cur->key, k++); -for (k =0 ; k < idnum; k++) { -if (check_parse_id (ids[k], map == cpus_map, pe)) +hashmap__for_each_entry ((&ctx.ids), cur, bkt) { +if (check_parse_id (cur->key, map == cpus_map, pe)) ret++; }
@@ -479,9 +481,7 @@ static int test_parsing(void)pr_debug ("On expression %s\n" , pe->metric_expr); ret++; } -for (k =0 ; k < idnum; k++) -zfree (&ids[k]); -free (ids); +expr__ctx_clear (&ctx); } }return ret;
tools/perf/util/expr.c +68 -57
154 lines not on this page · raw message
tools/perf/util/expr.h +9 -13
@@ -2,17 +2,10 @@#ifndef PARSE_CTX_H #define PARSE_CTX_H 1 -#define EXPR_MAX_OTHER 64 -#define MAX_PARSE_ID EXPR_MAX_OTHER - -struct expr_parse_id { -const char *name; -double val; -}; +#include <bpf/hashmap.h> struct expr_parse_ctx { -int num_ids; -struct expr_parse_id ids[MAX_PARSE_ID ]; +struct hashmap ids; }; struct expr_scanner_ctx {
@@ -21,9 +14,12 @@ struct expr_scanner_ctx { };void expr__ctx_init (struct expr_parse_ctx *ctx); -void expr__add_id (struct expr_parse_ctx *ctx,const char *id,double val); -int expr__parse (double *final_val,struct expr_parse_ctx *ctx,const char *expr,int runtime); -int expr__find_other (const char *expr,const char *one,const char ***other, -int *num_other,int runtime); +void expr__ctx_clear (struct expr_parse_ctx *ctx); +int expr__add_id (struct expr_parse_ctx *ctx,const char *id,double val); +int expr__get_id (struct expr_parse_ctx *ctx,const char *id,double *val_ptr); +int expr__parse (double *final_val,struct expr_parse_ctx *ctx, +const char *expr,int runtime); +int expr__find_other (const char *expr,const char *one, +struct expr_parse_ctx *ids,int runtime);#endif
tools/perf/util/expr.y +2 -20
@@ -47,19 +47,6 @@ static void expr_error(double *final_val __maybe_unused, pr_debug("%s\n", s); } -static int lookup_id(struct expr_parse_ctx *ctx, char *id, double *val) -{ - int i; - - for (i = 0; i < ctx->num_ids; i++) { - if (!strcasecmp(ctx->ids[i].name, id)) { - *val = ctx->ids[i].val; - return 0; - } - } - return -1; -} - %} %%
@@ -73,12 +60,7 @@ all_other: all_other other other: ID { - if (ctx->num_ids + 1 >= EXPR_MAX_OTHER) { - pr_err("failed: way too many variables"); - YYABORT; - } - - ctx->ids[ctx->num_ids++].name = $1; + expr__add_id(ctx, $1, 0.0); } | MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
@@ -93,7 +75,7 @@ if_expr: ; expr: NUMBER - | ID { if (lookup_id(ctx, $1, &$$) < 0) { + | ID { if (expr__get_id(ctx, $1, &$$)) { pr_debug("%s not found\n", $1); free($1); YYABORT;
tools/perf/util/metricgroup.c +43 -43
181 lines not on this page · raw message
tools/perf/util/stat-shadow.c +29 -17
113 lines not on this page · raw message
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 18/23 perf metricgroup: change evlist_used to a bitmap
In reply to Ian RogersUse a bitmap rather than an array of bools.
--- tools/perf/util/metricgroup.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
tools/perf/util/metricgroup.c +8 -10
@@ -95,7 +95,7 @@ struct egroup {static struct evsel *find_evsel_group (struct evlist *perf_evlist,struct expr_parse_ctx *pctx,struct evsel **metric_events, -bool *evlist_used) +unsigned long *evlist_used) {struct evsel *ev;bool leader_found;
@@ -105,7 +105,7 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,double *val_ptr; evlist__for_each_entry (perf_evlist, ev) { -if (evlist_used[j++]) +if (test_bit (j++, evlist_used))continue ;if (hashmap__find (&pctx->ids, ev->name, (void **)&val_ptr)) {if (!metric_events[i])
@@ -149,7 +149,7 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist, j++; } ev = metric_events[i]; - evlist_used[ev->idx] = true; +set_bit (ev->idx, evlist_used); }return metric_events[0 ];
@@ -165,13 +165,11 @@ static int metricgroup__setup_events(struct list_head *groups,int ret =0 ;struct egroup *eg;struct evsel *evsel; -bool *evlist_used; +unsigned long *evlist_used; - evlist_used =calloc (perf_evlist->core.nr_entries,sizeof (bool )); -if (!evlist_used) { - ret = -ENOMEM ; -return ret; - } + evlist_used =bitmap_alloc (perf_evlist->core.nr_entries); +if (!evlist_used) +return -ENOMEM ; list_for_each_entry (eg, groups, nd) {struct evsel **metric_events;
@@ -209,7 +207,7 @@ static int metricgroup__setup_events(struct list_head *groups,list_add (&expr->nd, &me->head); } -free (evlist_used); +bitmap_free (evlist_used);return ret; }
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 19/23 perf metricgroup: free metric_events on error
In reply to Ian RogersAvoid a simple memory leak.
--- tools/perf/util/metricgroup.c | 3 +++ 1 file changed, 3 insertions(+)
tools/perf/util/metricgroup.c +3 -0
@@ -185,6 +185,7 @@ static int metricgroup__setup_events(struct list_head *groups,if (!evsel) {pr_debug ("Cannot resolve %s: %s\n" , eg->metric_name, eg->metric_expr); +free (metric_events);continue ; }for (i =0 ; metric_events[i]; i++)
@@ -192,11 +193,13 @@ static int metricgroup__setup_events(struct list_head *groups, me =metricgroup__lookup (metric_events_list, evsel, true);if (!me) { ret = -ENOMEM ; +free (metric_events);break ; } expr =malloc (sizeof (struct metric_expr ));if (!expr) { ret = -ENOMEM ; +free (metric_events);break ; } expr->metric_expr = eg->metric_expr;
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 20/23 perf metricgroup: always place duration_time last
In reply to Ian RogersIf a metric contains the duration_time event then the event is placed outside of the metric's group of events. Rather than split the group, make it so the duration_time is immediately after the group.
--- tools/perf/util/metricgroup.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
tools/perf/util/metricgroup.c +10 -10
@@ -421,8 +421,8 @@ static void metricgroup__add_metric_weak_group(struct strbuf *events, struct expr_parse_ctx *ctx) {struct hashmap_entry *cur; -size_t bkt, i =0 ; -bool no_group = false; +size_t bkt; +bool no_group = true, has_duration = false;hashmap__for_each_entry ((&ctx->ids), cur, bkt) {pr_debug ("found event %s\n" , (const char *)cur->key);
@@ -432,20 +432,20 @@ static void metricgroup__add_metric_weak_group(struct strbuf *events,* group. */ if (!strcmp (cur->key,"duration_time" )) { -if (i >0 ) -strbuf_addf (events,"}:W," ); -strbuf_addf (events,"duration_time" ); - no_group = true; + has_duration = true;continue ; }strbuf_addf (events,"%s%s" , - i ==0 || no_group ?"{" :"," , + no_group ?"{" :"," , (const char *)cur->key); no_group = false; - i++; } -if (!no_group) -strbuf_addf (events,"}:W" ); +if (!no_group) { +strbuf_addf (events,"}:W" ); +if (has_duration) +strbuf_addf (events,",duration_time" ); + }else if (has_duration) +strbuf_addf (events,"duration_time" ); } static void metricgroup__add_metric_non_group(struct strbuf *events,
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 21/23 perf metricgroup: delay events string creation
In reply to Ian RogersCurrently event groups are placed into groups_list at the same time as the events string containing the events is built. Separate these two operations and build the groups_list first, then the event string from the groups_list. This adds an ability to reorder the groups_list that will be used in a later patch.
--- tools/perf/util/metricgroup.c | 38 +++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-)
tools/perf/util/metricgroup.c +25 -13
@@ -90,6 +90,7 @@ struct egroup {const char *metric_expr;const char *metric_unit;int runtime; +bool has_constraint; }; static struct evsel *find_evsel_group(struct evlist *perf_evlist,
@@ -496,8 +497,8 @@ int __weak arch_get_runtimeparam(void)return 1 ; } -static int __metricgroup__add_metric (struct strbuf *events, -struct list_head *group_list,struct pmu_event *pe,int runtime) +static int __metricgroup__add_metric (struct list_head *group_list, +struct pmu_event *pe,int runtime) {struct egroup *eg;
@@ -510,6 +511,7 @@ static int __metricgroup__add_metric(struct strbuf *events, eg->metric_expr = pe->metric_expr; eg->metric_unit = pe->unit; eg->runtime = runtime; + eg->has_constraint =metricgroup__has_constraint (pe);if (expr__find_other (pe->metric_expr,NULL , &eg->pctx, runtime) <0 ) {expr__ctx_clear (&eg->pctx);
@@ -517,14 +519,6 @@ static int __metricgroup__add_metric(struct strbuf *events,return -EINVAL ; } -if (events->len >0 ) -strbuf_addf (events,"," ); - -if (metricgroup__has_constraint (pe)) -metricgroup__add_metric_non_group (events, &eg->pctx); -else -metricgroup__add_metric_weak_group (events, &eg->pctx); -list_add_tail (&eg->nd, group_list);return 0 ;
@@ -535,6 +529,7 @@ static int metricgroup__add_metric(const char *metric, struct strbuf *events, {struct pmu_events_map *map =perf_pmu__find_map (NULL );struct pmu_event *pe; +struct egroup *eg;int i, ret = -EINVAL ;if (!map)
@@ -553,7 +548,8 @@ static int metricgroup__add_metric(const char *metric, struct strbuf *events,pr_debug ("metric expr %s for %s\n" , pe->metric_expr, pe->metric_name);if (!strstr (pe->metric_expr,"?" )) { - ret =__metricgroup__add_metric (events, group_list, pe,1 ); + ret =__metricgroup__add_metric (group_list, + pe,1 ); }else {int j, count;
@@ -564,13 +560,29 @@ static int metricgroup__add_metric(const char *metric, struct strbuf *events,* those events to group_list. */ -for (j =0 ; j < count; j++) - ret =__metricgroup__add_metric (events, group_list, pe, j); +for (j =0 ; j < count; j++) { + ret =__metricgroup__add_metric ( + group_list, pe, j); + } }if (ret == -ENOMEM )break ; } } +if (!ret) { + list_for_each_entry (eg, group_list, nd) { +if (events->len >0 ) +strbuf_addf (events,"," ); + +if (eg->has_constraint) { +metricgroup__add_metric_non_group (events, + &eg->pctx); + }else { +metricgroup__add_metric_weak_group (events, + &eg->pctx); + } + } + }return ret; }
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 22/23 perf metricgroup: order event groups by size
In reply to Ian RogersWhen adding event groups to the group list, insert them in size order. This performs an insertion sort on the group list. By placing the largest groups at the front of the group list it is possible to see if a larger group contains the same events as a later group. This can make the later group redundant - it can reuse the events from the large group. A later patch will add this sharing.
--- tools/perf/util/metricgroup.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
tools/perf/util/metricgroup.c +15 -1
@@ -519,7 +519,21 @@ static int __metricgroup__add_metric(struct list_head *group_list,return -EINVAL ; } -list_add_tail (&eg->nd, group_list); +if (list_empty (group_list)) +list_add (&eg->nd, group_list); +else { +struct list_head *pos; + +/* Place the largest groups at the front. */ +list_for_each_prev (pos, group_list) { + struct egroup *old = list_entry(pos, struct egroup, nd); + +if (hashmap__size (&eg->pctx.ids) <= +hashmap__size (&old->pctx.ids)) +break ; + } +list_add (&eg->nd, pos); + }return 0 ; }
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Ian Rogers
reviewerRFC PATCH v2 23/23 perf metricgroup: remove duped metric group events
In reply to Ian RogersA metric group contains multiple metrics. These metrics may use the same events. If metrics use separate events then it leads to more multiplexing and overall metric counts fail to sum to 100%. Modify how metrics are associated with events so that if the events in an earlier group satisfy the current metric, the same events are used. A record of used events is kept and at the end of processing unnecessary events are eliminated.
Before:
$ perf stat -a -M TopDownL1 sleep 1
Performance counter stats for 'system wide':
920,211,343 uops_issued.any # 0.5 Backend_Bound (16.56%)
1,977,733,128 idq_uops_not_delivered.core (16.56%)
51,668,510 int_misc.recovery_cycles (16.56%)
732,305,692 uops_retired.retire_slots (16.56%)
1,497,621,849 cycles (16.56%)
721,098,274 uops_issued.any # 0.1 Bad_Speculation (16.79%)
1,332,681,791 cycles (16.79%)
552,475,482 uops_retired.retire_slots (16.79%)
47,708,340 int_misc.recovery_cycles (16.79%)
1,383,713,292 cycles
# 0.4 Frontend_Bound (16.76%)
2,013,757,701 idq_uops_not_delivered.core (16.76%)
1,373,363,790 cycles
# 0.1 Retiring (33.54%)
577,302,589 uops_retired.retire_slots (33.54%)
392,766,987 inst_retired.any # 0.3 IPC (50.24%)
1,351,873,350 cpu_clk_unhalted.thread (50.24%)
1,332,510,318 cycles
# 5330041272.0 SLOTS (49.90%)
1.006336145 seconds time elapsedAfter:
$ perf stat -a -M TopDownL1 sleep 1
Performance counter stats for 'system wide':
765,949,145 uops_issued.any # 0.1 Bad_Speculation
# 0.5 Backend_Bound (50.09%)
1,883,830,591 idq_uops_not_delivered.core # 0.3 Frontend_Bound (50.09%)
48,237,080 int_misc.recovery_cycles (50.09%)
581,798,385 uops_retired.retire_slots # 0.1 Retiring (50.09%)
1,361,628,527 cycles
# 5446514108.0 SLOTS (50.09%)
391,415,714 inst_retired.any # 0.3 IPC (49.91%)
1,336,486,781 cpu_clk_unhalted.thread (49.91%)
1.005469298 seconds time elapsedNote: Bad_Speculation + Backend_Bound + Frontend_Bound + Retiring = 100% after, where as before it is 110%. After there are 2 groups, whereas before there are 6. After the cycles event appears once, before it appeared 5 times.
--- tools/perf/util/metricgroup.c | 97 ++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 36 deletions(-)
tools/perf/util/metricgroup.c +61 -36
@@ -93,44 +93,72 @@ struct egroup {bool has_constraint; }; +/** +* Find a group of events in perf_evlist that correpond to those from a parsed +* metric expression. +* @perf_evlist: a list of events something like: {metric1 leader, metric1 +* sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling, +* metric2 sibling}:W,duration_time +* @pctx: the parse context for the metric expression. +* @has_constraint: is there a contraint on the group of events? In which case +* the events won't be grouped. +* @metric_events: out argument, null terminated array of evsel's associated +* with the metric. +* @evlist_used: in/out argument, bitmap tracking which evlist events are used. +* @return the first metric event or NULL on failure. +*/ static struct evsel *find_evsel_group (struct evlist *perf_evlist,struct expr_parse_ctx *pctx, +bool has_constraint,struct evsel **metric_events,unsigned long *evlist_used) { -struct evsel *ev; -bool leader_found; -const size_t idnum =hashmap__size (&pctx->ids); -size_t i =0 ; -int j =0 ; -double *val_ptr; +struct evsel *ev, *current_leader =NULL ; +double *val_ptr; +int i =0 , matched_events =0 , events_to_match; +const int idnum = (int )hashmap__size (&pctx->ids); + +/* duration_time is grouped separately. */ +if (!has_constraint && +hashmap__find (&pctx->ids,"duration_time" , (void **)&val_ptr)) + events_to_match = idnum -1 ; +else + events_to_match = idnum; evlist__for_each_entry (perf_evlist, ev) { -if (test_bit (j++, evlist_used)) +/* +* Events with a constraint aren't grouped and match the first +* events available. +*/ +if (has_constraint && ev->weak_group)continue ; -if (hashmap__find (&pctx->ids, ev->name, (void **)&val_ptr)) { -if (!metric_events[i]) - metric_events[i] = ev; - i++; -if (i == idnum) -break ; - }else { -/* Discard the whole match and start again */ - i =0 ; +if (!has_constraint && ev->leader != current_leader) { +/* +* Start of a new group, discard the whole match and +* start again. +*/ + matched_events =0 ;memset (metric_events,0 ,sizeof (struct evsel *) * idnum); + current_leader = ev->leader; + } +if (hashmap__find (&pctx->ids, ev->name, (void **)&val_ptr)) + metric_events[matched_events++] = ev; +if (matched_events == events_to_match) +break ; + } -if (hashmap__find (&pctx->ids, ev->name, (void **)&val_ptr)) { -if (!metric_events[i]) - metric_events[i] = ev; - i++; -if (i == idnum) -break ; +if (events_to_match != idnum) { +/* Add the first duration_time. */ + evlist__for_each_entry (perf_evlist, ev) { +if (!strcmp (ev->name,"duration_time" )) { + metric_events[matched_events++] = ev; +break ; } } } -if (i != idnum) { +if (matched_events != idnum) {/* Not whole match */ return NULL ; }
@@ -138,18 +166,8 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist, metric_events[idnum] =NULL ;for (i =0 ; i < idnum; i++) { - leader_found = false; -evlist__for_each_entry (perf_evlist, ev) { -if (!leader_found && (ev == metric_events[i])) - leader_found = true; - -if (leader_found && - !strcmp (ev->name, metric_events[i]->name)) { - ev->metric_leader = metric_events[i]; - } - j++; - } ev = metric_events[i]; + ev->metric_leader = ev;set_bit (ev->idx, evlist_used); }
@@ -165,7 +183,7 @@ static int metricgroup__setup_events(struct list_head *groups,int i =0 ;int ret =0 ;struct egroup *eg; -struct evsel *evsel; +struct evsel *evsel, *tmp;unsigned long *evlist_used; evlist_used =bitmap_alloc (perf_evlist->core.nr_entries);
@@ -181,7 +199,8 @@ static int metricgroup__setup_events(struct list_head *groups, ret = -ENOMEM ;break ; } - evsel =find_evsel_group (perf_evlist, &eg->pctx, metric_events, + evsel =find_evsel_group (perf_evlist, &eg->pctx, + eg->has_constraint, metric_events, evlist_used);if (!evsel) { pr_debug("Cannot resolve %s: %s\n",
@@ -211,6 +230,12 @@ static int metricgroup__setup_events(struct list_head *groups,list_add (&expr->nd, &me->head); } + evlist__for_each_entry_safe (perf_evlist, tmp, evsel) { +if (!test_bit (evsel->idx, evlist_used)) { +evlist__remove (perf_evlist, evsel); +evsel__delete (evsel); + } + }bitmap_free (evlist_used);return ret;
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Issue of metrics for multiple uncore PMUs (was Re: [RFC PATCH v2 23/23] perf metricgroup: remove duped metric group events)
On 07/05/2020 15:08, Ian Rogers wrote:
Hi Ian,
I was wondering if you ever tested commit 2440689d62e9 ("perf metricgroup: Remove duped metric group events") for when we have a metric which aliases multiple instances of the same uncore PMU in the system?
I have been rebasing some of my arm64 perf work to v5.9-rc7, and find an issue where find_evsel_group() fails for the uncore metrics under the condition mentioned above.
Unfortunately I don't have an x86 machine to which this test applies. However, as an experiment, I added a test metric to my broadwell JSON:
tools/perf/pmu-events/arch/x86/broadwell/bdw-metrics.json +6 -0
@@ -348,5 +348,11 @@ "MetricExpr": "(cstate_pkg@c7\\-residency@ / msr@tsc@) * 100", "MetricGroup": "Power", "MetricName": "C7_Pkg_Residency" + }, + { + "BriefDescription": "test metric", + "MetricExpr": "UNC_CBO_XSNP_RESPONSE.MISS_XCORE * UNC_CBO_XSNP_RESPONSE.MISS_EVICTION", + "MetricGroup": "Test", + "MetricName": "test_metric_inc" }
]
And get this:
john@localhost:~/linux/tools/perf> sudo ./perf stat -v -M test_metric_inc sleep 1 Using CPUID GenuineIntel-6-3D-4
metric expr unc_cbo_xsnp_response.miss_xcore * unc_cbo_xsnp_response.miss_eviction for test_metric_inc found event unc_cbo_xsnp_response.miss_eviction
found event unc_cbo_xsnp_response.miss_xcore
adding {unc_cbo_xsnp_response.miss_eviction,unc_cbo_xsnp_response.miss_xcore}:W unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_1/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_0/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_1/umask=0x41,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_0/umask=0x41,event=0x22/
Cannot resolve test_metric_inc: unc_cbo_xsnp_response.miss_xcore * unc_cbo_xsnp_response.miss_eviction
task-clock: 688876 688876 688876
context-switches: 2 688876 688876
cpu-migrations: 0 688876 688876
page-faults: 69 688876 688876
cycles: 2101719 695690 695690
instructions: 1180534 695690 695690
branches: 249450 695690 695690
branch-misses: 10815 695690 695690
Performance counter stats for 'sleep 1':
0.69 msec task-clock # 0.001 CPUs
utilized
2 context-switches # 0.003 M/sec
0 cpu-migrations # 0.000 K/sec
69 page-faults # 0.100 M/sec
2,101,719 cycles # 3.051 GHz
1,180,534 instructions # 0.56 insn per
cycle
249,450 branches # 362.112 M/sec
10,815 branch-misses # 4.34% of all
branches
1.001177693 seconds time elapsed
0.001149000 seconds user
0.000000000 seconds sysjohn@localhost:~/linux/tools/perf>
Any idea what is going wrong here, before I have to dive in? The issue seems to be this named commit.
Thanks,
John
Quoted 9 lines
A metric group contains multiple metrics. These metrics may use the same events. If metrics use separate events then it leads to more multiplexing and overall metric counts fail to sum to 100%. Modify how metrics are associated with events so that if the events in an earlier group satisfy the current metric, the same events are used. A record of used events is kept and at the end of processing unnecessary events are eliminated.
Before:
Ian Rogers
reviewerRe: Issue of metrics for multiple uncore PMUs (was Re: [RFC PATCH v2 23/23] perf metricgroup: remove duped metric group events)
Quoted 9 lines · John Garry
On 07/05/2020 15:08, Ian Rogers wrote:
Hi Ian,
I was wondering if you ever tested commit
2440689d62e9("perf
metricgroup: Remove duped metric group events") for when we have a metric which aliases multiple instances of the same uncore PMU in the system?
Sorry for this, I hadn't tested such a metric and wasn't aware of how the aliasing worked. I sent a fix for this issue here:
https://lore.kernel.org/lkml/[email protected]/
Could you see if this addresses the issue for you? I don't see the change in Arnaldo's trees yet.
Thanks,
Ian
Quoted 96 lines
I have been rebasing some of my arm64 perf work to v5.9-rc7, and find an issue where
find_evsel_group()fails for the uncore metrics under the condition mentioned above.Unfortunately I don't have an x86 machine to which this test applies. However, as an experiment, I added a test metric to my broadwell JSON:
tools/perf/pmu-events/arch/x86/broadwell/bdw-metrics.json +6
-0@@ -348,5 +348,11 @@ "MetricExpr": "(cstate_pkg@c7\\-residency@ / msr@tsc@) * 100", "MetricGroup": "Power", "MetricName": "C7_Pkg_Residency" + }, + { + "BriefDescription": "test metric", + "MetricExpr": "UNC_CBO_XSNP_RESPONSE.MISS_XCORE * UNC_CBO_XSNP_RESPONSE.MISS_EVICTION", + "MetricGroup": "Test", + "MetricName": "test_metric_inc" }]
And get this:
john@localhost:~/linux/tools/perf> sudo ./perf stat -v -M
test_metric_incsleep 1
Using CPUID GenuineIntel-6-3D-4
metric expr unc_cbo_xsnp_response.miss_xcore * unc_cbo_xsnp_response.miss_eviction fortest_metric_inc
found event unc_cbo_xsnp_response.miss_eviction
found event unc_cbo_xsnp_response.miss_xcore
adding {unc_cbo_xsnp_response.miss_eviction,unc_cbo_xsnp_response.miss_xcore}:W unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_1/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_0/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_1/umask=0x41,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_0/umask=0x41,event=0x22/ Cannot resolvetest_metric_inc: unc_cbo_xsnp_response.miss_xcore * unc_cbo_xsnp_response.miss_eviction
task-clock: 688876 688876 688876
context-switches: 2 688876 688876
cpu-migrations: 0 688876 688876
page-faults: 69 688876 688876
cycles: 2101719 695690 695690
instructions: 1180534 695690 695690
branches: 249450 695690 695690
branch-misses: 10815 695690 695690Performance counter stats for 'sleep 1':
0.69 msec task-clock # 0.001 CPUs
utilized
2 context-switches # 0.003 M/sec0 cpu-migrations # 0.000 K/sec
69 page-faults # 0.100 M/sec
2,101,719 cycles # 3.051 GHz
1,180,534 instructions # 0.56 insn per
cycle
249,450 branches # 362.112 M/sec10,815 branch-misses # 4.34% of all
branches1.001177693 seconds time elapsed 0.001149000 seconds user 0.000000000 seconds sysjohn@localhost:~/linux/tools/perf>
Any idea what is going wrong here, before I have to dive in? The issue seems to be this named commit.
Thanks,
JohnQuoted 9 lines
A metric group contains multiple metrics. These metrics may use the same events. If metrics use separate events then it leads to more multiplexing and overall metric counts fail to sum to 100%. Modify how metrics are associated with events so that if the events in an earlier group satisfy the current metric, the same events are used. A record of used events is kept and at the end of processing unnecessary events are eliminated.
Before:
Re: Issue of metrics for multiple uncore PMUs (was Re: [RFC PATCH v2 23/23] perf metricgroup: remove duped metric group events)
Quoted 11 lines · Ian Rogers
Quoted 9 lines · John Garry
On 07/05/2020 15:08, Ian Rogers wrote:
Hi Ian,
I was wondering if you ever tested commit
2440689d62e9("perf
metricgroup: Remove duped metric group events") for when we have a metric which aliases multiple instances of the same uncore PMU in the system?
Quoted 5 lines
Sorry for this, I hadn't tested such a metric and wasn't aware of how the aliasing worked. I sent a fix for this issue here:
https://lore.kernel.org/lkml/[email protected]/
Could you see if this addresses the issue for you? I don't see the change in Arnaldo's trees yet.
Unfortunately this does not seem to fix my issue.
So for that patch, you say you fix metric expression for DRAM_BW_Use, which is:
{
"BriefDescription": "Average external Memory Bandwidth Use for reads
and writes [GB / sec]",
"MetricExpr": "( 64 * ( uncore_imc@cas_count_read@ +
uncore_imc@cas_count_write@ ) / 1000000000 ) / duration_time",
"MetricGroup": "Memory_BW",
"MetricName": "DRAM_BW_Use"
},
But this metric expression does not include any alias events; rather I think it is just cas_count_write + cas_count_read event count for PMU uncore_imc / duration_time.
When I say alias, I mean - as an example, we have event:
{
"BriefDescription": "write requests to memory controller.
Derived from unc_m_cas_count.wr",
"Counter": "0,1,2,3",
"EventCode": "0x4",
"EventName": "LLC_MISSES.MEM_WRITE",
"PerPkg": "1",
"ScaleUnit": "64Bytes",
"UMask": "0xC",
"Unit": "iMC"
},And then reference LLC_MISSES.MEM_WRITE in a metric expression:
"MetricExpr": "LLC_MISSES.MEM_WRITE / duration_time",
This is what seems to be broken for when the alias matches > 1 PMU.
Please check this.
Thanks,
John
Quoted 102 lines
Thanks,
IanQuoted 78 lines
I have been rebasing some of my arm64 perf work to v5.9-rc7, and find an issue where
find_evsel_group()fails for the uncore metrics under the condition mentioned above.Unfortunately I don't have an x86 machine to which this test applies. However, as an experiment, I added a test metric to my broadwell JSON:
tools/perf/pmu-events/arch/x86/broadwell/bdw-metrics.json +6
-0@@ -348,5 +348,11 @@ "MetricExpr": "(cstate_pkg@c7\\-residency@ / msr@tsc@) * 100", "MetricGroup": "Power", "MetricName": "C7_Pkg_Residency" + }, + { + "BriefDescription": "test metric", + "MetricExpr": "UNC_CBO_XSNP_RESPONSE.MISS_XCORE * UNC_CBO_XSNP_RESPONSE.MISS_EVICTION", + "MetricGroup": "Test", + "MetricName": "test_metric_inc" }]
And get this:
john@localhost:~/linux/tools/perf> sudo ./perf stat -v -M
test_metric_incsleep 1
Using CPUID GenuineIntel-6-3D-4
metric expr unc_cbo_xsnp_response.miss_xcore * unc_cbo_xsnp_response.miss_eviction fortest_metric_inc
found event unc_cbo_xsnp_response.miss_eviction
found event unc_cbo_xsnp_response.miss_xcore
adding {unc_cbo_xsnp_response.miss_eviction,unc_cbo_xsnp_response.miss_xcore}:W unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_1/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_0/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_1/umask=0x41,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_0/umask=0x41,event=0x22/ Cannot resolvetest_metric_inc: unc_cbo_xsnp_response.miss_xcore * unc_cbo_xsnp_response.miss_eviction
task-clock: 688876 688876 688876
context-switches: 2 688876 688876
cpu-migrations: 0 688876 688876
page-faults: 69 688876 688876
cycles: 2101719 695690 695690
instructions: 1180534 695690 695690
branches: 249450 695690 695690
branch-misses: 10815 695690 695690Performance counter stats for 'sleep 1':
0.69 msec task-clock # 0.001 CPUs
utilized
2 context-switches # 0.003 M/sec0 cpu-migrations # 0.000 K/sec
69 page-faults # 0.100 M/sec
2,101,719 cycles # 3.051 GHz
1,180,534 instructions # 0.56 insn per
cycle
249,450 branches # 362.112 M/sec10,815 branch-misses # 4.34% of all
branches1.001177693 seconds time elapsed 0.001149000 seconds user 0.000000000 seconds sysQuoted 18 lines
john@localhost:~/linux/tools/perf>
Any idea what is going wrong here, before I have to dive in? The issue seems to be this named commit.
Thanks,
JohnQuoted 9 lines
A metric group contains multiple metrics. These metrics may use the same events. If metrics use separate events then it leads to more multiplexing and overall metric counts fail to sum to 100%. Modify how metrics are associated with events so that if the events in an earlier group satisfy the current metric, the same events are used. A record of used events is kept and at the end of processing unnecessary events are eliminated.
Before:
.
Ian Rogers
reviewerRe: Issue of metrics for multiple uncore PMUs (was Re: [RFC PATCH v2 23/23] perf metricgroup: remove duped metric group events)
Quoted 51 lines · John Garry
Quoted 11 lines · Ian Rogers
Quoted 9 lines · John Garry
On 07/05/2020 15:08, Ian Rogers wrote:
Hi Ian,
I was wondering if you ever tested commit
2440689d62e9("perf
metricgroup: Remove duped metric group events") for when we have a metric which aliases multiple instances of the same uncore PMU in the system?Quoted 5 lines
Sorry for this, I hadn't tested such a metric and wasn't aware of how the aliasing worked. I sent a fix for this issue here:
https://lore.kernel.org/lkml/[email protected]/
Could you see if this addresses the issue for you? I don't see the change in Arnaldo's trees yet.Unfortunately this does not seem to fix my issue.
So for that patch, you say you fix metric expression for
DRAM_BW_Use, which is:{
"BriefDescription": "Average external Memory Bandwidth Use for reads
and writes [GB / sec]",
"MetricExpr": "( 64 * ( uncore_imc@cas_count_read@ +
uncore_imc@cas_count_write@ ) / 1000000000 ) /duration_time",
"MetricGroup": "Memory_BW",
"MetricName": "DRAM_BW_Use"
},But this metric expression does not include any alias events; rather I think it is just
cas_count_write+cas_count_readevent count for PMUuncore_imc/duration_time.When I say alias, I mean - as an example, we have event:
{
"BriefDescription": "write requests to memory controller.
Derived from unc_m_cas_count.wr","Counter": "0,1,2,3", "EventCode": "0x4", "EventName": "LLC_MISSES.MEM_WRITE", "PerPkg": "1", "ScaleUnit": "64Bytes", "UMask": "0xC", "Unit": "iMC" },
Quoted 7 lines
And then reference LLC_MISSES.MEM_WRITE in a metric expression:
"MetricExpr": "LLC_MISSES.MEM_WRITE /
duration_time",This is what seems to be broken for when the alias matches > 1 PMU.
Please check this.
Happy to check. Can you provide a reproduction? Looking on broadwell this metric doesn't exist.
Thanks,
Ian
Quoted 106 lines
Thanks,
JohnQuoted 102 lines
Thanks,
IanQuoted 78 lines
I have been rebasing some of my arm64 perf work to v5.9-rc7, and find an issue where
find_evsel_group()fails for the uncore metrics under the condition mentioned above.Unfortunately I don't have an x86 machine to which this test applies. However, as an experiment, I added a test metric to my broadwell JSON:
tools/perf/pmu-events/arch/x86/broadwell/bdw-metrics.json +6
-0@@ -348,5 +348,11 @@ "MetricExpr": "(cstate_pkg@c7\\-residency@ / msr@tsc@) * 100", "MetricGroup": "Power", "MetricName": "C7_Pkg_Residency" + }, + { + "BriefDescription": "test metric", + "MetricExpr": "UNC_CBO_XSNP_RESPONSE.MISS_XCORE * UNC_CBO_XSNP_RESPONSE.MISS_EVICTION", + "MetricGroup": "Test", + "MetricName": "test_metric_inc" }]
And get this:
john@localhost:~/linux/tools/perf> sudo ./perf stat -v -M
test_metric_incsleep 1
Using CPUID GenuineIntel-6-3D-4
metric expr unc_cbo_xsnp_response.miss_xcore * unc_cbo_xsnp_response.miss_eviction fortest_metric_inc
found event unc_cbo_xsnp_response.miss_eviction
found event unc_cbo_xsnp_response.miss_xcore
adding {unc_cbo_xsnp_response.miss_eviction,unc_cbo_xsnp_response.miss_xcore}:W unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_1/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_0/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_1/umask=0x41,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_0/umask=0x41,event=0x22/ Cannot resolvetest_metric_inc: unc_cbo_xsnp_response.miss_xcore * unc_cbo_xsnp_response.miss_eviction
task-clock: 688876 688876 688876
context-switches: 2 688876 688876
cpu-migrations: 0 688876 688876
page-faults: 69 688876 688876
cycles: 2101719 695690 695690
instructions: 1180534 695690 695690
branches: 249450 695690 695690
branch-misses: 10815 695690 695690Performance counter stats for 'sleep 1':
0.69 msec task-clock # 0.001 CPUs
utilized
2 context-switches # 0.003 M/sec0 cpu-migrations # 0.000 K/sec
69 page-faults # 0.100 M/sec
2,101,719 cycles # 3.051 GHz
1,180,534 instructions # 0.56 insn per
cycle
249,450 branches # 362.112 M/sec10,815 branch-misses # 4.34% of all
branches1.001177693 seconds time elapsed 0.001149000 seconds user 0.000000000 seconds sysQuoted 18 lines
john@localhost:~/linux/tools/perf>
Any idea what is going wrong here, before I have to dive in? The issue seems to be this named commit.
Thanks,
JohnQuoted 9 lines
A metric group contains multiple metrics. These metrics may use the same events. If metrics use separate events then it leads to more multiplexing and overall metric counts fail to sum to 100%. Modify how metrics are associated with events so that if the events in an earlier group satisfy the current metric, the same events are used. A record of used events is kept and at the end of processing unnecessary events are eliminated.
Before:
.
Re: Issue of metrics for multiple uncore PMUs (was Re: [RFC PATCH v2 23/23] perf metricgroup: remove duped metric group events)
Quoted 52 lines · Ian Rogers
Quoted 51 lines · John Garry
Quoted 11 lines · Ian Rogers
Quoted 9 lines · John Garry
On 07/05/2020 15:08, Ian Rogers wrote:
Hi Ian,
I was wondering if you ever tested commit
2440689d62e9("perf
metricgroup: Remove duped metric group events") for when we have a metric which aliases multiple instances of the same uncore PMU in the system?Quoted 5 lines
Sorry for this, I hadn't tested such a metric and wasn't aware of how the aliasing worked. I sent a fix for this issue here:
https://lore.kernel.org/lkml/[email protected]/
Could you see if this addresses the issue for you? I don't see the change in Arnaldo's trees yet.Unfortunately this does not seem to fix my issue.
So for that patch, you say you fix metric expression for
DRAM_BW_Use, which is:{
"BriefDescription": "Average external Memory Bandwidth Use for reads
and writes [GB / sec]",
"MetricExpr": "( 64 * ( uncore_imc@cas_count_read@ +
uncore_imc@cas_count_write@ ) / 1000000000 ) /duration_time",
"MetricGroup": "Memory_BW",
"MetricName": "DRAM_BW_Use"
},But this metric expression does not include any alias events; rather I think it is just
cas_count_write+cas_count_readevent count for PMUuncore_imc/duration_time.When I say alias, I mean - as an example, we have event:
{
"BriefDescription": "write requests to memory controller.
Derived from unc_m_cas_count.wr","Counter": "0,1,2,3", "EventCode": "0x4", "EventName": "LLC_MISSES.MEM_WRITE", "PerPkg": "1", "ScaleUnit": "64Bytes", "UMask": "0xC", "Unit": "iMC" },
Quoted 8 lines
Quoted 7 lines
And then reference LLC_MISSES.MEM_WRITE in a metric expression:
"MetricExpr": "LLC_MISSES.MEM_WRITE /
duration_time",This is what seems to be broken for when the alias matches > 1 PMU.
Please check this.
Hi Ian,
Quoted 1 line
Happy to check.
So I am, but the code is a little complicated :)
Quoted 2 lines
Can you provide a reproduction? Looking on broadwell this metric doesn't exist.
Right, I just added this test metric as my 2x x86 platform has no examples which I can find:
tools/perf/pmu-events/arch/x86/broadwell/bdw-metrics.json +6 -0
@@ -348,5 +348,11 @@ "MetricExpr": "(cstate_pkg@c7\\-residency@ / msr@tsc@) * 100", "MetricGroup": "Power", "MetricName": "C7_Pkg_Residency" + }, + { + "BriefDescription": "test metric", + "MetricExpr": "UNC_CBO_XSNP_RESPONSE.MISS_XCORE * UNC_CBO_XSNP_RESPONSE.MISS_EVICTION", + "MetricGroup": "Test", + "MetricName": "test_metric_inc" }
]
I'll try to find a better mainline example, though, but I'm not hopeful ...
Thanks,
John
Re: Issue of metrics for multiple uncore PMUs (was Re: [RFC PATCH v2 23/23] perf metricgroup: remove duped metric group events)
Quoted 21 lines · John Garry
Quoted 2 lines
Can you provide a reproduction? Looking on broadwell this metric doesn't exist.
Right, I just added this test metric as my 2x x86 platform has no examples which I can find:
tools/perf/pmu-events/arch/x86/broadwell/bdw-metrics.json +6
-0@@ -348,5 +348,11 @@ "MetricExpr": "(cstate_pkg@c7\\-residency@ / msr@tsc@) * 100", "MetricGroup": "Power", "MetricName": "C7_Pkg_Residency" + }, + { + "BriefDescription": "test metric", + "MetricExpr": "UNC_CBO_XSNP_RESPONSE.MISS_XCORE * UNC_CBO_XSNP_RESPONSE.MISS_EVICTION", + "MetricGroup": "Test", + "MetricName": "test_metric_inc" }
Quoted 2 lines
]
It seems that the code in find_evsel_group() does not properly handle the scenario of event alias matching different PMUs (as I already said).
So I got it working on top of "perf metricgroup: Fix uncore metric expressions" with the following change:
tools/perf/util/metricgroup.c +2 -2
@@ -213,7 +213,8 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,/* Ignore event if already used and merging is disabled. */ if (metric_no_merge &&test_bit (ev->idx, evlist_used))continue ; -if (!has_constraint && ev->leader != current_leader) { +if (!has_constraint && (!current_leader ||strcmp (current_leader->name, ev->leader->name))) {/* * Start of a new group, discard the whole match and * start again.
@@ -279,7 +280,8 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,* when then group is left. */ if (!has_constraint && - ev->leader != metric_events[i]->leader) +strcmp (ev->leader->name, metric_events[i]->leader->name))break ;if (!strcmp (metric_events[i]->name, ev->name)) {set_bit (ev->idx, evlist_used);
which gives for my test metric:
./perf stat -v -M test_metric_inc sleep 1
Using CPUID GenuineIntel-6-3D-4
metric expr unc_cbo_xsnp_response.miss_xcore / unc_cbo_xsnp_response.miss_eviction for test_metric_inc found event unc_cbo_xsnp_response.miss_eviction
found event unc_cbo_xsnp_response.miss_xcore
adding {unc_cbo_xsnp_response.miss_eviction,unc_cbo_xsnp_response.miss_xcore}:W unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_1/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_0/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_1/umask=0x41,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_0/umask=0x41,event=0x22/
Control descriptor is not initialized
unc_cbo_xsnp_response.miss_eviction: 595175 1001021311 1001021311 unc_cbo_xsnp_response.miss_eviction: 592516 1001020037 1001020037 unc_cbo_xsnp_response.miss_xcore: 39139 1001021311 1001021311 unc_cbo_xsnp_response.miss_xcore: 38718 1001020037 1001020037
Performance counter stats for 'system wide':
1,187,691 unc_cbo_xsnp_response.miss_eviction # 0.07
test_metric_inc
77,857 unc_cbo_xsnp_response.miss_xcore
1.001068918 seconds time elapsedJohn
Ian Rogers
reviewerRe: Issue of metrics for multiple uncore PMUs (was Re: [RFC PATCH v2 23/23] perf metricgroup: remove duped metric group events)
Quoted 82 lines · John Garry
Quoted 23 lines · John Garry
Quoted 2 lines
Can you provide a reproduction? Looking on broadwell this metric doesn't exist.
Right, I just added this test metric as my 2x x86 platform has no examples which I can find:
tools/perf/pmu-events/arch/x86/broadwell/bdw-metrics.json +6
-0@@ -348,5 +348,11 @@ "MetricExpr": "(cstate_pkg@c7\\-residency@ / msr@tsc@) * 100", "MetricGroup": "Power", "MetricName": "C7_Pkg_Residency" + }, + { + "BriefDescription": "test metric", + "MetricExpr": "UNC_CBO_XSNP_RESPONSE.MISS_XCORE * UNC_CBO_XSNP_RESPONSE.MISS_EVICTION", + "MetricGroup": "Test", + "MetricName": "test_metric_inc" }Quoted 2 lines
]
It seems that the code in
find_evsel_group()does not properly handle the scenario of event alias matching different PMUs (as I already said).So I got it working on top of "perf metricgroup: Fix uncore metric expressions" with the following change:
tools/perf/util/metricgroup.c +2
-2@@ -213,7 +213,8 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist,/* Ignore event if already used and merging is disabled. */ if (metric_no_merge && test_bit(ev->idx, evlist_used)) continue; -if (!has_constraint && ev->leader != current_leader) { +if (!has_constraint && (!current_leader ||strcmp (current_leader->name, ev->leader->name))) {/* * Start of a new group, discard the whole match and * start again. @@ -279,7 +280,8 @@ static struct evsel *find_evsel_group(struct evlist*perf_evlist, * when then group is left. */ if (!has_constraint && - ev->leader != metric_events[i]->leader) +strcmp (ev->leader->name, metric_events[i]->leader->name))break ;if (!strcmp (metric_events[i]->name, ev->name)) {set_bit (ev->idx, evlist_used);which gives for my test metric:
./perf stat -v -M
test_metric_incsleep 1
Using CPUID GenuineIntel-6-3D-4
metric expr unc_cbo_xsnp_response.miss_xcore / unc_cbo_xsnp_response.miss_eviction fortest_metric_inc
found event unc_cbo_xsnp_response.miss_eviction
found event unc_cbo_xsnp_response.miss_xcore
adding {unc_cbo_xsnp_response.miss_eviction,unc_cbo_xsnp_response.miss_xcore}:W unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_1/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_eviction -> uncore_cbox_0/umask=0x81,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_1/umask=0x41,event=0x22/ unc_cbo_xsnp_response.miss_xcore -> uncore_cbox_0/umask=0x41,event=0x22/ Control descriptor is not initialized unc_cbo_xsnp_response.miss_eviction: 595175 1001021311 1001021311 unc_cbo_xsnp_response.miss_eviction: 592516 1001020037 1001020037 unc_cbo_xsnp_response.miss_xcore: 39139 1001021311 1001021311 unc_cbo_xsnp_response.miss_xcore: 38718 1001020037 1001020037Performance counter stats for 'system wide':
Quoted 8 lines
1,187,691 unc_cbo_xsnp_response.miss_eviction # 0.07 test_metric_inc 77,857 unc_cbo_xsnp_response.miss_xcore 1.001068918 seconds time elapsedJohn
Thanks John! I was able to repro the problem, let me investigate what is happening here as it seems there may be something wrong with the grouping logic.
Ian