In a non-uniform memory access (NUMA) architecture, especially on an Arm-based Elastic Compute Service (ECS) instance, each NUMA node has local memory. When a program or process on one NUMA node needs to access code snippets on other NUMA nodes, cross-node access causes additional latency and performance overheads. To resolve the preceding issues, you can use the code duptext feature to copy code snippets from a remote node to an on-premises node.
Background information
Limits
Only the following instance types and images support the code duptext feature:
Instance type: ECS Bare Metal Instance families. For more information, see Overview.
Image: Alibaba Cloud Linux 3 images that run kernel version
5.10.112-11
or later.NoteTo query the kernel version of an image, run the
uname -r
command.
Enable or disable the code duptext feature
The code duptext feature can be controlled globally or by using the memcg. The kernel can use the code duptext feature for processes only when both the global switch and memcg switch are turned on to enable the code duptext feature.
Switch | Description |
/sys/kernel/mm/duptext/enabled | The global switch is used to control whether the code duptext feature is enabled for the current kernel system. Valid values: 0 and 1. Default value: 0.
|
/sys/fs/cgroup/memory/<memcg directory name>/memory.allow_duptext | When the global switch is turned on, the memcg switch can be used to control whether the code duptext feature is enabled for processes in each memcg. Valid values: 0 and 1. Default value: 0.
|
In addition to using the preceding switches, you can use the following methods to query subpage statistics.
Query the
nr_duptext
field in the/proc/vmstat
file or theDupText
field in the/proc/meminfo
file to view the subpage statistics on an instance.nr_duptext
indicates the number of subpages marked as duptext in the kernel.DupText
indicates the amount of memory that stores duptext data, in KB. A typical memory page is 4 KB in size.
Query the
/proc/pid/smaps
file to view the subpage statistics of processes.
Use the code duptext feature
In this example, a test program named test.c is compiled and executed on an ECS instance that has two NUMA nodes.
Connect to the ECS instance.
For more information, see Connect to a Linux instance by using a password or key.
(Optional) Run the following command to view information about the NUMA nodes of the ECS instance:
numactl -H
NoteIf you do not install the
numactl
tool, run thesudo yum install numactl
command to install the tool.The following figure shows that the instance has two NUMA nodes: node 0 and node 1.
Run the following command to compile the test program and generate an executable file.
In this example, the source code file of test.c is compiled on node 1, and node 1 generates the page cache of the test file.
numactl -N 1 -m 1 gcc test.c -o test
Run the following command to turn on the global switch for the code duptext feature:
sudo sh -c 'echo 1 > /sys/kernel/mm/duptext/enabled'
Run the following commands to create a memcg directory and enable the duptext feature for the memcg:
sudo mkdir /sys/fs/cgroup/memory/test sudo sh -c 'echo 1 > /sys/fs/cgroup/memory/test/memory.allow_duptext'
Run the following command to use the code duptext feature to avoid cross-node access.
In this example, the cgexec and numactl tools are used to run the executable file named test and bind the process to node 0. In this case, a copy of the test-related code snippet is created on node 0. The test program can access the code snippet copy on node 0, without cross-node access.
sudo cgexec -g "memory:test" numactl -N 0 -m 0 ./test
NoteIf you do not install the
cgexec
tool, run thesudo yum install -y libcgroup-tools
command to install the tool.Run the following command to view statistics about subpages of the test program:
sudo cat /proc/$(pidof test)/smaps
The following sample command output shows statistics about the subpages of the test program. A code copy of the test program is generated on node 0.
NoteYou can also run the following commands to view the subpage statistics on the instance:
cat /proc/vmstat | grep -i duptext cat /proc/meminfo | grep -i duptext
Disable the code duptext feature
You can disable the code duptext feature based on your business requirements. When the code duptext feature is disabled, the kernel automatically clears all subpages on the instance.
Connect to the ECS instance.
For more information, see Connect to a Linux instance by using a password or key.
Run the following command to disable the code duptext feature:
sudo sh -c 'echo 0 > /sys/kernel/mm/duptext/enabled'
Run the following command to verify that the code duptext feature is disabled:
cat /proc/vmstat | grep -i duptext cat /proc/meminfo | grep -i duptext
The following sample command output shows that all subpages on the instance are cleared, which indicates that the code duptext feature is disabled.