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/main-loop.h"
17 #include "qemu/timer.h"
18 #include "sysemu/block-backend.h"
20 #define CMD_NOFILE_OK 0x01
24 static cmdinfo_t *cmdtab;
27 static int compare_cmdname(const void *a, const void *b)
29 return strcmp(((const cmdinfo_t *)a)->name,
30 ((const cmdinfo_t *)b)->name);
33 void qemuio_add_command(const cmdinfo_t *ci)
35 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
36 cmdtab[ncmds - 1] = *ci;
37 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
40 int qemuio_command_usage(const cmdinfo_t *ci)
42 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
46 static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
48 if (ct->flags & CMD_FLAG_GLOBAL) {
51 if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
52 fprintf(stderr, "no file open, try 'help open'\n");
58 static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
63 if (!init_check_command(blk, ct)) {
67 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
68 if (ct->argmax == -1) {
70 "bad argument count %d to %s, expected at least %d arguments\n",
71 argc-1, cmd, ct->argmin);
72 } else if (ct->argmin == ct->argmax) {
74 "bad argument count %d to %s, expected %d arguments\n",
75 argc-1, cmd, ct->argmin);
78 "bad argument count %d to %s, expected between %d and %d arguments\n",
79 argc-1, cmd, ct->argmin, ct->argmax);
84 return ct->cfunc(blk, argc, argv);
87 static const cmdinfo_t *find_command(const char *cmd)
91 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
92 if (strcmp(ct->name, cmd) == 0 ||
93 (ct->altname && strcmp(ct->altname, cmd) == 0))
95 return (const cmdinfo_t *)ct;
101 /* Invoke fn() for commands with a matching prefix */
102 void qemuio_complete_command(const char *input,
103 void (*fn)(const char *cmd, void *opaque),
107 size_t input_len = strlen(input);
109 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
110 if (strncmp(input, ct->name, input_len) == 0) {
111 fn(ct->name, opaque);
116 static char **breakline(char *input, int *count)
120 char **rval = g_new0(char *, 1);
122 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
127 rval = g_renew(char *, rval, (c + 1));
135 static int64_t cvtnum(const char *s)
138 return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
141 #define EXABYTES(x) ((long long)(x) << 60)
142 #define PETABYTES(x) ((long long)(x) << 50)
143 #define TERABYTES(x) ((long long)(x) << 40)
144 #define GIGABYTES(x) ((long long)(x) << 30)
145 #define MEGABYTES(x) ((long long)(x) << 20)
146 #define KILOBYTES(x) ((long long)(x) << 10)
148 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
149 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
150 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
151 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
152 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
153 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
155 static void cvtstr(double value, char *str, size_t size)
160 if (value >= EXABYTES(1)) {
162 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
163 } else if (value >= PETABYTES(1)) {
165 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
166 } else if (value >= TERABYTES(1)) {
168 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
169 } else if (value >= GIGABYTES(1)) {
171 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
172 } else if (value >= MEGABYTES(1)) {
174 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
175 } else if (value >= KILOBYTES(1)) {
177 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
180 snprintf(str, size - 6, "%f", value);
183 trim = strstr(str, ".000");
185 strcpy(trim, suffix);
193 static struct timeval tsub(struct timeval t1, struct timeval t2)
195 t1.tv_usec -= t2.tv_usec;
196 if (t1.tv_usec < 0) {
197 t1.tv_usec += 1000000;
200 t1.tv_sec -= t2.tv_sec;
204 static double tdiv(double value, struct timeval tv)
206 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
209 #define HOURS(sec) ((sec) / (60 * 60))
210 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
211 #define SECONDS(sec) ((sec) % 60)
215 TERSE_FIXED_TIME = 0x1,
216 VERBOSE_FIXED_TIME = 0x2,
219 static void timestr(struct timeval *tv, char *ts, size_t size, int format)
221 double usec = (double)tv->tv_usec / 1000000.0;
223 if (format & TERSE_FIXED_TIME) {
224 if (!HOURS(tv->tv_sec)) {
225 snprintf(ts, size, "%u:%02u.%02u",
226 (unsigned int) MINUTES(tv->tv_sec),
227 (unsigned int) SECONDS(tv->tv_sec),
228 (unsigned int) (usec * 100));
231 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
234 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
235 snprintf(ts, size, "%u:%02u:%02u.%02u",
236 (unsigned int) HOURS(tv->tv_sec),
237 (unsigned int) MINUTES(tv->tv_sec),
238 (unsigned int) SECONDS(tv->tv_sec),
239 (unsigned int) (usec * 100));
241 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
246 * Parse the pattern argument to various sub-commands.
248 * Because the pattern is used as an argument to memset it must evaluate
249 * to an unsigned integer that fits into a single byte.
251 static int parse_pattern(const char *arg)
256 pattern = strtol(arg, &endptr, 0);
257 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
258 printf("%s is not a valid pattern byte\n", arg);
266 * Memory allocation helpers.
268 * Make sure memory is aligned by default, or purposefully misaligned if
269 * that is specified on the command line.
272 #define MISALIGN_OFFSET 16
273 static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
277 if (qemuio_misalign) {
278 len += MISALIGN_OFFSET;
280 buf = blk_blockalign(blk, len);
281 memset(buf, pattern, len);
282 if (qemuio_misalign) {
283 buf += MISALIGN_OFFSET;
288 static void qemu_io_free(void *p)
290 if (qemuio_misalign) {
291 p -= MISALIGN_OFFSET;
296 static void dump_buffer(const void *buffer, int64_t offset, int len)
301 for (i = 0, p = buffer; i < len; i += 16) {
302 const uint8_t *s = p;
304 printf("%08" PRIx64 ": ", offset + i);
305 for (j = 0; j < 16 && i + j < len; j++, p++) {
309 for (j = 0; j < 16 && i + j < len; j++, s++) {
320 static void print_report(const char *op, struct timeval *t, int64_t offset,
321 int count, int total, int cnt, int Cflag)
323 char s1[64], s2[64], ts[64];
325 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
327 cvtstr((double)total, s1, sizeof(s1));
328 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
329 printf("%s %d/%d bytes at offset %" PRId64 "\n",
330 op, total, count, offset);
331 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
332 s1, cnt, ts, s2, tdiv((double)cnt, *t));
333 } else {/* bytes,ops,time,bytes/sec,ops/sec */
334 printf("%d,%d,%s,%.3f,%.3f\n",
336 tdiv((double)total, *t),
337 tdiv((double)cnt, *t));
342 * Parse multiple length statements for vectored I/O, and construct an I/O
343 * vector matching it.
346 create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
349 size_t *sizes = g_new0(size_t, nr_iov);
355 for (i = 0; i < nr_iov; i++) {
361 printf("non-numeric length argument -- %s\n", arg);
365 /* should be SIZE_T_MAX, but that doesn't exist */
367 printf("too large length argument -- %s\n", arg);
372 printf("length argument %" PRId64
373 " is not sector aligned\n", len);
381 qemu_iovec_init(qiov, nr_iov);
383 buf = p = qemu_io_alloc(blk, count, pattern);
385 for (i = 0; i < nr_iov; i++) {
386 qemu_iovec_add(qiov, p, sizes[i]);
395 static int do_read(BlockBackend *blk, char *buf, int64_t offset, int count,
400 ret = blk_read(blk, offset >> 9, (uint8_t *)buf, count >> 9);
408 static int do_write(BlockBackend *blk, char *buf, int64_t offset, int count,
413 ret = blk_write(blk, offset >> 9, (uint8_t *)buf, count >> 9);
421 static int do_pread(BlockBackend *blk, char *buf, int64_t offset, int count,
424 *total = blk_pread(blk, offset, (uint8_t *)buf, count);
431 static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset, int count,
434 *total = blk_pwrite(blk, offset, (uint8_t *)buf, count);
450 static void coroutine_fn co_write_zeroes_entry(void *opaque)
452 CoWriteZeroes *data = opaque;
454 data->ret = blk_co_write_zeroes(data->blk, data->offset / BDRV_SECTOR_SIZE,
455 data->count / BDRV_SECTOR_SIZE, 0);
458 *data->total = data->ret;
462 *data->total = data->count;
465 static int do_co_write_zeroes(BlockBackend *blk, int64_t offset, int count,
469 CoWriteZeroes data = {
477 co = qemu_coroutine_create(co_write_zeroes_entry);
478 qemu_coroutine_enter(co, &data);
480 aio_poll(blk_get_aio_context(blk), true);
489 static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
490 int count, int *total)
494 ret = blk_write_compressed(blk, offset >> 9, (uint8_t *)buf, count >> 9);
502 static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
503 int count, int *total)
505 *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
512 static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
513 int count, int *total)
515 *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
522 #define NOT_DONE 0x7fffffff
523 static void aio_rw_done(void *opaque, int ret)
525 *(int *)opaque = ret;
528 static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
529 int64_t offset, int *total)
531 int async_ret = NOT_DONE;
533 blk_aio_readv(blk, offset >> 9, qiov, qiov->size >> 9,
534 aio_rw_done, &async_ret);
535 while (async_ret == NOT_DONE) {
536 main_loop_wait(false);
540 return async_ret < 0 ? async_ret : 1;
543 static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
544 int64_t offset, int *total)
546 int async_ret = NOT_DONE;
548 blk_aio_writev(blk, offset >> 9, qiov, qiov->size >> 9,
549 aio_rw_done, &async_ret);
550 while (async_ret == NOT_DONE) {
551 main_loop_wait(false);
555 return async_ret < 0 ? async_ret : 1;
558 struct multiwrite_async_ret {
563 static void multiwrite_cb(void *opaque, int ret)
565 struct multiwrite_async_ret *async_ret = opaque;
567 async_ret->num_done++;
569 async_ret->error = ret;
573 static int do_aio_multiwrite(BlockBackend *blk, BlockRequest* reqs,
574 int num_reqs, int *total)
577 struct multiwrite_async_ret async_ret = {
583 for (i = 0; i < num_reqs; i++) {
584 reqs[i].cb = multiwrite_cb;
585 reqs[i].opaque = &async_ret;
586 *total += reqs[i].qiov->size;
589 ret = blk_aio_multiwrite(blk, reqs, num_reqs);
594 while (async_ret.num_done < num_reqs) {
595 main_loop_wait(false);
598 return async_ret.error < 0 ? async_ret.error : 1;
601 static void read_help(void)
605 " reads a range of bytes from the given offset\n"
608 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
610 " Reads a segment of the currently open file, optionally dumping it to the\n"
611 " standard output stream (with -v option) for subsequent inspection.\n"
612 " -b, -- read from the VM state rather than the virtual disk\n"
613 " -C, -- report statistics in a machine parsable format\n"
614 " -l, -- length for pattern verification (only with -P)\n"
615 " -p, -- use blk_pread to read the file\n"
616 " -P, -- use a pattern to verify read data\n"
617 " -q, -- quiet mode, do not show I/O statistics\n"
618 " -s, -- start offset for pattern verification (only with -P)\n"
619 " -v, -- dump buffer to standard output\n"
623 static int read_f(BlockBackend *blk, int argc, char **argv);
625 static const cmdinfo_t read_cmd = {
631 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
632 .oneline = "reads a number of bytes at a specified offset",
636 static int read_f(BlockBackend *blk, int argc, char **argv)
638 struct timeval t1, t2;
639 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
640 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
645 /* Some compilers get confused and warn if this is not initialized. */
647 int pattern = 0, pattern_offset = 0, pattern_count = 0;
649 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
659 pattern_count = cvtnum(optarg);
660 if (pattern_count < 0) {
661 printf("non-numeric length argument -- %s\n", optarg);
670 pattern = parse_pattern(optarg);
680 pattern_offset = cvtnum(optarg);
681 if (pattern_offset < 0) {
682 printf("non-numeric length argument -- %s\n", optarg);
690 return qemuio_command_usage(&read_cmd);
694 if (optind != argc - 2) {
695 return qemuio_command_usage(&read_cmd);
698 if (bflag && pflag) {
699 printf("-b and -p cannot be specified at the same time\n");
703 offset = cvtnum(argv[optind]);
705 printf("non-numeric length argument -- %s\n", argv[optind]);
710 count = cvtnum(argv[optind]);
712 printf("non-numeric length argument -- %s\n", argv[optind]);
716 if (!Pflag && (lflag || sflag)) {
717 return qemuio_command_usage(&read_cmd);
721 pattern_count = count - pattern_offset;
724 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
725 printf("pattern verification range exceeds end of read data\n");
730 if (offset & 0x1ff) {
731 printf("offset %" PRId64 " is not sector aligned\n",
736 printf("count %d is not sector aligned\n",
742 buf = qemu_io_alloc(blk, count, 0xab);
744 gettimeofday(&t1, NULL);
746 cnt = do_pread(blk, buf, offset, count, &total);
748 cnt = do_load_vmstate(blk, buf, offset, count, &total);
750 cnt = do_read(blk, buf, offset, count, &total);
752 gettimeofday(&t2, NULL);
755 printf("read failed: %s\n", strerror(-cnt));
760 void *cmp_buf = g_malloc(pattern_count);
761 memset(cmp_buf, pattern, pattern_count);
762 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
763 printf("Pattern verification failed at offset %"
764 PRId64 ", %d bytes\n",
765 offset + pattern_offset, pattern_count);
775 dump_buffer(buf, offset, count);
778 /* Finally, report back -- -C gives a parsable format */
780 print_report("read", &t2, offset, count, total, cnt, Cflag);
788 static void readv_help(void)
792 " reads a range of bytes from the given offset into multiple buffers\n"
795 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
797 " Reads a segment of the currently open file, optionally dumping it to the\n"
798 " standard output stream (with -v option) for subsequent inspection.\n"
799 " Uses multiple iovec buffers if more than one byte range is specified.\n"
800 " -C, -- report statistics in a machine parsable format\n"
801 " -P, -- use a pattern to verify read data\n"
802 " -v, -- dump buffer to standard output\n"
803 " -q, -- quiet mode, do not show I/O statistics\n"
807 static int readv_f(BlockBackend *blk, int argc, char **argv);
809 static const cmdinfo_t readv_cmd = {
814 .args = "[-Cqv] [-P pattern ] off len [len..]",
815 .oneline = "reads a number of bytes at a specified offset",
819 static int readv_f(BlockBackend *blk, int argc, char **argv)
821 struct timeval t1, t2;
822 int Cflag = 0, qflag = 0, vflag = 0;
826 /* Some compilers get confused and warn if this is not initialized. */
833 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
840 pattern = parse_pattern(optarg);
852 return qemuio_command_usage(&readv_cmd);
856 if (optind > argc - 2) {
857 return qemuio_command_usage(&readv_cmd);
861 offset = cvtnum(argv[optind]);
863 printf("non-numeric length argument -- %s\n", argv[optind]);
868 if (offset & 0x1ff) {
869 printf("offset %" PRId64 " is not sector aligned\n",
874 nr_iov = argc - optind;
875 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
880 gettimeofday(&t1, NULL);
881 cnt = do_aio_readv(blk, &qiov, offset, &total);
882 gettimeofday(&t2, NULL);
885 printf("readv failed: %s\n", strerror(-cnt));
890 void *cmp_buf = g_malloc(qiov.size);
891 memset(cmp_buf, pattern, qiov.size);
892 if (memcmp(buf, cmp_buf, qiov.size)) {
893 printf("Pattern verification failed at offset %"
894 PRId64 ", %zd bytes\n", offset, qiov.size);
904 dump_buffer(buf, offset, qiov.size);
907 /* Finally, report back -- -C gives a parsable format */
909 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
912 qemu_iovec_destroy(&qiov);
917 static void write_help(void)
921 " writes a range of bytes from the given offset\n"
924 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
926 " Writes into a segment of the currently open file, using a buffer\n"
927 " filled with a set pattern (0xcdcdcdcd).\n"
928 " -b, -- write to the VM state rather than the virtual disk\n"
929 " -c, -- write compressed data with blk_write_compressed\n"
930 " -p, -- use blk_pwrite to write the file\n"
931 " -P, -- use different pattern to fill file\n"
932 " -C, -- report statistics in a machine parsable format\n"
933 " -q, -- quiet mode, do not show I/O statistics\n"
934 " -z, -- write zeroes using blk_co_write_zeroes\n"
938 static int write_f(BlockBackend *blk, int argc, char **argv);
940 static const cmdinfo_t write_cmd = {
946 .args = "[-bcCpqz] [-P pattern ] off len",
947 .oneline = "writes a number of bytes at a specified offset",
951 static int write_f(BlockBackend *blk, int argc, char **argv)
953 struct timeval t1, t2;
954 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
960 /* Some compilers get confused and warn if this is not initialized. */
964 while ((c = getopt(argc, argv, "bcCpP:qz")) != -1) {
980 pattern = parse_pattern(optarg);
992 return qemuio_command_usage(&write_cmd);
996 if (optind != argc - 2) {
997 return qemuio_command_usage(&write_cmd);
1000 if (bflag + pflag + zflag > 1) {
1001 printf("-b, -p, or -z cannot be specified at the same time\n");
1005 if (zflag && Pflag) {
1006 printf("-z and -P cannot be specified at the same time\n");
1010 offset = cvtnum(argv[optind]);
1012 printf("non-numeric length argument -- %s\n", argv[optind]);
1017 count = cvtnum(argv[optind]);
1019 printf("non-numeric length argument -- %s\n", argv[optind]);
1024 if (offset & 0x1ff) {
1025 printf("offset %" PRId64 " is not sector aligned\n",
1030 if (count & 0x1ff) {
1031 printf("count %d is not sector aligned\n",
1038 buf = qemu_io_alloc(blk, count, pattern);
1041 gettimeofday(&t1, NULL);
1043 cnt = do_pwrite(blk, buf, offset, count, &total);
1045 cnt = do_save_vmstate(blk, buf, offset, count, &total);
1047 cnt = do_co_write_zeroes(blk, offset, count, &total);
1049 cnt = do_write_compressed(blk, buf, offset, count, &total);
1051 cnt = do_write(blk, buf, offset, count, &total);
1053 gettimeofday(&t2, NULL);
1056 printf("write failed: %s\n", strerror(-cnt));
1064 /* Finally, report back -- -C gives a parsable format */
1066 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1081 " writes a range of bytes from the given offset source from multiple buffers\n"
1084 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1086 " Writes into a segment of the currently open file, using a buffer\n"
1087 " filled with a set pattern (0xcdcdcdcd).\n"
1088 " -P, -- use different pattern to fill file\n"
1089 " -C, -- report statistics in a machine parsable format\n"
1090 " -q, -- quiet mode, do not show I/O statistics\n"
1094 static int writev_f(BlockBackend *blk, int argc, char **argv);
1096 static const cmdinfo_t writev_cmd = {
1101 .args = "[-Cq] [-P pattern ] off len [len..]",
1102 .oneline = "writes a number of bytes at a specified offset",
1103 .help = writev_help,
1106 static int writev_f(BlockBackend *blk, int argc, char **argv)
1108 struct timeval t1, t2;
1109 int Cflag = 0, qflag = 0;
1113 /* Some compilers get confused and warn if this is not initialized. */
1119 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1128 pattern = parse_pattern(optarg);
1134 return qemuio_command_usage(&writev_cmd);
1138 if (optind > argc - 2) {
1139 return qemuio_command_usage(&writev_cmd);
1142 offset = cvtnum(argv[optind]);
1144 printf("non-numeric length argument -- %s\n", argv[optind]);
1149 if (offset & 0x1ff) {
1150 printf("offset %" PRId64 " is not sector aligned\n",
1155 nr_iov = argc - optind;
1156 buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
1161 gettimeofday(&t1, NULL);
1162 cnt = do_aio_writev(blk, &qiov, offset, &total);
1163 gettimeofday(&t2, NULL);
1166 printf("writev failed: %s\n", strerror(-cnt));
1174 /* Finally, report back -- -C gives a parsable format */
1176 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1178 qemu_iovec_destroy(&qiov);
1183 static void multiwrite_help(void)
1187 " writes a range of bytes from the given offset source from multiple buffers,\n"
1188 " in a batch of requests that may be merged by qemu\n"
1191 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1192 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1194 " Writes into a segment of the currently open file, using a buffer\n"
1195 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1196 " by one for each request contained in the multiwrite command.\n"
1197 " -P, -- use different pattern to fill file\n"
1198 " -C, -- report statistics in a machine parsable format\n"
1199 " -q, -- quiet mode, do not show I/O statistics\n"
1203 static int multiwrite_f(BlockBackend *blk, int argc, char **argv);
1205 static const cmdinfo_t multiwrite_cmd = {
1206 .name = "multiwrite",
1207 .cfunc = multiwrite_f,
1210 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1211 .oneline = "issues multiple write requests at once",
1212 .help = multiwrite_help,
1215 static int multiwrite_f(BlockBackend *blk, int argc, char **argv)
1217 struct timeval t1, t2;
1218 int Cflag = 0, qflag = 0;
1221 int64_t offset, first_offset = 0;
1222 /* Some compilers get confused and warn if this is not initialized. */
1227 QEMUIOVector *qiovs;
1231 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1240 pattern = parse_pattern(optarg);
1246 return qemuio_command_usage(&writev_cmd);
1250 if (optind > argc - 2) {
1251 return qemuio_command_usage(&writev_cmd);
1255 for (i = optind; i < argc; i++) {
1256 if (!strcmp(argv[i], ";")) {
1261 reqs = g_new0(BlockRequest, nr_reqs);
1262 buf = g_new0(char *, nr_reqs);
1263 qiovs = g_new(QEMUIOVector, nr_reqs);
1265 for (i = 0; i < nr_reqs && optind < argc; i++) {
1268 /* Read the offset of the request */
1269 offset = cvtnum(argv[optind]);
1271 printf("non-numeric offset argument -- %s\n", argv[optind]);
1276 if (offset & 0x1ff) {
1277 printf("offset %lld is not sector aligned\n",
1283 first_offset = offset;
1286 /* Read lengths for qiov entries */
1287 for (j = optind; j < argc; j++) {
1288 if (!strcmp(argv[j], ";")) {
1293 nr_iov = j - optind;
1296 buf[i] = create_iovec(blk, &qiovs[i], &argv[optind], nr_iov, pattern);
1297 if (buf[i] == NULL) {
1301 reqs[i].qiov = &qiovs[i];
1302 reqs[i].sector = offset >> 9;
1303 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1310 /* If there were empty requests at the end, ignore them */
1313 gettimeofday(&t1, NULL);
1314 cnt = do_aio_multiwrite(blk, reqs, nr_reqs, &total);
1315 gettimeofday(&t2, NULL);
1318 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1326 /* Finally, report back -- -C gives a parsable format */
1328 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1330 for (i = 0; i < nr_reqs; i++) {
1331 qemu_io_free(buf[i]);
1332 if (reqs[i].qiov != NULL) {
1333 qemu_iovec_destroy(&qiovs[i]);
1351 BlockAcctCookie acct;
1356 static void aio_write_done(void *opaque, int ret)
1358 struct aio_ctx *ctx = opaque;
1361 gettimeofday(&t2, NULL);
1365 printf("aio_write failed: %s\n", strerror(-ret));
1369 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1375 /* Finally, report back -- -C gives a parsable format */
1376 t2 = tsub(t2, ctx->t1);
1377 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1378 ctx->qiov.size, 1, ctx->Cflag);
1380 qemu_io_free(ctx->buf);
1381 qemu_iovec_destroy(&ctx->qiov);
1385 static void aio_read_done(void *opaque, int ret)
1387 struct aio_ctx *ctx = opaque;
1390 gettimeofday(&t2, NULL);
1393 printf("readv failed: %s\n", strerror(-ret));
1398 void *cmp_buf = g_malloc(ctx->qiov.size);
1400 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1401 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1402 printf("Pattern verification failed at offset %"
1403 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1408 block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
1415 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1418 /* Finally, report back -- -C gives a parsable format */
1419 t2 = tsub(t2, ctx->t1);
1420 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1421 ctx->qiov.size, 1, ctx->Cflag);
1423 qemu_io_free(ctx->buf);
1424 qemu_iovec_destroy(&ctx->qiov);
1428 static void aio_read_help(void)
1432 " asynchronously reads a range of bytes from the given offset\n"
1435 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1437 " Reads a segment of the currently open file, optionally dumping it to the\n"
1438 " standard output stream (with -v option) for subsequent inspection.\n"
1439 " The read is performed asynchronously and the aio_flush command must be\n"
1440 " used to ensure all outstanding aio requests have been completed.\n"
1441 " -C, -- report statistics in a machine parsable format\n"
1442 " -P, -- use a pattern to verify read data\n"
1443 " -v, -- dump buffer to standard output\n"
1444 " -q, -- quiet mode, do not show I/O statistics\n"
1448 static int aio_read_f(BlockBackend *blk, int argc, char **argv);
1450 static const cmdinfo_t aio_read_cmd = {
1452 .cfunc = aio_read_f,
1455 .args = "[-Cqv] [-P pattern ] off len [len..]",
1456 .oneline = "asynchronously reads a number of bytes",
1457 .help = aio_read_help,
1460 static int aio_read_f(BlockBackend *blk, int argc, char **argv)
1463 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1466 while ((c = getopt(argc, argv, "CP:qv")) != -1) {
1473 ctx->pattern = parse_pattern(optarg);
1474 if (ctx->pattern < 0) {
1487 return qemuio_command_usage(&aio_read_cmd);
1491 if (optind > argc - 2) {
1493 return qemuio_command_usage(&aio_read_cmd);
1496 ctx->offset = cvtnum(argv[optind]);
1497 if (ctx->offset < 0) {
1498 printf("non-numeric length argument -- %s\n", argv[optind]);
1504 if (ctx->offset & 0x1ff) {
1505 printf("offset %" PRId64 " is not sector aligned\n",
1511 nr_iov = argc - optind;
1512 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1513 if (ctx->buf == NULL) {
1518 gettimeofday(&ctx->t1, NULL);
1519 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1521 blk_aio_readv(blk, ctx->offset >> 9, &ctx->qiov,
1522 ctx->qiov.size >> 9, aio_read_done, ctx);
1526 static void aio_write_help(void)
1530 " asynchronously writes a range of bytes from the given offset source\n"
1531 " from multiple buffers\n"
1534 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1536 " Writes into a segment of the currently open file, using a buffer\n"
1537 " filled with a set pattern (0xcdcdcdcd).\n"
1538 " The write is performed asynchronously and the aio_flush command must be\n"
1539 " used to ensure all outstanding aio requests have been completed.\n"
1540 " -P, -- use different pattern to fill file\n"
1541 " -C, -- report statistics in a machine parsable format\n"
1542 " -q, -- quiet mode, do not show I/O statistics\n"
1546 static int aio_write_f(BlockBackend *blk, int argc, char **argv);
1548 static const cmdinfo_t aio_write_cmd = {
1549 .name = "aio_write",
1550 .cfunc = aio_write_f,
1553 .args = "[-Cq] [-P pattern ] off len [len..]",
1554 .oneline = "asynchronously writes a number of bytes",
1555 .help = aio_write_help,
1558 static int aio_write_f(BlockBackend *blk, int argc, char **argv)
1562 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1565 while ((c = getopt(argc, argv, "CqP:")) != -1) {
1574 pattern = parse_pattern(optarg);
1582 return qemuio_command_usage(&aio_write_cmd);
1586 if (optind > argc - 2) {
1588 return qemuio_command_usage(&aio_write_cmd);
1591 ctx->offset = cvtnum(argv[optind]);
1592 if (ctx->offset < 0) {
1593 printf("non-numeric length argument -- %s\n", argv[optind]);
1599 if (ctx->offset & 0x1ff) {
1600 printf("offset %" PRId64 " is not sector aligned\n",
1606 nr_iov = argc - optind;
1607 ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, pattern);
1608 if (ctx->buf == NULL) {
1613 gettimeofday(&ctx->t1, NULL);
1614 block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
1616 blk_aio_writev(blk, ctx->offset >> 9, &ctx->qiov,
1617 ctx->qiov.size >> 9, aio_write_done, ctx);
1621 static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
1627 static const cmdinfo_t aio_flush_cmd = {
1628 .name = "aio_flush",
1629 .cfunc = aio_flush_f,
1630 .oneline = "completes all outstanding aio requests"
1633 static int flush_f(BlockBackend *blk, int argc, char **argv)
1639 static const cmdinfo_t flush_cmd = {
1643 .oneline = "flush all in-core file state to disk",
1646 static int truncate_f(BlockBackend *blk, int argc, char **argv)
1651 offset = cvtnum(argv[1]);
1653 printf("non-numeric truncate argument -- %s\n", argv[1]);
1657 ret = blk_truncate(blk, offset);
1659 printf("truncate: %s\n", strerror(-ret));
1666 static const cmdinfo_t truncate_cmd = {
1669 .cfunc = truncate_f,
1673 .oneline = "truncates the current file at the given offset",
1676 static int length_f(BlockBackend *blk, int argc, char **argv)
1681 size = blk_getlength(blk);
1683 printf("getlength: %s\n", strerror(-size));
1687 cvtstr(size, s1, sizeof(s1));
1693 static const cmdinfo_t length_cmd = {
1697 .oneline = "gets the length of the current file",
1701 static int info_f(BlockBackend *blk, int argc, char **argv)
1703 BlockDriverState *bs = blk_bs(blk);
1704 BlockDriverInfo bdi;
1705 ImageInfoSpecific *spec_info;
1706 char s1[64], s2[64];
1709 if (bs->drv && bs->drv->format_name) {
1710 printf("format name: %s\n", bs->drv->format_name);
1712 if (bs->drv && bs->drv->protocol_name) {
1713 printf("format name: %s\n", bs->drv->protocol_name);
1716 ret = bdrv_get_info(bs, &bdi);
1721 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1722 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1724 printf("cluster size: %s\n", s1);
1725 printf("vm state offset: %s\n", s2);
1727 spec_info = bdrv_get_specific_info(bs);
1729 printf("Format specific information:\n");
1730 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1731 qapi_free_ImageInfoSpecific(spec_info);
1739 static const cmdinfo_t info_cmd = {
1743 .oneline = "prints information about the current file",
1746 static void discard_help(void)
1750 " discards a range of bytes from the given offset\n"
1753 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1755 " Discards a segment of the currently open file.\n"
1756 " -C, -- report statistics in a machine parsable format\n"
1757 " -q, -- quiet mode, do not show I/O statistics\n"
1761 static int discard_f(BlockBackend *blk, int argc, char **argv);
1763 static const cmdinfo_t discard_cmd = {
1769 .args = "[-Cq] off len",
1770 .oneline = "discards a number of bytes at a specified offset",
1771 .help = discard_help,
1774 static int discard_f(BlockBackend *blk, int argc, char **argv)
1776 struct timeval t1, t2;
1777 int Cflag = 0, qflag = 0;
1782 while ((c = getopt(argc, argv, "Cq")) != -1) {
1791 return qemuio_command_usage(&discard_cmd);
1795 if (optind != argc - 2) {
1796 return qemuio_command_usage(&discard_cmd);
1799 offset = cvtnum(argv[optind]);
1801 printf("non-numeric length argument -- %s\n", argv[optind]);
1806 count = cvtnum(argv[optind]);
1808 printf("non-numeric length argument -- %s\n", argv[optind]);
1812 gettimeofday(&t1, NULL);
1813 ret = blk_discard(blk, offset >> BDRV_SECTOR_BITS,
1814 count >> BDRV_SECTOR_BITS);
1815 gettimeofday(&t2, NULL);
1818 printf("discard failed: %s\n", strerror(-ret));
1822 /* Finally, report back -- -C gives a parsable format */
1825 print_report("discard", &t2, offset, count, count, 1, Cflag);
1832 static int alloc_f(BlockBackend *blk, int argc, char **argv)
1834 BlockDriverState *bs = blk_bs(blk);
1835 int64_t offset, sector_num;
1836 int nb_sectors, remaining;
1841 offset = cvtnum(argv[1]);
1843 printf("non-numeric offset argument -- %s\n", argv[1]);
1845 } else if (offset & 0x1ff) {
1846 printf("offset %" PRId64 " is not sector aligned\n",
1852 nb_sectors = cvtnum(argv[2]);
1853 if (nb_sectors < 0) {
1854 printf("non-numeric length argument -- %s\n", argv[2]);
1861 remaining = nb_sectors;
1863 sector_num = offset >> 9;
1865 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1867 printf("is_allocated failed: %s\n", strerror(-ret));
1876 nb_sectors -= remaining;
1881 cvtstr(offset, s1, sizeof(s1));
1883 printf("%d/%d sectors allocated at offset %s\n",
1884 sum_alloc, nb_sectors, s1);
1888 static const cmdinfo_t alloc_cmd = {
1894 .args = "off [sectors]",
1895 .oneline = "checks if a sector is present in the file",
1899 static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1900 int64_t nb_sectors, int64_t *pnum)
1902 int num, num_checked;
1905 num_checked = MIN(nb_sectors, INT_MAX);
1906 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1914 while (nb_sectors > 0 && ret == firstret) {
1918 num_checked = MIN(nb_sectors, INT_MAX);
1919 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1920 if (ret == firstret && num) {
1930 static int map_f(BlockBackend *blk, int argc, char **argv)
1933 int64_t nb_sectors, total_sectors;
1940 total_sectors = blk_nb_sectors(blk);
1941 if (total_sectors < 0) {
1942 error_report("Failed to query image length: %s",
1943 strerror(-total_sectors));
1947 nb_sectors = total_sectors;
1950 ret = map_is_allocated(blk_bs(blk), offset, nb_sectors, &num);
1952 error_report("Failed to get allocation status: %s", strerror(-ret));
1955 error_report("Unexpected end of image");
1959 retstr = ret ? " allocated" : "not allocated";
1960 cvtstr(offset << 9ULL, s1, sizeof(s1));
1961 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1962 "at offset %s (%d)\n",
1963 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1967 } while (offset < total_sectors);
1972 static const cmdinfo_t map_cmd = {
1978 .oneline = "prints the allocated areas of a file",
1981 static int break_f(BlockBackend *blk, int argc, char **argv)
1985 ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
1987 printf("Could not set breakpoint: %s\n", strerror(-ret));
1993 static int remove_break_f(BlockBackend *blk, int argc, char **argv)
1997 ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
1999 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
2005 static const cmdinfo_t break_cmd = {
2010 .args = "event tag",
2011 .oneline = "sets a breakpoint on event and tags the stopped "
2015 static const cmdinfo_t remove_break_cmd = {
2016 .name = "remove_break",
2019 .cfunc = remove_break_f,
2021 .oneline = "remove a breakpoint by tag",
2024 static int resume_f(BlockBackend *blk, int argc, char **argv)
2028 ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
2030 printf("Could not resume request: %s\n", strerror(-ret));
2036 static const cmdinfo_t resume_cmd = {
2042 .oneline = "resumes the request tagged as tag",
2045 static int wait_break_f(BlockBackend *blk, int argc, char **argv)
2047 while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
2048 aio_poll(blk_get_aio_context(blk), true);
2054 static const cmdinfo_t wait_break_cmd = {
2055 .name = "wait_break",
2058 .cfunc = wait_break_f,
2060 .oneline = "waits for the suspension of a request",
2063 static int abort_f(BlockBackend *blk, int argc, char **argv)
2068 static const cmdinfo_t abort_cmd = {
2071 .flags = CMD_NOFILE_OK,
2072 .oneline = "simulate a program crash using abort(3)",
2075 static void sigraise_help(void)
2079 " raises the given signal\n"
2082 " 'sigraise %i' - raises SIGTERM\n"
2084 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2085 " given to sigraise.\n"
2089 static int sigraise_f(BlockBackend *blk, int argc, char **argv);
2091 static const cmdinfo_t sigraise_cmd = {
2093 .cfunc = sigraise_f,
2096 .flags = CMD_NOFILE_OK,
2098 .oneline = "raises a signal",
2099 .help = sigraise_help,
2102 static int sigraise_f(BlockBackend *blk, int argc, char **argv)
2104 int sig = cvtnum(argv[1]);
2106 printf("non-numeric signal number argument -- %s\n", argv[1]);
2110 /* Using raise() to kill this process does not necessarily flush all open
2111 * streams. At least stdout and stderr (although the latter should be
2112 * non-buffered anyway) should be flushed, though. */
2120 static void sleep_cb(void *opaque)
2122 bool *expired = opaque;
2126 static int sleep_f(BlockBackend *blk, int argc, char **argv)
2130 struct QEMUTimer *timer;
2131 bool expired = false;
2133 ms = strtol(argv[1], &endptr, 0);
2134 if (ms < 0 || *endptr != '\0') {
2135 printf("%s is not a valid number\n", argv[1]);
2139 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2140 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2143 main_loop_wait(false);
2151 static const cmdinfo_t sleep_cmd = {
2156 .flags = CMD_NOFILE_OK,
2157 .oneline = "waits for the given value in milliseconds",
2160 static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2165 printf("%s ", ct->name);
2167 printf("(or %s) ", ct->altname);
2172 printf("%s ", ct->args);
2174 printf("-- %s\n", ct->oneline);
2177 static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2179 help_oneline(cmd, ct);
2185 static void help_all(void)
2187 const cmdinfo_t *ct;
2189 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2190 help_oneline(ct->name, ct);
2192 printf("\nUse 'help commandname' for extended help.\n");
2195 static int help_f(BlockBackend *blk, int argc, char **argv)
2197 const cmdinfo_t *ct;
2204 ct = find_command(argv[1]);
2206 printf("command %s not found\n", argv[1]);
2210 help_onecmd(argv[1], ct);
2214 static const cmdinfo_t help_cmd = {
2220 .flags = CMD_FLAG_GLOBAL,
2221 .args = "[command]",
2222 .oneline = "help for one or all commands",
2225 bool qemuio_command(BlockBackend *blk, const char *cmd)
2228 const cmdinfo_t *ct;
2233 input = g_strdup(cmd);
2234 v = breakline(input, &c);
2236 ct = find_command(v[0]);
2238 done = command(blk, ct, c, v);
2240 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2249 static void __attribute((constructor)) init_qemuio_commands(void)
2251 /* initialize commands */
2252 qemuio_add_command(&help_cmd);
2253 qemuio_add_command(&read_cmd);
2254 qemuio_add_command(&readv_cmd);
2255 qemuio_add_command(&write_cmd);
2256 qemuio_add_command(&writev_cmd);
2257 qemuio_add_command(&multiwrite_cmd);
2258 qemuio_add_command(&aio_read_cmd);
2259 qemuio_add_command(&aio_write_cmd);
2260 qemuio_add_command(&aio_flush_cmd);
2261 qemuio_add_command(&flush_cmd);
2262 qemuio_add_command(&truncate_cmd);
2263 qemuio_add_command(&length_cmd);
2264 qemuio_add_command(&info_cmd);
2265 qemuio_add_command(&discard_cmd);
2266 qemuio_add_command(&alloc_cmd);
2267 qemuio_add_command(&map_cmd);
2268 qemuio_add_command(&break_cmd);
2269 qemuio_add_command(&remove_break_cmd);
2270 qemuio_add_command(&resume_cmd);
2271 qemuio_add_command(&wait_break_cmd);
2272 qemuio_add_command(&abort_cmd);
2273 qemuio_add_command(&sleep_cmd);
2274 qemuio_add_command(&sigraise_cmd);