ia64/xen-unstable

view xen/common/sched_bvt.c @ 10570:8dc4af3f192c

[IA64] Implement and use DOM0_DOMAIN_STEUP.

DOM0_GETMEMLIST now reads ptes and use gpfn.
Domain builder reworked: calls DOMAIN_SETUP, setup start_info page.
SAL data are now in domain memory.
is_vti field added in domain.arch.
Many cleanups (indentation, static, warnings).

Signed-off-by: Tristan Gingold <tristan.gingold@bull.net>
author awilliam@xenbuild.aw
date Wed Jul 05 09:28:32 2006 -0600 (2006-07-05)
parents 6993a0f91efc
children 5e8c254c9dcd
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) ((struct bvt_cpu_info *)(schedule_data[cpu]).sched_priv)
64 #define RUNLIST(p) ((struct list_head *)&(EBVT_INFO(p)->run_list))
65 #define RUNQUEUE(cpu) ((struct list_head *)&(CPU_INFO(cpu)->runqueue))
66 #define CPU_SVT(cpu) (CPU_INFO(cpu)->svt)
68 #define MCU (s32)MICROSECS(100) /* Minimum unit */
69 #define MCU_ADVANCE 10 /* default weight */
70 #define TIME_SLOP (s32)MICROSECS(50) /* allow time to slip a bit */
71 #define CTX_MIN (s32)MICROSECS(10) /* Low limit for ctx_allow */
72 static s32 ctx_allow = (s32)MILLISECS(5); /* context switch allowance */
74 static inline void __add_to_runqueue_head(struct vcpu *d)
75 {
76 list_add(RUNLIST(d), RUNQUEUE(d->processor));
77 }
79 static inline void __add_to_runqueue_tail(struct vcpu *d)
80 {
81 list_add_tail(RUNLIST(d), RUNQUEUE(d->processor));
82 }
84 static inline void __del_from_runqueue(struct vcpu *d)
85 {
86 struct list_head *runlist = RUNLIST(d);
87 list_del(runlist);
88 runlist->next = NULL;
89 }
91 static inline int __task_on_runqueue(struct vcpu *d)
92 {
93 return (RUNLIST(d))->next != NULL;
94 }
97 /* Warp/unwarp timer functions */
98 static void warp_timer_fn(void *data)
99 {
100 struct bvt_dom_info *inf = data;
101 struct vcpu *v = inf->domain->vcpu[0];
103 vcpu_schedule_lock_irq(v);
105 inf->warp = 0;
107 /* unwarp equal to zero => stop warping */
108 if ( inf->warpu == 0 )
109 {
110 inf->warpback = 0;
111 cpu_raise_softirq(v->processor, SCHEDULE_SOFTIRQ);
112 }
114 set_timer(&inf->unwarp_timer, NOW() + inf->warpu);
116 vcpu_schedule_unlock_irq(v);
117 }
119 static void unwarp_timer_fn(void *data)
120 {
121 struct bvt_dom_info *inf = data;
122 struct vcpu *v = inf->domain->vcpu[0];
124 vcpu_schedule_lock_irq(v);
126 if ( inf->warpback )
127 {
128 inf->warp = 1;
129 cpu_raise_softirq(v->processor, SCHEDULE_SOFTIRQ);
130 }
132 vcpu_schedule_unlock_irq(v);
133 }
135 static inline u32 calc_avt(struct vcpu *v, s_time_t now)
136 {
137 u32 ranfor, mcus;
138 struct bvt_dom_info *inf = BVT_INFO(v->domain);
139 struct bvt_vcpu_info *einf = EBVT_INFO(v);
141 ranfor = (u32)(now - v->runstate.state_entry_time);
142 mcus = (ranfor + MCU - 1)/MCU;
144 return einf->avt + mcus * inf->mcu_advance;
145 }
147 /*
148 * Calculate the effective virtual time for a domain. Take into account
149 * warping limits
150 */
151 static inline u32 calc_evt(struct vcpu *d, u32 avt)
152 {
153 struct bvt_dom_info *inf = BVT_INFO(d->domain);
154 /* TODO The warp routines need to be rewritten GM */
156 if ( inf->warp )
157 return avt - inf->warp_value;
158 else
159 return avt;
160 }
162 /**
163 * bvt_init_vcpu - allocate BVT private structures for a VCPU.
164 * Returns non-zero on failure.
165 */
166 static int bvt_init_vcpu(struct vcpu *v)
167 {
168 struct domain *d = v->domain;
169 struct bvt_dom_info *inf;
170 struct bvt_vcpu_info *einf;
172 if ( (d->sched_priv == NULL) )
173 {
174 if ( (d->sched_priv = xmalloc(struct bvt_dom_info)) == NULL )
175 return -1;
176 memset(d->sched_priv, 0, sizeof(struct bvt_dom_info));
177 }
179 inf = BVT_INFO(d);
181 v->sched_priv = &inf->vcpu_inf[v->vcpu_id];
183 inf->vcpu_inf[v->vcpu_id].inf = BVT_INFO(d);
184 inf->vcpu_inf[v->vcpu_id].vcpu = v;
186 if ( v->vcpu_id == 0 )
187 {
188 inf->mcu_advance = MCU_ADVANCE;
189 inf->domain = v->domain;
190 inf->warpback = 0;
191 /* Set some default values here. */
192 inf->warp = 0;
193 inf->warp_value = 0;
194 inf->warpl = MILLISECS(2000);
195 inf->warpu = MILLISECS(1000);
196 /* Initialise the warp timers. */
197 init_timer(&inf->warp_timer, warp_timer_fn, inf, v->processor);
198 init_timer(&inf->unwarp_timer, unwarp_timer_fn, inf, v->processor);
199 }
201 einf = EBVT_INFO(v);
203 /* Allocate per-CPU context if this is the first domain to be added. */
204 if ( CPU_INFO(v->processor) == NULL )
205 {
206 schedule_data[v->processor].sched_priv = xmalloc(struct bvt_cpu_info);
207 BUG_ON(CPU_INFO(v->processor) == NULL);
208 INIT_LIST_HEAD(RUNQUEUE(v->processor));
209 CPU_SVT(v->processor) = 0;
210 }
212 if ( is_idle_vcpu(v) )
213 {
214 einf->avt = einf->evt = ~0U;
215 BUG_ON(__task_on_runqueue(v));
216 __add_to_runqueue_head(v);
217 }
218 else
219 {
220 /* Set avt and evt to system virtual time. */
221 einf->avt = CPU_SVT(v->processor);
222 einf->evt = CPU_SVT(v->processor);
223 }
225 return 0;
226 }
228 static void bvt_wake(struct vcpu *v)
229 {
230 struct bvt_vcpu_info *einf = EBVT_INFO(v);
231 struct vcpu *curr;
232 s_time_t now, r_time;
233 int cpu = v->processor;
234 u32 curr_evt;
236 if ( unlikely(__task_on_runqueue(v)) )
237 return;
239 __add_to_runqueue_head(v);
241 now = NOW();
243 /* Set the BVT parameters. AVT should always be updated
244 if CPU migration ocurred.*/
245 if ( (einf->avt < CPU_SVT(cpu)) || einf->migrated )
246 {
247 einf->avt = CPU_SVT(cpu);
248 einf->migrated = 0;
249 }
251 /* Deal with warping here. */
252 einf->evt = calc_evt(v, einf->avt);
254 curr = schedule_data[cpu].curr;
255 curr_evt = calc_evt(curr, calc_avt(curr, now));
256 /* Calculate the time the current domain would run assuming
257 the second smallest evt is of the newly woken domain */
258 r_time = curr->runstate.state_entry_time +
259 ((einf->evt - curr_evt) / BVT_INFO(curr->domain)->mcu_advance) +
260 ctx_allow;
262 if ( is_idle_vcpu(curr) || (einf->evt <= curr_evt) )
263 cpu_raise_softirq(cpu, SCHEDULE_SOFTIRQ);
264 else if ( schedule_data[cpu].s_timer.expires > r_time )
265 set_timer(&schedule_data[cpu].s_timer, r_time);
266 }
269 static void bvt_sleep(struct vcpu *v)
270 {
271 if ( schedule_data[v->processor].curr == v )
272 cpu_raise_softirq(v->processor, SCHEDULE_SOFTIRQ);
273 else if ( __task_on_runqueue(v) )
274 __del_from_runqueue(v);
275 }
278 static int bvt_set_affinity(struct vcpu *v, cpumask_t *affinity)
279 {
280 if ( v == current )
281 return cpu_isset(v->processor, *affinity) ? 0 : -EBUSY;
283 vcpu_pause(v);
284 v->cpu_affinity = *affinity;
285 v->processor = first_cpu(v->cpu_affinity);
286 EBVT_INFO(v)->migrated = 1;
287 vcpu_unpause(v);
289 return 0;
290 }
293 /**
294 * bvt_destroy_domain - free BVT private structures for a domain.
295 */
296 static void bvt_destroy_domain(struct domain *d)
297 {
298 struct bvt_dom_info *inf = BVT_INFO(d);
300 ASSERT(inf != NULL);
302 kill_timer(&inf->warp_timer);
303 kill_timer(&inf->unwarp_timer);
305 xfree(inf);
306 }
308 /* Control the scheduler. */
309 static int bvt_ctl(struct sched_ctl_cmd *cmd)
310 {
311 struct bvt_ctl *params = &cmd->u.bvt;
313 if ( cmd->direction == SCHED_INFO_PUT )
314 ctx_allow = params->ctx_allow;
315 else
316 {
317 if ( ctx_allow < CTX_MIN )
318 ctx_allow = CTX_MIN;
319 params->ctx_allow = ctx_allow;
320 }
322 return 0;
323 }
325 /* Adjust scheduling parameter for a given domain. */
326 static int bvt_adjdom(
327 struct domain *d, struct sched_adjdom_cmd *cmd)
328 {
329 struct bvt_adjdom *params = &cmd->u.bvt;
331 if ( cmd->direction == SCHED_INFO_PUT )
332 {
333 u32 mcu_adv = params->mcu_adv;
334 u32 warpback = params->warpback;
335 s32 warpvalue = params->warpvalue;
336 s_time_t warpl = params->warpl;
337 s_time_t warpu = params->warpu;
339 struct bvt_dom_info *inf = BVT_INFO(d);
341 /* Sanity -- this can avoid divide-by-zero. */
342 if ( (mcu_adv == 0) || (warpl < 0) || (warpu < 0) )
343 return -EINVAL;
345 inf->mcu_advance = mcu_adv;
346 inf->warpback = warpback;
347 /* The warp should be the same as warpback */
348 inf->warp = warpback;
349 inf->warp_value = warpvalue;
350 inf->warpl = MILLISECS(warpl);
351 inf->warpu = MILLISECS(warpu);
353 /* If the unwarp timer set up it needs to be removed */
354 stop_timer(&inf->unwarp_timer);
355 /* If we stop warping the warp timer needs to be removed */
356 if ( !warpback )
357 stop_timer(&inf->warp_timer);
358 }
359 else if ( cmd->direction == SCHED_INFO_GET )
360 {
361 struct bvt_dom_info *inf = BVT_INFO(d);
362 params->mcu_adv = inf->mcu_advance;
363 params->warpvalue = inf->warp_value;
364 params->warpback = inf->warpback;
365 params->warpl = inf->warpl;
366 params->warpu = inf->warpu;
367 }
369 return 0;
370 }
373 /*
374 * The main function
375 * - deschedule the current domain.
376 * - pick a new domain.
377 * i.e., the domain with lowest EVT.
378 * The runqueue should be ordered by EVT so that is easy.
379 */
380 static struct task_slice bvt_do_schedule(s_time_t now)
381 {
382 struct domain *d;
383 struct vcpu *prev = current, *next = NULL, *next_prime, *ed;
384 int cpu = prev->processor;
385 s32 r_time; /* time for new dom to run */
386 u32 next_evt, next_prime_evt, min_avt;
387 struct bvt_dom_info *prev_inf = BVT_INFO(prev->domain);
388 struct bvt_vcpu_info *prev_einf = EBVT_INFO(prev);
389 struct bvt_vcpu_info *p_einf = NULL;
390 struct bvt_vcpu_info *next_einf = NULL;
391 struct bvt_vcpu_info *next_prime_einf = NULL;
392 struct task_slice ret;
394 ASSERT(prev->sched_priv != NULL);
395 ASSERT(prev_einf != NULL);
396 ASSERT(__task_on_runqueue(prev));
398 if ( likely(!is_idle_vcpu(prev)) )
399 {
400 prev_einf->avt = calc_avt(prev, now);
401 prev_einf->evt = calc_evt(prev, prev_einf->avt);
403 if(prev_inf->warpback && prev_inf->warpl > 0)
404 stop_timer(&prev_inf->warp_timer);
406 __del_from_runqueue(prev);
408 if ( vcpu_runnable(prev) )
409 __add_to_runqueue_tail(prev);
410 }
413 /* We should at least have the idle task */
414 ASSERT(!list_empty(RUNQUEUE(cpu)));
416 /*
417 * scan through the run queue and pick the task with the lowest evt
418 * *and* the task the second lowest evt.
419 * this code is O(n) but we expect n to be small.
420 */
421 next_einf = EBVT_INFO(schedule_data[cpu].idle);
422 next_prime_einf = NULL;
424 next_evt = ~0U;
425 next_prime_evt = ~0U;
426 min_avt = ~0U;
428 list_for_each_entry ( p_einf, RUNQUEUE(cpu), run_list )
429 {
430 if ( p_einf->evt < next_evt )
431 {
432 next_prime_einf = next_einf;
433 next_prime_evt = next_evt;
434 next_einf = p_einf;
435 next_evt = p_einf->evt;
436 }
437 else if ( next_prime_evt == ~0U )
438 {
439 next_prime_evt = p_einf->evt;
440 next_prime_einf = p_einf;
441 }
442 else if ( p_einf->evt < next_prime_evt )
443 {
444 next_prime_evt = p_einf->evt;
445 next_prime_einf = p_einf;
446 }
448 /* Determine system virtual time. */
449 if ( p_einf->avt < min_avt )
450 min_avt = p_einf->avt;
451 }
453 if ( next_einf->inf->warp && next_einf->inf->warpl > 0 )
454 set_timer(&next_einf->inf->warp_timer, now + next_einf->inf->warpl);
456 /* Extract the domain pointers from the dom infos */
457 next = next_einf->vcpu;
458 next_prime = next_prime_einf->vcpu;
460 /* Update system virtual time. */
461 if ( min_avt != ~0U )
462 CPU_SVT(cpu) = min_avt;
464 /* check for virtual time overrun on this cpu */
465 if ( CPU_SVT(cpu) >= 0xf0000000 )
466 {
467 ASSERT(!local_irq_is_enabled());
469 write_lock(&domlist_lock);
471 for_each_domain ( d )
472 {
473 for_each_vcpu (d, ed) {
474 if ( ed->processor == cpu )
475 {
476 p_einf = EBVT_INFO(ed);
477 p_einf->evt -= 0xe0000000;
478 p_einf->avt -= 0xe0000000;
479 }
480 }
481 }
483 write_unlock(&domlist_lock);
485 CPU_SVT(cpu) -= 0xe0000000;
486 }
488 /* work out time for next run through scheduler */
489 if ( is_idle_vcpu(next) )
490 {
491 r_time = ctx_allow;
492 goto sched_done;
493 }
495 if ( (next_prime == NULL) || is_idle_vcpu(next_prime) )
496 {
497 /* We have only one runnable task besides the idle task. */
498 r_time = 10 * ctx_allow; /* RN: random constant */
499 goto sched_done;
500 }
502 /*
503 * If we are here then we have two runnable tasks.
504 * Work out how long 'next' can run till its evt is greater than
505 * 'next_prime's evt. Take context switch allowance into account.
506 */
507 ASSERT(next_prime_einf->evt >= next_einf->evt);
509 r_time = ((next_prime_einf->evt - next_einf->evt)/next_einf->inf->mcu_advance)
510 + ctx_allow;
512 ASSERT(r_time >= ctx_allow);
514 sched_done:
515 ret.task = next;
516 ret.time = r_time;
517 return ret;
518 }
521 static void bvt_dump_runq_el(struct vcpu *p)
522 {
523 struct bvt_vcpu_info *inf = EBVT_INFO(p);
525 printk("mcua=%d ev=0x%08X av=0x%08X ",
526 inf->inf->mcu_advance, inf->evt, inf->avt);
527 }
529 static void bvt_dump_settings(void)
530 {
531 printk("BVT: mcu=0x%08Xns ctx_allow=0x%08Xns ", (u32)MCU, (s32)ctx_allow );
532 }
534 static void bvt_dump_cpu_state(int i)
535 {
536 struct list_head *queue;
537 int loop = 0;
538 struct bvt_vcpu_info *vcpu_inf;
539 struct vcpu *v;
541 printk("svt=0x%08lX ", CPU_SVT(i));
543 queue = RUNQUEUE(i);
544 printk("QUEUE rq %lx n: %lx, p: %lx\n", (unsigned long)queue,
545 (unsigned long) queue->next, (unsigned long) queue->prev);
547 list_for_each_entry ( vcpu_inf, queue, run_list )
548 {
549 v = vcpu_inf->vcpu;
550 printk("%3d: %u has=%c ", loop++, v->domain->domain_id,
551 test_bit(_VCPUF_running, &v->vcpu_flags) ? 'T':'F');
552 bvt_dump_runq_el(v);
553 printk(" l: %p n: %p p: %p\n",
554 &vcpu_inf->run_list, vcpu_inf->run_list.next,
555 vcpu_inf->run_list.prev);
556 }
557 }
559 struct scheduler sched_bvt_def = {
560 .name = "Borrowed Virtual Time",
561 .opt_name = "bvt",
562 .sched_id = SCHED_BVT,
564 .init_vcpu = bvt_init_vcpu,
565 .destroy_domain = bvt_destroy_domain,
567 .do_schedule = bvt_do_schedule,
568 .control = bvt_ctl,
569 .adjdom = bvt_adjdom,
570 .dump_settings = bvt_dump_settings,
571 .dump_cpu_state = bvt_dump_cpu_state,
572 .sleep = bvt_sleep,
573 .wake = bvt_wake,
574 .set_affinity = bvt_set_affinity
575 };
577 /*
578 * Local variables:
579 * mode: C
580 * c-set-style: "BSD"
581 * c-basic-offset: 4
582 * tab-width: 4
583 * indent-tabs-mode: nil
584 * End:
585 */