Linux2.6.29 detailed analysis of CFS Scheduling (2)

Source: Internet
Author: User

 

 

Last time, I talked about the basic principles of CFS scheduling, and did not analyze the scheduling processes for wake-up and Process Migration, therefore, this article mainly analyzes the basic scheduling process in detail from several important scheduling points in the kernel, mainly in the form of flowcharts.

The kernel mainly has the following important starting points:
(1) Tick-related, that is, clock interruption

 

 

This is the whole process of updating vruntime during each interruption mentioned in the previous article. It can be understood that the vruntime is updated in the first half of the interruption, obviously, we will think about checking the tif_need_sched bit mentioned in the previous article and show that the part Where Schedule () is called is the lower part of the interrupt, in order to better understand the process of Interrupt Processing, I also made a flowchart (taking the clock interrupt as an example ):

 

 

 

 

 

 

 

 

(These Flowcharts are very large. If you cannot see them clearly, you can download them and enlarge them)

 

 

I will introduce the interrupt-related part in detail in future articles. I will not explain it here.

 

 

Let's summarize the entire process of this entry point:

 

 

When a clock is interrupted, the Kernel performs two operations (see Section ):

 

 

 

 

 

(1) do_timer (): Mainly used to update the system time.

 

 

(2) update_progress_times (): on the one hand, it executes the scheduler_tick () We mentioned above, and on the other hand it triggers a Soft Interrupt.

 

 

After the preceding steps are completed, the clock interruption will be returned, because other tasks are left for soft interruption. I will analyze some of the interruptions. Let's look at a piece of code when the interrupt returns:

 

 

Call do_irq
JMP ret_from_intr # Jump to ret_from_intr after do_irq is executed

 

 

Ret_from_intr:

 

 

..............
CMPL $ user_rpl, % eax # determine whether to return the user or kernel state
JB resume_kernel # not returning to v8086 or userspace

 

 

Entry (resume_userspace)
..............

 

 

 

 

 

JNE work_pending
JMP restore_all

 

 

Entry (resume_kernel)
..........

 

 

Need_resched:
Movl ti_flags (% EBP), % ECx # need_resched set?
Testb $ _ tif_need_resched, % Cl # Check whether tif_need_sched is set
JZ restore_all
Testl $ x86_eflags_if, pt_eflags (% ESP) # interrupts off (exception path )?
JZ restore_all
Call preempt_schedule_irq # The call schedule () is displayed here ()
JMP need_resched

 

 

 

 

 

Here we have tracked a complete scheduling process in the clock interrupt.

 

 

(2) The current process voluntarily abandons the CPU

 

 

Since you give up on your own initiative, you can call schedule () directly. This is done in many aspects of the kernel. You only need to search the call point of schedule. The following is a call starting with Schedule ().

 

 

Flowchart

:

 

 

 

 

 

 

 

 

Summarize the execution process:

 

 

(1) Clear tif_need_sched

 

 

(2) check whether the current process should be set to task_running. If not, delete it from the running queue.

 

 

(3) check whether there are other processes in the current running queue. If not, call idle_balance (CPU, rq) to balance the load.

 

 

(4) Put the current process back into the running queue and set the pointer of the current process to null.

 

 

(5) Select the next process as the current process

 

 

(6) If the current process is not equal to the selected process, switch the process and execute swtich_to. This part will be explained next time.

 

 

 

 

 

Let's talk about this too much, because I have a lot of details that I don't quite understand. Here are a few questions that I hope to discuss with you:

 

 

(1) the code in the Schedule () function and its expansion

 

 

If (prev-> State &&! (Preempt_count () & preempt_active )){
/* Prev-> state> 0 indicates that the current process is in the stopped state and can be preemptible */
If (unlikely (signal_pending_state (prev-> state, Prev )))
Prev-> state = task_running ;/**/
Else
Deactivate_task (RQ, Prev, 1);/* Delete Prev from the red/black tree */
Switch_count = & Prev-> nvcsw;

 

 

 

 

 

This part means that if the current process needs to stay in the running queue, set its Prev-> state = task_running; otherwise

 

 

Delete from the red/black tree.

 

 

Question 1: What is the condition for determining whether to continue to stay in the running queue?

 

 

If (unlikely (signal_pending_state (prev-> state, Prev )))

 

 

The guess is that when task_interruptible is in this status, the signal can be accepted at any time to continue running.

 

 

Problem 2: If you do not stay in the running queue, You need to delete the code from the red/black tree, and track the code that the deletion function ends with dequeue_entity ().

 

 

/* Clear partners */
Clear_buddies (cfs_rq, SE );
/* In most cases, the two are equal and not executed,
Because the current process is never in the running queue
What is the key issue? */
If (Se! = Cfs_rq-> curr)
_ Dequeue_entity (cfs_rq, SE );

 

 

What does this condition mean? Isn't se the current process? Under what circumstances can this judgment condition be established? It is better to have a specific example.

 

 

In most cases, Se = cfs_rq-> curr will not be executed because the current process is not in the running queue, this can be confirmed first when selecting a new process to run. For details, see pick_next_task ().

 



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.