transhuge.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 entirely 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. For example::
  101. echo always >/sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled
  102. Alternatively it is possible to specify that a given hugepage size
  103. will inherit the top-level "enabled" value::
  104. echo inherit >/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/enabled
  105. For example::
  106. echo inherit >/sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled
  107. The top-level setting (for use with "inherit") can be set by issuing
  108. one of the following commands::
  109. echo always >/sys/kernel/mm/transparent_hugepage/enabled
  110. echo madvise >/sys/kernel/mm/transparent_hugepage/enabled
  111. echo never >/sys/kernel/mm/transparent_hugepage/enabled
  112. By default, PMD-sized hugepages have enabled="inherit" and all other
  113. hugepage sizes have enabled="never". If enabling multiple hugepage
  114. sizes, the kernel will select the most appropriate enabled size for a
  115. given allocation.
  116. It's also possible to limit defrag efforts in the VM to generate
  117. anonymous hugepages in case they're not immediately free to madvise
  118. regions or to never try to defrag memory and simply fallback to regular
  119. pages unless hugepages are immediately available. Clearly if we spend CPU
  120. time to defrag memory, we would expect to gain even more by the fact we
  121. use hugepages later instead of regular pages. This isn't always
  122. guaranteed, but it may be more likely in case the allocation is for a
  123. MADV_HUGEPAGE region.
  124. ::
  125. echo always >/sys/kernel/mm/transparent_hugepage/defrag
  126. echo defer >/sys/kernel/mm/transparent_hugepage/defrag
  127. echo defer+madvise >/sys/kernel/mm/transparent_hugepage/defrag
  128. echo madvise >/sys/kernel/mm/transparent_hugepage/defrag
  129. echo never >/sys/kernel/mm/transparent_hugepage/defrag
  130. always
  131. means that an application requesting THP will stall on
  132. allocation failure and directly reclaim pages and compact
  133. memory in an effort to allocate a THP immediately. This may be
  134. desirable for virtual machines that benefit heavily from THP
  135. use and are willing to delay the VM start to utilise them.
  136. defer
  137. means that an application will wake kswapd in the background
  138. to reclaim pages and wake kcompactd to compact memory so that
  139. THP is available in the near future. It's the responsibility
  140. of khugepaged to then install the THP pages later.
  141. defer+madvise
  142. will enter direct reclaim and compaction like ``always``, but
  143. only for regions that have used madvise(MADV_HUGEPAGE); all
  144. other regions will wake kswapd in the background to reclaim
  145. pages and wake kcompactd to compact memory so that THP is
  146. available in the near future.
  147. madvise
  148. will enter direct reclaim like ``always`` but only for regions
  149. that are have used madvise(MADV_HUGEPAGE). This is the default
  150. behaviour.
  151. never
  152. should be self-explanatory.
  153. By default kernel tries to use huge, PMD-mappable zero page on read
  154. page fault to anonymous mapping. It's possible to disable huge zero
  155. page by writing 0 or enable it back by writing 1::
  156. echo 0 >/sys/kernel/mm/transparent_hugepage/use_zero_page
  157. echo 1 >/sys/kernel/mm/transparent_hugepage/use_zero_page
  158. Some userspace (such as a test program, or an optimized memory
  159. allocation library) may want to know the size (in bytes) of a
  160. PMD-mappable transparent hugepage::
  161. cat /sys/kernel/mm/transparent_hugepage/hpage_pmd_size
  162. All THPs at fault and collapse time will be added to _deferred_list,
  163. and will therefore be split under memory presure if they are considered
  164. "underused". A THP is underused if the number of zero-filled pages in
  165. the THP is above max_ptes_none (see below). It is possible to disable
  166. this behaviour by writing 0 to shrink_underused, and enable it by writing
  167. 1 to it::
  168. echo 0 > /sys/kernel/mm/transparent_hugepage/shrink_underused
  169. echo 1 > /sys/kernel/mm/transparent_hugepage/shrink_underused
  170. khugepaged will be automatically started when PMD-sized THP is enabled
  171. (either of the per-size anon control or the top-level control are set
  172. to "always" or "madvise"), and it'll be automatically shutdown when
  173. PMD-sized THP is disabled (when both the per-size anon control and the
  174. top-level control are "never")
  175. Khugepaged controls
  176. -------------------
  177. .. note::
  178. khugepaged currently only searches for opportunities to collapse to
  179. PMD-sized THP and no attempt is made to collapse to other THP
  180. sizes.
  181. khugepaged runs usually at low frequency so while one may not want to
  182. invoke defrag algorithms synchronously during the page faults, it
  183. should be worth invoking defrag at least in khugepaged. However it's
  184. also possible to disable defrag in khugepaged by writing 0 or enable
  185. defrag in khugepaged by writing 1::
  186. echo 0 >/sys/kernel/mm/transparent_hugepage/khugepaged/defrag
  187. echo 1 >/sys/kernel/mm/transparent_hugepage/khugepaged/defrag
  188. You can also control how many pages khugepaged should scan at each
  189. pass::
  190. /sys/kernel/mm/transparent_hugepage/khugepaged/pages_to_scan
  191. and how many milliseconds to wait in khugepaged between each pass (you
  192. can set this to 0 to run khugepaged at 100% utilization of one core)::
  193. /sys/kernel/mm/transparent_hugepage/khugepaged/scan_sleep_millisecs
  194. and how many milliseconds to wait in khugepaged if there's an hugepage
  195. allocation failure to throttle the next allocation attempt::
  196. /sys/kernel/mm/transparent_hugepage/khugepaged/alloc_sleep_millisecs
  197. The khugepaged progress can be seen in the number of pages collapsed (note
  198. that this counter may not be an exact count of the number of pages
  199. collapsed, since "collapsed" could mean multiple things: (1) A PTE mapping
  200. being replaced by a PMD mapping, or (2) All 4K physical pages replaced by
  201. one 2M hugepage. Each may happen independently, or together, depending on
  202. the type of memory and the failures that occur. As such, this value should
  203. be interpreted roughly as a sign of progress, and counters in /proc/vmstat
  204. consulted for more accurate accounting)::
  205. /sys/kernel/mm/transparent_hugepage/khugepaged/pages_collapsed
  206. for each pass::
  207. /sys/kernel/mm/transparent_hugepage/khugepaged/full_scans
  208. ``max_ptes_none`` specifies how many extra small pages (that are
  209. not already mapped) can be allocated when collapsing a group
  210. of small pages into one large page::
  211. /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none
  212. A higher value leads to use additional memory for programs.
  213. A lower value leads to gain less thp performance. Value of
  214. max_ptes_none can waste cpu time very little, you can
  215. ignore it.
  216. ``max_ptes_swap`` specifies how many pages can be brought in from
  217. swap when collapsing a group of pages into a transparent huge page::
  218. /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_swap
  219. A higher value can cause excessive swap IO and waste
  220. memory. A lower value can prevent THPs from being
  221. collapsed, resulting fewer pages being collapsed into
  222. THPs, and lower memory access performance.
  223. ``max_ptes_shared`` specifies how many pages can be shared across multiple
  224. processes. khugepaged might treat pages of THPs as shared if any page of
  225. that THP is shared. Exceeding the number would block the collapse::
  226. /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_shared
  227. A higher value may increase memory footprint for some workloads.
  228. Boot parameters
  229. ===============
  230. You can change the sysfs boot time default for the top-level "enabled"
  231. control by passing the parameter ``transparent_hugepage=always`` or
  232. ``transparent_hugepage=madvise`` or ``transparent_hugepage=never`` to the
  233. kernel command line.
  234. Alternatively, each supported anonymous THP size can be controlled by
  235. passing ``thp_anon=<size>[KMG],<size>[KMG]:<state>;<size>[KMG]-<size>[KMG]:<state>``,
  236. where ``<size>`` is the THP size (must be a power of 2 of PAGE_SIZE and
  237. supported anonymous THP) and ``<state>`` is one of ``always``, ``madvise``,
  238. ``never`` or ``inherit``.
  239. For example, the following will set 16K, 32K, 64K THP to ``always``,
  240. set 128K, 512K to ``inherit``, set 256K to ``madvise`` and 1M, 2M
  241. to ``never``::
  242. thp_anon=16K-64K:always;128K,512K:inherit;256K:madvise;1M-2M:never
  243. ``thp_anon=`` may be specified multiple times to configure all THP sizes as
  244. required. If ``thp_anon=`` is specified at least once, any anon THP sizes
  245. not explicitly configured on the command line are implicitly set to
  246. ``never``.
  247. ``transparent_hugepage`` setting only affects the global toggle. If
  248. ``thp_anon`` is not specified, PMD_ORDER THP will default to ``inherit``.
  249. However, if a valid ``thp_anon`` setting is provided by the user, the
  250. PMD_ORDER THP policy will be overridden. If the policy for PMD_ORDER
  251. is not defined within a valid ``thp_anon``, its policy will default to
  252. ``never``.
  253. Hugepages in tmpfs/shmem
  254. ========================
  255. You can control hugepage allocation policy in tmpfs with mount option
  256. ``huge=``. It can have following values:
  257. always
  258. Attempt to allocate huge pages every time we need a new page;
  259. never
  260. Do not allocate huge pages;
  261. within_size
  262. Only allocate huge page if it will be fully within i_size.
  263. Also respect fadvise()/madvise() hints;
  264. advise
  265. Only allocate huge pages if requested with fadvise()/madvise();
  266. The default policy is ``never``.
  267. ``mount -o remount,huge= /mountpoint`` works fine after mount: remounting
  268. ``huge=never`` will not attempt to break up huge pages at all, just stop more
  269. from being allocated.
  270. There's also sysfs knob to control hugepage allocation policy for internal
  271. shmem mount: /sys/kernel/mm/transparent_hugepage/shmem_enabled. The mount
  272. is used for SysV SHM, memfds, shared anonymous mmaps (of /dev/zero or
  273. MAP_ANONYMOUS), GPU drivers' DRM objects, Ashmem.
  274. In addition to policies listed above, shmem_enabled allows two further
  275. values:
  276. deny
  277. For use in emergencies, to force the huge option off from
  278. all mounts;
  279. force
  280. Force the huge option on for all - very useful for testing;
  281. Shmem can also use "multi-size THP" (mTHP) by adding a new sysfs knob to
  282. control mTHP allocation:
  283. '/sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/shmem_enabled',
  284. and its value for each mTHP is essentially consistent with the global
  285. setting. An 'inherit' option is added to ensure compatibility with these
  286. global settings. Conversely, the options 'force' and 'deny' are dropped,
  287. which are rather testing artifacts from the old ages.
  288. always
  289. Attempt to allocate <size> huge pages every time we need a new page;
  290. inherit
  291. Inherit the top-level "shmem_enabled" value. By default, PMD-sized hugepages
  292. have enabled="inherit" and all other hugepage sizes have enabled="never";
  293. never
  294. Do not allocate <size> huge pages;
  295. within_size
  296. Only allocate <size> huge page if it will be fully within i_size.
  297. Also respect fadvise()/madvise() hints;
  298. advise
  299. Only allocate <size> huge pages if requested with fadvise()/madvise();
  300. Need of application restart
  301. ===========================
  302. The transparent_hugepage/enabled and
  303. transparent_hugepage/hugepages-<size>kB/enabled values and tmpfs mount
  304. option only affect future behavior. So to make them effective you need
  305. to restart any application that could have been using hugepages. This
  306. also applies to the regions registered in khugepaged.
  307. Monitoring usage
  308. ================
  309. The number of PMD-sized anonymous transparent huge pages currently used by the
  310. system is available by reading the AnonHugePages field in ``/proc/meminfo``.
  311. To identify what applications are using PMD-sized anonymous transparent huge
  312. pages, it is necessary to read ``/proc/PID/smaps`` and count the AnonHugePages
  313. fields for each mapping. (Note that AnonHugePages only applies to traditional
  314. PMD-sized THP for historical reasons and should have been called
  315. AnonHugePmdMapped).
  316. The number of file transparent huge pages mapped to userspace is available
  317. by reading ShmemPmdMapped and ShmemHugePages fields in ``/proc/meminfo``.
  318. To identify what applications are mapping file transparent huge pages, it
  319. is necessary to read ``/proc/PID/smaps`` and count the FileHugeMapped fields
  320. for each mapping.
  321. Note that reading the smaps file is expensive and reading it
  322. frequently will incur overhead.
  323. There are a number of counters in ``/proc/vmstat`` that may be used to
  324. monitor how successfully the system is providing huge pages for use.
  325. thp_fault_alloc
  326. is incremented every time a huge page is successfully
  327. allocated and charged to handle a page fault.
  328. thp_collapse_alloc
  329. is incremented by khugepaged when it has found
  330. a range of pages to collapse into one huge page and has
  331. successfully allocated a new huge page to store the data.
  332. thp_fault_fallback
  333. is incremented if a page fault fails to allocate or charge
  334. a huge page and instead falls back to using small pages.
  335. thp_fault_fallback_charge
  336. is incremented if a page fault fails to charge a huge page and
  337. instead falls back to using small pages even though the
  338. allocation was successful.
  339. thp_collapse_alloc_failed
  340. is incremented if khugepaged found a range
  341. of pages that should be collapsed into one huge page but failed
  342. the allocation.
  343. thp_file_alloc
  344. is incremented every time a shmem huge page is successfully
  345. allocated (Note that despite being named after "file", the counter
  346. measures only shmem).
  347. thp_file_fallback
  348. is incremented if a shmem huge page is attempted to be allocated
  349. but fails and instead falls back to using small pages. (Note that
  350. despite being named after "file", the counter measures only shmem).
  351. thp_file_fallback_charge
  352. is incremented if a shmem huge page cannot be charged and instead
  353. falls back to using small pages even though the allocation was
  354. successful. (Note that despite being named after "file", the
  355. counter measures only shmem).
  356. thp_file_mapped
  357. is incremented every time a file or shmem huge page is mapped into
  358. user address space.
  359. thp_split_page
  360. is incremented every time a huge page is split into base
  361. pages. This can happen for a variety of reasons but a common
  362. reason is that a huge page is old and is being reclaimed.
  363. This action implies splitting all PMD the page mapped with.
  364. thp_split_page_failed
  365. is incremented if kernel fails to split huge
  366. page. This can happen if the page was pinned by somebody.
  367. thp_deferred_split_page
  368. is incremented when a huge page is put onto split
  369. queue. This happens when a huge page is partially unmapped and
  370. splitting it would free up some memory. Pages on split queue are
  371. going to be split under memory pressure.
  372. thp_underused_split_page
  373. is incremented when a huge page on the split queue was split
  374. because it was underused. A THP is underused if the number of
  375. zero pages in the THP is above a certain threshold
  376. (/sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none).
  377. thp_split_pmd
  378. is incremented every time a PMD split into table of PTEs.
  379. This can happen, for instance, when application calls mprotect() or
  380. munmap() on part of huge page. It doesn't split huge page, only
  381. page table entry.
  382. thp_zero_page_alloc
  383. is incremented every time a huge zero page used for thp is
  384. successfully allocated. Note, it doesn't count every map of
  385. the huge zero page, only its allocation.
  386. thp_zero_page_alloc_failed
  387. is incremented if kernel fails to allocate
  388. huge zero page and falls back to using small pages.
  389. thp_swpout
  390. is incremented every time a huge page is swapout in one
  391. piece without splitting.
  392. thp_swpout_fallback
  393. is incremented if a huge page has to be split before swapout.
  394. Usually because failed to allocate some continuous swap space
  395. for the huge page.
  396. In /sys/kernel/mm/transparent_hugepage/hugepages-<size>kB/stats, There are
  397. also individual counters for each huge page size, which can be utilized to
  398. monitor the system's effectiveness in providing huge pages for usage. Each
  399. counter has its own corresponding file.
  400. anon_fault_alloc
  401. is incremented every time a huge page is successfully
  402. allocated and charged to handle a page fault.
  403. anon_fault_fallback
  404. is incremented if a page fault fails to allocate or charge
  405. a huge page and instead falls back to using huge pages with
  406. lower orders or small pages.
  407. anon_fault_fallback_charge
  408. is incremented if a page fault fails to charge a huge page and
  409. instead falls back to using huge pages with lower orders or
  410. small pages even though the allocation was successful.
  411. swpout
  412. is incremented every time a huge page is swapped out in one
  413. piece without splitting.
  414. swpout_fallback
  415. is incremented if a huge page has to be split before swapout.
  416. Usually because failed to allocate some continuous swap space
  417. for the huge page.
  418. shmem_alloc
  419. is incremented every time a shmem huge page is successfully
  420. allocated.
  421. shmem_fallback
  422. is incremented if a shmem huge page is attempted to be allocated
  423. but fails and instead falls back to using small pages.
  424. shmem_fallback_charge
  425. is incremented if a shmem huge page cannot be charged and instead
  426. falls back to using small pages even though the allocation was
  427. successful.
  428. split
  429. is incremented every time a huge page is successfully split into
  430. smaller orders. This can happen for a variety of reasons but a
  431. common reason is that a huge page is old and is being reclaimed.
  432. split_failed
  433. is incremented if kernel fails to split huge
  434. page. This can happen if the page was pinned by somebody.
  435. split_deferred
  436. is incremented when a huge page is put onto split queue.
  437. This happens when a huge page is partially unmapped and splitting
  438. it would free up some memory. Pages on split queue are going to
  439. be split under memory pressure, if splitting is possible.
  440. nr_anon
  441. the number of anonymous THP we have in the whole system. These THPs
  442. might be currently entirely mapped or have partially unmapped/unused
  443. subpages.
  444. nr_anon_partially_mapped
  445. the number of anonymous THP which are likely partially mapped, possibly
  446. wasting memory, and have been queued for deferred memory reclamation.
  447. Note that in corner some cases (e.g., failed migration), we might detect
  448. an anonymous THP as "partially mapped" and count it here, even though it
  449. is not actually partially mapped anymore.
  450. As the system ages, allocating huge pages may be expensive as the
  451. system uses memory compaction to copy data around memory to free a
  452. huge page for use. There are some counters in ``/proc/vmstat`` to help
  453. monitor this overhead.
  454. compact_stall
  455. is incremented every time a process stalls to run
  456. memory compaction so that a huge page is free for use.
  457. compact_success
  458. is incremented if the system compacted memory and
  459. freed a huge page for use.
  460. compact_fail
  461. is incremented if the system tries to compact memory
  462. but failed.
  463. It is possible to establish how long the stalls were using the function
  464. tracer to record how long was spent in __alloc_pages() and
  465. using the mm_page_alloc tracepoint to identify which allocations were
  466. for huge pages.
  467. Optimizing the applications
  468. ===========================
  469. To be guaranteed that the kernel will map a THP immediately in any
  470. memory region, the mmap region has to be hugepage naturally
  471. aligned. posix_memalign() can provide that guarantee.
  472. Hugetlbfs
  473. =========
  474. You can use hugetlbfs on a kernel that has transparent hugepage
  475. support enabled just fine as always. No difference can be noted in
  476. hugetlbfs other than there will be less overall fragmentation. All
  477. usual features belonging to hugetlbfs are preserved and
  478. unaffected. libhugetlbfs will also work fine as usual.