s390: typo fix
[qemu/agraf.git] / qemu-io.c
blob25c282ec0979624d30a08ecb20cc4c5260798da4
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/time.h>
11 #include <sys/types.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <libgen.h>
17 #include "qemu-common.h"
18 #include "block_int.h"
19 #include "cmd.h"
21 #define VERSION "0.0.1"
23 #define CMD_NOFILE_OK 0x01
25 char *progname;
26 static BlockDriverState *bs;
28 static int misalign;
31 * Parse the pattern argument to various sub-commands.
33 * Because the pattern is used as an argument to memset it must evaluate
34 * to an unsigned integer that fits into a single byte.
36 static int parse_pattern(const char *arg)
38 char *endptr = NULL;
39 long pattern;
41 pattern = strtol(arg, &endptr, 0);
42 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
43 printf("%s is not a valid pattern byte\n", arg);
44 return -1;
47 return pattern;
51 * Memory allocation helpers.
53 * Make sure memory is aligned by default, or purposefully misaligned if
54 * that is specified on the command line.
57 #define MISALIGN_OFFSET 16
58 static void *qemu_io_alloc(size_t len, int pattern)
60 void *buf;
62 if (misalign)
63 len += MISALIGN_OFFSET;
64 buf = qemu_memalign(512, len);
65 memset(buf, pattern, len);
66 if (misalign)
67 buf += MISALIGN_OFFSET;
68 return buf;
71 static void qemu_io_free(void *p)
73 if (misalign)
74 p -= MISALIGN_OFFSET;
75 qemu_vfree(p);
78 static void
79 dump_buffer(const void *buffer, int64_t offset, int len)
81 int i, j;
82 const uint8_t *p;
84 for (i = 0, p = buffer; i < len; i += 16) {
85 const uint8_t *s = p;
87 printf("%08llx: ", (unsigned long long)offset + i);
88 for (j = 0; j < 16 && i + j < len; j++, p++)
89 printf("%02x ", *p);
90 printf(" ");
91 for (j = 0; j < 16 && i + j < len; j++, s++) {
92 if (isalnum(*s))
93 printf("%c", *s);
94 else
95 printf(".");
97 printf("\n");
101 static void
102 print_report(const char *op, struct timeval *t, int64_t offset,
103 int count, int total, int cnt, int Cflag)
105 char s1[64], s2[64], ts[64];
107 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
108 if (!Cflag) {
109 cvtstr((double)total, s1, sizeof(s1));
110 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
111 printf("%s %d/%d bytes at offset %lld\n",
112 op, total, count, (long long)offset);
113 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
114 s1, cnt, ts, s2, tdiv((double)cnt, *t));
115 } else {/* bytes,ops,time,bytes/sec,ops/sec */
116 printf("%d,%d,%s,%.3f,%.3f\n",
117 total, cnt, ts,
118 tdiv((double)total, *t),
119 tdiv((double)cnt, *t));
124 * Parse multiple length statements for vectored I/O, and construct an I/O
125 * vector matching it.
127 static void *
128 create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
130 size_t *sizes = calloc(nr_iov, sizeof(size_t));
131 size_t count = 0;
132 void *buf, *p;
133 int i;
135 for (i = 0; i < nr_iov; i++) {
136 char *arg = argv[i];
137 long long len;
139 len = cvtnum(arg);
140 if (len < 0) {
141 printf("non-numeric length argument -- %s\n", arg);
142 return NULL;
145 /* should be SIZE_T_MAX, but that doesn't exist */
146 if (len > UINT_MAX) {
147 printf("too large length argument -- %s\n", arg);
148 return NULL;
151 if (len & 0x1ff) {
152 printf("length argument %lld is not sector aligned\n",
153 len);
154 return NULL;
157 sizes[i] = len;
158 count += len;
161 qemu_iovec_init(qiov, nr_iov);
163 buf = p = qemu_io_alloc(count, pattern);
165 for (i = 0; i < nr_iov; i++) {
166 qemu_iovec_add(qiov, p, sizes[i]);
167 p += sizes[i];
170 free(sizes);
171 return buf;
174 static int do_read(char *buf, int64_t offset, int count, int *total)
176 int ret;
178 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
179 if (ret < 0)
180 return ret;
181 *total = count;
182 return 1;
185 static int do_write(char *buf, int64_t offset, int count, int *total)
187 int ret;
189 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
190 if (ret < 0)
191 return ret;
192 *total = count;
193 return 1;
196 static int do_pread(char *buf, int64_t offset, int count, int *total)
198 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
199 if (*total < 0)
200 return *total;
201 return 1;
204 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
206 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
207 if (*total < 0)
208 return *total;
209 return 1;
212 static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
214 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
215 if (*total < 0)
216 return *total;
217 return 1;
220 static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
222 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
223 if (*total < 0)
224 return *total;
225 return 1;
228 #define NOT_DONE 0x7fffffff
229 static void aio_rw_done(void *opaque, int ret)
231 *(int *)opaque = ret;
234 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
236 BlockDriverAIOCB *acb;
237 int async_ret = NOT_DONE;
239 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
240 aio_rw_done, &async_ret);
241 if (!acb)
242 return -EIO;
244 while (async_ret == NOT_DONE)
245 qemu_aio_wait();
247 *total = qiov->size;
248 return async_ret < 0 ? async_ret : 1;
251 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
253 BlockDriverAIOCB *acb;
254 int async_ret = NOT_DONE;
256 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
257 aio_rw_done, &async_ret);
258 if (!acb)
259 return -EIO;
261 while (async_ret == NOT_DONE)
262 qemu_aio_wait();
264 *total = qiov->size;
265 return async_ret < 0 ? async_ret : 1;
269 static void
270 read_help(void)
272 printf(
273 "\n"
274 " reads a range of bytes from the given offset\n"
275 "\n"
276 " Example:\n"
277 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
278 "\n"
279 " Reads a segment of the currently open file, optionally dumping it to the\n"
280 " standard output stream (with -v option) for subsequent inspection.\n"
281 " -b, -- read from the VM state rather than the virtual disk\n"
282 " -C, -- report statistics in a machine parsable format\n"
283 " -l, -- length for pattern verification (only with -P)\n"
284 " -p, -- use bdrv_pread to read the file\n"
285 " -P, -- use a pattern to verify read data\n"
286 " -q, -- quite mode, do not show I/O statistics\n"
287 " -s, -- start offset for pattern verification (only with -P)\n"
288 " -v, -- dump buffer to standard output\n"
289 "\n");
292 static int read_f(int argc, char **argv);
294 static const cmdinfo_t read_cmd = {
295 .name = "read",
296 .altname = "r",
297 .cfunc = read_f,
298 .argmin = 2,
299 .argmax = -1,
300 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
301 .oneline = "reads a number of bytes at a specified offset",
302 .help = read_help,
305 static int
306 read_f(int argc, char **argv)
308 struct timeval t1, t2;
309 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
310 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
311 int c, cnt;
312 char *buf;
313 int64_t offset;
314 int count;
315 /* Some compilers get confused and warn if this is not initialized. */
316 int total = 0;
317 int pattern = 0, pattern_offset = 0, pattern_count = 0;
319 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
320 switch (c) {
321 case 'b':
322 bflag = 1;
323 break;
324 case 'C':
325 Cflag = 1;
326 break;
327 case 'l':
328 lflag = 1;
329 pattern_count = cvtnum(optarg);
330 if (pattern_count < 0) {
331 printf("non-numeric length argument -- %s\n", optarg);
332 return 0;
334 break;
335 case 'p':
336 pflag = 1;
337 break;
338 case 'P':
339 Pflag = 1;
340 pattern = parse_pattern(optarg);
341 if (pattern < 0)
342 return 0;
343 break;
344 case 'q':
345 qflag = 1;
346 break;
347 case 's':
348 sflag = 1;
349 pattern_offset = cvtnum(optarg);
350 if (pattern_offset < 0) {
351 printf("non-numeric length argument -- %s\n", optarg);
352 return 0;
354 break;
355 case 'v':
356 vflag = 1;
357 break;
358 default:
359 return command_usage(&read_cmd);
363 if (optind != argc - 2)
364 return command_usage(&read_cmd);
366 if (bflag && pflag) {
367 printf("-b and -p cannot be specified at the same time\n");
368 return 0;
371 offset = cvtnum(argv[optind]);
372 if (offset < 0) {
373 printf("non-numeric length argument -- %s\n", argv[optind]);
374 return 0;
377 optind++;
378 count = cvtnum(argv[optind]);
379 if (count < 0) {
380 printf("non-numeric length argument -- %s\n", argv[optind]);
381 return 0;
384 if (!Pflag && (lflag || sflag)) {
385 return command_usage(&read_cmd);
388 if (!lflag) {
389 pattern_count = count - pattern_offset;
392 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
393 printf("pattern verfication range exceeds end of read data\n");
394 return 0;
397 if (!pflag)
398 if (offset & 0x1ff) {
399 printf("offset %lld is not sector aligned\n",
400 (long long)offset);
401 return 0;
403 if (count & 0x1ff) {
404 printf("count %d is not sector aligned\n",
405 count);
406 return 0;
410 buf = qemu_io_alloc(count, 0xab);
412 gettimeofday(&t1, NULL);
413 if (pflag)
414 cnt = do_pread(buf, offset, count, &total);
415 else if (bflag)
416 cnt = do_load_vmstate(buf, offset, count, &total);
417 else
418 cnt = do_read(buf, offset, count, &total);
419 gettimeofday(&t2, NULL);
421 if (cnt < 0) {
422 printf("read failed: %s\n", strerror(-cnt));
423 goto out;
426 if (Pflag) {
427 void* cmp_buf = malloc(pattern_count);
428 memset(cmp_buf, pattern, pattern_count);
429 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
430 printf("Pattern verification failed at offset %lld, "
431 "%d bytes\n",
432 (long long) offset + pattern_offset, pattern_count);
434 free(cmp_buf);
437 if (qflag)
438 goto out;
440 if (vflag)
441 dump_buffer(buf, offset, count);
443 /* Finally, report back -- -C gives a parsable format */
444 t2 = tsub(t2, t1);
445 print_report("read", &t2, offset, count, total, cnt, Cflag);
447 out:
448 qemu_io_free(buf);
450 return 0;
453 static void
454 readv_help(void)
456 printf(
457 "\n"
458 " reads a range of bytes from the given offset into multiple buffers\n"
459 "\n"
460 " Example:\n"
461 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
462 "\n"
463 " Reads a segment of the currently open file, optionally dumping it to the\n"
464 " standard output stream (with -v option) for subsequent inspection.\n"
465 " Uses multiple iovec buffers if more than one byte range is specified.\n"
466 " -C, -- report statistics in a machine parsable format\n"
467 " -P, -- use a pattern to verify read data\n"
468 " -v, -- dump buffer to standard output\n"
469 " -q, -- quite mode, do not show I/O statistics\n"
470 "\n");
473 static int readv_f(int argc, char **argv);
475 static const cmdinfo_t readv_cmd = {
476 .name = "readv",
477 .cfunc = readv_f,
478 .argmin = 2,
479 .argmax = -1,
480 .args = "[-Cqv] [-P pattern ] off len [len..]",
481 .oneline = "reads a number of bytes at a specified offset",
482 .help = readv_help,
485 static int
486 readv_f(int argc, char **argv)
488 struct timeval t1, t2;
489 int Cflag = 0, qflag = 0, vflag = 0;
490 int c, cnt;
491 char *buf;
492 int64_t offset;
493 int total;
494 int nr_iov;
495 QEMUIOVector qiov;
496 int pattern = 0;
497 int Pflag = 0;
499 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
500 switch (c) {
501 case 'C':
502 Cflag = 1;
503 break;
504 case 'P':
505 Pflag = 1;
506 pattern = parse_pattern(optarg);
507 if (pattern < 0)
508 return 0;
509 break;
510 case 'q':
511 qflag = 1;
512 break;
513 case 'v':
514 vflag = 1;
515 break;
516 default:
517 return command_usage(&readv_cmd);
521 if (optind > argc - 2)
522 return command_usage(&readv_cmd);
525 offset = cvtnum(argv[optind]);
526 if (offset < 0) {
527 printf("non-numeric length argument -- %s\n", argv[optind]);
528 return 0;
530 optind++;
532 if (offset & 0x1ff) {
533 printf("offset %lld is not sector aligned\n",
534 (long long)offset);
535 return 0;
538 nr_iov = argc - optind;
539 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
541 gettimeofday(&t1, NULL);
542 cnt = do_aio_readv(&qiov, offset, &total);
543 gettimeofday(&t2, NULL);
545 if (cnt < 0) {
546 printf("readv failed: %s\n", strerror(-cnt));
547 goto out;
550 if (Pflag) {
551 void* cmp_buf = malloc(qiov.size);
552 memset(cmp_buf, pattern, qiov.size);
553 if (memcmp(buf, cmp_buf, qiov.size)) {
554 printf("Pattern verification failed at offset %lld, "
555 "%zd bytes\n",
556 (long long) offset, qiov.size);
558 free(cmp_buf);
561 if (qflag)
562 goto out;
564 if (vflag)
565 dump_buffer(buf, offset, qiov.size);
567 /* Finally, report back -- -C gives a parsable format */
568 t2 = tsub(t2, t1);
569 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
571 out:
572 qemu_io_free(buf);
573 return 0;
576 static void
577 write_help(void)
579 printf(
580 "\n"
581 " writes a range of bytes from the given offset\n"
582 "\n"
583 " Example:\n"
584 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
585 "\n"
586 " Writes into a segment of the currently open file, using a buffer\n"
587 " filled with a set pattern (0xcdcdcdcd).\n"
588 " -b, -- write to the VM state rather than the virtual disk\n"
589 " -p, -- use bdrv_pwrite to write the file\n"
590 " -P, -- use different pattern to fill file\n"
591 " -C, -- report statistics in a machine parsable format\n"
592 " -q, -- quite mode, do not show I/O statistics\n"
593 "\n");
596 static int write_f(int argc, char **argv);
598 static const cmdinfo_t write_cmd = {
599 .name = "write",
600 .altname = "w",
601 .cfunc = write_f,
602 .argmin = 2,
603 .argmax = -1,
604 .args = "[-abCpq] [-P pattern ] off len",
605 .oneline = "writes a number of bytes at a specified offset",
606 .help = write_help,
609 static int
610 write_f(int argc, char **argv)
612 struct timeval t1, t2;
613 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
614 int c, cnt;
615 char *buf;
616 int64_t offset;
617 int count;
618 /* Some compilers get confused and warn if this is not initialized. */
619 int total = 0;
620 int pattern = 0xcd;
622 while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
623 switch (c) {
624 case 'b':
625 bflag = 1;
626 break;
627 case 'C':
628 Cflag = 1;
629 break;
630 case 'p':
631 pflag = 1;
632 break;
633 case 'P':
634 pattern = parse_pattern(optarg);
635 if (pattern < 0)
636 return 0;
637 break;
638 case 'q':
639 qflag = 1;
640 break;
641 default:
642 return command_usage(&write_cmd);
646 if (optind != argc - 2)
647 return command_usage(&write_cmd);
649 if (bflag && pflag) {
650 printf("-b and -p cannot be specified at the same time\n");
651 return 0;
654 offset = cvtnum(argv[optind]);
655 if (offset < 0) {
656 printf("non-numeric length argument -- %s\n", argv[optind]);
657 return 0;
660 optind++;
661 count = cvtnum(argv[optind]);
662 if (count < 0) {
663 printf("non-numeric length argument -- %s\n", argv[optind]);
664 return 0;
667 if (!pflag) {
668 if (offset & 0x1ff) {
669 printf("offset %lld is not sector aligned\n",
670 (long long)offset);
671 return 0;
674 if (count & 0x1ff) {
675 printf("count %d is not sector aligned\n",
676 count);
677 return 0;
681 buf = qemu_io_alloc(count, pattern);
683 gettimeofday(&t1, NULL);
684 if (pflag)
685 cnt = do_pwrite(buf, offset, count, &total);
686 else if (bflag)
687 cnt = do_save_vmstate(buf, offset, count, &total);
688 else
689 cnt = do_write(buf, offset, count, &total);
690 gettimeofday(&t2, NULL);
692 if (cnt < 0) {
693 printf("write failed: %s\n", strerror(-cnt));
694 goto out;
697 if (qflag)
698 goto out;
700 /* Finally, report back -- -C gives a parsable format */
701 t2 = tsub(t2, t1);
702 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
704 out:
705 qemu_io_free(buf);
707 return 0;
710 static void
711 writev_help(void)
713 printf(
714 "\n"
715 " writes a range of bytes from the given offset source from multiple buffers\n"
716 "\n"
717 " Example:\n"
718 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
719 "\n"
720 " Writes into a segment of the currently open file, using a buffer\n"
721 " filled with a set pattern (0xcdcdcdcd).\n"
722 " -P, -- use different pattern to fill file\n"
723 " -C, -- report statistics in a machine parsable format\n"
724 " -q, -- quite mode, do not show I/O statistics\n"
725 "\n");
728 static int writev_f(int argc, char **argv);
730 static const cmdinfo_t writev_cmd = {
731 .name = "writev",
732 .cfunc = writev_f,
733 .argmin = 2,
734 .argmax = -1,
735 .args = "[-Cq] [-P pattern ] off len [len..]",
736 .oneline = "writes a number of bytes at a specified offset",
737 .help = writev_help,
740 static int
741 writev_f(int argc, char **argv)
743 struct timeval t1, t2;
744 int Cflag = 0, qflag = 0;
745 int c, cnt;
746 char *buf;
747 int64_t offset;
748 int total;
749 int nr_iov;
750 int pattern = 0xcd;
751 QEMUIOVector qiov;
753 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
754 switch (c) {
755 case 'C':
756 Cflag = 1;
757 break;
758 case 'q':
759 qflag = 1;
760 break;
761 case 'P':
762 pattern = parse_pattern(optarg);
763 if (pattern < 0)
764 return 0;
765 break;
766 default:
767 return command_usage(&writev_cmd);
771 if (optind > argc - 2)
772 return command_usage(&writev_cmd);
774 offset = cvtnum(argv[optind]);
775 if (offset < 0) {
776 printf("non-numeric length argument -- %s\n", argv[optind]);
777 return 0;
779 optind++;
781 if (offset & 0x1ff) {
782 printf("offset %lld is not sector aligned\n",
783 (long long)offset);
784 return 0;
787 nr_iov = argc - optind;
788 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
790 gettimeofday(&t1, NULL);
791 cnt = do_aio_writev(&qiov, offset, &total);
792 gettimeofday(&t2, NULL);
794 if (cnt < 0) {
795 printf("writev failed: %s\n", strerror(-cnt));
796 goto out;
799 if (qflag)
800 goto out;
802 /* Finally, report back -- -C gives a parsable format */
803 t2 = tsub(t2, t1);
804 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
805 out:
806 qemu_io_free(buf);
807 return 0;
810 struct aio_ctx {
811 QEMUIOVector qiov;
812 int64_t offset;
813 char *buf;
814 int qflag;
815 int vflag;
816 int Cflag;
817 int Pflag;
818 int pattern;
819 struct timeval t1;
822 static void
823 aio_write_done(void *opaque, int ret)
825 struct aio_ctx *ctx = opaque;
826 struct timeval t2;
828 gettimeofday(&t2, NULL);
831 if (ret < 0) {
832 printf("aio_write failed: %s\n", strerror(-ret));
833 goto out;
836 if (ctx->qflag) {
837 goto out;
840 /* Finally, report back -- -C gives a parsable format */
841 t2 = tsub(t2, ctx->t1);
842 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
843 ctx->qiov.size, 1, ctx->Cflag);
844 out:
845 qemu_io_free(ctx->buf);
846 free(ctx);
849 static void
850 aio_read_done(void *opaque, int ret)
852 struct aio_ctx *ctx = opaque;
853 struct timeval t2;
855 gettimeofday(&t2, NULL);
857 if (ret < 0) {
858 printf("readv failed: %s\n", strerror(-ret));
859 goto out;
862 if (ctx->Pflag) {
863 void *cmp_buf = malloc(ctx->qiov.size);
865 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
866 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
867 printf("Pattern verification failed at offset %lld, "
868 "%zd bytes\n",
869 (long long) ctx->offset, ctx->qiov.size);
871 free(cmp_buf);
874 if (ctx->qflag) {
875 goto out;
878 if (ctx->vflag) {
879 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
882 /* Finally, report back -- -C gives a parsable format */
883 t2 = tsub(t2, ctx->t1);
884 print_report("read", &t2, ctx->offset, ctx->qiov.size,
885 ctx->qiov.size, 1, ctx->Cflag);
886 out:
887 qemu_io_free(ctx->buf);
888 free(ctx);
891 static void
892 aio_read_help(void)
894 printf(
895 "\n"
896 " asynchronously reads a range of bytes from the given offset\n"
897 "\n"
898 " Example:\n"
899 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
900 "\n"
901 " Reads a segment of the currently open file, optionally dumping it to the\n"
902 " standard output stream (with -v option) for subsequent inspection.\n"
903 " The read is performed asynchronously and should the aio_flush command \n"
904 " should be used to ensure all outstanding aio requests have been completed\n"
905 " -C, -- report statistics in a machine parsable format\n"
906 " -P, -- use a pattern to verify read data\n"
907 " -v, -- dump buffer to standard output\n"
908 " -q, -- quite mode, do not show I/O statistics\n"
909 "\n");
912 static int aio_read_f(int argc, char **argv);
914 static const cmdinfo_t aio_read_cmd = {
915 .name = "aio_read",
916 .cfunc = aio_read_f,
917 .argmin = 2,
918 .argmax = -1,
919 .args = "[-Cqv] [-P pattern ] off len [len..]",
920 .oneline = "asynchronously reads a number of bytes",
921 .help = aio_read_help,
924 static int
925 aio_read_f(int argc, char **argv)
927 int nr_iov, c;
928 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
929 BlockDriverAIOCB *acb;
931 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
932 switch (c) {
933 case 'C':
934 ctx->Cflag = 1;
935 break;
936 case 'P':
937 ctx->Pflag = 1;
938 ctx->pattern = parse_pattern(optarg);
939 if (ctx->pattern < 0)
940 return 0;
941 break;
942 case 'q':
943 ctx->qflag = 1;
944 break;
945 case 'v':
946 ctx->vflag = 1;
947 break;
948 default:
949 free(ctx);
950 return command_usage(&aio_read_cmd);
954 if (optind > argc - 2) {
955 free(ctx);
956 return command_usage(&aio_read_cmd);
959 ctx->offset = cvtnum(argv[optind]);
960 if (ctx->offset < 0) {
961 printf("non-numeric length argument -- %s\n", argv[optind]);
962 free(ctx);
963 return 0;
965 optind++;
967 if (ctx->offset & 0x1ff) {
968 printf("offset %lld is not sector aligned\n",
969 (long long)ctx->offset);
970 free(ctx);
971 return 0;
974 nr_iov = argc - optind;
975 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
977 gettimeofday(&ctx->t1, NULL);
978 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
979 ctx->qiov.size >> 9, aio_read_done, ctx);
980 if (!acb) {
981 free(ctx->buf);
982 free(ctx);
983 return -EIO;
986 return 0;
989 static void
990 aio_write_help(void)
992 printf(
993 "\n"
994 " asynchronously writes a range of bytes from the given offset source \n"
995 " from multiple buffers\n"
996 "\n"
997 " Example:\n"
998 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
999 "\n"
1000 " Writes into a segment of the currently open file, using a buffer\n"
1001 " filled with a set pattern (0xcdcdcdcd).\n"
1002 " The write is performed asynchronously and should the aio_flush command \n"
1003 " should be used to ensure all outstanding aio requests have been completed\n"
1004 " -P, -- use different pattern to fill file\n"
1005 " -C, -- report statistics in a machine parsable format\n"
1006 " -q, -- quite mode, do not show I/O statistics\n"
1007 "\n");
1010 static int aio_write_f(int argc, char **argv);
1012 static const cmdinfo_t aio_write_cmd = {
1013 .name = "aio_write",
1014 .cfunc = aio_write_f,
1015 .argmin = 2,
1016 .argmax = -1,
1017 .args = "[-Cq] [-P pattern ] off len [len..]",
1018 .oneline = "asynchronously writes a number of bytes",
1019 .help = aio_write_help,
1022 static int
1023 aio_write_f(int argc, char **argv)
1025 int nr_iov, c;
1026 int pattern = 0xcd;
1027 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1028 BlockDriverAIOCB *acb;
1030 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1031 switch (c) {
1032 case 'C':
1033 ctx->Cflag = 1;
1034 break;
1035 case 'q':
1036 ctx->qflag = 1;
1037 break;
1038 case 'P':
1039 pattern = parse_pattern(optarg);
1040 if (pattern < 0)
1041 return 0;
1042 break;
1043 default:
1044 free(ctx);
1045 return command_usage(&aio_write_cmd);
1049 if (optind > argc - 2) {
1050 free(ctx);
1051 return command_usage(&aio_write_cmd);
1054 ctx->offset = cvtnum(argv[optind]);
1055 if (ctx->offset < 0) {
1056 printf("non-numeric length argument -- %s\n", argv[optind]);
1057 free(ctx);
1058 return 0;
1060 optind++;
1062 if (ctx->offset & 0x1ff) {
1063 printf("offset %lld is not sector aligned\n",
1064 (long long)ctx->offset);
1065 free(ctx);
1066 return 0;
1069 nr_iov = argc - optind;
1070 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
1072 gettimeofday(&ctx->t1, NULL);
1073 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1074 ctx->qiov.size >> 9, aio_write_done, ctx);
1075 if (!acb) {
1076 free(ctx->buf);
1077 free(ctx);
1078 return -EIO;
1081 return 0;
1084 static int
1085 aio_flush_f(int argc, char **argv)
1087 qemu_aio_flush();
1088 return 0;
1091 static const cmdinfo_t aio_flush_cmd = {
1092 .name = "aio_flush",
1093 .cfunc = aio_flush_f,
1094 .oneline = "completes all outstanding aio requets"
1097 static int
1098 flush_f(int argc, char **argv)
1100 bdrv_flush(bs);
1101 return 0;
1104 static const cmdinfo_t flush_cmd = {
1105 .name = "flush",
1106 .altname = "f",
1107 .cfunc = flush_f,
1108 .oneline = "flush all in-core file state to disk",
1111 static int
1112 truncate_f(int argc, char **argv)
1114 int64_t offset;
1115 int ret;
1117 offset = cvtnum(argv[1]);
1118 if (offset < 0) {
1119 printf("non-numeric truncate argument -- %s\n", argv[1]);
1120 return 0;
1123 ret = bdrv_truncate(bs, offset);
1124 if (ret < 0) {
1125 printf("truncate: %s", strerror(ret));
1126 return 0;
1129 return 0;
1132 static const cmdinfo_t truncate_cmd = {
1133 .name = "truncate",
1134 .altname = "t",
1135 .cfunc = truncate_f,
1136 .argmin = 1,
1137 .argmax = 1,
1138 .args = "off",
1139 .oneline = "truncates the current file at the given offset",
1142 static int
1143 length_f(int argc, char **argv)
1145 int64_t size;
1146 char s1[64];
1148 size = bdrv_getlength(bs);
1149 if (size < 0) {
1150 printf("getlength: %s", strerror(size));
1151 return 0;
1154 cvtstr(size, s1, sizeof(s1));
1155 printf("%s\n", s1);
1156 return 0;
1160 static const cmdinfo_t length_cmd = {
1161 .name = "length",
1162 .altname = "l",
1163 .cfunc = length_f,
1164 .oneline = "gets the length of the current file",
1168 static int
1169 info_f(int argc, char **argv)
1171 BlockDriverInfo bdi;
1172 char s1[64], s2[64];
1173 int ret;
1175 if (bs->drv && bs->drv->format_name)
1176 printf("format name: %s\n", bs->drv->format_name);
1177 if (bs->drv && bs->drv->protocol_name)
1178 printf("format name: %s\n", bs->drv->protocol_name);
1180 ret = bdrv_get_info(bs, &bdi);
1181 if (ret)
1182 return 0;
1184 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1185 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1187 printf("cluster size: %s\n", s1);
1188 printf("vm state offset: %s\n", s2);
1190 return 0;
1195 static const cmdinfo_t info_cmd = {
1196 .name = "info",
1197 .altname = "i",
1198 .cfunc = info_f,
1199 .oneline = "prints information about the current file",
1202 static int
1203 alloc_f(int argc, char **argv)
1205 int64_t offset;
1206 int nb_sectors, remaining;
1207 char s1[64];
1208 int num, sum_alloc;
1209 int ret;
1211 offset = cvtnum(argv[1]);
1212 if (offset & 0x1ff) {
1213 printf("offset %lld is not sector aligned\n",
1214 (long long)offset);
1215 return 0;
1218 if (argc == 3)
1219 nb_sectors = cvtnum(argv[2]);
1220 else
1221 nb_sectors = 1;
1223 remaining = nb_sectors;
1224 sum_alloc = 0;
1225 while (remaining) {
1226 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1227 remaining -= num;
1228 if (ret) {
1229 sum_alloc += num;
1233 cvtstr(offset, s1, sizeof(s1));
1235 if (nb_sectors == 1)
1236 printf("sector allocated at offset %s\n", s1);
1237 else
1238 printf("%d/%d sectors allocated at offset %s\n",
1239 sum_alloc, nb_sectors, s1);
1240 return 0;
1243 static const cmdinfo_t alloc_cmd = {
1244 .name = "alloc",
1245 .altname = "a",
1246 .argmin = 1,
1247 .argmax = 2,
1248 .cfunc = alloc_f,
1249 .args = "off [sectors]",
1250 .oneline = "checks if a sector is present in the file",
1253 static int
1254 close_f(int argc, char **argv)
1256 bdrv_close(bs);
1257 bs = NULL;
1258 return 0;
1261 static const cmdinfo_t close_cmd = {
1262 .name = "close",
1263 .altname = "c",
1264 .cfunc = close_f,
1265 .oneline = "close the current open file",
1268 static int openfile(char *name, int flags, int growable)
1270 if (bs) {
1271 fprintf(stderr, "file open already, try 'help close'\n");
1272 return 1;
1275 bs = bdrv_new("hda");
1276 if (!bs)
1277 return 1;
1279 if (growable) {
1280 flags |= BDRV_O_FILE;
1283 if (bdrv_open(bs, name, flags) == -1) {
1284 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1285 bs = NULL;
1286 return 1;
1289 if (growable) {
1290 bs->growable = 1;
1292 return 0;
1295 static void
1296 open_help(void)
1298 printf(
1299 "\n"
1300 " opens a new file in the requested mode\n"
1301 "\n"
1302 " Example:\n"
1303 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1304 "\n"
1305 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1306 " -C, -- create new file if it doesn't exist\n"
1307 " -r, -- open file read-only\n"
1308 " -s, -- use snapshot file\n"
1309 " -n, -- disable host cache\n"
1310 " -g, -- allow file to grow (only applies to protocols)"
1311 "\n");
1314 static int open_f(int argc, char **argv);
1316 static const cmdinfo_t open_cmd = {
1317 .name = "open",
1318 .altname = "o",
1319 .cfunc = open_f,
1320 .argmin = 1,
1321 .argmax = -1,
1322 .flags = CMD_NOFILE_OK,
1323 .args = "[-Crsn] [path]",
1324 .oneline = "open the file specified by path",
1325 .help = open_help,
1328 static int
1329 open_f(int argc, char **argv)
1331 int flags = 0;
1332 int readonly = 0;
1333 int growable = 0;
1334 int c;
1336 while ((c = getopt(argc, argv, "snCrg")) != EOF) {
1337 switch (c) {
1338 case 's':
1339 flags |= BDRV_O_SNAPSHOT;
1340 break;
1341 case 'n':
1342 flags |= BDRV_O_NOCACHE;
1343 break;
1344 case 'C':
1345 flags |= BDRV_O_CREAT;
1346 break;
1347 case 'r':
1348 readonly = 1;
1349 break;
1350 case 'g':
1351 growable = 1;
1352 break;
1353 default:
1354 return command_usage(&open_cmd);
1358 if (readonly)
1359 flags |= BDRV_O_RDONLY;
1360 else
1361 flags |= BDRV_O_RDWR;
1363 if (optind != argc - 1)
1364 return command_usage(&open_cmd);
1366 return openfile(argv[optind], flags, growable);
1369 static int
1370 init_args_command(
1371 int index)
1373 /* only one device allowed so far */
1374 if (index >= 1)
1375 return 0;
1376 return ++index;
1379 static int
1380 init_check_command(
1381 const cmdinfo_t *ct)
1383 if (ct->flags & CMD_FLAG_GLOBAL)
1384 return 1;
1385 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1386 fprintf(stderr, "no file open, try 'help open'\n");
1387 return 0;
1389 return 1;
1392 static void usage(const char *name)
1394 printf(
1395 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1396 "QEMU Disk exerciser\n"
1397 "\n"
1398 " -C, --create create new file if it doesn't exist\n"
1399 " -c, --cmd command to execute\n"
1400 " -r, --read-only export read-only\n"
1401 " -s, --snapshot use snapshot file\n"
1402 " -n, --nocache disable host cache\n"
1403 " -g, --growable allow file to grow (only applies to protocols)\n"
1404 " -m, --misalign misalign allocations for O_DIRECT\n"
1405 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1406 " -h, --help display this help and exit\n"
1407 " -V, --version output version information and exit\n"
1408 "\n",
1409 name);
1413 int main(int argc, char **argv)
1415 int readonly = 0;
1416 int growable = 0;
1417 const char *sopt = "hVc:Crsnmgk";
1418 const struct option lopt[] = {
1419 { "help", 0, NULL, 'h' },
1420 { "version", 0, NULL, 'V' },
1421 { "offset", 1, NULL, 'o' },
1422 { "cmd", 1, NULL, 'c' },
1423 { "create", 0, NULL, 'C' },
1424 { "read-only", 0, NULL, 'r' },
1425 { "snapshot", 0, NULL, 's' },
1426 { "nocache", 0, NULL, 'n' },
1427 { "misalign", 0, NULL, 'm' },
1428 { "growable", 0, NULL, 'g' },
1429 { "native-aio", 0, NULL, 'k' },
1430 { NULL, 0, NULL, 0 }
1432 int c;
1433 int opt_index = 0;
1434 int flags = 0;
1436 progname = basename(argv[0]);
1438 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1439 switch (c) {
1440 case 's':
1441 flags |= BDRV_O_SNAPSHOT;
1442 break;
1443 case 'n':
1444 flags |= BDRV_O_NOCACHE;
1445 break;
1446 case 'c':
1447 add_user_command(optarg);
1448 break;
1449 case 'C':
1450 flags |= BDRV_O_CREAT;
1451 break;
1452 case 'r':
1453 readonly = 1;
1454 break;
1455 case 'm':
1456 misalign = 1;
1457 break;
1458 case 'g':
1459 growable = 1;
1460 break;
1461 case 'k':
1462 flags |= BDRV_O_NATIVE_AIO;
1463 break;
1464 case 'V':
1465 printf("%s version %s\n", progname, VERSION);
1466 exit(0);
1467 case 'h':
1468 usage(progname);
1469 exit(0);
1470 default:
1471 usage(progname);
1472 exit(1);
1476 if ((argc - optind) > 1) {
1477 usage(progname);
1478 exit(1);
1481 bdrv_init();
1483 /* initialize commands */
1484 quit_init();
1485 help_init();
1486 add_command(&open_cmd);
1487 add_command(&close_cmd);
1488 add_command(&read_cmd);
1489 add_command(&readv_cmd);
1490 add_command(&write_cmd);
1491 add_command(&writev_cmd);
1492 add_command(&aio_read_cmd);
1493 add_command(&aio_write_cmd);
1494 add_command(&aio_flush_cmd);
1495 add_command(&flush_cmd);
1496 add_command(&truncate_cmd);
1497 add_command(&length_cmd);
1498 add_command(&info_cmd);
1499 add_command(&alloc_cmd);
1501 add_args_command(init_args_command);
1502 add_check_command(init_check_command);
1504 /* open the device */
1505 if (readonly)
1506 flags |= BDRV_O_RDONLY;
1507 else
1508 flags |= BDRV_O_RDWR;
1510 if ((argc - optind) == 1)
1511 openfile(argv[optind], flags, growable);
1512 command_loop();
1515 * Make sure all outstanding requests get flushed the program exits.
1517 qemu_aio_flush();
1519 if (bs)
1520 bdrv_close(bs);
1521 return 0;