VM: memtype fix
[minix3.git] / test / testvm.c
blobdacf31a16b8a3cc8558710ccc877d9918ca0597b
1 /* testvm - service-started code that goes with test73.o
2 */
4 #include <minix/drivers.h>
5 #include <minix/chardriver.h>
6 #include <minix/ds.h>
7 #include <sys/stat.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <fcntl.h>
12 #include "testvm.h"
13 #include "common.h"
14 #include "testcache.h"
16 static char *pipefilename = NULL, *progname;
17 int pipefd = -1;
19 int memfd;
21 static char *bdata = NULL;
23 int dowriteblock(int b, int blocksize, u32_t seed, char *block)
25 int r;
27 r=vm_yield_block_get_block(VM_BLOCKID_NONE, b, bdata, blocksize);
29 if(r != OK && r != ESRCH) {
30 printf("dowriteblock: vm_yield_block_get_block get %d\n", r);
31 exit(1);
34 memcpy(bdata, block, blocksize);
36 r=vm_yield_block_get_block(b, VM_BLOCKID_NONE, bdata, blocksize);
38 if(r != OK) {
39 printf("dowriteblock: vm_yield_block_get_block yield %d\n", r);
40 exit(1);
43 return blocksize;
46 int readblock(int b, int blocksize, u32_t seed, char *block)
48 int r;
50 r=vm_yield_block_get_block(VM_BLOCKID_NONE, b, bdata, blocksize);
51 if(r == ESRCH) {
52 return OK_BLOCK_GONE;
54 if(r != OK) {
55 printf("readblock: vm_yield_block_get_block get %d\n", r);
56 exit(1);
59 memcpy(block, bdata, blocksize);
60 r=vm_yield_block_get_block(b, VM_BLOCKID_NONE, bdata, blocksize);
61 if(r != OK) {
62 printf("readblock: vm_yield_block_get_block yield %d\n", r);
63 exit(1);
66 return blocksize;
69 void testend(void) { vm_forgetblocks(); }
71 static void
72 writepipe(struct info *i)
74 if(write(pipefd, i, sizeof(*i)) != sizeof(*i)) {
75 printf("%s: pipe write failed\n", progname);
76 exit(1);
80 static int
81 testinit(void)
83 struct stat st;
84 int attempts = 0;
86 for(attempts = 0; attempts < 5 && pipefd < 0; attempts++) {
87 if(attempts > 0) sleep(1);
88 pipefd = open(pipefilename, O_WRONLY | O_NONBLOCK);
91 if(pipefd < 0) {
92 printf("%s: could not open pipe %s, errno %d\n",
93 progname, pipefilename, errno);
94 exit(1);
97 if(fstat(pipefd, &st) < 0) {
98 printf("%s: could not fstat pipe %s\n", progname, pipefilename);
99 exit(1);
102 if(!(st.st_mode & I_NAMED_PIPE)) {
103 printf("%s: file %s is not a pipe\n", progname, pipefilename);
104 exit(1);
107 return OK;
110 static int
111 sef_cb_init(int type, sef_init_info_t *UNUSED(info))
113 return OK;
116 static void
117 init(void)
119 /* SEF init */
120 sef_setcb_init_fresh(sef_cb_init);
121 sef_setcb_init_lu(sef_cb_init);
122 sef_setcb_init_restart(sef_cb_init);
124 sef_startup();
130 main(int argc, char *argv[])
132 struct info info;
133 int big;
134 u32_t totalmem, freemem, cachedmem;
136 progname = argv[0];
138 if(argc < 2) { printf("no args\n"); return 1; }
140 pipefilename=argv[1];
142 big = !!strstr(pipefilename, "big");
144 init();
146 info.result = time(NULL);
148 if(testinit() != OK) { printf("%s: testinit failed\n", progname); return 1; }
150 cachequiet(!big);
152 if(!(bdata = alloc_contig(PAGE_SIZE, 0, NULL))) {
153 printf("could not allocate block\n");
154 exit(1);
157 if(dotest(PAGE_SIZE, 10, 3)) { e(11); exit(1); }
158 if(dotest(PAGE_SIZE, 1000, 3)) { e(11); exit(1); }
159 if(dotest(PAGE_SIZE, 50000, 3)) { e(11); exit(1); }
160 if(big) {
161 getmem(&totalmem, &freemem, &cachedmem);
162 if(dotest(PAGE_SIZE, totalmem*1.5, 3)) { e(11); exit(1); }
165 info.result = 0;
167 writepipe(&info);
169 return 0;