Back to home page

OSCL-LXR

 
 

    


0001 .. _admin_guide_transhuge:
0002 
0003 ============================
0004 Transparent Hugepage Support
0005 ============================
0006 
0007 Objective
0008 =========
0009 
0010 Performance critical computing applications dealing with large memory
0011 working sets are already running on top of libhugetlbfs and in turn
0012 hugetlbfs. Transparent HugePage Support (THP) is an alternative mean of
0013 using huge pages for the backing of virtual memory with huge pages
0014 that supports the automatic promotion and demotion of page sizes and
0015 without the shortcomings of hugetlbfs.
0016 
0017 Currently THP only works for anonymous memory mappings and tmpfs/shmem.
0018 But in the future it can expand to other filesystems.
0019 
0020 .. note::
0021    in the examples below we presume that the basic page size is 4K and
0022    the huge page size is 2M, although the actual numbers may vary
0023    depending on the CPU architecture.
0024 
0025 The reason applications are running faster is because of two
0026 factors. The first factor is almost completely irrelevant and it's not
0027 of significant interest because it'll also have the downside of
0028 requiring larger clear-page copy-page in page faults which is a
0029 potentially negative effect. The first factor consists in taking a
0030 single page fault for each 2M virtual region touched by userland (so
0031 reducing the enter/exit kernel frequency by a 512 times factor). This
0032 only matters the first time the memory is accessed for the lifetime of
0033 a memory mapping. The second long lasting and much more important
0034 factor will affect all subsequent accesses to the memory for the whole
0035 runtime of the application. The second factor consist of two
0036 components:
0037 
0038 1) the TLB miss will run faster (especially with virtualization using
0039    nested pagetables but almost always also on bare metal without
0040    virtualization)
0041 
0042 2) a single TLB entry will be mapping a much larger amount of virtual
0043    memory in turn reducing the number of TLB misses. With
0044    virtualization and nested pagetables the TLB can be mapped of
0045    larger size only if both KVM and the Linux guest are using
0046    hugepages but a significant speedup already happens if only one of
0047    the two is using hugepages just because of the fact the TLB miss is
0048    going to run faster.
0049 
0050 THP can be enabled system wide or restricted to certain tasks or even
0051 memory ranges inside task's address space. Unless THP is completely
0052 disabled, there is ``khugepaged`` daemon that scans memory and
0053 collapses sequences of basic pages into huge pages.
0054 
0055 The THP behaviour is controlled via :ref:`sysfs <thp_sysfs>`
0056 interface and using madvise(2) and prctl(2) system calls.
0057 
0058 Transparent Hugepage Support maximizes the usefulness of free memory
0059 if compared to the reservation approach of hugetlbfs by allowing all
0060 unused memory to be used as cache or other movable (or even unmovable
0061 entities). It doesn't require reservation to prevent hugepage
0062 allocation failures to be noticeable from userland. It allows paging
0063 and all other advanced VM features to be available on the
0064 hugepages. It requires no modifications for applications to take
0065 advantage of it.
0066 
0067 Applications however can be further optimized to take advantage of
0068 this feature, like for example they've been optimized before to avoid
0069 a flood of mmap system calls for every malloc(4k). Optimizing userland
0070 is by far not mandatory and khugepaged already can take care of long
0071 lived page allocations even for hugepage unaware applications that
0072 deals with large amounts of memory.
0073 
0074 In certain cases when hugepages are enabled system wide, application
0075 may end up allocating more memory resources. An application may mmap a
0076 large region but only touch 1 byte of it, in that case a 2M page might
0077 be allocated instead of a 4k page for no good. This is why it's
0078 possible to disable hugepages system-wide and to only have them inside
0079 MADV_HUGEPAGE madvise regions.
0080 
0081 Embedded systems should enable hugepages only inside madvise regions
0082 to eliminate any risk of wasting any precious byte of memory and to
0083 only run faster.
0084 
0085 Applications that gets a lot of benefit from hugepages and that don't
0086 risk to lose memory by using hugepages, should use
0087 madvise(MADV_HUGEPAGE) on their critical mmapped regions.
0088 
0089 .. _thp_sysfs:
0090 
0091 sysfs
0092 =====
0093 
0094 Global THP controls
0095 -------------------
0096 
0097 Transparent Hugepage Support for anonymous memory can be entirely disabled
0098 (mostly for debugging purposes) or only enabled inside MADV_HUGEPAGE
0099 regions (to avoid the risk of consuming more memory resources) or enabled
0100 system wide. This can be achieved with one of::
0101 
0102         echo always >/sys/kernel/mm/transparent_hugepage/enabled
0103         echo madvise >/sys/kernel/mm/transparent_hugepage/enabled
0104         echo never >/sys/kernel/mm/transparent_hugepage/enabled
0105 
0106 It's also possible to limit defrag efforts in the VM to generate
0107 anonymous hugepages in case they're not immediately free to madvise
0108 regions or to never try to defrag memory and simply fallback to regular
0109 pages unless hugepages are immediately available. Clearly if we spend CPU
0110 time to defrag memory, we would expect to gain even more by the fact we
0111 use hugepages later instead of regular pages. This isn't always
0112 guaranteed, but it may be more likely in case the allocation is for a
0113 MADV_HUGEPAGE region.
0114 
0115 ::
0116 
0117         echo always >/sys/kernel/mm/transparent_hugepage/defrag
0118         echo defer >/sys/kernel/mm/transparent_hugepage/defrag
0119         echo defer+madvise >/sys/kernel/mm/transparent_hugepage/defrag
0120         echo madvise >/sys/kernel/mm/transparent_hugepage/defrag
0121         echo never >/sys/kernel/mm/transparent_hugepage/defrag
0122 
0123 always
0124         means that an application requesting THP will stall on
0125         allocation failure and directly reclaim pages and compact
0126         memory in an effort to allocate a THP immediately. This may be
0127         desirable for virtual machines that benefit heavily from THP
0128         use and are willing to delay the VM start to utilise them.
0129 
0130 defer
0131         means that an application will wake kswapd in the background
0132         to reclaim pages and wake kcompactd to compact memory so that
0133         THP is available in the near future. It's the responsibility
0134         of khugepaged to then install the THP pages later.
0135 
0136 defer+madvise
0137         will enter direct reclaim and compaction like ``always``, but
0138         only for regions that have used madvise(MADV_HUGEPAGE); all
0139         other regions will wake kswapd in the background to reclaim
0140         pages and wake kcompactd to compact memory so that THP is
0141         available in the near future.
0142 
0143 madvise
0144         will enter direct reclaim like ``always`` but only for regions
0145         that are have used madvise(MADV_HUGEPAGE). This is the default
0146         behaviour.
0147 
0148 never
0149         should be self-explanatory.
0150 
0151 By default kernel tries to use huge zero page on read page fault to
0152 anonymous mapping. It's possible to disable huge zero page by writing 0
0153 or enable it back by writing 1::
0154 
0155         echo 0 >/sys/kernel/mm/transparent_hugepage/use_zero_page
0156         echo 1 >/sys/kernel/mm/transparent_hugepage/use_zero_page
0157 
0158 Some userspace (such as a test program, or an optimized memory allocation
0159 library) may want to know the size (in bytes) of a transparent hugepage::
0160 
0161         cat /sys/kernel/mm/transparent_hugepage/hpage_pmd_size
0162 
0163 khugepaged will be automatically started when
0164 transparent_hugepage/enabled is set to "always" or "madvise, and it'll
0165 be automatically shutdown if it's set to "never".
0166 
0167 Khugepaged controls
0168 -------------------
0169 
0170 khugepaged runs usually at low frequency so while one may not want to
0171 invoke defrag algorithms synchronously during the page faults, it
0172 should be worth invoking defrag at least in khugepaged. However it's
0173 also possible to disable defrag in khugepaged by writing 0 or enable
0174 defrag in khugepaged by writing 1::
0175 
0176         echo 0 >/sys/kernel/mm/transparent_hugepage/khugepaged/defrag
0177         echo 1 >/sys/kernel/mm/transparent_hugepage/khugepaged/defrag
0178 
0179 You can also control how many pages khugepaged should scan at each
0180 pass::
0181 
0182         /sys/kernel/mm/transparent_hugepage/khugepaged/pages_to_scan
0183 
0184 and how many milliseconds to wait in khugepaged between each pass (you
0185 can set this to 0 to run khugepaged at 100% utilization of one core)::
0186 
0187         /sys/kernel/mm/transparent_hugepage/khugepaged/scan_sleep_millisecs
0188 
0189 and how many milliseconds to wait in khugepaged if there's an hugepage
0190 allocation failure to throttle the next allocation attempt::
0191 
0192         /sys/kernel/mm/transparent_hugepage/khugepaged/alloc_sleep_millisecs
0193 
0194 The khugepaged progress can be seen in the number of pages collapsed::
0195 
0196         /sys/kernel/mm/transparent_hugepage/khugepaged/pages_collapsed
0197 
0198 for each pass::
0199 
0200         /sys/kernel/mm/transparent_hugepage/khugepaged/full_scans
0201 
0202 ``max_ptes_none`` specifies how many extra small pages (that are
0203 not already mapped) can be allocated when collapsing a group
0204 of small pages into one large page::
0205 
0206         /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none
0207 
0208 A higher value leads to use additional memory for programs.
0209 A lower value leads to gain less thp performance. Value of
0210 max_ptes_none can waste cpu time very little, you can
0211 ignore it.
0212 
0213 ``max_ptes_swap`` specifies how many pages can be brought in from
0214 swap when collapsing a group of pages into a transparent huge page::
0215 
0216         /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_swap
0217 
0218 A higher value can cause excessive swap IO and waste
0219 memory. A lower value can prevent THPs from being
0220 collapsed, resulting fewer pages being collapsed into
0221 THPs, and lower memory access performance.
0222 
0223 ``max_ptes_shared`` specifies how many pages can be shared across multiple
0224 processes. Exceeding the number would block the collapse::
0225 
0226         /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_shared
0227 
0228 A higher value may increase memory footprint for some workloads.
0229 
0230 Boot parameter
0231 ==============
0232 
0233 You can change the sysfs boot time defaults of Transparent Hugepage
0234 Support by passing the parameter ``transparent_hugepage=always`` or
0235 ``transparent_hugepage=madvise`` or ``transparent_hugepage=never``
0236 to the kernel command line.
0237 
0238 Hugepages in tmpfs/shmem
0239 ========================
0240 
0241 You can control hugepage allocation policy in tmpfs with mount option
0242 ``huge=``. It can have following values:
0243 
0244 always
0245     Attempt to allocate huge pages every time we need a new page;
0246 
0247 never
0248     Do not allocate huge pages;
0249 
0250 within_size
0251     Only allocate huge page if it will be fully within i_size.
0252     Also respect fadvise()/madvise() hints;
0253 
0254 advise
0255     Only allocate huge pages if requested with fadvise()/madvise();
0256 
0257 The default policy is ``never``.
0258 
0259 ``mount -o remount,huge= /mountpoint`` works fine after mount: remounting
0260 ``huge=never`` will not attempt to break up huge pages at all, just stop more
0261 from being allocated.
0262 
0263 There's also sysfs knob to control hugepage allocation policy for internal
0264 shmem mount: /sys/kernel/mm/transparent_hugepage/shmem_enabled. The mount
0265 is used for SysV SHM, memfds, shared anonymous mmaps (of /dev/zero or
0266 MAP_ANONYMOUS), GPU drivers' DRM objects, Ashmem.
0267 
0268 In addition to policies listed above, shmem_enabled allows two further
0269 values:
0270 
0271 deny
0272     For use in emergencies, to force the huge option off from
0273     all mounts;
0274 force
0275     Force the huge option on for all - very useful for testing;
0276 
0277 Need of application restart
0278 ===========================
0279 
0280 The transparent_hugepage/enabled values and tmpfs mount option only affect
0281 future behavior. So to make them effective you need to restart any
0282 application that could have been using hugepages. This also applies to the
0283 regions registered in khugepaged.
0284 
0285 Monitoring usage
0286 ================
0287 
0288 The number of anonymous transparent huge pages currently used by the
0289 system is available by reading the AnonHugePages field in ``/proc/meminfo``.
0290 To identify what applications are using anonymous transparent huge pages,
0291 it is necessary to read ``/proc/PID/smaps`` and count the AnonHugePages fields
0292 for each mapping.
0293 
0294 The number of file transparent huge pages mapped to userspace is available
0295 by reading ShmemPmdMapped and ShmemHugePages fields in ``/proc/meminfo``.
0296 To identify what applications are mapping file transparent huge pages, it
0297 is necessary to read ``/proc/PID/smaps`` and count the FileHugeMapped fields
0298 for each mapping.
0299 
0300 Note that reading the smaps file is expensive and reading it
0301 frequently will incur overhead.
0302 
0303 There are a number of counters in ``/proc/vmstat`` that may be used to
0304 monitor how successfully the system is providing huge pages for use.
0305 
0306 thp_fault_alloc
0307         is incremented every time a huge page is successfully
0308         allocated to handle a page fault.
0309 
0310 thp_collapse_alloc
0311         is incremented by khugepaged when it has found
0312         a range of pages to collapse into one huge page and has
0313         successfully allocated a new huge page to store the data.
0314 
0315 thp_fault_fallback
0316         is incremented if a page fault fails to allocate
0317         a huge page and instead falls back to using small pages.
0318 
0319 thp_fault_fallback_charge
0320         is incremented if a page fault fails to charge a huge page and
0321         instead falls back to using small pages even though the
0322         allocation was successful.
0323 
0324 thp_collapse_alloc_failed
0325         is incremented if khugepaged found a range
0326         of pages that should be collapsed into one huge page but failed
0327         the allocation.
0328 
0329 thp_file_alloc
0330         is incremented every time a file huge page is successfully
0331         allocated.
0332 
0333 thp_file_fallback
0334         is incremented if a file huge page is attempted to be allocated
0335         but fails and instead falls back to using small pages.
0336 
0337 thp_file_fallback_charge
0338         is incremented if a file huge page cannot be charged and instead
0339         falls back to using small pages even though the allocation was
0340         successful.
0341 
0342 thp_file_mapped
0343         is incremented every time a file huge page is mapped into
0344         user address space.
0345 
0346 thp_split_page
0347         is incremented every time a huge page is split into base
0348         pages. This can happen for a variety of reasons but a common
0349         reason is that a huge page is old and is being reclaimed.
0350         This action implies splitting all PMD the page mapped with.
0351 
0352 thp_split_page_failed
0353         is incremented if kernel fails to split huge
0354         page. This can happen if the page was pinned by somebody.
0355 
0356 thp_deferred_split_page
0357         is incremented when a huge page is put onto split
0358         queue. This happens when a huge page is partially unmapped and
0359         splitting it would free up some memory. Pages on split queue are
0360         going to be split under memory pressure.
0361 
0362 thp_split_pmd
0363         is incremented every time a PMD split into table of PTEs.
0364         This can happen, for instance, when application calls mprotect() or
0365         munmap() on part of huge page. It doesn't split huge page, only
0366         page table entry.
0367 
0368 thp_zero_page_alloc
0369         is incremented every time a huge zero page is
0370         successfully allocated. It includes allocations which where
0371         dropped due race with other allocation. Note, it doesn't count
0372         every map of the huge zero page, only its allocation.
0373 
0374 thp_zero_page_alloc_failed
0375         is incremented if kernel fails to allocate
0376         huge zero page and falls back to using small pages.
0377 
0378 thp_swpout
0379         is incremented every time a huge page is swapout in one
0380         piece without splitting.
0381 
0382 thp_swpout_fallback
0383         is incremented if a huge page has to be split before swapout.
0384         Usually because failed to allocate some continuous swap space
0385         for the huge page.
0386 
0387 As the system ages, allocating huge pages may be expensive as the
0388 system uses memory compaction to copy data around memory to free a
0389 huge page for use. There are some counters in ``/proc/vmstat`` to help
0390 monitor this overhead.
0391 
0392 compact_stall
0393         is incremented every time a process stalls to run
0394         memory compaction so that a huge page is free for use.
0395 
0396 compact_success
0397         is incremented if the system compacted memory and
0398         freed a huge page for use.
0399 
0400 compact_fail
0401         is incremented if the system tries to compact memory
0402         but failed.
0403 
0404 It is possible to establish how long the stalls were using the function
0405 tracer to record how long was spent in __alloc_pages() and
0406 using the mm_page_alloc tracepoint to identify which allocations were
0407 for huge pages.
0408 
0409 Optimizing the applications
0410 ===========================
0411 
0412 To be guaranteed that the kernel will map a 2M page immediately in any
0413 memory region, the mmap region has to be hugepage naturally
0414 aligned. posix_memalign() can provide that guarantee.
0415 
0416 Hugetlbfs
0417 =========
0418 
0419 You can use hugetlbfs on a kernel that has transparent hugepage
0420 support enabled just fine as always. No difference can be noted in
0421 hugetlbfs other than there will be less overall fragmentation. All
0422 usual features belonging to hugetlbfs are preserved and
0423 unaffected. libhugetlbfs will also work fine as usual.