VM: memtype fix
[minix3.git] / test / test72.c
blobf376497ca6913d1555751a2df472ae7d847ca65b
1 /* Test 72 - libminixfs unit test.
3 * Exercise the caching functionality of libminixfs in isolation.
4 */
6 #include <minix/libminixfs.h>
7 #include <minix/sysutil.h>
8 #include <minix/syslib.h>
9 #include <minix/vm.h>
10 #include <minix/bdev.h>
11 #include <sys/types.h>
12 #include <sys/mman.h>
13 #include <sys/ioc_memory.h>
14 #include <stdio.h>
15 #include <stdarg.h>
16 #include <assert.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <math.h>
23 int max_error = 0;
25 #include "common.h"
26 #include "testcache.h"
28 #define MYMAJOR 40 /* doesn't really matter, shouldn't be NO_DEV though */
30 #define MYDEV makedev(MYMAJOR, 1)
32 static int curblocksize = -1;
34 static char *writtenblocks[MAXBLOCKS];
36 /* Some functions used by testcache.c */
38 int
39 dowriteblock(int b, int blocksize, u32_t seed, char *data)
41 struct buf *bp;
43 assert(blocksize == curblocksize);
45 if(!(bp = lmfs_get_block(MYDEV, b, NORMAL))) {
46 e(30);
47 return 0;
50 memcpy(bp->data, data, blocksize);
52 lmfs_markdirty(bp);
54 lmfs_put_block(bp, FULL_DATA_BLOCK);
56 return blocksize;
59 int
60 readblock(int b, int blocksize, u32_t seed, char *data)
62 struct buf *bp;
64 assert(blocksize == curblocksize);
66 if(!(bp = lmfs_get_block(MYDEV, b, NORMAL))) {
67 e(30);
68 return 0;
71 memcpy(data, bp->data, blocksize);
73 lmfs_put_block(bp, FULL_DATA_BLOCK);
75 return blocksize;
78 void testend(void)
80 int i;
81 for(i = 0; i < MAXBLOCKS; i++) {
82 if(writtenblocks[i]) {
83 free(writtenblocks[i]);
84 writtenblocks[i] = NULL;
89 /* Fake some libminixfs client functions */
91 int
92 fs_sync(void)
94 return 0;
97 void
98 fs_blockstats(u32_t *total, u32_t *free, u32_t *used)
100 *total = *free = *used = 0;
103 static void allocate(int b)
105 assert(curblocksize > 0);
106 assert(!writtenblocks[b]);
107 if(!(writtenblocks[b] = calloc(1, curblocksize))) {
108 fprintf(stderr, "out of memory allocating block %d\n", b);
109 exit(1);
113 /* Fake some libblockdriver functions */
114 ssize_t
115 bdev_gather(dev_t dev, u64_t pos, iovec_t *vec, int count, int flags)
117 int i, block;
118 ssize_t tot = 0;
119 assert(dev == MYDEV);
120 assert(curblocksize > 0);
121 assert(!(pos % curblocksize));
122 block = pos / curblocksize;
123 for(i = 0; i < count; i++) {
124 int subblocks;
125 char *data = (char *) vec[i].iov_addr;
126 assert(vec[i].iov_size > 0);
127 assert(!(vec[i].iov_size % curblocksize));
128 subblocks = vec[i].iov_size / curblocksize;
129 while(subblocks > 0) {
130 assert(block > 0);
131 assert(block < MAXBLOCKS);
132 if(!writtenblocks[block]) {
133 allocate(block);
135 memcpy(data, writtenblocks[block], curblocksize);
136 block++;
137 subblocks--;
138 data += curblocksize;
139 tot += curblocksize;
143 return tot;
146 ssize_t
147 bdev_scatter(dev_t dev, u64_t pos, iovec_t *vec, int count, int flags)
149 int i, block;
150 ssize_t tot = 0;
151 assert(dev == MYDEV);
152 assert(curblocksize > 0);
153 assert(!(pos % curblocksize));
154 block = pos / curblocksize;
155 for(i = 0; i < count; i++) {
156 int subblocks;
157 char *data = (char *) vec[i].iov_addr;
158 assert(vec[i].iov_size > 0);
159 assert(!(vec[i].iov_size % curblocksize));
160 subblocks = vec[i].iov_size / curblocksize;
161 while(subblocks > 0) {
162 assert(block >= 0);
163 assert(block < MAXBLOCKS);
164 if(!writtenblocks[block]) {
165 allocate(block);
167 memcpy(writtenblocks[block], data, curblocksize);
168 block++;
169 subblocks--;
170 data += curblocksize;
171 tot += curblocksize;
175 return tot;
178 ssize_t
179 bdev_read(dev_t dev, u64_t pos, char *data, size_t count, int flags)
181 int block;
182 ssize_t tot = 0;
183 int subblocks;
185 assert(dev == MYDEV);
186 assert(curblocksize > 0);
187 assert(!(pos % curblocksize));
188 assert(count > 0);
189 assert(!(count % curblocksize));
191 block = pos / curblocksize;
192 subblocks = count / curblocksize;
193 while(subblocks > 0) {
194 assert(block >= 0);
195 assert(block < MAXBLOCKS);
196 if(!writtenblocks[block]) {
197 allocate(block);
199 memcpy(data, writtenblocks[block], curblocksize);
200 block++;
201 subblocks--;
202 data += curblocksize;
203 tot += curblocksize;
206 return tot;
209 /* Fake some libsys functions */
211 __dead void
212 panic(const char *fmt, ...)
214 va_list va;
215 va_start(va, fmt);
216 vfprintf(stderr, fmt, va);
217 va_end(va);
219 exit(1);
222 int vm_forgetblock(u64_t id)
224 return ENOSYS;
227 int vm_yield_block_get_block(u64_t yieldid, u64_t getid, void *mem,
228 vir_bytes len)
230 return ENOSYS;
233 void vm_forgetblocks(void)
235 return;
239 vm_info_stats(struct vm_stats_info *vsi)
241 return ENOSYS;
244 void
245 util_stacktrace(void)
247 fprintf(stderr, "fake stacktrace\n");
250 void *alloc_contig(size_t len, int flags, phys_bytes *phys)
252 return malloc(len);
255 int free_contig(void *addr, size_t len)
257 free(addr);
258 return 0;
261 u32_t sqrt_approx(u32_t v)
263 return (u32_t) sqrt(v);
267 main(int argc, char *argv[])
269 int wss, cs, n = 0, p;
271 #define ITER 3
272 #define BLOCKS 200
274 start(72);
276 lmfs_setquiet(1);
278 /* Can the cache handle differently sized blocks? */
280 for(p = 1; p <= 3; p++) {
281 curblocksize = PAGE_SIZE*p;
282 lmfs_set_blocksize(curblocksize, MYMAJOR);
283 lmfs_buf_pool(BLOCKS);
284 if(dotest(curblocksize, BLOCKS, ITER)) e(n);
285 n++;
288 /* Can the cache handle various combinations of the working set
289 * being larger and smaller than the cache?
291 for(wss = 2; wss <= 3; wss++) {
292 int wsblocks = 10*wss*wss*wss*wss*wss;
293 for(cs = wsblocks/4; cs <= wsblocks*3; cs *= 1.5) {
294 curblocksize = PAGE_SIZE;
295 lmfs_set_blocksize(curblocksize, MYMAJOR);
296 lmfs_buf_pool(cs);
297 if(dotest(curblocksize, wsblocks, ITER)) e(n);
298 n++;
302 quit();
304 return 0;