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 "main-loop.h"
19 #include "block_int.h"
21 #include "trace/control.h"
23 #define VERSION "0.0.1"
25 #define CMD_NOFILE_OK 0x01
28 static BlockDriverState
*bs
;
33 * Parse the pattern argument to various sub-commands.
35 * Because the pattern is used as an argument to memset it must evaluate
36 * to an unsigned integer that fits into a single byte.
38 static int parse_pattern(const char *arg
)
43 pattern
= strtol(arg
, &endptr
, 0);
44 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
45 printf("%s is not a valid pattern byte\n", arg
);
53 * Memory allocation helpers.
55 * Make sure memory is aligned by default, or purposefully misaligned if
56 * that is specified on the command line.
59 #define MISALIGN_OFFSET 16
60 static void *qemu_io_alloc(size_t len
, int pattern
)
65 len
+= MISALIGN_OFFSET
;
67 buf
= qemu_blockalign(bs
, len
);
68 memset(buf
, pattern
, len
);
70 buf
+= MISALIGN_OFFSET
;
75 static void qemu_io_free(void *p
)
83 static void dump_buffer(const void *buffer
, int64_t offset
, int len
)
88 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
91 printf("%08" PRIx64
": ", offset
+ i
);
92 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
96 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
107 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
108 int count
, int total
, int cnt
, int Cflag
)
110 char s1
[64], s2
[64], ts
[64];
112 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
114 cvtstr((double)total
, s1
, sizeof(s1
));
115 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
116 printf("%s %d/%d bytes at offset %" PRId64
"\n",
117 op
, total
, count
, offset
);
118 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
119 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
120 } else {/* bytes,ops,time,bytes/sec,ops/sec */
121 printf("%d,%d,%s,%.3f,%.3f\n",
123 tdiv((double)total
, *t
),
124 tdiv((double)cnt
, *t
));
129 * Parse multiple length statements for vectored I/O, and construct an I/O
130 * vector matching it.
133 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
135 size_t *sizes
= g_new0(size_t, nr_iov
);
141 for (i
= 0; i
< nr_iov
; i
++) {
147 printf("non-numeric length argument -- %s\n", arg
);
151 /* should be SIZE_T_MAX, but that doesn't exist */
153 printf("too large length argument -- %s\n", arg
);
158 printf("length argument %" PRId64
159 " is not sector aligned\n", len
);
167 qemu_iovec_init(qiov
, nr_iov
);
169 buf
= p
= qemu_io_alloc(count
, pattern
);
171 for (i
= 0; i
< nr_iov
; i
++) {
172 qemu_iovec_add(qiov
, p
, sizes
[i
]);
181 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
185 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
193 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
197 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
205 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
207 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
214 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
216 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
231 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
233 CoWriteZeroes
*data
= opaque
;
235 data
->ret
= bdrv_co_write_zeroes(bs
, data
->offset
/ BDRV_SECTOR_SIZE
,
236 data
->count
/ BDRV_SECTOR_SIZE
);
239 *data
->total
= data
->ret
;
243 *data
->total
= data
->count
;
246 static int do_co_write_zeroes(int64_t offset
, int count
, int *total
)
249 CoWriteZeroes data
= {
256 co
= qemu_coroutine_create(co_write_zeroes_entry
);
257 qemu_coroutine_enter(co
, &data
);
268 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
270 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
277 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
279 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
286 #define NOT_DONE 0x7fffffff
287 static void aio_rw_done(void *opaque
, int ret
)
289 *(int *)opaque
= ret
;
292 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
294 int async_ret
= NOT_DONE
;
296 bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
297 aio_rw_done
, &async_ret
);
298 while (async_ret
== NOT_DONE
) {
299 main_loop_wait(false);
303 return async_ret
< 0 ? async_ret
: 1;
306 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
308 int async_ret
= NOT_DONE
;
310 bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
311 aio_rw_done
, &async_ret
);
312 while (async_ret
== NOT_DONE
) {
313 main_loop_wait(false);
317 return async_ret
< 0 ? async_ret
: 1;
320 struct multiwrite_async_ret
{
325 static void multiwrite_cb(void *opaque
, int ret
)
327 struct multiwrite_async_ret
*async_ret
= opaque
;
329 async_ret
->num_done
++;
331 async_ret
->error
= ret
;
335 static int do_aio_multiwrite(BlockRequest
* reqs
, int num_reqs
, int *total
)
338 struct multiwrite_async_ret async_ret
= {
344 for (i
= 0; i
< num_reqs
; i
++) {
345 reqs
[i
].cb
= multiwrite_cb
;
346 reqs
[i
].opaque
= &async_ret
;
347 *total
+= reqs
[i
].qiov
->size
;
350 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
355 while (async_ret
.num_done
< num_reqs
) {
356 main_loop_wait(false);
359 return async_ret
.error
< 0 ? async_ret
.error
: 1;
362 static void read_help(void)
366 " reads a range of bytes from the given offset\n"
369 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
371 " Reads a segment of the currently open file, optionally dumping it to the\n"
372 " standard output stream (with -v option) for subsequent inspection.\n"
373 " -b, -- read from the VM state rather than the virtual disk\n"
374 " -C, -- report statistics in a machine parsable format\n"
375 " -l, -- length for pattern verification (only with -P)\n"
376 " -p, -- use bdrv_pread to read the file\n"
377 " -P, -- use a pattern to verify read data\n"
378 " -q, -- quiet mode, do not show I/O statistics\n"
379 " -s, -- start offset for pattern verification (only with -P)\n"
380 " -v, -- dump buffer to standard output\n"
384 static int read_f(int argc
, char **argv
);
386 static const cmdinfo_t read_cmd
= {
392 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
393 .oneline
= "reads a number of bytes at a specified offset",
397 static int read_f(int argc
, char **argv
)
399 struct timeval t1
, t2
;
400 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
401 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
406 /* Some compilers get confused and warn if this is not initialized. */
408 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
410 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
420 pattern_count
= cvtnum(optarg
);
421 if (pattern_count
< 0) {
422 printf("non-numeric length argument -- %s\n", optarg
);
431 pattern
= parse_pattern(optarg
);
441 pattern_offset
= cvtnum(optarg
);
442 if (pattern_offset
< 0) {
443 printf("non-numeric length argument -- %s\n", optarg
);
451 return command_usage(&read_cmd
);
455 if (optind
!= argc
- 2) {
456 return command_usage(&read_cmd
);
459 if (bflag
&& pflag
) {
460 printf("-b and -p cannot be specified at the same time\n");
464 offset
= cvtnum(argv
[optind
]);
466 printf("non-numeric length argument -- %s\n", argv
[optind
]);
471 count
= cvtnum(argv
[optind
]);
473 printf("non-numeric length argument -- %s\n", argv
[optind
]);
477 if (!Pflag
&& (lflag
|| sflag
)) {
478 return command_usage(&read_cmd
);
482 pattern_count
= count
- pattern_offset
;
485 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
486 printf("pattern verification range exceeds end of read data\n");
491 if (offset
& 0x1ff) {
492 printf("offset %" PRId64
" is not sector aligned\n",
497 printf("count %d is not sector aligned\n",
503 buf
= qemu_io_alloc(count
, 0xab);
505 gettimeofday(&t1
, NULL
);
507 cnt
= do_pread(buf
, offset
, count
, &total
);
509 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
511 cnt
= do_read(buf
, offset
, count
, &total
);
513 gettimeofday(&t2
, NULL
);
516 printf("read failed: %s\n", strerror(-cnt
));
521 void *cmp_buf
= g_malloc(pattern_count
);
522 memset(cmp_buf
, pattern
, pattern_count
);
523 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
524 printf("Pattern verification failed at offset %"
525 PRId64
", %d bytes\n",
526 offset
+ pattern_offset
, pattern_count
);
536 dump_buffer(buf
, offset
, count
);
539 /* Finally, report back -- -C gives a parsable format */
541 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
549 static void readv_help(void)
553 " reads a range of bytes from the given offset into multiple buffers\n"
556 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
558 " Reads a segment of the currently open file, optionally dumping it to the\n"
559 " standard output stream (with -v option) for subsequent inspection.\n"
560 " Uses multiple iovec buffers if more than one byte range is specified.\n"
561 " -C, -- report statistics in a machine parsable format\n"
562 " -P, -- use a pattern to verify read data\n"
563 " -v, -- dump buffer to standard output\n"
564 " -q, -- quiet mode, do not show I/O statistics\n"
568 static int readv_f(int argc
, char **argv
);
570 static const cmdinfo_t readv_cmd
= {
575 .args
= "[-Cqv] [-P pattern ] off len [len..]",
576 .oneline
= "reads a number of bytes at a specified offset",
580 static int readv_f(int argc
, char **argv
)
582 struct timeval t1
, t2
;
583 int Cflag
= 0, qflag
= 0, vflag
= 0;
587 /* Some compilers get confused and warn if this is not initialized. */
594 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
601 pattern
= parse_pattern(optarg
);
613 return command_usage(&readv_cmd
);
617 if (optind
> argc
- 2) {
618 return command_usage(&readv_cmd
);
622 offset
= cvtnum(argv
[optind
]);
624 printf("non-numeric length argument -- %s\n", argv
[optind
]);
629 if (offset
& 0x1ff) {
630 printf("offset %" PRId64
" is not sector aligned\n",
635 nr_iov
= argc
- optind
;
636 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
641 gettimeofday(&t1
, NULL
);
642 cnt
= do_aio_readv(&qiov
, offset
, &total
);
643 gettimeofday(&t2
, NULL
);
646 printf("readv failed: %s\n", strerror(-cnt
));
651 void *cmp_buf
= g_malloc(qiov
.size
);
652 memset(cmp_buf
, pattern
, qiov
.size
);
653 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
654 printf("Pattern verification failed at offset %"
655 PRId64
", %zd bytes\n", offset
, qiov
.size
);
665 dump_buffer(buf
, offset
, qiov
.size
);
668 /* Finally, report back -- -C gives a parsable format */
670 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
673 qemu_iovec_destroy(&qiov
);
678 static void write_help(void)
682 " writes a range of bytes from the given offset\n"
685 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
687 " Writes into a segment of the currently open file, using a buffer\n"
688 " filled with a set pattern (0xcdcdcdcd).\n"
689 " -b, -- write to the VM state rather than the virtual disk\n"
690 " -p, -- use bdrv_pwrite to write the file\n"
691 " -P, -- use different pattern to fill file\n"
692 " -C, -- report statistics in a machine parsable format\n"
693 " -q, -- quiet mode, do not show I/O statistics\n"
694 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
698 static int write_f(int argc
, char **argv
);
700 static const cmdinfo_t write_cmd
= {
706 .args
= "[-bCpqz] [-P pattern ] off len",
707 .oneline
= "writes a number of bytes at a specified offset",
711 static int write_f(int argc
, char **argv
)
713 struct timeval t1
, t2
;
714 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
719 /* Some compilers get confused and warn if this is not initialized. */
723 while ((c
= getopt(argc
, argv
, "bCpP:qz")) != EOF
) {
736 pattern
= parse_pattern(optarg
);
748 return command_usage(&write_cmd
);
752 if (optind
!= argc
- 2) {
753 return command_usage(&write_cmd
);
756 if (bflag
+ pflag
+ zflag
> 1) {
757 printf("-b, -p, or -z cannot be specified at the same time\n");
761 if (zflag
&& Pflag
) {
762 printf("-z and -P cannot be specified at the same time\n");
766 offset
= cvtnum(argv
[optind
]);
768 printf("non-numeric length argument -- %s\n", argv
[optind
]);
773 count
= cvtnum(argv
[optind
]);
775 printf("non-numeric length argument -- %s\n", argv
[optind
]);
780 if (offset
& 0x1ff) {
781 printf("offset %" PRId64
" is not sector aligned\n",
787 printf("count %d is not sector aligned\n",
794 buf
= qemu_io_alloc(count
, pattern
);
797 gettimeofday(&t1
, NULL
);
799 cnt
= do_pwrite(buf
, offset
, count
, &total
);
801 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
803 cnt
= do_co_write_zeroes(offset
, count
, &total
);
805 cnt
= do_write(buf
, offset
, count
, &total
);
807 gettimeofday(&t2
, NULL
);
810 printf("write failed: %s\n", strerror(-cnt
));
818 /* Finally, report back -- -C gives a parsable format */
820 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
835 " writes a range of bytes from the given offset source from multiple buffers\n"
838 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
840 " Writes into a segment of the currently open file, using a buffer\n"
841 " filled with a set pattern (0xcdcdcdcd).\n"
842 " -P, -- use different pattern to fill file\n"
843 " -C, -- report statistics in a machine parsable format\n"
844 " -q, -- quiet mode, do not show I/O statistics\n"
848 static int writev_f(int argc
, char **argv
);
850 static const cmdinfo_t writev_cmd
= {
855 .args
= "[-Cq] [-P pattern ] off len [len..]",
856 .oneline
= "writes a number of bytes at a specified offset",
860 static int writev_f(int argc
, char **argv
)
862 struct timeval t1
, t2
;
863 int Cflag
= 0, qflag
= 0;
867 /* Some compilers get confused and warn if this is not initialized. */
873 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
882 pattern
= parse_pattern(optarg
);
888 return command_usage(&writev_cmd
);
892 if (optind
> argc
- 2) {
893 return command_usage(&writev_cmd
);
896 offset
= cvtnum(argv
[optind
]);
898 printf("non-numeric length argument -- %s\n", argv
[optind
]);
903 if (offset
& 0x1ff) {
904 printf("offset %" PRId64
" is not sector aligned\n",
909 nr_iov
= argc
- optind
;
910 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
915 gettimeofday(&t1
, NULL
);
916 cnt
= do_aio_writev(&qiov
, offset
, &total
);
917 gettimeofday(&t2
, NULL
);
920 printf("writev failed: %s\n", strerror(-cnt
));
928 /* Finally, report back -- -C gives a parsable format */
930 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
932 qemu_iovec_destroy(&qiov
);
937 static void multiwrite_help(void)
941 " writes a range of bytes from the given offset source from multiple buffers,\n"
942 " in a batch of requests that may be merged by qemu\n"
945 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
946 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
948 " Writes into a segment of the currently open file, using a buffer\n"
949 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
950 " by one for each request contained in the multiwrite command.\n"
951 " -P, -- use different pattern to fill file\n"
952 " -C, -- report statistics in a machine parsable format\n"
953 " -q, -- quiet mode, do not show I/O statistics\n"
957 static int multiwrite_f(int argc
, char **argv
);
959 static const cmdinfo_t multiwrite_cmd
= {
960 .name
= "multiwrite",
961 .cfunc
= multiwrite_f
,
964 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
965 .oneline
= "issues multiple write requests at once",
966 .help
= multiwrite_help
,
969 static int multiwrite_f(int argc
, char **argv
)
971 struct timeval t1
, t2
;
972 int Cflag
= 0, qflag
= 0;
975 int64_t offset
, first_offset
= 0;
976 /* Some compilers get confused and warn if this is not initialized. */
985 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
994 pattern
= parse_pattern(optarg
);
1000 return command_usage(&writev_cmd
);
1004 if (optind
> argc
- 2) {
1005 return command_usage(&writev_cmd
);
1009 for (i
= optind
; i
< argc
; i
++) {
1010 if (!strcmp(argv
[i
], ";")) {
1015 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
1016 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
1017 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
1019 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1022 /* Read the offset of the request */
1023 offset
= cvtnum(argv
[optind
]);
1025 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
1030 if (offset
& 0x1ff) {
1031 printf("offset %lld is not sector aligned\n",
1037 first_offset
= offset
;
1040 /* Read lengths for qiov entries */
1041 for (j
= optind
; j
< argc
; j
++) {
1042 if (!strcmp(argv
[j
], ";")) {
1047 nr_iov
= j
- optind
;
1050 buf
[i
] = create_iovec(&qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1051 if (buf
[i
] == NULL
) {
1055 reqs
[i
].qiov
= &qiovs
[i
];
1056 reqs
[i
].sector
= offset
>> 9;
1057 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1064 /* If there were empty requests at the end, ignore them */
1067 gettimeofday(&t1
, NULL
);
1068 cnt
= do_aio_multiwrite(reqs
, nr_reqs
, &total
);
1069 gettimeofday(&t2
, NULL
);
1072 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1080 /* Finally, report back -- -C gives a parsable format */
1082 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1084 for (i
= 0; i
< nr_reqs
; i
++) {
1085 qemu_io_free(buf
[i
]);
1086 if (reqs
[i
].qiov
!= NULL
) {
1087 qemu_iovec_destroy(&qiovs
[i
]);
1108 static void aio_write_done(void *opaque
, int ret
)
1110 struct aio_ctx
*ctx
= opaque
;
1113 gettimeofday(&t2
, NULL
);
1117 printf("aio_write failed: %s\n", strerror(-ret
));
1125 /* Finally, report back -- -C gives a parsable format */
1126 t2
= tsub(t2
, ctx
->t1
);
1127 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1128 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1130 qemu_io_free(ctx
->buf
);
1131 qemu_iovec_destroy(&ctx
->qiov
);
1135 static void aio_read_done(void *opaque
, int ret
)
1137 struct aio_ctx
*ctx
= opaque
;
1140 gettimeofday(&t2
, NULL
);
1143 printf("readv failed: %s\n", strerror(-ret
));
1148 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1150 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1151 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1152 printf("Pattern verification failed at offset %"
1153 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1163 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1166 /* Finally, report back -- -C gives a parsable format */
1167 t2
= tsub(t2
, ctx
->t1
);
1168 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1169 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1171 qemu_io_free(ctx
->buf
);
1172 qemu_iovec_destroy(&ctx
->qiov
);
1176 static void aio_read_help(void)
1180 " asynchronously reads a range of bytes from the given offset\n"
1183 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1185 " Reads a segment of the currently open file, optionally dumping it to the\n"
1186 " standard output stream (with -v option) for subsequent inspection.\n"
1187 " The read is performed asynchronously and the aio_flush command must be\n"
1188 " used to ensure all outstanding aio requests have been completed.\n"
1189 " -C, -- report statistics in a machine parsable format\n"
1190 " -P, -- use a pattern to verify read data\n"
1191 " -v, -- dump buffer to standard output\n"
1192 " -q, -- quiet mode, do not show I/O statistics\n"
1196 static int aio_read_f(int argc
, char **argv
);
1198 static const cmdinfo_t aio_read_cmd
= {
1200 .cfunc
= aio_read_f
,
1203 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1204 .oneline
= "asynchronously reads a number of bytes",
1205 .help
= aio_read_help
,
1208 static int aio_read_f(int argc
, char **argv
)
1211 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1213 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1220 ctx
->pattern
= parse_pattern(optarg
);
1221 if (ctx
->pattern
< 0) {
1234 return command_usage(&aio_read_cmd
);
1238 if (optind
> argc
- 2) {
1240 return command_usage(&aio_read_cmd
);
1243 ctx
->offset
= cvtnum(argv
[optind
]);
1244 if (ctx
->offset
< 0) {
1245 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1251 if (ctx
->offset
& 0x1ff) {
1252 printf("offset %" PRId64
" is not sector aligned\n",
1258 nr_iov
= argc
- optind
;
1259 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1260 if (ctx
->buf
== NULL
) {
1265 gettimeofday(&ctx
->t1
, NULL
);
1266 bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1267 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1271 static void aio_write_help(void)
1275 " asynchronously writes a range of bytes from the given offset source\n"
1276 " from multiple buffers\n"
1279 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1281 " Writes into a segment of the currently open file, using a buffer\n"
1282 " filled with a set pattern (0xcdcdcdcd).\n"
1283 " The write is performed asynchronously and the aio_flush command must be\n"
1284 " used to ensure all outstanding aio requests have been completed.\n"
1285 " -P, -- use different pattern to fill file\n"
1286 " -C, -- report statistics in a machine parsable format\n"
1287 " -q, -- quiet mode, do not show I/O statistics\n"
1291 static int aio_write_f(int argc
, char **argv
);
1293 static const cmdinfo_t aio_write_cmd
= {
1294 .name
= "aio_write",
1295 .cfunc
= aio_write_f
,
1298 .args
= "[-Cq] [-P pattern ] off len [len..]",
1299 .oneline
= "asynchronously writes a number of bytes",
1300 .help
= aio_write_help
,
1303 static int aio_write_f(int argc
, char **argv
)
1307 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1309 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1318 pattern
= parse_pattern(optarg
);
1326 return command_usage(&aio_write_cmd
);
1330 if (optind
> argc
- 2) {
1332 return command_usage(&aio_write_cmd
);
1335 ctx
->offset
= cvtnum(argv
[optind
]);
1336 if (ctx
->offset
< 0) {
1337 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1343 if (ctx
->offset
& 0x1ff) {
1344 printf("offset %" PRId64
" is not sector aligned\n",
1350 nr_iov
= argc
- optind
;
1351 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1352 if (ctx
->buf
== NULL
) {
1357 gettimeofday(&ctx
->t1
, NULL
);
1358 bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1359 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1363 static int aio_flush_f(int argc
, char **argv
)
1369 static const cmdinfo_t aio_flush_cmd
= {
1370 .name
= "aio_flush",
1371 .cfunc
= aio_flush_f
,
1372 .oneline
= "completes all outstanding aio requests"
1375 static int flush_f(int argc
, char **argv
)
1381 static const cmdinfo_t flush_cmd
= {
1385 .oneline
= "flush all in-core file state to disk",
1388 static int truncate_f(int argc
, char **argv
)
1393 offset
= cvtnum(argv
[1]);
1395 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1399 ret
= bdrv_truncate(bs
, offset
);
1401 printf("truncate: %s\n", strerror(-ret
));
1408 static const cmdinfo_t truncate_cmd
= {
1411 .cfunc
= truncate_f
,
1415 .oneline
= "truncates the current file at the given offset",
1418 static int length_f(int argc
, char **argv
)
1423 size
= bdrv_getlength(bs
);
1425 printf("getlength: %s\n", strerror(-size
));
1429 cvtstr(size
, s1
, sizeof(s1
));
1435 static const cmdinfo_t length_cmd
= {
1439 .oneline
= "gets the length of the current file",
1443 static int info_f(int argc
, char **argv
)
1445 BlockDriverInfo bdi
;
1446 char s1
[64], s2
[64];
1449 if (bs
->drv
&& bs
->drv
->format_name
) {
1450 printf("format name: %s\n", bs
->drv
->format_name
);
1452 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1453 printf("format name: %s\n", bs
->drv
->protocol_name
);
1456 ret
= bdrv_get_info(bs
, &bdi
);
1461 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1462 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1464 printf("cluster size: %s\n", s1
);
1465 printf("vm state offset: %s\n", s2
);
1472 static const cmdinfo_t info_cmd
= {
1476 .oneline
= "prints information about the current file",
1479 static void discard_help(void)
1483 " discards a range of bytes from the given offset\n"
1486 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1488 " Discards a segment of the currently open file.\n"
1489 " -C, -- report statistics in a machine parsable format\n"
1490 " -q, -- quiet mode, do not show I/O statistics\n"
1494 static int discard_f(int argc
, char **argv
);
1496 static const cmdinfo_t discard_cmd
= {
1502 .args
= "[-Cq] off len",
1503 .oneline
= "discards a number of bytes at a specified offset",
1504 .help
= discard_help
,
1507 static int discard_f(int argc
, char **argv
)
1509 struct timeval t1
, t2
;
1510 int Cflag
= 0, qflag
= 0;
1515 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1524 return command_usage(&discard_cmd
);
1528 if (optind
!= argc
- 2) {
1529 return command_usage(&discard_cmd
);
1532 offset
= cvtnum(argv
[optind
]);
1534 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1539 count
= cvtnum(argv
[optind
]);
1541 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1545 gettimeofday(&t1
, NULL
);
1546 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1547 count
>> BDRV_SECTOR_BITS
);
1548 gettimeofday(&t2
, NULL
);
1551 printf("discard failed: %s\n", strerror(-ret
));
1555 /* Finally, report back -- -C gives a parsable format */
1558 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1565 static int alloc_f(int argc
, char **argv
)
1567 int64_t offset
, sector_num
;
1568 int nb_sectors
, remaining
;
1573 offset
= cvtnum(argv
[1]);
1574 if (offset
& 0x1ff) {
1575 printf("offset %" PRId64
" is not sector aligned\n",
1581 nb_sectors
= cvtnum(argv
[2]);
1586 remaining
= nb_sectors
;
1588 sector_num
= offset
>> 9;
1590 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1597 nb_sectors
-= remaining
;
1602 cvtstr(offset
, s1
, sizeof(s1
));
1604 printf("%d/%d sectors allocated at offset %s\n",
1605 sum_alloc
, nb_sectors
, s1
);
1609 static const cmdinfo_t alloc_cmd
= {
1615 .args
= "off [sectors]",
1616 .oneline
= "checks if a sector is present in the file",
1619 static int map_f(int argc
, char **argv
)
1624 int num
, num_checked
;
1629 nb_sectors
= bs
->total_sectors
;
1632 num_checked
= MIN(nb_sectors
, INT_MAX
);
1633 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1634 retstr
= ret
? " allocated" : "not allocated";
1635 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1636 printf("[% 24" PRId64
"] % 8d/% 8d sectors %s at offset %s (%d)\n",
1637 offset
<< 9ULL, num
, num_checked
, retstr
, s1
, ret
);
1641 } while (offset
< bs
->total_sectors
);
1646 static const cmdinfo_t map_cmd
= {
1652 .oneline
= "prints the allocated areas of a file",
1655 static int abort_f(int argc
, char **argv
)
1660 static const cmdinfo_t abort_cmd
= {
1663 .flags
= CMD_NOFILE_OK
,
1664 .oneline
= "simulate a program crash using abort(3)",
1667 static int close_f(int argc
, char **argv
)
1674 static const cmdinfo_t close_cmd
= {
1678 .oneline
= "close the current open file",
1681 static int openfile(char *name
, int flags
, int growable
)
1684 fprintf(stderr
, "file open already, try 'help close'\n");
1689 if (bdrv_file_open(&bs
, name
, flags
)) {
1690 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1694 bs
= bdrv_new("hda");
1696 if (bdrv_open(bs
, name
, flags
, NULL
) < 0) {
1697 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1707 static void open_help(void)
1711 " opens a new file in the requested mode\n"
1714 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1716 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1717 " -r, -- open file read-only\n"
1718 " -s, -- use snapshot file\n"
1719 " -n, -- disable host cache\n"
1720 " -g, -- allow file to grow (only applies to protocols)"
1724 static int open_f(int argc
, char **argv
);
1726 static const cmdinfo_t open_cmd
= {
1732 .flags
= CMD_NOFILE_OK
,
1733 .args
= "[-Crsn] [path]",
1734 .oneline
= "open the file specified by path",
1738 static int open_f(int argc
, char **argv
)
1745 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
1748 flags
|= BDRV_O_SNAPSHOT
;
1751 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1760 return command_usage(&open_cmd
);
1765 flags
|= BDRV_O_RDWR
;
1768 if (optind
!= argc
- 1) {
1769 return command_usage(&open_cmd
);
1772 return openfile(argv
[optind
], flags
, growable
);
1775 static int init_args_command(int index
)
1777 /* only one device allowed so far */
1784 static int init_check_command(const cmdinfo_t
*ct
)
1786 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
1789 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1790 fprintf(stderr
, "no file open, try 'help open'\n");
1796 static void usage(const char *name
)
1799 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1800 "QEMU Disk exerciser\n"
1802 " -c, --cmd command to execute\n"
1803 " -r, --read-only export read-only\n"
1804 " -s, --snapshot use snapshot file\n"
1805 " -n, --nocache disable host cache\n"
1806 " -g, --growable allow file to grow (only applies to protocols)\n"
1807 " -m, --misalign misalign allocations for O_DIRECT\n"
1808 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1809 " -t, --cache=MODE use the given cache mode for the image\n"
1810 " -T, --trace FILE enable trace events listed in the given file\n"
1811 " -h, --help display this help and exit\n"
1812 " -V, --version output version information and exit\n"
1818 int main(int argc
, char **argv
)
1822 const char *sopt
= "hVc:rsnmgkt:T:";
1823 const struct option lopt
[] = {
1824 { "help", 0, NULL
, 'h' },
1825 { "version", 0, NULL
, 'V' },
1826 { "offset", 1, NULL
, 'o' },
1827 { "cmd", 1, NULL
, 'c' },
1828 { "read-only", 0, NULL
, 'r' },
1829 { "snapshot", 0, NULL
, 's' },
1830 { "nocache", 0, NULL
, 'n' },
1831 { "misalign", 0, NULL
, 'm' },
1832 { "growable", 0, NULL
, 'g' },
1833 { "native-aio", 0, NULL
, 'k' },
1834 { "cache", 1, NULL
, 't' },
1835 { "trace", 1, NULL
, 'T' },
1836 { NULL
, 0, NULL
, 0 }
1842 progname
= basename(argv
[0]);
1844 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1847 flags
|= BDRV_O_SNAPSHOT
;
1850 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1853 add_user_command(optarg
);
1865 flags
|= BDRV_O_NATIVE_AIO
;
1868 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
1869 error_report("Invalid cache option: %s", optarg
);
1874 if (!trace_backend_init(optarg
, NULL
)) {
1875 exit(1); /* error message will have been printed */
1879 printf("%s version %s\n", progname
, VERSION
);
1890 if ((argc
- optind
) > 1) {
1895 qemu_init_main_loop();
1898 /* initialize commands */
1901 add_command(&open_cmd
);
1902 add_command(&close_cmd
);
1903 add_command(&read_cmd
);
1904 add_command(&readv_cmd
);
1905 add_command(&write_cmd
);
1906 add_command(&writev_cmd
);
1907 add_command(&multiwrite_cmd
);
1908 add_command(&aio_read_cmd
);
1909 add_command(&aio_write_cmd
);
1910 add_command(&aio_flush_cmd
);
1911 add_command(&flush_cmd
);
1912 add_command(&truncate_cmd
);
1913 add_command(&length_cmd
);
1914 add_command(&info_cmd
);
1915 add_command(&discard_cmd
);
1916 add_command(&alloc_cmd
);
1917 add_command(&map_cmd
);
1918 add_command(&abort_cmd
);
1920 add_args_command(init_args_command
);
1921 add_check_command(init_check_command
);
1923 /* open the device */
1925 flags
|= BDRV_O_RDWR
;
1928 if ((argc
- optind
) == 1) {
1929 openfile(argv
[optind
], flags
, growable
);
1934 * Make sure all outstanding requests complete before the program exits.