How to use the command numactl (with examples)
- Linux
- December 25, 2023
Numactl is a command-line utility that allows users to control the NUMA (Non-Uniform Memory Access) policy for processes or shared memory on Linux systems. NUMA is a computer architecture design where a system has multiple memory nodes, and each node is directly connected to a subset of processors (CPUs). This architecture is beneficial for scalability and memory access, especially in large-scale systems.
Use case 1: Run a command on node 0 with memory allocated on node 0 and 1
Code:
numactl --cpunodebind=0 --membind=0,1 -- command command_arguments
Motivation: The motivation behind this use case is to bind a command to a specific CPU node (node 0) and allocate memory from multiple CPU nodes (node 0 and node 1). By doing so, we can optimize the performance by leveraging the locality of data access.
Explanation:
--cpunodebind=0
: This option binds the command to run only on CPUs of node 0.--membind=0,1
: This option allocates memory from both CPU nodes 0 and 1.-- command command_arguments
: This is the actual command that we want to run with the specified NUMA policy.
Example output:
Assuming we are running a command my_command
that requires memory access from multiple nodes, the output could be as follows:
Node: 0 | Command: my_command with arguments
Node: 1 | Command: my_command with arguments
Use case 2: Run a command on CPUs 0-4 and 8-12 of the current cpuset
Code:
numactl --physcpubind=+0-4,8-12 -- command command_arguments
Motivation: The motivation behind this use case is to restrict a command to run on specific CPUs. By doing so, we can control the affinity of the command to certain CPU cores for better performance and resource utilization.
Explanation:
--physcpubind=+0-4,8-12
: This option binds the command to run only on the specified set of CPUs.+
sign indicates that the specified CPUs are added to the existing cpuset.0-4
represents CPUs 0 to 4.8-12
represents CPUs 8 to 12.
-- command command_arguments
: This is the actual command that we want to run with the specified NUMA policy.
Example output:
Assuming we are running a command my_command
that performs CPU-intensive tasks, the output could be as follows:
Running my_command on CPUs: 0, 1, 2, 3, 4, 8, 9, 10, 11, 12
Use case 3: Run a command with its memory interleaved on all CPUs
Code:
numactl --interleave=all -- command command_arguments
Motivation: The motivation behind this use case is to distribute the memory interleaving evenly across all available CPUs. By using this approach, we can achieve a balanced memory access pattern, improving overall system performance and avoiding potential bottlenecks.
Explanation:
--interleave=all
: This option enables memory interleaving on all available CPUs.-- command command_arguments
: This is the actual command that we want to run with the specified NUMA policy.
Example output:
Assuming we are running a command my_command
that heavily relies on memory access, the output could be as follows:
Running my_command with interleaved memory on all available CPUs.
Conclusion:
The numactl
command is a powerful utility for controlling the NUMA policy of processes or shared memory. By leveraging the different options provided by numactl
, users can optimize resource allocation, improve performance, and exploit the benefits of NUMA architecture in their Linux systems.