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
);
221 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
223 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
230 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
232 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
239 #define NOT_DONE 0x7fffffff
240 static void aio_rw_done(void *opaque
, int ret
)
242 *(int *)opaque
= ret
;
245 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
247 int async_ret
= NOT_DONE
;
249 bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
250 aio_rw_done
, &async_ret
);
251 while (async_ret
== NOT_DONE
) {
256 return async_ret
< 0 ? async_ret
: 1;
259 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
261 int async_ret
= NOT_DONE
;
263 bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
264 aio_rw_done
, &async_ret
);
265 while (async_ret
== NOT_DONE
) {
270 return async_ret
< 0 ? async_ret
: 1;
273 struct multiwrite_async_ret
{
278 static void multiwrite_cb(void *opaque
, int ret
)
280 struct multiwrite_async_ret
*async_ret
= opaque
;
282 async_ret
->num_done
++;
284 async_ret
->error
= ret
;
288 static int do_aio_multiwrite(BlockRequest
* reqs
, int num_reqs
, int *total
)
291 struct multiwrite_async_ret async_ret
= {
297 for (i
= 0; i
< num_reqs
; i
++) {
298 reqs
[i
].cb
= multiwrite_cb
;
299 reqs
[i
].opaque
= &async_ret
;
300 *total
+= reqs
[i
].qiov
->size
;
303 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
308 while (async_ret
.num_done
< num_reqs
) {
312 return async_ret
.error
< 0 ? async_ret
.error
: 1;
315 static void read_help(void)
319 " reads a range of bytes from the given offset\n"
322 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
324 " Reads a segment of the currently open file, optionally dumping it to the\n"
325 " standard output stream (with -v option) for subsequent inspection.\n"
326 " -b, -- read from the VM state rather than the virtual disk\n"
327 " -C, -- report statistics in a machine parsable format\n"
328 " -l, -- length for pattern verification (only with -P)\n"
329 " -p, -- use bdrv_pread to read the file\n"
330 " -P, -- use a pattern to verify read data\n"
331 " -q, -- quiet mode, do not show I/O statistics\n"
332 " -s, -- start offset for pattern verification (only with -P)\n"
333 " -v, -- dump buffer to standard output\n"
337 static int read_f(int argc
, char **argv
);
339 static const cmdinfo_t read_cmd
= {
345 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
346 .oneline
= "reads a number of bytes at a specified offset",
350 static int read_f(int argc
, char **argv
)
352 struct timeval t1
, t2
;
353 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
354 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
359 /* Some compilers get confused and warn if this is not initialized. */
361 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
363 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
373 pattern_count
= cvtnum(optarg
);
374 if (pattern_count
< 0) {
375 printf("non-numeric length argument -- %s\n", optarg
);
384 pattern
= parse_pattern(optarg
);
394 pattern_offset
= cvtnum(optarg
);
395 if (pattern_offset
< 0) {
396 printf("non-numeric length argument -- %s\n", optarg
);
404 return command_usage(&read_cmd
);
408 if (optind
!= argc
- 2) {
409 return command_usage(&read_cmd
);
412 if (bflag
&& pflag
) {
413 printf("-b and -p cannot be specified at the same time\n");
417 offset
= cvtnum(argv
[optind
]);
419 printf("non-numeric length argument -- %s\n", argv
[optind
]);
424 count
= cvtnum(argv
[optind
]);
426 printf("non-numeric length argument -- %s\n", argv
[optind
]);
430 if (!Pflag
&& (lflag
|| sflag
)) {
431 return command_usage(&read_cmd
);
435 pattern_count
= count
- pattern_offset
;
438 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
439 printf("pattern verification range exceeds end of read data\n");
444 if (offset
& 0x1ff) {
445 printf("offset %" PRId64
" is not sector aligned\n",
450 printf("count %d is not sector aligned\n",
456 buf
= qemu_io_alloc(count
, 0xab);
458 gettimeofday(&t1
, NULL
);
460 cnt
= do_pread(buf
, offset
, count
, &total
);
462 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
464 cnt
= do_read(buf
, offset
, count
, &total
);
466 gettimeofday(&t2
, NULL
);
469 printf("read failed: %s\n", strerror(-cnt
));
474 void *cmp_buf
= g_malloc(pattern_count
);
475 memset(cmp_buf
, pattern
, pattern_count
);
476 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
477 printf("Pattern verification failed at offset %"
478 PRId64
", %d bytes\n",
479 offset
+ pattern_offset
, pattern_count
);
489 dump_buffer(buf
, offset
, count
);
492 /* Finally, report back -- -C gives a parsable format */
494 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
502 static void readv_help(void)
506 " reads a range of bytes from the given offset into multiple buffers\n"
509 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
511 " Reads a segment of the currently open file, optionally dumping it to the\n"
512 " standard output stream (with -v option) for subsequent inspection.\n"
513 " Uses multiple iovec buffers if more than one byte range is specified.\n"
514 " -C, -- report statistics in a machine parsable format\n"
515 " -P, -- use a pattern to verify read data\n"
516 " -v, -- dump buffer to standard output\n"
517 " -q, -- quiet mode, do not show I/O statistics\n"
521 static int readv_f(int argc
, char **argv
);
523 static const cmdinfo_t readv_cmd
= {
528 .args
= "[-Cqv] [-P pattern ] off len [len..]",
529 .oneline
= "reads a number of bytes at a specified offset",
533 static int readv_f(int argc
, char **argv
)
535 struct timeval t1
, t2
;
536 int Cflag
= 0, qflag
= 0, vflag
= 0;
540 /* Some compilers get confused and warn if this is not initialized. */
547 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
554 pattern
= parse_pattern(optarg
);
566 return command_usage(&readv_cmd
);
570 if (optind
> argc
- 2) {
571 return command_usage(&readv_cmd
);
575 offset
= cvtnum(argv
[optind
]);
577 printf("non-numeric length argument -- %s\n", argv
[optind
]);
582 if (offset
& 0x1ff) {
583 printf("offset %" PRId64
" is not sector aligned\n",
588 nr_iov
= argc
- optind
;
589 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
594 gettimeofday(&t1
, NULL
);
595 cnt
= do_aio_readv(&qiov
, offset
, &total
);
596 gettimeofday(&t2
, NULL
);
599 printf("readv failed: %s\n", strerror(-cnt
));
604 void *cmp_buf
= g_malloc(qiov
.size
);
605 memset(cmp_buf
, pattern
, qiov
.size
);
606 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
607 printf("Pattern verification failed at offset %"
608 PRId64
", %zd bytes\n", offset
, qiov
.size
);
618 dump_buffer(buf
, offset
, qiov
.size
);
621 /* Finally, report back -- -C gives a parsable format */
623 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
630 static void write_help(void)
634 " writes a range of bytes from the given offset\n"
637 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
639 " Writes into a segment of the currently open file, using a buffer\n"
640 " filled with a set pattern (0xcdcdcdcd).\n"
641 " -b, -- write to the VM state rather than the virtual disk\n"
642 " -p, -- use bdrv_pwrite to write the file\n"
643 " -P, -- use different pattern to fill file\n"
644 " -C, -- report statistics in a machine parsable format\n"
645 " -q, -- quiet mode, do not show I/O statistics\n"
649 static int write_f(int argc
, char **argv
);
651 static const cmdinfo_t write_cmd
= {
657 .args
= "[-abCpq] [-P pattern ] off len",
658 .oneline
= "writes a number of bytes at a specified offset",
662 static int write_f(int argc
, char **argv
)
664 struct timeval t1
, t2
;
665 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0;
670 /* Some compilers get confused and warn if this is not initialized. */
674 while ((c
= getopt(argc
, argv
, "bCpP:q")) != EOF
) {
686 pattern
= parse_pattern(optarg
);
695 return command_usage(&write_cmd
);
699 if (optind
!= argc
- 2) {
700 return command_usage(&write_cmd
);
703 if (bflag
&& pflag
) {
704 printf("-b and -p cannot be specified at the same time\n");
708 offset
= cvtnum(argv
[optind
]);
710 printf("non-numeric length argument -- %s\n", argv
[optind
]);
715 count
= cvtnum(argv
[optind
]);
717 printf("non-numeric length argument -- %s\n", argv
[optind
]);
722 if (offset
& 0x1ff) {
723 printf("offset %" PRId64
" is not sector aligned\n",
729 printf("count %d is not sector aligned\n",
735 buf
= qemu_io_alloc(count
, pattern
);
737 gettimeofday(&t1
, NULL
);
739 cnt
= do_pwrite(buf
, offset
, count
, &total
);
741 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
743 cnt
= do_write(buf
, offset
, count
, &total
);
745 gettimeofday(&t2
, NULL
);
748 printf("write failed: %s\n", strerror(-cnt
));
756 /* Finally, report back -- -C gives a parsable format */
758 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
771 " writes a range of bytes from the given offset source from multiple buffers\n"
774 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
776 " Writes into a segment of the currently open file, using a buffer\n"
777 " filled with a set pattern (0xcdcdcdcd).\n"
778 " -P, -- use different pattern to fill file\n"
779 " -C, -- report statistics in a machine parsable format\n"
780 " -q, -- quiet mode, do not show I/O statistics\n"
784 static int writev_f(int argc
, char **argv
);
786 static const cmdinfo_t writev_cmd
= {
791 .args
= "[-Cq] [-P pattern ] off len [len..]",
792 .oneline
= "writes a number of bytes at a specified offset",
796 static int writev_f(int argc
, char **argv
)
798 struct timeval t1
, t2
;
799 int Cflag
= 0, qflag
= 0;
803 /* Some compilers get confused and warn if this is not initialized. */
809 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
818 pattern
= parse_pattern(optarg
);
824 return command_usage(&writev_cmd
);
828 if (optind
> argc
- 2) {
829 return command_usage(&writev_cmd
);
832 offset
= cvtnum(argv
[optind
]);
834 printf("non-numeric length argument -- %s\n", argv
[optind
]);
839 if (offset
& 0x1ff) {
840 printf("offset %" PRId64
" is not sector aligned\n",
845 nr_iov
= argc
- optind
;
846 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
851 gettimeofday(&t1
, NULL
);
852 cnt
= do_aio_writev(&qiov
, offset
, &total
);
853 gettimeofday(&t2
, NULL
);
856 printf("writev failed: %s\n", strerror(-cnt
));
864 /* Finally, report back -- -C gives a parsable format */
866 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
872 static void multiwrite_help(void)
876 " writes a range of bytes from the given offset source from multiple buffers,\n"
877 " in a batch of requests that may be merged by qemu\n"
880 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
881 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
883 " Writes into a segment of the currently open file, using a buffer\n"
884 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
885 " by one for each request contained in the multiwrite command.\n"
886 " -P, -- use different pattern to fill file\n"
887 " -C, -- report statistics in a machine parsable format\n"
888 " -q, -- quiet mode, do not show I/O statistics\n"
892 static int multiwrite_f(int argc
, char **argv
);
894 static const cmdinfo_t multiwrite_cmd
= {
895 .name
= "multiwrite",
896 .cfunc
= multiwrite_f
,
899 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
900 .oneline
= "issues multiple write requests at once",
901 .help
= multiwrite_help
,
904 static int multiwrite_f(int argc
, char **argv
)
906 struct timeval t1
, t2
;
907 int Cflag
= 0, qflag
= 0;
910 int64_t offset
, first_offset
= 0;
911 /* Some compilers get confused and warn if this is not initialized. */
920 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
929 pattern
= parse_pattern(optarg
);
935 return command_usage(&writev_cmd
);
939 if (optind
> argc
- 2) {
940 return command_usage(&writev_cmd
);
944 for (i
= optind
; i
< argc
; i
++) {
945 if (!strcmp(argv
[i
], ";")) {
950 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
951 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
952 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
954 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
957 /* Read the offset of the request */
958 offset
= cvtnum(argv
[optind
]);
960 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
965 if (offset
& 0x1ff) {
966 printf("offset %lld is not sector aligned\n",
972 first_offset
= offset
;
975 /* Read lengths for qiov entries */
976 for (j
= optind
; j
< argc
; j
++) {
977 if (!strcmp(argv
[j
], ";")) {
985 buf
[i
] = create_iovec(&qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
986 if (buf
[i
] == NULL
) {
990 reqs
[i
].qiov
= &qiovs
[i
];
991 reqs
[i
].sector
= offset
>> 9;
992 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
999 /* If there were empty requests at the end, ignore them */
1002 gettimeofday(&t1
, NULL
);
1003 cnt
= do_aio_multiwrite(reqs
, nr_reqs
, &total
);
1004 gettimeofday(&t2
, NULL
);
1007 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1015 /* Finally, report back -- -C gives a parsable format */
1017 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1019 for (i
= 0; i
< nr_reqs
; i
++) {
1020 qemu_io_free(buf
[i
]);
1021 if (reqs
[i
].qiov
!= NULL
) {
1022 qemu_iovec_destroy(&qiovs
[i
]);
1043 static void aio_write_done(void *opaque
, int ret
)
1045 struct aio_ctx
*ctx
= opaque
;
1048 gettimeofday(&t2
, NULL
);
1052 printf("aio_write failed: %s\n", strerror(-ret
));
1060 /* Finally, report back -- -C gives a parsable format */
1061 t2
= tsub(t2
, ctx
->t1
);
1062 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1063 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1065 qemu_io_free(ctx
->buf
);
1069 static void aio_read_done(void *opaque
, int ret
)
1071 struct aio_ctx
*ctx
= opaque
;
1074 gettimeofday(&t2
, NULL
);
1077 printf("readv failed: %s\n", strerror(-ret
));
1082 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1084 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1085 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1086 printf("Pattern verification failed at offset %"
1087 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1097 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1100 /* Finally, report back -- -C gives a parsable format */
1101 t2
= tsub(t2
, ctx
->t1
);
1102 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1103 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1105 qemu_io_free(ctx
->buf
);
1109 static void aio_read_help(void)
1113 " asynchronously reads a range of bytes from the given offset\n"
1116 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1118 " Reads a segment of the currently open file, optionally dumping it to the\n"
1119 " standard output stream (with -v option) for subsequent inspection.\n"
1120 " The read is performed asynchronously and the aio_flush command must be\n"
1121 " used to ensure all outstanding aio requests have been completed.\n"
1122 " -C, -- report statistics in a machine parsable format\n"
1123 " -P, -- use a pattern to verify read data\n"
1124 " -v, -- dump buffer to standard output\n"
1125 " -q, -- quiet mode, do not show I/O statistics\n"
1129 static int aio_read_f(int argc
, char **argv
);
1131 static const cmdinfo_t aio_read_cmd
= {
1133 .cfunc
= aio_read_f
,
1136 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1137 .oneline
= "asynchronously reads a number of bytes",
1138 .help
= aio_read_help
,
1141 static int aio_read_f(int argc
, char **argv
)
1144 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1146 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1153 ctx
->pattern
= parse_pattern(optarg
);
1154 if (ctx
->pattern
< 0) {
1167 return command_usage(&aio_read_cmd
);
1171 if (optind
> argc
- 2) {
1173 return command_usage(&aio_read_cmd
);
1176 ctx
->offset
= cvtnum(argv
[optind
]);
1177 if (ctx
->offset
< 0) {
1178 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1184 if (ctx
->offset
& 0x1ff) {
1185 printf("offset %" PRId64
" is not sector aligned\n",
1191 nr_iov
= argc
- optind
;
1192 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1193 if (ctx
->buf
== NULL
) {
1198 gettimeofday(&ctx
->t1
, NULL
);
1199 bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1200 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1204 static void aio_write_help(void)
1208 " asynchronously writes a range of bytes from the given offset source\n"
1209 " from multiple buffers\n"
1212 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1214 " Writes into a segment of the currently open file, using a buffer\n"
1215 " filled with a set pattern (0xcdcdcdcd).\n"
1216 " The write is performed asynchronously and the aio_flush command must be\n"
1217 " used to ensure all outstanding aio requests have been completed.\n"
1218 " -P, -- use different pattern to fill file\n"
1219 " -C, -- report statistics in a machine parsable format\n"
1220 " -q, -- quiet mode, do not show I/O statistics\n"
1224 static int aio_write_f(int argc
, char **argv
);
1226 static const cmdinfo_t aio_write_cmd
= {
1227 .name
= "aio_write",
1228 .cfunc
= aio_write_f
,
1231 .args
= "[-Cq] [-P pattern ] off len [len..]",
1232 .oneline
= "asynchronously writes a number of bytes",
1233 .help
= aio_write_help
,
1236 static int aio_write_f(int argc
, char **argv
)
1240 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1242 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1251 pattern
= parse_pattern(optarg
);
1259 return command_usage(&aio_write_cmd
);
1263 if (optind
> argc
- 2) {
1265 return command_usage(&aio_write_cmd
);
1268 ctx
->offset
= cvtnum(argv
[optind
]);
1269 if (ctx
->offset
< 0) {
1270 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1276 if (ctx
->offset
& 0x1ff) {
1277 printf("offset %" PRId64
" is not sector aligned\n",
1283 nr_iov
= argc
- optind
;
1284 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1285 if (ctx
->buf
== NULL
) {
1290 gettimeofday(&ctx
->t1
, NULL
);
1291 bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1292 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1296 static int aio_flush_f(int argc
, char **argv
)
1302 static const cmdinfo_t aio_flush_cmd
= {
1303 .name
= "aio_flush",
1304 .cfunc
= aio_flush_f
,
1305 .oneline
= "completes all outstanding aio requests"
1308 static int flush_f(int argc
, char **argv
)
1314 static const cmdinfo_t flush_cmd
= {
1318 .oneline
= "flush all in-core file state to disk",
1321 static int truncate_f(int argc
, char **argv
)
1326 offset
= cvtnum(argv
[1]);
1328 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1332 ret
= bdrv_truncate(bs
, offset
);
1334 printf("truncate: %s\n", strerror(-ret
));
1341 static const cmdinfo_t truncate_cmd
= {
1344 .cfunc
= truncate_f
,
1348 .oneline
= "truncates the current file at the given offset",
1351 static int length_f(int argc
, char **argv
)
1356 size
= bdrv_getlength(bs
);
1358 printf("getlength: %s\n", strerror(-size
));
1362 cvtstr(size
, s1
, sizeof(s1
));
1368 static const cmdinfo_t length_cmd
= {
1372 .oneline
= "gets the length of the current file",
1376 static int info_f(int argc
, char **argv
)
1378 BlockDriverInfo bdi
;
1379 char s1
[64], s2
[64];
1382 if (bs
->drv
&& bs
->drv
->format_name
) {
1383 printf("format name: %s\n", bs
->drv
->format_name
);
1385 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1386 printf("format name: %s\n", bs
->drv
->protocol_name
);
1389 ret
= bdrv_get_info(bs
, &bdi
);
1394 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1395 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1397 printf("cluster size: %s\n", s1
);
1398 printf("vm state offset: %s\n", s2
);
1405 static const cmdinfo_t info_cmd
= {
1409 .oneline
= "prints information about the current file",
1412 static void discard_help(void)
1416 " discards a range of bytes from the given offset\n"
1419 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1421 " Discards a segment of the currently open file.\n"
1422 " -C, -- report statistics in a machine parsable format\n"
1423 " -q, -- quiet mode, do not show I/O statistics\n"
1427 static int discard_f(int argc
, char **argv
);
1429 static const cmdinfo_t discard_cmd
= {
1435 .args
= "[-Cq] off len",
1436 .oneline
= "discards a number of bytes at a specified offset",
1437 .help
= discard_help
,
1440 static int discard_f(int argc
, char **argv
)
1442 struct timeval t1
, t2
;
1443 int Cflag
= 0, qflag
= 0;
1448 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1457 return command_usage(&discard_cmd
);
1461 if (optind
!= argc
- 2) {
1462 return command_usage(&discard_cmd
);
1465 offset
= cvtnum(argv
[optind
]);
1467 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1472 count
= cvtnum(argv
[optind
]);
1474 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1478 gettimeofday(&t1
, NULL
);
1479 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1480 count
>> BDRV_SECTOR_BITS
);
1481 gettimeofday(&t2
, NULL
);
1484 printf("discard failed: %s\n", strerror(-ret
));
1488 /* Finally, report back -- -C gives a parsable format */
1491 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1498 static int alloc_f(int argc
, char **argv
)
1501 int nb_sectors
, remaining
;
1506 offset
= cvtnum(argv
[1]);
1507 if (offset
& 0x1ff) {
1508 printf("offset %" PRId64
" is not sector aligned\n",
1514 nb_sectors
= cvtnum(argv
[2]);
1519 remaining
= nb_sectors
;
1522 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1529 cvtstr(offset
, s1
, sizeof(s1
));
1531 printf("%d/%d sectors allocated at offset %s\n",
1532 sum_alloc
, nb_sectors
, s1
);
1536 static const cmdinfo_t alloc_cmd
= {
1542 .args
= "off [sectors]",
1543 .oneline
= "checks if a sector is present in the file",
1546 static int map_f(int argc
, char **argv
)
1551 int num
, num_checked
;
1556 nb_sectors
= bs
->total_sectors
;
1559 num_checked
= MIN(nb_sectors
, INT_MAX
);
1560 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1561 retstr
= ret
? " allocated" : "not allocated";
1562 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1563 printf("[% 24" PRId64
"] % 8d/% 8d sectors %s at offset %s (%d)\n",
1564 offset
<< 9ULL, num
, num_checked
, retstr
, s1
, ret
);
1568 } while (offset
< bs
->total_sectors
);
1573 static const cmdinfo_t map_cmd
= {
1579 .oneline
= "prints the allocated areas of a file",
1583 static int close_f(int argc
, char **argv
)
1590 static const cmdinfo_t close_cmd
= {
1594 .oneline
= "close the current open file",
1597 static int openfile(char *name
, int flags
, int growable
)
1600 fprintf(stderr
, "file open already, try 'help close'\n");
1605 if (bdrv_file_open(&bs
, name
, flags
)) {
1606 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1610 bs
= bdrv_new("hda");
1612 if (bdrv_open(bs
, name
, flags
, NULL
) < 0) {
1613 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1623 static void open_help(void)
1627 " opens a new file in the requested mode\n"
1630 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1632 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1633 " -r, -- open file read-only\n"
1634 " -s, -- use snapshot file\n"
1635 " -n, -- disable host cache\n"
1636 " -g, -- allow file to grow (only applies to protocols)"
1640 static int open_f(int argc
, char **argv
);
1642 static const cmdinfo_t open_cmd
= {
1648 .flags
= CMD_NOFILE_OK
,
1649 .args
= "[-Crsn] [path]",
1650 .oneline
= "open the file specified by path",
1654 static int open_f(int argc
, char **argv
)
1661 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
1664 flags
|= BDRV_O_SNAPSHOT
;
1667 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1676 return command_usage(&open_cmd
);
1681 flags
|= BDRV_O_RDWR
;
1684 if (optind
!= argc
- 1) {
1685 return command_usage(&open_cmd
);
1688 return openfile(argv
[optind
], flags
, growable
);
1691 static int init_args_command(int index
)
1693 /* only one device allowed so far */
1700 static int init_check_command(const cmdinfo_t
*ct
)
1702 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
1705 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1706 fprintf(stderr
, "no file open, try 'help open'\n");
1712 static void usage(const char *name
)
1715 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1716 "QEMU Disk exerciser\n"
1718 " -c, --cmd command to execute\n"
1719 " -r, --read-only export read-only\n"
1720 " -s, --snapshot use snapshot file\n"
1721 " -n, --nocache disable host cache\n"
1722 " -g, --growable allow file to grow (only applies to protocols)\n"
1723 " -m, --misalign misalign allocations for O_DIRECT\n"
1724 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1725 " -h, --help display this help and exit\n"
1726 " -V, --version output version information and exit\n"
1732 int main(int argc
, char **argv
)
1736 const char *sopt
= "hVc:rsnmgk";
1737 const struct option lopt
[] = {
1738 { "help", 0, NULL
, 'h' },
1739 { "version", 0, NULL
, 'V' },
1740 { "offset", 1, NULL
, 'o' },
1741 { "cmd", 1, NULL
, 'c' },
1742 { "read-only", 0, NULL
, 'r' },
1743 { "snapshot", 0, NULL
, 's' },
1744 { "nocache", 0, NULL
, 'n' },
1745 { "misalign", 0, NULL
, 'm' },
1746 { "growable", 0, NULL
, 'g' },
1747 { "native-aio", 0, NULL
, 'k' },
1748 { NULL
, 0, NULL
, 0 }
1754 progname
= basename(argv
[0]);
1756 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1759 flags
|= BDRV_O_SNAPSHOT
;
1762 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
1765 add_user_command(optarg
);
1777 flags
|= BDRV_O_NATIVE_AIO
;
1780 printf("%s version %s\n", progname
, VERSION
);
1791 if ((argc
- optind
) > 1) {
1798 /* initialize commands */
1801 add_command(&open_cmd
);
1802 add_command(&close_cmd
);
1803 add_command(&read_cmd
);
1804 add_command(&readv_cmd
);
1805 add_command(&write_cmd
);
1806 add_command(&writev_cmd
);
1807 add_command(&multiwrite_cmd
);
1808 add_command(&aio_read_cmd
);
1809 add_command(&aio_write_cmd
);
1810 add_command(&aio_flush_cmd
);
1811 add_command(&flush_cmd
);
1812 add_command(&truncate_cmd
);
1813 add_command(&length_cmd
);
1814 add_command(&info_cmd
);
1815 add_command(&discard_cmd
);
1816 add_command(&alloc_cmd
);
1817 add_command(&map_cmd
);
1819 add_args_command(init_args_command
);
1820 add_check_command(init_check_command
);
1822 /* open the device */
1824 flags
|= BDRV_O_RDWR
;
1827 if ((argc
- optind
) == 1) {
1828 openfile(argv
[optind
], flags
, growable
);
1833 * Make sure all outstanding requests complete before the program exits.