Implement the "Rotating staircase deadline" multilevel rotating priority queue cpu scheduler. list_spice_tail courtesy of Peter Zijlstra Signed-off-by: Con Kolivas fs/proc/array.c | 2 include/linux/init_task.h | 8 include/linux/list.h | 29 + include/linux/sched.h | 37 - kernel/exit.c | 1 kernel/rtmutex.c | 10 kernel/sched.c | 1239 +++++++++++++++++++--------------------------- mm/oom_kill.c | 2 8 files changed, 585 insertions(+), 743 deletions(-) --- Index: linux-2.6.20-rsdl/fs/proc/array.c =================================================================== --- linux-2.6.20-rsdl.orig/fs/proc/array.c 2007-02-06 11:07:09.000000000 +1100 +++ linux-2.6.20-rsdl/fs/proc/array.c 2007-02-06 11:19:02.000000000 +1100 @@ -165,7 +165,6 @@ static inline char * task_state(struct t rcu_read_lock(); buffer += sprintf(buffer, "State:\t%s\n" - "SleepAVG:\t%lu%%\n" "Tgid:\t%d\n" "Pid:\t%d\n" "PPid:\t%d\n" @@ -173,7 +172,6 @@ static inline char * task_state(struct t "Uid:\t%d\t%d\t%d\t%d\n" "Gid:\t%d\t%d\t%d\t%d\n", get_task_state(p), - (p->sleep_avg/1024)*100/(1020000000/1024), p->tgid, p->pid, pid_alive(p) ? rcu_dereference(p->real_parent)->tgid : 0, pid_alive(p) && p->ptrace ? rcu_dereference(p->parent)->pid : 0, Index: linux-2.6.20-rsdl/kernel/exit.c =================================================================== --- linux-2.6.20-rsdl.orig/kernel/exit.c 2007-02-06 11:07:09.000000000 +1100 +++ linux-2.6.20-rsdl/kernel/exit.c 2007-02-06 11:19:02.000000000 +1100 @@ -170,7 +170,6 @@ repeat: zap_leader = (leader->exit_signal == -1); } - sched_exit(p); write_unlock_irq(&tasklist_lock); proc_flush_task(p); release_thread(p); Index: linux-2.6.20-rsdl/include/linux/sched.h =================================================================== --- linux-2.6.20-rsdl.orig/include/linux/sched.h 2007-02-06 11:07:09.000000000 +1100 +++ linux-2.6.20-rsdl/include/linux/sched.h 2007-02-06 11:19:02.000000000 +1100 @@ -522,8 +522,9 @@ struct signal_struct { #define MAX_USER_RT_PRIO 100 #define MAX_RT_PRIO MAX_USER_RT_PRIO +#define PRIO_RANGE (40) -#define MAX_PRIO (MAX_RT_PRIO + 40) +#define MAX_PRIO (MAX_RT_PRIO + PRIO_RANGE) #define rt_prio(prio) unlikely((prio) < MAX_RT_PRIO) #define rt_task(p) rt_prio((p)->prio) @@ -789,14 +790,7 @@ struct mempolicy; struct pipe_inode_info; struct uts_namespace; -enum sleep_type { - SLEEP_NORMAL, - SLEEP_NONINTERACTIVE, - SLEEP_INTERACTIVE, - SLEEP_INTERRUPTED, -}; - -struct prio_array; +struct queues; struct task_struct { volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ @@ -813,22 +807,32 @@ struct task_struct { #endif #endif int load_weight; /* for niceness load balancing purposes */ - int prio, static_prio, normal_prio; - struct list_head run_list; - struct prio_array *array; + int prio, static_prio, queued_prio; + DECLARE_BITMAP(bitmap, PRIO_RANGE + 1); + struct queues *queue; + unsigned long last_run; + struct list_head prio_list; unsigned short ioprio; #ifdef CONFIG_BLK_DEV_IO_TRACE unsigned int btrace_seq; #endif - unsigned long sleep_avg; - unsigned long long timestamp, last_ran; + unsigned long long timestamp; + unsigned long tick_entitlement; + /* + * How much this task is entitled to run at the current priority + * before being requeued at a lower priority + */ + unsigned long quota; + /* + * How much this task contributes to the current priority queue + * length + */ + unsigned long long sched_time; /* sched_clock time spent running */ - enum sleep_type sleep_type; unsigned long policy; cpumask_t cpus_allowed; - unsigned int time_slice, first_time_slice; #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) struct sched_info sched_info; @@ -1291,7 +1295,6 @@ extern void FASTCALL(wake_up_new_task(st static inline void kick_process(struct task_struct *tsk) { } #endif extern void FASTCALL(sched_fork(struct task_struct * p, int clone_flags)); -extern void FASTCALL(sched_exit(struct task_struct * p)); extern int in_group_p(gid_t); extern int in_egroup_p(gid_t); Index: linux-2.6.20-rsdl/kernel/sched.c =================================================================== --- linux-2.6.20-rsdl.orig/kernel/sched.c 2007-02-06 11:07:09.000000000 +1100 +++ linux-2.6.20-rsdl/kernel/sched.c 2007-02-08 12:41:11.000000000 +1100 @@ -16,6 +16,8 @@ * by Davide Libenzi, preemptible kernel bits by Robert Love. * 2003-09-03 Interactivity tuning by Con Kolivas. * 2004-04-02 Scheduler domains code by Nick Piggin + * 2007-02-06 Rotating Staircase deadline scheduling policy by Con Kolivas + * RSDL v0.11 */ #include @@ -73,126 +75,39 @@ #define USER_PRIO(p) ((p)-MAX_RT_PRIO) #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) +#define SCHED_PRIO(p) ((p)+MAX_RT_PRIO) /* - * Some helpers for converting nanosecond timing to jiffy resolution + * Preemption needs to take into account that a low priority task can be + * at a higher queued_prio due to list merging. */ -#define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ)) -#define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ)) +#define TASK_PREEMPTS_CURR(p, curr) \ + (((p)->queued_prio < (curr)->queued_prio) || \ + (((p)->queued_prio == (curr)->queued_prio) && \ + ((p)->prio < (curr)->prio && ((curr)->prio > (curr)->queued_prio)))) /* - * These are the 'tuning knobs' of the scheduler: - * - * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger), - * default timeslice is 100 msecs, maximum timeslice is 800 msecs. - * Timeslices get refilled after they expire. + * This is the time all tasks within the same priority round robin. + * Set to a minimum of 6ms. */ -#define MIN_TIMESLICE max(5 * HZ / 1000, 1) -#define DEF_TIMESLICE (100 * HZ / 1000) -#define ON_RUNQUEUE_WEIGHT 30 -#define CHILD_PENALTY 95 -#define PARENT_PENALTY 100 -#define EXIT_WEIGHT 3 -#define PRIO_BONUS_RATIO 25 -#define MAX_BONUS (MAX_USER_PRIO * PRIO_BONUS_RATIO / 100) -#define INTERACTIVE_DELTA 2 -#define MAX_SLEEP_AVG (DEF_TIMESLICE * MAX_BONUS) -#define STARVATION_LIMIT (MAX_SLEEP_AVG) -#define NS_MAX_SLEEP_AVG (JIFFIES_TO_NS(MAX_SLEEP_AVG)) - -/* - * If a task is 'interactive' then we reinsert it in the active - * array after it has expired its current timeslice. (it will not - * continue to run immediately, it will still roundrobin with - * other interactive tasks.) - * - * This part scales the interactivity limit depending on niceness. - * - * We scale it linearly, offset by the INTERACTIVE_DELTA delta. - * Here are a few examples of different nice levels: - * - * TASK_INTERACTIVE(-20): [1,1,1,1,1,1,1,1,1,0,0] - * TASK_INTERACTIVE(-10): [1,1,1,1,1,1,1,0,0,0,0] - * TASK_INTERACTIVE( 0): [1,1,1,1,0,0,0,0,0,0,0] - * TASK_INTERACTIVE( 10): [1,1,0,0,0,0,0,0,0,0,0] - * TASK_INTERACTIVE( 19): [0,0,0,0,0,0,0,0,0,0,0] - * - * (the X axis represents the possible -5 ... 0 ... +5 dynamic - * priority range a task can explore, a value of '1' means the - * task is rated interactive.) - * - * Ie. nice +19 tasks can never get 'interactive' enough to be - * reinserted into the active array. And only heavily CPU-hog nice -20 - * tasks will be expired. Default nice 0 tasks are somewhere between, - * it takes some effort for them to get interactive, but it's not - * too hard. - */ - -#define CURRENT_BONUS(p) \ - (NS_TO_JIFFIES((p)->sleep_avg) * MAX_BONUS / \ - MAX_SLEEP_AVG) - -#define GRANULARITY (10 * HZ / 1000 ? : 1) - -#ifdef CONFIG_SMP -#define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ - (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)) * \ - num_online_cpus()) -#else -#define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ - (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1))) -#endif - -#define SCALE(v1,v1_max,v2_max) \ - (v1) * (v2_max) / (v1_max) - -#define DELTA(p) \ - (SCALE(TASK_NICE(p) + 20, 40, MAX_BONUS) - 20 * MAX_BONUS / 40 + \ - INTERACTIVE_DELTA) - -#define TASK_INTERACTIVE(p) \ - ((p)->prio <= (p)->static_prio - DELTA(p)) - -#define INTERACTIVE_SLEEP(p) \ - (JIFFIES_TO_NS(MAX_SLEEP_AVG * \ - (MAX_BONUS / 2 + DELTA((p)) + 1) / MAX_BONUS - 1)) - -#define TASK_PREEMPTS_CURR(p, rq) \ - ((p)->prio < (rq)->curr->prio) - -#define SCALE_PRIO(x, prio) \ - max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE) +#define RR_INTERVAL ((6 * HZ / 1001) + 1) +#define DEF_TIMESLICE (RR_INTERVAL * 19) -static unsigned int static_prio_timeslice(int static_prio) -{ - if (static_prio < NICE_TO_PRIO(0)) - return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio); - else - return SCALE_PRIO(DEF_TIMESLICE, static_prio); -} - -/* - * task_timeslice() scales user-nice values [ -20 ... 0 ... 19 ] - * to time slice values: [800ms ... 100ms ... 5ms] - * - * The higher a thread's priority, the bigger timeslices - * it gets during one round of execution. But even the lowest - * priority thread gets MIN_TIMESLICE worth of execution time. - */ - -static inline unsigned int task_timeslice(struct task_struct *p) -{ - return static_prio_timeslice(p->static_prio); -} - -/* - * These are the runqueue data structures: - */ +struct queues { + DECLARE_BITMAP(bitmap, MAX_PRIO + 1); + /* + * The bitmap of priorities queued; include 1 bit for delimiter. + * The dynamic bits are _lazily_ removed. + */ -struct prio_array { - unsigned int nr_active; - DECLARE_BITMAP(bitmap, MAX_PRIO+1); /* include 1 bit for delimiter */ struct list_head queue[MAX_PRIO]; + /* Tasks queued at each priority */ + + unsigned long prio_quota[MAX_PRIO]; + /* + * The quota of ticks the runqueue runs at each dynamic priority + * before cycling to the next priority. + */ }; /* @@ -224,14 +139,26 @@ struct rq { */ unsigned long nr_uninterruptible; - unsigned long expired_timestamp; /* Cached timestamp set by update_cpu_clock() */ unsigned long long most_recent_timestamp; struct task_struct *curr, *idle; unsigned long next_balance; struct mm_struct *prev_mm; - struct prio_array *active, *expired, arrays[2]; - int best_expired_prio; + + DECLARE_BITMAP(static_bitmap, MAX_PRIO + 1); + /* The bitmap of all static priorities queued */ + + unsigned long prio_queued[MAX_PRIO]; + /* The number of tasks at each static priority */ + + struct queues *active, *next_queue, queues[2]; + + int prio_level; + /* The current dynamic priority level this runqueue is at */ + + unsigned long prio_rotation; + /* How many times we have rotated the priority queue */ + atomic_t nr_iowait; #ifdef CONFIG_SMP @@ -568,13 +495,7 @@ static inline struct rq *this_rq_lock(vo #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) /* - * Called when a process is dequeued from the active array and given - * the cpu. We should note that with the exception of interactive - * tasks, the expired queue will become the active queue after the active - * queue is empty, without explicitly dequeuing and requeuing tasks in the - * expired queue. (Interactive tasks may be requeued directly to the - * active queue, thus delaying tasks in the expired queue from running; - * see scheduler_tick()). + * Called when a process is dequeued and given the cpu. * * This function is only called from sched_info_arrive(), rather than * dequeue_task(). Even though a task may be queued and dequeued multiple @@ -607,13 +528,11 @@ static void sched_info_arrive(struct tas } /* - * Called when a process is queued into either the active or expired - * array. The time is noted and later used to determine how long we - * had to wait for us to reach the cpu. Since the expired queue will - * become the active queue after active queue is empty, without dequeuing - * and requeuing any tasks, we are interested in queuing to either. It - * is unusual but not impossible for tasks to be dequeued and immediately - * requeued in the same or another array: this can happen in sched_yield(), + * Called when a process is queued. + * The time is noted and later used to determine how long we had to wait for + * us to reach the cpu. + * It is unusual but not impossible for tasks to be dequeued and immediately + * requeued: this can happen in sched_yield(), * set_user_nice(), and even load_balance() as it moves tasks from runqueue * to runqueue. * @@ -672,71 +591,158 @@ sched_info_switch(struct task_struct *pr #define sched_info_switch(t, next) do { } while (0) #endif /* CONFIG_SCHEDSTATS || CONFIG_TASK_DELAY_ACCT */ +static inline int task_queued(struct task_struct *task) +{ + return !list_empty(&task->prio_list); +} + +static inline void set_task_entitlement(struct task_struct *p, int prio, + struct queues *queue) +{ + __set_bit(USER_PRIO(prio), p->bitmap); + + /* + * In the case this task has been part of a merged list that has + * made it to higher priority than it should be, we remove the + * quota from its own priority since it will get a quota at this + * priority. + */ + if (prio < p->prio) + __set_bit(USER_PRIO(p->prio), p->bitmap); + p->tick_entitlement = p->quota; +} + /* - * Adding/removing a task to/from a priority array: + * The dynamic priority bits are cleared lazily so it often has false + * positives. Only the static_bitmap has hard accounting. rt_tasks can + * only be on the active queue. */ -static void dequeue_task(struct task_struct *p, struct prio_array *array) +static inline void set_dynamic_bit(struct rq *rq, struct queues *queue, + struct task_struct *p) { - array->nr_active--; - list_del(&p->run_list); - if (list_empty(array->queue + p->prio)) - __clear_bit(p->prio, array->bitmap); + __set_bit(p->queued_prio, queue->bitmap); } -static void enqueue_task(struct task_struct *p, struct prio_array *array) +static inline void set_queue_bits(struct rq *rq, struct queues *queue, + struct task_struct *p) { - sched_info_queued(p); - list_add_tail(&p->run_list, array->queue + p->prio); - __set_bit(p->prio, array->bitmap); - array->nr_active++; - p->array = array; + __set_bit(p->prio, rq->static_bitmap); + set_dynamic_bit(rq, queue, p); } /* - * Put task to the end of the run list without the overhead of dequeue - * followed by enqueue. + * Removing from a runqueue. */ -static void requeue_task(struct task_struct *p, struct prio_array *array) +static void dequeue_task(struct task_struct *p, struct rq *rq) { - list_move_tail(&p->run_list, array->queue + p->prio); + list_del_init(&p->prio_list); + if (!--rq->prio_queued[p->prio]) + __clear_bit(p->prio, rq->static_bitmap); + /* + * While we don't know where this task really is, the p->queued_prio + * is most likely so see if we can clear that bit to take some load + * off schedule(). + */ + if (list_empty(p->queue->queue + p->queued_prio)) + __clear_bit(p->queued_prio, p->queue->bitmap); } -static inline void -enqueue_task_head(struct task_struct *p, struct prio_array *array) +static inline void task_new_queue(struct task_struct *p, struct rq *rq) { - list_add(&p->run_list, array->queue + p->prio); - __set_bit(p->prio, array->bitmap); - array->nr_active++; - p->array = array; + bitmap_zero(p->bitmap, PRIO_RANGE); + p->last_run = rq->prio_rotation; } /* - * __normal_prio - return the priority that is based on the static - * priority but is modified by bonuses/penalties. - * - * We scale the actual sleep average [0 .... MAX_SLEEP_AVG] - * into the -5 ... 0 ... +5 bonus/penalty range. - * - * We use 25% of the full 0...39 priority range so that: - * - * 1) nice +19 interactive tasks do not preempt nice 0 CPU hogs. - * 2) nice -20 CPU hogs do not get preempted by nice 0 tasks. - * - * Both properties are important to certain workloads. + * current_queue_prio determines what queued_prio a non rt_task will be + * queued at. If the task has already been running during this runqueue's + * major rotation (rq->prio_rotation) then it continues at the same + * priority if it has tick entitlement left. If it does not have entitlement + * left, it finds the next priority slot according to its nice value that it + * has not extracted quota from. If it has not run during this major + * rotation, it starts at its static priority (p->prio) and has its + * bitmap quota cleared. If it does not have any slots left it has all its + * slots reset and is queued on the next_queue. */ +static int current_queue_prio(struct task_struct *p, struct rq *rq, + struct queues **queue) +{ + int queue_prio; + + if (p->last_run == rq->prio_rotation) { + if (p->tick_entitlement && + (*queue)->prio_quota[p->queued_prio]) + return p->queued_prio; + } else + task_new_queue(p, rq); + queue_prio = SCHED_PRIO(find_next_zero_bit(p->bitmap, PRIO_RANGE, + USER_PRIO(p->prio))); + if (queue_prio == MAX_PRIO) { + queue_prio = p->prio; + *queue = rq->next_queue; + bitmap_zero(p->bitmap, PRIO_RANGE); + } else + (*queue)->prio_quota[queue_prio] += p->quota; + set_task_entitlement(p, queue_prio, *queue); + p->queued_prio = queue_prio; + p->queue = *queue; + return queue_prio; +} + +/* + * Adding to a runqueue. The dynamic priority queue that it is added to is + * determined by the current dynamic priority of the runqueue it is being + * added to (see current_queue_prio above). The p->prio is not changed + * dynamically except in the case of real time priority inheritance. The + * bitmap stores a list of the static priorities, and the number of tasks + * stored at each p->prio level. + */ +static inline void __enqueue_task(struct task_struct *p, struct rq *rq, + struct queues **queue, int *queue_prio) +{ + if (rt_task(p)) + *queue_prio = p->prio; + else + *queue_prio = current_queue_prio(p, rq, queue); + rq->prio_queued[p->prio]++; + + sched_info_queued(p); + set_queue_bits(rq, *queue, p); +} + +static void enqueue_task(struct task_struct *p, struct rq *rq) +{ + struct queues *queue = rq->active; + int queue_prio; + + __enqueue_task(p, rq, &queue, &queue_prio); + list_add_tail(&p->prio_list, queue->queue + queue_prio); +} -static inline int __normal_prio(struct task_struct *p) +static inline void enqueue_task_head(struct task_struct *p, struct rq *rq) { - int bonus, prio; + struct queues *queue = rq->active; + int queue_prio; - bonus = CURRENT_BONUS(p) - MAX_BONUS / 2; + __enqueue_task(p, rq, &queue, &queue_prio); + list_add(&p->prio_list, queue->queue + queue_prio); +} - 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; +/* + * requeue_task is only called when p->prio does not change. p->queued_prio + * can change with dynamic tasks. + */ +static void requeue_task(struct task_struct *p, struct rq *rq, int queue_prio, + struct queues *queue) +{ + list_move_tail(&p->prio_list, queue->queue + queue_prio); + if (!rt_task(p)) { + if (list_empty(p->queue->queue + p->queued_prio)) + __clear_bit(p->queued_prio, p->queue->bitmap); + p->queued_prio = queue_prio; + p->queue = queue; + set_dynamic_bit(rq, queue, p); + } } /* @@ -748,6 +754,17 @@ static inline int __normal_prio(struct t * slice expiry etc. */ +/* slice - the total duration a task can run during one major rotation */ +static inline unsigned int slice(struct task_struct *p) +{ + unsigned int slice, rr; + + slice = rr = p->quota; + if (likely(!rt_task(p))) + slice += (PRIO_RANGE - 1 - TASK_USER_PRIO(p)) * rr; + return slice; +} + /* * Assume: static_prio_timeslice(NICE_TO_PRIO(0)) == DEF_TIMESLICE * If static_prio_timeslice() is ever changed to break this assumption then @@ -756,10 +773,9 @@ static inline int __normal_prio(struct t #define TIME_SLICE_NICE_ZERO DEF_TIMESLICE #define LOAD_WEIGHT(lp) \ (((lp) * SCHED_LOAD_SCALE) / TIME_SLICE_NICE_ZERO) -#define PRIO_TO_LOAD_WEIGHT(prio) \ - LOAD_WEIGHT(static_prio_timeslice(prio)) -#define RTPRIO_TO_LOAD_WEIGHT(rp) \ - (PRIO_TO_LOAD_WEIGHT(MAX_RT_PRIO) + LOAD_WEIGHT(rp)) +#define TASK_LOAD_WEIGHT(p) LOAD_WEIGHT(slice(p)) +#define RTPRIO_TO_LOAD_WEIGHT(rp) \ + (LOAD_WEIGHT((RR_INTERVAL + 20 + (rp)))) static void set_load_weight(struct task_struct *p) { @@ -776,7 +792,7 @@ static void set_load_weight(struct task_ #endif p->load_weight = RTPRIO_TO_LOAD_WEIGHT(p->rt_priority); } else - p->load_weight = PRIO_TO_LOAD_WEIGHT(p->static_prio); + p->load_weight = TASK_LOAD_WEIGHT(p); } static inline void @@ -804,149 +820,63 @@ static inline void dec_nr_running(struct } /* - * Calculate the expected normal priority: i.e. priority - * without taking RT-inheritance into account. Might be - * boosted by interactivity modifiers. Changes upon fork, - * setprio syscalls, and whenever the interactivity - * estimator recalculates. + * __activate_task - move a task to the runqueue. */ -static inline int normal_prio(struct task_struct *p) +static inline void __activate_task(struct task_struct *p, struct rq *rq) { - int prio; + enqueue_task(p, rq); + inc_nr_running(p, rq); +} - if (has_rt_policy(p)) - prio = MAX_RT_PRIO-1 - p->rt_priority; - else - prio = __normal_prio(p); - return prio; +/* + * __activate_idle_task - move idle task to the _front_ of runqueue. + */ +static inline void __activate_idle_task(struct task_struct *p, struct rq *rq) +{ + enqueue_task_head(p, rq); + inc_nr_running(p, rq); } /* * Calculate the current priority, i.e. the priority * taken into account by the scheduler. This value might - * be boosted by RT tasks, or might be boosted by - * interactivity modifiers. Will be RT if the task got - * RT-boosted. If not then it returns p->normal_prio. + * be boosted by RT tasks as it will be RT if the task got + * RT-boosted. If not then it returns p->static_prio. */ static int effective_prio(struct task_struct *p) { - p->normal_prio = normal_prio(p); /* * If we are RT tasks or we were boosted to RT priority, * keep the priority unchanged. Otherwise, update priority - * to the normal priority: + * to the static priority: */ if (!rt_prio(p->prio)) - return p->normal_prio; + return p->static_prio; return p->prio; } /* - * __activate_task - move a task to the runqueue. - */ -static void __activate_task(struct task_struct *p, struct rq *rq) -{ - struct prio_array *target = rq->active; - - if (batch_task(p)) - target = rq->expired; - enqueue_task(p, target); - inc_nr_running(p, rq); -} - -/* - * __activate_idle_task - move idle task to the _front_ of runqueue. - */ -static inline void __activate_idle_task(struct task_struct *p, struct rq *rq) -{ - enqueue_task_head(p, rq->active); - inc_nr_running(p, rq); -} - -/* - * Recalculate p->normal_prio and p->prio after having slept, - * updating the sleep-average too: - */ -static int recalc_task_prio(struct task_struct *p, unsigned long long now) -{ - /* Caller must always ensure 'now >= p->timestamp' */ - unsigned long sleep_time = now - p->timestamp; - - if (batch_task(p)) - sleep_time = 0; - - if (likely(sleep_time > 0)) { - /* - * This ceiling is set to the lowest priority that would allow - * a task to be reinserted into the active array on timeslice - * completion. - */ - unsigned long ceiling = INTERACTIVE_SLEEP(p); - - if (p->mm && sleep_time > ceiling && p->sleep_avg < ceiling) { - /* - * Prevents user tasks from achieving best priority - * with one single large enough sleep. - */ - p->sleep_avg = ceiling; - /* - * Using INTERACTIVE_SLEEP() as a ceiling places a - * nice(0) task 1ms sleep away from promotion, and - * gives it 700ms to round-robin with no chance of - * being demoted. This is more than generous, so - * mark this sleep as non-interactive to prevent the - * on-runqueue bonus logic from intervening should - * this task not receive cpu immediately. - */ - p->sleep_type = SLEEP_NONINTERACTIVE; - } else { - /* - * Tasks waking from uninterruptible sleep are - * limited in their sleep_avg rise as they - * are likely to be waiting on I/O - */ - if (p->sleep_type == SLEEP_NONINTERACTIVE && p->mm) { - if (p->sleep_avg >= ceiling) - sleep_time = 0; - else if (p->sleep_avg + sleep_time >= - ceiling) { - p->sleep_avg = ceiling; - sleep_time = 0; - } - } - - /* - * This code gives a bonus to interactive tasks. - * - * The boost works by updating the 'average sleep time' - * value here, based on ->timestamp. The more time a - * task spends sleeping, the higher the average gets - - * and the higher the priority boost gets as well. - */ - p->sleep_avg += sleep_time; - - } - if (p->sleep_avg > NS_MAX_SLEEP_AVG) - p->sleep_avg = NS_MAX_SLEEP_AVG; - } - - return effective_prio(p); + * All tasks have quotas based on RR_INTERVAL. From nice 0 to 19 they are + * all equal to it and below zero they get progressively larger making their + * effective quota significantly larger (see slice() above). rt tasks all + * get RR_INTERVAL. + */ +static unsigned int rr_interval(struct task_struct *p) +{ + int nice = TASK_NICE(p); + + if (nice < 0 && !rt_task(p)) + return RR_INTERVAL * (20 - nice) / 20; + return RR_INTERVAL; } /* * activate_task - move a task to the runqueue and do priority recalculation - * - * Update all the scheduling statistics stuff. (sleep average - * calculation, priority modifiers, etc.) */ static void activate_task(struct task_struct *p, struct rq *rq, int local) { - unsigned long long now; - - if (rt_task(p)) - goto out; + unsigned long long now = sched_clock(); - now = sched_clock(); #ifdef CONFIG_SMP if (!local) { /* Compensate for drifting sched_clock */ @@ -967,32 +897,9 @@ static void activate_task(struct task_st (now - p->timestamp) >> 20); } - p->prio = recalc_task_prio(p, now); - - /* - * This checks to make sure it's not an uninterruptible task - * that is now waking up. - */ - if (p->sleep_type == SLEEP_NORMAL) { - /* - * Tasks which were woken up by interrupts (ie. hw events) - * are most likely of interactive nature. So we give them - * the credit of extending their sleep time to the period - * of time they spend on the runqueue, waiting for execution - * on a CPU, first time around: - */ - if (in_interrupt()) - p->sleep_type = SLEEP_INTERRUPTED; - else { - /* - * Normal first-time wakeups get a credit too for - * on-runqueue time, but it will be weighted down: - */ - p->sleep_type = SLEEP_INTERACTIVE; - } - } + p->quota = rr_interval(p); + p->prio = effective_prio(p); p->timestamp = now; -out: __activate_task(p, rq); } @@ -1002,8 +909,7 @@ out: static void deactivate_task(struct task_struct *p, struct rq *rq) { dec_nr_running(p, rq); - dequeue_task(p, p->array); - p->array = NULL; + dequeue_task(p, rq); } /* @@ -1085,7 +991,7 @@ migrate_task(struct task_struct *p, int * If the task is not on a runqueue (and not running), then * it is sufficient to simply update the task's cpu field. */ - if (!p->array && !task_running(rq, p)) { + if (!task_queued(p) && !task_running(rq, p)) { set_task_cpu(p, dest_cpu); return 0; } @@ -1116,7 +1022,7 @@ void wait_task_inactive(struct task_stru repeat: rq = task_rq_lock(p, &flags); /* Must be off runqueue entirely, not preempted. */ - if (unlikely(p->array || task_running(rq, p))) { + if (unlikely(task_queued(p) || task_running(rq, p))) { /* If it's preempted, we yield. It could be a while. */ preempted = !task_running(rq, p); task_rq_unlock(rq, &flags); @@ -1381,6 +1287,19 @@ static inline int wake_idle(int cpu, str } #endif +static inline int task_preempts_curr(struct task_struct *p, struct rq *rq) +{ + struct task_struct *curr = rq->curr; + + return p->queue == rq->active && TASK_PREEMPTS_CURR(p, curr); +} + +static inline void try_preempt(struct task_struct *p, struct rq *rq) +{ + if (task_preempts_curr(p, rq)) + resched_task(rq->curr); +} + /*** * try_to_wake_up - wake up a thread * @p: the to-be-woken-up thread @@ -1412,7 +1331,7 @@ static int try_to_wake_up(struct task_st if (!(old_state & state)) goto out; - if (p->array) + if (task_queued(p)) goto out_running; cpu = task_cpu(p); @@ -1505,7 +1424,7 @@ out_set_cpu: old_state = p->state; if (!(old_state & state)) goto out; - if (p->array) + if (task_queued(p)) goto out_running; this_cpu = smp_processor_id(); @@ -1514,26 +1433,10 @@ out_set_cpu: out_activate: #endif /* CONFIG_SMP */ - if (old_state == TASK_UNINTERRUPTIBLE) { + if (old_state == TASK_UNINTERRUPTIBLE) rq->nr_uninterruptible--; - /* - * Tasks on involuntary sleep don't earn - * sleep_avg beyond just interactive state. - */ - p->sleep_type = SLEEP_NONINTERACTIVE; - } else /* - * Tasks that have marked their sleep as noninteractive get - * woken up with their sleep average not weighted in an - * interactive way. - */ - if (old_state & TASK_NONINTERACTIVE) - p->sleep_type = SLEEP_NONINTERACTIVE; - - - activate_task(p, rq, cpu == this_cpu); - /* * Sync wakeups (i.e. those types of wakeups where the waker * has indicated that it will leave the CPU in short order) * don't trigger a preemption, if the woken up task will run on @@ -1541,10 +1444,9 @@ out_activate: * the waker guarantees that the freshly woken up task is going * to be considered on this CPU.) */ - if (!sync || cpu != this_cpu) { - if (TASK_PREEMPTS_CURR(p, rq)) - resched_task(rq->curr); - } + activate_task(p, rq, cpu == this_cpu); + if (!sync || cpu != this_cpu) + try_preempt(p, rq); success = 1; out_running: @@ -1592,10 +1494,9 @@ void fastcall sched_fork(struct task_str /* * Make sure we do not leak PI boosting priority to the child: */ - p->prio = current->normal_prio; + p->prio = current->static_prio; - INIT_LIST_HEAD(&p->run_list); - p->array = NULL; + INIT_LIST_HEAD(&p->prio_list); #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) if (unlikely(sched_info_on())) memset(&p->sched_info, 0, sizeof(p->sched_info)); @@ -1607,30 +1508,6 @@ void fastcall sched_fork(struct task_str /* Want to start with kernel preemption disabled. */ task_thread_info(p)->preempt_count = 1; #endif - /* - * Share the timeslice between parent and child, thus the - * total amount of pending timeslices in the system doesn't change, - * resulting in more scheduling fairness. - */ - local_irq_disable(); - p->time_slice = (current->time_slice + 1) >> 1; - /* - * The remainder of the first timeslice might be recovered by - * the parent if the child exits early enough. - */ - p->first_time_slice = 1; - current->time_slice >>= 1; - p->timestamp = sched_clock(); - if (unlikely(!current->time_slice)) { - /* - * This case is rare, it happens when the parent has only - * a single jiffy left from its timeslice. Taking the - * runqueue lock is not a problem. - */ - current->time_slice = 1; - task_running_tick(cpu_rq(cpu), current); - } - local_irq_enable(); put_cpu(); } @@ -1652,38 +1529,16 @@ void fastcall wake_up_new_task(struct ta this_cpu = smp_processor_id(); cpu = task_cpu(p); - /* - * We decrease the sleep average of forking parents - * and children as well, to keep max-interactive tasks - * from forking tasks that are max-interactive. The parent - * (current) is done further down, under its lock. - */ - p->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(p) * - CHILD_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); - - p->prio = effective_prio(p); - if (likely(cpu == this_cpu)) { + activate_task(p, rq, 1); if (!(clone_flags & CLONE_VM)) { /* * The VM isn't cloned, so we're in a good position to * do child-runs-first in anticipation of an exec. This * usually avoids a lot of COW overhead. */ - if (unlikely(!current->array)) - __activate_task(p, rq); - else { - p->prio = current->prio; - p->normal_prio = current->normal_prio; - list_add_tail(&p->run_list, ¤t->run_list); - p->array = current->array; - p->array->nr_active++; - inc_nr_running(p, rq); - } set_need_resched(); - } else - /* Run child last */ - __activate_task(p, rq); + } /* * We skip the following code due to cpu == this_cpu * @@ -1700,53 +1555,19 @@ void fastcall wake_up_new_task(struct ta */ p->timestamp = (p->timestamp - this_rq->most_recent_timestamp) + rq->most_recent_timestamp; - __activate_task(p, rq); - if (TASK_PREEMPTS_CURR(p, rq)) - resched_task(rq->curr); + activate_task(p, rq, 0); + try_preempt(p, rq); /* * Parent and child are on different CPUs, now get the - * parent runqueue to update the parent's ->sleep_avg: + * parent runqueue to update the parent's ->flags: */ task_rq_unlock(rq, &flags); this_rq = task_rq_lock(current, &flags); } - current->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(current) * - PARENT_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); task_rq_unlock(this_rq, &flags); } -/* - * Potentially available exiting-child timeslices are - * retrieved here - this way the parent does not get - * penalized for creating too many threads. - * - * (this cannot be used to 'generate' timeslices - * artificially, because any timeslice recovered here - * was given away by the parent in the first place.) - */ -void fastcall sched_exit(struct task_struct *p) -{ - unsigned long flags; - struct rq *rq; - - /* - * If the child was a (relative-) CPU hog then decrease - * the sleep_avg of the parent as well. - */ - rq = task_rq_lock(p->parent, &flags); - if (p->first_time_slice && task_cpu(p) == task_cpu(p->parent)) { - p->parent->time_slice += p->time_slice; - if (unlikely(p->parent->time_slice > task_timeslice(p))) - p->parent->time_slice = task_timeslice(p); - } - if (p->sleep_avg < p->parent->sleep_avg) - p->parent->sleep_avg = p->parent->sleep_avg / - (EXIT_WEIGHT + 1) * EXIT_WEIGHT + p->sleep_avg / - (EXIT_WEIGHT + 1); - task_rq_unlock(rq, &flags); -} - /** * prepare_task_switch - prepare to switch tasks * @rq: the runqueue preparing to switch @@ -1949,7 +1770,7 @@ unsigned long nr_active(void) static inline int task_hot(struct task_struct *p, unsigned long long now, struct sched_domain *sd) { - return (long long)(now - p->last_ran) < (long long)sd->cache_hot_time; + return (long long)(now - p->timestamp) < (long long)sd->cache_hot_time; } /* @@ -2068,23 +1889,28 @@ void sched_exec(void) * pull_task - move a task from a remote runqueue to the local runqueue. * Both runqueues must be locked. */ -static void pull_task(struct rq *src_rq, struct prio_array *src_array, - struct task_struct *p, struct rq *this_rq, - struct prio_array *this_array, int this_cpu) +static void pull_task(struct rq *src_rq, struct task_struct *p, + struct rq *this_rq, int this_cpu) { - dequeue_task(p, src_array); + dequeue_task(p, src_rq); dec_nr_running(p, src_rq); set_task_cpu(p, this_cpu); inc_nr_running(p, this_rq); - enqueue_task(p, this_array); + + /* + * If this task has already been running on src_rq this priority + * cycle, make the new runqueue think it has been on its cycle + */ + if (p->last_run == src_rq->prio_rotation) + p->last_run = this_rq->prio_rotation; + enqueue_task(p, this_rq); p->timestamp = (p->timestamp - src_rq->most_recent_timestamp) + this_rq->most_recent_timestamp; /* * Note that idle threads have a prio of MAX_PRIO, for this test * to be always true for them. */ - if (TASK_PREEMPTS_CURR(p, this_rq)) - resched_task(this_rq->curr); + try_preempt(p, this_rq); } /* @@ -2127,8 +1953,6 @@ int can_migrate_task(struct task_struct return 1; } -#define rq_best_prio(rq) min((rq)->curr->prio, (rq)->best_expired_prio) - /* * move_tasks tries to move up to max_nr_move tasks and max_load_move weighted * load from busiest to this_rq, as part of a balancing operation within @@ -2143,7 +1967,6 @@ static int move_tasks(struct rq *this_rq { int idx, pulled = 0, pinned = 0, this_best_prio, best_prio, best_prio_seen, skip_for_load; - struct prio_array *array, *dst_array; struct list_head *head, *curr; struct task_struct *tmp; long rem_load_move; @@ -2153,8 +1976,8 @@ static int move_tasks(struct rq *this_rq rem_load_move = max_load_move; pinned = 1; - this_best_prio = rq_best_prio(this_rq); - best_prio = rq_best_prio(busiest); + this_best_prio = this_rq->curr->queued_prio; + best_prio = busiest->curr->queued_prio; /* * Enable handling of the case where there is more than one task * with the best priority. If the current running task is one @@ -2162,43 +1985,26 @@ static int move_tasks(struct rq *this_rq * and therefore it's safe to override the skip (based on load) of * any task we find with that prio. */ - best_prio_seen = best_prio == busiest->curr->prio; + best_prio_seen = best_prio == busiest->curr->queued_prio; - /* - * We first consider expired tasks. Those will likely not be - * executed in the near future, and they are most likely to - * be cache-cold, thus switching CPUs has the least effect - * on them. - */ - if (busiest->expired->nr_active) { - array = busiest->expired; - dst_array = this_rq->expired; - } else { - array = busiest->active; - dst_array = this_rq->active; - } - -new_array: /* Start searching at priority 0: */ idx = 0; skip_bitmap: if (!idx) - idx = sched_find_first_bit(array->bitmap); + idx = sched_find_first_bit(busiest->active->bitmap); else - idx = find_next_bit(array->bitmap, MAX_PRIO, idx); - if (idx >= MAX_PRIO) { - if (array == busiest->expired && busiest->active->nr_active) { - array = busiest->active; - dst_array = this_rq->active; - goto new_array; - } + idx = find_next_bit(busiest->active->bitmap, MAX_PRIO, idx); + if (idx >= MAX_PRIO) goto out; + if (unlikely(list_empty(busiest->active->queue + idx))) { + __clear_bit(idx, busiest->active->bitmap); + goto skip_bitmap; } - head = array->queue + idx; + head = busiest->active->queue + idx; curr = head->prev; skip_queue: - tmp = list_entry(curr, struct task_struct, run_list); + tmp = list_entry(curr, struct task_struct, prio_list); curr = curr->prev; @@ -2220,7 +2026,7 @@ skip_queue: goto skip_bitmap; } - pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu); + pull_task(busiest, tmp, this_rq, this_cpu); pulled++; rem_load_move -= tmp->load_weight; @@ -3015,8 +2821,8 @@ EXPORT_PER_CPU_SYMBOL(kstat); static inline void update_cpu_clock(struct task_struct *p, struct rq *rq, unsigned long long now) { - p->sched_time += now - p->last_ran; - p->last_ran = rq->most_recent_timestamp = now; + p->sched_time += now - p->timestamp; + p->timestamp = rq->most_recent_timestamp = now; } /* @@ -3029,34 +2835,13 @@ unsigned long long current_sched_time(co unsigned long flags; local_irq_save(flags); - ns = p->sched_time + sched_clock() - p->last_ran; + ns = p->sched_time + sched_clock() - p->timestamp; local_irq_restore(flags); return ns; } /* - * We place interactive tasks back into the active array, if possible. - * - * To guarantee that this does not starve expired tasks we ignore the - * interactivity of a task if the first expired task had to wait more - * than a 'reasonable' amount of time. This deadline timeout is - * load-dependent, as the frequency of array switched decreases with - * increasing number of running tasks. We also ignore the interactivity - * if a better static_prio task has expired: - */ -static inline int expired_starving(struct rq *rq) -{ - if (rq->curr->static_prio > rq->best_expired_prio) - return 1; - if (!STARVATION_LIMIT || !rq->expired_timestamp) - return 0; - if (jiffies - rq->expired_timestamp > STARVATION_LIMIT * rq->nr_running) - return 1; - return 0; -} - -/* * Account user cpu time to a process. * @p: the process that the cpu time gets accounted to * @hardirq_offset: the offset to subtract from hardirq_count() @@ -3104,7 +2889,6 @@ void account_system_time(struct task_str cpustat->iowait = cputime64_add(cpustat->iowait, tmp); else cpustat->idle = cputime64_add(cpustat->idle, tmp); - /* Account for system time used */ acct_update_integrals(p); } @@ -3129,87 +2913,126 @@ void account_steal_time(struct task_stru cpustat->steal = cputime64_add(cpustat->steal, tmp); } -static void task_running_tick(struct rq *rq, struct task_struct *p) +/* + * The task has used up its quota of running in this prio_level so it must be + * dropped a priority level. MAX_PRIO - 1 is a special case. + */ +static void task_expired_entitlement(struct rq *rq, struct task_struct *p) { - if (p->array != rq->active) { - /* Task has expired but was not scheduled yet */ - set_tsk_need_resched(p); + struct queues *queue = rq->active; + int queue_prio; + + set_tsk_need_resched(p); + if (rt_task(p)) { + p->tick_entitlement = p->quota; return; } - spin_lock(&rq->lock); - /* - * The task was 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 up its - * timeslice. This makes it possible for interactive tasks - * to use up their timeslices at their highest priority levels. - */ - if (rt_task(p)) { + queue_prio = current_queue_prio(p, rq, &queue); + requeue_task(p, rq, queue_prio, queue); +} + +/* + * A major priority rotation occurs when all priority quotas for this queue + * have been exhausted. + */ +static inline void major_prio_rotation(struct rq *rq) +{ + struct queues *new_queue = rq->next_queue; + + rq->next_queue = rq->active; + rq->active = new_queue; + rq->prio_rotation++; + bitmap_copy(rq->active->bitmap, rq->static_bitmap, MAX_PRIO); +} + +/* + * This is the heart of the priority management. + * + * We have used up the quota allocated to this priority level so we rotate + * the prio_level of the runqueue to the next lowest priority. We merge any + * remaining tasks at this level current_queue with the next priority and + * reset this level's queue. MAX_PRIO - 1 is a special case where we perform + * a major rotation. + */ +static inline void rotate_runqueue_priority(struct rq *rq) +{ + struct queues *q = rq->active; + int new_prio_level; + + if (rq->prio_level > MAX_PRIO - 2) { + /* Major rotation required */ + struct queues *new_queue = rq->next_queue; + + __clear_bit(MAX_PRIO - 1, q->bitmap); /* - * RR tasks need a special form of timeslice management. - * FIFO tasks have no timeslices. + * The static_bitmap gives us the highest p->prio task that + * is queued. This value is used as the queued_prio after + * the major rotation and all tasks remaining on this + * active queue are moved there. This means tasks can end + * up on a queued_prio better than their p->prio. */ - if ((p->policy == SCHED_RR) && !--p->time_slice) { - p->time_slice = task_timeslice(p); - p->first_time_slice = 0; - set_tsk_need_resched(p); - - /* put it at the end of the queue: */ - requeue_task(p, rq->active); + new_prio_level = find_next_bit(rq->static_bitmap, MAX_PRIO, + MAX_RT_PRIO); + if (unlikely(new_prio_level == MAX_PRIO)) { + /* + * Very rare. Happens when a task gets queued on the + * next_queue and doesn't get to run till the queue + * after. + */ + new_prio_level = MAX_PRIO - 1; + __set_bit(new_prio_level, q->bitmap); + } + if (!list_empty(q->queue + rq->prio_level)) { + list_splice_tail_init(q->queue + rq->prio_level, + new_queue->queue + new_prio_level); + } + memset(q->prio_quota, 0, ARRAY_SIZE(q->prio_quota)); + major_prio_rotation(rq); + } else { + /* Minor rotation */ + new_prio_level = rq->prio_level + 1; + __clear_bit(rq->prio_level, q->bitmap); + if (!list_empty(q->queue + rq->prio_level)) { + list_splice_tail_init(q->queue + rq->prio_level, + q->queue + new_prio_level); + __set_bit(new_prio_level, q->bitmap); } - goto out_unlock; } - if (!--p->time_slice) { - dequeue_task(p, rq->active); + rq->prio_level = new_prio_level; +} + +static void task_running_tick(struct rq *rq, struct task_struct *p) +{ + if (unlikely(!task_queued(p))) { + /* Task has expired but was not scheduled yet */ set_tsk_need_resched(p); - p->prio = effective_prio(p); - p->time_slice = task_timeslice(p); - p->first_time_slice = 0; - - if (!rq->expired_timestamp) - rq->expired_timestamp = jiffies; - if (!TASK_INTERACTIVE(p) || expired_starving(rq)) { - enqueue_task(p, rq->expired); - if (p->static_prio < rq->best_expired_prio) - rq->best_expired_prio = p->static_prio; - } else - enqueue_task(p, rq->active); - } else { - /* - * Prevent a too long timeslice allowing a task to monopolize - * the CPU. We do this by splitting up the timeslice into - * smaller pieces. - * - * Note: this does not mean the task's timeslices expire or - * get lost in any way, they just might be preempted by - * another task of equal priority. (one with higher - * priority would have preempted this task already.) We - * requeue this task to the end of the list on this priority - * level, which is in essence a round-robin of tasks with - * equal priority. - * - * This only applies to tasks in the interactive - * delta range with at least TIMESLICE_GRANULARITY to requeue. - */ - if (TASK_INTERACTIVE(p) && !((task_timeslice(p) - - p->time_slice) % TIMESLICE_GRANULARITY(p)) && - (p->time_slice >= TIMESLICE_GRANULARITY(p)) && - (p->array == rq->active)) { + return; + } + /* SCHED_FIFO tasks never run out of timeslice. */ + if (unlikely(p->policy == SCHED_FIFO)) + return; - requeue_task(p, rq->active); - set_tsk_need_resched(p); - } + spin_lock(&rq->lock); + /* + * Accounting is performed by both the task and the runqueue. This + * allows frequently sleeping tasks to get their proper quota of + * cpu as the runqueue will have their quota still available at + * the appropriate priority level. It also means frequently waking + * tasks that might miss the scheduler_tick() will get forced down + * priority regardless. + */ + if (!--p->tick_entitlement) + task_expired_entitlement(rq, p); + if (!rt_task(p) && !--rq->active->prio_quota[rq->prio_level]) { + rotate_runqueue_priority(rq); + set_tsk_need_resched(p); } -out_unlock: spin_unlock(&rq->lock); } /* * This function gets called by the timer code, with HZ frequency. * We call it with interrupts disabled. - * - * It also gets called by the fork code, when changing the parent's - * timeslices. */ void scheduler_tick(void) { @@ -3271,15 +3094,21 @@ static void wake_sleeping_dependent(int } } +static inline unsigned long remaining_slice(struct task_struct *p) +{ + return p->quota * (MAX_PRIO - 1 - p->queued_prio) + + p->tick_entitlement; +} + /* * number of 'lost' timeslices this task wont be able to fully - * utilize, if another task runs on a sibling. This models the + * utilise, if another task runs on a sibling. This models the * slowdown effect of other tasks running on siblings: */ static inline unsigned long smt_slice(struct task_struct *p, struct sched_domain *sd) { - return p->time_slice * (100 - sd->per_cpu_gain) / 100; + return remaining_slice(p) * (100 - sd->per_cpu_gain) / 100; } /* @@ -3342,8 +3171,8 @@ dependent_sleeper(int this_cpu, struct r ret = 1; } else { if (smt_curr->static_prio < p->static_prio && - !TASK_PREEMPTS_CURR(p, smt_rq) && - smt_slice(smt_curr, sd) > task_timeslice(p)) + !task_preempts_curr(p, smt_rq) && + smt_slice(smt_curr, sd) > slice(p)) ret = 1; } unlock: @@ -3400,10 +3229,63 @@ EXPORT_SYMBOL(sub_preempt_count); #endif -static inline int interactive_sleep(enum sleep_type sleep_type) +/* + * next_dynamic_task finds the next suitable dynamic task. Usually the + * selection is the next available bit. However it is possible in the worst + * case scenario that we need to check 40 priority levels and do one + * major priority rotation before we find it. The chance of this happening + * is extremely small. We do want all of this inlined since it is in + * the fast path though. + */ +static inline struct task_struct *next_dynamic_task(struct rq *rq, int idx) { - return (sleep_type == SLEEP_INTERACTIVE || - sleep_type == SLEEP_INTERRUPTED); + struct task_struct *next; + struct list_head *queue; + struct queues *q = rq->active; + +retry: + idx = find_next_bit(q->bitmap, MAX_PRIO, idx); + if (idx == MAX_PRIO) { + major_prio_rotation(rq); + q = rq->active; + idx = MAX_RT_PRIO; + goto retry; + } + if (list_empty(q->queue + idx)) { + /* + * This can happen because they are removed lazily after a + * rotate_runqueue_priority for O(1) promotion. + */ + __clear_bit(idx, q->bitmap); + idx++; + goto retry; + } + + queue = q->queue + idx; + next = list_entry(queue->next, struct task_struct, prio_list); + /* + * When the task is chosen it is checked to see if its quota has been + * added to this runqueue level which is only performed once per + * level per major rotation for each running task. + */ + if (next->last_run != rq->prio_rotation) { + /* Task has moved during major rotation */ + task_new_queue(next, rq); + set_task_entitlement(next, idx, q); + rq->active->prio_quota[idx] += next->quota; + } else if (!test_bit(USER_PRIO(idx), next->bitmap)) { + /* Task has moved during minor rotation */ + set_task_entitlement(next, idx, q); + rq->active->prio_quota[idx] += next->quota; + } + + if (unlikely(!rq->active->prio_quota[idx])) { + rotate_runqueue_priority(rq); + idx = MAX_RT_PRIO; + goto retry; + } + rq->prio_level = idx; + return next; } /* @@ -3412,13 +3294,11 @@ static inline int interactive_sleep(enum asmlinkage void __sched schedule(void) { struct task_struct *prev, *next; - struct prio_array *array; struct list_head *queue; unsigned long long now; - unsigned long run_time; - int cpu, idx, new_prio; long *switch_count; struct rq *rq; + int cpu, idx; /* * Test if we are atomic. Since do_exit() needs to call into @@ -3454,18 +3334,6 @@ need_resched_nonpreemptible: schedstat_inc(rq, sched_cnt); now = sched_clock(); - if (likely((long long)(now - prev->timestamp) < NS_MAX_SLEEP_AVG)) { - run_time = now - prev->timestamp; - if (unlikely((long long)(now - prev->timestamp) < 0)) - run_time = 0; - } else - run_time = NS_MAX_SLEEP_AVG; - - /* - * Tasks charged proportionately less run_time at high sleep_avg to - * delay them losing their interactive status - */ - run_time /= (CURRENT_BONUS(prev) ? : 1); spin_lock_irq(&rq->lock); @@ -3487,66 +3355,37 @@ need_resched_nonpreemptible: idle_balance(cpu, rq); if (!rq->nr_running) { next = rq->idle; - rq->expired_timestamp = 0; wake_sleeping_dependent(cpu); goto switch_tasks; } } - array = rq->active; - if (unlikely(!array->nr_active)) { - /* - * Switch the active and expired arrays. - */ - schedstat_inc(rq, sched_switch); - rq->active = rq->expired; - rq->expired = array; - array = rq->active; - rq->expired_timestamp = 0; - rq->best_expired_prio = MAX_PRIO; + idx = sched_find_first_bit(rq->active->bitmap); + if (!rt_prio(idx)) + next = next_dynamic_task(rq, idx); + else { + queue = rq->active->queue + idx; + next = list_entry(queue->next, struct task_struct, prio_list); } - idx = sched_find_first_bit(array->bitmap); - queue = array->queue + idx; - next = list_entry(queue->next, struct task_struct, run_list); - - if (!rt_task(next) && interactive_sleep(next->sleep_type)) { - unsigned long long delta = now - next->timestamp; - if (unlikely((long long)(now - next->timestamp) < 0)) - delta = 0; - - if (next->sleep_type == SLEEP_INTERACTIVE) - delta = delta * (ON_RUNQUEUE_WEIGHT * 128 / 100) / 128; - - array = next->array; - new_prio = recalc_task_prio(next, next->timestamp + delta); - - if (unlikely(next->prio != new_prio)) { - dequeue_task(next, array); - next->prio = new_prio; - enqueue_task(next, array); - } - } - next->sleep_type = SLEEP_NORMAL; if (dependent_sleeper(cpu, rq, next)) next = rq->idle; + else { + prefetch(next); + prefetch_stack(next); + } switch_tasks: if (next == rq->idle) schedstat_inc(rq, sched_goidle); - prefetch(next); - prefetch_stack(next); + prev->timestamp = now; clear_tsk_need_resched(prev); rcu_qsctr_inc(task_cpu(prev)); update_cpu_clock(prev, rq, now); - prev->sleep_avg -= run_time; - if ((long)prev->sleep_avg <= 0) - prev->sleep_avg = 0; - prev->timestamp = prev->last_ran = now; - sched_info_switch(prev, next); if (likely(prev != next)) { + next->queue = rq->active; next->timestamp = now; rq->nr_switches++; rq->curr = next; @@ -3972,35 +3811,27 @@ EXPORT_SYMBOL(sleep_on_timeout); * @prio: prio value (kernel-internal form) * * This function changes the 'effective' priority of a task. It does - * not touch ->normal_prio like __setscheduler(). + * not touch ->static_prio like __setscheduler(). * * Used by the rt_mutex code to implement priority inheritance logic. */ void rt_mutex_setprio(struct task_struct *p, int prio) { - struct prio_array *array; unsigned long flags; + int queued, oldprio; struct rq *rq; - int oldprio; BUG_ON(prio < 0 || prio > MAX_PRIO); rq = task_rq_lock(p, &flags); oldprio = p->prio; - array = p->array; - if (array) - dequeue_task(p, array); + if ((queued = task_queued(p))) + dequeue_task(p, rq); p->prio = prio; - if (array) { - /* - * If changing to an RT priority then queue it - * in the active array! - */ - if (rt_task(p)) - array = rq->active; - enqueue_task(p, array); + if (queued) { + enqueue_task(p, rq); /* * Reschedule if we are currently running on this runqueue and * our priority decreased, or if we are not currently running on @@ -4009,8 +3840,8 @@ void rt_mutex_setprio(struct task_struct if (task_running(rq, p)) { if (p->prio > oldprio) resched_task(rq->curr); - } else if (TASK_PREEMPTS_CURR(p, rq)) - resched_task(rq->curr); + } else + try_preempt(p, rq); } task_rq_unlock(rq, &flags); } @@ -4019,8 +3850,7 @@ void rt_mutex_setprio(struct task_struct void set_user_nice(struct task_struct *p, long nice) { - struct prio_array *array; - int old_prio, delta; + int queued, old_prio,delta; unsigned long flags; struct rq *rq; @@ -4041,9 +3871,8 @@ void set_user_nice(struct task_struct *p p->static_prio = NICE_TO_PRIO(nice); goto out_unlock; } - array = p->array; - if (array) { - dequeue_task(p, array); + if ((queued = task_queued(p))) { + dequeue_task(p, rq); dec_raw_weighted_load(rq, p); } @@ -4053,8 +3882,8 @@ void set_user_nice(struct task_struct *p p->prio = effective_prio(p); delta = p->prio - old_prio; - if (array) { - enqueue_task(p, array); + if (queued) { + enqueue_task(p, rq); inc_raw_weighted_load(rq, p); /* * If the task increased its priority or is running and @@ -4130,11 +3959,15 @@ asmlinkage long sys_nice(int increment) * * This is the priority value as seen by users in /proc. * RT tasks are offset by -200. Normal tasks are centered - * around 0, value goes from -16 to +15. + * around 0, value goes from 0 to +19. */ int task_prio(const struct task_struct *p) { - return p->prio - MAX_RT_PRIO; + /* + * p->queued_prio is used purely for displaying to userspace the + * dynamic priority when the task was queued. + */ + return p->queued_prio - MAX_RT_PRIO; } /** @@ -4177,18 +4010,12 @@ static inline struct task_struct *find_p /* Actually do priority change: must hold rq lock. */ static void __setscheduler(struct task_struct *p, int policy, int prio) { - BUG_ON(p->array); + BUG_ON(task_queued(p)); p->policy = policy; p->rt_priority = prio; - p->normal_prio = normal_prio(p); /* we are holding p->pi_lock already */ p->prio = rt_mutex_getprio(p); - /* - * SCHED_BATCH tasks are treated as perpetual CPU hogs: - */ - if (policy == SCHED_BATCH) - p->sleep_avg = 0; set_load_weight(p); } @@ -4204,8 +4031,7 @@ static void __setscheduler(struct task_s int sched_setscheduler(struct task_struct *p, int policy, struct sched_param *param) { - int retval, oldprio, oldpolicy = -1; - struct prio_array *array; + int queued, retval, oldprio, oldpolicy = -1; unsigned long flags; struct rq *rq; @@ -4279,12 +4105,11 @@ recheck: spin_unlock_irqrestore(&p->pi_lock, flags); goto recheck; } - array = p->array; - if (array) + if ((queued = task_queued(p))) deactivate_task(p, rq); oldprio = p->prio; __setscheduler(p, policy, param->sched_priority); - if (array) { + if (queued) { __activate_task(p, rq); /* * Reschedule if we are currently running on this runqueue and @@ -4294,8 +4119,8 @@ recheck: if (task_running(rq, p)) { if (p->prio > oldprio) resched_task(rq->curr); - } else if (TASK_PREEMPTS_CURR(p, rq)) - resched_task(rq->curr); + } else + try_preempt(p, rq); } __task_rq_unlock(rq); spin_unlock_irqrestore(&p->pi_lock, flags); @@ -4567,41 +4392,21 @@ asmlinkage long sys_sched_getaffinity(pi /** * sys_sched_yield - yield the current processor to other threads. * - * this function yields the current CPU by moving the calling thread - * to the expired array. If there are no other threads running on this - * CPU then this function will return. + * This function yields the current CPU by dropping the priority of current + * to the lowest priority. */ asmlinkage long sys_sched_yield(void) { struct rq *rq = this_rq_lock(); - struct prio_array *array = current->array, *target = rq->expired; + struct task_struct *p = current; + struct queues *queue = rq->next_queue; schedstat_inc(rq, yld_cnt); - /* - * We implement yielding by moving the task into the expired - * queue. - * - * (special rule: RT tasks will just roundrobin in the active - * array.) - */ - if (rt_task(current)) - target = rq->active; - if (array->nr_active == 1) { - schedstat_inc(rq, yld_act_empty); - if (!rq->expired->nr_active) - schedstat_inc(rq, yld_both_empty); - } else if (!rq->expired->nr_active) - schedstat_inc(rq, yld_exp_empty); - - if (array != target) { - dequeue_task(current, array); - enqueue_task(current, target); - } else - /* - * requeue_task is cheaper so perform that if possible. - */ - requeue_task(current, array); + schedstat_inc(rq, yld_cnt); + if (rt_task(p)) + queue = rq->active; + requeue_task(p, rq, p->prio, queue); /* * Since we are going to call schedule() anyway, there's @@ -4812,7 +4617,7 @@ long sys_sched_rr_get_interval(pid_t pid goto out_unlock; jiffies_to_timespec(p->policy == SCHED_FIFO ? - 0 : task_timeslice(p), &t); + 0 : slice(p), &t); read_unlock(&tasklist_lock); retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; out_nounlock: @@ -4940,11 +4745,12 @@ void __cpuinit init_idle(struct task_str struct rq *rq = cpu_rq(cpu); unsigned long flags; + bitmap_zero(idle->bitmap, PRIO_RANGE + 1); idle->timestamp = sched_clock(); - idle->sleep_avg = 0; - idle->array = NULL; - idle->prio = idle->normal_prio = MAX_PRIO; + idle->prio = MAX_PRIO; idle->state = TASK_RUNNING; + idle->queued_prio = MAX_PRIO; + idle->queue = rq->active; idle->cpus_allowed = cpumask_of_cpu(cpu); set_task_cpu(idle, cpu); @@ -5062,7 +4868,7 @@ static int __migrate_task(struct task_st goto out; set_task_cpu(p, dest_cpu); - if (p->array) { + if (task_queued(p)) { /* * Sync timestamp with rq_dest's before activating. * The same thing could be achieved by doing this step @@ -5073,8 +4879,7 @@ static int __migrate_task(struct task_st + rq_dest->most_recent_timestamp; deactivate_task(p, rq_src); __activate_task(p, rq_dest); - if (TASK_PREEMPTS_CURR(p, rq_dest)) - resched_task(rq_dest->curr); + try_preempt(p, rq_dest); } ret = 1; out: @@ -5303,11 +5108,11 @@ static void migrate_dead_tasks(unsigned for (arr = 0; arr < 2; arr++) { for (i = 0; i < MAX_PRIO; i++) { - struct list_head *list = &rq->arrays[arr].queue[i]; + struct list_head *list = &rq->queues[arr].queue[i]; while (!list_empty(list)) migrate_dead(dead_cpu, list_entry(list->next, - struct task_struct, run_list)); + struct task_struct, prio_list)); } } } @@ -6894,19 +6699,20 @@ int in_sched_functions(unsigned long add void __init sched_init(void) { - int i, j, k; + int i; for_each_possible_cpu(i) { - struct prio_array *array; struct rq *rq; + int j; rq = cpu_rq(i); spin_lock_init(&rq->lock); lockdep_set_class(&rq->lock, &rq->rq_lock_key); rq->nr_running = 0; - rq->active = rq->arrays; - rq->expired = rq->arrays + 1; - rq->best_expired_prio = MAX_PRIO; + rq->prio_rotation = 0; + rq->prio_level = MAX_RT_PRIO; + rq->active = rq->queues; + rq->next_queue = rq->queues + 1; #ifdef CONFIG_SMP rq->sd = NULL; @@ -6921,14 +6727,20 @@ void __init sched_init(void) atomic_set(&rq->nr_iowait, 0); for (j = 0; j < 2; j++) { - array = rq->arrays + j; + struct queues *queue; + int k; + + queue = rq->queues + j; for (k = 0; k < MAX_PRIO; k++) { - INIT_LIST_HEAD(array->queue + k); - __clear_bit(k, array->bitmap); + INIT_LIST_HEAD(queue->queue + k); + queue->prio_quota[k] = RR_INTERVAL; } - // delimiter for bitsearch - __set_bit(MAX_PRIO, array->bitmap); + bitmap_zero(queue->bitmap, MAX_PRIO); + /* delimiter for bitsearch */ + __set_bit(MAX_PRIO, queue->bitmap); } + bitmap_zero(rq->static_bitmap, MAX_PRIO); + __set_bit(MAX_PRIO, rq->static_bitmap); } set_load_weight(&init_task); @@ -6984,10 +6796,10 @@ EXPORT_SYMBOL(__might_sleep); #ifdef CONFIG_MAGIC_SYSRQ void normalize_rt_tasks(void) { - struct prio_array *array; struct task_struct *p; unsigned long flags; struct rq *rq; + int queued; read_lock_irq(&tasklist_lock); for_each_process(p) { @@ -6997,11 +6809,10 @@ void normalize_rt_tasks(void) spin_lock_irqsave(&p->pi_lock, flags); rq = __task_rq_lock(p); - array = p->array; - if (array) + if ((queued = task_queued(p))) deactivate_task(p, task_rq(p)); __setscheduler(p, SCHED_NORMAL, 0); - if (array) { + if (queued) { __activate_task(p, task_rq(p)); resched_task(rq->curr); } Index: linux-2.6.20-rsdl/kernel/rtmutex.c =================================================================== --- linux-2.6.20-rsdl.orig/kernel/rtmutex.c 2007-02-06 11:07:09.000000000 +1100 +++ linux-2.6.20-rsdl/kernel/rtmutex.c 2007-02-06 11:19:02.000000000 +1100 @@ -106,16 +106,16 @@ static inline void mark_rt_mutex_waiters /* * Calculate task priority from the waiter list priority * - * Return task->normal_prio when the waiter list is empty or when + * Return task->static_prio when the waiter list is empty or when * the waiter is not allowed to do priority boosting */ int rt_mutex_getprio(struct task_struct *task) { if (likely(!task_has_pi_waiters(task))) - return task->normal_prio; + return task->static_prio; return min(task_top_pi_waiter(task)->pi_list_entry.prio, - task->normal_prio); + task->static_prio); } /* @@ -335,7 +335,7 @@ static inline int try_to_steal_lock(stru * where current is boosted because it holds another * lock and gets unboosted because the booster is * interrupted, so we would delay a waiter with higher - * priority as current->normal_prio. + * priority as current->static_prio. * * Note: in the rare case of a SCHED_OTHER task changing * its priority and thus stealing the lock, next->task @@ -497,7 +497,7 @@ static void wakeup_next_waiter(struct rt * Clear the pi_blocked_on variable and enqueue a possible * waiter into the pi_waiters list of the pending owner. This * prevents that in case the pending owner gets unboosted a - * waiter with higher priority than pending-owner->normal_prio + * waiter with higher priority than pending-owner->static_prio * is blocked on the unboosted (pending) owner. */ spin_lock_irqsave(&pendowner->pi_lock, flags); Index: linux-2.6.20-rsdl/include/linux/init_task.h =================================================================== --- linux-2.6.20-rsdl.orig/include/linux/init_task.h 2007-02-06 11:07:09.000000000 +1100 +++ linux-2.6.20-rsdl/include/linux/init_task.h 2007-02-06 11:19:02.000000000 +1100 @@ -101,14 +101,16 @@ extern struct group_info init_groups; .lock_depth = -1, \ .prio = MAX_PRIO-20, \ .static_prio = MAX_PRIO-20, \ - .normal_prio = MAX_PRIO-20, \ + .queued_prio = MAX_PRIO-20, \ + .last_run = 0, \ .policy = SCHED_NORMAL, \ .cpus_allowed = CPU_MASK_ALL, \ .mm = NULL, \ .active_mm = &init_mm, \ - .run_list = LIST_HEAD_INIT(tsk.run_list), \ + .prio_list = LIST_HEAD_INIT(tsk.prio_list), \ .ioprio = 0, \ - .time_slice = HZ, \ + .quota = HZ, \ + .tick_entitlement = HZ, \ .tasks = LIST_HEAD_INIT(tsk.tasks), \ .ptrace_children= LIST_HEAD_INIT(tsk.ptrace_children), \ .ptrace_list = LIST_HEAD_INIT(tsk.ptrace_list), \ Index: linux-2.6.20-rsdl/mm/oom_kill.c =================================================================== --- linux-2.6.20-rsdl.orig/mm/oom_kill.c 2007-02-06 11:07:09.000000000 +1100 +++ linux-2.6.20-rsdl/mm/oom_kill.c 2007-02-06 11:19:02.000000000 +1100 @@ -291,7 +291,7 @@ static void __oom_kill_task(struct task_ * all the memory it needs. That way it should be able to * exit() and clear out its resources quickly... */ - p->time_slice = HZ; + p->tick_entitlement = HZ; set_tsk_thread_flag(p, TIF_MEMDIE); force_sig(SIGKILL, p); Index: linux-2.6.20-rsdl/include/linux/list.h =================================================================== --- linux-2.6.20-rsdl.orig/include/linux/list.h 2007-02-06 11:07:09.000000000 +1100 +++ linux-2.6.20-rsdl/include/linux/list.h 2007-02-06 11:19:02.000000000 +1100 @@ -332,6 +332,20 @@ static inline void __list_splice(struct at->prev = last; } +static inline void __list_splice_tail(struct list_head *list, + struct list_head *head) +{ + struct list_head *first = list->next; + struct list_head *last = list->prev; + struct list_head *at = head->prev; + + first->prev = at; + at->next = first; + + last->next = head; + head->prev = last; +} + /** * list_splice - join two lists * @list: the new list to add. @@ -343,6 +357,12 @@ static inline void list_splice(struct li __list_splice(list, head); } +static inline void list_splice_tail(struct list_head *list, struct list_head *head) +{ + if (!list_empty(list)) + __list_splice_tail(list, head); +} + /** * list_splice_init - join two lists and reinitialise the emptied list. * @list: the new list to add. @@ -359,6 +379,15 @@ static inline void list_splice_init(stru } } +static inline void list_splice_tail_init(struct list_head *list, + struct list_head *head) +{ + if (!list_empty(list)) { + __list_splice_tail(list, head); + INIT_LIST_HEAD(list); + } +} + /** * list_entry - get the struct for this entry * @ptr: the &struct list_head pointer.