gdunlap/sched-sim.hg

view sched_credit02.c @ 12:f34462422a94

c02: Add time-for-equal-credit
author George Dunlap <gdunlap@xensource.com>
date Tue Oct 20 18:15:55 2009 +0100 (2009-10-20)
parents 31f36108fc43
children cb624ff1d4fe
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 CREDIT_INIT 500
13 #define CREDIT_RESET 0
14 #define MAX_TIMER 200
15 #define MIN_TIMER 50
17 struct sched_vm {
18 struct list_head runq_elem;
19 struct vm *v;
21 int weight;
23 int credit;
24 int credit_per_min_timer; /* ? */
25 int start_time;
26 int vid;
27 };
29 struct {
30 struct list_head runq; /* Global run queue */
31 int max_vm;
32 struct sched_vm vms[MAX_VMS];
33 int ncpus;
35 int global_weight;
36 int scale_factor; /* ? */
38 int next_check;
39 } sched_priv;
43 static void reset_credit(int time)
44 {
45 int i;
46 for ( i=0; i<=sched_priv.max_vm; i++)
47 {
48 sched_priv.vms[i].credit = CREDIT_INIT;
49 sched_priv.vms[i].start_time = time;
50 }
51 /* No need to resort runq, as everyone's credit is now zero */
52 }
54 static void dump_credit(int time, struct sched_vm *svm)
55 {
56 printf("credit v%d %d %d\n", svm->vid, time, svm->credit);
57 }
59 static void burn_credit(struct sched_vm *svm, int time)
60 {
61 ASSERT(time >= svm->start_time);
63 svm->credit -= (time - svm->start_time);
64 svm->start_time = time;
66 dump_credit(time, svm);
67 }
69 static int calc_timer(struct sched_vm *svm)
70 {
71 int time = MAX_TIMER;
73 if ( time > svm->credit )
74 time = svm->credit;
76 if ( !list_empty(&sched_priv.runq) )
77 {
78 struct sched_vm *sq = list_entry(sched_priv.runq.next, struct sched_vm, runq_elem);
80 ASSERT(svm->credit >= sq->credit);
82 if ( time > (svm->credit - sq->credit) )
83 time = (svm->credit - sq->credit);
84 }
87 if ( time < MIN_TIMER )
88 time = MIN_TIMER;
89 return time;
90 }
92 static void runq_insert(struct sched_vm *svm)
93 {
94 struct list_head *iter;
95 int pos = 0;
97 list_for_each( iter, &sched_priv.runq )
98 {
99 struct sched_vm * iter_svm;
101 iter_svm = list_entry(iter, struct sched_vm, runq_elem);
103 if ( svm->credit > iter_svm->credit )
104 {
105 printf(" p%d v%d\n",
106 pos,
107 iter_svm->vid);
108 break;
109 }
110 pos++;
111 }
113 list_add_tail(&svm->runq_elem, iter);
114 }
116 static void sched_credit_init(void)
117 {
118 printf("%s()\n", __func__);
119 INIT_LIST_HEAD(&sched_priv.runq);
120 sched_priv.max_vm=0;
121 }
123 static void sched_credit_vm_init(int vid)
124 {
125 struct sched_vm *svm;
127 printf("%s: vm %d\n", __func__, vid);
129 if ( vid > MAX_VMS )
130 {
131 fprintf(stderr, "vid %d > MAX_VMS %d!\n", vid, MAX_VMS);
132 exit(1);
133 }
135 svm = sched_priv.vms + vid;
137 INIT_LIST_HEAD(&svm->runq_elem);
139 svm->vid = vid;
140 svm->v = vm_from_vid(vid);
142 svm->credit = CREDIT_INIT;
143 svm->weight = 1;
144 svm->start_time = 0;
146 if ( vid > sched_priv.max_vm )
147 sched_priv.max_vm = vid;
148 }
150 static void sched_credit_wake(int time, int vid)
151 {
152 struct vm *v;
153 struct sched_vm *svm;
155 v = vm_from_vid(vid);
157 printf("%s: time %d vid %d\n",
158 __func__, time, v->vid);
160 svm = sched_priv.vms + v->vid;
162 ASSERT(list_empty(&svm->runq_elem));
164 runq_insert(svm);
166 /* Scan for either:
167 * + an idle cpu to wake up, or
168 * + if there are cpus with lower credits, the lowest one
169 */
170 {
171 int i, ipid=-1, lowest = svm->credit;
174 for ( i=0; i<P.count; i++ )
175 {
176 if ( P.pcpus[i].idle )
177 {
178 printf(" %s: p%d idle, waking\n", __func__, i);
179 ipid=i;
180 break;
181 }
182 else if ( P.pcpus[i].current )
183 {
184 struct vm* ovm = P.pcpus[i].current;
185 int ovid = ovm->vid;
186 struct sched_vm *osvm = sched_priv.vms + ovid;
188 /* Update credits of currently running VM */
189 burn_credit(osvm, time);
191 if ( osvm->credit < lowest )
192 {
193 ipid = i;
194 lowest = osvm->credit;
195 }
196 }
198 }
200 if ( ipid >= 0 )
201 sim_sched_timer(0, ipid);
202 else
203 dump_credit(time, svm);
204 }
205 }
207 static struct vm* sched_credit_schedule(int time, int pid)
208 {
209 struct sched_vm *svm;
210 struct vm *next, *prev;
211 int timer;
213 printf("%s: time %d pid %d\n",
214 __func__, time, pid);
215 prev = current(pid);
217 if ( prev )
218 {
219 printf(" current v%d\n", prev->vid);
220 svm = sched_priv.vms + prev->vid;
222 burn_credit(svm, time);
224 if ( svm->v->runstate == RUNSTATE_RUNNING )
225 {
226 printf(" adding to runqueue\n");
227 runq_insert(svm);
228 }
229 }
231 /* Take guy on front of runqueue, set new timer */
232 if ( list_empty(&sched_priv.runq) )
233 {
234 printf(" No runnable entities\n");
235 return NULL;
236 }
238 svm = list_entry(sched_priv.runq.next, struct sched_vm, runq_elem);
240 list_del_init(&svm->runq_elem);
242 next = svm->v;
244 if ( svm->credit <= CREDIT_RESET )
245 {
246 printf(" vid %d credit %c, resetting credit at time %d\n",
247 svm->vid,
248 svm->credit,
249 time);
250 reset_credit(time);
251 }
253 dump_credit(time, svm);
254 svm->start_time = time;
256 timer = calc_timer(svm);
258 sim_sched_timer(timer, pid);
260 printf(" next: v%d\n", next->vid);
262 return next;
263 }
265 struct scheduler sched_credit02 =
266 {
267 .name="credit02",
268 .desc="Zero-start, burn based on weight, reset to zero at negative credit, with time-on-credit-equal",
269 .ops = {
270 .sched_init = sched_credit_init,
271 .vm_init = sched_credit_vm_init,
272 .wake = sched_credit_wake,
273 .schedule = sched_credit_schedule
274 }
275 };