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 "qemu/main-loop.h"
19 #include "block/block_int.h"
21 #include "trace/control.h"
23 #define VERSION "0.0.1"
25 #define CMD_NOFILE_OK 0x01
28 static BlockDriverState *bs;
32 static int64_t cvtnum(const char *s)
35 return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
39 * Parse the pattern argument to various sub-commands.
41 * Because the pattern is used as an argument to memset it must evaluate
42 * to an unsigned integer that fits into a single byte.
44 static int parse_pattern(const char *arg)
49 pattern = strtol(arg, &endptr, 0);
50 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
51 printf("%s is not a valid pattern byte\n", arg);
59 * Memory allocation helpers.
61 * Make sure memory is aligned by default, or purposefully misaligned if
62 * that is specified on the command line.
65 #define MISALIGN_OFFSET 16
66 static void *qemu_io_alloc(size_t len, int pattern)
71 len += MISALIGN_OFFSET;
73 buf = qemu_blockalign(bs, len);
74 memset(buf, pattern, len);
76 buf += MISALIGN_OFFSET;
81 static void qemu_io_free(void *p)
89 static void dump_buffer(const void *buffer, int64_t offset, int len)
94 for (i = 0, p = buffer; i < len; i += 16) {
97 printf("%08" PRIx64 ": ", offset + i);
98 for (j = 0; j < 16 && i + j < len; j++, p++) {
102 for (j = 0; j < 16 && i + j < len; j++, s++) {
113 static void print_report(const char *op, struct timeval *t, int64_t offset,
114 int count, int total, int cnt, int Cflag)
116 char s1[64], s2[64], ts[64];
118 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
120 cvtstr((double)total, s1, sizeof(s1));
121 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
122 printf("%s %d/%d bytes at offset %" PRId64 "\n",
123 op, total, count, offset);
124 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
125 s1, cnt, ts, s2, tdiv((double)cnt, *t));
126 } else {/* bytes,ops,time,bytes/sec,ops/sec */
127 printf("%d,%d,%s,%.3f,%.3f\n",
129 tdiv((double)total, *t),
130 tdiv((double)cnt, *t));
135 * Parse multiple length statements for vectored I/O, and construct an I/O
136 * vector matching it.
139 create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
141 size_t *sizes = g_new0(size_t, nr_iov);
147 for (i = 0; i < nr_iov; i++) {
153 printf("non-numeric length argument -- %s\n", arg);
157 /* should be SIZE_T_MAX, but that doesn't exist */
159 printf("too large length argument -- %s\n", arg);
164 printf("length argument %" PRId64
165 " is not sector aligned\n", len);
173 qemu_iovec_init(qiov, nr_iov);
175 buf = p = qemu_io_alloc(count, pattern);
177 for (i = 0; i < nr_iov; i++) {
178 qemu_iovec_add(qiov, p, sizes[i]);
187 static int do_read(char *buf, int64_t offset, int count, int *total)
191 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
199 static int do_write(char *buf, int64_t offset, int count, int *total)
203 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
211 static int do_pread(char *buf, int64_t offset, int count, int *total)
213 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
220 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
222 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
237 static void coroutine_fn co_write_zeroes_entry(void *opaque)
239 CoWriteZeroes *data = opaque;
241 data->ret = bdrv_co_write_zeroes(bs, data->offset / BDRV_SECTOR_SIZE,
242 data->count / BDRV_SECTOR_SIZE);
245 *data->total = data->ret;
249 *data->total = data->count;
252 static int do_co_write_zeroes(int64_t offset, int count, int *total)
255 CoWriteZeroes data = {
262 co = qemu_coroutine_create(co_write_zeroes_entry);
263 qemu_coroutine_enter(co, &data);
274 static int do_write_compressed(char *buf, int64_t offset, int count, int *total)
278 ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
286 static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
288 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
295 static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
297 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
304 #define NOT_DONE 0x7fffffff
305 static void aio_rw_done(void *opaque, int ret)
307 *(int *)opaque = ret;
310 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
312 int async_ret = NOT_DONE;
314 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
315 aio_rw_done, &async_ret);
316 while (async_ret == NOT_DONE) {
317 main_loop_wait(false);
321 return async_ret < 0 ? async_ret : 1;
324 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
326 int async_ret = NOT_DONE;
328 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
329 aio_rw_done, &async_ret);
330 while (async_ret == NOT_DONE) {
331 main_loop_wait(false);
335 return async_ret < 0 ? async_ret : 1;
338 struct multiwrite_async_ret {
343 static void multiwrite_cb(void *opaque, int ret)
345 struct multiwrite_async_ret *async_ret = opaque;
347 async_ret->num_done++;
349 async_ret->error = ret;
353 static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total)
356 struct multiwrite_async_ret async_ret = {
362 for (i = 0; i < num_reqs; i++) {
363 reqs[i].cb = multiwrite_cb;
364 reqs[i].opaque = &async_ret;
365 *total += reqs[i].qiov->size;
368 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
373 while (async_ret.num_done < num_reqs) {
374 main_loop_wait(false);
377 return async_ret.error < 0 ? async_ret.error : 1;
380 static void read_help(void)
384 " reads a range of bytes from the given offset\n"
387 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
389 " Reads a segment of the currently open file, optionally dumping it to the\n"
390 " standard output stream (with -v option) for subsequent inspection.\n"
391 " -b, -- read from the VM state rather than the virtual disk\n"
392 " -C, -- report statistics in a machine parsable format\n"
393 " -l, -- length for pattern verification (only with -P)\n"
394 " -p, -- use bdrv_pread to read the file\n"
395 " -P, -- use a pattern to verify read data\n"
396 " -q, -- quiet mode, do not show I/O statistics\n"
397 " -s, -- start offset for pattern verification (only with -P)\n"
398 " -v, -- dump buffer to standard output\n"
402 static int read_f(int argc, char **argv);
404 static const cmdinfo_t read_cmd = {
410 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
411 .oneline = "reads a number of bytes at a specified offset",
415 static int read_f(int argc, char **argv)
417 struct timeval t1, t2;
418 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
419 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
424 /* Some compilers get confused and warn if this is not initialized. */
426 int pattern = 0, pattern_offset = 0, pattern_count = 0;
428 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
438 pattern_count = cvtnum(optarg);
439 if (pattern_count < 0) {
440 printf("non-numeric length argument -- %s\n", optarg);
449 pattern = parse_pattern(optarg);
459 pattern_offset = cvtnum(optarg);
460 if (pattern_offset < 0) {
461 printf("non-numeric length argument -- %s\n", optarg);
469 return command_usage(&read_cmd);
473 if (optind != argc - 2) {
474 return command_usage(&read_cmd);
477 if (bflag && pflag) {
478 printf("-b and -p cannot be specified at the same time\n");
482 offset = cvtnum(argv[optind]);
484 printf("non-numeric length argument -- %s\n", argv[optind]);
489 count = cvtnum(argv[optind]);
491 printf("non-numeric length argument -- %s\n", argv[optind]);
495 if (!Pflag && (lflag || sflag)) {
496 return command_usage(&read_cmd);
500 pattern_count = count - pattern_offset;
503 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
504 printf("pattern verification range exceeds end of read data\n");
509 if (offset & 0x1ff) {
510 printf("offset %" PRId64 " is not sector aligned\n",
515 printf("count %d is not sector aligned\n",
521 buf = qemu_io_alloc(count, 0xab);
523 gettimeofday(&t1, NULL);
525 cnt = do_pread(buf, offset, count, &total);
527 cnt = do_load_vmstate(buf, offset, count, &total);
529 cnt = do_read(buf, offset, count, &total);
531 gettimeofday(&t2, NULL);
534 printf("read failed: %s\n", strerror(-cnt));
539 void *cmp_buf = g_malloc(pattern_count);
540 memset(cmp_buf, pattern, pattern_count);
541 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
542 printf("Pattern verification failed at offset %"
543 PRId64 ", %d bytes\n",
544 offset + pattern_offset, pattern_count);
554 dump_buffer(buf, offset, count);
557 /* Finally, report back -- -C gives a parsable format */
559 print_report("read", &t2, offset, count, total, cnt, Cflag);
567 static void readv_help(void)
571 " reads a range of bytes from the given offset into multiple buffers\n"
574 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
576 " Reads a segment of the currently open file, optionally dumping it to the\n"
577 " standard output stream (with -v option) for subsequent inspection.\n"
578 " Uses multiple iovec buffers if more than one byte range is specified.\n"
579 " -C, -- report statistics in a machine parsable format\n"
580 " -P, -- use a pattern to verify read data\n"
581 " -v, -- dump buffer to standard output\n"
582 " -q, -- quiet mode, do not show I/O statistics\n"
586 static int readv_f(int argc, char **argv);
588 static const cmdinfo_t readv_cmd = {
593 .args = "[-Cqv] [-P pattern ] off len [len..]",
594 .oneline = "reads a number of bytes at a specified offset",
598 static int readv_f(int argc, char **argv)
600 struct timeval t1, t2;
601 int Cflag = 0, qflag = 0, vflag = 0;
605 /* Some compilers get confused and warn if this is not initialized. */
612 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
619 pattern = parse_pattern(optarg);
631 return command_usage(&readv_cmd);
635 if (optind > argc - 2) {
636 return command_usage(&readv_cmd);
640 offset = cvtnum(argv[optind]);
642 printf("non-numeric length argument -- %s\n", argv[optind]);
647 if (offset & 0x1ff) {
648 printf("offset %" PRId64 " is not sector aligned\n",
653 nr_iov = argc - optind;
654 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
659 gettimeofday(&t1, NULL);
660 cnt = do_aio_readv(&qiov, offset, &total);
661 gettimeofday(&t2, NULL);
664 printf("readv failed: %s\n", strerror(-cnt));
669 void *cmp_buf = g_malloc(qiov.size);
670 memset(cmp_buf, pattern, qiov.size);
671 if (memcmp(buf, cmp_buf, qiov.size)) {
672 printf("Pattern verification failed at offset %"
673 PRId64 ", %zd bytes\n", offset, qiov.size);
683 dump_buffer(buf, offset, qiov.size);
686 /* Finally, report back -- -C gives a parsable format */
688 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
691 qemu_iovec_destroy(&qiov);
696 static void write_help(void)
700 " writes a range of bytes from the given offset\n"
703 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
705 " Writes into a segment of the currently open file, using a buffer\n"
706 " filled with a set pattern (0xcdcdcdcd).\n"
707 " -b, -- write to the VM state rather than the virtual disk\n"
708 " -c, -- write compressed data with bdrv_write_compressed\n"
709 " -p, -- use bdrv_pwrite to write the file\n"
710 " -P, -- use different pattern to fill file\n"
711 " -C, -- report statistics in a machine parsable format\n"
712 " -q, -- quiet mode, do not show I/O statistics\n"
713 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
717 static int write_f(int argc, char **argv);
719 static const cmdinfo_t write_cmd = {
725 .args = "[-bcCpqz] [-P pattern ] off len",
726 .oneline = "writes a number of bytes at a specified offset",
730 static int write_f(int argc, char **argv)
732 struct timeval t1, t2;
733 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
739 /* Some compilers get confused and warn if this is not initialized. */
743 while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
759 pattern = parse_pattern(optarg);
771 return command_usage(&write_cmd);
775 if (optind != argc - 2) {
776 return command_usage(&write_cmd);
779 if (bflag + pflag + zflag > 1) {
780 printf("-b, -p, or -z cannot be specified at the same time\n");
784 if (zflag && Pflag) {
785 printf("-z and -P cannot be specified at the same time\n");
789 offset = cvtnum(argv[optind]);
791 printf("non-numeric length argument -- %s\n", argv[optind]);
796 count = cvtnum(argv[optind]);
798 printf("non-numeric length argument -- %s\n", argv[optind]);
803 if (offset & 0x1ff) {
804 printf("offset %" PRId64 " is not sector aligned\n",
810 printf("count %d is not sector aligned\n",
817 buf = qemu_io_alloc(count, pattern);
820 gettimeofday(&t1, NULL);
822 cnt = do_pwrite(buf, offset, count, &total);
824 cnt = do_save_vmstate(buf, offset, count, &total);
826 cnt = do_co_write_zeroes(offset, count, &total);
828 cnt = do_write_compressed(buf, offset, count, &total);
830 cnt = do_write(buf, offset, count, &total);
832 gettimeofday(&t2, NULL);
835 printf("write failed: %s\n", strerror(-cnt));
843 /* Finally, report back -- -C gives a parsable format */
845 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
860 " writes a range of bytes from the given offset source from multiple buffers\n"
863 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
865 " Writes into a segment of the currently open file, using a buffer\n"
866 " filled with a set pattern (0xcdcdcdcd).\n"
867 " -P, -- use different pattern to fill file\n"
868 " -C, -- report statistics in a machine parsable format\n"
869 " -q, -- quiet mode, do not show I/O statistics\n"
873 static int writev_f(int argc, char **argv);
875 static const cmdinfo_t writev_cmd = {
880 .args = "[-Cq] [-P pattern ] off len [len..]",
881 .oneline = "writes a number of bytes at a specified offset",
885 static int writev_f(int argc, char **argv)
887 struct timeval t1, t2;
888 int Cflag = 0, qflag = 0;
892 /* Some compilers get confused and warn if this is not initialized. */
898 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
907 pattern = parse_pattern(optarg);
913 return command_usage(&writev_cmd);
917 if (optind > argc - 2) {
918 return command_usage(&writev_cmd);
921 offset = cvtnum(argv[optind]);
923 printf("non-numeric length argument -- %s\n", argv[optind]);
928 if (offset & 0x1ff) {
929 printf("offset %" PRId64 " is not sector aligned\n",
934 nr_iov = argc - optind;
935 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
940 gettimeofday(&t1, NULL);
941 cnt = do_aio_writev(&qiov, offset, &total);
942 gettimeofday(&t2, NULL);
945 printf("writev failed: %s\n", strerror(-cnt));
953 /* Finally, report back -- -C gives a parsable format */
955 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
957 qemu_iovec_destroy(&qiov);
962 static void multiwrite_help(void)
966 " writes a range of bytes from the given offset source from multiple buffers,\n"
967 " in a batch of requests that may be merged by qemu\n"
970 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
971 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
973 " Writes into a segment of the currently open file, using a buffer\n"
974 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
975 " by one for each request contained in the multiwrite command.\n"
976 " -P, -- use different pattern to fill file\n"
977 " -C, -- report statistics in a machine parsable format\n"
978 " -q, -- quiet mode, do not show I/O statistics\n"
982 static int multiwrite_f(int argc, char **argv);
984 static const cmdinfo_t multiwrite_cmd = {
985 .name = "multiwrite",
986 .cfunc = multiwrite_f,
989 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
990 .oneline = "issues multiple write requests at once",
991 .help = multiwrite_help,
994 static int multiwrite_f(int argc, char **argv)
996 struct timeval t1, t2;
997 int Cflag = 0, qflag = 0;
1000 int64_t offset, first_offset = 0;
1001 /* Some compilers get confused and warn if this is not initialized. */
1006 QEMUIOVector *qiovs;
1010 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1019 pattern = parse_pattern(optarg);
1025 return command_usage(&writev_cmd);
1029 if (optind > argc - 2) {
1030 return command_usage(&writev_cmd);
1034 for (i = optind; i < argc; i++) {
1035 if (!strcmp(argv[i], ";")) {
1040 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1041 buf = g_malloc0(nr_reqs * sizeof(*buf));
1042 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
1044 for (i = 0; i < nr_reqs && optind < argc; i++) {
1047 /* Read the offset of the request */
1048 offset = cvtnum(argv[optind]);
1050 printf("non-numeric offset argument -- %s\n", argv[optind]);
1055 if (offset & 0x1ff) {
1056 printf("offset %lld is not sector aligned\n",
1062 first_offset = offset;
1065 /* Read lengths for qiov entries */
1066 for (j = optind; j < argc; j++) {
1067 if (!strcmp(argv[j], ";")) {
1072 nr_iov = j - optind;
1075 buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
1076 if (buf[i] == NULL) {
1080 reqs[i].qiov = &qiovs[i];
1081 reqs[i].sector = offset >> 9;
1082 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1089 /* If there were empty requests at the end, ignore them */
1092 gettimeofday(&t1, NULL);
1093 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1094 gettimeofday(&t2, NULL);
1097 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1105 /* Finally, report back -- -C gives a parsable format */
1107 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1109 for (i = 0; i < nr_reqs; i++) {
1110 qemu_io_free(buf[i]);
1111 if (reqs[i].qiov != NULL) {
1112 qemu_iovec_destroy(&qiovs[i]);
1133 static void aio_write_done(void *opaque, int ret)
1135 struct aio_ctx *ctx = opaque;
1138 gettimeofday(&t2, NULL);
1142 printf("aio_write failed: %s\n", strerror(-ret));
1150 /* Finally, report back -- -C gives a parsable format */
1151 t2 = tsub(t2, ctx->t1);
1152 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1153 ctx->qiov.size, 1, ctx->Cflag);
1155 qemu_io_free(ctx->buf);
1156 qemu_iovec_destroy(&ctx->qiov);
1160 static void aio_read_done(void *opaque, int ret)
1162 struct aio_ctx *ctx = opaque;
1165 gettimeofday(&t2, NULL);
1168 printf("readv failed: %s\n", strerror(-ret));
1173 void *cmp_buf = g_malloc(ctx->qiov.size);
1175 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1176 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1177 printf("Pattern verification failed at offset %"
1178 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1188 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1191 /* Finally, report back -- -C gives a parsable format */
1192 t2 = tsub(t2, ctx->t1);
1193 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1194 ctx->qiov.size, 1, ctx->Cflag);
1196 qemu_io_free(ctx->buf);
1197 qemu_iovec_destroy(&ctx->qiov);
1201 static void aio_read_help(void)
1205 " asynchronously reads a range of bytes from the given offset\n"
1208 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1210 " Reads a segment of the currently open file, optionally dumping it to the\n"
1211 " standard output stream (with -v option) for subsequent inspection.\n"
1212 " The read is performed asynchronously and the aio_flush command must be\n"
1213 " used to ensure all outstanding aio requests have been completed.\n"
1214 " -C, -- report statistics in a machine parsable format\n"
1215 " -P, -- use a pattern to verify read data\n"
1216 " -v, -- dump buffer to standard output\n"
1217 " -q, -- quiet mode, do not show I/O statistics\n"
1221 static int aio_read_f(int argc, char **argv);
1223 static const cmdinfo_t aio_read_cmd = {
1225 .cfunc = aio_read_f,
1228 .args = "[-Cqv] [-P pattern ] off len [len..]",
1229 .oneline = "asynchronously reads a number of bytes",
1230 .help = aio_read_help,
1233 static int aio_read_f(int argc, char **argv)
1236 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1238 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1245 ctx->pattern = parse_pattern(optarg);
1246 if (ctx->pattern < 0) {
1259 return command_usage(&aio_read_cmd);
1263 if (optind > argc - 2) {
1265 return command_usage(&aio_read_cmd);
1268 ctx->offset = cvtnum(argv[optind]);
1269 if (ctx->offset < 0) {
1270 printf("non-numeric length argument -- %s\n", argv[optind]);
1276 if (ctx->offset & 0x1ff) {
1277 printf("offset %" PRId64 " is not sector aligned\n",
1283 nr_iov = argc - optind;
1284 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
1285 if (ctx->buf == NULL) {
1290 gettimeofday(&ctx->t1, NULL);
1291 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1292 ctx->qiov.size >> 9, aio_read_done, ctx);
1296 static void aio_write_help(void)
1300 " asynchronously writes a range of bytes from the given offset source\n"
1301 " from multiple buffers\n"
1304 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1306 " Writes into a segment of the currently open file, using a buffer\n"
1307 " filled with a set pattern (0xcdcdcdcd).\n"
1308 " The write is performed asynchronously and the aio_flush command must be\n"
1309 " used to ensure all outstanding aio requests have been completed.\n"
1310 " -P, -- use different pattern to fill file\n"
1311 " -C, -- report statistics in a machine parsable format\n"
1312 " -q, -- quiet mode, do not show I/O statistics\n"
1316 static int aio_write_f(int argc, char **argv);
1318 static const cmdinfo_t aio_write_cmd = {
1319 .name = "aio_write",
1320 .cfunc = aio_write_f,
1323 .args = "[-Cq] [-P pattern ] off len [len..]",
1324 .oneline = "asynchronously writes a number of bytes",
1325 .help = aio_write_help,
1328 static int aio_write_f(int argc, char **argv)
1332 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1334 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1343 pattern = parse_pattern(optarg);
1351 return command_usage(&aio_write_cmd);
1355 if (optind > argc - 2) {
1357 return command_usage(&aio_write_cmd);
1360 ctx->offset = cvtnum(argv[optind]);
1361 if (ctx->offset < 0) {
1362 printf("non-numeric length argument -- %s\n", argv[optind]);
1368 if (ctx->offset & 0x1ff) {
1369 printf("offset %" PRId64 " is not sector aligned\n",
1375 nr_iov = argc - optind;
1376 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
1377 if (ctx->buf == NULL) {
1382 gettimeofday(&ctx->t1, NULL);
1383 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1384 ctx->qiov.size >> 9, aio_write_done, ctx);
1388 static int aio_flush_f(int argc, char **argv)
1394 static const cmdinfo_t aio_flush_cmd = {
1395 .name = "aio_flush",
1396 .cfunc = aio_flush_f,
1397 .oneline = "completes all outstanding aio requests"
1400 static int flush_f(int argc, char **argv)
1406 static const cmdinfo_t flush_cmd = {
1410 .oneline = "flush all in-core file state to disk",
1413 static int truncate_f(int argc, char **argv)
1418 offset = cvtnum(argv[1]);
1420 printf("non-numeric truncate argument -- %s\n", argv[1]);
1424 ret = bdrv_truncate(bs, offset);
1426 printf("truncate: %s\n", strerror(-ret));
1433 static const cmdinfo_t truncate_cmd = {
1436 .cfunc = truncate_f,
1440 .oneline = "truncates the current file at the given offset",
1443 static int length_f(int argc, char **argv)
1448 size = bdrv_getlength(bs);
1450 printf("getlength: %s\n", strerror(-size));
1454 cvtstr(size, s1, sizeof(s1));
1460 static const cmdinfo_t length_cmd = {
1464 .oneline = "gets the length of the current file",
1468 static int info_f(int argc, char **argv)
1470 BlockDriverInfo bdi;
1471 char s1[64], s2[64];
1474 if (bs->drv && bs->drv->format_name) {
1475 printf("format name: %s\n", bs->drv->format_name);
1477 if (bs->drv && bs->drv->protocol_name) {
1478 printf("format name: %s\n", bs->drv->protocol_name);
1481 ret = bdrv_get_info(bs, &bdi);
1486 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1487 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1489 printf("cluster size: %s\n", s1);
1490 printf("vm state offset: %s\n", s2);
1497 static const cmdinfo_t info_cmd = {
1501 .oneline = "prints information about the current file",
1504 static void discard_help(void)
1508 " discards a range of bytes from the given offset\n"
1511 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1513 " Discards a segment of the currently open file.\n"
1514 " -C, -- report statistics in a machine parsable format\n"
1515 " -q, -- quiet mode, do not show I/O statistics\n"
1519 static int discard_f(int argc, char **argv);
1521 static const cmdinfo_t discard_cmd = {
1527 .args = "[-Cq] off len",
1528 .oneline = "discards a number of bytes at a specified offset",
1529 .help = discard_help,
1532 static int discard_f(int argc, char **argv)
1534 struct timeval t1, t2;
1535 int Cflag = 0, qflag = 0;
1540 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1549 return command_usage(&discard_cmd);
1553 if (optind != argc - 2) {
1554 return command_usage(&discard_cmd);
1557 offset = cvtnum(argv[optind]);
1559 printf("non-numeric length argument -- %s\n", argv[optind]);
1564 count = cvtnum(argv[optind]);
1566 printf("non-numeric length argument -- %s\n", argv[optind]);
1570 gettimeofday(&t1, NULL);
1571 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1572 count >> BDRV_SECTOR_BITS);
1573 gettimeofday(&t2, NULL);
1576 printf("discard failed: %s\n", strerror(-ret));
1580 /* Finally, report back -- -C gives a parsable format */
1583 print_report("discard", &t2, offset, count, count, 1, Cflag);
1590 static int alloc_f(int argc, char **argv)
1592 int64_t offset, sector_num;
1593 int nb_sectors, remaining;
1598 offset = cvtnum(argv[1]);
1600 printf("non-numeric offset argument -- %s\n", argv[1]);
1602 } else if (offset & 0x1ff) {
1603 printf("offset %" PRId64 " is not sector aligned\n",
1609 nb_sectors = cvtnum(argv[2]);
1610 if (nb_sectors < 0) {
1611 printf("non-numeric length argument -- %s\n", argv[2]);
1618 remaining = nb_sectors;
1620 sector_num = offset >> 9;
1622 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1629 nb_sectors -= remaining;
1634 cvtstr(offset, s1, sizeof(s1));
1636 printf("%d/%d sectors allocated at offset %s\n",
1637 sum_alloc, nb_sectors, s1);
1641 static const cmdinfo_t alloc_cmd = {
1647 .args = "off [sectors]",
1648 .oneline = "checks if a sector is present in the file",
1652 static int map_is_allocated(int64_t sector_num, int64_t nb_sectors, int64_t *pnum)
1654 int num, num_checked;
1657 num_checked = MIN(nb_sectors, INT_MAX);
1658 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1666 while (nb_sectors > 0 && ret == firstret) {
1670 num_checked = MIN(nb_sectors, INT_MAX);
1671 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1672 if (ret == firstret) {
1682 static int map_f(int argc, char **argv)
1692 nb_sectors = bs->total_sectors;
1695 ret = map_is_allocated(offset, nb_sectors, &num);
1697 error_report("Failed to get allocation status: %s", strerror(-ret));
1701 retstr = ret ? " allocated" : "not allocated";
1702 cvtstr(offset << 9ULL, s1, sizeof(s1));
1703 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1704 "at offset %s (%d)\n",
1705 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1709 } while (offset < bs->total_sectors);
1714 static const cmdinfo_t map_cmd = {
1720 .oneline = "prints the allocated areas of a file",
1723 static int break_f(int argc, char **argv)
1727 ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
1729 printf("Could not set breakpoint: %s\n", strerror(-ret));
1735 static const cmdinfo_t break_cmd = {
1740 .args = "event tag",
1741 .oneline = "sets a breakpoint on event and tags the stopped "
1745 static int resume_f(int argc, char **argv)
1749 ret = bdrv_debug_resume(bs, argv[1]);
1751 printf("Could not resume request: %s\n", strerror(-ret));
1757 static const cmdinfo_t resume_cmd = {
1763 .oneline = "resumes the request tagged as tag",
1766 static int wait_break_f(int argc, char **argv)
1768 while (!bdrv_debug_is_suspended(bs, argv[1])) {
1775 static const cmdinfo_t wait_break_cmd = {
1776 .name = "wait_break",
1779 .cfunc = wait_break_f,
1781 .oneline = "waits for the suspension of a request",
1784 static int abort_f(int argc, char **argv)
1789 static const cmdinfo_t abort_cmd = {
1792 .flags = CMD_NOFILE_OK,
1793 .oneline = "simulate a program crash using abort(3)",
1796 static int close_f(int argc, char **argv)
1803 static const cmdinfo_t close_cmd = {
1807 .oneline = "close the current open file",
1810 static int openfile(char *name, int flags, int growable)
1813 fprintf(stderr, "file open already, try 'help close'\n");
1818 if (bdrv_file_open(&bs, name, NULL, flags)) {
1819 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1823 bs = bdrv_new("hda");
1825 if (bdrv_open(bs, name, NULL, flags, NULL) < 0) {
1826 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1836 static void open_help(void)
1840 " opens a new file in the requested mode\n"
1843 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1845 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1846 " -r, -- open file read-only\n"
1847 " -s, -- use snapshot file\n"
1848 " -n, -- disable host cache\n"
1849 " -g, -- allow file to grow (only applies to protocols)"
1853 static int open_f(int argc, char **argv);
1855 static const cmdinfo_t open_cmd = {
1861 .flags = CMD_NOFILE_OK,
1862 .args = "[-Crsn] [path]",
1863 .oneline = "open the file specified by path",
1867 static int open_f(int argc, char **argv)
1874 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1877 flags |= BDRV_O_SNAPSHOT;
1880 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1889 return command_usage(&open_cmd);
1894 flags |= BDRV_O_RDWR;
1897 if (optind != argc - 1) {
1898 return command_usage(&open_cmd);
1901 return openfile(argv[optind], flags, growable);
1904 static int init_check_command(const cmdinfo_t *ct)
1906 if (ct->flags & CMD_FLAG_GLOBAL) {
1909 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1910 fprintf(stderr, "no file open, try 'help open'\n");
1916 static void usage(const char *name)
1919 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
1920 "QEMU Disk exerciser\n"
1922 " -c, --cmd command to execute\n"
1923 " -r, --read-only export read-only\n"
1924 " -s, --snapshot use snapshot file\n"
1925 " -n, --nocache disable host cache\n"
1926 " -g, --growable allow file to grow (only applies to protocols)\n"
1927 " -m, --misalign misalign allocations for O_DIRECT\n"
1928 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1929 " -t, --cache=MODE use the given cache mode for the image\n"
1930 " -T, --trace FILE enable trace events listed in the given file\n"
1931 " -h, --help display this help and exit\n"
1932 " -V, --version output version information and exit\n"
1938 int main(int argc, char **argv)
1942 const char *sopt = "hVc:d:rsnmgkt:T:";
1943 const struct option lopt[] = {
1944 { "help", 0, NULL, 'h' },
1945 { "version", 0, NULL, 'V' },
1946 { "offset", 1, NULL, 'o' },
1947 { "cmd", 1, NULL, 'c' },
1948 { "read-only", 0, NULL, 'r' },
1949 { "snapshot", 0, NULL, 's' },
1950 { "nocache", 0, NULL, 'n' },
1951 { "misalign", 0, NULL, 'm' },
1952 { "growable", 0, NULL, 'g' },
1953 { "native-aio", 0, NULL, 'k' },
1954 { "discard", 1, NULL, 'd' },
1955 { "cache", 1, NULL, 't' },
1956 { "trace", 1, NULL, 'T' },
1957 { NULL, 0, NULL, 0 }
1961 int flags = BDRV_O_UNMAP;
1963 progname = basename(argv[0]);
1965 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1968 flags |= BDRV_O_SNAPSHOT;
1971 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1974 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
1975 error_report("Invalid discard option: %s", optarg);
1980 add_user_command(optarg);
1992 flags |= BDRV_O_NATIVE_AIO;
1995 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
1996 error_report("Invalid cache option: %s", optarg);
2001 if (!trace_backend_init(optarg, NULL)) {
2002 exit(1); /* error message will have been printed */
2006 printf("%s version %s\n", progname, VERSION);
2017 if ((argc - optind) > 1) {
2022 qemu_init_main_loop();
2025 /* initialize commands */
2028 add_command(&open_cmd);
2029 add_command(&close_cmd);
2030 add_command(&read_cmd);
2031 add_command(&readv_cmd);
2032 add_command(&write_cmd);
2033 add_command(&writev_cmd);
2034 add_command(&multiwrite_cmd);
2035 add_command(&aio_read_cmd);
2036 add_command(&aio_write_cmd);
2037 add_command(&aio_flush_cmd);
2038 add_command(&flush_cmd);
2039 add_command(&truncate_cmd);
2040 add_command(&length_cmd);
2041 add_command(&info_cmd);
2042 add_command(&discard_cmd);
2043 add_command(&alloc_cmd);
2044 add_command(&map_cmd);
2045 add_command(&break_cmd);
2046 add_command(&resume_cmd);
2047 add_command(&wait_break_cmd);
2048 add_command(&abort_cmd);
2050 add_check_command(init_check_command);
2052 /* open the device */
2054 flags |= BDRV_O_RDWR;
2057 if ((argc - optind) == 1) {
2058 openfile(argv[optind], flags, growable);
2063 * Make sure all outstanding requests complete before the program exits.