Home> System Tutorial> LINUX> body text

Exploring context switching on Linux CPUs

WBOY
Release: 2024-02-05 13:06:10
forward
560 people have browsed it

As we all know, Linux is an operating system that supports multitasking. The number of tasks it can run at the same time far exceeds the number of CPUs. Of course, these tasks are not actually running at the same time (for a single CPU), but because the system allocates the CPU to these tasks in turn for a short period of time, creating the illusion of multiple tasks running at the same time.

CPU Context

Before each task runs, the CPU needs to know where to load and start the task. This means that the system needs to set the CPU'sregistersandprogram counterin advance.

CPU registers are small but very fast pieces of memory built into the CPU. The program counter is used to store the location of the instruction currently being executed by the CPU or the location of the next instruction to be executed.

Both of these are the necessary environments for the CPU before executing any tasks, so they are called "CPU context". Please refer to the picture below:

探讨 Linux CPU 的上下文切换

Now that you know what the CPU context is, I think it will be easy for you to understandCPU context switching. "CPU context switch" refers to saving the CPU context (CPU registers and program counter) of the previous task, then loading the context of the new task into these registers and program counter, and finally jumping to the program counter.

These saved contexts are stored in the system kernel and loaded again when task execution is rescheduled. This ensures that the original state of the task is not affected and the task appears to be running continuously.

Type of CPU context switch

You might say that CPU context switching is nothing more than updating CPU registers and program counter values, and these registers are designed to run tasks quickly, so why does it affect CPU performance?

Before answering this question, have you ever thought about what these "tasks" are? You might say that a task is aprocessor athread. Yes, processes and threads are the most common tasks, but there are other types of tasks besides that.

Don’t forgetHardware interruptis also a common task. The hardware trigger signal will cause the interrupt handler to be called.

Therefore, there are at least three different types of CPU context switches:

  • Process context switching
  • Thread context switching
  • Interrupt context switch

Let’s take a look one by one.

Process context switching

Linux divides the running space of the process into kernel space and user space according to the privilege level, which correspond to the CPU privilege levels ofRing 0andRing 3in the figure below respectively.

  • Kernel space(Ring 0) has the highest permissions and can directly access all resources
  • User Space(Ring 3) can only access restricted resources and cannot directly access hardware devices such as memory. It must betrapped into thekernel via asystem callin order to access these privileged resources.
探讨 Linux CPU 的上下文切换

Looking at it from another perspective, a process can run in both user space and kernel space. When a process is running inuser space, it is called theuser stateof the process. When it falls intokernel space, it is called the ## of the process. #kernelstate.

The conversion fromuser modetokernel modeneeds to be completed throughsystem call. For example, when we view the contents of a file, we need the following system call:

  • open():Open file
  • read(): Read the contents of the file
  • write(): Write the contents of the file to the output file (including standard output)
  • close():Close the file

So will CPU context switching occur during the above system call? of course.

This requires saving the location of the original user mode instruction in the CPU register first. Next, in order to execute kernel-mode code, the CPU registers need to be updated to the new location of the kernel-mode instructions. Finally, jump to the kernel state to run the kernel task.

Then after the system call ends, the CPU register needs torestorethe original saved user state, and then switch to user space to continue running the process.

Therefore, during a system call, there are actually two CPU context switches.

But it should be pointed out that the system call process will not involve process switching, nor will it involve switching of system resources such as virtual memory. This is different from what we usually call "process context switching". Process context switching refers to switching from one process to another, while the same process is always running during the system call

The system call process is usually calledprivileged mode switch, rather thancontext switch. But in fact, during the system call process, CPU context switching is also inevitable.

Process context switching vs system call

So what is the difference between process context switching and system calls? First of all, processes are managed by the kernel, and process switching can only occur in kernel mode. Therefore, the process context includes not only user space resources such asvirtual memory,stack, andglobal variables, but alsokernel stackandRegisterand other kernel space status.

SoProcess context switchingThere is one more step thansystem call:

Before saving the kernel state and CPU registers of the current process, you need to save the virtual memory, stack, etc. of the process; and load the kernel state of the next process.

According to Tsuna's test report, each context switch requires tens of nanoseconds to microseconds of CPU time. This time is considerable, especially in the case of a large number of process context switches, which can easily cause the CPU to spend a lot of time saving and restoring resources such as registers, kernel stacks, and virtual memory. This is exactly what we talked about in the last article, a significant factor that causes load average to rise.

So, when will the process be scheduled/switched to run on the CPU? In fact, there are many scenarios. Let me summarize them for you:

  • When a process's CPU time slice runs out, it will besuspendedby the system and switched to other processes waiting for the CPU to run.
  • When system resources are insufficient (such as insufficient memory), the process cannot run until resources are sufficient. At this time, the process will also besuspended, and the system will schedule other processes to run.
  • When a process automaticallysuspends itselfthrough thesleepfunction, it will naturally be rescheduled.
  • When a process with a higher priority is running, in order to ensure the running of the high-priority process, the current process will be suspended by the high-priority process.
  • When a hardware interrupt occurs, the process on the CPU will beinterrupted and suspended, and then execute the interrupt service routine in the kernel.

It is very necessary to understand these scenarios, because once there is a performance problem with context switching, they are the killer behind the scenes.

Thread context switching

The biggest difference between threads and processes is that threads are the basic unit oftask scheduling, while processes are the basic unit ofresource acquisition.

To put it bluntly, the so-called task scheduling in the kernel actually schedules threads; and the process only provides resources such as virtual memory and global variables for threads. Therefore, for threads and processes, we can understand it this way:

  • When a process has only one thread, it can be considered that one process is equal to one thread
  • When a process has multiple threads, these threads share the same resources, such as virtual memory and global variables.
  • In addition, threads also have their own private data, such as stacks and registers, which also need to be saved during context switches.

In this way, thread context switching can actually be divided into two situations:

  • First of all, the two threads before and after belong to different processes. At this time, since resources are not shared, the switching process is the same as process context switching.
  • Secondly, the two threads before and after belong to the same process. At this time, since the virtual memory is shared, the virtual memory resources remain unchanged during switching, and only the thread's private data, registers and other unshared data need to be switched.

Obviously, thread switching within the same process consumes less resources than switching multiple processes. This is also the advantage of multi-threading instead of multi-process.

Interrupt context switch

In addition to the previous two context switches, there is another scenario that also outputs CPU context switching, which isinterrupt.

In order to quickly respond to events, hardware interrupts will interrupt the normal scheduling and execution process, and then call theinterrupt handler.

When interrupting other processes, the current state of the process needs to be saved so that the process can still recover from the original state after the interruption.

Unlike process context, interrupt context switching does not involve the user state of the process. Therefore, even if the interrupt process interrupts the process in user mode, there is no need to save and restore user mode resources such as virtual memory and global variables of the process.

In addition, like process context switching, interrupt context switching will also consume CPU. Excessive switching times will consume a lot of CPU resources and even seriously reduce the overall performance of the system. Therefore, when you notice too many interrupts, you need to pay attention to check whether it will cause serious performance problems for your system.

in conclusion

In summary, no matter which scenario leads to context switching, you should know:

CPU context switching is one of the core functions to ensure the normal operation of the Linux system, and generally does not require our special attention.

However, excessive context switching will consume CPU time to save and restore data such as registers, kernel stacks, virtual memory, etc., thus shortening the actual running time of the process and causing a significant decrease in overall system performance.

The above is the detailed content of Exploring context switching on Linux CPUs. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!