gdunlap/sched-sim.hg

view sched_rr.c @ 1:ec2d50e41437

Handle multiple cpus.
author George Dunlap <gdunlap@xensource.com>
date Tue Oct 13 17:29:50 2009 +0100 (2009-10-13)
parents d27bb3c56e71
children 1d7310217c5a
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
5 #define ASSERT assert
7 #include "list.h"
8 #include "sim.h"
11 #define MAX_VMS 16
12 #define TSLICE 1000
14 struct sched_vm {
15 struct list_head queue;
16 int vid;
17 struct vm *v;
18 };
20 struct {
21 struct list_head queue;
22 struct sched_vm vms[MAX_VMS];
23 } sched_priv;
26 void sched_rr_init(void)
27 {
28 printf("%s()\n", __func__);
29 INIT_LIST_HEAD(&sched_priv.queue);
30 }
32 void sched_rr_vm_init(int vid)
33 {
34 struct sched_vm *svm;
36 printf("%s: vm %d\n", __func__, vid);
38 if ( vid > MAX_VMS )
39 {
40 fprintf(stderr, "vid %d > MAX_VMS %d!\n", vid, MAX_VMS);
41 exit(1);
42 }
44 svm = sched_priv.vms + vid;
46 INIT_LIST_HEAD(&svm->queue);
48 svm->vid = vid;
49 svm->v = vm_from_vid(vid);
51 }
53 void sched_rr_wake(int time, struct vm * v)
54 {
55 struct sched_vm *svm;
57 printf("%s: time %d vid %d\n",
58 __func__, time, v->vid);
60 svm = sched_priv.vms + v->vid;
62 ASSERT(list_empty(&svm->queue));
64 list_add_tail(&svm->queue, &sched_priv.queue);
66 /* Never preempt on wake; only kick idle processors */
67 if ( P.idle > 0 )
68 {
69 int i;
71 for ( i=0; i<P.count; i++ )
72 if ( P.pcpus[i].idle )
73 break;
75 printf(" %s: waking p%d\n", __func__, i);
76 sim_sched_timer(0, i);
77 }
78 }
80 struct vm* sched_rr_schedule(int time, int pid)
81 {
82 struct sched_vm *svm;
83 struct vm *next, *prev;
85 printf("%s: time %d pid %d\n",
86 __func__, time, pid);
87 prev = current(pid);
89 if ( prev )
90 {
91 printf(" current v%d\n", prev->vid);
92 svm = sched_priv.vms + prev->vid;
94 if ( svm->v->runstate == RUNSTATE_RUNNING )
95 {
96 printf(" adding to runqueue\n");
97 list_add_tail(&svm->queue, &sched_priv.queue);
98 }
99 }
101 /* Take guy on front of runqueue, set new timer */
102 if ( list_empty(&sched_priv.queue) )
103 {
104 printf(" No runnable entities\n");
105 return NULL;
106 }
108 svm = list_entry(sched_priv.queue.next, struct sched_vm, queue);
110 list_del_init(&svm->queue);
111 next = svm->v;
113 sim_sched_timer(TSLICE, pid);
115 printf(" next: v%d\n", next->vid);
117 return next;
118 }
120 struct scheduler sched_rr =
121 {
122 .name="round-robin",
123 .ops = {
124 .sched_init = sched_rr_init,
125 .vm_init = sched_rr_vm_init,
126 .wake = sched_rr_wake,
127 .schedule = sched_rr_schedule
128 }
129 };