]> git.sesse.net Git - bcachefs-tools-debian/blob - bcache-test.c
0f8ad3761bdf8868266765e301c8faed5f670f66
[bcachefs-tools-debian] / bcache-test.c
1 #define _FILE_OFFSET_BITS       64
2 #define _XOPEN_SOURCE 500
3 #define _GNU_SOURCE
4
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <linux/fs.h>
8 #include <math.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/ioctl.h>
14 #include <sys/klog.h>
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <time.h>
21
22 #include <openssl/rc4.h>
23 #include <openssl/md4.h>
24
25 static const unsigned char bcache_magic[] = {
26         0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
27         0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 };
28
29 unsigned char zero[4096];
30
31 bool klog = false;
32
33 #define Pread(fd, buf, size, offset) do {                               \
34         int _read = 0, _r;                                              \
35         while (_read < size) {                                          \
36                 _r = pread(fd, buf, (size) - _read, (offset) + _read);  \
37                 if (_r <= 0)                                            \
38                         goto err;                                       \
39                 _read += _r;                                            \
40         }                                                               \
41 } while (0)
42
43 #define Pwrite(fd, buf, size, offset) do {                              \
44         int _write = 0, _r;                                             \
45         while (_write < size) {                                         \
46                 _r = pwrite(fd, buf, (size) - _write, offset + _write); \
47                 if (_r < 0)                                             \
48                         goto err;                                       \
49                 _write += _r;                                           \
50         }                                                               \
51 } while (0)
52
53 /* Marsaglia polar method
54  */
55 double normal()
56 {
57         double x, y, s;
58         static double n = 0 / (double) 0;
59
60         if (n == n) {
61                 x = n;
62                 n = 0 / (double) 0;
63                 return x;
64         }
65
66         do {
67                 x = random() / (double) (RAND_MAX / 2) - 1;
68                 y = random() / (double) (RAND_MAX / 2) - 1;
69
70                 s = x * x + y * y;
71         } while (s >= 1);
72
73         s = sqrt(-2 * log(s) / s);
74         n =     y * s;
75         return  x * s;
76 }
77
78 long getblocks(int fd)
79 {
80         long ret;
81         struct stat statbuf;
82         if (fstat(fd, &statbuf)) {
83                 perror("stat error");
84                 exit(EXIT_FAILURE);
85         }
86         ret = statbuf.st_size / 512;
87         if (S_ISBLK(statbuf.st_mode))
88                 if (ioctl(fd, BLKGETSIZE, &ret)) {
89                         perror("ioctl error");
90                         exit(EXIT_FAILURE);
91                 }
92         return ret;
93 }
94
95 struct pagestuff {
96         unsigned char csum[16];
97         unsigned char oldcsum[16];
98         int readcount;
99         int writecount;
100 };
101
102 void flushlog(void)
103 {
104         char logbuf[1 << 21];
105         int w = 0, len;
106         static int fd;
107
108         if (!klog)
109                 return;
110
111         if (!fd) {
112                 klogctl(8, 0, 6);
113
114                 sprintf(logbuf, "log.%i", abs(random()) % 1000);
115                 fd = open(logbuf, O_WRONLY|O_CREAT|O_TRUNC, 0644);
116
117                 if (fd == -1) {
118                         perror("Error opening log file");
119                         exit(EXIT_FAILURE);
120                 }
121         }
122
123         len = klogctl(4, logbuf, 1 << 21);
124
125         if (len == -1) {
126                 perror("Error reading kernel log");
127                 exit(EXIT_FAILURE);
128         }
129
130         while (w < len) {
131                 int r = write(fd, logbuf + w, len - w);
132                 if (r == -1) {
133                         perror("Error writing log");
134                         exit(EXIT_FAILURE);
135                 }
136                 w += r;
137         }
138 }
139
140 void aio_loop(int nr)
141 {
142
143 }
144
145 void usage()
146 {
147         exit(EXIT_FAILURE);
148 }
149
150 int main(int argc, char **argv)
151 {
152         bool walk = false, randsize = false, verbose = false, csum = false, rtest = false, wtest = false;
153         int fd1, fd2 = 0, direct = 0, nbytes = 4096, j, o;
154         unsigned long size, i, offset = 0, done = 0, unique = 0, benchmark = 0;
155         void *buf1 = NULL, *buf2 = NULL;
156         struct pagestuff *pages, *p;
157         unsigned char c[16];
158         time_t last_printed = 0;
159         extern char *optarg;
160
161         RC4_KEY writedata;
162         RC4_set_key(&writedata, 16, bcache_magic);
163
164         while ((o = getopt(argc, argv, "dnwvscwlb:")) != EOF)
165                 switch (o) {
166                 case 'd':
167                         direct = O_DIRECT;
168                         break;
169                 case 'n':
170                         walk = true;
171                         break;
172                 case 'v':
173                         verbose = true;
174                         break;
175                 case 's':
176                         randsize = true;
177                         break;
178                 case 'c':
179                         csum = true;
180                         break;
181                 case 'w':
182                         wtest = true;
183                         break;
184                 case 'r':
185                         rtest = true;
186                         break;
187                 case 'l':
188                         klog = true;
189                         break;
190                 case 'b':
191                         benchmark = atol(optarg);
192                         break;
193                 default:
194                         usage();
195                 }
196
197         argv += optind;
198         argc -= optind;
199
200         if (!rtest && !wtest)
201                 rtest = true;
202
203         if (argc < 1) {
204                 printf("Please enter a device to test\n");
205                 exit(EXIT_FAILURE);
206         }
207
208         if (!csum && !benchmark && argc < 2) {
209                 printf("Please enter a device to compare against\n");
210                 exit(EXIT_FAILURE);
211         }
212
213         fd1 = open(argv[0], (wtest ? O_RDWR : O_RDONLY)|direct);
214         if (!csum && !benchmark)
215                 fd2 = open(argv[1], (wtest ? O_RDWR : O_RDONLY)|direct);
216
217         if (fd1 == -1 || fd2 == -1) {
218                 perror("Error opening device");
219                 exit(EXIT_FAILURE);
220         }
221
222         size = getblocks(fd1);
223         if (!csum && !benchmark)
224                 size = MIN(size, getblocks(fd2));
225
226         size = size / 8 - 16;
227         pages = calloc(size + 16, sizeof(*pages));
228         printf("size %li\n", size);
229
230         if (posix_memalign(&buf1, 4096, 4096 * 16) ||
231             posix_memalign(&buf2, 4096, 4096 * 16)) {
232                 printf("Could not allocate buffers\n");
233                 exit(EXIT_FAILURE);
234         }
235         //setvbuf(stdout, NULL, _IONBF, 0);
236
237         for (i = 0; !benchmark || i < benchmark; i++) {
238                 bool writing = (wtest && (i & 1)) || !rtest;
239                 nbytes = randsize ? drand48() * 16 + 1 : 1;
240                 nbytes <<= 12;
241
242                 offset >>= 12;
243                 offset += walk ? normal() * 20 : random();
244                 offset %= size;
245                 offset <<= 12;
246
247                 if (!(i % 200))
248                         flushlog();
249
250                 if (!verbose) {
251                         time_t now = time(NULL);
252                         if (now - last_printed >= 2) {
253                                 last_printed = now;
254                                 goto print;
255                         }
256                 } else
257 print:                  printf("Loop %6li offset %9li sectors %3i, %6lu mb done, %6lu mb unique\n",
258                                i, offset >> 9, nbytes >> 9, done >> 11, unique >> 11);
259
260                 done += nbytes >> 9;
261
262                 if (!writing)
263                         Pread(fd1, buf1, nbytes, offset);
264                 if (!writing && !csum && !benchmark)
265                         Pread(fd2, buf2, nbytes, offset);
266
267                 for (j = 0; j < nbytes; j += 4096) {
268                         p = &pages[(offset + j) / 4096];
269
270                         if (writing)
271                                 RC4(&writedata, 4096, zero, buf1 + j);
272
273                         if (csum) {
274                                 MD4(buf1 + j, 4096, &c[0]);
275
276                                 if (writing ||
277                                     (!p->readcount && !p->writecount)) {
278                                         memcpy(&p->oldcsum[0], &p->csum[0], 16);
279                                         memcpy(&p->csum[0], c, 16);
280                                 } else if (memcmp(&p->csum[0], c, 16))
281                                         goto bad;
282                         } else if (!writing && !benchmark &&
283                                    memcmp(buf1 + j,
284                                           buf2 + j,
285                                           4096))
286                                 goto bad;
287
288                         if (!p->writecount && !p->readcount)
289                                 unique += 8;
290
291                         writing ? p->writecount++ : p->readcount++;
292                 }
293                 if (writing)
294                         Pwrite(fd1, buf1, nbytes, offset);
295                 if (writing && !csum && !benchmark)
296                         Pwrite(fd2, buf2, nbytes, offset);
297         }
298         printf("Loop %6li offset %9li sectors %3i, %6lu mb done, %6lu mb unique\n",
299                i, offset >> 9, nbytes >> 9, done >> 11, unique >> 11);
300         exit(EXIT_SUCCESS);
301 err:
302         perror("IO error");
303         flushlog();
304         exit(EXIT_FAILURE);
305 bad:
306         printf("Bad read! loop %li offset %li readcount %i writecount %i\n",
307                i, (offset + j) >> 9, p->readcount, p->writecount);
308
309         if (!memcmp(&p->oldcsum[0], c, 16))
310                 printf("Matches previous csum\n");
311
312         flushlog();
313         exit(EXIT_FAILURE);
314 }