ia64/xen-unstable

view xen/common/sched_bvt.c @ 11128:f2f584093379

[POWERPC] Update .hgignore
Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com>
author kfraser@localhost.localdomain
date Tue Aug 15 10:38:59 2006 +0100 (2006-08-15)
parents 5e8c254c9dcd
children 88e6bd5e2b54
line source
1 /****************************************************************************
2 * (C) 2002-2003 - Rolf Neugebauer - Intel Research Cambridge
3 * (C) 2002-2003 University of Cambridge
4 * (C) 2004 - Mark Williamson - Intel Research Cambridge
5 ****************************************************************************
6 *
7 * File: common/schedule.c
8 * Author: Rolf Neugebauer & Keir Fraser
9 * Updated for generic API by Mark Williamson
10 *
11 * Description: CPU scheduling
12 * implements A Borrowed Virtual Time scheduler.
13 * (see Duda & Cheriton SOSP'99)
14 */
16 #include <xen/config.h>
17 #include <xen/init.h>
18 #include <xen/lib.h>
19 #include <xen/sched.h>
20 #include <xen/delay.h>
21 #include <xen/event.h>
22 #include <xen/time.h>
23 #include <xen/timer.h>
24 #include <xen/perfc.h>
25 #include <xen/sched-if.h>
26 #include <xen/softirq.h>
28 /* all per-domain BVT-specific scheduling info is stored here */
29 struct bvt_vcpu_info
30 {
31 struct list_head run_list; /* runqueue list pointers */
32 u32 avt; /* actual virtual time */
33 u32 evt; /* effective virtual time */
34 int migrated; /* migrated to a new CPU */
35 struct vcpu *vcpu;
36 struct bvt_dom_info *inf;
37 };
39 struct bvt_dom_info
40 {
41 struct domain *domain; /* domain this info belongs to */
42 u32 mcu_advance; /* inverse of weight */
43 int warpback; /* warp? */
44 int warp; /* warp set and within the warp
45 limits*/
46 s32 warp_value; /* virtual time warp */
47 s_time_t warpl; /* warp limit */
48 struct timer warp_timer; /* deals with warpl */
49 s_time_t warpu; /* unwarp time requirement */
50 struct timer unwarp_timer; /* deals with warpu */
52 struct bvt_vcpu_info vcpu_inf[MAX_VIRT_CPUS];
53 };
55 struct bvt_cpu_info
56 {
57 struct list_head runqueue;
58 unsigned long svt;
59 };
61 #define BVT_INFO(p) ((struct bvt_dom_info *)(p)->sched_priv)
62 #define EBVT_INFO(p) ((struct bvt_vcpu_info *)(p)->sched_priv)
63 #define CPU_INFO(cpu) \
64 ((struct bvt_cpu_info *)(per_cpu(schedule_data, cpu).sched_priv))
65 #define RUNLIST(p) ((struct list_head *)&(EBVT_INFO(p)->run_list))
66 #define RUNQUEUE(cpu) ((struct list_head *)&(CPU_INFO(cpu)->runqueue))
67 #define CPU_SVT(cpu) (CPU_INFO(cpu)->svt)
69 #define MCU (s32)MICROSECS(100) /* Minimum unit */
70 #define MCU_ADVANCE 10 /* default weight */
71 #define TIME_SLOP (s32)MICROSECS(50) /* allow time to slip a bit */
72 #define CTX_MIN (s32)MICROSECS(10) /* Low limit for ctx_allow */
73 static s32 ctx_allow = (s32)MILLISECS(5); /* context switch allowance */
75 static inline void __add_to_runqueue_head(struct vcpu *d)
76 {
77 list_add(RUNLIST(d), RUNQUEUE(d->processor));
78 }
80 static inline void __add_to_runqueue_tail(struct vcpu *d)
81 {
82 list_add_tail(RUNLIST(d), RUNQUEUE(d->processor));
83 }
85 static inline void __del_from_runqueue(struct vcpu *d)
86 {
87 struct list_head *runlist = RUNLIST(d);
88 list_del(runlist);
89 runlist->next = NULL;
90 }
92 static inline int __task_on_runqueue(struct vcpu *d)
93 {
94 return (RUNLIST(d))->next != NULL;
95 }
98 /* Warp/unwarp timer functions */
99 static void warp_timer_fn(void *data)
100 {
101 struct bvt_dom_info *inf = data;
102 struct vcpu *v = inf->domain->vcpu[0];
104 vcpu_schedule_lock_irq(v);
106 inf->warp = 0;
108 /* unwarp equal to zero => stop warping */
109 if ( inf->warpu == 0 )
110 {
111 inf->warpback = 0;
112 cpu_raise_softirq(v->processor, SCHEDULE_SOFTIRQ);
113 }
115 set_timer(&inf->unwarp_timer, NOW() + inf->warpu);
117 vcpu_schedule_unlock_irq(v);
118 }
120 static void unwarp_timer_fn(void *data)
121 {
122 struct bvt_dom_info *inf = data;
123 struct vcpu *v = inf->domain->vcpu[0];
125 vcpu_schedule_lock_irq(v);
127 if ( inf->warpback )
128 {
129 inf->warp = 1;
130 cpu_raise_softirq(v->processor, SCHEDULE_SOFTIRQ);
131 }
133 vcpu_schedule_unlock_irq(v);
134 }
136 static inline u32 calc_avt(struct vcpu *v, s_time_t now)
137 {
138 u32 ranfor, mcus;
139 struct bvt_dom_info *inf = BVT_INFO(v->domain);
140 struct bvt_vcpu_info *einf = EBVT_INFO(v);
142 ranfor = (u32)(now - v->runstate.state_entry_time);
143 mcus = (ranfor + MCU - 1)/MCU;
145 return einf->avt + mcus * inf->mcu_advance;
146 }
148 /*
149 * Calculate the effective virtual time for a domain. Take into account
150 * warping limits
151 */
152 static inline u32 calc_evt(struct vcpu *d, u32 avt)
153 {
154 struct bvt_dom_info *inf = BVT_INFO(d->domain);
155 /* TODO The warp routines need to be rewritten GM */
157 if ( inf->warp )
158 return avt - inf->warp_value;
159 else
160 return avt;
161 }
163 /**
164 * bvt_init_vcpu - allocate BVT private structures for a VCPU.
165 * Returns non-zero on failure.
166 */
167 static int bvt_init_vcpu(struct vcpu *v)
168 {
169 struct domain *d = v->domain;
170 struct bvt_dom_info *inf;
171 struct bvt_vcpu_info *einf;
173 if ( (d->sched_priv == NULL) )
174 {
175 if ( (d->sched_priv = xmalloc(struct bvt_dom_info)) == NULL )
176 return -1;
177 memset(d->sched_priv, 0, sizeof(struct bvt_dom_info));
178 }
180 inf = BVT_INFO(d);
182 v->sched_priv = &inf->vcpu_inf[v->vcpu_id];
184 inf->vcpu_inf[v->vcpu_id].inf = BVT_INFO(d);
185 inf->vcpu_inf[v->vcpu_id].vcpu = v;
187 if ( v->vcpu_id == 0 )
188 {
189 inf->mcu_advance = MCU_ADVANCE;
190 inf->domain = v->domain;
191 inf->warpback = 0;
192 /* Set some default values here. */
193 inf->warp = 0;
194 inf->warp_value = 0;
195 inf->warpl = MILLISECS(2000);
196 inf->warpu = MILLISECS(1000);
197 /* Initialise the warp timers. */
198 init_timer(&inf->warp_timer, warp_timer_fn, inf, v->processor);
199 init_timer(&inf->unwarp_timer, unwarp_timer_fn, inf, v->processor);
200 }
202 einf = EBVT_INFO(v);
204 /* Allocate per-CPU context if this is the first domain to be added. */
205 if ( CPU_INFO(v->processor) == NULL )
206 {
207 per_cpu(schedule_data, v->processor).sched_priv =
208 xmalloc(struct bvt_cpu_info);
209 BUG_ON(CPU_INFO(v->processor) == NULL);
210 INIT_LIST_HEAD(RUNQUEUE(v->processor));
211 CPU_SVT(v->processor) = 0;
212 }
214 if ( is_idle_vcpu(v) )
215 {
216 einf->avt = einf->evt = ~0U;
217 BUG_ON(__task_on_runqueue(v));
218 __add_to_runqueue_head(v);
219 }
220 else
221 {
222 /* Set avt and evt to system virtual time. */
223 einf->avt = CPU_SVT(v->processor);
224 einf->evt = CPU_SVT(v->processor);
225 }
227 return 0;
228 }
230 static void bvt_wake(struct vcpu *v)
231 {
232 struct bvt_vcpu_info *einf = EBVT_INFO(v);
233 struct vcpu *curr;
234 s_time_t now, r_time;
235 int cpu = v->processor;
236 u32 curr_evt;
238 if ( unlikely(__task_on_runqueue(v)) )
239 return;
241 __add_to_runqueue_head(v);
243 now = NOW();
245 /* Set the BVT parameters. AVT should always be updated
246 if CPU migration ocurred.*/
247 if ( (einf->avt < CPU_SVT(cpu)) || einf->migrated )
248 {
249 einf->avt = CPU_SVT(cpu);
250 einf->migrated = 0;
251 }
253 /* Deal with warping here. */
254 einf->evt = calc_evt(v, einf->avt);
256 curr = per_cpu(schedule_data, cpu).curr;
257 curr_evt = calc_evt(curr, calc_avt(curr, now));
258 /* Calculate the time the current domain would run assuming
259 the second smallest evt is of the newly woken domain */
260 r_time = curr->runstate.state_entry_time +
261 ((einf->evt - curr_evt) / BVT_INFO(curr->domain)->mcu_advance) +
262 ctx_allow;
264 if ( is_idle_vcpu(curr) || (einf->evt <= curr_evt) )
265 cpu_raise_softirq(cpu, SCHEDULE_SOFTIRQ);
266 else if ( per_cpu(schedule_data, cpu).s_timer.expires > r_time )
267 set_timer(&per_cpu(schedule_data, cpu).s_timer, r_time);
268 }
271 static void bvt_sleep(struct vcpu *v)
272 {
273 if ( per_cpu(schedule_data, v->processor).curr == v )
274 cpu_raise_softirq(v->processor, SCHEDULE_SOFTIRQ);
275 else if ( __task_on_runqueue(v) )
276 __del_from_runqueue(v);
277 }
280 static int bvt_set_affinity(struct vcpu *v, cpumask_t *affinity)
281 {
282 if ( v == current )
283 return cpu_isset(v->processor, *affinity) ? 0 : -EBUSY;
285 vcpu_pause(v);
286 v->cpu_affinity = *affinity;
287 v->processor = first_cpu(v->cpu_affinity);
288 EBVT_INFO(v)->migrated = 1;
289 vcpu_unpause(v);
291 return 0;
292 }
295 /**
296 * bvt_destroy_domain - free BVT private structures for a domain.
297 */
298 static void bvt_destroy_domain(struct domain *d)
299 {
300 struct bvt_dom_info *inf = BVT_INFO(d);
302 ASSERT(inf != NULL);
304 kill_timer(&inf->warp_timer);
305 kill_timer(&inf->unwarp_timer);
307 xfree(inf);
308 }
310 /* Control the scheduler. */
311 static int bvt_ctl(struct sched_ctl_cmd *cmd)
312 {
313 struct bvt_ctl *params = &cmd->u.bvt;
315 if ( cmd->direction == SCHED_INFO_PUT )
316 ctx_allow = params->ctx_allow;
317 else
318 {
319 if ( ctx_allow < CTX_MIN )
320 ctx_allow = CTX_MIN;
321 params->ctx_allow = ctx_allow;
322 }
324 return 0;
325 }
327 /* Adjust scheduling parameter for a given domain. */
328 static int bvt_adjdom(
329 struct domain *d, struct sched_adjdom_cmd *cmd)
330 {
331 struct bvt_adjdom *params = &cmd->u.bvt;
333 if ( cmd->direction == SCHED_INFO_PUT )
334 {
335 u32 mcu_adv = params->mcu_adv;
336 u32 warpback = params->warpback;
337 s32 warpvalue = params->warpvalue;
338 s_time_t warpl = params->warpl;
339 s_time_t warpu = params->warpu;
341 struct bvt_dom_info *inf = BVT_INFO(d);
343 /* Sanity -- this can avoid divide-by-zero. */
344 if ( (mcu_adv == 0) || (warpl < 0) || (warpu < 0) )
345 return -EINVAL;
347 inf->mcu_advance = mcu_adv;
348 inf->warpback = warpback;
349 /* The warp should be the same as warpback */
350 inf->warp = warpback;
351 inf->warp_value = warpvalue;
352 inf->warpl = MILLISECS(warpl);
353 inf->warpu = MILLISECS(warpu);
355 /* If the unwarp timer set up it needs to be removed */
356 stop_timer(&inf->unwarp_timer);
357 /* If we stop warping the warp timer needs to be removed */
358 if ( !warpback )
359 stop_timer(&inf->warp_timer);
360 }
361 else if ( cmd->direction == SCHED_INFO_GET )
362 {
363 struct bvt_dom_info *inf = BVT_INFO(d);
364 params->mcu_adv = inf->mcu_advance;
365 params->warpvalue = inf->warp_value;
366 params->warpback = inf->warpback;
367 params->warpl = inf->warpl;
368 params->warpu = inf->warpu;
369 }
371 return 0;
372 }
375 /*
376 * The main function
377 * - deschedule the current domain.
378 * - pick a new domain.
379 * i.e., the domain with lowest EVT.
380 * The runqueue should be ordered by EVT so that is easy.
381 */
382 static struct task_slice bvt_do_schedule(s_time_t now)
383 {
384 struct domain *d;
385 struct vcpu *prev = current, *next = NULL, *next_prime, *ed;
386 int cpu = prev->processor;
387 s32 r_time; /* time for new dom to run */
388 u32 next_evt, next_prime_evt, min_avt;
389 struct bvt_dom_info *prev_inf = BVT_INFO(prev->domain);
390 struct bvt_vcpu_info *prev_einf = EBVT_INFO(prev);
391 struct bvt_vcpu_info *p_einf = NULL;
392 struct bvt_vcpu_info *next_einf = NULL;
393 struct bvt_vcpu_info *next_prime_einf = NULL;
394 struct task_slice ret;
396 ASSERT(prev->sched_priv != NULL);
397 ASSERT(prev_einf != NULL);
398 ASSERT(__task_on_runqueue(prev));
400 if ( likely(!is_idle_vcpu(prev)) )
401 {
402 prev_einf->avt = calc_avt(prev, now);
403 prev_einf->evt = calc_evt(prev, prev_einf->avt);
405 if(prev_inf->warpback && prev_inf->warpl > 0)
406 stop_timer(&prev_inf->warp_timer);
408 __del_from_runqueue(prev);
410 if ( vcpu_runnable(prev) )
411 __add_to_runqueue_tail(prev);
412 }
415 /* We should at least have the idle task */
416 ASSERT(!list_empty(RUNQUEUE(cpu)));
418 /*
419 * scan through the run queue and pick the task with the lowest evt
420 * *and* the task the second lowest evt.
421 * this code is O(n) but we expect n to be small.
422 */
423 next_einf = EBVT_INFO(per_cpu(schedule_data, cpu).idle);
424 next_prime_einf = NULL;
426 next_evt = ~0U;
427 next_prime_evt = ~0U;
428 min_avt = ~0U;
430 list_for_each_entry ( p_einf, RUNQUEUE(cpu), run_list )
431 {
432 if ( p_einf->evt < next_evt )
433 {
434 next_prime_einf = next_einf;
435 next_prime_evt = next_evt;
436 next_einf = p_einf;
437 next_evt = p_einf->evt;
438 }
439 else if ( next_prime_evt == ~0U )
440 {
441 next_prime_evt = p_einf->evt;
442 next_prime_einf = p_einf;
443 }
444 else if ( p_einf->evt < next_prime_evt )
445 {
446 next_prime_evt = p_einf->evt;
447 next_prime_einf = p_einf;
448 }
450 /* Determine system virtual time. */
451 if ( p_einf->avt < min_avt )
452 min_avt = p_einf->avt;
453 }
455 if ( next_einf->inf->warp && next_einf->inf->warpl > 0 )
456 set_timer(&next_einf->inf->warp_timer, now + next_einf->inf->warpl);
458 /* Extract the domain pointers from the dom infos */
459 next = next_einf->vcpu;
460 next_prime = next_prime_einf->vcpu;
462 /* Update system virtual time. */
463 if ( min_avt != ~0U )
464 CPU_SVT(cpu) = min_avt;
466 /* check for virtual time overrun on this cpu */
467 if ( CPU_SVT(cpu) >= 0xf0000000 )
468 {
469 ASSERT(!local_irq_is_enabled());
471 write_lock(&domlist_lock);
473 for_each_domain ( d )
474 {
475 for_each_vcpu (d, ed) {
476 if ( ed->processor == cpu )
477 {
478 p_einf = EBVT_INFO(ed);
479 p_einf->evt -= 0xe0000000;
480 p_einf->avt -= 0xe0000000;
481 }
482 }
483 }
485 write_unlock(&domlist_lock);
487 CPU_SVT(cpu) -= 0xe0000000;
488 }
490 /* work out time for next run through scheduler */
491 if ( is_idle_vcpu(next) )
492 {
493 r_time = ctx_allow;
494 goto sched_done;
495 }
497 if ( (next_prime == NULL) || is_idle_vcpu(next_prime) )
498 {
499 /* We have only one runnable task besides the idle task. */
500 r_time = 10 * ctx_allow; /* RN: random constant */
501 goto sched_done;
502 }
504 /*
505 * If we are here then we have two runnable tasks.
506 * Work out how long 'next' can run till its evt is greater than
507 * 'next_prime's evt. Take context switch allowance into account.
508 */
509 ASSERT(next_prime_einf->evt >= next_einf->evt);
511 r_time = ((next_prime_einf->evt - next_einf->evt)/next_einf->inf->mcu_advance)
512 + ctx_allow;
514 ASSERT(r_time >= ctx_allow);
516 sched_done:
517 ret.task = next;
518 ret.time = r_time;
519 return ret;
520 }
523 static void bvt_dump_runq_el(struct vcpu *p)
524 {
525 struct bvt_vcpu_info *inf = EBVT_INFO(p);
527 printk("mcua=%d ev=0x%08X av=0x%08X ",
528 inf->inf->mcu_advance, inf->evt, inf->avt);
529 }
531 static void bvt_dump_settings(void)
532 {
533 printk("BVT: mcu=0x%08Xns ctx_allow=0x%08Xns ", (u32)MCU, (s32)ctx_allow );
534 }
536 static void bvt_dump_cpu_state(int i)
537 {
538 struct list_head *queue;
539 int loop = 0;
540 struct bvt_vcpu_info *vcpu_inf;
541 struct vcpu *v;
543 printk("svt=0x%08lX ", CPU_SVT(i));
545 queue = RUNQUEUE(i);
546 printk("QUEUE rq %lx n: %lx, p: %lx\n", (unsigned long)queue,
547 (unsigned long) queue->next, (unsigned long) queue->prev);
549 list_for_each_entry ( vcpu_inf, queue, run_list )
550 {
551 v = vcpu_inf->vcpu;
552 printk("%3d: %u has=%c ", loop++, v->domain->domain_id,
553 test_bit(_VCPUF_running, &v->vcpu_flags) ? 'T':'F');
554 bvt_dump_runq_el(v);
555 printk(" l: %p n: %p p: %p\n",
556 &vcpu_inf->run_list, vcpu_inf->run_list.next,
557 vcpu_inf->run_list.prev);
558 }
559 }
561 struct scheduler sched_bvt_def = {
562 .name = "Borrowed Virtual Time",
563 .opt_name = "bvt",
564 .sched_id = SCHED_BVT,
566 .init_vcpu = bvt_init_vcpu,
567 .destroy_domain = bvt_destroy_domain,
569 .do_schedule = bvt_do_schedule,
570 .control = bvt_ctl,
571 .adjdom = bvt_adjdom,
572 .dump_settings = bvt_dump_settings,
573 .dump_cpu_state = bvt_dump_cpu_state,
574 .sleep = bvt_sleep,
575 .wake = bvt_wake,
576 .set_affinity = bvt_set_affinity
577 };
579 /*
580 * Local variables:
581 * mode: C
582 * c-set-style: "BSD"
583 * c-basic-offset: 4
584 * tab-width: 4
585 * indent-tabs-mode: nil
586 * End:
587 */