summaryrefslogtreecommitdiff
path: root/minix/tests/test72.c
blob: 8e6cc1103a3ccaadd4f12f309358b971695c9e39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Test 72 - libminixfs unit test.
 *
 * Exercise the caching functionality of libminixfs in isolation.
 */

#define _MINIX_SYSTEM

#include <minix/sysutil.h>
#include <minix/syslib.h>
#include <minix/vm.h>
#include <minix/bdev.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/ioc_memory.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <minix/libminixfs.h>

int max_error = 0;

#include "common.h"
#include "testcache.h"

#define MYMAJOR	40	/* doesn't really matter, shouldn't be NO_DEV though */

#define MYDEV	makedev(MYMAJOR, 1)

static int curblocksize = -1;

static char *writtenblocks[MAXBLOCKS];

/* Some functions used by testcache.c */

int
dowriteblock(int b, int blocksize, u32_t seed, char *data)
{
	struct buf *bp;
	int r;

	assert(blocksize == curblocksize);

	if ((r = lmfs_get_block(&bp, MYDEV, b, NORMAL)) != 0) {
		e(30);
		return 0;
	}

	memcpy(bp->data, data, blocksize);

	lmfs_markdirty(bp);

	lmfs_put_block(bp);

	return blocksize;
}

int
readblock(int b, int blocksize, u32_t seed, char *data)
{
	struct buf *bp;
	int r;

	assert(blocksize == curblocksize);

	if ((r = lmfs_get_block(&bp, MYDEV, b, NORMAL)) != 0) {
		e(30);
		return 0;
	}

	memcpy(data, bp->data, blocksize);

	lmfs_put_block(bp);

	return blocksize;
}

void testend(void)
{
	int i;
	for(i = 0; i < MAXBLOCKS; i++) {
		if(writtenblocks[i]) {
			free(writtenblocks[i]);
			writtenblocks[i] = NULL;
		}
	}
}

/* Fake some libminixfs client functions */

static void allocate(int b)
{
	assert(curblocksize > 0);
	assert(!writtenblocks[b]);
	if(!(writtenblocks[b] = calloc(1, curblocksize))) {
		fprintf(stderr, "out of memory allocating block %d\n", b);
		exit(1);
	}
}

/* Fake some libblockdriver functions */
ssize_t
bdev_gather(dev_t dev, u64_t pos, iovec_t *vec, int count, int flags)
{
	int i, block;
	size_t size, block_off;
	ssize_t tot = 0;
	assert(dev == MYDEV);
	assert(curblocksize > 0);
	assert(!(pos % curblocksize));
	for(i = 0; i < count; i++) {
		char *data = (char *) vec[i].iov_addr;
		block = pos / curblocksize;
		block_off = (size_t)(pos % curblocksize);
		size = vec[i].iov_size;
		assert(size == PAGE_SIZE);
		assert(block >= 0);
		assert(block < MAXBLOCKS);
		assert(block_off + size <= curblocksize);
		if(!writtenblocks[block]) {
			allocate(block);
		}
		memcpy(data, writtenblocks[block] + block_off, size);
		pos += size;
		tot += size;
	}

	return tot;
}

ssize_t
bdev_scatter(dev_t dev, u64_t pos, iovec_t *vec, int count, int flags)
{
	int i, block;
	size_t size, block_off;
	ssize_t tot = 0;
	assert(dev == MYDEV);
	assert(curblocksize > 0);
	assert(!(pos % curblocksize));
	for(i = 0; i < count; i++) {
		char *data = (char *) vec[i].iov_addr;
		block = pos / curblocksize;
		block_off = (size_t)(pos % curblocksize);
		size = vec[i].iov_size;
		assert(size == PAGE_SIZE);
		assert(block >= 0);
		assert(block < MAXBLOCKS);
		assert(block_off + size <= curblocksize);
		if(!writtenblocks[block]) {
			allocate(block);
		}
		memcpy(writtenblocks[block] + block_off, data, size);
		pos += size;
		tot += size;
	}

	return tot;
}

ssize_t
bdev_read(dev_t dev, u64_t pos, char *data, size_t count, int flags)
{
	int block;

	assert(dev == MYDEV);
	assert(curblocksize > 0);
	assert(!(pos % curblocksize));
	assert(count > 0);
	assert(!(count % curblocksize));
	assert(count == PAGE_SIZE);
	assert(curblocksize == PAGE_SIZE);

	block = pos / curblocksize;
	assert(block >= 0);
	assert(block < MAXBLOCKS);
	if(!writtenblocks[block]) {
		allocate(block);
	}
	memcpy(data, writtenblocks[block], curblocksize);
	return count;
}

/* Fake some libsys functions */

__dead void
panic(const char *fmt, ...)
{
	va_list va;
	va_start(va, fmt);
	vfprintf(stderr, fmt, va);
	va_end(va);

	exit(1);
}

int
vm_info_stats(struct vm_stats_info *vsi)
{
	return ENOSYS;
}

void
util_stacktrace(void)
{
	fprintf(stderr, "fake stacktrace\n");
}

void *alloc_contig(size_t len, int flags, phys_bytes *phys)
{
	return malloc(len);
}

int free_contig(void *addr, size_t len)
{
	free(addr);
	return 0;
}

u32_t sqrt_approx(u32_t v)
{
	return (u32_t) sqrt(v);
}

int vm_set_cacheblock(void *block, dev_t dev, off_t dev_offset,
        ino_t ino, off_t ino_offset, u32_t *flags, int blocksize, int setflags)
{
	return ENOSYS;
}

void *vm_map_cacheblock(dev_t dev, off_t dev_offset,
        ino_t ino, off_t ino_offset, u32_t *flags, int blocksize)
{
	return MAP_FAILED;
}

int vm_forget_cacheblock(dev_t dev, off_t dev_offset, int blocksize)
{
	return 0;
}

int vm_clear_cache(dev_t dev)
{
	return 0;
}

int
main(int argc, char *argv[])
{
	size_t newblocksize;
	int wss, cs, n = 0, p;

#define ITER 3
#define BLOCKS 200

	start(72);

	lmfs_setquiet(1);

	/* Can the cache handle differently sized blocks? */

	for(p = 1; p <= 3; p++) {
		/* Do not update curblocksize until the cache is flushed. */
		newblocksize = PAGE_SIZE*p;
		lmfs_set_blocksize(newblocksize);
		curblocksize = newblocksize;	/* now it's safe to update */
		lmfs_buf_pool(BLOCKS);
		if(dotest(curblocksize, BLOCKS, ITER)) e(n);
		n++;
	}
	
	/* Can the cache handle various combinations of the working set
	 * being larger and smaller than the cache?
	 */
	for(wss = 2; wss <= 3; wss++) {
		int wsblocks = 10*wss*wss*wss*wss*wss;
		for(cs = wsblocks/4; cs <= wsblocks*3; cs *= 1.5) {
			lmfs_set_blocksize(PAGE_SIZE);
			curblocksize = PAGE_SIZE;	/* same as above */
			lmfs_buf_pool(cs);
		        if(dotest(curblocksize, wsblocks, ITER)) e(n);
			n++;
		}
	}

	quit();

	return 0;
}