gdunlap/sched-sim.hg

annotate sched_credit01.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
children e274ac3f81ff
rev   line source
gdunlap@5 1 #include <stdio.h>
gdunlap@5 2 #include <stdlib.h>
gdunlap@5 3 #include <assert.h>
gdunlap@5 4
gdunlap@5 5 #define ASSERT assert
gdunlap@5 6
gdunlap@5 7 #include "list.h"
gdunlap@5 8 #include "sim.h"
gdunlap@5 9
gdunlap@5 10
gdunlap@5 11 #define MAX_VMS 16
gdunlap@5 12 #define CREDIT_INIT 500
gdunlap@5 13 #define CREDIT_RESET 0
gdunlap@5 14 #define MAX_TIMER 200
gdunlap@5 15 #define MIN_TIMER 100
gdunlap@5 16
gdunlap@5 17 struct sched_vm {
gdunlap@5 18 struct list_head runq_elem;
gdunlap@5 19 struct vm *v;
gdunlap@5 20
gdunlap@5 21 int weight;
gdunlap@5 22
gdunlap@5 23 int credit;
gdunlap@5 24 int credit_per_min_timer; /* ? */
gdunlap@5 25 int start_time;
gdunlap@5 26 int vid;
gdunlap@5 27 };
gdunlap@5 28
gdunlap@5 29 struct {
gdunlap@5 30 struct list_head runq; /* Global run queue */
gdunlap@5 31 int max_vm;
gdunlap@5 32 struct sched_vm vms[MAX_VMS];
gdunlap@5 33 int ncpus;
gdunlap@5 34
gdunlap@5 35 int global_weight;
gdunlap@5 36 int scale_factor; /* ? */
gdunlap@5 37
gdunlap@5 38 int next_check;
gdunlap@5 39 } sched_priv;
gdunlap@5 40
gdunlap@5 41
gdunlap@5 42
gdunlap@5 43 void reset_credit(int time)
gdunlap@5 44 {
gdunlap@5 45 int i;
gdunlap@5 46 for ( i=0; i<=sched_priv.max_vm; i++)
gdunlap@5 47 {
gdunlap@5 48 sched_priv.vms[i].credit = CREDIT_INIT;
gdunlap@5 49 sched_priv.vms[i].start_time = time;
gdunlap@5 50 }
gdunlap@5 51 /* No need to resort runq, as everyone's credit is now zero */
gdunlap@5 52 }
gdunlap@5 53
gdunlap@5 54 void dump_credit(int time, struct sched_vm *svm)
gdunlap@5 55 {
gdunlap@5 56 printf("credit v%d %d %d\n", svm->vid, time, svm->credit);
gdunlap@5 57 }
gdunlap@5 58
gdunlap@5 59 void burn_credit(struct sched_vm *svm, int time)
gdunlap@5 60 {
gdunlap@5 61 ASSERT(time >= svm->start_time);
gdunlap@5 62
gdunlap@5 63 svm->credit -= (time - svm->start_time);
gdunlap@5 64 svm->start_time = time;
gdunlap@5 65
gdunlap@5 66 dump_credit(time, svm);
gdunlap@5 67 }
gdunlap@5 68
gdunlap@5 69 int calc_timer(struct sched_vm *svm)
gdunlap@5 70 {
gdunlap@5 71 int time = MAX_TIMER;
gdunlap@5 72
gdunlap@5 73 if ( time > svm->credit )
gdunlap@5 74 time = svm->credit;
gdunlap@5 75 #if 0
gdunlap@5 76 if ( !list_empty(&sched_priv.runq) )
gdunlap@5 77 {
gdunlap@5 78 struct sched_vm *sq = list_entry(sched_priv.runq.next, struct sched_vm, runq_elem);
gdunlap@5 79
gdunlap@5 80 ASSERT(svm->credit >= sq->credit);
gdunlap@5 81
gdunlap@5 82 if ( (svm->credit - sq->credit) < time )
gdunlap@5 83 time = (svm->credit - sq->credit);
gdunlap@5 84 }
gdunlap@5 85
gdunlap@5 86 #endif
gdunlap@5 87
gdunlap@5 88 if ( time < MIN_TIMER )
gdunlap@5 89 time = MIN_TIMER;
gdunlap@5 90 return time;
gdunlap@5 91 }
gdunlap@5 92
gdunlap@5 93 void runq_insert(struct sched_vm *svm)
gdunlap@5 94 {
gdunlap@5 95 struct list_head *iter;
gdunlap@5 96 int pos = 0;
gdunlap@5 97
gdunlap@5 98 list_for_each( iter, &sched_priv.runq )
gdunlap@5 99 {
gdunlap@5 100 struct sched_vm * iter_svm;
gdunlap@5 101
gdunlap@5 102 iter_svm = list_entry(iter, struct sched_vm, runq_elem);
gdunlap@5 103
gdunlap@5 104 if ( svm->credit > iter_svm->credit )
gdunlap@5 105 {
gdunlap@5 106 printf(" p%d v%d\n",
gdunlap@5 107 pos,
gdunlap@5 108 iter_svm->vid);
gdunlap@5 109 break;
gdunlap@5 110 }
gdunlap@5 111 pos++;
gdunlap@5 112 }
gdunlap@5 113
gdunlap@5 114 list_add_tail(&svm->runq_elem, iter);
gdunlap@5 115 }
gdunlap@5 116
gdunlap@5 117 static void sched_credit_init(void)
gdunlap@5 118 {
gdunlap@5 119 printf("%s()\n", __func__);
gdunlap@5 120 INIT_LIST_HEAD(&sched_priv.runq);
gdunlap@5 121 sched_priv.max_vm=0;
gdunlap@5 122 }
gdunlap@5 123
gdunlap@5 124 static void sched_credit_vm_init(int vid)
gdunlap@5 125 {
gdunlap@5 126 struct sched_vm *svm;
gdunlap@5 127
gdunlap@5 128 printf("%s: vm %d\n", __func__, vid);
gdunlap@5 129
gdunlap@5 130 if ( vid > MAX_VMS )
gdunlap@5 131 {
gdunlap@5 132 fprintf(stderr, "vid %d > MAX_VMS %d!\n", vid, MAX_VMS);
gdunlap@5 133 exit(1);
gdunlap@5 134 }
gdunlap@5 135
gdunlap@5 136 svm = sched_priv.vms + vid;
gdunlap@5 137
gdunlap@5 138 INIT_LIST_HEAD(&svm->runq_elem);
gdunlap@5 139
gdunlap@5 140 svm->vid = vid;
gdunlap@5 141 svm->v = vm_from_vid(vid);
gdunlap@5 142
gdunlap@5 143 svm->credit = CREDIT_INIT;
gdunlap@5 144 svm->weight = 1;
gdunlap@5 145 svm->start_time = 0;
gdunlap@5 146
gdunlap@5 147 if ( vid > sched_priv.max_vm )
gdunlap@5 148 sched_priv.max_vm = vid;
gdunlap@5 149 }
gdunlap@5 150
gdunlap@5 151 static void sched_credit_wake(int time, int vid)
gdunlap@5 152 {
gdunlap@5 153 struct vm *v;
gdunlap@5 154 struct sched_vm *svm;
gdunlap@5 155
gdunlap@5 156 v = vm_from_vid(vid);
gdunlap@5 157
gdunlap@5 158 printf("%s: time %d vid %d\n",
gdunlap@5 159 __func__, time, v->vid);
gdunlap@5 160
gdunlap@5 161 svm = sched_priv.vms + v->vid;
gdunlap@5 162
gdunlap@5 163 ASSERT(list_empty(&svm->runq_elem));
gdunlap@5 164
gdunlap@5 165 runq_insert(svm);
gdunlap@5 166
gdunlap@5 167 /* Never preempt on wake; only kick idle processors */
gdunlap@5 168 if ( P.idle > 0 )
gdunlap@5 169 {
gdunlap@5 170 int i;
gdunlap@5 171
gdunlap@5 172 for ( i=0; i<P.count; i++ )
gdunlap@5 173 if ( P.pcpus[i].idle )
gdunlap@5 174 break;
gdunlap@5 175
gdunlap@5 176 printf(" %s: waking p%d\n", __func__, i);
gdunlap@5 177 sim_sched_timer(0, i);
gdunlap@5 178 }
gdunlap@5 179 }
gdunlap@5 180
gdunlap@5 181 static struct vm* sched_credit_schedule(int time, int pid)
gdunlap@5 182 {
gdunlap@5 183 struct sched_vm *svm;
gdunlap@5 184 struct vm *next, *prev;
gdunlap@5 185 int timer;
gdunlap@5 186
gdunlap@5 187 printf("%s: time %d pid %d\n",
gdunlap@5 188 __func__, time, pid);
gdunlap@5 189 prev = current(pid);
gdunlap@5 190
gdunlap@5 191 if ( prev )
gdunlap@5 192 {
gdunlap@5 193 printf(" current v%d\n", prev->vid);
gdunlap@5 194 svm = sched_priv.vms + prev->vid;
gdunlap@5 195
gdunlap@5 196 burn_credit(svm, time);
gdunlap@5 197
gdunlap@5 198 if ( svm->v->runstate == RUNSTATE_RUNNING )
gdunlap@5 199 {
gdunlap@5 200 printf(" adding to runqueue\n");
gdunlap@5 201 runq_insert(svm);
gdunlap@5 202 }
gdunlap@5 203 }
gdunlap@5 204
gdunlap@5 205 /* Take guy on front of runqueue, set new timer */
gdunlap@5 206 if ( list_empty(&sched_priv.runq) )
gdunlap@5 207 {
gdunlap@5 208 printf(" No runnable entities\n");
gdunlap@5 209 return NULL;
gdunlap@5 210 }
gdunlap@5 211
gdunlap@5 212 svm = list_entry(sched_priv.runq.next, struct sched_vm, runq_elem);
gdunlap@5 213
gdunlap@5 214 list_del_init(&svm->runq_elem);
gdunlap@5 215
gdunlap@5 216 next = svm->v;
gdunlap@5 217
gdunlap@5 218 if ( svm->credit <= CREDIT_RESET )
gdunlap@5 219 {
gdunlap@5 220 printf(" vid %d credit %c, resetting credit at time %d\n",
gdunlap@5 221 svm->vid,
gdunlap@5 222 svm->credit,
gdunlap@5 223 time);
gdunlap@5 224 reset_credit(time);
gdunlap@5 225 }
gdunlap@5 226
gdunlap@5 227 dump_credit(time, svm);
gdunlap@5 228 svm->start_time = time;
gdunlap@5 229
gdunlap@5 230 timer = calc_timer(svm);
gdunlap@5 231
gdunlap@5 232 sim_sched_timer(timer, pid);
gdunlap@5 233
gdunlap@5 234 printf(" next: v%d\n", next->vid);
gdunlap@5 235
gdunlap@5 236 return next;
gdunlap@5 237 }
gdunlap@5 238
gdunlap@5 239 struct scheduler sched_credit01 =
gdunlap@5 240 {
gdunlap@5 241 .name="credit01",
gdunlap@5 242 .desc="Zero-start, burn based on weight, reset to zero at negative credit",
gdunlap@5 243 .ops = {
gdunlap@5 244 .sched_init = sched_credit_init,
gdunlap@5 245 .vm_init = sched_credit_vm_init,
gdunlap@5 246 .wake = sched_credit_wake,
gdunlap@5 247 .schedule = sched_credit_schedule
gdunlap@5 248 }
gdunlap@5 249 };