Linux2.6 kernel process scheduling series -- scheduler_tick () function 3. Update the time slice of a common process,

Source: Internet
Author: User

Linux2.6 kernel process scheduling series -- scheduler_tick () function 3. Update the time slice of a common process,

RT

/*** Running here indicates that the process is a common process. Update the time slice of a common process. * // * First, decrease the time slice counter of a common process. If you have used up, continue with the following operations */if (! -- P-> time_slice) {/*** the current process is removed from the active set after it is used up. */Dequeue_task (p, rq-> active);/*** Of course, since the current process has expired, you must set a rescheduling flag, * call schedule to select another process to run before the interrupt return. */Set_tsk_need_resched (p);/*** update the dynamic priority of the current process. * Worker tive_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);/*** re-enter the time slice of the process */p-> time_slice = task_timeslice (p ); /*** since a time slice of the current process has been used up, * Of course, you need to clear the first_time_slice flag. */P-> first_time_slice = 0;/*** if the expired_timestamp of the local running queue is 0, the set of expired processes is empty. * And the current process will immediately become an expired process. * if the current jiffies is assigned to expired_timestamp * expired_timestamp, it indicates the time when * The oldest process in the expired queue is inserted into the expired queue. */If (! Rq-> expired_timestamp) rq-> expired_timestamp = jiffies;/*** inserts the current process into the expired process set or the active process set. * TASK_INTERACTIVE checks whether the current process is an interactive process. * TASK_INTERACTIVE macro checks whether the waiting time of the first expired process in the running queue * has exceeded 1000 clock beats multiplied by the number of runable processes in the running queue * + 1. If yes, 1 is returned. * EXPIRED_STARVING indicates that if the static priority of the current process is greater than * The static priority of the expired process, 1 is returned. */if (! TASK_INTERACTIVE (p) | EXPIRED_STARVING (rq) {/*** the current process is not an interactive process or has a higher priority * in the expired queue, insert the current process to the expired queue. */Enqueue_task (p, rq-> expired);/*** if the current process has the highest priority in the expired 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;} The else/* process is an interactive process, and it is higher than the static priority of all processes in the expired queue. * then it is added to the active queue. This is actually a preferential treatment for interactive processes. */Enqueue_task (p, rq-> active);} else {/* The time slice of a common process has not been used up yet, you need to further check whether the time slice is too long * // *** check whether the time slice of the current process is too long, because for an interactive process, * after the time slice is used up, it may be inserted into the active queue, which may lead to a particularly long time slice of this * process. */If (TASK_INTERACTIVE (p )&&! (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 );}}

1. The dynamic priority of the worker tive_prio function compute process.

In addition to static priorities, normal processes also have dynamic priorities. The value range is 100 (highest priority )~ 139 (lowest priority ). The dynamic priority is the number used by the scheduler when a new process is selected for running. Its relationship with static priority is expressed by the following empirical formula:

Bonus is a value ranging from 0 to 10. If the value is smaller than 5, the dynamic priority is reduced to show punishment. If the value is greater than 5, the dynamic priority is increased to show rewards. The bonus value depends on the past situation of the process and is more accurate. It is related to the average sleep time of the process.

# Define CURRENT_BONUS (p) \ (NS_TO_JIFFIES (p)-> sleep_avg) * MAX_BONUS/\ MAX_SLEEP_AVG)/*** read current static_prio and sleep_avg fields, based on the dynamic priority of the company's computing process. */Static int inclutive_prio (task_t * p) {int bonus, prio; if (rt_task (p) return p-> prio; bonus = CURRENT_BONUS (p)-MAX_BONUS/2; prio = p-> static_prio-bonus; if (prio <MAX_RT_PRIO) prio = MAX_RT_PRIO; if (prio> MAX_PRIO-1) prio = MAX_PRIO-1; return prio ;}

2. The TASK_INTERACTIVE macro checks whether a process is an interactive process.

Roughly speaking, the average sleep time is the average number of nanoseconds consumed by the process in the sleep state. Note that this is definitely not an operation to calculate the average value of the past time. For example, the average sleep time calculated between TASK_INTERRUPTIBLE and TASK_UNINTERRUPTIBLE is different. In addition, the average sleep time of a process is decreased during running. Finally, the average sleep time is never greater than 1 s.

The average sleep time is also used by the scheduler to determine whether a given process is an interactive process or a batch process. More specifically, if a process meets the following formula, it is considered as an interactive process:

It is equivalent to the following formula:

#define DELTA(p) \(SCALE(TASK_NICE(p), 40, MAX_BONUS) + INTERACTIVE_DELTA)#define TASK_INTERACTIVE(p) \((p)->prio <= (p)->static_prio - DELTA(p))

 

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.