Case context switching and CPU usage

A. The section reviews

1. CPU context switching: CPU registers and program counter

2. CPU context can be divided into several different scenarios: process context switch, thread context switching, interrupt context switching

3. The thread is the basic unit of scheduling, the process is the basic unit of resource owners

II. Involuntary and voluntary context switch context switching

vmstat: only gives the overall system context switches circumstances, in order to view the details of each process, it is necessary pidstat

pidstat - W  . 5       # every 5s outputs a set of data (focus)

cswch / s: represents the number of voluntary context switches per second

nvcswch / s: involuntary context switches per second represents the number of

 

1. voluntary context switch

Voluntary context switch refers to the process can not obtain the required voluntary, resulting in a context switch, such as: insufficient I / O, memory and other system resources, will be voluntary context switch occurs

2. Involuntary context switches

Involuntary context switch in which a process time slice is due to other reasons, the system is forced to scheduled

3. Case Studies

sysbench is a multi-threaded benchmarking tool to assess the general database system parameters under different load cases

 

(1) Installation

a. yum install

yum  install -y SysBench # does not require advance preparation package

b. tar.gz package installation

# Unpack:
 tar . -Zxvf filename tar .gz 
# into the directory after decompression 
cd file name 
. / The configure 
# compile and install 
the make && the make  install

(2) Case operations and analysis steps

a. In the first run sysbench terminal, the analog system multi-threaded calls bottleneck

Question # benchmark tests run for 5 minutes to 10 threads, multi-threaded analog switch 
SysBench --threads = 10 --max- Time = 300 Threads RUN

b. In the second run the terminal vmstat, context switching situation observed

vmstat 3

Found: cs context switches from the column once more than 24 to more than 100 million, while observing several other indicators

r columns: ready queue has a length more than five, more than the number of the system CPU 4, a large number of CPU competition

us (user) and sy (System) Column: two columns combined CPU utilization rises above 80%, wherein the system CPU utilization, i.e. sy column up to 71%, indicating that the main CPU is occupied by the kernel

in the column: the number of interrupts rose to more than 40,000, the problem is also a potential interrupt handling

 

Integrated these indicators may know, too many system ready queue is too long, that is, running and waiting for the CPU process, resulting in a large number of context switches, and context switching has led to increased CPU utilization system

So in the end is what's causing these problems?

c. In a third terminal pidstat look, CPU, and process context switch case

pidstat - W -u . 1    # -w parameter represents the process of switching the output indicator, -u parameter, output from the CPU represents the use of indicators

It is found from the output pidstat, increased CPU really is sysbench lead, its CPU usage has reached more than 300%, but found a small number of context switches output, than more than 100 million vmstat see clearly too small more, how is this going?

Let us recall, mentioned several context switching scenes, one of which mentioned, the basic unit of the Linux scheduler is actually a thread, and we also simulated scene sysbench thread scheduling problem, it is not pidstat ignored threads data?

pidstat: The default is the index data show progress, coupled with indicators -t parameter, the output will thread

-WT pidstat . 1    # -WT index represents the output thread context switching

Can be seen, although the context sysbench process (that is, the main thread) switching frequency not seem like much, but it's the child thread context switching has a lot, it seems, the root causes of context switches, or too many threads sysbench

Three. CPU usage

CPU usage: load average: 0.00,0.56,1.60

What indicators commonly used to describe the performance test CPU performance of the system?

Estimated that many people may not be the average load, nor is CPU context switching, but another more intuitive indicators: CPU usage

CPU usage: it is the statistical unit of time usage of CPU, display as a percentage, that CPU usage is how come?

 

In order to maintain CPU time, Linux tick rate by pre-defined (denoted as HZ), trigger time interrupts, and using global variables recorded several beats since the turn, sent once every time interrupt, the value of all the variables plus 1

HZ kernel tick rate is configurable options, and the like may be provided 100,250,1000, different systems may set different values, can be viewed by the following command:

grep 'CONFIG_HZ=' /boot/config-$(uname -r)

At the same time, because of the beat rate of HZ is a kernel option, so users can not directly access the space program, space program for the convenience of users, the kernel also provides a user-space beatness USER_HZ, it is always fixed at 100, that is, 1/100 sec so that users do not need to worry about the space program kernel HZ is set how much? Because it sees is always a fixed value USER_HZ

Linux通过/proc虚拟文件系统,向用户空间提供了系统内部状态的信息,而/proc/stat提供的就是系统的CPU和任务统计信息,比如说,如果你只关心CPU的话,可以执行下面的命令:

这里输出的一个表格,其中,第一列表示的是CPU编号,如CPU,CPU0,CPU1,而第一行没有编号为2的CPU,表示的是所有CPU的累加,其他列则表示不同场景下CPU的累加节拍数,它的单位是USER_HZ,也就是10ms(1/100秒),这其实就是不同场景下的CPU时间

 

(1) 怎么查看CPU使用率

top

top:显示了系统总体的CPU和内存使用情况,以及各个进程的资源使用情况,默认是3s刷新一次

可以发现,top并没有细分进程的用户态CPU和内核态CPU,那要怎么查看每个进程的详细情况呢?是不是上一次用到的pidstat命令,它可以专门分析每个进程的CPU使用情况

pidstat 1 5   # 每隔1s输出一组数据,共输出5组

pidstat命令,CPU使用率如下:

%usr:用户态CPU使用率

%system:内核态CPU使用率

%guest:运行虚拟机CPU使用率

%wait:等待CPU使用率

%cpu:总的CPU使用率

最后Average部分,还计算了5组数据平均值

 

Guess you like

Origin www.cnblogs.com/my_captain/p/12667016.html