首页 > 其他分享 >[翻译]-hugePage的简要说明--部分内容

[翻译]-hugePage的简要说明--部分内容

时间:2023-02-01 16:26:06浏览次数:42  
标签:huge 大页 简要 -- hugepages hugePage 内存 nr size

hugePage的简要说明

本篇文档的主旨给linux内核支持的大页内存做一个简要的概述. 
大页内存的实现是建立在大多数现代架构所都支持的多级页大小的特性之上的. 
举例: x86架构下大部署CPU 的页面大小是4KB 或者是 2MB. 部分新的CPU支持1GB的页面大小. 
      IA64的架构支持 4K, 8K, 64K, 256K, 1M, 4M, 16M, 256M
      PPC64的架构支持: 4K 16MB
TLB是一个很小又很珍贵的资源, 主要用于快速实现虚拟内存到物理内存的专用工作. 
现代操作系统很关键的一个优化项目就在于随着内存的越来越大, 要更好的利用TLB以提高性能

用户可以使用mmap或者是shmget, shmat 这种基于标准的SYSV共享内存机制来使用大页内存. 

首先: linux的内核需要增加CONFIG_HUGETLBFS和CONFIG_HUGETLB_PAGE的配置项目.
      (其中CONFIG_HUGETLB_PAGE会因为CONFIG_HUGETLBFS的设置而自动生效)
/proc/meminfo 文件提供了系统内核里面大页内存池的详细信息. 
并且还会实时显示系统内核里面的大页的大小,以及空闲,占用了的,以及过量申请的的大页数量. 
大页内存的使用是需要使用带合适参数的系统调用以便映射到大页区域的. 

一般情况下/proc/meminfo 的战事信息主要如下: 
HugePages_Total: uuu
HugePages_Free:  vvv
HugePages_Rsvd:  www
HugePages_Surp:  xxx
Hugepagesize:    yyy kB
Hugetlb:         zzz kB

其中: 
HugePages_Total 大页内存的总量
HugePages_Free  没有被分配的大页内存数量.
HugePages_Rsvd  系统当前总共保留的大页数目,具体点是指程序已经向系统申请,但是还没有具体执行写入
                表示为尚未实际分配给程序的HugePages数目。
HugePages_Surp  超过设定的大页内存的数量. 
Hugepagesize    大页内存的单独每个页面的大小.
Hugetlb         大页内存的总计大小,单位为KB 可以理解为 Hugepagesize*HugePages_Total

/proc/filesystems  也会描述一个在内核中进行配置了的大页内存文件系统. 
/proc/sys/vm/nr_hugepages 指代Linux内核里面持久化大页内存页面多少
只有root用户权限才可以增加或者是改参数值的大小. 

只要是内存已经保留了大页内存的页面就不会因为内存压力还进入swap内存区域. 
并且大页内存不会用于非大页内存的使用场景, 是专用的. 
设置了大页内存后,用户可以使用合适的权限通过mmap后者是shmget等方式进行申请大页内存的使用

建议在系统启动时就直接修改好hugepages=N 的参数,  避免如果长时间使用导致内存随便无法申请足够打的大页内存. 
很多平台支持多种大小的内存页大小. 如果想使用其他大小的大页内存页大小, 需要在启动的参数中增加:
default_hugepagesz=<size> 的参数才可以, 这个参数需要使用精确的字节大小, 可以使用[kKmMgG] 作为后缀

/proc/sys/vm/nr_hugepages 里面其实使用的是默认的页大小. 并且是支持动态修改的, 可以使用如下命令来动态修改:
echo 20 > /proc/sys/vm/nr_hugepages
该命令会尽力在系统中申请20个大页内存. 用于进行分配和使用. 

需要注意在NUMA系统的情况时: 内核会尝试在所有的numa节点都平均分布大页的设置. 
这一块可能在单一numa节点内存不足时出现很多问题. 文章后面有一些详细的讨论
(看不太懂 -- 这句不是我说的 不是原文. )

是否能够成功申请到大页内存取决于系统上在申请大页内存的时刻是否有连续的并且可用的空闲内存. 
如果内核无法在一个numa节点上面申请到一个足够的内存. 会尝试从其他节点进行内存申请. 

系统管理员一般都喜欢在启动启动时就可以自动执行内存申请的命令.
可以设置为 local rc init files 实现自动加载. 
并且可以通过如下命令来查看不同numa节点的内存大页使用情况
cat /sys/devices/system/node/node*/meminfo | fgrep Huge

/proc/sys/vm/nr_overcommit_hugepages 
该参数会尝试再使用了nr_hugepages 以上的内存
如果设置了非零数值, 系统耗尽了 nr_hugepages 的内存之后 会尝试继续申请 非零数值的内存
但是需要注意, 如果这些内存被释放了, 也会反馈给内核,将大页内存的限制取消, 会继续正常分配内存. 
如果的确有需求申请大约持久的大页内存的数值, 建议直接修改nr_hugepages 的大小.对性能会更好一些. 

管理员也可以通过减少nr_hugepages 的数值来减少冻结的大页内存数量. 并且设置完之后内核会自动在
不同的numa节点上的空闲内存释放出来便于其他非大页内存使用. 

另外 /proc的文件系统与/sys下面是有一些对应的. 
比如 :/sys/kernel/mm/hugepages
hugepages-${size}kB
其实还有一些其他参数也在统计目录下面, 比如:
	nr_hugepages
	nr_hugepages_mempolicy
	nr_overcommit_hugepages
	free_hugepages
	resv_hugepages
	surplus_hugepages
这些参数会展示很多内核关于大页内存的默认值信息. 

任务内存管理侧率与大页内存申请和冻结的交互

申请或者是冻结内存的策略可以通过修改/proc虚拟文件系统和/sysfs文件系统的 nr_hugepages_mempolicy 参数来实现
但是需要注意如果启用了 nr_hugepages 的参数 nr_hugepages_mempolicy 的参数就会被忽略

我不想翻译这一段了, 自己看原文吧. 我不想用这个参数..网上都木有, 感觉容易掉坑里. 


每个numa节点的大页内存属性

/sysfs下面有一个基于大页内存控制上, 针对每一个numa节点的子节点内容
他具体体现在如下的设备内存信息中
/sys/devices/system/node/node[0-9]*/hugepages/

并且与系统级的大页内存设置相仿,也有如下的配置属性信息

	nr_hugepages
	free_hugepages
	surplus_hugepages

nr 和 free 与系统级的参数含义相仿. 
但是surplus 时当前节点没有可用的连续内存时, 系统会尝试从其他numa节点申请内存. 

使用大页内存

如果应用程序希望使用mmap的系统调用来申请大页内存
需要系统管理员 mount 一个 hugetlbfs 的文件类型. 
  mount -t hugetlbfs \
	-o uid=<value>,gid=<value>,mode=<value>,pagesize=<value>,size=<value>,\
	min_size=<value>,nr_inodes=<value> none /mnt/huge

这个命令会在/mnt/huge 目录下挂载一个虚拟的hugetlbfs类型的文件系统
任何在这个目录上创建的文件 文件权限默认值是  01777 
mmap的使用比较高深. 单词我都明白, 但是我尝试翻译了好几次,翻译不好, 请看原文吧..

This command mounts a (pseudo) filesystem of type hugetlbfs on the directory
/mnt/huge.  Any files created on /mnt/huge uses huge pages.  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.  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. 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. 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.  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.  The option
nr_inodes sets the maximum number of inodes that /mnt/huge can use.  If the
size, min_size or nr_inodes option is not provided on command line then
no limits are set.  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.

While read system calls are supported on files that reside on hugetlb
file systems, write system calls are not.

Regular chown, chgrp, and chmod commands (with right permissions) could be
used to change the file attributes on 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.

Users who wish to use hugetlb memory via shared memory segment should be a
member 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.

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.

参考资料

1) map_hugetlb: see tools/testing/selftests/vm/map_hugetlb.c

2) hugepage-shm:  see tools/testing/selftests/vm/hugepage-shm.c

3) hugepage-mmap:  see tools/testing/selftests/vm/hugepage-mmap.c

4) The libhugetlbfs (https://github.com/libhugetlbfs/libhugetlbfs) library
   provides a wide range of userspace too

标签:huge,大页,简要,--,hugepages,hugePage,内存,nr,size
From: https://www.cnblogs.com/jinanxiaolaohu/p/17083193.html

相关文章

  • Java基础学习09
    今天简单做小系统,之前也做过的类似的系统,想重新复习一次逻辑业务(2023-02-01-16:10:49)这次学到有了一个小的函数//获取本地时间并将时间格式化,调用sdf.format(date)输出......
  • B/S 前端大文件上传
    ​ 前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对Http协议较模糊,故这次采用渐进的方式来学习文件上传的......
  • [42S01] [Microsoft][SQL Server Native Client 11.0][SQL Server]数据库中已存在名为
    SQL server 下图中两个红色的地方不能保持一样,否则就会出现上面的错误 在 CONSTRAINT的后面表名中加上一些标识便可 (例_PK)与上面创建的表名区分开就可以了......
  • css行内元素的line-height属性导致的高度溢出问题。
    <divstyle="line-height:1.12;"><spanstyle="display:inline-block;">此处的文字高度要大于所属的span元素的高度。<span></span></span><div>这里......
  • HTTP1 与HTTP2的简要区别
    HTTP1.1与HTTP2的对比头信息压缩HTTP1.1中,每一次发送和响应,都有HTTP头信息。HTTP2压缩头信息,减少带宽。推送功能HTTP2之前,只能客户端发送数据,服务器端返回数据。HTTP2......
  • C语言练习------打字游戏
    1打字游戏(1)随机函数A:srand((unsigned)time(NULL));以当前时间为准,设置随机种子。注意:此函数,在每次开始游戏后调用一次即可。B:ch=rand();注意:rand()函数,每调用一次,产生一......
  • 快捷键的使用
    ctrl+C 复制ctrl+V 粘贴ctrl+X 剪切ctrl+A 全选ctrl+Z 撤销ctrl+S 保存alt+F4 关闭窗口右键删除--默认删除shift+delete 永久删除windows+E ......
  • JavaScript之异步编程
    什么是异步异步:Asynchronous,async是与同步synchronous,sync相对的概念。传统单线程编程中,程序的运行是同步的,指程序运行在一个控制流之中运行。而异步的概念就是不保证同......
  • ARC 155 题解
    A目前见过的最阴间的A。寻找规律,发现最后的回文串一定是由若干个周期拼起来的。当周期长度为偶数时,\(S\)和\(S'\)可以各拿半个周期。于是kmp求出border,再判一下,但......
  • XMLHttpRequest Level2的新功能
    可以设置HTTP请求的时限可以使用FormData对象管理表单数据可以上传文件可以获得数据传输的进度信息设置HTTP请求时限  FormData对象管理表单数据......