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 filesize, int num_aio, int step, int sparse, int direct, int keep)
75 static struct sigaction s;
79 struct io_event event;
82 s.sa_sigaction = sig_term_func;
83 s.sa_flags = SA_SIGINFO;
84 sigaction(SIGTERM, &s, 0);
86 if ((num_aio * step) > filesize) {
87 num_aio = filesize / step;
89 memset(&myctx, 0, sizeof(myctx));
90 io_queue_init(num_aio, &myctx);
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");
100 fd = open(filename, direct|O_WRONLY|O_CREAT, 0666);
103 perror("cannot create file");
108 ftruncate(fd, filesize);
111 * allocate the iocbs array and iocbs with buffers
114 for (i = 0; i < num_aio; i++) {
117 if (posix_memalign(&bufptr, align, writesize)) {
118 perror("cannot malloc aligned memory");
123 memset(bufptr, 1, writesize);
124 io_prep_pwrite(iocbs[i], fd, bufptr, writesize, offset);
129 * start the 1st num_aio write requests
131 if ((w = io_submit(myctx, num_aio, iocbs)) < 0) {
132 perror("io_submit failed");
138 fprintf(stderr, "io_submit() return %d\n", w);
141 * As AIO requests finish, keep issuing more AIO until done.
143 aio_inflight = num_aio;
145 fprintf(stderr, "aiodio_sparse: %d i/o in flight\n", aio_inflight);
146 while (offset < filesize) {
151 fprintf(stderr, "aiodio_sparse: offset %lld filesize %d inflight %d\n",
152 (long long)offset, filesize, aio_inflight);
154 if ((n = io_getevents(myctx, 1, 1, &event, 0)) != 1) {
156 fprintf(stderr, "io_getevents() returned %d\n", n);
160 fprintf(stderr, "aiodio_sparse: io_getevent() returned %d\n", n);
163 break; /* told to stop */
165 * check if write succeeded.
168 if (event.res2 != 0 || event.res != iocbp->u.c.nbytes) {
170 "AIO write offset %lld expected %ld got %ld\n",
171 iocbp->u.c.offset, iocbp->u.c.nbytes,
176 fprintf(stderr, "aiodio_sparse: io_getevent() res %ld res2 %ld\n",
177 event.res, event.res2);
179 /* start next write */
180 io_prep_pwrite(iocbp, fd, iocbp->u.c.buf, writesize, offset);
182 if ((w = io_submit(myctx, 1, &iocbp)) < 0) {
183 fprintf(stderr, "io_submit failed at offset %lld\n",
189 fprintf(stderr, "io_submit() return %d\n", w);
194 * wait for AIO requests in flight.
196 while (aio_inflight > 0) {
200 if ((n = io_getevents(myctx, 1, 1, &event, 0)) != 1) {
201 perror("io_getevents failed");
206 * check if write succeeded.
209 if (event.res2 != 0 || event.res != iocbp->u.c.nbytes) {
211 "AIO write offset %lld expected %ld got %ld\n",
212 iocbp->u.c.offset, iocbp->u.c.nbytes,
217 fprintf(stderr, "AIO DIO write done\n");
219 if ((fd = open(filename, O_RDONLY)) < 0)
222 bufptr = malloc(writesize);
223 for (offset = 0; offset < filesize; offset += step) {
224 unsigned char *badbuf;
226 lseek(fd, offset, SEEK_SET);
227 if (read(fd, bufptr, writesize) < writesize) {
228 fprintf(stderr, "short read() at offset %lld\n",
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);
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);
246 void dirty_freeblocks(char *filename, int size)
251 char filename2[PATH_MAX];
254 size = ((size + pg - 1) / pg) * pg;
255 sprintf(filename2, "%s.xx.%d", filename, getpid());
256 fd = open(filename2, O_CREAT|O_RDWR, 0666);
258 perror("cannot open file");
262 p = mmap(0, size, PROT_WRITE|PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
263 if (p == MAP_FAILED) {
264 perror("cannot mmap");
267 memset(p, 0xaa, size);
268 msync(p, size, MS_SYNC);
276 fprintf(stderr, "usage: dio_sparse [-n step] [-s filesize]"
277 " [-w writesize] [-r readsize] filename\n");
282 * Scale value by kilo, mega, or giga.
284 long long scale_by_kmg(long long value, char scale)
308 * aiodio_sparse [-r readsize] [-w writesize] [-n step] [-a align] [-i num_aio] filename
311 int main(int argc, char **argv)
313 char filename[PATH_MAX];
314 long alignment = 512;
315 int readsize = 65536;
316 int writesize = 65536;
317 int filesize = 100*1024*1024;
319 int step = 5*1024*1024;
320 int c, direct = O_DIRECT, keep = 0, sparse = 1;
322 extern int optind, optopt, opterr;
324 while ((c = getopt(argc, argv, "dr:w:n:a:s:i:DkS")) != -1) {
340 num_aio = atoi(optarg);
343 alignment = strtol(optarg, &endp, 0);
344 alignment = (int)scale_by_kmg((long long)alignment,
348 readsize = strtol(optarg, &endp, 0);
349 readsize = (int)scale_by_kmg((long long)readsize, *endp);
352 writesize = strtol(optarg, &endp, 0);
353 writesize = (int)scale_by_kmg((long long)writesize, *endp);
356 filesize = strtol(optarg, &endp, 0);
357 filesize = (int)scale_by_kmg((long long)filesize, *endp);
360 step = strtol(optarg, &endp, 0);
361 step = (int)scale_by_kmg((long long)step, *endp);
369 strncpy(filename, argv[argc-1], PATH_MAX);
372 * Create some dirty free blocks by allocating, writing, syncing,
373 * and then unlinking and freeing.
375 dirty_freeblocks(filename, filesize);
378 * Parent write to a hole in a file using async direct i/o
381 aiodio_sparse(filename, alignment, writesize, filesize, num_aio, step, sparse, direct, keep);