gdunlap/sched-sim.hg

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
5 #define ASSERT assert
7 #include "list.h"
8 #include "sim.h"
11 #define MAX_VMS 16
12 #define TSLICE 2000
14 struct sched_vm {
15 struct list_head queue;
16 int vid;
17 struct vm *v;
18 };
20 struct {
21 struct list_head queue;
22 struct sched_vm vms[MAX_VMS];
23 } sched_priv;
26 void sched_rr_init(void)
27 {
28 printf("%s()\n", __func__);
29 INIT_LIST_HEAD(&sched_priv.queue);
30 }
32 void sched_rr_vm_init(int vid)
33 {
34 struct sched_vm *svm;
36 printf("%s: vm %d\n", __func__, vid);
38 if ( vid > MAX_VMS )
39 {
40 fprintf(stderr, "vid %d > MAX_VMS %d!\n", vid, MAX_VMS);
41 exit(1);
42 }
44 svm = sched_priv.vms + vid;
46 INIT_LIST_HEAD(&svm->queue);
48 svm->vid = vid;
49 svm->v = vm_from_vid(vid);
51 }
53 void sched_rr_wake(int time, struct vm * v)
54 {
55 struct sched_vm *svm;
57 printf("%s: time %d vid %d\n",
58 __func__, time, v->vid);
60 svm = sched_priv.vms + v->vid;
62 ASSERT(list_empty(&svm->queue));
64 list_add_tail(&svm->queue, &sched_priv.queue);
65 }
67 struct vm* sched_rr_schedule(int time, int pid)
68 {
69 struct sched_vm *svm;
70 struct vm *next, *prev;
72 printf("%s: time %d pid %d\n",
73 __func__, time, pid);
74 prev = current(pid);
76 if ( prev )
77 {
78 printf(" current v%d\n", prev->vid);
79 svm = sched_priv.vms + prev->vid;
81 if ( svm->v->runstate == RUNSTATE_RUNNING )
82 {
83 printf(" adding to runqueue\n");
84 list_add_tail(&svm->queue, &sched_priv.queue);
85 }
86 }
88 /* Take guy on front of runqueue, set new timer */
89 if ( list_empty(&sched_priv.queue) )
90 {
91 printf(" No runnable entities\n");
92 return NULL;
93 }
95 svm = list_entry(sched_priv.queue.next, struct sched_vm, queue);
97 list_del_init(&svm->queue);
98 next = svm->v;
100 sim_sched_timer(TSLICE, pid);
102 printf(" next: v%d\n", next->vid);
104 return next;
105 }
107 struct scheduler sched_rr =
108 {
109 .name="round-robin",
110 .ops = {
111 .sched_init = sched_rr_init,
112 .vm_init = sched_rr_vm_init,
113 .wake = sched_rr_wake,
114 .schedule = sched_rr_schedule
115 }
116 };