Several methods of adjusting CPU usage under Linux

Source: Internet
Author: User
Tags nginx server

One, use Taskset to make full use of multi-core CPU, to equalize CPU utilization to each CPU

#taskset
-p, setting an existing PID instead of restarting a new task
-C, specifying a process that can specify multiple, comma-delimited, or range-specific, such as: 2,4,5,6-8.

1, switch a process to the specified CPU
TASKSET-CP 3 13290

2, let a program run on the specified CPU
Taskset-c 1,2,4-7 tar jcf test.tar.gz test

It should be noted that TASKSET-CP 3 13290 when setting an existing PID, the child process does not inherit the parent process,
Therefore, like tar zcf xxx.tar.gz xxx, it is best to specify the CPU at startup, and if it is already started, you need to specify the gzip process for the tar call.

Second, use nice and renice to set the priority of the program execution
Format: Nice [-n value] command

The Nice directive can change the priority level of program execution. The instruction allows the user to specify a priority level, called the nice value, when executing the program.
This value is from the highest priority of 20 to the lowest priority of 19. Negative values are the only root power.
For general users, you can also use the Nice directive to prioritize the execution of your program, but you can only make the nice value more elevated.

There are two ways to set a nice value for a program:
1, given a nice value when starting the execution of the program, with the Nice command
2. Adjust the nice value of the PID of a running program with the Renice command
It is usually backed up by adjusting the nice value so that it does not occupy a lot of system resources.

Cases:
Nice-n tar zcf test.tar.gz test

A program that is started by Nice and whose child processes inherit the nice value of the parent process.

View nice values
# nice-n-6 vim Test.txt &
# ps-l
F S UID PID PPID C PRI NI ADDR SZ Wchan TTY time CMD
4 S 0 19427 2637 0 75 0–16551 wait PTS/6 00:00:00 bash
4 T 0 21654 19427 0 71-6–23464 finish PTS/6 00:00:00 vim

Renice adjusting the nice value of a running program
Format: Renice [nice value] PID

Third, use Ulimit to limit CPU consumption time
Note that ulimit restricts the current shell process and its derived child processes. Therefore, you can call Ulimit in the script to limit the CPU usage time.
For example, limit the CPU time for tar in seconds.
# Cat Limit_cpu.sh
Ulimit-sht 100
Tar test.tar.gz test

If the tar takes longer than 100 seconds, tar exits, which may result in incomplete packaging, so it is not recommended to use Ulimit to limit CPU time.
In addition, the user can be restricted by modifying the system's/etc/security/limits configuration file.

Four, the use of the program comes with the use of CPU adjustment function
Some programs have the ability to adjust CPU usage, such as Nginx server, through its configuration file, you can specify the CPU for the worker process, as follows:
Worker_processes 3;
Worker_cpu_affinity 0001 0010 0100 1000;

Here 0001 0010 0100 1000 is a mask that represents the 1th, 2, 3, and 4 CPU cores, which makes the CPU usage average to each core.

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.