Kernel dynamic memory analysis

This page has notes and results from the project Kernel dynamic memory allocation tracking and reduction

[This page is fairly random at the moment...]

Instrumentation overview

 * Slab_accounting patches
 * uses __builtin_return_address(0) to record the address of the caller, the same mechanism used by kmem events
 * starts from very first allocation


 * Ftrace kmem events
 * does not start until ftrace system is initialized, after some allocations are already performed
 * supported in mainline - no need to add our own instrumentation

These two instrumentation methods are basically the same: trap each kmalloc, kfree, etc. event and produce relevant information with them. The difference between them is that the first post-processes the events in-kernel and create a /proc/slab_account file to access the results. This output is more or less like this:

total bytes allocated: 1256052 total bytes requested: 1077112 slack bytes allocated:  178940 number of allocs:         7414 number of frees:          5022 number of callers:         234 total   slack      req alloc/free  caller 2436     232     2204    29/0     bio_kmalloc+0x33 268       8      260     1/0     pci_alloc_host_bridge+0x1f 32       8       24     1/0     tracepoint_entry_add_probe.isra.2+0x86 44       8       36     1/0     cpuid4_cache_sysfs_init+0x30 0       0        0     0/3     platform_device_add_data+0x33 [...]

On the other hand, analysing ftrace kmem events will defer post-processing to be done at user space, thus achieving much more flexibility. A typical trace log would be like this:

TODO

The disadvantage of the ftrace method is that it needs to be initialized before capturing events. Currently, this initialization is done at fs_initcall and we're working on enabling them earlier. For more information, checkout this upstreamed patch:

trace: Move trace event enable from fs_initcall to core_initcall

This patch allows to enable events at core_initcall. It's also possible to enable it at early_initcall. Another posibility is to create a static ring buffer and then copy the captured events into the real ring buffer.

Also, we must find out if early allocations account for significant memory usage. If not, it may not be that important to capture them. Yet another possibility is to use a printk brute-force approach for very early allocations, and somehow coalesce the data into the final report.

Using debugfs and ftrace
For more information, please refer to the canonical trace documentation at the linux tree:


 * Documentation/trace/ftrace.txt
 * Documentation/trace/tracepoint-analysis.txt
 * and everything else inside Documentation/trace/

(Actually, some of this information has been copied from there.)

Debugfs
The debug filesystem it's a ram-based filesystem that can be used to output a lot of different debugging information. This filesystem is called debugfs and can be enabled with CONFIG_DEBUG_FS:

Kernel hacking [*] Debug filesystem

After you enable this option and boot the built kernel, it creates the directory /sys/kernel/debug as a location for the user to mount the debugfs filesystem. Do this manually:

$ mount -t debugfs none /sys/kernel/debug

You can add a link to type less and get less tired:

$ ln -s /debug /sys/kernel/debug

Tracing
Once we have enabled debugfs, you need to enable tracing support, selecting CONFIG_TRACING, CONFIG_FUNCTION_TRACER and CONFIG_DYNAMIC_FTRACE options.

Kernel hacking Tracers [*] Kernel Function Tracer [*] enable/disable ftrace tracepoints dynamically

This option will add a /sys/kernel/debug/tracing directory on your mounted debugfs filesystem. Traced events can be read through /sys/kernel/debug/tracing/trace. You can list available events by listing file /sys/kernel/debug/tracing/available_events.

To enable events on bootup you can add them to kernel parameters, for instance to enable kmem events: trace_event=kmem:kmalloc,kmem:kmem_cache_alloc,kmem:kfree,kmem:kmem_cache_free

Warning: if you use SLOB on non-NUMA systems, where you might expect kmalloc_node not get called, actually it is the only one called. This is due to SLOB implementing only kmalloc_node and having kmalloc call it without a node. Same goes to kem_cache_alloc_node.

Obtaining accurate call sites (or The painstaking task of wrestling against gcc)
The compiler inlines a lot automatically and without warning. In this scenario, it's impossible to get the real call site name based on just calling address.

When some function is inlined, it gets collapsed and it won't get listed as a symbol if you use tools like readelf, objdump, etc.

Does this matter? Well, it matters if you want to obtain an accurate call site report when tracing kernel memory events (which will see later).

However, there is one solution! You can turn off gcc inlining using some gcc options on kernel Makefile. You need to add these to your CFLAGS:

KBUILD_CFLAGS  += -fno-default-inline \ -fno-inline \ -fno-inline-small-functions \ -fno-indirect-inlining \ -fno-inline-functions-called-once

It's not yet clear if all of these are needed, or just some of them.

Using ./scripts/bloat-o-meter it's possible to determine the effect of using this gcc flags:

$ ./scripts/bloat-o-meter vmlinux-def vmlinux-no-inline add/remove: 1574/33 grow/shrink: 154/1099 up/down: 218552/-199352 (19200) function                                    old     new   delta hidinput_configure_usage                      -    8672   +8672 do_con_trol                                   -    2955   +2955 do_con_write                                  -    2223   +2223 copy_process                                  -    2078   +2078 [...]

The effect of disable inlining is now very clear:
 * There are 1574 new functions, and 33 less functions.
 * Some functions have grown, but many more have shrunk.
 * Overall, the kernel symbols's size is 19k bigger.

We must keep in mind that no matter what internal mechanisms we use to record call_site, if they're based on __builtin_address, then their accuracy will depend entirely on gcc *not* inlining automatically.

The enfasis is in the automatic part. There will be lots of functions we will need to get inlined in order to determine the caller correctly. These will be marked as __always_inline.

See upstreamed patch:

Makefile: Add option CONFIG_DISABLE_GCC_AUTOMATIC_INLINING)

Types of memory
The kernel, being a computer program, can consume memory in two different ways: statically and dynamically.

Static memory can be measured offline, therefore accounted before actually running the kernel using standard binary inspection utilites (readelf, objdump, size, etc). We will explore this utilities in detail.

Dynamic memory cannot be measured offline, and it's not only necessary to probe a running kernel but also to enable aditional probe code to trace each allocation. Fortunately for us, the linux kernel has ftrace which is a tracing framework that allows to trace general events, and in particular memory allocation events. We will explore this framework in detail.

Static memory
A compiled kernel will allocate static memory to store two kinds of symbols: code symbols (a.k.a text) and data symbols (a.k.a data).

For instance, let's look at this piece of C code:

long curr; int max = 10;

int foo(int var) {    int i = var + max; if (i < max) curr = i; }

We have three different symbols:
 * curr : a variable (data zero initialized)
 * max : an initialized variable (data non-zero initialized)
 * foo : a function (text)

Once this code is running each of these symbols will need memory for its own storage. However, the zero initialized variable will not use space in the compiled binary. This is due to a special section inside the binary (called bss for no good reason) where all the zero initialized variables are placed. Since they carry no information, they need no space. Static variables have the same life as the executing program.

On the other side, var and i variables are dynamically allocated, since they live in the stack. They are called automatic variables, meaning that they have a life cycle that's not under our control.

Note that when we talk about static memory, the word static has nothing to do with the C-language keyword. This keyword references a visibility class, where static means local, as opposed to global.

The size command
The most simple command to get a binary static size, is the wonderfully called size command. Let's start by seeing it in action:

$ size ./fs/ext2/ext2.o   text	   data	    bss	    dec	    hex	filename 51273	    68	      8	  51349	   c895	./fs/ext2/ext2.o

According to this output, this object file has roughly 50k bytes of text and 68 bytes of data. Now, size comes in two flavors: berkeley and sysv. Each of this shows a different output. The default is berkeley, so the previous example was a berkeley output.

However, if we use the same command with sysv output format, we'll find very different results:

$ size --format=sysv ./fs/ext2/ext2.o ./fs/ext2/ext2.o  : section            size   addr .text             43005      0 .init.text          138      0 .exit.text           25      0 .rodata            2304      0 .rodata.str1.1     1656      0 .rodata.str1.4     3485      0 .data                60      0 .exitcall.exit        4      0 .initcall6.init       4      0 .bss                  8      0 .note.GNU-stack       0      0 .comment            675      0 [...]

Here we see a more detailed description about each section size. Note the appearence of a .rodata (read-only data) section, of 2k byte large. This section is composed of read-only variables (e.g. marked const) that are not accounted by the standard size format.

We can conclude that standard size format gives an incomplete picture of the compiled object.

To add even more confusion to this picture, gcc can decide (at his own will) to put inside .rodata section symbols not marked as const. These symbols are not written by anyone, and gcc considers them as read-only (pretty smart, uh?). This means you can have a .rodata section bigger than what you expected to have.

This happens since gcc v4.7 (???)

readelf
This two commands can give us any information we need about a binary. In particular, they can output the complete list of symbols with detailed information about each one. Let's see an example of readelf on the same file we used for size. The output is stripped for clarity.

$ readelf -s fs/ext2/ext2.o

Symbol table '.symtab' contains 413 entries: Num:   Value  Size Type    Bind   Vis      Ndx Name 0: 00000000    0 NOTYPE  LOCAL  DEFAULT  UND 1: 00000000    0 SECTION LOCAL  DEFAULT    1 [...]  339: 00004da0   286 FUNC    GLOBAL DEFAULT    1 ext2_evict_inode 340: 000003e0   76 OBJECT  GLOBAL DEFAULT    7 ext2_nobh_aops 341: 00000780  128 OBJECT  GLOBAL DEFAULT    7 ext2_symlink_inode_operat 342: 000008d8   20 OBJECT  GLOBAL DEFAULT    7 ext2_xattr_acl_default_ha 343: 00000000    0 NOTYPE  GLOBAL DEFAULT  UND generic_file_aio_write 344: 00000280  128 OBJECT  GLOBAL DEFAULT    7 ext2_file_inode_operation 345: 00000000    0 NOTYPE  GLOBAL DEFAULT  UND __dquot_alloc_space 346: 00000000    0 NOTYPE  GLOBAL DEFAULT  UND generic_setxattr 347: 00000000    0 NOTYPE  GLOBAL DEFAULT  UND unlock_buffer 348: 000014c0   36 FUNC    GLOBAL DEFAULT    1 ext2_bg_num_gdb 349: 00005240  684 FUNC    GLOBAL DEFAULT    1 ext2_setattr [...]

For instance, ext2_nobh_aops is an OBJECT symbol (data) of 76 bytes and ext2_evict_inode is a FUNC symbol (text) of 286 bytes. Notice there are some UND symbols. They are undefined symbols for this file, that are defined elsewhere and therefore not of interest for us when inspecting a file size.

Of course, this output can be combined with grep to get fantastic results. Let's count the numbers of defined functions:

$ readelf -s fs/ext2/ext2.o | grep -v UND | grep FUNC

With a little awk magic we could even sum these sizes and get the size of the .text section. TODO!

objdump
TODO

Dynamic
Dynamic memory in kernel land is a little different from user land.

In user land, all one needs to do to get a chunk of memory is call malloc. In kernel land, we have a similar function: kmalloc. But we also have lots of other functions to alloc memory, and we must have some special considerations.

The first thing it's important to understand is that kernel obtains memory (well, in most architectures) on a fixed-size chunk, that we call a 'page' of memory. This page of memory tipically is 4096 bytes large, but this depends on the architecture.

In order to delivery smaller pieces of memory, the kernel have a few couple of layers that ultimately lets you do kmalloc(100) and get 100 bytes. These layers are called: buddy allocator and slab allocator.

We will focus on the latter. Slab allocator comes in three different flavors: SLAB, SLOB and SLUB. These funny names are historically, but the meaning is:


 * SLAB is the traditional
 * SLOB is aimed at tiny embedded systesm (e.g. without mmu)
 * SLUB is the default

Each of these implement the allocation in a different way, but they all share a common property: internal fragmentation.

Internal fragmentation
For different reasons (alignment, overhead, etc) when we request 100 bytes with kmalloc(100) the slab allocator may really allocate 128 bytes (or 140 bytes, we can't really know). These extra 28 bytes can't be used, and therefore you are wasting them. This is called internal fragmentation, and one of the main goals of the slab allocator is to minimize it. In other words, trying to match as nearly as possible the requested size with the truly allocated size.

Accounting with kmem events trace
Ftrace kmem events are a great source of information. By using them you can trace each kmalloc, getting the requested bytes, the allocated bytes, the caller address and the returned pointer. You can also trace kfree, getting the caller address and the freed pointer.

Once you have the caller address you can use System.map file to get the caller function name. Also, by using the returned pointer and correlating with kfree traces you can keep track of currently used dynamic memory by each kernel function / subsystem.

Let's see this in detail.

Enabling and reading kmem trace
We can activate this on boot up with kernel parameter trace_event. For instance, trace_event=kmem:kmalloc,kmem:kmem_cache_alloc,kmem:kfree,kmem:kmem_cache_free

or you can activate on-the-fly with:

TODO

Once you have enabled events, you can run your favourite program (in order to trigger some allocations). When you're done you may disable events (or not) and read them:

$ cat /sys/kernel/debug/tracing/trace > kmem.log

Let's see a piece of this log:

# #           init-1     [000] .N.. 0.170000: kmalloc: call_site=c104deff ptr=c147dd20 bytes_req=29 bytes_alloc=32 gfp_flags=GFP_KERNEL init-1    [000] .N.. 0.170000: kmalloc: call_site=c104e2ac ptr=c147dd00 bytes_req=24 bytes_alloc=32 gfp_flags=GFP_KERNEL init-1    [000] .N.. 0.170000: kmalloc: call_site=c104deff ptr=c147dce0 bytes_req=22 bytes_alloc=32 gfp_flags=GFP_KERNEL init-1    [000] .N.. 0.170000: kmalloc: call_site=c104e2ac ptr=c147dcc0 bytes_req=24 bytes_alloc=32 gfp_flags=GFP_KERNEL init-1    [000] .N.. 0.170000: kmalloc: call_site=c10c0185 ptr=c14a4ba0 bytes_req=36 bytes_alloc=64 gfp_flags=GFP_KERNEL|GFP_ZERO init-1    [000] .N.. 0.170000: kmalloc: call_site=c10bfa28 ptr=c147dca0 bytes_req=6 bytes_alloc=32 gfp_flags=GFP_KERNEL init-1    [000] .N.. 0.170000: kfree: call_site=c10bfa4a ptr= (null) init-1    [000] .N.. 0.170000: kmalloc: call_site=c10b9619 ptr=c147dc80 bytes_req=6 bytes_alloc=32 gfp_flags=GFP_KERNEL init-1    [000] .N.. 0.180000: kmem_cache_alloc: call_site=c10b963e ptr=c1610c08 bytes_req=64 bytes_alloc=64 gfp_flags=GFP_KERNEL|GFP_ZERO
 * 1) tracer: nop
 * 1) entries-in-buffer/entries-written: 10500/10500   #P:1
 * 1)                              _-=> irqs-off
 * 2)                             / _=> need-resched
 * 3)                            | / _---=> hardirq/softirq
 * 4)                            || / _--=> preempt-depth
 * 5)                            ||| /     delay
 * 6)           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION

This log can be post-processed. For your convenience we have a script trace_analyze.py that does exactly this (see below).

About kmem trace events
As we have seen there are a few more events than kmalloc and kfree. Let's see them all:


 * kmalloc
 * kfree
 * kmalloc_node
 * kmem_cache_alloc
 * kmem_cache_alloc_node
 * kmem_cache_free

Getting the script
You can get script last version from this project's github repository

git clone https://github.com/ezequielgarcia/kmem-probe-framework cd kmem-probe-framework ./post-process/trace_analyze.py -h

Using trace_analyze.py for static analysis
trace_analyze.py typically needs access to: a built kernel tree and an ftrace kmem log.

However, if one lacks the latter but can provide a built kernel tree, the script will fallback to 'static' analysis.

Using it
Usage is fairly simple

$ ./trace_analyze.py -k /usr/src/linux $ ./trace_analyze.py --kernel /usr/src/linux

This should produce a ringchart png file in the current directory, named linux.png.

Of course, you can use absolute and relative paths in that parameter

$ ./trace_analyze.py -k ./torvalds

If you're interested in a specific subsystem you can use a parameter to specify the directory tree branch to take as root

$ ./trace_analyze -k ./torvalds -b fs $ ./trace_analyze -k ./torvalds -b drivers $ ./trace_analyze -k ./torvalds -b mm

Each of this commands will produce a ringchart png file in the curent directory, named as the specified branch (fs.png, drivers.png, mm.png, etc).

Under the hood
The script will perform a directory walk, internally creating a tree matching the provided kernel tree. On each object file found (like fs/inode.o) it will perform a 'readelf --syms' to get a list of symbols contained in it.

Nothing fancy.

TODO

 * Account for minor differences between running 'size' and using this script

Producing a kmem trace log file
The purpose of trace_analyze script is to perform dynamic memory analysis. For this to work you need feed it with a kmem trace log file; of course, you also need to give hime a built kernel tree.

Such log must be produced on the running target kernel, but you can post-process it off-box. For instance, you boot your kernel with kmem parameters to enable ftrace kmem events: (it's recommended to enable all events, despite not running a NUMA machine).

kmem="kmem:kmalloc,kmem:kmalloc_node,kmem:kfree,kmem:kmem_cache_alloc,kmem:kmem_cache_alloc_node,kmem:kmem_cache_free"

This parameter will have linux to start tracing as soon as possible. Of course some early traces will be lost, see below.

(on your target kernel)

$ echo "0" > /sys/kernel/debug/tracing/tracing_on
 * 1) To stop tracing

$ cat /sys/kernel/debug/tracing/trace > kmem.log
 * 1) Dump

Now you need to get this file so you can post-process it using trace_analyze.py. In my case, I use qemu with a file backing serial device, so I simply do:

(on your target kernel) $ cat /sys/kernel/debug/tracing/trace > /dev/ttyS0

And I get the log on qemu's backing file.

Now you have everything you need to start the analysis. The script will post-process the logfile and will produce two kinds of output:
 * an account file
 * a ringchart png

Let's see how to obtain each of these.

Slab accounting file output
To obtain the account file you need to use --acount-file (-c) parameter, like this:

./trace_analyze.py -k ../torvalds -f kmem.log --account-file kmem_account.txt

This will produce an account file like this:

current bytes allocated:    669696 current bytes requested:    618823 current wasted bytes:        50873 number of allocs:             7649 number of frees:              2563 number of callers:             115 total   waste      net alloc/free  caller - 299200        0   298928  1100/1     alloc_inode+0x4fL 189824       0   140544  1483/385   __d_alloc+0x22L 51904       0    47552   811/68    sysfs_new_dirent+0x4eL 16384    8088    16384     1/0     __seq_open_private+0x24L 15936    1328    15936    83/0     device_create_vargs+0x42L 14720   10898    14016   460/22    sysfs_new_dirent+0x29L

You can tell the script to read only kmalloc events (notice the option name is *--malloc*):

./trace_analyze.py -k ../torvalds -f kmem.log -c kmem_account.txt --malloc

Or you can tell the script to read only kmem_cache events:

./trace_analyze.py -k ../torvalds -f kmem.log -c kmem_account.txt --cache If you want to order the account file you can use --order-by (-o):

./trace_analyze.py -k ../torvalds -f kmem.log -c kmem_account.txt --order-by=waste

The possible options for order-by parameter are:


 * total_dynamic: Added allocations size
 * current_dynamic: Currently allocated size
 * alloc_count: Number of allocations
 * free_count: Number of frees
 * waste: Currently wasted size

You can pick a directory to get an account file only for the allocations from that directory. This is done with the --branch (-b) option, just like we've done for the static analysis:

$ ./trace_analyze.py -k ../torvalds -f kmem.log -c kmem_account.txt -b drivers

All of these options can be combined. For instance, if you want to get kmalloc events only, coming from fs/ directory and ordered by current size:

$ ./trace_analyze.py -k ../torvalds -f kmem.log -b fs -c kmem_account.txt -o current_dynamic --malloc

Producing a pretty ringchart for dynamic allocations
As already explained in the static analysis section, it's possible to produce a ringchart to get 'the big picture' of dynamic allocations. You will need to have matplotlib installed, which should be as easy as:

$ {your_pkg_manager} install matplotlib

The script usage is very simple, just pass the parameter --rings (-r)

$ ./trace_analyze.py -k ../torvalds -f kmem.log --rings

This command will produce a png file named as 'linux.png' by default. The plot will show current dynamic allocations by default. You can control the used attrbute used for the ringchar plot using --rings-attr (-a) parameter. The available options are:


 * current: static + current dynamic size
 * static: static size
 * waste: wasted size
 * current_dynamic: current dynamic size
 * total_dyamic: added dynamic size

$ ./trace_analyze.py -k ../torvalds -f kmem.log -r -a waste

You can use --branch (-b) parameter to plot allocations made from just one directory. For instance, if you want to get wasted bytes for ext4 filesystem:

$ ./trace_analyze.py -k ../torvalds -f kmem.log -r -a waste -b fs/ext4

Or, if you want to see static footprint of arch-dependent mm code:

$ ./trace_analyze.py -k ../torvalds -f kmem.log -r -a static -b arch/x86/mm

Also, you can filter kmalloc or kmem_cache traces using either --malloc, or --cache:

$ ./trace_analyze.py -k linux/ -f boot_kmem.log -r --malloc

Pitfall: wrongly reported allocation (and how to fix it)
There are a number of functions (kstrdup, kmemdup, krealloc, etc) that do some kind of allocation on behalf of its caller.

Of course, we don't want to get trace reports from these functions, but rather from its caller. To acomplish this, we must use a variant of kmalloc, called kmalloc_track_caller, which does exactly that.

Let's see an example. As of today kvasprintf implementation looks like this

(see lib/kasprintf.c:14) char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap) {	/* code removed */ p = kmalloc(len+1, gfp);

And trace_analyze produces the account file

total   waste      net alloc/free  caller - 2161    1184     2161   148/0     kvasprintf

The source of this 148 allocations may be a single caller, or it may be multiple callers. We just can't know. However, if we replace kmalloc with kmalloc_track_caller, we're going to find that out.

char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap) {       /* code removed */ p = kmalloc_track_caller(len+1, gfp);

After running the re-built kernel, and comparing both current and previous account files, we find this is the real caller:

total   waste      net alloc/free  caller - 2161    1184     2161   148/0     kobject_set_name_vargs

So, we've accurately tracked this allocation down to the kobject code.

Reporting

 * extracting data to host
 * tool for extraction (perf?, cat /debugfs/tracing/ ?)
 * post-processing the data
 * grouping allocations (assigning to different subsystems, processes, or functional areas)
 * idea to post-process kmem events and correlate with */built-in.o
 * reporting on wasted bytes
 * reporting on memory fragmentation

Visualization
Matplotlib []

We will use matplotlib with its Wedge api to create a ring chart (similar to gnome baobab).

We'll get something like this (this is very minimal kernel run):

Mainline status

 * trace: Move trace event enable from fs_initcall to core_initcall
 * https://lkml.org/lkml/2012/9/8/152
 * mm: SLxB cleaning and trace accuracy improvement
 * https://lkml.org/lkml/2012/9/8/170
 * mm: Use __do_krealloc to do the krealloc job
 * git commit id: e21827aa []

Results so far (in random order)

 * There's a lot of fragmentation using the SLAB allocator. [how much?]
 * SLxB accounting is a dead-end (it won't be accepted into mainline)

more???

Testing feedback
There is some testing feedback from Tim on the Talk:Kernel_dynamic_memory_analysis page.