-
Notifications
You must be signed in to change notification settings - Fork 0
/
0001-Implement-lottery-scheduling.patch
157 lines (146 loc) · 3.68 KB
/
0001-Implement-lottery-scheduling.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
From cba60cc4f1acce44acf7d72756a494444e4a5c68 Mon Sep 17 00:00:00 2001
From: Aravind Vaddi <vav263@nyu.edu>
Date: Wed, 20 Nov 2019 17:31:50 -0500
Subject: [PATCH] Implement lottery scheduling
---
Makefile | 1 +
proc.c | 16 ++++++++++++++++
proc.h | 1 +
syscall.c | 2 ++
syscall.h | 1 +
sysproc.c | 11 +++++++++++
user.h | 1 +
usys.S | 1 +
8 files changed, 34 insertions(+)
diff --git a/Makefile b/Makefile
index 5d91068..7d17a81 100644
--- a/Makefile
+++ b/Makefile
@@ -174,6 +174,7 @@ UPROGS=\
_wc\
_zombie\
_hackbench\
+ _lotterytest\
fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
diff --git a/proc.c b/proc.c
index b122e3b..dab96e9 100644
--- a/proc.c
+++ b/proc.c
@@ -72,6 +72,7 @@ found:
p->context = (struct context*)sp;
memset(p->context, 0, sizeof *p->context);
p->context->eip = (uint)forkret;
+ p->tickets = 20;
return p;
}
@@ -280,10 +281,24 @@ scheduler(void)
// Loop over process table looking for process to run.
acquire(&ptable.lock);
+
+ long max_tickets = 0;
+
+ for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
+ if(p->state == RUNNABLE)
+ max_tickets += p->tickets;
+ }
+
+ long lottery_winner = random_at_most(max_tickets);
+
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
if(p->state != RUNNABLE)
continue;
+ lottery_winner -= p->tickets;
+ if(lottery_winner > 0) {
+ continue;
+ }
// Switch to chosen process. It is the process's job
// to release ptable.lock and then reacquire it
// before jumping back to us.
@@ -297,6 +312,7 @@ scheduler(void)
// Process is done running for now.
// It should have changed its p->state before coming back.
proc = 0;
+ break;
}
release(&ptable.lock);
diff --git a/proc.h b/proc.h
index 3b9c3ac..ca1b0dc 100644
--- a/proc.h
+++ b/proc.h
@@ -54,6 +54,7 @@ enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
// Per-process state
struct proc {
uint sz; // Size of process memory (bytes)
+ int tickets;
pde_t* pgdir; // Page table
char *kstack; // Bottom of kernel stack for this process
enum procstate state; // Process state
diff --git a/syscall.c b/syscall.c
index 0e06ad4..849877c 100644
--- a/syscall.c
+++ b/syscall.c
@@ -99,6 +99,7 @@ extern int sys_wait(void);
extern int sys_write(void);
extern int sys_uptime(void);
extern int sys_gettime(void);
+extern int sys_settickets(void);
static int (*syscalls[])(void) = {
[SYS_fork] sys_fork,
@@ -123,6 +124,7 @@ static int (*syscalls[])(void) = {
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_gettime] sys_gettime,
+[SYS_settickets] sys_settickets,
};
void
diff --git a/syscall.h b/syscall.h
index 6d6c224..e59bc4a 100644
--- a/syscall.h
+++ b/syscall.h
@@ -21,3 +21,4 @@
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_gettime 22
+#define SYS_settickets 23
diff --git a/sysproc.c b/sysproc.c
index ddaed7c..5782f02 100644
--- a/sysproc.c
+++ b/sysproc.c
@@ -98,3 +98,14 @@ sys_gettime(void) {
cmostime(d);
return 0;
}
+
+int
+sys_settickets(void) {
+
+ int n;
+ if(argint(0, &n) < 0)
+ return -1;
+
+ proc->tickets = n;
+ return 0;
+}
diff --git a/user.h b/user.h
index 46d1059..58e344c 100644
--- a/user.h
+++ b/user.h
@@ -24,6 +24,7 @@ char* sbrk(int);
int sleep(int);
int uptime(void);
int gettime(struct rtcdate *);
+int settickets(int);
// ulib.c
int stat(char*, struct stat*);
diff --git a/usys.S b/usys.S
index e556d66..27646a9 100644
--- a/usys.S
+++ b/usys.S
@@ -30,3 +30,4 @@ SYSCALL(sbrk)
SYSCALL(sleep)
SYSCALL(uptime)
SYSCALL(gettime)
+SYSCALL(settickets)
--
2.17.1