In a non-uniform memory access (NUMA) architecture, especially on Arm-based instances, accessing code segments on a remote NUMA node introduces latency. The code duplication feature resolves this by replicating code from remote nodes to the local node's memory, eliminating cross-node access and improving performance.
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-11or later.NoteTo query the kernel version of an image, run the
uname -rcommand.
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_duptextfield in the/proc/vmstatfile or theDupTextfield in the/proc/meminfofile to view the subpage statistics on an instance.nr_duptextindicates the number of subpages marked as duptext in the kernel.DupTextindicates the amount of memory that stores duptext data, in KB. A typical memory page is 4 KB in size.
Query the
/proc/pid/smapsfile 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 Use Workbench to connect to a Linux instance over SSH.
(Optional) Run the following command to view information about the NUMA nodes of the ECS instance:
numactl -HNoteIf you do not install the
numactltool, run thesudo yum install numactlcommand 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 testRun 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 ./testNoteIf you do not install the
cgexectool, run thesudo yum install -y libcgroup-toolscommand to install the tool.Run the following command to view statistics about subpages of the test program:
sudo cat /proc/$(pidof test)/smapsThe 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 Use Workbench to connect to a Linux instance over SSH.
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 duptextThe following sample command output shows that all subpages on the instance are cleared, which indicates that the code duptext feature is disabled.
