Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit bdacfaf

Browse files
namhyungPeter Zijlstra
authored andcommitted
perf core: Add a kmem_cache for struct perf_event
The kernel can allocate a lot of struct perf_event when profiling. For example, 256 cpu x 8 events x 20 cgroups = 40K instances of the struct would be allocated on a large system. The size of struct perf_event in my setup is 1152 byte. As it's allocated by kmalloc, the actual allocation size would be rounded up to 2K. Then there's 896 byte (~43%) of waste per instance resulting in total ~35MB with 40K instances. We can create a dedicated kmem_cache to avoid such a big unnecessary memory consumption. With this change, I can see below (note this machine has 112 cpus). # grep perf_event /proc/slabinfo perf_event 224 784 1152 7 2 : tunables 24 12 8 : slabdata 112 112 0 The sixth column is pages-per-slab which is 2, and the fifth column is obj-per-slab which is 7. Thus actually it can use 1152 x 7 = 8064 byte in the 8K, and wasted memory is (8192 - 8064) / 7 = ~18 byte per instance. Signed-off-by: Namhyung Kim <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 9483409 commit bdacfaf

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

kernel/events/core.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ static LIST_HEAD(pmus);
405405
static DEFINE_MUTEX(pmus_lock);
406406
static struct srcu_struct pmus_srcu;
407407
static cpumask_var_t perf_online_mask;
408+
static struct kmem_cache *perf_event_cache;
408409

409410
/*
410411
* perf event paranoia level:
@@ -4611,7 +4612,7 @@ static void free_event_rcu(struct rcu_head *head)
46114612
if (event->ns)
46124613
put_pid_ns(event->ns);
46134614
perf_event_free_filter(event);
4614-
kfree(event);
4615+
kmem_cache_free(perf_event_cache, event);
46154616
}
46164617

46174618
static void ring_buffer_attach(struct perf_event *event,
@@ -11293,7 +11294,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
1129311294
return ERR_PTR(-EINVAL);
1129411295
}
1129511296

11296-
event = kzalloc(sizeof(*event), GFP_KERNEL);
11297+
event = kmem_cache_zalloc(perf_event_cache, GFP_KERNEL);
1129711298
if (!event)
1129811299
return ERR_PTR(-ENOMEM);
1129911300

@@ -11497,7 +11498,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
1149711498
put_pid_ns(event->ns);
1149811499
if (event->hw.target)
1149911500
put_task_struct(event->hw.target);
11500-
kfree(event);
11501+
kmem_cache_free(perf_event_cache, event);
1150111502

1150211503
return ERR_PTR(err);
1150311504
}
@@ -13130,6 +13131,8 @@ void __init perf_event_init(void)
1313013131
ret = init_hw_breakpoint();
1313113132
WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
1313213133

13134+
perf_event_cache = KMEM_CACHE(perf_event, SLAB_PANIC);
13135+
1313313136
/*
1313413137
* Build time assertion that we keep the data_head at the intended
1313513138
* location. IOW, validation we got the __reserved[] size right.

0 commit comments

Comments
 (0)