1*387f9dfdSAndroid Build Coastguard WorkerDemonstrations of slabratetop, the Linux eBPF/bcc version. 2*387f9dfdSAndroid Build Coastguard Worker 3*387f9dfdSAndroid Build Coastguard Worker 4*387f9dfdSAndroid Build Coastguard Workerslabratetop shows the rate of allocations and total bytes from the kernel 5*387f9dfdSAndroid Build Coastguard Workermemory allocation caches (SLAB or SLUB), in a top-like display that refreshes. 6*387f9dfdSAndroid Build Coastguard WorkerFor example: 7*387f9dfdSAndroid Build Coastguard Worker 8*387f9dfdSAndroid Build Coastguard Worker# ./slabratetop 9*387f9dfdSAndroid Build Coastguard Worker<screen clears> 10*387f9dfdSAndroid Build Coastguard Worker07:01:35 loadavg: 0.38 0.21 0.12 1/342 13297 11*387f9dfdSAndroid Build Coastguard Worker 12*387f9dfdSAndroid Build Coastguard WorkerCACHE ALLOCS BYTES 13*387f9dfdSAndroid Build Coastguard Workerkmalloc-4096 3554 14557184 14*387f9dfdSAndroid Build Coastguard Workerkmalloc-256 2382 609536 15*387f9dfdSAndroid Build Coastguard Workercred_jar 2568 493056 16*387f9dfdSAndroid Build Coastguard Workeranon_vma_chain 2007 128448 17*387f9dfdSAndroid Build Coastguard Workeranon_vma 972 77760 18*387f9dfdSAndroid Build Coastguard Workersighand_cache 24 50688 19*387f9dfdSAndroid Build Coastguard Workermm_struct 49 50176 20*387f9dfdSAndroid Build Coastguard WorkerRAW 52 49920 21*387f9dfdSAndroid Build Coastguard Workerproc_inode_cache 59 38232 22*387f9dfdSAndroid Build Coastguard Workersignal_cache 24 26112 23*387f9dfdSAndroid Build Coastguard Workerdentry 135 25920 24*387f9dfdSAndroid Build Coastguard Workersock_inode_cache 29 18560 25*387f9dfdSAndroid Build Coastguard Workerfiles_cache 24 16896 26*387f9dfdSAndroid Build Coastguard Workerinode_cache 13 7696 27*387f9dfdSAndroid Build Coastguard WorkerTCP 2 3840 28*387f9dfdSAndroid Build Coastguard Workerpid 24 3072 29*387f9dfdSAndroid Build Coastguard Workersigqueue 17 2720 30*387f9dfdSAndroid Build Coastguard Workerext4_inode_cache 2 2160 31*387f9dfdSAndroid Build Coastguard Workerbuffer_head 16 1664 32*387f9dfdSAndroid Build Coastguard Workerxfs_trans 5 1160 33*387f9dfdSAndroid Build Coastguard Worker 34*387f9dfdSAndroid Build Coastguard WorkerBy default the screen refreshes every one second, and only the top 20 caches 35*387f9dfdSAndroid Build Coastguard Workerare shown. These can be tuned with options: see USAGE (-h). 36*387f9dfdSAndroid Build Coastguard Worker 37*387f9dfdSAndroid Build Coastguard WorkerThe output above showed that the kmalloc-4096 cache allocated the most, about 38*387f9dfdSAndroid Build Coastguard Worker14 Mbytes during this interval. This is a generic cache; other caches have 39*387f9dfdSAndroid Build Coastguard Workermore meaningful names ("dentry", "TCP", "pid", etc). 40*387f9dfdSAndroid Build Coastguard Worker 41*387f9dfdSAndroid Build Coastguard Workerslabtop(1) is a similar tool that shows the current static volume and usage 42*387f9dfdSAndroid Build Coastguard Workerof the caches. slabratetop shows the active call rates and total size of the 43*387f9dfdSAndroid Build Coastguard Workerallocations. 44*387f9dfdSAndroid Build Coastguard Worker 45*387f9dfdSAndroid Build Coastguard Worker 46*387f9dfdSAndroid Build Coastguard WorkerSince "kmalloc-4096" isn't very descriptive, I'm interested in seeing the 47*387f9dfdSAndroid Build Coastguard Workerkernel stacks that led to this allocation. In the future (maybe by now) the 48*387f9dfdSAndroid Build Coastguard Workerbcc trace tool could do this. As I'm writing this, it can't, so I'll use my 49*387f9dfdSAndroid Build Coastguard Workerolder ftrace-based kprobe tool as a workarond. This is from my perf-tools 50*387f9dfdSAndroid Build Coastguard Workercollection: https://github.com/brendangregg/perf-tools. 51*387f9dfdSAndroid Build Coastguard Worker 52*387f9dfdSAndroid Build Coastguard Worker# ./perf-tools/bin/kprobe -s 'p:kmem_cache_alloc name=+0(+96(%di)):string' 'name == "kmalloc-4096' | head -100 53*387f9dfdSAndroid Build Coastguard WorkerTracing kprobe kmem_cache_alloc. Ctrl-C to end. 54*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478331: kmem_cache_alloc: (kmem_cache_alloc+0x0/0x1b0) name="kmalloc-4096" 55*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478333: <stack trace> 56*387f9dfdSAndroid Build Coastguard Worker => kmem_cache_alloc 57*387f9dfdSAndroid Build Coastguard Worker => user_path_at_empty 58*387f9dfdSAndroid Build Coastguard Worker => vfs_fstatat 59*387f9dfdSAndroid Build Coastguard Worker => SYSC_newstat 60*387f9dfdSAndroid Build Coastguard Worker => SyS_newstat 61*387f9dfdSAndroid Build Coastguard Worker => entry_SYSCALL_64_fastpath 62*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478340: kmem_cache_alloc: (kmem_cache_alloc+0x0/0x1b0) name="kmalloc-4096" 63*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478341: <stack trace> 64*387f9dfdSAndroid Build Coastguard Worker => kmem_cache_alloc 65*387f9dfdSAndroid Build Coastguard Worker => user_path_at_empty 66*387f9dfdSAndroid Build Coastguard Worker => vfs_fstatat 67*387f9dfdSAndroid Build Coastguard Worker => SYSC_newstat 68*387f9dfdSAndroid Build Coastguard Worker => SyS_newstat 69*387f9dfdSAndroid Build Coastguard Worker => entry_SYSCALL_64_fastpath 70*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478345: kmem_cache_alloc: (kmem_cache_alloc+0x0/0x1b0) name="kmalloc-4096" 71*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478346: <stack trace> 72*387f9dfdSAndroid Build Coastguard Worker => kmem_cache_alloc 73*387f9dfdSAndroid Build Coastguard Worker => user_path_at_empty 74*387f9dfdSAndroid Build Coastguard Worker => vfs_fstatat 75*387f9dfdSAndroid Build Coastguard Worker => SYSC_newstat 76*387f9dfdSAndroid Build Coastguard Worker => SyS_newstat 77*387f9dfdSAndroid Build Coastguard Worker => entry_SYSCALL_64_fastpath 78*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478350: kmem_cache_alloc: (kmem_cache_alloc+0x0/0x1b0) name="kmalloc-4096" 79*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478351: <stack trace> 80*387f9dfdSAndroid Build Coastguard Worker => kmem_cache_alloc 81*387f9dfdSAndroid Build Coastguard Worker => user_path_at_empty 82*387f9dfdSAndroid Build Coastguard Worker => vfs_fstatat 83*387f9dfdSAndroid Build Coastguard Worker => SYSC_newstat 84*387f9dfdSAndroid Build Coastguard Worker => SyS_newstat 85*387f9dfdSAndroid Build Coastguard Worker => entry_SYSCALL_64_fastpath 86*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478355: kmem_cache_alloc: (kmem_cache_alloc+0x0/0x1b0) name="kmalloc-4096" 87*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478355: <stack trace> 88*387f9dfdSAndroid Build Coastguard Worker => kmem_cache_alloc 89*387f9dfdSAndroid Build Coastguard Worker => user_path_at_empty 90*387f9dfdSAndroid Build Coastguard Worker => vfs_fstatat 91*387f9dfdSAndroid Build Coastguard Worker => SYSC_newstat 92*387f9dfdSAndroid Build Coastguard Worker => SyS_newstat 93*387f9dfdSAndroid Build Coastguard Worker => entry_SYSCALL_64_fastpath 94*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478359: kmem_cache_alloc: (kmem_cache_alloc+0x0/0x1b0) name="kmalloc-4096" 95*387f9dfdSAndroid Build Coastguard Worker kprobe-3892 [002] d... 7888274.478359: <stack trace> 96*387f9dfdSAndroid Build Coastguard Worker => kmem_cache_alloc 97*387f9dfdSAndroid Build Coastguard Worker => user_path_at_empty 98*387f9dfdSAndroid Build Coastguard Worker => vfs_fstatat 99*387f9dfdSAndroid Build Coastguard Worker => SYSC_newstat 100*387f9dfdSAndroid Build Coastguard Worker => SyS_newstat 101*387f9dfdSAndroid Build Coastguard Worker => entry_SYSCALL_64_fastpath 102*387f9dfdSAndroid Build Coastguard Worker[...] 103*387f9dfdSAndroid Build Coastguard Worker 104*387f9dfdSAndroid Build Coastguard WorkerThis is just an example so that you can see it's possible to dig further. 105*387f9dfdSAndroid Build Coastguard WorkerPlease don't copy-n-paste that kprobe command, as it's unlikely to work (the 106*387f9dfdSAndroid Build Coastguard Worker"+0(+96(%di))" text is specific to a kernel version and architecture). 107*387f9dfdSAndroid Build Coastguard Worker 108*387f9dfdSAndroid Build Coastguard WorkerSo these allocations are coming from user_path_at_empty(), which calls other 109*387f9dfdSAndroid Build Coastguard Workerfunctions (not seen in the stack: I suspect it's a tail-call compiler 110*387f9dfdSAndroid Build Coastguard Workeroptimization). 111*387f9dfdSAndroid Build Coastguard Worker 112*387f9dfdSAndroid Build Coastguard Worker 113*387f9dfdSAndroid Build Coastguard WorkerUSAGE: 114*387f9dfdSAndroid Build Coastguard Worker 115*387f9dfdSAndroid Build Coastguard Worker# ./slabratetop -h 116*387f9dfdSAndroid Build Coastguard Workerusage: slabratetop [-h] [-C] [-r MAXROWS] [interval] [count] 117*387f9dfdSAndroid Build Coastguard Worker 118*387f9dfdSAndroid Build Coastguard WorkerKernel SLAB/SLUB memory cache allocation rate top 119*387f9dfdSAndroid Build Coastguard Worker 120*387f9dfdSAndroid Build Coastguard Workerpositional arguments: 121*387f9dfdSAndroid Build Coastguard Worker interval output interval, in seconds 122*387f9dfdSAndroid Build Coastguard Worker count number of outputs 123*387f9dfdSAndroid Build Coastguard Worker 124*387f9dfdSAndroid Build Coastguard Workeroptional arguments: 125*387f9dfdSAndroid Build Coastguard Worker -h, --help show this help message and exit 126*387f9dfdSAndroid Build Coastguard Worker -C, --noclear don't clear the screen 127*387f9dfdSAndroid Build Coastguard Worker -r MAXROWS, --maxrows MAXROWS 128*387f9dfdSAndroid Build Coastguard Worker maximum rows to print, default 20 129*387f9dfdSAndroid Build Coastguard Worker 130*387f9dfdSAndroid Build Coastguard Workerexamples: 131*387f9dfdSAndroid Build Coastguard Worker ./slabratetop # kmem_cache_alloc() top, 1 second refresh 132*387f9dfdSAndroid Build Coastguard Worker ./slabratetop -C # don't clear the screen 133*387f9dfdSAndroid Build Coastguard Worker ./slabratetop 5 # 5 second summaries 134*387f9dfdSAndroid Build Coastguard Worker ./slabratetop 5 10 # 5 second summaries, 10 times only 135