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
;
64 buf
= qemu_blockalign(bs
, len
);
65 memset(buf
, pattern
, len
);
67 buf
+= MISALIGN_OFFSET
;
71 static void qemu_io_free(void *p
)
79 dump_buffer(const void *buffer
, int64_t offset
, int len
)
84 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
87 printf("%08" PRIx64
": ", offset
+ i
);
88 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
91 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
102 print_report(const char *op
, struct timeval
*t
, int64_t offset
,
103 int count
, int total
, int cnt
, int Cflag
)
105 char s1
[64], s2
[64], ts
[64];
107 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
109 cvtstr((double)total
, s1
, sizeof(s1
));
110 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
111 printf("%s %d/%d bytes at offset %" PRId64
"\n",
112 op
, total
, count
, offset
);
113 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
114 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
115 } else {/* bytes,ops,time,bytes/sec,ops/sec */
116 printf("%d,%d,%s,%.3f,%.3f\n",
118 tdiv((double)total
, *t
),
119 tdiv((double)cnt
, *t
));
124 * Parse multiple length statements for vectored I/O, and construct an I/O
125 * vector matching it.
128 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
130 size_t *sizes
= calloc(nr_iov
, sizeof(size_t));
136 for (i
= 0; i
< nr_iov
; i
++) {
142 printf("non-numeric length argument -- %s\n", arg
);
146 /* should be SIZE_T_MAX, but that doesn't exist */
148 printf("too large length argument -- %s\n", arg
);
153 printf("length argument %" PRId64
154 " is not sector aligned\n", len
);
162 qemu_iovec_init(qiov
, nr_iov
);
164 buf
= p
= qemu_io_alloc(count
, pattern
);
166 for (i
= 0; i
< nr_iov
; i
++) {
167 qemu_iovec_add(qiov
, p
, sizes
[i
]);
176 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
180 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
187 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
191 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
198 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
200 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
206 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
208 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
214 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
216 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
222 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
224 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
230 #define NOT_DONE 0x7fffffff
231 static void aio_rw_done(void *opaque
, int ret
)
233 *(int *)opaque
= ret
;
236 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
238 BlockDriverAIOCB
*acb
;
239 int async_ret
= NOT_DONE
;
241 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
242 aio_rw_done
, &async_ret
);
246 while (async_ret
== NOT_DONE
)
250 return async_ret
< 0 ? async_ret
: 1;
253 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
255 BlockDriverAIOCB
*acb
;
256 int async_ret
= NOT_DONE
;
258 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
259 aio_rw_done
, &async_ret
);
263 while (async_ret
== NOT_DONE
)
267 return async_ret
< 0 ? async_ret
: 1;
270 struct multiwrite_async_ret
{
275 static void multiwrite_cb(void *opaque
, int ret
)
277 struct multiwrite_async_ret
*async_ret
= opaque
;
279 async_ret
->num_done
++;
281 async_ret
->error
= ret
;
285 static int do_aio_multiwrite(BlockRequest
* reqs
, int num_reqs
, int *total
)
288 struct multiwrite_async_ret async_ret
= {
294 for (i
= 0; i
< num_reqs
; i
++) {
295 reqs
[i
].cb
= multiwrite_cb
;
296 reqs
[i
].opaque
= &async_ret
;
297 *total
+= reqs
[i
].qiov
->size
;
300 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
305 while (async_ret
.num_done
< num_reqs
) {
309 return async_ret
.error
< 0 ? async_ret
.error
: 1;
317 " reads a range of bytes from the given offset\n"
320 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
322 " Reads a segment of the currently open file, optionally dumping it to the\n"
323 " standard output stream (with -v option) for subsequent inspection.\n"
324 " -b, -- read from the VM state rather than the virtual disk\n"
325 " -C, -- report statistics in a machine parsable format\n"
326 " -l, -- length for pattern verification (only with -P)\n"
327 " -p, -- use bdrv_pread to read the file\n"
328 " -P, -- use a pattern to verify read data\n"
329 " -q, -- quite mode, do not show I/O statistics\n"
330 " -s, -- start offset for pattern verification (only with -P)\n"
331 " -v, -- dump buffer to standard output\n"
335 static int read_f(int argc
, char **argv
);
337 static const cmdinfo_t read_cmd
= {
343 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
344 .oneline
= "reads a number of bytes at a specified offset",
349 read_f(int argc
, char **argv
)
351 struct timeval t1
, t2
;
352 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
353 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
358 /* Some compilers get confused and warn if this is not initialized. */
360 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
362 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
372 pattern_count
= cvtnum(optarg
);
373 if (pattern_count
< 0) {
374 printf("non-numeric length argument -- %s\n", optarg
);
383 pattern
= parse_pattern(optarg
);
392 pattern_offset
= cvtnum(optarg
);
393 if (pattern_offset
< 0) {
394 printf("non-numeric length argument -- %s\n", optarg
);
402 return command_usage(&read_cmd
);
406 if (optind
!= argc
- 2)
407 return command_usage(&read_cmd
);
409 if (bflag
&& pflag
) {
410 printf("-b and -p cannot be specified at the same time\n");
414 offset
= cvtnum(argv
[optind
]);
416 printf("non-numeric length argument -- %s\n", argv
[optind
]);
421 count
= cvtnum(argv
[optind
]);
423 printf("non-numeric length argument -- %s\n", argv
[optind
]);
427 if (!Pflag
&& (lflag
|| sflag
)) {
428 return command_usage(&read_cmd
);
432 pattern_count
= count
- pattern_offset
;
435 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
436 printf("pattern verfication range exceeds end of read data\n");
441 if (offset
& 0x1ff) {
442 printf("offset %" PRId64
" is not sector aligned\n",
447 printf("count %d is not sector aligned\n",
453 buf
= qemu_io_alloc(count
, 0xab);
455 gettimeofday(&t1
, NULL
);
457 cnt
= do_pread(buf
, offset
, count
, &total
);
459 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
461 cnt
= do_read(buf
, offset
, count
, &total
);
462 gettimeofday(&t2
, NULL
);
465 printf("read failed: %s\n", strerror(-cnt
));
470 void* cmp_buf
= malloc(pattern_count
);
471 memset(cmp_buf
, pattern
, pattern_count
);
472 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
473 printf("Pattern verification failed at offset %"
474 PRId64
", %d bytes\n",
475 offset
+ pattern_offset
, pattern_count
);
484 dump_buffer(buf
, offset
, count
);
486 /* Finally, report back -- -C gives a parsable format */
488 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
501 " reads a range of bytes from the given offset into multiple buffers\n"
504 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
506 " Reads a segment of the currently open file, optionally dumping it to the\n"
507 " standard output stream (with -v option) for subsequent inspection.\n"
508 " Uses multiple iovec buffers if more than one byte range is specified.\n"
509 " -C, -- report statistics in a machine parsable format\n"
510 " -P, -- use a pattern to verify read data\n"
511 " -v, -- dump buffer to standard output\n"
512 " -q, -- quite mode, do not show I/O statistics\n"
516 static int readv_f(int argc
, char **argv
);
518 static const cmdinfo_t readv_cmd
= {
523 .args
= "[-Cqv] [-P pattern ] off len [len..]",
524 .oneline
= "reads a number of bytes at a specified offset",
529 readv_f(int argc
, char **argv
)
531 struct timeval t1
, t2
;
532 int Cflag
= 0, qflag
= 0, vflag
= 0;
536 /* Some compilers get confused and warn if this is not initialized. */
543 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
550 pattern
= parse_pattern(optarg
);
561 return command_usage(&readv_cmd
);
565 if (optind
> argc
- 2)
566 return command_usage(&readv_cmd
);
569 offset
= cvtnum(argv
[optind
]);
571 printf("non-numeric length argument -- %s\n", argv
[optind
]);
576 if (offset
& 0x1ff) {
577 printf("offset %" PRId64
" is not sector aligned\n",
582 nr_iov
= argc
- optind
;
583 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
585 gettimeofday(&t1
, NULL
);
586 cnt
= do_aio_readv(&qiov
, offset
, &total
);
587 gettimeofday(&t2
, NULL
);
590 printf("readv failed: %s\n", strerror(-cnt
));
595 void* cmp_buf
= malloc(qiov
.size
);
596 memset(cmp_buf
, pattern
, qiov
.size
);
597 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
598 printf("Pattern verification failed at offset %"
599 PRId64
", %zd bytes\n",
609 dump_buffer(buf
, offset
, qiov
.size
);
611 /* Finally, report back -- -C gives a parsable format */
613 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
625 " writes a range of bytes from the given offset\n"
628 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
630 " Writes into a segment of the currently open file, using a buffer\n"
631 " filled with a set pattern (0xcdcdcdcd).\n"
632 " -b, -- write to the VM state rather than the virtual disk\n"
633 " -p, -- use bdrv_pwrite to write the file\n"
634 " -P, -- use different pattern to fill file\n"
635 " -C, -- report statistics in a machine parsable format\n"
636 " -q, -- quite mode, do not show I/O statistics\n"
640 static int write_f(int argc
, char **argv
);
642 static const cmdinfo_t write_cmd
= {
648 .args
= "[-abCpq] [-P pattern ] off len",
649 .oneline
= "writes a number of bytes at a specified offset",
654 write_f(int argc
, char **argv
)
656 struct timeval t1
, t2
;
657 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0;
662 /* Some compilers get confused and warn if this is not initialized. */
666 while ((c
= getopt(argc
, argv
, "bCpP:q")) != EOF
) {
678 pattern
= parse_pattern(optarg
);
686 return command_usage(&write_cmd
);
690 if (optind
!= argc
- 2)
691 return command_usage(&write_cmd
);
693 if (bflag
&& pflag
) {
694 printf("-b and -p cannot be specified at the same time\n");
698 offset
= cvtnum(argv
[optind
]);
700 printf("non-numeric length argument -- %s\n", argv
[optind
]);
705 count
= cvtnum(argv
[optind
]);
707 printf("non-numeric length argument -- %s\n", argv
[optind
]);
712 if (offset
& 0x1ff) {
713 printf("offset %" PRId64
" is not sector aligned\n",
719 printf("count %d is not sector aligned\n",
725 buf
= qemu_io_alloc(count
, pattern
);
727 gettimeofday(&t1
, NULL
);
729 cnt
= do_pwrite(buf
, offset
, count
, &total
);
731 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
733 cnt
= do_write(buf
, offset
, count
, &total
);
734 gettimeofday(&t2
, NULL
);
737 printf("write failed: %s\n", strerror(-cnt
));
744 /* Finally, report back -- -C gives a parsable format */
746 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
759 " writes a range of bytes from the given offset source from multiple buffers\n"
762 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
764 " Writes into a segment of the currently open file, using a buffer\n"
765 " filled with a set pattern (0xcdcdcdcd).\n"
766 " -P, -- use different pattern to fill file\n"
767 " -C, -- report statistics in a machine parsable format\n"
768 " -q, -- quite mode, do not show I/O statistics\n"
772 static int writev_f(int argc
, char **argv
);
774 static const cmdinfo_t writev_cmd
= {
779 .args
= "[-Cq] [-P pattern ] off len [len..]",
780 .oneline
= "writes a number of bytes at a specified offset",
785 writev_f(int argc
, char **argv
)
787 struct timeval t1
, t2
;
788 int Cflag
= 0, qflag
= 0;
792 /* Some compilers get confused and warn if this is not initialized. */
798 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
807 pattern
= parse_pattern(optarg
);
812 return command_usage(&writev_cmd
);
816 if (optind
> argc
- 2)
817 return command_usage(&writev_cmd
);
819 offset
= cvtnum(argv
[optind
]);
821 printf("non-numeric length argument -- %s\n", argv
[optind
]);
826 if (offset
& 0x1ff) {
827 printf("offset %" PRId64
" is not sector aligned\n",
832 nr_iov
= argc
- optind
;
833 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
835 gettimeofday(&t1
, NULL
);
836 cnt
= do_aio_writev(&qiov
, offset
, &total
);
837 gettimeofday(&t2
, NULL
);
840 printf("writev failed: %s\n", strerror(-cnt
));
847 /* Finally, report back -- -C gives a parsable format */
849 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
856 multiwrite_help(void)
860 " writes a range of bytes from the given offset source from multiple buffers,\n"
861 " in a batch of requests that may be merged by qemu\n"
864 " 'multiwrite 512 1k 1k ; 4k 1k' \n"
865 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
867 " Writes into a segment of the currently open file, using a buffer\n"
868 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
869 " by one for each request contained in the multiwrite command.\n"
870 " -P, -- use different pattern to fill file\n"
871 " -C, -- report statistics in a machine parsable format\n"
872 " -q, -- quiet mode, do not show I/O statistics\n"
876 static int multiwrite_f(int argc
, char **argv
);
878 static const cmdinfo_t multiwrite_cmd
= {
879 .name
= "multiwrite",
880 .cfunc
= multiwrite_f
,
883 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
884 .oneline
= "issues multiple write requests at once",
885 .help
= multiwrite_help
,
889 multiwrite_f(int argc
, char **argv
)
891 struct timeval t1
, t2
;
892 int Cflag
= 0, qflag
= 0;
895 int64_t offset
, first_offset
= 0;
896 /* Some compilers get confused and warn if this is not initialized. */
905 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
914 pattern
= parse_pattern(optarg
);
919 return command_usage(&writev_cmd
);
923 if (optind
> argc
- 2)
924 return command_usage(&writev_cmd
);
927 for (i
= optind
; i
< argc
; i
++) {
928 if (!strcmp(argv
[i
], ";")) {
933 reqs
= qemu_malloc(nr_reqs
* sizeof(*reqs
));
934 buf
= qemu_malloc(nr_reqs
* sizeof(*buf
));
935 qiovs
= qemu_malloc(nr_reqs
* sizeof(*qiovs
));
937 for (i
= 0; i
< nr_reqs
; i
++) {
940 /* Read the offset of the request */
941 offset
= cvtnum(argv
[optind
]);
943 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
948 if (offset
& 0x1ff) {
949 printf("offset %lld is not sector aligned\n",
955 first_offset
= offset
;
958 /* Read lengths for qiov entries */
959 for (j
= optind
; j
< argc
; j
++) {
960 if (!strcmp(argv
[j
], ";")) {
968 reqs
[i
].qiov
= &qiovs
[i
];
969 buf
[i
] = create_iovec(reqs
[i
].qiov
, &argv
[optind
], nr_iov
, pattern
);
970 reqs
[i
].sector
= offset
>> 9;
971 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
975 offset
+= reqs
[i
].qiov
->size
;
979 gettimeofday(&t1
, NULL
);
980 cnt
= do_aio_multiwrite(reqs
, nr_reqs
, &total
);
981 gettimeofday(&t2
, NULL
);
984 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
991 /* Finally, report back -- -C gives a parsable format */
993 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
995 for (i
= 0; i
< nr_reqs
; i
++) {
996 qemu_io_free(buf
[i
]);
997 qemu_iovec_destroy(&qiovs
[i
]);
1018 aio_write_done(void *opaque
, int ret
)
1020 struct aio_ctx
*ctx
= opaque
;
1023 gettimeofday(&t2
, NULL
);
1027 printf("aio_write failed: %s\n", strerror(-ret
));
1035 /* Finally, report back -- -C gives a parsable format */
1036 t2
= tsub(t2
, ctx
->t1
);
1037 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1038 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1040 qemu_io_free(ctx
->buf
);
1045 aio_read_done(void *opaque
, int ret
)
1047 struct aio_ctx
*ctx
= opaque
;
1050 gettimeofday(&t2
, NULL
);
1053 printf("readv failed: %s\n", strerror(-ret
));
1058 void *cmp_buf
= malloc(ctx
->qiov
.size
);
1060 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1061 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1062 printf("Pattern verification failed at offset %"
1063 PRId64
", %zd bytes\n",
1064 ctx
->offset
, ctx
->qiov
.size
);
1074 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1077 /* Finally, report back -- -C gives a parsable format */
1078 t2
= tsub(t2
, ctx
->t1
);
1079 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1080 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1082 qemu_io_free(ctx
->buf
);
1091 " asynchronously reads a range of bytes from the given offset\n"
1094 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1096 " Reads a segment of the currently open file, optionally dumping it to the\n"
1097 " standard output stream (with -v option) for subsequent inspection.\n"
1098 " The read is performed asynchronously and the aio_flush command must be\n"
1099 " used to ensure all outstanding aio requests have been completed\n"
1100 " -C, -- report statistics in a machine parsable format\n"
1101 " -P, -- use a pattern to verify read data\n"
1102 " -v, -- dump buffer to standard output\n"
1103 " -q, -- quite mode, do not show I/O statistics\n"
1107 static int aio_read_f(int argc
, char **argv
);
1109 static const cmdinfo_t aio_read_cmd
= {
1111 .cfunc
= aio_read_f
,
1114 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1115 .oneline
= "asynchronously reads a number of bytes",
1116 .help
= aio_read_help
,
1120 aio_read_f(int argc
, char **argv
)
1123 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
1124 BlockDriverAIOCB
*acb
;
1126 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1133 ctx
->pattern
= parse_pattern(optarg
);
1134 if (ctx
->pattern
< 0)
1145 return command_usage(&aio_read_cmd
);
1149 if (optind
> argc
- 2) {
1151 return command_usage(&aio_read_cmd
);
1154 ctx
->offset
= cvtnum(argv
[optind
]);
1155 if (ctx
->offset
< 0) {
1156 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1162 if (ctx
->offset
& 0x1ff) {
1163 printf("offset %" PRId64
" is not sector aligned\n",
1169 nr_iov
= argc
- optind
;
1170 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1172 gettimeofday(&ctx
->t1
, NULL
);
1173 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1174 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1185 aio_write_help(void)
1189 " asynchronously writes a range of bytes from the given offset source \n"
1190 " from multiple buffers\n"
1193 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1195 " Writes into a segment of the currently open file, using a buffer\n"
1196 " filled with a set pattern (0xcdcdcdcd).\n"
1197 " The write is performed asynchronously and the aio_flush command must be\n"
1198 " used to ensure all outstanding aio requests have been completed\n"
1199 " -P, -- use different pattern to fill file\n"
1200 " -C, -- report statistics in a machine parsable format\n"
1201 " -q, -- quite mode, do not show I/O statistics\n"
1205 static int aio_write_f(int argc
, char **argv
);
1207 static const cmdinfo_t aio_write_cmd
= {
1208 .name
= "aio_write",
1209 .cfunc
= aio_write_f
,
1212 .args
= "[-Cq] [-P pattern ] off len [len..]",
1213 .oneline
= "asynchronously writes a number of bytes",
1214 .help
= aio_write_help
,
1218 aio_write_f(int argc
, char **argv
)
1222 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
1223 BlockDriverAIOCB
*acb
;
1225 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1234 pattern
= parse_pattern(optarg
);
1240 return command_usage(&aio_write_cmd
);
1244 if (optind
> argc
- 2) {
1246 return command_usage(&aio_write_cmd
);
1249 ctx
->offset
= cvtnum(argv
[optind
]);
1250 if (ctx
->offset
< 0) {
1251 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1257 if (ctx
->offset
& 0x1ff) {
1258 printf("offset %" PRId64
" is not sector aligned\n",
1264 nr_iov
= argc
- optind
;
1265 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1267 gettimeofday(&ctx
->t1
, NULL
);
1268 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1269 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1280 aio_flush_f(int argc
, char **argv
)
1286 static const cmdinfo_t aio_flush_cmd
= {
1287 .name
= "aio_flush",
1288 .cfunc
= aio_flush_f
,
1289 .oneline
= "completes all outstanding aio requests"
1293 flush_f(int argc
, char **argv
)
1299 static const cmdinfo_t flush_cmd
= {
1303 .oneline
= "flush all in-core file state to disk",
1307 truncate_f(int argc
, char **argv
)
1312 offset
= cvtnum(argv
[1]);
1314 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1318 ret
= bdrv_truncate(bs
, offset
);
1320 printf("truncate: %s\n", strerror(-ret
));
1327 static const cmdinfo_t truncate_cmd
= {
1330 .cfunc
= truncate_f
,
1334 .oneline
= "truncates the current file at the given offset",
1338 length_f(int argc
, char **argv
)
1343 size
= bdrv_getlength(bs
);
1345 printf("getlength: %s\n", strerror(-size
));
1349 cvtstr(size
, s1
, sizeof(s1
));
1355 static const cmdinfo_t length_cmd
= {
1359 .oneline
= "gets the length of the current file",
1364 info_f(int argc
, char **argv
)
1366 BlockDriverInfo bdi
;
1367 char s1
[64], s2
[64];
1370 if (bs
->drv
&& bs
->drv
->format_name
)
1371 printf("format name: %s\n", bs
->drv
->format_name
);
1372 if (bs
->drv
&& bs
->drv
->protocol_name
)
1373 printf("format name: %s\n", bs
->drv
->protocol_name
);
1375 ret
= bdrv_get_info(bs
, &bdi
);
1379 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1380 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1382 printf("cluster size: %s\n", s1
);
1383 printf("vm state offset: %s\n", s2
);
1390 static const cmdinfo_t info_cmd
= {
1394 .oneline
= "prints information about the current file",
1402 " discards a range of bytes from the given offset\n"
1405 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1407 " Discards a segment of the currently open file.\n"
1408 " -C, -- report statistics in a machine parsable format\n"
1409 " -q, -- quite mode, do not show I/O statistics\n"
1413 static int discard_f(int argc
, char **argv
);
1415 static const cmdinfo_t discard_cmd
= {
1421 .args
= "[-Cq] off len",
1422 .oneline
= "discards a number of bytes at a specified offset",
1423 .help
= discard_help
,
1427 discard_f(int argc
, char **argv
)
1429 struct timeval t1
, t2
;
1430 int Cflag
= 0, qflag
= 0;
1435 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1444 return command_usage(&discard_cmd
);
1448 if (optind
!= argc
- 2) {
1449 return command_usage(&discard_cmd
);
1452 offset
= cvtnum(argv
[optind
]);
1454 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1459 count
= cvtnum(argv
[optind
]);
1461 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1465 gettimeofday(&t1
, NULL
);
1466 ret
= bdrv_discard(bs
, offset
, count
);
1467 gettimeofday(&t2
, NULL
);
1470 printf("discard failed: %s\n", strerror(-ret
));
1474 /* Finally, report back -- -C gives a parsable format */
1477 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1485 alloc_f(int argc
, char **argv
)
1488 int nb_sectors
, remaining
;
1493 offset
= cvtnum(argv
[1]);
1494 if (offset
& 0x1ff) {
1495 printf("offset %" PRId64
" is not sector aligned\n",
1501 nb_sectors
= cvtnum(argv
[2]);
1505 remaining
= nb_sectors
;
1508 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1515 cvtstr(offset
, s1
, sizeof(s1
));
1517 printf("%d/%d sectors allocated at offset %s\n",
1518 sum_alloc
, nb_sectors
, s1
);
1522 static const cmdinfo_t alloc_cmd
= {
1528 .args
= "off [sectors]",
1529 .oneline
= "checks if a sector is present in the file",
1533 map_f(int argc
, char **argv
)
1538 int num
, num_checked
;
1543 nb_sectors
= bs
->total_sectors
;
1546 num_checked
= MIN(nb_sectors
, INT_MAX
);
1547 ret
= bdrv_is_allocated(bs
, offset
, num_checked
, &num
);
1548 retstr
= ret
? " allocated" : "not allocated";
1549 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1550 printf("[% 24" PRId64
"] % 8d/% 8d sectors %s at offset %s (%d)\n",
1551 offset
<< 9ULL, num
, num_checked
, retstr
, s1
, ret
);
1555 } while(offset
< bs
->total_sectors
);
1560 static const cmdinfo_t map_cmd
= {
1566 .oneline
= "prints the allocated areas of a file",
1571 close_f(int argc
, char **argv
)
1578 static const cmdinfo_t close_cmd
= {
1582 .oneline
= "close the current open file",
1585 static int openfile(char *name
, int flags
, int growable
)
1588 fprintf(stderr
, "file open already, try 'help close'\n");
1593 if (bdrv_file_open(&bs
, name
, flags
)) {
1594 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1598 bs
= bdrv_new("hda");
1600 if (bdrv_open(bs
, name
, flags
, NULL
) < 0) {
1601 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1615 " opens a new file in the requested mode\n"
1618 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1620 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1621 " -r, -- open file read-only\n"
1622 " -s, -- use snapshot file\n"
1623 " -n, -- disable host cache\n"
1624 " -g, -- allow file to grow (only applies to protocols)"
1628 static int open_f(int argc
, char **argv
);
1630 static const cmdinfo_t open_cmd
= {
1636 .flags
= CMD_NOFILE_OK
,
1637 .args
= "[-Crsn] [path]",
1638 .oneline
= "open the file specified by path",
1643 open_f(int argc
, char **argv
)
1650 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
1653 flags
|= BDRV_O_SNAPSHOT
;
1656 flags
|= BDRV_O_NOCACHE
;
1665 return command_usage(&open_cmd
);
1670 flags
|= BDRV_O_RDWR
;
1673 if (optind
!= argc
- 1)
1674 return command_usage(&open_cmd
);
1676 return openfile(argv
[optind
], flags
, growable
);
1683 /* only one device allowed so far */
1691 const cmdinfo_t
*ct
)
1693 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1695 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1696 fprintf(stderr
, "no file open, try 'help open'\n");
1702 static void usage(const char *name
)
1705 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1706 "QEMU Disk exerciser\n"
1708 " -c, --cmd command to execute\n"
1709 " -r, --read-only export read-only\n"
1710 " -s, --snapshot use snapshot file\n"
1711 " -n, --nocache disable host cache\n"
1712 " -g, --growable allow file to grow (only applies to protocols)\n"
1713 " -m, --misalign misalign allocations for O_DIRECT\n"
1714 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1715 " -h, --help display this help and exit\n"
1716 " -V, --version output version information and exit\n"
1722 int main(int argc
, char **argv
)
1726 const char *sopt
= "hVc:rsnmgk";
1727 const struct option lopt
[] = {
1728 { "help", 0, NULL
, 'h' },
1729 { "version", 0, NULL
, 'V' },
1730 { "offset", 1, NULL
, 'o' },
1731 { "cmd", 1, NULL
, 'c' },
1732 { "read-only", 0, NULL
, 'r' },
1733 { "snapshot", 0, NULL
, 's' },
1734 { "nocache", 0, NULL
, 'n' },
1735 { "misalign", 0, NULL
, 'm' },
1736 { "growable", 0, NULL
, 'g' },
1737 { "native-aio", 0, NULL
, 'k' },
1738 { NULL
, 0, NULL
, 0 }
1744 progname
= basename(argv
[0]);
1746 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1749 flags
|= BDRV_O_SNAPSHOT
;
1752 flags
|= BDRV_O_NOCACHE
;
1755 add_user_command(optarg
);
1767 flags
|= BDRV_O_NATIVE_AIO
;
1770 printf("%s version %s\n", progname
, VERSION
);
1781 if ((argc
- optind
) > 1) {
1788 /* initialize commands */
1791 add_command(&open_cmd
);
1792 add_command(&close_cmd
);
1793 add_command(&read_cmd
);
1794 add_command(&readv_cmd
);
1795 add_command(&write_cmd
);
1796 add_command(&writev_cmd
);
1797 add_command(&multiwrite_cmd
);
1798 add_command(&aio_read_cmd
);
1799 add_command(&aio_write_cmd
);
1800 add_command(&aio_flush_cmd
);
1801 add_command(&flush_cmd
);
1802 add_command(&truncate_cmd
);
1803 add_command(&length_cmd
);
1804 add_command(&info_cmd
);
1805 add_command(&discard_cmd
);
1806 add_command(&alloc_cmd
);
1807 add_command(&map_cmd
);
1809 add_args_command(init_args_command
);
1810 add_check_command(init_check_command
);
1812 /* open the device */
1814 flags
|= BDRV_O_RDWR
;
1817 if ((argc
- optind
) == 1)
1818 openfile(argv
[optind
], flags
, growable
);
1822 * Make sure all outstanding requests get flushed the program exits.