From c8f3a23df9a8f60a863056dfb06f49e34ffe9a90 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Tue, 11 Oct 2022 11:38:13 +0000 Subject: [PATCH 1/3] libpmc: Allow using generic event aliases on Morello Since we don't have a .json pmu event description file for Morello, the pmc_pmu_enabled() check always fails and we don't use pmc_pmu_pmcallocate() which would check for aliases. --- lib/libpmc/libpmc.c | 6 ++++++ lib/libpmc/libpmc_pmu_util.c | 27 ++++++++++++++------------- lib/libpmc/libpmcinternal.h | 1 + 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/libpmc/libpmc.c b/lib/libpmc/libpmc.c index 0775887ce734..c90a6cd2706c 100644 --- a/lib/libpmc/libpmc.c +++ b/lib/libpmc/libpmc.c @@ -1114,6 +1114,12 @@ pmc_allocate(const char *ctrspec, enum pmc_mode mode, break; } + /* Try pmu_alias_get as another generic fallback/ */ + if (spec_copy == NULL) { + const char* alias_name = _pmu_alias_get(ctrspec); + if (alias_name) + spec_copy = strdup(alias_name); + } if (spec_copy == NULL) spec_copy = strdup(ctrspec); diff --git a/lib/libpmc/libpmc_pmu_util.c b/lib/libpmc/libpmc_pmu_util.c index 1ae0c573d157..649154815d6b 100644 --- a/lib/libpmc/libpmc_pmu_util.c +++ b/lib/libpmc/libpmc_pmu_util.c @@ -45,6 +45,7 @@ #include #include #include +#include "libpmcinternal.h" #include "pmu-events/pmu-events.h" struct pmu_alias { @@ -132,8 +133,8 @@ pmu_events_mfr(void) * */ -static const char * -pmu_alias_get(const char *name) +const char * +_pmu_alias_get(const char *name) { pmu_mfr_t mfr; struct pmu_alias *pa; @@ -156,8 +157,8 @@ pmu_alias_get(const char *name) } #elif defined(__powerpc64__) -static const char * -pmu_alias_get(const char *name) +const char * +_pmu_alias_get(const char *name) { return (name); } @@ -183,8 +184,8 @@ static struct pmu_alias pmu_armv8_alias_table[] = { {NULL, NULL}, }; -static const char * -pmu_alias_get(const char *name) +const char * +_pmu_alias_get(const char *name) { struct pmu_alias *pa; @@ -197,8 +198,8 @@ pmu_alias_get(const char *name) #else -static const char * -pmu_alias_get(const char *name) +const char * +_pmu_alias_get(const char *name) { return (name); @@ -284,7 +285,7 @@ pmc_pmu_idx_get_by_event(const char *cpuid, const char *event) int idx; const char *realname; - realname = pmu_alias_get(event); + realname = _pmu_alias_get(event); if (pmu_event_get(cpuid, realname, &idx) == NULL) return (-1); return (idx); @@ -365,7 +366,7 @@ pmc_pmu_sample_rate_get(const char *event_name) const struct pmu_event *pe; struct pmu_event_desc ped; - event_name = pmu_alias_get(event_name); + event_name = _pmu_alias_get(event_name); if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL) return (DEFAULT_SAMPLE_COUNT); if (pe->event == NULL) @@ -587,7 +588,7 @@ pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) bzero(&pm->pm_md, sizeof(pm->pm_md)); pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); - event_name = pmu_alias_get(event_name); + event_name = _pmu_alias_get(event_name); if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) return (ENOENT); assert(idx >= 0); @@ -615,7 +616,7 @@ pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) bzero(&pm->pm_md, sizeof(pm->pm_md)); pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); - event_name = pmu_alias_get(event_name); + event_name = _pmu_alias_get(event_name); if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) return (ENOENT); @@ -640,7 +641,7 @@ pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) struct pmu_event_desc ped; int idx = -1; - event_name = pmu_alias_get(event_name); + event_name = _pmu_alias_get(event_name); if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) return (ENOENT); if (pe->event == NULL) diff --git a/lib/libpmc/libpmcinternal.h b/lib/libpmc/libpmcinternal.h index 0e7c6631b00d..b995989298ec 100644 --- a/lib/libpmc/libpmcinternal.h +++ b/lib/libpmc/libpmcinternal.h @@ -35,5 +35,6 @@ * Prototypes. */ const char *_pmc_name_of_event(enum pmc_event _ev, enum pmc_cputype _cpu); +const char *_pmu_alias_get(const char *name); #endif /* LIBPMC_INTERNAL_H */ From 80ee18c0886381293d5b58b8d7e21e5b3932266c Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Tue, 11 Oct 2022 11:38:45 +0000 Subject: [PATCH 2/3] libpmc: Add "cycles" event alias for armv8 --- lib/libpmc/libpmc_pmu_util.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/libpmc/libpmc_pmu_util.c b/lib/libpmc/libpmc_pmu_util.c index 649154815d6b..cc9f60941cf9 100644 --- a/lib/libpmc/libpmc_pmu_util.c +++ b/lib/libpmc/libpmc_pmu_util.c @@ -176,6 +176,7 @@ static struct pmu_alias pmu_armv8_alias_table[] = { {"BRANCH-INSTRUCTION-RETIRED", "BR_RETIRED"}, {"BRANCH_MISSES_RETIRED", "BR_MIS_PRED_RETIRED"}, {"BRANCH-MISSES-RETIRED", "BR_MIS_PRED_RETIRED"}, + {"cycles", "CPU_CYCLES"}, {"unhalted-cycles", "CPU_CYCLES"}, {"instructions", "INST_RETIRED",}, {"branch-mispredicts", "BR_MIS_PRED_RETIRED"}, From 8b5c5461303e32e3c61d7d5c49d62360caac9c41 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Tue, 11 Oct 2022 12:28:43 +0000 Subject: [PATCH 3/3] libpmc: Support alias event specifiers with suffixes This change allows me to use events such as "cycles,usr" by saving the old suffix and restricting event aliases to not include suffixes (none of them do right now). --- lib/libpmc/libpmc.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/libpmc/libpmc.c b/lib/libpmc/libpmc.c index c90a6cd2706c..7618643d748f 100644 --- a/lib/libpmc/libpmc.c +++ b/lib/libpmc/libpmc.c @@ -1103,28 +1103,29 @@ pmc_allocate(const char *ctrspec, enum pmc_mode mode, pmc_config.pm_caps = 0; pmc_config.pm_class = 0; } - free(spec_copy); - spec_copy = NULL; /* replace an event alias with the canonical event specifier */ if (pmc_mdep_event_aliases) for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++) - if (!strcasecmp(ctrspec, alias->pm_alias)) { - spec_copy = strdup(alias->pm_spec); + if (!strcasecmp(ctrname, alias->pm_alias)) { + /* Aliases must not contain a comma. */ + if (strchr(alias->pm_alias, ',')) { + errno = EDOOFUS; + goto out; + } + ctrname = alias->pm_spec; break; } - - /* Try pmu_alias_get as another generic fallback/ */ - if (spec_copy == NULL) { - const char* alias_name = _pmu_alias_get(ctrspec); - if (alias_name) - spec_copy = strdup(alias_name); + /* Try pmu_alias_get as another generic fallback. */ + const char* alias_name = _pmu_alias_get(ctrname); + if (alias_name != ctrname) { + /* Aliases must not contain a comma. */ + if (strchr(alias_name, ',')) { + errno = EDOOFUS; + goto out; + } + ctrname = alias_name; } - if (spec_copy == NULL) - spec_copy = strdup(ctrspec); - - r = spec_copy; - ctrname = strsep(&r, ","); /* * If a explicit class prefix was given by the user, restrict the @@ -1173,8 +1174,7 @@ pmc_allocate(const char *ctrspec, enum pmc_mode mode, retval = 0; } out: - if (spec_copy) - free(spec_copy); + free(spec_copy); return (retval); }