Skip to contentSkip to conversation

tools/mm: add script to display /proc/buddyinfo from vmcore

PATCH New Aaron Tomlin 1 participant 1 message Oct 23 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/mm: add script to display /proc/buddyinfo from vmcore

When analysing crash dumps (vmcores), inspecting the buddy allocator's free page distribution across memory zones can be useful for diagnosing allocation failures, fragmentation, and watermark exhaustion.

Introduce tools/mm/show_buddyinfo.py, a drgn-based script that traverses online NUMA nodes and populated memory zones to inspect free_area[].nr_free and reconstruct the standard /proc/buddyinfo output.

For example:
❯ sudo drgn --no-default-symbols -c /proc/kcore -s vmlinux tools/mm/show_buddyinfo.py

Node 0, zone DMA           0      0      0      0      0      0      0      0      1      2      2
Node 0, zone DMA32       907    831    581    423    214     85      3      1      2      2      0
Node 0, zone Normal        1      1    143    731    552    182     32      0      0      0      0
---
 tools/mm/show_buddyinfo.py | 48 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100755 tools/mm/show_buddyinfo.py
tools/mm/show_buddyinfo.py new file +48 -0
new file mode 100755
index 000000000000..70372329e7c1
@@ -0,0 +1,48 @@
+#!/usr/bin/env drgn
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2026 Aaron Tomlin <[email protected]>
+
+from collections.abc import Iterator
+
+from drgn import Object, Program, get_default_prog
+from drgn.helpers.linux.nodemask import for_each_online_node
+
+DESC = """
+This is a drgn-based Python script that displays /proc/buddyinfo from a
+vmcore. For further information on drgn, please refer to
+https://github.com/osandov/drgn.
+"""
+
+
+def buddyinfo():
+    """Display the buddyinfo."""
+
+    def walk_zones_in_node(prog: Program) -> Iterator[tuple[Object, Object]]:
+        for node_nr in for_each_online_node(prog):
+            try:
+                pgdat = prog["node_data"][node_nr]
+            except (KeyError, Exception):
+                pgdat = prog["contig_page_data"].address_of_()
+            for zone in pgdat.node_zones:
+                yield pgdat, zone
+
+    def populated_zone(zone: Object) -> bool:
+        """Determine if the specified zone is populated."""
+        return zone.present_pages.value_() > 0
+
+    prog = get_default_prog()
+    for pgdat, zone in walk_zones_in_node(prog):
+        if populated_zone(zone):
+            print(
+                f"Node {pgdat.node_id.value_()}, zone "
+                f"{zone.name.string_().decode('utf-8'):8s} ",
+                end="",
+            )
+            for order in range(len(zone.free_area)):
+                print(f"{zone.free_area[order].nr_free.value_():6} ", end="")
+            print()
+
+
+if __name__ == "__main__":
+    buddyinfo()
Signature · 1 line
2.55.0