-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1905095.patch
550 lines (534 loc) · 13.8 KB
/
1905095.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
diff --git a/Makefile b/Makefile
index 39a99d7..aa8be30 100644
--- a/Makefile
+++ b/Makefile
@@ -28,7 +28,8 @@ OBJS = \
$K/sysfile.o \
$K/kernelvec.o \
$K/plic.o \
- $K/virtio_disk.o
+ $K/virtio_disk.o \
+ $K/rand.o \
# riscv64-unknown-elf- or riscv64-linux-gnu-
# perhaps in /opt/riscv/bin
@@ -132,6 +133,8 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
+ $U/_dummyproc\
+ $U/_testprocinfo\
fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
@@ -153,7 +156,7 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
then echo "-gdb tcp::$(GDBPORT)"; \
else echo "-s -p $(GDBPORT)"; fi)
ifndef CPUS
-CPUS := 3
+CPUS := 1
endif
QEMUOPTS = -machine virt -bios none -kernel $K/kernel -m 128M -smp $(CPUS) -nographic
diff --git a/kernel/defs.h b/kernel/defs.h
index a3c962b..0b40f56 100644
--- a/kernel/defs.h
+++ b/kernel/defs.h
@@ -106,6 +106,10 @@ void yield(void);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void);
+int settickets(int);
+int getpinfo(uint64);
+int getCurTickets(void);
+
// swtch.S
void swtch(struct context*, struct context*);
@@ -187,3 +191,6 @@ void virtio_disk_intr(void);
// number of elements in fixed-size array
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
+
+//rand.c
+long genrand(int);
diff --git a/kernel/param.h b/kernel/param.h
index 6624bff..c3ccecc 100644
--- a/kernel/param.h
+++ b/kernel/param.h
@@ -11,3 +11,7 @@
#define NBUF (MAXOPBLOCKS*3) // size of disk block cache
#define FSSIZE 2000 // size of file system in blocks
#define MAXPATH 128 // maximum file path name
+#define BOOST_INTERVAL 64 // time interval for boosting priority
+#define TIME_LIMIT_1 1 // time limit for priority 1
+#define TIME_LIMIT_2 2 // time limit for priority 2
+#define DEFAULT_TICKET_COUNT 10 // default ticket count
diff --git a/kernel/proc.c b/kernel/proc.c
index 959b778..87d3839 100644
--- a/kernel/proc.c
+++ b/kernel/proc.c
@@ -5,6 +5,7 @@
#include "spinlock.h"
#include "proc.h"
#include "defs.h"
+#include "pstat.h"
struct cpu cpus[NCPU];
@@ -114,6 +115,9 @@ allocproc(void)
for(p = proc; p < &proc[NPROC]; p++) {
acquire(&p->lock);
if(p->state == UNUSED) {
+ p->tickets_original = DEFAULT_TICKET_COUNT;
+ p->tickets_current = DEFAULT_TICKET_COUNT;
+ p->time_slices = 0;
goto found;
} else {
release(&p->lock);
@@ -318,7 +322,16 @@ fork(void)
np->parent = p;
release(&wait_lock);
+ int tickets_original = 0;
+ acquire(&p->lock);
+ tickets_original = p->tickets_original;
+ release(&p->lock);
+
+
acquire(&np->lock);
+ np->tickets_original = tickets_original;
+ np->tickets_current = tickets_original;
+ np->time_slices = 0;
np->state = RUNNABLE;
release(&np->lock);
@@ -451,23 +464,85 @@ scheduler(void)
for(;;){
// Avoid deadlock by ensuring that devices can interrupt.
intr_on();
+ c->proc = 0;
+ acquire(&tickslock);
+ if(ticks%BOOST_INTERVAL == 0){
+ for(p=proc; p<&proc[NPROC]; p++){
+ acquire(&p->lock);
+ p->inQ = 1;
+ release(&p->lock);
+ }
+ }
+ release(&tickslock);
+
+ int ctckts = getCurTickets();
+ if(ctckts==0){
+ for(p=proc; p<&proc[NPROC]; p++){
+ acquire(&p->lock);
+ p->tickets_current = p->tickets_original;
+ release(&p->lock);
+ }
+ continue;
+ }
- for(p = proc; p < &proc[NPROC]; p++) {
+ int tickets_in_q1 = 0;
+ for(p=proc; p<&proc[NPROC]; p++){
acquire(&p->lock);
- if(p->state == RUNNABLE) {
- // Switch to chosen process. It is the process's job
- // to release its lock and then reacquire it
- // before jumping back to us.
- p->state = RUNNING;
- c->proc = p;
- swtch(&c->context, &p->context);
-
- // Process is done running for now.
- // It should have changed its p->state before coming back.
- c->proc = 0;
+ if(p->inQ==1 && p->state == RUNNABLE){
+ tickets_in_q1 = tickets_in_q1 +1;
}
release(&p->lock);
}
+
+ int randTicket = genrand(ctckts) + 1;
+ int ctckts_cumu = 0;
+ int used = 0;
+ if(tickets_in_q1 > 0){
+ for(p=proc; p<&proc[NPROC]; p++){
+ acquire(&p->lock);
+ if(p->inQ==1 && p->state == RUNNABLE){
+ ctckts_cumu = ctckts_cumu + p->tickets_current;
+ if(ctckts_cumu >= randTicket && p->tickets_current > 0 && used == 0){
+ used=1;
+ p->tickets_current = p->tickets_current -1;
+ p->time_slices= p->time_slices+1;
+ p->genuine_time_slices= 0;
+ p->state = RUNNING;
+ c->proc = p;
+ swtch(&c->context, &p->context);
+ if(p->genuine_time_slices == TIME_LIMIT_1){
+ p->inQ = 2;
+ }
+ c->proc = 0;
+ }
+ }
+ release(&p->lock);
+ }
+ }
+ else{
+ for(p = proc; p < &proc[NPROC]; p++) {
+ acquire(&p->lock);
+ if(p->inQ==2 && p->state == RUNNABLE) {
+ // Switch to chosen process. It is the process's job
+ // to release its lock and then reacquire it
+ // before jumping back to us.
+ p->state = RUNNING;
+ c->proc = p;
+ swtch(&c->context, &p->context);
+ if(p->genuine_time_slices<TIME_LIMIT_2){
+ p->inQ=1;
+ }
+
+ // Process is done running for now.
+ // It should have changed its p->state before coming back.
+ c->proc = 0;
+ }
+ release(&p->lock);
+ }
+ }
+
+
+
}
}
@@ -504,7 +579,15 @@ yield(void)
{
struct proc *p = myproc();
acquire(&p->lock);
+ p->genuine_time_slices = p->genuine_time_slices +1;
p->state = RUNNABLE;
+
+ if(p->inQ==2 && p->genuine_time_slices < TIME_LIMIT_2){
+ release(&p->lock);
+ return;
+ }
+
+
sched();
release(&p->lock);
}
@@ -681,3 +764,55 @@ procdump(void)
printf("\n");
}
}
+
+int settickets(int tickets){
+ struct proc *p = myproc();
+ acquire(&p->lock);
+ p->tickets_original = tickets;
+ p->tickets_current = tickets;
+ p->time_slices = 0;
+ release(&p->lock);
+ return 0;
+}
+
+int getpinfo(uint64 addr){
+ struct proc *mp = myproc();
+ struct proc *p;
+
+ struct pstat pstat;
+ int i=0;
+ for(p=proc; p<&proc[NPROC]; p++){
+ acquire(&p->lock);
+ pstat.pid[i]= p->pid;
+ if(p->state != UNUSED){
+ pstat.inuse[i] = 1;
+ }
+ else{
+ pstat.inuse[i] = 0;
+ }
+ pstat.tickets_original[i]= p->tickets_original;
+ pstat.tickets_current[i]= p->tickets_current;
+ pstat.time_slices[i]= p->time_slices;
+ pstat.inQ[i]=p->inQ;
+ release(&p->lock);
+ i++;
+ }
+ if(copyout(mp->pagetable, addr, (char*)&pstat, sizeof(pstat)) < 0){
+ return -1;
+ }
+ return 0;
+}
+
+int getCurTickets(){
+ struct proc *p;
+ int t = 0;
+ for(p=proc; p<&proc[NPROC]; p++){
+ acquire(&p->lock);
+ if(p->state == RUNNABLE){
+ t = t+ p->tickets_current;
+ }
+ release(&p->lock);
+
+ }
+ return t;
+}
\ No newline at end of file
diff --git a/kernel/proc.h b/kernel/proc.h
index d021857..ce3b6b3 100644
--- a/kernel/proc.h
+++ b/kernel/proc.h
@@ -104,4 +104,11 @@ struct proc {
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
+
+ int tickets_original; // Original ticket count
+ int tickets_current; // Current ticket count
+ int time_slices; // Number of time slices
+ int genuine_time_slices;
+ int inQ; // Which queue the process is currently in
+ int inuse; // Whether this slot of the process table is in use (1 or 0)
};
diff --git a/kernel/pstat.h b/kernel/pstat.h
new file mode 100644
index 0000000..67101c6
--- /dev/null
+++ b/kernel/pstat.h
@@ -0,0 +1,14 @@
+#ifndef _PSTAT_H_
+#define _PSTAT_H_
+#include "param.h"
+
+struct pstat {
+ int pid[NPROC]; // the process ID of each process
+ int inuse[NPROC]; // whether this slot of the process table is being used (1 or 0)
+ int inQ[NPROC]; // which queue the process is currently in
+ int tickets_original[NPROC]; // the number of tickets each process originally had
+ int tickets_current[NPROC]; // the number of tickets each process currently has
+ int time_slices[NPROC]; // the number of time slices each process has been scheduled
+};
+
+#endif // _PSTAT_H_
diff --git a/kernel/rand.c b/kernel/rand.c
new file mode 100644
index 0000000..a798f4b
--- /dev/null
+++ b/kernel/rand.c
@@ -0,0 +1,66 @@
+#define N 624
+#define M 397
+#define MATRIX_A 0x9908b0df
+#define UPPER_MASK 0x80000000
+#define LOWER_MASK 0x7fffffff
+
+#define TEMPERING_MASK_B 0x9d2c5680
+#define TEMPERING_MASK_C 0xefc60000
+#define TEMPERING_SHIFT_U(y) (y >> 11)
+#define TEMPERING_SHIFT_S(y) (y << 7)
+#define TEMPERING_SHIFT_T(y) (y << 15)
+#define TEMPERING_SHIFT_L(y) (y >> 18)
+#define RAND_MAX 0x7fffffff
+static unsigned long mt[N];
+static int mti=N+1;
+static int set = 0;
+void
+sgenrand(seed)
+ unsigned long seed;
+{
+ mt[0]= seed & 0xffffffff;
+ for (mti=1; mti<N; mti++)
+ mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
+}
+
+long
+genrand(int upper)
+{
+ if( set == 0 ){
+ sgenrand(4357);
+ set = 1;
+ }
+
+ unsigned long y;
+ static unsigned long mag01[2]={0x0, MATRIX_A};
+
+ if (mti >= N) {
+ int kk;
+
+ if (mti == N+1)
+ sgenrand(4357);
+
+ for (kk=0;kk<N-M;kk++) {
+ y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
+ mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
+ }
+ for (;kk<N-1;kk++) {
+ y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
+ mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
+ }
+ y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
+ mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
+
+ mti = 0;
+ }
+
+ y = mt[mti++];
+ y ^= TEMPERING_SHIFT_U(y);
+ y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
+ y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
+ y ^= TEMPERING_SHIFT_L(y);
+
+ y = y & RAND_MAX;
+ y = y % upper;
+ return y;
+}
diff --git a/kernel/syscall.c b/kernel/syscall.c
index ed65409..ce52e87 100644
--- a/kernel/syscall.c
+++ b/kernel/syscall.c
@@ -101,6 +101,8 @@ extern uint64 sys_unlink(void);
extern uint64 sys_link(void);
extern uint64 sys_mkdir(void);
extern uint64 sys_close(void);
+extern uint64 sys_settickets(void);
+extern uint64 sys_getpinfo(void);
// An array mapping syscall numbers from syscall.h
// to the function that handles the system call.
@@ -126,6 +128,8 @@ static uint64 (*syscalls[])(void) = {
[SYS_link] sys_link,
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
+[SYS_settickets] sys_settickets,
+[SYS_getpinfo] sys_getpinfo,
};
void
diff --git a/kernel/syscall.h b/kernel/syscall.h
index bc5f356..7926b7f 100644
--- a/kernel/syscall.h
+++ b/kernel/syscall.h
@@ -20,3 +20,5 @@
#define SYS_link 19
#define SYS_mkdir 20
#define SYS_close 21
+#define SYS_settickets 22
+#define SYS_getpinfo 23
diff --git a/kernel/sysproc.c b/kernel/sysproc.c
index 1de184e..53c5573 100644
--- a/kernel/sysproc.c
+++ b/kernel/sysproc.c
@@ -5,6 +5,7 @@
#include "memlayout.h"
#include "spinlock.h"
#include "proc.h"
+#include "pstat.h"
uint64
sys_exit(void)
@@ -89,3 +90,29 @@ sys_uptime(void)
release(&tickslock);
return xticks;
}
+
+uint64
+sys_settickets(void)
+{
+ int n;
+ argint(0, &n);
+ if(n<1){
+ return settickets(DEFAULT_TICKET_COUNT);
+ }
+ else{
+ return settickets(n);
+ }
+}
+
+uint64
+sys_getpinfo(void)
+{
+ uint64 p;
+ argaddr(0, &p);
+ if(p<0){
+ return -1;
+ }
+ else{
+ return getpinfo(p);
+ }
+}
diff --git a/user/dummyproc.c b/user/dummyproc.c
new file mode 100644
index 0000000..12fc518
--- /dev/null
+++ b/user/dummyproc.c
@@ -0,0 +1,23 @@
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user/user.h"
+
+int
+main(int argc,char *argv[]){
+
+
+ if( argc < 2 ){
+ fprintf(2, "USAGE: %s SYS_CALL_NUM COMMAND\n", argv[0]);
+ exit(1);
+ }
+ int tk = atoi(argv[1]);
+ if (settickets(tk) == -1) {
+ fprintf(2, "%s: execution failed\n", argv[0]);
+ exit(1);
+ }
+
+ if( fork() == 0 ){
+ while(1);
+ }
+ exit(0);
+}
\ No newline at end of file
diff --git a/user/testprocinfo.c b/user/testprocinfo.c
new file mode 100644
index 0000000..63c38e5
--- /dev/null
+++ b/user/testprocinfo.c
@@ -0,0 +1,26 @@
+
+#include "kernel/types.h"
+#include "kernel/stat.h"
+#include "user/user.h"
+#include "kernel/pstat.h"
+
+int
+main(int argc,char *argv[]){
+ struct pstat st;
+
+ if (getpinfo(&st) < 0) {
+ fprintf(2, "%s: exec failed\n", argv[0]);
+ exit(1);
+ }
+
+ printf("PID | In Use | inQ | Original Tickets | Current Tickets | Time Slices\n");
+ for(int i=0;i<NPROC;i++){
+ if( st.inuse[i] == 0 ) continue;
+ printf("%d\t%d\t%d\t\t%d\t\t\t%d\t\t%d\n",
+ st.pid[i],st.inuse[i],st.inQ[i], st.tickets_original[i],
+ st.tickets_current[i], st.time_slices[i]);
+
+
+ }
+ exit(0);
+}
\ No newline at end of file
diff --git a/user/user.h b/user/user.h
index 4d398d5..c8eb7ed 100644
--- a/user/user.h
+++ b/user/user.h
@@ -1,4 +1,5 @@
struct stat;
+struct pstat;
// system calls
int fork(void);
@@ -22,6 +23,8 @@ int getpid(void);
char* sbrk(int);
int sleep(int);
int uptime(void);
+int settickets(int);
+int getpinfo(struct pstat*);
// ulib.c
int stat(const char*, struct stat*);
diff --git a/user/usys.pl b/user/usys.pl
index 01e426e..d2435c1 100755
--- a/user/usys.pl
+++ b/user/usys.pl
@@ -36,3 +36,5 @@ entry("getpid");
entry("sbrk");
entry("sleep");
entry("uptime");
+entry("settickets");
+entry("getpinfo");