]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - Documentation/vm/idle_page_tracking.rst
docs/admin-guide: introduce basic index for mm documentation
[mirror_ubuntu-jammy-kernel.git] / Documentation / vm / idle_page_tracking.rst
CommitLineData
e3f2025a
MR
1.. _idle_page_tracking:
2
3==================
4Idle Page Tracking
5==================
6
7Motivation
8==========
33c3fc71
VD
9
10The idle page tracking feature allows to track which memory pages are being
11accessed by a workload and which are idle. This information can be useful for
12estimating the workload's working set size, which, in turn, can be taken into
13account when configuring the workload parameters, setting memory cgroup limits,
14or deciding where to place the workload within a compute cluster.
15
16It is enabled by CONFIG_IDLE_PAGE_TRACKING=y.
17
e3f2025a 18.. _user_api:
33c3fc71 19
e3f2025a
MR
20User API
21========
22
23The idle page tracking API is located at ``/sys/kernel/mm/page_idle``.
24Currently, it consists of the only read-write file,
25``/sys/kernel/mm/page_idle/bitmap``.
33c3fc71
VD
26
27The file implements a bitmap where each bit corresponds to a memory page. The
28bitmap is represented by an array of 8-byte integers, and the page at PFN #i is
29mapped to bit #i%64 of array element #i/64, byte order is native. When a bit is
30set, the corresponding page is idle.
31
32A page is considered idle if it has not been accessed since it was marked idle
e3f2025a
MR
33(for more details on what "accessed" actually means see the :ref:`Implementation
34Details <impl_details>` section).
35To mark a page idle one has to set the bit corresponding to
33c3fc71
VD
36the page by writing to the file. A value written to the file is OR-ed with the
37current bitmap value.
38
39Only accesses to user memory pages are tracked. These are pages mapped to a
40process address space, page cache and buffer pages, swap cache pages. For other
41page types (e.g. SLAB pages) an attempt to mark a page idle is silently ignored,
42and hence such pages are never reported idle.
43
44For huge pages the idle flag is set only on the head page, so one has to read
e3f2025a 45``/proc/kpageflags`` in order to correctly count idle huge pages.
33c3fc71 46
e3f2025a 47Reading from or writing to ``/sys/kernel/mm/page_idle/bitmap`` will return
33c3fc71
VD
48-EINVAL if you are not starting the read/write on an 8-byte boundary, or
49if the size of the read/write is not a multiple of 8 bytes. Writing to
50this file beyond max PFN will return -ENXIO.
51
52That said, in order to estimate the amount of pages that are not used by a
53workload one should:
54
55 1. Mark all the workload's pages as idle by setting corresponding bits in
e3f2025a
MR
56 ``/sys/kernel/mm/page_idle/bitmap``. The pages can be found by reading
57 ``/proc/pid/pagemap`` if the workload is represented by a process, or by
58 filtering out alien pages using ``/proc/kpagecgroup`` in case the workload
59 is placed in a memory cgroup.
33c3fc71
VD
60
61 2. Wait until the workload accesses its working set.
62
e3f2025a
MR
63 3. Read ``/sys/kernel/mm/page_idle/bitmap`` and count the number of bits set.
64 If one wants to ignore certain types of pages, e.g. mlocked pages since they
65 are not reclaimable, he or she can filter them out using
66 ``/proc/kpageflags``.
67
ad56b738 68See Documentation/vm/pagemap.rst for more information about
e3f2025a 69``/proc/pid/pagemap``, ``/proc/kpageflags``, and ``/proc/kpagecgroup``.
33c3fc71 70
e3f2025a 71.. _impl_details:
33c3fc71 72
e3f2025a
MR
73Implementation Details
74======================
33c3fc71
VD
75
76The kernel internally keeps track of accesses to user memory pages in order to
77reclaim unreferenced pages first on memory shortage conditions. A page is
78considered referenced if it has been recently accessed via a process address
79space, in which case one or more PTEs it is mapped to will have the Accessed bit
80set, or marked accessed explicitly by the kernel (see mark_page_accessed()). The
81latter happens when:
82
83 - a userspace process reads or writes a page using a system call (e.g. read(2)
84 or write(2))
85
86 - a page that is used for storing filesystem buffers is read or written,
87 because a process needs filesystem metadata stored in it (e.g. lists a
88 directory tree)
89
90 - a page is accessed by a device driver using get_user_pages()
91
92When a dirty page is written to swap or disk as a result of memory reclaim or
93exceeding the dirty memory limit, it is not marked referenced.
94
95The idle memory tracking feature adds a new page flag, the Idle flag. This flag
e3f2025a
MR
96is set manually, by writing to ``/sys/kernel/mm/page_idle/bitmap`` (see the
97:ref:`User API <user_api>`
33c3fc71
VD
98section), and cleared automatically whenever a page is referenced as defined
99above.
100
101When a page is marked idle, the Accessed bit must be cleared in all PTEs it is
102mapped to, otherwise we will not be able to detect accesses to the page coming
103from a process address space. To avoid interference with the reclaimer, which,
104as noted above, uses the Accessed bit to promote actively referenced pages, one
105more page flag is introduced, the Young flag. When the PTE Accessed bit is
106cleared as a result of setting or updating a page's Idle flag, the Young flag
107is set on the page. The reclaimer treats the Young flag as an extra PTE
108Accessed bit and therefore will consider such a page as referenced.
109
110Since the idle memory tracking feature is based on the memory reclaimer logic,
111it only works with pages that are on an LRU list, other pages are silently
112ignored. That means it will ignore a user memory page if it is isolated, but
113since there are usually not many of them, it should not affect the overall
114result noticeably. In order not to stall scanning of the idle page bitmap,
115locked pages may be skipped too.