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"
14 #define CMD_NOFILE_OK 0x01
18 static cmdinfo_t *cmdtab;
21 static int compare_cmdname(const void *a, const void *b)
23 return strcmp(((const cmdinfo_t *)a)->name,
24 ((const cmdinfo_t *)b)->name);
27 void qemuio_add_command(const cmdinfo_t *ci)
29 cmdtab = g_realloc(cmdtab, ++ncmds * sizeof(*cmdtab));
30 cmdtab[ncmds - 1] = *ci;
31 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
34 int qemuio_command_usage(const cmdinfo_t *ci)
36 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
40 static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
42 if (ct->flags & CMD_FLAG_GLOBAL) {
45 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
46 fprintf(stderr, "no file open, try 'help open'\n");
52 static int command(BlockDriverState *bs, const cmdinfo_t *ct, int argc,
57 if (!init_check_command(bs, ct)) {
61 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
62 if (ct->argmax == -1) {
64 "bad argument count %d to %s, expected at least %d arguments\n",
65 argc-1, cmd, ct->argmin);
66 } else if (ct->argmin == ct->argmax) {
68 "bad argument count %d to %s, expected %d arguments\n",
69 argc-1, cmd, ct->argmin);
72 "bad argument count %d to %s, expected between %d and %d arguments\n",
73 argc-1, cmd, ct->argmin, ct->argmax);
78 return ct->cfunc(bs, argc, argv);
81 static const cmdinfo_t *find_command(const char *cmd)
85 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
86 if (strcmp(ct->name, cmd) == 0 ||
87 (ct->altname && strcmp(ct->altname, cmd) == 0))
89 return (const cmdinfo_t *)ct;
95 static char **breakline(char *input, int *count)
99 char **rval = g_malloc0(sizeof(char *));
102 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
107 tmp = g_realloc(rval, sizeof(*rval) * (c + 1));
123 static int64_t cvtnum(const char *s)
126 return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
129 #define EXABYTES(x) ((long long)(x) << 60)
130 #define PETABYTES(x) ((long long)(x) << 50)
131 #define TERABYTES(x) ((long long)(x) << 40)
132 #define GIGABYTES(x) ((long long)(x) << 30)
133 #define MEGABYTES(x) ((long long)(x) << 20)
134 #define KILOBYTES(x) ((long long)(x) << 10)
136 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
137 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
138 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
139 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
140 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
141 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
143 static void cvtstr(double value, char *str, size_t size)
148 if (value >= EXABYTES(1)) {
150 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
151 } else if (value >= PETABYTES(1)) {
153 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
154 } else if (value >= TERABYTES(1)) {
156 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
157 } else if (value >= GIGABYTES(1)) {
159 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
160 } else if (value >= MEGABYTES(1)) {
162 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
163 } else if (value >= KILOBYTES(1)) {
165 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
168 snprintf(str, size - 6, "%f", value);
171 trim = strstr(str, ".000");
173 strcpy(trim, suffix);
181 static struct timeval tsub(struct timeval t1, struct timeval t2)
183 t1.tv_usec -= t2.tv_usec;
184 if (t1.tv_usec < 0) {
185 t1.tv_usec += 1000000;
188 t1.tv_sec -= t2.tv_sec;
192 static double tdiv(double value, struct timeval tv)
194 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
197 #define HOURS(sec) ((sec) / (60 * 60))
198 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
199 #define SECONDS(sec) ((sec) % 60)
203 TERSE_FIXED_TIME = 0x1,
204 VERBOSE_FIXED_TIME = 0x2,
207 static void timestr(struct timeval *tv, char *ts, size_t size, int format)
209 double usec = (double)tv->tv_usec / 1000000.0;
211 if (format & TERSE_FIXED_TIME) {
212 if (!HOURS(tv->tv_sec)) {
213 snprintf(ts, size, "%u:%02u.%02u",
214 (unsigned int) MINUTES(tv->tv_sec),
215 (unsigned int) SECONDS(tv->tv_sec),
216 (unsigned int) (usec * 100));
219 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
222 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
223 snprintf(ts, size, "%u:%02u:%02u.%02u",
224 (unsigned int) HOURS(tv->tv_sec),
225 (unsigned int) MINUTES(tv->tv_sec),
226 (unsigned int) SECONDS(tv->tv_sec),
227 (unsigned int) (usec * 100));
229 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
234 * Parse the pattern argument to various sub-commands.
236 * Because the pattern is used as an argument to memset it must evaluate
237 * to an unsigned integer that fits into a single byte.
239 static int parse_pattern(const char *arg)
244 pattern = strtol(arg, &endptr, 0);
245 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
246 printf("%s is not a valid pattern byte\n", arg);
254 * Memory allocation helpers.
256 * Make sure memory is aligned by default, or purposefully misaligned if
257 * that is specified on the command line.
260 #define MISALIGN_OFFSET 16
261 static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern)
265 if (qemuio_misalign) {
266 len += MISALIGN_OFFSET;
268 buf = qemu_blockalign(bs, len);
269 memset(buf, pattern, len);
270 if (qemuio_misalign) {
271 buf += MISALIGN_OFFSET;
276 static void qemu_io_free(void *p)
278 if (qemuio_misalign) {
279 p -= MISALIGN_OFFSET;
284 static void dump_buffer(const void *buffer, int64_t offset, int len)
289 for (i = 0, p = buffer; i < len; i += 16) {
290 const uint8_t *s = p;
292 printf("%08" PRIx64 ": ", offset + i);
293 for (j = 0; j < 16 && i + j < len; j++, p++) {
297 for (j = 0; j < 16 && i + j < len; j++, s++) {
308 static void print_report(const char *op, struct timeval *t, int64_t offset,
309 int count, int total, int cnt, int Cflag)
311 char s1[64], s2[64], ts[64];
313 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
315 cvtstr((double)total, s1, sizeof(s1));
316 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
317 printf("%s %d/%d bytes at offset %" PRId64 "\n",
318 op, total, count, offset);
319 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
320 s1, cnt, ts, s2, tdiv((double)cnt, *t));
321 } else {/* bytes,ops,time,bytes/sec,ops/sec */
322 printf("%d,%d,%s,%.3f,%.3f\n",
324 tdiv((double)total, *t),
325 tdiv((double)cnt, *t));
330 * Parse multiple length statements for vectored I/O, and construct an I/O
331 * vector matching it.
334 create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov,
337 size_t *sizes = g_new0(size_t, nr_iov);
343 for (i = 0; i < nr_iov; i++) {
349 printf("non-numeric length argument -- %s\n", arg);
353 /* should be SIZE_T_MAX, but that doesn't exist */
355 printf("too large length argument -- %s\n", arg);
360 printf("length argument %" PRId64
361 " is not sector aligned\n", len);
369 qemu_iovec_init(qiov, nr_iov);
371 buf = p = qemu_io_alloc(bs, count, pattern);
373 for (i = 0; i < nr_iov; i++) {
374 qemu_iovec_add(qiov, p, sizes[i]);
383 static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
388 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
396 static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
401 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
409 static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
412 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
419 static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
422 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
430 BlockDriverState *bs;
438 static void coroutine_fn co_write_zeroes_entry(void *opaque)
440 CoWriteZeroes *data = opaque;
442 data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
443 data->count / BDRV_SECTOR_SIZE);
446 *data->total = data->ret;
450 *data->total = data->count;
453 static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
457 CoWriteZeroes data = {
465 co = qemu_coroutine_create(co_write_zeroes_entry);
466 qemu_coroutine_enter(co, &data);
477 static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
478 int count, int *total)
482 ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
490 static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
491 int count, int *total)
493 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
500 static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
501 int count, int *total)
503 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
510 #define NOT_DONE 0x7fffffff
511 static void aio_rw_done(void *opaque, int ret)
513 *(int *)opaque = ret;
516 static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov,
517 int64_t offset, int *total)
519 int async_ret = NOT_DONE;
521 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
522 aio_rw_done, &async_ret);
523 while (async_ret == NOT_DONE) {
524 main_loop_wait(false);
528 return async_ret < 0 ? async_ret : 1;
531 static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov,
532 int64_t offset, int *total)
534 int async_ret = NOT_DONE;
536 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
537 aio_rw_done, &async_ret);
538 while (async_ret == NOT_DONE) {
539 main_loop_wait(false);
543 return async_ret < 0 ? async_ret : 1;
546 struct multiwrite_async_ret {
551 static void multiwrite_cb(void *opaque, int ret)
553 struct multiwrite_async_ret *async_ret = opaque;
555 async_ret->num_done++;
557 async_ret->error = ret;
561 static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs,
562 int num_reqs, int *total)
565 struct multiwrite_async_ret async_ret = {
571 for (i = 0; i < num_reqs; i++) {
572 reqs[i].cb = multiwrite_cb;
573 reqs[i].opaque = &async_ret;
574 *total += reqs[i].qiov->size;
577 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
582 while (async_ret.num_done < num_reqs) {
583 main_loop_wait(false);
586 return async_ret.error < 0 ? async_ret.error : 1;
589 static void read_help(void)
593 " reads a range of bytes from the given offset\n"
596 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
598 " Reads a segment of the currently open file, optionally dumping it to the\n"
599 " standard output stream (with -v option) for subsequent inspection.\n"
600 " -b, -- read from the VM state rather than the virtual disk\n"
601 " -C, -- report statistics in a machine parsable format\n"
602 " -l, -- length for pattern verification (only with -P)\n"
603 " -p, -- use bdrv_pread to read the file\n"
604 " -P, -- use a pattern to verify read data\n"
605 " -q, -- quiet mode, do not show I/O statistics\n"
606 " -s, -- start offset for pattern verification (only with -P)\n"
607 " -v, -- dump buffer to standard output\n"
611 static int read_f(BlockDriverState *bs, int argc, char **argv);
613 static const cmdinfo_t read_cmd = {
619 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
620 .oneline = "reads a number of bytes at a specified offset",
624 static int read_f(BlockDriverState *bs, int argc, char **argv)
626 struct timeval t1, t2;
627 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
628 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
633 /* Some compilers get confused and warn if this is not initialized. */
635 int pattern = 0, pattern_offset = 0, pattern_count = 0;
637 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
647 pattern_count = cvtnum(optarg);
648 if (pattern_count < 0) {
649 printf("non-numeric length argument -- %s\n", optarg);
658 pattern = parse_pattern(optarg);
668 pattern_offset = cvtnum(optarg);
669 if (pattern_offset < 0) {
670 printf("non-numeric length argument -- %s\n", optarg);
678 return qemuio_command_usage(&read_cmd);
682 if (optind != argc - 2) {
683 return qemuio_command_usage(&read_cmd);
686 if (bflag && pflag) {
687 printf("-b and -p cannot be specified at the same time\n");
691 offset = cvtnum(argv[optind]);
693 printf("non-numeric length argument -- %s\n", argv[optind]);
698 count = cvtnum(argv[optind]);
700 printf("non-numeric length argument -- %s\n", argv[optind]);
704 if (!Pflag && (lflag || sflag)) {
705 return qemuio_command_usage(&read_cmd);
709 pattern_count = count - pattern_offset;
712 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
713 printf("pattern verification range exceeds end of read data\n");
718 if (offset & 0x1ff) {
719 printf("offset %" PRId64 " is not sector aligned\n",
724 printf("count %d is not sector aligned\n",
730 buf = qemu_io_alloc(bs, count, 0xab);
732 gettimeofday(&t1, NULL);
734 cnt = do_pread(bs, buf, offset, count, &total);
736 cnt = do_load_vmstate(bs, buf, offset, count, &total);
738 cnt = do_read(bs, buf, offset, count, &total);
740 gettimeofday(&t2, NULL);
743 printf("read failed: %s\n", strerror(-cnt));
748 void *cmp_buf = g_malloc(pattern_count);
749 memset(cmp_buf, pattern, pattern_count);
750 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
751 printf("Pattern verification failed at offset %"
752 PRId64 ", %d bytes\n",
753 offset + pattern_offset, pattern_count);
763 dump_buffer(buf, offset, count);
766 /* Finally, report back -- -C gives a parsable format */
768 print_report("read", &t2, offset, count, total, cnt, Cflag);
776 static void readv_help(void)
780 " reads a range of bytes from the given offset into multiple buffers\n"
783 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
785 " Reads a segment of the currently open file, optionally dumping it to the\n"
786 " standard output stream (with -v option) for subsequent inspection.\n"
787 " Uses multiple iovec buffers if more than one byte range is specified.\n"
788 " -C, -- report statistics in a machine parsable format\n"
789 " -P, -- use a pattern to verify read data\n"
790 " -v, -- dump buffer to standard output\n"
791 " -q, -- quiet mode, do not show I/O statistics\n"
795 static int readv_f(BlockDriverState *bs, int argc, char **argv);
797 static const cmdinfo_t readv_cmd = {
802 .args = "[-Cqv] [-P pattern ] off len [len..]",
803 .oneline = "reads a number of bytes at a specified offset",
807 static int readv_f(BlockDriverState *bs, int argc, char **argv)
809 struct timeval t1, t2;
810 int Cflag = 0, qflag = 0, vflag = 0;
814 /* Some compilers get confused and warn if this is not initialized. */
821 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
828 pattern = parse_pattern(optarg);
840 return qemuio_command_usage(&readv_cmd);
844 if (optind > argc - 2) {
845 return qemuio_command_usage(&readv_cmd);
849 offset = cvtnum(argv[optind]);
851 printf("non-numeric length argument -- %s\n", argv[optind]);
856 if (offset & 0x1ff) {
857 printf("offset %" PRId64 " is not sector aligned\n",
862 nr_iov = argc - optind;
863 buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab);
868 gettimeofday(&t1, NULL);
869 cnt = do_aio_readv(bs, &qiov, offset, &total);
870 gettimeofday(&t2, NULL);
873 printf("readv failed: %s\n", strerror(-cnt));
878 void *cmp_buf = g_malloc(qiov.size);
879 memset(cmp_buf, pattern, qiov.size);
880 if (memcmp(buf, cmp_buf, qiov.size)) {
881 printf("Pattern verification failed at offset %"
882 PRId64 ", %zd bytes\n", offset, qiov.size);
892 dump_buffer(buf, offset, qiov.size);
895 /* Finally, report back -- -C gives a parsable format */
897 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
900 qemu_iovec_destroy(&qiov);
905 static void write_help(void)
909 " writes a range of bytes from the given offset\n"
912 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
914 " Writes into a segment of the currently open file, using a buffer\n"
915 " filled with a set pattern (0xcdcdcdcd).\n"
916 " -b, -- write to the VM state rather than the virtual disk\n"
917 " -c, -- write compressed data with bdrv_write_compressed\n"
918 " -p, -- use bdrv_pwrite to write the file\n"
919 " -P, -- use different pattern to fill file\n"
920 " -C, -- report statistics in a machine parsable format\n"
921 " -q, -- quiet mode, do not show I/O statistics\n"
922 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
926 static int write_f(BlockDriverState *bs, int argc, char **argv);
928 static const cmdinfo_t write_cmd = {
934 .args = "[-bcCpqz] [-P pattern ] off len",
935 .oneline = "writes a number of bytes at a specified offset",
939 static int write_f(BlockDriverState *bs, int argc, char **argv)
941 struct timeval t1, t2;
942 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
948 /* Some compilers get confused and warn if this is not initialized. */
952 while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
968 pattern = parse_pattern(optarg);
980 return qemuio_command_usage(&write_cmd);
984 if (optind != argc - 2) {
985 return qemuio_command_usage(&write_cmd);
988 if (bflag + pflag + zflag > 1) {
989 printf("-b, -p, or -z cannot be specified at the same time\n");
993 if (zflag && Pflag) {
994 printf("-z and -P cannot be specified at the same time\n");
998 offset = cvtnum(argv[optind]);
1000 printf("non-numeric length argument -- %s\n", argv[optind]);
1005 count = cvtnum(argv[optind]);
1007 printf("non-numeric length argument -- %s\n", argv[optind]);
1012 if (offset & 0x1ff) {
1013 printf("offset %" PRId64 " is not sector aligned\n",
1018 if (count & 0x1ff) {
1019 printf("count %d is not sector aligned\n",
1026 buf = qemu_io_alloc(bs, count, pattern);
1029 gettimeofday(&t1, NULL);
1031 cnt = do_pwrite(bs, buf, offset, count, &total);
1033 cnt = do_save_vmstate(bs, buf, offset, count, &total);
1035 cnt = do_co_write_zeroes(bs, offset, count, &total);
1037 cnt = do_write_compressed(bs, buf, offset, count, &total);
1039 cnt = do_write(bs, buf, offset, count, &total);
1041 gettimeofday(&t2, NULL);
1044 printf("write failed: %s\n", strerror(-cnt));
1052 /* Finally, report back -- -C gives a parsable format */
1054 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1069 " writes a range of bytes from the given offset source from multiple buffers\n"
1072 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1074 " Writes into a segment of the currently open file, using a buffer\n"
1075 " filled with a set pattern (0xcdcdcdcd).\n"
1076 " -P, -- use different pattern to fill file\n"
1077 " -C, -- report statistics in a machine parsable format\n"
1078 " -q, -- quiet mode, do not show I/O statistics\n"
1082 static int writev_f(BlockDriverState *bs, int argc, char **argv);
1084 static const cmdinfo_t writev_cmd = {
1089 .args = "[-Cq] [-P pattern ] off len [len..]",
1090 .oneline = "writes a number of bytes at a specified offset",
1091 .help = writev_help,
1094 static int writev_f(BlockDriverState *bs, int argc, char **argv)
1096 struct timeval t1, t2;
1097 int Cflag = 0, qflag = 0;
1101 /* Some compilers get confused and warn if this is not initialized. */
1107 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1116 pattern = parse_pattern(optarg);
1122 return qemuio_command_usage(&writev_cmd);
1126 if (optind > argc - 2) {
1127 return qemuio_command_usage(&writev_cmd);
1130 offset = cvtnum(argv[optind]);
1132 printf("non-numeric length argument -- %s\n", argv[optind]);
1137 if (offset & 0x1ff) {
1138 printf("offset %" PRId64 " is not sector aligned\n",
1143 nr_iov = argc - optind;
1144 buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern);
1149 gettimeofday(&t1, NULL);
1150 cnt = do_aio_writev(bs, &qiov, offset, &total);
1151 gettimeofday(&t2, NULL);
1154 printf("writev failed: %s\n", strerror(-cnt));
1162 /* Finally, report back -- -C gives a parsable format */
1164 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1166 qemu_iovec_destroy(&qiov);
1171 static void multiwrite_help(void)
1175 " writes a range of bytes from the given offset source from multiple buffers,\n"
1176 " in a batch of requests that may be merged by qemu\n"
1179 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1180 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1182 " Writes into a segment of the currently open file, using a buffer\n"
1183 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1184 " by one for each request contained in the multiwrite command.\n"
1185 " -P, -- use different pattern to fill file\n"
1186 " -C, -- report statistics in a machine parsable format\n"
1187 " -q, -- quiet mode, do not show I/O statistics\n"
1191 static int multiwrite_f(BlockDriverState *bs, int argc, char **argv);
1193 static const cmdinfo_t multiwrite_cmd = {
1194 .name = "multiwrite",
1195 .cfunc = multiwrite_f,
1198 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1199 .oneline = "issues multiple write requests at once",
1200 .help = multiwrite_help,
1203 static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
1205 struct timeval t1, t2;
1206 int Cflag = 0, qflag = 0;
1209 int64_t offset, first_offset = 0;
1210 /* Some compilers get confused and warn if this is not initialized. */
1215 QEMUIOVector *qiovs;
1219 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1228 pattern = parse_pattern(optarg);
1234 return qemuio_command_usage(&writev_cmd);
1238 if (optind > argc - 2) {
1239 return qemuio_command_usage(&writev_cmd);
1243 for (i = optind; i < argc; i++) {
1244 if (!strcmp(argv[i], ";")) {
1249 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
1250 buf = g_malloc0(nr_reqs * sizeof(*buf));
1251 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
1253 for (i = 0; i < nr_reqs && optind < argc; i++) {
1256 /* Read the offset of the request */
1257 offset = cvtnum(argv[optind]);
1259 printf("non-numeric offset argument -- %s\n", argv[optind]);
1264 if (offset & 0x1ff) {
1265 printf("offset %lld is not sector aligned\n",
1271 first_offset = offset;
1274 /* Read lengths for qiov entries */
1275 for (j = optind; j < argc; j++) {
1276 if (!strcmp(argv[j], ";")) {
1281 nr_iov = j - optind;
1284 buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern);
1285 if (buf[i] == NULL) {
1289 reqs[i].qiov = &qiovs[i];
1290 reqs[i].sector = offset >> 9;
1291 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1298 /* If there were empty requests at the end, ignore them */
1301 gettimeofday(&t1, NULL);
1302 cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total);
1303 gettimeofday(&t2, NULL);
1306 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1314 /* Finally, report back -- -C gives a parsable format */
1316 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1318 for (i = 0; i < nr_reqs; i++) {
1319 qemu_io_free(buf[i]);
1320 if (reqs[i].qiov != NULL) {
1321 qemu_iovec_destroy(&qiovs[i]);
1342 static void aio_write_done(void *opaque, int ret)
1344 struct aio_ctx *ctx = opaque;
1347 gettimeofday(&t2, NULL);
1351 printf("aio_write failed: %s\n", strerror(-ret));
1359 /* Finally, report back -- -C gives a parsable format */
1360 t2 = tsub(t2, ctx->t1);
1361 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1362 ctx->qiov.size, 1, ctx->Cflag);
1364 qemu_io_free(ctx->buf);
1365 qemu_iovec_destroy(&ctx->qiov);
1369 static void aio_read_done(void *opaque, int ret)
1371 struct aio_ctx *ctx = opaque;
1374 gettimeofday(&t2, NULL);
1377 printf("readv failed: %s\n", strerror(-ret));
1382 void *cmp_buf = g_malloc(ctx->qiov.size);
1384 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1385 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1386 printf("Pattern verification failed at offset %"
1387 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1397 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1400 /* Finally, report back -- -C gives a parsable format */
1401 t2 = tsub(t2, ctx->t1);
1402 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1403 ctx->qiov.size, 1, ctx->Cflag);
1405 qemu_io_free(ctx->buf);
1406 qemu_iovec_destroy(&ctx->qiov);
1410 static void aio_read_help(void)
1414 " asynchronously reads a range of bytes from the given offset\n"
1417 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1419 " Reads a segment of the currently open file, optionally dumping it to the\n"
1420 " standard output stream (with -v option) for subsequent inspection.\n"
1421 " The read is performed asynchronously and the aio_flush command must be\n"
1422 " used to ensure all outstanding aio requests have been completed.\n"
1423 " -C, -- report statistics in a machine parsable format\n"
1424 " -P, -- use a pattern to verify read data\n"
1425 " -v, -- dump buffer to standard output\n"
1426 " -q, -- quiet mode, do not show I/O statistics\n"
1430 static int aio_read_f(BlockDriverState *bs, int argc, char **argv);
1432 static const cmdinfo_t aio_read_cmd = {
1434 .cfunc = aio_read_f,
1437 .args = "[-Cqv] [-P pattern ] off len [len..]",
1438 .oneline = "asynchronously reads a number of bytes",
1439 .help = aio_read_help,
1442 static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
1445 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1447 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1454 ctx->pattern = parse_pattern(optarg);
1455 if (ctx->pattern < 0) {
1468 return qemuio_command_usage(&aio_read_cmd);
1472 if (optind > argc - 2) {
1474 return qemuio_command_usage(&aio_read_cmd);
1477 ctx->offset = cvtnum(argv[optind]);
1478 if (ctx->offset < 0) {
1479 printf("non-numeric length argument -- %s\n", argv[optind]);
1485 if (ctx->offset & 0x1ff) {
1486 printf("offset %" PRId64 " is not sector aligned\n",
1492 nr_iov = argc - optind;
1493 ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1494 if (ctx->buf == NULL) {
1499 gettimeofday(&ctx->t1, NULL);
1500 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1501 ctx->qiov.size >> 9, aio_read_done, ctx);
1505 static void aio_write_help(void)
1509 " asynchronously writes a range of bytes from the given offset source\n"
1510 " from multiple buffers\n"
1513 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1515 " Writes into a segment of the currently open file, using a buffer\n"
1516 " filled with a set pattern (0xcdcdcdcd).\n"
1517 " The write is performed asynchronously and the aio_flush command must be\n"
1518 " used to ensure all outstanding aio requests have been completed.\n"
1519 " -P, -- use different pattern to fill file\n"
1520 " -C, -- report statistics in a machine parsable format\n"
1521 " -q, -- quiet mode, do not show I/O statistics\n"
1525 static int aio_write_f(BlockDriverState *bs, int argc, char **argv);
1527 static const cmdinfo_t aio_write_cmd = {
1528 .name = "aio_write",
1529 .cfunc = aio_write_f,
1532 .args = "[-Cq] [-P pattern ] off len [len..]",
1533 .oneline = "asynchronously writes a number of bytes",
1534 .help = aio_write_help,
1537 static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
1541 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1543 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1552 pattern = parse_pattern(optarg);
1560 return qemuio_command_usage(&aio_write_cmd);
1564 if (optind > argc - 2) {
1566 return qemuio_command_usage(&aio_write_cmd);
1569 ctx->offset = cvtnum(argv[optind]);
1570 if (ctx->offset < 0) {
1571 printf("non-numeric length argument -- %s\n", argv[optind]);
1577 if (ctx->offset & 0x1ff) {
1578 printf("offset %" PRId64 " is not sector aligned\n",
1584 nr_iov = argc - optind;
1585 ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern);
1586 if (ctx->buf == NULL) {
1591 gettimeofday(&ctx->t1, NULL);
1592 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1593 ctx->qiov.size >> 9, aio_write_done, ctx);
1597 static int aio_flush_f(BlockDriverState *bs, int argc, char **argv)
1603 static const cmdinfo_t aio_flush_cmd = {
1604 .name = "aio_flush",
1605 .cfunc = aio_flush_f,
1606 .oneline = "completes all outstanding aio requests"
1609 static int flush_f(BlockDriverState *bs, int argc, char **argv)
1615 static const cmdinfo_t flush_cmd = {
1619 .oneline = "flush all in-core file state to disk",
1622 static int truncate_f(BlockDriverState *bs, int argc, char **argv)
1627 offset = cvtnum(argv[1]);
1629 printf("non-numeric truncate argument -- %s\n", argv[1]);
1633 ret = bdrv_truncate(bs, offset);
1635 printf("truncate: %s\n", strerror(-ret));
1642 static const cmdinfo_t truncate_cmd = {
1645 .cfunc = truncate_f,
1649 .oneline = "truncates the current file at the given offset",
1652 static int length_f(BlockDriverState *bs, int argc, char **argv)
1657 size = bdrv_getlength(bs);
1659 printf("getlength: %s\n", strerror(-size));
1663 cvtstr(size, s1, sizeof(s1));
1669 static const cmdinfo_t length_cmd = {
1673 .oneline = "gets the length of the current file",
1677 static int info_f(BlockDriverState *bs, int argc, char **argv)
1679 BlockDriverInfo bdi;
1680 char s1[64], s2[64];
1683 if (bs->drv && bs->drv->format_name) {
1684 printf("format name: %s\n", bs->drv->format_name);
1686 if (bs->drv && bs->drv->protocol_name) {
1687 printf("format name: %s\n", bs->drv->protocol_name);
1690 ret = bdrv_get_info(bs, &bdi);
1695 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1696 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1698 printf("cluster size: %s\n", s1);
1699 printf("vm state offset: %s\n", s2);
1706 static const cmdinfo_t info_cmd = {
1710 .oneline = "prints information about the current file",
1713 static void discard_help(void)
1717 " discards a range of bytes from the given offset\n"
1720 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1722 " Discards a segment of the currently open file.\n"
1723 " -C, -- report statistics in a machine parsable format\n"
1724 " -q, -- quiet mode, do not show I/O statistics\n"
1728 static int discard_f(BlockDriverState *bs, int argc, char **argv);
1730 static const cmdinfo_t discard_cmd = {
1736 .args = "[-Cq] off len",
1737 .oneline = "discards a number of bytes at a specified offset",
1738 .help = discard_help,
1741 static int discard_f(BlockDriverState *bs, int argc, char **argv)
1743 struct timeval t1, t2;
1744 int Cflag = 0, qflag = 0;
1749 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1758 return qemuio_command_usage(&discard_cmd);
1762 if (optind != argc - 2) {
1763 return qemuio_command_usage(&discard_cmd);
1766 offset = cvtnum(argv[optind]);
1768 printf("non-numeric length argument -- %s\n", argv[optind]);
1773 count = cvtnum(argv[optind]);
1775 printf("non-numeric length argument -- %s\n", argv[optind]);
1779 gettimeofday(&t1, NULL);
1780 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1781 count >> BDRV_SECTOR_BITS);
1782 gettimeofday(&t2, NULL);
1785 printf("discard failed: %s\n", strerror(-ret));
1789 /* Finally, report back -- -C gives a parsable format */
1792 print_report("discard", &t2, offset, count, count, 1, Cflag);
1799 static int alloc_f(BlockDriverState *bs, int argc, char **argv)
1801 int64_t offset, sector_num;
1802 int nb_sectors, remaining;
1807 offset = cvtnum(argv[1]);
1809 printf("non-numeric offset argument -- %s\n", argv[1]);
1811 } else if (offset & 0x1ff) {
1812 printf("offset %" PRId64 " is not sector aligned\n",
1818 nb_sectors = cvtnum(argv[2]);
1819 if (nb_sectors < 0) {
1820 printf("non-numeric length argument -- %s\n", argv[2]);
1827 remaining = nb_sectors;
1829 sector_num = offset >> 9;
1831 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
1838 nb_sectors -= remaining;
1843 cvtstr(offset, s1, sizeof(s1));
1845 printf("%d/%d sectors allocated at offset %s\n",
1846 sum_alloc, nb_sectors, s1);
1850 static const cmdinfo_t alloc_cmd = {
1856 .args = "off [sectors]",
1857 .oneline = "checks if a sector is present in the file",
1861 static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1862 int64_t nb_sectors, int64_t *pnum)
1864 int num, num_checked;
1867 num_checked = MIN(nb_sectors, INT_MAX);
1868 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1876 while (nb_sectors > 0 && ret == firstret) {
1880 num_checked = MIN(nb_sectors, INT_MAX);
1881 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1882 if (ret == firstret) {
1892 static int map_f(BlockDriverState *bs, int argc, char **argv)
1902 nb_sectors = bs->total_sectors;
1905 ret = map_is_allocated(bs, offset, nb_sectors, &num);
1907 error_report("Failed to get allocation status: %s", strerror(-ret));
1911 retstr = ret ? " allocated" : "not allocated";
1912 cvtstr(offset << 9ULL, s1, sizeof(s1));
1913 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1914 "at offset %s (%d)\n",
1915 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1919 } while (offset < bs->total_sectors);
1924 static const cmdinfo_t map_cmd = {
1930 .oneline = "prints the allocated areas of a file",
1933 static int break_f(BlockDriverState *bs, int argc, char **argv)
1937 ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
1939 printf("Could not set breakpoint: %s\n", strerror(-ret));
1945 static const cmdinfo_t break_cmd = {
1950 .args = "event tag",
1951 .oneline = "sets a breakpoint on event and tags the stopped "
1955 static int resume_f(BlockDriverState *bs, int argc, char **argv)
1959 ret = bdrv_debug_resume(bs, argv[1]);
1961 printf("Could not resume request: %s\n", strerror(-ret));
1967 static const cmdinfo_t resume_cmd = {
1973 .oneline = "resumes the request tagged as tag",
1976 static int wait_break_f(BlockDriverState *bs, int argc, char **argv)
1978 while (!bdrv_debug_is_suspended(bs, argv[1])) {
1985 static const cmdinfo_t wait_break_cmd = {
1986 .name = "wait_break",
1989 .cfunc = wait_break_f,
1991 .oneline = "waits for the suspension of a request",
1994 static int abort_f(BlockDriverState *bs, int argc, char **argv)
1999 static const cmdinfo_t abort_cmd = {
2002 .flags = CMD_NOFILE_OK,
2003 .oneline = "simulate a program crash using abort(3)",
2006 static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2011 printf("%s ", ct->name);
2013 printf("(or %s) ", ct->altname);
2018 printf("%s ", ct->args);
2020 printf("-- %s\n", ct->oneline);
2023 static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2025 help_oneline(cmd, ct);
2031 static void help_all(void)
2033 const cmdinfo_t *ct;
2035 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2036 help_oneline(ct->name, ct);
2038 printf("\nUse 'help commandname' for extended help.\n");
2041 static int help_f(BlockDriverState *bs, int argc, char **argv)
2043 const cmdinfo_t *ct;
2050 ct = find_command(argv[1]);
2052 printf("command %s not found\n", argv[1]);
2056 help_onecmd(argv[1], ct);
2060 static const cmdinfo_t help_cmd = {
2066 .flags = CMD_FLAG_GLOBAL,
2067 .args = "[command]",
2068 .oneline = "help for one or all commands",
2071 bool qemuio_command(BlockDriverState *bs, const char *cmd)
2074 const cmdinfo_t *ct;
2079 input = g_strdup(cmd);
2080 v = breakline(input, &c);
2082 ct = find_command(v[0]);
2084 done = command(bs, ct, c, v);
2086 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2095 static void __attribute((constructor)) init_qemuio_commands(void)
2097 /* initialize commands */
2098 qemuio_add_command(&help_cmd);
2099 qemuio_add_command(&read_cmd);
2100 qemuio_add_command(&readv_cmd);
2101 qemuio_add_command(&write_cmd);
2102 qemuio_add_command(&writev_cmd);
2103 qemuio_add_command(&multiwrite_cmd);
2104 qemuio_add_command(&aio_read_cmd);
2105 qemuio_add_command(&aio_write_cmd);
2106 qemuio_add_command(&aio_flush_cmd);
2107 qemuio_add_command(&flush_cmd);
2108 qemuio_add_command(&truncate_cmd);
2109 qemuio_add_command(&length_cmd);
2110 qemuio_add_command(&info_cmd);
2111 qemuio_add_command(&discard_cmd);
2112 qemuio_add_command(&alloc_cmd);
2113 qemuio_add_command(&map_cmd);
2114 qemuio_add_command(&break_cmd);
2115 qemuio_add_command(&resume_cmd);
2116 qemuio_add_command(&wait_break_cmd);
2117 qemuio_add_command(&abort_cmd);