2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
11 #include <sys/types.h>
17 #include "qemu-common.h"
18 #include "block_int.h"
21 #define VERSION "0.0.1"
23 #define CMD_NOFILE_OK 0x01
26 static BlockDriverState
*bs
;
31 * Parse the pattern argument to various sub-commands.
33 * Because the pattern is used as an argument to memset it must evaluate
34 * to an unsigned integer that fits into a single byte.
36 static int parse_pattern(const char *arg
)
41 pattern
= strtol(arg
, &endptr
, 0);
42 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
43 printf("%s is not a valid pattern byte\n", arg
);
51 * Memory allocation helpers.
53 * Make sure memory is aligned by default, or purposefully misaligned if
54 * that is specified on the command line.
57 #define MISALIGN_OFFSET 16
58 static void *qemu_io_alloc(size_t len
, int pattern
)
63 len
+= MISALIGN_OFFSET
;
65 buf
= qemu_blockalign(bs
, len
);
66 memset(buf
, pattern
, len
);
68 buf
+= MISALIGN_OFFSET
;
73 static void qemu_io_free(void *p
)
81 static void dump_buffer(const void *buffer
, int64_t offset
, int len
)
86 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
89 printf("%08" PRIx64
": ", offset
+ i
);
90 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
94 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
105 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
106 int count
, int total
, int cnt
, int Cflag
)
108 char s1
[64], s2
[64], ts
[64];
110 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
112 cvtstr((double)total
, s1
, sizeof(s1
));
113 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
114 printf("%s %d/%d bytes at offset %" PRId64
"\n",
115 op
, total
, count
, offset
);
116 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
117 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
118 } else {/* bytes,ops,time,bytes/sec,ops/sec */
119 printf("%d,%d,%s,%.3f,%.3f\n",
121 tdiv((double)total
, *t
),
122 tdiv((double)cnt
, *t
));
127 * Parse multiple length statements for vectored I/O, and construct an I/O
128 * vector matching it.
131 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
133 size_t *sizes
= g_new0(size_t, nr_iov
);
139 for (i
= 0; i
< nr_iov
; i
++) {
145 printf("non-numeric length argument -- %s\n", arg
);
149 /* should be SIZE_T_MAX, but that doesn't exist */
151 printf("too large length argument -- %s\n", arg
);
156 printf("length argument %" PRId64
157 " is not sector aligned\n", len
);
165 qemu_iovec_init(qiov
, nr_iov
);
167 buf
= p
= qemu_io_alloc(count
, pattern
);
169 for (i
= 0; i
< nr_iov
; i
++) {
170 qemu_iovec_add(qiov
, p
, sizes
[i
]);
179 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
183 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
191 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
195 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
203 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
205 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
212 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
214 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
229 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
231 CoWriteZeroes
*data
= opaque
;
233 data
->ret
= bdrv_co_write_zeroes(bs
, data
->offset
/ BDRV_SECTOR_SIZE
,
234 data
->count
/ BDRV_SECTOR_SIZE
);
237 *data
->total
= data
->ret
;
241 *data
->total
= data
->count
;
244 static int do_co_write_zeroes(int64_t offset
, int count
, int *total
)
247 CoWriteZeroes data
= {
254 co
= qemu_coroutine_create(co_write_zeroes_entry
);
255 qemu_coroutine_enter(co
, &data
);
266 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
268 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
275 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
277 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
284 #define NOT_DONE 0x7fffffff
285 static void aio_rw_done(void *opaque
, int ret
)
287 *(int *)opaque
= ret
;
290 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
292 int async_ret
= NOT_DONE
;
294 bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
295 aio_rw_done
, &async_ret
);
296 while (async_ret
== NOT_DONE
) {
301 return async_ret
< 0 ? async_ret
: 1;
304 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
306 int async_ret
= NOT_DONE
;
308 bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
309 aio_rw_done
, &async_ret
);
310 while (async_ret
== NOT_DONE
) {
315 return async_ret
< 0 ? async_ret
: 1;
318 struct multiwrite_async_ret
{
323 static void multiwrite_cb(void *opaque
, int ret
)
325 struct multiwrite_async_ret
*async_ret
= opaque
;
327 async_ret
->num_done
++;
329 async_ret
->error
= ret
;
333 static int do_aio_multiwrite(BlockRequest
* reqs
, int num_reqs
, int *total
)
336 struct multiwrite_async_ret async_ret
= {
342 for (i
= 0; i
< num_reqs
; i
++) {
343 reqs
[i
].cb
= multiwrite_cb
;
344 reqs
[i
].opaque
= &async_ret
;
345 *total
+= reqs
[i
].qiov
->size
;
348 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
353 while (async_ret
.num_done
< num_reqs
) {
357 return async_ret
.error
< 0 ? async_ret
.error
: 1;
360 static void read_help(void)
364 " reads a range of bytes from the given offset\n"
367 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
369 " Reads a segment of the currently open file, optionally dumping it to the\n"
370 " standard output stream (with -v option) for subsequent inspection.\n"
371 " -b, -- read from the VM state rather than the virtual disk\n"
372 " -C, -- report statistics in a machine parsable format\n"
373 " -l, -- length for pattern verification (only with -P)\n"
374 " -p, -- use bdrv_pread to read the file\n"
375 " -P, -- use a pattern to verify read data\n"
376 " -q, -- quiet mode, do not show I/O statistics\n"
377 " -s, -- start offset for pattern verification (only with -P)\n"
378 " -v, -- dump buffer to standard output\n"
382 static int read_f(int argc
, char **argv
);
384 static const cmdinfo_t read_cmd
= {
390 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
391 .oneline
= "reads a number of bytes at a specified offset",
395 static int read_f(int argc
, char **argv
)
397 struct timeval t1
, t2
;
398 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
399 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
404 /* Some compilers get confused and warn if this is not initialized. */
406 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
408 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
418 pattern_count
= cvtnum(optarg
);
419 if (pattern_count
< 0) {
420 printf("non-numeric length argument -- %s\n", optarg
);
429 pattern
= parse_pattern(optarg
);
439 pattern_offset
= cvtnum(optarg
);
440 if (pattern_offset
< 0) {
441 printf("non-numeric length argument -- %s\n", optarg
);
449 return command_usage(&read_cmd
);
453 if (optind
!= argc
- 2) {
454 return command_usage(&read_cmd
);
457 if (bflag
&& pflag
) {
458 printf("-b and -p cannot be specified at the same time\n");
462 offset
= cvtnum(argv
[optind
]);
464 printf("non-numeric length argument -- %s\n", argv
[optind
]);
469 count
= cvtnum(argv
[optind
]);
471 printf("non-numeric length argument -- %s\n", argv
[optind
]);
475 if (!Pflag
&& (lflag
|| sflag
)) {
476 return command_usage(&read_cmd
);
480 pattern_count
= count
- pattern_offset
;
483 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
484 printf("pattern verification range exceeds end of read data\n");
489 if (offset
& 0x1ff) {
490 printf("offset %" PRId64
" is not sector aligned\n",
495 printf("count %d is not sector aligned\n",
501 buf
= qemu_io_alloc(count
, 0xab);
503 gettimeofday(&t1
, NULL
);
505 cnt
= do_pread(buf
, offset
, count
, &total
);
507 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
509 cnt
= do_read(buf
, offset
, count
, &total
);
511 gettimeofday(&t2
, NULL
);
514 printf("read failed: %s\n", strerror(-cnt
));
519 void *cmp_buf
= g_malloc(pattern_count
);
520 memset(cmp_buf
, pattern
, pattern_count
);
521 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
522 printf("Pattern verification failed at offset %"
523 PRId64
", %d bytes\n",
524 offset
+ pattern_offset
, pattern_count
);
534 dump_buffer(buf
, offset
, count
);
537 /* Finally, report back -- -C gives a parsable format */
539 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
547 static void readv_help(void)
551 " reads a range of bytes from the given offset into multiple buffers\n"
554 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
556 " Reads a segment of the currently open file, optionally dumping it to the\n"
557 " standard output stream (with -v option) for subsequent inspection.\n"
558 " Uses multiple iovec buffers if more than one byte range is specified.\n"
559 " -C, -- report statistics in a machine parsable format\n"
560 " -P, -- use a pattern to verify read data\n"
561 " -v, -- dump buffer to standard output\n"
562 " -q, -- quiet mode, do not show I/O statistics\n"
566 static int readv_f(int argc
, char **argv
);
568 static const cmdinfo_t readv_cmd
= {
573 .args
= "[-Cqv] [-P pattern ] off len [len..]",
574 .oneline
= "reads a number of bytes at a specified offset",
578 static int readv_f(int argc
, char **argv
)
580 struct timeval t1
, t2
;
581 int Cflag
= 0, qflag
= 0, vflag
= 0;
585 /* Some compilers get confused and warn if this is not initialized. */
592 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
599 pattern
= parse_pattern(optarg
);
611 return command_usage(&readv_cmd
);
615 if (optind
> argc
- 2) {
616 return command_usage(&readv_cmd
);
620 offset
= cvtnum(argv
[optind
]);
622 printf("non-numeric length argument -- %s\n", argv
[optind
]);
627 if (offset
& 0x1ff) {
628 printf("offset %" PRId64
" is not sector aligned\n",
633 nr_iov
= argc
- optind
;
634 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
639 gettimeofday(&t1
, NULL
);
640 cnt
= do_aio_readv(&qiov
, offset
, &total
);
641 gettimeofday(&t2
, NULL
);
644 printf("readv failed: %s\n", strerror(-cnt
));
649 void *cmp_buf
= g_malloc(qiov
.size
);
650 memset(cmp_buf
, pattern
, qiov
.size
);
651 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
652 printf("Pattern verification failed at offset %"
653 PRId64
", %zd bytes\n", offset
, qiov
.size
);
663 dump_buffer(buf
, offset
, qiov
.size
);
666 /* Finally, report back -- -C gives a parsable format */
668 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
675 static void write_help(void)
679 " writes a range of bytes from the given offset\n"
682 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
684 " Writes into a segment of the currently open file, using a buffer\n"
685 " filled with a set pattern (0xcdcdcdcd).\n"
686 " -b, -- write to the VM state rather than the virtual disk\n"
687 " -p, -- use bdrv_pwrite to write the file\n"
688 " -P, -- use different pattern to fill file\n"
689 " -C, -- report statistics in a machine parsable format\n"
690 " -q, -- quiet mode, do not show I/O statistics\n"
691 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
695 static int write_f(int argc
, char **argv
);
697 static const cmdinfo_t write_cmd
= {
703 .args
= "[-bCpqz] [-P pattern ] off len",
704 .oneline
= "writes a number of bytes at a specified offset",
708 static int write_f(int argc
, char **argv
)
710 struct timeval t1
, t2
;
711 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
716 /* Some compilers get confused and warn if this is not initialized. */
720 while ((c
= getopt(argc
, argv
, "bCpP:qz")) != EOF
) {
733 pattern
= parse_pattern(optarg
);
745 return command_usage(&write_cmd
);
749 if (optind
!= argc
- 2) {
750 return command_usage(&write_cmd
);
753 if (bflag
+ pflag
+ zflag
> 1) {
754 printf("-b, -p, or -z cannot be specified at the same time\n");
758 if (zflag
&& Pflag
) {
759 printf("-z and -P cannot be specified at the same time\n");
763 offset
= cvtnum(argv
[optind
]);
765 printf("non-numeric length argument -- %s\n", argv
[optind
]);
770 count
= cvtnum(argv
[optind
]);
772 printf("non-numeric length argument -- %s\n", argv
[optind
]);
777 if (offset
& 0x1ff) {
778 printf("offset %" PRId64
" is not sector aligned\n",
784 printf("count %d is not sector aligned\n",
791 buf
= qemu_io_alloc(count
, pattern
);
794 gettimeofday(&t1
, NULL
);
796 cnt
= do_pwrite(buf
, offset
, count
, &total
);
798 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
800 cnt
= do_co_write_zeroes(offset
, count
, &total
);
802 cnt
= do_write(buf
, offset
, count
, &total
);
804 gettimeofday(&t2
, NULL
);
807 printf("write failed: %s\n", strerror(-cnt
));
815 /* Finally, report back -- -C gives a parsable format */
817 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
832 " writes a range of bytes from the given offset source from multiple buffers\n"
835 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
837 " Writes into a segment of the currently open file, using a buffer\n"
838 " filled with a set pattern (0xcdcdcdcd).\n"
839 " -P, -- use different pattern to fill file\n"
840 " -C, -- report statistics in a machine parsable format\n"
841 " -q, -- quiet mode, do not show I/O statistics\n"
845 static int writev_f(int argc
, char **argv
);
847 static const cmdinfo_t writev_cmd
= {
852 .args
= "[-Cq] [-P pattern ] off len [len..]",
853 .oneline
= "writes a number of bytes at a specified offset",
857 static int writev_f(int argc
, char **argv
)
859 struct timeval t1
, t2
;
860 int Cflag
= 0, qflag
= 0;
864 /* Some compilers get confused and warn if this is not initialized. */
870 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
879 pattern
= parse_pattern(optarg
);
885 return command_usage(&writev_cmd
);
889 if (optind
> argc
- 2) {
890 return command_usage(&writev_cmd
);
893 offset
= cvtnum(argv
[optind
]);
895 printf("non-numeric length argument -- %s\n", argv
[optind
]);
900 if (offset
& 0x1ff) {
901 printf("offset %" PRId64
" is not sector aligned\n",
906 nr_iov
= argc
- optind
;
907 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
912 gettimeofday(&t1
, NULL
);
913 cnt
= do_aio_writev(&qiov
, offset
, &total
);
914 gettimeofday(&t2
, NULL
);
917 printf("writev failed: %s\n", strerror(-cnt
));
925 /* Finally, report back -- -C gives a parsable format */
927 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
933 static void multiwrite_help(void)
937 " writes a range of bytes from the given offset source from multiple buffers,\n"
938 " in a batch of requests that may be merged by qemu\n"
941 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
942 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
944 " Writes into a segment of the currently open file, using a buffer\n"
945 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
946 " by one for each request contained in the multiwrite command.\n"
947 " -P, -- use different pattern to fill file\n"
948 " -C, -- report statistics in a machine parsable format\n"
949 " -q, -- quiet mode, do not show I/O statistics\n"
953 static int multiwrite_f(int argc
, char **argv
);
955 static const cmdinfo_t multiwrite_cmd
= {
956 .name
= "multiwrite",
957 .cfunc
= multiwrite_f
,
960 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
961 .oneline
= "issues multiple write requests at once",
962 .help
= multiwrite_help
,
965 static int multiwrite_f(int argc
, char **argv
)
967 struct timeval t1
, t2
;
968 int Cflag
= 0, qflag
= 0;
971 int64_t offset
, first_offset
= 0;
972 /* Some compilers get confused and warn if this is not initialized. */
981 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
990 pattern
= parse_pattern(optarg
);
996 return command_usage(&writev_cmd
);
1000 if (optind
> argc
- 2) {
1001 return command_usage(&writev_cmd
);
1005 for (i
= optind
; i
< argc
; i
++) {
1006 if (!strcmp(argv
[i
], ";")) {
1011 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
1012 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
1013 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
1015 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1018 /* Read the offset of the request */
1019 offset
= cvtnum(argv
[optind
]);
1021 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
1026 if (offset
& 0x1ff) {
1027 printf("offset %lld is not sector aligned\n",
1033 first_offset
= offset
;
1036 /* Read lengths for qiov entries */
1037 for (j
= optind
; j
< argc
; j
++) {
1038 if (!strcmp(argv
[j
], ";")) {
1043 nr_iov
= j
- optind
;
1046 buf
[i
] = create_iovec(&qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1047 if (buf
[i
] == NULL
) {
1051 reqs
[i
].qiov
= &qiovs
[i
];
1052 reqs
[i
].sector
= offset
>> 9;
1053 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1060 /* If there were empty requests at the end, ignore them */
1063 gettimeofday(&t1
, NULL
);
1064 cnt
= do_aio_multiwrite(reqs
, nr_reqs
, &total
);
1065 gettimeofday(&t2
, NULL
);
1068 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1076 /* Finally, report back -- -C gives a parsable format */
1078 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1080 for (i
= 0; i
< nr_reqs
; i
++) {
1081 qemu_io_free(buf
[i
]);
1082 if (reqs
[i
].qiov
!= NULL
) {
1083 qemu_iovec_destroy(&qiovs
[i
]);
1104 static void aio_write_done(void *opaque
, int ret
)
1106 struct aio_ctx
*ctx
= opaque
;
1109 gettimeofday(&t2
, NULL
);
1113 printf("aio_write failed: %s\n", strerror(-ret
));
1121 /* Finally, report back -- -C gives a parsable format */
1122 t2
= tsub(t2
, ctx
->t1
);
1123 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1124 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1126 qemu_io_free(ctx
->buf
);
1130 static void aio_read_done(void *opaque
, int ret
)
1132 struct aio_ctx
*ctx
= opaque
;
1135 gettimeofday(&t2
, NULL
);
1138 printf("readv failed: %s\n", strerror(-ret
));
1143 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1145 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1146 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1147 printf("Pattern verification failed at offset %"
1148 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1158 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1161 /* Finally, report back -- -C gives a parsable format */
1162 t2
= tsub(t2
, ctx
->t1
);
1163 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1164 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1166 qemu_io_free(ctx
->buf
);
1170 static void aio_read_help(void)
1174 " asynchronously reads a range of bytes from the given offset\n"
1177 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1179 " Reads a segment of the currently open file, optionally dumping it to the\n"
1180 " standard output stream (with -v option) for subsequent inspection.\n"
1181 " The read is performed asynchronously and the aio_flush command must be\n"
1182 " used to ensure all outstanding aio requests have been completed.\n"
1183 " -C, -- report statistics in a machine parsable format\n"
1184 " -P, -- use a pattern to verify read data\n"
1185 " -v, -- dump buffer to standard output\n"
1186 " -q, -- quiet mode, do not show I/O statistics\n"
1190 static int aio_read_f(int argc
, char **argv
);
1192 static const cmdinfo_t aio_read_cmd
= {
1194 .cfunc
= aio_read_f
,
1197 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1198 .oneline
= "asynchronously reads a number of bytes",
1199 .help
= aio_read_help
,
1202 static int aio_read_f(int argc
, char **argv
)
1205 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1207 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1214 ctx
->pattern
= parse_pattern(optarg
);
1215 if (ctx
->pattern
< 0) {
1228 return command_usage(&aio_read_cmd
);
1232 if (optind
> argc
- 2) {
1234 return command_usage(&aio_read_cmd
);
1237 ctx
->offset
= cvtnum(argv
[optind
]);
1238 if (ctx
->offset
< 0) {
1239 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1245 if (ctx
->offset
& 0x1ff) {
1246 printf("offset %" PRId64
" is not sector aligned\n",
1252 nr_iov
= argc
- optind
;
1253 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1254 if (ctx
->buf
== NULL
) {
1259 gettimeofday(&ctx
->t1
, NULL
);
1260 bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1261 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1265 static void aio_write_help(void)
1269 " asynchronously writes a range of bytes from the given offset source\n"
1270 " from multiple buffers\n"
1273 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1275 " Writes into a segment of the currently open file, using a buffer\n"
1276 " filled with a set pattern (0xcdcdcdcd).\n"
1277 " The write is performed asynchronously and the aio_flush command must be\n"
1278 " used to ensure all outstanding aio requests have been completed.\n"
1279 " -P, -- use different pattern to fill file\n"
1280 " -C, -- report statistics in a machine parsable format\n"
1281 " -q, -- quiet mode, do not show I/O statistics\n"
1285 static int aio_write_f(int argc
, char **argv
);
1287 static const cmdinfo_t aio_write_cmd
= {
1288 .name
= "aio_write",
1289 .cfunc
= aio_write_f
,
1292 .args
= "[-Cq] [-P pattern ] off len [len..]",
1293 .oneline
= "asynchronously writes a number of bytes",
1294 .help
= aio_write_help
,
1297 static int aio_write_f(int argc
, char **argv
)
1301 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1303 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1312 pattern
= parse_pattern(optarg
);
1320 return command_usage(&aio_write_cmd
);
1324 if (optind
> argc
- 2) {
1326 return command_usage(&aio_write_cmd
);
1329 ctx
->offset
= cvtnum(argv
[optind
]);
1330 if (ctx
->offset
< 0) {
1331 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1337 if (ctx
->offset
& 0x1ff) {
1338 printf("offset %" PRId64
" is not sector aligned\n",
1344 nr_iov
= argc
- optind
;
1345 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1346 if (ctx
->buf
== NULL
) {
1351 gettimeofday(&ctx
->t1
, NULL
);
1352 bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1353 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1357 static int aio_flush_f(int argc
, char **argv
)
1363 static const cmdinfo_t aio_flush_cmd
= {
1364 .name
= "aio_flush",
1365 .cfunc
= aio_flush_f
,
1366 .oneline
= "completes all outstanding aio requests"
1369 static int flush_f(int argc
, char **argv
)
1375 static const cmdinfo_t flush_cmd
= {
1379 .oneline
= "flush all in-core file state to disk",
1382 static int truncate_f(int argc
, char **argv
)
1387 offset
= cvtnum(argv
[1]);
1389 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1393 ret
= bdrv_truncate(bs
, offset
);
1395 printf("truncate: %s\n", strerror(-ret
));
1402 static const cmdinfo_t truncate_cmd
= {
1405 .cfunc
= truncate_f
,
1409 .oneline
= "truncates the current file at the given offset",
1412 static int length_f(int argc
, char **argv
)
1417 size
= bdrv_getlength(bs
);
1419 printf("getlength: %s\n", strerror(-size
));
1423 cvtstr(size
, s1
, sizeof(s1
));
1429 static const cmdinfo_t length_cmd
= {
1433 .oneline
= "gets the length of the current file",
1437 static int info_f(int argc
, char **argv
)
1439 BlockDriverInfo bdi
;
1440 char s1
[64], s2
[64];
1443 if (bs
->drv
&& bs
->drv
->format_name
) {
1444 printf("format name: %s\n", bs
->drv
->format_name
);
1446 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1447 printf("format name: %s\n", bs
->drv
->protocol_name
);
1450 ret
= bdrv_get_info(bs
, &bdi
);
1455 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1456 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1458 printf("cluster size: %s\n", s1
);
1459 printf("vm state offset: %s\n", s2
);
1466 static const cmdinfo_t info_cmd
= {
1470 .oneline
= "prints information about the current file",
1473 static void discard_help(void)
1477 " discards a range of bytes from the given offset\n"
1480 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1482 " Discards a segment of the currently open file.\n"
1483 " -C, -- report statistics in a machine parsable format\n"
1484 " -q, -- quiet mode, do not show I/O statistics\n"
1488 static int discard_f(int argc
, char **argv
);
1490 static const cmdinfo_t discard_cmd
= {
1496 .args
= "[-Cq] off len",
1497 .oneline
= "discards a number of bytes at a specified offset",
1498 .help
= discard_help
,
1501 static int discard_f(int argc
, char **argv
)
1503 struct timeval t1
, t2
;
1504 int Cflag
= 0, qflag
= 0;
1509 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1518 return command_usage(&discard_cmd
);
1522 if (optind
!= argc
- 2) {
1523 return command_usage(&discard_cmd
);
1526 offset
= cvtnum(argv
[optind
]);
1528 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1533 count
= cvtnum(argv
[optind
]);
1535 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1539 gettimeofday(&t1
, NULL
);
1540 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1541 count
>> BDRV_SECTOR_BITS
);
1542 gettimeofday(&t2
, NULL
);
1545 printf("discard failed: %s\n", strerror(-ret
));
1549 /* Finally, report back -- -C gives a parsable format */
1552 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1559 static int alloc_f(int argc
, char **argv
)
1562 int nb_sectors
, remaining
;
1567 offset
= cvtnum(argv
[1]);
1568 if (offset
& 0x1ff) {
1569 printf("offset %" PRId64
" is not sector aligned\n",
1575 nb_sectors
= cvtnum(argv
[2]);
1580 remaining
= nb_sectors
;
1583 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1590 cvtstr(offset
, s1
, sizeof(s1
));
1592 printf("%d/%d sectors allocated at offset %s\n",
1593 sum_alloc
, nb_sectors
, s1
);
1597 static const cmdinfo_t alloc_cmd
= {
1603 .args
= "off [sectors]",
1604 .oneline
= "checks if a sector is present in the file",
1607 static int map_f(int argc
, char **argv
)
1612 int num
, num_checked
;
1617 nb_sectors
= bs
->total_sectors
;
1620 num_checked
= MIN(nb_sectors
, INT_MAX
);
1621 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1622 retstr
= ret
? " allocated" : "not allocated";
1623 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1624 printf("[% 24" PRId64
"] % 8d/% 8d sectors %s at offset %s (%d)\n",
1625 offset
<< 9ULL, num
, num_checked
, retstr
, s1
, ret
);
1629 } while (offset
< bs
->total_sectors
);
1634 static const cmdinfo_t map_cmd
= {
1640 .oneline
= "prints the allocated areas of a file",
1644 static int close_f(int argc
, char **argv
)
1651 static const cmdinfo_t close_cmd
= {
1655 .oneline
= "close the current open file",
1658 static int openfile(char *name
, int flags
, int growable
)
1661 fprintf(stderr
, "file open already, try 'help close'\n");
1666 if (bdrv_file_open(&bs
, name
, flags
)) {
1667 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1671 bs
= bdrv_new("hda");
1673 if (bdrv_open(bs
, name
, flags
, NULL
) < 0) {
1674 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1684 static void open_help(void)
1688 " opens a new file in the requested mode\n"
1691 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1693 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1694 " -r, -- open file read-only\n"
1695 " -s, -- use snapshot file\n"
1696 " -n, -- disable host cache\n"
1697 " -g, -- allow file to grow (only applies to protocols)"
1701 static int open_f(int argc
, char **argv
);
1703 static const cmdinfo_t open_cmd
= {
1709 .flags
= CMD_NOFILE_OK
,
1710 .args
= "[-Crsn] [path]",
1711 .oneline
= "open the file specified by path",
1715 static int open_f(int argc
, char **argv
)
1722 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
1725 flags
|= BDRV_O_SNAPSHOT
;
1728 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1737 return command_usage(&open_cmd
);
1742 flags
|= BDRV_O_RDWR
;
1745 if (optind
!= argc
- 1) {
1746 return command_usage(&open_cmd
);
1749 return openfile(argv
[optind
], flags
, growable
);
1752 static int init_args_command(int index
)
1754 /* only one device allowed so far */
1761 static int init_check_command(const cmdinfo_t
*ct
)
1763 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
1766 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1767 fprintf(stderr
, "no file open, try 'help open'\n");
1773 static void usage(const char *name
)
1776 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1777 "QEMU Disk exerciser\n"
1779 " -c, --cmd command to execute\n"
1780 " -r, --read-only export read-only\n"
1781 " -s, --snapshot use snapshot file\n"
1782 " -n, --nocache disable host cache\n"
1783 " -g, --growable allow file to grow (only applies to protocols)\n"
1784 " -m, --misalign misalign allocations for O_DIRECT\n"
1785 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1786 " -h, --help display this help and exit\n"
1787 " -V, --version output version information and exit\n"
1793 int main(int argc
, char **argv
)
1797 const char *sopt
= "hVc:rsnmgk";
1798 const struct option lopt
[] = {
1799 { "help", 0, NULL
, 'h' },
1800 { "version", 0, NULL
, 'V' },
1801 { "offset", 1, NULL
, 'o' },
1802 { "cmd", 1, NULL
, 'c' },
1803 { "read-only", 0, NULL
, 'r' },
1804 { "snapshot", 0, NULL
, 's' },
1805 { "nocache", 0, NULL
, 'n' },
1806 { "misalign", 0, NULL
, 'm' },
1807 { "growable", 0, NULL
, 'g' },
1808 { "native-aio", 0, NULL
, 'k' },
1809 { NULL
, 0, NULL
, 0 }
1815 progname
= basename(argv
[0]);
1817 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1820 flags
|= BDRV_O_SNAPSHOT
;
1823 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1826 add_user_command(optarg
);
1838 flags
|= BDRV_O_NATIVE_AIO
;
1841 printf("%s version %s\n", progname
, VERSION
);
1852 if ((argc
- optind
) > 1) {
1859 /* initialize commands */
1862 add_command(&open_cmd
);
1863 add_command(&close_cmd
);
1864 add_command(&read_cmd
);
1865 add_command(&readv_cmd
);
1866 add_command(&write_cmd
);
1867 add_command(&writev_cmd
);
1868 add_command(&multiwrite_cmd
);
1869 add_command(&aio_read_cmd
);
1870 add_command(&aio_write_cmd
);
1871 add_command(&aio_flush_cmd
);
1872 add_command(&flush_cmd
);
1873 add_command(&truncate_cmd
);
1874 add_command(&length_cmd
);
1875 add_command(&info_cmd
);
1876 add_command(&discard_cmd
);
1877 add_command(&alloc_cmd
);
1878 add_command(&map_cmd
);
1880 add_args_command(init_args_command
);
1881 add_check_command(init_check_command
);
1883 /* open the device */
1885 flags
|= BDRV_O_RDWR
;
1888 if ((argc
- optind
) == 1) {
1889 openfile(argv
[optind
], flags
, growable
);
1894 * Make sure all outstanding requests complete before the program exits.