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"
20 #include "trace/control.h"
22 #define VERSION "0.0.1"
24 #define CMD_NOFILE_OK 0x01
27 static BlockDriverState
*bs
;
32 * Parse the pattern argument to various sub-commands.
34 * Because the pattern is used as an argument to memset it must evaluate
35 * to an unsigned integer that fits into a single byte.
37 static int parse_pattern(const char *arg
)
42 pattern
= strtol(arg
, &endptr
, 0);
43 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
44 printf("%s is not a valid pattern byte\n", arg
);
52 * Memory allocation helpers.
54 * Make sure memory is aligned by default, or purposefully misaligned if
55 * that is specified on the command line.
58 #define MISALIGN_OFFSET 16
59 static void *qemu_io_alloc(size_t len
, int pattern
)
64 len
+= MISALIGN_OFFSET
;
66 buf
= qemu_blockalign(bs
, len
);
67 memset(buf
, pattern
, len
);
69 buf
+= MISALIGN_OFFSET
;
74 static void qemu_io_free(void *p
)
82 static void dump_buffer(const void *buffer
, int64_t offset
, int len
)
87 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
90 printf("%08" PRIx64
": ", offset
+ i
);
91 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
95 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
106 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
107 int count
, int total
, int cnt
, int Cflag
)
109 char s1
[64], s2
[64], ts
[64];
111 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
113 cvtstr((double)total
, s1
, sizeof(s1
));
114 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
115 printf("%s %d/%d bytes at offset %" PRId64
"\n",
116 op
, total
, count
, offset
);
117 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
118 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
119 } else {/* bytes,ops,time,bytes/sec,ops/sec */
120 printf("%d,%d,%s,%.3f,%.3f\n",
122 tdiv((double)total
, *t
),
123 tdiv((double)cnt
, *t
));
128 * Parse multiple length statements for vectored I/O, and construct an I/O
129 * vector matching it.
132 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
134 size_t *sizes
= g_new0(size_t, nr_iov
);
140 for (i
= 0; i
< nr_iov
; i
++) {
146 printf("non-numeric length argument -- %s\n", arg
);
150 /* should be SIZE_T_MAX, but that doesn't exist */
152 printf("too large length argument -- %s\n", arg
);
157 printf("length argument %" PRId64
158 " is not sector aligned\n", len
);
166 qemu_iovec_init(qiov
, nr_iov
);
168 buf
= p
= qemu_io_alloc(count
, pattern
);
170 for (i
= 0; i
< nr_iov
; i
++) {
171 qemu_iovec_add(qiov
, p
, sizes
[i
]);
180 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
184 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
192 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
196 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
204 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
206 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
213 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
215 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
230 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
232 CoWriteZeroes
*data
= opaque
;
234 data
->ret
= bdrv_co_write_zeroes(bs
, data
->offset
/ BDRV_SECTOR_SIZE
,
235 data
->count
/ BDRV_SECTOR_SIZE
);
238 *data
->total
= data
->ret
;
242 *data
->total
= data
->count
;
245 static int do_co_write_zeroes(int64_t offset
, int count
, int *total
)
248 CoWriteZeroes data
= {
255 co
= qemu_coroutine_create(co_write_zeroes_entry
);
256 qemu_coroutine_enter(co
, &data
);
267 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
269 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
276 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
278 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
285 #define NOT_DONE 0x7fffffff
286 static void aio_rw_done(void *opaque
, int ret
)
288 *(int *)opaque
= ret
;
291 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
293 int async_ret
= NOT_DONE
;
295 bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
296 aio_rw_done
, &async_ret
);
297 while (async_ret
== NOT_DONE
) {
302 return async_ret
< 0 ? async_ret
: 1;
305 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
307 int async_ret
= NOT_DONE
;
309 bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
310 aio_rw_done
, &async_ret
);
311 while (async_ret
== NOT_DONE
) {
316 return async_ret
< 0 ? async_ret
: 1;
319 struct multiwrite_async_ret
{
324 static void multiwrite_cb(void *opaque
, int ret
)
326 struct multiwrite_async_ret
*async_ret
= opaque
;
328 async_ret
->num_done
++;
330 async_ret
->error
= ret
;
334 static int do_aio_multiwrite(BlockRequest
* reqs
, int num_reqs
, int *total
)
337 struct multiwrite_async_ret async_ret
= {
343 for (i
= 0; i
< num_reqs
; i
++) {
344 reqs
[i
].cb
= multiwrite_cb
;
345 reqs
[i
].opaque
= &async_ret
;
346 *total
+= reqs
[i
].qiov
->size
;
349 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
354 while (async_ret
.num_done
< num_reqs
) {
358 return async_ret
.error
< 0 ? async_ret
.error
: 1;
361 static void read_help(void)
365 " reads a range of bytes from the given offset\n"
368 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
370 " Reads a segment of the currently open file, optionally dumping it to the\n"
371 " standard output stream (with -v option) for subsequent inspection.\n"
372 " -b, -- read from the VM state rather than the virtual disk\n"
373 " -C, -- report statistics in a machine parsable format\n"
374 " -l, -- length for pattern verification (only with -P)\n"
375 " -p, -- use bdrv_pread to read the file\n"
376 " -P, -- use a pattern to verify read data\n"
377 " -q, -- quiet mode, do not show I/O statistics\n"
378 " -s, -- start offset for pattern verification (only with -P)\n"
379 " -v, -- dump buffer to standard output\n"
383 static int read_f(int argc
, char **argv
);
385 static const cmdinfo_t read_cmd
= {
391 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
392 .oneline
= "reads a number of bytes at a specified offset",
396 static int read_f(int argc
, char **argv
)
398 struct timeval t1
, t2
;
399 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
400 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
405 /* Some compilers get confused and warn if this is not initialized. */
407 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
409 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
419 pattern_count
= cvtnum(optarg
);
420 if (pattern_count
< 0) {
421 printf("non-numeric length argument -- %s\n", optarg
);
430 pattern
= parse_pattern(optarg
);
440 pattern_offset
= cvtnum(optarg
);
441 if (pattern_offset
< 0) {
442 printf("non-numeric length argument -- %s\n", optarg
);
450 return command_usage(&read_cmd
);
454 if (optind
!= argc
- 2) {
455 return command_usage(&read_cmd
);
458 if (bflag
&& pflag
) {
459 printf("-b and -p cannot be specified at the same time\n");
463 offset
= cvtnum(argv
[optind
]);
465 printf("non-numeric length argument -- %s\n", argv
[optind
]);
470 count
= cvtnum(argv
[optind
]);
472 printf("non-numeric length argument -- %s\n", argv
[optind
]);
476 if (!Pflag
&& (lflag
|| sflag
)) {
477 return command_usage(&read_cmd
);
481 pattern_count
= count
- pattern_offset
;
484 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
485 printf("pattern verification range exceeds end of read data\n");
490 if (offset
& 0x1ff) {
491 printf("offset %" PRId64
" is not sector aligned\n",
496 printf("count %d is not sector aligned\n",
502 buf
= qemu_io_alloc(count
, 0xab);
504 gettimeofday(&t1
, NULL
);
506 cnt
= do_pread(buf
, offset
, count
, &total
);
508 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
510 cnt
= do_read(buf
, offset
, count
, &total
);
512 gettimeofday(&t2
, NULL
);
515 printf("read failed: %s\n", strerror(-cnt
));
520 void *cmp_buf
= g_malloc(pattern_count
);
521 memset(cmp_buf
, pattern
, pattern_count
);
522 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
523 printf("Pattern verification failed at offset %"
524 PRId64
", %d bytes\n",
525 offset
+ pattern_offset
, pattern_count
);
535 dump_buffer(buf
, offset
, count
);
538 /* Finally, report back -- -C gives a parsable format */
540 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
548 static void readv_help(void)
552 " reads a range of bytes from the given offset into multiple buffers\n"
555 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
557 " Reads a segment of the currently open file, optionally dumping it to the\n"
558 " standard output stream (with -v option) for subsequent inspection.\n"
559 " Uses multiple iovec buffers if more than one byte range is specified.\n"
560 " -C, -- report statistics in a machine parsable format\n"
561 " -P, -- use a pattern to verify read data\n"
562 " -v, -- dump buffer to standard output\n"
563 " -q, -- quiet mode, do not show I/O statistics\n"
567 static int readv_f(int argc
, char **argv
);
569 static const cmdinfo_t readv_cmd
= {
574 .args
= "[-Cqv] [-P pattern ] off len [len..]",
575 .oneline
= "reads a number of bytes at a specified offset",
579 static int readv_f(int argc
, char **argv
)
581 struct timeval t1
, t2
;
582 int Cflag
= 0, qflag
= 0, vflag
= 0;
586 /* Some compilers get confused and warn if this is not initialized. */
593 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
600 pattern
= parse_pattern(optarg
);
612 return command_usage(&readv_cmd
);
616 if (optind
> argc
- 2) {
617 return command_usage(&readv_cmd
);
621 offset
= cvtnum(argv
[optind
]);
623 printf("non-numeric length argument -- %s\n", argv
[optind
]);
628 if (offset
& 0x1ff) {
629 printf("offset %" PRId64
" is not sector aligned\n",
634 nr_iov
= argc
- optind
;
635 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
640 gettimeofday(&t1
, NULL
);
641 cnt
= do_aio_readv(&qiov
, offset
, &total
);
642 gettimeofday(&t2
, NULL
);
645 printf("readv failed: %s\n", strerror(-cnt
));
650 void *cmp_buf
= g_malloc(qiov
.size
);
651 memset(cmp_buf
, pattern
, qiov
.size
);
652 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
653 printf("Pattern verification failed at offset %"
654 PRId64
", %zd bytes\n", offset
, qiov
.size
);
664 dump_buffer(buf
, offset
, qiov
.size
);
667 /* Finally, report back -- -C gives a parsable format */
669 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
676 static void write_help(void)
680 " writes a range of bytes from the given offset\n"
683 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
685 " Writes into a segment of the currently open file, using a buffer\n"
686 " filled with a set pattern (0xcdcdcdcd).\n"
687 " -b, -- write to the VM state rather than the virtual disk\n"
688 " -p, -- use bdrv_pwrite to write the file\n"
689 " -P, -- use different pattern to fill file\n"
690 " -C, -- report statistics in a machine parsable format\n"
691 " -q, -- quiet mode, do not show I/O statistics\n"
692 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
696 static int write_f(int argc
, char **argv
);
698 static const cmdinfo_t write_cmd
= {
704 .args
= "[-bCpqz] [-P pattern ] off len",
705 .oneline
= "writes a number of bytes at a specified offset",
709 static int write_f(int argc
, char **argv
)
711 struct timeval t1
, t2
;
712 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
717 /* Some compilers get confused and warn if this is not initialized. */
721 while ((c
= getopt(argc
, argv
, "bCpP:qz")) != EOF
) {
734 pattern
= parse_pattern(optarg
);
746 return command_usage(&write_cmd
);
750 if (optind
!= argc
- 2) {
751 return command_usage(&write_cmd
);
754 if (bflag
+ pflag
+ zflag
> 1) {
755 printf("-b, -p, or -z cannot be specified at the same time\n");
759 if (zflag
&& Pflag
) {
760 printf("-z and -P cannot be specified at the same time\n");
764 offset
= cvtnum(argv
[optind
]);
766 printf("non-numeric length argument -- %s\n", argv
[optind
]);
771 count
= cvtnum(argv
[optind
]);
773 printf("non-numeric length argument -- %s\n", argv
[optind
]);
778 if (offset
& 0x1ff) {
779 printf("offset %" PRId64
" is not sector aligned\n",
785 printf("count %d is not sector aligned\n",
792 buf
= qemu_io_alloc(count
, pattern
);
795 gettimeofday(&t1
, NULL
);
797 cnt
= do_pwrite(buf
, offset
, count
, &total
);
799 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
801 cnt
= do_co_write_zeroes(offset
, count
, &total
);
803 cnt
= do_write(buf
, offset
, count
, &total
);
805 gettimeofday(&t2
, NULL
);
808 printf("write failed: %s\n", strerror(-cnt
));
816 /* Finally, report back -- -C gives a parsable format */
818 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
833 " writes a range of bytes from the given offset source from multiple buffers\n"
836 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
838 " Writes into a segment of the currently open file, using a buffer\n"
839 " filled with a set pattern (0xcdcdcdcd).\n"
840 " -P, -- use different pattern to fill file\n"
841 " -C, -- report statistics in a machine parsable format\n"
842 " -q, -- quiet mode, do not show I/O statistics\n"
846 static int writev_f(int argc
, char **argv
);
848 static const cmdinfo_t writev_cmd
= {
853 .args
= "[-Cq] [-P pattern ] off len [len..]",
854 .oneline
= "writes a number of bytes at a specified offset",
858 static int writev_f(int argc
, char **argv
)
860 struct timeval t1
, t2
;
861 int Cflag
= 0, qflag
= 0;
865 /* Some compilers get confused and warn if this is not initialized. */
871 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
880 pattern
= parse_pattern(optarg
);
886 return command_usage(&writev_cmd
);
890 if (optind
> argc
- 2) {
891 return command_usage(&writev_cmd
);
894 offset
= cvtnum(argv
[optind
]);
896 printf("non-numeric length argument -- %s\n", argv
[optind
]);
901 if (offset
& 0x1ff) {
902 printf("offset %" PRId64
" is not sector aligned\n",
907 nr_iov
= argc
- optind
;
908 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
913 gettimeofday(&t1
, NULL
);
914 cnt
= do_aio_writev(&qiov
, offset
, &total
);
915 gettimeofday(&t2
, NULL
);
918 printf("writev failed: %s\n", strerror(-cnt
));
926 /* Finally, report back -- -C gives a parsable format */
928 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
934 static void multiwrite_help(void)
938 " writes a range of bytes from the given offset source from multiple buffers,\n"
939 " in a batch of requests that may be merged by qemu\n"
942 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
943 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
945 " Writes into a segment of the currently open file, using a buffer\n"
946 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
947 " by one for each request contained in the multiwrite command.\n"
948 " -P, -- use different pattern to fill file\n"
949 " -C, -- report statistics in a machine parsable format\n"
950 " -q, -- quiet mode, do not show I/O statistics\n"
954 static int multiwrite_f(int argc
, char **argv
);
956 static const cmdinfo_t multiwrite_cmd
= {
957 .name
= "multiwrite",
958 .cfunc
= multiwrite_f
,
961 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
962 .oneline
= "issues multiple write requests at once",
963 .help
= multiwrite_help
,
966 static int multiwrite_f(int argc
, char **argv
)
968 struct timeval t1
, t2
;
969 int Cflag
= 0, qflag
= 0;
972 int64_t offset
, first_offset
= 0;
973 /* Some compilers get confused and warn if this is not initialized. */
982 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
991 pattern
= parse_pattern(optarg
);
997 return command_usage(&writev_cmd
);
1001 if (optind
> argc
- 2) {
1002 return command_usage(&writev_cmd
);
1006 for (i
= optind
; i
< argc
; i
++) {
1007 if (!strcmp(argv
[i
], ";")) {
1012 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
1013 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
1014 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
1016 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1019 /* Read the offset of the request */
1020 offset
= cvtnum(argv
[optind
]);
1022 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
1027 if (offset
& 0x1ff) {
1028 printf("offset %lld is not sector aligned\n",
1034 first_offset
= offset
;
1037 /* Read lengths for qiov entries */
1038 for (j
= optind
; j
< argc
; j
++) {
1039 if (!strcmp(argv
[j
], ";")) {
1044 nr_iov
= j
- optind
;
1047 buf
[i
] = create_iovec(&qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1048 if (buf
[i
] == NULL
) {
1052 reqs
[i
].qiov
= &qiovs
[i
];
1053 reqs
[i
].sector
= offset
>> 9;
1054 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1061 /* If there were empty requests at the end, ignore them */
1064 gettimeofday(&t1
, NULL
);
1065 cnt
= do_aio_multiwrite(reqs
, nr_reqs
, &total
);
1066 gettimeofday(&t2
, NULL
);
1069 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1077 /* Finally, report back -- -C gives a parsable format */
1079 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1081 for (i
= 0; i
< nr_reqs
; i
++) {
1082 qemu_io_free(buf
[i
]);
1083 if (reqs
[i
].qiov
!= NULL
) {
1084 qemu_iovec_destroy(&qiovs
[i
]);
1105 static void aio_write_done(void *opaque
, int ret
)
1107 struct aio_ctx
*ctx
= opaque
;
1110 gettimeofday(&t2
, NULL
);
1114 printf("aio_write failed: %s\n", strerror(-ret
));
1122 /* Finally, report back -- -C gives a parsable format */
1123 t2
= tsub(t2
, ctx
->t1
);
1124 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1125 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1127 qemu_io_free(ctx
->buf
);
1131 static void aio_read_done(void *opaque
, int ret
)
1133 struct aio_ctx
*ctx
= opaque
;
1136 gettimeofday(&t2
, NULL
);
1139 printf("readv failed: %s\n", strerror(-ret
));
1144 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1146 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1147 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1148 printf("Pattern verification failed at offset %"
1149 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1159 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1162 /* Finally, report back -- -C gives a parsable format */
1163 t2
= tsub(t2
, ctx
->t1
);
1164 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1165 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1167 qemu_io_free(ctx
->buf
);
1171 static void aio_read_help(void)
1175 " asynchronously reads a range of bytes from the given offset\n"
1178 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1180 " Reads a segment of the currently open file, optionally dumping it to the\n"
1181 " standard output stream (with -v option) for subsequent inspection.\n"
1182 " The read is performed asynchronously and the aio_flush command must be\n"
1183 " used to ensure all outstanding aio requests have been completed.\n"
1184 " -C, -- report statistics in a machine parsable format\n"
1185 " -P, -- use a pattern to verify read data\n"
1186 " -v, -- dump buffer to standard output\n"
1187 " -q, -- quiet mode, do not show I/O statistics\n"
1191 static int aio_read_f(int argc
, char **argv
);
1193 static const cmdinfo_t aio_read_cmd
= {
1195 .cfunc
= aio_read_f
,
1198 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1199 .oneline
= "asynchronously reads a number of bytes",
1200 .help
= aio_read_help
,
1203 static int aio_read_f(int argc
, char **argv
)
1206 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1208 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1215 ctx
->pattern
= parse_pattern(optarg
);
1216 if (ctx
->pattern
< 0) {
1229 return command_usage(&aio_read_cmd
);
1233 if (optind
> argc
- 2) {
1235 return command_usage(&aio_read_cmd
);
1238 ctx
->offset
= cvtnum(argv
[optind
]);
1239 if (ctx
->offset
< 0) {
1240 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1246 if (ctx
->offset
& 0x1ff) {
1247 printf("offset %" PRId64
" is not sector aligned\n",
1253 nr_iov
= argc
- optind
;
1254 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1255 if (ctx
->buf
== NULL
) {
1260 gettimeofday(&ctx
->t1
, NULL
);
1261 bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1262 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1266 static void aio_write_help(void)
1270 " asynchronously writes a range of bytes from the given offset source\n"
1271 " from multiple buffers\n"
1274 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1276 " Writes into a segment of the currently open file, using a buffer\n"
1277 " filled with a set pattern (0xcdcdcdcd).\n"
1278 " The write is performed asynchronously and the aio_flush command must be\n"
1279 " used to ensure all outstanding aio requests have been completed.\n"
1280 " -P, -- use different pattern to fill file\n"
1281 " -C, -- report statistics in a machine parsable format\n"
1282 " -q, -- quiet mode, do not show I/O statistics\n"
1286 static int aio_write_f(int argc
, char **argv
);
1288 static const cmdinfo_t aio_write_cmd
= {
1289 .name
= "aio_write",
1290 .cfunc
= aio_write_f
,
1293 .args
= "[-Cq] [-P pattern ] off len [len..]",
1294 .oneline
= "asynchronously writes a number of bytes",
1295 .help
= aio_write_help
,
1298 static int aio_write_f(int argc
, char **argv
)
1302 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1304 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1313 pattern
= parse_pattern(optarg
);
1321 return command_usage(&aio_write_cmd
);
1325 if (optind
> argc
- 2) {
1327 return command_usage(&aio_write_cmd
);
1330 ctx
->offset
= cvtnum(argv
[optind
]);
1331 if (ctx
->offset
< 0) {
1332 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1338 if (ctx
->offset
& 0x1ff) {
1339 printf("offset %" PRId64
" is not sector aligned\n",
1345 nr_iov
= argc
- optind
;
1346 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1347 if (ctx
->buf
== NULL
) {
1352 gettimeofday(&ctx
->t1
, NULL
);
1353 bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1354 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1358 static int aio_flush_f(int argc
, char **argv
)
1364 static const cmdinfo_t aio_flush_cmd
= {
1365 .name
= "aio_flush",
1366 .cfunc
= aio_flush_f
,
1367 .oneline
= "completes all outstanding aio requests"
1370 static int flush_f(int argc
, char **argv
)
1376 static const cmdinfo_t flush_cmd
= {
1380 .oneline
= "flush all in-core file state to disk",
1383 static int truncate_f(int argc
, char **argv
)
1388 offset
= cvtnum(argv
[1]);
1390 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1394 ret
= bdrv_truncate(bs
, offset
);
1396 printf("truncate: %s\n", strerror(-ret
));
1403 static const cmdinfo_t truncate_cmd
= {
1406 .cfunc
= truncate_f
,
1410 .oneline
= "truncates the current file at the given offset",
1413 static int length_f(int argc
, char **argv
)
1418 size
= bdrv_getlength(bs
);
1420 printf("getlength: %s\n", strerror(-size
));
1424 cvtstr(size
, s1
, sizeof(s1
));
1430 static const cmdinfo_t length_cmd
= {
1434 .oneline
= "gets the length of the current file",
1438 static int info_f(int argc
, char **argv
)
1440 BlockDriverInfo bdi
;
1441 char s1
[64], s2
[64];
1444 if (bs
->drv
&& bs
->drv
->format_name
) {
1445 printf("format name: %s\n", bs
->drv
->format_name
);
1447 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1448 printf("format name: %s\n", bs
->drv
->protocol_name
);
1451 ret
= bdrv_get_info(bs
, &bdi
);
1456 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1457 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1459 printf("cluster size: %s\n", s1
);
1460 printf("vm state offset: %s\n", s2
);
1467 static const cmdinfo_t info_cmd
= {
1471 .oneline
= "prints information about the current file",
1474 static void discard_help(void)
1478 " discards a range of bytes from the given offset\n"
1481 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1483 " Discards a segment of the currently open file.\n"
1484 " -C, -- report statistics in a machine parsable format\n"
1485 " -q, -- quiet mode, do not show I/O statistics\n"
1489 static int discard_f(int argc
, char **argv
);
1491 static const cmdinfo_t discard_cmd
= {
1497 .args
= "[-Cq] off len",
1498 .oneline
= "discards a number of bytes at a specified offset",
1499 .help
= discard_help
,
1502 static int discard_f(int argc
, char **argv
)
1504 struct timeval t1
, t2
;
1505 int Cflag
= 0, qflag
= 0;
1510 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1519 return command_usage(&discard_cmd
);
1523 if (optind
!= argc
- 2) {
1524 return command_usage(&discard_cmd
);
1527 offset
= cvtnum(argv
[optind
]);
1529 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1534 count
= cvtnum(argv
[optind
]);
1536 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1540 gettimeofday(&t1
, NULL
);
1541 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1542 count
>> BDRV_SECTOR_BITS
);
1543 gettimeofday(&t2
, NULL
);
1546 printf("discard failed: %s\n", strerror(-ret
));
1550 /* Finally, report back -- -C gives a parsable format */
1553 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1560 static int alloc_f(int argc
, char **argv
)
1563 int nb_sectors
, remaining
;
1568 offset
= cvtnum(argv
[1]);
1569 if (offset
& 0x1ff) {
1570 printf("offset %" PRId64
" is not sector aligned\n",
1576 nb_sectors
= cvtnum(argv
[2]);
1581 remaining
= nb_sectors
;
1584 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1591 cvtstr(offset
, s1
, sizeof(s1
));
1593 printf("%d/%d sectors allocated at offset %s\n",
1594 sum_alloc
, nb_sectors
, s1
);
1598 static const cmdinfo_t alloc_cmd
= {
1604 .args
= "off [sectors]",
1605 .oneline
= "checks if a sector is present in the file",
1608 static int map_f(int argc
, char **argv
)
1613 int num
, num_checked
;
1618 nb_sectors
= bs
->total_sectors
;
1621 num_checked
= MIN(nb_sectors
, INT_MAX
);
1622 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1623 retstr
= ret
? " allocated" : "not allocated";
1624 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1625 printf("[% 24" PRId64
"] % 8d/% 8d sectors %s at offset %s (%d)\n",
1626 offset
<< 9ULL, num
, num_checked
, retstr
, s1
, ret
);
1630 } while (offset
< bs
->total_sectors
);
1635 static const cmdinfo_t map_cmd
= {
1641 .oneline
= "prints the allocated areas of a file",
1645 static int close_f(int argc
, char **argv
)
1652 static const cmdinfo_t close_cmd
= {
1656 .oneline
= "close the current open file",
1659 static int openfile(char *name
, int flags
, int growable
)
1662 fprintf(stderr
, "file open already, try 'help close'\n");
1667 if (bdrv_file_open(&bs
, name
, flags
)) {
1668 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1672 bs
= bdrv_new("hda");
1674 if (bdrv_open(bs
, name
, flags
, NULL
) < 0) {
1675 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1685 static void open_help(void)
1689 " opens a new file in the requested mode\n"
1692 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1694 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1695 " -r, -- open file read-only\n"
1696 " -s, -- use snapshot file\n"
1697 " -n, -- disable host cache\n"
1698 " -g, -- allow file to grow (only applies to protocols)"
1702 static int open_f(int argc
, char **argv
);
1704 static const cmdinfo_t open_cmd
= {
1710 .flags
= CMD_NOFILE_OK
,
1711 .args
= "[-Crsn] [path]",
1712 .oneline
= "open the file specified by path",
1716 static int open_f(int argc
, char **argv
)
1723 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
1726 flags
|= BDRV_O_SNAPSHOT
;
1729 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1738 return command_usage(&open_cmd
);
1743 flags
|= BDRV_O_RDWR
;
1746 if (optind
!= argc
- 1) {
1747 return command_usage(&open_cmd
);
1750 return openfile(argv
[optind
], flags
, growable
);
1753 static int init_args_command(int index
)
1755 /* only one device allowed so far */
1762 static int init_check_command(const cmdinfo_t
*ct
)
1764 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
1767 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1768 fprintf(stderr
, "no file open, try 'help open'\n");
1774 static void usage(const char *name
)
1777 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1778 "QEMU Disk exerciser\n"
1780 " -c, --cmd command to execute\n"
1781 " -r, --read-only export read-only\n"
1782 " -s, --snapshot use snapshot file\n"
1783 " -n, --nocache disable host cache\n"
1784 " -g, --growable allow file to grow (only applies to protocols)\n"
1785 " -m, --misalign misalign allocations for O_DIRECT\n"
1786 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1787 " -T, --trace FILE enable trace events listed in the given file\n"
1788 " -h, --help display this help and exit\n"
1789 " -V, --version output version information and exit\n"
1795 int main(int argc
, char **argv
)
1799 const char *sopt
= "hVc:rsnmgkT:";
1800 const struct option lopt
[] = {
1801 { "help", 0, NULL
, 'h' },
1802 { "version", 0, NULL
, 'V' },
1803 { "offset", 1, NULL
, 'o' },
1804 { "cmd", 1, NULL
, 'c' },
1805 { "read-only", 0, NULL
, 'r' },
1806 { "snapshot", 0, NULL
, 's' },
1807 { "nocache", 0, NULL
, 'n' },
1808 { "misalign", 0, NULL
, 'm' },
1809 { "growable", 0, NULL
, 'g' },
1810 { "native-aio", 0, NULL
, 'k' },
1811 { "trace", 1, NULL
, 'T' },
1812 { NULL
, 0, NULL
, 0 }
1818 progname
= basename(argv
[0]);
1820 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1823 flags
|= BDRV_O_SNAPSHOT
;
1826 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1829 add_user_command(optarg
);
1841 flags
|= BDRV_O_NATIVE_AIO
;
1844 if (!trace_backend_init(optarg
, NULL
)) {
1845 exit(1); /* error message will have been printed */
1849 printf("%s version %s\n", progname
, VERSION
);
1860 if ((argc
- optind
) > 1) {
1867 qemu_init_main_loop();
1869 /* initialize commands */
1872 add_command(&open_cmd
);
1873 add_command(&close_cmd
);
1874 add_command(&read_cmd
);
1875 add_command(&readv_cmd
);
1876 add_command(&write_cmd
);
1877 add_command(&writev_cmd
);
1878 add_command(&multiwrite_cmd
);
1879 add_command(&aio_read_cmd
);
1880 add_command(&aio_write_cmd
);
1881 add_command(&aio_flush_cmd
);
1882 add_command(&flush_cmd
);
1883 add_command(&truncate_cmd
);
1884 add_command(&length_cmd
);
1885 add_command(&info_cmd
);
1886 add_command(&discard_cmd
);
1887 add_command(&alloc_cmd
);
1888 add_command(&map_cmd
);
1890 add_args_command(init_args_command
);
1891 add_check_command(init_check_command
);
1893 /* open the device */
1895 flags
|= BDRV_O_RDWR
;
1898 if ((argc
- optind
) == 1) {
1899 openfile(argv
[optind
], flags
, growable
);
1904 * Make sure all outstanding requests complete before the program exits.