transhuge.rst 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. ============================
  2. Transparent Hugepage Support
  3. ============================
  4. Objective
  5. =========
  6. Performance critical computing applications dealing with large memory
  7. working sets are already running on top of libhugetlbfs and in turn
  8. hugetlbfs. Transparent HugePage Support (THP) is an alternative mean of
  9. using huge pages for the backing of virtual memory with huge pages
  10. that supports the automatic promotion and demotion of page sizes and
  11. without the shortcomings of hugetlbfs.
  12. Currently THP only works for anonymous memory mappings and tmpfs/shmem.
  13. But in the future it can expand to other filesystems.
  14. .. note::
  15. in the examples below we presume that the basic page size is 4K and
  16. the huge page size is 2M, although the actual numbers may vary
  17. depending on the CPU architecture.
  18. The reason applications are running faster is because of two
  19. factors. The first factor is almost completely irrelevant and it's not
  20. of significant interest because it'll also have the downside of
  21. requiring larger clear-page copy-page in page faults which is a
  22. potentially negative effect. The first factor consists in taking a
  23. single page fault for each 2M virtual region touched by userland (so
  24. reducing the enter/exit kernel frequency by a 512 times factor). This
  25. only matters the first time the memory is accessed for the lifetime of
  26. a memory mapping. The second long lasting and much more important
  27. factor will affect all subsequent accesses to the memory for the whole
  28. runtime of the application. The second factor consist of two
  29. components:
  30. 1) the TLB miss will run faster (especially with virtualization using
  31. nested pagetables but almost always also on bare metal without
  32. virtualization)
  33. 2) a single TLB entry will be mapping a much larger amount of virtual
  34. memory in turn reducing the number of TLB misses. With
  35. virtualization and nested pagetables the TLB can be mapped of
  36. larger size only if both KVM and the Linux guest are using
  37. hugepages but a significant speedup already happens if only one of
  38. the two is using hugepages just because of the fact the TLB miss is
  39. going to run faster.
  40. Modern kernels support "multi-size THP" (mTHP), which introduces the
  41. ability to allocate memory in blocks that are bigger than a base page
  42. but smaller than traditional PMD-size (as described above), in
  43. increments of a power-of-2 number of pages. mTHP can back anonymous
  44. memory (for example 16K, 32K, 64K, etc). These THPs continue to be
  45. PTE-mapped, but in many cases can still provide similar benefits to
  46. those outlined above: Page faults are significantly reduced (by a
  47. factor of e.g. 4, 8, 16, etc), but latency spikes are much less
  48. prominent because the size of each page isn't as huge as the PMD-sized
  49. variant and there is less memory to clear in each page fault. Some
  50. architectures also employ TLB compression mechanisms to squeeze more
  51. entries in when a set of PTEs are virtually and physically contiguous
  52. and approporiately aligned. In this case, TLB misses will occur less
  53. often.
  54. THP can be enabled system wide or restricted to certain tasks or even
  55. memory ranges inside task's address space. Unless THP is completely
  56. disabled, there is ``khugepaged`` daemon that scans memory and
  57. collapses sequences of basic pages into PMD-sized huge pages.
  58. The THP behaviour is controlled via :ref:`sysfs <thp_sysfs>`
  59. interface and using madvise(2) and prctl(2) system calls.
  60. Transparent Hugepage Support maximizes the usefulness of free memory
  61. if compared to the reservation approach of hugetlbfs by allowing all
  62. unused memory to be used as cache or other movable (or even unmovable
  63. entities). It doesn't require reservation to prevent hugepage
  64. allocation failures to be noticeable from userland. It allows paging
  65. and all other advanced VM features to be available on the
  66. hugepages. It requires no modifications for applications to take
  67. advantage of it.
  68. Applications however can be further optimized to take advantage of
  69. this feature, like for example they've been optimized before to avoid
  70. a flood of mmap system calls for every malloc(4k). Optimizing userland
  71. is by far not mandatory and khugepaged already can take care of long
  72. lived page allocations even for hugepage unaware applications that
  73. deals with large amounts of memory.
  74. In certain cases when hugepages are enabled system wide, application
  75. may end up allocating more memory resources. An application may mmap a
  76. large region but only touch 1 byte of it, in that case a 2M page might
  77. be allocated instead of a 4k page for no good. This is why it's
  78. possible to disable hugepages system-wide and to only have them inside
  79. MADV_HUGEPAGE madvise regions.
  80. Embedded systems should enable hugepages only inside madvise regions
  81. to eliminate any risk of wasting any precious byte of memory and to
  82. only run faster.
  83. Applications that gets a lot of benefit from hugepages and that don't
  84. risk to lose memory by using hugepages, should use
  85. madvise(MADV_HUGEPAGE) on their critical mmapped regions.
  86. .. _thp_sysfs:
  87. sysfs
  88. =====
  89. Global THP controls
  90. -------------------
  91. Transparent Hugepage Support for anonymous memory can be disabled
  92. (mostly for debugging purposes) or only enabled inside MADV_HUGEPAGE
  93. regions (to avoid the risk of consuming more memory resources) or enabled
  94. system wide. This can be achieved per-supported-THP-size with one of::
  95. echo always >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/enabled
  96. echo madvise >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/enabled
  97. echo never >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/enabled
  98. where <size> is the hugepage size being addressed, the available sizes
  99. for which vary by system.
  100. .. note:: Setting "never" in all sysfs THP controls does **not** disable
  101. Transparent Huge Pages globally. This is because ``madvise(...,
  102. MADV_COLLAPSE)`` ignores these settings and collapses ranges to
  103. PMD-sized huge pages unconditionally.
  104. For example::
  105. echo always >/sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled
  106. Alternatively it is possible to specify that a given hugepage size
  107. will inherit the top-level "enabled" value::
  108. echo inherit >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/enabled
  109. For example::
  110. echo inherit >/sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled
  111. The top-level setting (for use with "inherit") can be set by issuing
  112. one of the following commands::
  113. echo always >/sys/kernel/mm/transparent_hugepage/enabled
  114. echo madvise >/sys/kernel/mm/transparent_hugepage/enabled
  115. echo never >/sys/kernel/mm/transparent_hugepage/enabled
  116. By default, PMD-sized hugepages have enabled="inherit" and all other
  117. hugepage sizes have enabled="never". If enabling multiple hugepage
  118. sizes, the kernel will select the most appropriate enabled size for a
  119. given allocation.
  120. It's also possible to limit defrag efforts in the VM to generate
  121. anonymous hugepages in case they're not immediately free to madvise
  122. regions or to never try to defrag memory and simply fallback to regular
  123. pages unless hugepages are immediately available. Clearly if we spend CPU
  124. time to defrag memory, we would expect to gain even more by the fact we
  125. use hugepages later instead of regular pages. This isn't always
  126. guaranteed, but it may be more likely in case the allocation is for a
  127. MADV_HUGEPAGE region.
  128. ::
  129. echo always >/sys/kernel/mm/transparent_hugepage/defrag
  130. echo defer >/sys/kernel/mm/transparent_hugepage/defrag
  131. echo defer+madvise >/sys/kernel/mm/transparent_hugepage/defrag
  132. echo madvise >/sys/kernel/mm/transparent_hugepage/defrag
  133. echo never >/sys/kernel/mm/transparent_hugepage/defrag
  134. always
  135. means that an application requesting THP will stall on
  136. allocation failure and directly reclaim pages and compact
  137. memory in an effort to allocate a THP immediately. This may be
  138. desirable for virtual machines that benefit heavily from THP
  139. use and are willing to delay the VM start to utilise them.
  140. defer
  141. means that an application will wake kswapd in the background
  142. to reclaim pages and wake kcompactd to compact memory so that
  143. THP is available in the near future. It's the responsibility
  144. of khugepaged to then install the THP pages later.
  145. defer+madvise
  146. will enter direct reclaim and compaction like ``always``, but
  147. only for regions that have used madvise(MADV_HUGEPAGE); all
  148. other regions will wake kswapd in the background to reclaim
  149. pages and wake kcompactd to compact memory so that THP is
  150. available in the near future.
  151. madvise
  152. will enter direct reclaim like ``always`` but only for regions
  153. that are have used madvise(MADV_HUGEPAGE). This is the default
  154. behaviour.
  155. never
  156. should be self-explanatory. Note that ``madvise(...,
  157. MADV_COLLAPSE)`` can still cause transparent huge pages to be
  158. obtained even if this mode is specified everywhere.
  159. By default kernel tries to use huge, PMD-mappable zero page on read
  160. page fault to anonymous mapping. It's possible to disable huge zero
  161. page by writing 0 or enable it back by writing 1::
  162. echo 0 >/sys/kernel/mm/transparent_hugepage/use_zero_page
  163. echo 1 >/sys/kernel/mm/transparent_hugepage/use_zero_page
  164. Some userspace (such as a test program, or an optimized memory
  165. allocation library) may want to know the size (in bytes) of a
  166. PMD-mappable transparent hugepage::
  167. cat /sys/kernel/mm/transparent_hugepage/hpage_pmd_size
  168. All THPs at fault and collapse time will be added to _deferred_list,
  169. and will therefore be split under memory presure if they are considered
  170. "underused". A THP is underused if the number of zero-filled pages in
  171. the THP is above max_ptes_none (see below). It is possible to disable
  172. this behaviour by writing 0 to shrink_underused, and enable it by writing
  173. 1 to it::
  174. echo 0 > /sys/kernel/mm/transparent_hugepage/shrink_underused
  175. echo 1 > /sys/kernel/mm/transparent_hugepage/shrink_underused
  176. khugepaged will be automatically started when PMD-sized THP is enabled
  177. (either of the per-size anon control or the top-level control are set
  178. to "always" or "madvise"), and it'll be automatically shutdown when
  179. PMD-sized THP is disabled (when both the per-size anon control and the
  180. top-level control are "never")
  181. process THP controls
  182. --------------------
  183. A process can control its own THP behaviour using the ``PR_SET_THP_DISABLE``
  184. and ``PR_GET_THP_DISABLE`` pair of prctl(2) calls. The THP behaviour set using
  185. ``PR_SET_THP_DISABLE`` is inherited across fork(2) and execve(2). These calls
  186. support the following arguments::
  187. prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0):
  188. This will disable THPs completely for the process, irrespective
  189. of global THP controls or madvise(..., MADV_COLLAPSE) being used.
  190. prctl(PR_SET_THP_DISABLE, 1, PR_THP_DISABLE_EXCEPT_ADVISED, 0, 0):
  191. This will disable THPs for the process except when the usage of THPs is
  192. advised. Consequently, THPs will only be used when:
  193. - Global THP controls are set to "always" or "madvise" and
  194. madvise(..., MADV_HUGEPAGE) or madvise(..., MADV_COLLAPSE) is used.
  195. - Global THP controls are set to "never" and madvise(..., MADV_COLLAPSE)
  196. is used. This is the same behavior as if THPs would not be disabled on
  197. a process level.
  198. Note that MADV_COLLAPSE is currently always rejected if
  199. madvise(..., MADV_NOHUGEPAGE) is set on an area.
  200. prctl(PR_SET_THP_DISABLE, 0, 0, 0, 0):
  201. This will re-enable THPs for the process, as if they were never disabled.
  202. Whether THPs will actually be used depends on global THP controls and
  203. madvise() calls.
  204. prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0):
  205. This returns a value whose bits indicate how THP-disable is configured:
  206. Bits
  207. 1 0 Value Description
  208. |0|0| 0 No THP-disable behaviour specified.
  209. |0|1| 1 THP is entirely disabled for this process.
  210. |1|1| 3 THP-except-advised mode is set for this process.
  211. Khugepaged controls
  212. -------------------
  213. .. note::
  214. khugepaged currently only searches for opportunities to collapse to
  215. PMD-sized THP and no attempt is made to collapse to other THP
  216. sizes.
  217. khugepaged runs usually at low frequency so while one may not want to
  218. invoke defrag algorithms synchronously during the page faults, it
  219. should be worth invoking defrag at least in khugepaged. However it's
  220. also possible to disable defrag in khugepaged by writing 0 or enable
  221. defrag in khugepaged by writing 1::
  222. echo 0 >/sys/kernel/mm/transparent_hugepage/khugepaged/defrag
  223. echo 1 >/sys/kernel/mm/transparent_hugepage/khugepaged/defrag
  224. You can also control how many pages khugepaged should scan at each
  225. pass::
  226. /sys/kernel/mm/transparent_hugepage/khugepaged/pages_to_scan
  227. and how many milliseconds to wait in khugepaged between each pass (you
  228. can set this to 0 to run khugepaged at 100% utilization of one core)::
  229. /sys/kernel/mm/transparent_hugepage/khugepaged/scan_sleep_millisecs
  230. and how many milliseconds to wait in khugepaged if there's an hugepage
  231. allocation failure to throttle the next allocation attempt::
  232. /sys/kernel/mm/transparent_hugepage/khugepaged/alloc_sleep_millisecs
  233. The khugepaged progress can be seen in the number of pages collapsed (note
  234. that this counter may not be an exact count of the number of pages
  235. collapsed, since "collapsed" could mean multiple things: (1) A PTE mapping
  236. being replaced by a PMD mapping, or (2) All 4K physical pages replaced by
  237. one 2M hugepage. Each may happen independently, or together, depending on
  238. the type of memory and the failures that occur. As such, this value should
  239. be interpreted roughly as a sign of progress, and counters in /proc/vmstat
  240. consulted for more accurate accounting)::
  241. /sys/kernel/mm/transparent_hugepage/khugepaged/pages_collapsed
  242. for each pass::
  243. /sys/kernel/mm/transparent_hugepage/khugepaged/full_scans
  244. ``max_ptes_none`` specifies how many extra small pages (that are
  245. not already mapped) can be allocated when collapsing a group
  246. of small pages into one large page::
  247. /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none
  248. A higher value leads to use additional memory for programs.
  249. A lower value leads to gain less thp performance. Value of
  250. max_ptes_none can waste cpu time very little, you can
  251. ignore it.
  252. ``max_ptes_swap`` specifies how many pages can be brought in from
  253. swap when collapsing a group of pages into a transparent huge page::
  254. /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_swap
  255. A higher value can cause excessive swap IO and waste
  256. memory. A lower value can prevent THPs from being
  257. collapsed, resulting fewer pages being collapsed into
  258. THPs, and lower memory access performance.
  259. ``max_ptes_shared`` specifies how many pages can be shared across multiple
  260. processes. khugepaged might treat pages of THPs as shared if any page of
  261. that THP is shared. Exceeding the number would block the collapse::
  262. /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_shared
  263. A higher value may increase memory footprint for some workloads.
  264. Boot parameters
  265. ===============
  266. You can change the sysfs boot time default for the top-level "enabled"
  267. control by passing the parameter ``transparent_hugepage=always`` or
  268. ``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
  269. kernel command line.
  270. Alternatively, each supported anonymous THP size can be controlled by
  271. passing ``thp_anon=<size>[KMG],<size>[KMG]:<state>;<size>[KMG]-<size>[KMG]:<state>``,
  272. where ``<size>`` is the THP size (must be a power of 2 of PAGE_SIZE and
  273. supported anonymous THP) and ``<state>`` is one of ``always``, ``madvise``,
  274. ``never`` or ``inherit``.
  275. For example, the following will set 16K, 32K, 64K THP to ``always``,
  276. set 128K, 512K to ``inherit``, set 256K to ``madvise`` and 1M, 2M
  277. to ``never``::
  278. thp_anon=16K-64K:always;128K,512K:inherit;256K:madvise;1M-2M:never
  279. ``thp_anon=`` may be specified multiple times to configure all THP sizes as
  280. required. If ``thp_anon=`` is specified at least once, any anon THP sizes
  281. not explicitly configured on the command line are implicitly set to
  282. ``never``.
  283. ``transparent_hugepage`` setting only affects the global toggle. If
  284. ``thp_anon`` is not specified, PMD_ORDER THP will default to ``inherit``.
  285. However, if a valid ``thp_anon`` setting is provided by the user, the
  286. PMD_ORDER THP policy will be overridden. If the policy for PMD_ORDER
  287. is not defined within a valid ``thp_anon``, its policy will default to
  288. ``never``.
  289. Similarly to ``transparent_hugepage``, you can control the hugepage
  290. allocation policy for the internal shmem mount by using the kernel parameter
  291. ``transparent_hugepage_shmem=<policy>``, where ``<policy>`` is one of the
  292. seven valid policies for shmem (``always``, ``within_size``, ``advise``,
  293. ``never``, ``deny``, and ``force``).
  294. Similarly to ``transparent_hugepage_shmem``, you can control the default
  295. hugepage allocation policy for the tmpfs mount by using the kernel parameter
  296. ``transparent_hugepage_tmpfs=<policy>``, where ``<policy>`` is one of the
  297. four valid policies for tmpfs (``always``, ``within_size``, ``advise``,
  298. ``never``). The tmpfs mount default policy is ``never``.
  299. Additionally, Kconfig options are available to set the default hugepage
  300. policies for shmem (``CONFIG_TRANSPARENT_HUGEPAGE_SHMEM_HUGE_*``) and tmpfs
  301. (``CONFIG_TRANSPARENT_HUGEPAGE_TMPFS_HUGE_*``) at build time. Refer to the
  302. Kconfig help for more details.
  303. In the same manner as ``thp_anon`` controls each supported anonymous THP
  304. size, ``thp_shmem`` controls each supported shmem THP size. ``thp_shmem``
  305. has the same format as ``thp_anon``, but also supports the policy
  306. ``within_size``.
  307. ``thp_shmem=`` may be specified multiple times to configure all THP sizes
  308. as required. If ``thp_shmem=`` is specified at least once, any shmem THP
  309. sizes not explicitly configured on the command line are implicitly set to
  310. ``never``.
  311. ``transparent_hugepage_shmem`` setting only affects the global toggle. If
  312. ``thp_shmem`` is not specified, PMD_ORDER hugepage will default to
  313. ``inherit``. However, if a valid ``thp_shmem`` setting is provided by the
  314. user, the PMD_ORDER hugepage policy will be overridden. If the policy for
  315. PMD_ORDER is not defined within a valid ``thp_shmem``, its policy will
  316. default to ``never``.
  317. Hugepages in tmpfs/shmem
  318. ========================
  319. Traditionally, tmpfs only supported a single huge page size ("PMD"). Today,
  320. it also supports smaller sizes just like anonymous memory, often referred
  321. to as "multi-size THP" (mTHP). Huge pages of any size are commonly
  322. represented in the kernel as "large folios".
  323. While there is fine control over the huge page sizes to use for the internal
  324. shmem mount (see below), ordinary tmpfs mounts will make use of all available
  325. huge page sizes without any control over the exact sizes, behaving more like
  326. other file systems.
  327. tmpfs mounts
  328. ------------
  329. The THP allocation policy for tmpfs mounts can be adjusted using the mount
  330. option: ``huge=``. It can have following values:
  331. always
  332. Attempt to allocate huge pages every time we need a new page;
  333. Always try PMD-sized huge pages first, and fall back to smaller-sized
  334. huge pages if the PMD-sized huge page allocation fails;
  335. never
  336. Do not allocate huge pages. Note that ``madvise(..., MADV_COLLAPSE)``
  337. can still cause transparent huge pages to be obtained even if this mode
  338. is specified everywhere;
  339. within_size
  340. Only allocate huge page if it will be fully within i_size;
  341. Always try PMD-sized huge pages first, and fall back to smaller-sized
  342. huge pages if the PMD-sized huge page allocation fails;
  343. Also respect madvise() hints;
  344. advise
  345. Only allocate huge pages if requested with madvise();
  346. Remember, that the kernel may use huge pages of all available sizes, and
  347. that no fine control as for the internal tmpfs mount is available.
  348. The default policy in the past was ``never``, but it can now be adjusted
  349. using the kernel parameter ``transparent_hugepage_tmpfs=<policy>``.
  350. ``mount -o remount,huge= /mountpoint`` works fine after mount: remounting
  351. ``huge=never`` will not attempt to break up huge pages at all, just stop more
  352. from being allocated.
  353. In addition to policies listed above, the sysfs knob
  354. /sys/kernel/mm/transparent_hugepage/shmem_enabled will affect the
  355. allocation policy of tmpfs mounts, when set to the following values:
  356. deny
  357. For use in emergencies, to force the huge option off from
  358. all mounts;
  359. force
  360. Force the huge option on for all - very useful for testing;
  361. shmem / internal tmpfs
  362. ----------------------
  363. The mount internal tmpfs mount is used for SysV SHM, memfds, shared anonymous
  364. mmaps (of /dev/zero or MAP_ANONYMOUS), GPU drivers' DRM objects, Ashmem.
  365. To control the THP allocation policy for this internal tmpfs mount, the
  366. sysfs knob /sys/kernel/mm/transparent_hugepage/shmem_enabled and the knobs
  367. per THP size in
  368. '/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/shmem_enabled'
  369. can be used.
  370. The global knob has the same semantics as the ``huge=`` mount options
  371. for tmpfs mounts, except that the different huge page sizes can be controlled
  372. individually, and will only use the setting of the global knob when the
  373. per-size knob is set to 'inherit'.
  374. The options 'force' and 'deny' are dropped for the individual sizes, which
  375. are rather testing artifacts from the old ages.
  376. always
  377. Attempt to allocate <size> huge pages every time we need a new page;
  378. inherit
  379. Inherit the top-level "shmem_enabled" value. By default, PMD-sized hugepages
  380. have enabled="inherit" and all other hugepage sizes have enabled="never";
  381. never
  382. Do not allocate <size> huge pages. Note that ``madvise(...,
  383. MADV_COLLAPSE)`` can still cause transparent huge pages to be obtained
  384. even if this mode is specified everywhere;
  385. within_size
  386. Only allocate <size> huge page if it will be fully within i_size.
  387. Also respect madvise() hints;
  388. advise
  389. Only allocate <size> huge pages if requested with madvise();
  390. Need of application restart
  391. ===========================
  392. The transparent_hugepage/enabled and
  393. transparent_hugepage/hugepages-<size>kB/enabled values and tmpfs mount
  394. option only affect future behavior. So to make them effective you need
  395. to restart any application that could have been using hugepages. This
  396. also applies to the regions registered in khugepaged.
  397. Monitoring usage
  398. ================
  399. The number of PMD-sized anonymous transparent huge pages currently used by the
  400. system is available by reading the AnonHugePages field in ``/proc/meminfo``.
  401. To identify what applications are using PMD-sized anonymous transparent huge
  402. pages, it is necessary to read ``/proc/PID/smaps`` and count the AnonHugePages
  403. fields for each mapping. (Note that AnonHugePages only applies to traditional
  404. PMD-sized THP for historical reasons and should have been called
  405. AnonHugePmdMapped).
  406. The number of file transparent huge pages mapped to userspace is available
  407. by reading ShmemPmdMapped and ShmemHugePages fields in ``/proc/meminfo``.
  408. To identify what applications are mapping file transparent huge pages, it
  409. is necessary to read ``/proc/PID/smaps`` and count the FilePmdMapped fields
  410. for each mapping.
  411. Note that reading the smaps file is expensive and reading it
  412. frequently will incur overhead.
  413. There are a number of counters in ``/proc/vmstat`` that may be used to
  414. monitor how successfully the system is providing huge pages for use.
  415. thp_fault_alloc
  416. is incremented every time a huge page is successfully
  417. allocated and charged to handle a page fault.
  418. thp_collapse_alloc
  419. is incremented by khugepaged when it has found
  420. a range of pages to collapse into one huge page and has
  421. successfully allocated a new huge page to store the data.
  422. thp_fault_fallback
  423. is incremented if a page fault fails to allocate or charge
  424. a huge page and instead falls back to using small pages.
  425. thp_fault_fallback_charge
  426. is incremented if a page fault fails to charge a huge page and
  427. instead falls back to using small pages even though the
  428. allocation was successful.
  429. thp_collapse_alloc_failed
  430. is incremented if khugepaged found a range
  431. of pages that should be collapsed into one huge page but failed
  432. the allocation.
  433. thp_file_alloc
  434. is incremented every time a shmem huge page is successfully
  435. allocated (Note that despite being named after "file", the counter
  436. measures only shmem).
  437. thp_file_fallback
  438. is incremented if a shmem huge page is attempted to be allocated
  439. but fails and instead falls back to using small pages. (Note that
  440. despite being named after "file", the counter measures only shmem).
  441. thp_file_fallback_charge
  442. is incremented if a shmem huge page cannot be charged and instead
  443. falls back to using small pages even though the allocation was
  444. successful. (Note that despite being named after "file", the
  445. counter measures only shmem).
  446. thp_file_mapped
  447. is incremented every time a file or shmem huge page is mapped into
  448. user address space.
  449. thp_split_page
  450. is incremented every time a huge page is split into base
  451. pages. This can happen for a variety of reasons but a common
  452. reason is that a huge page is old and is being reclaimed.
  453. This action implies splitting all PMD the page mapped with.
  454. thp_split_page_failed
  455. is incremented if kernel fails to split huge
  456. page. This can happen if the page was pinned by somebody.
  457. thp_deferred_split_page
  458. is incremented when a huge page is put onto split
  459. queue. This happens when a huge page is partially unmapped and
  460. splitting it would free up some memory. Pages on split queue are
  461. going to be split under memory pressure.
  462. thp_underused_split_page
  463. is incremented when a huge page on the split queue was split
  464. because it was underused. A THP is underused if the number of
  465. zero pages in the THP is above a certain threshold
  466. (/sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none).
  467. thp_split_pmd
  468. is incremented every time a PMD split into table of PTEs.
  469. This can happen, for instance, when application calls mprotect() or
  470. munmap() on part of huge page. It doesn't split huge page, only
  471. page table entry.
  472. thp_zero_page_alloc
  473. is incremented every time a huge zero page used for thp is
  474. successfully allocated. Note, it doesn't count every map of
  475. the huge zero page, only its allocation.
  476. thp_zero_page_alloc_failed
  477. is incremented if kernel fails to allocate
  478. huge zero page and falls back to using small pages.
  479. thp_swpout
  480. is incremented every time a huge page is swapout in one
  481. piece without splitting.
  482. thp_swpout_fallback
  483. is incremented if a huge page has to be split before swapout.
  484. Usually because failed to allocate some continuous swap space
  485. for the huge page.
  486. In /sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/stats, There are
  487. also individual counters for each huge page size, which can be utilized to
  488. monitor the system's effectiveness in providing huge pages for usage. Each
  489. counter has its own corresponding file.
  490. anon_fault_alloc
  491. is incremented every time a huge page is successfully
  492. allocated and charged to handle a page fault.
  493. anon_fault_fallback
  494. is incremented if a page fault fails to allocate or charge
  495. a huge page and instead falls back to using huge pages with
  496. lower orders or small pages.
  497. anon_fault_fallback_charge
  498. is incremented if a page fault fails to charge a huge page and
  499. instead falls back to using huge pages with lower orders or
  500. small pages even though the allocation was successful.
  501. zswpout
  502. is incremented every time a huge page is swapped out to zswap in one
  503. piece without splitting.
  504. swpin
  505. is incremented every time a huge page is swapped in from a non-zswap
  506. swap device in one piece.
  507. swpin_fallback
  508. is incremented if swapin fails to allocate or charge a huge page
  509. and instead falls back to using huge pages with lower orders or
  510. small pages.
  511. swpin_fallback_charge
  512. is incremented if swapin fails to charge a huge page and instead
  513. falls back to using huge pages with lower orders or small pages
  514. even though the allocation was successful.
  515. swpout
  516. is incremented every time a huge page is swapped out to a non-zswap
  517. swap device in one piece without splitting.
  518. swpout_fallback
  519. is incremented if a huge page has to be split before swapout.
  520. Usually because failed to allocate some continuous swap space
  521. for the huge page.
  522. shmem_alloc
  523. is incremented every time a shmem huge page is successfully
  524. allocated.
  525. shmem_fallback
  526. is incremented if a shmem huge page is attempted to be allocated
  527. but fails and instead falls back to using small pages.
  528. shmem_fallback_charge
  529. is incremented if a shmem huge page cannot be charged and instead
  530. falls back to using small pages even though the allocation was
  531. successful.
  532. split
  533. is incremented every time a huge page is successfully split into
  534. smaller orders. This can happen for a variety of reasons but a
  535. common reason is that a huge page is old and is being reclaimed.
  536. split_failed
  537. is incremented if kernel fails to split huge
  538. page. This can happen if the page was pinned by somebody.
  539. split_deferred
  540. is incremented when a huge page is put onto split queue.
  541. This happens when a huge page is partially unmapped and splitting
  542. it would free up some memory. Pages on split queue are going to
  543. be split under memory pressure, if splitting is possible.
  544. nr_anon
  545. the number of anonymous THP we have in the whole system. These THPs
  546. might be currently entirely mapped or have partially unmapped/unused
  547. subpages.
  548. nr_anon_partially_mapped
  549. the number of anonymous THP which are likely partially mapped, possibly
  550. wasting memory, and have been queued for deferred memory reclamation.
  551. Note that in corner some cases (e.g., failed migration), we might detect
  552. an anonymous THP as "partially mapped" and count it here, even though it
  553. is not actually partially mapped anymore.
  554. As the system ages, allocating huge pages may be expensive as the
  555. system uses memory compaction to copy data around memory to free a
  556. huge page for use. There are some counters in ``/proc/vmstat`` to help
  557. monitor this overhead.
  558. compact_stall
  559. is incremented every time a process stalls to run
  560. memory compaction so that a huge page is free for use.
  561. compact_success
  562. is incremented if the system compacted memory and
  563. freed a huge page for use.
  564. compact_fail
  565. is incremented if the system tries to compact memory
  566. but failed.
  567. It is possible to establish how long the stalls were using the function
  568. tracer to record how long was spent in __alloc_pages() and
  569. using the mm_page_alloc tracepoint to identify which allocations were
  570. for huge pages.
  571. Optimizing the applications
  572. ===========================
  573. To be guaranteed that the kernel will map a THP immediately in any
  574. memory region, the mmap region has to be hugepage naturally
  575. aligned. posix_memalign() can provide that guarantee.
  576. Hugetlbfs
  577. =========
  578. You can use hugetlbfs on a kernel that has transparent hugepage
  579. support enabled just fine as always. No difference can be noted in
  580. hugetlbfs other than there will be less overall fragmentation. All
  581. usual features belonging to hugetlbfs are preserved and
  582. unaffected. libhugetlbfs will also work fine as usual.