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 /* Some compilers get confused and warn if this is not initialized. */
216 int pattern = 0, pattern_offset = 0, pattern_count = 0;
218 while ((c = getopt(argc, argv, "Cl:pP:qs:v")) != EOF) {
225 pattern_count = cvtnum(optarg);
226 if (pattern_count < 0) {
227 printf("non-numeric length argument -- %s\n", optarg);
236 pattern = atoi(optarg);
243 pattern_offset = cvtnum(optarg);
244 if (pattern_offset < 0) {
245 printf("non-numeric length argument -- %s\n", optarg);
253 return command_usage(&read_cmd);
257 if (optind != argc - 2)
258 return command_usage(&read_cmd);
260 offset = cvtnum(argv[optind]);
262 printf("non-numeric length argument -- %s\n", argv[optind]);
267 count = cvtnum(argv[optind]);
269 printf("non-numeric length argument -- %s\n", argv[optind]);
273 if (!Pflag && (lflag || sflag)) {
274 return command_usage(&read_cmd);
278 pattern_count = count - pattern_offset;
281 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
282 printf("pattern verfication range exceeds end of read data\n");
287 if (offset & 0x1ff) {
288 printf("offset %lld is not sector aligned\n",
293 printf("count %d is not sector aligned\n",
299 buf = qemu_io_alloc(count, 0xab);
301 gettimeofday(&t1, NULL);
303 cnt = do_pread(buf, offset, count, &total);
305 cnt = do_read(buf, offset, count, &total);
306 gettimeofday(&t2, NULL);
309 printf("read failed: %s\n", strerror(-cnt));
314 void* cmp_buf = malloc(pattern_count);
315 memset(cmp_buf, pattern, pattern_count);
316 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
317 printf("Pattern verification failed at offset %lld, "
319 (long long) offset + pattern_offset, pattern_count);
328 dump_buffer(buf, offset, count);
330 /* Finally, report back -- -C gives a parsable format */
332 print_report("read", &t2, offset, count, total, cnt, Cflag);
339 static const cmdinfo_t read_cmd = {
345 .args = "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
346 .oneline = "reads a number of bytes at a specified offset",
350 static const cmdinfo_t readv_cmd;
357 " reads a range of bytes from the given offset into multiple buffers\n"
360 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
362 " Reads a segment of the currently open file, optionally dumping it to the\n"
363 " standard output stream (with -v option) for subsequent inspection.\n"
364 " Uses multiple iovec buffers if more than one byte range is specified.\n"
365 " -C, -- report statistics in a machine parsable format\n"
366 " -P, -- use a pattern to verify read data\n"
367 " -v, -- dump buffer to standard output\n"
368 " -q, -- quite mode, do not show I/O statistics\n"
373 readv_f(int argc, char **argv)
375 struct timeval t1, t2;
376 int Cflag = 0, qflag = 0, vflag = 0;
380 int count = 0, total;
386 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
393 pattern = atoi(optarg);
402 return command_usage(&readv_cmd);
406 if (optind > argc - 2)
407 return command_usage(&readv_cmd);
410 offset = cvtnum(argv[optind]);
412 printf("non-numeric length argument -- %s\n", argv[optind]);
417 if (offset & 0x1ff) {
418 printf("offset %lld is not sector aligned\n",
424 printf("count %d is not sector aligned\n",
429 for (i = optind; i < argc; i++) {
432 len = cvtnum(argv[i]);
434 printf("non-numeric length argument -- %s\n", argv[i]);
440 nr_iov = argc - optind;
441 qemu_iovec_init(&qiov, nr_iov);
442 buf = p = qemu_io_alloc(count, 0xab);
443 for (i = 0; i < nr_iov; i++) {
446 len = cvtnum(argv[optind]);
448 printf("non-numeric length argument -- %s\n",
453 qemu_iovec_add(&qiov, p, len);
458 gettimeofday(&t1, NULL);
459 cnt = do_aio_readv(&qiov, offset, &total);
460 gettimeofday(&t2, NULL);
463 printf("readv failed: %s\n", strerror(-cnt));
468 void* cmp_buf = malloc(count);
469 memset(cmp_buf, pattern, count);
470 if (memcmp(buf, cmp_buf, count)) {
471 printf("Pattern verification failed at offset %lld, "
473 (long long) offset, count);
482 dump_buffer(buf, offset, qiov.size);
484 /* Finally, report back -- -C gives a parsable format */
486 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
493 static const cmdinfo_t readv_cmd = {
498 .args = "[-Cqv] [-P pattern ] off len [len..]",
499 .oneline = "reads a number of bytes at a specified offset",
503 static const cmdinfo_t write_cmd;
510 " writes a range of bytes from the given offset\n"
513 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
515 " Writes into a segment of the currently open file, using a buffer\n"
516 " filled with a set pattern (0xcdcdcdcd).\n"
517 " -p, -- use bdrv_pwrite to write the file\n"
518 " -P, -- use different pattern to fill file\n"
519 " -C, -- report statistics in a machine parsable format\n"
520 " -q, -- quite mode, do not show I/O statistics\n"
525 write_f(int argc, char **argv)
527 struct timeval t1, t2;
528 int Cflag = 0, pflag = 0, qflag = 0;
533 /* Some compilers get confused and warn if this is not initialized. */
537 while ((c = getopt(argc, argv, "CpP:q")) != EOF) {
546 pattern = atoi(optarg);
552 return command_usage(&write_cmd);
556 if (optind != argc - 2)
557 return command_usage(&write_cmd);
559 offset = cvtnum(argv[optind]);
561 printf("non-numeric length argument -- %s\n", argv[optind]);
566 count = cvtnum(argv[optind]);
568 printf("non-numeric length argument -- %s\n", argv[optind]);
573 if (offset & 0x1ff) {
574 printf("offset %lld is not sector aligned\n",
580 printf("count %d is not sector aligned\n",
586 buf = qemu_io_alloc(count, pattern);
588 gettimeofday(&t1, NULL);
590 cnt = do_pwrite(buf, offset, count, &total);
592 cnt = do_write(buf, offset, count, &total);
593 gettimeofday(&t2, NULL);
596 printf("write failed: %s\n", strerror(-cnt));
603 /* Finally, report back -- -C gives a parsable format */
605 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
612 static const cmdinfo_t write_cmd = {
618 .args = "[-aCpq] [-P pattern ] off len",
619 .oneline = "writes a number of bytes at a specified offset",
623 static const cmdinfo_t writev_cmd;
630 " writes a range of bytes from the given offset source from multiple buffers\n"
633 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
635 " Writes into a segment of the currently open file, using a buffer\n"
636 " filled with a set pattern (0xcdcdcdcd).\n"
637 " -P, -- use different pattern to fill file\n"
638 " -C, -- report statistics in a machine parsable format\n"
639 " -q, -- quite mode, do not show I/O statistics\n"
644 writev_f(int argc, char **argv)
646 struct timeval t1, t2;
647 int Cflag = 0, qflag = 0;
651 int count = 0, total;
656 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
665 pattern = atoi(optarg);
668 return command_usage(&writev_cmd);
672 if (optind > argc - 2)
673 return command_usage(&writev_cmd);
675 offset = cvtnum(argv[optind]);
677 printf("non-numeric length argument -- %s\n", argv[optind]);
682 if (offset & 0x1ff) {
683 printf("offset %lld is not sector aligned\n",
689 printf("count %d is not sector aligned\n",
695 for (i = optind; i < argc; i++) {
698 len = cvtnum(argv[optind]);
700 printf("non-numeric length argument -- %s\n", argv[i]);
706 nr_iov = argc - optind;
707 qemu_iovec_init(&qiov, nr_iov);
708 buf = p = qemu_io_alloc(count, pattern);
709 for (i = 0; i < nr_iov; i++) {
712 len = cvtnum(argv[optind]);
714 printf("non-numeric length argument -- %s\n",
719 qemu_iovec_add(&qiov, p, len);
724 gettimeofday(&t1, NULL);
725 cnt = do_aio_writev(&qiov, offset, &total);
726 gettimeofday(&t2, NULL);
729 printf("writev failed: %s\n", strerror(-cnt));
736 /* Finally, report back -- -C gives a parsable format */
738 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
745 static const cmdinfo_t writev_cmd = {
750 .args = "[-Cq] [-P pattern ] off len [len..]",
751 .oneline = "writes a number of bytes at a specified offset",
756 flush_f(int argc, char **argv)
762 static const cmdinfo_t flush_cmd = {
766 .oneline = "flush all in-core file state to disk",
770 truncate_f(int argc, char **argv)
775 offset = cvtnum(argv[1]);
777 printf("non-numeric truncate argument -- %s\n", argv[1]);
781 ret = bdrv_truncate(bs, offset);
783 printf("truncate: %s", strerror(ret));
790 static const cmdinfo_t truncate_cmd = {
797 .oneline = "truncates the current file at the given offset",
801 length_f(int argc, char **argv)
806 size = bdrv_getlength(bs);
808 printf("getlength: %s", strerror(size));
812 cvtstr(size, s1, sizeof(s1));
818 static const cmdinfo_t length_cmd = {
822 .oneline = "gets the length of the current file",
827 info_f(int argc, char **argv)
833 if (bs->drv && bs->drv->format_name)
834 printf("format name: %s\n", bs->drv->format_name);
835 if (bs->drv && bs->drv->protocol_name)
836 printf("format name: %s\n", bs->drv->protocol_name);
838 ret = bdrv_get_info(bs, &bdi);
842 cvtstr(bdi.cluster_size, s1, sizeof(s1));
843 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
845 printf("cluster size: %s\n", s1);
846 printf("vm state offset: %s\n", s2);
853 static const cmdinfo_t info_cmd = {
857 .oneline = "prints information about the current file",
861 alloc_f(int argc, char **argv)
870 offset = cvtnum(argv[1]);
871 if (offset & 0x1ff) {
872 printf("offset %lld is not sector aligned\n",
878 nb_sectors = cvtnum(argv[2]);
882 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
884 cvtstr(offset, s1, sizeof(s1));
886 retstr = ret ? "allocated" : "not allocated";
888 printf("sector %s at offset %s\n", retstr, s1);
890 printf("%d/%d sectors %s at offset %s\n",
891 num, nb_sectors, retstr, s1);
895 static const cmdinfo_t alloc_cmd = {
901 .args = "off [sectors]",
902 .oneline = "checks if a sector is present in the file",
906 close_f(int argc, char **argv)
913 static const cmdinfo_t close_cmd = {
917 .oneline = "close the current open file",
920 static int openfile(char *name, int flags)
923 fprintf(stderr, "file open already, try 'help close'\n");
927 bs = bdrv_new("hda");
931 if (bdrv_open(bs, name, flags) == -1) {
932 fprintf(stderr, "%s: can't open device %s\n", progname, name);
945 " opens a new file in the requested mode\n"
948 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
950 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
951 " -C, -- create new file if it doesn't exist\n"
952 " -r, -- open file read-only\n"
953 " -s, -- use snapshot file\n"
954 " -n, -- disable host cache\n"
958 static const cmdinfo_t open_cmd;
961 open_f(int argc, char **argv)
967 while ((c = getopt(argc, argv, "snCr")) != EOF) {
970 flags |= BDRV_O_SNAPSHOT;
973 flags |= BDRV_O_NOCACHE;
976 flags |= BDRV_O_CREAT;
982 return command_usage(&open_cmd);
987 flags |= BDRV_O_RDONLY;
989 flags |= BDRV_O_RDWR;
991 if (optind != argc - 1)
992 return command_usage(&open_cmd);
994 return openfile(argv[optind], flags);
997 static const cmdinfo_t open_cmd = {
1003 .flags = CMD_NOFILE_OK,
1004 .args = "[-Crsn] [path]",
1005 .oneline = "open the file specified by path",
1013 /* only one device allowed so far */
1021 const cmdinfo_t *ct)
1023 if (ct->flags & CMD_FLAG_GLOBAL)
1025 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1026 fprintf(stderr, "no file open, try 'help open'\n");
1032 static void usage(const char *name)
1035 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1036 "QEMU Disk excerciser\n"
1038 " -C, --create create new file if it doesn't exist\n"
1039 " -c, --cmd command to execute\n"
1040 " -r, --read-only export read-only\n"
1041 " -s, --snapshot use snapshot file\n"
1042 " -n, --nocache disable host cache\n"
1043 " -m, --misalign misalign allocations for O_DIRECT\n"
1044 " -h, --help display this help and exit\n"
1045 " -V, --version output version information and exit\n"
1051 int main(int argc, char **argv)
1054 const char *sopt = "hVc:Crsnm";
1055 struct option lopt[] = {
1056 { "help", 0, 0, 'h' },
1057 { "version", 0, 0, 'V' },
1058 { "offset", 1, 0, 'o' },
1059 { "cmd", 1, 0, 'c' },
1060 { "create", 0, 0, 'C' },
1061 { "read-only", 0, 0, 'r' },
1062 { "snapshot", 0, 0, 's' },
1063 { "nocache", 0, 0, 'n' },
1064 { "misalign", 0, 0, 'm' },
1071 progname = basename(argv[0]);
1073 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1076 flags |= BDRV_O_SNAPSHOT;
1079 flags |= BDRV_O_NOCACHE;
1082 add_user_command(optarg);
1085 flags |= BDRV_O_CREAT;
1094 printf("%s version %s\n", progname, VERSION);
1105 if ((argc - optind) > 1) {
1112 /* initialize commands */
1115 add_command(&open_cmd);
1116 add_command(&close_cmd);
1117 add_command(&read_cmd);
1118 add_command(&readv_cmd);
1119 add_command(&write_cmd);
1120 add_command(&writev_cmd);
1121 add_command(&flush_cmd);
1122 add_command(&truncate_cmd);
1123 add_command(&length_cmd);
1124 add_command(&info_cmd);
1125 add_command(&alloc_cmd);
1127 add_args_command(init_args_command);
1128 add_check_command(init_check_command);
1130 /* open the device */
1132 flags |= BDRV_O_RDONLY;
1134 flags |= BDRV_O_RDWR;
1136 if ((argc - optind) == 1)
1137 openfile(argv[optind], flags);