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