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 <><
pgp01ROXTHZ22.pgp
Description: signature