How to check Centos context switching: centos Context

Source: Internet
Author: User

How to check Centos context switching: centos Context
1. What is Context Switch )?

Context switching, also known as process switching or task switching, refers to switching the CPU from one process or thread to another process or thread.

The operating system can run multiple processes at the same time. However, a single CPU can only execute one task at the same time. The operating system uses time slice rotation to make users feel that these tasks are ongoing at the same time. The CPU serves each task for a certain period of time, saves the status of the current task, and continues to serve the next task after loading the status of the next task. The task state is saved and reloaded. This process is called context switching.

 

When a process is executed, the values in all the registers of the CPU, the status of the process, and the content in the stack are called the context of the process. When the kernel needs to switch to another process, it needs to save all the statuses of the current process, that is, the context of the current process, so that when the process is re-executed, the status of the switch must be executed. In LINUX, the current process context is stored in the task data structure of the process. In the event of interruption, the kernel executes the interrupt service routine in the kernel state in the context of the interrupted process. However, all required resources are retained to resume the execution of interrupted processes when the relay service ends.

2. What is the consumption of context switching?

Direct Consumption includes: the CPU registers need to be saved and loaded, the code of the system scheduler needs to be executed, the TLB instance needs to be reloaded, And the CPU pipeline needs to be flushed.

Indirect consumption: data is shared among multi-core caches. The impact of indirect consumption on the program depends on the size of operation data in the thread workspace.

Context switching is usually computing-intensive. That is to say, it takes a considerable processor time. During dozens or even hundreds of switches per second, each switch takes a matter of nanoseconds. Therefore, context switching means a lot of CPU time consumption for the system. In fact, it may be the most time-consuming operation in the operating system.

3. How do I check whether context switching is frequent?

(1) Confirm CPU usage

Whether the CPU is idle.

(2) vmstat 1

From the output of vmstat, we can see whether block_in and block_out of io items are frequent, whether the number of interruptions (in) and context switching (cs) of system items are frequent per second.

(3) pidstat-w 1

Pidstat shows whether cswch (Voluntary context switching) and nvcswch (Voluntary context switching) of each process are frequent.

Cswch/s: the number of active (voluntary) Context switching times for a task per second. When a task is in a blocking wait state, it will take the initiative to give up its CPU resources.
Nvcswch/s: the number of times that the task is passive (not voluntary) to switch the context. The time slice allocated by the CPU to a task has been used up, so the process will be forced to give up the CPU execution right.

(4)/proc/stat

/Proc/stat contains CPU activity information. Context switching is one of the items. It starts with ctxt and indicates the total number of context switches since the system is started.

Cat/proc/stat | grep ctxt & sleep 30 & cat/proc/stat | grep ctxt

The preceding command can calculate the number of context switches per second = difference value between the two/30

The preceding command can be used to locate the process with frequent context switching.

4. What causes frequent context switching?
  • After the time slice of the current task is used up, the system CPU can schedule the next task normally.
  • When the current execution task encounters IO blocking, the scheduler suspends the task and continues the next task.
  • Multiple tasks seize lock resources. If the current task is not snatched, the scheduler suspends the task and continues the next task.
  • User code suspends the current task to allow CPU time
  • Hardware interruption

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.