Skip to content

Commit cd63c22

Browse files
captain5050namhyung
authored andcommitted
perf parse-events: Minor __add_event refactoring
Rename cpu_list to user_cpus. If a PMU isn't given, find it early from the perf_event_attr. Make the pmu_cpus more explicitly a copy from the PMU (except when user_cpus are given). Derive the cpus from pmu_cpus and user_cpus as appropriate. Handle strdup errors on name and metric_id. Reviewed-by: Thomas Falcon <thomas.falcon@intel.com> Signed-off-by: Ian Rogers <irogers@google.com> Tested-by: James Clark <james.clark@linaro.org> Link: https://lore.kernel.org/r/20250719030517.1990983-11-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 3cb614a commit cd63c22

1 file changed

Lines changed: 48 additions & 21 deletions

File tree

tools/perf/util/parse-events.c

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,12 @@ __add_event(struct list_head *list, int *idx,
259259
bool init_attr,
260260
const char *name, const char *metric_id, struct perf_pmu *pmu,
261261
struct list_head *config_terms, struct evsel *first_wildcard_match,
262-
struct perf_cpu_map *cpu_list, u64 alternate_hw_config)
262+
struct perf_cpu_map *user_cpus, u64 alternate_hw_config)
263263
{
264264
struct evsel *evsel;
265265
bool is_pmu_core;
266-
struct perf_cpu_map *cpus;
267-
bool has_cpu_list = !perf_cpu_map__is_empty(cpu_list);
266+
struct perf_cpu_map *cpus, *pmu_cpus;
267+
bool has_user_cpus = !perf_cpu_map__is_empty(user_cpus);
268268

269269
/*
270270
* Ensure the first_wildcard_match's PMU matches that of the new event
@@ -288,8 +288,6 @@ __add_event(struct list_head *list, int *idx,
288288
}
289289

290290
if (pmu) {
291-
is_pmu_core = pmu->is_core;
292-
cpus = perf_cpu_map__get(has_cpu_list ? cpu_list : pmu->cpus);
293291
perf_pmu__warn_invalid_formats(pmu);
294292
if (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX) {
295293
perf_pmu__warn_invalid_config(pmu, attr->config, name,
@@ -301,48 +299,77 @@ __add_event(struct list_head *list, int *idx,
301299
perf_pmu__warn_invalid_config(pmu, attr->config3, name,
302300
PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
303301
}
302+
}
303+
/*
304+
* If a PMU wasn't given, such as for legacy events, find now that
305+
* warnings won't be generated.
306+
*/
307+
if (!pmu)
308+
pmu = perf_pmus__find_by_attr(attr);
309+
310+
if (pmu) {
311+
is_pmu_core = pmu->is_core;
312+
pmu_cpus = perf_cpu_map__get(pmu->cpus);
304313
} else {
305314
is_pmu_core = (attr->type == PERF_TYPE_HARDWARE ||
306315
attr->type == PERF_TYPE_HW_CACHE);
307-
if (has_cpu_list)
308-
cpus = perf_cpu_map__get(cpu_list);
309-
else
310-
cpus = is_pmu_core ? cpu_map__online() : NULL;
316+
pmu_cpus = is_pmu_core ? cpu_map__online() : NULL;
317+
}
318+
319+
if (has_user_cpus) {
320+
cpus = perf_cpu_map__get(user_cpus);
321+
/* Existing behavior that pmu_cpus matches the given user ones. */
322+
perf_cpu_map__put(pmu_cpus);
323+
pmu_cpus = perf_cpu_map__get(user_cpus);
324+
} else {
325+
cpus = perf_cpu_map__get(pmu_cpus);
311326
}
327+
312328
if (init_attr)
313329
event_attr_init(attr);
314330

315331
evsel = evsel__new_idx(attr, *idx);
316-
if (!evsel) {
317-
perf_cpu_map__put(cpus);
318-
return NULL;
332+
if (!evsel)
333+
goto out_err;
334+
335+
if (name) {
336+
evsel->name = strdup(name);
337+
if (!evsel->name)
338+
goto out_err;
339+
}
340+
341+
if (metric_id) {
342+
evsel->metric_id = strdup(metric_id);
343+
if (!evsel->metric_id)
344+
goto out_err;
319345
}
320346

321347
(*idx)++;
322348
evsel->core.cpus = cpus;
323-
evsel->core.pmu_cpus = perf_cpu_map__get(cpus);
349+
evsel->core.pmu_cpus = pmu_cpus;
324350
evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
325351
evsel->core.is_pmu_core = is_pmu_core;
326352
evsel->pmu = pmu;
327353
evsel->alternate_hw_config = alternate_hw_config;
328354
evsel->first_wildcard_match = first_wildcard_match;
329355

330-
if (name)
331-
evsel->name = strdup(name);
332-
333-
if (metric_id)
334-
evsel->metric_id = strdup(metric_id);
335-
336356
if (config_terms)
337357
list_splice_init(config_terms, &evsel->config_terms);
338358

339359
if (list)
340360
list_add_tail(&evsel->core.node, list);
341361

342-
if (has_cpu_list)
343-
evsel__warn_user_requested_cpus(evsel, cpu_list);
362+
if (has_user_cpus)
363+
evsel__warn_user_requested_cpus(evsel, user_cpus);
344364

345365
return evsel;
366+
out_err:
367+
perf_cpu_map__put(cpus);
368+
perf_cpu_map__put(pmu_cpus);
369+
zfree(&evsel->name);
370+
zfree(&evsel->metric_id);
371+
free(evsel);
372+
return NULL;
346373
}
347374

348375
struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,

0 commit comments

Comments
 (0)