Context switching performance analysis of Linux

And before each task runs, the task from the CPU needs to know where to load, and from where to start to run, that is, the system needs to help it set up in advance CPU registers and program counter

CPU registers , a small built-in CPU capacity but fast memory. And the program counter, the instruction is used to store the location of the CPU is executed, or the next instruction to be executed position. Before they are CPU running any task, you must rely on the environment, it is also called the CPU context

Context switching CPU, CPU context is saved first before a task (i.e. CPU registers and program counter), and then adding
the carrier to which the new task context registers and program counter, and finally jump to a new program counter refers to position, run a new task.

Depending on the task, context switching the CPU can be divided into several different scenarios, that is, a process context switch, thread context switching and interrupt context switching .

Process context switch

Linux accordance with privilege level, the operational space of the process is divided into Ring 0 kernel space and user space, CPU privilege level and Ring 3. The process can either run in user space, and can run in kernel space. The process is running in user space, is called user mode process, and into kernel space of time, known as the kernel mode processes.
Transition from user mode to kernel mode, it needs to be done by the system call. For example, when we view the contents of the file, you need to call several times to complete the system: first call open () to open the file, and then call read () reads the contents of the file, and call write () to write the contents of the standard output, and finally call close () to close the file.
Kernel space (Ring 0) has the highest authority, direct access to all resources;
user space (Ring 3) only have access to limited resources, you can not directly access memory and other hardware devices, must fall into the kernel through system calls, to access these privileges resources.

Process system calls are switched CPU context does not happen? The answer of course is yes

CPU registers the original instruction position of the user state, need to be saved. Next, in order to perform the code kernel mode,
the CPU registers need to be updated to the new location of kernel mode instruction. And finally jump to the kernel mode tasks running kernel

And after the end of a system call, CPU registers need to restore previously saved user mode, and then switch to the user space, continue to run the process. So, once the process of system calls, in fact, it occurred twice CPU context switching.

Call process system, and does not involve virtual memory resources processes such as user mode, it will not switch routine. We usually refer to this process with context switching is not the same:

Process context switch means to switch from one process to another process to run.
The system calls the process has been running in the same process

Therefore, the system calls the process is commonly referred to as privileged mode switching, rather than context switching. But in fact, the system calls the process, context switching CPU or can not be avoided

Process context switch calls with the system, what difference does it make?

Process are managed and scheduled by the kernel, the switching process can only occur in kernel mode. Therefore, the context of the process includes not only the resources of the user's virtual memory space, stack, global variables, also includes the state of the kernel space kernel stack, registers, etc.
Thus, the process of context switching system calls on more than one step: before saving the current process status and CPU core registers need to first process the saved virtual memory, stack, etc.; and the loading process of the next kernel mode after, you need to refresh the process's virtual memory and user stack.

As shown below, the context save and restore the context of the process is not "free", you need the kernel running on the CPU to complete

Each context switch requires several tens of nanoseconds to several microseconds of CPU time. This time is considerable, especially in the case of a process context switch more frequently, it is easy to cause the CPU to spend a lot of time on saving and restoring registers, the kernel stack and virtual memory and other resources, thereby greatly reducing the real running time process. This is exactly the one we are talking about an important cause the average load factor increased.

Linux by TLB (Translation Lookaside Buffer) to manage virtual memory mapping to physical memory. When the updated virtual memory, TLB also need to refresh the memory of the visit will also slow down. Especially on multiprocessor systems, cache is shared by multiple processors, flush the cache processor will not only affect the current process, but also affect other processors share cache of process

When will the process context switch

Only need to switch context switching process, in other words, only in the process of scheduling the time, only need to switch context. Linux for each CPU maintains a ready queue, the active processes (ie running and is waiting for the CPU process) in accordance with the priority and sequencing CPU wait time, and then select the required process most CPU, which is the highest priority and CPU wait the longest process to run

When will the process is scheduled to run on the CPU it?

Executing the process is terminated, the CPU used before it will be released, this time from the ready queue again, take over a new process to run. In fact, there are many other scenarios will trigger the process of scheduling, where I'll give you one by one comb down.
First, in order to ensure that all processes can get a fair scheduling, CPU time is divided into time slices of a section, these times longer be assigned to each process in turn. Thus, when the time slice of a process is exhausted, the system will be suspended, cut
switch to other processes are waiting for CPU operation.
Second, the process on system resources (such as insufficient memory), to wait until the resources to meet the run, this time the process will be suspended, other processes running by the scheduling system.
Third, when the process through such methods will sleep the sleep function on their own initiative pending, it will naturally be rescheduled.
Fourth, when there is a higher priority process is running, in order to ensure the operation of high-priority process, the current process will be suspended, to be run by high-priority process.
When Fifth, a hardware interrupt occurs, the process on the CPU will be interrupted pending in favor of the interrupt service routine in the kernel.

Thread context switching

1, the biggest difference between threads and processes that thread is the basic unit of scheduling, and the process is the basic unit of resource owners.
The so-called task scheduling core, in fact, the object is thread scheduling; and the process only to the thread provides virtual memory resources, global variables, and so on.
Threads and processes can be understood:

When the process has only one thread, the process can be considered equivalent to threads.
When the process has multiple threads that share the same virtual memory and global variables and other resources. These resources when a context switch is not required to modify.
Further, the thread has its own private data, such as stack and registers, etc., these context switches also need to be saved

In fact, the thread context switching can be divided into two situations:
First, before and after the two threads belong to different processes. In this case, because the resources are not shared, so just switching process is the same process context switching.
The second, before and after the two threads belong to the same process. In this case, virtual memory is shared, so when switching, virtual memory, these resources remain intact, only need to switch the thread private data, registers, etc. do not share data

Threads within the same process switch, switch between multiple processes consume less than the resources, and this, it is also multi-threaded multi-process instead of an advantage

Interrupt context switching

In order to respond quickly to events hardware, scheduling and interrupt the normal execution process will interrupt the process and instead call the interrupt handler, event response equipment. And when interrupted by other processes, it is necessary to preserve the current state of the process, so that after the interruption, the process can still recover from the original run state.
With different process context, interrupt context switching process does not involve user mode. So, even interrupting the process interrupted by a user is in the process state does not need to save and restore the process of user mode virtual memory resources, global variables, and so on.

Interrupt context, in fact, it comprises only the kernel mode interrupt service routine performs the necessary state, including CPU registers, the kernel stack, a hardware interrupt parameters. On the same CPU, the interrupt handling has a higher priority than the process, so do not interrupt context switching and process context switch occurs simultaneously. Similarly, due to the interruption it will interrupt the normal process of scheduling and execution, so most of the interrupt handler dapper, in order to end as quickly as possible execution. In addition, with the same process context switch, interrupt context switching also need to consume CPU, to switch too many times will spend a lot of CPU, or even seriously degrade overall system performance. So, when you find interrupted too many times, we need to pay attention to the investigation if it will give you a serious system performance issues

How to view the system context switching case

Vmstat use this tool to query the system context switches, the main focus on the following items

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 running and the number of CPU waiting process.
b (Blocked) it is in the number of non-interrupted sleep process

Other

Free free physical memory size

A buffer between the device and the device buff

Linux / Unix systems is used to store, catalog what's inside content, permissions, etc. `` cache

And a buffer memory between the cache cpu

cache memory directly to our open files to the file buffer to do, I take up this machine probably more than 300 M (here is the clever Linux / Unix, and a portion of free physical memory used to do cache files and directories, in order to improve the performance of a program executed when a program uses memory, buffer / cached will soon be used.)

si per second read from disk into virtual memory size, if this value is greater than 0, indicating that not enough physical memory or a memory leak, and memory-intensive process're looking to get rid of. My machine has lots of RAM, everything is normal.

Size of the virtual memory is written to disk per second, so if this value is greater than 0, above.

the number of blocks bi block device received per second, where the block device refers to all disks and other block device on the system, the default block size is 1024byte
nothing I IO operations on the machine, it has been a 0, but I have a copy of the deal the large amount of data (2-3T) machines have seen up to 140000 / s, write speed of the disk almost 140M per second

Bo number of blocks transmitted per block device, for example, we read the file, must be greater than 0 bo. bi bo and generally have close to zero, or is IO too often, need to be adjusted.

us user CPU time
I have ever done in a very frequent encryption and decryption on the server, you can see us close to the 100, r run queue 80 (machine doing the stress test, poor performance).

sy system CPU time, if too high, the system is calling for a long time, for example, is frequently IO operations.

id CPU idle time, in general, id + us + sy = 100, generally I think id be idle CPU usage, us is user CPU usage, sy is the system CPU usage.

wa waiting for IO CPU time.

Check the details of each process

cswch, it represents the number of context switches per second voluntary (voluntary context switches), and

nvcswch, represents involuntary context switches per second (non voluntary context switches) number

The so-called voluntary context switch, in which a process can not obtain the required resources, resulting in a context switch. For example,
when the lack of I / O, memory and other system resources, voluntary context switch occurs.
Rather than voluntary context switch, it refers to the process time slice is due to other reasons, the system is forced scheduled, and thus the occurrence of
a context switch. For example, when a large number of processes are in competition for CPU, prone to involuntary context switches

case study

1, where a context switch to see under normal conditions

2, the analog switch multithreaded

3. Check that the context switch case

4, where the context switch to see pidstat

analysis

1, it was found from a context switch cs column 66 has risen to more than 870,000

R列:就绪队列长度已经到了7,远远超过了cpu的个数2,所以会产生大量的cpu竞争

us和sy列:cpu使用率加起来已经到100%其中系统 CPU 使用率,也就是 sy 列高达 87%,说明 CPU 主要是被内核占用了

in 列:中断次数也上升到了 3 万左右,说明中断处理也是个潜在的问题

综合这几个指标,可以知道,系统的就绪队列过长,也就是正在运行和等待 CPU 的进程数过多,导致了大量的上下文切换,而上下文切换又导致了系统 CPU 的占用率升高

2,从 pidstat 的输出你可以发现,CPU 使用率的升高果然是 sysbench 导致的,它的 CPU使用率已经达到了 100%。但上下文切换则是来自其他进程,包括非自愿上下文切换频率最高的 pidstat ,以及自愿上下文切换频率最高的内核线程 kworker 和 sshd

3,使用-wt 参数表示输出线程的上下文切换指标

虽然 sysbench 进程(也就是主线程)的上下文切换次数看起来并不多,但它的子线程的上下文切换次数却有很多。看来,上下文切换罪魁祸首,还是过多的sysbench 线程

4,查看/proc/interrupts ,查看中断使用情况

/proc 实际上是 Linux 的一个虚拟文件系统,用于内核空间与用户空间之间的通信。/proc/interrupts 就是这种通信机制的一部分,提供了一个只读的中断使用情

watch -d cat /proc/interrupts

可以通过vmstat 、 pidstat 和 /proc/interrupts 等工具,来辅助排查性能问题的根源

Guess you like

Origin www.cnblogs.com/mrwuzs/p/11279511.html