1 /* include/linux/ckrm_sched.h - Supports CKRM scheduling
3 * Copyright (C) Haoqiang Zheng, IBM Corp. 2004
4 * Copyright (C) Hubertus Franke, IBM Corp. 2004
6 * Latest version, more details at http://ckrm.sf.net
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
18 #include <linux/sched.h>
19 #include <linux/ckrm_rc.h>
20 #include <linux/ckrm_classqueue.h>
22 #define BITMAP_SIZE ((((MAX_PRIO+1+7)/8)+sizeof(long)-1)/sizeof(long))
25 unsigned int nr_active;
26 unsigned long bitmap[BITMAP_SIZE];
27 struct list_head queue[MAX_PRIO];
30 #ifdef CONFIG_CKRM_CPU_SCHEDULE
31 #define rq_active(p,rq) (get_task_lrq(p)->active)
32 #define rq_expired(p,rq) (get_task_lrq(p)->expired)
33 int __init init_ckrm_sched_res(void);
35 #define rq_active(p,rq) (rq->active)
36 #define rq_expired(p,rq) (rq->expired)
37 static inline void init_ckrm_sched_res(void) {}
38 static inline int ckrm_cpu_monitor_init(void) {return 0;}
41 #ifdef CONFIG_CKRM_CPU_SCHEDULE
42 struct ckrm_runqueue {
43 cq_node_t classqueue_linkobj; /*links in classqueue */
44 struct ckrm_cpu_class *cpu_class; // class it belongs to
45 struct classqueue_struct *classqueue; // classqueue it belongs tow
46 unsigned long long uncounted_ns;
48 prio_array_t *active, *expired, arrays[2];
50 set to 0 on init, become null or array switch
51 set to jiffies whenever an non-interactive job expires
52 reset to jiffies if expires
54 unsigned long expired_timestamp;
57 * highest priority of tasks in active
58 * initialized to be MAX_PRIO
59 * updated on enqueue, dequeue
64 unsigned long lrq_load;
69 * unused CPU time accumulated while thoe class
70 * is inactive goes to savings
73 * a class can't accumulate more than SAVING_THRESHOLD of savings
74 * savings are kept in normalized form (like cvt)
75 * so when task share change the savings should be scaled accordingly
77 unsigned long long savings;
79 unsigned long magic; //for debugging
82 typedef struct ckrm_runqueue ckrm_lrq_t;
85 * ckrm_cpu_class_stat - cpu usage statistics maintained for each class
88 struct ckrm_cpu_class_stat {
91 unsigned long long total_ns; /*how much nano-secs it has consumed */
93 struct ckrm_cpu_demand_stat local_stats[NR_CPUS];
98 unsigned long max_demand; /* the maximun a class can consume */
99 int egrt,megrt; /*effective guarantee*/
100 int ehl,mehl; /*effective hard limit, my effective hard limit*/
103 * eshare: for both default class and its children
104 * meshare: just for the default class
110 #define CKRM_CPU_CLASS_MAGIC 0x7af2abe3
112 #define USAGE_SAMPLE_FREQ HZ //sample every 1 seconds
113 #define NS_PER_SAMPLE (USAGE_SAMPLE_FREQ*(NSEC_PER_SEC/HZ))
114 #define USAGE_WINDOW_SIZE 60 //keep the last 60 sample
117 unsigned long samples[USAGE_WINDOW_SIZE]; //record usages
118 unsigned long sample_pointer; //pointer for the sliding window
119 unsigned long long last_ns; //ns for last sample
120 unsigned long long last_sample_jiffies; //in number of jiffies
124 * manages the class status
125 * there should be only one instance of this object for each class in the whole system
127 struct ckrm_cpu_class {
128 struct ckrm_core_class *core;
129 struct ckrm_core_class *parent;
130 struct ckrm_shares shares;
131 spinlock_t cnt_lock; // always grab parent's lock first and then child's
132 struct ckrm_cpu_class_stat stat;
133 struct list_head links; // for linking up in cpu classes
134 ckrm_lrq_t local_queues[NR_CPUS]; // runqueues
135 struct ckrm_usage usage;
136 unsigned long magic; //for debugging
139 #define cpu_class_weight(cls) (cls->stat.meshare)
140 #define local_class_weight(lrq) (lrq->local_weight)
142 static inline int valid_cpu_class(struct ckrm_cpu_class * cls)
144 return (cls && cls->magic == CKRM_CPU_CLASS_MAGIC);
147 struct classqueue_struct *get_cpu_classqueue(int cpu);
148 struct ckrm_cpu_class * get_default_cpu_class(void);
151 static inline void ckrm_usage_init(struct ckrm_usage* usage)
155 for (i=0; i < USAGE_WINDOW_SIZE; i++)
156 usage->samples[i] = 0;
157 usage->sample_pointer = 0;
159 usage->last_sample_jiffies = 0;
163 * this function can be called at any frequency
164 * it's self-contained
166 static inline void ckrm_sample_usage(struct ckrm_cpu_class* clsptr)
168 struct ckrm_usage* usage = &clsptr->usage;
169 unsigned long long cur_sample;
170 int duration = jiffies - usage->last_sample_jiffies;
172 // printk("\tckrm_sample_usage %ld %p: %lld\n",jiffies, clsptr,cur_sample);
174 if (duration < USAGE_SAMPLE_FREQ)
177 cur_sample = clsptr->stat.total_ns - usage->last_ns;
178 //scale it based on the sample duration
179 cur_sample *= ((duration << 10)/USAGE_SAMPLE_FREQ);
182 usage->samples[usage->sample_pointer++] = cur_sample;
183 usage->last_sample_jiffies = jiffies;
184 usage->last_ns = clsptr->stat.total_ns;
185 if (usage->sample_pointer >= USAGE_WINDOW_SIZE)
186 usage->sample_pointer = 0;
189 //duration is specified in number of jiffies
190 //return the usage in percentage
191 static inline int get_ckrm_usage(struct ckrm_cpu_class* clsptr, int duration)
193 int nr_samples = duration/USAGE_SAMPLE_FREQ?:1;
194 struct ckrm_usage* usage = &clsptr->usage;
195 unsigned long long total = 0;
198 if (nr_samples > USAGE_WINDOW_SIZE)
199 nr_samples = USAGE_WINDOW_SIZE;
201 idx = usage->sample_pointer;
202 for (i = 0; i< nr_samples; i++) {
204 idx = USAGE_WINDOW_SIZE;
206 total += usage->samples[idx];
209 do_div(total,nr_samples);
210 do_div(total,NS_PER_SAMPLE);
211 // printk("percent %lld\n",total);
216 #define lrq_nr_running(lrq) \
217 (lrq->active->nr_active + lrq->expired->nr_active)
219 static inline ckrm_lrq_t *
220 get_ckrm_lrq(struct ckrm_cpu_class*cls, int cpu)
222 return &(cls->local_queues[cpu]);
225 static inline ckrm_lrq_t *get_task_lrq(struct task_struct *p)
227 return &(p->cpu_class->local_queues[task_cpu(p)]);
230 #define task_list_entry(list) list_entry(list,struct task_struct,run_list)
231 #define class_list_entry(list) list_entry(list,struct ckrm_runqueue,classqueue_linkobj)
233 /* some additional interfaces exported from sched.c */
235 extern rwlock_t class_list_lock;
236 extern struct list_head active_cpu_classes;
237 unsigned int task_timeslice(task_t *p);
238 void _ckrm_cpu_change_class(task_t *task, struct ckrm_cpu_class *newcls);
240 void init_cpu_classes(void);
241 void init_cpu_class(struct ckrm_cpu_class *cls,ckrm_shares_t* shares);
242 void ckrm_cpu_change_class(void *task, void *old, void *new);
245 #define CPU_DEMAND_ENQUEUE 0
246 #define CPU_DEMAND_DEQUEUE 1
247 #define CPU_DEMAND_DESCHEDULE 2
248 #define CPU_DEMAND_INIT 3
250 /*functions exported by ckrm_cpu_monitor.c*/
251 void ckrm_cpu_monitor(void);
252 int ckrm_cpu_monitor_init(void);
253 void ckrm_cpu_stat_init(struct ckrm_cpu_class_stat *stat);
254 void cpu_demand_event(struct ckrm_cpu_demand_stat* local_stat, int event, unsigned long long len);
255 void adjust_local_weight(void);
257 #define get_task_lrq_stat(p) (&(p)->cpu_class->stat.local_stats[task_cpu(p)])
258 #define get_cls_local_stat(cls,cpu) (&(cls)->stat.local_stats[cpu])
259 #define get_rq_local_stat(lrq,cpu) (get_cls_local_stat((lrq)->cpu_class,cpu))
261 #define CLASS_QUANTIZER 22 //shift from ns to increase class bonus
262 #define PRIORITY_QUANTIZER 0 //controls how much a high prio task can borrow
263 #define CKRM_SHARE_ACCURACY 10
264 #define NSEC_PER_MS 1000000
265 #define NSEC_PER_JIFFIES (NSEC_PER_SEC/HZ)
268 #define MAX_SAVINGS_ABSOLUTE (10LLU*NSEC_PER_SEC) // 10 seconds
270 #define CVT_UPDATE_TICK ((HZ/2)?:1)
272 // ABSOLUTE_CKRM_TUNING determines whether classes can make up
273 // lost time in absolute time or in relative values
275 #define ABSOLUTE_CKRM_TUNING // preferred due to more predictable behavior
277 #ifdef ABSOLUTE_CKRM_TUNING
279 #define MAX_SAVINGS MAX_SAVINGS_ABSOLUTE
280 //an absolute bonus of 200ms for classes when reactivated
281 #define INTERACTIVE_BONUS(lrq) ((200*NSEC_PER_MS)/local_class_weight(lrq))
282 #define SAVINGS_LEAK_SPEED (CVT_UPDATE_TICK/10*NSEC_PER_JIFFIES)
284 #define scale_cvt(val,lrq) ((val)*local_class_weight(lrq))
285 #define unscale_cvt(val,lrq) (do_div(val,local_class_weight(lrq)))
289 #define MAX_SAVINGS (MAX_SAVINGS_ABSOLUTE >> CKRM_SHARE_ACCURACY)
291 * to improve system responsiveness
292 * an inactive class is put a little bit ahead of the current class when it wakes up
293 * the amount is set in normalized termis to simplify the calculation
294 * for class with 100% share, it can be 2s ahead
295 * while for class with 10% share, it can be 200ms ahead
297 #define INTERACTIVE_BONUS(lrq) (2*NSEC_PER_MS)
300 * normalized savings can't be more than MAX_NORMALIZED_SAVINGS
301 * based on the current configuration
302 * this means that a class with share 100% will accumulate 10s at most
303 * while a class with 1% of the share can only accumulate 100ms
306 //a class with share 100% can get 100ms every 500ms
307 //while a class with share 10% can only get 10ms every 500ms
308 #define SAVINGS_LEAK_SPEED ((CVT_UPDATE_TICK/5*NSEC_PER_JIFFIES) >> CKRM_SHARE_ACCURACY)
310 #define scale_cvt(val,lrq) (val)
311 #define unscale_cvt(val,lrq) (val)
317 * get_effective_prio: return the effective priority of a class local queue
319 * class priority = progress * a + urgency * b
320 * progress = queue cvt
321 * urgency = queue top priority
322 * a and b are scaling factors
323 * currently, prio increases by 1 if either: top_priority increase by one
324 * or, local_cvt increases by 4ms
326 static inline int get_effective_prio(ckrm_lrq_t * lrq)
330 prio = lrq->local_cvt >> CLASS_QUANTIZER; // cumulative usage
331 prio += lrq->top_priority >> PRIORITY_QUANTIZER; // queue urgency
336 CVT_t get_local_cur_cvt(int cpu);
339 * update_class_priority:
341 * called whenever cvt or top_priority changes
343 * internal: (calling structure)
344 * update_class_priority
345 * -- set_top_priority
346 * -- class_enqueue_task
347 * -- class_dequeue_task
348 * -- rq_get_next_task (queue switch)
349 * -- update_local_cvt
352 static inline void update_class_priority(ckrm_lrq_t *local_rq)
354 int effective_prio = get_effective_prio(local_rq);
355 classqueue_update_prio(local_rq->classqueue,
356 &local_rq->classqueue_linkobj,
361 * set the new top priority and reposition the queue
362 * called when: task enqueue/dequeue and queue switch
364 static inline void set_top_priority(ckrm_lrq_t *lrq,
367 lrq->top_priority = new_priority;
368 update_class_priority(lrq);
372 * task_load: how much load this task counts
374 static inline unsigned long task_load(struct task_struct* p)
376 return (task_timeslice(p) * p->demand_stat.cpu_demand);
380 * runqueue load is the local_weight of all the classes on this cpu
381 * must be called with class_list_lock held
383 static inline unsigned long ckrm_cpu_load(int cpu)
385 struct ckrm_cpu_class *clsptr;
387 struct ckrm_cpu_demand_stat* l_stat;
391 list_for_each_entry(clsptr,&active_cpu_classes,links) {
392 lrq = get_ckrm_lrq(clsptr,cpu);
393 l_stat = get_cls_local_stat(clsptr,cpu);
394 load = lrq->local_weight;
395 if (l_stat->cpu_demand < load)
396 load = l_stat->cpu_demand;
402 static inline void class_enqueue_task(struct task_struct *p,
403 prio_array_t * array)
408 lrq = get_task_lrq(p);
410 cpu_demand_event(&p->demand_stat,CPU_DEMAND_ENQUEUE,0);
411 lrq->lrq_load += task_load(p);
413 if ((p->prio < lrq->top_priority) && (array == lrq->active))
414 set_top_priority(lrq, p->prio);
416 if (! cls_in_classqueue(&lrq->classqueue_linkobj)) {
417 cpu_demand_event(get_task_lrq_stat(p),CPU_DEMAND_ENQUEUE,0);
418 effective_prio = get_effective_prio(lrq);
419 classqueue_enqueue(lrq->classqueue, &lrq->classqueue_linkobj, effective_prio);
424 static inline void class_dequeue_task(struct task_struct *p,
425 prio_array_t * array)
427 ckrm_lrq_t *lrq = get_task_lrq(p);
428 unsigned long load = task_load(p);
430 BUG_ON(lrq->lrq_load < load);
431 lrq->lrq_load -= load;
433 cpu_demand_event(&p->demand_stat,CPU_DEMAND_DEQUEUE,0);
435 if ((array == lrq->active) && (p->prio == lrq->top_priority)
436 && list_empty(&(array->queue[p->prio])))
437 set_top_priority(lrq,
438 find_next_bit(array->bitmap, MAX_PRIO,
443 * called after a task is switched out. Update the local cvt accounting
444 * we need to stick with long instead of long long due to nonexistent 64-bit division
446 static inline void update_local_cvt(struct task_struct *p, unsigned long nsec)
448 ckrm_lrq_t * lrq = get_task_lrq(p);
450 unsigned long cvt_inc = nsec / local_class_weight(lrq);
452 lrq->local_cvt += cvt_inc;
453 lrq->uncounted_ns += nsec;
455 update_class_priority(lrq);
458 static inline int class_preempts_curr(struct task_struct * p, struct task_struct* curr)
460 struct cq_node_struct* node1 = &(get_task_lrq(p)->classqueue_linkobj);
461 struct cq_node_struct* node2 = &(get_task_lrq(curr)->classqueue_linkobj);
463 return (class_compare_prio(node1,node2) < 0);
467 * return a random value with range [0, (val-1)]
469 static inline int get_ckrm_rand(unsigned long val)
472 static int last_rand[NR_CPUS];
473 int cpu = smp_processor_id();
475 rand = last_rand[cpu];
480 last_rand[cpu] = rand;
484 void update_class_cputime(int this_cpu);
486 /**********************************************/
487 /* PID_LOAD_BALANCING */
488 /**********************************************/
489 struct ckrm_load_struct {
490 unsigned long load_p; /*propotional*/
491 unsigned long load_i; /*integral */
492 long load_d; /*derivative */
495 typedef struct ckrm_load_struct ckrm_load_t;
497 static inline void ckrm_load_init(ckrm_load_t* ckrm_load) {
498 ckrm_load->load_p = 0;
499 ckrm_load->load_i = 0;
500 ckrm_load->load_d = 0;
503 void ckrm_load_sample(ckrm_load_t* ckrm_load,int cpu);
504 long pid_get_pressure(ckrm_load_t* ckrm_load, int local_group);
505 #define rq_ckrm_load(rq) (&((rq)->ckrm_load))
507 static inline void ckrm_sched_tick(int j,int this_cpu,struct ckrm_load_struct* ckrm_load)
509 read_lock(&class_list_lock);
512 ckrm_load_sample(ckrm_load,this_cpu);
515 if (!(j % CVT_UPDATE_TICK)) {
516 classqueue_update_base(get_cpu_classqueue(this_cpu));
517 update_class_cputime(this_cpu);
520 read_unlock(&class_list_lock);
523 #endif /*CONFIG_CKRM_CPU_SCHEDULE */