1 /* Test 72 - libminixfs unit test.
3 * Exercise the caching functionality of libminixfs in isolation.
6 #include <minix/libminixfs.h>
7 #include <minix/sysutil.h>
8 #include <minix/syslib.h>
10 #include <minix/bdev.h>
11 #include <sys/types.h>
13 #include <sys/ioc_memory.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 */
39 dowriteblock(int b
, int blocksize
, u32_t seed
, char *data
)
43 assert(blocksize
== curblocksize
);
45 if(!(bp
= lmfs_get_block(MYDEV
, b
, NORMAL
))) {
50 memcpy(bp
->data
, data
, blocksize
);
54 lmfs_put_block(bp
, FULL_DATA_BLOCK
);
60 readblock(int b
, int blocksize
, u32_t seed
, char *data
)
64 assert(blocksize
== curblocksize
);
66 if(!(bp
= lmfs_get_block(MYDEV
, b
, NORMAL
))) {
71 memcpy(data
, bp
->data
, blocksize
);
73 lmfs_put_block(bp
, FULL_DATA_BLOCK
);
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 */
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
);
113 /* Fake some libblockdriver functions */
115 bdev_gather(dev_t dev
, u64_t pos
, iovec_t
*vec
, int count
, int flags
)
119 assert(dev
== MYDEV
);
120 assert(curblocksize
> 0);
121 assert(!(pos
% curblocksize
));
122 block
= pos
/ curblocksize
;
123 for(i
= 0; i
< count
; i
++) {
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) {
131 assert(block
< MAXBLOCKS
);
132 if(!writtenblocks
[block
]) {
135 memcpy(data
, writtenblocks
[block
], curblocksize
);
138 data
+= curblocksize
;
147 bdev_scatter(dev_t dev
, u64_t pos
, iovec_t
*vec
, int count
, int flags
)
151 assert(dev
== MYDEV
);
152 assert(curblocksize
> 0);
153 assert(!(pos
% curblocksize
));
154 block
= pos
/ curblocksize
;
155 for(i
= 0; i
< count
; i
++) {
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) {
163 assert(block
< MAXBLOCKS
);
164 if(!writtenblocks
[block
]) {
167 memcpy(writtenblocks
[block
], data
, curblocksize
);
170 data
+= curblocksize
;
179 bdev_read(dev_t dev
, u64_t pos
, char *data
, size_t count
, int flags
)
185 assert(dev
== MYDEV
);
186 assert(curblocksize
> 0);
187 assert(!(pos
% curblocksize
));
189 assert(!(count
% curblocksize
));
191 block
= pos
/ curblocksize
;
192 subblocks
= count
/ curblocksize
;
193 while(subblocks
> 0) {
195 assert(block
< MAXBLOCKS
);
196 if(!writtenblocks
[block
]) {
199 memcpy(data
, writtenblocks
[block
], curblocksize
);
202 data
+= curblocksize
;
209 /* Fake some libsys functions */
212 panic(const char *fmt
, ...)
216 vfprintf(stderr
, fmt
, va
);
222 int vm_forgetblock(u64_t id
)
227 int vm_yield_block_get_block(u64_t yieldid
, u64_t getid
, void *mem
,
233 void vm_forgetblocks(void)
239 vm_info_stats(struct vm_stats_info
*vsi
)
245 util_stacktrace(void)
247 fprintf(stderr
, "fake stacktrace\n");
250 void *alloc_contig(size_t len
, int flags
, phys_bytes
*phys
)
255 int free_contig(void *addr
, size_t len
)
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
;
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
);
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
);
297 if(dotest(curblocksize
, wsblocks
, ITER
)) e(n
);