-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage_policy_test.c
110 lines (91 loc) · 2.75 KB
/
page_policy_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
#include <xinu.h>
#define PAGESIZE 4096
#define PAGE_ALLOCATION 100
/* NOTE: This does not check if paging is enabled or not, you
should check that before starting the tests.
*/
/* Set to 1 to test page replacement
* Set to 0 to check page fault handling is correct
*/
#define PAGE_REPLACEMENT 1
// Return a deterministic value per addr for testing.
uint32 get_test_value(uint32 *addr) {
static uint32 v1 = 0x12345678;
static uint32 v2 = 0xdeadbeef;
return (uint32)addr + v1 + ((uint32)addr * v2);
}
static void do_policy_test(void) {
uint32 npages = PAGE_ALLOCATION - 1;
uint32 nbytes = npages * PAGESIZE;
kprintf("Running Page Replacement Policy Test, with NFRAMES = %d\n", NFRAMES);
char *mem = vgetmem(nbytes);
if (mem == (char*) SYSERR) {
panic("Page Replacement Policy Test failed\n");
return;
}
// Write data
for (uint32 i = 0; i<npages; i++) {
uint32 *p = (uint32*)(mem + (i * PAGESIZE));
// kprintf("Write Iteration [%3d] at 0x%08x\n", i, p);
for (uint32 j=0; j<PAGESIZE; j=j+4) {
uint32 v = get_test_value(p);
*p++ = v;
}
sleepms(20); // to make it slower
}
// Check the data was truly written
for (uint32 i = 0; i<npages; i++) {
uint32 *p = (uint32*)(mem + (i * PAGESIZE));
kprintf("Check Iteration [%3d] at 0x%08x\n", i, p);
for (uint32 j=0; j<PAGESIZE; j=j+4) {
uint32 v = get_test_value(p);
ASSERT(*p++ == v);
}
sleepms(20); // to make it slower
}
if (vfreemem(mem, nbytes) == SYSERR) {
panic("Policy Test: vfreemem() failed.\n");
} else {
#if PAGE_REPLACEMENT == 1
kprintf("\nPage Replacement Policy Test Finished.\n");
#else
kprintf("\nPage Fault Handling Test Finished\n");
#endif
kprintf("Here NFRAMES = %d\n", NFRAMES);
}
}
/**
* Just iterate through a lot of pages, and check if the output satisfies the policy.
* Based on the hooks: hook_pfault and hook_pswap_out, you can ascertain if the test
* passes or not. The hooks are supposed to throw a panic if the policy is not being
* followed. (NFRAMES should be <= 50 for page replacement testing)
*/
void page_policy_test(void) {
recvclr();
#if PAGE_REPLACEMENT == 1
kprintf("Starting Policy (FIFO) Testing Test\n");
if (NFRAMES > 50) {
kprintf("Test Failed. Please set NFRAMES to <= 50 and try again.\n");
return;
}
#else
kprintf("Starting Page Fault Handling Test\n");
if (NFRAMES < 200) {
kprintf("Test Failed. Please set NFRAMES to >= 200 and try again\n");
return;
}
#endif
pid32 p = vcreate(do_policy_test, INITSTK, PAGE_ALLOCATION,
INITPRIO, "page rep", 0, NULL);
resume(p);
while (1) {
if(proctab[p].prstate == PR_FREE) {
break;
}
else {
sleepms(100);
}
}
kprintf("\n\nTest Passed.\n\n");
return;
}