gdunlap/sched-sim.hg

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/simulator.c	Tue Oct 13 16:06:36 2009 +0100
     1.3 @@ -0,0 +1,491 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <stdio.h>
     1.6 +#include <assert.h>
     1.7 +
     1.8 +#define ASSERT assert
     1.9 +
    1.10 +#include "stats.h"
    1.11 +#include "list.h"
    1.12 +#include "sim.h"
    1.13 +#include "workload.h"
    1.14 +
    1.15 +FILE *warn;
    1.16 +
    1.17 +enum event_type {
    1.18 +    EVT_BLOCK,
    1.19 +    EVT_WAKE,
    1.20 +    EVT_TIMER,
    1.21 +    EVT_MAX
    1.22 +};
    1.23 +
    1.24 +char *event_name[EVT_MAX] = {
    1.25 +    [EVT_BLOCK]="block",
    1.26 +    [EVT_WAKE] ="wake ",
    1.27 +    [EVT_TIMER]="timer"
    1.28 +};
    1.29 +
    1.30 +struct event {
    1.31 +    struct list_head event_list;
    1.32 +    enum event_type type;
    1.33 +    int time;
    1.34 +    int param;  /* Usually VM ID */ 
    1.35 +};
    1.36 +
    1.37 +char * state_name[STATE_MAX] = {
    1.38 +    [STATE_RUN]=    "run    ",
    1.39 +    [STATE_PREEMPT]="preempt",
    1.40 +    [STATE_WAKE]=   "wake   ",
    1.41 +    [STATE_BLOCK]=  "block  ",
    1.42 +};
    1.43 +
    1.44 +struct {
    1.45 +    int now;
    1.46 +    struct list_head events;
    1.47 +    struct list_head *timer;
    1.48 +    const struct sched_ops *sched_ops;
    1.49 +} sim;
    1.50 +
    1.51 +
    1.52 +struct {
    1.53 +    int count;
    1.54 +    struct vm vms[MAX_VMS];
    1.55 +} V;
    1.56 +
    1.57 +extern struct scheduler sched_rr;
    1.58 +int default_scheduler = 0;
    1.59 +struct scheduler *schedulers[] =
    1.60 +{
    1.61 +    &sched_rr,
    1.62 +};
    1.63 +
    1.64 +/* Options */
    1.65 +struct {
    1.66 +    int time_limit;
    1.67 +    int pcpu_count;
    1.68 +    const struct workload * workload;
    1.69 +    const struct scheduler * scheduler;
    1.70 +} opt = {
    1.71 +    .time_limit = 100000,
    1.72 +    .pcpu_count = 1,
    1.73 +    .workload = NULL,
    1.74 +};
    1.75 +
    1.76 +struct global_pcpu_data P;
    1.77 +
    1.78 +/* Sim list interface */
    1.79 +/* NB: Caller must free if they're not going to use it! */
    1.80 +#define list_event(_l) (list_entry((_l), struct event, event_list))
    1.81 +
    1.82 +struct event* sim_remove_event(int type, int param)
    1.83 +{
    1.84 +    struct event* ret = NULL;
    1.85 +    struct list_head *pos, *tmp;
    1.86 +
    1.87 +    /* Look for an event that matches this one and remove it */
    1.88 +    list_for_each_safe(pos, tmp, &sim.events)
    1.89 +    {
    1.90 +        struct event *tevt = list_event(pos);
    1.91 +        if ( tevt->type == type
    1.92 +             && tevt->param == param )
    1.93 +        {
    1.94 +            list_del(pos);
    1.95 +            ret = tevt;
    1.96 +            break;
    1.97 +        }
    1.98 +    }
    1.99 +
   1.100 +    return ret;
   1.101 +}
   1.102 +
   1.103 +void sim_insert_event(int time, int type, int param, int reset)
   1.104 +{
   1.105 +    struct list_head *pos = NULL;
   1.106 +    struct event *evt=NULL;
   1.107 +
   1.108 +    ASSERT(time >= sim.now);
   1.109 +
   1.110 +    if ( reset )
   1.111 +        evt=sim_remove_event(type, param);
   1.112 +
   1.113 +    if ( !evt )
   1.114 +        evt = (struct event *)malloc(sizeof(*evt));
   1.115 +
   1.116 +    printf(" [insert t%d %s param%d]\n",
   1.117 +           evt->time, event_name[evt->type], evt->param);
   1.118 +
   1.119 +    evt->time = time;
   1.120 +    evt->type = type;
   1.121 +    evt->param = param;
   1.122 +
   1.123 +    INIT_LIST_HEAD(&evt->event_list);
   1.124 +
   1.125 +    list_for_each(pos, &sim.events)
   1.126 +    {
   1.127 +        if ( list_event(pos)->time > evt->time )
   1.128 +            break;
   1.129 +    }
   1.130 +    list_add_tail(&evt->event_list, pos);
   1.131 +}
   1.132 +
   1.133 +struct event sim_next_event(void)
   1.134 +{
   1.135 +    struct event *evt;
   1.136 +    struct list_head *next;
   1.137 +
   1.138 +    ASSERT(!list_empty(&sim.events));
   1.139 +
   1.140 +    next=sim.events.next;
   1.141 +
   1.142 +    list_del(next);
   1.143 +    
   1.144 +    evt=list_event(next);
   1.145 +
   1.146 +    printf("%d: evt %s param%d\n",
   1.147 +           evt->time, event_name[evt->type], evt->param);
   1.148 +
   1.149 +    free(evt);
   1.150 +
   1.151 +    /* XXX */
   1.152 +    return *evt;
   1.153 +}
   1.154 +
   1.155 +/*
   1.156 + * VM simulation
   1.157 + */
   1.158 +void vm_next_event(struct vm *v)
   1.159 +{
   1.160 +    v->phase_index = ( v->phase_index + 1 ) % v->workload->phase_count;
   1.161 +
   1.162 +    v->e = v->workload->list + v->phase_index;
   1.163 +}
   1.164 +
   1.165 +struct vm* vm_from_vid(int vid)
   1.166 +{
   1.167 +    ASSERT(vid < V.count);
   1.168 +
   1.169 +    return V.vms + vid;
   1.170 +}
   1.171 +
   1.172 +void vm_block(int now, struct vm *v)
   1.173 +{
   1.174 +    ASSERT(v->e->type == PHASE_RUN);
   1.175 +    v->time_this_phase += now - v->state_start_time;
   1.176 +    printf("%s: v%d time_this_phase %d\n",
   1.177 +           __func__, v->vid, v->time_this_phase);
   1.178 +
   1.179 +    ASSERT(v->time_this_phase == v->e->time);
   1.180 +
   1.181 +    vm_next_event(v);
   1.182 +    
   1.183 +    ASSERT(v->e->type == PHASE_BLOCK);
   1.184 +
   1.185 +    sim_insert_event(now + v->e->time, EVT_WAKE, v->vid, 0);
   1.186 +    v->time_this_phase = 0;
   1.187 +    v->was_preempted = 0;
   1.188 +}
   1.189 +
   1.190 +/* Called when wake event happens; increment timer and reset state */
   1.191 +void vm_wake(int now, struct vm *v)
   1.192 +{
   1.193 +    ASSERT(v->e->type == PHASE_BLOCK);
   1.194 +    ASSERT(v->time_this_phase == 0);
   1.195 +
   1.196 +    v->time_this_phase = now - v->state_start_time;
   1.197 +
   1.198 +    if ( now != 0 )
   1.199 +        ASSERT(v->time_this_phase == v->e->time);
   1.200 +
   1.201 +    vm_next_event(v);
   1.202 +
   1.203 +    v->time_this_phase = 0;
   1.204 +}
   1.205 +
   1.206 +/* Called when actually starting to run; make block event and set state */
   1.207 +void vm_run(int now, struct vm *v)
   1.208 +{
   1.209 +    ASSERT(v->e->type == PHASE_RUN);
   1.210 +    ASSERT(v->time_this_phase < v->e->time);
   1.211 +
   1.212 +    sim_insert_event(now + v->e->time - v->time_this_phase, EVT_BLOCK, v->vid, 0);
   1.213 +    v->state_start_time = now;
   1.214 +}
   1.215 +
   1.216 +/* Preempt: Remove block event, update amount of runtime (so that when it runs again we can accurately
   1.217 + * generate a new block event) */
   1.218 +void vm_preempt(int now, struct vm *v)
   1.219 +{
   1.220 +    struct event* evt;
   1.221 +
   1.222 +    if ( ( evt = sim_remove_event(EVT_BLOCK, v->vid) ) )
   1.223 +        free(evt);
   1.224 +
   1.225 +    v->time_this_phase += now - v->state_start_time;
   1.226 +    printf("%s: v%d time_this_phase %d\n",
   1.227 +           __func__, v->vid, v->time_this_phase);
   1.228 +
   1.229 +    ASSERT(v->time_this_phase < v->e->time);
   1.230 +
   1.231 +    v->was_preempted = 1;
   1.232 +}
   1.233 +
   1.234 +
   1.235 +/* Callbacks the scheduler may make */
   1.236 +void sim_sched_timer(int time, int pid)
   1.237 +{
   1.238 +    sim_insert_event(sim.now + time, EVT_TIMER, pid, 1);
   1.239 +}
   1.240 +
   1.241 +void sim_runstate_change(int now, struct vm *v, int new_runstate)
   1.242 +{
   1.243 +    int ostate, nstate;
   1.244 +    int stime = now - v->state_start_time;
   1.245 +
   1.246 +    /* Valid transitions:
   1.247 +     * + R->A (preemption): remove block event
   1.248 +     * + R->B (block)     : Insert wake event
   1.249 +     * + A->R (run)       : Insert block event
   1.250 +     * + B->A (wake)      : No action necessary
   1.251 +     */
   1.252 +
   1.253 +    switch ( v->runstate )
   1.254 +    {
   1.255 +    case RUNSTATE_RUNNING:
   1.256 +        ostate = STATE_RUN;
   1.257 +        break;
   1.258 +    case RUNSTATE_RUNNABLE:
   1.259 +        if ( v->was_preempted )
   1.260 +            ostate = STATE_PREEMPT;
   1.261 +        else
   1.262 +            ostate = STATE_WAKE;
   1.263 +        break;
   1.264 +    case RUNSTATE_BLOCKED:
   1.265 +        ostate = STATE_BLOCK;
   1.266 +        break;
   1.267 +    }
   1.268 +
   1.269 +    update_cycles(&v->stats.state[ostate], stime);
   1.270 +
   1.271 +
   1.272 +    if ( v->runstate == RUNSTATE_RUNNING
   1.273 +         && new_runstate == RUNSTATE_RUNNABLE )
   1.274 +    {
   1.275 +        nstate = STATE_PREEMPT;
   1.276 +        vm_preempt(now, v);
   1.277 +    }
   1.278 +    else if ( v->runstate == RUNSTATE_RUNNING
   1.279 +              && new_runstate == RUNSTATE_BLOCKED )
   1.280 +    {
   1.281 +        nstate = STATE_BLOCK;
   1.282 +        vm_block(now, v);
   1.283 +    }
   1.284 +    else if ( v->runstate == RUNSTATE_RUNNABLE
   1.285 +              && new_runstate == RUNSTATE_RUNNING )
   1.286 +    {
   1.287 +        nstate = STATE_RUN;
   1.288 +        vm_run(now, v);
   1.289 +    }
   1.290 +    else if ( v->runstate == RUNSTATE_BLOCKED
   1.291 +              && new_runstate == RUNSTATE_RUNNABLE )
   1.292 +    {
   1.293 +        nstate = STATE_WAKE;
   1.294 +        vm_wake(now, v);
   1.295 +    }
   1.296 +    else
   1.297 +        goto unexpected_transition;
   1.298 +
   1.299 +    printf("%d: v%d %s %d -> %s\n",
   1.300 +           now, v->vid, state_name[ostate], stime, state_name[nstate]);
   1.301 +
   1.302 +    v->runstate = new_runstate;
   1.303 +    v->state_start_time = now;
   1.304 +
   1.305 +    return;
   1.306 +
   1.307 +unexpected_transition:
   1.308 +    fprintf(stderr, "Unexpected transition for vm %d: %d->%d\n",
   1.309 +            v->vid,
   1.310 +            v->runstate,
   1.311 +            new_runstate);
   1.312 +    exit(1);
   1.313 +}
   1.314 +
   1.315 +/* 
   1.316 + * Main loop
   1.317 + */
   1.318 +void simulate(void)
   1.319 +{
   1.320 +    while ( sim.now < opt.time_limit )
   1.321 +    {
   1.322 +        /* Take next event off list */
   1.323 +        struct event evt;
   1.324 +
   1.325 +        evt = sim_next_event();
   1.326 +
   1.327 +        sim.now = evt.time;
   1.328 +
   1.329 +        switch(evt.type)
   1.330 +        {
   1.331 +        case EVT_WAKE:
   1.332 +        {
   1.333 +            struct vm *v = vm_from_vid(evt.param);
   1.334 +            ASSERT(v->processor == -1);
   1.335 +            sim_runstate_change(sim.now, v, RUNSTATE_RUNNABLE);
   1.336 +            sim.sched_ops->wake(sim.now, v);
   1.337 +        }
   1.338 +        break;
   1.339 +        case EVT_BLOCK:
   1.340 +        {
   1.341 +            struct vm *v = vm_from_vid(evt.param);
   1.342 +
   1.343 +            ASSERT(v->processor != -1);
   1.344 +            ASSERT(v->processor <= P.count);
   1.345 +
   1.346 +            sim_runstate_change(sim.now, v, RUNSTATE_BLOCKED);
   1.347 +
   1.348 +            evt.param = v->processor; /* FIXME */
   1.349 +        }
   1.350 +        /* FALL-THRU */
   1.351 +        case EVT_TIMER:
   1.352 +        {
   1.353 +            struct vm *prev, *next;
   1.354 +            int pid = evt.param;
   1.355 +
   1.356 +            ASSERT(pid < P.count);
   1.357 +
   1.358 +            prev = P.pcpus[pid].current;
   1.359 +
   1.360 +            next = sim.sched_ops->schedule(sim.now, pid);
   1.361 +
   1.362 +            if ( prev && prev != next )
   1.363 +            {
   1.364 +                prev->processor = -1;
   1.365 +                if( prev->runstate != RUNSTATE_BLOCKED )
   1.366 +                    sim_runstate_change(sim.now, prev, RUNSTATE_RUNNABLE);
   1.367 +            }
   1.368 +
   1.369 +            sim_runstate_change(sim.now, next, RUNSTATE_RUNNING);
   1.370 +            P.pcpus[pid].current = next;
   1.371 +            next->processor = pid;
   1.372 +        }
   1.373 +        break;
   1.374 +        default:
   1.375 +            fprintf(stderr, "Unexpected event type: %d\n", evt.type);
   1.376 +            exit(1);
   1.377 +            break;
   1.378 +        }
   1.379 +    }
   1.380 +}
   1.381 +
   1.382 +void init(void)
   1.383 +{
   1.384 +    int vid, i;
   1.385 +    const struct workload *w;
   1.386 +
   1.387 +    /* Initialize simulation variables */
   1.388 +    sim.now=0;
   1.389 +    sim.timer=NULL;
   1.390 +    INIT_LIST_HEAD(&sim.events);
   1.391 +    sim.sched_ops = &opt.scheduler->ops;
   1.392 +
   1.393 +    /* Initialize pcpus */
   1.394 +    P.count = opt.pcpu_count;
   1.395 +    for ( i=0; i<P.count; i++ )
   1.396 +    {
   1.397 +        P.pcpus[i].pid = i;
   1.398 +        P.pcpus[i].current = NULL;
   1.399 +    }
   1.400 +
   1.401 +    /* Initialize scheduler */
   1.402 +    sim.sched_ops->sched_init();
   1.403 +
   1.404 +    /* Initialize vms */
   1.405 +    w=opt.workload;
   1.406 +    for ( vid=0; vid<w->vm_count; vid++)
   1.407 +    {
   1.408 +        struct vm *v = V.vms+vid;
   1.409 +
   1.410 +        v->vid = vid;
   1.411 +        v->runstate = RUNSTATE_BLOCKED;
   1.412 +        v->processor = -1;
   1.413 +        v->private = NULL;
   1.414 +
   1.415 +        v->state_start_time = 0;
   1.416 +        v->time_this_phase = 0;
   1.417 +        
   1.418 +
   1.419 +        v->phase_index = -1;
   1.420 +        v->e = NULL;
   1.421 +        v->workload = w->vm_workloads+vid;
   1.422 +        
   1.423 +        V.count++;
   1.424 +
   1.425 +        sim.sched_ops->vm_init(vid);
   1.426 +    }
   1.427 +
   1.428 +    /* Set VM starting conditions */
   1.429 +    for ( vid=0; vid<V.count; vid++)
   1.430 +    {
   1.431 +        struct vm *v = V.vms+vid;
   1.432 +
   1.433 +        switch(v->workload->list[0].type)
   1.434 +        {
   1.435 +        case PHASE_RUN:
   1.436 +            v->phase_index = v->workload->phase_count - 1;
   1.437 +            v->e = v->workload->list + v->phase_index;
   1.438 +
   1.439 +            sim_insert_event(sim.now, EVT_WAKE, v->vid, 0);
   1.440 +            v->state_start_time = sim.now;
   1.441 +            v->time_this_phase = 0;
   1.442 +            break;
   1.443 +        case PHASE_BLOCK:
   1.444 +            v->phase_index = 0;
   1.445 +            v->e = v->workload->list;
   1.446 +
   1.447 +            sim_insert_event(sim.now + v->e->time, EVT_WAKE, v->vid, 0);
   1.448 +            v->state_start_time = sim.now;
   1.449 +            v->time_this_phase = 0;
   1.450 +            break;
   1.451 +        }
   1.452 +    }
   1.453 +
   1.454 +    /* Insert initial scheduler timer */
   1.455 +    for ( i=0; i<P.count; i++)
   1.456 +        sim_insert_event(sim.now, EVT_TIMER, i, 1);
   1.457 +}
   1.458 +
   1.459 +void report(void)
   1.460 +{
   1.461 +    int i, j;
   1.462 +
   1.463 +    for ( i=0; i<V.count; i++ )
   1.464 +    {
   1.465 +        struct vm *v = V.vms + i;
   1.466 +
   1.467 +        printf("VM %d\n", i);
   1.468 +        for ( j = 0; j < STATE_MAX ; j++ )
   1.469 +        {
   1.470 +            char s[128];
   1.471 +            snprintf(s, 128, " %s", state_name[j]);
   1.472 +            print_cycle_summary(&v->stats.state[j],   sim.now, s);
   1.473 +        }
   1.474 +    }
   1.475 +}
   1.476 +
   1.477 +int main(int argc, char * argv[])
   1.478 +{
   1.479 +    warn = stdout;
   1.480 +
   1.481 +    /* Read opts, config file? */
   1.482 +    if ( !opt.workload )
   1.483 +        opt.workload = builtin_workloads+default_workload;
   1.484 +
   1.485 +    if ( !opt.scheduler )
   1.486 +        opt.scheduler = schedulers[default_scheduler];
   1.487 +    /* Setup simulation */
   1.488 +    init();
   1.489 +    
   1.490 +    /* Run simulation */
   1.491 +    simulate();
   1.492 +    /* Report statistics */
   1.493 +    report();
   1.494 +}