Re: [PATCH] oom: add sysctl to enable slab memory dump

From: Pekka Enberg
Date: Wed Feb 22 2012 - 08:17:36 EST


On Wed, Feb 22, 2012 at 1:53 PM, Rafael Aquini <aquini@xxxxxxxxxx> wrote:
> Adds a new sysctl, 'oom_dump_slabs', that enables the kernel to produce a
> dump of all eligible system slab caches when performing an OOM-killing.
> Information includes per cache active objects, total objects, object size,
> cache name and cache size.
>
> The eligibility for being reported is given by an auxiliary sysctl,
> 'oom_dump_slabs_ratio', which express (in percentage) the memory committed
> ratio between a particular cache size and the total slab size.
>
> This, alongside with all other data dumped in OOM events, is very helpful
> information in diagnosing why there was an OOM condition specially when
> kernel code is under investigation.
>
> Signed-off-by: Rafael Aquini <aquini@xxxxxxxxxx>

Makes sense. Do you have an example how an out-of-memory slab cache
dump looks like?

> diff --git a/mm/slab.c b/mm/slab.c
> index f0bd785..c2b5d14 100644
> --- a/mm/slab.c
> +++ b/mm/slab.c
> @@ -4629,3 +4629,75 @@ size_t ksize(const void *objp)
>        return obj_size(virt_to_cache(objp));
>  }
>  EXPORT_SYMBOL(ksize);
> +
> +/**
> + * oom_dump_slabs - dump top slab cache users
> + * @ratio: memory committed ratio between a cache size and the total slab size
> + *
> + * Dumps the current memory state of all eligible slab caches.
> + * State information includes cache's active objects, total objects,
> + * object size, cache name, and cache size.
> + */
> +void oom_dump_slabs(int ratio)
> +{
> +       struct kmem_cache *cachep;
> +       struct kmem_list3 *l3;
> +       struct slab *slabp;
> +       unsigned long active_objs, num_objs, free_objects, cache_size;
> +       unsigned long active_slabs, num_slabs, slab_total_mem;
> +       int node;

[snip]

> +       list_for_each_entry(cachep, &cache_chain, next) {
> +               num_objs = 0;
> +               num_slabs = 0;
> +               active_objs = 0;
> +               free_objects = 0;
> +               active_slabs = 0;

Minor style nit: just define the zeroed variables in this block.

> +
> +               for_each_online_node(node) {
> +                       l3 = cachep->nodelists[node];
> +                       if (!l3)
> +                               continue;
> +void oom_dump_slabs(int ratio)
> +{
> +       unsigned long cache_size, slab_total_mem;
> +       unsigned long nr_objs, nr_free, nr_inuse;
> +       struct kmem_cache *cachep;
> +       int node;
> +
> +       slab_total_mem = (global_page_state(NR_SLAB_RECLAIMABLE) +
> +                       global_page_state(NR_SLAB_UNRECLAIMABLE)) << PAGE_SHIFT;
> +
> +       if (ratio < 0)
> +               ratio = 0;
> +
> +       if (ratio > 100)
> +               ratio = 100;
> +
> +       pr_info("--- oom_dump_slabs:\n");
> +       pr_info("<active_objs>    <num_objs>     <objsize>  <cache_name>\n");
> +       down_read(&slub_lock);
> +       list_for_each_entry(cachep, &slab_caches, list) {
> +               nr_objs = 0;
> +               nr_free = 0;

ditto.

> +
> +               for_each_online_node(node) {
> +                       struct kmem_cache_node *n = get_node(cachep, node);
> +                       if (!n)
> +                               continue;
> +
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/