2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
11 #include "qemu-common.h"
12 #include "block/block_int.h"
15 #define CMD_NOFILE_OK 0x01
19 static cmdinfo_t
*cmdtab
;
22 static int compare_cmdname(const void *a
, const void *b
)
24 return strcmp(((const cmdinfo_t
*)a
)->name
,
25 ((const cmdinfo_t
*)b
)->name
);
28 void qemuio_add_command(const cmdinfo_t
*ci
)
30 cmdtab
= g_realloc(cmdtab
, ++ncmds
* sizeof(*cmdtab
));
31 cmdtab
[ncmds
- 1] = *ci
;
32 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
35 int qemuio_command_usage(const cmdinfo_t
*ci
)
37 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
41 static int init_check_command(BlockDriverState
*bs
, const cmdinfo_t
*ct
)
43 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
46 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
47 fprintf(stderr
, "no file open, try 'help open'\n");
53 static int command(const cmdinfo_t
*ct
, int argc
, char **argv
)
57 if (!init_check_command(qemuio_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(qemuio_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
);
130 * Parse the pattern argument to various sub-commands.
132 * Because the pattern is used as an argument to memset it must evaluate
133 * to an unsigned integer that fits into a single byte.
135 static int parse_pattern(const char *arg
)
140 pattern
= strtol(arg
, &endptr
, 0);
141 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
142 printf("%s is not a valid pattern byte\n", arg
);
150 * Memory allocation helpers.
152 * Make sure memory is aligned by default, or purposefully misaligned if
153 * that is specified on the command line.
156 #define MISALIGN_OFFSET 16
157 static void *qemu_io_alloc(BlockDriverState
*bs
, size_t len
, int pattern
)
161 if (qemuio_misalign
) {
162 len
+= MISALIGN_OFFSET
;
164 buf
= qemu_blockalign(bs
, len
);
165 memset(buf
, pattern
, len
);
166 if (qemuio_misalign
) {
167 buf
+= MISALIGN_OFFSET
;
172 static void qemu_io_free(void *p
)
174 if (qemuio_misalign
) {
175 p
-= MISALIGN_OFFSET
;
180 static void dump_buffer(const void *buffer
, int64_t offset
, int len
)
185 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
186 const uint8_t *s
= p
;
188 printf("%08" PRIx64
": ", offset
+ i
);
189 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
193 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
204 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
205 int count
, int total
, int cnt
, int Cflag
)
207 char s1
[64], s2
[64], ts
[64];
209 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
211 cvtstr((double)total
, s1
, sizeof(s1
));
212 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
213 printf("%s %d/%d bytes at offset %" PRId64
"\n",
214 op
, total
, count
, offset
);
215 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
216 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
217 } else {/* bytes,ops,time,bytes/sec,ops/sec */
218 printf("%d,%d,%s,%.3f,%.3f\n",
220 tdiv((double)total
, *t
),
221 tdiv((double)cnt
, *t
));
226 * Parse multiple length statements for vectored I/O, and construct an I/O
227 * vector matching it.
230 create_iovec(BlockDriverState
*bs
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
233 size_t *sizes
= g_new0(size_t, nr_iov
);
239 for (i
= 0; i
< nr_iov
; i
++) {
245 printf("non-numeric length argument -- %s\n", arg
);
249 /* should be SIZE_T_MAX, but that doesn't exist */
251 printf("too large length argument -- %s\n", arg
);
256 printf("length argument %" PRId64
257 " is not sector aligned\n", len
);
265 qemu_iovec_init(qiov
, nr_iov
);
267 buf
= p
= qemu_io_alloc(bs
, count
, pattern
);
269 for (i
= 0; i
< nr_iov
; i
++) {
270 qemu_iovec_add(qiov
, p
, sizes
[i
]);
279 static int do_read(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
284 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
292 static int do_write(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
297 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
305 static int do_pread(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
308 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
315 static int do_pwrite(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
318 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
326 BlockDriverState
*bs
;
334 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
336 CoWriteZeroes
*data
= opaque
;
338 data
->ret
= bdrv_co_write_zeroes(data
->bs
, data
->offset
/ BDRV_SECTOR_SIZE
,
339 data
->count
/ BDRV_SECTOR_SIZE
);
342 *data
->total
= data
->ret
;
346 *data
->total
= data
->count
;
349 static int do_co_write_zeroes(BlockDriverState
*bs
, int64_t offset
, int count
,
353 CoWriteZeroes data
= {
361 co
= qemu_coroutine_create(co_write_zeroes_entry
);
362 qemu_coroutine_enter(co
, &data
);
373 static int do_write_compressed(BlockDriverState
*bs
, char *buf
, int64_t offset
,
374 int count
, int *total
)
378 ret
= bdrv_write_compressed(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
386 static int do_load_vmstate(BlockDriverState
*bs
, char *buf
, int64_t offset
,
387 int count
, int *total
)
389 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
396 static int do_save_vmstate(BlockDriverState
*bs
, char *buf
, int64_t offset
,
397 int count
, int *total
)
399 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
406 #define NOT_DONE 0x7fffffff
407 static void aio_rw_done(void *opaque
, int ret
)
409 *(int *)opaque
= ret
;
412 static int do_aio_readv(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
413 int64_t offset
, int *total
)
415 int async_ret
= NOT_DONE
;
417 bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
418 aio_rw_done
, &async_ret
);
419 while (async_ret
== NOT_DONE
) {
420 main_loop_wait(false);
424 return async_ret
< 0 ? async_ret
: 1;
427 static int do_aio_writev(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
428 int64_t offset
, int *total
)
430 int async_ret
= NOT_DONE
;
432 bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
433 aio_rw_done
, &async_ret
);
434 while (async_ret
== NOT_DONE
) {
435 main_loop_wait(false);
439 return async_ret
< 0 ? async_ret
: 1;
442 struct multiwrite_async_ret
{
447 static void multiwrite_cb(void *opaque
, int ret
)
449 struct multiwrite_async_ret
*async_ret
= opaque
;
451 async_ret
->num_done
++;
453 async_ret
->error
= ret
;
457 static int do_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
* reqs
,
458 int num_reqs
, int *total
)
461 struct multiwrite_async_ret async_ret
= {
467 for (i
= 0; i
< num_reqs
; i
++) {
468 reqs
[i
].cb
= multiwrite_cb
;
469 reqs
[i
].opaque
= &async_ret
;
470 *total
+= reqs
[i
].qiov
->size
;
473 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
478 while (async_ret
.num_done
< num_reqs
) {
479 main_loop_wait(false);
482 return async_ret
.error
< 0 ? async_ret
.error
: 1;
485 static void read_help(void)
489 " reads a range of bytes from the given offset\n"
492 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
494 " Reads a segment of the currently open file, optionally dumping it to the\n"
495 " standard output stream (with -v option) for subsequent inspection.\n"
496 " -b, -- read from the VM state rather than the virtual disk\n"
497 " -C, -- report statistics in a machine parsable format\n"
498 " -l, -- length for pattern verification (only with -P)\n"
499 " -p, -- use bdrv_pread to read the file\n"
500 " -P, -- use a pattern to verify read data\n"
501 " -q, -- quiet mode, do not show I/O statistics\n"
502 " -s, -- start offset for pattern verification (only with -P)\n"
503 " -v, -- dump buffer to standard output\n"
507 static int read_f(BlockDriverState
*bs
, int argc
, char **argv
);
509 static const cmdinfo_t read_cmd
= {
515 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
516 .oneline
= "reads a number of bytes at a specified offset",
520 static int read_f(BlockDriverState
*bs
, int argc
, char **argv
)
522 struct timeval t1
, t2
;
523 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
524 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
529 /* Some compilers get confused and warn if this is not initialized. */
531 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
533 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
543 pattern_count
= cvtnum(optarg
);
544 if (pattern_count
< 0) {
545 printf("non-numeric length argument -- %s\n", optarg
);
554 pattern
= parse_pattern(optarg
);
564 pattern_offset
= cvtnum(optarg
);
565 if (pattern_offset
< 0) {
566 printf("non-numeric length argument -- %s\n", optarg
);
574 return qemuio_command_usage(&read_cmd
);
578 if (optind
!= argc
- 2) {
579 return qemuio_command_usage(&read_cmd
);
582 if (bflag
&& pflag
) {
583 printf("-b and -p cannot be specified at the same time\n");
587 offset
= cvtnum(argv
[optind
]);
589 printf("non-numeric length argument -- %s\n", argv
[optind
]);
594 count
= cvtnum(argv
[optind
]);
596 printf("non-numeric length argument -- %s\n", argv
[optind
]);
600 if (!Pflag
&& (lflag
|| sflag
)) {
601 return qemuio_command_usage(&read_cmd
);
605 pattern_count
= count
- pattern_offset
;
608 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
609 printf("pattern verification range exceeds end of read data\n");
614 if (offset
& 0x1ff) {
615 printf("offset %" PRId64
" is not sector aligned\n",
620 printf("count %d is not sector aligned\n",
626 buf
= qemu_io_alloc(bs
, count
, 0xab);
628 gettimeofday(&t1
, NULL
);
630 cnt
= do_pread(bs
, buf
, offset
, count
, &total
);
632 cnt
= do_load_vmstate(bs
, buf
, offset
, count
, &total
);
634 cnt
= do_read(bs
, buf
, offset
, count
, &total
);
636 gettimeofday(&t2
, NULL
);
639 printf("read failed: %s\n", strerror(-cnt
));
644 void *cmp_buf
= g_malloc(pattern_count
);
645 memset(cmp_buf
, pattern
, pattern_count
);
646 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
647 printf("Pattern verification failed at offset %"
648 PRId64
", %d bytes\n",
649 offset
+ pattern_offset
, pattern_count
);
659 dump_buffer(buf
, offset
, count
);
662 /* Finally, report back -- -C gives a parsable format */
664 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
672 static void readv_help(void)
676 " reads a range of bytes from the given offset into multiple buffers\n"
679 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
681 " Reads a segment of the currently open file, optionally dumping it to the\n"
682 " standard output stream (with -v option) for subsequent inspection.\n"
683 " Uses multiple iovec buffers if more than one byte range is specified.\n"
684 " -C, -- report statistics in a machine parsable format\n"
685 " -P, -- use a pattern to verify read data\n"
686 " -v, -- dump buffer to standard output\n"
687 " -q, -- quiet mode, do not show I/O statistics\n"
691 static int readv_f(BlockDriverState
*bs
, int argc
, char **argv
);
693 static const cmdinfo_t readv_cmd
= {
698 .args
= "[-Cqv] [-P pattern ] off len [len..]",
699 .oneline
= "reads a number of bytes at a specified offset",
703 static int readv_f(BlockDriverState
*bs
, int argc
, char **argv
)
705 struct timeval t1
, t2
;
706 int Cflag
= 0, qflag
= 0, vflag
= 0;
710 /* Some compilers get confused and warn if this is not initialized. */
717 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
724 pattern
= parse_pattern(optarg
);
736 return qemuio_command_usage(&readv_cmd
);
740 if (optind
> argc
- 2) {
741 return qemuio_command_usage(&readv_cmd
);
745 offset
= cvtnum(argv
[optind
]);
747 printf("non-numeric length argument -- %s\n", argv
[optind
]);
752 if (offset
& 0x1ff) {
753 printf("offset %" PRId64
" is not sector aligned\n",
758 nr_iov
= argc
- optind
;
759 buf
= create_iovec(bs
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
764 gettimeofday(&t1
, NULL
);
765 cnt
= do_aio_readv(bs
, &qiov
, offset
, &total
);
766 gettimeofday(&t2
, NULL
);
769 printf("readv failed: %s\n", strerror(-cnt
));
774 void *cmp_buf
= g_malloc(qiov
.size
);
775 memset(cmp_buf
, pattern
, qiov
.size
);
776 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
777 printf("Pattern verification failed at offset %"
778 PRId64
", %zd bytes\n", offset
, qiov
.size
);
788 dump_buffer(buf
, offset
, qiov
.size
);
791 /* Finally, report back -- -C gives a parsable format */
793 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
796 qemu_iovec_destroy(&qiov
);
801 static void write_help(void)
805 " writes a range of bytes from the given offset\n"
808 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
810 " Writes into a segment of the currently open file, using a buffer\n"
811 " filled with a set pattern (0xcdcdcdcd).\n"
812 " -b, -- write to the VM state rather than the virtual disk\n"
813 " -c, -- write compressed data with bdrv_write_compressed\n"
814 " -p, -- use bdrv_pwrite to write the file\n"
815 " -P, -- use different pattern to fill file\n"
816 " -C, -- report statistics in a machine parsable format\n"
817 " -q, -- quiet mode, do not show I/O statistics\n"
818 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
822 static int write_f(BlockDriverState
*bs
, int argc
, char **argv
);
824 static const cmdinfo_t write_cmd
= {
830 .args
= "[-bcCpqz] [-P pattern ] off len",
831 .oneline
= "writes a number of bytes at a specified offset",
835 static int write_f(BlockDriverState
*bs
, int argc
, char **argv
)
837 struct timeval t1
, t2
;
838 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
844 /* Some compilers get confused and warn if this is not initialized. */
848 while ((c
= getopt(argc
, argv
, "bcCpP:qz")) != EOF
) {
864 pattern
= parse_pattern(optarg
);
876 return qemuio_command_usage(&write_cmd
);
880 if (optind
!= argc
- 2) {
881 return qemuio_command_usage(&write_cmd
);
884 if (bflag
+ pflag
+ zflag
> 1) {
885 printf("-b, -p, or -z cannot be specified at the same time\n");
889 if (zflag
&& Pflag
) {
890 printf("-z and -P cannot be specified at the same time\n");
894 offset
= cvtnum(argv
[optind
]);
896 printf("non-numeric length argument -- %s\n", argv
[optind
]);
901 count
= cvtnum(argv
[optind
]);
903 printf("non-numeric length argument -- %s\n", argv
[optind
]);
908 if (offset
& 0x1ff) {
909 printf("offset %" PRId64
" is not sector aligned\n",
915 printf("count %d is not sector aligned\n",
922 buf
= qemu_io_alloc(bs
, count
, pattern
);
925 gettimeofday(&t1
, NULL
);
927 cnt
= do_pwrite(bs
, buf
, offset
, count
, &total
);
929 cnt
= do_save_vmstate(bs
, buf
, offset
, count
, &total
);
931 cnt
= do_co_write_zeroes(bs
, offset
, count
, &total
);
933 cnt
= do_write_compressed(bs
, buf
, offset
, count
, &total
);
935 cnt
= do_write(bs
, buf
, offset
, count
, &total
);
937 gettimeofday(&t2
, NULL
);
940 printf("write failed: %s\n", strerror(-cnt
));
948 /* Finally, report back -- -C gives a parsable format */
950 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
965 " writes a range of bytes from the given offset source from multiple buffers\n"
968 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
970 " Writes into a segment of the currently open file, using a buffer\n"
971 " filled with a set pattern (0xcdcdcdcd).\n"
972 " -P, -- use different pattern to fill file\n"
973 " -C, -- report statistics in a machine parsable format\n"
974 " -q, -- quiet mode, do not show I/O statistics\n"
978 static int writev_f(BlockDriverState
*bs
, int argc
, char **argv
);
980 static const cmdinfo_t writev_cmd
= {
985 .args
= "[-Cq] [-P pattern ] off len [len..]",
986 .oneline
= "writes a number of bytes at a specified offset",
990 static int writev_f(BlockDriverState
*bs
, int argc
, char **argv
)
992 struct timeval t1
, t2
;
993 int Cflag
= 0, qflag
= 0;
997 /* Some compilers get confused and warn if this is not initialized. */
1003 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1012 pattern
= parse_pattern(optarg
);
1018 return qemuio_command_usage(&writev_cmd
);
1022 if (optind
> argc
- 2) {
1023 return qemuio_command_usage(&writev_cmd
);
1026 offset
= cvtnum(argv
[optind
]);
1028 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1033 if (offset
& 0x1ff) {
1034 printf("offset %" PRId64
" is not sector aligned\n",
1039 nr_iov
= argc
- optind
;
1040 buf
= create_iovec(bs
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1045 gettimeofday(&t1
, NULL
);
1046 cnt
= do_aio_writev(bs
, &qiov
, offset
, &total
);
1047 gettimeofday(&t2
, NULL
);
1050 printf("writev failed: %s\n", strerror(-cnt
));
1058 /* Finally, report back -- -C gives a parsable format */
1060 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1062 qemu_iovec_destroy(&qiov
);
1067 static void multiwrite_help(void)
1071 " writes a range of bytes from the given offset source from multiple buffers,\n"
1072 " in a batch of requests that may be merged by qemu\n"
1075 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1076 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1078 " Writes into a segment of the currently open file, using a buffer\n"
1079 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1080 " by one for each request contained in the multiwrite command.\n"
1081 " -P, -- use different pattern to fill file\n"
1082 " -C, -- report statistics in a machine parsable format\n"
1083 " -q, -- quiet mode, do not show I/O statistics\n"
1087 static int multiwrite_f(BlockDriverState
*bs
, int argc
, char **argv
);
1089 static const cmdinfo_t multiwrite_cmd
= {
1090 .name
= "multiwrite",
1091 .cfunc
= multiwrite_f
,
1094 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1095 .oneline
= "issues multiple write requests at once",
1096 .help
= multiwrite_help
,
1099 static int multiwrite_f(BlockDriverState
*bs
, int argc
, char **argv
)
1101 struct timeval t1
, t2
;
1102 int Cflag
= 0, qflag
= 0;
1105 int64_t offset
, first_offset
= 0;
1106 /* Some compilers get confused and warn if this is not initialized. */
1111 QEMUIOVector
*qiovs
;
1115 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1124 pattern
= parse_pattern(optarg
);
1130 return qemuio_command_usage(&writev_cmd
);
1134 if (optind
> argc
- 2) {
1135 return qemuio_command_usage(&writev_cmd
);
1139 for (i
= optind
; i
< argc
; i
++) {
1140 if (!strcmp(argv
[i
], ";")) {
1145 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
1146 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
1147 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
1149 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1152 /* Read the offset of the request */
1153 offset
= cvtnum(argv
[optind
]);
1155 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
1160 if (offset
& 0x1ff) {
1161 printf("offset %lld is not sector aligned\n",
1167 first_offset
= offset
;
1170 /* Read lengths for qiov entries */
1171 for (j
= optind
; j
< argc
; j
++) {
1172 if (!strcmp(argv
[j
], ";")) {
1177 nr_iov
= j
- optind
;
1180 buf
[i
] = create_iovec(bs
, &qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1181 if (buf
[i
] == NULL
) {
1185 reqs
[i
].qiov
= &qiovs
[i
];
1186 reqs
[i
].sector
= offset
>> 9;
1187 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1194 /* If there were empty requests at the end, ignore them */
1197 gettimeofday(&t1
, NULL
);
1198 cnt
= do_aio_multiwrite(bs
, reqs
, nr_reqs
, &total
);
1199 gettimeofday(&t2
, NULL
);
1202 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1210 /* Finally, report back -- -C gives a parsable format */
1212 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1214 for (i
= 0; i
< nr_reqs
; i
++) {
1215 qemu_io_free(buf
[i
]);
1216 if (reqs
[i
].qiov
!= NULL
) {
1217 qemu_iovec_destroy(&qiovs
[i
]);
1238 static void aio_write_done(void *opaque
, int ret
)
1240 struct aio_ctx
*ctx
= opaque
;
1243 gettimeofday(&t2
, NULL
);
1247 printf("aio_write failed: %s\n", strerror(-ret
));
1255 /* Finally, report back -- -C gives a parsable format */
1256 t2
= tsub(t2
, ctx
->t1
);
1257 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1258 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1260 qemu_io_free(ctx
->buf
);
1261 qemu_iovec_destroy(&ctx
->qiov
);
1265 static void aio_read_done(void *opaque
, int ret
)
1267 struct aio_ctx
*ctx
= opaque
;
1270 gettimeofday(&t2
, NULL
);
1273 printf("readv failed: %s\n", strerror(-ret
));
1278 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1280 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1281 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1282 printf("Pattern verification failed at offset %"
1283 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1293 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1296 /* Finally, report back -- -C gives a parsable format */
1297 t2
= tsub(t2
, ctx
->t1
);
1298 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1299 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1301 qemu_io_free(ctx
->buf
);
1302 qemu_iovec_destroy(&ctx
->qiov
);
1306 static void aio_read_help(void)
1310 " asynchronously reads a range of bytes from the given offset\n"
1313 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1315 " Reads a segment of the currently open file, optionally dumping it to the\n"
1316 " standard output stream (with -v option) for subsequent inspection.\n"
1317 " The read is performed asynchronously and the aio_flush command must be\n"
1318 " used to ensure all outstanding aio requests have been completed.\n"
1319 " -C, -- report statistics in a machine parsable format\n"
1320 " -P, -- use a pattern to verify read data\n"
1321 " -v, -- dump buffer to standard output\n"
1322 " -q, -- quiet mode, do not show I/O statistics\n"
1326 static int aio_read_f(BlockDriverState
*bs
, int argc
, char **argv
);
1328 static const cmdinfo_t aio_read_cmd
= {
1330 .cfunc
= aio_read_f
,
1333 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1334 .oneline
= "asynchronously reads a number of bytes",
1335 .help
= aio_read_help
,
1338 static int aio_read_f(BlockDriverState
*bs
, int argc
, char **argv
)
1341 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1343 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1350 ctx
->pattern
= parse_pattern(optarg
);
1351 if (ctx
->pattern
< 0) {
1364 return qemuio_command_usage(&aio_read_cmd
);
1368 if (optind
> argc
- 2) {
1370 return qemuio_command_usage(&aio_read_cmd
);
1373 ctx
->offset
= cvtnum(argv
[optind
]);
1374 if (ctx
->offset
< 0) {
1375 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1381 if (ctx
->offset
& 0x1ff) {
1382 printf("offset %" PRId64
" is not sector aligned\n",
1388 nr_iov
= argc
- optind
;
1389 ctx
->buf
= create_iovec(bs
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1390 if (ctx
->buf
== NULL
) {
1395 gettimeofday(&ctx
->t1
, NULL
);
1396 bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1397 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1401 static void aio_write_help(void)
1405 " asynchronously writes a range of bytes from the given offset source\n"
1406 " from multiple buffers\n"
1409 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1411 " Writes into a segment of the currently open file, using a buffer\n"
1412 " filled with a set pattern (0xcdcdcdcd).\n"
1413 " The write is performed asynchronously and the aio_flush command must be\n"
1414 " used to ensure all outstanding aio requests have been completed.\n"
1415 " -P, -- use different pattern to fill file\n"
1416 " -C, -- report statistics in a machine parsable format\n"
1417 " -q, -- quiet mode, do not show I/O statistics\n"
1421 static int aio_write_f(BlockDriverState
*bs
, int argc
, char **argv
);
1423 static const cmdinfo_t aio_write_cmd
= {
1424 .name
= "aio_write",
1425 .cfunc
= aio_write_f
,
1428 .args
= "[-Cq] [-P pattern ] off len [len..]",
1429 .oneline
= "asynchronously writes a number of bytes",
1430 .help
= aio_write_help
,
1433 static int aio_write_f(BlockDriverState
*bs
, int argc
, char **argv
)
1437 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1439 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1448 pattern
= parse_pattern(optarg
);
1456 return qemuio_command_usage(&aio_write_cmd
);
1460 if (optind
> argc
- 2) {
1462 return qemuio_command_usage(&aio_write_cmd
);
1465 ctx
->offset
= cvtnum(argv
[optind
]);
1466 if (ctx
->offset
< 0) {
1467 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1473 if (ctx
->offset
& 0x1ff) {
1474 printf("offset %" PRId64
" is not sector aligned\n",
1480 nr_iov
= argc
- optind
;
1481 ctx
->buf
= create_iovec(bs
, &ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1482 if (ctx
->buf
== NULL
) {
1487 gettimeofday(&ctx
->t1
, NULL
);
1488 bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1489 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1493 static int aio_flush_f(BlockDriverState
*bs
, int argc
, char **argv
)
1499 static const cmdinfo_t aio_flush_cmd
= {
1500 .name
= "aio_flush",
1501 .cfunc
= aio_flush_f
,
1502 .oneline
= "completes all outstanding aio requests"
1505 static int flush_f(BlockDriverState
*bs
, int argc
, char **argv
)
1511 static const cmdinfo_t flush_cmd
= {
1515 .oneline
= "flush all in-core file state to disk",
1518 static int truncate_f(BlockDriverState
*bs
, int argc
, char **argv
)
1523 offset
= cvtnum(argv
[1]);
1525 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1529 ret
= bdrv_truncate(bs
, offset
);
1531 printf("truncate: %s\n", strerror(-ret
));
1538 static const cmdinfo_t truncate_cmd
= {
1541 .cfunc
= truncate_f
,
1545 .oneline
= "truncates the current file at the given offset",
1548 static int length_f(BlockDriverState
*bs
, int argc
, char **argv
)
1553 size
= bdrv_getlength(bs
);
1555 printf("getlength: %s\n", strerror(-size
));
1559 cvtstr(size
, s1
, sizeof(s1
));
1565 static const cmdinfo_t length_cmd
= {
1569 .oneline
= "gets the length of the current file",
1573 static int info_f(BlockDriverState
*bs
, int argc
, char **argv
)
1575 BlockDriverInfo bdi
;
1576 char s1
[64], s2
[64];
1579 if (bs
->drv
&& bs
->drv
->format_name
) {
1580 printf("format name: %s\n", bs
->drv
->format_name
);
1582 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1583 printf("format name: %s\n", bs
->drv
->protocol_name
);
1586 ret
= bdrv_get_info(bs
, &bdi
);
1591 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1592 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1594 printf("cluster size: %s\n", s1
);
1595 printf("vm state offset: %s\n", s2
);
1602 static const cmdinfo_t info_cmd
= {
1606 .oneline
= "prints information about the current file",
1609 static void discard_help(void)
1613 " discards a range of bytes from the given offset\n"
1616 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1618 " Discards a segment of the currently open file.\n"
1619 " -C, -- report statistics in a machine parsable format\n"
1620 " -q, -- quiet mode, do not show I/O statistics\n"
1624 static int discard_f(BlockDriverState
*bs
, int argc
, char **argv
);
1626 static const cmdinfo_t discard_cmd
= {
1632 .args
= "[-Cq] off len",
1633 .oneline
= "discards a number of bytes at a specified offset",
1634 .help
= discard_help
,
1637 static int discard_f(BlockDriverState
*bs
, int argc
, char **argv
)
1639 struct timeval t1
, t2
;
1640 int Cflag
= 0, qflag
= 0;
1645 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1654 return qemuio_command_usage(&discard_cmd
);
1658 if (optind
!= argc
- 2) {
1659 return qemuio_command_usage(&discard_cmd
);
1662 offset
= cvtnum(argv
[optind
]);
1664 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1669 count
= cvtnum(argv
[optind
]);
1671 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1675 gettimeofday(&t1
, NULL
);
1676 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1677 count
>> BDRV_SECTOR_BITS
);
1678 gettimeofday(&t2
, NULL
);
1681 printf("discard failed: %s\n", strerror(-ret
));
1685 /* Finally, report back -- -C gives a parsable format */
1688 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1695 static int alloc_f(BlockDriverState
*bs
, int argc
, char **argv
)
1697 int64_t offset
, sector_num
;
1698 int nb_sectors
, remaining
;
1703 offset
= cvtnum(argv
[1]);
1705 printf("non-numeric offset argument -- %s\n", argv
[1]);
1707 } else if (offset
& 0x1ff) {
1708 printf("offset %" PRId64
" is not sector aligned\n",
1714 nb_sectors
= cvtnum(argv
[2]);
1715 if (nb_sectors
< 0) {
1716 printf("non-numeric length argument -- %s\n", argv
[2]);
1723 remaining
= nb_sectors
;
1725 sector_num
= offset
>> 9;
1727 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1734 nb_sectors
-= remaining
;
1739 cvtstr(offset
, s1
, sizeof(s1
));
1741 printf("%d/%d sectors allocated at offset %s\n",
1742 sum_alloc
, nb_sectors
, s1
);
1746 static const cmdinfo_t alloc_cmd
= {
1752 .args
= "off [sectors]",
1753 .oneline
= "checks if a sector is present in the file",
1757 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1758 int64_t nb_sectors
, int64_t *pnum
)
1760 int num
, num_checked
;
1763 num_checked
= MIN(nb_sectors
, INT_MAX
);
1764 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1772 while (nb_sectors
> 0 && ret
== firstret
) {
1776 num_checked
= MIN(nb_sectors
, INT_MAX
);
1777 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1778 if (ret
== firstret
) {
1788 static int map_f(BlockDriverState
*bs
, int argc
, char **argv
)
1798 nb_sectors
= bs
->total_sectors
;
1801 ret
= map_is_allocated(bs
, offset
, nb_sectors
, &num
);
1803 error_report("Failed to get allocation status: %s", strerror(-ret
));
1807 retstr
= ret
? " allocated" : "not allocated";
1808 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1809 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1810 "at offset %s (%d)\n",
1811 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
1815 } while (offset
< bs
->total_sectors
);
1820 static const cmdinfo_t map_cmd
= {
1826 .oneline
= "prints the allocated areas of a file",
1829 static int break_f(BlockDriverState
*bs
, int argc
, char **argv
)
1833 ret
= bdrv_debug_breakpoint(bs
, argv
[1], argv
[2]);
1835 printf("Could not set breakpoint: %s\n", strerror(-ret
));
1841 static const cmdinfo_t break_cmd
= {
1846 .args
= "event tag",
1847 .oneline
= "sets a breakpoint on event and tags the stopped "
1851 static int resume_f(BlockDriverState
*bs
, int argc
, char **argv
)
1855 ret
= bdrv_debug_resume(bs
, argv
[1]);
1857 printf("Could not resume request: %s\n", strerror(-ret
));
1863 static const cmdinfo_t resume_cmd
= {
1869 .oneline
= "resumes the request tagged as tag",
1872 static int wait_break_f(BlockDriverState
*bs
, int argc
, char **argv
)
1874 while (!bdrv_debug_is_suspended(bs
, argv
[1])) {
1881 static const cmdinfo_t wait_break_cmd
= {
1882 .name
= "wait_break",
1885 .cfunc
= wait_break_f
,
1887 .oneline
= "waits for the suspension of a request",
1890 static int abort_f(BlockDriverState
*bs
, int argc
, char **argv
)
1895 static const cmdinfo_t abort_cmd
= {
1898 .flags
= CMD_NOFILE_OK
,
1899 .oneline
= "simulate a program crash using abort(3)",
1902 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
1907 printf("%s ", ct
->name
);
1909 printf("(or %s) ", ct
->altname
);
1914 printf("%s ", ct
->args
);
1916 printf("-- %s\n", ct
->oneline
);
1919 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
1921 help_oneline(cmd
, ct
);
1927 static void help_all(void)
1929 const cmdinfo_t
*ct
;
1931 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
1932 help_oneline(ct
->name
, ct
);
1934 printf("\nUse 'help commandname' for extended help.\n");
1937 static int help_f(BlockDriverState
*bs
, int argc
, char **argv
)
1939 const cmdinfo_t
*ct
;
1946 ct
= find_command(argv
[1]);
1948 printf("command %s not found\n", argv
[1]);
1952 help_onecmd(argv
[1], ct
);
1956 static const cmdinfo_t help_cmd
= {
1962 .flags
= CMD_FLAG_GLOBAL
,
1963 .args
= "[command]",
1964 .oneline
= "help for one or all commands",
1967 bool qemuio_command(const char *cmd
)
1970 const cmdinfo_t
*ct
;
1975 input
= g_strdup(cmd
);
1976 v
= breakline(input
, &c
);
1978 ct
= find_command(v
[0]);
1980 done
= command(ct
, c
, v
);
1982 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
1991 static void __attribute((constructor
)) init_qemuio_commands(void)
1993 /* initialize commands */
1994 qemuio_add_command(&help_cmd
);
1995 qemuio_add_command(&read_cmd
);
1996 qemuio_add_command(&readv_cmd
);
1997 qemuio_add_command(&write_cmd
);
1998 qemuio_add_command(&writev_cmd
);
1999 qemuio_add_command(&multiwrite_cmd
);
2000 qemuio_add_command(&aio_read_cmd
);
2001 qemuio_add_command(&aio_write_cmd
);
2002 qemuio_add_command(&aio_flush_cmd
);
2003 qemuio_add_command(&flush_cmd
);
2004 qemuio_add_command(&truncate_cmd
);
2005 qemuio_add_command(&length_cmd
);
2006 qemuio_add_command(&info_cmd
);
2007 qemuio_add_command(&discard_cmd
);
2008 qemuio_add_command(&alloc_cmd
);
2009 qemuio_add_command(&map_cmd
);
2010 qemuio_add_command(&break_cmd
);
2011 qemuio_add_command(&resume_cmd
);
2012 qemuio_add_command(&wait_break_cmd
);
2013 qemuio_add_command(&abort_cmd
);