debuggers.hg

view xen/common/sched_bvt.c @ 3763:0823f72cd071

bitkeeper revision 1.1159.223.80 (4208de05Xtv_u_3smJSRU6ex6bTAfA)

Some functions aren't static and could be (damn C language!).

I tried turning on -Wmissing-prototypes: unfortunately gives warnings
for functions used in asm, which means introducing gratuitous prototypes
for them. Not sure it's worth it.

1) keyhandler.c: keypress_softirq() and do_task_queues() can be static.
2) physdev.c: pcidev_dom0_hidden() can be static.
3) resource.c/resource.h: check_region is deprecated (racy): remove.
4) sched_bvt.c: lots of things can be static.
5) pci/compat.c: not required for Xen.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> (authored)
Signed-off-by: ian.pratt@cl.cam.ac.uk
author iap10@freefall.cl.cam.ac.uk
date Tue Feb 08 15:43:01 2005 +0000 (2005-02-08)
parents dee91b44a753
children a00d7a994a59 0dc3b8b8c298
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4 -*-
2 ****************************************************************************
3 * (C) 2002-2003 - Rolf Neugebauer - Intel Research Cambridge
4 * (C) 2002-2003 University of Cambridge
5 * (C) 2004 - Mark Williamson - Intel Research Cambridge
6 ****************************************************************************
7 *
8 * File: common/schedule.c
9 * Author: Rolf Neugebauer & Keir Fraser
10 * Updated for generic API by Mark Williamson
11 *
12 * Description: CPU scheduling
13 * implements A Borrowed Virtual Time scheduler.
14 * (see Duda & Cheriton SOSP'99)
15 */
17 #include <xen/config.h>
18 #include <xen/init.h>
19 #include <xen/lib.h>
20 #include <xen/sched.h>
21 #include <xen/delay.h>
22 #include <xen/event.h>
23 #include <xen/time.h>
24 #include <xen/ac_timer.h>
25 #include <xen/perfc.h>
26 #include <xen/sched-if.h>
27 #include <xen/slab.h>
28 #include <xen/softirq.h>
30 /* all per-domain BVT-specific scheduling info is stored here */
31 struct bvt_dom_info
32 {
33 struct domain *domain; /* domain this info belongs to */
34 struct list_head run_list; /* runqueue list pointers */
35 u32 mcu_advance; /* inverse of weight */
36 u32 avt; /* actual virtual time */
37 u32 evt; /* effective virtual time */
38 int warpback; /* warp? */
39 int warp; /* warp set and within the warp
40 limits*/
41 s32 warp_value; /* virtual time warp */
42 s_time_t warpl; /* warp limit */
43 struct ac_timer warp_timer; /* deals with warpl */
44 s_time_t warpu; /* unwarp time requirement */
45 struct ac_timer unwarp_timer; /* deals with warpu */
46 };
48 struct bvt_cpu_info
49 {
50 struct list_head runqueue;
51 unsigned long svt;
52 };
54 #define BVT_INFO(p) ((struct bvt_dom_info *)(p)->sched_priv)
55 #define CPU_INFO(cpu) ((struct bvt_cpu_info *)(schedule_data[cpu]).sched_priv)
56 #define RUNLIST(p) ((struct list_head *)&(BVT_INFO(p)->run_list))
57 #define RUNQUEUE(cpu) ((struct list_head *)&(CPU_INFO(cpu)->runqueue))
58 #define CPU_SVT(cpu) (CPU_INFO(cpu)->svt)
60 #define MCU (s32)MICROSECS(100) /* Minimum unit */
61 #define MCU_ADVANCE 10 /* default weight */
62 #define TIME_SLOP (s32)MICROSECS(50) /* allow time to slip a bit */
63 static s32 ctx_allow = (s32)MILLISECS(5); /* context switch allowance */
65 static xmem_cache_t *dom_info_cache;
67 static inline void __add_to_runqueue_head(struct domain *d)
68 {
69 list_add(RUNLIST(d), RUNQUEUE(d->processor));
70 }
72 static inline void __add_to_runqueue_tail(struct domain *d)
73 {
74 list_add_tail(RUNLIST(d), RUNQUEUE(d->processor));
75 }
77 static inline void __del_from_runqueue(struct domain *d)
78 {
79 struct list_head *runlist = RUNLIST(d);
80 list_del(runlist);
81 runlist->next = NULL;
82 }
84 static inline int __task_on_runqueue(struct domain *d)
85 {
86 return (RUNLIST(d))->next != NULL;
87 }
90 /* Warp/unwarp timer functions */
91 static void warp_timer_fn(unsigned long pointer)
92 {
93 struct bvt_dom_info *inf = (struct bvt_dom_info *)pointer;
94 unsigned int cpu = inf->domain->processor;
96 spin_lock_irq(&schedule_data[cpu].schedule_lock);
98 inf->warp = 0;
100 /* unwarp equal to zero => stop warping */
101 if ( inf->warpu == 0 )
102 {
103 inf->warpback = 0;
104 cpu_raise_softirq(cpu, SCHEDULE_SOFTIRQ);
105 }
107 /* set unwarp timer */
108 inf->unwarp_timer.expires = NOW() + inf->warpu;
109 add_ac_timer(&inf->unwarp_timer);
111 spin_unlock_irq(&schedule_data[cpu].schedule_lock);
112 }
114 static void unwarp_timer_fn(unsigned long pointer)
115 {
116 struct bvt_dom_info *inf = (struct bvt_dom_info *)pointer;
117 unsigned int cpu = inf->domain->processor;
119 spin_lock_irq(&schedule_data[cpu].schedule_lock);
121 if ( inf->warpback )
122 {
123 inf->warp = 1;
124 cpu_raise_softirq(cpu, SCHEDULE_SOFTIRQ);
125 }
127 spin_unlock_irq(&schedule_data[cpu].schedule_lock);
128 }
130 static inline u32 calc_avt(struct domain *d, s_time_t now)
131 {
132 u32 ranfor, mcus;
133 struct bvt_dom_info *inf = BVT_INFO(d);
135 ranfor = (u32)(now - d->lastschd);
136 mcus = (ranfor + MCU - 1)/MCU;
138 return inf->avt + mcus * inf->mcu_advance;
139 }
141 /*
142 * Calculate the effective virtual time for a domain. Take into account
143 * warping limits
144 */
145 static inline u32 calc_evt(struct domain *d, u32 avt)
146 {
147 struct bvt_dom_info *inf = BVT_INFO(d);
148 /* TODO The warp routines need to be rewritten GM */
150 if ( inf->warp )
151 return avt - inf->warp_value;
152 else
153 return avt;
154 }
156 /**
157 * bvt_alloc_task - allocate BVT private structures for a task
158 * @p: task to allocate private structures for
159 *
160 * Returns non-zero on failure.
161 */
162 static int bvt_alloc_task(struct domain *d)
163 {
164 if ( (d->sched_priv = xmem_cache_alloc(dom_info_cache)) == NULL )
165 return -1;
166 memset(d->sched_priv, 0, sizeof(struct bvt_dom_info));
167 return 0;
168 }
170 /*
171 * Add and remove a domain
172 */
173 static void bvt_add_task(struct domain *d)
174 {
175 struct bvt_dom_info *inf = BVT_INFO(d);
176 ASSERT(inf != NULL);
177 ASSERT(d != NULL);
179 inf->mcu_advance = MCU_ADVANCE;
180 inf->domain = d;
181 inf->warpback = 0;
182 /* Set some default values here. */
183 inf->warp = 0;
184 inf->warp_value = 0;
185 inf->warpl = MILLISECS(2000);
186 inf->warpu = MILLISECS(1000);
187 /* initialise the timers */
188 init_ac_timer(&inf->warp_timer);
189 inf->warp_timer.cpu = d->processor;
190 inf->warp_timer.data = (unsigned long)inf;
191 inf->warp_timer.function = &warp_timer_fn;
192 init_ac_timer(&inf->unwarp_timer);
193 inf->unwarp_timer.cpu = d->processor;
194 inf->unwarp_timer.data = (unsigned long)inf;
195 inf->unwarp_timer.function = &unwarp_timer_fn;
197 if ( d->id == IDLE_DOMAIN_ID )
198 {
199 inf->avt = inf->evt = ~0U;
200 }
201 else
202 {
203 /* Set avt and evt to system virtual time. */
204 inf->avt = CPU_SVT(d->processor);
205 inf->evt = CPU_SVT(d->processor);
206 }
207 }
209 static int bvt_init_idle_task(struct domain *p)
210 {
211 if ( bvt_alloc_task(p) < 0 )
212 return -1;
214 bvt_add_task(p);
216 set_bit(DF_RUNNING, &p->flags);
217 if ( !__task_on_runqueue(p) )
218 __add_to_runqueue_head(p);
220 return 0;
221 }
223 static void bvt_wake(struct domain *d)
224 {
225 struct bvt_dom_info *inf = BVT_INFO(d);
226 struct domain *curr;
227 s_time_t now, r_time;
228 int cpu = d->processor;
229 u32 curr_evt;
231 if ( unlikely(__task_on_runqueue(d)) )
232 return;
234 __add_to_runqueue_head(d);
236 now = NOW();
238 /* Set the BVT parameters. AVT should always be updated
239 if CPU migration ocurred.*/
240 if ( inf->avt < CPU_SVT(cpu) ||
241 unlikely(test_bit(DF_MIGRATED, &d->flags)) )
242 inf->avt = CPU_SVT(cpu);
244 /* Deal with warping here. */
245 inf->evt = calc_evt(d, inf->avt);
247 curr = schedule_data[cpu].curr;
248 curr_evt = calc_evt(curr, calc_avt(curr, now));
249 /* Calculate the time the current domain would run assuming
250 the second smallest evt is of the newly woken domain */
251 r_time = curr->lastschd +
252 ((inf->evt - curr_evt) / BVT_INFO(curr)->mcu_advance) +
253 ctx_allow;
255 if ( is_idle_task(curr) || (inf->evt <= curr_evt) )
256 cpu_raise_softirq(cpu, SCHEDULE_SOFTIRQ);
257 else if ( schedule_data[cpu].s_timer.expires > r_time )
258 mod_ac_timer(&schedule_data[cpu].s_timer, r_time);
259 }
262 static void bvt_sleep(struct domain *d)
263 {
264 if ( test_bit(DF_RUNNING, &d->flags) )
265 cpu_raise_softirq(d->processor, SCHEDULE_SOFTIRQ);
266 else if ( __task_on_runqueue(d) )
267 __del_from_runqueue(d);
268 }
270 /**
271 * bvt_free_task - free BVT private structures for a task
272 * @d: task
273 */
274 static void bvt_free_task(struct domain *d)
275 {
276 ASSERT(d->sched_priv != NULL);
277 xmem_cache_free(dom_info_cache, d->sched_priv);
278 }
280 /* Control the scheduler. */
281 static int bvt_ctl(struct sched_ctl_cmd *cmd)
282 {
283 struct bvt_ctl *params = &cmd->u.bvt;
285 if ( cmd->direction == SCHED_INFO_PUT )
286 ctx_allow = params->ctx_allow;
287 else
288 params->ctx_allow = ctx_allow;
290 return 0;
291 }
293 /* Adjust scheduling parameter for a given domain. */
294 static int bvt_adjdom(
295 struct domain *d, struct sched_adjdom_cmd *cmd)
296 {
297 struct bvt_adjdom *params = &cmd->u.bvt;
299 if ( cmd->direction == SCHED_INFO_PUT )
300 {
301 u32 mcu_adv = params->mcu_adv;
302 u32 warpback = params->warpback;
303 s32 warpvalue = params->warpvalue;
304 s_time_t warpl = params->warpl;
305 s_time_t warpu = params->warpu;
307 struct bvt_dom_info *inf = BVT_INFO(d);
309 /* Sanity -- this can avoid divide-by-zero. */
310 if ( (mcu_adv == 0) || (warpl < 0) || (warpu < 0) )
311 return -EINVAL;
313 inf->mcu_advance = mcu_adv;
314 inf->warpback = warpback;
315 /* The warp should be the same as warpback */
316 inf->warp = warpback;
317 inf->warp_value = warpvalue;
318 inf->warpl = MILLISECS(warpl);
319 inf->warpu = MILLISECS(warpu);
321 /* If the unwarp timer set up it needs to be removed */
322 rem_ac_timer(&inf->unwarp_timer);
323 /* If we stop warping the warp timer needs to be removed */
324 if ( !warpback )
325 rem_ac_timer(&inf->warp_timer);
326 }
327 else if ( cmd->direction == SCHED_INFO_GET )
328 {
329 struct bvt_dom_info *inf = BVT_INFO(d);
330 params->mcu_adv = inf->mcu_advance;
331 params->warpvalue = inf->warp_value;
332 params->warpback = inf->warpback;
333 params->warpl = inf->warpl;
334 params->warpu = inf->warpu;
335 }
337 return 0;
338 }
341 /*
342 * The main function
343 * - deschedule the current domain.
344 * - pick a new domain.
345 * i.e., the domain with lowest EVT.
346 * The runqueue should be ordered by EVT so that is easy.
347 */
348 static task_slice_t bvt_do_schedule(s_time_t now)
349 {
350 struct domain *prev = current, *next = NULL, *next_prime, *p;
351 int cpu = prev->processor;
352 s32 r_time; /* time for new dom to run */
353 u32 next_evt, next_prime_evt, min_avt;
354 struct bvt_dom_info *prev_inf = BVT_INFO(prev);
355 struct bvt_dom_info *p_inf = NULL;
356 struct bvt_dom_info *next_inf = NULL;
357 struct bvt_dom_info *next_prime_inf = NULL;
358 task_slice_t ret;
360 ASSERT(prev->sched_priv != NULL);
361 ASSERT(prev_inf != NULL);
362 ASSERT(__task_on_runqueue(prev));
364 if ( likely(!is_idle_task(prev)) )
365 {
366 prev_inf->avt = calc_avt(prev, now);
367 prev_inf->evt = calc_evt(prev, prev_inf->avt);
369 if(prev_inf->warpback && prev_inf->warpl > 0)
370 rem_ac_timer(&prev_inf->warp_timer);
372 __del_from_runqueue(prev);
374 if ( domain_runnable(prev) )
375 __add_to_runqueue_tail(prev);
376 }
379 /* We should at least have the idle task */
380 ASSERT(!list_empty(RUNQUEUE(cpu)));
382 /*
383 * scan through the run queue and pick the task with the lowest evt
384 * *and* the task the second lowest evt.
385 * this code is O(n) but we expect n to be small.
386 */
387 next_inf = BVT_INFO(schedule_data[cpu].idle);
388 next_prime_inf = NULL;
390 next_evt = ~0U;
391 next_prime_evt = ~0U;
392 min_avt = ~0U;
394 list_for_each_entry ( p_inf, RUNQUEUE(cpu), run_list )
395 {
396 if ( p_inf->evt < next_evt )
397 {
398 next_prime_inf = next_inf;
399 next_prime_evt = next_evt;
400 next_inf = p_inf;
401 next_evt = p_inf->evt;
402 }
403 else if ( next_prime_evt == ~0U )
404 {
405 next_prime_evt = p_inf->evt;
406 next_prime_inf = p_inf;
407 }
408 else if ( p_inf->evt < next_prime_evt )
409 {
410 next_prime_evt = p_inf->evt;
411 next_prime_inf = p_inf;
412 }
414 /* Determine system virtual time. */
415 if ( p_inf->avt < min_avt )
416 min_avt = p_inf->avt;
417 }
419 if(next_inf->warp && next_inf->warpl > 0)
420 {
421 /* Set the timer up */
422 next_inf->warp_timer.expires = now + next_inf->warpl;
423 /* Add it to the heap */
424 add_ac_timer(&next_inf->warp_timer);
425 }
427 /* Extract the domain pointers from the dom infos */
428 next = next_inf->domain;
429 next_prime = next_prime_inf->domain;
431 /* Update system virtual time. */
432 if ( min_avt != ~0U )
433 CPU_SVT(cpu) = min_avt;
435 /* check for virtual time overrun on this cpu */
436 if ( CPU_SVT(cpu) >= 0xf0000000 )
437 {
438 ASSERT(!local_irq_is_enabled());
440 write_lock(&domlist_lock);
442 for_each_domain ( p )
443 {
444 if ( p->processor == cpu )
445 {
446 p_inf = BVT_INFO(p);
447 p_inf->evt -= 0xe0000000;
448 p_inf->avt -= 0xe0000000;
449 }
450 }
452 write_unlock(&domlist_lock);
454 CPU_SVT(cpu) -= 0xe0000000;
455 }
457 /* work out time for next run through scheduler */
458 if ( is_idle_task(next) )
459 {
460 r_time = ctx_allow;
461 goto sched_done;
462 }
464 if ( (next_prime == NULL) || is_idle_task(next_prime) )
465 {
466 /* We have only one runnable task besides the idle task. */
467 r_time = 10 * ctx_allow; /* RN: random constant */
468 goto sched_done;
469 }
471 /*
472 * If we are here then we have two runnable tasks.
473 * Work out how long 'next' can run till its evt is greater than
474 * 'next_prime's evt. Take context switch allowance into account.
475 */
476 ASSERT(next_prime_inf->evt >= next_inf->evt);
478 r_time = ((next_prime_inf->evt - next_inf->evt)/next_inf->mcu_advance)
479 + ctx_allow;
481 ASSERT(r_time >= ctx_allow);
483 sched_done:
484 ret.task = next;
485 ret.time = r_time;
486 return ret;
487 }
490 static void bvt_dump_runq_el(struct domain *p)
491 {
492 struct bvt_dom_info *inf = BVT_INFO(p);
494 printk("mcua=%d ev=0x%08X av=0x%08X ",
495 inf->mcu_advance, inf->evt, inf->avt);
496 }
498 static void bvt_dump_settings(void)
499 {
500 printk("BVT: mcu=0x%08Xns ctx_allow=0x%08Xns ", (u32)MCU, (s32)ctx_allow );
501 }
503 static void bvt_dump_cpu_state(int i)
504 {
505 struct list_head *queue;
506 int loop = 0;
507 struct bvt_dom_info *d_inf;
508 struct domain *d;
510 printk("svt=0x%08lX ", CPU_SVT(i));
512 queue = RUNQUEUE(i);
513 printk("QUEUE rq %lx n: %lx, p: %lx\n", (unsigned long)queue,
514 (unsigned long) queue->next, (unsigned long) queue->prev);
516 list_for_each_entry ( d_inf, queue, run_list )
517 {
518 d = d_inf->domain;
519 printk("%3d: %u has=%c ", loop++, d->id,
520 test_bit(DF_RUNNING, &d->flags) ? 'T':'F');
521 bvt_dump_runq_el(d);
522 printk("c=0x%X%08X\n", (u32)(d->cpu_time>>32), (u32)d->cpu_time);
523 printk(" l: %p n: %p p: %p\n",
524 &d_inf->run_list, d_inf->run_list.next, d_inf->run_list.prev);
525 }
526 }
528 /* Initialise the data structures. */
529 static int bvt_init_scheduler(void)
530 {
531 int i;
533 for ( i = 0; i < NR_CPUS; i++ )
534 {
535 schedule_data[i].sched_priv = xmalloc(sizeof(struct bvt_cpu_info));
537 if ( schedule_data[i].sched_priv == NULL )
538 {
539 printk("Failed to allocate BVT scheduler per-CPU memory!\n");
540 return -1;
541 }
543 INIT_LIST_HEAD(RUNQUEUE(i));
545 CPU_SVT(i) = 0; /* XXX do I really need to do this? */
546 }
548 dom_info_cache = xmem_cache_create(
549 "BVT dom info", sizeof(struct bvt_dom_info), 0, 0, NULL, NULL);
550 if ( dom_info_cache == NULL )
551 {
552 printk("BVT: Failed to allocate domain info SLAB cache");
553 return -1;
554 }
556 return 0;
557 }
559 struct scheduler sched_bvt_def = {
560 .name = "Borrowed Virtual Time",
561 .opt_name = "bvt",
562 .sched_id = SCHED_BVT,
564 .init_scheduler = bvt_init_scheduler,
565 .init_idle_task = bvt_init_idle_task,
566 .alloc_task = bvt_alloc_task,
567 .add_task = bvt_add_task,
568 .free_task = bvt_free_task,
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 };