Optimizing the performance of CPU context switching Linux

How to view the system's CPU context switching case

vmstat CPU context can view system handover situation. It is a common system performance analysis tool, the analysis system is mainly used where memory is also used to analyze the number of CPU context switching and interrupt system.

Run the following commands: vmstat 5 (every 5s outputs a set of data)

Here Insert Picture Description

It should focus on the specific content of the following columns:
cs: that is, the number of context switches per second context switch, such as the image above the red box.
in: the number of interrupts per second. As the figure above the blue box
r: Ready queue length, which is running and the number of CPU waiting process. As the figure above the green box
b: the number is in the process of uninterruptible sleep.
Id: i.e. the proportion of idle cpu idle i.e.
us + sy: cpu is the load situation, i.e., cpu utilization.

The above vmstat just given the overall context of the CPU switch, if you want to view CPU context switching for each process you need to use pidstat command with the -w option.

ubuntu execute the following commands: pidstat -w 5

Here Insert Picture Description

The above output
PID as the process ID,
cswch / S voluntary context switches per second.
cswch / s per second, compared with involuntary context switches.

The so-called voluntary context switch refers to the process can not obtain the resources they need, such as will be voluntary context switch occurs when insufficient IO, memory and other system resources.
Rather than voluntary context switch, it is the process time slice has been reached due to the reasons such systems are being forced to schedule a context switch occurs Further, for example, a large number of processes are involuntary context switch occurs when competitive CPU.

case study

Sysbench used to simulate switching of multithreaded scheduling
sysbench is a multithreaded benchmark tool, generally used to evaluate the different load database system parameters. In this case just using it as an exception process to simulate CPU context switch times too many problems.

(1)安装sysbench
Sudo apt-get update
Sudo apt-get install sysbench

(2) the first use case where the CPU context switching system idle view vmstat
Here Insert Picture Description

Context switching above the maximum output frequency 291 is later maintained at substantially 130 times the proportion of about 98% cpu idle ready queue r is 0

(3) then the command to simulate the system running as a terminal multithreaded scheduling bottleneck
# run for 5 minutes to 10 threads benchmark, the analog multi-thread switching problem
sysbench -test = threads -num-threads = 10 - -max-time = 300 run
Here Insert Picture Description

Then in a second run the following ends vmstat command to view handover situation context.
Here Insert Picture Description

The above output information from the CPU context can be seen at this time of switching times has reached 372,662 times cpu idle state id number r is 0 ready queue reached 8
The above can be known since the number of the ready queue is much greater than eight to achieve large number of CPU competition CPU 2 thus produced, resulting in a large number of context switches CPU so that the CPU load increases up to 100%.

(4) So in the end there is how to analyze which process is causing so much cpu context switch it?
In a third terminal in order to continue the following run pidstat -w -u 1
Here Insert Picture Description

From the output pidstat can clearly see the cpu load increase is really caused by the sysbench CPU occupancy rate has reached 96.33%. But the number of context switches are arranged below in order sysbench not find it not only turned out to be caused by sysbench index data show thread after pidstat command displays the default process data indicators need to add the -t option.

Here Insert Picture Description

Here Insert Picture Description

You can see from the above output out of context switches threads sysbench generated due to reach 30,000 times.

summary

(1) Check the number of context switches cpu
the vmstat 5 5 groups of data output per second
(2) sysbench analog multithreaded scheduling
SysBench -num--test Threads Threads = 10 = 300 = RUN --max-Time
(. 3) view the process status
pidstat -w -u 1
if the output process or thread status using the following command
pidstat -w -u -t 1
which is the data index -u -w output process is the output data indicators index -t output data of CPU threads

Guess you like

Origin blog.csdn.net/yuewen2008/article/details/93370229