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.
11 #include <sys/types.h>
17 #include "qemu-common.h"
18 #include "block_int.h"
21 #define VERSION "0.0.1"
23 #define CMD_NOFILE_OK 0x01
26 static BlockDriverState
*bs
;
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
)
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
);
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
)
63 len
+= MISALIGN_OFFSET
;
65 buf
= qemu_blockalign(bs
, len
);
66 memset(buf
, pattern
, len
);
68 buf
+= MISALIGN_OFFSET
;
73 static void qemu_io_free(void *p
)
81 static void dump_buffer(const void *buffer
, int64_t offset
, int len
)
86 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
89 printf("%08" PRIx64
": ", offset
+ i
);
90 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
94 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
105 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
106 int count
, int total
, int cnt
, int Cflag
)
108 char s1
[64], s2
[64], ts
[64];
110 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
112 cvtstr((double)total
, s1
, sizeof(s1
));
113 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
114 printf("%s %d/%d bytes at offset %" PRId64
"\n",
115 op
, total
, count
, offset
);
116 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
117 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
118 } else {/* bytes,ops,time,bytes/sec,ops/sec */
119 printf("%d,%d,%s,%.3f,%.3f\n",
121 tdiv((double)total
, *t
),
122 tdiv((double)cnt
, *t
));
127 * Parse multiple length statements for vectored I/O, and construct an I/O
128 * vector matching it.
131 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
133 size_t *sizes
= calloc(nr_iov
, sizeof(size_t));
139 for (i
= 0; i
< nr_iov
; i
++) {
145 printf("non-numeric length argument -- %s\n", arg
);
149 /* should be SIZE_T_MAX, but that doesn't exist */
151 printf("too large length argument -- %s\n", arg
);
156 printf("length argument %" PRId64
157 " is not sector aligned\n", len
);
165 qemu_iovec_init(qiov
, nr_iov
);
167 buf
= p
= qemu_io_alloc(count
, pattern
);
169 for (i
= 0; i
< nr_iov
; i
++) {
170 qemu_iovec_add(qiov
, p
, sizes
[i
]);
179 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
183 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
191 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
195 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
203 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
205 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
212 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
214 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
221 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
223 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
230 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
232 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
239 #define NOT_DONE 0x7fffffff
240 static void aio_rw_done(void *opaque
, int ret
)
242 *(int *)opaque
= ret
;
245 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
247 BlockDriverAIOCB
*acb
;
248 int async_ret
= NOT_DONE
;
250 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
251 aio_rw_done
, &async_ret
);
255 while (async_ret
== NOT_DONE
) {
260 return async_ret
< 0 ? async_ret
: 1;
263 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
265 BlockDriverAIOCB
*acb
;
266 int async_ret
= NOT_DONE
;
268 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
269 aio_rw_done
, &async_ret
);
274 while (async_ret
== NOT_DONE
) {
279 return async_ret
< 0 ? async_ret
: 1;
282 struct multiwrite_async_ret
{
287 static void multiwrite_cb(void *opaque
, int ret
)
289 struct multiwrite_async_ret
*async_ret
= opaque
;
291 async_ret
->num_done
++;
293 async_ret
->error
= ret
;
297 static int do_aio_multiwrite(BlockRequest
* reqs
, int num_reqs
, int *total
)
300 struct multiwrite_async_ret async_ret
= {
306 for (i
= 0; i
< num_reqs
; i
++) {
307 reqs
[i
].cb
= multiwrite_cb
;
308 reqs
[i
].opaque
= &async_ret
;
309 *total
+= reqs
[i
].qiov
->size
;
312 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
317 while (async_ret
.num_done
< num_reqs
) {
321 return async_ret
.error
< 0 ? async_ret
.error
: 1;
324 static void read_help(void)
328 " reads a range of bytes from the given offset\n"
331 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
333 " Reads a segment of the currently open file, optionally dumping it to the\n"
334 " standard output stream (with -v option) for subsequent inspection.\n"
335 " -b, -- read from the VM state rather than the virtual disk\n"
336 " -C, -- report statistics in a machine parsable format\n"
337 " -l, -- length for pattern verification (only with -P)\n"
338 " -p, -- use bdrv_pread to read the file\n"
339 " -P, -- use a pattern to verify read data\n"
340 " -q, -- quiet mode, do not show I/O statistics\n"
341 " -s, -- start offset for pattern verification (only with -P)\n"
342 " -v, -- dump buffer to standard output\n"
346 static int read_f(int argc
, char **argv
);
348 static const cmdinfo_t read_cmd
= {
354 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
355 .oneline
= "reads a number of bytes at a specified offset",
359 static int read_f(int argc
, char **argv
)
361 struct timeval t1
, t2
;
362 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
363 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
368 /* Some compilers get confused and warn if this is not initialized. */
370 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
372 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
382 pattern_count
= cvtnum(optarg
);
383 if (pattern_count
< 0) {
384 printf("non-numeric length argument -- %s\n", optarg
);
393 pattern
= parse_pattern(optarg
);
403 pattern_offset
= cvtnum(optarg
);
404 if (pattern_offset
< 0) {
405 printf("non-numeric length argument -- %s\n", optarg
);
413 return command_usage(&read_cmd
);
417 if (optind
!= argc
- 2) {
418 return command_usage(&read_cmd
);
421 if (bflag
&& pflag
) {
422 printf("-b and -p cannot be specified at the same time\n");
426 offset
= cvtnum(argv
[optind
]);
428 printf("non-numeric length argument -- %s\n", argv
[optind
]);
433 count
= cvtnum(argv
[optind
]);
435 printf("non-numeric length argument -- %s\n", argv
[optind
]);
439 if (!Pflag
&& (lflag
|| sflag
)) {
440 return command_usage(&read_cmd
);
444 pattern_count
= count
- pattern_offset
;
447 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
448 printf("pattern verfication range exceeds end of read data\n");
453 if (offset
& 0x1ff) {
454 printf("offset %" PRId64
" is not sector aligned\n",
459 printf("count %d is not sector aligned\n",
465 buf
= qemu_io_alloc(count
, 0xab);
467 gettimeofday(&t1
, NULL
);
469 cnt
= do_pread(buf
, offset
, count
, &total
);
471 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
473 cnt
= do_read(buf
, offset
, count
, &total
);
475 gettimeofday(&t2
, NULL
);
478 printf("read failed: %s\n", strerror(-cnt
));
483 void *cmp_buf
= malloc(pattern_count
);
484 memset(cmp_buf
, pattern
, pattern_count
);
485 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
486 printf("Pattern verification failed at offset %"
487 PRId64
", %d bytes\n",
488 offset
+ pattern_offset
, pattern_count
);
498 dump_buffer(buf
, offset
, count
);
501 /* Finally, report back -- -C gives a parsable format */
503 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
511 static void readv_help(void)
515 " reads a range of bytes from the given offset into multiple buffers\n"
518 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
520 " Reads a segment of the currently open file, optionally dumping it to the\n"
521 " standard output stream (with -v option) for subsequent inspection.\n"
522 " Uses multiple iovec buffers if more than one byte range is specified.\n"
523 " -C, -- report statistics in a machine parsable format\n"
524 " -P, -- use a pattern to verify read data\n"
525 " -v, -- dump buffer to standard output\n"
526 " -q, -- quiet mode, do not show I/O statistics\n"
530 static int readv_f(int argc
, char **argv
);
532 static const cmdinfo_t readv_cmd
= {
537 .args
= "[-Cqv] [-P pattern ] off len [len..]",
538 .oneline
= "reads a number of bytes at a specified offset",
542 static int readv_f(int argc
, char **argv
)
544 struct timeval t1
, t2
;
545 int Cflag
= 0, qflag
= 0, vflag
= 0;
549 /* Some compilers get confused and warn if this is not initialized. */
556 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
563 pattern
= parse_pattern(optarg
);
575 return command_usage(&readv_cmd
);
579 if (optind
> argc
- 2) {
580 return command_usage(&readv_cmd
);
584 offset
= cvtnum(argv
[optind
]);
586 printf("non-numeric length argument -- %s\n", argv
[optind
]);
591 if (offset
& 0x1ff) {
592 printf("offset %" PRId64
" is not sector aligned\n",
597 nr_iov
= argc
- optind
;
598 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
603 gettimeofday(&t1
, NULL
);
604 cnt
= do_aio_readv(&qiov
, offset
, &total
);
605 gettimeofday(&t2
, NULL
);
608 printf("readv failed: %s\n", strerror(-cnt
));
613 void *cmp_buf
= malloc(qiov
.size
);
614 memset(cmp_buf
, pattern
, qiov
.size
);
615 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
616 printf("Pattern verification failed at offset %"
617 PRId64
", %zd bytes\n", offset
, qiov
.size
);
627 dump_buffer(buf
, offset
, qiov
.size
);
630 /* Finally, report back -- -C gives a parsable format */
632 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
639 static void write_help(void)
643 " writes a range of bytes from the given offset\n"
646 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
648 " Writes into a segment of the currently open file, using a buffer\n"
649 " filled with a set pattern (0xcdcdcdcd).\n"
650 " -b, -- write to the VM state rather than the virtual disk\n"
651 " -p, -- use bdrv_pwrite to write the file\n"
652 " -P, -- use different pattern to fill file\n"
653 " -C, -- report statistics in a machine parsable format\n"
654 " -q, -- quiet mode, do not show I/O statistics\n"
658 static int write_f(int argc
, char **argv
);
660 static const cmdinfo_t write_cmd
= {
666 .args
= "[-abCpq] [-P pattern ] off len",
667 .oneline
= "writes a number of bytes at a specified offset",
671 static int write_f(int argc
, char **argv
)
673 struct timeval t1
, t2
;
674 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0;
679 /* Some compilers get confused and warn if this is not initialized. */
683 while ((c
= getopt(argc
, argv
, "bCpP:q")) != EOF
) {
695 pattern
= parse_pattern(optarg
);
704 return command_usage(&write_cmd
);
708 if (optind
!= argc
- 2) {
709 return command_usage(&write_cmd
);
712 if (bflag
&& pflag
) {
713 printf("-b and -p cannot be specified at the same time\n");
717 offset
= cvtnum(argv
[optind
]);
719 printf("non-numeric length argument -- %s\n", argv
[optind
]);
724 count
= cvtnum(argv
[optind
]);
726 printf("non-numeric length argument -- %s\n", argv
[optind
]);
731 if (offset
& 0x1ff) {
732 printf("offset %" PRId64
" is not sector aligned\n",
738 printf("count %d is not sector aligned\n",
744 buf
= qemu_io_alloc(count
, pattern
);
746 gettimeofday(&t1
, NULL
);
748 cnt
= do_pwrite(buf
, offset
, count
, &total
);
750 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
752 cnt
= do_write(buf
, offset
, count
, &total
);
754 gettimeofday(&t2
, NULL
);
757 printf("write failed: %s\n", strerror(-cnt
));
765 /* Finally, report back -- -C gives a parsable format */
767 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
780 " writes a range of bytes from the given offset source from multiple buffers\n"
783 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
785 " Writes into a segment of the currently open file, using a buffer\n"
786 " filled with a set pattern (0xcdcdcdcd).\n"
787 " -P, -- use different pattern to fill file\n"
788 " -C, -- report statistics in a machine parsable format\n"
789 " -q, -- quiet mode, do not show I/O statistics\n"
793 static int writev_f(int argc
, char **argv
);
795 static const cmdinfo_t writev_cmd
= {
800 .args
= "[-Cq] [-P pattern ] off len [len..]",
801 .oneline
= "writes a number of bytes at a specified offset",
805 static int writev_f(int argc
, char **argv
)
807 struct timeval t1
, t2
;
808 int Cflag
= 0, qflag
= 0;
812 /* Some compilers get confused and warn if this is not initialized. */
818 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
827 pattern
= parse_pattern(optarg
);
833 return command_usage(&writev_cmd
);
837 if (optind
> argc
- 2) {
838 return command_usage(&writev_cmd
);
841 offset
= cvtnum(argv
[optind
]);
843 printf("non-numeric length argument -- %s\n", argv
[optind
]);
848 if (offset
& 0x1ff) {
849 printf("offset %" PRId64
" is not sector aligned\n",
854 nr_iov
= argc
- optind
;
855 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
860 gettimeofday(&t1
, NULL
);
861 cnt
= do_aio_writev(&qiov
, offset
, &total
);
862 gettimeofday(&t2
, NULL
);
865 printf("writev failed: %s\n", strerror(-cnt
));
873 /* Finally, report back -- -C gives a parsable format */
875 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
881 static void multiwrite_help(void)
885 " writes a range of bytes from the given offset source from multiple buffers,\n"
886 " in a batch of requests that may be merged by qemu\n"
889 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
890 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
892 " Writes into a segment of the currently open file, using a buffer\n"
893 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
894 " by one for each request contained in the multiwrite command.\n"
895 " -P, -- use different pattern to fill file\n"
896 " -C, -- report statistics in a machine parsable format\n"
897 " -q, -- quiet mode, do not show I/O statistics\n"
901 static int multiwrite_f(int argc
, char **argv
);
903 static const cmdinfo_t multiwrite_cmd
= {
904 .name
= "multiwrite",
905 .cfunc
= multiwrite_f
,
908 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
909 .oneline
= "issues multiple write requests at once",
910 .help
= multiwrite_help
,
913 static int multiwrite_f(int argc
, char **argv
)
915 struct timeval t1
, t2
;
916 int Cflag
= 0, qflag
= 0;
919 int64_t offset
, first_offset
= 0;
920 /* Some compilers get confused and warn if this is not initialized. */
929 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
938 pattern
= parse_pattern(optarg
);
944 return command_usage(&writev_cmd
);
948 if (optind
> argc
- 2) {
949 return command_usage(&writev_cmd
);
953 for (i
= optind
; i
< argc
; i
++) {
954 if (!strcmp(argv
[i
], ";")) {
959 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
960 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
961 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
963 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
966 /* Read the offset of the request */
967 offset
= cvtnum(argv
[optind
]);
969 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
974 if (offset
& 0x1ff) {
975 printf("offset %lld is not sector aligned\n",
981 first_offset
= offset
;
984 /* Read lengths for qiov entries */
985 for (j
= optind
; j
< argc
; j
++) {
986 if (!strcmp(argv
[j
], ";")) {
994 buf
[i
] = create_iovec(&qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
995 if (buf
[i
] == NULL
) {
999 reqs
[i
].qiov
= &qiovs
[i
];
1000 reqs
[i
].sector
= offset
>> 9;
1001 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1008 /* If there were empty requests at the end, ignore them */
1011 gettimeofday(&t1
, NULL
);
1012 cnt
= do_aio_multiwrite(reqs
, nr_reqs
, &total
);
1013 gettimeofday(&t2
, NULL
);
1016 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1024 /* Finally, report back -- -C gives a parsable format */
1026 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1028 for (i
= 0; i
< nr_reqs
; i
++) {
1029 qemu_io_free(buf
[i
]);
1030 if (reqs
[i
].qiov
!= NULL
) {
1031 qemu_iovec_destroy(&qiovs
[i
]);
1052 static void aio_write_done(void *opaque
, int ret
)
1054 struct aio_ctx
*ctx
= opaque
;
1057 gettimeofday(&t2
, NULL
);
1061 printf("aio_write failed: %s\n", strerror(-ret
));
1069 /* Finally, report back -- -C gives a parsable format */
1070 t2
= tsub(t2
, ctx
->t1
);
1071 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1072 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1074 qemu_io_free(ctx
->buf
);
1078 static void aio_read_done(void *opaque
, int ret
)
1080 struct aio_ctx
*ctx
= opaque
;
1083 gettimeofday(&t2
, NULL
);
1086 printf("readv failed: %s\n", strerror(-ret
));
1091 void *cmp_buf
= malloc(ctx
->qiov
.size
);
1093 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1094 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1095 printf("Pattern verification failed at offset %"
1096 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1106 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1109 /* Finally, report back -- -C gives a parsable format */
1110 t2
= tsub(t2
, ctx
->t1
);
1111 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1112 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1114 qemu_io_free(ctx
->buf
);
1118 static void aio_read_help(void)
1122 " asynchronously reads a range of bytes from the given offset\n"
1125 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1127 " Reads a segment of the currently open file, optionally dumping it to the\n"
1128 " standard output stream (with -v option) for subsequent inspection.\n"
1129 " The read is performed asynchronously and the aio_flush command must be\n"
1130 " used to ensure all outstanding aio requests have been completed\n"
1131 " -C, -- report statistics in a machine parsable format\n"
1132 " -P, -- use a pattern to verify read data\n"
1133 " -v, -- dump buffer to standard output\n"
1134 " -q, -- quiet mode, do not show I/O statistics\n"
1138 static int aio_read_f(int argc
, char **argv
);
1140 static const cmdinfo_t aio_read_cmd
= {
1142 .cfunc
= aio_read_f
,
1145 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1146 .oneline
= "asynchronously reads a number of bytes",
1147 .help
= aio_read_help
,
1150 static int aio_read_f(int argc
, char **argv
)
1153 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
1154 BlockDriverAIOCB
*acb
;
1156 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1163 ctx
->pattern
= parse_pattern(optarg
);
1164 if (ctx
->pattern
< 0) {
1177 return command_usage(&aio_read_cmd
);
1181 if (optind
> argc
- 2) {
1183 return command_usage(&aio_read_cmd
);
1186 ctx
->offset
= cvtnum(argv
[optind
]);
1187 if (ctx
->offset
< 0) {
1188 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1194 if (ctx
->offset
& 0x1ff) {
1195 printf("offset %" PRId64
" is not sector aligned\n",
1201 nr_iov
= argc
- optind
;
1202 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1203 if (ctx
->buf
== NULL
) {
1208 gettimeofday(&ctx
->t1
, NULL
);
1209 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1210 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1220 static void aio_write_help(void)
1224 " asynchronously writes a range of bytes from the given offset source\n"
1225 " from multiple buffers\n"
1228 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1230 " Writes into a segment of the currently open file, using a buffer\n"
1231 " filled with a set pattern (0xcdcdcdcd).\n"
1232 " The write is performed asynchronously and the aio_flush command must be\n"
1233 " used to ensure all outstanding aio requests have been completed\n"
1234 " -P, -- use different pattern to fill file\n"
1235 " -C, -- report statistics in a machine parsable format\n"
1236 " -q, -- quiet mode, do not show I/O statistics\n"
1240 static int aio_write_f(int argc
, char **argv
);
1242 static const cmdinfo_t aio_write_cmd
= {
1243 .name
= "aio_write",
1244 .cfunc
= aio_write_f
,
1247 .args
= "[-Cq] [-P pattern ] off len [len..]",
1248 .oneline
= "asynchronously writes a number of bytes",
1249 .help
= aio_write_help
,
1252 static int aio_write_f(int argc
, char **argv
)
1256 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
1257 BlockDriverAIOCB
*acb
;
1259 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1268 pattern
= parse_pattern(optarg
);
1276 return command_usage(&aio_write_cmd
);
1280 if (optind
> argc
- 2) {
1282 return command_usage(&aio_write_cmd
);
1285 ctx
->offset
= cvtnum(argv
[optind
]);
1286 if (ctx
->offset
< 0) {
1287 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1293 if (ctx
->offset
& 0x1ff) {
1294 printf("offset %" PRId64
" is not sector aligned\n",
1300 nr_iov
= argc
- optind
;
1301 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1302 if (ctx
->buf
== NULL
) {
1307 gettimeofday(&ctx
->t1
, NULL
);
1308 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1309 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1319 static int aio_flush_f(int argc
, char **argv
)
1325 static const cmdinfo_t aio_flush_cmd
= {
1326 .name
= "aio_flush",
1327 .cfunc
= aio_flush_f
,
1328 .oneline
= "completes all outstanding aio requests"
1331 static int flush_f(int argc
, char **argv
)
1337 static const cmdinfo_t flush_cmd
= {
1341 .oneline
= "flush all in-core file state to disk",
1344 static int truncate_f(int argc
, char **argv
)
1349 offset
= cvtnum(argv
[1]);
1351 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1355 ret
= bdrv_truncate(bs
, offset
);
1357 printf("truncate: %s\n", strerror(-ret
));
1364 static const cmdinfo_t truncate_cmd
= {
1367 .cfunc
= truncate_f
,
1371 .oneline
= "truncates the current file at the given offset",
1374 static int length_f(int argc
, char **argv
)
1379 size
= bdrv_getlength(bs
);
1381 printf("getlength: %s\n", strerror(-size
));
1385 cvtstr(size
, s1
, sizeof(s1
));
1391 static const cmdinfo_t length_cmd
= {
1395 .oneline
= "gets the length of the current file",
1399 static int info_f(int argc
, char **argv
)
1401 BlockDriverInfo bdi
;
1402 char s1
[64], s2
[64];
1405 if (bs
->drv
&& bs
->drv
->format_name
) {
1406 printf("format name: %s\n", bs
->drv
->format_name
);
1408 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1409 printf("format name: %s\n", bs
->drv
->protocol_name
);
1412 ret
= bdrv_get_info(bs
, &bdi
);
1417 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1418 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1420 printf("cluster size: %s\n", s1
);
1421 printf("vm state offset: %s\n", s2
);
1428 static const cmdinfo_t info_cmd
= {
1432 .oneline
= "prints information about the current file",
1435 static void discard_help(void)
1439 " discards a range of bytes from the given offset\n"
1442 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1444 " Discards a segment of the currently open file.\n"
1445 " -C, -- report statistics in a machine parsable format\n"
1446 " -q, -- quiet mode, do not show I/O statistics\n"
1450 static int discard_f(int argc
, char **argv
);
1452 static const cmdinfo_t discard_cmd
= {
1458 .args
= "[-Cq] off len",
1459 .oneline
= "discards a number of bytes at a specified offset",
1460 .help
= discard_help
,
1463 static int discard_f(int argc
, char **argv
)
1465 struct timeval t1
, t2
;
1466 int Cflag
= 0, qflag
= 0;
1471 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1480 return command_usage(&discard_cmd
);
1484 if (optind
!= argc
- 2) {
1485 return command_usage(&discard_cmd
);
1488 offset
= cvtnum(argv
[optind
]);
1490 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1495 count
= cvtnum(argv
[optind
]);
1497 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1501 gettimeofday(&t1
, NULL
);
1502 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1503 count
>> BDRV_SECTOR_BITS
);
1504 gettimeofday(&t2
, NULL
);
1507 printf("discard failed: %s\n", strerror(-ret
));
1511 /* Finally, report back -- -C gives a parsable format */
1514 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1521 static int alloc_f(int argc
, char **argv
)
1524 int nb_sectors
, remaining
;
1529 offset
= cvtnum(argv
[1]);
1530 if (offset
& 0x1ff) {
1531 printf("offset %" PRId64
" is not sector aligned\n",
1537 nb_sectors
= cvtnum(argv
[2]);
1542 remaining
= nb_sectors
;
1545 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1552 cvtstr(offset
, s1
, sizeof(s1
));
1554 printf("%d/%d sectors allocated at offset %s\n",
1555 sum_alloc
, nb_sectors
, s1
);
1559 static const cmdinfo_t alloc_cmd
= {
1565 .args
= "off [sectors]",
1566 .oneline
= "checks if a sector is present in the file",
1569 static int map_f(int argc
, char **argv
)
1574 int num
, num_checked
;
1579 nb_sectors
= bs
->total_sectors
;
1582 num_checked
= MIN(nb_sectors
, INT_MAX
);
1583 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1584 retstr
= ret
? " allocated" : "not allocated";
1585 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1586 printf("[% 24" PRId64
"] % 8d/% 8d sectors %s at offset %s (%d)\n",
1587 offset
<< 9ULL, num
, num_checked
, retstr
, s1
, ret
);
1591 } while (offset
< bs
->total_sectors
);
1596 static const cmdinfo_t map_cmd
= {
1602 .oneline
= "prints the allocated areas of a file",
1606 static int close_f(int argc
, char **argv
)
1613 static const cmdinfo_t close_cmd
= {
1617 .oneline
= "close the current open file",
1620 static int openfile(char *name
, int flags
, int growable
)
1623 fprintf(stderr
, "file open already, try 'help close'\n");
1628 if (bdrv_file_open(&bs
, name
, flags
)) {
1629 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1633 bs
= bdrv_new("hda");
1635 if (bdrv_open(bs
, name
, flags
, NULL
) < 0) {
1636 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1646 static void open_help(void)
1650 " opens a new file in the requested mode\n"
1653 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1655 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1656 " -r, -- open file read-only\n"
1657 " -s, -- use snapshot file\n"
1658 " -n, -- disable host cache\n"
1659 " -g, -- allow file to grow (only applies to protocols)"
1663 static int open_f(int argc
, char **argv
);
1665 static const cmdinfo_t open_cmd
= {
1671 .flags
= CMD_NOFILE_OK
,
1672 .args
= "[-Crsn] [path]",
1673 .oneline
= "open the file specified by path",
1677 static int open_f(int argc
, char **argv
)
1684 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
1687 flags
|= BDRV_O_SNAPSHOT
;
1690 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1699 return command_usage(&open_cmd
);
1704 flags
|= BDRV_O_RDWR
;
1707 if (optind
!= argc
- 1) {
1708 return command_usage(&open_cmd
);
1711 return openfile(argv
[optind
], flags
, growable
);
1714 static int init_args_command(int index
)
1716 /* only one device allowed so far */
1723 static int init_check_command(const cmdinfo_t
*ct
)
1725 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
1728 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1729 fprintf(stderr
, "no file open, try 'help open'\n");
1735 static void usage(const char *name
)
1738 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1739 "QEMU Disk exerciser\n"
1741 " -c, --cmd command to execute\n"
1742 " -r, --read-only export read-only\n"
1743 " -s, --snapshot use snapshot file\n"
1744 " -n, --nocache disable host cache\n"
1745 " -g, --growable allow file to grow (only applies to protocols)\n"
1746 " -m, --misalign misalign allocations for O_DIRECT\n"
1747 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1748 " -h, --help display this help and exit\n"
1749 " -V, --version output version information and exit\n"
1755 int main(int argc
, char **argv
)
1759 const char *sopt
= "hVc:rsnmgk";
1760 const struct option lopt
[] = {
1761 { "help", 0, NULL
, 'h' },
1762 { "version", 0, NULL
, 'V' },
1763 { "offset", 1, NULL
, 'o' },
1764 { "cmd", 1, NULL
, 'c' },
1765 { "read-only", 0, NULL
, 'r' },
1766 { "snapshot", 0, NULL
, 's' },
1767 { "nocache", 0, NULL
, 'n' },
1768 { "misalign", 0, NULL
, 'm' },
1769 { "growable", 0, NULL
, 'g' },
1770 { "native-aio", 0, NULL
, 'k' },
1771 { NULL
, 0, NULL
, 0 }
1777 progname
= basename(argv
[0]);
1779 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1782 flags
|= BDRV_O_SNAPSHOT
;
1785 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1788 add_user_command(optarg
);
1800 flags
|= BDRV_O_NATIVE_AIO
;
1803 printf("%s version %s\n", progname
, VERSION
);
1814 if ((argc
- optind
) > 1) {
1821 /* initialize commands */
1824 add_command(&open_cmd
);
1825 add_command(&close_cmd
);
1826 add_command(&read_cmd
);
1827 add_command(&readv_cmd
);
1828 add_command(&write_cmd
);
1829 add_command(&writev_cmd
);
1830 add_command(&multiwrite_cmd
);
1831 add_command(&aio_read_cmd
);
1832 add_command(&aio_write_cmd
);
1833 add_command(&aio_flush_cmd
);
1834 add_command(&flush_cmd
);
1835 add_command(&truncate_cmd
);
1836 add_command(&length_cmd
);
1837 add_command(&info_cmd
);
1838 add_command(&discard_cmd
);
1839 add_command(&alloc_cmd
);
1840 add_command(&map_cmd
);
1842 add_args_command(init_args_command
);
1843 add_check_command(init_check_command
);
1845 /* open the device */
1847 flags
|= BDRV_O_RDWR
;
1850 if ((argc
- optind
) == 1) {
1851 openfile(argv
[optind
], flags
, growable
);
1856 * Make sure all outstanding requests get flushed the program exits.