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
|
#include <cpu.h>
//#include <drivers/uart.h>
#include <globals.h>
#include <graphics/lfb.h>
#include <lib/kmem.h>
#include <sys/core.h>
#include <sys/schedule.h>
#include <util/lock.h>
#include <util/mutex.h>
#include <util/status.h>
extern void atest(void);
void qualitative_tests(void);
void mutex_contention_helper(struct Mutex* m)
{
lock_mutex(m);
sys0(SYS_YIELD);
unlock_mutex(m);
}
static int x = 0;
static int y = 13;
#define TEST_STR_CLR " "
#define TEST_RESULT_WIDTH 15
#define TEST_COUNT 4096
#define TEST_BIN_COUNT 32
void test_entry(void)
{
x = 0;
draw_hex32(0, y-1, nextpid);
draw_string(0, y+4, "Starting tests");
unsigned long long ti, tf, dt=0,len;
unsigned int tidx = 0;
unsigned long bins[TEST_BIN_COUNT];
for (int i = 0; i < TEST_BIN_COUNT; i++) {
bins[i] = 0;
}
// Test 1: Tracing Time
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
sys0_64(SYS_TIME, &ti);
sys0_64(SYS_TIME, &tf);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 2: Yield Time
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
sys0_64(SYS_TIME, &ti);
sys0(SYS_YIELD);
sys0_64(SYS_TIME, &tf);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 3: Add Thread, Lower Priority
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
sys0_64(SYS_TIME, &ti);
add_thread(atest, 0, 3);
sys0_64(SYS_TIME, &tf);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 4: Add Thread, Higher Priority
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
sys0_64(SYS_TIME, &ti);
add_thread(atest, 0, 0);
sys0_64(SYS_TIME, &tf);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 5: Create Mutex
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
sys0_64(SYS_TIME, &ti);
struct Mutex* m = create_mutex(0);
sys0_64(SYS_TIME, &tf);
delete_mutex(m);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 6: Delete Mutex
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
struct Mutex* m = create_mutex(0);
sys0_64(SYS_TIME, &ti);
delete_mutex(m);
sys0_64(SYS_TIME, &tf);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 7: Lock Mutex
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
struct Mutex* m = create_mutex(0);
sys0_64(SYS_TIME, &ti);
lock_mutex(m);
sys0_64(SYS_TIME, &tf);
delete_mutex(m);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 7a: Lock Contended Mutex
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
struct Mutex* m = create_mutex(0);
add_thread(mutex_contention_helper, m, 2);
sys0(SYS_YIELD);
sys0_64(SYS_TIME, &ti);
lock_mutex(m);
sys0_64(SYS_TIME, &tf);
delete_mutex(m);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 8: Unlock Mutex
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
struct Mutex* m = create_mutex(0);
lock_mutex(m);
sys0_64(SYS_TIME, &ti);
unlock_mutex(m);
sys0_64(SYS_TIME, &tf);
delete_mutex(m);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Semaphore
static unsigned long sem = 0;
// Test 9: Semaphore Decrease
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
sem = 1;
sys0_64(SYS_TIME, &ti);
sys1(SYS_SEMAPHORE_P, &sem);
sys0_64(SYS_TIME, &tf);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 10: Semaphore Increase
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
sem = 0;
sys0_64(SYS_TIME, &ti);
sys1(SYS_SEMAPHORE_V, &sem);
sys0_64(SYS_TIME, &tf);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// Test 10a: Semaphore Increase - No Schedule
dt = 0;
for(int i = 0; i < TEST_COUNT; i++) {
sem = 1;
sys0_64(SYS_TIME, &ti);
sys1(SYS_SEMAPHORE_V, &sem);
sys0_64(SYS_TIME, &tf);
dt += tf - ti;
if ((tf-ti) < TEST_BIN_COUNT)
bins[(tf-ti)]++;
}
for (int i = 0; i < TEST_BIN_COUNT; i++) {
draw_hex32(tidx, y+6+i, i);
draw_string(tidx+9, y+6+i, TEST_STR_CLR);
draw_u10(tidx+9, y+6+i, bins[i]);
bins[i] = 0;
}
draw_string(tidx, y+5, " ");
len = draw_u10(tidx, y+5, dt/TEST_COUNT);
draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
tidx += TEST_RESULT_WIDTH;
draw_hex32(0, y-1, nextpid);
// // Test 7: Tick Latency
//#define DELAY_TIME 512000
// unsigned long center = 0;
// sys0_64(SYS_TIME, &ti);
// delay(DELAY_TIME);
// sys0_64(SYS_TIME, &tf);
// center = (tf - ti - 10);
// if (10 > (tf-ti))
// center = 0;
// dt = 0;
// unsigned long j = 0;
// for(int i = 0; i < TEST_COUNT; i++) {
// sys0_64(SYS_TIME, &ti);
// delay(DELAY_TIME);
// sys0_64(SYS_TIME, &tf);
// dt += tf - ti;
// if ((tf-ti-center) < TEST_BIN_COUNT)
// bins[(tf-ti)-center]++;
// else
// j++;
// }
// for (int i = 0; i < TEST_BIN_COUNT; i++) {
// draw_hex32(tidx, y+6+i, i);
// draw_string(tidx+9, y+6+i, TEST_STR_CLR);
// draw_u10(tidx+9, y+6+i, bins[i]);
// bins[i] = 0;
// }
// draw_hex32(tidx, y+4, j);
// draw_string(tidx, y+5, " ");
// len = draw_u10(tidx, y+5, dt/TEST_COUNT);
// draw_u10(tidx+len+1, y+5, dt%TEST_COUNT);
// tidx += TEST_RESULT_WIDTH;
// draw_hex32(0, y-1, nextpid);
add_thread(qualitative_tests, 0, 4);
}
//static struct Mutex testm = {.addr = 0, .pid = 0};
static struct Lock testm = {.pid = 0};
void priority_inversion_test1(void);
void priority_inversion_test2(void);
void priority_inversion_test3(void);
void priority_inversion_test4(void);
void priority_inversion_test1(void)
{
draw_cletter(x++, y+2, 'S', 0xFF0000);
// Try Lock
draw_cletter(x++, y+2, 'T', 0xFF0000);
lock(&testm);
// Lock Acquired
draw_cletter(x++, y+2, 'L', 0xFF0000);
// Add Thread to Assist with Priority Inversion
// Check
// - Show that this thread gets temporarily
// promoted
add_thread(priority_inversion_test3, 0, 2);
// Unlock
draw_cletter(x++, y+2, 'U', 0xFF0000);
unlock(&testm);
draw_cletter(x++, y+2, 'F', 0xFF0000);
}
void priority_inversion_test2(void)
{
draw_cletter(x++, y+0, 'S', 0x0000FF);
// Add Thread to Assist with Priority Inversion
// Check
// - Show that Thread 1 is Prepended To Queue
add_thread(priority_inversion_test4, 0, 3);
// Try Lock
draw_cletter(x++, y+0, 'T', 0x0000FF);
lock(&testm);
// Lock Acquired
draw_cletter(x++, y+0, 'L', 0x0000FF);
// Unlock
draw_cletter(x++, y+0, 'U', 0x0000FF);
unlock(&testm);
draw_cletter(x++, y+0, 'F', 0x0000FF);
}
void priority_inversion_test3(void)
{
draw_cletter(x++, y+1, 'S', 0x00FF00);
// Add thread to Assist with Priority Inversion
// Check
// - Add high priority thread that will try
// to lock the mutex
add_thread(priority_inversion_test2, 0, 1);
draw_cletter(x++, y+1, 'F', 0x00FF00);
}
void priority_inversion_test4(void)
{
draw_cletter(x++, y+2, 'S', 0xAFAF00);
// Do nothing,
// just show that this is executed last
draw_cletter(x++, y+2, 'F', 0xAFAF00);
}
static unsigned long test_semaphore = 0;
void semaphore_test1(void)
{
draw_cletter(x++, y+1, ' ', 0xFF0000);
draw_cletter(x++, y+1, 'S', 0xFF0000);
// Try to decrement semaphore
draw_cletter(x++, y+1, 'T', 0xFF0000);
sys1(SYS_SEMAPHORE_P, &test_semaphore);
// Semaphore decremented
draw_cletter(x++, y+1, 'P', 0xFF0000);
draw_cletter(x++, y+1, 'F', 0xFF0000);
}
void semaphore_test2(void)
{
draw_cletter(x++, y+2, 'S', 0xFF00);
// Increment semaphore
draw_cletter(x++, y+2, 'V', 0xFF00);
sys1(SYS_SEMAPHORE_V, &test_semaphore);
draw_cletter(x++, y+2, 'F', 0xFF00);
}
static struct Mutex* dead1 = 0;
static struct Mutex* dead2 = 0;
void deadlock_test2(void)
{
draw_cletter(x++, y+1, 'S', 0xFF0000);
// Try Lock 1
draw_cletter(x++, y+1, 'T', 0xFF0000);
lock_mutex(dead1);
// Lock 1 Acquired
draw_cletter(x++, y+1, 'L', 0xFF0000);
// Try Lock 2
draw_cletter(x++, y+1, 't', 0xFF0000);
lock_mutex(dead2);
// Lock 2 Acquired
draw_cletter(x++, y+1, 'l', 0xFF0000);
// Unlock Locks
draw_cletter(x++, y+1, 'u', 0xFF0000);
unlock_mutex(dead2);
draw_cletter(x++, y+1, 'U', 0xFF0000);
unlock_mutex(dead1);
draw_cletter(x++, y+1, 'F', 0xFF0000);
}
void deadlock_test1(void)
{
draw_cletter(x++, y+2, ' ', 0xFF00);
draw_cletter(x++, y+2, 'S', 0xFF00);
dead1 = create_mutex((void*)0xDEADBEEF);
dead2 = create_mutex((void*)0x12345678);
// Try Lock 2
draw_cletter(x++, y+2, 't', 0xFF00);
lock_mutex(dead2);
// Lock 2 Acquired
draw_cletter(x++, y+2, 'l', 0xFF00);
// Create Higher priority thread to
// check deadlock condition
draw_cletter(x++, y+2, 'A', 0xFF00);
add_thread(deadlock_test2, 0, 4);
// Try Lock 1 - This would deadlock
// if no mechanism is in place to
// prevent it
draw_cletter(x++, y+2, 'T', 0xFF00);
lock_mutex(dead1);
// Lock 1 Acquired - Deadlock condition
// properly handled
draw_cletter(x++, y+2, 'L', 0xFF00);
// Unlock Locks
draw_cletter(x++, y+2, 'u', 0xFF00);
unlock_mutex(dead2);
draw_cletter(x++, y+2, 'U', 0xFF00);
unlock_mutex(dead1);
delete_mutex(dead1);
delete_mutex(dead2);
draw_cletter(x++, y+2, 'F', 0xFF00);
}
void qualitative_tests(void)
{
draw_string(0, y+0, " ");
draw_string(0, y+1, " ");
draw_string(0, y+2, " ");
draw_string(0, y+3, " ");
x = 0;
add_thread(priority_inversion_test1, 0, 3);
add_thread(deadlock_test1, 0, 5);
add_thread(semaphore_test1, 0, 6);
add_thread(semaphore_test2, 0, 7);
add_thread(time_status, 0, 8);
}
|