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 " -p, -- use bdrv_pread to read the file\n"
195 " -P, -- use a pattern to verify read data\n"
196 " -C, -- report statistics in a machine parsable format\n"
197 " -v, -- dump buffer to standard output\n"
198 " -q, -- quite mode, do not show I/O statistics\n"
203 read_f(int argc
, char **argv
)
205 struct timeval t1
, t2
;
206 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
214 while ((c
= getopt(argc
, argv
, "CpP:qv")) != EOF
) {
224 pattern
= atoi(optarg
);
233 return command_usage(&read_cmd
);
237 if (optind
!= argc
- 2)
238 return command_usage(&read_cmd
);
240 offset
= cvtnum(argv
[optind
]);
242 printf("non-numeric length argument -- %s\n", argv
[optind
]);
247 count
= cvtnum(argv
[optind
]);
249 printf("non-numeric length argument -- %s\n", argv
[optind
]);
254 if (offset
& 0x1ff) {
255 printf("offset %lld is not sector aligned\n",
260 printf("count %d is not sector aligned\n",
266 buf
= qemu_io_alloc(count
, 0xab);
268 gettimeofday(&t1
, NULL
);
270 cnt
= do_pread(buf
, offset
, count
, &total
);
272 cnt
= do_read(buf
, offset
, count
, &total
);
273 gettimeofday(&t2
, NULL
);
276 printf("read failed: %s\n", strerror(-cnt
));
281 void* cmp_buf
= malloc(count
);
282 memset(cmp_buf
, pattern
, count
);
283 if (memcmp(buf
, cmp_buf
, count
)) {
284 printf("Pattern verification failed at offset %lld, "
286 (long long) offset
, count
);
295 dump_buffer(buf
, offset
, count
);
297 /* Finally, report back -- -C gives a parsable format */
299 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
306 static const cmdinfo_t read_cmd
= {
312 .args
= "[-aCpqv] [-P pattern ] off len",
313 .oneline
= "reads a number of bytes at a specified offset",
317 static const cmdinfo_t readv_cmd
;
324 " reads a range of bytes from the given offset into multiple buffers\n"
327 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
329 " Reads a segment of the currently open file, optionally dumping it to the\n"
330 " standard output stream (with -v option) for subsequent inspection.\n"
331 " Uses multiple iovec buffers if more than one byte range is specified.\n"
332 " -C, -- report statistics in a machine parsable format\n"
333 " -P, -- use a pattern to verify read data\n"
334 " -v, -- dump buffer to standard output\n"
335 " -q, -- quite mode, do not show I/O statistics\n"
340 readv_f(int argc
, char **argv
)
342 struct timeval t1
, t2
;
343 int Cflag
= 0, qflag
= 0, vflag
= 0;
347 int count
= 0, total
;
353 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
360 pattern
= atoi(optarg
);
369 return command_usage(&readv_cmd
);
373 if (optind
> argc
- 2)
374 return command_usage(&readv_cmd
);
377 offset
= cvtnum(argv
[optind
]);
379 printf("non-numeric length argument -- %s\n", argv
[optind
]);
384 if (offset
& 0x1ff) {
385 printf("offset %lld is not sector aligned\n",
391 printf("count %d is not sector aligned\n",
396 for (i
= optind
; i
< argc
; i
++) {
399 len
= cvtnum(argv
[i
]);
401 printf("non-numeric length argument -- %s\n", argv
[i
]);
407 nr_iov
= argc
- optind
;
408 qemu_iovec_init(&qiov
, nr_iov
);
409 buf
= p
= qemu_io_alloc(count
, 0xab);
410 for (i
= 0; i
< nr_iov
; i
++) {
413 len
= cvtnum(argv
[optind
]);
415 printf("non-numeric length argument -- %s\n",
420 qemu_iovec_add(&qiov
, p
, len
);
425 gettimeofday(&t1
, NULL
);
426 cnt
= do_aio_readv(&qiov
, offset
, &total
);
427 gettimeofday(&t2
, NULL
);
430 printf("readv failed: %s\n", strerror(-cnt
));
435 void* cmp_buf
= malloc(count
);
436 memset(cmp_buf
, pattern
, count
);
437 if (memcmp(buf
, cmp_buf
, count
)) {
438 printf("Pattern verification failed at offset %lld, "
440 (long long) offset
, count
);
449 dump_buffer(buf
, offset
, qiov
.size
);
451 /* Finally, report back -- -C gives a parsable format */
453 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
460 static const cmdinfo_t readv_cmd
= {
465 .args
= "[-Cqv] [-P pattern ] off len [len..]",
466 .oneline
= "reads a number of bytes at a specified offset",
470 static const cmdinfo_t write_cmd
;
477 " writes a range of bytes from the given offset\n"
480 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
482 " Writes into a segment of the currently open file, using a buffer\n"
483 " filled with a set pattern (0xcdcdcdcd).\n"
484 " -p, -- use bdrv_pwrite to write the file\n"
485 " -P, -- use different pattern to fill file\n"
486 " -C, -- report statistics in a machine parsable format\n"
487 " -q, -- quite mode, do not show I/O statistics\n"
492 write_f(int argc
, char **argv
)
494 struct timeval t1
, t2
;
495 int Cflag
= 0, pflag
= 0, qflag
= 0;
502 while ((c
= getopt(argc
, argv
, "CpP:q")) != EOF
) {
511 pattern
= atoi(optarg
);
517 return command_usage(&write_cmd
);
521 if (optind
!= argc
- 2)
522 return command_usage(&write_cmd
);
524 offset
= cvtnum(argv
[optind
]);
526 printf("non-numeric length argument -- %s\n", argv
[optind
]);
531 count
= cvtnum(argv
[optind
]);
533 printf("non-numeric length argument -- %s\n", argv
[optind
]);
538 if (offset
& 0x1ff) {
539 printf("offset %lld is not sector aligned\n",
545 printf("count %d is not sector aligned\n",
551 buf
= qemu_io_alloc(count
, pattern
);
553 gettimeofday(&t1
, NULL
);
555 cnt
= do_pwrite(buf
, offset
, count
, &total
);
557 cnt
= do_write(buf
, offset
, count
, &total
);
558 gettimeofday(&t2
, NULL
);
561 printf("write failed: %s\n", strerror(-cnt
));
568 /* Finally, report back -- -C gives a parsable format */
570 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
577 static const cmdinfo_t write_cmd
= {
583 .args
= "[-aCpq] [-P pattern ] off len",
584 .oneline
= "writes a number of bytes at a specified offset",
588 static const cmdinfo_t writev_cmd
;
595 " writes a range of bytes from the given offset source from multiple buffers\n"
598 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
600 " Writes into a segment of the currently open file, using a buffer\n"
601 " filled with a set pattern (0xcdcdcdcd).\n"
602 " -P, -- use different pattern to fill file\n"
603 " -C, -- report statistics in a machine parsable format\n"
604 " -q, -- quite mode, do not show I/O statistics\n"
609 writev_f(int argc
, char **argv
)
611 struct timeval t1
, t2
;
612 int Cflag
= 0, qflag
= 0;
616 int count
= 0, total
;
621 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
630 pattern
= atoi(optarg
);
633 return command_usage(&writev_cmd
);
637 if (optind
> argc
- 2)
638 return command_usage(&writev_cmd
);
640 offset
= cvtnum(argv
[optind
]);
642 printf("non-numeric length argument -- %s\n", argv
[optind
]);
647 if (offset
& 0x1ff) {
648 printf("offset %lld is not sector aligned\n",
654 printf("count %d is not sector aligned\n",
660 for (i
= optind
; i
< argc
; i
++) {
663 len
= cvtnum(argv
[optind
]);
665 printf("non-numeric length argument -- %s\n", argv
[i
]);
671 nr_iov
= argc
- optind
;
672 qemu_iovec_init(&qiov
, nr_iov
);
673 buf
= p
= qemu_io_alloc(count
, pattern
);
674 for (i
= 0; i
< nr_iov
; i
++) {
677 len
= cvtnum(argv
[optind
]);
679 printf("non-numeric length argument -- %s\n",
684 qemu_iovec_add(&qiov
, p
, len
);
689 gettimeofday(&t1
, NULL
);
690 cnt
= do_aio_writev(&qiov
, offset
, &total
);
691 gettimeofday(&t2
, NULL
);
694 printf("writev failed: %s\n", strerror(-cnt
));
701 /* Finally, report back -- -C gives a parsable format */
703 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
710 static const cmdinfo_t writev_cmd
= {
715 .args
= "[-Cq] [-P pattern ] off len [len..]",
716 .oneline
= "writes a number of bytes at a specified offset",
721 flush_f(int argc
, char **argv
)
727 static const cmdinfo_t flush_cmd
= {
731 .oneline
= "flush all in-core file state to disk",
735 truncate_f(int argc
, char **argv
)
740 offset
= cvtnum(argv
[1]);
742 printf("non-numeric truncate argument -- %s\n", argv
[1]);
746 ret
= bdrv_truncate(bs
, offset
);
748 printf("truncate: %s", strerror(ret
));
755 static const cmdinfo_t truncate_cmd
= {
762 .oneline
= "truncates the current file at the given offset",
766 length_f(int argc
, char **argv
)
771 size
= bdrv_getlength(bs
);
773 printf("getlength: %s", strerror(size
));
777 cvtstr(size
, s1
, sizeof(s1
));
783 static const cmdinfo_t length_cmd
= {
787 .oneline
= "gets the length of the current file",
792 info_f(int argc
, char **argv
)
798 if (bs
->drv
&& bs
->drv
->format_name
)
799 printf("format name: %s\n", bs
->drv
->format_name
);
800 if (bs
->drv
&& bs
->drv
->protocol_name
)
801 printf("format name: %s\n", bs
->drv
->protocol_name
);
803 ret
= bdrv_get_info(bs
, &bdi
);
807 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
808 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
810 printf("cluster size: %s\n", s1
);
811 printf("vm state offset: %s\n", s2
);
818 static const cmdinfo_t info_cmd
= {
822 .oneline
= "prints information about the current file",
826 alloc_f(int argc
, char **argv
)
835 offset
= cvtnum(argv
[1]);
836 if (offset
& 0x1ff) {
837 printf("offset %lld is not sector aligned\n",
843 nb_sectors
= cvtnum(argv
[2]);
847 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
849 cvtstr(offset
, s1
, sizeof(s1
));
851 retstr
= ret
? "allocated" : "not allocated";
853 printf("sector %s at offset %s\n", retstr
, s1
);
855 printf("%d/%d sectors %s at offset %s\n",
856 num
, nb_sectors
, retstr
, s1
);
860 static const cmdinfo_t alloc_cmd
= {
866 .args
= "off [sectors]",
867 .oneline
= "checks if a sector is present in the file",
871 close_f(int argc
, char **argv
)
878 static const cmdinfo_t close_cmd
= {
882 .oneline
= "close the current open file",
885 static int openfile(char *name
, int flags
)
888 fprintf(stderr
, "file open already, try 'help close'\n");
892 bs
= bdrv_new("hda");
896 if (bdrv_open(bs
, name
, flags
) == -1) {
897 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
910 " opens a new file in the requested mode\n"
913 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
915 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
916 " -C, -- create new file if it doesn't exist\n"
917 " -r, -- open file read-only\n"
918 " -s, -- use snapshot file\n"
919 " -n, -- disable host cache\n"
923 static const cmdinfo_t open_cmd
;
926 open_f(int argc
, char **argv
)
932 while ((c
= getopt(argc
, argv
, "snCr")) != EOF
) {
935 flags
|= BDRV_O_SNAPSHOT
;
938 flags
|= BDRV_O_NOCACHE
;
941 flags
|= BDRV_O_CREAT
;
947 return command_usage(&open_cmd
);
952 flags
|= BDRV_O_RDONLY
;
954 flags
|= BDRV_O_RDWR
;
956 if (optind
!= argc
- 1)
957 return command_usage(&open_cmd
);
959 return openfile(argv
[optind
], flags
);
962 static const cmdinfo_t open_cmd
= {
968 .flags
= CMD_NOFILE_OK
,
969 .args
= "[-Crsn] [path]",
970 .oneline
= "open the file specified by path",
978 /* only one device allowed so far */
988 if (ct
->flags
& CMD_FLAG_GLOBAL
)
990 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
991 fprintf(stderr
, "no file open, try 'help open'\n");
997 static void usage(const char *name
)
1000 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1001 "QEMU Disk excerciser\n"
1003 " -C, --create create new file if it doesn't exist\n"
1004 " -c, --cmd command to execute\n"
1005 " -r, --read-only export read-only\n"
1006 " -s, --snapshot use snapshot file\n"
1007 " -n, --nocache disable host cache\n"
1008 " -m, --misalign misalign allocations for O_DIRECT\n"
1009 " -h, --help display this help and exit\n"
1010 " -V, --version output version information and exit\n"
1016 int main(int argc
, char **argv
)
1019 const char *sopt
= "hVc:Crsnm";
1020 struct option lopt
[] = {
1021 { "help", 0, 0, 'h' },
1022 { "version", 0, 0, 'V' },
1023 { "offset", 1, 0, 'o' },
1024 { "cmd", 1, 0, 'c' },
1025 { "create", 0, 0, 'C' },
1026 { "read-only", 0, 0, 'r' },
1027 { "snapshot", 0, 0, 's' },
1028 { "nocache", 0, 0, 'n' },
1029 { "misalign", 0, 0, 'm' },
1036 progname
= basename(argv
[0]);
1038 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1041 flags
|= BDRV_O_SNAPSHOT
;
1044 flags
|= BDRV_O_NOCACHE
;
1047 add_user_command(optarg
);
1050 flags
|= BDRV_O_CREAT
;
1059 printf("%s version %s\n", progname
, VERSION
);
1070 if ((argc
- optind
) > 1) {
1077 /* initialize commands */
1080 add_command(&open_cmd
);
1081 add_command(&close_cmd
);
1082 add_command(&read_cmd
);
1083 add_command(&readv_cmd
);
1084 add_command(&write_cmd
);
1085 add_command(&writev_cmd
);
1086 add_command(&flush_cmd
);
1087 add_command(&truncate_cmd
);
1088 add_command(&length_cmd
);
1089 add_command(&info_cmd
);
1090 add_command(&alloc_cmd
);
1092 add_args_command(init_args_command
);
1093 add_check_command(init_check_command
);
1095 /* open the device */
1097 flags
|= BDRV_O_RDONLY
;
1099 flags
|= BDRV_O_RDWR
;
1101 if ((argc
- optind
) == 1)
1102 openfile(argv
[optind
], flags
);