public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@arndb.de>
To: Matt Mackall <mpm@selenic.com>
Cc: linux-kernel <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@osdl.org>
Subject: Re: [PATCH] /proc/kmalloc
Date: Sun, 20 Feb 2005 22:59:33 +0100	[thread overview]
Message-ID: <200502202259.34156.arnd@arndb.de> (raw)
In-Reply-To: <20050220204743.GE3120@waste.org>

[-- Attachment #1: Type: text/plain, Size: 2539 bytes --]

On Sünndag 20 Februar 2005 21:47, Matt Mackall wrote:
> I've been sitting on this for over a year now, kicking it out in the
> hopes that someone finds it useful. kernel.org was down when I was
> tidying this up so it's against 2.6.10 which is what I had handy.
>
> /proc/kmalloc allocation tracing

Nice. I have done something similar for the buddy allocator but never
got around to sending it.

> This quick hack adds accounting for kmalloc/kfree callers. This can
> aid in tracking down memory leaks and large dynamic memory users. The
> stock version use ~280k of memory for hash tables and can track 32k
> active allocations.
> 
> Here's some sample output from my laptop:
> 
> total bytes allocated: 47118848   
> slack bytes allocated:  8717262
> net bytes allocated:    2825920
> number of allocs:        132796
> number of frees:         122629
> number of callers:          325
> lost callers:                 0
> lost allocs:                  0
> unknown frees:                0
> 
>    total    slack      net alloc/free  caller
>    24576        0        0     3/3     copy_thread+0x1ad

The format is not really easy to parse, it probably makes sense to
split the two parts into separate files. I also think that debugfs
would be a more appropriate place to put this in than procfs.

> +void __kmalloc_account(const void *caller, const void *addr, int size, int req)
> +{
> +	int i, hasha, hashc;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&kma_lock, flags);
> +	if(req >= 0) /* kmalloc */
> +	{
> +		/* find callers slot */
> +		hashc = kma_hash(caller, MAX_CALLER_TABLE);
> +		for (i = 0; i < MAX_CALLER_TABLE; i++) {
> +			if (!kma_caller[hashc].caller ||
> +			    kma_caller[hashc].caller == caller)
> +				break;
> +			hashc = (hashc + 1) % MAX_CALLER_TABLE;
> +		}

The housekeeping that is needed for the hash implementation is rather
complicated. The code that I wrote did a static allocation from inside
a macro, like

#define kmalloc(_size, _gfp) \
	({ \
		static struct kma_caller _caller \
			__attribute__((section(".kmalloc.data"))) = { \
			.func = __FUNCTION__, \
			.line = __LINE__, \
		}; \
		_caller.count++; \
		_caller.size += (_size); \
		__kmalloc((_size), (_gfp)); \
	})

Then I could simply print out all allocations by walking through the
special linker section. OTOH, your implementation has the advantage
that it can directly match kmalloc/kfree pairs and that it does not
rely on special linker magic.

	Arnd <><

[-- Attachment #2: signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

  reply	other threads:[~2005-02-20 22:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-20 20:47 [PATCH] /proc/kmalloc Matt Mackall
2005-02-20 21:59 ` Arnd Bergmann [this message]
2005-02-21  2:43 ` Baruch Even
2005-02-21 13:53   ` Pekka Enberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200502202259.34156.arnd@arndb.de \
    --to=arnd@arndb.de \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpm@selenic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox