@@ -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
348375struct evsel * parse_events__add_event (int idx , struct perf_event_attr * attr ,
0 commit comments