For monitoring the memory usage per stack trace, it is more efficient to use the handle number as unique identifier of a stack trace than to, for example, read/hash all stack traces to uniquely identify them everytime. The handle number is a unique identifier for stack traces in stackdepot. This patch adds the option 'print_handle' to print the handle number of stack traces in 'show_stacks'. Testing: - Without handles (default): # cat /sys/kernel/debug/page_owner_stacks/show_stacks > f1 - With handles (new option): # echo 1 >/sys/kernel/debug/page_owner_stacks/print_handle # cat /sys/kernel/debug/page_owner_stacks/show_stacks > f2 - Same number of lines for 'nr_base_pages' and 'handle': # grep -c '^nr_base_pages:' f1 873 # grep -c '^handle:' f2 873 Signed-off-by: Mauricio Faria de Oliveira --- mm/page_owner.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/page_owner.c b/mm/page_owner.c index c3ca21132c2c..420426749239 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -887,6 +887,7 @@ static void *stack_next(struct seq_file *m, void *v, loff_t *ppos) } static unsigned long page_owner_pages_threshold; +static bool page_owner_print_handle; static int stack_print(struct seq_file *m, void *v) { @@ -908,6 +909,8 @@ static int stack_print(struct seq_file *m, void *v) for (i = 0; i < nr_entries; i++) seq_printf(m, " %pS\n", (void *)entries[i]); + if (page_owner_print_handle) + seq_printf(m, "handle: %d\n", stack_record->handle.handle); seq_printf(m, "nr_base_pages: %d\n\n", nr_base_pages); return 0; @@ -968,6 +971,8 @@ static int __init pageowner_init(void) &page_owner_stack_operations); debugfs_create_file("count_threshold", 0600, dir, NULL, &proc_page_owner_threshold); + debugfs_create_bool("print_handle", 0600, dir, + &page_owner_print_handle); return 0; } -- 2.48.1 For monitoring the memory usage per stack trace, it is more efficient to read _just_ the handle number of the stack traces _without_ stack traces themselves, and their number of base pages. The stack traces are required only at the time to associate memory usage of a handle number with its stack trace (eg, see top-consuming handles). Before that, it is sufficient to have just the handle number, as it can be matched with a stack trace later (possible with the previous patch). This patch adds the option 'print_stack' option (enabled by default) to print stack traces in 'show_stacks'. Testing: - Enable handle numbers: # echo 1 >/sys/kernel/debug/page_owner_stacks/print_handle - With stacks (default): # cat /sys/kernel/debug/page_owner_stacks/show_stacks > f1 - Without stacks (new option): # echo 0 >/sys/kernel/debug/page_owner_stacks/print_stack # cat /sys/kernel/debug/page_owner_stacks/show_stacks > f2 - Differences: # cat f1 get_page_from_freelist+0x14ab/0x16a0 ... do_syscall_64+0xa4/0x290 handle: 15728651 nr_base_pages: 2 get_page_from_freelist+0x14ab/0x16a0 ... do_sys_openat2+0x8a/0xe0 handle: 8388619 nr_base_pages: 16 ... # cat f2 handle: 15728651 nr_base_pages: 2 handle: 8388619 nr_base_pages: 16 ... Signed-off-by: Mauricio Faria de Oliveira --- mm/page_owner.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index 420426749239..25221676676d 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -888,6 +888,7 @@ static void *stack_next(struct seq_file *m, void *v, loff_t *ppos) static unsigned long page_owner_pages_threshold; static bool page_owner_print_handle; +static bool page_owner_print_stack = true; static int stack_print(struct seq_file *m, void *v) { @@ -900,15 +901,17 @@ static int stack_print(struct seq_file *m, void *v) if (!stack->stack_record) return 0; - nr_entries = stack_record->size; - entries = stack_record->entries; nr_base_pages = refcount_read(&stack_record->count) - 1; if (nr_base_pages < 1 || nr_base_pages < page_owner_pages_threshold) return 0; - for (i = 0; i < nr_entries; i++) - seq_printf(m, " %pS\n", (void *)entries[i]); + if (page_owner_print_stack) { + nr_entries = stack_record->size; + entries = stack_record->entries; + for (i = 0; i < nr_entries; i++) + seq_printf(m, " %pS\n", (void *)entries[i]); + } if (page_owner_print_handle) seq_printf(m, "handle: %d\n", stack_record->handle.handle); seq_printf(m, "nr_base_pages: %d\n\n", nr_base_pages); @@ -973,6 +976,8 @@ static int __init pageowner_init(void) &proc_page_owner_threshold); debugfs_create_bool("print_handle", 0600, dir, &page_owner_print_handle); + debugfs_create_bool("print_stack", 0600, dir, + &page_owner_print_stack); return 0; } -- 2.48.1 Describe and provide examples for 'print_handle' and 'print_stack'. Signed-off-by: Mauricio Faria de Oliveira --- Documentation/mm/page_owner.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Documentation/mm/page_owner.rst b/Documentation/mm/page_owner.rst index 3a45a20fc05a..fac14ff2e4a5 100644 --- a/Documentation/mm/page_owner.rst +++ b/Documentation/mm/page_owner.rst @@ -27,7 +27,10 @@ enabled. Other usages are more than welcome. It can also be used to show all the stacks and their current number of allocated base pages, which gives us a quick overview of where the memory is going without the need to screen through all the pages and match the -allocation and free operation. +allocation and free operation. It's also possible to show only a numeric +identifier of all the stacks (without stack traces) and their number of +allocated base pages (faster to read and parse, eg, for monitoring) that +can be matched with stacks later (options print_handle and print_stack). page owner is disabled by default. So, if you'd like to use it, you need to add "page_owner=on" to your boot cmdline. If the kernel is built @@ -95,6 +98,7 @@ Usage ... ... echo 7000 > /sys/kernel/debug/page_owner_stacks/count_threshold + echo 1 > /sys/kernel/debug/page_owner_stacks/print_handle cat /sys/kernel/debug/page_owner_stacks/show_stacks> stacks_7000.txt cat stacks_7000.txt post_alloc_hook+0x177/0x1a0 @@ -113,6 +117,15 @@ Usage __do_sys_finit_module+0x381/0x730 do_syscall_64+0x8d/0x150 entry_SYSCALL_64_after_hwframe+0x62/0x6a + handle: 42 + nr_base_pages: 20824 + ... + + echo 1 > /sys/kernel/debug/page_owner_stacks/print_handle + echo 0 > /sys/kernel/debug/page_owner_stacks/print_stack + cat /sys/kernel/debug/page_owner_stacks/show_stacks > handles_7000.txt + cat handles_7000.txt + handle: 42 nr_base_pages: 20824 ... -- 2.48.1