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.
10 #include <sys/types.h>
15 #include "qemu-common.h"
16 #include "block_int.h"
19 #define VERSION "0.0.1"
21 #define CMD_NOFILE_OK 0x01
24 static BlockDriverState
*bs
;
29 * Memory allocation helpers.
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len
, int pattern
)
41 len
+= MISALIGN_OFFSET
;
42 buf
= qemu_memalign(512, len
);
43 memset(buf
, pattern
, len
);
45 buf
+= MISALIGN_OFFSET
;
49 static void qemu_io_free(void *p
)
57 dump_buffer(char *buffer
, int64_t offset
, int len
)
62 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
65 printf("%08llx: ", (unsigned long long)offset
+ i
);
66 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
69 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
80 print_report(const char *op
, struct timeval
*t
, int64_t offset
,
81 int count
, int total
, int cnt
, int Cflag
)
83 char s1
[64], s2
[64], ts
[64];
85 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
87 cvtstr((double)total
, s1
, sizeof(s1
));
88 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
89 printf("%s %d/%d bytes at offset %lld\n",
90 op
, total
, count
, (long long)offset
);
91 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
92 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
93 } else {/* bytes,ops,time,bytes/sec,ops/sec */
94 printf("%d,%d,%s,%.3f,%.3f\n",
96 tdiv((double)total
, *t
),
97 tdiv((double)cnt
, *t
));
101 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
105 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
112 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
116 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
123 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
125 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
131 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
133 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
139 #define NOT_DONE 0x7fffffff
140 static void aio_rw_done(void *opaque
, int ret
)
142 *(int *)opaque
= ret
;
145 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
147 BlockDriverAIOCB
*acb
;
148 int async_ret
= NOT_DONE
;
150 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
151 aio_rw_done
, &async_ret
);
155 while (async_ret
== NOT_DONE
)
159 return async_ret
< 0 ? async_ret
: 1;
162 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
164 BlockDriverAIOCB
*acb
;
165 int async_ret
= NOT_DONE
;
167 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
168 aio_rw_done
, &async_ret
);
172 while (async_ret
== NOT_DONE
)
176 return async_ret
< 0 ? async_ret
: 1;
180 static const cmdinfo_t read_cmd
;
187 " reads a range of bytes from the given offset\n"
190 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
192 " Reads a segment of the currently open file, optionally dumping it to the\n"
193 " standard output stream (with -v option) for subsequent inspection.\n"
194 " -C, -- report statistics in a machine parsable format\n"
195 " -l, -- length for pattern verification (only with -P)\n"
196 " -p, -- use bdrv_pread to read the file\n"
197 " -P, -- use a pattern to verify read data\n"
198 " -q, -- quite mode, do not show I/O statistics\n"
199 " -s, -- start offset for pattern verification (only with -P)\n"
200 " -v, -- dump buffer to standard output\n"
205 read_f(int argc
, char **argv
)
207 struct timeval t1
, t2
;
208 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
209 int Pflag
= 0, sflag
= 0, lflag
= 0;
214 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
216 while ((c
= getopt(argc
, argv
, "Cl:pP:qs:v")) != EOF
) {
223 pattern_count
= cvtnum(optarg
);
224 if (pattern_count
< 0) {
225 printf("non-numeric length argument -- %s\n", optarg
);
234 pattern
= atoi(optarg
);
241 pattern_offset
= cvtnum(optarg
);
242 if (pattern_offset
< 0) {
243 printf("non-numeric length argument -- %s\n", optarg
);
251 return command_usage(&read_cmd
);
255 if (optind
!= argc
- 2)
256 return command_usage(&read_cmd
);
258 offset
= cvtnum(argv
[optind
]);
260 printf("non-numeric length argument -- %s\n", argv
[optind
]);
265 count
= cvtnum(argv
[optind
]);
267 printf("non-numeric length argument -- %s\n", argv
[optind
]);
271 if (!Pflag
&& (lflag
|| sflag
)) {
272 return command_usage(&read_cmd
);
276 pattern_count
= count
- pattern_offset
;
279 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
280 printf("pattern verfication range exceeds end of read data\n");
285 if (offset
& 0x1ff) {
286 printf("offset %lld is not sector aligned\n",
291 printf("count %d is not sector aligned\n",
297 buf
= qemu_io_alloc(count
, 0xab);
299 gettimeofday(&t1
, NULL
);
301 cnt
= do_pread(buf
, offset
, count
, &total
);
303 cnt
= do_read(buf
, offset
, count
, &total
);
304 gettimeofday(&t2
, NULL
);
307 printf("read failed: %s\n", strerror(-cnt
));
312 void* cmp_buf
= malloc(pattern_count
);
313 memset(cmp_buf
, pattern
, pattern_count
);
314 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
315 printf("Pattern verification failed at offset %lld, "
317 (long long) offset
+ pattern_offset
, pattern_count
);
326 dump_buffer(buf
, offset
, count
);
328 /* Finally, report back -- -C gives a parsable format */
330 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
337 static const cmdinfo_t read_cmd
= {
343 .args
= "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
344 .oneline
= "reads a number of bytes at a specified offset",
348 static const cmdinfo_t readv_cmd
;
355 " reads a range of bytes from the given offset into multiple buffers\n"
358 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
360 " Reads a segment of the currently open file, optionally dumping it to the\n"
361 " standard output stream (with -v option) for subsequent inspection.\n"
362 " Uses multiple iovec buffers if more than one byte range is specified.\n"
363 " -C, -- report statistics in a machine parsable format\n"
364 " -P, -- use a pattern to verify read data\n"
365 " -v, -- dump buffer to standard output\n"
366 " -q, -- quite mode, do not show I/O statistics\n"
371 readv_f(int argc
, char **argv
)
373 struct timeval t1
, t2
;
374 int Cflag
= 0, qflag
= 0, vflag
= 0;
378 int count
= 0, total
;
384 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
391 pattern
= atoi(optarg
);
400 return command_usage(&readv_cmd
);
404 if (optind
> argc
- 2)
405 return command_usage(&readv_cmd
);
408 offset
= cvtnum(argv
[optind
]);
410 printf("non-numeric length argument -- %s\n", argv
[optind
]);
415 if (offset
& 0x1ff) {
416 printf("offset %lld is not sector aligned\n",
422 printf("count %d is not sector aligned\n",
427 for (i
= optind
; i
< argc
; i
++) {
430 len
= cvtnum(argv
[i
]);
432 printf("non-numeric length argument -- %s\n", argv
[i
]);
438 nr_iov
= argc
- optind
;
439 qemu_iovec_init(&qiov
, nr_iov
);
440 buf
= p
= qemu_io_alloc(count
, 0xab);
441 for (i
= 0; i
< nr_iov
; i
++) {
444 len
= cvtnum(argv
[optind
]);
446 printf("non-numeric length argument -- %s\n",
451 qemu_iovec_add(&qiov
, p
, len
);
456 gettimeofday(&t1
, NULL
);
457 cnt
= do_aio_readv(&qiov
, offset
, &total
);
458 gettimeofday(&t2
, NULL
);
461 printf("readv failed: %s\n", strerror(-cnt
));
466 void* cmp_buf
= malloc(count
);
467 memset(cmp_buf
, pattern
, count
);
468 if (memcmp(buf
, cmp_buf
, count
)) {
469 printf("Pattern verification failed at offset %lld, "
471 (long long) offset
, count
);
480 dump_buffer(buf
, offset
, qiov
.size
);
482 /* Finally, report back -- -C gives a parsable format */
484 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
491 static const cmdinfo_t readv_cmd
= {
496 .args
= "[-Cqv] [-P pattern ] off len [len..]",
497 .oneline
= "reads a number of bytes at a specified offset",
501 static const cmdinfo_t write_cmd
;
508 " writes a range of bytes from the given offset\n"
511 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
513 " Writes into a segment of the currently open file, using a buffer\n"
514 " filled with a set pattern (0xcdcdcdcd).\n"
515 " -p, -- use bdrv_pwrite to write the file\n"
516 " -P, -- use different pattern to fill file\n"
517 " -C, -- report statistics in a machine parsable format\n"
518 " -q, -- quite mode, do not show I/O statistics\n"
523 write_f(int argc
, char **argv
)
525 struct timeval t1
, t2
;
526 int Cflag
= 0, pflag
= 0, qflag
= 0;
533 while ((c
= getopt(argc
, argv
, "CpP:q")) != EOF
) {
542 pattern
= atoi(optarg
);
548 return command_usage(&write_cmd
);
552 if (optind
!= argc
- 2)
553 return command_usage(&write_cmd
);
555 offset
= cvtnum(argv
[optind
]);
557 printf("non-numeric length argument -- %s\n", argv
[optind
]);
562 count
= cvtnum(argv
[optind
]);
564 printf("non-numeric length argument -- %s\n", argv
[optind
]);
569 if (offset
& 0x1ff) {
570 printf("offset %lld is not sector aligned\n",
576 printf("count %d is not sector aligned\n",
582 buf
= qemu_io_alloc(count
, pattern
);
584 gettimeofday(&t1
, NULL
);
586 cnt
= do_pwrite(buf
, offset
, count
, &total
);
588 cnt
= do_write(buf
, offset
, count
, &total
);
589 gettimeofday(&t2
, NULL
);
592 printf("write failed: %s\n", strerror(-cnt
));
599 /* Finally, report back -- -C gives a parsable format */
601 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
608 static const cmdinfo_t write_cmd
= {
614 .args
= "[-aCpq] [-P pattern ] off len",
615 .oneline
= "writes a number of bytes at a specified offset",
619 static const cmdinfo_t writev_cmd
;
626 " writes a range of bytes from the given offset source from multiple buffers\n"
629 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
631 " Writes into a segment of the currently open file, using a buffer\n"
632 " filled with a set pattern (0xcdcdcdcd).\n"
633 " -P, -- use different pattern to fill file\n"
634 " -C, -- report statistics in a machine parsable format\n"
635 " -q, -- quite mode, do not show I/O statistics\n"
640 writev_f(int argc
, char **argv
)
642 struct timeval t1
, t2
;
643 int Cflag
= 0, qflag
= 0;
647 int count
= 0, total
;
652 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
661 pattern
= atoi(optarg
);
664 return command_usage(&writev_cmd
);
668 if (optind
> argc
- 2)
669 return command_usage(&writev_cmd
);
671 offset
= cvtnum(argv
[optind
]);
673 printf("non-numeric length argument -- %s\n", argv
[optind
]);
678 if (offset
& 0x1ff) {
679 printf("offset %lld is not sector aligned\n",
685 printf("count %d is not sector aligned\n",
691 for (i
= optind
; i
< argc
; i
++) {
694 len
= cvtnum(argv
[optind
]);
696 printf("non-numeric length argument -- %s\n", argv
[i
]);
702 nr_iov
= argc
- optind
;
703 qemu_iovec_init(&qiov
, nr_iov
);
704 buf
= p
= qemu_io_alloc(count
, pattern
);
705 for (i
= 0; i
< nr_iov
; i
++) {
708 len
= cvtnum(argv
[optind
]);
710 printf("non-numeric length argument -- %s\n",
715 qemu_iovec_add(&qiov
, p
, len
);
720 gettimeofday(&t1
, NULL
);
721 cnt
= do_aio_writev(&qiov
, offset
, &total
);
722 gettimeofday(&t2
, NULL
);
725 printf("writev failed: %s\n", strerror(-cnt
));
732 /* Finally, report back -- -C gives a parsable format */
734 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
741 static const cmdinfo_t writev_cmd
= {
746 .args
= "[-Cq] [-P pattern ] off len [len..]",
747 .oneline
= "writes a number of bytes at a specified offset",
752 flush_f(int argc
, char **argv
)
758 static const cmdinfo_t flush_cmd
= {
762 .oneline
= "flush all in-core file state to disk",
766 truncate_f(int argc
, char **argv
)
771 offset
= cvtnum(argv
[1]);
773 printf("non-numeric truncate argument -- %s\n", argv
[1]);
777 ret
= bdrv_truncate(bs
, offset
);
779 printf("truncate: %s", strerror(ret
));
786 static const cmdinfo_t truncate_cmd
= {
793 .oneline
= "truncates the current file at the given offset",
797 length_f(int argc
, char **argv
)
802 size
= bdrv_getlength(bs
);
804 printf("getlength: %s", strerror(size
));
808 cvtstr(size
, s1
, sizeof(s1
));
814 static const cmdinfo_t length_cmd
= {
818 .oneline
= "gets the length of the current file",
823 info_f(int argc
, char **argv
)
829 if (bs
->drv
&& bs
->drv
->format_name
)
830 printf("format name: %s\n", bs
->drv
->format_name
);
831 if (bs
->drv
&& bs
->drv
->protocol_name
)
832 printf("format name: %s\n", bs
->drv
->protocol_name
);
834 ret
= bdrv_get_info(bs
, &bdi
);
838 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
839 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
841 printf("cluster size: %s\n", s1
);
842 printf("vm state offset: %s\n", s2
);
849 static const cmdinfo_t info_cmd
= {
853 .oneline
= "prints information about the current file",
857 alloc_f(int argc
, char **argv
)
866 offset
= cvtnum(argv
[1]);
867 if (offset
& 0x1ff) {
868 printf("offset %lld is not sector aligned\n",
874 nb_sectors
= cvtnum(argv
[2]);
878 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
880 cvtstr(offset
, s1
, sizeof(s1
));
882 retstr
= ret
? "allocated" : "not allocated";
884 printf("sector %s at offset %s\n", retstr
, s1
);
886 printf("%d/%d sectors %s at offset %s\n",
887 num
, nb_sectors
, retstr
, s1
);
891 static const cmdinfo_t alloc_cmd
= {
897 .args
= "off [sectors]",
898 .oneline
= "checks if a sector is present in the file",
902 close_f(int argc
, char **argv
)
909 static const cmdinfo_t close_cmd
= {
913 .oneline
= "close the current open file",
916 static int openfile(char *name
, int flags
)
919 fprintf(stderr
, "file open already, try 'help close'\n");
923 bs
= bdrv_new("hda");
927 if (bdrv_open(bs
, name
, flags
) == -1) {
928 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
941 " opens a new file in the requested mode\n"
944 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
946 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
947 " -C, -- create new file if it doesn't exist\n"
948 " -r, -- open file read-only\n"
949 " -s, -- use snapshot file\n"
950 " -n, -- disable host cache\n"
954 static const cmdinfo_t open_cmd
;
957 open_f(int argc
, char **argv
)
963 while ((c
= getopt(argc
, argv
, "snCr")) != EOF
) {
966 flags
|= BDRV_O_SNAPSHOT
;
969 flags
|= BDRV_O_NOCACHE
;
972 flags
|= BDRV_O_CREAT
;
978 return command_usage(&open_cmd
);
983 flags
|= BDRV_O_RDONLY
;
985 flags
|= BDRV_O_RDWR
;
987 if (optind
!= argc
- 1)
988 return command_usage(&open_cmd
);
990 return openfile(argv
[optind
], flags
);
993 static const cmdinfo_t open_cmd
= {
999 .flags
= CMD_NOFILE_OK
,
1000 .args
= "[-Crsn] [path]",
1001 .oneline
= "open the file specified by path",
1009 /* only one device allowed so far */
1017 const cmdinfo_t
*ct
)
1019 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1021 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1022 fprintf(stderr
, "no file open, try 'help open'\n");
1028 static void usage(const char *name
)
1031 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1032 "QEMU Disk excerciser\n"
1034 " -C, --create create new file if it doesn't exist\n"
1035 " -c, --cmd command to execute\n"
1036 " -r, --read-only export read-only\n"
1037 " -s, --snapshot use snapshot file\n"
1038 " -n, --nocache disable host cache\n"
1039 " -m, --misalign misalign allocations for O_DIRECT\n"
1040 " -h, --help display this help and exit\n"
1041 " -V, --version output version information and exit\n"
1047 int main(int argc
, char **argv
)
1050 const char *sopt
= "hVc:Crsnm";
1051 struct option lopt
[] = {
1052 { "help", 0, 0, 'h' },
1053 { "version", 0, 0, 'V' },
1054 { "offset", 1, 0, 'o' },
1055 { "cmd", 1, 0, 'c' },
1056 { "create", 0, 0, 'C' },
1057 { "read-only", 0, 0, 'r' },
1058 { "snapshot", 0, 0, 's' },
1059 { "nocache", 0, 0, 'n' },
1060 { "misalign", 0, 0, 'm' },
1067 progname
= basename(argv
[0]);
1069 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1072 flags
|= BDRV_O_SNAPSHOT
;
1075 flags
|= BDRV_O_NOCACHE
;
1078 add_user_command(optarg
);
1081 flags
|= BDRV_O_CREAT
;
1090 printf("%s version %s\n", progname
, VERSION
);
1101 if ((argc
- optind
) > 1) {
1108 /* initialize commands */
1111 add_command(&open_cmd
);
1112 add_command(&close_cmd
);
1113 add_command(&read_cmd
);
1114 add_command(&readv_cmd
);
1115 add_command(&write_cmd
);
1116 add_command(&writev_cmd
);
1117 add_command(&flush_cmd
);
1118 add_command(&truncate_cmd
);
1119 add_command(&length_cmd
);
1120 add_command(&info_cmd
);
1121 add_command(&alloc_cmd
);
1123 add_args_command(init_args_command
);
1124 add_check_command(init_check_command
);
1126 /* open the device */
1128 flags
|= BDRV_O_RDONLY
;
1130 flags
|= BDRV_O_RDWR
;
1132 if ((argc
- optind
) == 1)
1133 openfile(argv
[optind
], flags
);