1 /* gcc -g -Wall -O2 aiodio_sparse.c -o aiodio_sparse -laio */
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)
12 #include <sys/types.h>
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
33 unsigned char *check_zero(unsigned char *buf, int size)
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);
47 fprintf(stderr, "buf %p, p %p\n", buf, p);
53 return 0; /* all zeros */
56 volatile int got_signal;
59 sig_term_func(int i, siginfo_t *si, void *p)
62 fprintf(stderr, "sig(%d, %p, %p)\n", i, si, p);
67 * do async DIO writes to a sparse file
69 void aiodio_sparse(char *filename, int align, int writesize, int startoffset, int filesize,
70 int num_aio, int step, int sparse, int direct, int keep)
76 static struct sigaction s;
80 struct io_event event;
83 s.sa_sigaction = sig_term_func;
84 s.sa_flags = SA_SIGINFO;
85 sigaction(SIGTERM, &s, 0);
87 if ((num_aio * step) > filesize) {
88 num_aio = filesize / step;
90 memset(&myctx, 0, sizeof(myctx));
91 io_queue_init(num_aio, &myctx);
93 iocbs = (struct iocb **)calloc(num_aio, sizeof(struct iocb *));
94 for (i = 0; i < num_aio; i++) {
95 if ((iocbs[i] = (struct iocb *)calloc(1, sizeof(struct iocb))) == 0) {
96 perror("cannot malloc iocb");
101 fd = open(filename, direct|O_WRONLY|O_CREAT, 0666);
104 perror("cannot create file");
109 ftruncate(fd, filesize);
112 * allocate the iocbs array and iocbs with buffers
114 offset = startoffset;
115 for (i = 0; i < num_aio; i++) {
118 if (posix_memalign(&bufptr, align, writesize)) {
119 perror("cannot malloc aligned memory");
124 memset(bufptr, 1, writesize);
125 io_prep_pwrite(iocbs[i], fd, bufptr, writesize, offset);
130 * start the 1st num_aio write requests
132 if ((w = io_submit(myctx, num_aio, iocbs)) < 0) {
133 perror("io_submit failed");
139 fprintf(stderr, "io_submit() return %d\n", w);
142 * As AIO requests finish, keep issuing more AIO until done.
144 aio_inflight = num_aio;
146 fprintf(stderr, "aiodio_sparse: %d i/o in flight\n", aio_inflight);
147 while (offset < filesize) {
152 fprintf(stderr, "aiodio_sparse: offset %lld filesize %d inflight %d\n",
153 (long long)offset, filesize, aio_inflight);
155 if ((n = io_getevents(myctx, 1, 1, &event, 0)) != 1) {
157 fprintf(stderr, "io_getevents() returned %d\n", n);
161 fprintf(stderr, "aiodio_sparse: io_getevent() returned %d\n", n);
164 break; /* told to stop */
166 * check if write succeeded.
169 if (event.res2 != 0 || event.res != iocbp->u.c.nbytes) {
171 "AIO write offset %lld expected %ld got %ld\n",
172 iocbp->u.c.offset, iocbp->u.c.nbytes,
177 fprintf(stderr, "aiodio_sparse: io_getevent() res %ld res2 %ld\n",
178 event.res, event.res2);
180 /* start next write */
181 io_prep_pwrite(iocbp, fd, iocbp->u.c.buf, writesize, offset);
183 if ((w = io_submit(myctx, 1, &iocbp)) < 0) {
184 fprintf(stderr, "io_submit failed at offset %lld\n",
190 fprintf(stderr, "io_submit() return %d\n", w);
195 * wait for AIO requests in flight.
197 while (aio_inflight > 0) {
201 if ((n = io_getevents(myctx, 1, 1, &event, 0)) != 1) {
202 perror("io_getevents failed");
207 * check if write succeeded.
210 if (event.res2 != 0 || event.res != iocbp->u.c.nbytes) {
212 "AIO write offset %lld expected %ld got %ld\n",
213 iocbp->u.c.offset, iocbp->u.c.nbytes,
218 fprintf(stderr, "AIO DIO write done\n");
220 if ((fd = open(filename, O_RDONLY)) < 0)
223 bufptr = malloc(writesize);
224 for (offset = startoffset; offset < filesize; offset += step) {
225 unsigned char *badbuf;
228 fprintf(stderr, "seek to %ld and read %d\n", offset, writesize);
229 lseek(fd, offset, SEEK_SET);
230 if (read(fd, bufptr, writesize) < writesize) {
231 fprintf(stderr, "short read() at offset %lld\n",
235 if ((badbuf = check_zero(bufptr, writesize))) {
236 fprintf(stderr, "non-one read at offset %lld\n",
237 (long long)(offset + badbuf - (unsigned char*)bufptr));
238 fprintf(stderr, "*** WARNING *** %s has not been unlinked; if you don't rm it manually first, it may influence the next run\n", filename);
246 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);
249 void dirty_freeblocks(char *filename, int size)
254 char filename2[PATH_MAX];
257 size = ((size + pg - 1) / pg) * pg;
258 sprintf(filename2, "%s.xx.%d", filename, getpid());
259 fd = open(filename2, O_CREAT|O_RDWR, 0666);
261 perror("cannot open file");
265 p = mmap(0, size, PROT_WRITE|PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
266 if (p == MAP_FAILED) {
267 perror("cannot mmap");
270 memset(p, 0xaa, size);
271 msync(p, size, MS_SYNC);
279 fprintf(stderr, "usage: dio_sparse [-n step] [-s filesize]"
280 " [-w writesize] [-o startoffset] [-r readsize] filename\n");
285 * Scale value by kilo, mega, or giga.
287 long long scale_by_kmg(long long value, char scale)
311 * aiodio_sparse [-r readsize] [-w writesize] [-o startoffset] [-n step] [-a align] [-i num_aio] filename
314 int main(int argc, char **argv)
316 char filename[PATH_MAX];
317 long alignment = 512;
318 int readsize = 65536;
319 int writesize = 65536;
321 int filesize = 100*1024*1024;
323 int step = 5*1024*1024;
324 int c, direct = O_DIRECT, keep = 0, sparse = 1;
326 extern int optind, optopt, opterr;
328 while ((c = getopt(argc, argv, "dr:w:n:o:a:s:i:DkS")) != -1) {
344 num_aio = atoi(optarg);
347 alignment = strtol(optarg, &endp, 0);
348 alignment = (int)scale_by_kmg((long long)alignment,
352 readsize = strtol(optarg, &endp, 0);
353 readsize = (int)scale_by_kmg((long long)readsize, *endp);
356 writesize = strtol(optarg, &endp, 0);
357 writesize = (int)scale_by_kmg((long long)writesize, *endp);
360 filesize = strtol(optarg, &endp, 0);
361 filesize = (int)scale_by_kmg((long long)filesize, *endp);
364 step = strtol(optarg, &endp, 0);
365 step = (int)scale_by_kmg((long long)step, *endp);
368 startoffset = strtol(optarg, &endp, 0);
369 startoffset = (int)scale_by_kmg((long long)startoffset, *endp);
377 strncpy(filename, argv[argc-1], PATH_MAX);
380 * Create some dirty free blocks by allocating, writing, syncing,
381 * and then unlinking and freeing.
383 dirty_freeblocks(filename, filesize);
386 * Parent write to a hole in a file using async direct i/o
389 aiodio_sparse(filename, alignment, writesize, startoffset, filesize, num_aio, step, sparse, direct, keep);