tools/mm/show_page_info.py
Source file repositories/reference/linux-study-clean/tools/mm/show_page_info.py
File Facts
- System
- Linux kernel
- Corpus path
tools/mm/show_page_info.py- Extension
.py- Size
- 6653 bytes
- Lines
- 170
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: tools
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
#!/usr/bin/env drgn
# SPDX-License-Identifier: GPL-2.0-only
# Copyright (C) 2025 Ye Liu <liuye@kylinos.cn>
import argparse
import sys
from drgn import Object, FaultError, PlatformFlags, cast
from drgn.helpers.linux import find_task, follow_page, page_size
from drgn.helpers.linux.mm import (
decode_page_flags, page_to_pfn, page_to_phys, page_to_virt, vma_find,
PageSlab, PageCompound, PageHead, PageTail, compound_head, compound_order, compound_nr
)
from drgn.helpers.linux.cgroup import cgroup_name, cgroup_path
DESC = """
This is a drgn script to show the page state.
For more info on drgn, visit https://github.com/osandov/drgn.
"""
def format_page_data(page):
"""
Format raw page data into a readable hex dump with "RAW:" prefix.
:param page: drgn.Object instance representing the page.
:return: Formatted string of memory contents.
"""
try:
address = page.value_()
size = prog.type("struct page").size
if prog.platform.flags & PlatformFlags.IS_64_BIT:
word_size = 8
else:
word_size = 4
num_words = size // word_size
values = []
for i in range(num_words):
word_address = address + i * word_size
word = prog.read_word(word_address)
values.append(f"{word:0{word_size * 2}x}")
lines = [f"RAW: {' '.join(values[i:i + 4])}" for i in range(0, len(values), 4)]
return "\n".join(lines)
except FaultError as e:
return f"Error reading memory: {e}"
except Exception as e:
return f"Unexpected error: {e}"
def get_memcg_info(page):
"""Retrieve memory cgroup information for a page."""
try:
MEMCG_DATA_OBJEXTS = prog.constant("MEMCG_DATA_OBJEXTS").value_()
MEMCG_DATA_KMEM = prog.constant("MEMCG_DATA_KMEM").value_()
mask = prog.constant('__NR_MEMCG_DATA_FLAGS').value_() - 1
memcg_data = page.memcg_data.read_()
if memcg_data & MEMCG_DATA_OBJEXTS:
slabobj_ext = cast("struct slabobj_ext *", memcg_data & ~mask)
memcg = slabobj_ext.objcg.memcg.value_()
elif memcg_data & MEMCG_DATA_KMEM:
objcg = cast("struct obj_cgroup *", memcg_data & ~mask)
memcg = objcg.memcg.value_()
else:
memcg = cast("struct mem_cgroup *", memcg_data & ~mask)
if memcg.value_() == 0:
return "none", "/sys/fs/cgroup/memory/"
cgrp = memcg.css.cgroup
Annotation
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.