ia64/xen-unstable

view xen/common/schedule.c @ 18450:1a7705924dbe

xentrace 1/7: Trace runstate changes.

Also, introduce SCHED_MIN and SCHED_VERBOSE, so that we can trace
just runstate changes if we want to.

Signed-off-by: George Dunlap <george.dunlap@eu.citrix.com>
Signed-off-by: Trolle Selander <trolle.selander@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Sep 08 15:47:47 2008 +0100 (2008-09-08)
parents ae9b223a675d
children 4ffc70556000
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: Generic CPU scheduling code
12 * implements support functionality for the Xen scheduler API.
13 *
14 */
16 #ifndef COMPAT
17 #include <xen/config.h>
18 #include <xen/init.h>
19 #include <xen/lib.h>
20 #include <xen/sched.h>
21 #include <xen/domain.h>
22 #include <xen/delay.h>
23 #include <xen/event.h>
24 #include <xen/time.h>
25 #include <xen/timer.h>
26 #include <xen/perfc.h>
27 #include <xen/sched-if.h>
28 #include <xen/softirq.h>
29 #include <xen/trace.h>
30 #include <xen/mm.h>
31 #include <xen/errno.h>
32 #include <xen/guest_access.h>
33 #include <xen/multicall.h>
34 #include <public/sched.h>
35 #include <xsm/xsm.h>
37 /* opt_sched: scheduler - default to credit */
38 static char opt_sched[10] = "credit";
39 string_param("sched", opt_sched);
41 #define TIME_SLOP (s32)MICROSECS(50) /* allow time to slip a bit */
43 /* Various timer handlers. */
44 static void s_timer_fn(void *unused);
45 static void vcpu_periodic_timer_fn(void *data);
46 static void vcpu_singleshot_timer_fn(void *data);
47 static void poll_timer_fn(void *data);
49 /* This is global for now so that private implementations can reach it */
50 DEFINE_PER_CPU(struct schedule_data, schedule_data);
52 extern struct scheduler sched_sedf_def;
53 extern struct scheduler sched_credit_def;
54 static struct scheduler *schedulers[] = {
55 &sched_sedf_def,
56 &sched_credit_def,
57 NULL
58 };
60 static struct scheduler ops;
62 #define SCHED_OP(fn, ...) \
63 (( ops.fn != NULL ) ? ops.fn( __VA_ARGS__ ) \
64 : (typeof(ops.fn(__VA_ARGS__)))0 )
66 static inline void trace_runstate_change(struct vcpu *v, int new_state)
67 {
68 struct { uint32_t vcpu:16, domain:16; } d;
69 uint32_t event;
71 if ( likely(!tb_init_done) )
72 return;
74 d.vcpu = v->vcpu_id;
75 d.domain = v->domain->domain_id;
77 event = TRC_SCHED_RUNSTATE_CHANGE;
78 event |= ( v->runstate.state & 0x3 ) << 8;
79 event |= ( new_state & 0x3 ) << 4;
81 __trace_var(event, 1/*tsc*/, sizeof(d), (unsigned char *)&d);
82 }
84 static inline void vcpu_runstate_change(
85 struct vcpu *v, int new_state, s_time_t new_entry_time)
86 {
87 ASSERT(v->runstate.state != new_state);
88 ASSERT(spin_is_locked(&per_cpu(schedule_data,v->processor).schedule_lock));
90 trace_runstate_change(v, new_state);
92 v->runstate.time[v->runstate.state] +=
93 new_entry_time - v->runstate.state_entry_time;
94 v->runstate.state_entry_time = new_entry_time;
95 v->runstate.state = new_state;
96 }
98 void vcpu_runstate_get(struct vcpu *v, struct vcpu_runstate_info *runstate)
99 {
100 if ( likely(v == current) )
101 {
102 /* Fast lock-free path. */
103 memcpy(runstate, &v->runstate, sizeof(*runstate));
104 ASSERT(runstate->state == RUNSTATE_running);
105 runstate->time[RUNSTATE_running] += NOW() - runstate->state_entry_time;
106 }
107 else
108 {
109 vcpu_schedule_lock_irq(v);
110 memcpy(runstate, &v->runstate, sizeof(*runstate));
111 runstate->time[runstate->state] += NOW() - runstate->state_entry_time;
112 vcpu_schedule_unlock_irq(v);
113 }
114 }
116 int sched_init_vcpu(struct vcpu *v, unsigned int processor)
117 {
118 struct domain *d = v->domain;
120 /*
121 * Initialize processor and affinity settings. The idler, and potentially
122 * domain-0 VCPUs, are pinned onto their respective physical CPUs.
123 */
124 v->processor = processor;
125 if ( is_idle_domain(d) || d->is_pinned )
126 v->cpu_affinity = cpumask_of_cpu(processor);
127 else
128 cpus_setall(v->cpu_affinity);
130 /* Initialise the per-vcpu timers. */
131 init_timer(&v->periodic_timer, vcpu_periodic_timer_fn,
132 v, v->processor);
133 init_timer(&v->singleshot_timer, vcpu_singleshot_timer_fn,
134 v, v->processor);
135 init_timer(&v->poll_timer, poll_timer_fn,
136 v, v->processor);
138 /* Idle VCPUs are scheduled immediately. */
139 if ( is_idle_domain(d) )
140 {
141 per_cpu(schedule_data, v->processor).curr = v;
142 per_cpu(schedule_data, v->processor).idle = v;
143 v->is_running = 1;
144 }
146 TRACE_2D(TRC_SCHED_DOM_ADD, v->domain->domain_id, v->vcpu_id);
148 return SCHED_OP(init_vcpu, v);
149 }
151 void sched_destroy_vcpu(struct vcpu *v)
152 {
153 kill_timer(&v->periodic_timer);
154 kill_timer(&v->singleshot_timer);
155 kill_timer(&v->poll_timer);
156 SCHED_OP(destroy_vcpu, v);
157 }
159 int sched_init_domain(struct domain *d)
160 {
161 return SCHED_OP(init_domain, d);
162 }
164 void sched_destroy_domain(struct domain *d)
165 {
166 SCHED_OP(destroy_domain, d);
167 }
169 void vcpu_sleep_nosync(struct vcpu *v)
170 {
171 unsigned long flags;
173 vcpu_schedule_lock_irqsave(v, flags);
175 if ( likely(!vcpu_runnable(v)) )
176 {
177 if ( v->runstate.state == RUNSTATE_runnable )
178 vcpu_runstate_change(v, RUNSTATE_offline, NOW());
180 SCHED_OP(sleep, v);
181 }
183 vcpu_schedule_unlock_irqrestore(v, flags);
185 TRACE_2D(TRC_SCHED_SLEEP, v->domain->domain_id, v->vcpu_id);
186 }
188 void vcpu_sleep_sync(struct vcpu *v)
189 {
190 vcpu_sleep_nosync(v);
192 while ( !vcpu_runnable(v) && v->is_running )
193 cpu_relax();
195 sync_vcpu_execstate(v);
196 }
198 void vcpu_wake(struct vcpu *v)
199 {
200 unsigned long flags;
202 vcpu_schedule_lock_irqsave(v, flags);
204 if ( likely(vcpu_runnable(v)) )
205 {
206 if ( v->runstate.state >= RUNSTATE_blocked )
207 vcpu_runstate_change(v, RUNSTATE_runnable, NOW());
208 SCHED_OP(wake, v);
209 }
210 else if ( !test_bit(_VPF_blocked, &v->pause_flags) )
211 {
212 if ( v->runstate.state == RUNSTATE_blocked )
213 vcpu_runstate_change(v, RUNSTATE_offline, NOW());
214 }
216 vcpu_schedule_unlock_irqrestore(v, flags);
218 TRACE_2D(TRC_SCHED_WAKE, v->domain->domain_id, v->vcpu_id);
219 }
221 void vcpu_unblock(struct vcpu *v)
222 {
223 if ( !test_and_clear_bit(_VPF_blocked, &v->pause_flags) )
224 return;
226 /* Polling period ends when a VCPU is unblocked. */
227 if ( unlikely(v->poll_evtchn != 0) )
228 {
229 v->poll_evtchn = 0;
230 /*
231 * We *must* re-clear _VPF_blocked to avoid racing other wakeups of
232 * this VCPU (and it then going back to sleep on poll_mask).
233 * Test-and-clear is idiomatic and ensures clear_bit not reordered.
234 */
235 if ( test_and_clear_bit(v->vcpu_id, v->domain->poll_mask) )
236 clear_bit(_VPF_blocked, &v->pause_flags);
237 }
239 vcpu_wake(v);
240 }
242 static void vcpu_migrate(struct vcpu *v)
243 {
244 unsigned long flags;
245 int old_cpu;
247 vcpu_schedule_lock_irqsave(v, flags);
249 /*
250 * NB. Check of v->running happens /after/ setting migration flag
251 * because they both happen in (different) spinlock regions, and those
252 * regions are strictly serialised.
253 */
254 if ( v->is_running ||
255 !test_and_clear_bit(_VPF_migrating, &v->pause_flags) )
256 {
257 vcpu_schedule_unlock_irqrestore(v, flags);
258 return;
259 }
261 /* Switch to new CPU, then unlock old CPU. */
262 old_cpu = v->processor;
263 v->processor = SCHED_OP(pick_cpu, v);
264 spin_unlock_irqrestore(
265 &per_cpu(schedule_data, old_cpu).schedule_lock, flags);
267 /* Wake on new CPU. */
268 vcpu_wake(v);
269 }
271 /*
272 * Force a VCPU through a deschedule/reschedule path.
273 * For example, using this when setting the periodic timer period means that
274 * most periodic-timer state need only be touched from within the scheduler
275 * which can thus be done without need for synchronisation.
276 */
277 void vcpu_force_reschedule(struct vcpu *v)
278 {
279 vcpu_schedule_lock_irq(v);
280 if ( v->is_running )
281 set_bit(_VPF_migrating, &v->pause_flags);
282 vcpu_schedule_unlock_irq(v);
284 if ( test_bit(_VPF_migrating, &v->pause_flags) )
285 {
286 vcpu_sleep_nosync(v);
287 vcpu_migrate(v);
288 }
289 }
291 static int __vcpu_set_affinity(
292 struct vcpu *v, cpumask_t *affinity,
293 bool_t old_lock_status, bool_t new_lock_status)
294 {
295 cpumask_t online_affinity, old_affinity;
297 cpus_and(online_affinity, *affinity, cpu_online_map);
298 if ( cpus_empty(online_affinity) )
299 return -EINVAL;
301 vcpu_schedule_lock_irq(v);
303 if ( v->affinity_locked != old_lock_status )
304 {
305 BUG_ON(!v->affinity_locked);
306 vcpu_schedule_unlock_irq(v);
307 return -EBUSY;
308 }
310 v->affinity_locked = new_lock_status;
312 old_affinity = v->cpu_affinity;
313 v->cpu_affinity = *affinity;
314 *affinity = old_affinity;
315 if ( !cpu_isset(v->processor, v->cpu_affinity) )
316 set_bit(_VPF_migrating, &v->pause_flags);
318 vcpu_schedule_unlock_irq(v);
320 if ( test_bit(_VPF_migrating, &v->pause_flags) )
321 {
322 vcpu_sleep_nosync(v);
323 vcpu_migrate(v);
324 }
326 return 0;
327 }
329 int vcpu_set_affinity(struct vcpu *v, cpumask_t *affinity)
330 {
331 if ( v->domain->is_pinned )
332 return -EINVAL;
333 return __vcpu_set_affinity(v, affinity, 0, 0);
334 }
336 int vcpu_lock_affinity(struct vcpu *v, cpumask_t *affinity)
337 {
338 return __vcpu_set_affinity(v, affinity, 0, 1);
339 }
341 void vcpu_unlock_affinity(struct vcpu *v, cpumask_t *affinity)
342 {
343 cpumask_t online_affinity;
345 /* Do not fail if no CPU in old affinity mask is online. */
346 cpus_and(online_affinity, *affinity, cpu_online_map);
347 if ( cpus_empty(online_affinity) )
348 *affinity = cpu_online_map;
350 if ( __vcpu_set_affinity(v, affinity, 1, 0) != 0 )
351 BUG();
352 }
354 /* Block the currently-executing domain until a pertinent event occurs. */
355 static long do_block(void)
356 {
357 struct vcpu *v = current;
359 local_event_delivery_enable();
360 set_bit(_VPF_blocked, &v->pause_flags);
362 /* Check for events /after/ blocking: avoids wakeup waiting race. */
363 if ( local_events_need_delivery() )
364 {
365 clear_bit(_VPF_blocked, &v->pause_flags);
366 }
367 else
368 {
369 TRACE_2D(TRC_SCHED_BLOCK, v->domain->domain_id, v->vcpu_id);
370 raise_softirq(SCHEDULE_SOFTIRQ);
371 }
373 return 0;
374 }
376 static long do_poll(struct sched_poll *sched_poll)
377 {
378 struct vcpu *v = current;
379 struct domain *d = v->domain;
380 evtchn_port_t port;
381 long rc;
382 unsigned int i;
384 /* Fairly arbitrary limit. */
385 if ( sched_poll->nr_ports > 128 )
386 return -EINVAL;
388 if ( !guest_handle_okay(sched_poll->ports, sched_poll->nr_ports) )
389 return -EFAULT;
391 set_bit(_VPF_blocked, &v->pause_flags);
392 v->poll_evtchn = -1;
393 set_bit(v->vcpu_id, d->poll_mask);
395 #ifndef CONFIG_X86 /* set_bit() implies mb() on x86 */
396 /* Check for events /after/ setting flags: avoids wakeup waiting race. */
397 smp_mb();
399 /*
400 * Someone may have seen we are blocked but not that we are polling, or
401 * vice versa. We are certainly being woken, so clean up and bail. Beyond
402 * this point others can be guaranteed to clean up for us if they wake us.
403 */
404 rc = 0;
405 if ( (v->poll_evtchn == 0) ||
406 !test_bit(_VPF_blocked, &v->pause_flags) ||
407 !test_bit(v->vcpu_id, d->poll_mask) )
408 goto out;
409 #endif
411 for ( i = 0; i < sched_poll->nr_ports; i++ )
412 {
413 rc = -EFAULT;
414 if ( __copy_from_guest_offset(&port, sched_poll->ports, i, 1) )
415 goto out;
417 rc = -EINVAL;
418 if ( port >= MAX_EVTCHNS(d) )
419 goto out;
421 rc = 0;
422 if ( test_bit(port, &shared_info(d, evtchn_pending)) )
423 goto out;
424 }
426 if ( sched_poll->nr_ports == 1 )
427 v->poll_evtchn = port;
429 if ( sched_poll->timeout != 0 )
430 set_timer(&v->poll_timer, sched_poll->timeout);
432 TRACE_2D(TRC_SCHED_BLOCK, d->domain_id, v->vcpu_id);
433 raise_softirq(SCHEDULE_SOFTIRQ);
435 return 0;
437 out:
438 v->poll_evtchn = 0;
439 clear_bit(v->vcpu_id, d->poll_mask);
440 clear_bit(_VPF_blocked, &v->pause_flags);
441 return rc;
442 }
444 /* Voluntarily yield the processor for this allocation. */
445 static long do_yield(void)
446 {
447 TRACE_2D(TRC_SCHED_YIELD, current->domain->domain_id, current->vcpu_id);
448 raise_softirq(SCHEDULE_SOFTIRQ);
449 return 0;
450 }
452 long do_sched_op_compat(int cmd, unsigned long arg)
453 {
454 long ret = 0;
456 switch ( cmd )
457 {
458 case SCHEDOP_yield:
459 {
460 ret = do_yield();
461 break;
462 }
464 case SCHEDOP_block:
465 {
466 ret = do_block();
467 break;
468 }
470 case SCHEDOP_shutdown:
471 {
472 TRACE_3D(TRC_SCHED_SHUTDOWN,
473 current->domain->domain_id, current->vcpu_id, arg);
474 domain_shutdown(current->domain, (u8)arg);
475 break;
476 }
478 default:
479 ret = -ENOSYS;
480 }
482 return ret;
483 }
485 typedef long ret_t;
487 #endif /* !COMPAT */
489 ret_t do_sched_op(int cmd, XEN_GUEST_HANDLE(void) arg)
490 {
491 ret_t ret = 0;
493 switch ( cmd )
494 {
495 case SCHEDOP_yield:
496 {
497 ret = do_yield();
498 break;
499 }
501 case SCHEDOP_block:
502 {
503 ret = do_block();
504 break;
505 }
507 case SCHEDOP_shutdown:
508 {
509 struct sched_shutdown sched_shutdown;
511 ret = -EFAULT;
512 if ( copy_from_guest(&sched_shutdown, arg, 1) )
513 break;
515 ret = 0;
516 TRACE_3D(TRC_SCHED_SHUTDOWN,
517 current->domain->domain_id, current->vcpu_id,
518 sched_shutdown.reason);
519 domain_shutdown(current->domain, (u8)sched_shutdown.reason);
521 break;
522 }
524 case SCHEDOP_poll:
525 {
526 struct sched_poll sched_poll;
528 ret = -EFAULT;
529 if ( copy_from_guest(&sched_poll, arg, 1) )
530 break;
532 ret = do_poll(&sched_poll);
534 break;
535 }
537 case SCHEDOP_remote_shutdown:
538 {
539 struct domain *d;
540 struct sched_remote_shutdown sched_remote_shutdown;
542 ret = -EFAULT;
543 if ( copy_from_guest(&sched_remote_shutdown, arg, 1) )
544 break;
546 ret = -ESRCH;
547 d = rcu_lock_domain_by_id(sched_remote_shutdown.domain_id);
548 if ( d == NULL )
549 break;
551 if ( !IS_PRIV_FOR(current->domain, d) )
552 {
553 rcu_unlock_domain(d);
554 return -EPERM;
555 }
557 ret = xsm_schedop_shutdown(current->domain, d);
558 if ( ret )
559 {
560 rcu_unlock_domain(d);
561 return ret;
562 }
564 domain_shutdown(d, (u8)sched_remote_shutdown.reason);
566 rcu_unlock_domain(d);
567 ret = 0;
569 break;
570 }
572 default:
573 ret = -ENOSYS;
574 }
576 return ret;
577 }
579 #ifndef COMPAT
581 /* Per-vcpu oneshot-timer hypercall. */
582 long do_set_timer_op(s_time_t timeout)
583 {
584 struct vcpu *v = current;
585 s_time_t offset = timeout - NOW();
587 if ( timeout == 0 )
588 {
589 stop_timer(&v->singleshot_timer);
590 }
591 else if ( unlikely(timeout < 0) || /* overflow into 64th bit? */
592 unlikely((offset > 0) && ((uint32_t)(offset >> 50) != 0)) )
593 {
594 /*
595 * Linux workaround: occasionally we will see timeouts a long way in
596 * the future due to wrapping in Linux's jiffy time handling. We check
597 * for timeouts wrapped negative, and for positive timeouts more than
598 * about 13 days in the future (2^50ns). The correct fix is to trigger
599 * an interrupt immediately (since Linux in fact has pending work to
600 * do in this situation). However, older guests also set a long timeout
601 * when they have *no* pending timers at all: setting an immediate
602 * timeout in this case can burn a lot of CPU. We therefore go for a
603 * reasonable middleground of triggering a timer event in 100ms.
604 */
605 gdprintk(XENLOG_INFO,
606 "Warning: huge timeout set by vcpu %d: %"PRIx64"\n",
607 v->vcpu_id, (uint64_t)timeout);
608 set_timer(&v->singleshot_timer, NOW() + MILLISECS(100));
609 }
610 else
611 {
612 if ( v->singleshot_timer.cpu != smp_processor_id() )
613 {
614 stop_timer(&v->singleshot_timer);
615 v->singleshot_timer.cpu = smp_processor_id();
616 }
618 set_timer(&v->singleshot_timer, timeout);
619 }
621 return 0;
622 }
624 /* sched_id - fetch ID of current scheduler */
625 int sched_id(void)
626 {
627 return ops.sched_id;
628 }
630 /* Adjust scheduling parameter for a given domain. */
631 long sched_adjust(struct domain *d, struct xen_domctl_scheduler_op *op)
632 {
633 struct vcpu *v;
634 long ret;
636 if ( (op->sched_id != ops.sched_id) ||
637 ((op->cmd != XEN_DOMCTL_SCHEDOP_putinfo) &&
638 (op->cmd != XEN_DOMCTL_SCHEDOP_getinfo)) )
639 return -EINVAL;
641 /*
642 * Most VCPUs we can simply pause. If we are adjusting this VCPU then
643 * we acquire the local schedule_lock to guard against concurrent updates.
644 *
645 * We only acquire the local schedule lock after we have paused all other
646 * VCPUs in this domain. There are two reasons for this:
647 * 1- We don't want to hold up interrupts as pausing a VCPU can
648 * trigger a tlb shootdown.
649 * 2- Pausing other VCPUs involves briefly locking the schedule
650 * lock of the CPU they are running on. This CPU could be the
651 * same as ours.
652 */
654 for_each_vcpu ( d, v )
655 {
656 if ( v != current )
657 vcpu_pause(v);
658 }
660 if ( d == current->domain )
661 vcpu_schedule_lock_irq(current);
663 if ( (ret = SCHED_OP(adjust, d, op)) == 0 )
664 TRACE_1D(TRC_SCHED_ADJDOM, d->domain_id);
666 if ( d == current->domain )
667 vcpu_schedule_unlock_irq(current);
669 for_each_vcpu ( d, v )
670 {
671 if ( v != current )
672 vcpu_unpause(v);
673 }
675 return ret;
676 }
678 static void vcpu_periodic_timer_work(struct vcpu *v)
679 {
680 s_time_t now = NOW();
681 uint64_t periodic_next_event;
683 ASSERT(!active_timer(&v->periodic_timer));
685 if ( v->periodic_period == 0 )
686 return;
688 periodic_next_event = v->periodic_last_event + v->periodic_period;
690 /* The timer subsystem may call us up to TIME_SLOP ahead of deadline. */
691 if ( (now + TIME_SLOP) > periodic_next_event )
692 {
693 send_timer_event(v);
694 v->periodic_last_event = now;
695 periodic_next_event = now + v->periodic_period;
696 }
698 v->periodic_timer.cpu = smp_processor_id();
699 set_timer(&v->periodic_timer, periodic_next_event);
700 }
702 /*
703 * The main function
704 * - deschedule the current domain (scheduler independent).
705 * - pick a new domain (scheduler dependent).
706 */
707 static void schedule(void)
708 {
709 struct vcpu *prev = current, *next = NULL;
710 s_time_t now = NOW();
711 struct schedule_data *sd;
712 struct task_slice next_slice;
713 s32 r_time; /* time for new dom to run */
715 ASSERT(!in_irq());
716 ASSERT(this_cpu(mc_state).flags == 0);
718 perfc_incr(sched_run);
720 sd = &this_cpu(schedule_data);
722 spin_lock_irq(&sd->schedule_lock);
724 stop_timer(&sd->s_timer);
726 /* get policy-specific decision on scheduling... */
727 next_slice = ops.do_schedule(now);
729 r_time = next_slice.time;
730 next = next_slice.task;
732 sd->curr = next;
734 set_timer(&sd->s_timer, now + r_time);
736 if ( unlikely(prev == next) )
737 {
738 spin_unlock_irq(&sd->schedule_lock);
739 return continue_running(prev);
740 }
742 TRACE_2D(TRC_SCHED_SWITCH_INFPREV,
743 prev->domain->domain_id,
744 now - prev->runstate.state_entry_time);
745 TRACE_3D(TRC_SCHED_SWITCH_INFNEXT,
746 next->domain->domain_id,
747 (next->runstate.state == RUNSTATE_runnable) ?
748 (now - next->runstate.state_entry_time) : 0,
749 r_time);
751 ASSERT(prev->runstate.state == RUNSTATE_running);
752 vcpu_runstate_change(
753 prev,
754 (test_bit(_VPF_blocked, &prev->pause_flags) ? RUNSTATE_blocked :
755 (vcpu_runnable(prev) ? RUNSTATE_runnable : RUNSTATE_offline)),
756 now);
758 ASSERT(next->runstate.state != RUNSTATE_running);
759 vcpu_runstate_change(next, RUNSTATE_running, now);
761 ASSERT(!next->is_running);
762 next->is_running = 1;
764 spin_unlock_irq(&sd->schedule_lock);
766 perfc_incr(sched_ctx);
768 stop_timer(&prev->periodic_timer);
770 /* Ensure that the domain has an up-to-date time base. */
771 update_vcpu_system_time(next);
772 vcpu_periodic_timer_work(next);
774 TRACE_4D(TRC_SCHED_SWITCH,
775 prev->domain->domain_id, prev->vcpu_id,
776 next->domain->domain_id, next->vcpu_id);
778 context_switch(prev, next);
779 }
781 void context_saved(struct vcpu *prev)
782 {
783 /* Clear running flag /after/ writing context to memory. */
784 smp_wmb();
786 prev->is_running = 0;
788 /* Check for migration request /after/ clearing running flag. */
789 smp_mb();
791 if ( unlikely(test_bit(_VPF_migrating, &prev->pause_flags)) )
792 vcpu_migrate(prev);
793 }
795 /* The scheduler timer: force a run through the scheduler */
796 static void s_timer_fn(void *unused)
797 {
798 raise_softirq(SCHEDULE_SOFTIRQ);
799 perfc_incr(sched_irq);
800 }
802 /* Per-VCPU periodic timer function: sends a virtual timer interrupt. */
803 static void vcpu_periodic_timer_fn(void *data)
804 {
805 struct vcpu *v = data;
806 vcpu_periodic_timer_work(v);
807 }
809 /* Per-VCPU single-shot timer function: sends a virtual timer interrupt. */
810 static void vcpu_singleshot_timer_fn(void *data)
811 {
812 struct vcpu *v = data;
813 send_timer_event(v);
814 }
816 /* SCHEDOP_poll timeout callback. */
817 static void poll_timer_fn(void *data)
818 {
819 struct vcpu *v = data;
821 if ( test_and_clear_bit(v->vcpu_id, v->domain->poll_mask) )
822 vcpu_unblock(v);
823 }
825 /* Initialise the data structures. */
826 void __init scheduler_init(void)
827 {
828 int i;
830 open_softirq(SCHEDULE_SOFTIRQ, schedule);
832 for_each_cpu ( i )
833 {
834 spin_lock_init(&per_cpu(schedule_data, i).schedule_lock);
835 init_timer(&per_cpu(schedule_data, i).s_timer, s_timer_fn, NULL, i);
836 }
838 for ( i = 0; schedulers[i] != NULL; i++ )
839 {
840 ops = *schedulers[i];
841 if ( strcmp(ops.opt_name, opt_sched) == 0 )
842 break;
843 }
845 if ( schedulers[i] == NULL )
846 printk("Could not find scheduler: %s\n", opt_sched);
848 printk("Using scheduler: %s (%s)\n", ops.name, ops.opt_name);
849 SCHED_OP(init);
850 }
852 void dump_runq(unsigned char key)
853 {
854 s_time_t now = NOW();
855 int i;
856 unsigned long flags;
858 local_irq_save(flags);
860 printk("Scheduler: %s (%s)\n", ops.name, ops.opt_name);
861 SCHED_OP(dump_settings);
862 printk("NOW=0x%08X%08X\n", (u32)(now>>32), (u32)now);
864 for_each_online_cpu ( i )
865 {
866 spin_lock(&per_cpu(schedule_data, i).schedule_lock);
867 printk("CPU[%02d] ", i);
868 SCHED_OP(dump_cpu_state, i);
869 spin_unlock(&per_cpu(schedule_data, i).schedule_lock);
870 }
872 local_irq_restore(flags);
873 }
875 #ifdef CONFIG_COMPAT
876 #include "compat/schedule.c"
877 #endif
879 #endif /* !COMPAT */
881 /*
882 * Local variables:
883 * mode: C
884 * c-set-style: "BSD"
885 * c-basic-offset: 4
886 * tab-width: 4
887 * indent-tabs-mode: nil
888 * End:
889 */