1 /* Test 72 - libminixfs unit test.
3 * Exercise the caching functionality of libminixfs in isolation.
8 #include <minix/sysutil.h>
9 #include <minix/syslib.h>
11 #include <minix/bdev.h>
12 #include <sys/types.h>
14 #include <sys/ioc_memory.h>
23 #include <minix/libminixfs.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
)
46 assert(blocksize
== curblocksize
);
48 if ((r
= lmfs_get_block(&bp
, MYDEV
, b
, NORMAL
)) != 0) {
53 memcpy(bp
->data
, data
, blocksize
);
63 readblock(int b
, int blocksize
, u32_t seed
, char *data
)
68 assert(blocksize
== curblocksize
);
70 if ((r
= lmfs_get_block(&bp
, MYDEV
, b
, NORMAL
)) != 0) {
75 memcpy(data
, bp
->data
, blocksize
);
85 for(i
= 0; i
< MAXBLOCKS
; i
++) {
86 if(writtenblocks
[i
]) {
87 free(writtenblocks
[i
]);
88 writtenblocks
[i
] = NULL
;
93 /* Fake some libminixfs client functions */
95 static void allocate(int b
)
97 assert(curblocksize
> 0);
98 assert(!writtenblocks
[b
]);
99 if(!(writtenblocks
[b
] = calloc(1, curblocksize
))) {
100 fprintf(stderr
, "out of memory allocating block %d\n", b
);
105 /* Fake some libblockdriver functions */
107 bdev_gather(dev_t dev
, u64_t pos
, iovec_t
*vec
, int count
, int flags
)
110 size_t size
, block_off
;
112 assert(dev
== MYDEV
);
113 assert(curblocksize
> 0);
114 assert(!(pos
% curblocksize
));
115 for(i
= 0; i
< count
; i
++) {
116 char *data
= (char *) vec
[i
].iov_addr
;
117 block
= pos
/ curblocksize
;
118 block_off
= (size_t)(pos
% curblocksize
);
119 size
= vec
[i
].iov_size
;
120 assert(size
== PAGE_SIZE
);
122 assert(block
< MAXBLOCKS
);
123 assert(block_off
+ size
<= curblocksize
);
124 if(!writtenblocks
[block
]) {
127 memcpy(data
, writtenblocks
[block
] + block_off
, size
);
136 bdev_scatter(dev_t dev
, u64_t pos
, iovec_t
*vec
, int count
, int flags
)
139 size_t size
, block_off
;
141 assert(dev
== MYDEV
);
142 assert(curblocksize
> 0);
143 assert(!(pos
% curblocksize
));
144 for(i
= 0; i
< count
; i
++) {
145 char *data
= (char *) vec
[i
].iov_addr
;
146 block
= pos
/ curblocksize
;
147 block_off
= (size_t)(pos
% curblocksize
);
148 size
= vec
[i
].iov_size
;
149 assert(size
== PAGE_SIZE
);
151 assert(block
< MAXBLOCKS
);
152 assert(block_off
+ size
<= curblocksize
);
153 if(!writtenblocks
[block
]) {
156 memcpy(writtenblocks
[block
] + block_off
, data
, size
);
165 bdev_read(dev_t dev
, u64_t pos
, char *data
, size_t count
, int flags
)
169 assert(dev
== MYDEV
);
170 assert(curblocksize
> 0);
171 assert(!(pos
% curblocksize
));
173 assert(!(count
% curblocksize
));
174 assert(count
== PAGE_SIZE
);
175 assert(curblocksize
== PAGE_SIZE
);
177 block
= pos
/ curblocksize
;
179 assert(block
< MAXBLOCKS
);
180 if(!writtenblocks
[block
]) {
183 memcpy(data
, writtenblocks
[block
], curblocksize
);
187 /* Fake some libsys functions */
190 panic(const char *fmt
, ...)
194 vfprintf(stderr
, fmt
, va
);
201 vm_info_stats(struct vm_stats_info
*vsi
)
207 util_stacktrace(void)
209 fprintf(stderr
, "fake stacktrace\n");
212 void *alloc_contig(size_t len
, int flags
, phys_bytes
*phys
)
217 int free_contig(void *addr
, size_t len
)
223 u32_t
sqrt_approx(u32_t v
)
225 return (u32_t
) sqrt(v
);
228 int vm_set_cacheblock(void *block
, dev_t dev
, off_t dev_offset
,
229 ino_t ino
, off_t ino_offset
, u32_t
*flags
, int blocksize
, int setflags
)
234 void *vm_map_cacheblock(dev_t dev
, off_t dev_offset
,
235 ino_t ino
, off_t ino_offset
, u32_t
*flags
, int blocksize
)
240 int vm_forget_cacheblock(dev_t dev
, off_t dev_offset
, int blocksize
)
245 int vm_clear_cache(dev_t dev
)
251 main(int argc
, char *argv
[])
254 int wss
, cs
, n
= 0, p
;
263 /* Can the cache handle differently sized blocks? */
265 for(p
= 1; p
<= 3; p
++) {
266 /* Do not update curblocksize until the cache is flushed. */
267 newblocksize
= PAGE_SIZE
*p
;
268 lmfs_set_blocksize(newblocksize
);
269 curblocksize
= newblocksize
; /* now it's safe to update */
270 lmfs_buf_pool(BLOCKS
);
271 if(dotest(curblocksize
, BLOCKS
, ITER
)) e(n
);
275 /* Can the cache handle various combinations of the working set
276 * being larger and smaller than the cache?
278 for(wss
= 2; wss
<= 3; wss
++) {
279 int wsblocks
= 10*wss
*wss
*wss
*wss
*wss
;
280 for(cs
= wsblocks
/4; cs
<= wsblocks
*3; cs
*= 1.5) {
281 lmfs_set_blocksize(PAGE_SIZE
);
282 curblocksize
= PAGE_SIZE
; /* same as above */
284 if(dotest(curblocksize
, wsblocks
, ITER
)) e(n
);