b56f91a5d8b60e9b0b16fc16584edd6608026bb8
[xfstests-dev.git] / src / aio-dio-regress / aiodio_sparse2.c
1 /* gcc -g -Wall -O2 aiodio_sparse.c -o aiodio_sparse -laio */
2
3 /*
4  * From http://developer.osdl.org/daniel/AIO/TESTS/aiodio_sparse.c
5  * With patch from https://bugzilla.redhat.com/attachment.cgi?id=142124
6  * (Bug https://bugzilla.redhat.com/show_bug.cgi?id=217098)
7  */
8
9 #ifndef _GNU_SOURCE
10 #define _GNU_SOURCE
11 #endif
12 #include <sys/types.h>
13 #include <errno.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <limits.h>
20 #include <sys/mman.h>
21 #include <sys/wait.h>
22
23 #include <libaio.h>
24
25 int debug;
26
27 /*
28  * aiodio_sparse - issue async O_DIRECT writes to holes is a file while
29  *      concurrently reading the file and checking that the read never reads
30  *      uninitailized data.
31  */
32
33 unsigned char *check_zero(unsigned char *buf, int size)
34 {
35         unsigned char *p;
36
37         p = buf;
38
39         while (size > 0) {
40                 if (*buf != 1) {
41                         fprintf(stderr, "non one buffer at buf[%ld] => 0x%02x,%02x,%02x,%02x\n",
42                                 (long)(buf - p), (unsigned int)buf[0],
43                                 size > 1 ? (unsigned int)buf[1] : 0,
44                                 size > 2 ? (unsigned int)buf[2] : 0,
45                                 size > 3 ? (unsigned int)buf[3] : 0);
46                         if (debug)
47                                 fprintf(stderr, "buf %p, p %p\n", buf, p);
48                         return buf;
49                 }
50                 buf++;
51                 size--;
52         }
53         return 0;       /* all zeros */
54 }
55
56 volatile int got_signal;
57
58 void
59 sig_term_func(int i, siginfo_t *si, void *p)
60 {
61         if (debug)
62                 fprintf(stderr, "sig(%d, %p, %p)\n", i, si, p);
63         got_signal++;
64 }
65
66 /*
67  * do async DIO writes to a sparse file
68  */
69 void aiodio_sparse(char *filename, int align, int writesize, int filesize, int num_aio, int step, int sparse, int direct, int keep)
70 {
71         int fd;
72         void *bufptr;
73         int i;
74         int w;
75         static struct sigaction s;
76         struct iocb **iocbs;
77         off_t offset;
78         io_context_t myctx;
79         struct io_event event;
80         int aio_inflight;
81
82         s.sa_sigaction = sig_term_func;
83         s.sa_flags = SA_SIGINFO;
84         sigaction(SIGTERM, &s, 0);
85
86         if ((num_aio * step) > filesize) {
87                 num_aio = filesize / step;
88         }
89         memset(&myctx, 0, sizeof(myctx));
90         io_queue_init(num_aio, &myctx);
91
92         iocbs = (struct iocb **)calloc(num_aio, sizeof(struct iocb *));
93         for (i = 0; i < num_aio; i++) {
94                 if ((iocbs[i] = (struct iocb *)calloc(1, sizeof(struct iocb))) == 0) {
95                         perror("cannot malloc iocb");
96                         return;
97                 }
98         }
99
100         fd = open(filename, direct|O_WRONLY|O_CREAT, 0666);
101
102         if (fd < 0) {
103                 perror("cannot create file");
104                 return;
105         }
106
107         if (sparse)
108                 ftruncate(fd, filesize);
109
110         /*
111          * allocate the iocbs array and iocbs with buffers
112          */
113         offset = 0;
114         for (i = 0; i < num_aio; i++) {
115                 void *bufptr;
116
117                 if (posix_memalign(&bufptr, align, writesize)) {
118                         perror("cannot malloc aligned memory");
119                         close(fd);
120                         unlink(filename);
121                         return;
122                 }
123                 memset(bufptr, 1, writesize);
124                 io_prep_pwrite(iocbs[i], fd, bufptr, writesize, offset);
125                 offset += step;
126         }
127
128         /*
129          * start the 1st num_aio write requests
130          */
131         if ((w = io_submit(myctx, num_aio, iocbs)) < 0) {
132                 perror("io_submit failed");
133                 close(fd);
134                 unlink(filename);
135                 return;
136         }
137         if (debug)
138                 fprintf(stderr, "io_submit() return %d\n", w);
139
140         /*
141          * As AIO requests finish, keep issuing more AIO until done.
142          */
143         aio_inflight = num_aio;
144         if (debug)
145                 fprintf(stderr, "aiodio_sparse: %d i/o in flight\n", aio_inflight);
146         while (offset < filesize)  {
147                 int n;
148                 struct iocb *iocbp;
149
150                 if (debug)
151                         fprintf(stderr, "aiodio_sparse: offset %lld filesize %d inflight %d\n",
152                                 (long long)offset, filesize, aio_inflight);
153
154                 if ((n = io_getevents(myctx, 1, 1, &event, 0)) != 1) {
155                         if (-n != EINTR)
156                                 fprintf(stderr, "io_getevents() returned %d\n", n);
157                         break;
158                 }
159                 if (debug)
160                         fprintf(stderr, "aiodio_sparse: io_getevent() returned %d\n", n);
161                 aio_inflight--;
162                 if (got_signal)
163                         break;          /* told to stop */
164                 /*
165                  * check if write succeeded.
166                  */
167                 iocbp = event.obj;
168                 if (event.res2 != 0 || event.res != iocbp->u.c.nbytes) {
169                         fprintf(stderr,
170                                 "AIO write offset %lld expected %ld got %ld\n",
171                                 iocbp->u.c.offset, iocbp->u.c.nbytes,
172                                 event.res);
173                         break;
174                 }
175                 if (debug)
176                         fprintf(stderr, "aiodio_sparse: io_getevent() res %ld res2 %ld\n",
177                                 event.res, event.res2);
178
179                 /* start next write */
180                 io_prep_pwrite(iocbp, fd, iocbp->u.c.buf, writesize, offset);
181                 offset += step;
182                 if ((w = io_submit(myctx, 1, &iocbp)) < 0) {
183                         fprintf(stderr, "io_submit failed at offset %lld\n",
184                                 (long long)offset);
185                         perror("");
186                         break;
187                 }
188                 if (debug)
189                         fprintf(stderr, "io_submit() return %d\n", w);
190                 aio_inflight++;
191         }
192
193         /*
194          * wait for AIO requests in flight.
195          */
196         while (aio_inflight > 0) {
197                 int n;
198                 struct iocb *iocbp;
199
200                 if ((n = io_getevents(myctx, 1, 1, &event, 0)) != 1) {
201                         perror("io_getevents failed");
202                         break;
203                 }
204                 aio_inflight--;
205                 /*
206                  * check if write succeeded.
207                  */
208                 iocbp = event.obj;
209                 if (event.res2 != 0 || event.res != iocbp->u.c.nbytes) {
210                         fprintf(stderr,
211                                 "AIO write offset %lld expected %ld got %ld\n",
212                                 iocbp->u.c.offset, iocbp->u.c.nbytes,
213                                 event.res);
214                 }
215         }
216         if (debug)
217                 fprintf(stderr, "AIO DIO write done\n");
218         close(fd);
219         if ((fd = open(filename, O_RDONLY)) < 0)
220                 exit(1);
221
222         bufptr = malloc(writesize);
223         for (offset = 0; offset < filesize; offset += step)  {
224                 unsigned char *badbuf;
225
226                 lseek(fd, offset, SEEK_SET);
227                 if (read(fd, bufptr, writesize) < writesize) {
228                         fprintf(stderr, "short read() at offset %lld\n",
229                                 (long long) offset);
230                         exit(11);
231                 }
232                 if ((badbuf = check_zero(bufptr, writesize))) {
233                         fprintf(stderr, "non-one read at offset %lld\n",
234                                 (long long)(offset + badbuf - (unsigned char*)bufptr));
235                         fprintf(stderr, "*** WARNING *** %s has not been unlinked; if you don't rm it manually first, it may influence the next run\n", filename);
236                         exit(10);
237                 }
238         }
239         close(fd);
240         if (!keep)
241                 unlink(filename);
242         else
243                 fprintf(stderr, "*** WARNING *** You requested %s not to be unlinked; if you don't rm it manually first, it may influence the next run\n", filename);
244 }
245
246 void dirty_freeblocks(char *filename, int size)
247 {
248         int fd;
249         void *p;
250         int pg;
251         char filename2[PATH_MAX];
252
253         pg = getpagesize();
254         size = ((size + pg - 1) / pg) * pg;
255         sprintf(filename2, "%s.xx.%d", filename, getpid());
256         fd = open(filename2, O_CREAT|O_RDWR, 0666);
257         if (fd < 0) {
258                 perror("cannot open file");
259                 exit(2);
260         }
261         ftruncate(fd, size);
262         p = mmap(0, size, PROT_WRITE|PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
263         if (p == MAP_FAILED) {
264                 perror("cannot mmap");
265                 exit(2);
266         }
267         memset(p, 0xaa, size);
268         msync(p, size, MS_SYNC);
269         munmap(p, size);
270         close(fd);
271         unlink(filename2);
272 }
273
274 void usage()
275 {
276         fprintf(stderr, "usage: dio_sparse [-n step] [-s filesize]"
277                 " [-w writesize] [-r readsize] filename\n");
278         exit(1);
279 }
280
281 /*
282  * Scale value by kilo, mega, or giga.
283  */
284 long long scale_by_kmg(long long value, char scale)
285 {
286         switch (scale) {
287         case 'g':
288         case 'G':
289                 value *= 1024;
290         case 'm':
291         case 'M':
292                 value *= 1024;
293         case 'k':
294         case 'K':
295                 value *= 1024;
296                 break;
297         case '\0':
298                 break;
299         default:
300                 usage();
301                 break;
302         }
303         return value;
304 }
305
306 /*
307  *      usage:
308  * aiodio_sparse [-r readsize] [-w writesize] [-n step] [-a align] [-i num_aio] filename
309  */
310
311 int main(int argc, char **argv)
312 {
313         char filename[PATH_MAX];
314         long alignment = 512;
315         int readsize = 65536;
316         int writesize = 65536;
317         int filesize = 100*1024*1024;
318         int num_aio = 16;
319         int step = 5*1024*1024;
320         int c, direct = O_DIRECT, keep = 0, sparse = 1;
321         extern char *optarg;
322         extern int optind, optopt, opterr;
323
324         while ((c = getopt(argc, argv, "dr:w:n:a:s:i:DkS")) != -1) {
325                 char *endp;
326                 switch (c) {
327                 case 'D':
328                         direct = 0;
329                         break;
330                 case 'k':
331                         keep = 1;
332                         break;
333                 case 'S':
334                         sparse = 0;
335                         break;
336                 case 'd':
337                         debug++;
338                         break;
339                 case 'i':
340                         num_aio = atoi(optarg);
341                         break;
342                 case 'a':
343                         alignment = strtol(optarg, &endp, 0);
344                         alignment = (int)scale_by_kmg((long long)alignment,
345                                                         *endp);
346                         break;
347                 case 'r':
348                         readsize = strtol(optarg, &endp, 0);
349                         readsize = (int)scale_by_kmg((long long)readsize, *endp);
350                         break;
351                 case 'w':
352                         writesize = strtol(optarg, &endp, 0);
353                         writesize = (int)scale_by_kmg((long long)writesize, *endp);
354                         break;
355                 case 's':
356                         filesize = strtol(optarg, &endp, 0);
357                         filesize = (int)scale_by_kmg((long long)filesize, *endp);
358                         break;
359                 case 'n':
360                         step = strtol(optarg, &endp, 0);
361                         step = (int)scale_by_kmg((long long)step, *endp);
362                         break;
363                 case '?':
364                         usage();
365                         break;
366                 }
367         }
368
369         strncpy(filename, argv[argc-1], PATH_MAX);
370
371         /*
372          * Create some dirty free blocks by allocating, writing, syncing,
373          * and then unlinking and freeing.
374          */
375         dirty_freeblocks(filename, filesize);
376
377         /*
378          * Parent write to a hole in a file using async direct i/o
379          */
380
381         aiodio_sparse(filename, alignment, writesize, filesize, num_aio, step, sparse, direct, keep);
382
383         return 0;
384 }