[Repost] CPU context switching (below)

CPU context switch (below)

https://www.jianshu.com/p/9efdef39af64

 

Excessive context switching will consume CPU time in the storage and restoration of data such as registers, kernel stacks, and virtual memory, shortening the time that the process is actually running, and becoming a culprit in the system performance.

Since context switching has such a big impact on system performance, you can't wait to know, how do you view context switching? Here, we can use the vmstat tool to query the context switching of the system.

vmstat is a commonly used system performance analysis tool, mainly used to analyze the memory usage of the system, and is also commonly used to analyze the number of CPU context switches and interrupts.

For example, the following is an example of using vmstat:


# 每隔5秒输出1组数据
$ vmstat 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 7005360 91564 818900 0 0 0 0 25 33 0 0 100 0 0 

Let's look at this result together. You can first try to interpret the meaning of each column yourself. Here, I emphasize the following four columns that require special attention:

  • cs (context switch) is the number of context switches per second.
  • in (interrupt) is the number of interrupts per second.
  • r (Running or Runnable) is the length of the ready queue, which is the number of processes that are running and waiting for the CPU.
  • b (Blocked) is the number of processes in an uninterruptible sleep state.

It can be seen that the number of context switches cs in this example is 33, while the number of system interrupts in is 25, and the length of the ready queue r and the number of uninterruptible processes b are both 0.

vmstat only gives the overall context switching of the system. To see the details of each process, we need to use the pidstat we mentioned earlier. Add it to the -w option, you can view the context switching of each process.

For example:


# 每隔5秒输出1组数据
$ pidstat -w 5
Linux 4.15.0 (ubuntu) 09/23/18 _x86_64_ (2 CPU) 08:18:26 UID PID cswch/s nvcswch/s Command 08:18:31 0 1 0.20 0.00 systemd 08:18:31 0 8 5.40 0.00 rcu_sched ... 

There are two columns of content in this result that we focus on. One is cswch, which represents the number of voluntary context switches per second, and the other is nvcswch, which represents the number of non-voluntary context switches per second. You must keep these two concepts in mind, because they imply different performance issues:

  • The so-called voluntary context switch refers to the context switch caused by the process unable to obtain the required resources. For example, when system resources such as I / O and memory are insufficient, voluntary context switching occurs.

  • Involuntary context switching refers to the context switching that the process is forced to schedule by the system due to the time slice has expired. For example, when a large number of processes are competing for the CPU, involuntary context switching is prone to occur.

There are more voluntary context switches, indicating that the process is waiting for resources, and other problems such as I / O may occur;

Involuntary context switching has increased, indicating that the process is being forced to schedule, that is, all are competing for CPU, indicating that CPU has indeed become a bottleneck;

The number of interrupts has increased, indicating that the CPU is occupied by an interrupt handler, and you need to analyze the specific interrupt type by looking at the / proc / interrupts file.

original

https://time.geekbang.org/column/article/70077

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12687854.html