tools/mm/page_owner_sort: fix --sort, add module filter, improve usage
Subsystems (3)
v2 cover tools/mm/page_owner_sort: fix --sort, add module filter, improve usage
From: Ye Liu <[email protected]>
This series improves the page_owner_sort tool with a bug fix, a new module-name feature, and better usage text.
Patch 1 fixes a long-standing bug where --sort was silently ignored when used without a short option (-a, -m, -p, etc.). The COMP_NO_FLAG case fell through to COMP_NUM and overwrote the sort conditions configured by parse_sort_args().
Patch 2 adds kernel module name support for sort, cull, and filter operations. Page owner stack traces already contain module names in the "function+0xNN/0xNN [module]" format produced by %pS, but page_owner_sort had no way to use them. Records without module frames are assigned "vmlinux".
# Aggregate page usage per module ./page_owner_sort input.txt output.txt --cull=mod # Filter to records from xfs module only ./page_owner_sort input.txt output.txt --module xfs # Sort by module name, then by pid descending ./page_owner_sort input.txt output.txt --sort=mod,-pid
Patch 3 lists all available sort keys with abbreviations and examples directly in the --sort help section so users no longer need to read the source to discover valid keys.
Ye Liu (3):
tools/mm/page_owner_sort: fix --sort option being silently ignored tools/mm/page_owner_sort: add module name sort/cull/filter support tools/mm/page_owner_sort: show available sort keys in usage text
Documentation/mm/page_owner.rst | 8 +- tools/mm/page_owner_sort.c | 135 +++++++++++++++++++++++++++----- 2 files changed, 123 insertions(+), 20 deletions(-)
---
v2:
- Patch 2: use regexec() directly in get_module() instead of search_pattern() to avoid excessive debug spam for the common vmlinux case when debug_on is true
- Patch 2: anchor module regex to stack trace format "+0xNN/0xNN [module]" to prevent false matches on kernel thread comm names like [khugepaged]
- Patch 2: add '-' to regex character class to support modules with hyphens (e.g. aa-bb-cc)
- Link:https://lore.kernel.org/all/[email protected]/
Signature · 1 line
2.25.1
PATCH v2 1/3 tools/mm/page_owner_sort: fix --sort option being silently ignored
From: Ye Liu <[email protected]>
When --sort is used without any short option (-a, -m, -p, etc.), compare_flag remains COMP_NO_FLAG. The switch (compare_flag) then falls through to the COMP_NUM case and calls set_single_cmp(), which unconditionally overwrites the sort conditions that parse_sort_args() already configured. This makes --sort silently ineffective unless a short option is also supplied.
Split COMP_NO_FLAG out of the COMP_NUM fallthrough so that --sort is respected when no short option is present.
Reproduction:
# Before fix: ascending order (ignored --sort=-pid) ./page_owner_sort --sort=-pid input.txt output.txt # After fix: descending order as expected
--- tools/mm/page_owner_sort.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/mm/page_owner_sort.c +4 -0
@@ -821,6 +821,10 @@ int main(int argc, char **argv)set_single_cmp (compare_stacktrace,SORT_ASC );break ;case COMP_NO_FLAG : +if (sc.size >0 ) +break ; +set_single_cmp (compare_num,SORT_DESC ); +break ;case COMP_NUM :set_single_cmp (compare_num,SORT_DESC );break ;
Signature · 1 line
2.25.1
PATCH v2 2/3 tools/mm/page_owner_sort: add module name sort/cull/filter support
In reply to Ye LiuFrom: Ye Liu <[email protected]>
Page owner stack traces already contain kernel module names in the "function+0xNN/0xNN [module]" format produced by %pS, but page_owner_sort has no way to sort, cull, or filter by module.
Extract the first module name from each record's stack trace using an anchored regex that matches the stack trace frame format "+0x[0-9a-f]+/0x[0-9a-f]+\\s*\\[([a-zA-Z0-9_-]+)\\]".
The regex is anchored to the stack trace frame to avoid false matches on kernel thread comm names such as [khugepaged] that appear in the record header. The character class includes '-' so modules with hyphens (e.g. aa-bb-cc) are correctly matched.
Records whose stack traces contain no module frames are assigned "vmlinux".
New options:
-M Sort by module name --sort=mod Sort by module name (supports +/- prefix) --cull=mod Cull (aggregate) by module name --module <modlist> Filter to records matching the given module(s)
The module field is also printed in cull output when relevant.
--- Documentation/mm/page_owner.rst | 8 ++- tools/mm/page_owner_sort.c | 122 +++++++++++++++++++++++++++----- 2 files changed, 110 insertions(+), 20 deletions(-)
Documentation/mm/page_owner.rst +7 -1
@@ -199,6 +199,7 @@ Usage -p Sort by pid. -P Sort by tgid. -n Sort by task command name. + -M Sort by module name. -r Sort by memory release time. -s Sort by stack trace. -t Sort by times (default).
@@ -240,8 +241,10 @@ Usage group ID numbers appear in <tgidlist>. --name <cmdlist> Select by task command name. This selects the blocks whose task command name appear in <cmdlist>. + --module <modlist> Select by module name. This selects the blocks whose + module name appear in <modlist>. - <pidlist>, <tgidlist>, <cmdlist> are single arguments in the form of a comma-separated list, + <pidlist>, <tgidlist>, <cmdlist>, <modlist> are single arguments in the form of a comma-separated list, which offers a way to specify individual selecting rules.
@@ -249,6 +252,7 @@ Usage ./page_owner_sort <input> <output> --pid=1 ./page_owner_sort <input> <output> --tgid=1,2,3 ./page_owner_sort <input> <output> --name name1,name2 + ./page_owner_sort <input> <output> --module xfs,ext4 STANDARD FORMAT SPECIFIERS ==========================
@@ -265,6 +269,7 @@ STANDARD FORMAT SPECIFIERS ft free_ts timestamp of the page when it was released at alloc_ts timestamp of the page when it was allocated ator allocator memory allocator for pages + mod module kernel module name For --cull option:
@@ -275,6 +280,7 @@ STANDARD FORMAT SPECIFIERS f free whether the page has been released or not st stacktrace stack trace of the page allocation ator allocator memory allocator for pages + mod module kernel module name Filtering page_owner output ============================
tools/mm/page_owner_sort.c +103 -19
292 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v2 3/3 tools/mm/page_owner_sort: show available sort keys in usage text
In reply to Ye LiuFrom: Ye Liu <[email protected]>
The --sort option accepts abbreviated or complete key names, but the usage text never listed them. Users had to read the source or the documentation to discover valid keys.
List all available keys (full form and abbreviation) with a brief description and examples directly in the --sort help section.
--- tools/mm/page_owner_sort.c | 9 +++++++++ 1 file changed, 9 insertions(+)
tools/mm/page_owner_sort.c +9 -0
@@ -772,6 +772,15 @@ static void usage(void)"\t\t\targument in the form of a comma-separated list with some\n" "\t\t\tcommon fields predefined (pid, tgid, comm, stacktrace, allocator, module)\n" "--sort <order>\t\tSpecify sort order as: [+|-]key[,[+|-]key[,...]]\n" +"\t\t\tAvailable keys:\n" +"\t\t\t pid(p), tgid(tg), name(n), stacktrace(st),\n" +"\t\t\t txt(T), alloc_ts(at), allocator(ator), module(mod)\n" +"\t\t\tThe \"+\" is optional since default direction is\n" +"\t\t\tincreasing numerical or lexicographic order.\n" +"\t\t\tMixed use of abbreviated and complete-form is allowed.\n" +"\t\t\tExamples:\n" +"\t\t\t --sort=n,+pid,-tgid\n" +"\t\t\t --sort=mod,at\n" ); }
Signature · 1 line
2.25.1
David Hildenbrand (Arm)
maintainerRe: [PATCH v2 1/3] tools/mm/page_owner_sort: fix --sort option being silently ignored
In reply to Ye LiuQuoted 27 lines · Ye Liu
From: Ye Liu <[email protected]>
When --sort is used without any short option (-a, -m, -p, etc.),
compare_flagremainsCOMP_NO_FLAG. The switch (compare_flag) then falls through to theCOMP_NUMcase and callsset_single_cmp(), which unconditionally overwrites the sort conditions thatparse_sort_args()already configured. This makes --sort silently ineffective unless a short option is also supplied.Split
COMP_NO_FLAGout of theCOMP_NUMfallthrough so that --sort is respected when no short option is present.Reproduction:
# Before fix: ascending order (ignored --sort=-pid) ./page_owner_sort --sort=-pid input.txt output.txt # After fix: descending order as expectedSigned-off-by Ye Liu <[email protected]>--- tools/mm/page_owner_sort.c | 4 ++++ 1 file changed, 4 insertions(+)tools/mm/page_owner_sort.c +0
-0
tools/mm/page_owner_sort.c int main(int argc, char **argv) · line 827
Quoted 7 lines
tools/mm/page_owner_sort.c +4
-0@@ -821,6 +821,10 @@ int main(int argc, char **argv)set_single_cmp (compare_stacktrace,SORT_ASC );break ;case COMP_NO_FLAG : +if (sc.size >0 ) +break ; +set_single_cmp (compare_num,SORT_DESC ); +break ;
Why not a fallthrough; after the check?
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v2 2/3] tools/mm/page_owner_sort: add module name sort/cull/filter support
In reply to Ye LiuQuoted 23 lines · Ye Liu
From: Ye Liu <[email protected]>
Page owner stack traces already contain kernel module names in the "function+0xNN/0xNN [module]" format produced by %pS, but
page_owner_sorthas no way to sort, cull, or filter by module.Extract the first module name from each record's stack trace using an anchored regex that matches the stack trace frame format "+0x[0-9a-f]+/0x[0-9a-f]+\\s*\\[([a-zA-Z0-9_-]+)\\]".
The regex is anchored to the stack trace frame to avoid false matches on kernel thread comm names such as [khugepaged] that appear in the record header. The character class includes '-' so modules with hyphens (e.g. aa-bb-cc) are correctly matched.
Records whose stack traces contain no module frames are assigned "vmlinux".
New options:
-M Sort by module name --sort=mod Sort by module name (supports +/- prefix) --cull=mod Cull (aggregate) by module name --module <modlist> Filter to records matching the given module(s)
Quoted 4 lines
The module field is also printed in cull output when relevant.
Signed-off-by Ye Liu <[email protected]>---
Nothing jumped at me when skimming over it.
Signature · 3 lines
Cheers, David
Re: [PATCH v2 3/3] tools/mm/page_owner_sort: show available sort keys in usage text
In reply to Ye LiuQuoted 11 lines · Ye Liu
From: Ye Liu <[email protected]>
The --sort option accepts abbreviated or complete key names, but the usage text never listed them. Users had to read the source or the documentation to discover valid keys.
List all available keys (full form and abbreviation) with a brief description and examples directly in the --sort help section.
Signed-off-by Ye Liu <[email protected]>---
Signature · 3 lines
Cheers, David