perf tools: Adding formula support
v2 cover perf tools: Adding formula support
hi,
adding support to define counters for stat command, by using user defined events and counters.
Initial RFC is here:
http://marc.info/?l=linux-kernel&m=135825930106535&w=2
v2 main changes:
- counter formula grammar changed
(details in patch 4 changelog)
- using -e 'formula-<name>' way of using configured <name> set for stat command
(details in patch 7 changelog)
thanks for comments,
jirka
---
Jiri Olsa (8):
perf tools: Move start conditions to start of the flex file perf tools: Factorize event parsing to be more general perf tools: Add formula-* parsing support for events perf tools: Add formula interface to interface formula definitions perf tools: Add support to preload default formulas perf tests: Add automated tests for formula object perf stat: Add support to process formulas
perf list: List formulas counters
tools/perf/Makefile | 20 ++++ tools/perf/builtin-list.c | 3 + tools/perf/builtin-stat.c | 83 +++++++++++++- tools/perf/formulas/default.conf | 26 +++++ tools/perf/tests/builtin-test.c | 4 + tools/perf/tests/formula.c | 183 ++++++++++++++++++++++++++++++ tools/perf/tests/parse-events.c | 8 -- tools/perf/tests/tests.h | 10 ++ tools/perf/util/evlist.c | 13 +++ tools/perf/util/evlist.h | 9 ++ tools/perf/util/formula.c | 600 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/formula.h | 142 +++++++++++++++++++++++ tools/perf/util/formula.l | 119 ++++++++++++++++++++ tools/perf/util/formula.y | 249 +++++++++++++++++++++++++++++++++++++++++ tools/perf/util/parse-events.c | 56 ++++++++++ tools/perf/util/parse-events.h | 20 ++++ tools/perf/util/parse-events.l | 73 ++++++------ tools/perf/util/parse-events.y | 80 +++++++++++-- 18 files changed, 1646 insertions(+), 52 deletions(-) create mode 100644 tools/perf/formulas/default.conf create mode 100644 tools/perf/tests/formula.c create mode 100644 tools/perf/util/formula.c create mode 100644 tools/perf/util/formula.h create mode 100644 tools/perf/util/formula.l create mode 100644 tools/perf/util/formula.y
PATCH v2 1/8 perf tools: Move start conditions to start of the flex file
Moving start conditions to start of the flex file so it's clear what the INITIAL condition rules are.
Plus adding default rule for INITIAL condition. This prevents default space to be printed for events like:
$ ./perf stat -e "cycles " kill 2>/dev/null
$
^^^^^^^^--- tools/perf/util/parse-events.l | 63 +++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 31 deletions(-)
tools/perf/util/parse-events.l +32 -31
@@ -125,6 +125,37 @@ modifier_bp [rwx]{1,3} } +<config>{ +config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); } +config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); } +config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); } +name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); } +period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); } +branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); } +, { return ','; } +"/" { BEGIN(INITIAL); return '/'; } +{name_minus} { return str(yyscanner, PE_NAME); } +} + +<mem>{ +{modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); } +: { return ':'; } +{num_dec} { return value(yyscanner, 10); } +{num_hex} { return value(yyscanner, 16); } + /* + * We need to separate 'mem:' scanner part, in order to get specific + * modifier bits parsed out. Otherwise we would need to handle PE_NAME + * and we'd need to parse it manually. During the escape from <mem> + * state we need to put the escaping char back, so we dont miss it. + */ +. { unput(*yytext); BEGIN(INITIAL); } + /* + * We destroy the scanner after reaching EOF, + * but anyway just to be sure get back to INIT state. + */ +<<EOF>> { BEGIN(INITIAL); } +} + cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); } stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); } stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
@@ -160,18 +191,6 @@ speculative-read|speculative-load | refs|Reference|ops|access | misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); } -<config>{ -config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); } -config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); } -config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); } -name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); } -period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); } -branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); } -, { return ','; } -"/" { BEGIN(INITIAL); return '/'; } -{name_minus} { return str(yyscanner, PE_NAME); } -} - mem: { BEGIN(mem); return PE_PREFIX_MEM; } r{num_raw_hex} { return raw(yyscanner); } {num_dec} { return value(yyscanner, 10); }
@@ -187,25 +206,7 @@ r{num_raw_hex} { return raw(yyscanner); } "}" { return '}'; } = { return '='; } \n { } - -<mem>{ -{modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); } -: { return ':'; } -{num_dec} { return value(yyscanner, 10); } -{num_hex} { return value(yyscanner, 16); } - /* - * We need to separate 'mem:' scanner part, in order to get specific - * modifier bits parsed out. Otherwise we would need to handle PE_NAME - * and we'd need to parse it manually. During the escape from <mem> - * state we need to put the escaping char back, so we dont miss it. - */ -. { unput(*yytext); BEGIN(INITIAL); } - /* - * We destroy the scanner after reaching EOF, - * but anyway just to be sure get back to INIT state. - */ -<<EOF>> { BEGIN(INITIAL); } -} +. { } %%
Signature · 1 line
1.7.11.7
PATCH v2 2/8 perf tools: Factorize event parsing to be more general
In reply to Jiri OlsaMaking the event parsing processing more general so we can plug in strings with special processing like formula definition.
The formula changes are coming in following patches.
--- tools/perf/util/parse-events.c | 21 ++++++++++++++++++ tools/perf/util/parse-events.h | 17 +++++++++++++++ tools/perf/util/parse-events.y | 48 +++++++++++++++++++++++++++++++----------- 3 files changed, 74 insertions(+), 12 deletions(-)
tools/perf/util/parse-events.c +21 -0
@@ -1238,3 +1238,24 @@ void parse_events__free_terms(struct list_head *terms)free (terms); } + +int parse_events_config_process (struct parse_events_evlist *data, +struct list_head *head) +{ +struct parse_events_config *cfg, *h; + +list_for_each_entry_safe (cfg, h, head, list) { +switch (cfg->type) { +case PARSE_EVENTS_CONFIG_EVENTS : +parse_events_update_lists (cfg->events, &data->list); +break ; +default : +break ; + } + +list_del (&cfg->list); +free (cfg); + } + +return 0 ; +}
tools/perf/util/parse-events.h +17 -0
@@ -71,6 +71,23 @@ struct parse_events_terms {struct list_head *terms; }; +enum parse_events_config_type { +PARSE_EVENTS_CONFIG_EVENTS , +}; + +struct parse_events_config { +enum parse_events_config_type type; + +union { +struct list_head *events; +void *val; + }; + +struct list_head list; +}; + +int parse_events_config_process (struct parse_events_evlist *data, +struct list_head *head);int parse_events__is_hardcoded_term (struct parse_events_term *term);int parse_events_term__num (struct parse_events_term **_term,int type_term,char *config,u64 num);
tools/perf/util/parse-events.y +36 -12
85 lines not on this page · raw message
Signature · 1 line
1.7.11.7
PATCH v2 3/8 perf tools: Add formula-* parsing support for events
In reply to Jiri OlsaAdding a possibility to specify formula-$NAME within the -e option same way as an event.
This adds only the functionality to parse out the formula names and populate string array within struct perf_evlist.
The formula processing itself will come in next patches.
--- tools/perf/util/evlist.h | 5 +++++ tools/perf/util/parse-events.c | 32 ++++++++++++++++++++++++++++++++ tools/perf/util/parse-events.h | 3 +++ tools/perf/util/parse-events.l | 10 +++++++++- tools/perf/util/parse-events.y | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-)
tools/perf/util/evlist.h +5 -0
@@ -42,6 +42,7 @@ struct perf_evlist {struct thread_map *threads;struct cpu_map *cpus;struct perf_evsel *selected; +char **formulas; };struct perf_evsel_str_handler {
@@ -164,4 +165,8 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md, pc->data_tail = tail; } +static inline bool perf_evlist__has_formulas (struct perf_evlist *evlist) +{ +return evlist->formulas !=NULL ; +}#endif /* __PERF_EVLIST_H */
tools/perf/util/parse-events.c +32 -0
@@ -877,6 +877,7 @@ int parse_events(struct perf_evlist *evlist, const char *str)int entries = data.idx - evlist->nr_entries;perf_evlist__splice_list_tail (evlist, &data.list, entries); evlist->nr_groups += data.nr_groups; + evlist->formulas = data.formulas;return 0 ; }
@@ -1239,6 +1240,31 @@ void parse_events__free_terms(struct list_head *terms)free (terms); } +static char **formula_add (char **f,char *new) +{ +int i; +#define FORMULAS_CNT 20 + +if (!f) { + f =zalloc (sizeof (char *) *FORMULAS_CNT +1 ); +if (!f) +return NULL ; + } + +for (i =0 ; f[i] && (i <FORMULAS_CNT ); i++); + +if (i ==FORMULAS_CNT ) { +pr_err ("Too many formula defined, max = %d\n" , +FORMULAS_CNT ); +return NULL ; + } + +pr_debug ("parse events: formula %s\n" , new); + + f[i] = new; +return f; +} +int parse_events_config_process (struct parse_events_evlist *data,struct list_head *head) {
@@ -1249,6 +1275,12 @@ int parse_events_config_process(struct parse_events_evlist *data,case PARSE_EVENTS_CONFIG_EVENTS :parse_events_update_lists (cfg->events, &data->list);break ; +case PARSE_EVENTS_CONFIG_FORMULA : + data->formulas =formula_add (data->formulas, + cfg->formula); +if (!data->formulas) +return -1 ; +break ;default :break ; }
tools/perf/util/parse-events.h +3 -0
@@ -63,6 +63,7 @@ struct parse_events_term {struct parse_events_evlist {struct list_head list; +char **formulas;int idx;int nr_groups; };
@@ -73,6 +74,7 @@ struct parse_events_terms {enum parse_events_config_type {PARSE_EVENTS_CONFIG_EVENTS , +PARSE_EVENTS_CONFIG_FORMULA , };struct parse_events_config {
@@ -80,6 +82,7 @@ struct parse_events_config {union {struct list_head *events; +char *formula;void *val; };
tools/perf/util/parse-events.l +9 -1
@@ -72,10 +72,12 @@ static int term(yyscan_t scanner, int type) %x mem %s config %x event +%x formula group [^,{}/]*[{][^}]*[}][^,{}/]* event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* event [^,{}/]+ +formula formula-[^,{}/]+ num_dec [0-9]+ num_hex 0x[a-fA-F0-9]+
@@ -106,7 +108,7 @@ modifier_bp [rwx]{1,3} %} <event>{ - +{formula} | {group} { BEGIN(INITIAL); yyless(0); }
@@ -156,6 +158,11 @@ branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE <<EOF>> { BEGIN(INITIAL); } } +<formula>{ +- { return '-'; } +{name_minus} { BEGIN(INITIAL); return str(yyscanner, PE_FORMULA_NAME); } +} + cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); } stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); } stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
@@ -191,6 +198,7 @@ speculative-read|speculative-load | refs|Reference|ops|access | misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); } +formula { BEGIN(formula); return PE_FORMULA; } mem: { BEGIN(mem); return PE_PREFIX_MEM; } r{num_raw_hex} { return raw(yyscanner); } {num_dec} { return value(yyscanner, 10); }
tools/perf/util/parse-events.y +34 -0
@@ -55,6 +55,7 @@ static inc_group_count(struct list_head *list, %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP %token PE_ERROR +%token PE_FORMULA PE_FORMULA_NAME %type <num> PE_VALUE %type <num> PE_VALUE_SYM_HW %type <num> PE_VALUE_SYM_SW
@@ -66,6 +67,7 @@ static inc_group_count(struct list_head *list, %type <str> PE_MODIFIER_EVENT %type <str> PE_MODIFIER_BP %type <str> PE_EVENT_NAME +%type <str> PE_FORMULA_NAME %type <num> value_sym %type <head> event_config %type <term> event_term
@@ -85,6 +87,8 @@ static inc_group_count(struct list_head *list, %type <head> group %type <head> groups %type <cfg> groups_config +%type <cfg> groups_formula +%type <str> formula %union {
@@ -118,6 +122,15 @@ groups ',' groups_config $$ = head; } | +groups ',' groups_formula +{ + struct list_head *head = $1; + struct parse_events_config *cfg = $3; + + list_add_tail(&cfg->list, head); + $$ = head; +} +| groups_config { struct list_head *head = HEAD();
@@ -126,6 +139,15 @@ groups_config list_add_tail(&cfg->list, head); $$ = head; } +| +groups_formula +{ + struct list_head *head = HEAD(); + struct parse_events_config *cfg = $1; + + list_add_tail(&cfg->list, head); + $$ = head; +} groups_config: group
@@ -436,6 +458,18 @@ PE_TERM $$ = term; } +groups_formula: +formula +{ + $$ = CONFIG(FORMULA, $1); +} + +formula: +PE_FORMULA '-' PE_FORMULA_NAME +{ + $$ = strdup($3); +} + sep_dc: ':' | sep_slash_dc: '/' | ':' |
Signature · 1 line
1.7.11.7
Re: [PATCH 3/8] perf tools: Add formula-* parsing support for events
Quoted 8 lines · Jiri Olsa
tools/perf/util/parse-events.c +8
-0+static char **formula_add (char **f,char *new) +{ +int i; +#define FORMULAS_CNT 20 + +if (!f) { + f =zalloc (sizeof (char *) *FORMULAS_CNT +1 ); +if (!f)
tools/perf/util/parse-events.c void parse_events__free_terms(struct list_head *terms) · line 1260
Quoted 10 lines
tools/perf/util/parse-events.c +10
-0+return NULL ; + } + +for (i =0 ; f[i] && (i <FORMULAS_CNT ); i++); + +if (i ==FORMULAS_CNT ) { +pr_err ("Too many formula defined, max = %d\n" , +FORMULAS_CNT ); +return NULL ; + }
Why have an array of arbitrary limit versus a linked list?
David
Re: [PATCH 3/8] perf tools: Add formula-* parsing support for events
Quoted 13 lines · David Ahern
Quoted 12 lines · Jiri Olsa
tools/perf/util/parse-events.c +12
-0+static char **formula_add (char **f,char *new) +{ +int i; +#define FORMULAS_CNT 20 + +if (!f) { + f =zalloc (sizeof (char *) *FORMULAS_CNT +1 ); +if (!f) +return NULL ; + } + +for (i =0 ; f[i] && (i <FORMULAS_CNT ); i++);
Quoted 8 lines
tools/perf/util/parse-events.c void parse_events__free_terms(struct list_head *terms) · line 1260
Quoted 6 lines
tools/perf/util/parse-events.c +6
-0+ +if (i ==FORMULAS_CNT ) { +pr_err ("Too many formula defined, max = %d\n" , +FORMULAS_CNT ); +return NULL ; + }Why have an array of arbitrary limit versus a linked list?
well, I dont need to define new struct, just an array of string pointers..
jirka
PATCH v2 4/8 perf tools: Add formula interface to interface formula definitions
In reply to Jiri OlsaAdding formula object to interface formula definitions like:
set {
events {
CY = cycles
IN = instructions
BR = branches
} cpi = CY / IN
bdensity = IN / BR
print cpi
print bdensity
}
The 'set' defines set of counter that share same events. Each 'set' defines:
events - event string that would go into stat -e option
counters - any number of counters based on above events
Each event and counter is defined as an assignment (via =) to the name(tag), which could be later used within counter formulas.
Each counter (cpi/branch-rate above) defines formula that produces the counter number.
Formula grammar:
expr: '-' expr |
expr '+' expr |
expr '-' expr |
expr '*' expr |
expr '/' expr |
value |name
where name could be any event or counter name(tag), like:
(considering the architectural events name patchset
is included)
set {
events {
IN = instructions
R0 = cpu/OFFCORE_RESPONSE_0,offcore_rsp=OFFCORE_RESPONSE.(DMND_IFETCH|LLC_MISS_LOCAL)/
} kinst = IN / 1000
llc_imiss = R0 / kinst
print llc_imiss
}
It's possible to enable counter to be displayed via 'print' label on separate line. Only counters marked like that will be displayed to the user.
Interface:
perf_formula__init
- initialize perf_formula handler
perf_formula__load
- load file into the handler
perf_formula__free
- cleanup
perf_formula__set
- get 'set' handler
perf_formula__evlist
- update perf_evlist with needed events
perf_formula__print
- display output ratios
--- tools/perf/Makefile | 11 + tools/perf/util/evlist.c | 13 ++ tools/perf/util/evlist.h | 4 + tools/perf/util/formula.c | 513 ++++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/formula.h | 137 +++++++++++++ tools/perf/util/formula.l | 119 +++++++++++ tools/perf/util/formula.y | 249 ++++++++++++++++++++++ 7 files changed, 1046 insertions(+) create mode 100644 tools/perf/util/formula.c create mode 100644 tools/perf/util/formula.h create mode 100644 tools/perf/util/formula.l create mode 100644 tools/perf/util/formula.y
tools/perf/Makefile +11 -0
@@ -326,8 +326,15 @@ $(OUTPUT)util/pmu-flex.c: util/pmu.l $(OUTPUT)util/pmu-bison.c $(OUTPUT)util/pmu-bison.c: util/pmu.y $(QUIET_BISON)$(BISON) -v util/pmu.y -d -o $(OUTPUT)util/pmu-bison.c -p perf_pmu_ +$(OUTPUT)util/formula-flex.c: util/formula.l util/formula.y $(OUTPUT)util/formula-bison.c + $(QUIET_FLEX)$(FLEX) --header-file=$(OUTPUT)util/formula-flex.h $(PARSER_DEBUG_FLEX) -t util/formula.l > $(OUTPUT)util/formula-flex.c + +$(OUTPUT)util/formula-bison.c: util/formula.y + $(QUIET_BISON)$(BISON) -v util/formula.y -d $(PARSER_DEBUG_BISON) -o $(OUTPUT)util/formula-bison.c + $(OUTPUT)util/parse-events.o: $(OUTPUT)util/parse-events-flex.c $(OUTPUT)util/parse-events-bison.c $(OUTPUT)util/pmu.o: $(OUTPUT)util/pmu-flex.c $(OUTPUT)util/pmu-bison.c +$(OUTPUT)util/formula.o: $(OUTPUT)util/formula-flex.c $(OUTPUT)util/formula-bison.c LIB_FILE=$(OUTPUT)libperf.a
@@ -420,6 +427,7 @@ LIB_H += util/intlist.h LIB_H += util/perf_regs.h LIB_H += util/unwind.h LIB_H += util/vdso.h +LIB_H += util/formula.h LIB_H += ui/helpline.h LIB_H += ui/progress.h LIB_H += ui/util.h
@@ -492,6 +500,9 @@ LIB_OBJS += $(OUTPUT)util/rblist.o LIB_OBJS += $(OUTPUT)util/intlist.o LIB_OBJS += $(OUTPUT)util/vdso.o LIB_OBJS += $(OUTPUT)util/stat.o +LIB_OBJS += $(OUTPUT)util/formula.o +LIB_OBJS += $(OUTPUT)util/formula-flex.o +LIB_OBJS += $(OUTPUT)util/formula-bison.o LIB_OBJS += $(OUTPUT)ui/setup.o LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/evlist.c +13 -0
@@ -863,3 +863,16 @@ size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)return printed +fprintf (fp,"\n" );; } + +struct perf_evsel * +perf_evlist__find_evsel_name (struct perf_evlist *evlist,char *name) +{ +struct perf_evsel *evsel; + +list_for_each_entry (evsel, &evlist->entries, node) { +if (strstr (perf_evsel__name (evsel), name)) +return evsel; + } + +return NULL ; +}
tools/perf/util/evlist.h +4 -0
@@ -169,4 +169,8 @@ static inline bool perf_evlist__has_formulas(struct perf_evlist *evlist) {return evlist->formulas !=NULL ; } + +struct perf_evsel * +perf_evlist__find_evsel_name (struct perf_evlist *evlist,char *name); +#endif /* __PERF_EVLIST_H */
tools/perf/util/formula.c new file +513 -0
513 lines not on this page · raw message
tools/perf/util/formula.h new file +137 -0
137 lines not on this page · raw message
tools/perf/util/formula.l new file +119 -0
119 lines not on this page · raw message
tools/perf/util/formula.y new file +249 -0
249 lines not on this page · raw message
Signature · 1 line
1.7.11.7
Peter Zijlstra
maintainerRe: [PATCH 4/8] perf tools: Add formula interface to interface formula definitions
Quoted 5 lines · Jiri Olsa
Each counter (cpi/branch-rate above) defines formula that produces the counter number.
Formula grammar:
Quoted 8 lines
expr: '-' expr | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr | value |name
I was going to suggest adding (), but then I saw
Quoted 7 lines
+<expr>{
+"*" { return '*'; }
+"-" { return '-'; }
+"+" { return '+'; }
+"/" { return '/'; }
+"(" { return '('; }
+")" { return ')'; }
tools/perf/util/formula.y line 241
Quoted 7 lines
tools/perf/util/formula.y new file +6
-0+ +'(' expr ')' +{ + $$ = $2; + pr_debug2("( %F )\n", $$); +}
:-)
PATCH v2 5/8 perf tools: Add support to preload default formulas
In reply to Jiri OlsaAdding support to read all formula definition files under common locations:
'formula'
- under perf developement tree
'PREFIX/PERF_EXEC_PATH/formulas'
- when installed
Plus adding very basic formula definitions for CPI and branch related counters.
--- tools/perf/Makefile | 8 ++++++ tools/perf/formulas/default.conf | 26 ++++++++++++++++++++ tools/perf/util/formula.c | 53 ++++++++++++++++++++++++++++++++++++++++ tools/perf/util/formula.h | 3 +++ 4 files changed, 90 insertions(+) create mode 100644 tools/perf/formulas/default.conf
tools/perf/Makefile +8 -0
@@ -1010,6 +1010,12 @@ $(OUTPUT)util/exec_cmd.o: util/exec_cmd.c $(OUTPUT)PERF-CFLAGS '-DPREFIX="$(prefix_SQ)"' \ $< +$(OUTPUT)util/formula.o: util/formula.c $(OUTPUT)PERF-CFLAGS + $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) \ + '-DPERF_EXEC_PATH="$(perfexecdir_SQ)"' \ + '-DPREFIX="$(prefix_SQ)"' \ + $< + $(OUTPUT)tests/attr.o: tests/attr.c $(OUTPUT)PERF-CFLAGS $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) \ '-DBINDIR="$(bindir_SQ)"' -DPYTHON='"$(PYTHON_WORD)"' \
@@ -1201,6 +1207,8 @@ install-bin: all $(INSTALL) tests/attr.py '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests' $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr' $(INSTALL) tests/attr/* '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr' + $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/formulas' + $(INSTALL) -m 644 formulas/* $(DESTDIR_SQ)$(perfexec_instdir_SQ)/formulas install: install-bin try-install-man
tools/perf/formulas/default.conf new file +26 -0
@@ -0,0 +1,26 @@ +cpi { + events { + CY = cycles:u + IN = instructions:u + } + + cpi = CY / IN + + print cpi +} + +branch { + events { + IN = instructions:u + BI = branch-instructions:u + BM = branch-misses:u + } + + branch-rate = BI / IN + branch-miss-rate = BM / IN + branch-miss-ratio = BM / BI + + print branch-rate + print branch-miss-rate + print branch-miss-ratio +}
tools/perf/util/formula.c +53 -0
62 lines not on this page · raw message
tools/perf/util/formula.h +3 -0
@@ -106,6 +106,7 @@ struct perf_formula_config {void perf_formula__init (struct perf_formula *f);int perf_formula__load (struct perf_formula *f,char *path); +int perf_formula__load_dir (struct perf_formula *f,char *path);int perf_formula__free (struct perf_formula *f); struct perf_formula_set*
@@ -134,4 +135,6 @@ perf_formula_set__new(char *name, struct list_head *head);double perf_formula_expr__resolve (struct perf_formula_expr *expr,char *name); +int perf_formula__preload (struct perf_formula *f); +int perf_formula__preload_arch (struct perf_formula *f);#endif /* __PERF_FORMULA */
Signature · 1 line
1.7.11.7
Re: [PATCH 5/8] perf tools: Add support to preload default formulas
Quoted 5 lines · Jiri Olsa
tools/perf/util/formula.c +5
-0+int perf_formula__load_dir (struct perf_formula *f,char *path) +{ +struct dirent *ent; +DIR *dir; +int ret =0 ;
tools/perf/util/formula.c int perf_formula__load(struct perf_formula *f, char *path) · line 176
Quoted 10 lines
tools/perf/util/formula.c +10
-0+ + dir =opendir (path); +if (!dir) { +pr_err ("formula: can't open dir '%s' - %s\n" , + path,strerror (errno)); +return -1 ; + } + +while (!ret && (ent =readdir (dir))) { +char file[PATH_MAX ];
readdir_r? this is going into libperf, so might as well be thread safe.
David
Re: [PATCH 5/8] perf tools: Add support to preload default formulas
Quoted 10 lines · David Ahern
Quoted 9 lines · Jiri Olsa
tools/perf/util/formula.c +9
-0+int perf_formula__load_dir (struct perf_formula *f,char *path) +{ +struct dirent *ent; +DIR *dir; +int ret =0 ; + + dir =opendir (path); +if (!dir) { + pr_err("formula: can't open dir '%s' - %s\n",
Quoted 8 lines
tools/perf/util/formula.c int perf_formula__load(struct perf_formula *f, char *path) · line 176
Quoted 6 lines
tools/perf/util/formula.c +6
-0+ path, strerror(errno)); +return -1 ; + } + +while (!ret && (ent =readdir (dir))) { +char file[PATH_MAX ];
readdir_r? this is going into libperf, so might as well be thread safe.
right, will do
thanks,
jirka
PATCH v2 6/8 perf tests: Add automated tests for formula object
In reply to Jiri OlsaAdding automated tests to test basic formula object definitions and interface.
--- tools/perf/Makefile | 1 + tools/perf/tests/builtin-test.c | 4 + tools/perf/tests/formula.c | 183 ++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/parse-events.c | 8 -- tools/perf/tests/tests.h | 10 +++ 5 files changed, 198 insertions(+), 8 deletions(-) create mode 100644 tools/perf/tests/formula.c
tools/perf/Makefile +1 -0
@@ -532,6 +532,7 @@ LIB_OBJS += $(OUTPUT)tests/bp_signal.o LIB_OBJS += $(OUTPUT)tests/bp_signal_overflow.o LIB_OBJS += $(OUTPUT)tests/task-exit.o LIB_OBJS += $(OUTPUT)tests/sw-clock.o +LIB_OBJS += $(OUTPUT)tests/formula.o BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o BUILTIN_OBJS += $(OUTPUT)builtin-bench.o
tools/perf/tests/builtin-test.c +4 -0
@@ -94,6 +94,10 @@ static struct test { .func = test__sw_clock_freq, }, { + .desc = "Test formula interface", + .func = test__formula, + }, + { .func = NULL, }, };
tools/perf/tests/formula.c new file +183 -0
183 lines not on this page · raw message
tools/perf/tests/parse-events.c +0 -8
@@ -7,14 +7,6 @@#include "tests.h" #include <linux/hw_breakpoint.h> -#define TEST_ASSERT_VAL (text, cond) \ -do { \ - if (!(cond)) { \ - pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \ - return -1; \ - } \ -} while (0) -#define PERF_TP_SAMPLE_TYPE (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | \ PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD)
tools/perf/tests/tests.h +10 -0
@@ -7,6 +7,15 @@ enum { TEST_SKIP = -2, }; +#define TEST_ASSERT_VAL (text, cond) \ +do { \ + if (!(cond)) { \ + pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \ + return -1; \ + } \ +} while (0) + +/* Tests */ int test__vmlinux_matches_kallsyms (void );int test__open_syscall_event (void );
@@ -27,5 +36,6 @@ int test__bp_signal(void);int test__bp_signal_overflow (void );int test__task_exit (void );int test__sw_clock_freq (void ); +int test__formula (void );#endif /* TESTS_H */
Signature · 1 line
1.7.11.7
Re: [PATCH 6/8] perf tests: Add automated tests for formula object
Quoted 7 lines · Jiri Olsa
tools/perf/tests/formula.c new file +7
-0+static int __test_basics (char *file) +{ +struct perf_formula fml; +struct perf_formula_set *set; +struct perf_formula_counter *counter; +struct perf_evlist *evlist; +struct perf_evsel *evsel;
tools/perf/tests/formula.c line 55
Quoted 10 lines
tools/perf/tests/formula.c new file +10
-0+struct perf_counts *counts; +struct perf_formula_expr expr; +int ret; + +pr_debug ("file %s\n" , file); + +perf_formula__init (&fml); + + ret =perf_formula__load (&fml, file); +TEST_ASSERT_VAL ("failed to load formula" , !ret);
With all the asserts when something goes wrong you should delete the file right after perf_formula__load. so maybe
file = file_get(data, sizeof(data));
ret = perf_formula__load(&fml, file);file_put(file);
David
Re: [PATCH 6/8] perf tests: Add automated tests for formula object
Quoted 22 lines · David Ahern
Quoted 7 lines · Jiri Olsa
tools/perf/tests/formula.c new file +7
-0+static int __test_basics (char *file) +{ +struct perf_formula fml; +struct perf_formula_set *set; +struct perf_formula_counter *counter; +struct perf_evlist *evlist; +struct perf_evsel *evsel;tools/perf/tests/formula.c line 55
Quoted 10 lines
tools/perf/tests/formula.c new file +10
-0+struct perf_counts *counts; +struct perf_formula_expr expr; +int ret; + +pr_debug ("file %s\n" , file); + +perf_formula__init (&fml); + + ret =perf_formula__load (&fml, file); +TEST_ASSERT_VAL ("failed to load formula" , !ret);With all the asserts when something goes wrong you should delete the file right after
perf_formula__load. so maybe
Quoted 3 lines
file =
file_get(data, sizeof(data));
ret =perf_formula__load(&fml, file);file_put(file);
hum, the TEST_ASSERT_VAL call 'return' from __test_basics on error and that goes back to test_basics where file_put is called..
thanks,
jirka
Re: [PATCH 6/8] perf tests: Add automated tests for formula object
Quoted 2 lines · Jiri Olsa
hum, the
TEST_ASSERT_VALcall 'return' from__test_basicson error and that goes back totest_basicswherefile_putis called..
ok. I filled in the meaning of TEST_ASSERT_VAL -- thinking it killed the program at that point as in assert().
David
PATCH v2 7/8 perf stat: Add support to process formulas
In reply to Jiri OlsaAdding -f option to specify the formula configuration file to load. The formula name itself is specified within the '-e' option.
With following formula.conf config file:
---
cpi {
events {
CY = cycles:u
IN = instructions:u
}
cpi = CY / IN
print cpi
}
branch {
events {
IN = instructions:u
BI = branch-instructions:u BM = branch-misses:u
}
branch-rate = BI / IN
branch-miss-rate = BM / IN branch-miss-ratio = BM / BI
print branch-rate
print branch-miss-rate
print branch-miss-ratio
}
---
You'll get following result:
$ ./perf stat -f formula.conf -e formula-branch kill
usage: kill [ -s signal | -p ] [ -a ] pid ...
kill -l [ signal ]Performance counter stats for 'kill':
187,342 instructions:u # 0.00 insns per cycle
41,996 branch-instructions:u
4,826 branch-misses:u # 11.49% of all branches 0.054313993 seconds time elapsed
0.22416757 branch-rate
0.02576037 branch-miss-rate
0.11491571 branch-miss-ratio--- tools/perf/builtin-stat.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-)
tools/perf/builtin-stat.c +82 -1
@@ -56,6 +56,7 @@#include "util/cpumap.h" #include "util/thread.h" #include "util/thread_map.h" +#include "util/formula.h" #include <stdlib.h> #include <sys/prctl.h>
@@ -72,6 +73,10 @@ static void print_aggr(char *prefix);static struct perf_evlist *evsel_list; +static struct perf_formula formula; +static struct perf_formula_set *formula_set; +static bool formula_no_preload; +static struct perf_target target = { .uid =UINT_MAX , };
@@ -380,6 +385,12 @@ static void print_interval(void)diff_timespec (&rs, &ts, &ref_time);sprintf (prefix,"%6lu.%09lu%s" , rs.tv_sec, rs.tv_nsec, csv_sep); +if (formula_set) { +perf_formula__print (output, &formula, formula_set, + evsel_list,NULL ); +return ; + } +if (num_print_interval ==0 && !csv_output) {switch (aggr_mode) {case AGGR_SOCKET :
@@ -1340,6 +1351,51 @@ static int add_default_attributes(void)return perf_evlist__add_default_attrs (evsel_list, very_very_detailed_attrs); } +static int formula_option (const struct option *opt__maybe_unused , +const char *str,int unset__maybe_unused ) +{ +if (perf_formula__load (&formula, (char *) str)) { +pr_err ("formula: failed to load formula file '%s'\n" , str); +return -1 ; + } + + formula_no_preload = true; + +pr_debug ("formula file '%s'\n" , str); +return 0 ; +} + +static int formula_resolve (struct perf_evlist *evlist) +{ +char **f = evlist->formulas; +char *name = *f; + +if (!formula_no_preload &&perf_formula__preload (&formula)) { +pr_err ("formula: failed to preload default formulas\n" ); +return -1 ; + } + +if (f[1 ]) { +pr_err ("formula: only single formula supported for stat\n" ); +return -1 ; + } + + formula_set =perf_formula__set (&formula, name); +if (!formula_set) { +pr_err ("formula: failed to find formula '%s'\n" , name); +return -1 ; + } + +if (perf_formula__evlist (&formula, formula_set, evlist)) { +pr_err ("formula: failed to load formula events for %s\n" , + name); +return -1 ; + } + +pr_debug ("loaded formula '%s'\n" , name); +return 0 ; +} +int cmd_stat (int argc,const char **argv,const char *prefix__maybe_unused ) {bool append_file = false;
@@ -1351,6 +1407,9 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) parse_events_option),OPT_CALLBACK (0 ,"filter" , &evsel_list,"filter" ,"event filter" , parse_filter), +OPT_CALLBACK ('f' ,"formula" ,NULL ,"formula" , +"counters formula file" , + formula_option),OPT_BOOLEAN ('i' ,"no-inherit" , &no_inherit,"child tasks do not inherit counters" ), OPT_STRING('p', "pid", &target.pid, "pid",
@@ -1412,9 +1471,22 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)if (evsel_list ==NULL )return -ENOMEM ; +perf_formula__init (&formula); + argc =parse_options (argc, argv, options, stat_usage,PARSE_OPT_STOP_AT_NON_OPTION ); +if (perf_evlist__has_formulas (evsel_list)) { +if (aggr_mode ==AGGR_NONE ) { +pr_err ("formula: Cannot run formulas with " +"no aggr mode\n" ); +return -1 ; + } + +if (formula_resolve (evsel_list)) +usage_with_options (stat_usage, options); + } + output = stderr;if (output_name &&strcmp (output_name,"-" )) output =NULL ;
@@ -1540,13 +1612,22 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) } } -if (!forever && status !=-1 && !interval) +if (!forever && status !=-1 && !interval) {print_stat (argc, argv); +if (formula_set) +perf_formula__print (output, &formula, formula_set, + evsel_list,NULL ); + } +perf_evlist__free_stats (evsel_list); out_free_maps:perf_evlist__delete_maps (evsel_list); out:perf_evlist__delete (evsel_list); + +if (formula_set) +perf_formula__free (&formula); +return status; }
Signature · 1 line
1.7.11.7
PATCH v2 8/8 perf list: List formulas counters
In reply to Jiri OlsaMake default formulas appear in 'perf list' command output with [Formula counter].
--- tools/perf/builtin-list.c | 3 +++ tools/perf/util/formula.c | 34 ++++++++++++++++++++++++++++++++++ tools/perf/util/formula.h | 2 ++ tools/perf/util/parse-events.c | 3 +++ 4 files changed, 42 insertions(+)
tools/perf/builtin-list.c +3 -0
@@ -13,6 +13,7 @@#include "util/parse-events.h" #include "util/cache.h" +#include "util/formula.h" int cmd_list (int argc,const char **argv,const char *prefix__maybe_unused ) {
@@ -37,6 +38,8 @@ int cmd_list(int argc, const char **argv, const char *prefix __maybe_unused)else if (strcmp (argv[i],"cache" ) ==0 ||strcmp (argv[i],"hwcache" ) ==0 )print_hwcache_events (NULL , false); +else if (strcmp (argv[i],"formulas" ) ==0 ) +print_formulas ();else if (strcmp (argv[i],"--raw-dump" ) ==0 )print_events (NULL , true);else {
tools/perf/util/formula.c +34 -0
@@ -564,3 +564,37 @@ int perf_formula__preload(struct perf_formula *f)return ret ? ret :perf_formula__preload_arch (f); } + +static int print_set_cb (struct perf_formula_set *set,void *data) +{ +bool *printed = (bool *) data; +char name[100 ]; + +scnprintf (name,100 ,"formula-%s" , set->name); +printf (" %-50s [Formula counter]\n" , name); + +if (!*printed) + *printed = true; + +return CB_NEXT ; +} + +void print_formulas (void ) +{ +struct perf_formula f; +bool printed = false; + +perf_formula__init (&f); + +if (perf_formula__preload (&f)) { +pr_err ("formula: failed to preload formulas\n" ); +return ; + } + +printf ("\n" ); + +for_each_set (&f, print_set_cb, &printed); + +if (printed) +printf ("\n" ); +}
tools/perf/util/formula.h +2 -0
@@ -137,4 +137,6 @@ double perf_formula_expr__resolve(struct perf_formula_expr *expr,int perf_formula__preload (struct perf_formula *f);int perf_formula__preload_arch (struct perf_formula *f); + +void print_formulas (void );#endif /* __PERF_FORMULA */
tools/perf/util/parse-events.c +3 -0
@@ -15,6 +15,7 @@#define YY_EXTRA_TYPE int#include "parse-events-flex.h" #include "pmu.h" +#include "formula.h" #define MAX_NAME_LEN 100
@@ -1133,6 +1134,8 @@ void print_events(const char *event_glob, bool name_only)print_hwcache_events (event_glob, name_only); +print_formulas (); +if (event_glob !=NULL )return ;
Signature · 1 line
1.7.11.7
Quoted 7 lines · Jiri Olsa
hi,
adding support to define counters for stat command, by using user defined events and counters.Initial RFC is here:
http://marc.info/?l=linux-kernel&m=135825930106535&w=2
Quoted 6 lines
v2 main changes:
- counter formula grammar changed
(details in patch 4 changelog)
- using -e 'formula-<name>' way of using configured <name> set for stat command
(details in patch 7 changelog)
forgot.. changes could be reached in here:git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/formula3
jirka