custom message for vm_update
[minix3.git] / test / test72.c
blob2b4b2ae9be3cb1cc559dc25a45c48523319f2292
1 /* Test 72 - libminixfs unit test.
3 * Exercise the caching functionality of libminixfs in isolation.
4 */
6 #define _MINIX_SYSTEM
8 #include <minix/libminixfs.h>
9 #include <minix/sysutil.h>
10 #include <minix/syslib.h>
11 #include <minix/vm.h>
12 #include <minix/bdev.h>
13 #include <sys/types.h>
14 #include <sys/mman.h>
15 #include <sys/ioc_memory.h>
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <assert.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <math.h>
25 int max_error = 0;
27 #include "common.h"
28 #include "testcache.h"
30 #define MYMAJOR 40 /* doesn't really matter, shouldn't be NO_DEV though */
32 #define MYDEV makedev(MYMAJOR, 1)
34 static int curblocksize = -1;
36 static char *writtenblocks[MAXBLOCKS];
38 /* Some functions used by testcache.c */
40 int
41 dowriteblock(int b, int blocksize, u32_t seed, char *data)
43 struct buf *bp;
45 assert(blocksize == curblocksize);
47 if(!(bp = lmfs_get_block(MYDEV, b, NORMAL))) {
48 e(30);
49 return 0;
52 memcpy(bp->data, data, blocksize);
54 lmfs_markdirty(bp);
56 lmfs_put_block(bp, FULL_DATA_BLOCK);
58 return blocksize;
61 int
62 readblock(int b, int blocksize, u32_t seed, char *data)
64 struct buf *bp;
66 assert(blocksize == curblocksize);
68 if(!(bp = lmfs_get_block(MYDEV, b, NORMAL))) {
69 e(30);
70 return 0;
73 memcpy(data, bp->data, blocksize);
75 lmfs_put_block(bp, FULL_DATA_BLOCK);
77 return blocksize;
80 void testend(void)
82 int i;
83 for(i = 0; i < MAXBLOCKS; i++) {
84 if(writtenblocks[i]) {
85 free(writtenblocks[i]);
86 writtenblocks[i] = NULL;
91 /* Fake some libminixfs client functions */
93 int
94 fs_sync(void)
96 return 0;
99 void
100 fs_blockstats(u64_t *total, u64_t *free, u64_t *used)
102 *total = *free = *used = 0;
105 static void allocate(int b)
107 assert(curblocksize > 0);
108 assert(!writtenblocks[b]);
109 if(!(writtenblocks[b] = calloc(1, curblocksize))) {
110 fprintf(stderr, "out of memory allocating block %d\n", b);
111 exit(1);
115 /* Fake some libblockdriver functions */
116 ssize_t
117 bdev_gather(dev_t dev, u64_t pos, iovec_t *vec, int count, int flags)
119 int i;
120 ssize_t tot = 0;
121 assert(dev == MYDEV);
122 assert(curblocksize > 0);
123 assert(!(pos % curblocksize));
124 for(i = 0; i < count; i++) {
125 int subpages, block, block_off;
126 char *data = (char *) vec[i].iov_addr;
127 assert(!(pos % curblocksize));
128 block = pos / curblocksize;
129 block_off = pos % curblocksize;
130 assert(!(vec[i].iov_size % PAGE_SIZE));
131 subpages = vec[i].iov_size / PAGE_SIZE;
132 while(subpages > 0) {
133 assert(block >= 0);
134 assert(block < MAXBLOCKS);
135 assert(block_off >= 0);
136 assert(block_off < curblocksize);
137 if(!writtenblocks[block]) {
138 allocate(block);
140 memcpy(data, writtenblocks[block] + block_off,
141 PAGE_SIZE);
142 block++;
143 subpages--;
144 data += PAGE_SIZE;
145 tot += PAGE_SIZE;
146 block_off += PAGE_SIZE;
150 return tot;
153 ssize_t
154 bdev_scatter(dev_t dev, u64_t pos, iovec_t *vec, int count, int flags)
156 int i, block;
157 ssize_t tot = 0;
158 assert(dev == MYDEV);
159 assert(curblocksize > 0);
160 assert(!(pos % curblocksize));
161 block = pos / curblocksize;
162 for(i = 0; i < count; i++) {
163 int subblocks;
164 char *data = (char *) vec[i].iov_addr;
165 assert(vec[i].iov_size > 0);
166 assert(!(vec[i].iov_size % PAGE_SIZE));
167 subblocks = vec[i].iov_size / curblocksize;
168 while(subblocks > 0) {
169 assert(block >= 0);
170 assert(block < MAXBLOCKS);
171 if(!writtenblocks[block]) {
172 allocate(block);
174 memcpy(writtenblocks[block], data, curblocksize);
175 block++;
176 subblocks--;
177 data += curblocksize;
178 tot += curblocksize;
182 return tot;
185 ssize_t
186 bdev_read(dev_t dev, u64_t pos, char *data, size_t count, int flags)
188 int block;
189 ssize_t tot = 0;
190 int subblocks;
192 assert(dev == MYDEV);
193 assert(curblocksize > 0);
194 assert(!(pos % curblocksize));
195 assert(count > 0);
196 assert(!(count % curblocksize));
198 block = pos / curblocksize;
199 subblocks = count / curblocksize;
200 while(subblocks > 0) {
201 assert(block >= 0);
202 assert(block < MAXBLOCKS);
203 if(!writtenblocks[block]) {
204 allocate(block);
206 memcpy(data, writtenblocks[block], curblocksize);
207 block++;
208 subblocks--;
209 data += curblocksize;
210 tot += curblocksize;
213 return tot;
216 /* Fake some libsys functions */
218 __dead void
219 panic(const char *fmt, ...)
221 va_list va;
222 va_start(va, fmt);
223 vfprintf(stderr, fmt, va);
224 va_end(va);
226 exit(1);
230 vm_info_stats(struct vm_stats_info *vsi)
232 return ENOSYS;
235 void
236 util_stacktrace(void)
238 fprintf(stderr, "fake stacktrace\n");
241 void *alloc_contig(size_t len, int flags, phys_bytes *phys)
243 return malloc(len);
246 int free_contig(void *addr, size_t len)
248 free(addr);
249 return 0;
252 u32_t sqrt_approx(u32_t v)
254 return (u32_t) sqrt(v);
257 int vm_set_cacheblock(void *block, dev_t dev, off_t dev_offset,
258 ino_t ino, off_t ino_offset, u32_t *flags, int blocksize)
260 return ENOSYS;
263 void *vm_map_cacheblock(dev_t dev, off_t dev_offset,
264 ino_t ino, off_t ino_offset, u32_t *flags, int blocksize)
266 return MAP_FAILED;
269 int vm_clear_cache(dev_t dev)
271 return 0;
275 main(int argc, char *argv[])
277 int wss, cs, n = 0, p;
279 #define ITER 3
280 #define BLOCKS 200
282 start(72);
284 lmfs_setquiet(1);
286 /* Can the cache handle differently sized blocks? */
288 for(p = 1; p <= 3; p++) {
289 curblocksize = PAGE_SIZE*p;
290 lmfs_set_blocksize(curblocksize, MYMAJOR);
291 lmfs_buf_pool(BLOCKS);
292 if(dotest(curblocksize, BLOCKS, ITER)) e(n);
293 n++;
296 /* Can the cache handle various combinations of the working set
297 * being larger and smaller than the cache?
299 for(wss = 2; wss <= 3; wss++) {
300 int wsblocks = 10*wss*wss*wss*wss*wss;
301 for(cs = wsblocks/4; cs <= wsblocks*3; cs *= 1.5) {
302 curblocksize = PAGE_SIZE;
303 lmfs_set_blocksize(curblocksize, MYMAJOR);
304 lmfs_buf_pool(cs);
305 if(dotest(curblocksize, wsblocks, ITER)) e(n);
306 n++;
310 quit();
312 return 0;