configure xen in a single place
[qemu/aliguori-queue.git] / qemu-io.c
blob6c35a071c4d09536926572a2aa66c0be5001c8b7
1 /*
2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10 #include <sys/types.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13 #include <getopt.h>
15 #include "qemu-common.h"
16 #include "block_int.h"
17 #include "cmd.h"
19 #define VERSION "0.0.1"
21 #define CMD_NOFILE_OK 0x01
23 char *progname;
24 static BlockDriverState *bs;
26 static int misalign;
29 * Memory allocation helpers.
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len, int pattern)
38 void *buf;
40 if (misalign)
41 len += MISALIGN_OFFSET;
42 buf = qemu_memalign(512, len);
43 memset(buf, pattern, len);
44 if (misalign)
45 buf += MISALIGN_OFFSET;
46 return buf;
49 static void qemu_io_free(void *p)
51 if (misalign)
52 p -= MISALIGN_OFFSET;
53 qemu_vfree(p);
56 static void
57 dump_buffer(const void *buffer, int64_t offset, int len)
59 int i, j;
60 const uint8_t *p;
62 for (i = 0, p = buffer; i < len; i += 16) {
63 const uint8_t *s = p;
65 printf("%08llx: ", (unsigned long long)offset + i);
66 for (j = 0; j < 16 && i + j < len; j++, p++)
67 printf("%02x ", *p);
68 printf(" ");
69 for (j = 0; j < 16 && i + j < len; j++, s++) {
70 if (isalnum(*s))
71 printf("%c", *s);
72 else
73 printf(".");
75 printf("\n");
79 static void
80 print_report(const char *op, struct timeval *t, int64_t offset,
81 int count, int total, int cnt, int Cflag)
83 char s1[64], s2[64], ts[64];
85 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
86 if (!Cflag) {
87 cvtstr((double)total, s1, sizeof(s1));
88 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
89 printf("%s %d/%d bytes at offset %lld\n",
90 op, total, count, (long long)offset);
91 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
92 s1, cnt, ts, s2, tdiv((double)cnt, *t));
93 } else {/* bytes,ops,time,bytes/sec,ops/sec */
94 printf("%d,%d,%s,%.3f,%.3f\n",
95 total, cnt, ts,
96 tdiv((double)total, *t),
97 tdiv((double)cnt, *t));
102 * Parse multiple length statements for vectored I/O, and construct an I/O
103 * vector matching it.
105 static void *
106 create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
108 size_t *sizes = calloc(nr_iov, sizeof(size_t));
109 size_t count = 0;
110 void *buf, *p;
111 int i;
113 for (i = 0; i < nr_iov; i++) {
114 char *arg = argv[i];
115 long long len;
117 len = cvtnum(arg);
118 if (len < 0) {
119 printf("non-numeric length argument -- %s\n", arg);
120 return NULL;
123 /* should be SIZE_T_MAX, but that doesn't exist */
124 if (len > UINT_MAX) {
125 printf("too large length argument -- %s\n", arg);
126 return NULL;
129 if (len & 0x1ff) {
130 printf("length argument %lld is not sector aligned\n",
131 len);
132 return NULL;
135 sizes[i] = len;
136 count += len;
139 qemu_iovec_init(qiov, nr_iov);
141 buf = p = qemu_io_alloc(count, pattern);
143 for (i = 0; i < nr_iov; i++) {
144 qemu_iovec_add(qiov, p, sizes[i]);
145 p += sizes[i];
148 free(sizes);
149 return buf;
152 static int do_read(char *buf, int64_t offset, int count, int *total)
154 int ret;
156 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
157 if (ret < 0)
158 return ret;
159 *total = count;
160 return 1;
163 static int do_write(char *buf, int64_t offset, int count, int *total)
165 int ret;
167 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
168 if (ret < 0)
169 return ret;
170 *total = count;
171 return 1;
174 static int do_pread(char *buf, int64_t offset, int count, int *total)
176 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
177 if (*total < 0)
178 return *total;
179 return 1;
182 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
184 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
185 if (*total < 0)
186 return *total;
187 return 1;
190 static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
192 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
193 if (*total < 0)
194 return *total;
195 return 1;
198 static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
200 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
201 if (*total < 0)
202 return *total;
203 return 1;
206 #define NOT_DONE 0x7fffffff
207 static void aio_rw_done(void *opaque, int ret)
209 *(int *)opaque = ret;
212 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
214 BlockDriverAIOCB *acb;
215 int async_ret = NOT_DONE;
217 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
218 aio_rw_done, &async_ret);
219 if (!acb)
220 return -EIO;
222 while (async_ret == NOT_DONE)
223 qemu_aio_wait();
225 *total = qiov->size;
226 return async_ret < 0 ? async_ret : 1;
229 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
231 BlockDriverAIOCB *acb;
232 int async_ret = NOT_DONE;
234 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
235 aio_rw_done, &async_ret);
236 if (!acb)
237 return -EIO;
239 while (async_ret == NOT_DONE)
240 qemu_aio_wait();
242 *total = qiov->size;
243 return async_ret < 0 ? async_ret : 1;
247 static const cmdinfo_t read_cmd;
249 static void
250 read_help(void)
252 printf(
253 "\n"
254 " reads a range of bytes from the given offset\n"
255 "\n"
256 " Example:\n"
257 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
258 "\n"
259 " Reads a segment of the currently open file, optionally dumping it to the\n"
260 " standard output stream (with -v option) for subsequent inspection.\n"
261 " -b, -- read from the VM state rather than the virtual disk\n"
262 " -C, -- report statistics in a machine parsable format\n"
263 " -l, -- length for pattern verification (only with -P)\n"
264 " -p, -- use bdrv_pread to read the file\n"
265 " -P, -- use a pattern to verify read data\n"
266 " -q, -- quite mode, do not show I/O statistics\n"
267 " -s, -- start offset for pattern verification (only with -P)\n"
268 " -v, -- dump buffer to standard output\n"
269 "\n");
272 static int
273 read_f(int argc, char **argv)
275 struct timeval t1, t2;
276 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
277 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
278 int c, cnt;
279 char *buf;
280 int64_t offset;
281 int count;
282 /* Some compilers get confused and warn if this is not initialized. */
283 int total = 0;
284 int pattern = 0, pattern_offset = 0, pattern_count = 0;
286 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
287 switch (c) {
288 case 'b':
289 bflag = 1;
290 break;
291 case 'C':
292 Cflag = 1;
293 break;
294 case 'l':
295 lflag = 1;
296 pattern_count = cvtnum(optarg);
297 if (pattern_count < 0) {
298 printf("non-numeric length argument -- %s\n", optarg);
299 return 0;
301 break;
302 case 'p':
303 pflag = 1;
304 break;
305 case 'P':
306 Pflag = 1;
307 pattern = atoi(optarg);
308 break;
309 case 'q':
310 qflag = 1;
311 break;
312 case 's':
313 sflag = 1;
314 pattern_offset = cvtnum(optarg);
315 if (pattern_offset < 0) {
316 printf("non-numeric length argument -- %s\n", optarg);
317 return 0;
319 break;
320 case 'v':
321 vflag = 1;
322 break;
323 default:
324 return command_usage(&read_cmd);
328 if (optind != argc - 2)
329 return command_usage(&read_cmd);
331 if (bflag && pflag) {
332 printf("-b and -p cannot be specified at the same time\n");
333 return 0;
336 offset = cvtnum(argv[optind]);
337 if (offset < 0) {
338 printf("non-numeric length argument -- %s\n", argv[optind]);
339 return 0;
342 optind++;
343 count = cvtnum(argv[optind]);
344 if (count < 0) {
345 printf("non-numeric length argument -- %s\n", argv[optind]);
346 return 0;
349 if (!Pflag && (lflag || sflag)) {
350 return command_usage(&read_cmd);
353 if (!lflag) {
354 pattern_count = count - pattern_offset;
357 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
358 printf("pattern verfication range exceeds end of read data\n");
359 return 0;
362 if (!pflag)
363 if (offset & 0x1ff) {
364 printf("offset %lld is not sector aligned\n",
365 (long long)offset);
366 return 0;
368 if (count & 0x1ff) {
369 printf("count %d is not sector aligned\n",
370 count);
371 return 0;
375 buf = qemu_io_alloc(count, 0xab);
377 gettimeofday(&t1, NULL);
378 if (pflag)
379 cnt = do_pread(buf, offset, count, &total);
380 else if (bflag)
381 cnt = do_load_vmstate(buf, offset, count, &total);
382 else
383 cnt = do_read(buf, offset, count, &total);
384 gettimeofday(&t2, NULL);
386 if (cnt < 0) {
387 printf("read failed: %s\n", strerror(-cnt));
388 goto out;
391 if (Pflag) {
392 void* cmp_buf = malloc(pattern_count);
393 memset(cmp_buf, pattern, pattern_count);
394 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
395 printf("Pattern verification failed at offset %lld, "
396 "%d bytes\n",
397 (long long) offset + pattern_offset, pattern_count);
399 free(cmp_buf);
402 if (qflag)
403 goto out;
405 if (vflag)
406 dump_buffer(buf, offset, count);
408 /* Finally, report back -- -C gives a parsable format */
409 t2 = tsub(t2, t1);
410 print_report("read", &t2, offset, count, total, cnt, Cflag);
412 out:
413 qemu_io_free(buf);
415 return 0;
418 static const cmdinfo_t read_cmd = {
419 .name = "read",
420 .altname = "r",
421 .cfunc = read_f,
422 .argmin = 2,
423 .argmax = -1,
424 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
425 .oneline = "reads a number of bytes at a specified offset",
426 .help = read_help,
429 static const cmdinfo_t readv_cmd;
431 static void
432 readv_help(void)
434 printf(
435 "\n"
436 " reads a range of bytes from the given offset into multiple buffers\n"
437 "\n"
438 " Example:\n"
439 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
440 "\n"
441 " Reads a segment of the currently open file, optionally dumping it to the\n"
442 " standard output stream (with -v option) for subsequent inspection.\n"
443 " Uses multiple iovec buffers if more than one byte range is specified.\n"
444 " -C, -- report statistics in a machine parsable format\n"
445 " -P, -- use a pattern to verify read data\n"
446 " -v, -- dump buffer to standard output\n"
447 " -q, -- quite mode, do not show I/O statistics\n"
448 "\n");
451 static int
452 readv_f(int argc, char **argv)
454 struct timeval t1, t2;
455 int Cflag = 0, qflag = 0, vflag = 0;
456 int c, cnt;
457 char *buf;
458 int64_t offset;
459 int total;
460 int nr_iov;
461 QEMUIOVector qiov;
462 int pattern = 0;
463 int Pflag = 0;
465 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
466 switch (c) {
467 case 'C':
468 Cflag = 1;
469 break;
470 case 'P':
471 Pflag = 1;
472 pattern = atoi(optarg);
473 break;
474 case 'q':
475 qflag = 1;
476 break;
477 case 'v':
478 vflag = 1;
479 break;
480 default:
481 return command_usage(&readv_cmd);
485 if (optind > argc - 2)
486 return command_usage(&readv_cmd);
489 offset = cvtnum(argv[optind]);
490 if (offset < 0) {
491 printf("non-numeric length argument -- %s\n", argv[optind]);
492 return 0;
494 optind++;
496 if (offset & 0x1ff) {
497 printf("offset %lld is not sector aligned\n",
498 (long long)offset);
499 return 0;
502 nr_iov = argc - optind;
503 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
505 gettimeofday(&t1, NULL);
506 cnt = do_aio_readv(&qiov, offset, &total);
507 gettimeofday(&t2, NULL);
509 if (cnt < 0) {
510 printf("readv failed: %s\n", strerror(-cnt));
511 goto out;
514 if (Pflag) {
515 void* cmp_buf = malloc(qiov.size);
516 memset(cmp_buf, pattern, qiov.size);
517 if (memcmp(buf, cmp_buf, qiov.size)) {
518 printf("Pattern verification failed at offset %lld, "
519 "%zd bytes\n",
520 (long long) offset, qiov.size);
522 free(cmp_buf);
525 if (qflag)
526 goto out;
528 if (vflag)
529 dump_buffer(buf, offset, qiov.size);
531 /* Finally, report back -- -C gives a parsable format */
532 t2 = tsub(t2, t1);
533 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
535 out:
536 qemu_io_free(buf);
537 return 0;
540 static const cmdinfo_t readv_cmd = {
541 .name = "readv",
542 .cfunc = readv_f,
543 .argmin = 2,
544 .argmax = -1,
545 .args = "[-Cqv] [-P pattern ] off len [len..]",
546 .oneline = "reads a number of bytes at a specified offset",
547 .help = readv_help,
550 static const cmdinfo_t write_cmd;
552 static void
553 write_help(void)
555 printf(
556 "\n"
557 " writes a range of bytes from the given offset\n"
558 "\n"
559 " Example:\n"
560 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
561 "\n"
562 " Writes into a segment of the currently open file, using a buffer\n"
563 " filled with a set pattern (0xcdcdcdcd).\n"
564 " -b, -- write to the VM state rather than the virtual disk\n"
565 " -p, -- use bdrv_pwrite to write the file\n"
566 " -P, -- use different pattern to fill file\n"
567 " -C, -- report statistics in a machine parsable format\n"
568 " -q, -- quite mode, do not show I/O statistics\n"
569 "\n");
572 static int
573 write_f(int argc, char **argv)
575 struct timeval t1, t2;
576 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
577 int c, cnt;
578 char *buf;
579 int64_t offset;
580 int count;
581 /* Some compilers get confused and warn if this is not initialized. */
582 int total = 0;
583 int pattern = 0xcd;
585 while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
586 switch (c) {
587 case 'b':
588 bflag = 1;
589 break;
590 case 'C':
591 Cflag = 1;
592 break;
593 case 'p':
594 pflag = 1;
595 break;
596 case 'P':
597 pattern = atoi(optarg);
598 break;
599 case 'q':
600 qflag = 1;
601 break;
602 default:
603 return command_usage(&write_cmd);
607 if (optind != argc - 2)
608 return command_usage(&write_cmd);
610 if (bflag && pflag) {
611 printf("-b and -p cannot be specified at the same time\n");
612 return 0;
615 offset = cvtnum(argv[optind]);
616 if (offset < 0) {
617 printf("non-numeric length argument -- %s\n", argv[optind]);
618 return 0;
621 optind++;
622 count = cvtnum(argv[optind]);
623 if (count < 0) {
624 printf("non-numeric length argument -- %s\n", argv[optind]);
625 return 0;
628 if (!pflag) {
629 if (offset & 0x1ff) {
630 printf("offset %lld is not sector aligned\n",
631 (long long)offset);
632 return 0;
635 if (count & 0x1ff) {
636 printf("count %d is not sector aligned\n",
637 count);
638 return 0;
642 buf = qemu_io_alloc(count, pattern);
644 gettimeofday(&t1, NULL);
645 if (pflag)
646 cnt = do_pwrite(buf, offset, count, &total);
647 else if (bflag)
648 cnt = do_save_vmstate(buf, offset, count, &total);
649 else
650 cnt = do_write(buf, offset, count, &total);
651 gettimeofday(&t2, NULL);
653 if (cnt < 0) {
654 printf("write failed: %s\n", strerror(-cnt));
655 goto out;
658 if (qflag)
659 goto out;
661 /* Finally, report back -- -C gives a parsable format */
662 t2 = tsub(t2, t1);
663 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
665 out:
666 qemu_io_free(buf);
668 return 0;
671 static const cmdinfo_t write_cmd = {
672 .name = "write",
673 .altname = "w",
674 .cfunc = write_f,
675 .argmin = 2,
676 .argmax = -1,
677 .args = "[-abCpq] [-P pattern ] off len",
678 .oneline = "writes a number of bytes at a specified offset",
679 .help = write_help,
682 static const cmdinfo_t writev_cmd;
684 static void
685 writev_help(void)
687 printf(
688 "\n"
689 " writes a range of bytes from the given offset source from multiple buffers\n"
690 "\n"
691 " Example:\n"
692 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
693 "\n"
694 " Writes into a segment of the currently open file, using a buffer\n"
695 " filled with a set pattern (0xcdcdcdcd).\n"
696 " -P, -- use different pattern to fill file\n"
697 " -C, -- report statistics in a machine parsable format\n"
698 " -q, -- quite mode, do not show I/O statistics\n"
699 "\n");
702 static int
703 writev_f(int argc, char **argv)
705 struct timeval t1, t2;
706 int Cflag = 0, qflag = 0;
707 int c, cnt;
708 char *buf;
709 int64_t offset;
710 int total;
711 int nr_iov;
712 int pattern = 0xcd;
713 QEMUIOVector qiov;
715 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
716 switch (c) {
717 case 'C':
718 Cflag = 1;
719 break;
720 case 'q':
721 qflag = 1;
722 break;
723 case 'P':
724 pattern = atoi(optarg);
725 break;
726 default:
727 return command_usage(&writev_cmd);
731 if (optind > argc - 2)
732 return command_usage(&writev_cmd);
734 offset = cvtnum(argv[optind]);
735 if (offset < 0) {
736 printf("non-numeric length argument -- %s\n", argv[optind]);
737 return 0;
739 optind++;
741 if (offset & 0x1ff) {
742 printf("offset %lld is not sector aligned\n",
743 (long long)offset);
744 return 0;
747 nr_iov = argc - optind;
748 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
750 gettimeofday(&t1, NULL);
751 cnt = do_aio_writev(&qiov, offset, &total);
752 gettimeofday(&t2, NULL);
754 if (cnt < 0) {
755 printf("writev failed: %s\n", strerror(-cnt));
756 goto out;
759 if (qflag)
760 goto out;
762 /* Finally, report back -- -C gives a parsable format */
763 t2 = tsub(t2, t1);
764 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
765 out:
766 qemu_io_free(buf);
767 return 0;
770 static const cmdinfo_t writev_cmd = {
771 .name = "writev",
772 .cfunc = writev_f,
773 .argmin = 2,
774 .argmax = -1,
775 .args = "[-Cq] [-P pattern ] off len [len..]",
776 .oneline = "writes a number of bytes at a specified offset",
777 .help = writev_help,
780 struct aio_ctx {
781 QEMUIOVector qiov;
782 int64_t offset;
783 char *buf;
784 int qflag;
785 int vflag;
786 int Cflag;
787 int Pflag;
788 int pattern;
789 struct timeval t1;
792 static void
793 aio_write_done(void *opaque, int ret)
795 struct aio_ctx *ctx = opaque;
796 struct timeval t2;
798 gettimeofday(&t2, NULL);
801 if (ret < 0) {
802 printf("aio_write failed: %s\n", strerror(-ret));
803 goto out;
806 if (ctx->qflag) {
807 goto out;
810 /* Finally, report back -- -C gives a parsable format */
811 t2 = tsub(t2, ctx->t1);
812 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
813 ctx->qiov.size, 1, ctx->Cflag);
814 out:
815 qemu_io_free(ctx->buf);
816 free(ctx);
819 static const cmdinfo_t aio_read_cmd;
821 static void
822 aio_read_done(void *opaque, int ret)
824 struct aio_ctx *ctx = opaque;
825 struct timeval t2;
827 gettimeofday(&t2, NULL);
829 if (ret < 0) {
830 printf("readv failed: %s\n", strerror(-ret));
831 goto out;
834 if (ctx->Pflag) {
835 void *cmp_buf = malloc(ctx->qiov.size);
837 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
838 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
839 printf("Pattern verification failed at offset %lld, "
840 "%zd bytes\n",
841 (long long) ctx->offset, ctx->qiov.size);
843 free(cmp_buf);
846 if (ctx->qflag) {
847 goto out;
850 if (ctx->vflag) {
851 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
854 /* Finally, report back -- -C gives a parsable format */
855 t2 = tsub(t2, ctx->t1);
856 print_report("read", &t2, ctx->offset, ctx->qiov.size,
857 ctx->qiov.size, 1, ctx->Cflag);
858 out:
859 qemu_io_free(ctx->buf);
860 free(ctx);
863 static void
864 aio_read_help(void)
866 printf(
867 "\n"
868 " asynchronously reads a range of bytes from the given offset\n"
869 "\n"
870 " Example:\n"
871 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
872 "\n"
873 " Reads a segment of the currently open file, optionally dumping it to the\n"
874 " standard output stream (with -v option) for subsequent inspection.\n"
875 " The read is performed asynchronously and should the aio_flush command \n"
876 " should be used to ensure all outstanding aio requests have been completed\n"
877 " -C, -- report statistics in a machine parsable format\n"
878 " -P, -- use a pattern to verify read data\n"
879 " -v, -- dump buffer to standard output\n"
880 " -q, -- quite mode, do not show I/O statistics\n"
881 "\n");
884 static int
885 aio_read_f(int argc, char **argv)
887 int nr_iov, c;
888 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
889 BlockDriverAIOCB *acb;
891 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
892 switch (c) {
893 case 'C':
894 ctx->Cflag = 1;
895 break;
896 case 'P':
897 ctx->Pflag = 1;
898 ctx->pattern = atoi(optarg);
899 break;
900 case 'q':
901 ctx->qflag = 1;
902 break;
903 case 'v':
904 ctx->vflag = 1;
905 break;
906 default:
907 free(ctx);
908 return command_usage(&aio_read_cmd);
912 if (optind > argc - 2) {
913 free(ctx);
914 return command_usage(&aio_read_cmd);
917 ctx->offset = cvtnum(argv[optind]);
918 if (ctx->offset < 0) {
919 printf("non-numeric length argument -- %s\n", argv[optind]);
920 free(ctx);
921 return 0;
923 optind++;
925 if (ctx->offset & 0x1ff) {
926 printf("offset %lld is not sector aligned\n",
927 (long long)ctx->offset);
928 free(ctx);
929 return 0;
932 nr_iov = argc - optind;
933 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
935 gettimeofday(&ctx->t1, NULL);
936 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
937 ctx->qiov.size >> 9, aio_read_done, ctx);
938 if (!acb) {
939 free(ctx->buf);
940 free(ctx);
941 return -EIO;
944 return 0;
947 static const cmdinfo_t aio_read_cmd = {
948 .name = "aio_read",
949 .cfunc = aio_read_f,
950 .argmin = 2,
951 .argmax = -1,
952 .args = "[-Cqv] [-P pattern ] off len [len..]",
953 .oneline = "asynchronously reads a number of bytes",
954 .help = aio_read_help,
957 static const cmdinfo_t aio_write_cmd;
959 static void
960 aio_write_help(void)
962 printf(
963 "\n"
964 " asynchronously writes a range of bytes from the given offset source \n"
965 " from multiple buffers\n"
966 "\n"
967 " Example:\n"
968 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
969 "\n"
970 " Writes into a segment of the currently open file, using a buffer\n"
971 " filled with a set pattern (0xcdcdcdcd).\n"
972 " The write is performed asynchronously and should the aio_flush command \n"
973 " should be used to ensure all outstanding aio requests have been completed\n"
974 " -P, -- use different pattern to fill file\n"
975 " -C, -- report statistics in a machine parsable format\n"
976 " -q, -- quite mode, do not show I/O statistics\n"
977 "\n");
981 static int
982 aio_write_f(int argc, char **argv)
984 int nr_iov, c;
985 int pattern = 0xcd;
986 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
987 BlockDriverAIOCB *acb;
989 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
990 switch (c) {
991 case 'C':
992 ctx->Cflag = 1;
993 break;
994 case 'q':
995 ctx->qflag = 1;
996 break;
997 case 'P':
998 pattern = atoi(optarg);
999 break;
1000 default:
1001 free(ctx);
1002 return command_usage(&aio_write_cmd);
1006 if (optind > argc - 2) {
1007 free(ctx);
1008 return command_usage(&aio_write_cmd);
1011 ctx->offset = cvtnum(argv[optind]);
1012 if (ctx->offset < 0) {
1013 printf("non-numeric length argument -- %s\n", argv[optind]);
1014 free(ctx);
1015 return 0;
1017 optind++;
1019 if (ctx->offset & 0x1ff) {
1020 printf("offset %lld is not sector aligned\n",
1021 (long long)ctx->offset);
1022 free(ctx);
1023 return 0;
1026 nr_iov = argc - optind;
1027 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
1029 gettimeofday(&ctx->t1, NULL);
1030 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1031 ctx->qiov.size >> 9, aio_write_done, ctx);
1032 if (!acb) {
1033 free(ctx->buf);
1034 free(ctx);
1035 return -EIO;
1038 return 0;
1041 static const cmdinfo_t aio_write_cmd = {
1042 .name = "aio_write",
1043 .cfunc = aio_write_f,
1044 .argmin = 2,
1045 .argmax = -1,
1046 .args = "[-Cq] [-P pattern ] off len [len..]",
1047 .oneline = "asynchronously writes a number of bytes",
1048 .help = aio_write_help,
1051 static int
1052 aio_flush_f(int argc, char **argv)
1054 qemu_aio_flush();
1055 return 0;
1058 static const cmdinfo_t aio_flush_cmd = {
1059 .name = "aio_flush",
1060 .cfunc = aio_flush_f,
1061 .oneline = "completes all outstanding aio requets"
1064 static int
1065 flush_f(int argc, char **argv)
1067 bdrv_flush(bs);
1068 return 0;
1071 static const cmdinfo_t flush_cmd = {
1072 .name = "flush",
1073 .altname = "f",
1074 .cfunc = flush_f,
1075 .oneline = "flush all in-core file state to disk",
1078 static int
1079 truncate_f(int argc, char **argv)
1081 int64_t offset;
1082 int ret;
1084 offset = cvtnum(argv[1]);
1085 if (offset < 0) {
1086 printf("non-numeric truncate argument -- %s\n", argv[1]);
1087 return 0;
1090 ret = bdrv_truncate(bs, offset);
1091 if (ret < 0) {
1092 printf("truncate: %s", strerror(ret));
1093 return 0;
1096 return 0;
1099 static const cmdinfo_t truncate_cmd = {
1100 .name = "truncate",
1101 .altname = "t",
1102 .cfunc = truncate_f,
1103 .argmin = 1,
1104 .argmax = 1,
1105 .args = "off",
1106 .oneline = "truncates the current file at the given offset",
1109 static int
1110 length_f(int argc, char **argv)
1112 int64_t size;
1113 char s1[64];
1115 size = bdrv_getlength(bs);
1116 if (size < 0) {
1117 printf("getlength: %s", strerror(size));
1118 return 0;
1121 cvtstr(size, s1, sizeof(s1));
1122 printf("%s\n", s1);
1123 return 0;
1127 static const cmdinfo_t length_cmd = {
1128 .name = "length",
1129 .altname = "l",
1130 .cfunc = length_f,
1131 .oneline = "gets the length of the current file",
1135 static int
1136 info_f(int argc, char **argv)
1138 BlockDriverInfo bdi;
1139 char s1[64], s2[64];
1140 int ret;
1142 if (bs->drv && bs->drv->format_name)
1143 printf("format name: %s\n", bs->drv->format_name);
1144 if (bs->drv && bs->drv->protocol_name)
1145 printf("format name: %s\n", bs->drv->protocol_name);
1147 ret = bdrv_get_info(bs, &bdi);
1148 if (ret)
1149 return 0;
1151 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1152 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1154 printf("cluster size: %s\n", s1);
1155 printf("vm state offset: %s\n", s2);
1157 return 0;
1162 static const cmdinfo_t info_cmd = {
1163 .name = "info",
1164 .altname = "i",
1165 .cfunc = info_f,
1166 .oneline = "prints information about the current file",
1169 static int
1170 alloc_f(int argc, char **argv)
1172 int64_t offset;
1173 int nb_sectors;
1174 char s1[64];
1175 int num;
1176 int ret;
1177 const char *retstr;
1179 offset = cvtnum(argv[1]);
1180 if (offset & 0x1ff) {
1181 printf("offset %lld is not sector aligned\n",
1182 (long long)offset);
1183 return 0;
1186 if (argc == 3)
1187 nb_sectors = cvtnum(argv[2]);
1188 else
1189 nb_sectors = 1;
1191 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1193 cvtstr(offset, s1, sizeof(s1));
1195 retstr = ret ? "allocated" : "not allocated";
1196 if (nb_sectors == 1)
1197 printf("sector %s at offset %s\n", retstr, s1);
1198 else
1199 printf("%d/%d sectors %s at offset %s\n",
1200 num, nb_sectors, retstr, s1);
1201 return 0;
1204 static const cmdinfo_t alloc_cmd = {
1205 .name = "alloc",
1206 .altname = "a",
1207 .argmin = 1,
1208 .argmax = 2,
1209 .cfunc = alloc_f,
1210 .args = "off [sectors]",
1211 .oneline = "checks if a sector is present in the file",
1214 static int
1215 close_f(int argc, char **argv)
1217 bdrv_close(bs);
1218 bs = NULL;
1219 return 0;
1222 static const cmdinfo_t close_cmd = {
1223 .name = "close",
1224 .altname = "c",
1225 .cfunc = close_f,
1226 .oneline = "close the current open file",
1229 static int openfile(char *name, int flags, int growable)
1231 if (bs) {
1232 fprintf(stderr, "file open already, try 'help close'\n");
1233 return 1;
1236 bs = bdrv_new("hda");
1237 if (!bs)
1238 return 1;
1240 if (growable) {
1241 flags |= BDRV_O_FILE;
1244 if (bdrv_open(bs, name, flags) == -1) {
1245 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1246 bs = NULL;
1247 return 1;
1250 if (growable) {
1251 bs->growable = 1;
1253 return 0;
1256 static void
1257 open_help(void)
1259 printf(
1260 "\n"
1261 " opens a new file in the requested mode\n"
1262 "\n"
1263 " Example:\n"
1264 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1265 "\n"
1266 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1267 " -C, -- create new file if it doesn't exist\n"
1268 " -r, -- open file read-only\n"
1269 " -s, -- use snapshot file\n"
1270 " -n, -- disable host cache\n"
1271 " -g, -- allow file to grow (only applies to protocols)"
1272 "\n");
1275 static const cmdinfo_t open_cmd;
1277 static int
1278 open_f(int argc, char **argv)
1280 int flags = 0;
1281 int readonly = 0;
1282 int growable = 0;
1283 int c;
1285 while ((c = getopt(argc, argv, "snCrg")) != EOF) {
1286 switch (c) {
1287 case 's':
1288 flags |= BDRV_O_SNAPSHOT;
1289 break;
1290 case 'n':
1291 flags |= BDRV_O_NOCACHE;
1292 break;
1293 case 'C':
1294 flags |= BDRV_O_CREAT;
1295 break;
1296 case 'r':
1297 readonly = 1;
1298 break;
1299 case 'g':
1300 growable = 1;
1301 break;
1302 default:
1303 return command_usage(&open_cmd);
1307 if (readonly)
1308 flags |= BDRV_O_RDONLY;
1309 else
1310 flags |= BDRV_O_RDWR;
1312 if (optind != argc - 1)
1313 return command_usage(&open_cmd);
1315 return openfile(argv[optind], flags, growable);
1318 static const cmdinfo_t open_cmd = {
1319 .name = "open",
1320 .altname = "o",
1321 .cfunc = open_f,
1322 .argmin = 1,
1323 .argmax = -1,
1324 .flags = CMD_NOFILE_OK,
1325 .args = "[-Crsn] [path]",
1326 .oneline = "open the file specified by path",
1327 .help = open_help,
1330 static int
1331 init_args_command(
1332 int index)
1334 /* only one device allowed so far */
1335 if (index >= 1)
1336 return 0;
1337 return ++index;
1340 static int
1341 init_check_command(
1342 const cmdinfo_t *ct)
1344 if (ct->flags & CMD_FLAG_GLOBAL)
1345 return 1;
1346 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1347 fprintf(stderr, "no file open, try 'help open'\n");
1348 return 0;
1350 return 1;
1353 static void usage(const char *name)
1355 printf(
1356 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1357 "QEMU Disk exerciser\n"
1358 "\n"
1359 " -C, --create create new file if it doesn't exist\n"
1360 " -c, --cmd command to execute\n"
1361 " -r, --read-only export read-only\n"
1362 " -s, --snapshot use snapshot file\n"
1363 " -n, --nocache disable host cache\n"
1364 " -g, --growable allow file to grow (only applies to protocols)\n"
1365 " -m, --misalign misalign allocations for O_DIRECT\n"
1366 " -h, --help display this help and exit\n"
1367 " -V, --version output version information and exit\n"
1368 "\n",
1369 name);
1373 int main(int argc, char **argv)
1375 int readonly = 0;
1376 int growable = 0;
1377 const char *sopt = "hVc:Crsnmg";
1378 struct option lopt[] = {
1379 { "help", 0, 0, 'h' },
1380 { "version", 0, 0, 'V' },
1381 { "offset", 1, 0, 'o' },
1382 { "cmd", 1, 0, 'c' },
1383 { "create", 0, 0, 'C' },
1384 { "read-only", 0, 0, 'r' },
1385 { "snapshot", 0, 0, 's' },
1386 { "nocache", 0, 0, 'n' },
1387 { "misalign", 0, 0, 'm' },
1388 { "growable", 0, 0, 'g' },
1389 { NULL, 0, 0, 0 }
1391 int c;
1392 int opt_index = 0;
1393 int flags = 0;
1395 progname = basename(argv[0]);
1397 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1398 switch (c) {
1399 case 's':
1400 flags |= BDRV_O_SNAPSHOT;
1401 break;
1402 case 'n':
1403 flags |= BDRV_O_NOCACHE;
1404 break;
1405 case 'c':
1406 add_user_command(optarg);
1407 break;
1408 case 'C':
1409 flags |= BDRV_O_CREAT;
1410 break;
1411 case 'r':
1412 readonly = 1;
1413 break;
1414 case 'm':
1415 misalign = 1;
1416 break;
1417 case 'g':
1418 growable = 1;
1419 break;
1420 case 'V':
1421 printf("%s version %s\n", progname, VERSION);
1422 exit(0);
1423 case 'h':
1424 usage(progname);
1425 exit(0);
1426 default:
1427 usage(progname);
1428 exit(1);
1432 if ((argc - optind) > 1) {
1433 usage(progname);
1434 exit(1);
1437 bdrv_init();
1439 /* initialize commands */
1440 quit_init();
1441 help_init();
1442 add_command(&open_cmd);
1443 add_command(&close_cmd);
1444 add_command(&read_cmd);
1445 add_command(&readv_cmd);
1446 add_command(&write_cmd);
1447 add_command(&writev_cmd);
1448 add_command(&aio_read_cmd);
1449 add_command(&aio_write_cmd);
1450 add_command(&aio_flush_cmd);
1451 add_command(&flush_cmd);
1452 add_command(&truncate_cmd);
1453 add_command(&length_cmd);
1454 add_command(&info_cmd);
1455 add_command(&alloc_cmd);
1457 add_args_command(init_args_command);
1458 add_check_command(init_check_command);
1460 /* open the device */
1461 if (readonly)
1462 flags |= BDRV_O_RDONLY;
1463 else
1464 flags |= BDRV_O_RDWR;
1466 if ((argc - optind) == 1)
1467 openfile(argv[optind], flags, growable);
1468 command_loop();
1471 * Make sure all outstanding requests get flushed the program exits.
1473 qemu_aio_flush();
1475 if (bs)
1476 bdrv_close(bs);
1477 return 0;