perf evsel: Add a helper to get the value of a config field

This will be used by aux PMUs to read an already written value for
configuring their events and for also testing.

Its helper perf_pmu__format_unpack() does the opposite of the existing
pmu_format_value() so rename that one to perf_pmu__format_pack() so it's
clear how they are related.

Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: James Clark <james.clark@linaro.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
James Clark
2026-01-14 15:57:19 +00:00
committed by Arnaldo Carvalho de Melo
parent 87775abac8
commit 34b4cfbe5c
4 changed files with 74 additions and 9 deletions

View File

@@ -1379,7 +1379,47 @@ void evsel__set_config_if_unset(struct evsel *evsel, const char *config_name,
return;
/* Otherwise replace it */
pmu_format_value(format->bits, val, vp, /*zero=*/true);
perf_pmu__format_pack(format->bits, val, vp, /*zero=*/true);
}
int evsel__get_config_val(const struct evsel *evsel, const char *config_name,
u64 *val)
{
struct perf_pmu_format *format = pmu_find_format(&evsel->pmu->format, config_name);
if (!format || bitmap_empty(format->bits, PERF_PMU_FORMAT_BITS)) {
pr_err("Unknown/empty format name: %s\n", config_name);
*val = 0;
return -EINVAL;
}
switch (format->value) {
case PERF_PMU_FORMAT_VALUE_CONFIG:
*val = perf_pmu__format_unpack(format->bits,
evsel->core.attr.config);
return 0;
case PERF_PMU_FORMAT_VALUE_CONFIG1:
*val = perf_pmu__format_unpack(format->bits,
evsel->core.attr.config1);
return 0;
case PERF_PMU_FORMAT_VALUE_CONFIG2:
*val = perf_pmu__format_unpack(format->bits,
evsel->core.attr.config2);
return 0;
case PERF_PMU_FORMAT_VALUE_CONFIG3:
*val = perf_pmu__format_unpack(format->bits,
evsel->core.attr.config3);
return 0;
case PERF_PMU_FORMAT_VALUE_CONFIG4:
*val = perf_pmu__format_unpack(format->bits,
evsel->core.attr.config4);
return 0;
default:
pr_err("Unknown format value: %d\n", format->value);
*val = 0;
return -EINVAL;
}
}
void __weak arch_evsel__set_sample_weight(struct evsel *evsel)

View File

@@ -575,6 +575,8 @@ void evsel__uniquify_counter(struct evsel *counter);
((((src) >> (pos)) & ((1ull << (size)) - 1)) << (63 - ((pos) + (size) - 1)))
u64 evsel__bitfield_swap_branch_flags(u64 value);
int evsel__get_config_val(const struct evsel *evsel, const char *config_name,
u64 *val);
void evsel__set_config_if_unset(struct evsel *evsel, const char *config_name,
u64 val);

View File

@@ -1337,6 +1337,26 @@ void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
}
}
/*
* Unpacks a raw config[n] value using the sparse bitfield that defines a
* format attr. For example "config1:1,6-7,44" defines a 4 bit value across non
* contiguous bits and this function returns those 4 bits as a value.
*/
u64 perf_pmu__format_unpack(unsigned long *format, u64 config_val)
{
int val_bit = 0;
u64 res = 0;
int fmt_bit;
for_each_set_bit(fmt_bit, format, PERF_PMU_FORMAT_BITS) {
if (config_val & (1ULL << fmt_bit))
res |= BIT_ULL(val_bit);
val_bit++;
}
return res;
}
struct perf_pmu_format *pmu_find_format(const struct list_head *formats,
const char *name)
{
@@ -1379,7 +1399,8 @@ int perf_pmu__format_type(const struct perf_pmu *pmu, const char *name)
* Sets value based on the format definition (format parameter)
* and unformatted value (value parameter).
*/
void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, bool zero)
void perf_pmu__format_pack(unsigned long *format, __u64 value, __u64 *v,
bool zero)
{
unsigned long fbit, vbit;
@@ -1496,23 +1517,23 @@ static int pmu_config_term(const struct perf_pmu *pmu,
switch (term->type_term) {
case PARSE_EVENTS__TERM_TYPE_CONFIG:
assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
pmu_format_value(bits, term->val.num, &attr->config, zero);
perf_pmu__format_pack(bits, term->val.num, &attr->config, zero);
break;
case PARSE_EVENTS__TERM_TYPE_CONFIG1:
assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
pmu_format_value(bits, term->val.num, &attr->config1, zero);
perf_pmu__format_pack(bits, term->val.num, &attr->config1, zero);
break;
case PARSE_EVENTS__TERM_TYPE_CONFIG2:
assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
pmu_format_value(bits, term->val.num, &attr->config2, zero);
perf_pmu__format_pack(bits, term->val.num, &attr->config2, zero);
break;
case PARSE_EVENTS__TERM_TYPE_CONFIG3:
assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
pmu_format_value(bits, term->val.num, &attr->config3, zero);
perf_pmu__format_pack(bits, term->val.num, &attr->config3, zero);
break;
case PARSE_EVENTS__TERM_TYPE_CONFIG4:
assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
pmu_format_value(bits, term->val.num, &attr->config4, zero);
perf_pmu__format_pack(bits, term->val.num, &attr->config4, zero);
break;
case PARSE_EVENTS__TERM_TYPE_LEGACY_HARDWARE_CONFIG:
assert(term->type_val == PARSE_EVENTS__TERM_TYPE_NUM);
@@ -1650,7 +1671,7 @@ static int pmu_config_term(const struct perf_pmu *pmu,
*/
}
pmu_format_value(format->bits, val, vp, zero);
perf_pmu__format_pack(format->bits, val, vp, zero);
return 0;
}

View File

@@ -279,12 +279,14 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_
u64 *alternate_hw_config, struct parse_events_error *err);
int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb);
void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, bool zero);
void perf_pmu__format_pack(unsigned long *format, __u64 value, __u64 *v,
bool zero);
struct perf_pmu_format *pmu_find_format(const struct list_head *formats,
const char *name);
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);
int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_callback cb);
u64 perf_pmu__format_unpack(unsigned long *format, u64 config_val);
bool is_pmu_core(const char *name);
bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu);