perf evsel: Support sparse fields in evsel__set_config_if_unset()

Sparse config fields are technically supported although currently
unused. field_prep() only works for contiguous bitfields so replace it
with pmu_format_value().

pmu_format_value() also takes a bitmap rather than a u64 so replace
'u64 bits' with format->bits.

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:16 +00:00 committed by Arnaldo Carvalho de Melo
parent 11ac460605
commit 5b5e01304f
3 changed files with 42 additions and 37 deletions

View File

@ -1323,25 +1323,28 @@ struct evsel_config_term *__evsel__get_config_term(struct evsel *evsel, enum evs
* the bit pattern. It is shifted into position by this function, so to set
* something to true, pass 1 for val rather than a pre shifted value.
*/
#define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
void evsel__set_config_if_unset(struct evsel *evsel, const char *config_name,
u64 val)
{
u64 user_bits = 0, bits;
u64 user_bits = 0;
struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
struct perf_pmu_format *format = pmu_find_format(&evsel->pmu->format,
config_name);
int fbit;
if (!format)
return;
if (term)
user_bits = term->val.cfg_chg;
bits = perf_pmu__format_bits(evsel->pmu, config_name);
/* Do nothing if the user changed the value */
if (bits & user_bits)
return;
for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
if ((1ULL << fbit) & user_bits)
return;
/* Otherwise replace it */
evsel->core.attr.config &= ~bits;
evsel->core.attr.config |= field_prep(bits, val);
pmu_format_value(format->bits, val, &evsel->core.attr.config, /*zero=*/true);
}
void __weak arch_evsel__set_sample_weight(struct evsel *evsel)

View File

@ -118,31 +118,6 @@ struct perf_pmu_alias {
bool info_loaded;
};
/**
* struct perf_pmu_format - Values from a format file read from
* <sysfs>/devices/cpu/format/ held in struct perf_pmu.
*
* For example, the contents of <sysfs>/devices/cpu/format/event may be
* "config:0-7" and will be represented here as name="event",
* 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;
/**
* @value : Which config value the format relates to. Supported values
* are from PERF_PMU_FORMAT_VALUE_CONFIG to
* PERF_PMU_FORMAT_VALUE_CONFIG_END.
*/
u16 value;
/** @loaded: Has the contents been loaded/parsed. */
bool loaded;
};
static int pmu_aliases_parse(struct perf_pmu *pmu);
static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
@ -1362,8 +1337,8 @@ void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
}
}
static struct perf_pmu_format *
pmu_find_format(const struct list_head *formats, const char *name)
struct perf_pmu_format *pmu_find_format(const struct list_head *formats,
const char *name)
{
struct perf_pmu_format *format;
@ -1404,8 +1379,7 @@ int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
* Sets value based on the format definition (format parameter)
* and unformatted value (value parameter).
*/
static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
bool zero)
void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, bool zero)
{
unsigned long fbit, vbit;

View File

@ -233,6 +233,31 @@ struct pmu_event_info {
bool deprecated;
};
/**
* struct perf_pmu_format - Values from a format file read from
* <sysfs>/devices/cpu/format/ held in struct perf_pmu.
*
* For example, the contents of <sysfs>/devices/cpu/format/event may be
* "config:0-7" and will be represented here as name="event",
* 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;
/**
* @value : Which config value the format relates to. Supported values
* are from PERF_PMU_FORMAT_VALUE_CONFIG to
* PERF_PMU_FORMAT_VALUE_CONFIG_END.
*/
u16 value;
/** @loaded: Has the contents been loaded/parsed. */
bool loaded;
};
typedef int (*pmu_event_callback)(void *state, struct pmu_event_info *info);
typedef int (*pmu_format_callback)(void *state, const char *name, int config,
const unsigned long *bits);
@ -254,6 +279,9 @@ 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);
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);