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.
12 #include "sysemu/block-backend.h"
13 #include "block/block.h"
14 #include "block/block_int.h" /* for info_f() */
15 #include "block/qapi.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "sysemu/block-backend.h"
21 #define CMD_NOFILE_OK 0x01
25 static cmdinfo_t *cmdtab;
28 static int compare_cmdname(const void *a, const void *b)
30 return strcmp(((const cmdinfo_t *)a)->name,
31 ((const cmdinfo_t *)b)->name);
34 void qemuio_add_command(const cmdinfo_t *ci)
36 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
37 cmdtab[ncmds - 1] = *ci;
38 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
41 int qemuio_command_usage(const cmdinfo_t *ci)
43 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
47 static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
49 if (ct->flags & CMD_FLAG_GLOBAL) {
52 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
53 fprintf(stderr, "no file open, try 'help open'\n");
59 static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
64 if (!init_check_command(blk, ct)) {
68 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
69 if (ct->argmax == -1) {
71 "bad argument count %d to %s, expected at least %d arguments\n",
72 argc-1, cmd, ct->argmin);
73 } else if (ct->argmin == ct->argmax) {
75 "bad argument count %d to %s, expected %d arguments\n",
76 argc-1, cmd, ct->argmin);
79 "bad argument count %d to %s, expected between %d and %d arguments\n",
80 argc-1, cmd, ct->argmin, ct->argmax);
85 return ct->cfunc(blk, argc, argv);
88 static const cmdinfo_t *find_command(const char *cmd)
92 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
93 if (strcmp(ct->name, cmd) == 0 ||
94 (ct->altname && strcmp(ct->altname, cmd) == 0))
96 return (const cmdinfo_t *)ct;
102 /* Invoke fn() for commands with a matching prefix */
103 void qemuio_complete_command(const char *input,
104 void (*fn)(const char *cmd, void *opaque),
108 size_t input_len = strlen(input);
110 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
111 if (strncmp(input, ct->name, input_len) == 0) {
112 fn(ct->name, opaque);
117 static char **breakline(char *input, int *count)
121 char **rval = g_new0(char *, 1);
123 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
128 rval = g_renew(char *, rval, (c + 1));
136 static int64_t cvtnum(const char *s)
139 return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
142 #define EXABYTES(x) ((long long)(x) << 60)
143 #define PETABYTES(x) ((long long)(x) << 50)
144 #define TERABYTES(x) ((long long)(x) << 40)
145 #define GIGABYTES(x) ((long long)(x) << 30)
146 #define MEGABYTES(x) ((long long)(x) << 20)
147 #define KILOBYTES(x) ((long long)(x) << 10)
149 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
150 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
151 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
152 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
153 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
154 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
156 static void cvtstr(double value, char *str, size_t size)
161 if (value >= EXABYTES(1)) {
163 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
164 } else if (value >= PETABYTES(1)) {
166 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
167 } else if (value >= TERABYTES(1)) {
169 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
170 } else if (value >= GIGABYTES(1)) {
172 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
173 } else if (value >= MEGABYTES(1)) {
175 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
176 } else if (value >= KILOBYTES(1)) {
178 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
181 snprintf(str, size - 6, "%f", value);
184 trim = strstr(str, ".000");
186 strcpy(trim, suffix);
194 static struct timeval tsub(struct timeval t1, struct timeval t2)
196 t1.tv_usec -= t2.tv_usec;
197 if (t1.tv_usec < 0) {
198 t1.tv_usec += 1000000;
201 t1.tv_sec -= t2.tv_sec;
205 static double tdiv(double value, struct timeval tv)
207 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
210 #define HOURS(sec) ((sec) / (60 * 60))
211 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
212 #define SECONDS(sec) ((sec) % 60)
216 TERSE_FIXED_TIME = 0x1,
217 VERBOSE_FIXED_TIME = 0x2,
220 static void timestr(struct timeval *tv, char *ts, size_t size, int format)
222 double usec = (double)tv->tv_usec / 1000000.0;
224 if (format & TERSE_FIXED_TIME) {
225 if (!HOURS(tv->tv_sec)) {
226 snprintf(ts, size, "%u:%02u.%02u",
227 (unsigned int) MINUTES(tv->tv_sec),
228 (unsigned int) SECONDS(tv->tv_sec),
229 (unsigned int) (usec * 100));
232 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
235 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
236 snprintf(ts, size, "%u:%02u:%02u.%02u",
237 (unsigned int) HOURS(tv->tv_sec),
238 (unsigned int) MINUTES(tv->tv_sec),
239 (unsigned int) SECONDS(tv->tv_sec),
240 (unsigned int) (usec * 100));
242 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
247 * Parse the pattern argument to various sub-commands.
249 * Because the pattern is used as an argument to memset it must evaluate
250 * to an unsigned integer that fits into a single byte.
252 static int parse_pattern(const char *arg)
257 pattern = strtol(arg, &endptr, 0);
258 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
259 printf("%s is not a valid pattern byte\n", arg);
267 * Memory allocation helpers.
269 * Make sure memory is aligned by default, or purposefully misaligned if
270 * that is specified on the command line.
273 #define MISALIGN_OFFSET 16
274 static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
278 if (qemuio_misalign) {
279 len += MISALIGN_OFFSET;
281 buf = blk_blockalign(blk, len);
282 memset(buf, pattern, len);
283 if (qemuio_misalign) {
284 buf += MISALIGN_OFFSET;
289 static void qemu_io_free(void *p)
291 if (qemuio_misalign) {
292 p -= MISALIGN_OFFSET;
297 static void dump_buffer(const void *buffer, int64_t offset, int len)
302 for (i = 0, p = buffer; i < len; i += 16) {
303 const uint8_t *s = p;
305 printf("%08" PRIx64 ": ", offset + i);
306 for (j = 0; j < 16 && i + j < len; j++, p++) {
310 for (j = 0; j < 16 && i + j < len; j++, s++) {
321 static void print_report(const char *op, struct timeval *t, int64_t offset,
322 int count, int total, int cnt, int Cflag)
324 char s1[64], s2[64], ts[64];
326 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
328 cvtstr((double)total, s1, sizeof(s1));
329 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
330 printf("%s %d/%d bytes at offset %" PRId64 "\n",
331 op, total, count, offset);
332 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
333 s1, cnt, ts, s2, tdiv((double)cnt, *t));
334 } else {/* bytes,ops,time,bytes/sec,ops/sec */
335 printf("%d,%d,%s,%.3f,%.3f\n",
337 tdiv((double)total, *t),
338 tdiv((double)cnt, *t));
343 * Parse multiple length statements for vectored I/O, and construct an I/O
344 * vector matching it.
347 create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
350 size_t *sizes = g_new0(size_t, nr_iov);
356 for (i = 0; i < nr_iov; i++) {
362 printf("non-numeric length argument -- %s\n", arg);
366 /* should be SIZE_T_MAX, but that doesn't exist */
368 printf("too large length argument -- %s\n", arg);
373 printf("length argument %" PRId64
374 " is not sector aligned\n", len);
382 qemu_iovec_init(qiov, nr_iov);
384 buf = p = qemu_io_alloc(blk, count, pattern);
386 for (i = 0; i < nr_iov; i++) {
387 qemu_iovec_add(qiov, p, sizes[i]);
396 static int do_read(BlockBackend *blk, char *buf, int64_t offset, int count,
401 ret = blk_read(blk, offset >> 9, (uint8_t *)buf, count >> 9);
409 static int do_write(BlockBackend *blk, char *buf, int64_t offset, int count,
414 ret = blk_write(blk, offset >> 9, (uint8_t *)buf, count >> 9);
422 static int do_pread(BlockBackend *blk, char *buf, int64_t offset, int count,
425 *total = blk_pread(blk, offset, (uint8_t *)buf, count);
432 static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset, int count,
435 *total = blk_pwrite(blk, offset, (uint8_t *)buf, count);
451 static void coroutine_fn co_write_zeroes_entry(void *opaque)
453 CoWriteZeroes *data = opaque;
455 data->ret = blk_co_write_zeroes(data->blk, data->offset / BDRV_SECTOR_SIZE,
456 data->count / BDRV_SECTOR_SIZE, 0);
459 *data->total = data->ret;
463 *data->total = data->count;
466 static int do_co_write_zeroes(BlockBackend *blk, int64_t offset, int count,
470 CoWriteZeroes data = {
478 co = qemu_coroutine_create(co_write_zeroes_entry);
479 qemu_coroutine_enter(co, &data);
481 aio_poll(blk_get_aio_context(blk), true);
490 static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
491 int count, int *total)
495 ret = blk_write_compressed(blk, offset >> 9, (uint8_t *)buf, count >> 9);
503 static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
504 int count, int *total)
506 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
513 static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
514 int count, int *total)
516 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
523 #define NOT_DONE 0x7fffffff
524 static void aio_rw_done(void *opaque, int ret)
526 *(int *)opaque = ret;
529 static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
530 int64_t offset, int *total)
532 int async_ret = NOT_DONE;
534 blk_aio_readv(blk, offset >> 9, qiov, qiov->size >> 9,
535 aio_rw_done, &async_ret);
536 while (async_ret == NOT_DONE) {
537 main_loop_wait(false);
541 return async_ret < 0 ? async_ret : 1;
544 static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
545 int64_t offset, int *total)
547 int async_ret = NOT_DONE;
549 blk_aio_writev(blk, offset >> 9, qiov, qiov->size >> 9,
550 aio_rw_done, &async_ret);
551 while (async_ret == NOT_DONE) {
552 main_loop_wait(false);
556 return async_ret < 0 ? async_ret : 1;
559 struct multiwrite_async_ret {
564 static void multiwrite_cb(void *opaque, int ret)
566 struct multiwrite_async_ret *async_ret = opaque;
568 async_ret->num_done++;
570 async_ret->error = ret;
574 static int do_aio_multiwrite(BlockBackend *blk, BlockRequest* reqs,
575 int num_reqs, int *total)
578 struct multiwrite_async_ret async_ret = {
584 for (i = 0; i < num_reqs; i++) {
585 reqs[i].cb = multiwrite_cb;
586 reqs[i].opaque = &async_ret;
587 *total += reqs[i].qiov->size;
590 ret = blk_aio_multiwrite(blk, reqs, num_reqs);
595 while (async_ret.num_done < num_reqs) {
596 main_loop_wait(false);
599 return async_ret.error < 0 ? async_ret.error : 1;
602 static void read_help(void)
606 " reads a range of bytes from the given offset\n"
609 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
611 " Reads a segment of the currently open file, optionally dumping it to the\n"
612 " standard output stream (with -v option) for subsequent inspection.\n"
613 " -b, -- read from the VM state rather than the virtual disk\n"
614 " -C, -- report statistics in a machine parsable format\n"
615 " -l, -- length for pattern verification (only with -P)\n"
616 " -p, -- use blk_pread to read the file\n"
617 " -P, -- use a pattern to verify read data\n"
618 " -q, -- quiet mode, do not show I/O statistics\n"
619 " -s, -- start offset for pattern verification (only with -P)\n"
620 " -v, -- dump buffer to standard output\n"
624 static int read_f(BlockBackend *blk, int argc, char **argv);
626 static const cmdinfo_t read_cmd = {
632 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
633 .oneline = "reads a number of bytes at a specified offset",
637 static int read_f(BlockBackend *blk, int argc, char **argv)
639 struct timeval t1, t2;
640 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
641 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
646 /* Some compilers get confused and warn if this is not initialized. */
648 int pattern = 0, pattern_offset = 0, pattern_count = 0;
650 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
660 pattern_count = cvtnum(optarg);
661 if (pattern_count < 0) {
662 printf("non-numeric length argument -- %s\n", optarg);
671 pattern = parse_pattern(optarg);
681 pattern_offset = cvtnum(optarg);
682 if (pattern_offset < 0) {
683 printf("non-numeric length argument -- %s\n", optarg);
691 return qemuio_command_usage(&read_cmd);
695 if (optind != argc - 2) {
696 return qemuio_command_usage(&read_cmd);
699 if (bflag && pflag) {
700 printf("-b and -p cannot be specified at the same time\n");
704 offset = cvtnum(argv[optind]);
706 printf("non-numeric length argument -- %s\n", argv[optind]);
711 count = cvtnum(argv[optind]);
713 printf("non-numeric length argument -- %s\n", argv[optind]);
717 if (!Pflag && (lflag || sflag)) {
718 return qemuio_command_usage(&read_cmd);
722 pattern_count = count - pattern_offset;
725 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
726 printf("pattern verification range exceeds end of read data\n");
731 if (offset & 0x1ff) {
732 printf("offset %" PRId64 " is not sector aligned\n",
737 printf("count %d is not sector aligned\n",
743 buf = qemu_io_alloc(blk, count, 0xab);
745 gettimeofday(&t1, NULL);
747 cnt = do_pread(blk, buf, offset, count, &total);
749 cnt = do_load_vmstate(blk, buf, offset, count, &total);
751 cnt = do_read(blk, buf, offset, count, &total);
753 gettimeofday(&t2, NULL);
756 printf("read failed: %s\n", strerror(-cnt));
761 void *cmp_buf = g_malloc(pattern_count);
762 memset(cmp_buf, pattern, pattern_count);
763 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
764 printf("Pattern verification failed at offset %"
765 PRId64 ", %d bytes\n",
766 offset + pattern_offset, pattern_count);
776 dump_buffer(buf, offset, count);
779 /* Finally, report back -- -C gives a parsable format */
781 print_report("read", &t2, offset, count, total, cnt, Cflag);
789 static void readv_help(void)
793 " reads a range of bytes from the given offset into multiple buffers\n"
796 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
798 " Reads a segment of the currently open file, optionally dumping it to the\n"
799 " standard output stream (with -v option) for subsequent inspection.\n"
800 " Uses multiple iovec buffers if more than one byte range is specified.\n"
801 " -C, -- report statistics in a machine parsable format\n"
802 " -P, -- use a pattern to verify read data\n"
803 " -v, -- dump buffer to standard output\n"
804 " -q, -- quiet mode, do not show I/O statistics\n"
808 static int readv_f(BlockBackend *blk, int argc, char **argv);
810 static const cmdinfo_t readv_cmd = {
815 .args = "[-Cqv] [-P pattern ] off len [len..]",
816 .oneline = "reads a number of bytes at a specified offset",
820 static int readv_f(BlockBackend *blk, int argc, char **argv)
822 struct timeval t1, t2;
823 int Cflag = 0, qflag = 0, vflag = 0;
827 /* Some compilers get confused and warn if this is not initialized. */
834 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
841 pattern = parse_pattern(optarg);
853 return qemuio_command_usage(&readv_cmd);
857 if (optind > argc - 2) {
858 return qemuio_command_usage(&readv_cmd);
862 offset = cvtnum(argv[optind]);
864 printf("non-numeric length argument -- %s\n", argv[optind]);
869 if (offset & 0x1ff) {
870 printf("offset %" PRId64 " is not sector aligned\n",
875 nr_iov = argc - optind;
876 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
881 gettimeofday(&t1, NULL);
882 cnt = do_aio_readv(blk, &qiov, offset, &total);
883 gettimeofday(&t2, NULL);
886 printf("readv failed: %s\n", strerror(-cnt));
891 void *cmp_buf = g_malloc(qiov.size);
892 memset(cmp_buf, pattern, qiov.size);
893 if (memcmp(buf, cmp_buf, qiov.size)) {
894 printf("Pattern verification failed at offset %"
895 PRId64 ", %zd bytes\n", offset, qiov.size);
905 dump_buffer(buf, offset, qiov.size);
908 /* Finally, report back -- -C gives a parsable format */
910 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
913 qemu_iovec_destroy(&qiov);
918 static void write_help(void)
922 " writes a range of bytes from the given offset\n"
925 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
927 " Writes into a segment of the currently open file, using a buffer\n"
928 " filled with a set pattern (0xcdcdcdcd).\n"
929 " -b, -- write to the VM state rather than the virtual disk\n"
930 " -c, -- write compressed data with blk_write_compressed\n"
931 " -p, -- use blk_pwrite to write the file\n"
932 " -P, -- use different pattern to fill file\n"
933 " -C, -- report statistics in a machine parsable format\n"
934 " -q, -- quiet mode, do not show I/O statistics\n"
935 " -z, -- write zeroes using blk_co_write_zeroes\n"
939 static int write_f(BlockBackend *blk, int argc, char **argv);
941 static const cmdinfo_t write_cmd = {
947 .args = "[-bcCpqz] [-P pattern ] off len",
948 .oneline = "writes a number of bytes at a specified offset",
952 static int write_f(BlockBackend *blk, int argc, char **argv)
954 struct timeval t1, t2;
955 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
961 /* Some compilers get confused and warn if this is not initialized. */
965 while ((c = getopt(argc, argv, "bcCpP:qz")) != -1) {
981 pattern = parse_pattern(optarg);
993 return qemuio_command_usage(&write_cmd);
997 if (optind != argc - 2) {
998 return qemuio_command_usage(&write_cmd);
1001 if (bflag + pflag + zflag > 1) {
1002 printf("-b, -p, or -z cannot be specified at the same time\n");
1006 if (zflag && Pflag) {
1007 printf("-z and -P cannot be specified at the same time\n");
1011 offset = cvtnum(argv[optind]);
1013 printf("non-numeric length argument -- %s\n", argv[optind]);
1018 count = cvtnum(argv[optind]);
1020 printf("non-numeric length argument -- %s\n", argv[optind]);
1025 if (offset & 0x1ff) {
1026 printf("offset %" PRId64 " is not sector aligned\n",
1031 if (count & 0x1ff) {
1032 printf("count %d is not sector aligned\n",
1039 buf = qemu_io_alloc(blk, count, pattern);
1042 gettimeofday(&t1, NULL);
1044 cnt = do_pwrite(blk, buf, offset, count, &total);
1046 cnt = do_save_vmstate(blk, buf, offset, count, &total);
1048 cnt = do_co_write_zeroes(blk, offset, count, &total);
1050 cnt = do_write_compressed(blk, buf, offset, count, &total);
1052 cnt = do_write(blk, buf, offset, count, &total);
1054 gettimeofday(&t2, NULL);
1057 printf("write failed: %s\n", strerror(-cnt));
1065 /* Finally, report back -- -C gives a parsable format */
1067 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1082 " writes a range of bytes from the given offset source from multiple buffers\n"
1085 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1087 " Writes into a segment of the currently open file, using a buffer\n"
1088 " filled with a set pattern (0xcdcdcdcd).\n"
1089 " -P, -- use different pattern to fill file\n"
1090 " -C, -- report statistics in a machine parsable format\n"
1091 " -q, -- quiet mode, do not show I/O statistics\n"
1095 static int writev_f(BlockBackend *blk, int argc, char **argv);
1097 static const cmdinfo_t writev_cmd = {
1102 .args = "[-Cq] [-P pattern ] off len [len..]",
1103 .oneline = "writes a number of bytes at a specified offset",
1104 .help = writev_help,
1107 static int writev_f(BlockBackend *blk, int argc, char **argv)
1109 struct timeval t1, t2;
1110 int Cflag = 0, qflag = 0;
1114 /* Some compilers get confused and warn if this is not initialized. */
1120 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1129 pattern = parse_pattern(optarg);
1135 return qemuio_command_usage(&writev_cmd);
1139 if (optind > argc - 2) {
1140 return qemuio_command_usage(&writev_cmd);
1143 offset = cvtnum(argv[optind]);
1145 printf("non-numeric length argument -- %s\n", argv[optind]);
1150 if (offset & 0x1ff) {
1151 printf("offset %" PRId64 " is not sector aligned\n",
1156 nr_iov = argc - optind;
1157 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1162 gettimeofday(&t1, NULL);
1163 cnt = do_aio_writev(blk, &qiov, offset, &total);
1164 gettimeofday(&t2, NULL);
1167 printf("writev failed: %s\n", strerror(-cnt));
1175 /* Finally, report back -- -C gives a parsable format */
1177 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1179 qemu_iovec_destroy(&qiov);
1184 static void multiwrite_help(void)
1188 " writes a range of bytes from the given offset source from multiple buffers,\n"
1189 " in a batch of requests that may be merged by qemu\n"
1192 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1193 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1195 " Writes into a segment of the currently open file, using a buffer\n"
1196 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1197 " by one for each request contained in the multiwrite command.\n"
1198 " -P, -- use different pattern to fill file\n"
1199 " -C, -- report statistics in a machine parsable format\n"
1200 " -q, -- quiet mode, do not show I/O statistics\n"
1204 static int multiwrite_f(BlockBackend *blk, int argc, char **argv);
1206 static const cmdinfo_t multiwrite_cmd = {
1207 .name = "multiwrite",
1208 .cfunc = multiwrite_f,
1211 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1212 .oneline = "issues multiple write requests at once",
1213 .help = multiwrite_help,
1216 static int multiwrite_f(BlockBackend *blk, int argc, char **argv)
1218 struct timeval t1, t2;
1219 int Cflag = 0, qflag = 0;
1222 int64_t offset, first_offset = 0;
1223 /* Some compilers get confused and warn if this is not initialized. */
1228 QEMUIOVector *qiovs;
1232 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1241 pattern = parse_pattern(optarg);
1247 return qemuio_command_usage(&writev_cmd);
1251 if (optind > argc - 2) {
1252 return qemuio_command_usage(&writev_cmd);
1256 for (i = optind; i < argc; i++) {
1257 if (!strcmp(argv[i], ";")) {
1262 reqs = g_new0(BlockRequest, nr_reqs);
1263 buf = g_new0(char *, nr_reqs);
1264 qiovs = g_new(QEMUIOVector, nr_reqs);
1266 for (i = 0; i < nr_reqs && optind < argc; i++) {
1269 /* Read the offset of the request */
1270 offset = cvtnum(argv[optind]);
1272 printf("non-numeric offset argument -- %s\n", argv[optind]);
1277 if (offset & 0x1ff) {
1278 printf("offset %lld is not sector aligned\n",
1284 first_offset = offset;
1287 /* Read lengths for qiov entries */
1288 for (j = optind; j < argc; j++) {
1289 if (!strcmp(argv[j], ";")) {
1294 nr_iov = j - optind;
1297 buf[i] = create_iovec(blk, &qiovs[i], &argv[optind], nr_iov, pattern);
1298 if (buf[i] == NULL) {
1302 reqs[i].qiov = &qiovs[i];
1303 reqs[i].sector = offset >> 9;
1304 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1311 /* If there were empty requests at the end, ignore them */
1314 gettimeofday(&t1, NULL);
1315 cnt = do_aio_multiwrite(blk, reqs, nr_reqs, &total);
1316 gettimeofday(&t2, NULL);
1319 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1327 /* Finally, report back -- -C gives a parsable format */
1329 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1331 for (i = 0; i < nr_reqs; i++) {
1332 qemu_io_free(buf[i]);
1333 if (reqs[i].qiov != NULL) {
1334 qemu_iovec_destroy(&qiovs[i]);
1352 BlockAcctCookie acct;
1357 static void aio_write_done(void *opaque, int ret)
1359 struct aio_ctx *ctx = opaque;
1362 gettimeofday(&t2, NULL);
1366 printf("aio_write failed: %s\n", strerror(-ret));
1370 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1376 /* Finally, report back -- -C gives a parsable format */
1377 t2 = tsub(t2, ctx->t1);
1378 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1379 ctx->qiov.size, 1, ctx->Cflag);
1381 qemu_io_free(ctx->buf);
1382 qemu_iovec_destroy(&ctx->qiov);
1386 static void aio_read_done(void *opaque, int ret)
1388 struct aio_ctx *ctx = opaque;
1391 gettimeofday(&t2, NULL);
1394 printf("readv failed: %s\n", strerror(-ret));
1399 void *cmp_buf = g_malloc(ctx->qiov.size);
1401 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1402 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1403 printf("Pattern verification failed at offset %"
1404 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1409 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1416 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1419 /* Finally, report back -- -C gives a parsable format */
1420 t2 = tsub(t2, ctx->t1);
1421 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1422 ctx->qiov.size, 1, ctx->Cflag);
1424 qemu_io_free(ctx->buf);
1425 qemu_iovec_destroy(&ctx->qiov);
1429 static void aio_read_help(void)
1433 " asynchronously reads a range of bytes from the given offset\n"
1436 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1438 " Reads a segment of the currently open file, optionally dumping it to the\n"
1439 " standard output stream (with -v option) for subsequent inspection.\n"
1440 " The read is performed asynchronously and the aio_flush command must be\n"
1441 " used to ensure all outstanding aio requests have been completed.\n"
1442 " -C, -- report statistics in a machine parsable format\n"
1443 " -P, -- use a pattern to verify read data\n"
1444 " -v, -- dump buffer to standard output\n"
1445 " -q, -- quiet mode, do not show I/O statistics\n"
1449 static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1451 static const cmdinfo_t aio_read_cmd = {
1453 .cfunc = aio_read_f,
1456 .args = "[-Cqv] [-P pattern ] off len [len..]",
1457 .oneline = "asynchronously reads a number of bytes",
1458 .help = aio_read_help,
1461 static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1464 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1467 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
1474 ctx->pattern = parse_pattern(optarg);
1475 if (ctx->pattern < 0) {
1488 return qemuio_command_usage(&aio_read_cmd);
1492 if (optind > argc - 2) {
1494 return qemuio_command_usage(&aio_read_cmd);
1497 ctx->offset = cvtnum(argv[optind]);
1498 if (ctx->offset < 0) {
1499 printf("non-numeric length argument -- %s\n", argv[optind]);
1505 if (ctx->offset & 0x1ff) {
1506 printf("offset %" PRId64 " is not sector aligned\n",
1512 nr_iov = argc - optind;
1513 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1514 if (ctx->buf == NULL) {
1519 gettimeofday(&ctx->t1, NULL);
1520 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1522 blk_aio_readv(blk, ctx->offset >> 9, &ctx->qiov,
1523 ctx->qiov.size >> 9, aio_read_done, ctx);
1527 static void aio_write_help(void)
1531 " asynchronously writes a range of bytes from the given offset source\n"
1532 " from multiple buffers\n"
1535 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1537 " Writes into a segment of the currently open file, using a buffer\n"
1538 " filled with a set pattern (0xcdcdcdcd).\n"
1539 " The write is performed asynchronously and the aio_flush command must be\n"
1540 " used to ensure all outstanding aio requests have been completed.\n"
1541 " -P, -- use different pattern to fill file\n"
1542 " -C, -- report statistics in a machine parsable format\n"
1543 " -q, -- quiet mode, do not show I/O statistics\n"
1547 static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1549 static const cmdinfo_t aio_write_cmd = {
1550 .name = "aio_write",
1551 .cfunc = aio_write_f,
1554 .args = "[-Cq] [-P pattern ] off len [len..]",
1555 .oneline = "asynchronously writes a number of bytes",
1556 .help = aio_write_help,
1559 static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1563 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1566 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1575 pattern = parse_pattern(optarg);
1583 return qemuio_command_usage(&aio_write_cmd);
1587 if (optind > argc - 2) {
1589 return qemuio_command_usage(&aio_write_cmd);
1592 ctx->offset = cvtnum(argv[optind]);
1593 if (ctx->offset < 0) {
1594 printf("non-numeric length argument -- %s\n", argv[optind]);
1600 if (ctx->offset & 0x1ff) {
1601 printf("offset %" PRId64 " is not sector aligned\n",
1607 nr_iov = argc - optind;
1608 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, pattern);
1609 if (ctx->buf == NULL) {
1614 gettimeofday(&ctx->t1, NULL);
1615 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1617 blk_aio_writev(blk, ctx->offset >> 9, &ctx->qiov,
1618 ctx->qiov.size >> 9, aio_write_done, ctx);
1622 static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1628 static const cmdinfo_t aio_flush_cmd = {
1629 .name = "aio_flush",
1630 .cfunc = aio_flush_f,
1631 .oneline = "completes all outstanding aio requests"
1634 static int flush_f(BlockBackend *blk, int argc, char **argv)
1640 static const cmdinfo_t flush_cmd = {
1644 .oneline = "flush all in-core file state to disk",
1647 static int truncate_f(BlockBackend *blk, int argc, char **argv)
1652 offset = cvtnum(argv[1]);
1654 printf("non-numeric truncate argument -- %s\n", argv[1]);
1658 ret = blk_truncate(blk, offset);
1660 printf("truncate: %s\n", strerror(-ret));
1667 static const cmdinfo_t truncate_cmd = {
1670 .cfunc = truncate_f,
1674 .oneline = "truncates the current file at the given offset",
1677 static int length_f(BlockBackend *blk, int argc, char **argv)
1682 size = blk_getlength(blk);
1684 printf("getlength: %s\n", strerror(-size));
1688 cvtstr(size, s1, sizeof(s1));
1694 static const cmdinfo_t length_cmd = {
1698 .oneline = "gets the length of the current file",
1702 static int info_f(BlockBackend *blk, int argc, char **argv)
1704 BlockDriverState *bs = blk_bs(blk);
1705 BlockDriverInfo bdi;
1706 ImageInfoSpecific *spec_info;
1707 char s1[64], s2[64];
1710 if (bs->drv && bs->drv->format_name) {
1711 printf("format name: %s\n", bs->drv->format_name);
1713 if (bs->drv && bs->drv->protocol_name) {
1714 printf("format name: %s\n", bs->drv->protocol_name);
1717 ret = bdrv_get_info(bs, &bdi);
1722 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1723 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1725 printf("cluster size: %s\n", s1);
1726 printf("vm state offset: %s\n", s2);
1728 spec_info = bdrv_get_specific_info(bs);
1730 printf("Format specific information:\n");
1731 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1732 qapi_free_ImageInfoSpecific(spec_info);
1740 static const cmdinfo_t info_cmd = {
1744 .oneline = "prints information about the current file",
1747 static void discard_help(void)
1751 " discards a range of bytes from the given offset\n"
1754 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1756 " Discards a segment of the currently open file.\n"
1757 " -C, -- report statistics in a machine parsable format\n"
1758 " -q, -- quiet mode, do not show I/O statistics\n"
1762 static int discard_f(BlockBackend *blk, int argc, char **argv);
1764 static const cmdinfo_t discard_cmd = {
1770 .args = "[-Cq] off len",
1771 .oneline = "discards a number of bytes at a specified offset",
1772 .help = discard_help,
1775 static int discard_f(BlockBackend *blk, int argc, char **argv)
1777 struct timeval t1, t2;
1778 int Cflag = 0, qflag = 0;
1783 while ((c = getopt(argc, argv, "Cq")) != -1) {
1792 return qemuio_command_usage(&discard_cmd);
1796 if (optind != argc - 2) {
1797 return qemuio_command_usage(&discard_cmd);
1800 offset = cvtnum(argv[optind]);
1802 printf("non-numeric length argument -- %s\n", argv[optind]);
1807 count = cvtnum(argv[optind]);
1809 printf("non-numeric length argument -- %s\n", argv[optind]);
1813 gettimeofday(&t1, NULL);
1814 ret = blk_discard(blk, offset >> BDRV_SECTOR_BITS,
1815 count >> BDRV_SECTOR_BITS);
1816 gettimeofday(&t2, NULL);
1819 printf("discard failed: %s\n", strerror(-ret));
1823 /* Finally, report back -- -C gives a parsable format */
1826 print_report("discard", &t2, offset, count, count, 1, Cflag);
1833 static int alloc_f(BlockBackend *blk, int argc, char **argv)
1835 BlockDriverState *bs = blk_bs(blk);
1836 int64_t offset, sector_num;
1837 int nb_sectors, remaining;
1842 offset = cvtnum(argv[1]);
1844 printf("non-numeric offset argument -- %s\n", argv[1]);
1846 } else if (offset & 0x1ff) {
1847 printf("offset %" PRId64 " is not sector aligned\n",
1853 nb_sectors = cvtnum(argv[2]);
1854 if (nb_sectors < 0) {
1855 printf("non-numeric length argument -- %s\n", argv[2]);
1862 remaining = nb_sectors;
1864 sector_num = offset >> 9;
1866 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1868 printf("is_allocated failed: %s\n", strerror(-ret));
1877 nb_sectors -= remaining;
1882 cvtstr(offset, s1, sizeof(s1));
1884 printf("%d/%d sectors allocated at offset %s\n",
1885 sum_alloc, nb_sectors, s1);
1889 static const cmdinfo_t alloc_cmd = {
1895 .args = "off [sectors]",
1896 .oneline = "checks if a sector is present in the file",
1900 static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1901 int64_t nb_sectors, int64_t *pnum)
1903 int num, num_checked;
1906 num_checked = MIN(nb_sectors, INT_MAX);
1907 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1915 while (nb_sectors > 0 && ret == firstret) {
1919 num_checked = MIN(nb_sectors, INT_MAX);
1920 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1921 if (ret == firstret && num) {
1931 static int map_f(BlockBackend *blk, int argc, char **argv)
1934 int64_t nb_sectors, total_sectors;
1941 total_sectors = blk_nb_sectors(blk);
1942 if (total_sectors < 0) {
1943 error_report("Failed to query image length: %s",
1944 strerror(-total_sectors));
1948 nb_sectors = total_sectors;
1951 ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
1953 error_report("Failed to get allocation status: %s", strerror(-ret));
1956 error_report("Unexpected end of image");
1960 retstr = ret ? " allocated" : "not allocated";
1961 cvtstr(offset << 9ULL, s1, sizeof(s1));
1962 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1963 "at offset %s (%d)\n",
1964 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1968 } while (offset < total_sectors);
1973 static const cmdinfo_t map_cmd = {
1979 .oneline = "prints the allocated areas of a file",
1982 static int break_f(BlockBackend *blk, int argc, char **argv)
1986 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
1988 printf("Could not set breakpoint: %s\n", strerror(-ret));
1994 static int remove_break_f(BlockBackend *blk, int argc, char **argv)
1998 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
2000 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2006 static const cmdinfo_t break_cmd = {
2011 .args = "event tag",
2012 .oneline = "sets a breakpoint on event and tags the stopped "
2016 static const cmdinfo_t remove_break_cmd = {
2017 .name = "remove_break",
2020 .cfunc = remove_break_f,
2022 .oneline = "remove a breakpoint by tag",
2025 static int resume_f(BlockBackend *blk, int argc, char **argv)
2029 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2031 printf("Could not resume request: %s\n", strerror(-ret));
2037 static const cmdinfo_t resume_cmd = {
2043 .oneline = "resumes the request tagged as tag",
2046 static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2048 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2049 aio_poll(blk_get_aio_context(blk), true);
2055 static const cmdinfo_t wait_break_cmd = {
2056 .name = "wait_break",
2059 .cfunc = wait_break_f,
2061 .oneline = "waits for the suspension of a request",
2064 static int abort_f(BlockBackend *blk, int argc, char **argv)
2069 static const cmdinfo_t abort_cmd = {
2072 .flags = CMD_NOFILE_OK,
2073 .oneline = "simulate a program crash using abort(3)",
2076 static void sigraise_help(void)
2080 " raises the given signal\n"
2083 " 'sigraise %i' - raises SIGTERM\n"
2085 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2086 " given to sigraise.\n"
2090 static int sigraise_f(BlockBackend *blk, int argc, char **argv);
2092 static const cmdinfo_t sigraise_cmd = {
2094 .cfunc = sigraise_f,
2097 .flags = CMD_NOFILE_OK,
2099 .oneline = "raises a signal",
2100 .help = sigraise_help,
2103 static int sigraise_f(BlockBackend *blk, int argc, char **argv)
2105 int sig = cvtnum(argv[1]);
2107 printf("non-numeric signal number argument -- %s\n", argv[1]);
2111 /* Using raise() to kill this process does not necessarily flush all open
2112 * streams. At least stdout and stderr (although the latter should be
2113 * non-buffered anyway) should be flushed, though. */
2121 static void sleep_cb(void *opaque)
2123 bool *expired = opaque;
2127 static int sleep_f(BlockBackend *blk, int argc, char **argv)
2131 struct QEMUTimer *timer;
2132 bool expired = false;
2134 ms = strtol(argv[1], &endptr, 0);
2135 if (ms < 0 || *endptr != '\0') {
2136 printf("%s is not a valid number\n", argv[1]);
2140 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2141 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2144 main_loop_wait(false);
2152 static const cmdinfo_t sleep_cmd = {
2157 .flags = CMD_NOFILE_OK,
2158 .oneline = "waits for the given value in milliseconds",
2161 static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2166 printf("%s ", ct->name);
2168 printf("(or %s) ", ct->altname);
2173 printf("%s ", ct->args);
2175 printf("-- %s\n", ct->oneline);
2178 static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2180 help_oneline(cmd, ct);
2186 static void help_all(void)
2188 const cmdinfo_t *ct;
2190 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2191 help_oneline(ct->name, ct);
2193 printf("\nUse 'help commandname' for extended help.\n");
2196 static int help_f(BlockBackend *blk, int argc, char **argv)
2198 const cmdinfo_t *ct;
2205 ct = find_command(argv[1]);
2207 printf("command %s not found\n", argv[1]);
2211 help_onecmd(argv[1], ct);
2215 static const cmdinfo_t help_cmd = {
2221 .flags = CMD_FLAG_GLOBAL,
2222 .args = "[command]",
2223 .oneline = "help for one or all commands",
2226 bool qemuio_command(BlockBackend *blk, const char *cmd)
2229 const cmdinfo_t *ct;
2234 input = g_strdup(cmd);
2235 v = breakline(input, &c);
2237 ct = find_command(v[0]);
2239 done = command(blk, ct, c, v);
2241 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2250 static void __attribute((constructor)) init_qemuio_commands(void)
2252 /* initialize commands */
2253 qemuio_add_command(&help_cmd);
2254 qemuio_add_command(&read_cmd);
2255 qemuio_add_command(&readv_cmd);
2256 qemuio_add_command(&write_cmd);
2257 qemuio_add_command(&writev_cmd);
2258 qemuio_add_command(&multiwrite_cmd);
2259 qemuio_add_command(&aio_read_cmd);
2260 qemuio_add_command(&aio_write_cmd);
2261 qemuio_add_command(&aio_flush_cmd);
2262 qemuio_add_command(&flush_cmd);
2263 qemuio_add_command(&truncate_cmd);
2264 qemuio_add_command(&length_cmd);
2265 qemuio_add_command(&info_cmd);
2266 qemuio_add_command(&discard_cmd);
2267 qemuio_add_command(&alloc_cmd);
2268 qemuio_add_command(&map_cmd);
2269 qemuio_add_command(&break_cmd);
2270 qemuio_add_command(&remove_break_cmd);
2271 qemuio_add_command(&resume_cmd);
2272 qemuio_add_command(&wait_break_cmd);
2273 qemuio_add_command(&abort_cmd);
2274 qemuio_add_command(&sleep_cmd);
2275 qemuio_add_command(&sigraise_cmd);