Adjusting the Timer Mechanism to Reduce Unnecessary Clock Interrupts
Principles
In versions earlier than Linux kernel 2.6.17, the Linux kernel sets a periodic clock interrupt for each CPU, and it processes some cron jobs with this interrupt, such as thread scheduling. As a result, even if the CPU does not need a timer, many clock interrupts are generated, wasting resources. Linux kernel 2.6.17 introduces the nohz mechanism to program the time of clock interrupts and reduce unnecessary clock interrupts.
Modification Method
Run the cat /proc/cmdline command to check the startup parameters of the Linux kernel. If the keyword nohz=off exists, the nohz mechanism is disabled and needs to be enabled. The modification method is as follows:
Before and after the modification, you can run the following command to check the number of scheduling times of timer_tick. In the command, $PID indicates the ID of the process to be checked. You can check the process with high CPU usage.
1 2 | perf sched record -- sleep 1 -p $PID perf sched latency -s max |
The following information is displayed, in which 591 indicates the number of scheduling times in the statistical period. A smaller value indicates that the modification takes effect.
1 | timer_tick:(97) | 7.364 ms | 591 | avg: 0.012 ms | max: 1.268 ms |
- In the /boot directory, run the find -name grub.cfg command to obtain the configuration file of the boot parameters.
- Remove nohz=off from the configuration file.
- Restart the server.