-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.c
586 lines (481 loc) · 14.3 KB
/
test.c
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* pa2 tester
* bmilton
* 25 jan 2016
* latest update: 26 jan 2016
*/
#include "test.h"
int totalFailCounter = 0;
int verbose = 0;
int should_run_havoc = 0;
int inSlackRange(int expected, int actual) {
if(actual >= expected) return 1;
if(!SLACK) return 0;
int minimumAllowed = expected * 0.9;
return(actual >= minimumAllowed);
}
int get_next_sched() {
if (LOGIC_IN_HANDLETIMERINTR) {
HandleTimerIntr();
}
return SchedProc();
}
int test_arbitrary(int numprocs) {
int failCounter = 0;
SetSchedPolicy(ARBITRARY);
InitSched();
int proc;
for (proc = 1; proc <= numprocs; proc++)
StartingProc(proc);
for (proc = 0; proc < 500; proc++) {
int pid = get_next_sched();
if (!pid) {
Printf("ARBITRARY ERR: Received bad proces %d\n", pid);
failCounter++;
}
}
for (proc = 1; proc <= numprocs; proc++)
EndingProc(proc);
totalFailCounter += failCounter;
return failCounter;
}
int test_fifo_normal(int numprocs) {
int failCounter = 0;
SetSchedPolicy(FIFO);
InitSched();
int proc;
for (proc = 1; proc <= numprocs; proc++) {
StartingProc(proc);
}
for (proc = 1; proc <= numprocs; proc++) {
int next = get_next_sched();
if (next != proc) {
Printf("FIFO ERR: Received process %d but expected %d\n", next, proc);
failCounter++;
}
EndingProc(proc);
}
/* check if all process are exited */
if (get_next_sched()) {
Printf("FIFO ERR: Not all processes have exited\n");
failCounter++;
}
totalFailCounter += failCounter;
return failCounter;
}
int test_lifo_normal(int numprocs) {
int failCounter = 0;
SetSchedPolicy(LIFO);
InitSched();
int proc;
for (proc = 1; proc <= numprocs; proc++) {
StartingProc(proc);
}
for (proc = numprocs; proc > 0; proc--) {
int next = get_next_sched();
if (next != proc) {
Printf("LIFO ERR: Received process %d but expected %d\n", next, proc);
failCounter++;
}
EndingProc(proc);
}
/* check if all process are exited */
if (get_next_sched()) {
Printf("LIFO ERR: Not all processes have exited\n");
failCounter++;
}
totalFailCounter += failCounter;
return failCounter;
}
int test_rr_normal(int numprocs) {
int failCounter = 0;
SetSchedPolicy(ROUNDROBIN);
InitSched();
for (int i = 1; i <= numprocs; i++) {
StartingProc(i);
}
int* decisions = calloc(numprocs, 4);
for(int t = 0; t < numprocs * 5; t++) {
// have we made at least numprocs decisions yet?
if(t >= numprocs){
int* counts = calloc(numprocs + 1, 4);
for(int i = 0; i < numprocs; i++) {
counts[decisions[i]]++;
}
for(int i = 1; i < numprocs; i++) {
if(counts[i] != 1){
Printf("ROUND ROBIN ERR: Process %d received %d ticks in one round, expecting 1\n",
i, counts[i]);
failCounter++;
}
}
free(counts);
}
decisions[t % numprocs] = get_next_sched();
}
free(decisions);
for (int i = 1; i <= numprocs; i++) {
EndingProc(i);
}
/* check if all process are exited */
if (get_next_sched()) {
Printf("ROUND ROBIN ERR: Not all processes have exited\n");
failCounter++;
}
totalFailCounter += failCounter;
return failCounter;
}
int test_rr_middle_exit() {
// this test attempts to break a 'next pointer' implementation.
// it only runs one cycle through
int failCounter = 0;
SetSchedPolicy(ROUNDROBIN);
InitSched();
int counts[6];
for (int i = 1; i <= 5; i++) {
StartingProc(i);
counts[i] = 0;
}
// Run each once as in standard RR
for (int i = 1; i <= 5; i++) {
counts[get_next_sched()]++;
}
// Ensure that each was run once
for (int i = 1; i <= 5; i++) {
if (counts[i] != 1) {
Printf("ROUND ROBIN2 ERR: Process %d received %d ticks in one round, expected 1\n",
i, counts[i]);
failCounter++;
}
counts[i] = 0;
}
// End a process prematurely
EndingProc(3);
// Ensure one process ran twice, process 3 didnt run, everyone else ran once
for (int i = 1; i <= 5; i++) {
counts[get_next_sched()]++;
}
int twiceProcessRan = 0;
for (int i = 1; i <= 5; i++) {
if (i == 3) {
if (counts[i] != 0) {
Printf("ROUND ROBIN2 ERR: Process %d was ended but RR still selected it!\n",
i);
failCounter++;
}
continue;
}
if (counts[i] == 2) {
if (twiceProcessRan) {
Printf("ROUND ROBIN2 ERR: Process %d got 2 but expected 1 (6 ticks occured, a single process already took 2)\n", i);
failCounter++;
}
else
twiceProcessRan = 1;
}
else if (counts[i] != 1) {
Printf("ROUND ROBIN2 ERR: Process %d got %d but expected 1\n", i, counts[i]);
failCounter++;
}
}
for (int i = 1; i <= 5; i++) {
if (i == 3)
continue;
EndingProc(i);
}
totalFailCounter += failCounter;
return failCounter;
}
int test_proportional_normal(int numprocs) {
int failCounter = 0;
SetSchedPolicy(PROPORTIONAL);
InitSched();
int i, j;
int proportions[numprocs+1];
int counts[numprocs+1];
for (i = 1; i <= numprocs; i++) {
StartingProc(i);
proportions[i] = (rand() % (100 / numprocs)) + 1;
counts[i] = 0;
}
for (i = 1; i <= numprocs; i++) {
if (MyRequestCPUrate(i, proportions[i]) == -1) {
failCounter++;
Printf("PROPORTIONAL ERR: Process requested %d%% CPU and MyRequestCPUrate wrongly returned -1\n",
proportions[i]);
}
}
for (i = 0; i < 100; i++) {
counts[get_next_sched()]++;
}
for (i = 1; i <= numprocs; i++) {
if (!inSlackRange(proportions[i], counts[i]) &&
counts[i] < proportions[i]) {
Printf("PROPORTIONAL ERR: %d requested %d%% but received %d\n", i,
proportions[i], counts[i]);
failCounter++;
}
}
for (i = 1; i <= numprocs; i++)
EndingProc(i);
/* check if all process are exited */
if (get_next_sched()) {
Printf("PROPORTIONAL ERR: Not all processes have exited\n");
failCounter++;
}
totalFailCounter += failCounter;
return failCounter;
}
int test_proportional_hog(int numprocs) {
int failCounter = 0;
SetSchedPolicy(PROPORTIONAL);
InitSched();
int i, iter;
int counts[numprocs+1];
for (i = 1; i <= numprocs; i++) {
StartingProc(i);
counts[i] = 0;
}
if (MyRequestCPUrate(1, 100) == -1) {
Printf("PROPORTIONAL2 ERR: A single process requested 100%% CPU and MyRequestCPURate returned -1\n");
failCounter++;
}
for (i = 2; i <= numprocs; i++) {
if (MyRequestCPUrate(i, 20) != -1) {
Printf("PROPORTIONAL2 ERR: Process %d requested unavailable space and MyRequestCPUrate returned 0\n",
i);
failCounter++;
}
}
for (iter = 0; iter < 100; iter++) {
counts[get_next_sched()]++;
}
if (counts[1] < (100 - (numprocs - 1))) {
Printf("PROPORTIONAL2 ERR: Process 1 should have received %d%% CPU time but got %d%%\n",
(100 - (numprocs - 1)), counts[1]);
failCounter++;
}
for (i = 2; i <= numprocs; i++) {
if (counts[i] > 0) {
Printf("PROPORTIONAL2 ERR: Process %d shouldn't have received >0%% CPU time (Received %d%%) "
"since process 1 requested 100%%. \n",
i, counts[i]);
failCounter++;
}
counts[i] = 0;
}
EndingProc(1);
for (iter = 0; iter < 100; iter++) {
counts[get_next_sched()]++;
}
for (i = 2; i <= numprocs; i++) {
if (!inSlackRange(100 / (numprocs-1), counts[i])) {
Printf("PROPORTIONAL2 ERR: Process %d was expected to receive %d%% CPU (RR after no requests"
" for CPU were made), but received %d%%\n", i, (100/(numprocs-1)), counts[i]);
failCounter++;
}
EndingProc(i);
}
if (get_next_sched()) {
Printf("PROPORTIONAL2 ERR: Not all processes have exited\n");
failCounter++;
}
totalFailCounter += failCounter;
return failCounter;
}
int test_proportional_huge(int numprocs) {
int failCounter = 0;
SetSchedPolicy(PROPORTIONAL);
InitSched();
int i, iter;
int counts[numprocs+1];
for (i = 1; i <= numprocs; i++) {
StartingProc(i);
counts[i] = 0;
}
if (MyRequestCPUrate(1, 100) == -1) {
Printf("PROPORTIONAL3 ERR: MyRequestCPUrate returned -1 when CPU was available\n");
failCounter++;
}
for (iter = 0; iter < 50000; iter++) {
counts[get_next_sched()]++;
}
if (counts[1] != 50000) {
Printf("PROPORTIONAL3 ERR: Process 1 should have received 50000 ticks but got %d\n",
counts[1]);
failCounter++;
}
for (i = 2; i <= numprocs; i++) {
if (counts[i]) {
Printf("PROPORTIONAL3 ERR: Process %d received %d ticks but should have received none "
"since process 1 requested 100%%\n", i, counts[i]);
failCounter++;
}
}
for (i = 1; i <= numprocs; i++)
EndingProc(i);
if (get_next_sched()) {
Printf("PROPORTIONAL3 ERR: Not all processes have exited\n");
failCounter++;
}
totalFailCounter += failCounter;
return failCounter;
}
/*
* A simple test case to ensure that any processes with no CPU allocation
* don't take too much CPU, even if there is less than 1% to go around. Also
* ensures that 1% is appropriately split among processes with no CPU
* allocation.
*/
int test_proportional_split_amongst_procs(int numprocs) {
int failCounter = 0;
SetSchedPolicy(PROPORTIONAL);
InitSched();
int i, iter;
int counts[numprocs+1];
int numRecvd = 0;
if (verbose)
Printf("start PROPORTIONALSPLIT with %d procs\n", numprocs);
for (i = 1; i <= numprocs; i++) {
StartingProc(i);
counts[i] = 0;
}
if (MyRequestCPUrate(1, 99) == -1) {
Printf("PROPORTIONALSPLIT ERR: MyRequestCPUrate returned -1 when CPU was available\n");
failCounter++;
}
for (iter = 0; iter < 500; iter++) {
counts[get_next_sched()]++;
}
if (!inSlackRange(495, counts[1])) {
Printf("PROPORTIONALSPLIT ERR: Process 1 should have received at least %d CPU ticks but got %d\n",
(int) (495 * 0.9), counts[1]);
failCounter++;
}
for (i = 1; i <= numprocs; i++) {
if (verbose)
Printf("proc %d recvd %d ticks\n", i, counts[i]);
if (i > 1 && counts[i] >= 1)
numRecvd++;
}
if (numprocs >= 6 && numRecvd < 5) {
Printf("PROPORTIONALSPLIT ERR:"
" At >5 procs should have received a cpu tick"
" since process 1 requested 99%%\n");
failCounter++;
}
for (i = 1; i <= numprocs; i++)
EndingProc(i);
if (get_next_sched()) {
Printf("PROPORTIONALSPLIT ERR: Not all processes have exited\n");
failCounter++;
}
totalFailCounter += failCounter;
return failCounter;
}
/*
* A test case to ensure one process flooding with requests doesn't allow it
* to hog all CPU.
*/
int test_proportional_reqhog(int numprocs) {
int failCounter = 0;
SetSchedPolicy(PROPORTIONAL);
InitSched();
int i, iter;
int counts[numprocs+1];
int unallocCount = 0;
if (verbose)
Printf("start PROPORTIONALREQHOG with %d procs\n", numprocs);
for (i = 1; i <= numprocs; i++) {
StartingProc(i);
counts[i] = 0;
}
for (iter = 0; iter < 500; iter++) {
if (MyRequestCPUrate(1, 90) == -1) {
Printf("PROPORTIONALREQHOG ERR: MyRequestCPUrate returned -1 when CPU was available\n");
failCounter++;
}
}
for (iter = 0; iter < 500; iter++) {
counts[get_next_sched()]++;
}
if (!inSlackRange(450, counts[1])) {
Printf("PROPORTIONALREQHOG ERR: Process 1 should have received at least %d CPU ticks but got %d\n",
(int) (450 * 0.9), counts[1]);
failCounter++;
}
for (i = 1; i <= numprocs; i++) {
if (verbose)
Printf("proc %d recvd %d ticks\n", i, counts[i]);
if (i > 1)
unallocCount += counts[i];
}
if (numprocs > 1 && !inSlackRange(50, unallocCount)) {
Printf("PROPORTIONALREQHOG ERR: Processes which allocated no CPU should "
"have received at least %d CPU ticks total but got %d\n",
(int) (50 * 0.9), unallocCount);
failCounter++;
}
for (i = 1; i <= numprocs; i++)
EndingProc(i);
if (get_next_sched()) {
Printf("PROPORTIONALREQHOG ERR: Not all processes have exited\n");
failCounter++;
}
totalFailCounter += failCounter;
return failCounter;
}
int test(int (*testerFunction) (int)) {
int i, failures;
failures = 0;
for (i = 1; i <= MAXPROCS; i++) {
failures += testerFunction(i);
}
return failures;
}
void parseCommandLineArg(int argc, char** argv){
for (int i = 1; i < argc; i++){
if (!strcmp(argv[i], "--havoc"))
should_run_havoc= 1;
else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--verbose"))
verbose = 1;
}
}
void Main(int argc, char** argv) {
parseCommandLineArg(argc, argv);
srand(120 * 0xDEAD);
Printf("%d arbitrary failures\n", test(&test_arbitrary));
Printf("%d fifo failures\n", test(&test_fifo_normal));
Printf("%d lifo failures\n", test(&test_lifo_normal));
Printf("%d roundrobin failures\n", test(&test_rr_normal));
Printf("%d roundrobin2 failures\n", test_rr_middle_exit());
Printf("%d proportional failures\n", test(&test_proportional_normal));
Printf("%d proportional2 failures\n", test(&test_proportional_hog));
Printf("%d proportional3 failures\n", test(&test_proportional_huge));
Printf("%d proportionalSPLIT failures\n", test(&test_proportional_split_amongst_procs));
Printf("%d proportionalREQHOG failures\n", test(&test_proportional_reqhog));
if(should_run_havoc){
int havoc_fails;
// +-------------------- length of test, ticks
// | +-------------- 1/P(start new process?)
// | | +----------- 1/P(end random process?)
// | | | +------- 1/P(process storm?)
// | | | | +--- 1/P(request new priority?)
// test_havoc(100000, 4, 4, 500, 0);
// stress implementation of equal distribution
havoc_fails += test_havoc(1000000, 10, 10, 500, 0);
// stress choice of new pass value
havoc_fails += test_havoc(1000000, 4, 4, 500, 4);
havoc_fails += test_havoc(1000000, 0, 5, 100, 10);
// test stability over a long period
havoc_fails += test_havoc(100000, 0, 0, 2, 0);
havoc_fails += test_havoc(100000, 0, 0, 2, 10000);
// old default
havoc_fails += test_havoc(1000000, 1100, 1000, 50000, 500);
totalFailCounter += havoc_fails;
Printf("%d havoc failures\n", havoc_fails);
}
Printf("%d Failures\n", totalFailCounter);
if (totalFailCounter == 0)
Printf("ALL TESTS PASSED!");
}