Process scheduling Series Ulk--scheduler_tick () function

Source: Internet
Author: User

Reference is ulk Third Edition, Linux2.6.11.12 kernel version.

The scheduler relies on several functions to complete the dispatch, the most important of which is the Scheduler_tick function.

/** * maintains current up-to-date time_slice counter * each time the clock beats arrives, the Scheduler_tick function is called to perform the operation associated with the schedule. */void Scheduler_tick (void) {int cpu = SMP_PROCESSOR_ID (); runqueue_t *rq = This_rq ();//Macro THIS_RQ () generates the address of the local CPU run queue task_t * p = current;/** * The current value of the TSC converted to nanoseconds is stored in the Timestamp_last_tick of the local run queue. This timestamp is obtained by Sched_clock. */rq->timestamp_last_tick = Sched_clock ();/** * Checks if the current process is an idle process. Swapper process, is No. 0 process */if (p = = rq->idle) {/** * check if there are other running processes in the run queue, in addition to the idle process. * If available, set the Tif_need_scheduled field of the current process to force scheduling. */if (Wake_priority_sleeper (RQ)) Goto Out;rebalance_tick (CPU, RQ, sched_idle);/** * There is no need to update the time slice counter of the IDLE process, so it is returned directly here. */return;} /* Task might has expired already, but not scheduled off yet *//** * check if Current->array points to the active linked list where the queue is running locally. * If not, indicates that the process has expired but has not been replaced, set the TIF_NEED_SCHEDULED flag to force a reschedule. */if (P->array! = rq->active) {set_tsk_need_resched (P); goto out;} /** * Gets the spin lock of the running queue. */spin_lock (&rq->lock);/* The task is running during this tick-update the * time slice counter. Note:we do not update a thread ' s * priority until it Either goes to sleep or uses-its * timeslice. This makes it possible for interactive tasks * To use up their timeslices at their highest priority levels. *//** * Decrements the current process's time slice counter and checks if the time slice has been exhausted. * Because of the different scheduling types of the process, the operations performed by the functions vary greatly. */if (Rt_task (p)) {/* If it is a real-time process, it is further based on a real-time process of FIFO or RR type *//* * RR tasks need a special form of timeslice management. * FIFO T asks has no timeslices. *//** * For SCHED_RR type of real-time process, it is necessary to decrement its time slice. * For SCHED_FIFO type of real-time process, do nothing and exit. */if ((P->policy = = sched_rr) &&!--p->time_slice) {/** * real-time process for SCHED_RR type, if its time slice is exhausted, perform this action, To achieve the goal of seizing the current process. * If necessary, seize as soon as possible. */p->time_slice = Task_timeslice (P);/* Recalculates its time slice, which calculates its time slice based on the static priority of the process. *//** * Until here, it is not the first time that the process has run, it has used its time slice once and set the First_time_slice to 0. * This way, it does not return the remaining time slices to the parent process even if it exits. */p->first_time_slice = 0;/** * Set the dispatch flag to achieve the purpose of preemption as soon as possible. */set_tsk_need_resched (P);/* put it at the end of the queue: *//** * Put the real-time process at the end of the queue. This way, there are other RR processes with the same priority in the list, and other processes can be run. */requeue_task (P, rq->active);} Goto Out_unlock;} /** * Run to this to indicate that the process is a normal process. Now start updating the time slice of the normal process。 */if (!--p->time_slice) {/* First decrements the time slice counter of the normal process. If you are finished, continue with the following *//** * Now that you're done, remove the current process from the active collection. */dequeue_task (P, rq->active);/** * Of course, now that the current process has expired, you must set the reschedule flag to call schedule to select another process to run before the break returns. */set_tsk_need_resched (P);/** * Updates the dynamic priority of the current process. * Effective_prio calculates the dynamic priority of a process based on the Static_prio and Sleep_avg fields of the current process. */p->prio = Effective_prio (p);/** * Time slice of the refill process */p->time_slice = Task_timeslice (p);/** * Since a time slice of the current process is exhausted, Of course, you need to clear the First_time_slice flag. */p->first_time_slice = 0;/** * If the expired_timestamp of the local run queue is 0, the expired process collection is empty. * and the current process becomes an expired process immediately, assigning the current jiffies to Expired_timestamp * Expired_timestamp represents the time in the current queue where the oldest process in the expiration queue is inserted into the expiration queue. */if (!rq->expired_timestamp) Rq->expired_timestamp = jiffies;/** * Inserts the current process into an outdated collection or an activity collection. * Task_interactive Determines whether the current process is an interactive process. * The Task_interactive macro checks whether the wait time for the first expired process in the running queue has exceeded 1000 clock beats multiplied by the number of running processes in the run queue +1, if it is returned 1. * Returns 1 if the static priority of the current process is greater than the static priority of the expired process. */IF (! Task_interactive (P) | | Expired_starving (RQ)) {/** * The current process is not an interactive process, or there are higher priority processes in the expiration queue, insert the current process into an expired queue. */enqueue_task (P, rq->expired);/** * If the current process is the highest priority in the expiration queue, the highest priority of the expired queue is updated. */if (P->static_prio < Rq->best_expired_prio) Rq->best_expired_prio = P->static_prio;} Elseenqueue_task (P, rq->active);/* The process is an interactive process and is higher than the static priority of all processes in the expiration queue, then it is added to the active queue. This is actually a preferential treatment for interactive processes. */} else {/* The time slice of the normal process has not run out and needs to be checked further if the time slice is too long *//* * Prevent a too long timeslice allowing a task to monopolize * the CPU. We do this by splitting-the timeslice into * smaller pieces. * * Note:this does not mean the task ' s timeslices expire or * get lost on any ', ' they just might being preempted by * anot Her task of equal priority. (one with higher * priority would has preempted this task already.) We * Requeue The "This task" to the end of the "on" This "level", which is in essence a round-robin of the tasks with * Equal priority. * * This is applies to tasks in the interactive * Delta range with at least timeslice_granularity to Requeue. *//** * Check whether the time slice of the current process is too long, because for interactive processes, it may be inserted into the active queue after the time slice is exhausted, which may lead to a particularly long time slice of the process. */if (Task_interactive (p) &AMP;&AMP;! ((Task_timeslice (P)-p->time_slice)% timeslice_granularity (p)) && (P->time_slice >= Timeslice_ Granularity (P)) && (P->array = = rq->active)) {Requeue_task (P, rq->active); set_tsk_need_resched (P);}} out_unlock:/** * Release spin lock. */spin_unlock (&rq->lock); out:/** * Call the Rebalance_tick function, which should ensure that the running queue of different CPUs contains the same number of running processes that are basically the same. */rebalance_tick (CPU, RQ, not_idle);}

Process scheduling Series Ulk--scheduler_tick () function

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.