CPU context switch (on)

CPU context switching

We often say that the increase in the average load and cpu are not directly related, in different scenarios cpu load increase will cause the system, but the system is not necessarily the cpu load increase caused.

First, the system load is too high of three scenarios

  • cpu-intensive processes use a lot of cpu load will lead to an average increase, when the two are the same.
  • io-intensive process, waiting io can lead to increased average load, but not necessarily high cpu.

  • Waiting for a lot of cpu scheduling process will lead to increased average load, this time cpu usage will be higher.

A large number of processes competing for cpu (which is above the third scene), is often overlooked, cpu although not used, but in the competition, the load will happen?

We all know that linux is a multi-tasking operating system that supports much larger than the cpu number of tasks running at the same time, of course, the task is not run at the same time, but the system in a very short period of time, will turn cpu allocated to them, causing multiple tasks at the same time run illusion. And before each task runs, cpu need to know where to load the task, and from where to start to run, that is, the system needs to help it set up in advance cpu registers and program counter .

Two, CPU registers and program counter

1. cpu registers:

cpu built-in capacity is small, but extremely fast memory.

2. Program Counter:

Cpu command is used to store the location being executed, or the next instruction to be executed position. They are cpu before running any task, you must rely on the environment, it is also called the cpu context.

3. cpu context switch

context switching cpu: cpu is a previous task context (i.e., CPU registers and program counter) is saved, and then loading the new task context to these registers and program counter, and finally jump to a new location within the meaning of the program counter, run new task.

These preserved context, will be stored in the system kernel, and load come in again when the task is re-scheduled for execution. This ensures that the original status of the task will not be affected, so the task still looks continuous operation.

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

3.1. The process context switch

linux installation privilege level, the process of running space into kernel space and user space

![] Enterprise micro-channel capture _15486532103731.png

  • Kernel space (Ring0) has the highest authority, direct access to all resources;

  • User space (Ring3) only limited access rights, can not directly access memory and other hardware devices, must fall into the kernel through system calls to access privileged resources.

Therefore, the process can be run either in kernel space and user space to run, run in user space, called user mode. Running in kernel space, called the kernel mode.

3.1.1. The system call has occurred cpu context switch
  • System call : cpu command register position inside the original user state, need to be saved. Next, in order to perform the code kernel mode, cpu registers need to be updated to the new location of kernel mode instruction. Finally, just jump to the kernel mode running kernel tasks.

  • After the system call : cpu registers need to restore the original state saved by the user, and then switch to the user space, continue to run the process. Therefore, a system call, in fact, sent two cpu context switch.
  • Call process system, and does not involve virtual memory resources processes such as user mode, it will not switch routine. Therefore, the system call is usually called privileged mode switching, but in fact, a system call can not be avoided cpu context switch.
3.1.2. A process context switch calls the difference with the system

Process is managed and scheduled by the kernel, the process of switching only occurs in kernel mode, so the context of the process includes a virtual memory resource user mode, user stack, global variables only. Further comprising a state in kernel space kernel stack, register or the like.

Therefore, the process of switching more than one step system calls: Before you save the current state of the kernel process and cpu registers, you need to first virtual memory, user stack saved user state; after loading the kernel mode to the next process, You need to refresh the process's virtual kernel and user stack.

Context save and restore the context of the process is not free, you need to run memory to complete on the cpu.

Each context switch requires tens of nanoseconds to subtle cpu time. This time is considerable. In the case of a process context switch times more, can easily lead to a lot of cpu time spent on saving and restoring registers, the kernel stack and virtual memory and other resources, thus greatly reducing the time to really run the process. This is also an important factor leading to the increase in average load.

linux by (tlb) to manage virtual memory and direct mapping between physical memory, virtual memory after the update, tlb also updated, along with slower memory access. Especially on multiprocessor systems, cache is shared by multiple processors, the cache is refreshed not only affect the course of the current processor, other processors will also affect the process of shared cache.

When 3.1.3. The process of switching

Executing the process terminates, will release cpu, cpu take this time to a new process from the ready queue to run inside, there are other non-normal scene can lead to process switch.

  • Time slice is exhausted : the process can be so in order to ensure fair scheduling, CPU time, a time slice is divided into a section, which in turn time slices allocated to each process. When a time slice is exhausted, the system will be suspended, waiting to switch to other processes running cpu.

  • Insufficient system resources (such as: insufficient memory) : this time the process will be suspended, other processes running by the scheduling system.

  • Initiative to suspend the process: when the process this way will sleep the sleep function on their own initiative hangs.

  • High-priority process : When a high priority process appears, in order to ensure high-priority process is running, the current process will be a system hang

  • When the hardware interrupt : When a hardware interrupt occurs, the process will be interrupted on Cpu suspended in favor of the interrupt service routine in the kernel.

    3.2. Thread context switching

    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. So, for threads and processes it can be understood as:

  • When the process has only one thread, that thread can process the task.

  • When the process has multiple threads that share the same virtual memory, 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: a register stack, and the like, these context switches also need to be saved.

So:

When the two threads do not belong to the same process, thread context switching time. Since resources are not shared, so when switching between processes is equivalent to switching.

When the two threads of the same process, thread context switching time. Because the sharing of resources, only need to switch the thread private data.

Switching between process consumes more resources than switching between threads, so there have been replaced by multi-threaded multi-process advantages.

3.3 interrupt context switch

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. When interrupted by other processes, we need to save up the current state of the process.

With different process context switch, the interrupt context switching process does not involve user mode. Therefore, even if the process is interrupted by interrupting the process of a user mode, you do not need to save and restore the process's virtual memory. User mode resource global variables. Interrupt context, in fact, include only kernel mode interrupt service routine must state, including the cpu registers, the kernel stack, a hardware interrupt parameters.

A cup of the same, the interrupt handler has a higher priority than the process, so the interrupt context switching and thread context switch on will not occur simultaneously with the process. So most of the interrupt handler dapper, in order to perform end as soon as possible.

Summary

  1. cpu context switch, while maintaining one of the core functions of Linux systems up and running, in general, we do not need special attention.
  2. Excessive context switching, will cpu time consumed in saving and restoring registers, the kernel stack and virtual memory and other data, thus reducing the time to run a real process, resulting in system performance degradation.

Guess you like

Origin blog.51cto.com/12924846/2406421