gdunlap/sched-sim.hg

diff sched_rr.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/sched_rr.c	Tue Oct 13 16:06:36 2009 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <assert.h>
     1.7 +
     1.8 +#define ASSERT assert
     1.9 +
    1.10 +#include "list.h"
    1.11 +#include "sim.h"
    1.12 +
    1.13 +
    1.14 +#define MAX_VMS 16
    1.15 +#define TSLICE 2000
    1.16 +
    1.17 +struct sched_vm {
    1.18 +    struct list_head queue;
    1.19 +    int vid;
    1.20 +    struct vm *v;
    1.21 +};
    1.22 +
    1.23 +struct {
    1.24 +    struct list_head queue;
    1.25 +    struct sched_vm vms[MAX_VMS];
    1.26 +} sched_priv;
    1.27 +
    1.28 +
    1.29 +void sched_rr_init(void)
    1.30 +{
    1.31 +    printf("%s()\n", __func__);
    1.32 +    INIT_LIST_HEAD(&sched_priv.queue);
    1.33 +}
    1.34 +
    1.35 +void sched_rr_vm_init(int vid)
    1.36 +{
    1.37 +    struct sched_vm *svm;
    1.38 +
    1.39 +    printf("%s: vm %d\n", __func__, vid);
    1.40 +
    1.41 +    if ( vid > MAX_VMS )
    1.42 +    {
    1.43 +        fprintf(stderr, "vid %d > MAX_VMS %d!\n", vid, MAX_VMS);
    1.44 +        exit(1);
    1.45 +    }
    1.46 +
    1.47 +    svm = sched_priv.vms + vid;
    1.48 +
    1.49 +    INIT_LIST_HEAD(&svm->queue);
    1.50 +
    1.51 +    svm->vid = vid;
    1.52 +    svm->v = vm_from_vid(vid);
    1.53 +    
    1.54 +}
    1.55 +
    1.56 +void sched_rr_wake(int time, struct vm * v)
    1.57 +{
    1.58 +    struct sched_vm *svm;
    1.59 +
    1.60 +    printf("%s: time %d vid %d\n",
    1.61 +           __func__, time, v->vid);
    1.62 +
    1.63 +    svm = sched_priv.vms + v->vid;
    1.64 +
    1.65 +    ASSERT(list_empty(&svm->queue));
    1.66 +
    1.67 +    list_add_tail(&svm->queue, &sched_priv.queue);
    1.68 +}
    1.69 +
    1.70 +struct vm* sched_rr_schedule(int time, int pid)
    1.71 +{
    1.72 +    struct sched_vm *svm;
    1.73 +    struct vm *next, *prev;
    1.74 +
    1.75 +    printf("%s: time %d pid %d\n",
    1.76 +           __func__, time, pid);
    1.77 +    prev = current(pid);
    1.78 +
    1.79 +    if ( prev )
    1.80 +    {
    1.81 +        printf(" current v%d\n", prev->vid);
    1.82 +        svm = sched_priv.vms + prev->vid;
    1.83 +
    1.84 +        if ( svm->v->runstate == RUNSTATE_RUNNING )
    1.85 +        {
    1.86 +            printf(" adding to runqueue\n");
    1.87 +            list_add_tail(&svm->queue, &sched_priv.queue);
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    /* Take guy on front of runqueue, set new timer */
    1.92 +    if ( list_empty(&sched_priv.queue) )
    1.93 +    {
    1.94 +        printf(" No runnable entities\n");
    1.95 +        return NULL;
    1.96 +    }
    1.97 +
    1.98 +    svm = list_entry(sched_priv.queue.next, struct sched_vm, queue);
    1.99 +
   1.100 +    list_del_init(&svm->queue);
   1.101 +    next = svm->v;
   1.102 +
   1.103 +    sim_sched_timer(TSLICE, pid);
   1.104 +
   1.105 +    printf(" next: v%d\n", next->vid);
   1.106 +    
   1.107 +    return next;
   1.108 +}
   1.109 +
   1.110 +struct scheduler sched_rr =
   1.111 +{
   1.112 +    .name="round-robin",
   1.113 +    .ops = {
   1.114 +        .sched_init = sched_rr_init,
   1.115 +        .vm_init    = sched_rr_vm_init,
   1.116 +        .wake       = sched_rr_wake,
   1.117 +        .schedule   = sched_rr_schedule
   1.118 +    }
   1.119 +};