Skip to content

Commit 66d258c

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/core: Optimize perf_init_event()
Andi reported that he was hitting the linear search in perf_init_event() a lot. Make more agressive use of the IDR lookup to avoid hitting the linear search. With exception of PERF_TYPE_SOFTWARE (which relies on a hideous hack), we can put everything in the IDR. On top of that, we can alias TYPE_HARDWARE and TYPE_HW_CACHE to TYPE_RAW on the lookup side. This greatly reduces the chances of hitting the linear search. Reported-by: Andi Kleen <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vince Weaver <[email protected]> Signed-off-by: Ingo Molnar <[email protected]>
1 parent db0503e commit 66d258c

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

kernel/events/core.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10080,7 +10080,7 @@ static struct lock_class_key cpuctx_lock;
1008010080

1008110081
int perf_pmu_register(struct pmu *pmu, const char *name, int type)
1008210082
{
10083-
int cpu, ret;
10083+
int cpu, ret, max = PERF_TYPE_MAX;
1008410084

1008510085
mutex_lock(&pmus_lock);
1008610086
ret = -ENOMEM;
@@ -10093,12 +10093,17 @@ int perf_pmu_register(struct pmu *pmu, const char *name, int type)
1009310093
goto skip_type;
1009410094
pmu->name = name;
1009510095

10096-
if (type < 0) {
10097-
type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
10098-
if (type < 0) {
10099-
ret = type;
10096+
if (type != PERF_TYPE_SOFTWARE) {
10097+
if (type >= 0)
10098+
max = type;
10099+
10100+
ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL);
10101+
if (ret < 0)
1010010102
goto free_pdc;
10101-
}
10103+
10104+
WARN_ON(type >= 0 && ret != type);
10105+
10106+
type = ret;
1010210107
}
1010310108
pmu->type = type;
1010410109

@@ -10188,7 +10193,7 @@ int perf_pmu_register(struct pmu *pmu, const char *name, int type)
1018810193
put_device(pmu->dev);
1018910194

1019010195
free_idr:
10191-
if (pmu->type >= PERF_TYPE_MAX)
10196+
if (pmu->type != PERF_TYPE_SOFTWARE)
1019210197
idr_remove(&pmu_idr, pmu->type);
1019310198

1019410199
free_pdc:
@@ -10210,7 +10215,7 @@ void perf_pmu_unregister(struct pmu *pmu)
1021010215
synchronize_rcu();
1021110216

1021210217
free_percpu(pmu->pmu_disable_count);
10213-
if (pmu->type >= PERF_TYPE_MAX)
10218+
if (pmu->type != PERF_TYPE_SOFTWARE)
1021410219
idr_remove(&pmu_idr, pmu->type);
1021510220
if (pmu_bus_running) {
1021610221
if (pmu->nr_addr_filters)
@@ -10280,9 +10285,8 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
1028010285

1028110286
static struct pmu *perf_init_event(struct perf_event *event)
1028210287
{
10288+
int idx, type, ret;
1028310289
struct pmu *pmu;
10284-
int idx;
10285-
int ret;
1028610290

1028710291
idx = srcu_read_lock(&pmus_srcu);
1028810292

@@ -10295,12 +10299,27 @@ static struct pmu *perf_init_event(struct perf_event *event)
1029510299
}
1029610300

1029710301
rcu_read_lock();
10298-
pmu = idr_find(&pmu_idr, event->attr.type);
10302+
/*
10303+
* PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE
10304+
* are often aliases for PERF_TYPE_RAW.
10305+
*/
10306+
type = event->attr.type;
10307+
if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)
10308+
type = PERF_TYPE_RAW;
10309+
10310+
again:
10311+
pmu = idr_find(&pmu_idr, type);
1029910312
rcu_read_unlock();
1030010313
if (pmu) {
1030110314
ret = perf_try_init_event(pmu, event);
10315+
if (ret == -ENOENT && event->attr.type != type) {
10316+
type = event->attr.type;
10317+
goto again;
10318+
}
10319+
1030210320
if (ret)
1030310321
pmu = ERR_PTR(ret);
10322+
1030410323
goto unlock;
1030510324
}
1030610325

0 commit comments

Comments
 (0)