#Bash 的 chmem 命令
chmem [OPTION]... [SIZE|RANGE]
功能
配置内存的状态。
chmem 命令用于设置特定大小或范围的内存,可联机或脱机。
类型
可执行文件(/usr/sbin/chmem),属于 util-linux。
参数
OPTIONS选项:-b,--blocks- 启用或禁用BLOCK-d,--disable- 禁用内存-e,--enable- 启用内存-z,--zone ZONE- 启动或禁用 ZONEDMA- 直接内存访问(Direct Memory Access)DMA32- 32 位 DMANormal- 常规内存Highmem- 没有在内核页表中永久映射的物理内存部分Movable- 可移动页Device- 位于持久性内存(PMEM)和图形处理单元(GPU) 等设备上的内存
-v,--verbose- 打印详细信息-h,--help- 显示帮助-V,--version- 显示版本
#示例
配置指定大小的内存
$ lsmem # 查看哪些内存在线
RANGE SIZE STATE REMOVABLE BLOCK
0x0000000000000000-0x000000005fffffff 1.5G online yes 0-11
0x0000000100000000-0x000000041fffffff 12.5G online yes 32-131
Memory block size: 128M
Total online memory: 14G
Total offline memory: 0B
$ sudo chmem --disable 128M # 禁用 128M 内存
$ lsmem
RANGE SIZE STATE REMOVABLE BLOCK
0x0000000000000000-0x000000005fffffff 1.5G online yes 0-11
0x0000000100000000-0x000000040fffffff 12G online yes 32-129
0x0000000410000000-0x0000000417ffffff 128M offline 130
Memory block size: 128M
Total online memory: 13.9G
Total offline memory: 128M
$ sudo chmem --enable 128M # 启用 128M 内存
配置指定的内存块
$ sudo chmem --disable -b 128 # 禁用 #128 内存块
$ lsmem
RANGE SIZE STATE REMOVABLE BLOCK
0x0000000000000000-0x000000005fffffff 1.5G online yes 0-11
0x0000000100000000-0x00000003ffffffff 12G online yes 32-127
0x0000000400000000-0x0000000407ffffff 128M offline 128
0x0000000408000000-0x000000041fffffff 384M online yes 129-131
Memory block size: 128M
Total online memory: 13.9G
Total offline memory: 128M
$ sudo chmem --enable -b 128 # 启用 #128 内存块
配置指定范围的内存
$ sudo chmem --disable 0x0000000410000000-0x0000000417ffffff # 禁用此范围的内存
$ lsmem
RANGE SIZE STATE REMOVABLE BLOCK
0x0000000000000000-0x000000005fffffff 1.5G online yes 0-11
0x0000000100000000-0x000000040fffffff 12G online yes 32-129
0x0000000410000000-0x0000000417ffffff 128M offline 130
Memory block size: 128M
Total online memory: 13.9G
Total offline memory: 128M
$ sudo chmem --enable 0x0000000410000000-0x0000000417ffffff # 启用此范围的内存
配置指定 ZONE 上的内存
$ sudo chmem --disable 128M -z DMA # 禁用 DMA 上的 128M 内存
$ lsmem
RANGE SIZE STATE REMOVABLE BLOCK
0x0000000000000000-0x0000000047ffffff 1.1G online yes 0-8
0x0000000048000000-0x000000004fffffff 128M offline 9
0x0000000050000000-0x000000005fffffff 256M online yes 10-11
0x0000000100000000-0x000000041fffffff 12.5G online yes 32-131
Memory block size: 128M
Total online memory: 13.9G
Total offline memory: 128M
$ sudo chmem --enable 128M -z DMA # 启用 DMA 上的 128M 内存
#推荐阅读
#手册
CHMEM(8) System Administration CHMEM(8)
NAME
chmem - configure memory
SYNOPSIS
chmem [-h] [-V*] [-v] [-e|-d] [SIZE|RANGE -b BLOCKRANGE] [-z ZONE]
DESCRIPTION
The chmem command sets a particular size or range of memory online or
offline.
• Specify SIZE as <size>[m|M|g|G]. With m or M, <size> specifies the
memory size in MiB (1024 x 1024 bytes). With g or G, <size>
specifies the memory size in GiB (1024 x 1024 x 1024 bytes). The
default unit is MiB.
• Specify RANGE in the form 0x<start>-0x<end> as shown in the output
of the lsmem(1) command. <start> is the hexadecimal address of the
first byte and <end> is the hexadecimal address of the last byte in
the memory range.
• Specify BLOCKRANGE in the form <first>-<last> or <block> as shown
in the output of the lsmem(1) command. <first> is the number of the
first memory block and <last> is the number of the last memory
block in the memory range. Alternatively a single block can be
specified. BLOCKRANGE requires the --blocks option.
• Specify ZONE as the name of a memory zone, as shown in the output
of the lsmem -o +ZONES command. The output shows one or more valid
memory zones for each memory range. If multiple zones are shown,
then the memory range currently belongs to the first zone. By
default, chmem will set memory online to the zone Movable, if this
is among the valid zones. This default can be changed by specifying
the --zone option with another valid zone. For memory ballooning,
it is recommended to select the zone Movable for memory online and
offline, if possible. Memory in this zone is much more likely to be
able to be offlined again, but it cannot be used for arbitrary
kernel allocations, only for migratable pages (e.g., anonymous and
page cache pages). Use the --help option to see all available
zones.
SIZE and RANGE must be aligned to the Linux memory block size, as shown
in the output of the lsmem(1) command.
Setting memory online can fail for various reasons. On virtualized
systems it can fail if the hypervisor does not have enough memory left,
for example because memory was overcommitted. Setting memory offline
can fail if Linux cannot free the memory. If only part of the requested
memory can be set online or offline, a message tells you how much
memory was set online or offline instead of the requested amount.
When setting memory online chmem starts with the lowest memory block
numbers. When setting memory offline chmem starts with the highest
memory block numbers.
OPTIONS
-b, --blocks
Use a BLOCKRANGE parameter instead of RANGE or SIZE for the
--enable and --disable options.
-d, --disable
Set the specified RANGE, SIZE, or BLOCKRANGE of memory offline.
-e, --enable
Set the specified RANGE, SIZE, or BLOCKRANGE of memory online.
-z, --zone
Select the memory ZONE where to set the specified RANGE, SIZE, or
BLOCKRANGE of memory online or offline. By default, memory will be
set online to the zone Movable, if possible.
-v, --verbose
Verbose mode. Causes chmem to print debugging messages about it’s
progress.
-h, --help
Display help text and exit.
-V, --version
Print version and exit.
EXIT STATUS
chmem has the following exit status values:
0
success
1
failure
64
partial success
EXAMPLE
chmem --enable 1024
This command requests 1024 MiB of memory to be set online.
chmem -e 2g
This command requests 2 GiB of memory to be set online.
chmem --disable 0x00000000e4000000-0x00000000f3ffffff
This command requests the memory range starting with
0x00000000e4000000 and ending with 0x00000000f3ffffff to be set
offline.
chmem -b -d 10
This command requests the memory block number 10 to be set offline.
SEE ALSO
lsmem(1)
REPORTING BUGS
For bug reports, use the issue tracker at
https://github.com/util-linux/util-linux/issues.
AVAILABILITY
The chmem command is part of the util-linux package which can be
downloaded from Linux Kernel Archive
<https://www.kernel.org/pub/linux/utils/util-linux/>.
util-linux 2.39.3 2023-10-23 CHMEM(8)