Lazily load PMU data
Ian Rogers
reviewercover Lazily load PMU data
Lazily load PMU data both from sysfs and json files. Reorganize json data to be more PMU oriented to facilitate this, for example, json data is now sorted into arrays for their PMU.
In refactoring the code some changes were made to get rid of maximum encoding sizes for events (256 bytes), with input files being directly passed to the lex generated code. There is also a small event parse error message improvement.
Some results from an Intel tigerlake laptop running Debian:
Binary size reduction of 1.4% or 143,264 bytes because the PMU name no longer appears in the string.
stat -e cpu/cycles/ minor faults reduced from 1733 to 1667, open calls reduced from 171 to 94.
stat default minor faults reduced from 1085 to 1727, open calls reduced from 654 to 343.
Average PMU scanning reduced from 4720.641usec to 2927.293usec. Average core PMU scanning reduced from 1004.658usec to 232.668usec (4.3x faster).
Ian Rogers (25):
perf script ibs: Remove unused include
perf pmu: Avoid a path name copy
perf pmu: Move perf_pmu__set_format to pmu.y
perf pmu: Reduce scope of perf_pmu_error
perf pmu: Avoid passing format list to perf_pmu__config_terms perf pmu: Avoid passing format list to perf_pmu__format_type perf pmu: Avoid passing format list to perf_pmu__format_bits perf pmu: Pass PMU rather than aliases and format perf pmu: Make the loading of formats lazy
perf pmu: Abstract alias/event struct
perf pmu-events: Add extra underscore to function names perf jevents: Group events by PMU
perf parse-events: Improve error message for double setting perf s390 s390_cpumcfdg_dump: Don't scan all PMUs perf pmu-events: Reduce processed events by passing PMU perf pmu-events: Add pmu_events_table__find_event perf pmu: Parse sysfs events directly from a file perf pmu: Prefer passing pmu to aliases list
perf pmu: Merge json events with sysfs at load time perf pmu: Cache json events table
perf pmu: Lazily add json events
perf pmu: Scan type early to fail an invalid PMU quickly perf pmu: Be lazy about loading event info files from sysfs perf pmu: Lazily load sysfs aliases
perf jevents: Sort strings in the big C string to reduce faults
tools/perf/arch/x86/util/intel-pt.c | 39 +- tools/perf/bench/pmu-scan.c | 8 +- tools/perf/pmu-events/empty-pmu-events.c | 49 +- tools/perf/pmu-events/jevents.py | 319 +++++++-- tools/perf/pmu-events/pmu-events.h | 15 +- tools/perf/tests/parse-events.c | 2 +- tools/perf/tests/pmu-events.c | 183 ++--- tools/perf/tests/pmu.c | 76 +- tools/perf/util/amd-sample-raw.c | 1 - tools/perf/util/metricgroup.c | 10 +- tools/perf/util/parse-events.c | 91 ++- tools/perf/util/parse-events.h | 3 +- tools/perf/util/pmu.c | 872 +++++++++++++++-------- tools/perf/util/pmu.h | 110 ++- tools/perf/util/pmu.y | 32 +- tools/perf/util/pmus.c | 230 ++---- tools/perf/util/s390-sample-raw.c | 50 +- 17 files changed, 1251 insertions(+), 839 deletions(-)
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 1/25 perf script ibs: Remove unused include
Done to reduce dependencies on pmu-events.h.
--- tools/perf/util/amd-sample-raw.c | 1 - 1 file changed, 1 deletion(-)
tools/perf/util/amd-sample-raw.c +0 -1
@@ -15,7 +15,6 @@#include "session.h" #include "evlist.h" #include "sample-raw.h" -#include "pmu-events/pmu-events.h" #include "util/sample.h" static u32 cpu_family, cpu_model, ibs_fetch_type, ibs_op_type;
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 2/25 perf pmu: Avoid a path name copy
In reply to Ian RogersRather than read a base path and append into a 2nd path, read the base path directly into output buffer and append to that.
--- tools/perf/util/pmu.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
tools/perf/util/pmu.c +7 -5
@@ -1756,17 +1756,19 @@ int perf_pmu__event_source_devices_fd(void)* then pathname will be filled with * "/sys/bus/event_source/devices/cs_etm/format" * -* Return 0 if the sysfs mountpoint couldn't be found or if no -* characters were written. +* Return 0 if the sysfs mountpoint couldn't be found, if no characters were +* written or if the buffer size is exceeded. */ int perf_pmu__pathname_scnprintf (char *buf,size_t size,const char *pmu_name,const char *filename) { -char base_path[PATH_MAX ]; +size_t len; -if (!perf_pmu__event_source_devices_scnprintf (base_path,sizeof (base_path))) + len =perf_pmu__event_source_devices_scnprintf (buf, size); +if (!len || (len +strlen (pmu_name) +strlen (filename) +1 ) >= size)return 0 ; -return scnprintf (buf, size,"%s%s/%s" , base_path, pmu_name, filename); + +return scnprintf (buf + len, size - len,"%s/%s" , pmu_name, filename); }int perf_pmu__pathname_fd (int dirfd,const char *pmu_name,const char *filename,int flags)
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 3/25 perf pmu: Move perf_pmu__set_format to pmu.y
In reply to Ian RogersAvoid having the function in the C and header file, as it is only used locally by pmu.y.
--- tools/perf/util/pmu.c | 12 ------------ tools/perf/util/pmu.h | 1 - tools/perf/util/pmu.y | 12 ++++++++++++ 3 files changed, 12 insertions(+), 13 deletions(-)
tools/perf/util/pmu.c +0 -12
@@ -1417,18 +1417,6 @@ int perf_pmu__new_format(struct list_head *list, char *name,return 0 ; } -void perf_pmu__set_format (unsigned long *bits,long from,long to) -{ -long b; - -if (!to) - to = from; - -memset (bits,0 ,BITS_TO_BYTES (PERF_PMU_FORMAT_BITS )); -for (b = from; b <= to; b++) -__set_bit (b, bits); -} -void perf_pmu__del_formats (struct list_head *formats) {struct perf_pmu_format *fmt, *tmp;
tools/perf/util/pmu.h +0 -1
@@ -231,7 +231,6 @@ void perf_pmu_error(struct list_head *list, char *name, void *scanner, char consint perf_pmu__new_format (struct list_head *list,char *name,int config,unsigned long *bits); -void perf_pmu__set_format (unsigned long *bits,long from,long to);int perf_pmu__format_parse (int dirfd,struct list_head *head);void perf_pmu__del_formats (struct list_head *formats);bool perf_pmu__has_format (const struct perf_pmu *pmu,const char *name);
tools/perf/util/pmu.y +12 -0
@@ -21,6 +21,18 @@ do { \ YYABORT; \ } while (0) +static void perf_pmu__set_format(unsigned long *bits, long from, long to) +{ + long b; + + if (!to) + to = from; + + memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); + for (b = from; b <= to; b++) + __set_bit(b, bits); +} + %} %token PP_CONFIG
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 4/25 perf pmu: Reduce scope of perf_pmu_error
In reply to Ian RogersMove declaration from header file to pmu.y and make static.
--- tools/perf/util/pmu.h | 1 - tools/perf/util/pmu.y | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-)
tools/perf/util/pmu.h +0 -1
@@ -227,7 +227,6 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, struct perf_pmu_info *info);struct list_head *perf_pmu__alias (struct perf_pmu *pmu,struct list_head *head_terms); -void perf_pmu_error (struct list_head *list,char *name,void *scanner,char const *msg);int perf_pmu__new_format (struct list_head *list,char *name,int config,unsigned long *bits);
tools/perf/util/pmu.y +3 -1
@@ -21,6 +21,8 @@ do { \ YYABORT; \ } while (0) +static void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg); + static void perf_pmu__set_format(unsigned long *bits, long from, long to) { long b;
@@ -93,7 +95,7 @@ PP_VALUE %% -void perf_pmu_error(struct list_head *list __maybe_unused, +static void perf_pmu_error(struct list_head *list __maybe_unused, char *name __maybe_unused, void *scanner __maybe_unused, char const *msg __maybe_unused)
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 5/25 perf pmu: Avoid passing format list to perf_pmu__config_terms
In reply to Ian RogersAbstract the format list better, hiding it in the PMU, by changing perf_pmu__config_terms the PMU rather than the format list in the PMU. Change the PMU test to pass a dummy PMU for this purpose. Changing the test allows perf_pmu__del_formats to become static.
--- tools/perf/arch/x86/util/intel-pt.c | 30 ++++-------- tools/perf/tests/pmu.c | 76 ++++++++++++++++------------- tools/perf/util/pmu.c | 19 +++----- tools/perf/util/pmu.h | 3 +- 4 files changed, 61 insertions(+), 67 deletions(-)
tools/perf/arch/x86/util/intel-pt.c +10 -20
@@ -60,8 +60,7 @@ struct intel_pt_recording {size_t priv_size; }; -static int intel_pt_parse_terms_with_default (const char *pmu_name, -struct list_head *formats, +static int intel_pt_parse_terms_with_default (struct perf_pmu *pmu,const char *str,u64 *config) {
@@ -80,8 +79,7 @@ static int intel_pt_parse_terms_with_default(const char *pmu_name, goto out_free; attr.config = *config; - err =perf_pmu__config_terms (pmu_name, formats, &attr, terms, true, -NULL ); + err =perf_pmu__config_terms (pmu, &attr, terms,/*zero=*/ true,/*err=*/ NULL );if (err) goto out_free;
@@ -91,12 +89,10 @@ static int intel_pt_parse_terms_with_default(const char *pmu_name,return err; } -static int intel_pt_parse_terms (const char *pmu_name,struct list_head *formats, -const char *str,u64 *config) +static int intel_pt_parse_terms (struct perf_pmu *pmu,const char *str,u64 *config) { *config =0 ; -return intel_pt_parse_terms_with_default (pmu_name, formats, str, - config); +return intel_pt_parse_terms_with_default (pmu, str, config); }static u64 intel_pt_masked_bits (u64 mask,u64 bits)
@@ -236,8 +232,7 @@ 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->name, &intel_pt_pmu->format, buf, - &config); +intel_pt_parse_terms (intel_pt_pmu, buf, &config);close (dirfd);return config;
@@ -348,16 +343,12 @@ 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->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); +intel_pt_parse_terms (intel_pt_pmu,"tsc" , &tsc_bit); +intel_pt_parse_terms (intel_pt_pmu,"noretcomp" , &noretcomp_bit); +intel_pt_parse_terms (intel_pt_pmu,"mtc" , &mtc_bit); mtc_freq_bits =perf_pmu__format_bits (&intel_pt_pmu->format,"mtc_period" ); -intel_pt_parse_terms (intel_pt_pmu->name, &intel_pt_pmu->format, -"cyc" , &cyc_bit); +intel_pt_parse_terms (intel_pt_pmu,"cyc" , &cyc_bit);intel_pt_tsc_ctc_ratio (&tsc_ctc_ratio_n, &tsc_ctc_ratio_d);
@@ -781,8 +772,7 @@ static int intel_pt_recording_options(struct auxtrace_record *itr, intel_pt_evsel->core.attr.aux_watermark = aux_watermark; } -intel_pt_parse_terms (intel_pt_pmu->name, &intel_pt_pmu->format, -"tsc" , &tsc_bit); +intel_pt_parse_terms (intel_pt_pmu,"tsc" , &tsc_bit);if (opts->full_auxtrace && (intel_pt_evsel->core.attr.config & tsc_bit)) have_timing_info = true;
tools/perf/tests/pmu.c +42 -34
96 lines not on this page · raw message
tools/perf/util/pmu.c +8 -11
@@ -1135,8 +1135,7 @@ 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(const char *pmu_name, - struct list_head *formats, +static int pmu_config_term(struct perf_pmu *pmu, struct perf_event_attr *attr, struct parse_events_term *term, struct list_head *head_terms,
@@ -1160,15 +1159,15 @@ static int pmu_config_term(const char *pmu_name,if (parse_events__is_hardcoded_term (term))return 0 ; - format =pmu_find_format (formats, term->config); + format =pmu_find_format (&pmu->format, term->config);if (!format) { -char *pmu_term =pmu_formats_string (formats); +char *pmu_term =pmu_formats_string (&pmu->format);char *unknown_term;char *help_msg;if (asprintf (&unknown_term,"unknown term '%s' for pmu '%s'" , - term->config, pmu_name) <0 ) + term->config, pmu->name) <0 ) unknown_term =NULL ; help_msg =parse_events_formats_error_string (pmu_term);if (err) {
@@ -1259,7 +1258,7 @@ static int pmu_config_term(const char *pmu_name,return 0 ; } -int perf_pmu__config_terms (const char *pmu_name,struct list_head *formats, +int perf_pmu__config_terms (struct perf_pmu *pmu,struct perf_event_attr *attr,struct list_head *head_terms,bool zero,struct parse_events_error *err)
@@ -1267,8 +1266,7 @@ int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,struct parse_events_term *term;list_for_each_entry (term, head_terms, list) { -if (pmu_config_term (pmu_name, formats, attr, term, head_terms, - zero, err)) +if (pmu_config_term (pmu, attr, term, head_terms, zero, err))return -EINVAL ; }
@@ -1286,8 +1284,7 @@ int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, {bool zero = !!pmu->default_config; -return perf_pmu__config_terms (pmu->name, &pmu->format, attr, - head_terms, zero, err); +return perf_pmu__config_terms (pmu, attr, head_terms, zero, err); } static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
@@ -1417,7 +1414,7 @@ int perf_pmu__new_format(struct list_head *list, char *name,return 0 ; } -void perf_pmu__del_formats (struct list_head *formats) +static void perf_pmu__del_formats (struct list_head *formats) {struct perf_pmu_format *fmt, *tmp;
tools/perf/util/pmu.h +1 -2
@@ -217,7 +217,7 @@ void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu);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 (const char *pmu_name,struct list_head *formats, +int perf_pmu__config_terms (struct perf_pmu *pmu,struct perf_event_attr *attr,struct list_head *head_terms,bool zero,struct parse_events_error *error);
@@ -231,7 +231,6 @@ struct list_head *perf_pmu__alias(struct perf_pmu *pmu,int perf_pmu__new_format (struct list_head *list,char *name,int config,unsigned long *bits);int perf_pmu__format_parse (int dirfd,struct list_head *head); -void perf_pmu__del_formats (struct list_head *formats);bool perf_pmu__has_format (const struct perf_pmu *pmu,const char *name);bool is_pmu_core (const char *name);
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 6/25 perf pmu: Avoid passing format list to perf_pmu__format_type
In reply to Ian RogersPass the pmu so the format list can be better abstracted and later lazily loaded.
--- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 4 ++-- tools/perf/util/pmu.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
tools/perf/util/parse-events.c +1 -1
@@ -1159,7 +1159,7 @@ static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,list_for_each_entry (term, head_config, list) {switch (term->type_term) {case PARSE_EVENTS__TERM_TYPE_USER : - type =perf_pmu__format_type (&pmu->format, term->config); + type =perf_pmu__format_type (pmu, term->config);if (type !=PERF_PMU_FORMAT_VALUE_CONFIG )continue ; bits |=perf_pmu__format_bits (&pmu->format, term->config);
tools/perf/util/pmu.c +2 -2
@@ -1038,9 +1038,9 @@ __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)return bits; } -int perf_pmu__format_type (struct list_head *formats,const char *name) +int perf_pmu__format_type (struct perf_pmu *pmu,const char *name) { -struct perf_pmu_format *format =pmu_find_format (formats, name); +struct perf_pmu_format *format =pmu_find_format (&pmu->format, name);if (!format)return -1 ;
tools/perf/util/pmu.h +1 -1
@@ -222,7 +222,7 @@ int perf_pmu__config_terms(struct perf_pmu *pmu,struct list_head *head_terms, bool zero, struct parse_events_error *error);__u64 perf_pmu__format_bits (struct list_head *formats,const char *name); -int perf_pmu__format_type (struct list_head *formats,const char *name); +int perf_pmu__format_type (struct perf_pmu *pmu,const char *name);int perf_pmu__check_alias (struct perf_pmu *pmu,struct list_head *head_terms,struct perf_pmu_info *info); struct list_head *perf_pmu__alias(struct perf_pmu *pmu,
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 7/25 perf pmu: Avoid passing format list to perf_pmu__format_bits
In reply to Ian RogersPass the pmu so the format list can be better abstracted and later lazily loaded.
--- tools/perf/arch/x86/util/intel-pt.c | 7 +++---- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 6 +++--- tools/perf/util/pmu.h | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-)
tools/perf/arch/x86/util/intel-pt.c +3 -4
@@ -122,7 +122,7 @@ static int intel_pt_read_config(struct perf_pmu *intel_pt_pmu, const char *str, *res =0 ; - mask =perf_pmu__format_bits (&intel_pt_pmu->format, str); + mask =perf_pmu__format_bits (intel_pt_pmu, str);if (!mask)return -EINVAL ;
@@ -346,8 +346,7 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,intel_pt_parse_terms (intel_pt_pmu,"tsc" , &tsc_bit);intel_pt_parse_terms (intel_pt_pmu,"noretcomp" , &noretcomp_bit);intel_pt_parse_terms (intel_pt_pmu,"mtc" , &mtc_bit); - mtc_freq_bits =perf_pmu__format_bits (&intel_pt_pmu->format, -"mtc_period" ); + mtc_freq_bits =perf_pmu__format_bits (intel_pt_pmu,"mtc_period" );intel_pt_parse_terms (intel_pt_pmu,"cyc" , &cyc_bit);intel_pt_tsc_ctc_ratio (&tsc_ctc_ratio_n, &tsc_ctc_ratio_d);
@@ -502,7 +501,7 @@ static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu, int dirfd, valid |=1 ; - bits =perf_pmu__format_bits (&intel_pt_pmu->format, name); + bits =perf_pmu__format_bits (intel_pt_pmu, name); config &= bits;
tools/perf/util/parse-events.c +1 -1
@@ -1162,7 +1162,7 @@ static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config, type =perf_pmu__format_type (pmu, term->config);if (type !=PERF_PMU_FORMAT_VALUE_CONFIG )continue ; - bits |=perf_pmu__format_bits (&pmu->format, term->config); + bits |=perf_pmu__format_bits (pmu, term->config);break ;case PARSE_EVENTS__TERM_TYPE_CONFIG : bits = ~(u64 )0 ;
tools/perf/util/pmu.c +3 -3
@@ -1000,7 +1000,7 @@ void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,if (term) user_bits = term->val.cfg_chg; - bits =perf_pmu__format_bits (&pmu->format, config_name); + bits =perf_pmu__format_bits (pmu, config_name);/* Do nothing if the user changed the value */ if (bits & user_bits)
@@ -1023,9 +1023,9 @@ pmu_find_format(struct list_head *formats, const char *name)return NULL ; } -__u64 perf_pmu__format_bits (struct list_head *formats,const char *name) +__u64 perf_pmu__format_bits (struct perf_pmu *pmu,const char *name) { -struct perf_pmu_format *format =pmu_find_format (formats, name); +struct perf_pmu_format *format =pmu_find_format (&pmu->format, name);__u64 bits =0 ;int fbit;
tools/perf/util/pmu.h +1 -1
@@ -221,7 +221,7 @@ int perf_pmu__config_terms(struct perf_pmu *pmu,struct perf_event_attr *attr, struct list_head *head_terms, bool zero, struct parse_events_error *error); -__u64 perf_pmu__format_bits (struct list_head *formats,const char *name); +__u64 perf_pmu__format_bits (struct perf_pmu *pmu,const char *name);int perf_pmu__format_type (struct perf_pmu *pmu,const char *name);int perf_pmu__check_alias (struct perf_pmu *pmu,struct list_head *head_terms,struct perf_pmu_info *info);
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 8/25 perf pmu: Pass PMU rather than aliases and format
In reply to Ian RogersPass the pmu so the aliases and format list can be better abstracted and later lazily loaded.
--- tools/perf/tests/pmu-events.c | 47 ++++++++++-------------- tools/perf/tests/pmu.c | 2 +- tools/perf/util/pmu.c | 69 +++++++++++++++++++---------------- tools/perf/util/pmu.h | 9 ++--- 4 files changed, 62 insertions(+), 65 deletions(-)
tools/perf/tests/pmu-events.c +19 -28
@@ -496,26 +496,13 @@ static int test__pmu_event_table(struct test_suite *test __maybe_unused,return 0 ; } -static struct perf_pmu_alias *find_alias (const char *test_event,struct list_head *aliases) -{ -struct perf_pmu_alias *alias; - -list_for_each_entry (alias, aliases, list) -if (!strcmp (test_event, alias->name)) -return alias; - -return NULL ; -} -/* Verify aliases are as expected */ static int __test_core_pmu_event_aliases (char *pmu_name,int *count) {struct perf_pmu_test_event const **test_event_table;struct perf_pmu *pmu; -LIST_HEAD (aliases);int res =0 ;const struct pmu_events_table *table =find_core_events_table ("testarch" ,"testcpu" ); -struct perf_pmu_alias *a, *tmp;if (!table)return -1 ;
@@ -526,14 +513,18 @@ static int __test_core_pmu_event_aliases(char *pmu_name, int *count)if (!pmu)return -1 ; - pmu->name = pmu_name; +INIT_LIST_HEAD (&pmu->format); +INIT_LIST_HEAD (&pmu->aliases); +INIT_LIST_HEAD (&pmu->caps); +INIT_LIST_HEAD (&pmu->list); + pmu->name =strdup (pmu_name); -pmu_add_cpu_aliases_table (&aliases, pmu, table); +pmu_add_cpu_aliases_table (pmu, table);for (; *test_event_table; test_event_table++) {struct perf_pmu_test_event const *test_event = *test_event_table;struct pmu_event const *event = &test_event->event; -struct perf_pmu_alias *alias =find_alias (event->name, &aliases); +struct perf_pmu_alias *alias =perf_pmu__find_alias (pmu, event->name);if (!alias) { pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
@@ -551,12 +542,8 @@ static int __test_core_pmu_event_aliases(char *pmu_name, int *count)pr_debug2 ("testing aliases core PMU %s: matched event %s\n" , pmu_name, alias->name); } +perf_pmu__delete (pmu); -list_for_each_entry_safe (a, tmp, &aliases, list) { -list_del (&a->list); -perf_pmu_free_alias (a); - } -free (pmu);return res; }
@@ -568,17 +555,16 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)const char *pmu_name = pmu->name;struct perf_pmu_alias *a, *tmp, *alias;const struct pmu_events_table *events_table; -LIST_HEAD (aliases);int res =0 ; events_table =find_core_events_table ("testarch" ,"testcpu" );if (!events_table)return -1 ; -pmu_add_cpu_aliases_table (&aliases, pmu, events_table); -pmu_add_sys_aliases (&aliases, pmu); +pmu_add_cpu_aliases_table (pmu, events_table); +pmu_add_sys_aliases (pmu);/* Count how many aliases we generated */ -list_for_each_entry (alias, &aliases, list) +list_for_each_entry (alias, &pmu->aliases, list) alias_count++;/* Count how many aliases we expect from the known table */
@@ -592,7 +578,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu) goto out; } -list_for_each_entry (alias, &aliases, list) { +list_for_each_entry (alias, &pmu->aliases, list) {bool matched = false;for (table = &test_pmu->aliases[0 ]; *table; table++) {
@@ -625,7 +611,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu) } out: -list_for_each_entry_safe (a, tmp, &aliases, list) { +list_for_each_entry_safe (a, tmp, &pmu->aliases, list) {list_del (&a->list);perf_pmu_free_alias (a); }
@@ -732,8 +718,13 @@ static int test__aliases(struct test_suite *test __maybe_unused, }for (i =0 ; i <ARRAY_SIZE (test_pmus); i++) { -int res =__test_uncore_pmu_event_aliases (&test_pmus[i]); +int res; + +INIT_LIST_HEAD (&test_pmus[i].pmu.format); +INIT_LIST_HEAD (&test_pmus[i].pmu.aliases); +INIT_LIST_HEAD (&test_pmus[i].pmu.caps); + res =__test_uncore_pmu_event_aliases (&test_pmus[i]);if (res)return res; }
tools/perf/tests/pmu.c +1 -1
@@ -171,7 +171,7 @@ static int test__pmu(struct test_suite *test __maybe_unused, int subtest __maybe } pmu->name =strdup ("perf-pmu-test" ); - ret =perf_pmu__format_parse (fd, &pmu->format); + ret =perf_pmu__format_parse (pmu, fd);if (ret) goto out;
tools/perf/util/pmu.c +38 -31
165 lines not on this page · raw message
tools/perf/util/pmu.h +4 -5
@@ -213,7 +213,7 @@ struct perf_pmu_alias {char *pmu_name; }; -void pmu_add_sys_aliases (struct list_head *head,struct perf_pmu *pmu); +void pmu_add_sys_aliases (struct perf_pmu *pmu);int perf_pmu__config (struct perf_pmu *pmu,struct perf_event_attr *attr,struct list_head *head_terms,struct parse_events_error *error);
@@ -225,12 +225,11 @@ __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);int perf_pmu__format_type (struct perf_pmu *pmu,const char *name);int perf_pmu__check_alias (struct perf_pmu *pmu,struct list_head *head_terms,struct perf_pmu_info *info); -struct list_head *perf_pmu__alias (struct perf_pmu *pmu, -struct list_head *head_terms); +struct perf_pmu_alias *perf_pmu__find_alias (struct perf_pmu *pmu,const char *event);int perf_pmu__new_format (struct list_head *list,char *name,int config,unsigned long *bits); -int perf_pmu__format_parse (int dirfd,struct list_head *head); +int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd);bool perf_pmu__has_format (const struct perf_pmu *pmu,const char *name);bool is_pmu_core (const char *name);
@@ -255,7 +254,7 @@ bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name);int perf_pmu__test (void );struct perf_event_attr *perf_pmu__get_default_config (struct perf_pmu *pmu); -void pmu_add_cpu_aliases_table (struct list_head *head,struct perf_pmu *pmu, +void pmu_add_cpu_aliases_table (struct perf_pmu *pmu,const struct pmu_events_table *table);char *perf_pmu__getcpuid (struct perf_pmu *pmu);
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 9/25 perf pmu: Make the loading of formats lazy
In reply to Ian RogersThe sysfs format files are loaded eagerly in a PMU. Add a flag so that we create the format but only load the contents when necessary.
Reduce the size of the value in struct perf_pmu_format and avoid holes so there is no additional space requirement.
For "perf stat -e cycles true" this reduces the number of openat calls from 648 to 573 (about 12%). The benchmark pmu scan speed is improved by roughly 5%.
Before: $ perf bench internals pmu-scan
Computing performance of sysfs PMU event scan for 100 times
Average core PMU scanning took: 1061.100 usec (+- 9.965 usec) Average PMU scanning took: 4725.300 usec (+- 260.599 usec)
After: $ perf bench internals pmu-scan
Computing performance of sysfs PMU event scan for 100 times
Average core PMU scanning took: 989.170 usec (+- 6.873 usec) Average PMU scanning took: 4520.960 usec (+- 251.272 usec)
--- tools/perf/tests/pmu.c | 2 +- tools/perf/util/pmu.c | 140 +++++++++++++++++++++++++++-------------- tools/perf/util/pmu.h | 5 +- tools/perf/util/pmu.y | 20 +++--- 4 files changed, 102 insertions(+), 65 deletions(-)
tools/perf/tests/pmu.c +1 -1
@@ -171,7 +171,7 @@ static int test__pmu(struct test_suite *test __maybe_unused, int subtest __maybe } pmu->name =strdup ("perf-pmu-test" ); - ret =perf_pmu__format_parse (pmu, fd); + ret =perf_pmu__format_parse (pmu, fd,/*eager_load=*/ true);if (ret) goto out;
tools/perf/util/pmu.c +92 -48
210 lines not on this page · raw message
tools/perf/util/pmu.h +2 -3
@@ -227,9 +227,8 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, struct perf_pmu_info *info);struct perf_pmu_alias *perf_pmu__find_alias (struct perf_pmu *pmu,const char *event); -int perf_pmu__new_format (struct list_head *list,char *name, -int config,unsigned long *bits); -int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd); +int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd,bool eager_load); +void perf_pmu_format__set_value (void *format,int config,unsigned long *bits);bool perf_pmu__has_format (const struct perf_pmu *pmu,const char *name);bool is_pmu_core (const char *name);
tools/perf/util/pmu.y +7 -13
@@ -1,6 +1,5 @@ %define api.pure full -%parse-param {struct list_head *format} -%parse-param {char *name} +%parse-param {void *format} %parse-param {void *scanner} %lex-param {void* scanner}
@@ -21,7 +20,7 @@ do { \ YYABORT; \ } while (0) -static void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg); +static void perf_pmu_error(void *format, void *scanner, const char *msg); static void perf_pmu__set_format(unsigned long *bits, long from, long to) {
@@ -59,16 +58,12 @@ format_term format_term: PP_CONFIG ':' bits { - ABORT_ON(perf_pmu__new_format(format, name, - PERF_PMU_FORMAT_VALUE_CONFIG, - $3)); + perf_pmu_format__set_value(format, PERF_PMU_FORMAT_VALUE_CONFIG, $3); } | PP_CONFIG PP_VALUE ':' bits { - ABORT_ON(perf_pmu__new_format(format, name, - $2, - $4)); + perf_pmu_format__set_value(format, $2, $4); } bits:
@@ -95,9 +90,8 @@ PP_VALUE %% -static void perf_pmu_error(struct list_head *list __maybe_unused, - char *name __maybe_unused, - void *scanner __maybe_unused, - char const *msg __maybe_unused) +static void perf_pmu_error(void *format __maybe_unused, + void *scanner __maybe_unused, + const char *msg __maybe_unused) { }
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v1 09/25] perf pmu: Make the loading of formats lazy
Em Wed, Aug 23, 2023 at 01:08:12AM -0700, Ian Rogers escreveu:
Quoted 69 lines
The sysfs format files are loaded eagerly in a PMU. Add a flag so that we create the format but only load the contents when necessary.
Reduce the size of the value in struct
perf_pmu_formatand avoid holes so there is no additional space requirement.For "perf stat -e cycles true" this reduces the number of openat calls from 648 to 573 (about 12%). The benchmark pmu scan speed is improved by roughly 5%.
Before: $ perf bench internals pmu-scan
Computing performance of sysfs PMU event scan for 100 times
Average core PMU scanning took: 1061.100 usec (+- 9.965 usec) Average PMU scanning took: 4725.300 usec (+- 260.599 usec)After: $ perf bench internals pmu-scan
Computing performance of sysfs PMU event scan for 100 times
Average core PMU scanning took: 989.170 usec (+- 6.873 usec) Average PMU scanning took: 4520.960 usec (+- 251.272 usec)Signed-off-by Ian Rogers <[email protected]>--- tools/perf/tests/pmu.c | 2 +- tools/perf/util/pmu.c | 140 +++++++++++++++++++++++++++-------------- tools/perf/util/pmu.h | 5 +- tools/perf/util/pmu.y | 20 +++--- 4 files changed, 102 insertions(+), 65 deletions(-)tools/perf/tests/pmu.c +1
-1@@ -171,7 +171,7 @@ static int test__pmu(struct test_suite *test __maybe_unused, int subtest __maybe } pmu->name =strdup ("perf-pmu-test" ); - ret =perf_pmu__format_parse (pmu, fd); + ret =perf_pmu__format_parse (pmu, fd,/*eager_load=*/ true);if (ret) goto out;tools/perf/util/pmu.c +7
-5@@ -40,6 +40,10 @@ struct perf_pmu perf_pmu__fake;* value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. */ struct perf_pmu_format { +/** @list: Element on list within struct perf_pmu. */ +struct list_head list; +/** @bits: Which config bits are set by this format value. */ + DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);/** @name: The modifier/file name. */ char *name;/** @@ -47,18 +51,75 @@ struct perf_pmu_format {* are from PERF_PMU_FORMAT_VALUE_CONFIG to * PERF_PMU_FORMAT_VALUE_CONFIG_END. */ -int value; -/** @bits: Which config bits are set by this format value. */ -DECLARE_BITMAP (bits,PERF_PMU_FORMAT_BITS ); -/** @list: Element on list within struct perf_pmu. */ -struct list_head list; +u16 value; +/** @loaded: Has the contents been loaded/parsed. */ +bool loaded; };
tools/perf/util/pmu.c struct perf_pmu_format { · line 67
Quoted 10 lines
tools/perf/util/pmu.c +9
-0+static struct perf_pmu_format *perf_pmu__new_format (struct list_head *list,char *name) +{ +struct perf_pmu_format *format; + + format =zalloc (sizeof (*format)); +if (!format) +return NULL ; + + format->name =strdup (name);
We need to check this for failure, later you assume it is non-NULL, calling strlen() on it, etc.
I applied all the previous patches, will review the others later.
- Arnaldo
Quoted 256 lines
tools/perf/util/pmu.c +76
-43+list_add_tail (&format->list, list); +return format; +} + +/* Called at the end of parsing a format. */ +void perf_pmu_format__set_value (void *vformat,int config,unsigned long *bits) +{ +struct perf_pmu_format *format = vformat; + + format->value = config; +memcpy (format->bits, bits,sizeof (format->bits)); +} + +static void __perf_pmu_format__load (struct perf_pmu_format *format,FILE *file) +{ +void *scanner; +int ret; + + ret =perf_pmu_lex_init (&scanner); +if (ret) +return ; + +perf_pmu_set_in (file, scanner); + ret =perf_pmu_parse (format, scanner); +perf_pmu_lex_destroy (scanner); + format->loaded = true; +} + +static void perf_pmu_format__load (struct perf_pmu *pmu,struct perf_pmu_format *format) +{ +char path[PATH_MAX ]; +FILE *file =NULL ; + +if (format->loaded) +return ; + +if (!perf_pmu__pathname_scnprintf (path,sizeof (path), pmu->name,"format" )) +return ; + +assert (strlen (path) +strlen (format->name) +2 <sizeof (path)); +strcat (path,"/" ); +strcat (path, format->name); + + file =fopen (path,"r" ); +if (!file) +return ; +__perf_pmu_format__load (format, file); +fclose (file); +} +/* * Parse & process all the sysfs attributes located under * the directory specified in 'dir' parameter. */ -int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd) +int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd,bool eager_load) {struct dirent *evt_ent;DIR *format_dir;@@ -68,37 +129,35 @@ int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd)if (!format_dir)return -EINVAL ; -while (!ret && (evt_ent =readdir (format_dir))) { +while ((evt_ent =readdir (format_dir)) !=NULL ) { +struct perf_pmu_format *format;char *name = evt_ent->d_name; -int fd; -void *scanner; -FILE *file;if (!strcmp (name,"." ) || !strcmp (name,".." ))continue ; - - ret = -EINVAL ; - fd =openat (dirfd, name,O_RDONLY ); -if (fd <0 ) -break ; - - file =fdopen (fd,"r" ); -if (!file) { -close (fd); + format =perf_pmu__new_format (&pmu->format, name); +if (!format) { + ret = -ENOMEM ;break ; } - ret =perf_pmu_lex_init (&scanner); -if (ret) { +if (eager_load) { +FILE *file; +int fd =openat (dirfd, name,O_RDONLY ); + +if (fd <0 ) { + ret = -errno; +break ; + } + file =fdopen (fd,"r" ); +if (!file) { +close (fd); +break ; + } +__perf_pmu_format__load (format, file);fclose (file); -break ; } - -perf_pmu_set_in (file, scanner); - ret =perf_pmu_parse (&pmu->format, name, scanner); -perf_pmu_lex_destroy (scanner); -fclose (file); }closedir (format_dir);@@ -119,7 +178,7 @@ static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)return 0 ;/* it'll close the fd */ -if (perf_pmu__format_parse (pmu, fd)) +if (perf_pmu__format_parse (pmu, fd,/*eager_load=*/ false))return -1 ;return 0 ;@@ -962,13 +1021,15 @@ void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)if (pmu == &perf_pmu__fake)return ; -list_for_each_entry (format, &pmu->format, list) +list_for_each_entry (format, &pmu->format, list) { +perf_pmu_format__load (pmu, format);if (format->value >=PERF_PMU_FORMAT_VALUE_CONFIG_END ) {pr_warning ("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'" "which is not supported by this version of perf!\n" , pmu->name, format->name, format->value);return ; } + } }bool evsel__is_aux_event (const struct evsel *evsel)@@ -1041,6 +1102,7 @@ int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)if (!format)return -1 ; +perf_pmu_format__load (pmu, format);return format->value; }@@ -1177,7 +1239,7 @@ static int pmu_config_term(struct perf_pmu *pmu,free (pmu_term);return -EINVAL ; } - +perf_pmu_format__load (pmu, format);switch (format->value) {case PERF_PMU_FORMAT_VALUE_CONFIG : vp = &attr->config;@@ -1403,24 +1465,6 @@ struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, const char *evreturn NULL ; } - -int perf_pmu__new_format (struct list_head *list,char *name, -int config,unsigned long *bits) -{ -struct perf_pmu_format *format; - - format =zalloc (sizeof (*format)); -if (!format) -return -ENOMEM ; - - format->name =strdup (name); - format->value = config; -memcpy (format->bits, bits,sizeof (format->bits)); - -list_add_tail (&format->list, list); -return 0 ; -} -static void perf_pmu__del_formats (struct list_head *formats) {struct perf_pmu_format *fmt, *tmp;tools/perf/util/pmu.h +2
-3@@ -227,9 +227,8 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, struct perf_pmu_info *info);struct perf_pmu_alias *perf_pmu__find_alias (struct perf_pmu *pmu,const char *event); -int perf_pmu__new_format (struct list_head *list,char *name, -int config,unsigned long *bits); -int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd); +int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd,bool eager_load); +void perf_pmu_format__set_value (void *format,int config,unsigned long *bits);bool perf_pmu__has_format (const struct perf_pmu *pmu,const char *name);bool is_pmu_core (const char *name);tools/perf/util/pmu.y +7
-13@@ -1,6 +1,5 @@ %define api.pure full -%parse-param {struct list_head *format} -%parse-param {char *name} +%parse-param {void *format} %parse-param {void *scanner} %lex-param {void* scanner}@@ -21,7 +20,7 @@ do { \ YYABORT; \ } while (0) -static void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg); +static void perf_pmu_error(void *format, void *scanner, const char *msg); static void perf_pmu__set_format(unsigned long *bits, long from, long to) {@@ -59,16 +58,12 @@ format_term format_term: PP_CONFIG ':' bits { - ABORT_ON(perf_pmu__new_format(format, name, - PERF_PMU_FORMAT_VALUE_CONFIG, - $3)); + perf_pmu_format__set_value(format, PERF_PMU_FORMAT_VALUE_CONFIG, $3); } | PP_CONFIG PP_VALUE ':' bits { - ABORT_ON(perf_pmu__new_format(format, name, - $2, - $4)); + perf_pmu_format__set_value(format, $2, $4); } bits:@@ -95,9 +90,8 @@ PP_VALUE %% -static void perf_pmu_error(struct list_head *list __maybe_unused, - char *name __maybe_unused, - void *scanner __maybe_unused, - char const *msg __maybe_unused) +static void perf_pmu_error(void *format __maybe_unused, + void *scanner __maybe_unused, + const char *msg __maybe_unused) { }Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Signature · 1 line
- Arnaldo
Ian Rogers
reviewerRe: [PATCH v1 09/25] perf pmu: Make the loading of formats lazy
Quoted 85 lines · Arnaldo Carvalho de Melo
Em Wed, Aug 23, 2023 at 01:08:12AM -0700, Ian Rogers escreveu:
Quoted 69 lines
The sysfs format files are loaded eagerly in a PMU. Add a flag so that we create the format but only load the contents when necessary.
Reduce the size of the value in struct
perf_pmu_formatand avoid holes so there is no additional space requirement.For "perf stat -e cycles true" this reduces the number of openat calls from 648 to 573 (about 12%). The benchmark pmu scan speed is improved by roughly 5%.
Before: $ perf bench internals pmu-scan
Computing performance of sysfs PMU event scan for 100 times
Average core PMU scanning took: 1061.100 usec (+- 9.965 usec) Average PMU scanning took: 4725.300 usec (+- 260.599 usec)After: $ perf bench internals pmu-scan
Computing performance of sysfs PMU event scan for 100 times
Average core PMU scanning took: 989.170 usec (+- 6.873 usec) Average PMU scanning took: 4520.960 usec (+- 251.272 usec)Signed-off-by Ian Rogers <[email protected]>--- tools/perf/tests/pmu.c | 2 +- tools/perf/util/pmu.c | 140 +++++++++++++++++++++++++++-------------- tools/perf/util/pmu.h | 5 +- tools/perf/util/pmu.y | 20 +++--- 4 files changed, 102 insertions(+), 65 deletions(-)tools/perf/tests/pmu.c +1
-1@@ -171,7 +171,7 @@ static int test__pmu(struct test_suite *test __maybe_unused, int subtest __maybe } pmu->name =strdup ("perf-pmu-test" ); - ret =perf_pmu__format_parse (pmu, fd); + ret =perf_pmu__format_parse (pmu, fd,/*eager_load=*/ true);if (ret) goto out;tools/perf/util/pmu.c +7
-5@@ -40,6 +40,10 @@ struct perf_pmu perf_pmu__fake;* value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. */ struct perf_pmu_format { +/** @list: Element on list within struct perf_pmu. */ +struct list_head list; +/** @bits: Which config bits are set by this format value. */ + DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);/** @name: The modifier/file name. */ char *name;/** @@ -47,18 +51,75 @@ struct perf_pmu_format {* are from PERF_PMU_FORMAT_VALUE_CONFIG to * PERF_PMU_FORMAT_VALUE_CONFIG_END. */ -int value; -/** @bits: Which config bits are set by this format value. */ -DECLARE_BITMAP (bits,PERF_PMU_FORMAT_BITS ); -/** @list: Element on list within struct perf_pmu. */ -struct list_head list; +u16 value; +/** @loaded: Has the contents been loaded/parsed. */ +bool loaded; };tools/perf/util/pmu.c struct perf_pmu_format { · line 67
Quoted 10 lines
tools/perf/util/pmu.c +9
-0+static struct perf_pmu_format *perf_pmu__new_format (struct list_head *list,char *name) +{ +struct perf_pmu_format *format; + + format =zalloc (sizeof (*format)); +if (!format) +return NULL ; + + format->name =strdup (name);We need to check this for failure, later you assume it is non-NULL, calling
strlen()on it, etc.
Quoted 1 line
I applied all the previous patches, will review the others later.
Thanks, Arnaldo I'll fix this and rebase on to tmp.perf-tools-next for v2.
Quoted 163 lines
- Arnaldo
Quoted 161 lines
tools/perf/util/pmu.c +76
-25+list_add_tail (&format->list, list); +return format; +} + +/* Called at the end of parsing a format. */ +void perf_pmu_format__set_value (void *vformat,int config,unsigned long *bits) +{ +struct perf_pmu_format *format = vformat; + + format->value = config; +memcpy (format->bits, bits,sizeof (format->bits)); +} + +static void __perf_pmu_format__load (struct perf_pmu_format *format,FILE *file) +{ +void *scanner; +int ret; + + ret =perf_pmu_lex_init (&scanner); +if (ret) +return ; + +perf_pmu_set_in (file, scanner); + ret =perf_pmu_parse (format, scanner); +perf_pmu_lex_destroy (scanner); + format->loaded = true; +} + +static void perf_pmu_format__load (struct perf_pmu *pmu,struct perf_pmu_format *format) +{ +char path[PATH_MAX ]; +FILE *file =NULL ; + +if (format->loaded) +return ; + +if (!perf_pmu__pathname_scnprintf (path,sizeof (path), pmu->name,"format" )) +return ; + +assert (strlen (path) +strlen (format->name) +2 <sizeof (path)); +strcat (path,"/" ); +strcat (path, format->name); + + file =fopen (path,"r" ); +if (!file) +return ; +__perf_pmu_format__load (format, file); +fclose (file); +} +/* * Parse & process all the sysfs attributes located under * the directory specified in 'dir' parameter. */ -int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd) +int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd,bool eager_load) {struct dirent *evt_ent;DIR *format_dir;@@ -68,37 +129,35 @@ int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd)if (!format_dir)return -EINVAL ; -while (!ret && (evt_ent =readdir (format_dir))) { +while ((evt_ent =readdir (format_dir)) !=NULL ) { +struct perf_pmu_format *format;char *name = evt_ent->d_name; -int fd; -void *scanner; -FILE *file;if (!strcmp (name,"." ) || !strcmp (name,".." ))continue ; - - ret = -EINVAL ; - fd =openat (dirfd, name,O_RDONLY ); -if (fd <0 ) -break ; - - file =fdopen (fd,"r" ); -if (!file) { -close (fd); + format =perf_pmu__new_format (&pmu->format, name); +if (!format) { + ret = -ENOMEM ;break ; } - ret =perf_pmu_lex_init (&scanner); -if (ret) { +if (eager_load) { +FILE *file; +int fd =openat (dirfd, name,O_RDONLY ); + +if (fd <0 ) { + ret = -errno; +break ; + } + file =fdopen (fd,"r" ); +if (!file) { +close (fd); +break ; + } +__perf_pmu_format__load (format, file);fclose (file); -break ; } - -perf_pmu_set_in (file, scanner); - ret =perf_pmu_parse (&pmu->format, name, scanner); -perf_pmu_lex_destroy (scanner); -fclose (file); }closedir (format_dir);@@ -119,7 +178,7 @@ static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)return 0 ;/* it'll close the fd */ -if (perf_pmu__format_parse (pmu, fd)) +if (perf_pmu__format_parse (pmu, fd,/*eager_load=*/ false))return -1 ;return 0 ;@@ -962,13 +1021,15 @@ void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)if (pmu == &perf_pmu__fake)return ; -list_for_each_entry (format, &pmu->format, list) +list_for_each_entry (format, &pmu->format, list) { +perf_pmu_format__load (pmu, format);if (format->value >=PERF_PMU_FORMAT_VALUE_CONFIG_END ) {pr_warning ("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'" "which is not supported by this version of perf!\n" , pmu->name, format->name, format->value);return ; } + } }bool evsel__is_aux_event (const struct evsel *evsel)@@ -1041,6 +1102,7 @@ int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)if (!format)return -1 ; +perf_pmu_format__load (pmu, format);return format->value; }@@ -1177,7 +1239,7 @@ static int pmu_config_term(struct perf_pmu *pmu,free (pmu_term);return -EINVAL ; } - +perf_pmu_format__load (pmu, format);switch (format->value) {case PERF_PMU_FORMAT_VALUE_CONFIG : vp = &attr->config;@@ -1403,24 +1465,6 @@ struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, const char *ev
Quoted 13 lines
tools/perf/util/pmu.c struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, const char *ev · line 1416
Quoted 13 lines
tools/perf/util/pmu.c +0
-11return NULL ; } - -int perf_pmu__new_format (struct list_head *list,char *name, -int config,unsigned long *bits) -{ -struct perf_pmu_format *format; - - format =zalloc (sizeof (*format)); -if (!format) -return -ENOMEM ; - - format->name =strdup (name);
The unchecked strdup was a copy-and-paste from here.
Thanks,
Ian
Quoted 86 lines
Quoted 80 lines
tools/perf/util/pmu.c +0
-7- format->value = config; -memcpy (format->bits, bits,sizeof (format->bits)); - -list_add_tail (&format->list, list); -return 0 ; -} -static void perf_pmu__del_formats (struct list_head *formats) {struct perf_pmu_format *fmt, *tmp;tools/perf/util/pmu.h +2
-3@@ -227,9 +227,8 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, struct perf_pmu_info *info);struct perf_pmu_alias *perf_pmu__find_alias (struct perf_pmu *pmu,const char *event); -int perf_pmu__new_format (struct list_head *list,char *name, -int config,unsigned long *bits); -int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd); +int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd,bool eager_load); +void perf_pmu_format__set_value (void *format,int config,unsigned long *bits);bool perf_pmu__has_format (const struct perf_pmu *pmu,const char *name);bool is_pmu_core (const char *name);tools/perf/util/pmu.y +7
-14@@ -1,6 +1,5 @@ %define api.pure full -%parse-param {struct list_head *format} -%parse-param {char *name} +%parse-param {void *format} %parse-param {void *scanner} %lex-param {void* scanner}@@ -21,7 +20,7 @@ do { \ YYABORT; \ } while (0) -static void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg); +static void perf_pmu_error(void *format, void *scanner, const char *msg); static void perf_pmu__set_format(unsigned long *bits, long from, long to) {@@ -59,16 +58,12 @@ format_term format_term: PP_CONFIG ':' bits { - ABORT_ON(perf_pmu__new_format(format, name, - PERF_PMU_FORMAT_VALUE_CONFIG, - $3)); + perf_pmu_format__set_value(format, PERF_PMU_FORMAT_VALUE_CONFIG, $3); } | PP_CONFIG PP_VALUE ':' bits { - ABORT_ON(perf_pmu__new_format(format, name, - $2, - $4)); + perf_pmu_format__set_value(format, $2, $4); } bits:@@ -95,9 +90,8 @@ PP_VALUE %% -static void perf_pmu_error(struct list_head *list __maybe_unused, - char *name __maybe_unused, - void *scanner __maybe_unused, - char const *msg __maybe_unused) +static void perf_pmu_error(void *format __maybe_unused, + void *scanner __maybe_unused, + const char *msg __maybe_unused) { } --Quoted 2 lines
2.42.0.rc1.204.g551eb34607-goog
--
- Arnaldo
Ian Rogers
reviewerPATCH 10/25 perf pmu: Abstract alias/event struct
In reply to Ian RogersIn order to be able to lazily compute aliases/events for a PMU, move the struct perf_pmu_alias into pmu.c. Add perf_pmu__find_event and perf_pmu__for_each_event that take a callback that is called for the found event or for each event. The layout of struct pmu and the event/alias list is unchanged but the API is altered so that aliases are no longer directly accessed, allowing for later changes.
--- tools/perf/bench/pmu-scan.c | 8 +- tools/perf/tests/pmu-events.c | 101 +++++++-------- tools/perf/util/parse-events.c | 67 ++++------ tools/perf/util/pmu.c | 212 +++++++++++++++++++++++++++--- tools/perf/util/pmu.h | 71 +++------- tools/perf/util/pmus.c | 230 +++++++++++---------------------- 6 files changed, 366 insertions(+), 323 deletions(-)
tools/perf/bench/pmu-scan.c +2 -6
@@ -57,9 +57,7 @@ static int save_result(void) r->is_core = pmu->is_core; r->nr_caps = pmu->nr_caps; - r->nr_aliases =0 ; -list_for_each (list, &pmu->aliases) - r->nr_aliases++; + r->nr_aliases =perf_pmu__num_events (pmu); r->nr_formats =0 ;list_for_each (list, &pmu->format)
@@ -98,9 +96,7 @@ static int check_result(bool core_only)return -1 ; } - nr =0 ; -list_for_each (list, &pmu->aliases) - nr++; + nr =perf_pmu__num_events (pmu);if (nr != r->nr_aliases) {pr_err ("Unmatched number of event aliases in %s: expect %d vs got %d\n" , pmu->name, r->nr_aliases, nr);
tools/perf/tests/pmu-events.c +48 -53
146 lines not on this page · raw message
tools/perf/util/parse-events.c +27 -40
93 lines not on this page · raw message
tools/perf/util/pmu.c +194 -18
263 lines not on this page · raw message
tools/perf/util/pmu.h +17 -54
98 lines not on this page · raw message
tools/perf/util/pmus.c +78 -152
297 lines not on this page · raw message
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 11/25 perf pmu-events: Add extra underscore to function names
In reply to Ian RogersAdd extra underscore before "for" of pmu_events_table_for_each_event and pmu_metrics_table_for_each_metric.
--- tools/perf/pmu-events/empty-pmu-events.c | 10 +++++----- tools/perf/pmu-events/jevents.py | 12 ++++++------ tools/perf/pmu-events/pmu-events.h | 4 ++-- tools/perf/tests/pmu-events.c | 4 ++-- tools/perf/util/metricgroup.c | 10 +++++----- tools/perf/util/pmu.c | 2 +- tools/perf/util/s390-sample-raw.c | 2 +- 7 files changed, 22 insertions(+), 22 deletions(-)
tools/perf/pmu-events/empty-pmu-events.c +5 -5
@@ -266,7 +266,7 @@ static const struct pmu_sys_events pmu_sys_event_tables[] = { }, }; -int pmu_events_table_for_each_event (const struct pmu_events_table *table,pmu_event_iter_fn fn, +int pmu_events_table__for_each_event (const struct pmu_events_table *table,pmu_event_iter_fn fn,void *data) {for (const struct pmu_event *pe = &table->entries[0 ]; pe->name; pe++) {
@@ -278,7 +278,7 @@ int pmu_events_table_for_each_event(const struct pmu_events_table *table, pmu_evreturn 0 ; } -int pmu_metrics_table_for_each_metric (const struct pmu_metrics_table *table,pmu_metric_iter_fn fn, +int pmu_metrics_table__for_each_metric (const struct pmu_metrics_table *table,pmu_metric_iter_fn fn,void *data) {for (const struct pmu_metric *pm = &table->entries[0 ]; pm->metric_expr; pm++) {
@@ -371,7 +371,7 @@ const struct pmu_metrics_table *find_core_metrics_table(const char *arch, constint pmu_for_each_core_event (pmu_event_iter_fn fn,void *data) {for (const struct pmu_events_map *tables = &pmu_events_map[0 ]; tables->arch; tables++) { -int ret =pmu_events_table_for_each_event (&tables->event_table, fn, data); +int ret =pmu_events_table__for_each_event (&tables->event_table, fn, data);if (ret)return ret;
@@ -384,7 +384,7 @@ int pmu_for_each_core_metric(pmu_metric_iter_fn fn, void *data)for (const struct pmu_events_map *tables = &pmu_events_map[0 ]; tables->arch; tables++) { -int ret =pmu_metrics_table_for_each_metric (&tables->metric_table, fn, data); +int ret =pmu_metrics_table__for_each_metric (&tables->metric_table, fn, data);if (ret)return ret;
@@ -408,7 +408,7 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data)for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0 ]; tables->name; tables++) { -int ret =pmu_events_table_for_each_event (&tables->table, fn, data); +int ret =pmu_events_table__for_each_event (&tables->table, fn, data);if (ret)return ret;
tools/perf/pmu-events/jevents.py +6 -6
@@ -753,7 +753,7 @@ static void decompress_metric(int offset, struct pmu_metric *pm) _args.output_file.write('\twhile (*p++);') _args.output_file.write("""} -int pmu_events_table_for_each_event(const struct pmu_events_table *table, +int pmu_events_table__for_each_event(const struct pmu_events_table *table, pmu_event_iter_fn fn, void *data) {
@@ -771,7 +771,7 @@ int pmu_events_table_for_each_event(const struct pmu_events_table *table, return 0; } -int pmu_metrics_table_for_each_metric(const struct pmu_metrics_table *table, +int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn, void *data) {
@@ -870,7 +870,7 @@ int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data) for (const struct pmu_events_map *tables = &pmu_events_map[0]; tables->arch; tables++) { - int ret = pmu_events_table_for_each_event(&tables->event_table, fn, data); + int ret = pmu_events_table__for_each_event(&tables->event_table, fn, data); if (ret) return ret;
@@ -883,7 +883,7 @@ int pmu_for_each_core_metric(pmu_metric_iter_fn fn, void *data) for (const struct pmu_events_map *tables = &pmu_events_map[0]; tables->arch; tables++) { - int ret = pmu_metrics_table_for_each_metric(&tables->metric_table, fn, data); + int ret = pmu_metrics_table__for_each_metric(&tables->metric_table, fn, data); if (ret) return ret;
@@ -907,7 +907,7 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data) for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0]; tables->name; tables++) { - int ret = pmu_events_table_for_each_event(&tables->event_table, fn, data); + int ret = pmu_events_table__for_each_event(&tables->event_table, fn, data); if (ret) return ret;
@@ -920,7 +920,7 @@ int pmu_for_each_sys_metric(pmu_metric_iter_fn fn, void *data) for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0]; tables->name; tables++) { - int ret = pmu_metrics_table_for_each_metric(&tables->metric_table, fn, data); + int ret = pmu_metrics_table__for_each_metric(&tables->metric_table, fn, data); if (ret) return ret;
tools/perf/pmu-events/pmu-events.h +2 -2
@@ -77,9 +77,9 @@ typedef int (*pmu_metric_iter_fn)(const struct pmu_metric *pm,const struct pmu_metrics_table *table, void *data); -int pmu_events_table_for_each_event (const struct pmu_events_table *table,pmu_event_iter_fn fn, +int pmu_events_table__for_each_event (const struct pmu_events_table *table,pmu_event_iter_fn fn,void *data); -int pmu_metrics_table_for_each_metric (const struct pmu_metrics_table *table,pmu_metric_iter_fn fn, +int pmu_metrics_table__for_each_metric (const struct pmu_metrics_table *table,pmu_metric_iter_fn fn,void *data);const struct pmu_events_table *perf_pmu__find_events_table (struct perf_pmu *pmu);
tools/perf/tests/pmu-events.c +2 -2
@@ -477,12 +477,12 @@ static int test__pmu_event_table(struct test_suite *test __maybe_unused,if (!table || !sys_event_table)return -1 ; - err =pmu_events_table_for_each_event (table, test__pmu_event_table_core_callback, + err =pmu_events_table__for_each_event (table, test__pmu_event_table_core_callback, &map_events);if (err)return err; - err =pmu_events_table_for_each_event (sys_event_table, test__pmu_event_table_sys_callback, + err =pmu_events_table__for_each_event (sys_event_table, test__pmu_event_table_sys_callback, &map_events);if (err)return err;
tools/perf/util/metricgroup.c +5 -5
@@ -527,7 +527,7 @@ void metricgroup__print(const struct print_callbacks *print_cb, void *print_stat groups.node_delete = mep_delete; table =pmu_metrics_table__find ();if (table) { -pmu_metrics_table_for_each_metric (table, +pmu_metrics_table__for_each_metric (table, metricgroup__add_to_mep_groups_callback, &groups); }
@@ -1069,7 +1069,7 @@ static bool metricgroup__find_metric(const char *pmu, .pm = pm, }; -return pmu_metrics_table_for_each_metric (table, metricgroup__find_metric_callback, &data) +return pmu_metrics_table__for_each_metric (table, metricgroup__find_metric_callback, &data) ? true : false; }
@@ -1255,7 +1255,7 @@ static int metricgroup__add_metric(const char *pmu, const char *metric_name, con* Iterate over all metrics seeing if metric matches either the * name or group. When it does add the metric to the list. */ - ret =pmu_metrics_table_for_each_metric (table, metricgroup__add_metric_callback, + ret =pmu_metrics_table__for_each_metric (table, metricgroup__add_metric_callback, &data);if (ret) goto out;
@@ -1740,7 +1740,7 @@ bool metricgroup__has_metric(const char *pmu, const char *metric)if (!table)return false; -return pmu_metrics_table_for_each_metric (table, metricgroup__has_metric_callback, &data) +return pmu_metrics_table__for_each_metric (table, metricgroup__has_metric_callback, &data) ? true : false; }
@@ -1770,7 +1770,7 @@ unsigned int metricgroups__topdown_max_level(void)if (!table)return false; -pmu_metrics_table_for_each_metric (table, metricgroup__topdown_max_level_callback, +pmu_metrics_table__for_each_metric (table, metricgroup__topdown_max_level_callback, &max_level);return max_level; }
tools/perf/util/pmu.c +1 -1
@@ -892,7 +892,7 @@ void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_tab .pmu = pmu, }; -pmu_events_table_for_each_event (table, pmu_add_cpu_aliases_map_callback, &data); +pmu_events_table__for_each_event (table, pmu_add_cpu_aliases_map_callback, &data);free (data.default_pmu_name); }
tools/perf/util/s390-sample-raw.c +1 -1
@@ -168,7 +168,7 @@ static const char *get_counter_name(int set, int nr, const struct pmu_events_tabif (!table)return NULL ; -pmu_events_table_for_each_event (table, get_counter_name_callback, &data); +pmu_events_table__for_each_event (table, get_counter_name_callback, &data);return data.result; }
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 12/25 perf jevents: Group events by PMU
In reply to Ian RogersPrior to this change a cpuid would map to a list of events where the PMU would be encoded alongside the event information. This change breaks apart each group of events so that there is a group per PMU. A new table is added with the PMU's name and the list of events, the original table now holding an array of these per PMU tables.
These changes are to make it easier to get per PMU information about events, rather than the current approach of scanning all events. The perf binary size with BPF skeletons on x86 is reduced by about 1%. The unidentified PMU is now always expanded to "cpu".
--- tools/perf/pmu-events/jevents.py | 181 +++++++++++++++++++++++-------- tools/perf/tests/pmu-events.c | 30 +++-- 2 files changed, 154 insertions(+), 57 deletions(-)
tools/perf/pmu-events/jevents.py +136 -45
@@ -42,7 +42,7 @@ _metricgroups = {} # Order specific JsonEvent attributes will be visited. _json_event_attributes = [ # cmp_sevent related attributes. - 'name', 'pmu', 'topic', 'desc', + 'name', 'topic', 'desc', # Seems useful, put it early. 'event', # Short things in alphabetical order.
@@ -53,7 +53,7 @@ _json_event_attributes = [ # Attributes that are in pmu_metric rather than pmu_event. _json_metric_attributes = [ - 'pmu', 'metric_name', 'metric_group', 'metric_expr', 'metric_threshold', + 'metric_name', 'metric_group', 'metric_expr', 'metric_threshold', 'desc', 'long_desc', 'unit', 'compat', 'metricgroup_no_group', 'default_metricgroup_name', 'aggr_mode', 'event_grouping' ]
@@ -252,7 +252,7 @@ class JsonEvent: def unit_to_pmu(unit: str) -> Optional[str]: """Convert a JSON Unit to Linux PMU name.""" if not unit: - return None + return 'cpu' # Comment brought over from jevents.c: # it's not realistic to keep adding these, we need something more scalable ... table = {
@@ -343,10 +343,13 @@ class JsonEvent: self.desc += extra_desc if self.long_desc and extra_desc: self.long_desc += extra_desc - if self.pmu: - if self.desc and not self.desc.endswith('. '): - self.desc += '. ' - self.desc = (self.desc if self.desc else '') + ('Unit: ' + self.pmu + ' ') + if self.pmu and self.pmu != 'cpu': + if not self.desc: + self.desc = 'Unit: ' + self.pmu + else: + if not self.desc.endswith('. '): + self.desc += '. ' + self.desc += 'Unit: ' + self.pmu if arch_std: if arch_std.lower() in _arch_std_events: event = _arch_std_events[arch_std.lower()].event
@@ -437,13 +440,13 @@ def add_events_table_entries(item: os.DirEntry, topic: str) -> None: def print_pending_events() -> None: """Optionally close events table.""" - def event_cmp_key(j: JsonEvent) -> Tuple[bool, str, str, str, str]: + def event_cmp_key(j: JsonEvent) -> Tuple[str, str, bool, str, str]: def fix_none(s: Optional[str]) -> str: if s is None: return '' return s - return (j.desc is not None, fix_none(j.topic), fix_none(j.name), fix_none(j.pmu), + return (fix_none(j.pmu).replace(',','_'), fix_none(j.name), j.desc is not None, fix_none(j.topic), fix_none(j.metric_name)) global _pending_events
@@ -458,13 +461,36 @@ def print_pending_events() -> None: global event_tables _event_tables.append(_pending_events_tblname) - _args.output_file.write( - f'static const struct compact_pmu_event {_pending_events_tblname}[] = {{\n') - + first = True + last_pmu = None + pmus = set() for event in sorted(_pending_events, key=event_cmp_key): + if event.pmu != last_pmu: + if not first: + _args.output_file.write('};\n') + pmu_name = event.pmu.replace(',', '_') + _args.output_file.write( + f'static const struct compact_pmu_event {_pending_events_tblname}_{pmu_name}[] = {{\n') + first = False + last_pmu = event.pmu + pmus.add((event.pmu, pmu_name)) + _args.output_file.write(event.to_c_string(metric=False)) _pending_events = [] + _args.output_file.write(f""" +}}; + +const struct pmu_table_entry {_pending_events_tblname}[] = {{ +""") + for (pmu, tbl_pmu) in sorted(pmus): + pmu_name = f"{pmu}\\000" + _args.output_file.write(f"""{{ + .entries = {_pending_events_tblname}_{tbl_pmu}, + .num_entries = ARRAY_SIZE({_pending_events_tblname}_{tbl_pmu}), + .pmu_name = {{ {_bcs.offsets[pmu_name]} /* {pmu_name} */ }}, +}}, +""") _args.output_file.write('};\n\n') def print_pending_metrics() -> None:
@@ -490,13 +516,36 @@ def print_pending_metrics() -> None: global metric_tables _metric_tables.append(_pending_metrics_tblname) - _args.output_file.write( - f'static const struct compact_pmu_event {_pending_metrics_tblname}[] = {{\n') - + first = True + last_pmu = None + pmus = set() for metric in sorted(_pending_metrics, key=metric_cmp_key): + if metric.pmu != last_pmu: + if not first: + _args.output_file.write('};\n') + pmu_name = metric.pmu.replace(',', '_') + _args.output_file.write( + f'static const struct compact_pmu_event {_pending_metrics_tblname}_{pmu_name}[] = {{\n') + first = False + last_pmu = metric.pmu + pmus.add((metric.pmu, pmu_name)) + _args.output_file.write(metric.to_c_string(metric=True)) _pending_metrics = [] + _args.output_file.write(f""" +}}; + +const struct pmu_table_entry {_pending_metrics_tblname}[] = {{ +""") + for (pmu, tbl_pmu) in sorted(pmus): + pmu_name = f"{pmu}\\000" + _args.output_file.write(f"""{{ + .entries = {_pending_metrics_tblname}_{tbl_pmu}, + .num_entries = ARRAY_SIZE({_pending_metrics_tblname}_{tbl_pmu}), + .pmu_name = {{ {_bcs.offsets[pmu_name]} /* {pmu_name} */ }}, +}}, +""") _args.output_file.write('};\n\n') def get_topic(topic: str) -> str:
@@ -532,6 +581,8 @@ def preprocess_one_file(parents: Sequence[str], item: os.DirEntry) -> None: topic = get_topic(item.name) for event in read_json_events(item.path, topic): + pmu_name = f"{event.pmu}\\000" + _bcs.add(pmu_name) if event.name: _bcs.add(event.build_c_string(metric=False)) if event.metric_name:
@@ -577,14 +628,14 @@ def print_mapping_table(archs: Sequence[str]) -> None: _args.output_file.write(""" /* Struct used to make the PMU event table implementation opaque to callers. */ struct pmu_events_table { - const struct compact_pmu_event *entries; - size_t length; + const struct pmu_table_entry *pmus; + uint32_t num_pmus; }; /* Struct used to make the PMU metric table implementation opaque to callers. */ struct pmu_metrics_table { - const struct compact_pmu_event *entries; - size_t length; + const struct pmu_table_entry *pmus; + uint32_t num_pmus; }; /*
@@ -614,12 +665,12 @@ const struct pmu_events_map pmu_events_map[] = { \t.arch = "testarch", \t.cpuid = "testcpu", \t.event_table = { -\t\t.entries = pmu_events__test_soc_cpu, -\t\t.length = ARRAY_SIZE(pmu_events__test_soc_cpu), +\t\t.pmus = pmu_events__test_soc_cpu, +\t\t.num_pmus = ARRAY_SIZE(pmu_events__test_soc_cpu), \t}, \t.metric_table = { -\t\t.entries = pmu_metrics__test_soc_cpu, -\t\t.length = ARRAY_SIZE(pmu_metrics__test_soc_cpu), +\t\t.pmus = pmu_metrics__test_soc_cpu, +\t\t.num_pmus = ARRAY_SIZE(pmu_metrics__test_soc_cpu), \t} }, """)
@@ -649,12 +700,12 @@ const struct pmu_events_map pmu_events_map[] = { \t.arch = "{arch}", \t.cpuid = "{cpuid}", \t.event_table = {{ -\t\t.entries = {event_tblname}, -\t\t.length = {event_size} +\t\t.pmus = {event_tblname}, +\t\t.num_pmus = {event_size} \t}}, \t.metric_table = {{ -\t\t.entries = {metric_tblname}, -\t\t.length = {metric_size} +\t\t.pmus = {metric_tblname}, +\t\t.num_pmus = {metric_size} \t}} }}, """)
@@ -685,15 +736,15 @@ static const struct pmu_sys_events pmu_sys_event_tables[] = { for tblname in _sys_event_tables: _args.output_file.write(f"""\t{{ \t\t.event_table = {{ -\t\t\t.entries = {tblname}, -\t\t\t.length = ARRAY_SIZE({tblname}) +\t\t\t.pmus = {tblname}, +\t\t\t.num_pmus = ARRAY_SIZE({tblname}) \t\t}},""") metric_tblname = _sys_event_table_to_metric_table_mapping[tblname] if metric_tblname in _sys_metric_tables: _args.output_file.write(f""" \t\t.metric_table = {{ -\t\t\t.entries = {metric_tblname}, -\t\t\t.length = ARRAY_SIZE({metric_tblname}) +\t\t\t.pmus = {metric_tblname}, +\t\t\t.num_pmus = ARRAY_SIZE({metric_tblname}) \t\t}},""") printed_metric_tables.append(metric_tblname) _args.output_file.write(f"""
@@ -753,18 +804,56 @@ static void decompress_metric(int offset, struct pmu_metric *pm) _args.output_file.write('\twhile (*p++);') _args.output_file.write("""} +static int pmu_events_table__for_each_event_pmu(const struct pmu_events_table *table, + const struct pmu_table_entry *pmu, + pmu_event_iter_fn fn, + void *data) +{ + int ret; + struct pmu_event pe = { + .pmu = &big_c_string[pmu->pmu_name.offset], + }; + + for (uint32_t i = 0; i < pmu->num_entries; i++) { + decompress_event(pmu->entries[i].offset, &pe); + if (!pe.name) + continue; + ret = fn(&pe, table, data); + if (ret) + return ret; + } + return 0; + } + int pmu_events_table__for_each_event(const struct pmu_events_table *table, pmu_event_iter_fn fn, void *data) { - for (size_t i = 0; i < table->length; i++) { - struct pmu_event pe; - int ret; + for (size_t i = 0; i < table->num_pmus; i++) { + int ret = pmu_events_table__for_each_event_pmu(table, &table->pmus[i], + fn, data); - decompress_event(table->entries[i].offset, &pe); - if (!pe.name) + if (ret) + return ret; + } + return 0; +} + +static int pmu_metrics_table__for_each_metric_pmu(const struct pmu_metrics_table *table, + const struct pmu_table_entry *pmu, + pmu_metric_iter_fn fn, + void *data) +{ + int ret; + struct pmu_metric pm = { + .pmu = &big_c_string[pmu->pmu_name.offset], + }; + + for (uint32_t i = 0; i < pmu->num_entries; i++) { + decompress_metric(pmu->entries[i].offset, &pm); + if (!pm.metric_expr) continue; - ret = fn(&pe, table, data); + ret = fn(&pm, table, data); if (ret) return ret; }
@@ -775,14 +864,10 @@ int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn, void *data) { - for (size_t i = 0; i < table->length; i++) { - struct pmu_metric pm; - int ret; + for (size_t i = 0; i < table->num_pmus; i++) { + int ret = pmu_metrics_table__for_each_metric_pmu(table, &table->pmus[i], + fn, data); - decompress_metric(table->entries[i].offset, &pm); - if (!pm.metric_expr) - continue; - ret = fn(&pm, table, data); if (ret) return ret; }
@@ -1010,7 +1095,13 @@ such as "arm/cortex-a34".''', #include <stddef.h> struct compact_pmu_event { - int offset; + int offset; +}; + +struct pmu_table_entry { + const struct compact_pmu_event *entries; + uint32_t num_entries; + struct compact_pmu_event pmu_name; }; """)
tools/perf/tests/pmu-events.c +18 -12
@@ -44,6 +44,7 @@ struct perf_pmu_test_pmu {static const struct perf_pmu_test_event bp_l1_btb_correct = { .event = { + .pmu ="cpu" , .name ="bp_l1_btb_correct" , .event ="event=0x8a" , .desc ="L1 BTB Correction" ,
@@ -55,6 +56,7 @@ static const struct perf_pmu_test_event bp_l1_btb_correct = {static const struct perf_pmu_test_event bp_l2_btb_correct = { .event = { + .pmu ="cpu" , .name ="bp_l2_btb_correct" , .event ="event=0x8b" , .desc ="L2 BTB Correction" ,
@@ -66,6 +68,7 @@ static const struct perf_pmu_test_event bp_l2_btb_correct = {static const struct perf_pmu_test_event segment_reg_loads_any = { .event = { + .pmu ="cpu" , .name ="segment_reg_loads.any" , .event ="event=0x6,period=200000,umask=0x80" , .desc ="Number of segment register loads" ,
@@ -77,6 +80,7 @@ static const struct perf_pmu_test_event segment_reg_loads_any = {static const struct perf_pmu_test_event dispatch_blocked_any = { .event = { + .pmu ="cpu" , .name ="dispatch_blocked.any" , .event ="event=0x9,period=200000,umask=0x20" , .desc ="Memory cluster signals to block micro-op dispatch for any reason" ,
@@ -88,6 +92,7 @@ static const struct perf_pmu_test_event dispatch_blocked_any = {static const struct perf_pmu_test_event eist_trans = { .event = { + .pmu ="cpu" , .name ="eist_trans" , .event ="event=0x3a,period=200000,umask=0x0" , .desc ="Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions" ,
@@ -99,6 +104,7 @@ static const struct perf_pmu_test_event eist_trans = {static const struct perf_pmu_test_event l3_cache_rd = { .event = { + .pmu ="cpu" , .name ="l3_cache_rd" , .event ="event=0x40" , .desc ="L3 cache access, read" ,
@@ -123,7 +129,7 @@ static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd = { .event = { .name = "uncore_hisi_ddrc.flux_wcmd", .event = "event=0x2", - .desc = "DDRC write commands. Unit: hisi_sccl,ddrc ", + .desc = "DDRC write commands. Unit: hisi_sccl,ddrc", .topic = "uncore", .long_desc = "DDRC write commands", .pmu = "hisi_sccl,ddrc",
@@ -137,7 +143,7 @@ static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction = { .event = { .name = "unc_cbo_xsnp_response.miss_eviction", .event = "event=0x22,umask=0x81", - .desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core. Unit: uncore_cbox ", + .desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core. Unit: uncore_cbox", .topic = "uncore", .long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core", .pmu = "uncore_cbox",
@@ -151,7 +157,7 @@ static const struct perf_pmu_test_event uncore_hyphen = { .event = { .name = "event-hyphen", .event = "event=0xe0,umask=0x00", - .desc = "UNC_CBO_HYPHEN. Unit: uncore_cbox ", + .desc = "UNC_CBO_HYPHEN. Unit: uncore_cbox", .topic = "uncore", .long_desc = "UNC_CBO_HYPHEN", .pmu = "uncore_cbox",
@@ -165,7 +171,7 @@ static const struct perf_pmu_test_event uncore_two_hyph = { .event = { .name = "event-two-hyph", .event = "event=0xc0,umask=0x00", - .desc = "UNC_CBO_TWO_HYPH. Unit: uncore_cbox ", + .desc = "UNC_CBO_TWO_HYPH. Unit: uncore_cbox", .topic = "uncore", .long_desc = "UNC_CBO_TWO_HYPH", .pmu = "uncore_cbox",
@@ -179,7 +185,7 @@ static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = { .event = { .name = "uncore_hisi_l3c.rd_hit_cpipe", .event = "event=0x7", - .desc = "Total read hits. Unit: hisi_sccl,l3c ", + .desc = "Total read hits. Unit: hisi_sccl,l3c", .topic = "uncore", .long_desc = "Total read hits", .pmu = "hisi_sccl,l3c",
@@ -193,7 +199,7 @@ static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = { .event = { .name = "uncore_imc_free_running.cache_miss", .event = "event=0x12", - .desc = "Total cache misses. Unit: uncore_imc_free_running ", + .desc = "Total cache misses. Unit: uncore_imc_free_running", .topic = "uncore", .long_desc = "Total cache misses", .pmu = "uncore_imc_free_running",
@@ -207,7 +213,7 @@ static const struct perf_pmu_test_event uncore_imc_cache_hits = { .event = { .name = "uncore_imc.cache_hits", .event = "event=0x34", - .desc = "Total cache hits. Unit: uncore_imc ", + .desc = "Total cache hits. Unit: uncore_imc", .topic = "uncore", .long_desc = "Total cache hits", .pmu = "uncore_imc",
@@ -232,13 +238,13 @@ static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = { .event = { .name = "sys_ddr_pmu.write_cycles", .event = "event=0x2b", - .desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ", + .desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu", .topic = "uncore", .pmu = "uncore_sys_ddr_pmu", .compat = "v8", }, .alias_str = "event=0x2b", - .alias_long_desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ", + .alias_long_desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu", .matching_pmu = "uncore_sys_ddr_pmu", };
@@ -246,13 +252,13 @@ static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = { .event = { .name = "sys_ccn_pmu.read_cycles", .event = "config=0x2c", - .desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ", + .desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu", .topic = "uncore", .pmu = "uncore_sys_ccn_pmu", .compat = "0x01", }, .alias_str = "config=0x2c", - .alias_long_desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ", + .alias_long_desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu", .matching_pmu = "uncore_sys_ccn_pmu", };
@@ -403,7 +409,7 @@ static int test__pmu_event_table_core_callback(const struct pmu_event *pe,struct perf_pmu_test_event const **test_event_table;bool found = false; -if (pe->pmu) +if (strcmp (pe->pmu,"cpu" )) test_event_table = &uncore_events[0 ];else test_event_table = &core_events[0 ];
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 13/25 perf parse-events: Improve error message for double setting
In reply to Ian RogersDouble setting information for an event would produce an error message associated with the PMU rather than the term that was double setting. Improve the error message to be on the term.
Before:
```
$ perf stat -e 'cpu/inst_retired.any,inst_retired.any/' true
event syntax error: 'cpu/inst_retired.any,inst_retired.any/'
\___ Bad event or PMU
Unabled to find PMU or event on a PMU of 'cpu'
Run 'perf list' for a list of valid events
```After:
```
$ perf stat -e 'cpu/inst_retired.any,inst_retired.any/' true
event syntax error: '..etired.any,inst_retired.any/'
\___ Bad event or PMU
Unabled to find PMU or event on a PMU of 'cpu'
Initial error:
event syntax error: '..etired.any,inst_retired.any/'
\___ Attempt to set event's scale twice
Run 'perf list' for a list of valid events
```--- tools/perf/util/parse-events.c | 2 +- tools/perf/util/pmu.c | 34 +++++++++++++++++++++++++++------- tools/perf/util/pmu.h | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-)
tools/perf/util/parse-events.c +1 -1
@@ -1348,7 +1348,7 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,return evsel ?0 : -ENOMEM ; } -if (!parse_state->fake_pmu &&perf_pmu__check_alias (pmu, head_config, &info)) +if (!parse_state->fake_pmu &&perf_pmu__check_alias (pmu, head_config, &info, err))return -EINVAL ;if (verbose >1 ) {
tools/perf/util/pmu.c +27 -7
@@ -1439,17 +1439,33 @@ static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,static int check_info_data (struct perf_pmu_alias *alias, -struct perf_pmu_info *info) +struct perf_pmu_info *info, +struct parse_events_error *err, +int column) {/* * Only one term in event definition can * define unit, scale and snapshot, fail * if there's more than one. */ -if ((info->unit && alias->unit[0 ]) || - (info->scale && alias->scale) || - (info->snapshot && alias->snapshot)) +if (info->unit && alias->unit[0 ]) { +parse_events_error__handle (err, column, +strdup ("Attempt to set event's unit twice" ), +NULL ); +return -EINVAL ; + } +if (info->scale && alias->scale) { +parse_events_error__handle (err, column, +strdup ("Attempt to set event's scale twice" ), +NULL ); +return -EINVAL ; + } +if (info->snapshot && alias->snapshot) { +parse_events_error__handle (err, column, +strdup ("Attempt to set event snapshot twice" ), +NULL );return -EINVAL ; + }if (alias->unit[0 ]) info->unit = alias->unit;
@@ -1468,7 +1484,7 @@ static int check_info_data(struct perf_pmu_alias *alias,* defined for the alias */ int perf_pmu__check_alias (struct perf_pmu *pmu,struct list_head *head_terms, -struct perf_pmu_info *info) +struct perf_pmu_info *info,struct parse_events_error *err) {struct parse_events_term *term, *h;struct perf_pmu_alias *alias;
@@ -1489,10 +1505,14 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,if (!alias)continue ; ret =pmu_alias_terms (alias, &term->list); -if (ret) +if (ret) { +parse_events_error__handle (err, term->err_term, +strdup ("Failure to duplicate terms" ), +NULL );return ret; + } - ret =check_info_data (alias, info); + ret =check_info_data (alias, info, err, term->err_term);if (ret)return ret;
tools/perf/util/pmu.h +1 -1
@@ -185,7 +185,7 @@ int perf_pmu__config_terms(struct perf_pmu *pmu,__u64 perf_pmu__format_bits (struct perf_pmu *pmu,const char *name);int perf_pmu__format_type (struct perf_pmu *pmu,const char *name);int perf_pmu__check_alias (struct perf_pmu *pmu,struct list_head *head_terms, -struct perf_pmu_info *info); +struct perf_pmu_info *info,struct parse_events_error *err);int perf_pmu__find_event (struct perf_pmu *pmu,const char *event,void *state,pmu_event_callback cb);int perf_pmu__format_parse (struct perf_pmu *pmu,int dirfd,bool eager_load);
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 14/25 perf s390 s390_cpumcfdg_dump: Don't scan all PMUs
In reply to Ian RogersRather than scanning all PMUs for a counter name, scan the PMU associated with the evsel of the sample. This is done to remove a dependence on pmu-events.h.
--- tools/perf/util/s390-sample-raw.c | 50 ++++++++++++++++--------------- 1 file changed, 26 insertions(+), 24 deletions(-)
tools/perf/util/s390-sample-raw.c +26 -24
@@ -27,7 +27,7 @@#include "color.h" #include "sample-raw.h" #include "s390-cpumcf-kernel.h" -#include "pmu-events/pmu-events.h" +#include "util/pmu.h" #include "util/sample.h" static size_t ctrset_size (struct cf_ctrset_entry *set)
@@ -132,56 +132,57 @@ static int get_counterset_start(int setnr)struct get_counter_name_data {int wanted; -const char *result; +char *result; }; -static int get_counter_name_callback (const struct pmu_event *evp, -const struct pmu_events_table *table__maybe_unused , -void *vdata) +static int get_counter_name_callback (void *vdata,struct pmu_event_info *info) {struct get_counter_name_data *data = vdata;int rc, event_nr; +const char *event_str; -if (evp->name ==NULL || evp->event ==NULL ) +if (info->str ==NULL )return 0 ; - rc =sscanf (evp->event,"event=%x" , &event_nr); + + event_str =strstr (info->str,"event=" ); +if (!event_str) +return 0 ; + + rc =sscanf (event_str,"event=%x" , &event_nr);if (rc ==1 && event_nr == data->wanted) { - data->result = evp->name; + data->result =strdup (info->name);return 1 ;/* Terminate the search. */ }return 0 ; } -/* Scan the PMU table and extract the logical name of a counter from the -* PMU events table. Input is the counter set and counter number with in the -* set. Construct the event number and use this as key. If they match return -* the name of this counter. +/* Scan the PMU and extract the logical name of a counter from the event. Input +* is the counter set and counter number with in the set. Construct the event +* number and use this as key. If they match return the name of this counter. * If no match is found a NULL pointer is returned. */ -static const char *get_counter_name (int set,int nr,const struct pmu_events_table *table) +static char *get_counter_name (int set,int nr,struct perf_pmu *pmu) {struct get_counter_name_data data = { .wanted =get_counterset_start (set) + nr, .result =NULL , }; -if (!table) +if (!pmu)return NULL ; -pmu_events_table__for_each_event (table, get_counter_name_callback, &data); +perf_pmu__for_each_event (pmu, &data, get_counter_name_callback);return data.result; } -static void s390_cpumcfdg_dump (struct perf_sample *sample) +static void s390_cpumcfdg_dump (struct perf_pmu *pmu,struct perf_sample *sample) {size_t i, len = sample->raw_size, offset =0 ;unsigned char *buf = sample->raw_data;const char *color =PERF_COLOR_BLUE ;struct cf_ctrset_entry *cep, ce; -const struct pmu_events_table *table;u64 *p; - table =pmu_events_table__find ();while (offset < len) { cep = (struct cf_ctrset_entry *)(buf + offset);
@@ -199,11 +200,12 @@ static void s390_cpumcfdg_dump(struct perf_sample *sample)color_fprintf (stdout, color," [%#08zx] Counterset:%d" " Counters:%d\n" , offset, ce.set, ce.ctr);for (i =0 , p = (u64 *)(cep +1 ); i < ce.ctr; ++i, ++p) { -const char *ev_name =get_counter_name (ce.set, i, table); +char *ev_name =get_counter_name (ce.set, i, pmu);color_fprintf (stdout, color,"\tCounter:%03d %s Value:%#018lx\n" , i, ev_name ?:"<unknown>" ,be64_to_cpu (*p)); +free (ev_name); } offset +=ctrset_size (&ce); }
@@ -216,14 +218,14 @@ static void s390_cpumcfdg_dump(struct perf_sample *sample)*/ void evlist__s390_sample_raw (struct evlist *evlist,union perf_event *event,struct perf_sample *sample) { -struct evsel *ev_bc000; +struct evsel *evsel;if (event->header.type !=PERF_RECORD_SAMPLE )return ; - ev_bc000 =evlist__event2evsel (evlist, event); -if (ev_bc000 ==NULL || - ev_bc000->core.attr.config !=PERF_EVENT_CPUM_CF_DIAG ) + evsel =evlist__event2evsel (evlist, event); +if (evsel ==NULL || + evsel->core.attr.config !=PERF_EVENT_CPUM_CF_DIAG )return ;/* Display raw data on screen */
@@ -231,5 +233,5 @@ void evlist__s390_sample_raw(struct evlist *evlist, union perf_event *event, strpr_err ("Invalid counter set data encountered\n" );return ; } -s390_cpumcfdg_dump (sample); +s390_cpumcfdg_dump (evsel->pmu, sample); }
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 15/25 perf pmu-events: Reduce processed events by passing PMU
In reply to Ian RogersPass the PMU to pmu_events_table__for_each_event so that entries that don't match don't need to be processed by callback. If a NULL PMU is passed then all PMUs are processed. perf bench internals pmu-scan "Average PMU scanning" performance is reduced by about 5% on an Intel tigerlake.
--- tools/perf/pmu-events/empty-pmu-events.c | 15 ++++++---- tools/perf/pmu-events/jevents.py | 18 ++++++++---- tools/perf/pmu-events/pmu-events.h | 4 ++- tools/perf/tests/pmu-events.c | 6 ++-- tools/perf/util/pmu.c | 35 +++++++----------------- tools/perf/util/pmu.h | 1 + 6 files changed, 41 insertions(+), 38 deletions(-)
tools/perf/pmu-events/empty-pmu-events.c +10 -5
@@ -266,12 +266,16 @@ static const struct pmu_sys_events pmu_sys_event_tables[] = { }, }; -int pmu_events_table__for_each_event (const struct pmu_events_table *table,pmu_event_iter_fn fn, -void *data) +int pmu_events_table__for_each_event (const struct pmu_events_table *table,struct perf_pmu *pmu, +pmu_event_iter_fn fn,void *data) {for (const struct pmu_event *pe = &table->entries[0 ]; pe->name; pe++) { -int ret =fn (pe, table, data); +int ret; +if (pmu && !pmu__name_match (pmu, pe->pmu)) +continue ; + + ret =fn (pe, table, data);if (ret)return ret; }
@@ -371,7 +375,8 @@ const struct pmu_metrics_table *find_core_metrics_table(const char *arch, constint pmu_for_each_core_event (pmu_event_iter_fn fn,void *data) {for (const struct pmu_events_map *tables = &pmu_events_map[0 ]; tables->arch; tables++) { -int ret =pmu_events_table__for_each_event (&tables->event_table, fn, data); +int ret =pmu_events_table__for_each_event (&tables->event_table, +/*pmu=*/ NULL , fn, data);if (ret)return ret;
@@ -408,7 +413,7 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data)for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0 ]; tables->name; tables++) { -int ret =pmu_events_table__for_each_event (&tables->table, fn, data); +int ret =pmu_events_table__for_each_event (&tables->table,/*pmu=*/ NULL , fn, data);if (ret)return ret;
tools/perf/pmu-events/jevents.py +13 -5
@@ -826,14 +826,20 @@ static int pmu_events_table__for_each_event_pmu(const struct pmu_events_table *t } int pmu_events_table__for_each_event(const struct pmu_events_table *table, + struct perf_pmu *pmu, pmu_event_iter_fn fn, void *data) { for (size_t i = 0; i < table->num_pmus; i++) { - int ret = pmu_events_table__for_each_event_pmu(table, &table->pmus[i], - fn, data); + const struct pmu_table_entry *table_pmu = &table->pmus[i]; + const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset]; + int ret; - if (ret) + if (pmu && !pmu__name_match(pmu, pmu_name)) + continue; + + ret = pmu_events_table__for_each_event_pmu(table, table_pmu, fn, data); + if (pmu || ret) return ret; } return 0;
@@ -955,7 +961,8 @@ int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data) for (const struct pmu_events_map *tables = &pmu_events_map[0]; tables->arch; tables++) { - int ret = pmu_events_table__for_each_event(&tables->event_table, fn, data); + int ret = pmu_events_table__for_each_event(&tables->event_table, + /*pmu=*/ NULL, fn, data); if (ret) return ret;
@@ -992,7 +999,8 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data) for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0]; tables->name; tables++) { - int ret = pmu_events_table__for_each_event(&tables->event_table, fn, data); + int ret = pmu_events_table__for_each_event(&tables->event_table, + /*pmu=*/ NULL, fn, data); if (ret) return ret;
tools/perf/pmu-events/pmu-events.h +3 -1
@@ -77,7 +77,9 @@ typedef int (*pmu_metric_iter_fn)(const struct pmu_metric *pm,const struct pmu_metrics_table *table, void *data); -int pmu_events_table__for_each_event (const struct pmu_events_table *table,pmu_event_iter_fn fn, +int pmu_events_table__for_each_event (const struct pmu_events_table *table, +struct perf_pmu *pmu, +pmu_event_iter_fn fn,void *data);int pmu_metrics_table__for_each_metric (const struct pmu_metrics_table *table,pmu_metric_iter_fn fn,void *data);
tools/perf/tests/pmu-events.c +4 -2
@@ -483,12 +483,14 @@ static int test__pmu_event_table(struct test_suite *test __maybe_unused,if (!table || !sys_event_table)return -1 ; - err =pmu_events_table__for_each_event (table, test__pmu_event_table_core_callback, + err =pmu_events_table__for_each_event (table,/*pmu=*/ NULL , + test__pmu_event_table_core_callback, &map_events);if (err)return err; - err =pmu_events_table__for_each_event (sys_event_table, test__pmu_event_table_sys_callback, + err =pmu_events_table__for_each_event (sys_event_table,/*pmu=*/ NULL , + test__pmu_event_table_sys_callback, &map_events);if (err)return err;
tools/perf/util/pmu.c +10 -25
@@ -855,28 +855,14 @@ static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)return res; } -struct pmu_add_cpu_aliases_map_data { -/* List being added to. */ -struct list_head *head; -/* If a pmu_event lacks a given PMU the default used. */ -char *default_pmu_name; -/* The PMU that we're searching for events for. */ -struct perf_pmu *pmu; -}; -static int pmu_add_cpu_aliases_map_callback (const struct pmu_event *pe,const struct pmu_events_table *table__maybe_unused ,void *vdata) { -struct pmu_add_cpu_aliases_map_data *data = vdata; -const char *pname = pe->pmu ?: data->default_pmu_name; +struct list_head *head = vdata; -if (!strcmp (pname, data->pmu->name) || - (data->pmu->is_uncore &&pmu_uncore_alias_match (pname, data->pmu->name))) { -/* need type casts to override 'const' */ -__perf_pmu__new_alias (data->head,-1 , (char *)pe->name, (char *)pe->desc, - (char *)pe->event, pe); - } +/* need type casts to override 'const' */ +__perf_pmu__new_alias (head,-1 , (char *)pe->name, (char *)pe->desc, (char *)pe->event, pe);return 0 ; }
@@ -886,14 +872,7 @@ static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,*/ void pmu_add_cpu_aliases_table (struct perf_pmu *pmu,const struct pmu_events_table *table) { -struct pmu_add_cpu_aliases_map_data data = { - .head = &pmu->aliases, - .default_pmu_name =perf_pmus__default_pmu_name (), - .pmu = pmu, - }; - -pmu_events_table__for_each_event (table, pmu_add_cpu_aliases_map_callback, &data); -free (data.default_pmu_name); +pmu_events_table__for_each_event (table, pmu, pmu_add_cpu_aliases_map_callback, &pmu->aliases); }static void pmu_add_cpu_aliases (struct perf_pmu *pmu)
@@ -1709,6 +1688,12 @@ int perf_pmu__for_each_event(const struct perf_pmu *pmu, void *state, pmu_event_return ret; } +bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name) +{ +return !strcmp (pmu->name, pmu_name) || + (pmu->is_uncore &&pmu_uncore_alias_match (pmu_name, pmu->name)); +} +bool perf_pmu__is_software (const struct perf_pmu *pmu) {if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
tools/perf/util/pmu.h +1 -0
@@ -198,6 +198,7 @@ bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu);bool perf_pmu__have_event (const struct perf_pmu *pmu,const char *name);size_t perf_pmu__num_events (const struct perf_pmu *pmu);int perf_pmu__for_each_event (const struct perf_pmu *pmu,void *state,pmu_event_callback cb); +bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name);/** * perf_pmu_is_software - is the PMU a software PMU as in it uses the
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 16/25 perf pmu-events: Add pmu_events_table__find_event
In reply to Ian Rogersjevents stores events sorted by name. Add a find function that will binary search event names avoiding the need to linearly search through events. Add a test in tests/pmu-events.c. If the PMU or event aren't found -1000 is returned. If the event is found but no callback function given, 0 is returned. This allows the find function also act as a test for existence.
--- tools/perf/pmu-events/empty-pmu-events.c | 16 ++++++ tools/perf/pmu-events/jevents.py | 64 ++++++++++++++++++++++++ tools/perf/pmu-events/pmu-events.h | 5 ++ tools/perf/tests/pmu-events.c | 5 ++ 4 files changed, 90 insertions(+)
tools/perf/pmu-events/empty-pmu-events.c +16 -0
@@ -282,6 +282,22 @@ int pmu_events_table__for_each_event(const struct pmu_events_table *table, strucreturn 0 ; } +int pmu_events_table__find_event (const struct pmu_events_table *table, +struct perf_pmu *pmu, +const char *name, +pmu_event_iter_fn fn, +void *data) +{ +for (const struct pmu_event *pe = &table->entries[0 ]; pe->name; pe++) { +if (pmu && !pmu__name_match (pmu, pe->pmu)) +continue ; + +if (!strcasecmp (pe->name, name)) +return fn (pe, table, data); + } +return -1000 ; +} +int pmu_metrics_table__for_each_metric (const struct pmu_metrics_table *table,pmu_metric_iter_fn fn,void *data) {
tools/perf/pmu-events/jevents.py +64 -0
76 lines not on this page · raw message
tools/perf/pmu-events/pmu-events.h +5 -0
@@ -81,6 +81,11 @@ int pmu_events_table__for_each_event(const struct pmu_events_table *table,struct perf_pmu *pmu, pmu_event_iter_fn fn, void *data); +int pmu_events_table__find_event (const struct pmu_events_table *table, +struct perf_pmu *pmu, +const char *name, +pmu_event_iter_fn fn, +void *data);int pmu_metrics_table__for_each_metric (const struct pmu_metrics_table *table,pmu_metric_iter_fn fn,void *data);
tools/perf/tests/pmu-events.c +5 -0
@@ -546,6 +546,11 @@ static int __test_core_pmu_event_aliases(char *pmu_name, int *count)pmu_add_cpu_aliases_table (pmu, table); + res =pmu_events_table__find_event (table, pmu,"bp_l1_btb_correct" ,NULL ,NULL ); +if (res !=0 ) { +pr_debug ("Missing test event in test architecture" ); +return res; + }for (; *test_event_table; test_event_table++) {struct perf_pmu_test_event test_event = **test_event_table;struct pmu_event const *event = &test_event.event;
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 17/25 perf pmu: Parse sysfs events directly from a file
In reply to Ian RogersRather than read a sysfs events file into a 256 byte char buffer, pass the FILE* directly to the lex/yacc parser. This avoids there being a maximum events file size. While changing the API, constify some arguments to remove unnecessary casts. Allocating the read buffer decreases the performance of pmu-scan by around 3%.
--- tools/perf/arch/x86/util/intel-pt.c | 2 +- tools/perf/tests/parse-events.c | 2 +- tools/perf/util/parse-events.c | 18 +++++++---- tools/perf/util/parse-events.h | 3 +- tools/perf/util/pmu.c | 48 ++++++++++------------------- 5 files changed, 33 insertions(+), 40 deletions(-)
tools/perf/arch/x86/util/intel-pt.c +1 -1
@@ -74,7 +74,7 @@ static int intel_pt_parse_terms_with_default(struct perf_pmu *pmu,INIT_LIST_HEAD (terms); - err =parse_events_terms (terms, str); + err =parse_events_terms (terms, str,/*input=*/ NULL );if (err) goto out_free;
tools/perf/tests/parse-events.c +1 -1
@@ -2472,7 +2472,7 @@ static int test_term(const struct terms_test *t)INIT_LIST_HEAD (&terms); - ret =parse_events_terms (&terms, t->str); + ret =parse_events_terms (&terms, t->str,/*input=*/ NULL );if (ret) {pr_debug ("failed to parse terms '%s', err %d\n" , t->str , ret);
tools/perf/util/parse-events.c +12 -6
@@ -1715,6 +1715,7 @@ int parse_events_name(struct list_head *list, const char *name) }static int parse_events__scanner (const char *str, +FILE *input,struct parse_events_state *parse_state) {YY_BUFFER_STATE buffer;
@@ -1725,7 +1726,10 @@ static int parse_events__scanner(const char *str,if (ret)return ret; - buffer =parse_events__scan_string (str, scanner); +if (str) + buffer =parse_events__scan_string (str, scanner); +else +parse_events_set_in (input, scanner);#ifdef PARSER_DEBUG parse_events_debug =1 ;
@@ -1733,8 +1737,10 @@ static int parse_events__scanner(const char *str,#endif ret =parse_events_parse (parse_state, scanner); -parse_events__flush_buffer (buffer, scanner); -parse_events__delete_buffer (buffer, scanner); +if (str) { +parse_events__flush_buffer (buffer, scanner); +parse_events__delete_buffer (buffer, scanner); + }parse_events_lex_destroy (scanner);return ret; }
@@ -1742,7 +1748,7 @@ static int parse_events__scanner(const char *str,/* * parse event config string, return a list of event terms. */ -int parse_events_terms (struct list_head *terms,const char *str) +int parse_events_terms (struct list_head *terms,const char *str,FILE *input) {struct parse_events_state parse_state = { .terms =NULL ,
@@ -1750,7 +1756,7 @@ int parse_events_terms(struct list_head *terms, const char *str) };int ret; - ret =parse_events__scanner (str, &parse_state); + ret =parse_events__scanner (str, input, &parse_state);if (!ret) {list_splice (parse_state.terms, terms);
@@ -2001,7 +2007,7 @@ int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filte };int ret, ret2; - ret =parse_events__scanner (str, &parse_state); + ret =parse_events__scanner (str,/*input=*/ NULL , &parse_state);if (!ret &&list_empty (&parse_state.list)) {WARN_ONCE (true,"WARNING: event parser found nothing\n" );
tools/perf/util/parse-events.h +2 -1
@@ -9,6 +9,7 @@#include <stdbool.h> #include <linux/types.h> #include <linux/perf_event.h> +#include <stdio.h> #include <string.h> struct evsel ;
@@ -42,7 +43,7 @@ static inline int parse_events(struct evlist *evlist, const char *str,int parse_event (struct evlist *evlist,const char *str); -int parse_events_terms (struct list_head *terms,const char *str); +int parse_events_terms (struct list_head *terms,const char *str,FILE *input);int parse_filter (const struct option *opt,const char *str,int unset);int exclude_perf (const struct option *opt,const char *arg,int unset);
tools/perf/util/pmu.c +17 -31
108 lines not on this page · raw message
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 18/25 perf pmu: Prefer passing pmu to aliases list
In reply to Ian RogersThe aliases list is part of the PMU. Rather than pass the aliases list, pass the full PMU simplifying some callbacks.
--- tools/perf/util/pmu.c | 44 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 28 deletions(-)
tools/perf/util/pmu.c +16 -28
@@ -431,12 +431,12 @@ static void perf_pmu__del_aliases(struct perf_pmu *pmu)/* Merge an alias, search in alias list. If this name is already * present merge both of them to combine all information. */ -static bool perf_pmu_merge_alias (struct perf_pmu_alias *newalias, -struct list_head *alist) +static bool perf_pmu_merge_alias (struct perf_pmu *pmu, +struct perf_pmu_alias *newalias) {struct perf_pmu_alias *a; -list_for_each_entry (a, alist, list) { +list_for_each_entry (a, &pmu->aliases, list) {if (!strcasecmp (newalias->name, a->name)) {if (newalias->pmu_name && a->pmu_name && !strcasecmp (newalias->pmu_name, a->pmu_name)) {
@@ -450,7 +450,7 @@ static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,return false; } -static int perf_pmu__new_alias (struct list_head *list,int dirfd,const char *name, +static int perf_pmu__new_alias (struct perf_pmu *pmu,int dirfd,const char *name,const char *desc,const char *val,FILE *val_fd,const struct pmu_event *pe) {
@@ -532,8 +532,8 @@ static int perf_pmu__new_alias(struct list_head *list, int dirfd, const char *na alias->str =strdup (newval); alias->pmu_name = pmu_name ?strdup (pmu_name) :NULL ; -if (!perf_pmu_merge_alias (alias, list)) -list_add_tail (&alias->list, list); +if (!perf_pmu_merge_alias (pmu, alias)) +list_add_tail (&alias->list, &pmu->aliases);return 0 ; }
@@ -559,7 +559,7 @@ static inline bool pmu_alias_info_file(char *name)* Process all the sysfs attributes located under the directory * specified in 'dir' parameter. */ -static int pmu_aliases_parse (int dirfd,struct list_head *head) +static int pmu_aliases_parse (struct perf_pmu *pmu,int dirfd) {struct dirent *evt_ent;DIR *event_dir;
@@ -593,7 +593,7 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)continue ; } -if (perf_pmu__new_alias (head, dirfd, name,/*desc=*/ NULL , +if (perf_pmu__new_alias (pmu, dirfd, name,/*desc=*/ NULL ,/*val=*/ NULL , file,/*pe=*/ NULL ) <0 )pr_debug ("Cannot set up %s\n" , name);fclose (file);
@@ -616,7 +616,7 @@ static int pmu_aliases(struct perf_pmu *pmu, int dirfd, const char *name)return 0 ;/* it'll close the fd */ -if (pmu_aliases_parse (fd, &pmu->aliases)) +if (pmu_aliases_parse (pmu, fd))return -1 ;return 0 ;
@@ -844,10 +844,9 @@ static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,const struct pmu_events_table *table__maybe_unused , void *vdata) { - struct list_head *head = vdata; + struct perf_pmu *pmu = vdata; -/* need type casts to override 'const' */ -perf_pmu__new_alias (head,-1 , pe->name, pe->desc, pe->event,/*val_fd=*/ NULL , pe); +perf_pmu__new_alias (pmu,-1 , pe->name, pe->desc, pe->event,/*val_fd=*/ NULL , pe);return 0 ; }
@@ -857,7 +856,7 @@ static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,*/ void pmu_add_cpu_aliases_table (struct perf_pmu *pmu,const struct pmu_events_table *table) { -pmu_events_table__for_each_event (table, pmu, pmu_add_cpu_aliases_map_callback, &pmu->aliases); +pmu_events_table__for_each_event (table, pmu, pmu_add_cpu_aliases_map_callback, pmu); }static void pmu_add_cpu_aliases (struct perf_pmu *pmu)
@@ -871,24 +870,18 @@ static void pmu_add_cpu_aliases(struct perf_pmu *pmu)pmu_add_cpu_aliases_table (pmu, table); } -struct pmu_sys_event_iter_data { -struct list_head *head; -struct perf_pmu *pmu; -}; -static int pmu_add_sys_aliases_iter_fn (const struct pmu_event *pe,const struct pmu_events_table *table__maybe_unused , -void *data) +void *vdata) { -struct pmu_sys_event_iter_data *idata = data; -struct perf_pmu *pmu = idata->pmu; +struct perf_pmu *pmu = vdata;if (!pe->compat || !pe->pmu)return 0 ;if (!strcmp (pmu->id, pe->compat) &&pmu_uncore_alias_match (pe->pmu, pmu->name)) { - perf_pmu__new_alias(idata->head, -1, + perf_pmu__new_alias(pmu, -1, pe->name, pe->desc, pe->event,
@@ -901,15 +894,10 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,void pmu_add_sys_aliases (struct perf_pmu *pmu) { -struct pmu_sys_event_iter_data idata = { - .head = &pmu->aliases, - .pmu = pmu, - }; -if (!pmu->id)return ; -pmu_for_each_sys_event (pmu_add_sys_aliases_iter_fn, &idata); +pmu_for_each_sys_event (pmu_add_sys_aliases_iter_fn, pmu); } struct perf_event_attr * __weak
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 19/25 perf pmu: Merge json events with sysfs at load time
In reply to Ian RogersRather than load all sysfs events then parsing all json events and merging with ones that already exist. When a sysfs event is loaded, look for a corresponding json event and merge immediately.
To simplify the logic, early exit the perf_pmu__new_alias function if an alias is attempted to be added twice - as merging has already been explicitly handled.
Fix the copying of terms to a merged alias and some ENOMEM paths.
--- tools/perf/util/pmu.c | 177 +++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 89 deletions(-)
tools/perf/util/pmu.c +88 -89
@@ -373,38 +373,6 @@ static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,return 0 ; } -static void perf_pmu_assign_str (char *name,const char *field,char **old_str, -char **new_str) -{ -if (!*old_str) - goto set_new; - -if (*new_str) {/* Have new string, check with old */ -if (strcasecmp (*old_str, *new_str)) -pr_debug ("alias %s differs in field '%s'\n" , - name, field); -zfree (old_str); - }else /* Nothing new --> keep old string */ -return ; -set_new: - *old_str = *new_str; - *new_str =NULL ; -} - -static void perf_pmu_update_alias (struct perf_pmu_alias *old, -struct perf_pmu_alias *newalias) -{ -perf_pmu_assign_str (old->name,"desc" , &old->desc, &newalias->desc); -perf_pmu_assign_str (old->name,"long_desc" , &old->long_desc, - &newalias->long_desc); -perf_pmu_assign_str (old->name,"topic" , &old->topic, &newalias->topic); -perf_pmu_assign_str (old->name,"value" , &old->str, &newalias->str); - old->scale = newalias->scale; - old->per_pkg = newalias->per_pkg; - old->snapshot = newalias->snapshot; -memcpy (old->unit, newalias->unit,sizeof (old->unit)); -} -/* Delete an alias entry. */ static void perf_pmu_free_alias (struct perf_pmu_alias *newalias) {
@@ -428,26 +396,58 @@ static void perf_pmu__del_aliases(struct perf_pmu *pmu) } } -/* Merge an alias, search in alias list. If this name is already -* present merge both of them to combine all information. -*/ -static bool perf_pmu_merge_alias (struct perf_pmu *pmu, -struct perf_pmu_alias *newalias) +static struct perf_pmu_alias *perf_pmu__find_alias (const struct perf_pmu *pmu,const char *name) { -struct perf_pmu_alias *a; +struct perf_pmu_alias *alias; -list_for_each_entry (a, &pmu->aliases, list) { -if (!strcasecmp (newalias->name, a->name)) { -if (newalias->pmu_name && a->pmu_name && - !strcasecmp (newalias->pmu_name, a->pmu_name)) { -continue ; - } -perf_pmu_update_alias (a, newalias); -perf_pmu_free_alias (newalias); -return true; - } +list_for_each_entry (alias, &pmu->aliases, list) { +if (!strcasecmp (alias->name, name)) +return alias; } -return false; +return NULL ; +} + +static bool assign_str (const char *name,const char *field,char **old_str, +const char *new_str) +{ +if (!*old_str && new_str) { + *old_str =strdup (new_str); +return true; + } + +if (!new_str || !strcasecmp (*old_str, new_str)) +return false;/* Nothing to update. */ + +pr_debug ("alias %s differs in field '%s' ('%s' != '%s')\n" , + name, field, *old_str, new_str); +zfree (old_str); + *old_str =strdup (new_str); +return true; +} + +static int update_alias (const struct pmu_event *pe, +const struct pmu_events_table *table__maybe_unused , +void *vdata) +{ +struct perf_pmu_alias *alias = vdata; +int ret =0 ; + +assign_str (pe->name,"desc" , &alias->desc, pe->desc); +assign_str (pe->name,"long_desc" , &alias->long_desc, pe->long_desc); +assign_str (pe->name,"topic" , &alias->topic, pe->topic); + alias->per_pkg = pe->perpkg; +if (assign_str (pe->name,"value" , &alias->str, pe->event)) { +parse_events_terms__purge (&alias->terms); + ret =parse_events_terms (&alias->terms, pe->event,/*input=*/ NULL ); + } +if (!ret && pe->unit) { +char *unit; + + ret =perf_pmu__convert_scale (pe->unit, &unit, &alias->scale); +if (!ret) +snprintf (alias->unit,sizeof (alias->unit),"%s" , unit); + } +return ret; } static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char *name,
@@ -461,6 +461,11 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char *nameconst char *long_desc =NULL , *topic =NULL , *unit =NULL , *pmu_name =NULL ;bool deprecated = false, perpkg = false; +if (perf_pmu__find_alias (pmu, name)) { +/* Alias was already created/loaded. */ +return 0 ; + } +if (pe) { long_desc = pe->long_desc; topic = pe->topic;
@@ -488,27 +493,6 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char *namereturn ret; } -/* Scan event and remove leading zeroes, spaces, newlines, some -* platforms have terms specified as -* event=0x0091 (read from files ../<PMU>/events/<FILE> -* and terms specified as event=0x91 (read from JSON files). -* -* Rebuild string to make alias->str member comparable. -*/ -memset (newval,0 ,sizeof (newval)); - ret =0 ; -list_for_each_entry (term, &alias->terms, list) { -if (ret) - ret +=scnprintf (newval + ret,sizeof (newval) - ret, -"," ); -if (term->type_val ==PARSE_EVENTS__TERM_TYPE_NUM ) - ret +=scnprintf (newval + ret,sizeof (newval) - ret, -"%s=%#x" , term->config, term->val.num); -else if (term->type_val ==PARSE_EVENTS__TERM_TYPE_STR ) - ret +=scnprintf (newval + ret,sizeof (newval) - ret, -"%s=%s" , term->config, term->val.str); - } - alias->name =strdup (name);if (dirfd >=0 ) {/*
@@ -524,17 +508,43 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char *name alias->long_desc = long_desc ?strdup (long_desc) : desc ?strdup (desc) :NULL ; alias->topic = topic ?strdup (topic) :NULL ; + alias->pmu_name = pmu_name ?strdup (pmu_name) :NULL ;if (unit) { -if (perf_pmu__convert_scale (unit, (char **)&unit, &alias->scale) <0 ) +if (perf_pmu__convert_scale (unit, (char **)&unit, &alias->scale) <0 ) { +perf_pmu_free_alias (alias);return -1 ; + }snprintf (alias->unit,sizeof (alias->unit),"%s" , unit); } - alias->str =strdup (newval); - alias->pmu_name = pmu_name ?strdup (pmu_name) :NULL ; +if (!pe) { +/* Update an event from sysfs with json data. */ +const struct pmu_events_table *table =perf_pmu__find_events_table (pmu); -if (!perf_pmu_merge_alias (pmu, alias)) -list_add_tail (&alias->list, &pmu->aliases); +if (table) +pmu_events_table__find_event (table, pmu, name, update_alias, alias); + } +/* Scan event and remove leading zeroes, spaces, newlines, some +* platforms have terms specified as +* event=0x0091 (read from files ../<PMU>/events/<FILE> +* and terms specified as event=0x91 (read from JSON files). +* +* Rebuild string to make alias->str member comparable. +*/ + ret =0 ; +list_for_each_entry (term, &alias->terms, list) { +if (ret) + ret +=scnprintf (newval + ret,sizeof (newval) - ret, +"," ); +if (term->type_val ==PARSE_EVENTS__TERM_TYPE_NUM ) + ret +=scnprintf (newval + ret,sizeof (newval) - ret, +"%s=%#x" , term->config, term->val.num); +else if (term->type_val ==PARSE_EVENTS__TERM_TYPE_STR ) + ret +=scnprintf (newval + ret,sizeof (newval) - ret, +"%s=%s" , term->config, term->val.str); + } + alias->str =strdup (newval); +list_add_tail (&alias->list, &pmu->aliases);return 0 ; }
@@ -940,6 +950,9 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const charINIT_LIST_HEAD (&pmu->format);INIT_LIST_HEAD (&pmu->aliases);INIT_LIST_HEAD (&pmu->caps); + pmu->name =strdup (name); +if (!pmu->name) + goto err;/* * The pmu data we store & need consists of the pmu * type value and format definitions. Load both right
@@ -958,9 +971,6 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char } pmu->is_core =is_pmu_core (name); pmu->cpus =pmu_cpumask (dirfd, name, pmu->is_core); - pmu->name =strdup (name); -if (!pmu->name) - goto err;/* Read type, and ensure that type value is successfully assigned (return 1) */ if (perf_pmu__scan_file_at (pmu, dirfd,"type" ,"%u" , &type) !=1 )
@@ -1353,17 +1363,6 @@ int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,return perf_pmu__config_terms (pmu, attr, head_terms, zero, err); } -static struct perf_pmu_alias *perf_pmu__find_alias (const struct perf_pmu *pmu,const char *str) -{ -struct perf_pmu_alias *alias; - -list_for_each_entry (alias, &pmu->aliases, list) { -if (!strcasecmp (alias->name, str)) -return alias; - } -return NULL ; -} -static struct perf_pmu_alias *pmu_find_alias (struct perf_pmu *pmu,struct parse_events_term *term) {
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 20/25 perf pmu: Cache json events table
In reply to Ian RogersCache the json events table so that finding it isn't done per event/alias. Change the events table find so that when the PMU is given, if the PMU has no json events return null. Update usage to always use the PMU variable.
--- tools/perf/pmu-events/jevents.py | 14 ++++++++++++-- tools/perf/tests/pmu-events.c | 2 ++ tools/perf/util/pmu.c | 17 ++++++++--------- tools/perf/util/pmu.h | 4 ++++ 4 files changed, 26 insertions(+), 11 deletions(-)
tools/perf/pmu-events/jevents.py +12 -2
@@ -948,7 +948,7 @@ const struct pmu_events_table *perf_pmu__find_events_table(struct perf_pmu *pmu) { const struct pmu_events_table *table = NULL; char *cpuid = perf_pmu__getcpuid(pmu); - int i; + size_t i; /* on some platforms which uses cpus map, cpuid can be NULL for * PMUs other than CORE PMUs.
@@ -968,7 +968,17 @@ const struct pmu_events_table *perf_pmu__find_events_table(struct perf_pmu *pmu) } } free(cpuid); - return table; + if (!pmu) + return table; + + for (i = 0; i < table->num_pmus; i++) { + 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)) + return table; + } + return NULL; } const struct pmu_metrics_table *perf_pmu__find_metrics_table(struct perf_pmu *pmu)
tools/perf/tests/pmu-events.c +2 -0
@@ -544,6 +544,7 @@ static int __test_core_pmu_event_aliases(char *pmu_name, int *count)INIT_LIST_HEAD (&pmu->list); pmu->name =strdup (pmu_name); + pmu->events_table = table;pmu_add_cpu_aliases_table (pmu, table); res =pmu_events_table__find_event (table, pmu,"bp_l1_btb_correct" ,NULL ,NULL );
@@ -583,6 +584,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu) events_table =find_core_events_table ("testarch" ,"testcpu" );if (!events_table)return -1 ; + pmu->events_table = events_table;pmu_add_cpu_aliases_table (pmu, events_table);pmu_add_sys_aliases (pmu);
tools/perf/util/pmu.c +8 -9
@@ -518,10 +518,10 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char *name }if (!pe) {/* Update an event from sysfs with json data. */ -const struct pmu_events_table *table =perf_pmu__find_events_table (pmu); - -if (table) -pmu_events_table__find_event (table, pmu, name, update_alias, alias); +if (pmu->events_table) { +pmu_events_table__find_event (pmu->events_table, pmu, name, + update_alias, alias); + } }/* Scan event and remove leading zeroes, spaces, newlines, some
@@ -871,13 +871,10 @@ void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_tabstatic void pmu_add_cpu_aliases (struct perf_pmu *pmu) { -const struct pmu_events_table *table; - - table =perf_pmu__find_events_table (pmu); -if (!table) +if (!pmu->events_table)return ; -pmu_add_cpu_aliases_table (pmu, table); +pmu_add_cpu_aliases_table (pmu, pmu->events_table); } static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
@@ -988,6 +985,7 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const charif (pmu->is_uncore) pmu->id =pmu_id (name); pmu->max_precise =pmu_max_precise (dirfd, pmu); + pmu->events_table =perf_pmu__find_events_table (pmu);pmu_add_cpu_aliases (pmu);pmu_add_sys_aliases (pmu);list_add_tail (&pmu->list, pmus);
@@ -1001,6 +999,7 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const charreturn NULL ; } +/* Creates the PMU when sysfs scanning fails. */ struct perf_pmu *perf_pmu__create_placeholder_core_pmu (struct list_head *core_pmus) {
tools/perf/util/pmu.h +4 -0
@@ -114,6 +114,10 @@ struct perf_pmu {* from json events in pmu-events.c. */ struct list_head aliases; +/** +* @events_table: The events table for json events in pmu-events.c. +*/ +const struct pmu_events_table *events_table;/** @caps_initialized: Has the list caps been initialized? */ bool caps_initialized;/** @nr_caps: The length of the list caps. */
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 21/25 perf pmu: Lazily add json events
In reply to Ian RogersRather than scanning all json events and adding them when a PMU is created, add the alias when the json event is needed.
Average core PMU scanning run time reduced by 60.2%. Average PMU scanning run time reduced by 15%. Page faults with no events reduced by 74 page faults, 4% of total.
--- tools/perf/pmu-events/empty-pmu-events.c | 14 +++++++ tools/perf/pmu-events/jevents.py | 15 +++++++ tools/perf/pmu-events/pmu-events.h | 4 ++ tools/perf/tests/pmu-events.c | 2 + tools/perf/util/pmu.c | 50 ++++++++++++++++++------ tools/perf/util/pmu.h | 15 +++++-- 6 files changed, 85 insertions(+), 15 deletions(-)
tools/perf/pmu-events/empty-pmu-events.c +14 -0
@@ -298,6 +298,20 @@ int pmu_events_table__find_event(const struct pmu_events_table *table,return -1000 ; } +size_t pmu_events_table__num_events (const struct pmu_events_table *table, +struct perf_pmu *pmu) +{ +size_t count =0 ; + +for (const struct pmu_event *pe = &table->entries[0 ]; pe->name; pe++) { +if (pmu && !pmu__name_match (pmu, pe->pmu)) +continue ; + + count++; + } +return count; +} +int pmu_metrics_table__for_each_metric (const struct pmu_metrics_table *table,pmu_metric_iter_fn fn,void *data) {
tools/perf/pmu-events/jevents.py +15 -0
@@ -909,6 +909,21 @@ int pmu_events_table__find_event(const struct pmu_events_table *table, return -1000; } +size_t pmu_events_table__num_events(const struct pmu_events_table *table, + struct perf_pmu *pmu) +{ + size_t count = 0; + + for (size_t i = 0; i < table->num_pmus; i++) { + 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)) + count += table_pmu->num_entries; + } + return count; +} + static int pmu_metrics_table__for_each_metric_pmu(const struct pmu_metrics_table *table, const struct pmu_table_entry *pmu, pmu_metric_iter_fn fn,
tools/perf/pmu-events/pmu-events.h +4 -0
@@ -3,6 +3,7 @@#define PMU_EVENTS_H #include <stdbool.h> +#include <stddef.h> struct perf_pmu ;
@@ -86,6 +87,9 @@ int pmu_events_table__find_event(const struct pmu_events_table *table,const char *name, pmu_event_iter_fn fn, void *data); +size_t pmu_events_table__num_events (const struct pmu_events_table *table, +struct perf_pmu *pmu); +int pmu_metrics_table__for_each_metric (const struct pmu_metrics_table *table,pmu_metric_iter_fn fn,void *data);
tools/perf/tests/pmu-events.c +2 -0
@@ -546,6 +546,7 @@ static int __test_core_pmu_event_aliases(char *pmu_name, int *count) pmu->events_table = table;pmu_add_cpu_aliases_table (pmu, table); + pmu->cpu_aliases_added = true; res =pmu_events_table__find_event (table, pmu,"bp_l1_btb_correct" ,NULL ,NULL );if (res !=0 ) {
@@ -586,6 +587,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)return -1 ; pmu->events_table = events_table;pmu_add_cpu_aliases_table (pmu, events_table); + pmu->cpu_aliases_added = true;pmu_add_sys_aliases (pmu);/* Count how many aliases we generated */
tools/perf/util/pmu.c +38 -12
110 lines not on this page · raw message
tools/perf/util/pmu.h +12 -3
@@ -118,6 +118,15 @@ struct perf_pmu {* @events_table: The events table for json events in pmu-events.c. */ const struct pmu_events_table *events_table; +/** @sysfs_aliases: Number of sysfs aliases loaded. */ +uint32_t sysfs_aliases; +/** @sysfs_aliases: Number of json event aliases loaded. */ +uint32_t loaded_json_aliases; +/** +* @cpu_aliases_added: Have all json events table entries for the PMU +* been added? +*/ +bool cpu_aliases_added;/** @caps_initialized: Has the list caps been initialized? */ bool caps_initialized;/** @nr_caps: The length of the list caps. */
@@ -199,9 +208,9 @@ bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name);bool is_pmu_core (const char *name);bool perf_pmu__supports_legacy_cache (const struct perf_pmu *pmu);bool perf_pmu__auto_merge_stats (const struct perf_pmu *pmu); -bool perf_pmu__have_event (const struct perf_pmu *pmu,const char *name); -size_t perf_pmu__num_events (const struct perf_pmu *pmu); -int perf_pmu__for_each_event (const struct perf_pmu *pmu,void *state,pmu_event_callback cb); +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,void *state,pmu_event_callback cb);bool pmu__name_match (const struct perf_pmu *pmu,const char *pmu_name);/**
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerQuoted 12 lines · Ian Rogers
Lazily load PMU data both from sysfs and json files. Reorganize json data to be more PMU oriented to facilitate this, for example, json data is now sorted into arrays for their PMU.
In refactoring the code some changes were made to get rid of maximum encoding sizes for events (256 bytes), with input files being directly passed to the lex generated code. There is also a small event parse error message improvement.
Some results from an Intel tigerlake laptop running Debian:
Quoted 8 lines
Binary size reduction of 1.4% or 143,264 bytes because the PMU name no longer appears in the string.
stat -e cpu/cycles/ minor faults reduced from 1733 to 1667, open calls reduced from 171 to 94.
stat default minor faults reduced from 1085 to 1727, open calls reduced from 654 to 343.
s/1085/1805/
Thanks,
Ian
Quoted 54 lines
Average PMU scanning reduced from 4720.641usec to 2927.293usec. Average core PMU scanning reduced from 1004.658usec to 232.668usec (4.3x faster).
Ian Rogers (25):
perf script ibs: Remove unused include
perf pmu: Avoid a path name copy
perf pmu: Moveperf_pmu__set_formatto pmu.y
perf pmu: Reduce scope ofperf_pmu_error
perf pmu: Avoid passing format list toperf_pmu__config_termsperf pmu: Avoid passing format list toperf_pmu__format_typeperf pmu: Avoid passing format list toperf_pmu__format_bitsperf pmu: Pass PMU rather than aliases and format perf pmu: Make the loading of formats lazy
perf pmu: Abstract alias/event struct
perf pmu-events: Add extra underscore to function names perf jevents: Group events by PMU
perf parse-events: Improve error message for double setting perf s390s390_cpumcfdg_dump: Don't scan all PMUs perf pmu-events: Reduce processed events by passing PMU perf pmu-events: Addpmu_events_table__find_eventperf pmu: Parse sysfs events directly from a file perf pmu: Prefer passing pmu to aliases list
perf pmu: Merge json events with sysfs at load time perf pmu: Cache json events table
perf pmu: Lazily add json events
perf pmu: Scan type early to fail an invalid PMU quickly perf pmu: Be lazy about loading event info files from sysfs perf pmu: Lazily load sysfs aliases
perf jevents: Sort strings in the big C string to reduce faultstools/perf/arch/x86/util/intel-pt.c | 39 +- tools/perf/bench/pmu-scan.c | 8 +- tools/perf/pmu-events/empty-pmu-events.c | 49 +- tools/perf/pmu-events/jevents.py | 319 +++++++-- tools/perf/pmu-events/pmu-events.h | 15 +- tools/perf/tests/parse-events.c | 2 +- tools/perf/tests/pmu-events.c | 183 ++--- tools/perf/tests/pmu.c | 76 +- tools/perf/util/amd-sample-raw.c | 1 - tools/perf/util/metricgroup.c | 10 +- tools/perf/util/parse-events.c | 91 ++- tools/perf/util/parse-events.h | 3 +- tools/perf/util/pmu.c | 872 +++++++++++++++-------- tools/perf/util/pmu.h | 110 ++- tools/perf/util/pmu.y | 32 +- tools/perf/util/pmus.c | 230 ++---- tools/perf/util/s390-sample-raw.c | 50 +- 17 files changed, 1251 insertions(+), 839 deletions(-)--
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 22/25 perf pmu: Scan type early to fail an invalid PMU quickly
In reply to Ian RogersScan sysfs PMU's type early so that format and aliases aren't attempted to be loaded if the PMU name is invalid. This is the case for event_pmu tokens in parse-events.y where a wildcard name is first assumed to be a PMU name.
--- tools/perf/util/pmu.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
tools/perf/util/pmu.c +12 -8
@@ -953,12 +953,21 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const charif (!pmu)return NULL ; -INIT_LIST_HEAD (&pmu->format); -INIT_LIST_HEAD (&pmu->aliases); -INIT_LIST_HEAD (&pmu->caps); pmu->name =strdup (name);if (!pmu->name) goto err; + +/* +* Read type early to fail fast if a lookup name isn't a PMU. Ensure +* that type value is successfully assigned (return 1). +*/ +if (perf_pmu__scan_file_at (pmu, dirfd,"type" ,"%u" , &type) !=1 ) + goto err; + +INIT_LIST_HEAD (&pmu->format); +INIT_LIST_HEAD (&pmu->aliases); +INIT_LIST_HEAD (&pmu->caps); +/* * The pmu data we store & need consists of the pmu * type value and format definitions. Load both right
@@ -978,10 +987,6 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char pmu->is_core =is_pmu_core (name); pmu->cpus =pmu_cpumask (dirfd, name, pmu->is_core); -/* Read type, and ensure that type value is successfully assigned (return 1) */ -if (perf_pmu__scan_file_at (pmu, dirfd,"type" ,"%u" , &type) !=1 ) - goto err; - alias_name =pmu_find_alias_name (name);if (alias_name) { pmu->alias_name =strdup (alias_name);
@@ -1007,7 +1012,6 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const charreturn NULL ; } -/* Creates the PMU when sysfs scanning fails. */ struct perf_pmu *perf_pmu__create_placeholder_core_pmu (struct list_head *core_pmus) {
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 23/25 perf pmu: Be lazy about loading event info files from sysfs
In reply to Ian RogersEvent info is only needed when an event is parsed or when merging data from an json and sysfs event. Be lazy in its loading to reduce file accesses.
--- tools/perf/util/pmu.c | 128 +++++++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 45 deletions(-)
tools/perf/util/pmu.c +83 -45
@@ -58,6 +58,11 @@ struct perf_pmu_alias {struct list_head terms;/** @list: List element of struct perf_pmu aliases. */ struct list_head list; +/** +* @pmu_name: The name copied from the json struct pmu_event. This can +* differ from the PMU name as it won't have suffixes. +*/ +char *pmu_name;/** @unit: Units for the event, such as bytes or cache lines. */ char unit[UNIT_MAX_LEN +1 ];/** @scale: Value to scale read counter values by. */
@@ -79,11 +84,10 @@ struct perf_pmu_alias {* default. */ bool deprecated; -/** -* @pmu_name: The name copied from the json struct pmu_event. This can -* differ from the PMU name as it won't have suffixes. -*/ -char *pmu_name; +/** @from_sysfs: Was the alias from sysfs or a json event? */ +bool from_sysfs; +/** @info_loaded: Have the scale, unit and other values been read from disk? */ +bool info_loaded; };/**
@@ -276,17 +280,21 @@ int perf_pmu__convert_scale(const char *scale, char **end, double *sval)return ret; } -static int perf_pmu__parse_scale (struct perf_pmu_alias *alias,int dirfd,const char *name) +static int perf_pmu__parse_scale (struct perf_pmu *pmu,struct perf_pmu_alias *alias) {struct stat st;ssize_t sret; +size_t len;char scale[128 ];int fd, ret =-1 ;char path[PATH_MAX ]; -scnprintf (path,PATH_MAX ,"%s.scale" , name); + len =perf_pmu__event_source_devices_scnprintf (path,sizeof (path)); +if (!len) +return 0 ; +scnprintf (path + len,sizeof (path) - len,"%s/%s.scale" , pmu->name, alias->name); - fd =openat (dirfd, path,O_RDONLY ); + fd =open (path,O_RDONLY );if (fd ==-1 )return -1 ;
@@ -308,15 +316,20 @@ static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, constreturn ret; } -static int perf_pmu__parse_unit (struct perf_pmu_alias *alias,int dirfd,const char *name) +static int perf_pmu__parse_unit (struct perf_pmu *pmu,struct perf_pmu_alias *alias) {char path[PATH_MAX ]; +size_t len;ssize_t sret;int fd; -scnprintf (path,PATH_MAX ,"%s.unit" , name); - fd =openat (dirfd, path,O_RDONLY ); + len =perf_pmu__event_source_devices_scnprintf (path,sizeof (path)); +if (!len) +return 0 ; +scnprintf (path + len,sizeof (path) - len,"%s/%s.unit" , pmu->name, alias->name); + + fd =open (path,O_RDONLY );if (fd ==-1 )return -1 ;
@@ -339,14 +352,18 @@ static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, const c }static int -perf_pmu__parse_per_pkg (struct perf_pmu_alias *alias,int dirfd,const char *name) +perf_pmu__parse_per_pkg (struct perf_pmu *pmu,struct perf_pmu_alias *alias) {char path[PATH_MAX ]; +size_t len;int fd; -scnprintf (path,PATH_MAX ,"%s.per-pkg" , name); + len =perf_pmu__event_source_devices_scnprintf (path,sizeof (path)); +if (!len) +return 0 ; +scnprintf (path + len,sizeof (path) - len,"%s/%s.per-pkg" , pmu->name, alias->name); - fd =openat (dirfd, path,O_RDONLY ); + fd =open (path,O_RDONLY );if (fd ==-1 )return -1 ;
@@ -356,15 +373,18 @@ perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, const char *namreturn 0 ; } -static int perf_pmu__parse_snapshot (struct perf_pmu_alias *alias, -int dirfd,const char *name) +static int perf_pmu__parse_snapshot (struct perf_pmu *pmu,struct perf_pmu_alias *alias) {char path[PATH_MAX ]; +size_t len;int fd; -scnprintf (path,PATH_MAX ,"%s.snapshot" , name); + len =perf_pmu__event_source_devices_scnprintf (path,sizeof (path)); +if (!len) +return 0 ; +scnprintf (path + len,sizeof (path) - len,"%s/%s.snapshot" , pmu->name, alias->name); - fd =openat (dirfd, path,O_RDONLY ); + fd =open (path,O_RDONLY );if (fd ==-1 )return -1 ;
@@ -425,32 +445,52 @@ static bool assign_str(const char *name, const char *field, char **old_str,return true; } +static void read_alias_info (struct perf_pmu *pmu,struct perf_pmu_alias *alias) +{ +if (!alias->from_sysfs || alias->info_loaded) +return ; + +/* +* load unit name and scale if available +*/ +perf_pmu__parse_unit (pmu, alias); +perf_pmu__parse_scale (pmu, alias); +perf_pmu__parse_per_pkg (pmu, alias); +perf_pmu__parse_snapshot (pmu, alias); +} + +struct update_alias_data { +struct perf_pmu *pmu; +struct perf_pmu_alias *alias; +}; +static int update_alias (const struct pmu_event *pe,const struct pmu_events_table *table__maybe_unused ,void *vdata) { -struct perf_pmu_alias *alias = vdata; +struct update_alias_data *data = vdata;int ret =0 ; -assign_str (pe->name,"desc" , &alias->desc, pe->desc); -assign_str (pe->name,"long_desc" , &alias->long_desc, pe->long_desc); -assign_str (pe->name,"topic" , &alias->topic, pe->topic); - alias->per_pkg = pe->perpkg; -if (assign_str (pe->name,"value" , &alias->str, pe->event)) { -parse_events_terms__purge (&alias->terms); - ret =parse_events_terms (&alias->terms, pe->event,/*input=*/ NULL ); +read_alias_info (data->pmu, data->alias); +assign_str (pe->name,"desc" , &data->alias->desc, pe->desc); +assign_str (pe->name,"long_desc" , &data->alias->long_desc, pe->long_desc); +assign_str (pe->name,"topic" , &data->alias->topic, pe->topic); + data->alias->per_pkg = pe->perpkg; +if (assign_str (pe->name,"value" , &data->alias->str, pe->event)) { +parse_events_terms__purge (&data->alias->terms); + ret =parse_events_terms (&data->alias->terms, pe->event,/*input=*/ NULL ); }if (!ret && pe->unit) {char *unit; - ret =perf_pmu__convert_scale (pe->unit, &unit, &alias->scale); + ret =perf_pmu__convert_scale (pe->unit, &unit, &data->alias->scale);if (!ret) -snprintf (alias->unit,sizeof (alias->unit),"%s" , unit); +snprintf (data->alias->unit,sizeof (data->alias->unit),"%s" , unit); }return ret; } -static int perf_pmu__new_alias (struct perf_pmu *pmu,int dirfd,const char *name, +static int perf_pmu__new_alias (struct perf_pmu *pmu,const char *name,const char *desc,const char *val,FILE *val_fd,const struct pmu_event *pe) {
@@ -494,16 +534,6 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char *name } alias->name =strdup (name); -if (dirfd >=0 ) { -/* -* load unit name and scale if available -*/ -perf_pmu__parse_unit (alias, dirfd, name); -perf_pmu__parse_scale (alias, dirfd, name); -perf_pmu__parse_per_pkg (alias, dirfd, name); -perf_pmu__parse_snapshot (alias, dirfd, name); - } - alias->desc = desc ?strdup (desc) :NULL ; alias->long_desc = long_desc ?strdup (long_desc) : desc ?strdup (desc) :NULL ;
@@ -518,9 +548,15 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char *name }if (!pe) {/* Update an event from sysfs with json data. */ +struct update_alias_data data = { + .pmu = pmu, + .alias = alias, + }; + + alias->from_sysfs = true;if (pmu->events_table) {if (pmu_events_table__find_event (pmu->events_table, pmu, name, - update_alias, alias) ==0 ) + update_alias, &data) ==0 ) pmu->loaded_json_aliases++; } }
@@ -608,7 +644,7 @@ static int pmu_aliases_parse(struct perf_pmu *pmu, int dirfd)continue ; } -if (perf_pmu__new_alias (pmu, dirfd, name,/*desc=*/ NULL , +if (perf_pmu__new_alias (pmu, name,/*desc=*/ NULL ,/*val=*/ NULL , file,/*pe=*/ NULL ) <0 )pr_debug ("Cannot set up %s\n" , name);fclose (file);
@@ -861,7 +897,7 @@ static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, {struct perf_pmu *pmu = vdata; -perf_pmu__new_alias (pmu,-1 , pe->name, pe->desc, pe->event,/*val_fd=*/ NULL , pe); +perf_pmu__new_alias (pmu, pe->name, pe->desc, pe->event,/*val_fd=*/ NULL , pe);return 0 ; }
@@ -897,7 +933,7 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,if (!strcmp (pmu->id, pe->compat) &&pmu_uncore_alias_match (pe->pmu, pmu->name)) { - perf_pmu__new_alias(pmu, -1, + perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event,
@@ -1413,11 +1449,13 @@ static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, } -static int check_info_data (struct perf_pmu_alias *alias, +static int check_info_data (struct perf_pmu *pmu, +struct perf_pmu_alias *alias,struct perf_pmu_info *info,struct parse_events_error *err,int column) { +read_alias_info (pmu, alias);/* * Only one term in event definition can * define unit, scale and snapshot, fail
@@ -1487,7 +1525,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,return ret; } - ret =check_info_data (alias, info, err, term->err_term); + ret =check_info_data (pmu, alias, info, err, term->err_term);if (ret)return ret;
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 24/25 perf pmu: Lazily load sysfs aliases
In reply to Ian RogersDon't load sysfs aliases for a PMU when the PMU is first created, defer until an alias needs to be found. For the pmu-scan benchmark, average core PMU scanning is reduced by 30.8%, and average PMU scanning by 12.6%.
--- tools/perf/tests/pmu-events.c | 2 + tools/perf/util/pmu.c | 81 ++++++++++++++++++----------------- tools/perf/util/pmu.h | 2 + 3 files changed, 46 insertions(+), 39 deletions(-)
tools/perf/tests/pmu-events.c +2 -0
@@ -547,6 +547,7 @@ static int __test_core_pmu_event_aliases(char *pmu_name, int *count) pmu->events_table = table;pmu_add_cpu_aliases_table (pmu, table); pmu->cpu_aliases_added = true; + pmu->sysfs_aliases_loaded = true; res =pmu_events_table__find_event (table, pmu,"bp_l1_btb_correct" ,NULL ,NULL );if (res !=0 ) {
@@ -588,6 +589,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu) pmu->events_table = events_table;pmu_add_cpu_aliases_table (pmu, events_table); pmu->cpu_aliases_added = true; + pmu->sysfs_aliases_loaded = true;pmu_add_sys_aliases (pmu);/* Count how many aliases we generated */
tools/perf/util/pmu.c +42 -39
156 lines not on this page · raw message
tools/perf/util/pmu.h +2 -0
@@ -122,6 +122,8 @@ struct perf_pmu {uint32_t sysfs_aliases;/** @sysfs_aliases: Number of json event aliases loaded. */ uint32_t loaded_json_aliases; +/** @sysfs_aliases_loaded: Are sysfs aliases loaded from disk? */ +bool sysfs_aliases_loaded;/** * @cpu_aliases_added: Have all json events table entries for the PMU * been added?
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Ian Rogers
reviewerPATCH 25/25 perf jevents: Sort strings in the big C string to reduce faults
In reply to Ian RogersSort the strings within the big C string based on whether they were for a metric and then by when they were added. This helps group related strings and reduce minor faults by approximately 10 in 1740, about 0.57%.
--- tools/perf/pmu-events/jevents.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
tools/perf/pmu-events/jevents.py +23 -8
@@ -113,13 +113,24 @@ class BigCString: strings: Set[str] big_string: Sequence[str] offsets: Dict[str, int] + insert_number: int + insert_point: Dict[str, int] + metrics: Set[str] def __init__(self): self.strings = set() + self.insert_number = 0; + self.insert_point = {} + self.metrics = set() - def add(self, s: str) -> None: + def add(self, s: str, metric: bool) -> None: """Called to add to the big string.""" - self.strings.add(s) + if s not in self.strings: + self.strings.add(s) + self.insert_point[s] = self.insert_number + self.insert_number += 1 + if metric: + self.metrics.add(s) def compute(self) -> None: """Called once all strings are added to compute the string and offsets."""
@@ -160,8 +171,11 @@ class BigCString: self.big_string = [] self.offsets = {} + def string_cmp_key(s: str) -> Tuple[bool, int, str]: + return (s in self.metrics, self.insert_point[s], s) + # Emit all strings that aren't folded in a sorted manner. - for s in sorted(self.strings): + for s in sorted(self.strings, key=string_cmp_key): if s not in folded_strings: self.offsets[s] = big_string_offset self.big_string.append(f'/* offset={big_string_offset} */ "')
@@ -574,19 +588,20 @@ def preprocess_one_file(parents: Sequence[str], item: os.DirEntry) -> None: assert len(mgroup) > 1, parents description = f"{metricgroup_descriptions[mgroup]}\\000" mgroup = f"{mgroup}\\000" - _bcs.add(mgroup) - _bcs.add(description) + _bcs.add(mgroup, metric=True) + _bcs.add(description, metric=True) _metricgroups[mgroup] = description return topic = get_topic(item.name) for event in read_json_events(item.path, topic): pmu_name = f"{event.pmu}\\000" - _bcs.add(pmu_name) if event.name: - _bcs.add(event.build_c_string(metric=False)) + _bcs.add(pmu_name, metric=False) + _bcs.add(event.build_c_string(metric=False), metric=False) if event.metric_name: - _bcs.add(event.build_c_string(metric=True)) + _bcs.add(pmu_name, metric=True) + _bcs.add(event.build_c_string(metric=True), metric=True) def process_one_file(parents: Sequence[str], item: os.DirEntry) -> None: """Process a JSON file during the main walk."""
Signature · 1 line
2.42.0.rc1.204.g551eb34607-goog
Arnaldo Carvalho de Melo
maintainerEm Wed, Aug 23, 2023 at 01:08:03AM -0700, Ian Rogers escreveu:
Quoted 17 lines
Lazily load PMU data both from sysfs and json files. Reorganize json data to be more PMU oriented to facilitate this, for example, json data is now sorted into arrays for their PMU.
In refactoring the code some changes were made to get rid of maximum encoding sizes for events (256 bytes), with input files being directly passed to the lex generated code. There is also a small event parse error message improvement.
Some results from an Intel tigerlake laptop running Debian:
Binary size reduction of 1.4% or 143,264 bytes because the PMU name no longer appears in the string.
stat -e cpu/cycles/ minor faults reduced from 1733 to 1667, open calls reduced from 171 to 94.
Quoted 6 lines
stat default minor faults reduced from 1085 to 1727, open calls reduced from 654 to 343.
Average PMU scanning reduced from 4720.641usec to 2927.293usec. Average core PMU scanning reduced from 1004.658usec to 232.668usec (4.3x faster).
I'm now chasing this one when building it on ubuntu arm64
CC /tmp/build/perf/util/env.oarch/arm64/util/../../arm/util/cs-etm.c: In function 'cs_etm_validate_context_id':arch/arm64/util/../../arm/util/cs-etm.c:82:26: error: passing argument 1 of 'perf_pmu__format_bits' from incompatible pointer type [-Werror=incompatible-pointer-types]
(perf_pmu__format_bits(&cs_etm_pmu->format, "contextid") |
^
In file included from arch/arm64/util/../../arm/util/../../../util/header.h:13:0,
from arch/arm64/util/../../arm/util/../../../util/session.h:7,
from arch/arm64/util/../../arm/util/cs-etm.c:31:
arch/arm64/util/../../arm/util/../../../util/pmu.h:224:7: note: expected 'struct perf_pmu *' but argument is of type 'struct list_head *'
__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);
^~~~~~~~~~~~~~~~~~~~~
arch/arm64/util/../../arm/util/cs-etm.c:83:26: error: passing argument 1 of 'perf_pmu__format_bits' from incompatible pointer type [-Werror=incompatible-pointer-types]
perf_pmu__format_bits(&cs_etm_pmu->format, "contextid1") |
^
In file included from arch/arm64/util/../../arm/util/../../../util/header.h:13:0,
from arch/arm64/util/../../arm/util/../../../util/session.h:7,
from arch/arm64/util/../../arm/util/cs-etm.c:31:
arch/arm64/util/../../arm/util/../../../util/pmu.h:224:7: note: expected 'struct perf_pmu *' but argument is of type 'struct list_head *'
__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);
^~~~~~~~~~~~~~~~~~~~~
arch/arm64/util/../../arm/util/cs-etm.c:84:26: error: passing argument 1 of 'perf_pmu__format_bits' from incompatible pointer type [-Werror=incompatible-pointer-types]
perf_pmu__format_bits(&cs_etm_pmu->format, "contextid2"));
^
In file included from arch/arm64/util/../../arm/util/../../../util/header.h:13:0,
from arch/arm64/util/../../arm/util/../../../util/session.h:7,
from arch/arm64/util/../../arm/util/cs-etm.c:31:
arch/arm64/util/../../arm/util/../../../util/pmu.h:224:7: note: expected 'struct perf_pmu *' but argument is of type 'struct list_head *'
__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);
^~~~~~~~~~~~~~~~~~~~~
arch/arm64/util/../../arm/util/cs-etm.c:109:28: error: passing argument 1 of 'perf_pmu__format_bits' from incompatible pointer type [-Werror=incompatible-pointer-types]
perf_pmu__format_bits(&cs_etm_pmu->format, "contextid1")) {
^
In file included from arch/arm64/util/../../arm/util/../../../util/header.h:13:0,
from arch/arm64/util/../../arm/util/../../../util/session.h:7,
from arch/arm64/util/../../arm/util/cs-etm.c:31:
arch/arm64/util/../../arm/util/../../../util/pmu.h:224:7: note: expected 'struct perf_pmu *' but argument is of type 'struct list_head *'
__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);
^~~~~~~~~~~~~~~~~~~~~
arch/arm64/util/../../arm/util/cs-etm.c:125:28: error: passing argument 1 of 'perf_pmu__format_bits' from incompatible pointer type [-Werror=incompatible-pointer-types]
perf_pmu__format_bits(&cs_etm_pmu->format, "contextid2")) {
^
In file included from arch/arm64/util/../../arm/util/../../../util/header.h:13:0,
from arch/arm64/util/../../arm/util/../../../util/session.h:7,
from arch/arm64/util/../../arm/util/cs-etm.c:31:
arch/arm64/util/../../arm/util/../../../util/pmu.h:224:7: note: expected 'struct perf_pmu *' but argument is of type 'struct list_head *'
__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);
^~~~~~~~~~~~~~~~~~~~~
CC /tmp/build/perf/bench/epoll-ctl.o
arch/arm64/util/../../arm/util/cs-etm.c: In function 'cs_etm_validate_timestamp':
arch/arm64/util/../../arm/util/cs-etm.c:154:30: error: passing argument 1 of 'perf_pmu__format_bits' from incompatible pointer type [-Werror=incompatible-pointer-types]
perf_pmu__format_bits(&cs_etm_pmu->format, "timestamp")))
^
In file included from arch/arm64/util/../../arm/util/../../../util/header.h:13:0,
from arch/arm64/util/../../arm/util/../../../util/session.h:7,
from arch/arm64/util/../../arm/util/cs-etm.c:31:
arch/arm64/util/../../arm/util/../../../util/pmu.h:224:7: note: expected 'struct perf_pmu *' but argument is of type 'struct list_head *'
__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);
^~~~~~~~~~~~~~~~~~~~~
CC /tmp/build/perf/tests/vmlinux-kallsyms.o
CC /tmp/build/perf/arch/arm64/util/mem-events.o
CC /tmp/build/perf/bench/synthesize.o
CC /tmp/build/perf/tests/perf-record.o
CC /tmp/build/perf/bench/kallsyms-parse.o
arch/arm64/util/arm-spe.c: In function 'arm_spe_recording_options':
arch/arm64/util/arm-spe.c:233:30: error: passing argument 1 of 'perf_pmu__format_bits' from incompatible pointer type [-Werror=incompatible-pointer-types]
bit = perf_pmu__format_bits(&arm_spe_pmu->format, "pa_enable");
^
In file included from arch/arm64/util/../../../util/header.h:13:0,
from arch/arm64/util/../../../util/session.h:7,
from arch/arm64/util/arm-spe.c:19:
arch/arm64/util/../../../util/pmu.h:224:7: note: expected 'struct perf_pmu *' but argument is of type 'struct list_head *'
__u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);
^~~~~~~~~~~~~~~~~~~~~
CC /tmp/build/perf/bench/find-bit-bench.o
CC /tmp/build/perf/tests/evsel-roundtrip-name.o
CC /tmp/build/perf/bench/inject-buildid.o
CC /tmp/build/perf/util/event.oA message that is not in the archive (sent off-list or not yet received): CAP-5=fXYDMo6GgSaLuC3YMNr66yAXLMyZoAOMpdgmMb=xazCOw@mail.gmail.com
Arnaldo Carvalho de Melo
maintainerEm Wed, Aug 23, 2023 at 09:45:50AM -0700, Ian Rogers escreveu:
Quoted 25 lines
Quoted 22 lines · Arnaldo Carvalho de Melo
Em Wed, Aug 23, 2023 at 01:08:03AM -0700, Ian Rogers escreveu:
Quoted 9 lines
Lazily load PMU data both from sysfs and json files. Reorganize json data to be more PMU oriented to facilitate this, for example, json data is now sorted into arrays for their PMU.
In refactoring the code some changes were made to get rid of maximum encoding sizes for events (256 bytes), with input files being directly passed to the lex generated code. There is also a small event parse error message improvement.
Quoted 6 lines
Some results from an Intel tigerlake laptop running Debian:
Binary size reduction of 1.4% or 143,264 bytes because the PMU name no longer appears in the string.
stat -e cpu/cycles/ minor faults reduced from 1733 to 1667, open calls
reduced
Quoted 5 lines
from 171 to 94.
stat default minor faults reduced from 1085 to 1727, open calls reduced from 654 to 343.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Average PMU scanning reduced from 4720.641usec to 2927.293usec. Average core PMU scanning reduced from 1004.658usec to 232.668usec (4.3x faster).
I'm now chasing this one when building it on ubuntu arm64
I'll fix and send a v2.
Its fixed already, I'm pushing it to tmp.perf-tools-next
tools/perf/arch/arm/util/cs-etm.c +6 -6
@@ -79,9 +79,9 @@ static int cs_etm_validate_context_id(struct auxtrace_record *itr,int err;u32 val;u64 contextid = evsel->core.attr.config & - (perf_pmu__format_bits (&cs_etm_pmu->format,"contextid" ) | -perf_pmu__format_bits (&cs_etm_pmu->format,"contextid1" ) | -perf_pmu__format_bits (&cs_etm_pmu->format,"contextid2" )); + (perf_pmu__format_bits (cs_etm_pmu,"contextid" ) | +perf_pmu__format_bits (cs_etm_pmu,"contextid1" ) | +perf_pmu__format_bits (cs_etm_pmu,"contextid2" ));if (!contextid)return 0 ;
@@ -106,7 +106,7 @@ static int cs_etm_validate_context_id(struct auxtrace_record *itr, }if (contextid & -perf_pmu__format_bits (&cs_etm_pmu->format,"contextid1" )) { +perf_pmu__format_bits (cs_etm_pmu,"contextid1" )) {/* * TRCIDR2.CIDSIZE, bit [9-5], indicates whether contextID * tracing is supported:
@@ -122,7 +122,7 @@ static int cs_etm_validate_context_id(struct auxtrace_record *itr, }if (contextid & -perf_pmu__format_bits (&cs_etm_pmu->format,"contextid2" )) { +perf_pmu__format_bits (cs_etm_pmu,"contextid2" )) {/* * TRCIDR2.VMIDOPT[30:29] != 0 and * TRCIDR2.VMIDSIZE[14:10] == 0b00100 (32bit virtual contextid)
@@ -151,7 +151,7 @@ static int cs_etm_validate_timestamp(struct auxtrace_record *itr,u32 val;if (!(evsel->core.attr.config & -perf_pmu__format_bits (&cs_etm_pmu->format,"timestamp" ))) +perf_pmu__format_bits (cs_etm_pmu,"timestamp" )))return 0 ;if (!cs_etm_is_etmv4 (itr, cpu)) {
Ian Rogers
reviewerQuoted 31 lines · Arnaldo Carvalho de Melo
Em Wed, Aug 23, 2023 at 09:45:50AM -0700, Ian Rogers escreveu:
Quoted 29 lines
Quoted 26 lines · Arnaldo Carvalho de Melo
Em Wed, Aug 23, 2023 at 01:08:03AM -0700, Ian Rogers escreveu:
Quoted 9 lines
Lazily load PMU data both from sysfs and json files. Reorganize json data to be more PMU oriented to facilitate this, for example, json data is now sorted into arrays for their PMU.
In refactoring the code some changes were made to get rid of maximum encoding sizes for events (256 bytes), with input files being directly passed to the lex generated code. There is also a small event parse error message improvement.
Quoted 6 lines
Some results from an Intel tigerlake laptop running Debian:
Binary size reduction of 1.4% or 143,264 bytes because the PMU name no longer appears in the string.
stat -e cpu/cycles/ minor faults reduced from 1733 to 1667, open calls
reduced
Quoted 8 lines
from 171 to 94.
stat default minor faults reduced from 1085 to 1727, open calls reduced from 654 to 343.
Average PMU scanning reduced from 4720.641usec to 2927.293usec. Average core PMU scanning reduced from 1004.658usec to 232.668usec (4.3x faster).
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I'm now chasing this one when building it on ubuntu arm64
I'll fix and send a v2.
Its fixed already, I'm pushing it to tmp.perf-tools-next
Thanks!
Ian
Quoted 44 lines
tools/perf/arch/arm/util/cs-etm.c +6
-6@@ -79,9 +79,9 @@ static int cs_etm_validate_context_id(struct auxtrace_record *itr,int err;u32 val;u64 contextid = evsel->core.attr.config & - (perf_pmu__format_bits (&cs_etm_pmu->format,"contextid" ) | -perf_pmu__format_bits (&cs_etm_pmu->format,"contextid1" ) | -perf_pmu__format_bits (&cs_etm_pmu->format,"contextid2" )); + (perf_pmu__format_bits (cs_etm_pmu,"contextid" ) | +perf_pmu__format_bits (cs_etm_pmu,"contextid1" ) | +perf_pmu__format_bits (cs_etm_pmu,"contextid2" ));if (!contextid)return 0 ;@@ -106,7 +106,7 @@ static int cs_etm_validate_context_id(struct auxtrace_record *itr, }if (contextid & -perf_pmu__format_bits (&cs_etm_pmu->format,"contextid1" )) { +perf_pmu__format_bits (cs_etm_pmu,"contextid1" )) {/* * TRCIDR2.CIDSIZE, bit [9-5], indicates whether contextID * tracing is supported: @@ -122,7 +122,7 @@ static int cs_etm_validate_context_id(struct auxtrace_record *itr, }if (contextid & -perf_pmu__format_bits (&cs_etm_pmu->format,"contextid2" )) { +perf_pmu__format_bits (cs_etm_pmu,"contextid2" )) {/* * TRCIDR2.VMIDOPT[30:29] != 0 and * TRCIDR2.VMIDSIZE[14:10] == 0b00100 (32bit virtual contextid) @@ -151,7 +151,7 @@ static int cs_etm_validate_timestamp(struct auxtrace_record *itr,u32 val;if (!(evsel->core.attr.config & -perf_pmu__format_bits (&cs_etm_pmu->format,"timestamp" ))) +perf_pmu__format_bits (cs_etm_pmu,"timestamp" )))return 0 ;if (!cs_etm_is_etmv4 (itr, cpu)) {