Linux CPU affinity

Source: Internet
Author: User

In Linux, we know thatNice, reniceCommands change the execution priority of processes, and high-priority processes give priority to execution, so as to ensure the running of important tasks to a certain extent.

In addition to Nice and renice, you can use CPU affinity to specify the processors on which processes are running. CPU affinity indicates the tendency of a process to run on a given CPU for as long as possible without being migrated to another processor.
The 2.6 Linux kernel implements the CPU affinity interface,
Note: ApplicationProgramIf CPU affinity is specified, the application will only run on the specified processor. Even if other processors are idle, the application will not run on another processor.

In the following two cases, you may need to use the custom process CPU affinity:
1. Ensure high-priority tasks
If there are some high-priority tasks, you can allocate other tasks to the specified processor for execution. This high-priority task sets CPU affinity to other processors, avoid interference of non-important tasks with high-priority tasks.
A good case is that in a distributed computing system, data/index merge or data import is often performed in the early morning. To avoid the impact on service processes, these tasks can be executed on a single processor.
2. Test complex programs
For programs that claim to be scaled out, if resources are limited, you can define the CPU affinity of the process to gradually test the processing capability of programs with different number of processors!

In Linux, the CPU affinity of a specified process can be displayed through taskset (schedutils must be installed ).
Taskset has the following functions:
$ Taskset-help
Usage: taskset [Options] [mask | CPU-list] [pid | cmd [ARGs...]

Options:
-A, -- all-tasks operate on all the tasks (threads) for a given PID
-P, -- PID operate on existing given PID
-C, -- CPU-list display and specify CPUs in List format
......

Instance:
Compile a simple C program (to keep the CPU busy ):

 
# Include <stdio. h> void main (INT argc, char ** argv) {for (INT I = 0; I <= 10000000000000; I ++) {if (I = 100000000) {I = 0; printf ("program is running! \ N ");}}}

Generate an executable file: gcc-O test. C-STD = c99

Start two instances and bind processes to cpu0:
Taskset-C 0./test
Taskset-C 0./test

Enter "1" on top to view the system CPU status:

:
CPU 1 is idle, CPU 0 uses 100%

Continue the test, migrate the test with PID 18057 to cpu1, and run:
$ Taskset-PC 1 18057
PID 18057's current affinity list: 0
PID 18057's New Affinity list: 1

Observe the CPU status:

:
Cpu0 and cpu1 are in a busy state!

Continue testing! Migrate the test with PID 18210 to cpu1. run the following command:

:
Cpu0 is idle, and cpu1 is busy!

Reference:

Processor affinity
Management processor affinity
Drain server: run the process on the specified CPU

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.