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 "block/block_int.h"
13 #include "block/qapi.h"
14 #include "qemu/main-loop.h"
15 #include "qemu/timer.h"
17 #define CMD_NOFILE_OK 0x01
21 static cmdinfo_t *cmdtab;
24 static int compare_cmdname(const void *a, const void *b)
26 return strcmp(((const cmdinfo_t *)a)->name,
27 ((const cmdinfo_t *)b)->name);
30 void qemuio_add_command(const cmdinfo_t *ci)
32 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
33 cmdtab[ncmds - 1] = *ci;
34 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
37 int qemuio_command_usage(const cmdinfo_t *ci)
39 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
43 static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
45 if (ct->flags & CMD_FLAG_GLOBAL) {
48 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
49 fprintf(stderr, "no file open, try 'help open'\n");
55 static int command(BlockDriverState *bs, const cmdinfo_t *ct, int argc,
60 if (!init_check_command(bs, ct)) {
64 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
65 if (ct->argmax == -1) {
67 "bad argument count %d to %s, expected at least %d arguments\n",
68 argc-1, cmd, ct->argmin);
69 } else if (ct->argmin == ct->argmax) {
71 "bad argument count %d to %s, expected %d arguments\n",
72 argc-1, cmd, ct->argmin);
75 "bad argument count %d to %s, expected between %d and %d arguments\n",
76 argc-1, cmd, ct->argmin, ct->argmax);
81 return ct->cfunc(bs, argc, argv);
84 static const cmdinfo_t *find_command(const char *cmd)
88 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
89 if (strcmp(ct->name, cmd) == 0 ||
90 (ct->altname && strcmp(ct->altname, cmd) == 0))
92 return (const cmdinfo_t *)ct;
98 /* Invoke fn() for commands with a matching prefix */
99 void qemuio_complete_command(const char *input,
100 void (*fn)(const char *cmd, void *opaque),
104 size_t input_len = strlen(input);
106 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
107 if (strncmp(input, ct->name, input_len) == 0) {
108 fn(ct->name, opaque);
113 static char **breakline(char *input, int *count)
117 char **rval = g_new0(char *, 1);
119 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
124 rval = g_renew(char *, rval, (c + 1));
132 static int64_t cvtnum(const char *s)
135 return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
138 #define EXABYTES(x) ((long long)(x) << 60)
139 #define PETABYTES(x) ((long long)(x) << 50)
140 #define TERABYTES(x) ((long long)(x) << 40)
141 #define GIGABYTES(x) ((long long)(x) << 30)
142 #define MEGABYTES(x) ((long long)(x) << 20)
143 #define KILOBYTES(x) ((long long)(x) << 10)
145 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
146 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
147 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
148 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
149 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
150 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
152 static void cvtstr(double value, char *str, size_t size)
157 if (value >= EXABYTES(1)) {
159 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
160 } else if (value >= PETABYTES(1)) {
162 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
163 } else if (value >= TERABYTES(1)) {
165 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
166 } else if (value >= GIGABYTES(1)) {
168 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
169 } else if (value >= MEGABYTES(1)) {
171 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
172 } else if (value >= KILOBYTES(1)) {
174 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
177 snprintf(str, size - 6, "%f", value);
180 trim = strstr(str, ".000");
182 strcpy(trim, suffix);
190 static struct timeval tsub(struct timeval t1, struct timeval t2)
192 t1.tv_usec -= t2.tv_usec;
193 if (t1.tv_usec < 0) {
194 t1.tv_usec += 1000000;
197 t1.tv_sec -= t2.tv_sec;
201 static double tdiv(double value, struct timeval tv)
203 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
206 #define HOURS(sec) ((sec) / (60 * 60))
207 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
208 #define SECONDS(sec) ((sec) % 60)
212 TERSE_FIXED_TIME = 0x1,
213 VERBOSE_FIXED_TIME = 0x2,
216 static void timestr(struct timeval *tv, char *ts, size_t size, int format)
218 double usec = (double)tv->tv_usec / 1000000.0;
220 if (format & TERSE_FIXED_TIME) {
221 if (!HOURS(tv->tv_sec)) {
222 snprintf(ts, size, "%u:%02u.%02u",
223 (unsigned int) MINUTES(tv->tv_sec),
224 (unsigned int) SECONDS(tv->tv_sec),
225 (unsigned int) (usec * 100));
228 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
231 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
232 snprintf(ts, size, "%u:%02u:%02u.%02u",
233 (unsigned int) HOURS(tv->tv_sec),
234 (unsigned int) MINUTES(tv->tv_sec),
235 (unsigned int) SECONDS(tv->tv_sec),
236 (unsigned int) (usec * 100));
238 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
243 * Parse the pattern argument to various sub-commands.
245 * Because the pattern is used as an argument to memset it must evaluate
246 * to an unsigned integer that fits into a single byte.
248 static int parse_pattern(const char *arg)
253 pattern = strtol(arg, &endptr, 0);
254 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
255 printf("%s is not a valid pattern byte\n", arg);
263 * Memory allocation helpers.
265 * Make sure memory is aligned by default, or purposefully misaligned if
266 * that is specified on the command line.
269 #define MISALIGN_OFFSET 16
270 static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern)
274 if (qemuio_misalign) {
275 len += MISALIGN_OFFSET;
277 buf = qemu_blockalign(bs, len);
278 memset(buf, pattern, len);
279 if (qemuio_misalign) {
280 buf += MISALIGN_OFFSET;
285 static void qemu_io_free(void *p)
287 if (qemuio_misalign) {
288 p -= MISALIGN_OFFSET;
293 static void dump_buffer(const void *buffer, int64_t offset, int len)
298 for (i = 0, p = buffer; i < len; i += 16) {
299 const uint8_t *s = p;
301 printf("%08" PRIx64 ": ", offset + i);
302 for (j = 0; j < 16 && i + j < len; j++, p++) {
306 for (j = 0; j < 16 && i + j < len; j++, s++) {
317 static void print_report(const char *op, struct timeval *t, int64_t offset,
318 int count, int total, int cnt, int Cflag)
320 char s1[64], s2[64], ts[64];
322 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
324 cvtstr((double)total, s1, sizeof(s1));
325 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
326 printf("%s %d/%d bytes at offset %" PRId64 "\n",
327 op, total, count, offset);
328 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
329 s1, cnt, ts, s2, tdiv((double)cnt, *t));
330 } else {/* bytes,ops,time,bytes/sec,ops/sec */
331 printf("%d,%d,%s,%.3f,%.3f\n",
333 tdiv((double)total, *t),
334 tdiv((double)cnt, *t));
339 * Parse multiple length statements for vectored I/O, and construct an I/O
340 * vector matching it.
343 create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov,
346 size_t *sizes = g_new0(size_t, nr_iov);
352 for (i = 0; i < nr_iov; i++) {
358 printf("non-numeric length argument -- %s\n", arg);
362 /* should be SIZE_T_MAX, but that doesn't exist */
364 printf("too large length argument -- %s\n", arg);
369 printf("length argument %" PRId64
370 " is not sector aligned\n", len);
378 qemu_iovec_init(qiov, nr_iov);
380 buf = p = qemu_io_alloc(bs, count, pattern);
382 for (i = 0; i < nr_iov; i++) {
383 qemu_iovec_add(qiov, p, sizes[i]);
392 static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
397 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
405 static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
410 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
418 static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
421 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
428 static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
431 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
439 BlockDriverState *bs;
447 static void coroutine_fn co_write_zeroes_entry(void *opaque)
449 CoWriteZeroes *data = opaque;
451 data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
452 data->count / BDRV_SECTOR_SIZE, 0);
455 *data->total = data->ret;
459 *data->total = data->count;
462 static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
466 CoWriteZeroes data = {
474 co = qemu_coroutine_create(co_write_zeroes_entry);
475 qemu_coroutine_enter(co, &data);
477 aio_poll(bdrv_get_aio_context(bs), true);
486 static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
487 int count, int *total)
491 ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
499 static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
500 int count, int *total)
502 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
509 static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
510 int count, int *total)
512 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
519 #define NOT_DONE 0x7fffffff
520 static void aio_rw_done(void *opaque, int ret)
522 *(int *)opaque = ret;
525 static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov,
526 int64_t offset, int *total)
528 int async_ret = NOT_DONE;
530 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
531 aio_rw_done, &async_ret);
532 while (async_ret == NOT_DONE) {
533 main_loop_wait(false);
537 return async_ret < 0 ? async_ret : 1;
540 static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov,
541 int64_t offset, int *total)
543 int async_ret = NOT_DONE;
545 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
546 aio_rw_done, &async_ret);
547 while (async_ret == NOT_DONE) {
548 main_loop_wait(false);
552 return async_ret < 0 ? async_ret : 1;
555 struct multiwrite_async_ret {
560 static void multiwrite_cb(void *opaque, int ret)
562 struct multiwrite_async_ret *async_ret = opaque;
564 async_ret->num_done++;
566 async_ret->error = ret;
570 static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs,
571 int num_reqs, int *total)
574 struct multiwrite_async_ret async_ret = {
580 for (i = 0; i < num_reqs; i++) {
581 reqs[i].cb = multiwrite_cb;
582 reqs[i].opaque = &async_ret;
583 *total += reqs[i].qiov->size;
586 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
591 while (async_ret.num_done < num_reqs) {
592 main_loop_wait(false);
595 return async_ret.error < 0 ? async_ret.error : 1;
598 static void read_help(void)
602 " reads a range of bytes from the given offset\n"
605 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
607 " Reads a segment of the currently open file, optionally dumping it to the\n"
608 " standard output stream (with -v option) for subsequent inspection.\n"
609 " -b, -- read from the VM state rather than the virtual disk\n"
610 " -C, -- report statistics in a machine parsable format\n"
611 " -l, -- length for pattern verification (only with -P)\n"
612 " -p, -- use bdrv_pread to read the file\n"
613 " -P, -- use a pattern to verify read data\n"
614 " -q, -- quiet mode, do not show I/O statistics\n"
615 " -s, -- start offset for pattern verification (only with -P)\n"
616 " -v, -- dump buffer to standard output\n"
620 static int read_f(BlockDriverState *bs, int argc, char **argv);
622 static const cmdinfo_t read_cmd = {
628 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
629 .oneline = "reads a number of bytes at a specified offset",
633 static int read_f(BlockDriverState *bs, int argc, char **argv)
635 struct timeval t1, t2;
636 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
637 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
642 /* Some compilers get confused and warn if this is not initialized. */
644 int pattern = 0, pattern_offset = 0, pattern_count = 0;
646 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
656 pattern_count = cvtnum(optarg);
657 if (pattern_count < 0) {
658 printf("non-numeric length argument -- %s\n", optarg);
667 pattern = parse_pattern(optarg);
677 pattern_offset = cvtnum(optarg);
678 if (pattern_offset < 0) {
679 printf("non-numeric length argument -- %s\n", optarg);
687 return qemuio_command_usage(&read_cmd);
691 if (optind != argc - 2) {
692 return qemuio_command_usage(&read_cmd);
695 if (bflag && pflag) {
696 printf("-b and -p cannot be specified at the same time\n");
700 offset = cvtnum(argv[optind]);
702 printf("non-numeric length argument -- %s\n", argv[optind]);
707 count = cvtnum(argv[optind]);
709 printf("non-numeric length argument -- %s\n", argv[optind]);
713 if (!Pflag && (lflag || sflag)) {
714 return qemuio_command_usage(&read_cmd);
718 pattern_count = count - pattern_offset;
721 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
722 printf("pattern verification range exceeds end of read data\n");
727 if (offset & 0x1ff) {
728 printf("offset %" PRId64 " is not sector aligned\n",
733 printf("count %d is not sector aligned\n",
739 buf = qemu_io_alloc(bs, count, 0xab);
741 gettimeofday(&t1, NULL);
743 cnt = do_pread(bs, buf, offset, count, &total);
745 cnt = do_load_vmstate(bs, buf, offset, count, &total);
747 cnt = do_read(bs, buf, offset, count, &total);
749 gettimeofday(&t2, NULL);
752 printf("read failed: %s\n", strerror(-cnt));
757 void *cmp_buf = g_malloc(pattern_count);
758 memset(cmp_buf, pattern, pattern_count);
759 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
760 printf("Pattern verification failed at offset %"
761 PRId64 ", %d bytes\n",
762 offset + pattern_offset, pattern_count);
772 dump_buffer(buf, offset, count);
775 /* Finally, report back -- -C gives a parsable format */
777 print_report("read", &t2, offset, count, total, cnt, Cflag);
785 static void readv_help(void)
789 " reads a range of bytes from the given offset into multiple buffers\n"
792 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
794 " Reads a segment of the currently open file, optionally dumping it to the\n"
795 " standard output stream (with -v option) for subsequent inspection.\n"
796 " Uses multiple iovec buffers if more than one byte range is specified.\n"
797 " -C, -- report statistics in a machine parsable format\n"
798 " -P, -- use a pattern to verify read data\n"
799 " -v, -- dump buffer to standard output\n"
800 " -q, -- quiet mode, do not show I/O statistics\n"
804 static int readv_f(BlockDriverState *bs, int argc, char **argv);
806 static const cmdinfo_t readv_cmd = {
811 .args = "[-Cqv] [-P pattern ] off len [len..]",
812 .oneline = "reads a number of bytes at a specified offset",
816 static int readv_f(BlockDriverState *bs, int argc, char **argv)
818 struct timeval t1, t2;
819 int Cflag = 0, qflag = 0, vflag = 0;
823 /* Some compilers get confused and warn if this is not initialized. */
830 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
837 pattern = parse_pattern(optarg);
849 return qemuio_command_usage(&readv_cmd);
853 if (optind > argc - 2) {
854 return qemuio_command_usage(&readv_cmd);
858 offset = cvtnum(argv[optind]);
860 printf("non-numeric length argument -- %s\n", argv[optind]);
865 if (offset & 0x1ff) {
866 printf("offset %" PRId64 " is not sector aligned\n",
871 nr_iov = argc - optind;
872 buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab);
877 gettimeofday(&t1, NULL);
878 cnt = do_aio_readv(bs, &qiov, offset, &total);
879 gettimeofday(&t2, NULL);
882 printf("readv failed: %s\n", strerror(-cnt));
887 void *cmp_buf = g_malloc(qiov.size);
888 memset(cmp_buf, pattern, qiov.size);
889 if (memcmp(buf, cmp_buf, qiov.size)) {
890 printf("Pattern verification failed at offset %"
891 PRId64 ", %zd bytes\n", offset, qiov.size);
901 dump_buffer(buf, offset, qiov.size);
904 /* Finally, report back -- -C gives a parsable format */
906 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
909 qemu_iovec_destroy(&qiov);
914 static void write_help(void)
918 " writes a range of bytes from the given offset\n"
921 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
923 " Writes into a segment of the currently open file, using a buffer\n"
924 " filled with a set pattern (0xcdcdcdcd).\n"
925 " -b, -- write to the VM state rather than the virtual disk\n"
926 " -c, -- write compressed data with bdrv_write_compressed\n"
927 " -p, -- use bdrv_pwrite to write the file\n"
928 " -P, -- use different pattern to fill file\n"
929 " -C, -- report statistics in a machine parsable format\n"
930 " -q, -- quiet mode, do not show I/O statistics\n"
931 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
935 static int write_f(BlockDriverState *bs, int argc, char **argv);
937 static const cmdinfo_t write_cmd = {
943 .args = "[-bcCpqz] [-P pattern ] off len",
944 .oneline = "writes a number of bytes at a specified offset",
948 static int write_f(BlockDriverState *bs, int argc, char **argv)
950 struct timeval t1, t2;
951 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
957 /* Some compilers get confused and warn if this is not initialized. */
961 while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
977 pattern = parse_pattern(optarg);
989 return qemuio_command_usage(&write_cmd);
993 if (optind != argc - 2) {
994 return qemuio_command_usage(&write_cmd);
997 if (bflag + pflag + zflag > 1) {
998 printf("-b, -p, or -z cannot be specified at the same time\n");
1002 if (zflag && Pflag) {
1003 printf("-z and -P cannot be specified at the same time\n");
1007 offset = cvtnum(argv[optind]);
1009 printf("non-numeric length argument -- %s\n", argv[optind]);
1014 count = cvtnum(argv[optind]);
1016 printf("non-numeric length argument -- %s\n", argv[optind]);
1021 if (offset & 0x1ff) {
1022 printf("offset %" PRId64 " is not sector aligned\n",
1027 if (count & 0x1ff) {
1028 printf("count %d is not sector aligned\n",
1035 buf = qemu_io_alloc(bs, count, pattern);
1038 gettimeofday(&t1, NULL);
1040 cnt = do_pwrite(bs, buf, offset, count, &total);
1042 cnt = do_save_vmstate(bs, buf, offset, count, &total);
1044 cnt = do_co_write_zeroes(bs, offset, count, &total);
1046 cnt = do_write_compressed(bs, buf, offset, count, &total);
1048 cnt = do_write(bs, buf, offset, count, &total);
1050 gettimeofday(&t2, NULL);
1053 printf("write failed: %s\n", strerror(-cnt));
1061 /* Finally, report back -- -C gives a parsable format */
1063 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1078 " writes a range of bytes from the given offset source from multiple buffers\n"
1081 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1083 " Writes into a segment of the currently open file, using a buffer\n"
1084 " filled with a set pattern (0xcdcdcdcd).\n"
1085 " -P, -- use different pattern to fill file\n"
1086 " -C, -- report statistics in a machine parsable format\n"
1087 " -q, -- quiet mode, do not show I/O statistics\n"
1091 static int writev_f(BlockDriverState *bs, int argc, char **argv);
1093 static const cmdinfo_t writev_cmd = {
1098 .args = "[-Cq] [-P pattern ] off len [len..]",
1099 .oneline = "writes a number of bytes at a specified offset",
1100 .help = writev_help,
1103 static int writev_f(BlockDriverState *bs, int argc, char **argv)
1105 struct timeval t1, t2;
1106 int Cflag = 0, qflag = 0;
1110 /* Some compilers get confused and warn if this is not initialized. */
1116 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1125 pattern = parse_pattern(optarg);
1131 return qemuio_command_usage(&writev_cmd);
1135 if (optind > argc - 2) {
1136 return qemuio_command_usage(&writev_cmd);
1139 offset = cvtnum(argv[optind]);
1141 printf("non-numeric length argument -- %s\n", argv[optind]);
1146 if (offset & 0x1ff) {
1147 printf("offset %" PRId64 " is not sector aligned\n",
1152 nr_iov = argc - optind;
1153 buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern);
1158 gettimeofday(&t1, NULL);
1159 cnt = do_aio_writev(bs, &qiov, offset, &total);
1160 gettimeofday(&t2, NULL);
1163 printf("writev failed: %s\n", strerror(-cnt));
1171 /* Finally, report back -- -C gives a parsable format */
1173 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1175 qemu_iovec_destroy(&qiov);
1180 static void multiwrite_help(void)
1184 " writes a range of bytes from the given offset source from multiple buffers,\n"
1185 " in a batch of requests that may be merged by qemu\n"
1188 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1189 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1191 " Writes into a segment of the currently open file, using a buffer\n"
1192 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1193 " by one for each request contained in the multiwrite command.\n"
1194 " -P, -- use different pattern to fill file\n"
1195 " -C, -- report statistics in a machine parsable format\n"
1196 " -q, -- quiet mode, do not show I/O statistics\n"
1200 static int multiwrite_f(BlockDriverState *bs, int argc, char **argv);
1202 static const cmdinfo_t multiwrite_cmd = {
1203 .name = "multiwrite",
1204 .cfunc = multiwrite_f,
1207 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1208 .oneline = "issues multiple write requests at once",
1209 .help = multiwrite_help,
1212 static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
1214 struct timeval t1, t2;
1215 int Cflag = 0, qflag = 0;
1218 int64_t offset, first_offset = 0;
1219 /* Some compilers get confused and warn if this is not initialized. */
1224 QEMUIOVector *qiovs;
1228 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1237 pattern = parse_pattern(optarg);
1243 return qemuio_command_usage(&writev_cmd);
1247 if (optind > argc - 2) {
1248 return qemuio_command_usage(&writev_cmd);
1252 for (i = optind; i < argc; i++) {
1253 if (!strcmp(argv[i], ";")) {
1258 reqs = g_new0(BlockRequest, nr_reqs);
1259 buf = g_new0(char *, nr_reqs);
1260 qiovs = g_new(QEMUIOVector, nr_reqs);
1262 for (i = 0; i < nr_reqs && optind < argc; i++) {
1265 /* Read the offset of the request */
1266 offset = cvtnum(argv[optind]);
1268 printf("non-numeric offset argument -- %s\n", argv[optind]);
1273 if (offset & 0x1ff) {
1274 printf("offset %lld is not sector aligned\n",
1280 first_offset = offset;
1283 /* Read lengths for qiov entries */
1284 for (j = optind; j < argc; j++) {
1285 if (!strcmp(argv[j], ";")) {
1290 nr_iov = j - optind;
1293 buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern);
1294 if (buf[i] == NULL) {
1298 reqs[i].qiov = &qiovs[i];
1299 reqs[i].sector = offset >> 9;
1300 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1307 /* If there were empty requests at the end, ignore them */
1310 gettimeofday(&t1, NULL);
1311 cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total);
1312 gettimeofday(&t2, NULL);
1315 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1323 /* Finally, report back -- -C gives a parsable format */
1325 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1327 for (i = 0; i < nr_reqs; i++) {
1328 qemu_io_free(buf[i]);
1329 if (reqs[i].qiov != NULL) {
1330 qemu_iovec_destroy(&qiovs[i]);
1351 static void aio_write_done(void *opaque, int ret)
1353 struct aio_ctx *ctx = opaque;
1356 gettimeofday(&t2, NULL);
1360 printf("aio_write failed: %s\n", strerror(-ret));
1368 /* Finally, report back -- -C gives a parsable format */
1369 t2 = tsub(t2, ctx->t1);
1370 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1371 ctx->qiov.size, 1, ctx->Cflag);
1373 qemu_io_free(ctx->buf);
1374 qemu_iovec_destroy(&ctx->qiov);
1378 static void aio_read_done(void *opaque, int ret)
1380 struct aio_ctx *ctx = opaque;
1383 gettimeofday(&t2, NULL);
1386 printf("readv failed: %s\n", strerror(-ret));
1391 void *cmp_buf = g_malloc(ctx->qiov.size);
1393 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1394 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1395 printf("Pattern verification failed at offset %"
1396 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1406 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1409 /* Finally, report back -- -C gives a parsable format */
1410 t2 = tsub(t2, ctx->t1);
1411 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1412 ctx->qiov.size, 1, ctx->Cflag);
1414 qemu_io_free(ctx->buf);
1415 qemu_iovec_destroy(&ctx->qiov);
1419 static void aio_read_help(void)
1423 " asynchronously reads a range of bytes from the given offset\n"
1426 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1428 " Reads a segment of the currently open file, optionally dumping it to the\n"
1429 " standard output stream (with -v option) for subsequent inspection.\n"
1430 " The read is performed asynchronously and the aio_flush command must be\n"
1431 " used to ensure all outstanding aio requests have been completed.\n"
1432 " -C, -- report statistics in a machine parsable format\n"
1433 " -P, -- use a pattern to verify read data\n"
1434 " -v, -- dump buffer to standard output\n"
1435 " -q, -- quiet mode, do not show I/O statistics\n"
1439 static int aio_read_f(BlockDriverState *bs, int argc, char **argv);
1441 static const cmdinfo_t aio_read_cmd = {
1443 .cfunc = aio_read_f,
1446 .args = "[-Cqv] [-P pattern ] off len [len..]",
1447 .oneline = "asynchronously reads a number of bytes",
1448 .help = aio_read_help,
1451 static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
1454 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1456 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1463 ctx->pattern = parse_pattern(optarg);
1464 if (ctx->pattern < 0) {
1477 return qemuio_command_usage(&aio_read_cmd);
1481 if (optind > argc - 2) {
1483 return qemuio_command_usage(&aio_read_cmd);
1486 ctx->offset = cvtnum(argv[optind]);
1487 if (ctx->offset < 0) {
1488 printf("non-numeric length argument -- %s\n", argv[optind]);
1494 if (ctx->offset & 0x1ff) {
1495 printf("offset %" PRId64 " is not sector aligned\n",
1501 nr_iov = argc - optind;
1502 ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1503 if (ctx->buf == NULL) {
1508 gettimeofday(&ctx->t1, NULL);
1509 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1510 ctx->qiov.size >> 9, aio_read_done, ctx);
1514 static void aio_write_help(void)
1518 " asynchronously writes a range of bytes from the given offset source\n"
1519 " from multiple buffers\n"
1522 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1524 " Writes into a segment of the currently open file, using a buffer\n"
1525 " filled with a set pattern (0xcdcdcdcd).\n"
1526 " The write is performed asynchronously and the aio_flush command must be\n"
1527 " used to ensure all outstanding aio requests have been completed.\n"
1528 " -P, -- use different pattern to fill file\n"
1529 " -C, -- report statistics in a machine parsable format\n"
1530 " -q, -- quiet mode, do not show I/O statistics\n"
1534 static int aio_write_f(BlockDriverState *bs, int argc, char **argv);
1536 static const cmdinfo_t aio_write_cmd = {
1537 .name = "aio_write",
1538 .cfunc = aio_write_f,
1541 .args = "[-Cq] [-P pattern ] off len [len..]",
1542 .oneline = "asynchronously writes a number of bytes",
1543 .help = aio_write_help,
1546 static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
1550 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1552 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1561 pattern = parse_pattern(optarg);
1569 return qemuio_command_usage(&aio_write_cmd);
1573 if (optind > argc - 2) {
1575 return qemuio_command_usage(&aio_write_cmd);
1578 ctx->offset = cvtnum(argv[optind]);
1579 if (ctx->offset < 0) {
1580 printf("non-numeric length argument -- %s\n", argv[optind]);
1586 if (ctx->offset & 0x1ff) {
1587 printf("offset %" PRId64 " is not sector aligned\n",
1593 nr_iov = argc - optind;
1594 ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern);
1595 if (ctx->buf == NULL) {
1600 gettimeofday(&ctx->t1, NULL);
1601 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1602 ctx->qiov.size >> 9, aio_write_done, ctx);
1606 static int aio_flush_f(BlockDriverState *bs, int argc, char **argv)
1612 static const cmdinfo_t aio_flush_cmd = {
1613 .name = "aio_flush",
1614 .cfunc = aio_flush_f,
1615 .oneline = "completes all outstanding aio requests"
1618 static int flush_f(BlockDriverState *bs, int argc, char **argv)
1624 static const cmdinfo_t flush_cmd = {
1628 .oneline = "flush all in-core file state to disk",
1631 static int truncate_f(BlockDriverState *bs, int argc, char **argv)
1636 offset = cvtnum(argv[1]);
1638 printf("non-numeric truncate argument -- %s\n", argv[1]);
1642 ret = bdrv_truncate(bs, offset);
1644 printf("truncate: %s\n", strerror(-ret));
1651 static const cmdinfo_t truncate_cmd = {
1654 .cfunc = truncate_f,
1658 .oneline = "truncates the current file at the given offset",
1661 static int length_f(BlockDriverState *bs, int argc, char **argv)
1666 size = bdrv_getlength(bs);
1668 printf("getlength: %s\n", strerror(-size));
1672 cvtstr(size, s1, sizeof(s1));
1678 static const cmdinfo_t length_cmd = {
1682 .oneline = "gets the length of the current file",
1686 static int info_f(BlockDriverState *bs, int argc, char **argv)
1688 BlockDriverInfo bdi;
1689 ImageInfoSpecific *spec_info;
1690 char s1[64], s2[64];
1693 if (bs->drv && bs->drv->format_name) {
1694 printf("format name: %s\n", bs->drv->format_name);
1696 if (bs->drv && bs->drv->protocol_name) {
1697 printf("format name: %s\n", bs->drv->protocol_name);
1700 ret = bdrv_get_info(bs, &bdi);
1705 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1706 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1708 printf("cluster size: %s\n", s1);
1709 printf("vm state offset: %s\n", s2);
1711 spec_info = bdrv_get_specific_info(bs);
1713 printf("Format specific information:\n");
1714 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1715 qapi_free_ImageInfoSpecific(spec_info);
1723 static const cmdinfo_t info_cmd = {
1727 .oneline = "prints information about the current file",
1730 static void discard_help(void)
1734 " discards a range of bytes from the given offset\n"
1737 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1739 " Discards a segment of the currently open file.\n"
1740 " -C, -- report statistics in a machine parsable format\n"
1741 " -q, -- quiet mode, do not show I/O statistics\n"
1745 static int discard_f(BlockDriverState *bs, int argc, char **argv);
1747 static const cmdinfo_t discard_cmd = {
1753 .args = "[-Cq] off len",
1754 .oneline = "discards a number of bytes at a specified offset",
1755 .help = discard_help,
1758 static int discard_f(BlockDriverState *bs, int argc, char **argv)
1760 struct timeval t1, t2;
1761 int Cflag = 0, qflag = 0;
1766 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1775 return qemuio_command_usage(&discard_cmd);
1779 if (optind != argc - 2) {
1780 return qemuio_command_usage(&discard_cmd);
1783 offset = cvtnum(argv[optind]);
1785 printf("non-numeric length argument -- %s\n", argv[optind]);
1790 count = cvtnum(argv[optind]);
1792 printf("non-numeric length argument -- %s\n", argv[optind]);
1796 gettimeofday(&t1, NULL);
1797 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1798 count >> BDRV_SECTOR_BITS);
1799 gettimeofday(&t2, NULL);
1802 printf("discard failed: %s\n", strerror(-ret));
1806 /* Finally, report back -- -C gives a parsable format */
1809 print_report("discard", &t2, offset, count, count, 1, Cflag);
1816 static int alloc_f(BlockDriverState *bs, int argc, char **argv)
1818 int64_t offset, sector_num;
1819 int nb_sectors, remaining;
1824 offset = cvtnum(argv[1]);
1826 printf("non-numeric offset argument -- %s\n", argv[1]);
1828 } else if (offset & 0x1ff) {
1829 printf("offset %" PRId64 " is not sector aligned\n",
1835 nb_sectors = cvtnum(argv[2]);
1836 if (nb_sectors < 0) {
1837 printf("non-numeric length argument -- %s\n", argv[2]);
1844 remaining = nb_sectors;
1846 sector_num = offset >> 9;
1848 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1850 printf("is_allocated failed: %s\n", strerror(-ret));
1859 nb_sectors -= remaining;
1864 cvtstr(offset, s1, sizeof(s1));
1866 printf("%d/%d sectors allocated at offset %s\n",
1867 sum_alloc, nb_sectors, s1);
1871 static const cmdinfo_t alloc_cmd = {
1877 .args = "off [sectors]",
1878 .oneline = "checks if a sector is present in the file",
1882 static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1883 int64_t nb_sectors, int64_t *pnum)
1885 int num, num_checked;
1888 num_checked = MIN(nb_sectors, INT_MAX);
1889 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1897 while (nb_sectors > 0 && ret == firstret) {
1901 num_checked = MIN(nb_sectors, INT_MAX);
1902 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1903 if (ret == firstret && num) {
1913 static int map_f(BlockDriverState *bs, int argc, char **argv)
1923 nb_sectors = bs->total_sectors;
1926 ret = map_is_allocated(bs, offset, nb_sectors, &num);
1928 error_report("Failed to get allocation status: %s", strerror(-ret));
1931 error_report("Unexpected end of image");
1935 retstr = ret ? " allocated" : "not allocated";
1936 cvtstr(offset << 9ULL, s1, sizeof(s1));
1937 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1938 "at offset %s (%d)\n",
1939 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1943 } while (offset < bs->total_sectors);
1948 static const cmdinfo_t map_cmd = {
1954 .oneline = "prints the allocated areas of a file",
1957 static int break_f(BlockDriverState *bs, int argc, char **argv)
1961 ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
1963 printf("Could not set breakpoint: %s\n", strerror(-ret));
1969 static int remove_break_f(BlockDriverState *bs, int argc, char **argv)
1973 ret = bdrv_debug_remove_breakpoint(bs, argv[1]);
1975 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
1981 static const cmdinfo_t break_cmd = {
1986 .args = "event tag",
1987 .oneline = "sets a breakpoint on event and tags the stopped "
1991 static const cmdinfo_t remove_break_cmd = {
1992 .name = "remove_break",
1995 .cfunc = remove_break_f,
1997 .oneline = "remove a breakpoint by tag",
2000 static int resume_f(BlockDriverState *bs, int argc, char **argv)
2004 ret = bdrv_debug_resume(bs, argv[1]);
2006 printf("Could not resume request: %s\n", strerror(-ret));
2012 static const cmdinfo_t resume_cmd = {
2018 .oneline = "resumes the request tagged as tag",
2021 static int wait_break_f(BlockDriverState *bs, int argc, char **argv)
2023 while (!bdrv_debug_is_suspended(bs, argv[1])) {
2024 aio_poll(bdrv_get_aio_context(bs), true);
2030 static const cmdinfo_t wait_break_cmd = {
2031 .name = "wait_break",
2034 .cfunc = wait_break_f,
2036 .oneline = "waits for the suspension of a request",
2039 static int abort_f(BlockDriverState *bs, int argc, char **argv)
2044 static const cmdinfo_t abort_cmd = {
2047 .flags = CMD_NOFILE_OK,
2048 .oneline = "simulate a program crash using abort(3)",
2051 static void sigraise_help(void)
2055 " raises the given signal\n"
2058 " 'sigraise %i' - raises SIGTERM\n"
2060 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2061 " given to sigraise.\n"
2065 static int sigraise_f(BlockDriverState *bs, int argc, char **argv);
2067 static const cmdinfo_t sigraise_cmd = {
2069 .cfunc = sigraise_f,
2072 .flags = CMD_NOFILE_OK,
2074 .oneline = "raises a signal",
2075 .help = sigraise_help,
2078 static int sigraise_f(BlockDriverState *bs, int argc, char **argv)
2080 int sig = cvtnum(argv[1]);
2082 printf("non-numeric signal number argument -- %s\n", argv[1]);
2086 /* Using raise() to kill this process does not necessarily flush all open
2087 * streams. At least stdout and stderr (although the latter should be
2088 * non-buffered anyway) should be flushed, though. */
2096 static void sleep_cb(void *opaque)
2098 bool *expired = opaque;
2102 static int sleep_f(BlockDriverState *bs, int argc, char **argv)
2106 struct QEMUTimer *timer;
2107 bool expired = false;
2109 ms = strtol(argv[1], &endptr, 0);
2110 if (ms < 0 || *endptr != '\0') {
2111 printf("%s is not a valid number\n", argv[1]);
2115 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2116 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2119 main_loop_wait(false);
2127 static const cmdinfo_t sleep_cmd = {
2132 .flags = CMD_NOFILE_OK,
2133 .oneline = "waits for the given value in milliseconds",
2136 static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2141 printf("%s ", ct->name);
2143 printf("(or %s) ", ct->altname);
2148 printf("%s ", ct->args);
2150 printf("-- %s\n", ct->oneline);
2153 static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2155 help_oneline(cmd, ct);
2161 static void help_all(void)
2163 const cmdinfo_t *ct;
2165 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2166 help_oneline(ct->name, ct);
2168 printf("\nUse 'help commandname' for extended help.\n");
2171 static int help_f(BlockDriverState *bs, int argc, char **argv)
2173 const cmdinfo_t *ct;
2180 ct = find_command(argv[1]);
2182 printf("command %s not found\n", argv[1]);
2186 help_onecmd(argv[1], ct);
2190 static const cmdinfo_t help_cmd = {
2196 .flags = CMD_FLAG_GLOBAL,
2197 .args = "[command]",
2198 .oneline = "help for one or all commands",
2201 bool qemuio_command(BlockDriverState *bs, const char *cmd)
2204 const cmdinfo_t *ct;
2209 input = g_strdup(cmd);
2210 v = breakline(input, &c);
2212 ct = find_command(v[0]);
2214 done = command(bs, ct, c, v);
2216 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2225 static void __attribute((constructor)) init_qemuio_commands(void)
2227 /* initialize commands */
2228 qemuio_add_command(&help_cmd);
2229 qemuio_add_command(&read_cmd);
2230 qemuio_add_command(&readv_cmd);
2231 qemuio_add_command(&write_cmd);
2232 qemuio_add_command(&writev_cmd);
2233 qemuio_add_command(&multiwrite_cmd);
2234 qemuio_add_command(&aio_read_cmd);
2235 qemuio_add_command(&aio_write_cmd);
2236 qemuio_add_command(&aio_flush_cmd);
2237 qemuio_add_command(&flush_cmd);
2238 qemuio_add_command(&truncate_cmd);
2239 qemuio_add_command(&length_cmd);
2240 qemuio_add_command(&info_cmd);
2241 qemuio_add_command(&discard_cmd);
2242 qemuio_add_command(&alloc_cmd);
2243 qemuio_add_command(&map_cmd);
2244 qemuio_add_command(&break_cmd);
2245 qemuio_add_command(&remove_break_cmd);
2246 qemuio_add_command(&resume_cmd);
2247 qemuio_add_command(&wait_break_cmd);
2248 qemuio_add_command(&abort_cmd);
2249 qemuio_add_command(&sleep_cmd);
2250 qemuio_add_command(&sigraise_cmd);