summaryrefslogtreecommitdiff
path: root/bcache-test.c
blob: 49bb72374f75ea19b60da2229c0a40eb46b2d84f (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
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE

#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/fs.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdint.h>

#define Pread(fd, buf, size, offset) do {				\
	int _read = 0, _r;						\
	while (_read < size) {						\
		_r = pread(fd, buf, (size) - _read, (offset) + _read);	\
		if (_r <= 0)						\
			goto err;					\
		_read += _r;						\
	}								\
} while (0)

/* Marsaglia polar method
 */
double normal()
{
	double x, y, s;
	static double n = 0 / (double) 0;

	if (n == n) {
		x = n;
		n = 0 / (double) 0;
		return x;
	}
	
	do {
		x = random() / (double) (RAND_MAX / 2) - 1;
		y = random() / (double) (RAND_MAX / 2) - 1;

		s = x * x + y * y;
	} while (s >= 1);

	s = sqrt(-2 * log(s) / s);
	n =	y * s;
	return  x * s;
}

uint32_t fletcher32(uint16_t *data, size_t len)
{
        uint32_t sum1 = 0xffff, sum2 = 0xffff;
 
        while (len) {
                unsigned tlen = len > 360 ? 360 : len;
                len -= tlen;
                do {
                        sum1 += *data++;
                        sum2 += sum1;
                } while (--tlen);
                sum1 = (sum1 & 0xffff) + (sum1 >> 16);
                sum2 = (sum2 & 0xffff) + (sum2 >> 16);
        }
        /* Second reduction step to reduce sums to 16 bits */
        sum1 = (sum1 & 0xffff) + (sum1 >> 16);
        sum2 = (sum2 & 0xffff) + (sum2 >> 16);
        return sum2 << 16 | sum1;
}

long getblocks(int fd)
{
	long ret;
	struct stat statbuf;
	if (fstat(fd, &statbuf)) {
		perror("stat error");
		exit(EXIT_FAILURE);
	}
	ret = statbuf.st_size / 512;
	if (S_ISBLK(statbuf.st_mode))
		if (ioctl(fd, BLKGETSIZE, &ret)) {
			perror("ioctl error");
			exit(EXIT_FAILURE);
		}
	return ret;
}

int main(int argc, char **argv)
{
	bool walk = false, randsize = false, verbose = false, csum = false;
	int fd1, fd2 = 0, direct = 0, nbytes = 4096, j;
	unsigned long size, i, offset = 0;
	void *buf1 = NULL, *buf2 = NULL;
	uint32_t *csums = NULL;

	if (argc < 3) {
		printf("Please enter a cache device and raw device\n");
		exit(EXIT_FAILURE);
	}

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "direct") == 0)
			direct = O_DIRECT;
		else if (strcmp(argv[i], "walk") == 0)
			walk = true;
		else if (strcmp(argv[i], "verbose") == 0)
			verbose = true;
		else if (strcmp(argv[i], "size") == 0)
			randsize = true;
		else if (strcmp(argv[i], "csum") == 0)
			csum= true;
		else
			break;
	}

	fd1 = open(argv[i], O_RDONLY|direct);
	size = getblocks(fd1);

	if (!csum) {
		fd2 = open(argv[2], O_RDONLY|direct);
		size = MIN(size, getblocks(fd2));
	} else
		csums = calloc((size / 8 + 1), sizeof(*csums));

	if (fd1 == -1 || fd2 == -1) {
		perror("Error opening device");
		exit(EXIT_FAILURE);
	}

	size = size / 8 - 16;
	printf("size %li\n", size);

	if (posix_memalign(&buf1, 4096, 4096 * 16) ||
	    posix_memalign(&buf2, 4096, 4096 * 16)) {
		printf("Could not allocate buffers\n");
		exit(EXIT_FAILURE);
	}
	setvbuf(stdout, NULL, _IONBF, 0);

	for (i = 0;; i++) {
		if (randsize)
			nbytes = 4096 * (int) (drand48() * 16 + 1);

		offset += walk ? normal() * 100 : random();
		offset %= size;
		assert(offset < size);

		if (verbose)
			printf("Loop %li offset %li sectors %i\n",
			       i, offset << 3, nbytes >> 9);
		else if (!(i % 100))
			printf("Loop %li\n", i);

		Pread(fd1, buf1, nbytes, offset << 12);

		if (!csum) {
			Pread(fd2, buf2, nbytes, offset << 12);

			for (j = 0; j < nbytes; j += 512)
				if (memcmp(buf1 + j,
					   buf2 + j,
					   512)) {
					printf("Bad read! loop %li offset %li sectors %i, sector %i\n",
					       i, offset << 3, nbytes >> 9, j >> 9);
					exit(EXIT_FAILURE);
				}
		} else
			for (j = 0; j < nbytes / 4096; j++) {
				int c = fletcher32(buf1 + j * 4096, 4096);
				if (!csums[offset + j])
					csums[offset + j] = c;
				else if (csums[offset + j] != c) {
					printf("Bad read! loop %li offset %li sectors %i, sector %i\n",
					       i, offset << 3, nbytes >> 9, j << 3);
					exit(EXIT_FAILURE);
				}
			}
	}
err:
	perror("Read error");
	exit(EXIT_FAILURE);
}