perf hwmon related improvements
Ian Rogers
reviewerv3 cover perf hwmon related improvements
Try to do less scanning of files/directories with or without hwmon PMUs. Don't merge all events with the same name, only merge those on the same PMU ignoring suffix. Tidy PMU name matching to distinguish no suffix or wildcard matching. Refactor uniquification so the evsels with the same name as other evsels in the evlist are uniquified.
v3: Address potential segv and styling nits from Kan Liang.
v2: Rename ignore suffix PMU name matching that is really a
wildcard/prefix match. Use a proper ignore suffix when not merging counters purely on name.
Ian Rogers (5):
perf evsel: Reduce scanning core PMUs in is_hybrid perf pmus: Restructure pmu_read_sysfs to scan fewer PMUs perf pmu: Rename name matching for no suffix or wildcard variants perf stat: Don't merge counters purely on name
perf stat: Changes to event name uniquification
tools/perf/pmu-events/empty-pmu-events.c | 8 +- tools/perf/pmu-events/jevents.py | 8 +- tools/perf/tests/pmu.c | 85 ++++---- tools/perf/util/evsel.c | 4 +- tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 256 ++++++++++++++++------- tools/perf/util/pmu.h | 7 +- tools/perf/util/pmus.c | 146 ++++++++----- tools/perf/util/stat-display.c | 111 +++++++--- tools/perf/util/stat.c | 13 +- 11 files changed, 424 insertions(+), 217 deletions(-)
Signature · 1 line
2.48.1.362.g079036d154-goog
Ian Rogers
reviewerPATCH v3 1/5 perf evsel: Reduce scanning core PMUs in is_hybrid
evsel__is_hybrid returns true if there are multiple core PMUs and the evsel is for a core PMU. Determining the number of core PMUs can require loading/scanning PMUs. There's no point doing the scanning if evsel for the is_hybrid test isn't core so reorder the tests to reduce PMU scanning.
--- tools/perf/util/evsel.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/evsel.c +2 -2
@@ -3866,10 +3866,10 @@ void evsel__zero_per_pkg(struct evsel *evsel)*/ bool evsel__is_hybrid (const struct evsel *evsel) { -if (perf_pmus__num_core_pmus () ==1 ) +if (!evsel->core.is_pmu_core)return false; -return evsel->core.is_pmu_core; +return perf_pmus__num_core_pmus () >1 ; }struct evsel *evsel__leader (const struct evsel *evsel)
Signature · 1 line
2.48.1.362.g079036d154-goog
Ian Rogers
reviewerPATCH v3 2/5 perf pmus: Restructure pmu_read_sysfs to scan fewer PMUs
In reply to Ian RogersRather than scanning core or all PMUs, allow pmu_read_sysfs to read some combination of core, other, hwmon and tool PMUs. The PMUs that should be read and are already read are held as bitmaps. It is known that a "hwmon_" prefix is necessary for a hwmon PMU's name, similarly with "tool", so only scan those PMUs in situations the PMU name or the PMU's type number make sense to.
The number of openat system calls reduces from 276 to 98 for a hwmon event. The number of openats for regular perf events isn't changed.
--- tools/perf/util/pmu.h | 2 + tools/perf/util/pmus.c | 146 +++++++++++++++++++++++++++-------------- 2 files changed, 97 insertions(+), 51 deletions(-)
tools/perf/util/pmu.h +2 -0
@@ -37,6 +37,8 @@ struct perf_pmu_caps { };enum { +PERF_PMU_TYPE_PE_START =0 , +PERF_PMU_TYPE_PE_END =0xFFFEFFFF ,PERF_PMU_TYPE_HWMON_START =0xFFFF0000 ,PERF_PMU_TYPE_HWMON_END =0xFFFFFFFD ,PERF_PMU_TYPE_TOOL =0xFFFFFFFE ,
tools/perf/util/pmus.c +95 -51
241 lines not on this page · raw message
Signature · 1 line
2.48.1.362.g079036d154-goog
Ian Rogers
reviewerPATCH v3 3/5 perf pmu: Rename name matching for no suffix or wildcard variants
In reply to Ian RogersWildcard PMU naming will match a name like pmu_1 to a PMU name like pmu_10 but not to a PMU name like pmu_2 as the suffix forms part of the match. No suffix matching will match pmu_10 to either pmu_1 or pmu_2. Add or rename matching functions on PMU to make it clearer what kind of matching is being performed.
--- tools/perf/pmu-events/empty-pmu-events.c | 8 +- tools/perf/pmu-events/jevents.py | 8 +- tools/perf/tests/pmu.c | 85 ++++---- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 256 ++++++++++++++++------- tools/perf/util/pmu.h | 5 +- 6 files changed, 235 insertions(+), 129 deletions(-)
tools/perf/pmu-events/empty-pmu-events.c +4 -4
@@ -422,7 +422,7 @@ int pmu_events_table__for_each_event(const struct pmu_events_table *table,const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset];int ret; -if (pmu && !pmu__name_match (pmu, pmu_name)) +if (pmu && !perf_pmu__name_wildcard_match (pmu, pmu_name))continue ; ret =pmu_events_table__for_each_event_pmu (table, table_pmu, fn, data);
@@ -443,7 +443,7 @@ int pmu_events_table__find_event(const struct pmu_events_table *table,const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset];int ret; -if (!pmu__name_match (pmu, pmu_name)) +if (!perf_pmu__name_wildcard_match (pmu, pmu_name))continue ; ret =pmu_events_table__find_event_pmu (table, table_pmu, name, fn, data);
@@ -462,7 +462,7 @@ size_t pmu_events_table__num_events(const struct pmu_events_table *table,const struct pmu_table_entry *table_pmu = &table->pmus[i];const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset]; -if (pmu__name_match (pmu, pmu_name)) +if (perf_pmu__name_wildcard_match (pmu, pmu_name)) count += table_pmu->num_entries; }return count;
@@ -581,7 +581,7 @@ const struct pmu_events_table *perf_pmu__find_events_table(struct perf_pmu *pmu)const struct pmu_table_entry *table_pmu = &map->event_table.pmus[i];const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset]; -if (pmu__name_match (pmu, pmu_name)) +if (perf_pmu__name_wildcard_match (pmu, pmu_name))return &map->event_table; }return NULL ;
tools/perf/pmu-events/jevents.py +4 -4
@@ -945,7 +945,7 @@ int pmu_events_table__for_each_event(const struct pmu_events_table *table, const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset]; int ret; - if (pmu && !pmu__name_match(pmu, pmu_name)) + if (pmu && !perf_pmu__name_wildcard_match(pmu, pmu_name)) continue; ret = pmu_events_table__for_each_event_pmu(table, table_pmu, fn, data);
@@ -966,7 +966,7 @@ int pmu_events_table__find_event(const struct pmu_events_table *table, const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset]; int ret; - if (!pmu__name_match(pmu, pmu_name)) + if (!perf_pmu__name_wildcard_match(pmu, pmu_name)) continue; ret = pmu_events_table__find_event_pmu(table, table_pmu, name, fn, data);
@@ -985,7 +985,7 @@ size_t pmu_events_table__num_events(const struct pmu_events_table *table, const struct pmu_table_entry *table_pmu = &table->pmus[i]; const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset]; - if (pmu__name_match(pmu, pmu_name)) + if (perf_pmu__name_wildcard_match(pmu, pmu_name)) count += table_pmu->num_entries; } return count;
@@ -1104,7 +1104,7 @@ const struct pmu_events_table *perf_pmu__find_events_table(struct perf_pmu *pmu) const struct pmu_table_entry *table_pmu = &map->event_table.pmus[i]; const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset]; - if (pmu__name_match(pmu, pmu_name)) + if (perf_pmu__name_wildcard_match(pmu, pmu_name)) return &map->event_table; } return NULL;
tools/perf/tests/pmu.c +44 -41
120 lines not on this page · raw message
tools/perf/util/parse-events.c +1 -1
@@ -1660,7 +1660,7 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state/* Failed to add, try wildcard expansion of event_or_pmu as a PMU name. */ while ((pmu = perf_pmus__scan(pmu)) != NULL) { if (!parse_events__filter_pmu(parse_state, pmu) && - perf_pmu__match(pmu, event_or_pmu)) { + perf_pmu__wildcard_match(pmu, event_or_pmu)) { bool auto_merge_stats = perf_pmu__auto_merge_stats(pmu); if (!parse_events_add_pmu(parse_state, *listp, pmu,
tools/perf/util/pmu.c +179 -77
322 lines not on this page · raw message
tools/perf/util/pmu.h +3 -2
@@ -240,7 +240,8 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name);size_t perf_pmu__num_events (struct perf_pmu *pmu);int perf_pmu__for_each_event (struct perf_pmu *pmu,bool skip_duplicate_pmus,void *state,pmu_event_callback cb); -bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name); +bool perf_pmu__name_wildcard_match (const struct perf_pmu *pmu,const char *to_match); +bool perf_pmu__name_no_suffix_match (const struct perf_pmu *pmu,const char *to_match);/** * perf_pmu_is_software - is the PMU a software PMU as in it uses the
@@ -275,7 +276,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, const char *config_name);void perf_pmu__warn_invalid_formats (struct perf_pmu *pmu); -bool perf_pmu__match (const struct perf_pmu *pmu,const char *tok); +bool perf_pmu__wildcard_match (const struct perf_pmu *pmu,const char *wildcard_to_match);int perf_pmu__event_source_devices_scnprintf (char *pathname,size_t size); int perf_pmu__pathname_scnprintf(char *buf, size_t size,
Signature · 1 line
2.48.1.362.g079036d154-goog
Namhyung Kim
maintainerRe: [PATCH v3 3/5] perf pmu: Rename name matching for no suffix or wildcard variants
Hi Ian,
Quoted 16 lines · Ian Rogers
Wildcard PMU naming will match a name like
pmu_1to a PMU name likepmu_10but not to a PMU name likepmu_2as the suffix forms part of the match. No suffix matching will matchpmu_10to eitherpmu_1orpmu_2. Add or rename matching functions on PMU to make it clearer what kind of matching is being performed.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/pmu-events/empty-pmu-events.c | 8 +- tools/perf/pmu-events/jevents.py | 8 +- tools/perf/tests/pmu.c | 85 ++++---- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 256 ++++++++++++++++------- tools/perf/util/pmu.h | 5 +- 6 files changed, 235 insertions(+), 129 deletions(-)
[...]
Quoted 10 lines
tools/perf/util/pmu.c +0
-2@@ -847,21 +847,23 @@ static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, const char *name, }/** -* perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any -* trailing suffix? The Suffix must be in form
tools/perf/util/pmu.c static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, const char *name, · line 856
Quoted 10 lines
tools/perf/util/pmu.c +5
-3-* tok_{digits}, or tok{digits}. +* perf_pmu__match_wildcard - Does the pmu_name start with tok and is then only +* followed by nothing or a suffix? tok may contain +* part of a suffix. * @pmu_name: The pmu_name with possible suffix. -* @tok: The possible match to pmu_name without suffix. +* @tok: The wildcard argument to match. */ -static bool perf_pmu__match_ignoring_suffix (const char *pmu_name,const char *tok) +static bool perf_pmu__match_wildcard (const char *pmu_name,const char *tok)
It'd be nice if we use a function prefix with "__" only if the first argument is the corresponding type. How about renaming it to pmu_name_match_wildcard() and others?
Otherwise, looks good to me.
Thanks,
Namhyung
Quoted 338 lines
tools/perf/util/pmu.c +174
-72{const char *p, *suffix;bool has_hex = false; +size_t tok_len =strlen (tok); -if (strncmp (pmu_name, tok,strlen (tok))) +/* Check start of pmu_name for equality. */ +if (strncmp (pmu_name, tok, tok_len))return false; - suffix = p = pmu_name +strlen (tok); + suffix = p = pmu_name + tok_len;if (*p ==0 )return true;@@ -887,60 +889,84 @@ static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *to }/** -* pmu_uncore_alias_match - does name match the PMU name? -* @pmu_name: the json struct pmu_event name. This may lack a suffix (which +* perf_pmu__match_ignoring_suffix_uncore - Does the pmu_name match tok ignoring +* any trailing suffix on pmu_name and +* tok? The Suffix must be in form +* tok_{digits}, or tok{digits}. +* @pmu_name: The pmu_name with possible suffix. +* @tok: The possible match to pmu_name. +*/ +static bool perf_pmu__match_ignoring_suffix_uncore (const char *pmu_name,const char *tok) +{ +size_t pmu_name_len, tok_len; + +/* For robustness, check for NULL. */ +if (pmu_name ==NULL ) +return tok ==NULL ; + +/* uncore_ prefixes are ignored. */ +if (!strncmp (pmu_name,"uncore_" ,7 )) + pmu_name +=7 ; +if (!strncmp (tok,"uncore_" ,7 )) + tok +=7 ; + + pmu_name_len =pmu_name_len_no_suffix (pmu_name); + tok_len =pmu_name_len_no_suffix (tok); +if (pmu_name_len != tok_len) +return false; + +return strncmp (pmu_name, tok, pmu_name_len) ==0 ; +} + + +/** +* perf_pmu__match_wildcard_uncore - does to_match match the PMU's name? +* @pmu_name: The pmu->name or pmu->alias to match against. +* @to_match: the json struct pmu_event name. This may lack a suffix (which * matches) or be of the form "socket,pmuname" which will match * "socketX_pmunameY". -* @name: a real full PMU name as from sysfs. */ -static bool pmu_uncore_alias_match (const char *pmu_name,const char *name) +static bool perf_pmu__match_wildcard_uncore (const char *pmu_name,const char *to_match) { -char *tmp =NULL , *tok, *str; -bool res; - -if (strchr (pmu_name,',' ) ==NULL ) -return perf_pmu__match_ignoring_suffix (name, pmu_name); +char *mutable_to_match, *tok, *tmp; - str =strdup (pmu_name); -if (!str) +if (!pmu_name)return false; -/* -* uncore alias may be from different PMU with common prefix -*/ - tok =strtok_r (str,"," , &tmp); -if (strncmp (pmu_name, tok,strlen (tok))) { - res = false; - goto out; - } +/* uncore_ prefixes are ignored. */ +if (!strncmp (pmu_name,"uncore_" ,7 )) + pmu_name +=7 ; +if (!strncmp (to_match,"uncore_" ,7 )) + to_match +=7 ; -/* -* Match more complex aliases where the alias name is a comma-delimited -* list of tokens, orderly contained in the matching PMU name. -* -* Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we -* match "socket" in "socketX_pmunameY" and then "pmuname" in -* "pmunameY". -*/ -while (1 ) { -char *next_tok =strtok_r (NULL ,"," , &tmp); +if (strchr (to_match,',' ) ==NULL ) +return perf_pmu__match_wildcard (pmu_name, to_match); - name =strstr (name, tok); -if (!name || - (!next_tok && !perf_pmu__match_ignoring_suffix (name, tok))) { - res = false; - goto out; +/* Process comma separated list of PMU name components. */ + mutable_to_match =strdup (to_match); +if (!mutable_to_match) +return false; + + tok =strtok_r (mutable_to_match,"," , &tmp); +while (tok) { +size_t tok_len =strlen (tok); + +if (strncmp (pmu_name, tok, tok_len)) { +/* Mismatch between part of pmu_name and tok. */ +free (mutable_to_match); +return false; } -if (!next_tok) -break ; - tok = next_tok; - name +=strlen (tok); +/* Move pmu_name forward over tok and suffix. */ + pmu_name += tok_len; +while (*pmu_name !='\0' &&isdigit (*pmu_name)) + pmu_name++; +if (*pmu_name =='_' ) + pmu_name++; + + tok =strtok_r (NULL ,"," , &tmp); } - - res = true; -out: -free (str); -return res; +free (mutable_to_match); +return *pmu_name =='\0' ; }bool pmu_uncore_identifier_match (const char *compat,const char *id)@@ -1003,11 +1029,19 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, {struct perf_pmu *pmu = vdata; -if (!pe->compat || !pe->pmu) +if (!pe->compat || !pe->pmu) { +/* No data to match. */ return 0 ; + } + +if (!perf_pmu__match_wildcard_uncore (pmu->name, pe->pmu) && + !perf_pmu__match_wildcard_uncore (pmu->alias_name, pe->pmu)) { +/* PMU name/alias_name don't match. */ +return 0 ; + } -if (pmu_uncore_alias_match (pe->pmu, pmu->name) && -pmu_uncore_identifier_match (pe->compat, pmu->id)) { +if (pmu_uncore_identifier_match (pe->compat, pmu->id)) { +/* Id matched. */ perf_pmu__new_alias(pmu, pe->name, pe->desc,@@ -1016,7 +1050,6 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, pe, EVENT_SRC_SYS_JSON); } -return 0 ; }@@ -1974,15 +2007,82 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,return ret; } -bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name) +static bool perf_pmu___name_match (const struct perf_pmu *pmu,const char *to_match,bool wildcard) { -return !strcmp (pmu->name, pmu_name) || - (pmu->is_uncore &&pmu_uncore_alias_match (pmu_name, pmu->name)) || +const char *names[2 ] = { + pmu->name, + pmu->alias_name, + }; +if (pmu->is_core) { +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *name = names[i]; + +if (!name) +continue ; + +if (!strcmp (name, to_match)) { +/* Exact name match. */ +return true; + } + } +if (!strcmp (to_match,"default_core" )) { +/* +* jevents and tests use default_core as a marker for any core +* PMU as the PMU name varies across architectures. +*/ +return true; + } +return false; + } +if (!pmu->is_uncore) {/* -* jevents and tests use default_core as a marker for any core -* PMU as the PMU name varies across architectures. +* PMU isn't core or uncore, some kind of broken CPU mask +* situation. Only match exact name. */ - (pmu->is_core && !strcmp (pmu_name,"default_core" )); +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *name = names[i]; + +if (!name) +continue ; + +if (!strcmp (name, to_match)) { +/* Exact name match. */ +return true; + } + } +return false; + } +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *name = names[i]; + +if (wildcard &&perf_pmu__match_wildcard_uncore (name, to_match)) +return true; +if (!wildcard &&perf_pmu__match_ignoring_suffix_uncore (name, to_match)) +return true; + } +return false; +} + +/** +* perf_pmu__name_wildcard_match - Called by the jevents generated code to see +* if pmu matches the json to_match string. +* @pmu: The pmu whose name/alias to match. +* @to_match: The possible match to pmu_name. +*/ +bool perf_pmu__name_wildcard_match (const struct perf_pmu *pmu,const char *to_match) +{ +return perf_pmu___name_match (pmu, to_match,/*wildcard=*/ true); +} + +/** +* perf_pmu__name_no_suffix_match - Does pmu's name match to_match ignoring any +* trailing suffix on the pmu_name and/or tok? +* @pmu: The pmu whose name/alias to match. +* @to_match: The possible match to pmu_name. +*/ +bool perf_pmu__name_no_suffix_match (const struct perf_pmu *pmu,const char *to_match) +{ +return perf_pmu___name_match (pmu, to_match,/*wildcard=*/ false); }bool perf_pmu__is_software (const struct perf_pmu *pmu)@@ -2229,29 +2329,31 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, name ?: "N/A", buf, config_name, config); } -bool perf_pmu__match (const struct perf_pmu *pmu,const char *tok) +bool perf_pmu__wildcard_match (const struct perf_pmu *pmu,const char *wildcard_to_match) { -const char *name = pmu->name; -bool need_fnmatch =strisglob (tok); +const char *names[2 ] = { + pmu->name, + pmu->alias_name, + }; +bool need_fnmatch =strisglob (wildcard_to_match); -if (!strncmp (tok,"uncore_" ,7 )) - tok +=7 ; -if (!strncmp (name,"uncore_" ,7 )) - name +=7 ; +if (!strncmp (wildcard_to_match,"uncore_" ,7 )) + wildcard_to_match +=7 ; -if (perf_pmu__match_ignoring_suffix (name, tok) || - (need_fnmatch && !fnmatch (tok, name,0 ))) -return true; +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *pmu_name = names[i]; - name = pmu->alias_name; -if (!name) -return false; +if (!pmu_name) +continue ; -if (!strncmp (name,"uncore_" ,7 )) - name +=7 ; +if (!strncmp (pmu_name,"uncore_" ,7 )) + pmu_name +=7 ; -return perf_pmu__match_ignoring_suffix (name, tok) || - (need_fnmatch && !fnmatch (tok, name,0 )); +if (perf_pmu__match_wildcard (pmu_name, wildcard_to_match) || + (need_fnmatch && !fnmatch (wildcard_to_match, pmu_name,0 ))) +return true; + } +return false; }int perf_pmu__event_source_devices_scnprintf (char *pathname,size_t size)tools/perf/util/pmu.h +3
-2@@ -240,7 +240,8 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name);size_t perf_pmu__num_events (struct perf_pmu *pmu);int perf_pmu__for_each_event (struct perf_pmu *pmu,bool skip_duplicate_pmus,void *state,pmu_event_callback cb); -bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name); +bool perf_pmu__name_wildcard_match (const struct perf_pmu *pmu,const char *to_match); +bool perf_pmu__name_no_suffix_match (const struct perf_pmu *pmu,const char *to_match);/** * perf_pmu_is_software - is the PMU a software PMU as in it uses the @@ -275,7 +276,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, const char *config_name);void perf_pmu__warn_invalid_formats (struct perf_pmu *pmu); -bool perf_pmu__match (const struct perf_pmu *pmu,const char *tok); +bool perf_pmu__wildcard_match (const struct perf_pmu *pmu,const char *wildcard_to_match);int perf_pmu__event_source_devices_scnprintf (char *pathname,size_t size); int perf_pmu__pathname_scnprintf(char *buf, size_t size,Signature · 1 line
2.48.1.362.g079036d154-goog
Ian Rogers
reviewerRe: [PATCH v3 3/5] perf pmu: Rename name matching for no suffix or wildcard variants
Quoted 46 lines · Namhyung Kim
Hi Ian,
Quoted 16 lines · Ian Rogers
Wildcard PMU naming will match a name like
pmu_1to a PMU name likepmu_10but not to a PMU name likepmu_2as the suffix forms part of the match. No suffix matching will matchpmu_10to eitherpmu_1orpmu_2. Add or rename matching functions on PMU to make it clearer what kind of matching is being performed.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/pmu-events/empty-pmu-events.c | 8 +- tools/perf/pmu-events/jevents.py | 8 +- tools/perf/tests/pmu.c | 85 ++++---- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 256 ++++++++++++++++------- tools/perf/util/pmu.h | 5 +- 6 files changed, 235 insertions(+), 129 deletions(-)[...]
Quoted 10 lines
tools/perf/util/pmu.c +0
-2@@ -847,21 +847,23 @@ static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, const char *name, }/** -* perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any -* trailing suffix? The Suffix must be in form tools/perf/util/pmu.c static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, const char *name, · line 856
Quoted 10 lines
tools/perf/util/pmu.c +5
-3-* tok_{digits}, or tok{digits}. +* perf_pmu__match_wildcard - Does the pmu_name start with tok and is then only +* followed by nothing or a suffix? tok may contain +* part of a suffix. * @pmu_name: The pmu_name with possible suffix. -* @tok: The possible match to pmu_name without suffix. +* @tok: The wildcard argument to match. */ -static bool perf_pmu__match_ignoring_suffix (const char *pmu_name,const char *tok) +static bool perf_pmu__match_wildcard (const char *pmu_name,const char *tok)It'd be nice if we use a function prefix with "__" only if the first argument is the corresponding type. How about renaming it to
pmu_name_match_wildcard()and others?
Quoted 1 line
Otherwise, looks good to me.
I think the intent is to make them look like (in the C++ sense) static member functions. I was following the existing convention as with perf_pmu__event_source_devices_scnprintf, perf_pmu__pathname_scnprintf, perf_pmu__convert_scale, perf_pmu__event_source_devices_fd, perf_pmu__pathname_fd, all the perf_pmus functions, etc.
Thanks,
Ian
Quoted 342 lines
Thanks,
NamhyungQuoted 338 lines
tools/perf/util/pmu.c +174
-72{const char *p, *suffix;bool has_hex = false; +size_t tok_len =strlen (tok); -if (strncmp (pmu_name, tok,strlen (tok))) +/* Check start of pmu_name for equality. */ +if (strncmp (pmu_name, tok, tok_len))return false; - suffix = p = pmu_name +strlen (tok); + suffix = p = pmu_name + tok_len;if (*p ==0 )return true;@@ -887,60 +889,84 @@ static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *to }/** -* pmu_uncore_alias_match - does name match the PMU name? -* @pmu_name: the json struct pmu_event name. This may lack a suffix (which +* perf_pmu__match_ignoring_suffix_uncore - Does the pmu_name match tok ignoring +* any trailing suffix on pmu_name and +* tok? The Suffix must be in form +* tok_{digits}, or tok{digits}. +* @pmu_name: The pmu_name with possible suffix. +* @tok: The possible match to pmu_name. +*/ +static bool perf_pmu__match_ignoring_suffix_uncore (const char *pmu_name,const char *tok) +{ +size_t pmu_name_len, tok_len; + +/* For robustness, check for NULL. */ +if (pmu_name ==NULL ) +return tok ==NULL ; + +/* uncore_ prefixes are ignored. */ +if (!strncmp (pmu_name,"uncore_" ,7 )) + pmu_name +=7 ; +if (!strncmp (tok,"uncore_" ,7 )) + tok +=7 ; + + pmu_name_len =pmu_name_len_no_suffix (pmu_name); + tok_len =pmu_name_len_no_suffix (tok); +if (pmu_name_len != tok_len) +return false; + +return strncmp (pmu_name, tok, pmu_name_len) ==0 ; +} + + +/** +* perf_pmu__match_wildcard_uncore - does to_match match the PMU's name? +* @pmu_name: The pmu->name or pmu->alias to match against. +* @to_match: the json struct pmu_event name. This may lack a suffix (which * matches) or be of the form "socket,pmuname" which will match * "socketX_pmunameY". -* @name: a real full PMU name as from sysfs. */ -static bool pmu_uncore_alias_match (const char *pmu_name,const char *name) +static bool perf_pmu__match_wildcard_uncore (const char *pmu_name,const char *to_match) { -char *tmp =NULL , *tok, *str; -bool res; - -if (strchr (pmu_name,',' ) ==NULL ) -return perf_pmu__match_ignoring_suffix (name, pmu_name); +char *mutable_to_match, *tok, *tmp; - str =strdup (pmu_name); -if (!str) +if (!pmu_name)return false; -/* -* uncore alias may be from different PMU with common prefix -*/ - tok =strtok_r (str,"," , &tmp); -if (strncmp (pmu_name, tok,strlen (tok))) { - res = false; - goto out; - } +/* uncore_ prefixes are ignored. */ +if (!strncmp (pmu_name,"uncore_" ,7 )) + pmu_name +=7 ; +if (!strncmp (to_match,"uncore_" ,7 )) + to_match +=7 ; -/* -* Match more complex aliases where the alias name is a comma-delimited -* list of tokens, orderly contained in the matching PMU name. -* -* Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we -* match "socket" in "socketX_pmunameY" and then "pmuname" in -* "pmunameY". -*/ -while (1 ) { -char *next_tok =strtok_r (NULL ,"," , &tmp); +if (strchr (to_match,',' ) ==NULL ) +return perf_pmu__match_wildcard (pmu_name, to_match); - name =strstr (name, tok); -if (!name || - (!next_tok && !perf_pmu__match_ignoring_suffix (name, tok))) { - res = false; - goto out; +/* Process comma separated list of PMU name components. */ + mutable_to_match =strdup (to_match); +if (!mutable_to_match) +return false; + + tok =strtok_r (mutable_to_match,"," , &tmp); +while (tok) { +size_t tok_len =strlen (tok); + +if (strncmp (pmu_name, tok, tok_len)) { +/* Mismatch between part of pmu_name and tok. */ +free (mutable_to_match); +return false; } -if (!next_tok) -break ; - tok = next_tok; - name +=strlen (tok); +/* Move pmu_name forward over tok and suffix. */ + pmu_name += tok_len; +while (*pmu_name !='\0' &&isdigit (*pmu_name)) + pmu_name++; +if (*pmu_name =='_' ) + pmu_name++; + + tok =strtok_r (NULL ,"," , &tmp); } - - res = true; -out: -free (str); -return res; +free (mutable_to_match); +return *pmu_name =='\0' ; }bool pmu_uncore_identifier_match (const char *compat,const char *id)@@ -1003,11 +1029,19 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, {struct perf_pmu *pmu = vdata; -if (!pe->compat || !pe->pmu) +if (!pe->compat || !pe->pmu) { +/* No data to match. */ return 0 ; + } + +if (!perf_pmu__match_wildcard_uncore (pmu->name, pe->pmu) && + !perf_pmu__match_wildcard_uncore (pmu->alias_name, pe->pmu)) { +/* PMU name/alias_name don't match. */ +return 0 ; + } -if (pmu_uncore_alias_match (pe->pmu, pmu->name) && -pmu_uncore_identifier_match (pe->compat, pmu->id)) { +if (pmu_uncore_identifier_match (pe->compat, pmu->id)) { +/* Id matched. */ perf_pmu__new_alias(pmu, pe->name, pe->desc,@@ -1016,7 +1050,6 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, pe, EVENT_SRC_SYS_JSON); } -return 0 ; }@@ -1974,15 +2007,82 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,return ret; } -bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name) +static bool perf_pmu___name_match (const struct perf_pmu *pmu,const char *to_match,bool wildcard) { -return !strcmp (pmu->name, pmu_name) || - (pmu->is_uncore &&pmu_uncore_alias_match (pmu_name, pmu->name)) || +const char *names[2 ] = { + pmu->name, + pmu->alias_name, + }; +if (pmu->is_core) { +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *name = names[i]; + +if (!name) +continue ; + +if (!strcmp (name, to_match)) { +/* Exact name match. */ +return true; + } + } +if (!strcmp (to_match,"default_core" )) { +/* +* jevents and tests use default_core as a marker for any core +* PMU as the PMU name varies across architectures. +*/ +return true; + } +return false; + } +if (!pmu->is_uncore) {/* -* jevents and tests use default_core as a marker for any core -* PMU as the PMU name varies across architectures. +* PMU isn't core or uncore, some kind of broken CPU mask +* situation. Only match exact name. */ - (pmu->is_core && !strcmp (pmu_name,"default_core" )); +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *name = names[i]; + +if (!name) +continue ; + +if (!strcmp (name, to_match)) { +/* Exact name match. */ +return true; + } + } +return false; + } +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *name = names[i]; + +if (wildcard &&perf_pmu__match_wildcard_uncore (name, to_match)) +return true; +if (!wildcard &&perf_pmu__match_ignoring_suffix_uncore (name, to_match)) +return true; + } +return false; +} + +/** +* perf_pmu__name_wildcard_match - Called by the jevents generated code to see +* if pmu matches the json to_match string. +* @pmu: The pmu whose name/alias to match. +* @to_match: The possible match to pmu_name. +*/ +bool perf_pmu__name_wildcard_match (const struct perf_pmu *pmu,const char *to_match) +{ +return perf_pmu___name_match (pmu, to_match,/*wildcard=*/ true); +} + +/** +* perf_pmu__name_no_suffix_match - Does pmu's name match to_match ignoring any +* trailing suffix on the pmu_name and/or tok? +* @pmu: The pmu whose name/alias to match. +* @to_match: The possible match to pmu_name. +*/ +bool perf_pmu__name_no_suffix_match (const struct perf_pmu *pmu,const char *to_match) +{ +return perf_pmu___name_match (pmu, to_match,/*wildcard=*/ false); }bool perf_pmu__is_software (const struct perf_pmu *pmu)@@ -2229,29 +2329,31 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, name ?: "N/A", buf, config_name, config); } -bool perf_pmu__match (const struct perf_pmu *pmu,const char *tok) +bool perf_pmu__wildcard_match (const struct perf_pmu *pmu,const char *wildcard_to_match) { -const char *name = pmu->name; -bool need_fnmatch =strisglob (tok); +const char *names[2 ] = { + pmu->name, + pmu->alias_name, + }; +bool need_fnmatch =strisglob (wildcard_to_match); -if (!strncmp (tok,"uncore_" ,7 )) - tok +=7 ; -if (!strncmp (name,"uncore_" ,7 )) - name +=7 ; +if (!strncmp (wildcard_to_match,"uncore_" ,7 )) + wildcard_to_match +=7 ; -if (perf_pmu__match_ignoring_suffix (name, tok) || - (need_fnmatch && !fnmatch (tok, name,0 ))) -return true; +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *pmu_name = names[i]; - name = pmu->alias_name; -if (!name) -return false; +if (!pmu_name) +continue ; -if (!strncmp (name,"uncore_" ,7 )) - name +=7 ; +if (!strncmp (pmu_name,"uncore_" ,7 )) + pmu_name +=7 ; -return perf_pmu__match_ignoring_suffix (name, tok) || - (need_fnmatch && !fnmatch (tok, name,0 )); +if (perf_pmu__match_wildcard (pmu_name, wildcard_to_match) || + (need_fnmatch && !fnmatch (wildcard_to_match, pmu_name,0 ))) +return true; + } +return false; }int perf_pmu__event_source_devices_scnprintf (char *pathname,size_t size)tools/perf/util/pmu.h +3
-3@@ -240,7 +240,8 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name);size_t perf_pmu__num_events (struct perf_pmu *pmu);int perf_pmu__for_each_event (struct perf_pmu *pmu,bool skip_duplicate_pmus,void *state,pmu_event_callback cb); -bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name); +bool perf_pmu__name_wildcard_match (const struct perf_pmu *pmu,const char *to_match); +bool perf_pmu__name_no_suffix_match (const struct perf_pmu *pmu,const char *to_match);/** * perf_pmu_is_software - is the PMU a software PMU as in it uses the @@ -275,7 +276,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, const char *config_name);void perf_pmu__warn_invalid_formats (struct perf_pmu *pmu); -bool perf_pmu__match (const struct perf_pmu *pmu,const char *tok); +bool perf_pmu__wildcard_match (const struct perf_pmu *pmu,const char *wildcard_to_match);int perf_pmu__event_source_devices_scnprintf (char *pathname,size_t size); int perf_pmu__pathname_scnprintf(char *buf, size_t size, --2.48.1.362.g079036d154-goog
Namhyung Kim
maintainerRe: [PATCH v3 3/5] perf pmu: Rename name matching for no suffix or wildcard variants
Quoted 47 lines · Ian Rogers
Quoted 46 lines · Namhyung Kim
Hi Ian,
Quoted 16 lines · Ian Rogers
Wildcard PMU naming will match a name like
pmu_1to a PMU name likepmu_10but not to a PMU name likepmu_2as the suffix forms part of the match. No suffix matching will matchpmu_10to eitherpmu_1orpmu_2. Add or rename matching functions on PMU to make it clearer what kind of matching is being performed.Signed-off-by Ian Rogers <[email protected]>--- tools/perf/pmu-events/empty-pmu-events.c | 8 +- tools/perf/pmu-events/jevents.py | 8 +- tools/perf/tests/pmu.c | 85 ++++---- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 256 ++++++++++++++++------- tools/perf/util/pmu.h | 5 +- 6 files changed, 235 insertions(+), 129 deletions(-)[...]
Quoted 10 lines
tools/perf/util/pmu.c +0
-2@@ -847,21 +847,23 @@ static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, const char *name, }/** -* perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any -* trailing suffix? The Suffix must be in form tools/perf/util/pmu.c static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, const char *name, · line 856
Quoted 10 lines
tools/perf/util/pmu.c +5
-3-* tok_{digits}, or tok{digits}. +* perf_pmu__match_wildcard - Does the pmu_name start with tok and is then only +* followed by nothing or a suffix? tok may contain +* part of a suffix. * @pmu_name: The pmu_name with possible suffix. -* @tok: The possible match to pmu_name without suffix. +* @tok: The wildcard argument to match. */ -static bool perf_pmu__match_ignoring_suffix (const char *pmu_name,const char *tok) +static bool perf_pmu__match_wildcard (const char *pmu_name,const char *tok)It'd be nice if we use a function prefix with "__" only if the first argument is the corresponding type. How about renaming it to
pmu_name_match_wildcard()and others?
Quoted 8 lines
Quoted 1 line
Otherwise, looks good to me.
I think the intent is to make them look like (in the C++ sense) static member functions. I was following the existing convention as with
perf_pmu__event_source_devices_scnprintf,perf_pmu__pathname_scnprintf,perf_pmu__convert_scale,perf_pmu__event_source_devices_fd,perf_pmu__pathname_fd, all theperf_pmusfunctions, etc.
I still think it's better to match the prefix with the data type but we can fix them all later.
Thanks,
Namhyung
Quoted 339 lines
Quoted 339 lines
Quoted 339 lines
tools/perf/util/pmu.c +174
-72{const char *p, *suffix;bool has_hex = false; +size_t tok_len =strlen (tok); -if (strncmp (pmu_name, tok,strlen (tok))) +/* Check start of pmu_name for equality. */ +if (strncmp (pmu_name, tok, tok_len))return false; - suffix = p = pmu_name +strlen (tok); + suffix = p = pmu_name + tok_len;if (*p ==0 )return true;@@ -887,60 +889,84 @@ static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *to }/** -* pmu_uncore_alias_match - does name match the PMU name? -* @pmu_name: the json struct pmu_event name. This may lack a suffix (which +* perf_pmu__match_ignoring_suffix_uncore - Does the pmu_name match tok ignoring +* any trailing suffix on pmu_name and +* tok? The Suffix must be in form +* tok_{digits}, or tok{digits}. +* @pmu_name: The pmu_name with possible suffix. +* @tok: The possible match to pmu_name. +*/ +static bool perf_pmu__match_ignoring_suffix_uncore (const char *pmu_name,const char *tok) +{ +size_t pmu_name_len, tok_len; + +/* For robustness, check for NULL. */ +if (pmu_name ==NULL ) +return tok ==NULL ; + +/* uncore_ prefixes are ignored. */ +if (!strncmp (pmu_name,"uncore_" ,7 )) + pmu_name +=7 ; +if (!strncmp (tok,"uncore_" ,7 )) + tok +=7 ; + + pmu_name_len =pmu_name_len_no_suffix (pmu_name); + tok_len =pmu_name_len_no_suffix (tok); +if (pmu_name_len != tok_len) +return false; + +return strncmp (pmu_name, tok, pmu_name_len) ==0 ; +} + + +/** +* perf_pmu__match_wildcard_uncore - does to_match match the PMU's name? +* @pmu_name: The pmu->name or pmu->alias to match against. +* @to_match: the json struct pmu_event name. This may lack a suffix (which * matches) or be of the form "socket,pmuname" which will match * "socketX_pmunameY". -* @name: a real full PMU name as from sysfs. */ -static bool pmu_uncore_alias_match (const char *pmu_name,const char *name) +static bool perf_pmu__match_wildcard_uncore (const char *pmu_name,const char *to_match) { -char *tmp =NULL , *tok, *str; -bool res; - -if (strchr (pmu_name,',' ) ==NULL ) -return perf_pmu__match_ignoring_suffix (name, pmu_name); +char *mutable_to_match, *tok, *tmp; - str =strdup (pmu_name); -if (!str) +if (!pmu_name)return false; -/* -* uncore alias may be from different PMU with common prefix -*/ - tok =strtok_r (str,"," , &tmp); -if (strncmp (pmu_name, tok,strlen (tok))) { - res = false; - goto out; - } +/* uncore_ prefixes are ignored. */ +if (!strncmp (pmu_name,"uncore_" ,7 )) + pmu_name +=7 ; +if (!strncmp (to_match,"uncore_" ,7 )) + to_match +=7 ; -/* -* Match more complex aliases where the alias name is a comma-delimited -* list of tokens, orderly contained in the matching PMU name. -* -* Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we -* match "socket" in "socketX_pmunameY" and then "pmuname" in -* "pmunameY". -*/ -while (1 ) { -char *next_tok =strtok_r (NULL ,"," , &tmp); +if (strchr (to_match,',' ) ==NULL ) +return perf_pmu__match_wildcard (pmu_name, to_match); - name =strstr (name, tok); -if (!name || - (!next_tok && !perf_pmu__match_ignoring_suffix (name, tok))) { - res = false; - goto out; +/* Process comma separated list of PMU name components. */ + mutable_to_match =strdup (to_match); +if (!mutable_to_match) +return false; + + tok =strtok_r (mutable_to_match,"," , &tmp); +while (tok) { +size_t tok_len =strlen (tok); + +if (strncmp (pmu_name, tok, tok_len)) { +/* Mismatch between part of pmu_name and tok. */ +free (mutable_to_match); +return false; } -if (!next_tok) -break ; - tok = next_tok; - name +=strlen (tok); +/* Move pmu_name forward over tok and suffix. */ + pmu_name += tok_len; +while (*pmu_name !='\0' &&isdigit (*pmu_name)) + pmu_name++; +if (*pmu_name =='_' ) + pmu_name++; + + tok =strtok_r (NULL ,"," , &tmp); } - - res = true; -out: -free (str); -return res; +free (mutable_to_match); +return *pmu_name =='\0' ; }bool pmu_uncore_identifier_match (const char *compat,const char *id)@@ -1003,11 +1029,19 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, {struct perf_pmu *pmu = vdata; -if (!pe->compat || !pe->pmu) +if (!pe->compat || !pe->pmu) { +/* No data to match. */ return 0 ; + } + +if (!perf_pmu__match_wildcard_uncore (pmu->name, pe->pmu) && + !perf_pmu__match_wildcard_uncore (pmu->alias_name, pe->pmu)) { +/* PMU name/alias_name don't match. */ +return 0 ; + } -if (pmu_uncore_alias_match (pe->pmu, pmu->name) && -pmu_uncore_identifier_match (pe->compat, pmu->id)) { +if (pmu_uncore_identifier_match (pe->compat, pmu->id)) { +/* Id matched. */ perf_pmu__new_alias(pmu, pe->name, pe->desc,@@ -1016,7 +1050,6 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, pe, EVENT_SRC_SYS_JSON); } -return 0 ; }@@ -1974,15 +2007,82 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,return ret; } -bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name) +static bool perf_pmu___name_match (const struct perf_pmu *pmu,const char *to_match,bool wildcard) { -return !strcmp (pmu->name, pmu_name) || - (pmu->is_uncore &&pmu_uncore_alias_match (pmu_name, pmu->name)) || +const char *names[2 ] = { + pmu->name, + pmu->alias_name, + }; +if (pmu->is_core) { +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *name = names[i]; + +if (!name) +continue ; + +if (!strcmp (name, to_match)) { +/* Exact name match. */ +return true; + } + } +if (!strcmp (to_match,"default_core" )) { +/* +* jevents and tests use default_core as a marker for any core +* PMU as the PMU name varies across architectures. +*/ +return true; + } +return false; + } +if (!pmu->is_uncore) {/* -* jevents and tests use default_core as a marker for any core -* PMU as the PMU name varies across architectures. +* PMU isn't core or uncore, some kind of broken CPU mask +* situation. Only match exact name. */ - (pmu->is_core && !strcmp (pmu_name,"default_core" )); +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *name = names[i]; + +if (!name) +continue ; + +if (!strcmp (name, to_match)) { +/* Exact name match. */ +return true; + } + } +return false; + } +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *name = names[i]; + +if (wildcard &&perf_pmu__match_wildcard_uncore (name, to_match)) +return true; +if (!wildcard &&perf_pmu__match_ignoring_suffix_uncore (name, to_match)) +return true; + } +return false; +} + +/** +* perf_pmu__name_wildcard_match - Called by the jevents generated code to see +* if pmu matches the json to_match string. +* @pmu: The pmu whose name/alias to match. +* @to_match: The possible match to pmu_name. +*/ +bool perf_pmu__name_wildcard_match (const struct perf_pmu *pmu,const char *to_match) +{ +return perf_pmu___name_match (pmu, to_match,/*wildcard=*/ true); +} + +/** +* perf_pmu__name_no_suffix_match - Does pmu's name match to_match ignoring any +* trailing suffix on the pmu_name and/or tok? +* @pmu: The pmu whose name/alias to match. +* @to_match: The possible match to pmu_name. +*/ +bool perf_pmu__name_no_suffix_match (const struct perf_pmu *pmu,const char *to_match) +{ +return perf_pmu___name_match (pmu, to_match,/*wildcard=*/ false); }bool perf_pmu__is_software (const struct perf_pmu *pmu)@@ -2229,29 +2329,31 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, name ?: "N/A", buf, config_name, config); } -bool perf_pmu__match (const struct perf_pmu *pmu,const char *tok) +bool perf_pmu__wildcard_match (const struct perf_pmu *pmu,const char *wildcard_to_match) { -const char *name = pmu->name; -bool need_fnmatch =strisglob (tok); +const char *names[2 ] = { + pmu->name, + pmu->alias_name, + }; +bool need_fnmatch =strisglob (wildcard_to_match); -if (!strncmp (tok,"uncore_" ,7 )) - tok +=7 ; -if (!strncmp (name,"uncore_" ,7 )) - name +=7 ; +if (!strncmp (wildcard_to_match,"uncore_" ,7 )) + wildcard_to_match +=7 ; -if (perf_pmu__match_ignoring_suffix (name, tok) || - (need_fnmatch && !fnmatch (tok, name,0 ))) -return true; +for (size_t i =0 ; i <ARRAY_SIZE (names); i++) { +const char *pmu_name = names[i]; - name = pmu->alias_name; -if (!name) -return false; +if (!pmu_name) +continue ; -if (!strncmp (name,"uncore_" ,7 )) - name +=7 ; +if (!strncmp (pmu_name,"uncore_" ,7 )) + pmu_name +=7 ; -return perf_pmu__match_ignoring_suffix (name, tok) || - (need_fnmatch && !fnmatch (tok, name,0 )); +if (perf_pmu__match_wildcard (pmu_name, wildcard_to_match) || + (need_fnmatch && !fnmatch (wildcard_to_match, pmu_name,0 ))) +return true; + } +return false; }int perf_pmu__event_source_devices_scnprintf (char *pathname,size_t size)tools/perf/util/pmu.h +3
-3@@ -240,7 +240,8 @@ bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name);size_t perf_pmu__num_events (struct perf_pmu *pmu);int perf_pmu__for_each_event (struct perf_pmu *pmu,bool skip_duplicate_pmus,void *state,pmu_event_callback cb); -bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name); +bool perf_pmu__name_wildcard_match (const struct perf_pmu *pmu,const char *to_match); +bool perf_pmu__name_no_suffix_match (const struct perf_pmu *pmu,const char *to_match);/** * perf_pmu_is_software - is the PMU a software PMU as in it uses the @@ -275,7 +276,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, const char *config_name);void perf_pmu__warn_invalid_formats (struct perf_pmu *pmu); -bool perf_pmu__match (const struct perf_pmu *pmu,const char *tok); +bool perf_pmu__wildcard_match (const struct perf_pmu *pmu,const char *wildcard_to_match);int perf_pmu__event_source_devices_scnprintf (char *pathname,size_t size); int perf_pmu__pathname_scnprintf(char *buf, size_t size, --2.48.1.362.g079036d154-goog
Ian Rogers
reviewerPATCH v3 4/5 perf stat: Don't merge counters purely on name
In reply to Ian RogersCounter merging was added in commit 942c5593393d ("perf stat: Add perf_stat_merge_counters()"), however, it merges events with the same name on different PMUs regardless of whether the different PMUs are actually of the same type (ie they differ only in the suffix on the PMU). For hwmon events there may be a temp1 event on every PMU, but the PMU names are all unique and don't differ just by a suffix. The merging is over eager and will merge all the hwmon counters together meaning an aggregated and very large temp1 value is shown. The same would be true for say cache events and memory controller events where the PMUs differ but the event names are the same.
Fix the problem by correctly saying two PMUs alias when they differ only by suffix.
Note, there is an overlap with evsel's merged_stat with aggregation and the evsel's metric_leader where aggregation happens for metrics.
--- tools/perf/util/stat.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
tools/perf/util/stat.c +11 -2
@@ -535,7 +535,10 @@ static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)return 0 ; } -/* events should have the same name, scale, unit, cgroup but on different PMUs */ +/* +* Events should have the same name, scale, unit, cgroup but on different core +* PMUs or on different but matching uncore PMUs. +*/ static bool evsel__is_alias (struct evsel *evsel_a,struct evsel *evsel_b) {if (strcmp (evsel__name (evsel_a),evsel__name (evsel_b)))
@@ -553,7 +556,13 @@ static bool evsel__is_alias(struct evsel *evsel_a, struct evsel *evsel_b)if (evsel__is_clock (evsel_a) !=evsel__is_clock (evsel_b))return false; -return evsel_a->pmu != evsel_b->pmu; +if (evsel_a->pmu == evsel_b->pmu || evsel_a->pmu ==NULL || evsel_b->pmu ==NULL ) +return false; + +if (evsel_a->pmu->is_core) +return evsel_b->pmu->is_core; + +return perf_pmu__name_no_suffix_match (evsel_a->pmu, evsel_b->pmu->name); }static void evsel__merge_aliases (struct evsel *evsel)
Signature · 1 line
2.48.1.362.g079036d154-goog
Ian Rogers
reviewerPATCH v3 5/5 perf stat: Changes to event name uniquification
In reply to Ian RogersThe existing logic would disable uniquification on an evlist or enable it per evsel, this is unfortunate as uniquification is most needed when events have the same name and so the whole evlist must be considered. Change the initial disable uniquify on an evlist processing to also set a needs_uniquify flag, for cases like the matching event names. This must be done as an initial pass as uniquification of an event name will change the behavior of the check. Keep the per counter uniquification but now only uniquify event names when the needs_uniquify flag is set.
Before this change a hwmon like temp1 wouldn't be uniquified and afterwards it will (ie the PMU is added to the temp1 event's name).
--- tools/perf/util/evsel.h | 1 + tools/perf/util/stat-display.c | 111 +++++++++++++++++++++++---------- 2 files changed, 79 insertions(+), 33 deletions(-)
tools/perf/util/evsel.h +1 -0
@@ -119,6 +119,7 @@ struct evsel {bool errored;bool needs_auxtrace_mmap;bool default_metricgroup;/* A member of the Default metricgroup */ +bool needs_uniquify;struct hashmap *per_pkg_mask;int err;int script_output_type;
tools/perf/util/stat-display.c +78 -33
167 lines not on this page · raw message
Signature · 1 line
2.48.1.362.g079036d154-goog
Quoted 11 lines · Ian Rogers
Try to do less scanning of files/directories with or without hwmon PMUs. Don't merge all events with the same name, only merge those on the same PMU ignoring suffix. Tidy PMU name matching to distinguish no suffix or wildcard matching. Refactor uniquification so the evsels with the same name as other evsels in the evlist are uniquified.
v3: Address potential segv and styling nits from Kan Liang.
v2: Rename ignore suffix PMU name matching that is really a
wildcard/prefix match. Use a proper ignore suffix when not merging counters purely on name.
Quoted 7 lines
Ian Rogers (5):
perf evsel: Reduce scanning core PMUs inis_hybridperf pmus: Restructurepmu_read_sysfsto scan fewer PMUs perf pmu: Rename name matching for no suffix or wildcard variants perf stat: Don't merge counters purely on name
perf stat: Changes to event name uniquification
I didn't do much tests on the hwmon. But it changes the common codes which impact other PMUs. I did tests mainly on core, uncore and other HW PMUs on a hybrid platform. No regression is found.
The patches look good to me.
Thanks,
Kan
Quoted 13 lines
tools/perf/pmu-events/empty-pmu-events.c | 8 +- tools/perf/pmu-events/jevents.py | 8 +- tools/perf/tests/pmu.c | 85 ++++---- tools/perf/util/evsel.c | 4 +- tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 256 ++++++++++++++++------- tools/perf/util/pmu.h | 7 +- tools/perf/util/pmus.c | 146 ++++++++----- tools/perf/util/stat-display.c | 111 +++++++--- tools/perf/util/stat.c | 13 +- 11 files changed, 424 insertions(+), 217 deletions(-)
Quoted 6 lines · Ian Rogers
Try to do less scanning of files/directories with or without hwmon PMUs. Don't merge all events with the same name, only merge those on the same PMU ignoring suffix. Tidy PMU name matching to distinguish no suffix or wildcard matching. Refactor uniquification so the evsels with the same name as other evsels in the evlist are uniquified.
Quoted 6 lines
v3: Address potential segv and styling nits from Kan Liang.
v2: Rename ignore suffix PMU name matching that is really a
wildcard/prefix match. Use a proper ignore suffix when not merging counters purely on name.[...]
Applied to perf-tools-next, thanks!
Best regards,
Namhyung