gdunlap/sched-sim.hg

view simulator.c @ 2:1d7310217c5a

Add command-line options
author George Dunlap <gdunlap@xensource.com>
date Wed Oct 14 16:12:26 2009 +0100 (2009-10-14)
parents ec2d50e41437
children d957708efa86
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 int default_scheduler = 0;
61 struct scheduler *schedulers[] =
62 {
63 &sched_rr,
64 NULL
65 };
67 /* Options */
69 struct global_pcpu_data P;
71 /* Sim list interface */
72 /* NB: Caller must free if they're not going to use it! */
73 #define list_event(_l) (list_entry((_l), struct event, event_list))
75 struct event* sim_remove_event(int type, int param)
76 {
77 struct event* ret = NULL;
78 struct list_head *pos, *tmp;
80 /* Look for an event that matches this one and remove it */
81 list_for_each_safe(pos, tmp, &sim.events)
82 {
83 struct event *tevt = list_event(pos);
84 if ( tevt->type == type
85 && tevt->param == param )
86 {
87 list_del(pos);
88 ret = tevt;
89 break;
90 }
91 }
93 return ret;
94 }
96 void sim_insert_event(int time, int type, int param, int reset)
97 {
98 struct list_head *pos = NULL;
99 struct event *evt=NULL;
101 ASSERT(time >= sim.now);
103 if ( reset )
104 evt=sim_remove_event(type, param);
106 if ( !evt )
107 evt = (struct event *)malloc(sizeof(*evt));
109 evt->time = time;
110 evt->type = type;
111 evt->param = param;
113 printf(" [insert t%d %s param%d]\n",
114 evt->time, event_name[evt->type], evt->param);
116 INIT_LIST_HEAD(&evt->event_list);
118 list_for_each(pos, &sim.events)
119 {
120 if ( list_event(pos)->time > evt->time )
121 break;
122 }
123 list_add_tail(&evt->event_list, pos);
124 }
126 struct event sim_next_event(void)
127 {
128 struct event *evt;
129 struct list_head *next;
131 ASSERT(!list_empty(&sim.events));
133 next=sim.events.next;
135 list_del(next);
137 evt=list_event(next);
139 printf("%d: evt %s param%d\n",
140 evt->time, event_name[evt->type], evt->param);
142 free(evt);
144 /* XXX */
145 return *evt;
146 }
148 /*
149 * VM simulation
150 */
151 void vm_next_event(struct vm *v)
152 {
153 v->phase_index = ( v->phase_index + 1 ) % v->workload->phase_count;
155 v->e = v->workload->list + v->phase_index;
156 }
158 struct vm* vm_from_vid(int vid)
159 {
160 if ( vid >= V.count )
161 {
162 fprintf(stderr, "%s: v%d >= V.count %d!\n",
163 __func__, vid, V.count);
164 exit(1);
165 }
167 return V.vms + vid;
168 }
170 void vm_block(int now, struct vm *v)
171 {
172 ASSERT(v->e->type == PHASE_RUN);
173 v->time_this_phase += now - v->state_start_time;
174 printf("%s: v%d time_this_phase %d\n",
175 __func__, v->vid, v->time_this_phase);
177 ASSERT(v->time_this_phase == v->e->time);
179 vm_next_event(v);
181 ASSERT(v->e->type == PHASE_BLOCK);
183 sim_insert_event(now + v->e->time, EVT_WAKE, v->vid, 0);
184 v->time_this_phase = 0;
185 v->was_preempted = 0;
186 }
188 /* Called when wake event happens; increment timer and reset state */
189 void vm_wake(int now, struct vm *v)
190 {
191 ASSERT(v->e->type == PHASE_BLOCK);
192 ASSERT(v->time_this_phase == 0);
194 v->time_this_phase = now - v->state_start_time;
196 if ( now != 0 )
197 ASSERT(v->time_this_phase == v->e->time);
199 vm_next_event(v);
201 v->time_this_phase = 0;
202 }
204 /* Called when actually starting to run; make block event and set state */
205 void vm_run(int now, struct vm *v)
206 {
207 ASSERT(v->e->type == PHASE_RUN);
208 ASSERT(v->time_this_phase < v->e->time);
210 sim_insert_event(now + v->e->time - v->time_this_phase, EVT_BLOCK, v->vid, 0);
211 v->state_start_time = now;
212 }
214 /* Preempt: Remove block event, update amount of runtime (so that when it runs again we can accurately
215 * generate a new block event) */
216 void vm_preempt(int now, struct vm *v)
217 {
218 struct event* evt;
220 if ( ( evt = sim_remove_event(EVT_BLOCK, v->vid) ) )
221 free(evt);
223 v->time_this_phase += now - v->state_start_time;
224 printf("%s: v%d time_this_phase %d\n",
225 __func__, v->vid, v->time_this_phase);
227 ASSERT(v->time_this_phase < v->e->time);
229 v->was_preempted = 1;
230 }
233 /* Callbacks the scheduler may make */
234 void sim_sched_timer(int time, int pid)
235 {
236 if ( pid >= P.count )
237 {
238 fprintf(stderr, "%s: p%d >= P.count %d\n",
239 __func__, pid, P.count);
240 exit(1);
241 }
243 if ( P.pcpus[pid].idle )
244 {
245 P.pcpus[pid].idle = 0;
246 P.idle--;
247 }
248 sim_insert_event(sim.now + time, EVT_TIMER, pid, 1);
249 }
251 void sim_runstate_change(int now, struct vm *v, int new_runstate)
252 {
253 int ostate, nstate;
254 int stime = now - v->state_start_time;
256 /* Valid transitions:
257 * + R->A (preemption): remove block event
258 * + R->B (block) : Insert wake event
259 * + A->R (run) : Insert block event
260 * + B->A (wake) : No action necessary
261 */
263 switch ( v->runstate )
264 {
265 case RUNSTATE_RUNNING:
266 ostate = STATE_RUN;
267 break;
268 case RUNSTATE_RUNNABLE:
269 if ( v->was_preempted )
270 ostate = STATE_PREEMPT;
271 else
272 ostate = STATE_WAKE;
273 break;
274 case RUNSTATE_BLOCKED:
275 ostate = STATE_BLOCK;
276 break;
277 }
279 update_cycles(&v->stats.state[ostate], stime);
282 if ( v->runstate == RUNSTATE_RUNNING
283 && new_runstate == RUNSTATE_RUNNABLE )
284 {
285 nstate = STATE_PREEMPT;
286 vm_preempt(now, v);
287 }
288 else if ( v->runstate == RUNSTATE_RUNNING
289 && new_runstate == RUNSTATE_BLOCKED )
290 {
291 nstate = STATE_BLOCK;
292 vm_block(now, v);
293 }
294 else if ( v->runstate == RUNSTATE_RUNNABLE
295 && new_runstate == RUNSTATE_RUNNING )
296 {
297 nstate = STATE_RUN;
298 vm_run(now, v);
299 }
300 else if ( v->runstate == RUNSTATE_BLOCKED
301 && new_runstate == RUNSTATE_RUNNABLE )
302 {
303 nstate = STATE_WAKE;
304 vm_wake(now, v);
305 }
306 else
307 goto unexpected_transition;
309 printf("%d: v%d %s %d -> %s\n",
310 now, v->vid, state_name[ostate], stime, state_name[nstate]);
312 v->runstate = new_runstate;
313 v->state_start_time = now;
315 return;
317 unexpected_transition:
318 fprintf(stderr, "Unexpected transition for vm %d: %d->%d\n",
319 v->vid,
320 v->runstate,
321 new_runstate);
322 exit(1);
323 }
325 /*
326 * Main loop
327 */
328 void simulate(void)
329 {
330 while ( sim.now < opt.time_limit )
331 {
332 /* Take next event off list */
333 struct event evt;
335 evt = sim_next_event();
337 sim.now = evt.time;
339 switch(evt.type)
340 {
341 case EVT_WAKE:
342 {
343 struct vm *v = vm_from_vid(evt.param);
344 ASSERT(v->processor == -1);
345 sim_runstate_change(sim.now, v, RUNSTATE_RUNNABLE);
346 sim.sched_ops->wake(sim.now, v->vid);
347 }
348 break;
349 case EVT_BLOCK:
350 {
351 struct vm *v = vm_from_vid(evt.param);
353 ASSERT(v->processor != -1);
354 ASSERT(v->processor <= P.count);
356 sim_runstate_change(sim.now, v, RUNSTATE_BLOCKED);
358 evt.param = v->processor; /* FIXME */
359 }
360 /* FALL-THRU */
361 case EVT_TIMER:
362 {
363 struct vm *prev, *next;
364 int pid = evt.param;
366 ASSERT(pid < P.count);
368 prev = P.pcpus[pid].current;
370 next = sim.sched_ops->schedule(sim.now, pid);
372 if ( prev && prev != next )
373 {
374 prev->processor = -1;
375 if( prev->runstate != RUNSTATE_BLOCKED )
376 sim_runstate_change(sim.now, prev, RUNSTATE_RUNNABLE);
377 }
380 P.pcpus[pid].current = next;
381 if ( next )
382 {
383 if ( next != prev )
384 {
385 sim_runstate_change(sim.now, next, RUNSTATE_RUNNING);
386 next->processor = pid;
387 }
388 }
389 else
390 {
391 P.pcpus[pid].idle = 1;
392 P.idle++;
393 }
394 }
395 break;
396 default:
397 fprintf(stderr, "Unexpected event type: %d\n", evt.type);
398 exit(1);
399 break;
400 }
401 }
402 }
404 void init(void)
405 {
406 int vid, i;
407 const struct workload *w;
409 /* Initialize simulation variables */
410 sim.now=0;
411 sim.timer=NULL;
412 INIT_LIST_HEAD(&sim.events);
413 sim.sched_ops = &opt.scheduler->ops;
415 /* Initialize pcpus */
416 P.count = opt.pcpu_count;
417 P.idle = 0;
418 for ( i=0; i<P.count; i++ )
419 {
420 P.pcpus[i].pid = i;
421 P.pcpus[i].idle = 1;
422 P.idle++;
423 P.pcpus[i].current = NULL;
424 }
426 /* Initialize scheduler */
427 sim.sched_ops->sched_init();
429 /* Initialize vms */
430 w=opt.workload;
431 V.count = 0;
432 for ( vid=0; vid<w->vm_count; vid++)
433 {
434 struct vm *v = V.vms+vid;
436 v->vid = vid;
437 v->runstate = RUNSTATE_BLOCKED;
438 v->processor = -1;
439 v->private = NULL;
441 v->state_start_time = 0;
442 v->time_this_phase = 0;
445 v->phase_index = -1;
446 v->e = NULL;
447 v->workload = w->vm_workloads+vid;
449 V.count++;
451 sim.sched_ops->vm_init(vid);
452 }
454 /* Set VM starting conditions */
455 for ( vid=0; vid<V.count; vid++)
456 {
457 struct vm *v = V.vms+vid;
459 switch(v->workload->list[0].type)
460 {
461 case PHASE_RUN:
462 v->phase_index = v->workload->phase_count - 1;
463 v->e = v->workload->list + v->phase_index;
465 sim_insert_event(sim.now, EVT_WAKE, v->vid, 0);
466 v->state_start_time = sim.now;
467 v->time_this_phase = 0;
468 break;
469 case PHASE_BLOCK:
470 v->phase_index = 0;
471 v->e = v->workload->list;
473 sim_insert_event(sim.now + v->e->time, EVT_WAKE, v->vid, 0);
474 v->state_start_time = sim.now;
475 v->time_this_phase = 0;
476 break;
477 }
478 }
479 }
481 void report(void)
482 {
483 int i, j;
485 for ( i=0; i<V.count; i++ )
486 {
487 struct vm *v = V.vms + i;
489 printf("VM %d\n", i);
490 for ( j = 0; j < STATE_MAX ; j++ )
491 {
492 char s[128];
493 snprintf(s, 128, " %s", state_name[j]);
494 print_cycle_summary(&v->stats.state[j], sim.now, s);
495 }
496 }
497 }
499 int main(int argc, char * argv[])
500 {
501 warn = stdout;
503 parse_options(argc, argv);
505 /* Setup simulation */
506 init();
508 /* Run simulation */
509 simulate();
510 /* Report statistics */
511 report();
512 }