In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the meaning of CPU context switching". Friends who are interested may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what CPU context switching means.
The CPU context can be understood as the state of the CPU register and the program counter PC, which record the state of the current task of the CPU. CPU context switching saves the context of the current cpu and then loads the corresponding context of the new task, which is stored in the system kernel and loaded again when the task is rescheduled for execution. This ensures that the original state of the task is not affected and that the task appears to be running continuously.
Tasks generally include:
1. Process
two。 Thread
3. Interrupt context
CPU context switching can be caused by privilege mode switching, process context switching, thread context switching and interrupt context switching.
Privileged mode switching
Linux divides the running space of the process into kernel space and user space according to the privilege level, which corresponds to Ring 0 and Ring 3 of the CPU privilege level in the figure below. Kernel space (Ring 0) has the highest permissions and can directly access all resources; user space (Ring 3) can only access restricted resources, not memory and other hardware devices, and must be trapped into the kernel through system calls to access these privileged resources.
Processes can run in either user space or kernel space. When the open (), read (), write (), close () system functions are called, CPU saves the instruction location of the original user state, and then updates the new location of the kernel state instruction in the CPU register. Finally, the kernel state function is executed. When the system call is over, CPU returns to its original user state and switches back to user space to continue the process.
You can see that the two processes of user state-> kernel state and kernel state-> user state have produced two CPU context switches. However, it should be noted that process user-mode resources such as virtual memory are not involved in the process of system call.
Process context switching
The process context includes not only the user space resources such as virtual memory, stack and global variables, but also the state of kernel space such as kernel stack and register. Compared with the system call, the switching between processes also needs to save the content.
With a large number of process context switches, it is easy to cause CPU to spend more time on the preservation and recovery of registers, virtual memory, kernel stack and other resources, reducing the process running time and increasing the average load. Linux manages the mapping of virtual memory to physical memory through TLB (Translation Lookaside Buffer). When virtual memory is updated, TLB also needs to be refreshed, and memory access slows down. Especially on multiprocessor systems, the cache is shared by multiple processors. Refreshing the cache will affect not only the processes of the current processors, but also the processes of other processors that share the cache.
Process context switching occurs in the process of process scheduling.
It is mainly triggered in the following scenarios:
CPU time is divided into time slices. After the time slices of the current process are exhausted, they will be suspended by the system and switched to other processes waiting for CPU to run.
When the process is short of system resources (such as insufficient memory), it can not run until the resources are satisfied. At this time, the process will be suspended and other processes will be scheduled to run.
When a process suspends itself actively through methods such as the sleep function sleep or sched_yield, it will naturally be rescheduled.
When there is a higher priority process running, in order to ensure the running of the high priority process, the current process will be suspended and run by the high priority process.
When a hardware interrupt occurs, the process on the CPU is suspended and the interrupt service program in the kernel is executed instead.
Thread context switch
The biggest difference between a thread and a process is that a thread is the basic unit of scheduling, while a process is the basic unit of resource ownership. In the task scheduling in the linux kernel, the actual scheduling object is the thread, while the process only provides the thread with virtual memory, global variables and other resources. There is only one thread in the process, and the process is equal to the thread. Because the threads in the same process share virtual memory, the related switching only needs to save its private data, registers, etc., so the intra-process thread switching consumes less resources than the inter-process switching.
Interrupt context switch
In order to respond quickly to hardware events, interrupt handling interrupts the normal scheduling and execution of the process, and instead calls the interrupt handler to respond to device events. When interrupting other processes, you need to save the current state of the process, so that after the interruption is over, the process can still resume running from its original state. Unlike the process context, interrupt context switching does not involve the user state of the process. Therefore, even if the interrupt process interrupts a process that is in user mode, there is no need to save and restore the process's virtual memory, global variables and other user-state resources. In fact, the interrupt context only includes the state necessary for kernel interrupt service program execution, including CPU registers, kernel stack, hardware interrupt parameters, and so on. For the same CPU, interrupt processing has a higher priority than a process, so interrupt context switching does not occur at the same time as process context switching. By the same token, because interrupts interrupt the scheduling and execution of normal processes, most interrupt handlers are short to finish execution as soon as possible. In addition, like process context switching, interrupt context switching also needs to consume CPU, too many switching times will consume a lot of CPU, and even seriously reduce the overall performance of the system. Therefore, when you find that there are too many interrupts, you need to be careful to check whether it will cause serious performance problems to your system.
Context cut Analysis of CPU
Vmstat is a commonly used system performance analysis tool, which is mainly used to analyze the memory usage of the system, as well as the number of CPU context switches and interrupts.
Root@ECSab169d:~# vmstat 5procs-memory- swap---io-----system---cpu- r b swpd free buff cache si so bi bo in cs us sy id wa st 10 90720 3960176 1036 3014980 0 35 85 01 10 980 0 90720 3959928 1036 3015004 00 0 24 240 358 00 99 00 01 90720 3959356 1036 3015068 00 0 34 260 390 10 99 00 00 90720 3959204 1036 3015088 00 0 23 234 340 00 99 00 00 90720 3960708 1036 3015088 00 0 609 297 429 00 99 00 00 90720 3960772 1036 3015112 00 0 140 295 410 00 99 00 00 90720 3960688 1036 3015164 0 0 0 36 269 406 0 0 99 0 0
R is the length of the ready queue, that is, the number of processes running and waiting for CPU.
B is the number of processes in uninterruptible sleep.
The size of the swpd swap partition is generally extended using swap when there is insufficient memory.
The size of free's free physical memory, my machine has a total of 8g of memory, leaving 3415m.
The buff Linux/Unix system is used to store the cache of what contents and permissions are in the directory. My computer takes up more than 300m.
Cache cache is directly used to memorize the files we open and buffer them. My local machine takes up more than 300m. (here is the wisdom of Linux/Unix. Use part of the free physical memory to cache files and directories in order to improve the performance of program execution. When the program uses memory, buffer/cached will be used quickly.)
Si reads the amount of virtual memory from disk per second. If this value is greater than 0, it means that the physical memory is insufficient or there is a memory leak. Find the memory-consuming process to solve it. My machine has plenty of memory and everything is fine.
So the amount of virtual memory written to disk per second. If this value is greater than 0, it is the same as above.
The number of blocks received by bi block devices per second. The block devices here refer to all disks and other block devices on the system. The default block size is 1024byte. There is no IO operation on my computer, so it has always been 0, but I have seen that it can reach 140000amp s on a machine that handles copying a large amount of data (2-3T), and the disk write speed is about 140m per second.
The number of blocks sent by a bo block device per second, for example, when we read a file, the bo should be greater than 0. Bi and bo are generally close to 0, or IO is too frequent and needs to be adjusted.
Number of interrupts per second of CPU in in, including time interrupts
Cs context switching per second
As you can see, in this example, the number of context switches cs is 33, while the number of system interrupts in is 25, and the ready queue length r and the number of uninterruptible processes b are both 0.
Vmstat only gives the overall context switching of the system, and to see the details of each process, you need to use the pidstat we mentioned earlier. Add the-w option to it and you can view the context switching of each process.
Root@xxxxx:~# pidstat-w 5Linux 4.15.0-66-generic (xxxxx) 01 watchdog/005:30:37 25Universe 2021 _ x86 pound 644 CPU 05:30:32 PM UID PID cswch/s nvcswch/s Command05:30:37 PM 0 8 18.16 0.00 rcu_sched05:30:37 PM 0 11 0.20 0.00 PM 0 183 4.59 0.00 kworker/3:1H05:30:37 PM 0 388 2.40 0.00 kworker/0:1H05:30:37 PM 0 546 0.20 0.00 irqbalance05:30:37 PM 0 621 9.98 0.00 qemu-ga05:30:37 PM 109 663 0.40 0.00 uml _ switch05:30:37 PM 0 1275 0.40 0.00 master05:30:37 PM 110 1277 0.20 0.00 qmgr05:30:37 PM 0 5896 7.39 0.00 kworker/0:005:30:37 PM 0 13579 8.18 0.00 sshd05:30:37 PM 0 13766 0.20 0.00 vmstat05:30:37 PM 0 26398 5.19 0.00 kworker/u8:105:30:37 PM 0 28341 6.19 0.00 kworker/u8:205:30:37 PM 0 28898 5.99 1.40 bash
There are two columns in this result that we focus on. One is cswch, which represents the number of voluntary context switches per second (voluntary context switches), and the other is nvcswch, which represents the number of involuntary context switches per second (non voluntary context switches).
Voluntary context switching: refers to the context switch caused by the process's inability to obtain the resources it needs. For example, voluntary context switching will occur when system resources such as Icano, memory and so on are insufficient.
Involuntary context switching: it refers to the context switch in which the process is forced to be scheduled by the system because the time slice has arrived and other reasons. For example, involuntary context switching can easily occur when a large number of processes are competing for CPU.
Summary
Voluntary context switching has become more and more, indicating that processes are waiting for resources, and other problems such as Igamo may have occurred.
Involuntary context switching has increased, indicating that processes are being forcibly scheduled, that is, they are all competing for CPU, indicating that CPU has indeed become a bottleneck.
The number of interrupts has increased, indicating that CPU is occupied by interrupt handlers, and specific interrupt types need to be analyzed by looking at the / proc/interrupts file.
At this point, I believe you have a deeper understanding of "what is the meaning of CPU context switching". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 256
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.