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 int64_t cvtnum(const char *s
)
22 return strtosz_suffix(s
, &end
, STRTOSZ_DEFSUFFIX_B
);
26 * Parse the pattern argument to various sub-commands.
28 * Because the pattern is used as an argument to memset it must evaluate
29 * to an unsigned integer that fits into a single byte.
31 static int parse_pattern(const char *arg
)
36 pattern
= strtol(arg
, &endptr
, 0);
37 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
38 printf("%s is not a valid pattern byte\n", arg
);
46 * Memory allocation helpers.
48 * Make sure memory is aligned by default, or purposefully misaligned if
49 * that is specified on the command line.
52 #define MISALIGN_OFFSET 16
53 static void *qemu_io_alloc(BlockDriverState
*bs
, size_t len
, int pattern
)
57 if (qemuio_misalign
) {
58 len
+= MISALIGN_OFFSET
;
60 buf
= qemu_blockalign(bs
, len
);
61 memset(buf
, pattern
, len
);
62 if (qemuio_misalign
) {
63 buf
+= MISALIGN_OFFSET
;
68 static void qemu_io_free(void *p
)
70 if (qemuio_misalign
) {
76 static void dump_buffer(const void *buffer
, int64_t offset
, int len
)
81 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
84 printf("%08" PRIx64
": ", offset
+ i
);
85 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
89 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
100 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
101 int count
, int total
, int cnt
, int Cflag
)
103 char s1
[64], s2
[64], ts
[64];
105 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
107 cvtstr((double)total
, s1
, sizeof(s1
));
108 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
109 printf("%s %d/%d bytes at offset %" PRId64
"\n",
110 op
, total
, count
, offset
);
111 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
112 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
113 } else {/* bytes,ops,time,bytes/sec,ops/sec */
114 printf("%d,%d,%s,%.3f,%.3f\n",
116 tdiv((double)total
, *t
),
117 tdiv((double)cnt
, *t
));
122 * Parse multiple length statements for vectored I/O, and construct an I/O
123 * vector matching it.
126 create_iovec(BlockDriverState
*bs
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
129 size_t *sizes
= g_new0(size_t, nr_iov
);
135 for (i
= 0; i
< nr_iov
; i
++) {
141 printf("non-numeric length argument -- %s\n", arg
);
145 /* should be SIZE_T_MAX, but that doesn't exist */
147 printf("too large length argument -- %s\n", arg
);
152 printf("length argument %" PRId64
153 " is not sector aligned\n", len
);
161 qemu_iovec_init(qiov
, nr_iov
);
163 buf
= p
= qemu_io_alloc(bs
, count
, pattern
);
165 for (i
= 0; i
< nr_iov
; i
++) {
166 qemu_iovec_add(qiov
, p
, sizes
[i
]);
175 static int do_read(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
180 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
188 static int do_write(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
193 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
201 static int do_pread(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
204 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
211 static int do_pwrite(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
214 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
222 BlockDriverState
*bs
;
230 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
232 CoWriteZeroes
*data
= opaque
;
234 data
->ret
= bdrv_co_write_zeroes(data
->bs
, data
->offset
/ BDRV_SECTOR_SIZE
,
235 data
->count
/ BDRV_SECTOR_SIZE
);
238 *data
->total
= data
->ret
;
242 *data
->total
= data
->count
;
245 static int do_co_write_zeroes(BlockDriverState
*bs
, int64_t offset
, int count
,
249 CoWriteZeroes data
= {
257 co
= qemu_coroutine_create(co_write_zeroes_entry
);
258 qemu_coroutine_enter(co
, &data
);
269 static int do_write_compressed(BlockDriverState
*bs
, char *buf
, int64_t offset
,
270 int count
, int *total
)
274 ret
= bdrv_write_compressed(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
282 static int do_load_vmstate(BlockDriverState
*bs
, char *buf
, int64_t offset
,
283 int count
, int *total
)
285 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
292 static int do_save_vmstate(BlockDriverState
*bs
, char *buf
, int64_t offset
,
293 int count
, int *total
)
295 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
302 #define NOT_DONE 0x7fffffff
303 static void aio_rw_done(void *opaque
, int ret
)
305 *(int *)opaque
= ret
;
308 static int do_aio_readv(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
309 int64_t offset
, int *total
)
311 int async_ret
= NOT_DONE
;
313 bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
314 aio_rw_done
, &async_ret
);
315 while (async_ret
== NOT_DONE
) {
316 main_loop_wait(false);
320 return async_ret
< 0 ? async_ret
: 1;
323 static int do_aio_writev(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
324 int64_t offset
, int *total
)
326 int async_ret
= NOT_DONE
;
328 bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
329 aio_rw_done
, &async_ret
);
330 while (async_ret
== NOT_DONE
) {
331 main_loop_wait(false);
335 return async_ret
< 0 ? async_ret
: 1;
338 struct multiwrite_async_ret
{
343 static void multiwrite_cb(void *opaque
, int ret
)
345 struct multiwrite_async_ret
*async_ret
= opaque
;
347 async_ret
->num_done
++;
349 async_ret
->error
= ret
;
353 static int do_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
* reqs
,
354 int num_reqs
, int *total
)
357 struct multiwrite_async_ret async_ret
= {
363 for (i
= 0; i
< num_reqs
; i
++) {
364 reqs
[i
].cb
= multiwrite_cb
;
365 reqs
[i
].opaque
= &async_ret
;
366 *total
+= reqs
[i
].qiov
->size
;
369 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
374 while (async_ret
.num_done
< num_reqs
) {
375 main_loop_wait(false);
378 return async_ret
.error
< 0 ? async_ret
.error
: 1;
381 static void read_help(void)
385 " reads a range of bytes from the given offset\n"
388 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
390 " Reads a segment of the currently open file, optionally dumping it to the\n"
391 " standard output stream (with -v option) for subsequent inspection.\n"
392 " -b, -- read from the VM state rather than the virtual disk\n"
393 " -C, -- report statistics in a machine parsable format\n"
394 " -l, -- length for pattern verification (only with -P)\n"
395 " -p, -- use bdrv_pread to read the file\n"
396 " -P, -- use a pattern to verify read data\n"
397 " -q, -- quiet mode, do not show I/O statistics\n"
398 " -s, -- start offset for pattern verification (only with -P)\n"
399 " -v, -- dump buffer to standard output\n"
403 static int read_f(BlockDriverState
*bs
, int argc
, char **argv
);
405 static const cmdinfo_t read_cmd
= {
411 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
412 .oneline
= "reads a number of bytes at a specified offset",
416 static int read_f(BlockDriverState
*bs
, int argc
, char **argv
)
418 struct timeval t1
, t2
;
419 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
420 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
425 /* Some compilers get confused and warn if this is not initialized. */
427 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
429 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
439 pattern_count
= cvtnum(optarg
);
440 if (pattern_count
< 0) {
441 printf("non-numeric length argument -- %s\n", optarg
);
450 pattern
= parse_pattern(optarg
);
460 pattern_offset
= cvtnum(optarg
);
461 if (pattern_offset
< 0) {
462 printf("non-numeric length argument -- %s\n", optarg
);
470 return command_usage(&read_cmd
);
474 if (optind
!= argc
- 2) {
475 return command_usage(&read_cmd
);
478 if (bflag
&& pflag
) {
479 printf("-b and -p cannot be specified at the same time\n");
483 offset
= cvtnum(argv
[optind
]);
485 printf("non-numeric length argument -- %s\n", argv
[optind
]);
490 count
= cvtnum(argv
[optind
]);
492 printf("non-numeric length argument -- %s\n", argv
[optind
]);
496 if (!Pflag
&& (lflag
|| sflag
)) {
497 return command_usage(&read_cmd
);
501 pattern_count
= count
- pattern_offset
;
504 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
505 printf("pattern verification range exceeds end of read data\n");
510 if (offset
& 0x1ff) {
511 printf("offset %" PRId64
" is not sector aligned\n",
516 printf("count %d is not sector aligned\n",
522 buf
= qemu_io_alloc(bs
, count
, 0xab);
524 gettimeofday(&t1
, NULL
);
526 cnt
= do_pread(bs
, buf
, offset
, count
, &total
);
528 cnt
= do_load_vmstate(bs
, buf
, offset
, count
, &total
);
530 cnt
= do_read(bs
, buf
, offset
, count
, &total
);
532 gettimeofday(&t2
, NULL
);
535 printf("read failed: %s\n", strerror(-cnt
));
540 void *cmp_buf
= g_malloc(pattern_count
);
541 memset(cmp_buf
, pattern
, pattern_count
);
542 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
543 printf("Pattern verification failed at offset %"
544 PRId64
", %d bytes\n",
545 offset
+ pattern_offset
, pattern_count
);
555 dump_buffer(buf
, offset
, count
);
558 /* Finally, report back -- -C gives a parsable format */
560 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
568 static void readv_help(void)
572 " reads a range of bytes from the given offset into multiple buffers\n"
575 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
577 " Reads a segment of the currently open file, optionally dumping it to the\n"
578 " standard output stream (with -v option) for subsequent inspection.\n"
579 " Uses multiple iovec buffers if more than one byte range is specified.\n"
580 " -C, -- report statistics in a machine parsable format\n"
581 " -P, -- use a pattern to verify read data\n"
582 " -v, -- dump buffer to standard output\n"
583 " -q, -- quiet mode, do not show I/O statistics\n"
587 static int readv_f(BlockDriverState
*bs
, int argc
, char **argv
);
589 static const cmdinfo_t readv_cmd
= {
594 .args
= "[-Cqv] [-P pattern ] off len [len..]",
595 .oneline
= "reads a number of bytes at a specified offset",
599 static int readv_f(BlockDriverState
*bs
, int argc
, char **argv
)
601 struct timeval t1
, t2
;
602 int Cflag
= 0, qflag
= 0, vflag
= 0;
606 /* Some compilers get confused and warn if this is not initialized. */
613 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
620 pattern
= parse_pattern(optarg
);
632 return command_usage(&readv_cmd
);
636 if (optind
> argc
- 2) {
637 return command_usage(&readv_cmd
);
641 offset
= cvtnum(argv
[optind
]);
643 printf("non-numeric length argument -- %s\n", argv
[optind
]);
648 if (offset
& 0x1ff) {
649 printf("offset %" PRId64
" is not sector aligned\n",
654 nr_iov
= argc
- optind
;
655 buf
= create_iovec(bs
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
660 gettimeofday(&t1
, NULL
);
661 cnt
= do_aio_readv(bs
, &qiov
, offset
, &total
);
662 gettimeofday(&t2
, NULL
);
665 printf("readv failed: %s\n", strerror(-cnt
));
670 void *cmp_buf
= g_malloc(qiov
.size
);
671 memset(cmp_buf
, pattern
, qiov
.size
);
672 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
673 printf("Pattern verification failed at offset %"
674 PRId64
", %zd bytes\n", offset
, qiov
.size
);
684 dump_buffer(buf
, offset
, qiov
.size
);
687 /* Finally, report back -- -C gives a parsable format */
689 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
692 qemu_iovec_destroy(&qiov
);
697 static void write_help(void)
701 " writes a range of bytes from the given offset\n"
704 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
706 " Writes into a segment of the currently open file, using a buffer\n"
707 " filled with a set pattern (0xcdcdcdcd).\n"
708 " -b, -- write to the VM state rather than the virtual disk\n"
709 " -c, -- write compressed data with bdrv_write_compressed\n"
710 " -p, -- use bdrv_pwrite to write the file\n"
711 " -P, -- use different pattern to fill file\n"
712 " -C, -- report statistics in a machine parsable format\n"
713 " -q, -- quiet mode, do not show I/O statistics\n"
714 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
718 static int write_f(BlockDriverState
*bs
, int argc
, char **argv
);
720 static const cmdinfo_t write_cmd
= {
726 .args
= "[-bcCpqz] [-P pattern ] off len",
727 .oneline
= "writes a number of bytes at a specified offset",
731 static int write_f(BlockDriverState
*bs
, int argc
, char **argv
)
733 struct timeval t1
, t2
;
734 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
740 /* Some compilers get confused and warn if this is not initialized. */
744 while ((c
= getopt(argc
, argv
, "bcCpP:qz")) != EOF
) {
760 pattern
= parse_pattern(optarg
);
772 return command_usage(&write_cmd
);
776 if (optind
!= argc
- 2) {
777 return command_usage(&write_cmd
);
780 if (bflag
+ pflag
+ zflag
> 1) {
781 printf("-b, -p, or -z cannot be specified at the same time\n");
785 if (zflag
&& Pflag
) {
786 printf("-z and -P cannot be specified at the same time\n");
790 offset
= cvtnum(argv
[optind
]);
792 printf("non-numeric length argument -- %s\n", argv
[optind
]);
797 count
= cvtnum(argv
[optind
]);
799 printf("non-numeric length argument -- %s\n", argv
[optind
]);
804 if (offset
& 0x1ff) {
805 printf("offset %" PRId64
" is not sector aligned\n",
811 printf("count %d is not sector aligned\n",
818 buf
= qemu_io_alloc(bs
, count
, pattern
);
821 gettimeofday(&t1
, NULL
);
823 cnt
= do_pwrite(bs
, buf
, offset
, count
, &total
);
825 cnt
= do_save_vmstate(bs
, buf
, offset
, count
, &total
);
827 cnt
= do_co_write_zeroes(bs
, offset
, count
, &total
);
829 cnt
= do_write_compressed(bs
, buf
, offset
, count
, &total
);
831 cnt
= do_write(bs
, buf
, offset
, count
, &total
);
833 gettimeofday(&t2
, NULL
);
836 printf("write failed: %s\n", strerror(-cnt
));
844 /* Finally, report back -- -C gives a parsable format */
846 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
861 " writes a range of bytes from the given offset source from multiple buffers\n"
864 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
866 " Writes into a segment of the currently open file, using a buffer\n"
867 " filled with a set pattern (0xcdcdcdcd).\n"
868 " -P, -- use different pattern to fill file\n"
869 " -C, -- report statistics in a machine parsable format\n"
870 " -q, -- quiet mode, do not show I/O statistics\n"
874 static int writev_f(BlockDriverState
*bs
, int argc
, char **argv
);
876 static const cmdinfo_t writev_cmd
= {
881 .args
= "[-Cq] [-P pattern ] off len [len..]",
882 .oneline
= "writes a number of bytes at a specified offset",
886 static int writev_f(BlockDriverState
*bs
, int argc
, char **argv
)
888 struct timeval t1
, t2
;
889 int Cflag
= 0, qflag
= 0;
893 /* Some compilers get confused and warn if this is not initialized. */
899 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
908 pattern
= parse_pattern(optarg
);
914 return command_usage(&writev_cmd
);
918 if (optind
> argc
- 2) {
919 return command_usage(&writev_cmd
);
922 offset
= cvtnum(argv
[optind
]);
924 printf("non-numeric length argument -- %s\n", argv
[optind
]);
929 if (offset
& 0x1ff) {
930 printf("offset %" PRId64
" is not sector aligned\n",
935 nr_iov
= argc
- optind
;
936 buf
= create_iovec(bs
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
941 gettimeofday(&t1
, NULL
);
942 cnt
= do_aio_writev(bs
, &qiov
, offset
, &total
);
943 gettimeofday(&t2
, NULL
);
946 printf("writev failed: %s\n", strerror(-cnt
));
954 /* Finally, report back -- -C gives a parsable format */
956 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
958 qemu_iovec_destroy(&qiov
);
963 static void multiwrite_help(void)
967 " writes a range of bytes from the given offset source from multiple buffers,\n"
968 " in a batch of requests that may be merged by qemu\n"
971 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
972 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
974 " Writes into a segment of the currently open file, using a buffer\n"
975 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
976 " by one for each request contained in the multiwrite command.\n"
977 " -P, -- use different pattern to fill file\n"
978 " -C, -- report statistics in a machine parsable format\n"
979 " -q, -- quiet mode, do not show I/O statistics\n"
983 static int multiwrite_f(BlockDriverState
*bs
, int argc
, char **argv
);
985 static const cmdinfo_t multiwrite_cmd
= {
986 .name
= "multiwrite",
987 .cfunc
= multiwrite_f
,
990 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
991 .oneline
= "issues multiple write requests at once",
992 .help
= multiwrite_help
,
995 static int multiwrite_f(BlockDriverState
*bs
, int argc
, char **argv
)
997 struct timeval t1
, t2
;
998 int Cflag
= 0, qflag
= 0;
1001 int64_t offset
, first_offset
= 0;
1002 /* Some compilers get confused and warn if this is not initialized. */
1007 QEMUIOVector
*qiovs
;
1011 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1020 pattern
= parse_pattern(optarg
);
1026 return command_usage(&writev_cmd
);
1030 if (optind
> argc
- 2) {
1031 return command_usage(&writev_cmd
);
1035 for (i
= optind
; i
< argc
; i
++) {
1036 if (!strcmp(argv
[i
], ";")) {
1041 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
1042 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
1043 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
1045 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1048 /* Read the offset of the request */
1049 offset
= cvtnum(argv
[optind
]);
1051 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
1056 if (offset
& 0x1ff) {
1057 printf("offset %lld is not sector aligned\n",
1063 first_offset
= offset
;
1066 /* Read lengths for qiov entries */
1067 for (j
= optind
; j
< argc
; j
++) {
1068 if (!strcmp(argv
[j
], ";")) {
1073 nr_iov
= j
- optind
;
1076 buf
[i
] = create_iovec(bs
, &qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1077 if (buf
[i
] == NULL
) {
1081 reqs
[i
].qiov
= &qiovs
[i
];
1082 reqs
[i
].sector
= offset
>> 9;
1083 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1090 /* If there were empty requests at the end, ignore them */
1093 gettimeofday(&t1
, NULL
);
1094 cnt
= do_aio_multiwrite(bs
, reqs
, nr_reqs
, &total
);
1095 gettimeofday(&t2
, NULL
);
1098 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1106 /* Finally, report back -- -C gives a parsable format */
1108 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1110 for (i
= 0; i
< nr_reqs
; i
++) {
1111 qemu_io_free(buf
[i
]);
1112 if (reqs
[i
].qiov
!= NULL
) {
1113 qemu_iovec_destroy(&qiovs
[i
]);
1134 static void aio_write_done(void *opaque
, int ret
)
1136 struct aio_ctx
*ctx
= opaque
;
1139 gettimeofday(&t2
, NULL
);
1143 printf("aio_write failed: %s\n", strerror(-ret
));
1151 /* Finally, report back -- -C gives a parsable format */
1152 t2
= tsub(t2
, ctx
->t1
);
1153 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1154 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1156 qemu_io_free(ctx
->buf
);
1157 qemu_iovec_destroy(&ctx
->qiov
);
1161 static void aio_read_done(void *opaque
, int ret
)
1163 struct aio_ctx
*ctx
= opaque
;
1166 gettimeofday(&t2
, NULL
);
1169 printf("readv failed: %s\n", strerror(-ret
));
1174 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1176 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1177 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1178 printf("Pattern verification failed at offset %"
1179 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1189 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1192 /* Finally, report back -- -C gives a parsable format */
1193 t2
= tsub(t2
, ctx
->t1
);
1194 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1195 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1197 qemu_io_free(ctx
->buf
);
1198 qemu_iovec_destroy(&ctx
->qiov
);
1202 static void aio_read_help(void)
1206 " asynchronously reads a range of bytes from the given offset\n"
1209 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1211 " Reads a segment of the currently open file, optionally dumping it to the\n"
1212 " standard output stream (with -v option) for subsequent inspection.\n"
1213 " The read is performed asynchronously and the aio_flush command must be\n"
1214 " used to ensure all outstanding aio requests have been completed.\n"
1215 " -C, -- report statistics in a machine parsable format\n"
1216 " -P, -- use a pattern to verify read data\n"
1217 " -v, -- dump buffer to standard output\n"
1218 " -q, -- quiet mode, do not show I/O statistics\n"
1222 static int aio_read_f(BlockDriverState
*bs
, int argc
, char **argv
);
1224 static const cmdinfo_t aio_read_cmd
= {
1226 .cfunc
= aio_read_f
,
1229 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1230 .oneline
= "asynchronously reads a number of bytes",
1231 .help
= aio_read_help
,
1234 static int aio_read_f(BlockDriverState
*bs
, int argc
, char **argv
)
1237 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1239 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1246 ctx
->pattern
= parse_pattern(optarg
);
1247 if (ctx
->pattern
< 0) {
1260 return command_usage(&aio_read_cmd
);
1264 if (optind
> argc
- 2) {
1266 return command_usage(&aio_read_cmd
);
1269 ctx
->offset
= cvtnum(argv
[optind
]);
1270 if (ctx
->offset
< 0) {
1271 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1277 if (ctx
->offset
& 0x1ff) {
1278 printf("offset %" PRId64
" is not sector aligned\n",
1284 nr_iov
= argc
- optind
;
1285 ctx
->buf
= create_iovec(bs
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1286 if (ctx
->buf
== NULL
) {
1291 gettimeofday(&ctx
->t1
, NULL
);
1292 bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1293 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1297 static void aio_write_help(void)
1301 " asynchronously writes a range of bytes from the given offset source\n"
1302 " from multiple buffers\n"
1305 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1307 " Writes into a segment of the currently open file, using a buffer\n"
1308 " filled with a set pattern (0xcdcdcdcd).\n"
1309 " The write is performed asynchronously and the aio_flush command must be\n"
1310 " used to ensure all outstanding aio requests have been completed.\n"
1311 " -P, -- use different pattern to fill file\n"
1312 " -C, -- report statistics in a machine parsable format\n"
1313 " -q, -- quiet mode, do not show I/O statistics\n"
1317 static int aio_write_f(BlockDriverState
*bs
, int argc
, char **argv
);
1319 static const cmdinfo_t aio_write_cmd
= {
1320 .name
= "aio_write",
1321 .cfunc
= aio_write_f
,
1324 .args
= "[-Cq] [-P pattern ] off len [len..]",
1325 .oneline
= "asynchronously writes a number of bytes",
1326 .help
= aio_write_help
,
1329 static int aio_write_f(BlockDriverState
*bs
, int argc
, char **argv
)
1333 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1335 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1344 pattern
= parse_pattern(optarg
);
1352 return command_usage(&aio_write_cmd
);
1356 if (optind
> argc
- 2) {
1358 return command_usage(&aio_write_cmd
);
1361 ctx
->offset
= cvtnum(argv
[optind
]);
1362 if (ctx
->offset
< 0) {
1363 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1369 if (ctx
->offset
& 0x1ff) {
1370 printf("offset %" PRId64
" is not sector aligned\n",
1376 nr_iov
= argc
- optind
;
1377 ctx
->buf
= create_iovec(bs
, &ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1378 if (ctx
->buf
== NULL
) {
1383 gettimeofday(&ctx
->t1
, NULL
);
1384 bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1385 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1389 static int aio_flush_f(BlockDriverState
*bs
, int argc
, char **argv
)
1395 static const cmdinfo_t aio_flush_cmd
= {
1396 .name
= "aio_flush",
1397 .cfunc
= aio_flush_f
,
1398 .oneline
= "completes all outstanding aio requests"
1401 static int flush_f(BlockDriverState
*bs
, int argc
, char **argv
)
1407 static const cmdinfo_t flush_cmd
= {
1411 .oneline
= "flush all in-core file state to disk",
1414 static int truncate_f(BlockDriverState
*bs
, int argc
, char **argv
)
1419 offset
= cvtnum(argv
[1]);
1421 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1425 ret
= bdrv_truncate(bs
, offset
);
1427 printf("truncate: %s\n", strerror(-ret
));
1434 static const cmdinfo_t truncate_cmd
= {
1437 .cfunc
= truncate_f
,
1441 .oneline
= "truncates the current file at the given offset",
1444 static int length_f(BlockDriverState
*bs
, int argc
, char **argv
)
1449 size
= bdrv_getlength(bs
);
1451 printf("getlength: %s\n", strerror(-size
));
1455 cvtstr(size
, s1
, sizeof(s1
));
1461 static const cmdinfo_t length_cmd
= {
1465 .oneline
= "gets the length of the current file",
1469 static int info_f(BlockDriverState
*bs
, int argc
, char **argv
)
1471 BlockDriverInfo bdi
;
1472 char s1
[64], s2
[64];
1475 if (bs
->drv
&& bs
->drv
->format_name
) {
1476 printf("format name: %s\n", bs
->drv
->format_name
);
1478 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1479 printf("format name: %s\n", bs
->drv
->protocol_name
);
1482 ret
= bdrv_get_info(bs
, &bdi
);
1487 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1488 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1490 printf("cluster size: %s\n", s1
);
1491 printf("vm state offset: %s\n", s2
);
1498 static const cmdinfo_t info_cmd
= {
1502 .oneline
= "prints information about the current file",
1505 static void discard_help(void)
1509 " discards a range of bytes from the given offset\n"
1512 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1514 " Discards a segment of the currently open file.\n"
1515 " -C, -- report statistics in a machine parsable format\n"
1516 " -q, -- quiet mode, do not show I/O statistics\n"
1520 static int discard_f(BlockDriverState
*bs
, int argc
, char **argv
);
1522 static const cmdinfo_t discard_cmd
= {
1528 .args
= "[-Cq] off len",
1529 .oneline
= "discards a number of bytes at a specified offset",
1530 .help
= discard_help
,
1533 static int discard_f(BlockDriverState
*bs
, int argc
, char **argv
)
1535 struct timeval t1
, t2
;
1536 int Cflag
= 0, qflag
= 0;
1541 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1550 return command_usage(&discard_cmd
);
1554 if (optind
!= argc
- 2) {
1555 return command_usage(&discard_cmd
);
1558 offset
= cvtnum(argv
[optind
]);
1560 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1565 count
= cvtnum(argv
[optind
]);
1567 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1571 gettimeofday(&t1
, NULL
);
1572 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1573 count
>> BDRV_SECTOR_BITS
);
1574 gettimeofday(&t2
, NULL
);
1577 printf("discard failed: %s\n", strerror(-ret
));
1581 /* Finally, report back -- -C gives a parsable format */
1584 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1591 static int alloc_f(BlockDriverState
*bs
, int argc
, char **argv
)
1593 int64_t offset
, sector_num
;
1594 int nb_sectors
, remaining
;
1599 offset
= cvtnum(argv
[1]);
1601 printf("non-numeric offset argument -- %s\n", argv
[1]);
1603 } else if (offset
& 0x1ff) {
1604 printf("offset %" PRId64
" is not sector aligned\n",
1610 nb_sectors
= cvtnum(argv
[2]);
1611 if (nb_sectors
< 0) {
1612 printf("non-numeric length argument -- %s\n", argv
[2]);
1619 remaining
= nb_sectors
;
1621 sector_num
= offset
>> 9;
1623 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1630 nb_sectors
-= remaining
;
1635 cvtstr(offset
, s1
, sizeof(s1
));
1637 printf("%d/%d sectors allocated at offset %s\n",
1638 sum_alloc
, nb_sectors
, s1
);
1642 static const cmdinfo_t alloc_cmd
= {
1648 .args
= "off [sectors]",
1649 .oneline
= "checks if a sector is present in the file",
1653 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1654 int64_t nb_sectors
, int64_t *pnum
)
1656 int num
, num_checked
;
1659 num_checked
= MIN(nb_sectors
, INT_MAX
);
1660 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1668 while (nb_sectors
> 0 && ret
== firstret
) {
1672 num_checked
= MIN(nb_sectors
, INT_MAX
);
1673 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1674 if (ret
== firstret
) {
1684 static int map_f(BlockDriverState
*bs
, int argc
, char **argv
)
1694 nb_sectors
= bs
->total_sectors
;
1697 ret
= map_is_allocated(bs
, offset
, nb_sectors
, &num
);
1699 error_report("Failed to get allocation status: %s", strerror(-ret
));
1703 retstr
= ret
? " allocated" : "not allocated";
1704 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1705 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1706 "at offset %s (%d)\n",
1707 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
1711 } while (offset
< bs
->total_sectors
);
1716 static const cmdinfo_t map_cmd
= {
1722 .oneline
= "prints the allocated areas of a file",
1725 static int break_f(BlockDriverState
*bs
, int argc
, char **argv
)
1729 ret
= bdrv_debug_breakpoint(bs
, argv
[1], argv
[2]);
1731 printf("Could not set breakpoint: %s\n", strerror(-ret
));
1737 static const cmdinfo_t break_cmd
= {
1742 .args
= "event tag",
1743 .oneline
= "sets a breakpoint on event and tags the stopped "
1747 static int resume_f(BlockDriverState
*bs
, int argc
, char **argv
)
1751 ret
= bdrv_debug_resume(bs
, argv
[1]);
1753 printf("Could not resume request: %s\n", strerror(-ret
));
1759 static const cmdinfo_t resume_cmd
= {
1765 .oneline
= "resumes the request tagged as tag",
1768 static int wait_break_f(BlockDriverState
*bs
, int argc
, char **argv
)
1770 while (!bdrv_debug_is_suspended(bs
, argv
[1])) {
1777 static const cmdinfo_t wait_break_cmd
= {
1778 .name
= "wait_break",
1781 .cfunc
= wait_break_f
,
1783 .oneline
= "waits for the suspension of a request",
1786 static int abort_f(BlockDriverState
*bs
, int argc
, char **argv
)
1791 static const cmdinfo_t abort_cmd
= {
1794 .flags
= CMD_NOFILE_OK
,
1795 .oneline
= "simulate a program crash using abort(3)",
1798 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
1803 printf("%s ", ct
->name
);
1805 printf("(or %s) ", ct
->altname
);
1810 printf("%s ", ct
->args
);
1812 printf("-- %s\n", ct
->oneline
);
1815 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
1817 help_oneline(cmd
, ct
);
1823 static void help_all(void)
1825 const cmdinfo_t
*ct
;
1827 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
1828 help_oneline(ct
->name
, ct
);
1830 printf("\nUse 'help commandname' for extended help.\n");
1833 static int help_f(BlockDriverState
*bs
, int argc
, char **argv
)
1835 const cmdinfo_t
*ct
;
1842 ct
= find_command(argv
[1]);
1844 printf("command %s not found\n", argv
[1]);
1848 help_onecmd(argv
[1], ct
);
1852 static const cmdinfo_t help_cmd
= {
1858 .flags
= CMD_FLAG_GLOBAL
,
1859 .args
= "[command]",
1860 .oneline
= "help for one or all commands",
1863 static int init_check_command(BlockDriverState
*bs
, const cmdinfo_t
*ct
)
1865 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
1868 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1869 fprintf(stderr
, "no file open, try 'help open'\n");
1875 bool qemuio_command(const char *cmd
)
1878 const cmdinfo_t
*ct
;
1883 input
= g_strdup(cmd
);
1884 v
= breakline(input
, &c
);
1886 ct
= find_command(v
[0]);
1888 done
= command(ct
, c
, v
);
1890 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
1899 static void __attribute((constructor
)) init_qemuio_commands(void)
1901 /* initialize commands */
1902 add_command(&help_cmd
);
1903 add_command(&read_cmd
);
1904 add_command(&readv_cmd
);
1905 add_command(&write_cmd
);
1906 add_command(&writev_cmd
);
1907 add_command(&multiwrite_cmd
);
1908 add_command(&aio_read_cmd
);
1909 add_command(&aio_write_cmd
);
1910 add_command(&aio_flush_cmd
);
1911 add_command(&flush_cmd
);
1912 add_command(&truncate_cmd
);
1913 add_command(&length_cmd
);
1914 add_command(&info_cmd
);
1915 add_command(&discard_cmd
);
1916 add_command(&alloc_cmd
);
1917 add_command(&map_cmd
);
1918 add_command(&break_cmd
);
1919 add_command(&resume_cmd
);
1920 add_command(&wait_break_cmd
);
1921 add_command(&abort_cmd
);
1923 add_check_command(init_check_command
);