1 /* Test 72 - libminixfs unit test.
3 * Exercise the caching functionality of libminixfs in isolation.
8 #include <minix/libminixfs.h>
9 #include <minix/sysutil.h>
10 #include <minix/syslib.h>
12 #include <minix/bdev.h>
13 #include <sys/types.h>
15 #include <sys/ioc_memory.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 */
41 dowriteblock(int b
, int blocksize
, u32_t seed
, char *data
)
45 assert(blocksize
== curblocksize
);
47 if(!(bp
= lmfs_get_block(MYDEV
, b
, NORMAL
))) {
52 memcpy(bp
->data
, data
, blocksize
);
56 lmfs_put_block(bp
, FULL_DATA_BLOCK
);
62 readblock(int b
, int blocksize
, u32_t seed
, char *data
)
66 assert(blocksize
== curblocksize
);
68 if(!(bp
= lmfs_get_block(MYDEV
, b
, NORMAL
))) {
73 memcpy(data
, bp
->data
, blocksize
);
75 lmfs_put_block(bp
, FULL_DATA_BLOCK
);
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 */
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
);
115 /* Fake some libblockdriver functions */
117 bdev_gather(dev_t dev
, u64_t pos
, iovec_t
*vec
, int count
, int flags
)
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) {
134 assert(block
< MAXBLOCKS
);
135 assert(block_off
>= 0);
136 assert(block_off
< curblocksize
);
137 if(!writtenblocks
[block
]) {
140 memcpy(data
, writtenblocks
[block
] + block_off
,
146 block_off
+= PAGE_SIZE
;
154 bdev_scatter(dev_t dev
, u64_t pos
, iovec_t
*vec
, int count
, int flags
)
158 assert(dev
== MYDEV
);
159 assert(curblocksize
> 0);
160 assert(!(pos
% curblocksize
));
161 block
= pos
/ curblocksize
;
162 for(i
= 0; i
< count
; i
++) {
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) {
170 assert(block
< MAXBLOCKS
);
171 if(!writtenblocks
[block
]) {
174 memcpy(writtenblocks
[block
], data
, curblocksize
);
177 data
+= curblocksize
;
186 bdev_read(dev_t dev
, u64_t pos
, char *data
, size_t count
, int flags
)
192 assert(dev
== MYDEV
);
193 assert(curblocksize
> 0);
194 assert(!(pos
% curblocksize
));
196 assert(!(count
% curblocksize
));
198 block
= pos
/ curblocksize
;
199 subblocks
= count
/ curblocksize
;
200 while(subblocks
> 0) {
202 assert(block
< MAXBLOCKS
);
203 if(!writtenblocks
[block
]) {
206 memcpy(data
, writtenblocks
[block
], curblocksize
);
209 data
+= curblocksize
;
216 /* Fake some libsys functions */
219 panic(const char *fmt
, ...)
223 vfprintf(stderr
, fmt
, va
);
230 vm_info_stats(struct vm_stats_info
*vsi
)
236 util_stacktrace(void)
238 fprintf(stderr
, "fake stacktrace\n");
241 void *alloc_contig(size_t len
, int flags
, phys_bytes
*phys
)
246 int free_contig(void *addr
, size_t len
)
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
)
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
)
269 int vm_clear_cache(dev_t dev
)
275 main(int argc
, char *argv
[])
277 int wss
, cs
, n
= 0, p
;
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
);
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
);
305 if(dotest(curblocksize
, wsblocks
, ITER
)) e(n
);