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
);
677 static void write_help(void)
681 " writes a range of bytes from the given offset\n"
684 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
686 " Writes into a segment of the currently open file, using a buffer\n"
687 " filled with a set pattern (0xcdcdcdcd).\n"
688 " -b, -- write to the VM state rather than the virtual disk\n"
689 " -p, -- use bdrv_pwrite to write the file\n"
690 " -P, -- use different pattern to fill file\n"
691 " -C, -- report statistics in a machine parsable format\n"
692 " -q, -- quiet mode, do not show I/O statistics\n"
693 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
697 static int write_f(int argc
, char **argv
);
699 static const cmdinfo_t write_cmd
= {
705 .args
= "[-bCpqz] [-P pattern ] off len",
706 .oneline
= "writes a number of bytes at a specified offset",
710 static int write_f(int argc
, char **argv
)
712 struct timeval t1
, t2
;
713 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
718 /* Some compilers get confused and warn if this is not initialized. */
722 while ((c
= getopt(argc
, argv
, "bCpP:qz")) != EOF
) {
735 pattern
= parse_pattern(optarg
);
747 return command_usage(&write_cmd
);
751 if (optind
!= argc
- 2) {
752 return command_usage(&write_cmd
);
755 if (bflag
+ pflag
+ zflag
> 1) {
756 printf("-b, -p, or -z cannot be specified at the same time\n");
760 if (zflag
&& Pflag
) {
761 printf("-z and -P cannot be specified at the same time\n");
765 offset
= cvtnum(argv
[optind
]);
767 printf("non-numeric length argument -- %s\n", argv
[optind
]);
772 count
= cvtnum(argv
[optind
]);
774 printf("non-numeric length argument -- %s\n", argv
[optind
]);
779 if (offset
& 0x1ff) {
780 printf("offset %" PRId64
" is not sector aligned\n",
786 printf("count %d is not sector aligned\n",
793 buf
= qemu_io_alloc(count
, pattern
);
796 gettimeofday(&t1
, NULL
);
798 cnt
= do_pwrite(buf
, offset
, count
, &total
);
800 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
802 cnt
= do_co_write_zeroes(offset
, count
, &total
);
804 cnt
= do_write(buf
, offset
, count
, &total
);
806 gettimeofday(&t2
, NULL
);
809 printf("write failed: %s\n", strerror(-cnt
));
817 /* Finally, report back -- -C gives a parsable format */
819 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
834 " writes a range of bytes from the given offset source from multiple buffers\n"
837 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
839 " Writes into a segment of the currently open file, using a buffer\n"
840 " filled with a set pattern (0xcdcdcdcd).\n"
841 " -P, -- use different pattern to fill file\n"
842 " -C, -- report statistics in a machine parsable format\n"
843 " -q, -- quiet mode, do not show I/O statistics\n"
847 static int writev_f(int argc
, char **argv
);
849 static const cmdinfo_t writev_cmd
= {
854 .args
= "[-Cq] [-P pattern ] off len [len..]",
855 .oneline
= "writes a number of bytes at a specified offset",
859 static int writev_f(int argc
, char **argv
)
861 struct timeval t1
, t2
;
862 int Cflag
= 0, qflag
= 0;
866 /* Some compilers get confused and warn if this is not initialized. */
872 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
881 pattern
= parse_pattern(optarg
);
887 return command_usage(&writev_cmd
);
891 if (optind
> argc
- 2) {
892 return command_usage(&writev_cmd
);
895 offset
= cvtnum(argv
[optind
]);
897 printf("non-numeric length argument -- %s\n", argv
[optind
]);
902 if (offset
& 0x1ff) {
903 printf("offset %" PRId64
" is not sector aligned\n",
908 nr_iov
= argc
- optind
;
909 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
914 gettimeofday(&t1
, NULL
);
915 cnt
= do_aio_writev(&qiov
, offset
, &total
);
916 gettimeofday(&t2
, NULL
);
919 printf("writev failed: %s\n", strerror(-cnt
));
927 /* Finally, report back -- -C gives a parsable format */
929 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
935 static void multiwrite_help(void)
939 " writes a range of bytes from the given offset source from multiple buffers,\n"
940 " in a batch of requests that may be merged by qemu\n"
943 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
944 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
946 " Writes into a segment of the currently open file, using a buffer\n"
947 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
948 " by one for each request contained in the multiwrite command.\n"
949 " -P, -- use different pattern to fill file\n"
950 " -C, -- report statistics in a machine parsable format\n"
951 " -q, -- quiet mode, do not show I/O statistics\n"
955 static int multiwrite_f(int argc
, char **argv
);
957 static const cmdinfo_t multiwrite_cmd
= {
958 .name
= "multiwrite",
959 .cfunc
= multiwrite_f
,
962 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
963 .oneline
= "issues multiple write requests at once",
964 .help
= multiwrite_help
,
967 static int multiwrite_f(int argc
, char **argv
)
969 struct timeval t1
, t2
;
970 int Cflag
= 0, qflag
= 0;
973 int64_t offset
, first_offset
= 0;
974 /* Some compilers get confused and warn if this is not initialized. */
983 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
992 pattern
= parse_pattern(optarg
);
998 return command_usage(&writev_cmd
);
1002 if (optind
> argc
- 2) {
1003 return command_usage(&writev_cmd
);
1007 for (i
= optind
; i
< argc
; i
++) {
1008 if (!strcmp(argv
[i
], ";")) {
1013 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
1014 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
1015 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
1017 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1020 /* Read the offset of the request */
1021 offset
= cvtnum(argv
[optind
]);
1023 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
1028 if (offset
& 0x1ff) {
1029 printf("offset %lld is not sector aligned\n",
1035 first_offset
= offset
;
1038 /* Read lengths for qiov entries */
1039 for (j
= optind
; j
< argc
; j
++) {
1040 if (!strcmp(argv
[j
], ";")) {
1045 nr_iov
= j
- optind
;
1048 buf
[i
] = create_iovec(&qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1049 if (buf
[i
] == NULL
) {
1053 reqs
[i
].qiov
= &qiovs
[i
];
1054 reqs
[i
].sector
= offset
>> 9;
1055 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1062 /* If there were empty requests at the end, ignore them */
1065 gettimeofday(&t1
, NULL
);
1066 cnt
= do_aio_multiwrite(reqs
, nr_reqs
, &total
);
1067 gettimeofday(&t2
, NULL
);
1070 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1078 /* Finally, report back -- -C gives a parsable format */
1080 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1082 for (i
= 0; i
< nr_reqs
; i
++) {
1083 qemu_io_free(buf
[i
]);
1084 if (reqs
[i
].qiov
!= NULL
) {
1085 qemu_iovec_destroy(&qiovs
[i
]);
1106 static void aio_write_done(void *opaque
, int ret
)
1108 struct aio_ctx
*ctx
= opaque
;
1111 gettimeofday(&t2
, NULL
);
1115 printf("aio_write failed: %s\n", strerror(-ret
));
1123 /* Finally, report back -- -C gives a parsable format */
1124 t2
= tsub(t2
, ctx
->t1
);
1125 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1126 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1128 qemu_io_free(ctx
->buf
);
1132 static void aio_read_done(void *opaque
, int ret
)
1134 struct aio_ctx
*ctx
= opaque
;
1137 gettimeofday(&t2
, NULL
);
1140 printf("readv failed: %s\n", strerror(-ret
));
1145 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1147 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1148 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1149 printf("Pattern verification failed at offset %"
1150 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1160 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1163 /* Finally, report back -- -C gives a parsable format */
1164 t2
= tsub(t2
, ctx
->t1
);
1165 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1166 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1168 qemu_io_free(ctx
->buf
);
1172 static void aio_read_help(void)
1176 " asynchronously reads a range of bytes from the given offset\n"
1179 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1181 " Reads a segment of the currently open file, optionally dumping it to the\n"
1182 " standard output stream (with -v option) for subsequent inspection.\n"
1183 " The read is performed asynchronously and the aio_flush command must be\n"
1184 " used to ensure all outstanding aio requests have been completed.\n"
1185 " -C, -- report statistics in a machine parsable format\n"
1186 " -P, -- use a pattern to verify read data\n"
1187 " -v, -- dump buffer to standard output\n"
1188 " -q, -- quiet mode, do not show I/O statistics\n"
1192 static int aio_read_f(int argc
, char **argv
);
1194 static const cmdinfo_t aio_read_cmd
= {
1196 .cfunc
= aio_read_f
,
1199 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1200 .oneline
= "asynchronously reads a number of bytes",
1201 .help
= aio_read_help
,
1204 static int aio_read_f(int argc
, char **argv
)
1207 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1209 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1216 ctx
->pattern
= parse_pattern(optarg
);
1217 if (ctx
->pattern
< 0) {
1230 return command_usage(&aio_read_cmd
);
1234 if (optind
> argc
- 2) {
1236 return command_usage(&aio_read_cmd
);
1239 ctx
->offset
= cvtnum(argv
[optind
]);
1240 if (ctx
->offset
< 0) {
1241 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1247 if (ctx
->offset
& 0x1ff) {
1248 printf("offset %" PRId64
" is not sector aligned\n",
1254 nr_iov
= argc
- optind
;
1255 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1256 if (ctx
->buf
== NULL
) {
1261 gettimeofday(&ctx
->t1
, NULL
);
1262 bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1263 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1267 static void aio_write_help(void)
1271 " asynchronously writes a range of bytes from the given offset source\n"
1272 " from multiple buffers\n"
1275 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1277 " Writes into a segment of the currently open file, using a buffer\n"
1278 " filled with a set pattern (0xcdcdcdcd).\n"
1279 " The write is performed asynchronously and the aio_flush command must be\n"
1280 " used to ensure all outstanding aio requests have been completed.\n"
1281 " -P, -- use different pattern to fill file\n"
1282 " -C, -- report statistics in a machine parsable format\n"
1283 " -q, -- quiet mode, do not show I/O statistics\n"
1287 static int aio_write_f(int argc
, char **argv
);
1289 static const cmdinfo_t aio_write_cmd
= {
1290 .name
= "aio_write",
1291 .cfunc
= aio_write_f
,
1294 .args
= "[-Cq] [-P pattern ] off len [len..]",
1295 .oneline
= "asynchronously writes a number of bytes",
1296 .help
= aio_write_help
,
1299 static int aio_write_f(int argc
, char **argv
)
1303 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1305 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1314 pattern
= parse_pattern(optarg
);
1322 return command_usage(&aio_write_cmd
);
1326 if (optind
> argc
- 2) {
1328 return command_usage(&aio_write_cmd
);
1331 ctx
->offset
= cvtnum(argv
[optind
]);
1332 if (ctx
->offset
< 0) {
1333 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1339 if (ctx
->offset
& 0x1ff) {
1340 printf("offset %" PRId64
" is not sector aligned\n",
1346 nr_iov
= argc
- optind
;
1347 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1348 if (ctx
->buf
== NULL
) {
1353 gettimeofday(&ctx
->t1
, NULL
);
1354 bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1355 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1359 static int aio_flush_f(int argc
, char **argv
)
1365 static const cmdinfo_t aio_flush_cmd
= {
1366 .name
= "aio_flush",
1367 .cfunc
= aio_flush_f
,
1368 .oneline
= "completes all outstanding aio requests"
1371 static int flush_f(int argc
, char **argv
)
1377 static const cmdinfo_t flush_cmd
= {
1381 .oneline
= "flush all in-core file state to disk",
1384 static int truncate_f(int argc
, char **argv
)
1389 offset
= cvtnum(argv
[1]);
1391 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1395 ret
= bdrv_truncate(bs
, offset
);
1397 printf("truncate: %s\n", strerror(-ret
));
1404 static const cmdinfo_t truncate_cmd
= {
1407 .cfunc
= truncate_f
,
1411 .oneline
= "truncates the current file at the given offset",
1414 static int length_f(int argc
, char **argv
)
1419 size
= bdrv_getlength(bs
);
1421 printf("getlength: %s\n", strerror(-size
));
1425 cvtstr(size
, s1
, sizeof(s1
));
1431 static const cmdinfo_t length_cmd
= {
1435 .oneline
= "gets the length of the current file",
1439 static int info_f(int argc
, char **argv
)
1441 BlockDriverInfo bdi
;
1442 char s1
[64], s2
[64];
1445 if (bs
->drv
&& bs
->drv
->format_name
) {
1446 printf("format name: %s\n", bs
->drv
->format_name
);
1448 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1449 printf("format name: %s\n", bs
->drv
->protocol_name
);
1452 ret
= bdrv_get_info(bs
, &bdi
);
1457 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1458 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1460 printf("cluster size: %s\n", s1
);
1461 printf("vm state offset: %s\n", s2
);
1468 static const cmdinfo_t info_cmd
= {
1472 .oneline
= "prints information about the current file",
1475 static void discard_help(void)
1479 " discards a range of bytes from the given offset\n"
1482 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1484 " Discards a segment of the currently open file.\n"
1485 " -C, -- report statistics in a machine parsable format\n"
1486 " -q, -- quiet mode, do not show I/O statistics\n"
1490 static int discard_f(int argc
, char **argv
);
1492 static const cmdinfo_t discard_cmd
= {
1498 .args
= "[-Cq] off len",
1499 .oneline
= "discards a number of bytes at a specified offset",
1500 .help
= discard_help
,
1503 static int discard_f(int argc
, char **argv
)
1505 struct timeval t1
, t2
;
1506 int Cflag
= 0, qflag
= 0;
1511 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1520 return command_usage(&discard_cmd
);
1524 if (optind
!= argc
- 2) {
1525 return command_usage(&discard_cmd
);
1528 offset
= cvtnum(argv
[optind
]);
1530 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1535 count
= cvtnum(argv
[optind
]);
1537 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1541 gettimeofday(&t1
, NULL
);
1542 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1543 count
>> BDRV_SECTOR_BITS
);
1544 gettimeofday(&t2
, NULL
);
1547 printf("discard failed: %s\n", strerror(-ret
));
1551 /* Finally, report back -- -C gives a parsable format */
1554 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1561 static int alloc_f(int argc
, char **argv
)
1563 int64_t offset
, sector_num
;
1564 int nb_sectors
, remaining
;
1569 offset
= cvtnum(argv
[1]);
1570 if (offset
& 0x1ff) {
1571 printf("offset %" PRId64
" is not sector aligned\n",
1577 nb_sectors
= cvtnum(argv
[2]);
1582 remaining
= nb_sectors
;
1584 sector_num
= offset
>> 9;
1586 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1593 nb_sectors
-= remaining
;
1598 cvtstr(offset
, s1
, sizeof(s1
));
1600 printf("%d/%d sectors allocated at offset %s\n",
1601 sum_alloc
, nb_sectors
, s1
);
1605 static const cmdinfo_t alloc_cmd
= {
1611 .args
= "off [sectors]",
1612 .oneline
= "checks if a sector is present in the file",
1615 static int map_f(int argc
, char **argv
)
1620 int num
, num_checked
;
1625 nb_sectors
= bs
->total_sectors
;
1628 num_checked
= MIN(nb_sectors
, INT_MAX
);
1629 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1630 retstr
= ret
? " allocated" : "not allocated";
1631 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1632 printf("[% 24" PRId64
"] % 8d/% 8d sectors %s at offset %s (%d)\n",
1633 offset
<< 9ULL, num
, num_checked
, retstr
, s1
, ret
);
1637 } while (offset
< bs
->total_sectors
);
1642 static const cmdinfo_t map_cmd
= {
1648 .oneline
= "prints the allocated areas of a file",
1652 static int close_f(int argc
, char **argv
)
1659 static const cmdinfo_t close_cmd
= {
1663 .oneline
= "close the current open file",
1666 static int openfile(char *name
, int flags
, int growable
)
1669 fprintf(stderr
, "file open already, try 'help close'\n");
1674 if (bdrv_file_open(&bs
, name
, flags
)) {
1675 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1679 bs
= bdrv_new("hda");
1681 if (bdrv_open(bs
, name
, flags
, NULL
) < 0) {
1682 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1692 static void open_help(void)
1696 " opens a new file in the requested mode\n"
1699 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1701 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1702 " -r, -- open file read-only\n"
1703 " -s, -- use snapshot file\n"
1704 " -n, -- disable host cache\n"
1705 " -g, -- allow file to grow (only applies to protocols)"
1709 static int open_f(int argc
, char **argv
);
1711 static const cmdinfo_t open_cmd
= {
1717 .flags
= CMD_NOFILE_OK
,
1718 .args
= "[-Crsn] [path]",
1719 .oneline
= "open the file specified by path",
1723 static int open_f(int argc
, char **argv
)
1730 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
1733 flags
|= BDRV_O_SNAPSHOT
;
1736 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1745 return command_usage(&open_cmd
);
1750 flags
|= BDRV_O_RDWR
;
1753 if (optind
!= argc
- 1) {
1754 return command_usage(&open_cmd
);
1757 return openfile(argv
[optind
], flags
, growable
);
1760 static int init_args_command(int index
)
1762 /* only one device allowed so far */
1769 static int init_check_command(const cmdinfo_t
*ct
)
1771 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
1774 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1775 fprintf(stderr
, "no file open, try 'help open'\n");
1781 static void usage(const char *name
)
1784 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1785 "QEMU Disk exerciser\n"
1787 " -c, --cmd command to execute\n"
1788 " -r, --read-only export read-only\n"
1789 " -s, --snapshot use snapshot file\n"
1790 " -n, --nocache disable host cache\n"
1791 " -g, --growable allow file to grow (only applies to protocols)\n"
1792 " -m, --misalign misalign allocations for O_DIRECT\n"
1793 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1794 " -t, --cache=MODE use the given cache mode for the image\n"
1795 " -T, --trace FILE enable trace events listed in the given file\n"
1796 " -h, --help display this help and exit\n"
1797 " -V, --version output version information and exit\n"
1803 int main(int argc
, char **argv
)
1807 const char *sopt
= "hVc:rsnmgkt:T:";
1808 const struct option lopt
[] = {
1809 { "help", 0, NULL
, 'h' },
1810 { "version", 0, NULL
, 'V' },
1811 { "offset", 1, NULL
, 'o' },
1812 { "cmd", 1, NULL
, 'c' },
1813 { "read-only", 0, NULL
, 'r' },
1814 { "snapshot", 0, NULL
, 's' },
1815 { "nocache", 0, NULL
, 'n' },
1816 { "misalign", 0, NULL
, 'm' },
1817 { "growable", 0, NULL
, 'g' },
1818 { "native-aio", 0, NULL
, 'k' },
1819 { "cache", 1, NULL
, 't' },
1820 { "trace", 1, NULL
, 'T' },
1821 { NULL
, 0, NULL
, 0 }
1827 progname
= basename(argv
[0]);
1829 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1832 flags
|= BDRV_O_SNAPSHOT
;
1835 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1838 add_user_command(optarg
);
1850 flags
|= BDRV_O_NATIVE_AIO
;
1853 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
1854 error_report("Invalid cache option: %s", optarg
);
1859 if (!trace_backend_init(optarg
, NULL
)) {
1860 exit(1); /* error message will have been printed */
1864 printf("%s version %s\n", progname
, VERSION
);
1875 if ((argc
- optind
) > 1) {
1882 qemu_init_main_loop();
1884 /* initialize commands */
1887 add_command(&open_cmd
);
1888 add_command(&close_cmd
);
1889 add_command(&read_cmd
);
1890 add_command(&readv_cmd
);
1891 add_command(&write_cmd
);
1892 add_command(&writev_cmd
);
1893 add_command(&multiwrite_cmd
);
1894 add_command(&aio_read_cmd
);
1895 add_command(&aio_write_cmd
);
1896 add_command(&aio_flush_cmd
);
1897 add_command(&flush_cmd
);
1898 add_command(&truncate_cmd
);
1899 add_command(&length_cmd
);
1900 add_command(&info_cmd
);
1901 add_command(&discard_cmd
);
1902 add_command(&alloc_cmd
);
1903 add_command(&map_cmd
);
1905 add_args_command(init_args_command
);
1906 add_check_command(init_check_command
);
1908 /* open the device */
1910 flags
|= BDRV_O_RDWR
;
1913 if ((argc
- optind
) == 1) {
1914 openfile(argv
[optind
], flags
, growable
);
1919 * Make sure all outstanding requests complete before the program exits.