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, -- quiet 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, -- quiet 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, -- quiet 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, -- quiet 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, -- quiet 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) {
1147 return command_usage(&aio_read_cmd);
1151 if (optind > argc - 2) {
1153 return command_usage(&aio_read_cmd);
1156 ctx->offset = cvtnum(argv[optind]);
1157 if (ctx->offset < 0) {
1158 printf("non-numeric length argument -- %s\n", argv[optind]);
1164 if (ctx->offset & 0x1ff) {
1165 printf("offset %" PRId64 " is not sector aligned\n",
1171 nr_iov = argc - optind;
1172 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
1174 gettimeofday(&ctx->t1, NULL);
1175 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1176 ctx->qiov.size >> 9, aio_read_done, ctx);
1187 aio_write_help(void)
1191 " asynchronously writes a range of bytes from the given offset source \n"
1192 " from multiple buffers\n"
1195 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1197 " Writes into a segment of the currently open file, using a buffer\n"
1198 " filled with a set pattern (0xcdcdcdcd).\n"
1199 " The write is performed asynchronously and the aio_flush command must be\n"
1200 " used to ensure all outstanding aio requests have been completed\n"
1201 " -P, -- use different pattern to fill file\n"
1202 " -C, -- report statistics in a machine parsable format\n"
1203 " -q, -- quiet mode, do not show I/O statistics\n"
1207 static int aio_write_f(int argc, char **argv);
1209 static const cmdinfo_t aio_write_cmd = {
1210 .name = "aio_write",
1211 .cfunc = aio_write_f,
1214 .args = "[-Cq] [-P pattern ] off len [len..]",
1215 .oneline = "asynchronously writes a number of bytes",
1216 .help = aio_write_help,
1220 aio_write_f(int argc, char **argv)
1224 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1225 BlockDriverAIOCB *acb;
1227 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1236 pattern = parse_pattern(optarg);
1242 return command_usage(&aio_write_cmd);
1246 if (optind > argc - 2) {
1248 return command_usage(&aio_write_cmd);
1251 ctx->offset = cvtnum(argv[optind]);
1252 if (ctx->offset < 0) {
1253 printf("non-numeric length argument -- %s\n", argv[optind]);
1259 if (ctx->offset & 0x1ff) {
1260 printf("offset %" PRId64 " is not sector aligned\n",
1266 nr_iov = argc - optind;
1267 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
1269 gettimeofday(&ctx->t1, NULL);
1270 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1271 ctx->qiov.size >> 9, aio_write_done, ctx);
1282 aio_flush_f(int argc, char **argv)
1288 static const cmdinfo_t aio_flush_cmd = {
1289 .name = "aio_flush",
1290 .cfunc = aio_flush_f,
1291 .oneline = "completes all outstanding aio requests"
1295 flush_f(int argc, char **argv)
1301 static const cmdinfo_t flush_cmd = {
1305 .oneline = "flush all in-core file state to disk",
1309 truncate_f(int argc, char **argv)
1314 offset = cvtnum(argv[1]);
1316 printf("non-numeric truncate argument -- %s\n", argv[1]);
1320 ret = bdrv_truncate(bs, offset);
1322 printf("truncate: %s\n", strerror(-ret));
1329 static const cmdinfo_t truncate_cmd = {
1332 .cfunc = truncate_f,
1336 .oneline = "truncates the current file at the given offset",
1340 length_f(int argc, char **argv)
1345 size = bdrv_getlength(bs);
1347 printf("getlength: %s\n", strerror(-size));
1351 cvtstr(size, s1, sizeof(s1));
1357 static const cmdinfo_t length_cmd = {
1361 .oneline = "gets the length of the current file",
1366 info_f(int argc, char **argv)
1368 BlockDriverInfo bdi;
1369 char s1[64], s2[64];
1372 if (bs->drv && bs->drv->format_name)
1373 printf("format name: %s\n", bs->drv->format_name);
1374 if (bs->drv && bs->drv->protocol_name)
1375 printf("format name: %s\n", bs->drv->protocol_name);
1377 ret = bdrv_get_info(bs, &bdi);
1381 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1382 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1384 printf("cluster size: %s\n", s1);
1385 printf("vm state offset: %s\n", s2);
1392 static const cmdinfo_t info_cmd = {
1396 .oneline = "prints information about the current file",
1404 " discards a range of bytes from the given offset\n"
1407 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1409 " Discards a segment of the currently open file.\n"
1410 " -C, -- report statistics in a machine parsable format\n"
1411 " -q, -- quiet mode, do not show I/O statistics\n"
1415 static int discard_f(int argc, char **argv);
1417 static const cmdinfo_t discard_cmd = {
1423 .args = "[-Cq] off len",
1424 .oneline = "discards a number of bytes at a specified offset",
1425 .help = discard_help,
1429 discard_f(int argc, char **argv)
1431 struct timeval t1, t2;
1432 int Cflag = 0, qflag = 0;
1437 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1446 return command_usage(&discard_cmd);
1450 if (optind != argc - 2) {
1451 return command_usage(&discard_cmd);
1454 offset = cvtnum(argv[optind]);
1456 printf("non-numeric length argument -- %s\n", argv[optind]);
1461 count = cvtnum(argv[optind]);
1463 printf("non-numeric length argument -- %s\n", argv[optind]);
1467 gettimeofday(&t1, NULL);
1468 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS, count >> BDRV_SECTOR_BITS);
1469 gettimeofday(&t2, NULL);
1472 printf("discard failed: %s\n", strerror(-ret));
1476 /* Finally, report back -- -C gives a parsable format */
1479 print_report("discard", &t2, offset, count, count, 1, Cflag);
1487 alloc_f(int argc, char **argv)
1490 int nb_sectors, remaining;
1495 offset = cvtnum(argv[1]);
1496 if (offset & 0x1ff) {
1497 printf("offset %" PRId64 " is not sector aligned\n",
1503 nb_sectors = cvtnum(argv[2]);
1507 remaining = nb_sectors;
1510 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1517 cvtstr(offset, s1, sizeof(s1));
1519 printf("%d/%d sectors allocated at offset %s\n",
1520 sum_alloc, nb_sectors, s1);
1524 static const cmdinfo_t alloc_cmd = {
1530 .args = "off [sectors]",
1531 .oneline = "checks if a sector is present in the file",
1535 map_f(int argc, char **argv)
1540 int num, num_checked;
1545 nb_sectors = bs->total_sectors;
1548 num_checked = MIN(nb_sectors, INT_MAX);
1549 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1550 retstr = ret ? " allocated" : "not allocated";
1551 cvtstr(offset << 9ULL, s1, sizeof(s1));
1552 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1553 offset << 9ULL, num, num_checked, retstr, s1, ret);
1557 } while(offset < bs->total_sectors);
1562 static const cmdinfo_t map_cmd = {
1568 .oneline = "prints the allocated areas of a file",
1573 close_f(int argc, char **argv)
1580 static const cmdinfo_t close_cmd = {
1584 .oneline = "close the current open file",
1587 static int openfile(char *name, int flags, int growable)
1590 fprintf(stderr, "file open already, try 'help close'\n");
1595 if (bdrv_file_open(&bs, name, flags)) {
1596 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1600 bs = bdrv_new("hda");
1602 if (bdrv_open(bs, name, flags, NULL) < 0) {
1603 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1617 " opens a new file in the requested mode\n"
1620 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1622 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1623 " -r, -- open file read-only\n"
1624 " -s, -- use snapshot file\n"
1625 " -n, -- disable host cache\n"
1626 " -g, -- allow file to grow (only applies to protocols)"
1630 static int open_f(int argc, char **argv);
1632 static const cmdinfo_t open_cmd = {
1638 .flags = CMD_NOFILE_OK,
1639 .args = "[-Crsn] [path]",
1640 .oneline = "open the file specified by path",
1645 open_f(int argc, char **argv)
1652 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1655 flags |= BDRV_O_SNAPSHOT;
1658 flags |= BDRV_O_NOCACHE;
1667 return command_usage(&open_cmd);
1672 flags |= BDRV_O_RDWR;
1675 if (optind != argc - 1)
1676 return command_usage(&open_cmd);
1678 return openfile(argv[optind], flags, growable);
1685 /* only one device allowed so far */
1693 const cmdinfo_t *ct)
1695 if (ct->flags & CMD_FLAG_GLOBAL)
1697 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1698 fprintf(stderr, "no file open, try 'help open'\n");
1704 static void usage(const char *name)
1707 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1708 "QEMU Disk exerciser\n"
1710 " -c, --cmd command to execute\n"
1711 " -r, --read-only export read-only\n"
1712 " -s, --snapshot use snapshot file\n"
1713 " -n, --nocache disable host cache\n"
1714 " -g, --growable allow file to grow (only applies to protocols)\n"
1715 " -m, --misalign misalign allocations for O_DIRECT\n"
1716 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1717 " -h, --help display this help and exit\n"
1718 " -V, --version output version information and exit\n"
1724 int main(int argc, char **argv)
1728 const char *sopt = "hVc:rsnmgk";
1729 const struct option lopt[] = {
1730 { "help", 0, NULL, 'h' },
1731 { "version", 0, NULL, 'V' },
1732 { "offset", 1, NULL, 'o' },
1733 { "cmd", 1, NULL, 'c' },
1734 { "read-only", 0, NULL, 'r' },
1735 { "snapshot", 0, NULL, 's' },
1736 { "nocache", 0, NULL, 'n' },
1737 { "misalign", 0, NULL, 'm' },
1738 { "growable", 0, NULL, 'g' },
1739 { "native-aio", 0, NULL, 'k' },
1740 { NULL, 0, NULL, 0 }
1746 progname = basename(argv[0]);
1748 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1751 flags |= BDRV_O_SNAPSHOT;
1754 flags |= BDRV_O_NOCACHE;
1757 add_user_command(optarg);
1769 flags |= BDRV_O_NATIVE_AIO;
1772 printf("%s version %s\n", progname, VERSION);
1783 if ((argc - optind) > 1) {
1790 /* initialize commands */
1793 add_command(&open_cmd);
1794 add_command(&close_cmd);
1795 add_command(&read_cmd);
1796 add_command(&readv_cmd);
1797 add_command(&write_cmd);
1798 add_command(&writev_cmd);
1799 add_command(&multiwrite_cmd);
1800 add_command(&aio_read_cmd);
1801 add_command(&aio_write_cmd);
1802 add_command(&aio_flush_cmd);
1803 add_command(&flush_cmd);
1804 add_command(&truncate_cmd);
1805 add_command(&length_cmd);
1806 add_command(&info_cmd);
1807 add_command(&discard_cmd);
1808 add_command(&alloc_cmd);
1809 add_command(&map_cmd);
1811 add_args_command(init_args_command);
1812 add_check_command(init_check_command);
1814 /* open the device */
1816 flags |= BDRV_O_RDWR;
1819 if ((argc - optind) == 1)
1820 openfile(argv[optind], flags, growable);
1824 * Make sure all outstanding requests get flushed the program exits.