gdunlap/sched-sim.hg

view simulator.c @ 5:18f3d6e25ffc

Add credit01 scheduler
author George Dunlap <gdunlap@xensource.com>
date Mon Oct 19 20:14:47 2009 +0100 (2009-10-19)
parents d957708efa86
children 403bd7680df6
line source
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <assert.h>
5 #define ASSERT assert
7 #include "stats.h"
8 #include "list.h"
9 #include "sim.h"
10 #include "workload.h"
11 #include "sched.h"
12 #include "options.h"
14 FILE *warn;
16 enum event_type {
17 EVT_BLOCK,
18 EVT_WAKE,
19 EVT_TIMER,
20 EVT_MAX
21 };
23 char *event_name[EVT_MAX] = {
24 [EVT_BLOCK]="block",
25 [EVT_WAKE] ="wake ",
26 [EVT_TIMER]="timer"
27 };
29 struct event {
30 struct list_head event_list;
31 enum event_type type;
32 int time;
33 int param; /* Usually VM ID */
34 };
36 char * state_name[STATE_MAX] = {
37 [STATE_RUN]= "run ",
38 [STATE_PREEMPT]="preempt",
39 [STATE_WAKE]= "wake ",
40 [STATE_BLOCK]= "block ",
41 };
43 struct {
44 int now;
45 struct list_head events;
46 struct list_head *timer;
47 const struct sched_ops *sched_ops;
48 } sim;
51 #ifndef VM_DATA_PUBLIC
52 struct global_vm_data {
53 int count;
54 struct vm vms[MAX_VMS];
55 };
56 #endif
57 struct global_vm_data V;
59 extern struct scheduler sched_rr;
60 extern struct scheduler sched_credit01;
61 int default_scheduler = 0;
62 struct scheduler *schedulers[] =
63 {
64 &sched_rr,
65 &sched_credit01,
66 NULL
67 };
69 /* Options */
71 struct global_pcpu_data P;
73 /* Sim list interface */
74 /* NB: Caller must free if they're not going to use it! */
75 #define list_event(_l) (list_entry((_l), struct event, event_list))
77 struct event* sim_remove_event(int type, int param)
78 {
79 struct event* ret = NULL;
80 struct list_head *pos, *tmp;
82 /* Look for an event that matches this one and remove it */
83 list_for_each_safe(pos, tmp, &sim.events)
84 {
85 struct event *tevt = list_event(pos);
86 if ( tevt->type == type
87 && tevt->param == param )
88 {
89 list_del(pos);
90 ret = tevt;
91 break;
92 }
93 }
95 return ret;
96 }
98 void sim_insert_event(int time, int type, int param, int reset)
99 {
100 struct list_head *pos = NULL;
101 struct event *evt=NULL;
103 ASSERT(time >= sim.now);
105 if ( reset )
106 evt=sim_remove_event(type, param);
108 if ( !evt )
109 evt = (struct event *)malloc(sizeof(*evt));
111 evt->time = time;
112 evt->type = type;
113 evt->param = param;
115 printf(" [insert t%d %s param%d]\n",
116 evt->time, event_name[evt->type], evt->param);
118 INIT_LIST_HEAD(&evt->event_list);
120 list_for_each(pos, &sim.events)
121 {
122 if ( list_event(pos)->time > evt->time )
123 break;
124 }
125 list_add_tail(&evt->event_list, pos);
126 }
128 struct event sim_next_event(void)
129 {
130 struct event *evt;
131 struct list_head *next;
133 ASSERT(!list_empty(&sim.events));
135 next=sim.events.next;
137 list_del(next);
139 evt=list_event(next);
141 printf("%d: evt %s param%d\n",
142 evt->time, event_name[evt->type], evt->param);
144 free(evt);
146 /* XXX */
147 return *evt;
148 }
150 /*
151 * VM simulation
152 */
153 void vm_next_event(struct vm *v)
154 {
155 v->phase_index = ( v->phase_index + 1 ) % v->workload->phase_count;
157 v->e = v->workload->list + v->phase_index;
158 }
160 struct vm* vm_from_vid(int vid)
161 {
162 if ( vid >= V.count )
163 {
164 fprintf(stderr, "%s: v%d >= V.count %d!\n",
165 __func__, vid, V.count);
166 exit(1);
167 }
169 return V.vms + vid;
170 }
172 void vm_block(int now, struct vm *v)
173 {
174 ASSERT(v->e->type == PHASE_RUN);
175 v->time_this_phase += now - v->state_start_time;
176 printf("%s: v%d time_this_phase %d (evt %d)\n",
177 __func__, v->vid, v->time_this_phase, v->e->time);
179 ASSERT(v->time_this_phase == v->e->time);
181 vm_next_event(v);
183 ASSERT(v->e->type == PHASE_BLOCK);
185 sim_insert_event(now + v->e->time, EVT_WAKE, v->vid, 0);
186 v->time_this_phase = 0;
187 v->was_preempted = 0;
188 }
190 /* Called when wake event happens; increment timer and reset state */
191 void vm_wake(int now, struct vm *v)
192 {
193 ASSERT(v->e->type == PHASE_BLOCK);
194 ASSERT(v->time_this_phase == 0);
196 v->time_this_phase = now - v->state_start_time;
198 if ( now != 0 )
199 ASSERT(v->time_this_phase == v->e->time);
201 vm_next_event(v);
203 v->time_this_phase = 0;
204 }
206 /* Called when actually starting to run; make block event and set state */
207 void vm_run(int now, struct vm *v)
208 {
209 ASSERT(v->e->type == PHASE_RUN);
210 ASSERT(v->time_this_phase <= v->e->time);
212 sim_insert_event(now + v->e->time - v->time_this_phase, EVT_BLOCK, v->vid, 0);
213 v->state_start_time = now;
214 }
216 /* Preempt: Remove block event, update amount of runtime (so that when it runs again we can accurately
217 * generate a new block event) */
218 void vm_preempt(int now, struct vm *v)
219 {
220 struct event* evt;
222 v->time_this_phase += now - v->state_start_time;
223 printf("%s: v%d time_this_phase %d (evt %d)\n",
224 __func__, v->vid, v->time_this_phase, v->e->time);
226 ASSERT( v->time_this_phase <= v->e->time );
228 /* Only remove block event if we still have more runtime left */
229 if ( ( evt = sim_remove_event(EVT_BLOCK, v->vid) ) )
230 free(evt);
232 v->was_preempted = 1;
233 }
236 /* Callbacks the scheduler may make */
237 void sim_sched_timer(int time, int pid)
238 {
239 if ( time < 0 )
240 {
241 fprintf(stderr, "%s: Time %d < 0!\n",
242 __func__, time);
243 abort();
244 }
246 if ( pid >= P.count )
247 {
248 fprintf(stderr, "%s: p%d >= P.count %d\n",
249 __func__, pid, P.count);
250 exit(1);
251 }
253 if ( P.pcpus[pid].idle )
254 {
255 P.pcpus[pid].idle = 0;
256 P.idle--;
257 }
258 sim_insert_event(sim.now + time, EVT_TIMER, pid, 1);
259 }
261 void sim_runstate_change(int now, struct vm *v, int new_runstate)
262 {
263 int ostate, nstate;
264 int stime = now - v->state_start_time;
266 /* Valid transitions:
267 * + R->A (preemption): remove block event
268 * + R->B (block) : Insert wake event
269 * + A->R (run) : Insert block event
270 * + B->A (wake) : No action necessary
271 */
273 switch ( v->runstate )
274 {
275 case RUNSTATE_RUNNING:
276 ostate = STATE_RUN;
277 break;
278 case RUNSTATE_RUNNABLE:
279 if ( v->was_preempted )
280 ostate = STATE_PREEMPT;
281 else
282 ostate = STATE_WAKE;
283 break;
284 case RUNSTATE_BLOCKED:
285 ostate = STATE_BLOCK;
286 break;
287 }
289 update_cycles(&v->stats.state[ostate], stime);
292 if ( v->runstate == RUNSTATE_RUNNING
293 && new_runstate == RUNSTATE_RUNNABLE )
294 {
295 nstate = STATE_PREEMPT;
296 vm_preempt(now, v);
297 }
298 else if ( v->runstate == RUNSTATE_RUNNING
299 && new_runstate == RUNSTATE_BLOCKED )
300 {
301 nstate = STATE_BLOCK;
302 vm_block(now, v);
303 }
304 else if ( v->runstate == RUNSTATE_RUNNABLE
305 && new_runstate == RUNSTATE_RUNNING )
306 {
307 nstate = STATE_RUN;
308 vm_run(now, v);
309 }
310 else if ( v->runstate == RUNSTATE_BLOCKED
311 && new_runstate == RUNSTATE_RUNNABLE )
312 {
313 nstate = STATE_WAKE;
314 vm_wake(now, v);
315 }
316 else
317 goto unexpected_transition;
319 printf("%d: v%d %s %d -> %s\n",
320 now, v->vid, state_name[ostate], stime, state_name[nstate]);
322 v->runstate = new_runstate;
323 v->state_start_time = now;
325 return;
327 unexpected_transition:
328 fprintf(stderr, "Unexpected transition for vm %d: %d->%d\n",
329 v->vid,
330 v->runstate,
331 new_runstate);
332 exit(1);
333 }
335 /*
336 * Main loop
337 */
338 void simulate(void)
339 {
340 while ( sim.now < opt.time_limit )
341 {
342 /* Take next event off list */
343 struct event evt;
345 evt = sim_next_event();
347 sim.now = evt.time;
349 switch(evt.type)
350 {
351 case EVT_WAKE:
352 {
353 struct vm *v = vm_from_vid(evt.param);
354 ASSERT(v->processor == -1);
355 sim_runstate_change(sim.now, v, RUNSTATE_RUNNABLE);
356 sim.sched_ops->wake(sim.now, v->vid);
357 }
358 break;
359 case EVT_BLOCK:
360 {
361 struct vm *v = vm_from_vid(evt.param);
363 ASSERT(v->processor != -1);
364 ASSERT(v->processor <= P.count);
366 sim_runstate_change(sim.now, v, RUNSTATE_BLOCKED);
368 evt.param = v->processor; /* FIXME */
369 }
370 /* FALL-THRU */
371 case EVT_TIMER:
372 {
373 struct vm *prev, *next;
374 int pid = evt.param;
376 ASSERT(pid < P.count);
378 prev = P.pcpus[pid].current;
380 next = sim.sched_ops->schedule(sim.now, pid);
382 if ( prev && prev != next )
383 {
384 prev->processor = -1;
385 if( prev->runstate != RUNSTATE_BLOCKED )
386 sim_runstate_change(sim.now, prev, RUNSTATE_RUNNABLE);
387 }
390 P.pcpus[pid].current = next;
391 if ( next )
392 {
393 if ( next != prev )
394 {
395 sim_runstate_change(sim.now, next, RUNSTATE_RUNNING);
396 next->processor = pid;
397 }
398 }
399 else
400 {
401 if ( P.pcpus[pid].idle )
402 {
403 fprintf(stderr, "Strange, pid %d already idle!\n",
404 pid);
405 abort();
406 }
408 /* If the pcpu is going idle, clear all timers from it */
409 sim_remove_event(EVT_TIMER, pid);
411 P.pcpus[pid].idle = 1;
412 P.idle++;
413 }
414 }
415 break;
416 default:
417 fprintf(stderr, "Unexpected event type: %d\n", evt.type);
418 exit(1);
419 break;
420 }
421 }
422 }
424 void init(void)
425 {
426 int vid, i;
427 const struct workload *w;
429 /* Initialize simulation variables */
430 sim.now=0;
431 sim.timer=NULL;
432 INIT_LIST_HEAD(&sim.events);
433 sim.sched_ops = &opt.scheduler->ops;
435 /* Initialize pcpus */
436 P.count = opt.pcpu_count;
437 P.idle = 0;
438 for ( i=0; i<P.count; i++ )
439 {
440 P.pcpus[i].pid = i;
441 P.pcpus[i].idle = 1;
442 P.idle++;
443 P.pcpus[i].current = NULL;
444 }
446 /* Initialize scheduler */
447 sim.sched_ops->sched_init();
449 /* Initialize vms */
450 w=opt.workload;
451 V.count = 0;
452 for ( vid=0; vid<w->vm_count; vid++)
453 {
454 struct vm *v = V.vms+vid;
456 v->vid = vid;
457 v->runstate = RUNSTATE_BLOCKED;
458 v->processor = -1;
459 v->private = NULL;
461 v->state_start_time = 0;
462 v->time_this_phase = 0;
465 v->phase_index = -1;
466 v->e = NULL;
467 v->workload = w->vm_workloads+vid;
469 V.count++;
471 sim.sched_ops->vm_init(vid);
472 }
474 /* Set VM starting conditions */
475 for ( vid=0; vid<V.count; vid++)
476 {
477 struct vm *v = V.vms+vid;
479 switch(v->workload->list[0].type)
480 {
481 case PHASE_RUN:
482 v->phase_index = v->workload->phase_count - 1;
483 v->e = v->workload->list + v->phase_index;
485 sim_insert_event(sim.now, EVT_WAKE, v->vid, 0);
486 v->state_start_time = sim.now;
487 v->time_this_phase = 0;
488 break;
489 case PHASE_BLOCK:
490 v->phase_index = 0;
491 v->e = v->workload->list;
493 sim_insert_event(sim.now + v->e->time, EVT_WAKE, v->vid, 0);
494 v->state_start_time = sim.now;
495 v->time_this_phase = 0;
496 break;
497 }
498 }
499 }
501 void report(void)
502 {
503 int i, j;
505 for ( i=0; i<V.count; i++ )
506 {
507 struct vm *v = V.vms + i;
509 printf("VM %d\n", i);
510 for ( j = 0; j < STATE_MAX ; j++ )
511 {
512 char s[128];
513 snprintf(s, 128, " %s", state_name[j]);
514 print_cycle_summary(&v->stats.state[j], sim.now, s);
515 }
516 }
517 }
519 int main(int argc, char * argv[])
520 {
521 warn = stdout;
523 parse_options(argc, argv);
525 /* Setup simulation */
526 init();
528 /* Run simulation */
529 simulate();
530 /* Report statistics */
531 report();
532 }