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
)
175 *total
= qiov
->size
>> 9;
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 " -p, -- use bdrv_pread to read the file\n"
195 " -C, -- report statistics in a machine parsable format\n"
196 " -v, -- dump buffer to standard output\n"
197 " -q, -- quite mode, do not show I/O statistics\n"
202 read_f(int argc
, char **argv
)
204 struct timeval t1
, t2
;
205 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
211 while ((c
= getopt(argc
, argv
, "Cpqv")) != EOF
) {
226 return command_usage(&read_cmd
);
230 if (optind
!= argc
- 2)
231 return command_usage(&read_cmd
);
233 offset
= cvtnum(argv
[optind
]);
235 printf("non-numeric length argument -- %s\n", argv
[optind
]);
240 count
= cvtnum(argv
[optind
]);
242 printf("non-numeric length argument -- %s\n", argv
[optind
]);
247 if (offset
& 0x1ff) {
248 printf("offset %lld is not sector aligned\n",
253 printf("count %d is not sector aligned\n",
259 buf
= qemu_io_alloc(count
, 0xab);
261 gettimeofday(&t1
, NULL
);
263 cnt
= do_pread(buf
, offset
, count
, &total
);
265 cnt
= do_read(buf
, offset
, count
, &total
);
266 gettimeofday(&t2
, NULL
);
269 printf("read failed: %s\n", strerror(-cnt
));
277 dump_buffer(buf
, offset
, count
);
279 /* Finally, report back -- -C gives a parsable format */
281 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
288 static const cmdinfo_t read_cmd
= {
294 .args
= "[-aCpqv] off len",
295 .oneline
= "reads a number of bytes at a specified offset",
299 static const cmdinfo_t readv_cmd
;
306 " reads a range of bytes from the given offset into multiple buffers\n"
309 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
311 " Reads a segment of the currently open file, optionally dumping it to the\n"
312 " standard output stream (with -v option) for subsequent inspection.\n"
313 " Uses multiple iovec buffers if more than one byte range is specified.\n"
314 " -C, -- report statistics in a machine parsable format\n"
315 " -v, -- dump buffer to standard output\n"
316 " -q, -- quite mode, do not show I/O statistics\n"
321 readv_f(int argc
, char **argv
)
323 struct timeval t1
, t2
;
324 int Cflag
= 0, qflag
= 0, vflag
= 0;
328 int count
= 0, total
;
332 while ((c
= getopt(argc
, argv
, "Cqv")) != EOF
) {
344 return command_usage(&readv_cmd
);
348 if (optind
> argc
- 2)
349 return command_usage(&readv_cmd
);
352 offset
= cvtnum(argv
[optind
]);
354 printf("non-numeric length argument -- %s\n", argv
[optind
]);
359 if (offset
& 0x1ff) {
360 printf("offset %lld is not sector aligned\n",
366 printf("count %d is not sector aligned\n",
371 for (i
= optind
; i
< argc
; i
++) {
374 len
= cvtnum(argv
[i
]);
376 printf("non-numeric length argument -- %s\n", argv
[i
]);
382 nr_iov
= argc
- optind
;
383 qemu_iovec_init(&qiov
, nr_iov
);
384 buf
= p
= qemu_io_alloc(count
, 0xab);
385 for (i
= 0; i
< nr_iov
; i
++) {
388 len
= cvtnum(argv
[optind
]);
390 printf("non-numeric length argument -- %s\n",
395 qemu_iovec_add(&qiov
, p
, len
);
400 gettimeofday(&t1
, NULL
);
401 cnt
= do_aio_readv(&qiov
, offset
, &total
);
402 gettimeofday(&t2
, NULL
);
405 printf("readv failed: %s\n", strerror(-cnt
));
413 dump_buffer(buf
, offset
, qiov
.size
);
415 /* Finally, report back -- -C gives a parsable format */
417 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
424 static const cmdinfo_t readv_cmd
= {
429 .args
= "[-Cqv] off len [len..]",
430 .oneline
= "reads a number of bytes at a specified offset",
434 static const cmdinfo_t write_cmd
;
441 " writes a range of bytes from the given offset\n"
444 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
446 " Writes into a segment of the currently open file, using a buffer\n"
447 " filled with a set pattern (0xcdcdcdcd).\n"
448 " -p, -- use bdrv_pwrite to write the file\n"
449 " -P, -- use different pattern to fill file\n"
450 " -C, -- report statistics in a machine parsable format\n"
451 " -q, -- quite mode, do not show I/O statistics\n"
456 write_f(int argc
, char **argv
)
458 struct timeval t1
, t2
;
459 int Cflag
= 0, pflag
= 0, qflag
= 0;
466 while ((c
= getopt(argc
, argv
, "CpP:q")) != EOF
) {
475 pattern
= atoi(optarg
);
481 return command_usage(&write_cmd
);
485 if (optind
!= argc
- 2)
486 return command_usage(&write_cmd
);
488 offset
= cvtnum(argv
[optind
]);
490 printf("non-numeric length argument -- %s\n", argv
[optind
]);
495 count
= cvtnum(argv
[optind
]);
497 printf("non-numeric length argument -- %s\n", argv
[optind
]);
502 if (offset
& 0x1ff) {
503 printf("offset %lld is not sector aligned\n",
509 printf("count %d is not sector aligned\n",
515 buf
= qemu_io_alloc(count
, pattern
);
517 gettimeofday(&t1
, NULL
);
519 cnt
= do_pwrite(buf
, offset
, count
, &total
);
521 cnt
= do_write(buf
, offset
, count
, &total
);
522 gettimeofday(&t2
, NULL
);
525 printf("write failed: %s\n", strerror(-cnt
));
532 /* Finally, report back -- -C gives a parsable format */
534 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
541 static const cmdinfo_t write_cmd
= {
547 .args
= "[-aCpq] [-P pattern ] off len",
548 .oneline
= "writes a number of bytes at a specified offset",
552 static const cmdinfo_t writev_cmd
;
559 " writes a range of bytes from the given offset source from multiple buffers\n"
562 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
564 " Writes into a segment of the currently open file, using a buffer\n"
565 " filled with a set pattern (0xcdcdcdcd).\n"
566 " -P, -- use different pattern to fill file\n"
567 " -C, -- report statistics in a machine parsable format\n"
568 " -q, -- quite mode, do not show I/O statistics\n"
573 writev_f(int argc
, char **argv
)
575 struct timeval t1
, t2
;
576 int Cflag
= 0, qflag
= 0;
580 int count
= 0, total
;
585 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
594 pattern
= atoi(optarg
);
597 return command_usage(&writev_cmd
);
601 if (optind
> argc
- 2)
602 return command_usage(&writev_cmd
);
604 offset
= cvtnum(argv
[optind
]);
606 printf("non-numeric length argument -- %s\n", argv
[optind
]);
611 if (offset
& 0x1ff) {
612 printf("offset %lld is not sector aligned\n",
618 printf("count %d is not sector aligned\n",
624 for (i
= optind
; i
< argc
; i
++) {
627 len
= cvtnum(argv
[optind
]);
629 printf("non-numeric length argument -- %s\n", argv
[i
]);
635 nr_iov
= argc
- optind
;
636 qemu_iovec_init(&qiov
, nr_iov
);
637 buf
= p
= qemu_io_alloc(count
, 0xab);
638 for (i
= 0; i
< nr_iov
; i
++) {
641 len
= cvtnum(argv
[optind
]);
643 printf("non-numeric length argument -- %s\n",
648 qemu_iovec_add(&qiov
, p
, len
);
653 gettimeofday(&t1
, NULL
);
654 cnt
= do_aio_writev(&qiov
, offset
, &total
);
655 gettimeofday(&t2
, NULL
);
658 printf("writev failed: %s\n", strerror(-cnt
));
665 /* Finally, report back -- -C gives a parsable format */
667 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
674 static const cmdinfo_t writev_cmd
= {
679 .args
= "[-Cq] [-P pattern ] off len [len..]",
680 .oneline
= "writes a number of bytes at a specified offset",
685 flush_f(int argc
, char **argv
)
691 static const cmdinfo_t flush_cmd
= {
695 .oneline
= "flush all in-core file state to disk",
699 truncate_f(int argc
, char **argv
)
704 offset
= cvtnum(argv
[1]);
706 printf("non-numeric truncate argument -- %s\n", argv
[1]);
710 ret
= bdrv_truncate(bs
, offset
);
712 printf("truncate: %s", strerror(ret
));
719 static const cmdinfo_t truncate_cmd
= {
726 .oneline
= "truncates the current file at the given offset",
730 length_f(int argc
, char **argv
)
735 size
= bdrv_getlength(bs
);
737 printf("getlength: %s", strerror(size
));
741 cvtstr(size
, s1
, sizeof(s1
));
747 static const cmdinfo_t length_cmd
= {
751 .oneline
= "gets the length of the current file",
756 info_f(int argc
, char **argv
)
762 if (bs
->drv
&& bs
->drv
->format_name
)
763 printf("format name: %s\n", bs
->drv
->format_name
);
764 if (bs
->drv
&& bs
->drv
->protocol_name
)
765 printf("format name: %s\n", bs
->drv
->protocol_name
);
767 ret
= bdrv_get_info(bs
, &bdi
);
771 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
772 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
774 printf("cluster size: %s\n", s1
);
775 printf("vm state offset: %s\n", s2
);
782 static const cmdinfo_t info_cmd
= {
786 .oneline
= "prints information about the current file",
790 alloc_f(int argc
, char **argv
)
798 offset
= cvtnum(argv
[1]);
799 if (offset
& 0x1ff) {
800 printf("offset %lld is not sector aligned\n",
806 nb_sectors
= cvtnum(argv
[2]);
810 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
812 printf("is_allocated: %s", strerror(ret
));
816 cvtstr(offset
, s1
, sizeof(s1
));
819 printf("sector allocated at offset %s\n", s1
);
821 printf("%d/%d sectors allocated at offset %s\n",
822 num
, nb_sectors
, s1
);
826 static const cmdinfo_t alloc_cmd
= {
832 .args
= "off [sectors]",
833 .oneline
= "checks if a sector is present in the file",
837 close_f(int argc
, char **argv
)
844 static const cmdinfo_t close_cmd
= {
848 .oneline
= "close the current open file",
851 static int openfile(char *name
, int flags
)
854 fprintf(stderr
, "file open already, try 'help close'\n");
858 bs
= bdrv_new("hda");
862 if (bdrv_open(bs
, name
, flags
) == -1) {
863 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
876 " opens a new file in the requested mode\n"
879 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
881 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
882 " -C, -- create new file if it doesn't exist\n"
883 " -r, -- open file read-only\n"
884 " -s, -- use snapshot file\n"
885 " -n, -- disable host cache\n"
889 static const cmdinfo_t open_cmd
;
892 open_f(int argc
, char **argv
)
898 while ((c
= getopt(argc
, argv
, "snCr")) != EOF
) {
901 flags
|= BDRV_O_SNAPSHOT
;
904 flags
|= BDRV_O_NOCACHE
;
907 flags
|= BDRV_O_CREAT
;
913 return command_usage(&open_cmd
);
918 flags
|= BDRV_O_RDONLY
;
920 flags
|= BDRV_O_RDWR
;
922 if (optind
!= argc
- 1)
923 return command_usage(&open_cmd
);
925 return openfile(argv
[optind
], flags
);
928 static const cmdinfo_t open_cmd
= {
934 .flags
= CMD_NOFILE_OK
,
935 .args
= "[-Crsn] [path]",
936 .oneline
= "open the file specified by path",
944 /* only one device allowed so far */
954 if (ct
->flags
& CMD_FLAG_GLOBAL
)
956 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
957 fprintf(stderr
, "no file open, try 'help open'\n");
963 static void usage(const char *name
)
966 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
967 "QEMU Disk excerciser\n"
969 " -C, --create create new file if it doesn't exist\n"
970 " -c, --cmd command to execute\n"
971 " -r, --read-only export read-only\n"
972 " -s, --snapshot use snapshot file\n"
973 " -n, --nocache disable host cache\n"
974 " -m, --misalign misalign allocations for O_DIRECT\n"
975 " -h, --help display this help and exit\n"
976 " -V, --version output version information and exit\n"
982 int main(int argc
, char **argv
)
985 const char *sopt
= "hVc:Crsnm";
986 struct option lopt
[] = {
987 { "help", 0, 0, 'h' },
988 { "version", 0, 0, 'V' },
989 { "offset", 1, 0, 'o' },
990 { "cmd", 1, 0, 'c' },
991 { "create", 0, 0, 'C' },
992 { "read-only", 0, 0, 'r' },
993 { "snapshot", 0, 0, 's' },
994 { "nocache", 0, 0, 'n' },
995 { "misalign", 0, 0, 'm' },
1002 progname
= basename(argv
[0]);
1004 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1007 flags
|= BDRV_O_SNAPSHOT
;
1010 flags
|= BDRV_O_NOCACHE
;
1013 add_user_command(optarg
);
1016 flags
|= BDRV_O_CREAT
;
1025 printf("%s version %s\n", progname
, VERSION
);
1036 if ((argc
- optind
) > 1) {
1043 /* initialize commands */
1046 add_command(&open_cmd
);
1047 add_command(&close_cmd
);
1048 add_command(&read_cmd
);
1049 add_command(&readv_cmd
);
1050 add_command(&write_cmd
);
1051 add_command(&writev_cmd
);
1052 add_command(&flush_cmd
);
1053 add_command(&truncate_cmd
);
1054 add_command(&length_cmd
);
1055 add_command(&info_cmd
);
1056 add_command(&alloc_cmd
);
1058 add_args_command(init_args_command
);
1059 add_check_command(init_check_command
);
1061 /* open the device */
1063 flags
|= BDRV_O_RDONLY
;
1065 flags
|= BDRV_O_RDWR
;
1067 if ((argc
- optind
) == 1)
1068 openfile(argv
[optind
], flags
);