gdunlap/sched-sim.hg

annotate simulator.c @ 0:d27bb3c56e71

Inital commit.
author George Dunlap <gdunlap@xensource.com>
date Tue Oct 13 16:06:36 2009 +0100 (2009-10-13)
parents
children ec2d50e41437
rev   line source
gdunlap@0 1 #include <stdlib.h>
gdunlap@0 2 #include <stdio.h>
gdunlap@0 3 #include <assert.h>
gdunlap@0 4
gdunlap@0 5 #define ASSERT assert
gdunlap@0 6
gdunlap@0 7 #include "stats.h"
gdunlap@0 8 #include "list.h"
gdunlap@0 9 #include "sim.h"
gdunlap@0 10 #include "workload.h"
gdunlap@0 11
gdunlap@0 12 FILE *warn;
gdunlap@0 13
gdunlap@0 14 enum event_type {
gdunlap@0 15 EVT_BLOCK,
gdunlap@0 16 EVT_WAKE,
gdunlap@0 17 EVT_TIMER,
gdunlap@0 18 EVT_MAX
gdunlap@0 19 };
gdunlap@0 20
gdunlap@0 21 char *event_name[EVT_MAX] = {
gdunlap@0 22 [EVT_BLOCK]="block",
gdunlap@0 23 [EVT_WAKE] ="wake ",
gdunlap@0 24 [EVT_TIMER]="timer"
gdunlap@0 25 };
gdunlap@0 26
gdunlap@0 27 struct event {
gdunlap@0 28 struct list_head event_list;
gdunlap@0 29 enum event_type type;
gdunlap@0 30 int time;
gdunlap@0 31 int param; /* Usually VM ID */
gdunlap@0 32 };
gdunlap@0 33
gdunlap@0 34 char * state_name[STATE_MAX] = {
gdunlap@0 35 [STATE_RUN]= "run ",
gdunlap@0 36 [STATE_PREEMPT]="preempt",
gdunlap@0 37 [STATE_WAKE]= "wake ",
gdunlap@0 38 [STATE_BLOCK]= "block ",
gdunlap@0 39 };
gdunlap@0 40
gdunlap@0 41 struct {
gdunlap@0 42 int now;
gdunlap@0 43 struct list_head events;
gdunlap@0 44 struct list_head *timer;
gdunlap@0 45 const struct sched_ops *sched_ops;
gdunlap@0 46 } sim;
gdunlap@0 47
gdunlap@0 48
gdunlap@0 49 struct {
gdunlap@0 50 int count;
gdunlap@0 51 struct vm vms[MAX_VMS];
gdunlap@0 52 } V;
gdunlap@0 53
gdunlap@0 54 extern struct scheduler sched_rr;
gdunlap@0 55 int default_scheduler = 0;
gdunlap@0 56 struct scheduler *schedulers[] =
gdunlap@0 57 {
gdunlap@0 58 &sched_rr,
gdunlap@0 59 };
gdunlap@0 60
gdunlap@0 61 /* Options */
gdunlap@0 62 struct {
gdunlap@0 63 int time_limit;
gdunlap@0 64 int pcpu_count;
gdunlap@0 65 const struct workload * workload;
gdunlap@0 66 const struct scheduler * scheduler;
gdunlap@0 67 } opt = {
gdunlap@0 68 .time_limit = 100000,
gdunlap@0 69 .pcpu_count = 1,
gdunlap@0 70 .workload = NULL,
gdunlap@0 71 };
gdunlap@0 72
gdunlap@0 73 struct global_pcpu_data P;
gdunlap@0 74
gdunlap@0 75 /* Sim list interface */
gdunlap@0 76 /* NB: Caller must free if they're not going to use it! */
gdunlap@0 77 #define list_event(_l) (list_entry((_l), struct event, event_list))
gdunlap@0 78
gdunlap@0 79 struct event* sim_remove_event(int type, int param)
gdunlap@0 80 {
gdunlap@0 81 struct event* ret = NULL;
gdunlap@0 82 struct list_head *pos, *tmp;
gdunlap@0 83
gdunlap@0 84 /* Look for an event that matches this one and remove it */
gdunlap@0 85 list_for_each_safe(pos, tmp, &sim.events)
gdunlap@0 86 {
gdunlap@0 87 struct event *tevt = list_event(pos);
gdunlap@0 88 if ( tevt->type == type
gdunlap@0 89 && tevt->param == param )
gdunlap@0 90 {
gdunlap@0 91 list_del(pos);
gdunlap@0 92 ret = tevt;
gdunlap@0 93 break;
gdunlap@0 94 }
gdunlap@0 95 }
gdunlap@0 96
gdunlap@0 97 return ret;
gdunlap@0 98 }
gdunlap@0 99
gdunlap@0 100 void sim_insert_event(int time, int type, int param, int reset)
gdunlap@0 101 {
gdunlap@0 102 struct list_head *pos = NULL;
gdunlap@0 103 struct event *evt=NULL;
gdunlap@0 104
gdunlap@0 105 ASSERT(time >= sim.now);
gdunlap@0 106
gdunlap@0 107 if ( reset )
gdunlap@0 108 evt=sim_remove_event(type, param);
gdunlap@0 109
gdunlap@0 110 if ( !evt )
gdunlap@0 111 evt = (struct event *)malloc(sizeof(*evt));
gdunlap@0 112
gdunlap@0 113 printf(" [insert t%d %s param%d]\n",
gdunlap@0 114 evt->time, event_name[evt->type], evt->param);
gdunlap@0 115
gdunlap@0 116 evt->time = time;
gdunlap@0 117 evt->type = type;
gdunlap@0 118 evt->param = param;
gdunlap@0 119
gdunlap@0 120 INIT_LIST_HEAD(&evt->event_list);
gdunlap@0 121
gdunlap@0 122 list_for_each(pos, &sim.events)
gdunlap@0 123 {
gdunlap@0 124 if ( list_event(pos)->time > evt->time )
gdunlap@0 125 break;
gdunlap@0 126 }
gdunlap@0 127 list_add_tail(&evt->event_list, pos);
gdunlap@0 128 }
gdunlap@0 129
gdunlap@0 130 struct event sim_next_event(void)
gdunlap@0 131 {
gdunlap@0 132 struct event *evt;
gdunlap@0 133 struct list_head *next;
gdunlap@0 134
gdunlap@0 135 ASSERT(!list_empty(&sim.events));
gdunlap@0 136
gdunlap@0 137 next=sim.events.next;
gdunlap@0 138
gdunlap@0 139 list_del(next);
gdunlap@0 140
gdunlap@0 141 evt=list_event(next);
gdunlap@0 142
gdunlap@0 143 printf("%d: evt %s param%d\n",
gdunlap@0 144 evt->time, event_name[evt->type], evt->param);
gdunlap@0 145
gdunlap@0 146 free(evt);
gdunlap@0 147
gdunlap@0 148 /* XXX */
gdunlap@0 149 return *evt;
gdunlap@0 150 }
gdunlap@0 151
gdunlap@0 152 /*
gdunlap@0 153 * VM simulation
gdunlap@0 154 */
gdunlap@0 155 void vm_next_event(struct vm *v)
gdunlap@0 156 {
gdunlap@0 157 v->phase_index = ( v->phase_index + 1 ) % v->workload->phase_count;
gdunlap@0 158
gdunlap@0 159 v->e = v->workload->list + v->phase_index;
gdunlap@0 160 }
gdunlap@0 161
gdunlap@0 162 struct vm* vm_from_vid(int vid)
gdunlap@0 163 {
gdunlap@0 164 ASSERT(vid < V.count);
gdunlap@0 165
gdunlap@0 166 return V.vms + vid;
gdunlap@0 167 }
gdunlap@0 168
gdunlap@0 169 void vm_block(int now, struct vm *v)
gdunlap@0 170 {
gdunlap@0 171 ASSERT(v->e->type == PHASE_RUN);
gdunlap@0 172 v->time_this_phase += now - v->state_start_time;
gdunlap@0 173 printf("%s: v%d time_this_phase %d\n",
gdunlap@0 174 __func__, v->vid, v->time_this_phase);
gdunlap@0 175
gdunlap@0 176 ASSERT(v->time_this_phase == v->e->time);
gdunlap@0 177
gdunlap@0 178 vm_next_event(v);
gdunlap@0 179
gdunlap@0 180 ASSERT(v->e->type == PHASE_BLOCK);
gdunlap@0 181
gdunlap@0 182 sim_insert_event(now + v->e->time, EVT_WAKE, v->vid, 0);
gdunlap@0 183 v->time_this_phase = 0;
gdunlap@0 184 v->was_preempted = 0;
gdunlap@0 185 }
gdunlap@0 186
gdunlap@0 187 /* Called when wake event happens; increment timer and reset state */
gdunlap@0 188 void vm_wake(int now, struct vm *v)
gdunlap@0 189 {
gdunlap@0 190 ASSERT(v->e->type == PHASE_BLOCK);
gdunlap@0 191 ASSERT(v->time_this_phase == 0);
gdunlap@0 192
gdunlap@0 193 v->time_this_phase = now - v->state_start_time;
gdunlap@0 194
gdunlap@0 195 if ( now != 0 )
gdunlap@0 196 ASSERT(v->time_this_phase == v->e->time);
gdunlap@0 197
gdunlap@0 198 vm_next_event(v);
gdunlap@0 199
gdunlap@0 200 v->time_this_phase = 0;
gdunlap@0 201 }
gdunlap@0 202
gdunlap@0 203 /* Called when actually starting to run; make block event and set state */
gdunlap@0 204 void vm_run(int now, struct vm *v)
gdunlap@0 205 {
gdunlap@0 206 ASSERT(v->e->type == PHASE_RUN);
gdunlap@0 207 ASSERT(v->time_this_phase < v->e->time);
gdunlap@0 208
gdunlap@0 209 sim_insert_event(now + v->e->time - v->time_this_phase, EVT_BLOCK, v->vid, 0);
gdunlap@0 210 v->state_start_time = now;
gdunlap@0 211 }
gdunlap@0 212
gdunlap@0 213 /* Preempt: Remove block event, update amount of runtime (so that when it runs again we can accurately
gdunlap@0 214 * generate a new block event) */
gdunlap@0 215 void vm_preempt(int now, struct vm *v)
gdunlap@0 216 {
gdunlap@0 217 struct event* evt;
gdunlap@0 218
gdunlap@0 219 if ( ( evt = sim_remove_event(EVT_BLOCK, v->vid) ) )
gdunlap@0 220 free(evt);
gdunlap@0 221
gdunlap@0 222 v->time_this_phase += now - v->state_start_time;
gdunlap@0 223 printf("%s: v%d time_this_phase %d\n",
gdunlap@0 224 __func__, v->vid, v->time_this_phase);
gdunlap@0 225
gdunlap@0 226 ASSERT(v->time_this_phase < v->e->time);
gdunlap@0 227
gdunlap@0 228 v->was_preempted = 1;
gdunlap@0 229 }
gdunlap@0 230
gdunlap@0 231
gdunlap@0 232 /* Callbacks the scheduler may make */
gdunlap@0 233 void sim_sched_timer(int time, int pid)
gdunlap@0 234 {
gdunlap@0 235 sim_insert_event(sim.now + time, EVT_TIMER, pid, 1);
gdunlap@0 236 }
gdunlap@0 237
gdunlap@0 238 void sim_runstate_change(int now, struct vm *v, int new_runstate)
gdunlap@0 239 {
gdunlap@0 240 int ostate, nstate;
gdunlap@0 241 int stime = now - v->state_start_time;
gdunlap@0 242
gdunlap@0 243 /* Valid transitions:
gdunlap@0 244 * + R->A (preemption): remove block event
gdunlap@0 245 * + R->B (block) : Insert wake event
gdunlap@0 246 * + A->R (run) : Insert block event
gdunlap@0 247 * + B->A (wake) : No action necessary
gdunlap@0 248 */
gdunlap@0 249
gdunlap@0 250 switch ( v->runstate )
gdunlap@0 251 {
gdunlap@0 252 case RUNSTATE_RUNNING:
gdunlap@0 253 ostate = STATE_RUN;
gdunlap@0 254 break;
gdunlap@0 255 case RUNSTATE_RUNNABLE:
gdunlap@0 256 if ( v->was_preempted )
gdunlap@0 257 ostate = STATE_PREEMPT;
gdunlap@0 258 else
gdunlap@0 259 ostate = STATE_WAKE;
gdunlap@0 260 break;
gdunlap@0 261 case RUNSTATE_BLOCKED:
gdunlap@0 262 ostate = STATE_BLOCK;
gdunlap@0 263 break;
gdunlap@0 264 }
gdunlap@0 265
gdunlap@0 266 update_cycles(&v->stats.state[ostate], stime);
gdunlap@0 267
gdunlap@0 268
gdunlap@0 269 if ( v->runstate == RUNSTATE_RUNNING
gdunlap@0 270 && new_runstate == RUNSTATE_RUNNABLE )
gdunlap@0 271 {
gdunlap@0 272 nstate = STATE_PREEMPT;
gdunlap@0 273 vm_preempt(now, v);
gdunlap@0 274 }
gdunlap@0 275 else if ( v->runstate == RUNSTATE_RUNNING
gdunlap@0 276 && new_runstate == RUNSTATE_BLOCKED )
gdunlap@0 277 {
gdunlap@0 278 nstate = STATE_BLOCK;
gdunlap@0 279 vm_block(now, v);
gdunlap@0 280 }
gdunlap@0 281 else if ( v->runstate == RUNSTATE_RUNNABLE
gdunlap@0 282 && new_runstate == RUNSTATE_RUNNING )
gdunlap@0 283 {
gdunlap@0 284 nstate = STATE_RUN;
gdunlap@0 285 vm_run(now, v);
gdunlap@0 286 }
gdunlap@0 287 else if ( v->runstate == RUNSTATE_BLOCKED
gdunlap@0 288 && new_runstate == RUNSTATE_RUNNABLE )
gdunlap@0 289 {
gdunlap@0 290 nstate = STATE_WAKE;
gdunlap@0 291 vm_wake(now, v);
gdunlap@0 292 }
gdunlap@0 293 else
gdunlap@0 294 goto unexpected_transition;
gdunlap@0 295
gdunlap@0 296 printf("%d: v%d %s %d -> %s\n",
gdunlap@0 297 now, v->vid, state_name[ostate], stime, state_name[nstate]);
gdunlap@0 298
gdunlap@0 299 v->runstate = new_runstate;
gdunlap@0 300 v->state_start_time = now;
gdunlap@0 301
gdunlap@0 302 return;
gdunlap@0 303
gdunlap@0 304 unexpected_transition:
gdunlap@0 305 fprintf(stderr, "Unexpected transition for vm %d: %d->%d\n",
gdunlap@0 306 v->vid,
gdunlap@0 307 v->runstate,
gdunlap@0 308 new_runstate);
gdunlap@0 309 exit(1);
gdunlap@0 310 }
gdunlap@0 311
gdunlap@0 312 /*
gdunlap@0 313 * Main loop
gdunlap@0 314 */
gdunlap@0 315 void simulate(void)
gdunlap@0 316 {
gdunlap@0 317 while ( sim.now < opt.time_limit )
gdunlap@0 318 {
gdunlap@0 319 /* Take next event off list */
gdunlap@0 320 struct event evt;
gdunlap@0 321
gdunlap@0 322 evt = sim_next_event();
gdunlap@0 323
gdunlap@0 324 sim.now = evt.time;
gdunlap@0 325
gdunlap@0 326 switch(evt.type)
gdunlap@0 327 {
gdunlap@0 328 case EVT_WAKE:
gdunlap@0 329 {
gdunlap@0 330 struct vm *v = vm_from_vid(evt.param);
gdunlap@0 331 ASSERT(v->processor == -1);
gdunlap@0 332 sim_runstate_change(sim.now, v, RUNSTATE_RUNNABLE);
gdunlap@0 333 sim.sched_ops->wake(sim.now, v);
gdunlap@0 334 }
gdunlap@0 335 break;
gdunlap@0 336 case EVT_BLOCK:
gdunlap@0 337 {
gdunlap@0 338 struct vm *v = vm_from_vid(evt.param);
gdunlap@0 339
gdunlap@0 340 ASSERT(v->processor != -1);
gdunlap@0 341 ASSERT(v->processor <= P.count);
gdunlap@0 342
gdunlap@0 343 sim_runstate_change(sim.now, v, RUNSTATE_BLOCKED);
gdunlap@0 344
gdunlap@0 345 evt.param = v->processor; /* FIXME */
gdunlap@0 346 }
gdunlap@0 347 /* FALL-THRU */
gdunlap@0 348 case EVT_TIMER:
gdunlap@0 349 {
gdunlap@0 350 struct vm *prev, *next;
gdunlap@0 351 int pid = evt.param;
gdunlap@0 352
gdunlap@0 353 ASSERT(pid < P.count);
gdunlap@0 354
gdunlap@0 355 prev = P.pcpus[pid].current;
gdunlap@0 356
gdunlap@0 357 next = sim.sched_ops->schedule(sim.now, pid);
gdunlap@0 358
gdunlap@0 359 if ( prev && prev != next )
gdunlap@0 360 {
gdunlap@0 361 prev->processor = -1;
gdunlap@0 362 if( prev->runstate != RUNSTATE_BLOCKED )
gdunlap@0 363 sim_runstate_change(sim.now, prev, RUNSTATE_RUNNABLE);
gdunlap@0 364 }
gdunlap@0 365
gdunlap@0 366 sim_runstate_change(sim.now, next, RUNSTATE_RUNNING);
gdunlap@0 367 P.pcpus[pid].current = next;
gdunlap@0 368 next->processor = pid;
gdunlap@0 369 }
gdunlap@0 370 break;
gdunlap@0 371 default:
gdunlap@0 372 fprintf(stderr, "Unexpected event type: %d\n", evt.type);
gdunlap@0 373 exit(1);
gdunlap@0 374 break;
gdunlap@0 375 }
gdunlap@0 376 }
gdunlap@0 377 }
gdunlap@0 378
gdunlap@0 379 void init(void)
gdunlap@0 380 {
gdunlap@0 381 int vid, i;
gdunlap@0 382 const struct workload *w;
gdunlap@0 383
gdunlap@0 384 /* Initialize simulation variables */
gdunlap@0 385 sim.now=0;
gdunlap@0 386 sim.timer=NULL;
gdunlap@0 387 INIT_LIST_HEAD(&sim.events);
gdunlap@0 388 sim.sched_ops = &opt.scheduler->ops;
gdunlap@0 389
gdunlap@0 390 /* Initialize pcpus */
gdunlap@0 391 P.count = opt.pcpu_count;
gdunlap@0 392 for ( i=0; i<P.count; i++ )
gdunlap@0 393 {
gdunlap@0 394 P.pcpus[i].pid = i;
gdunlap@0 395 P.pcpus[i].current = NULL;
gdunlap@0 396 }
gdunlap@0 397
gdunlap@0 398 /* Initialize scheduler */
gdunlap@0 399 sim.sched_ops->sched_init();
gdunlap@0 400
gdunlap@0 401 /* Initialize vms */
gdunlap@0 402 w=opt.workload;
gdunlap@0 403 for ( vid=0; vid<w->vm_count; vid++)
gdunlap@0 404 {
gdunlap@0 405 struct vm *v = V.vms+vid;
gdunlap@0 406
gdunlap@0 407 v->vid = vid;
gdunlap@0 408 v->runstate = RUNSTATE_BLOCKED;
gdunlap@0 409 v->processor = -1;
gdunlap@0 410 v->private = NULL;
gdunlap@0 411
gdunlap@0 412 v->state_start_time = 0;
gdunlap@0 413 v->time_this_phase = 0;
gdunlap@0 414
gdunlap@0 415
gdunlap@0 416 v->phase_index = -1;
gdunlap@0 417 v->e = NULL;
gdunlap@0 418 v->workload = w->vm_workloads+vid;
gdunlap@0 419
gdunlap@0 420 V.count++;
gdunlap@0 421
gdunlap@0 422 sim.sched_ops->vm_init(vid);
gdunlap@0 423 }
gdunlap@0 424
gdunlap@0 425 /* Set VM starting conditions */
gdunlap@0 426 for ( vid=0; vid<V.count; vid++)
gdunlap@0 427 {
gdunlap@0 428 struct vm *v = V.vms+vid;
gdunlap@0 429
gdunlap@0 430 switch(v->workload->list[0].type)
gdunlap@0 431 {
gdunlap@0 432 case PHASE_RUN:
gdunlap@0 433 v->phase_index = v->workload->phase_count - 1;
gdunlap@0 434 v->e = v->workload->list + v->phase_index;
gdunlap@0 435
gdunlap@0 436 sim_insert_event(sim.now, EVT_WAKE, v->vid, 0);
gdunlap@0 437 v->state_start_time = sim.now;
gdunlap@0 438 v->time_this_phase = 0;
gdunlap@0 439 break;
gdunlap@0 440 case PHASE_BLOCK:
gdunlap@0 441 v->phase_index = 0;
gdunlap@0 442 v->e = v->workload->list;
gdunlap@0 443
gdunlap@0 444 sim_insert_event(sim.now + v->e->time, EVT_WAKE, v->vid, 0);
gdunlap@0 445 v->state_start_time = sim.now;
gdunlap@0 446 v->time_this_phase = 0;
gdunlap@0 447 break;
gdunlap@0 448 }
gdunlap@0 449 }
gdunlap@0 450
gdunlap@0 451 /* Insert initial scheduler timer */
gdunlap@0 452 for ( i=0; i<P.count; i++)
gdunlap@0 453 sim_insert_event(sim.now, EVT_TIMER, i, 1);
gdunlap@0 454 }
gdunlap@0 455
gdunlap@0 456 void report(void)
gdunlap@0 457 {
gdunlap@0 458 int i, j;
gdunlap@0 459
gdunlap@0 460 for ( i=0; i<V.count; i++ )
gdunlap@0 461 {
gdunlap@0 462 struct vm *v = V.vms + i;
gdunlap@0 463
gdunlap@0 464 printf("VM %d\n", i);
gdunlap@0 465 for ( j = 0; j < STATE_MAX ; j++ )
gdunlap@0 466 {
gdunlap@0 467 char s[128];
gdunlap@0 468 snprintf(s, 128, " %s", state_name[j]);
gdunlap@0 469 print_cycle_summary(&v->stats.state[j], sim.now, s);
gdunlap@0 470 }
gdunlap@0 471 }
gdunlap@0 472 }
gdunlap@0 473
gdunlap@0 474 int main(int argc, char * argv[])
gdunlap@0 475 {
gdunlap@0 476 warn = stdout;
gdunlap@0 477
gdunlap@0 478 /* Read opts, config file? */
gdunlap@0 479 if ( !opt.workload )
gdunlap@0 480 opt.workload = builtin_workloads+default_workload;
gdunlap@0 481
gdunlap@0 482 if ( !opt.scheduler )
gdunlap@0 483 opt.scheduler = schedulers[default_scheduler];
gdunlap@0 484 /* Setup simulation */
gdunlap@0 485 init();
gdunlap@0 486
gdunlap@0 487 /* Run simulation */
gdunlap@0 488 simulate();
gdunlap@0 489 /* Report statistics */
gdunlap@0 490 report();
gdunlap@0 491 }