HugeTLB Pages (翻译by chatgpt)

发布时间 2023-12-01 11:02:20作者: 摩斯电码

原文:https://www.kernel.org/doc/html/latest/admin-guide/mm/hugetlbpage.html

内核中内存管理文档汇总:Memory Management

Overview

The intent of this file is to give a brief summary of hugetlbpage support in the Linux kernel. This support is built on top of multiple page size support that is provided by most modern architectures. For example, x86 CPUs normally support 4K and 2M (1G if architecturally supported) page sizes, ia64 architecture supports multiple page sizes 4K, 8K, 64K, 256K, 1M, 4M, 16M, 256M and ppc64 supports 4K and 16M. A TLB is a cache of virtual-to-physical translations. Typically this is a very scarce resource on processor. Operating systems try to make best use of limited number of TLB resources. This optimization is more critical now as bigger and bigger physical memories (several GBs) are more readily available.
这个文件的目的是对 Linux 内核中的 hugetlbpage 支持进行简要总结。这种支持是建立在大多数现代架构提供的多页面大小支持之上的。例如,x86 CPU 通常支持 4K 和 2M(如果在架构上支持,则为 1G)的页面大小,ia64 架构支持多种页面大小,包括 4K、8K、64K、256K、1M、4M、16M、256M,而 ppc64 则支持 4K 和 16M。TLB 是虚拟到物理地址转换的缓存。处理器上通常这是一个非常稀缺的资源。操作系统试图充分利用有限数量的 TLB 资源。随着越来越大的物理内存(数 GB)更容易获得,这种优化现在变得更加关键。

Users can use the huge page support in Linux kernel by either using the mmap system call or standard SYSV shared memory system calls (shmget, shmat).
用户可以通过使用 mmap 系统调用或标准的 SYSV 共享内存系统调用(shmget、shmat)在 Linux 内核中使用大页支持。

First the Linux kernel needs to be built with the CONFIG_HUGETLBFS (present under "File systems") and CONFIG_HUGETLB_PAGE (selected automatically when CONFIG_HUGETLBFS is selected) configuration options.
首先,Linux 内核需要使用 CONFIG_HUGETLBFS(位于“文件系统”下)和 CONFIG_HUGETLB_PAGE(在选择 CONFIG_HUGETLBFS 时会自动选择)配置选项进行构建。

The /proc/meminfo file provides information about the total number of persistent hugetlb pages in the kernel's huge page pool. It also displays default huge page size and information about the number of free, reserved and surplus huge pages in the pool of huge pages of default size. The huge page size is needed for generating the proper alignment and size of the arguments to system calls that map huge page regions.
/proc/meminfo 文件提供了有关内核巨大页面池中持久 hugetlb 页面总数的信息。它还显示了默认的巨大页面大小以及有关默认大小的巨大页面池中的空闲、保留和多余巨大页面数量的信息。巨大页面大小对于生成映射巨大页面区域的系统调用参数的正确对齐和大小是必要的。

The output of cat /proc/meminfo will include lines like:
cat /proc/meminfo的输出将包括以下行:

HugePages_Total: uuu
HugePages_Free:  vvv
HugePages_Rsvd:  www
HugePages_Surp:  xxx
Hugepagesize:    yyy kB
Hugetlb:         zzz kB

where:

HugePages_Total
is the size of the pool of huge pages.
是巨大页面池的大小。

HugePages_Free
is the number of huge pages in the pool that are not yet allocated.
是巨大页面池的大小。

HugePages_Rsvd
is short for "reserved," and is the number of huge pages for which a commitment to allocate from the pool has been made, but no allocation has yet been made. Reserved huge pages guarantee that an application will be able to allocate a huge page from the pool of huge pages at fault time.
是"reserved"的缩写,表示已经做出了从池中分配巨大页面的承诺,但尚未进行分配的巨大页面数。预留的巨大页面保证应用程序能够在发生错误时从巨大页面池中分配一个巨大页面。

HugePages_Surp
is short for "surplus," and is the number of huge pages in the pool above the value in /proc/sys/vm/nr_hugepages. The maximum number of surplus huge pages is controlled by /proc/sys/vm/nr_overcommit_hugepages. Note: When the feature of freeing unused vmemmap pages associated with each hugetlb page is enabled, the number of surplus huge pages may be temporarily larger than the maximum number of surplus huge pages when the system is under memory pressure.
是"surplus"的缩写,表示池中超出/proc/sys/vm/nr_hugepages值的巨大页面数。多余巨大页面的最大数量由/proc/sys/vm/nr_overcommit_hugepages控制。注意:当启用了释放与每个hugetlb页面关联的未使用vmemmap页面的功能时,系统在内存压力下可能会暂时拥有比最大多余巨大页面数量更多的多余巨大页面。

Hugepagesize
is the default hugepage size (in kB).
是默认的巨大页面大小(以kB为单位)。

Hugetlb
is the total amount of memory (in kB), consumed by huge pages of all sizes. If huge pages of different sizes are in use, this number will exceed HugePages_Total * Hugepagesize. To get more detailed information, please, refer to /sys/kernel/mm/hugepages (described below).
是所有大小的巨大页面消耗的总内存量(以kB为单位)。如果使用不同大小的巨大页面,这个数字将超过HugePages_Total * Hugepagesize。要获取更详细的信息,请参考/sys/kernel/mm/hugepages(下文有描述)。

/proc/filesystems should also show a filesystem of type "hugetlbfs" configured in the kernel.
/proc/filesystems还应显示内核中配置的类型为"hugetlbfs"的文件系统。

/proc/sys/vm/nr_hugepages indicates the current number of "persistent" huge pages in the kernel's huge page pool. "Persistent" huge pages will be returned to the huge page pool when freed by a task. A user with root privileges can dynamically allocate more or free some persistent huge pages by increasing or decreasing the value of nr_hugepages.
/proc/sys/vm/nr_hugepages指示内核巨大页面池中当前的"持久"巨大页面数。"持久"巨大页面将在被任务释放时返回到巨大页面池中。具有root权限的用户可以通过增加或减少nr_hugepages的值来动态分配更多或释放一些持久巨大页面。

Note: When the feature of freeing unused vmemmap pages associated with each hugetlb page is enabled, we can fail to free the huge pages triggered by the user when the system is under memory pressure. Please try again later.
注意:当启用了释放与每个hugetlb页面关联的未使用vmemmap页面的功能时,在内存压力下,我们可能无法释放用户触发的巨大页面。请稍后重试。

Pages that are used as huge pages are reserved inside the kernel and cannot be used for other purposes. Huge pages cannot be swapped out under memory pressure.
作为巨大页面使用的页面在内核中被保留,不能用于其他目的。在内存压力下,巨大页面不能被交换出去。

Once a number of huge pages have been pre-allocated to the kernel huge page pool, a user with appropriate privilege can use either the mmap system call or shared memory system calls to use the huge pages. See the discussion of Using Huge Pages, below.
一旦一定数量的巨大页面被预先分配到内核的巨大页面池中,具有适当特权的用户可以使用mmap系统调用或共享内存系统调用来使用巨大页面。请参阅下文的使用巨大页面的讨论。

The administrator can allocate persistent huge pages on the kernel boot command line by specifying the "hugepages=N" parameter, where 'N' = the number of huge pages requested. This is the most reliable method of allocating huge pages as memory has not yet become fragmented.
管理员可以通过在内核引导命令行上指定"hugepages=N"参数来分配持久巨大页面,其中'N'表示请求的巨大页面数。这是分配巨大页面最可靠的方法,因为内存尚未变得碎片化。

Some platforms support multiple huge page sizes. To allocate huge pages of a specific size, one must precede the huge pages boot command parameters with a huge page size selection parameter "hugepagesz=". must be specified in bytes with optional scale suffix [kKmMgG]. The default huge page size may be selected with the "default_hugepagesz=" boot parameter.
一些平台支持多个巨大页面大小。要分配特定大小的巨大页面,必须在巨大页面引导命令参数之前加上巨大页面大小选择参数"hugepagesz="。 必须以字节为单位,并带有可选的比例后缀[kKmMgG]。默认的巨大页面大小可以通过"default_hugepagesz="引导参数选择。

Hugetlb boot command line parameter semantics

hugepagesz
Specify a huge page size. Used in conjunction with hugepages parameter to preallocate a number of huge pages of the specified size. Hence, hugepagesz and hugepages are typically specified in pairs such as:
指定巨大页面的大小。与hugepages参数一起使用,预先分配指定大小的巨大页面数量。因此,hugepagesz和hugepages通常成对指定,例如:

hugepagesz=2M hugepages=512
hugepagesz can only be specified once on the command line for a specific huge page size. Valid huge page sizes are architecture dependent.
指定巨大页面的大小。与hugepages参数一起使用,预先分配指定大小的巨大页面数量。因此,hugepagesz和hugepages通常成对指定,例如:

hugepages
Specify the number of huge pages to preallocate. This typically follows a valid hugepagesz or default_hugepagesz parameter. However, if hugepages is the first or only hugetlb command line parameter it implicitly specifies the number of huge pages of default size to allocate. If the number of huge pages of default size is implicitly specified, it can not be overwritten by a hugepagesz,hugepages parameter pair for the default size. This parameter also has a node format. The node format specifies the number of huge pages to allocate on specific nodes.
指定要预先分配的巨大页面数量。这通常跟随有效的hugepagesz或default_hugepagesz参数。但是,如果hugepages是第一个或唯一的hugetlb命令行参数,它隐式地指定要分配的默认大小的巨大页面数量。如果默认大小的巨大页面数量被隐式指定,它就不能被hugepagesz、hugepages参数对覆盖。此参数还有一个节点格式。节点格式指定要在特定节点上分配的巨大页面数量。

For example, on an architecture with 2M default huge page size:
例如,在具有2M默认巨大页面大小的架构上:

hugepages=256 hugepagesz=2M hugepages=512
will result in 256 2M huge pages being allocated and a warning message indicating that the hugepages=512 parameter is ignored. If a hugepages parameter is preceded by an invalid hugepagesz parameter, it will be ignored.
将导致分配256个2M巨大页面,并显示一个警告消息,指示忽略了hugepages=512参数。如果一个hugepages参数前面有一个无效的hugepagesz参数,它将被忽略。

Node format example:
节点格式示例:

hugepagesz=2M hugepages=0:1,1:2
It will allocate 1 2M hugepage on node0 and 2 2M hugepages on node1. If the node number is invalid, the parameter will be ignored.
它将在节点0上分配1个2M巨大页面,并在节点1上分配2个2M巨大页面。如果节点号无效,该参数将被忽略。

default_hugepagesz
Specify the default huge page size. This parameter can only be specified once on the command line. default_hugepagesz can optionally be followed by the hugepages parameter to preallocate a specific number of huge pages of default size. The number of default sized huge pages to preallocate can also be implicitly specified as mentioned in the hugepages section above. Therefore, on an architecture with 2M default huge page size:
指定默认的巨大页面大小。此参数只能在命令行上指定一次。default_hugepagesz可以选择后跟hugepages参数,以预先分配默认大小的巨大页面数量。要预先分配的默认大小的巨大页面数量也可以像上面的hugepages部分提到的那样隐式指定。因此,在具有2M默认巨大页面大小的架构上:

hugepages=256
default_hugepagesz=2M hugepages=256
hugepages=256 default_hugepagesz=2M

will all result in 256 2M huge pages being allocated. Valid default huge page size is architecture dependent.
都将导致分配256个2M巨大页面。有效的默认巨大页面大小取决于架构。

hugetlb_free_vmemmap
When CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP is set, this enables HugeTLB Vmemmap Optimization (HVO).
当设置了CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP时,这将启用HugeTLB Vmemmap优化(HVO)。

When multiple huge page sizes are supported, /proc/sys/vm/nr_hugepages indicates the current number of pre-allocated huge pages of the default size. Thus, one can use the following command to dynamically allocate/deallocate default sized persistent huge pages:
当支持多个巨大页面大小时,/proc/sys/vm/nr_hugepages指示默认大小的预先分配巨大页面的当前数量。因此,可以使用以下命令动态分配/释放默认大小的持久巨大页面:

echo 20 > /proc/sys/vm/nr_hugepages
This command will try to adjust the number of default sized huge pages in the huge page pool to 20, allocating or freeing huge pages, as required.
此命令将尝试调整巨大页面池中默认大小的巨大页面数量为20,根据需要分配或释放巨大页面。

On a NUMA platform, the kernel will attempt to distribute the huge page pool over all the set of allowed nodes specified by the NUMA memory policy of the task that modifies nr_hugepages. The default for the allowed nodes--when the task has default memory policy--is all on-line nodes with memory. Allowed nodes with insufficient available, contiguous memory for a huge page will be silently skipped when allocating persistent huge pages. See the discussion below of the interaction of task memory policy, cpusets and per node attributes with the allocation and freeing of persistent huge pages.
在NUMA平台上,内核将尝试在由修改nr_hugepages的任务的NUMA内存策略指定的允许节点集上分发巨大页面池。当分配持久巨大页面时,当允许节点的可用连续内存不足时,将静默地跳过。请参阅下面关于任务内存策略、cpusets和每个节点属性与分配和释放持久巨大页面的交互的讨论。

The success or failure of huge page allocation depends on the amount of physically contiguous memory that is present in system at the time of the allocation attempt. If the kernel is unable to allocate huge pages from some nodes in a NUMA system, it will attempt to make up the difference by allocating extra pages on other nodes with sufficient available contiguous memory, if any.
巨大页面分配的成功或失败取决于系统中存在的物理连续内存的数量。如果内核无法从NUMA系统中的某些节点分配巨大页面,它将尝试通过在其他具有足够可用连续内存的节点上分配额外页面来弥补差距。

System administrators may want to put this command in one of the local rc init files. This will enable the kernel to allocate huge pages early in the boot process when the possibility of getting physical contiguous pages is still very high. Administrators can verify the number of huge pages actually allocated by checking the sysctl or meminfo. To check the per node distribution of huge pages in a NUMA system, use:
系统管理员可能希望将此命令放在本地rc初始化文件之一中。这将使内核能够在引导过程的早期分配巨大页面,当获取物理连续页面的可能性仍然非常高时。管理员可以通过检查sysctl或meminfo来验证实际分配的巨大页面数量。要检查NUMA系统中巨大页面的每个节点分布,使用:

cat /sys/devices/system/node/node*/meminfo | fgrep Huge

/proc/sys/vm/nr_overcommit_hugepages specifies how large the pool of huge pages can grow, if more huge pages than /proc/sys/vm/nr_hugepages are requested by applications. Writing any non-zero value into this file indicates that the hugetlb subsystem is allowed to try to obtain that number of "surplus" huge pages from the kernel's normal page pool, when the persistent huge page pool is exhausted. As these surplus huge pages become unused, they are freed back to the kernel's normal page pool.
/proc/sys/vm/nr_overcommit_hugepages指定巨大页面池可以增长的大小,如果应用程序请求的巨大页面数量超过了/proc/sys/vm/nr_hugepages。将任何非零值写入此文件表示hugetlb子系统被允许尝试从内核的正常页面池中获取该数量的“多余”巨大页面,当持久巨大页面池耗尽时。当这些多余的巨大页面变得未使用时,它们将被释放回内核的正常页面池。

When increasing the huge page pool size via nr_hugepages, any existing surplus pages will first be promoted to persistent huge pages. Then, additional huge pages will be allocated, if necessary and if possible, to fulfill the new persistent huge page pool size.
通过增加nr_hugepages来增加巨大页面池的大小时,任何现有的多余页面将首先提升为持久巨大页面。然后,如果必要且可能,将分配额外的巨大页面,以满足新的持久巨大页面池大小。

The administrator may shrink the pool of persistent huge pages for the default huge page size by setting the nr_hugepages sysctl to a smaller value. The kernel will attempt to balance the freeing of huge pages across all nodes in the memory policy of the task modifying nr_hugepages. Any free huge pages on the selected nodes will be freed back to the kernel's normal page pool.
管理员可以通过将nr_hugepages sysctl设置为较小的值来缩小默认巨大页面大小的持久巨大页面池。内核将尝试在修改nr_hugepages的任务的内存策略的所有节点上平衡释放巨大页面。所选节点上的任何空闲巨大页面将被释放回内核的正常页面池。

Caveat: Shrinking the persistent huge page pool via nr_hugepages such that it becomes less than the number of huge pages in use will convert the balance of the in-use huge pages to surplus huge pages. This will occur even if the number of surplus pages would exceed the overcommit value. As long as this condition holds--that is, until nr_hugepages+nr_overcommit_hugepages is increased sufficiently, or the surplus huge pages go out of use and are freed-- no more surplus huge pages will be allowed to be allocated.
注意:通过将nr_hugepages缩小到小于正在使用的巨大页面数量的持久巨大页面池,将会将正在使用的巨大页面的余额转换为多余的巨大页面。即使多余页面的数量超过了超额值,这也会发生。只要这种情况持续存在--也就是说,直到nr_hugepages+nr_overcommit_hugepages足够增加,或者多余的巨大页面不再使用并被释放--将不允许分配更多的多余巨大页面。

With support for multiple huge page pools at run-time available, much of the huge page userspace interface in /proc/sys/vm has been duplicated in sysfs. The /proc interfaces discussed above have been retained for backwards compatibility. The root huge page control directory in sysfs is:
在运行时支持多个巨大页面池时,/proc/sys/vm中的巨大页面用户空间接口的大部分已在sysfs中复制。上面讨论的/proc接口已保留以确保向后兼容性。sysfs中的根巨大页面控制目录是:

/sys/kernel/mm/hugepages
For each huge page size supported by the running kernel, a subdirectory will exist, of the form:
对于运行中的内核支持的每个巨大页面大小,将存在一个子目录,格式如下:

  • hugepages-${size}kB
    Inside each of these directories, the set of files contained in /proc will exist. In addition, two additional interfaces for demoting huge pages may exist:
    在每个这样的目录中,/proc 中包含的一组文件将存在。此外,可能存在两个用于降低大页的额外接口:
    demote
    demote_size
    nr_hugepages
    nr_hugepages_mempolicy
    nr_overcommit_hugepages
    free_hugepages
    resv_hugepages
    surplus_hugepages
    

The demote interfaces provide the ability to split a huge page into smaller huge pages. For example, the x86 architecture supports both 1GB and 2MB huge pages sizes. A 1GB huge page can be split into 512 2MB huge pages. Demote interfaces are not available for the smallest huge page size. The demote interfaces are:
降低接口提供了将大页拆分为更小的大页的能力。例如,x86 架构支持 1GB 和 2MB 的大页大小。1GB 的大页可以拆分为 512 2MB 的大页。最小的大页大小不支持降低接口。降低接口包括:

  • demote_size
    is the size of demoted pages. When a page is demoted a corresponding number of huge pages of demote_size will be created. By default, demote_size is set to the next smaller huge page size. If there are multiple smaller huge page sizes, demote_size can be set to any of these smaller sizes. Only huge page sizes less than the current huge pages size are allowed.
    是降低页面的大小。当页面被降低时,将创建相应数量的 demote_size 大小的大页。默认情况下,demote_size 设置为下一个较小的大页大小。如果存在多个较小的大页大小,则 demote_size 可以设置为任何这些较小的大小。只允许比当前大页大小小的大页大小。

  • demote
    is used to demote a number of huge pages. A user with root privileges can write to this file. It may not be possible to demote the requested number of huge pages. To determine how many pages were actually demoted, compare the value of nr_hugepages before and after writing to the demote interface. demote is a write only interface.
    用于降低一定数量的大页。具有 root 权限的用户可以向此文件写入。可能无法降低请求的大页数量。要确定实际降低了多少页,请比较写入 demote 接口之前和之后的 nr_hugepages 的值。demote 是一个只写接口。

The interfaces which are the same as in /proc (all except demote and demote_size) function as described above for the default huge page-sized case.
/proc 中相同的接口(除了 demote 和 demote_size 之外的所有接口)在默认的大页大小情况下的功能如上所述。

Interaction of Task Memory Policy with Huge Page Allocation/Freeing

Whether huge pages are allocated and freed via the /proc interface or the /sysfs interface using the nr_hugepages_mempolicy attribute, the NUMA nodes from which huge pages are allocated or freed are controlled by the NUMA memory policy of the task that modifies the nr_hugepages_mempolicy sysctl or attribute. When the nr_hugepages attribute is used, mempolicy is ignored.
无论是通过 /proc 接口还是通过 /sysfs 接口使用 nr_hugepages_mempolicy 属性来分配和释放大页,都受到修改 nr_hugepages_mempolicy sysctl 或属性的任务的 NUMA 内存策略的控制。当使用 nr_hugepages 属性时,mempolicy 将被忽略。

The recommended method to allocate or free huge pages to/from the kernel huge page pool, using the nr_hugepages example above, is:
推荐的方法是使用以下示例,从内核的大页池中分配或释放大页:

numactl --interleave <node-list> echo 20 \
                            >/proc/sys/vm/nr_hugepages_mempolicy

or, more succinctly:
或者更简洁地:

numactl -m <node-list> echo 20 >/proc/sys/vm/nr_hugepages_mempolicy

This will allocate or free abs(20 - nr_hugepages) to or from the nodes specified in , depending on whether number of persistent huge pages is initially less than or greater than 20, respectively. No huge pages will be allocated nor freed on any node not included in the specified .
这将根据初始持久性大页的数量是否小于或大于 20,向指定的 节点分配或释放 abs(20 - nr_hugepages) 个大页。不会在未包含在指定 中的任何节点上分配或释放大页。

When adjusting the persistent hugepage count via nr_hugepages_mempolicy, any memory policy mode--bind, preferred, local or interleave--may be used. The resulting effect on persistent huge page allocation is as follows:
通过 nr_hugepages_mempolicy 调整持久性大页计数时,可以使用任何内存策略模式--bind、preferred、local 或 interleave。对持久性大页分配的影响如下:

  1. Regardless of mempolicy mode [see NUMA Memory Policy], persistent huge pages will be distributed across the node or nodes specified in the mempolicy as if "interleave" had been specified. However, if a node in the policy does not contain sufficient contiguous memory for a huge page, the allocation will not "fallback" to the nearest neighbor node with sufficient contiguous memory. To do this would cause undesirable imbalance in the distribution of the huge page pool, or possibly, allocation of persistent huge pages on nodes not allowed by the task's memory policy.
    无论 mempolicy 模式如何[参见 NUMA 内存策略],持久性大页将分布在 mempolicy 中指定的节点或节点上,就好像已经指定了 "interleave" 一样。但是,如果策略中的节点不包含足够的连续内存来存放一个大页,分配将不会“回退”到具有足够连续内存的最近邻节点。这样做会导致大页池的分布不平衡,或者可能会在任务的内存策略允许的节点上分配持久性大页。

  2. One or more nodes may be specified with the bind or interleave policy. If more than one node is specified with the preferred policy, only the lowest numeric id will be used. Local policy will select the node where the task is running at the time the nodes_allowed mask is constructed. For local policy to be deterministic, the task must be bound to a cpu or cpus in a single node. Otherwise, the task could be migrated to some other node at any time after launch and the resulting node will be indeterminate. Thus, local policy is not very useful for this purpose. Any of the other mempolicy modes may be used to specify a single node.
    可以使用 bind 或 interleave 策略指定一个或多个节点。如果使用 preferred 策略指定了多个节点,则只使用编号最低的节点。local 策略将选择构建 nodes_allowed 掩码时任务所在的节点。为了使 local 策略具有确定性,任务必须绑定到单个节点中的一个 CPU 或多个 CPU。否则,任务可能在启动后随时迁移到其他节点,结果节点将是不确定的。因此,local 策略对此目的并不是非常有用。任何其他 mempolicy 模式都可以用来指定单个节点。

  3. The nodes allowed mask will be derived from any non-default task mempolicy, whether this policy was set explicitly by the task itself or one of its ancestors, such as numactl. This means that if the task is invoked from a shell with non-default policy, that policy will be used. One can specify a node list of "all" with numactl --interleave or --membind [-m] to achieve interleaving over all nodes in the system or cpuset.
    nodes allowed 掩码将从任何非默认任务 mempolicy 派生,无论此策略是任务本身还是其祖先(例如 numactl)显式设置的。这意味着如果任务从具有非默认策略的 shell 中调用,将使用该策略。可以使用 numactl --interleave 或 --membind [-m] 指定一个 "all" 节点列表,以实现在系统或 cpuset 中的所有节点上进行交错。

  4. Any task mempolicy specified--e.g., using numactl--will be constrained by the resource limits of any cpuset in which the task runs. Thus, there will be no way for a task with non-default policy running in a cpuset with a subset of the system nodes to allocate huge pages outside the cpuset without first moving to a cpuset that contains all of the desired nodes.
    任何指定的任务 mempolicy--例如使用 numactl--将受到任务运行的 cpuset 的资源限制的约束。因此,对于在包含系统节点子集的 cpuset 中运行的具有非默认策略的任务,没有办法在不首先移动到包含所有所需节点的 cpuset 的情况下在 cpuset 外分配大页。

  5. Boot-time huge page allocation attempts to distribute the requested number of huge pages over all on-lines nodes with memory.
    启动时的大页分配尝试将请求的大页数量分布在具有内存的所有在线节点上。

Per Node Hugepages Attributes

A subset of the contents of the root huge page control directory in sysfs, described above, will be replicated under each the system device of each NUMA node with memory in:
在sysfs中的根巨页控制目录的部分内容将在每个NUMA节点的系统设备下进行复制,路径为:

/sys/devices/system/node/node[0-9]*/hugepages/
Under this directory, the subdirectory for each supported huge page size contains the following attribute files:
在这个目录下,每个支持的巨页大小的子目录包含以下属性文件:

nr_hugepages
free_hugepages
surplus_hugepages

The free_' and surplus_' attribute files are read-only. They return the number of free and surplus [overcommitted] huge pages, respectively, on the parent node.
free_和surplus_属性文件是只读的。它们分别返回父节点上空闲和剩余[过度分配]的巨页数量。

The nr_hugepages attribute returns the total number of huge pages on the specified node. When this attribute is written, the number of persistent huge pages on the parent node will be adjusted to the specified value, if sufficient resources exist, regardless of the task's mempolicy or cpuset constraints.
nr_hugepages属性返回指定节点上的巨页总数。当写入此属性时,如果存在足够的资源,父节点上的持久巨页数量将调整为指定值,而不考虑任务的内存策略或cpuset约束。

Note that the number of overcommit and reserve pages remain global quantities, as we don't know until fault time, when the faulting task's mempolicy is applied, from which node the huge page allocation will be attempted.
请注意,过度分配和保留页面的数量仍然是全局数量,因为在故障时,当应用故障任务的内存策略被应用时,我们不知道巨页分配将在哪个节点上尝试。

Using Huge Pages

If the user applications are going to request huge pages using mmap system call, then it is required that system administrator mount a file system of type hugetlbfs:
如果用户应用程序将使用mmap系统调用请求巨页,那么需要系统管理员挂载类型为hugetlbfs的文件系统:

mount -t hugetlbfs \
      -o uid=<value>,gid=<value>,mode=<value>,pagesize=<value>,size=<value>,\
      min_size=<value>,nr_inodes=<value> none /mnt/huge

This command mounts a (pseudo) filesystem of type hugetlbfs on the directory /mnt/huge. Any file created on /mnt/huge uses huge pages.
此命令在目录/mnt/huge上挂载了一个(伪)hugetlbfs类型的文件系统。在/mnt/huge上创建的任何文件都将使用巨页。

The uid and gid options sets the owner and group of the root of the file system. By default the uid and gid of the current process are taken.
uid和gid选项设置了文件系统根目录的所有者和组。默认情况下,会采用当前进程的uid和gid。

The mode option sets the mode of root of file system to value & 01777. This value is given in octal. By default the value 0755 is picked.
mode选项将文件系统根目录的模式设置为值和01777的与操作。这个值是以八进制表示的。默认情况下选择值0755。

If the platform supports multiple huge page sizes, the pagesize option can be used to specify the huge page size and associated pool. pagesize is specified in bytes. If pagesize is not specified the platform's default huge page size and associated pool will be used.
如果平台支持多个巨页大小,可以使用pagesize选项来指定巨页大小和关联的池。pagesize以字节为单位指定。如果未指定pagesize,则将使用平台的默认巨页大小和关联的池。

The size option sets the maximum value of memory (huge pages) allowed for that filesystem (/mnt/huge). The size option can be specified in bytes, or as a percentage of the specified huge page pool (nr_hugepages). The size is rounded down to HPAGE_SIZE boundary.
size选项设置了该文件系统(/mnt/huge)允许的内存(巨页)的最大值。size选项可以以字节或作为指定的巨页池(nr_hugepages)的百分比来指定。大小将向下舍入到HPAGE_SIZE边界。

The min_size option sets the minimum value of memory (huge pages) allowed for the filesystem. min_size can be specified in the same way as size, either bytes or a percentage of the huge page pool. At mount time, the number of huge pages specified by min_size are reserved for use by the filesystem. If there are not enough free huge pages available, the mount will fail. As huge pages are allocated to the filesystem and freed, the reserve count is adjusted so that the sum of allocated and reserved huge pages is always at least min_size.
min_size选项设置了文件系统允许的内存(巨页)的最小值。min_size可以以与size相同的方式指定,即字节或巨页池的百分比。在挂载时,min_size指定的巨页数量将被保留供文件系统使用。如果没有足够的空闲巨页可用,挂载将失败。随着巨页分配给文件系统并释放,保留计数将被调整,以便分配和保留的巨页总数始终至少为min_size。

The option nr_inodes sets the maximum number of inodes that /mnt/huge can use.
nr_inodes选项设置了/mnt/huge可以使用的最大inode数。

If the size, min_size or nr_inodes option is not provided on command line then no limits are set.
如果在命令行上未提供size、min_size或nr_inodes选项,则不设置限制。

For pagesize, size, min_size and nr_inodes options, you can use [G|g]/[M|m]/[K|k] to represent giga/mega/kilo. For example, size=2K has the same meaning as size=2048.
对于pagesize、size、min_size和nr_inodes选项,可以使用[G|g]/[M|m]/[K|k]来表示千兆/兆/千。例如,size=2K与size=2048具有相同的含义。

While read system calls are supported on files that reside on hugetlb file systems, write system calls are not.
虽然hugetlb文件系统上的文件支持读取系统调用,但不支持写入系统调用。

Regular chown, chgrp, and chmod commands (with right permissions) could be used to change the file attributes on hugetlbfs.
可以使用常规的chown、chgrp和chmod命令(具有正确权限)来更改hugetlbfs上的文件属性。

Also, it is important to note that no such mount command is required if applications are going to use only shmat/shmget system calls or mmap with MAP_HUGETLB. For an example of how to use mmap with MAP_HUGETLB see map_hugetlb below.
另外,需要注意的是,如果应用程序只使用shmat/shmget系统调用或带有MAP_HUGETLB的mmap,那么不需要执行这样的挂载命令。有关如何使用带有MAP_HUGETLB的mmap的示例,请参见下面的map_hugetlb。

Users who wish to use hugetlb memory via shared memory segment should be members of a supplementary group and system admin needs to configure that gid into /proc/sys/vm/hugetlb_shm_group. It is possible for same or different applications to use any combination of mmaps and shm* calls, though the mount of filesystem will be required for using mmap calls without MAP_HUGETLB.
希望通过共享内存段使用hugetlb内存的用户应该是辅助组的成员,并且系统管理员需要将该gid配置到/proc/sys/vm/hugetlb_shm_group中。同一个或不同的应用程序可以使用任意组合的mmap和shm*调用,但是如果要使用不带MAP_HUGETLB的mmap调用,则需要挂载文件系统。

Syscalls that operate on memory backed by hugetlb pages only have their lengths aligned to the native page size of the processor; they will normally fail with errno set to EINVAL or exclude hugetlb pages that extend beyond the length if not hugepage aligned. For example, munmap(2) will fail if memory is backed by a hugetlb page and the length is smaller than the hugepage size.
仅对由hugetlb页支持的内存进行操作的系统调用,它们的长度将对齐到处理器的本机页大小;如果长度不是巨页对齐的话,它们通常会失败,并将errno设置为EINVAL,或者排除超出长度的hugetlb页。例如,如果内存由hugetlb页支持,并且长度小于巨页大小,那么munmap(2)将失败。

Examples

map_hugetlb
see tools/testing/selftests/mm/map_hugetlb.c

hugepage-shm
see tools/testing/selftests/mm/hugepage-shm.c

hugepage-mmap
see tools/testing/selftests/mm/hugepage-mmap.c

The libhugetlbfs library provides a wide range of userspace tools to help with huge page usability, environment setup, and control.