xref: /minix3/minix/tests/testvm.c (revision e321f6558257ab50a8b750451c96d1a3ca19e300)
1433d6423SLionel Sambuc /* testvm - service-started code that goes with test73.o
2433d6423SLionel Sambuc  */
3433d6423SLionel Sambuc 
4433d6423SLionel Sambuc #include <minix/drivers.h>
5433d6423SLionel Sambuc #include <minix/chardriver.h>
6433d6423SLionel Sambuc #include <minix/ds.h>
7433d6423SLionel Sambuc #include <sys/stat.h>
8433d6423SLionel Sambuc #include <sys/mman.h>
9433d6423SLionel Sambuc #include <stdio.h>
10433d6423SLionel Sambuc #include <stdlib.h>
11433d6423SLionel Sambuc #include <fcntl.h>
12433d6423SLionel Sambuc 
13433d6423SLionel Sambuc #include "testvm.h"
14433d6423SLionel Sambuc #include "common.h"
15433d6423SLionel Sambuc #include "testcache.h"
16433d6423SLionel Sambuc 
17433d6423SLionel Sambuc #define MYMAJOR 40      /* doesn't really matter, shouldn't be NO_DEV though */
18433d6423SLionel Sambuc #define MYDEV   makedev(MYMAJOR, 1)
19433d6423SLionel Sambuc 
20433d6423SLionel Sambuc static char *pipefilename = NULL, *progname;
21433d6423SLionel Sambuc int pipefd = -1;
22433d6423SLionel Sambuc 
23433d6423SLionel Sambuc int memfd;
24433d6423SLionel Sambuc 
25433d6423SLionel Sambuc static char *bdata = NULL;
26433d6423SLionel Sambuc 
dowriteblock(int b,int blocksize,u32_t seed,char * block)27433d6423SLionel Sambuc int dowriteblock(int b, int blocksize, u32_t seed, char *block)
28433d6423SLionel Sambuc {
29433d6423SLionel Sambuc 	int r;
30433d6423SLionel Sambuc 	char *bdata;
31433d6423SLionel Sambuc 	int mustset = 0;
32433d6423SLionel Sambuc 	u64_t dev_off = (u64_t) b * blocksize;
33433d6423SLionel Sambuc 
34433d6423SLionel Sambuc 	if((bdata = vm_map_cacheblock(MYDEV, dev_off,
35433d6423SLionel Sambuc 		VMC_NO_INODE, 0, NULL, blocksize)) == MAP_FAILED) {
36433d6423SLionel Sambuc 		if((bdata = mmap(0, blocksize,
37433d6423SLionel Sambuc 		       PROT_READ|PROT_WRITE, MAP_ANON, -1, 0)) == MAP_FAILED) {
38433d6423SLionel Sambuc 			printf("mmap failed\n");
39433d6423SLionel Sambuc 			exit(1);
40433d6423SLionel Sambuc 		}
41433d6423SLionel Sambuc 		mustset = 1;
42433d6423SLionel Sambuc 	}
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc 	memcpy(bdata, block, blocksize);
45433d6423SLionel Sambuc 
46433d6423SLionel Sambuc 	if(mustset && (r=vm_set_cacheblock(bdata, MYDEV, dev_off,
47*e321f655SDavid van Moolenbroek 		VMC_NO_INODE, 0, NULL, blocksize, 0)) != OK) {
48433d6423SLionel Sambuc 		printf("dowriteblock: vm_set_cacheblock failed %d\n", r);
49433d6423SLionel Sambuc 		exit(1);
50433d6423SLionel Sambuc 	}
51433d6423SLionel Sambuc 
52433d6423SLionel Sambuc 	if(munmap(bdata, blocksize) < 0) {
53433d6423SLionel Sambuc 		printf("dowriteblock: munmap failed %d\n", r);
54433d6423SLionel Sambuc 		exit(1);
55433d6423SLionel Sambuc 	}
56433d6423SLionel Sambuc 
57433d6423SLionel Sambuc 	return blocksize;
58433d6423SLionel Sambuc }
59433d6423SLionel Sambuc 
readblock(int b,int blocksize,u32_t seed,char * block)60433d6423SLionel Sambuc int readblock(int b, int blocksize, u32_t seed, char *block)
61433d6423SLionel Sambuc {
62433d6423SLionel Sambuc 	char *bdata;
63433d6423SLionel Sambuc 	u64_t dev_off = (u64_t) b * blocksize;
64433d6423SLionel Sambuc 
65433d6423SLionel Sambuc 	if((bdata = vm_map_cacheblock(MYDEV, dev_off,
66433d6423SLionel Sambuc 		VMC_NO_INODE, 0, NULL, blocksize)) == MAP_FAILED) {
67433d6423SLionel Sambuc 		return OK_BLOCK_GONE;
68433d6423SLionel Sambuc 	}
69433d6423SLionel Sambuc 
70433d6423SLionel Sambuc 	memcpy(block, bdata, blocksize);
71433d6423SLionel Sambuc 
72433d6423SLionel Sambuc 	if(munmap(bdata, blocksize) < 0) {
73433d6423SLionel Sambuc 		printf("dowriteblock: munmap failed\n");
74433d6423SLionel Sambuc 		exit(1);
75433d6423SLionel Sambuc 	}
76433d6423SLionel Sambuc 
77433d6423SLionel Sambuc 	return blocksize;
78433d6423SLionel Sambuc }
79433d6423SLionel Sambuc 
testend(void)80433d6423SLionel Sambuc void testend(void) { }
81433d6423SLionel Sambuc 
82433d6423SLionel Sambuc static void
writepipe(struct info * i)83433d6423SLionel Sambuc writepipe(struct info *i)
84433d6423SLionel Sambuc {
85433d6423SLionel Sambuc 	if(write(pipefd, i, sizeof(*i)) != sizeof(*i)) {
86433d6423SLionel Sambuc 		printf("%s: pipe write failed\n", progname);
87433d6423SLionel Sambuc 		exit(1);
88433d6423SLionel Sambuc 	}
89433d6423SLionel Sambuc }
90433d6423SLionel Sambuc 
91433d6423SLionel Sambuc static int
testinit(void)92433d6423SLionel Sambuc testinit(void)
93433d6423SLionel Sambuc {
94433d6423SLionel Sambuc 	struct stat st;
95433d6423SLionel Sambuc 	int attempts = 0;
96433d6423SLionel Sambuc 
97433d6423SLionel Sambuc 	for(attempts = 0; attempts < 5 && pipefd < 0; attempts++) {
98433d6423SLionel Sambuc 		if(attempts > 0) sleep(1);
99433d6423SLionel Sambuc 		pipefd = open(pipefilename, O_WRONLY | O_NONBLOCK);
100433d6423SLionel Sambuc 	}
101433d6423SLionel Sambuc 
102433d6423SLionel Sambuc 	if(pipefd < 0) {
103433d6423SLionel Sambuc 		printf("%s: could not open pipe %s, errno %d\n",
104433d6423SLionel Sambuc 			progname, pipefilename, errno);
105433d6423SLionel Sambuc 		exit(1);
106433d6423SLionel Sambuc 	}
107433d6423SLionel Sambuc 
108433d6423SLionel Sambuc 	if(fstat(pipefd, &st) < 0) {
109433d6423SLionel Sambuc 		printf("%s: could not fstat pipe %s\n", progname, pipefilename);
110433d6423SLionel Sambuc 		exit(1);
111433d6423SLionel Sambuc 	}
112433d6423SLionel Sambuc 
113433d6423SLionel Sambuc 	if(!(st.st_mode & I_NAMED_PIPE)) {
114433d6423SLionel Sambuc 		printf("%s: file %s is not a pipe\n", progname, pipefilename);
115433d6423SLionel Sambuc 		exit(1);
116433d6423SLionel Sambuc 	}
117433d6423SLionel Sambuc 
118433d6423SLionel Sambuc 	return OK;
119433d6423SLionel Sambuc }
120433d6423SLionel Sambuc 
121433d6423SLionel Sambuc static int
sef_cb_init(int type,sef_init_info_t * UNUSED (info))122433d6423SLionel Sambuc sef_cb_init(int type, sef_init_info_t *UNUSED(info))
123433d6423SLionel Sambuc {
124433d6423SLionel Sambuc 	return OK;
125433d6423SLionel Sambuc }
126433d6423SLionel Sambuc 
127433d6423SLionel Sambuc static void
init(void)128433d6423SLionel Sambuc init(void)
129433d6423SLionel Sambuc {
130433d6423SLionel Sambuc 	/* SEF init */
131433d6423SLionel Sambuc 	sef_setcb_init_fresh(sef_cb_init);
132433d6423SLionel Sambuc 	sef_setcb_init_lu(sef_cb_init);
133433d6423SLionel Sambuc 	sef_setcb_init_restart(sef_cb_init);
134433d6423SLionel Sambuc 
135433d6423SLionel Sambuc 	sef_startup();
136433d6423SLionel Sambuc }
137433d6423SLionel Sambuc 
138433d6423SLionel Sambuc 
139433d6423SLionel Sambuc 
140433d6423SLionel Sambuc int
main(int argc,char * argv[])141433d6423SLionel Sambuc main(int argc, char *argv[])
142433d6423SLionel Sambuc {
143433d6423SLionel Sambuc 	struct info info;
144433d6423SLionel Sambuc 	int big;
145433d6423SLionel Sambuc 	u32_t totalmem, freemem, cachedmem;
146433d6423SLionel Sambuc 
147433d6423SLionel Sambuc 	progname = argv[0];
148433d6423SLionel Sambuc 
149433d6423SLionel Sambuc 	if(argc < 2) { printf("no args\n"); return 1; }
150433d6423SLionel Sambuc 
151433d6423SLionel Sambuc 	pipefilename=argv[1];
152433d6423SLionel Sambuc 
153433d6423SLionel Sambuc 	big = !!strstr(pipefilename, "big");
154433d6423SLionel Sambuc 
155433d6423SLionel Sambuc 	init();
156433d6423SLionel Sambuc 
157433d6423SLionel Sambuc 	info.result = time(NULL);
158433d6423SLionel Sambuc 
159433d6423SLionel Sambuc 	if(testinit() != OK) { printf("%s: testinit failed\n", progname); return 1; }
160433d6423SLionel Sambuc 
161433d6423SLionel Sambuc 	cachequiet(!big);
162433d6423SLionel Sambuc 
163433d6423SLionel Sambuc 	if(!(bdata = alloc_contig(PAGE_SIZE, 0, NULL))) {
164433d6423SLionel Sambuc 		printf("could not allocate block\n");
165433d6423SLionel Sambuc 		exit(1);
166433d6423SLionel Sambuc 	}
167433d6423SLionel Sambuc 
168433d6423SLionel Sambuc 	if(dotest(PAGE_SIZE,       10, 3)) { e(11); exit(1); }
169433d6423SLionel Sambuc 	if(dotest(PAGE_SIZE,     1000, 3)) { e(11); exit(1); }
170433d6423SLionel Sambuc 	if(dotest(PAGE_SIZE,    50000, 3)) { e(11); exit(1); }
171433d6423SLionel Sambuc 	if(big) {
172433d6423SLionel Sambuc 		getmem(&totalmem, &freemem, &cachedmem);
173433d6423SLionel Sambuc 		if(dotest(PAGE_SIZE, totalmem*1.5, 3)) { e(11); exit(1); }
174433d6423SLionel Sambuc 	}
175433d6423SLionel Sambuc 
176433d6423SLionel Sambuc 	info.result = 0;
177433d6423SLionel Sambuc 
178433d6423SLionel Sambuc 	writepipe(&info);
179433d6423SLionel Sambuc 
180433d6423SLionel Sambuc 	vm_clear_cache(MYDEV);
181433d6423SLionel Sambuc 
182433d6423SLionel Sambuc 	return 0;
183433d6423SLionel Sambuc }
184433d6423SLionel Sambuc 
185