amt_wanted_min added to resource requests
[akaros.git] / user / apps / roslib / mproctests.c
1 #include <lib.h>
2 #include <ros/mman.h>
3 #include <ros/resource.h>
4 #include <syswrapper.h>
5 #include <stdio.h>
6
7 // ghetto udelay, put in a lib somewhere and export the tsc freq
8 #include <arch/arch.h>
9 void udelay(uint64_t usec, uint64_t tsc_freq)
10 {
11         uint64_t start, end, now;
12
13         start = read_tsc();
14     end = start + (tsc_freq * usec) / 1000000;
15         if (end == 0) cprintf("This is terribly wrong \n");
16         do {
17         cpu_relax();
18         now = read_tsc();
19         } while (now < end || (now > start && end < start));
20         return;
21 }
22
23 #define TEST_MMAP                                        1
24 #define TEST_ONE_CORE                            2
25 #define TEST_ASK_FOR_TOO_MANY_CORES      3
26 #define TEST_INCREMENTAL_CHANGES         4
27 #define TEST_YIELD_OUT_OF_ORDER          5
28 #define TEST_YIELD_0_OUT_OF_ORDER        6
29 #define TEST_YIELD_ALL               7
30 #define TEST_SWITCH_TO_RUNNABLE_S        8
31 #define TEST_CRAZY_YIELDS                        9
32 #define TEST_CONCURRENT_SYSCALLS        10
33
34 int test = TEST_SWITCH_TO_RUNNABLE_S;
35
36 static void global_tests(uint32_t vcoreid);
37
38 int main(int argc, char** argv)
39 {
40         uint32_t vcoreid;
41         error_t retval;
42         prepare_for_multi_mode();
43
44         if ((vcoreid = newcore())) {
45                 cprintf("Should never see me! (from vcore %d)\n", vcoreid);
46         } else { // core 0
47                 cprintf("Hello from else vcore 0\n");
48                 cprintf("Multi-Goodbye, world, from PID: %d!\n", sys_getpid());
49                 switch (test) {
50                         case TEST_MMAP:
51                                 cprintf("Testing MMAP\n");
52                                 void *CT(8*PGSIZE) addr;
53                                 addr = sys_mmap((void*SNT)USTACKTOP - 20*PGSIZE, 8*PGSIZE, 3,
54                                                 MAP_FIXED, 0, 0);
55                                 cprintf("got addr = 0x%08x\n", addr);
56                                 *(int*)addr = 0xdeadbeef;
57                                 *(int*)(addr + 3*PGSIZE) = 0xcafebabe;
58                                 // these should work
59                                 cprintf("reading addr: 0x%08x\n", *(int*)addr);
60                                 cprintf("reading addr+3pg: 0x%08x\n", *(int*)(addr + 3*PGSIZE));
61                                 // this should fault
62                                 cprintf("Should page fault and die now.\n");
63                                 { TRUSTEDBLOCK
64                                 *(int*)(addr - 3*PGSIZE) = 0xdeadbeef;
65                                 }
66                                 cprintf("Should not see me!!!!!!!!!!!!!!!!!!\n");
67                                 while(1);
68                         case TEST_ONE_CORE:
69                                 retval = sys_resource_req(RES_CORES, 1, 1, 0);
70                                 cprintf("One core test's core0's retval: %d\n", retval);
71                                 cprintf("Check to see it's on a worker core.\n");
72                                 while(1);
73                         case TEST_ASK_FOR_TOO_MANY_CORES:
74                                 retval = sys_resource_req(RES_CORES, 12, 1, 0);
75                                 cprintf("Asked for too many, retval: %d\n", retval);
76                                 return 0;
77                         case TEST_INCREMENTAL_CHANGES:
78                                 retval = sys_resource_req(RES_CORES, 4, 1, 0);
79                                 break;
80                         default:
81                                 retval = sys_resource_req(RES_CORES, 7, 1, 0);
82                 }
83                 cprintf("Should see me if you want to relocate core0's context "
84                         "when moving from RUNNING_S\n");
85         }
86
87         // vcore0 only below here
88         switch (test) {
89                 case TEST_YIELD_OUT_OF_ORDER:
90                         udelay(10000000, 1995014570);
91                         cprintf("Core 2 should have yielded, asking for another\n");
92                         retval = sys_resource_req(RES_CORES, 7, 1, 0);
93                         break;
94                 case TEST_YIELD_0_OUT_OF_ORDER:
95                         udelay(5000000, 1995014570);
96                         cprintf("Core %d yielding\n", vcoreid);
97                         yield();
98                         cprintf("Core 0 came back where it left off in RUNNING_M!!!\n");
99                         break;
100         }
101         global_tests(vcoreid);
102         cprintf("Vcore %d Done!\n", vcoreid);
103         while (1);
104         return 0;
105 }
106
107 void hart_entry(void)
108 {
109         uint32_t vcoreid;
110         static int first_time = 1; // used by vcore2
111         error_t retval;
112
113         vcoreid = newcore();
114         cprintf("Hello from hart_entry in vcore %d\n", vcoreid);
115
116         if ((vcoreid == 2) && first_time) {
117                 first_time = 0;
118                 switch (test) {
119                         case TEST_INCREMENTAL_CHANGES:
120                                 // Testing asking for less than we already have
121                                 udelay(1000000, 1995014570); // KVM's freq.  Whatever.
122                                 cprintf("Asking for too few:\n");
123                                 retval = sys_resource_req(RES_CORES, 2, 1, 0);
124                                 cprintf("Should be -EINVAL(7): %d\n", retval);
125                                 // Testing getting more while running
126                                 cprintf("Asking for more while running:\n");
127                                 udelay(1000000, 1995014570);
128                                 retval = sys_resource_req(RES_CORES, 7, 1, 0);
129                                 cprintf("core2's retval: %d\n", retval);
130                                 break;
131                         case TEST_YIELD_OUT_OF_ORDER:
132                                 cprintf("Core %d yielding\n", vcoreid);
133                                 yield();
134                                 break;
135                         case TEST_YIELD_0_OUT_OF_ORDER:
136                                 udelay(7500000, 1995014570);
137                                 cprintf("Core 0 should have yielded, asking for another\n");
138                                 retval = sys_resource_req(RES_CORES, 7, 1, 0);
139                 }
140         }
141         global_tests(vcoreid);
142         cprintf("Vcore %d Done!\n", vcoreid);
143 }
144
145 static void global_tests(uint32_t vcoreid)
146 {
147         error_t retval;
148         switch (test) {
149                 case TEST_YIELD_ALL:
150                         cprintf("Core %d yielding\n", vcoreid);
151                         yield();
152                         // should be RUNNABLE_M now, amt_wanted == 1
153                         while(1);
154                 case TEST_SWITCH_TO_RUNNABLE_S:
155                         if (vcoreid == 2) {
156                                 cprintf("Core %d trying to request 0/ switch to _S\n", vcoreid);
157                                 retval = sys_resource_req(RES_CORES, 0, 0, 0);
158                                 // will only see this if we are scheduled()
159                                 cprintf("Core %d back up! (retval:%d)\n", vcoreid, retval);
160                                 cprintf("And exiting\n");
161                                 exit();
162                         } 
163                         while(1);
164                 case TEST_CRAZY_YIELDS:
165                         udelay(300000*vcoreid, 1995014570);
166                         sys_resource_req(RES_CORES, 7, 1, 0);
167                         yield();
168                         cprintf("should  never see me, unless you slip into *_S\n");
169                         break;
170                 case TEST_CONCURRENT_SYSCALLS:
171                         for (int i = 0; i < 10; i++) {
172                                 for (int j = 0; j < 100; j++)
173                                         sys_null();
174                                 cprintf("Hello from vcore %d, iteration %d\n", vcoreid, i);
175                         }
176                         break;
177         }
178 }