2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
12 #include "block/block_int.h"
13 #include "block/qapi.h"
14 #include "qemu/main-loop.h"
16 #define CMD_NOFILE_OK 0x01
20 static cmdinfo_t
*cmdtab
;
23 static int compare_cmdname(const void *a
, const void *b
)
25 return strcmp(((const cmdinfo_t
*)a
)->name
,
26 ((const cmdinfo_t
*)b
)->name
);
29 void qemuio_add_command(const cmdinfo_t
*ci
)
31 cmdtab
= g_realloc(cmdtab
, ++ncmds
* sizeof(*cmdtab
));
32 cmdtab
[ncmds
- 1] = *ci
;
33 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
36 int qemuio_command_usage(const cmdinfo_t
*ci
)
38 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
42 static int init_check_command(BlockDriverState
*bs
, const cmdinfo_t
*ct
)
44 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
47 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
48 fprintf(stderr
, "no file open, try 'help open'\n");
54 static int command(BlockDriverState
*bs
, const cmdinfo_t
*ct
, int argc
,
59 if (!init_check_command(bs
, ct
)) {
63 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
64 if (ct
->argmax
== -1) {
66 "bad argument count %d to %s, expected at least %d arguments\n",
67 argc
-1, cmd
, ct
->argmin
);
68 } else if (ct
->argmin
== ct
->argmax
) {
70 "bad argument count %d to %s, expected %d arguments\n",
71 argc
-1, cmd
, ct
->argmin
);
74 "bad argument count %d to %s, expected between %d and %d arguments\n",
75 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
80 return ct
->cfunc(bs
, argc
, argv
);
83 static const cmdinfo_t
*find_command(const char *cmd
)
87 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
88 if (strcmp(ct
->name
, cmd
) == 0 ||
89 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
91 return (const cmdinfo_t
*)ct
;
97 /* Invoke fn() for commands with a matching prefix */
98 void qemuio_complete_command(const char *input
,
99 void (*fn
)(const char *cmd
, void *opaque
),
103 size_t input_len
= strlen(input
);
105 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
106 if (strncmp(input
, ct
->name
, input_len
) == 0) {
107 fn(ct
->name
, opaque
);
112 static char **breakline(char *input
, int *count
)
116 char **rval
= g_malloc0(sizeof(char *));
119 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
124 tmp
= g_realloc(rval
, sizeof(*rval
) * (c
+ 1));
140 static int64_t cvtnum(const char *s
)
143 return strtosz_suffix(s
, &end
, STRTOSZ_DEFSUFFIX_B
);
146 #define EXABYTES(x) ((long long)(x) << 60)
147 #define PETABYTES(x) ((long long)(x) << 50)
148 #define TERABYTES(x) ((long long)(x) << 40)
149 #define GIGABYTES(x) ((long long)(x) << 30)
150 #define MEGABYTES(x) ((long long)(x) << 20)
151 #define KILOBYTES(x) ((long long)(x) << 10)
153 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
154 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
155 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
156 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
157 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
158 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
160 static void cvtstr(double value
, char *str
, size_t size
)
165 if (value
>= EXABYTES(1)) {
167 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
168 } else if (value
>= PETABYTES(1)) {
170 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
171 } else if (value
>= TERABYTES(1)) {
173 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
174 } else if (value
>= GIGABYTES(1)) {
176 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
177 } else if (value
>= MEGABYTES(1)) {
179 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
180 } else if (value
>= KILOBYTES(1)) {
182 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
185 snprintf(str
, size
- 6, "%f", value
);
188 trim
= strstr(str
, ".000");
190 strcpy(trim
, suffix
);
198 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
200 t1
.tv_usec
-= t2
.tv_usec
;
201 if (t1
.tv_usec
< 0) {
202 t1
.tv_usec
+= 1000000;
205 t1
.tv_sec
-= t2
.tv_sec
;
209 static double tdiv(double value
, struct timeval tv
)
211 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
214 #define HOURS(sec) ((sec) / (60 * 60))
215 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
216 #define SECONDS(sec) ((sec) % 60)
220 TERSE_FIXED_TIME
= 0x1,
221 VERBOSE_FIXED_TIME
= 0x2,
224 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
226 double usec
= (double)tv
->tv_usec
/ 1000000.0;
228 if (format
& TERSE_FIXED_TIME
) {
229 if (!HOURS(tv
->tv_sec
)) {
230 snprintf(ts
, size
, "%u:%02u.%02u",
231 (unsigned int) MINUTES(tv
->tv_sec
),
232 (unsigned int) SECONDS(tv
->tv_sec
),
233 (unsigned int) (usec
* 100));
236 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
239 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
240 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
241 (unsigned int) HOURS(tv
->tv_sec
),
242 (unsigned int) MINUTES(tv
->tv_sec
),
243 (unsigned int) SECONDS(tv
->tv_sec
),
244 (unsigned int) (usec
* 100));
246 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
251 * Parse the pattern argument to various sub-commands.
253 * Because the pattern is used as an argument to memset it must evaluate
254 * to an unsigned integer that fits into a single byte.
256 static int parse_pattern(const char *arg
)
261 pattern
= strtol(arg
, &endptr
, 0);
262 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
263 printf("%s is not a valid pattern byte\n", arg
);
271 * Memory allocation helpers.
273 * Make sure memory is aligned by default, or purposefully misaligned if
274 * that is specified on the command line.
277 #define MISALIGN_OFFSET 16
278 static void *qemu_io_alloc(BlockDriverState
*bs
, size_t len
, int pattern
)
282 if (qemuio_misalign
) {
283 len
+= MISALIGN_OFFSET
;
285 buf
= qemu_blockalign(bs
, len
);
286 memset(buf
, pattern
, len
);
287 if (qemuio_misalign
) {
288 buf
+= MISALIGN_OFFSET
;
293 static void qemu_io_free(void *p
)
295 if (qemuio_misalign
) {
296 p
-= MISALIGN_OFFSET
;
301 static void dump_buffer(const void *buffer
, int64_t offset
, int len
)
306 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
307 const uint8_t *s
= p
;
309 printf("%08" PRIx64
": ", offset
+ i
);
310 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
314 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
325 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
326 int count
, int total
, int cnt
, int Cflag
)
328 char s1
[64], s2
[64], ts
[64];
330 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
332 cvtstr((double)total
, s1
, sizeof(s1
));
333 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
334 printf("%s %d/%d bytes at offset %" PRId64
"\n",
335 op
, total
, count
, offset
);
336 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
337 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
338 } else {/* bytes,ops,time,bytes/sec,ops/sec */
339 printf("%d,%d,%s,%.3f,%.3f\n",
341 tdiv((double)total
, *t
),
342 tdiv((double)cnt
, *t
));
347 * Parse multiple length statements for vectored I/O, and construct an I/O
348 * vector matching it.
351 create_iovec(BlockDriverState
*bs
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
354 size_t *sizes
= g_new0(size_t, nr_iov
);
360 for (i
= 0; i
< nr_iov
; i
++) {
366 printf("non-numeric length argument -- %s\n", arg
);
370 /* should be SIZE_T_MAX, but that doesn't exist */
372 printf("too large length argument -- %s\n", arg
);
377 printf("length argument %" PRId64
378 " is not sector aligned\n", len
);
386 qemu_iovec_init(qiov
, nr_iov
);
388 buf
= p
= qemu_io_alloc(bs
, count
, pattern
);
390 for (i
= 0; i
< nr_iov
; i
++) {
391 qemu_iovec_add(qiov
, p
, sizes
[i
]);
400 static int do_read(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
405 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
413 static int do_write(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
418 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
426 static int do_pread(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
429 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
436 static int do_pwrite(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
439 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
447 BlockDriverState
*bs
;
455 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
457 CoWriteZeroes
*data
= opaque
;
459 data
->ret
= bdrv_co_write_zeroes(data
->bs
, data
->offset
/ BDRV_SECTOR_SIZE
,
460 data
->count
/ BDRV_SECTOR_SIZE
, 0);
463 *data
->total
= data
->ret
;
467 *data
->total
= data
->count
;
470 static int do_co_write_zeroes(BlockDriverState
*bs
, int64_t offset
, int count
,
474 CoWriteZeroes data
= {
482 co
= qemu_coroutine_create(co_write_zeroes_entry
);
483 qemu_coroutine_enter(co
, &data
);
494 static int do_write_compressed(BlockDriverState
*bs
, char *buf
, int64_t offset
,
495 int count
, int *total
)
499 ret
= bdrv_write_compressed(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
507 static int do_load_vmstate(BlockDriverState
*bs
, char *buf
, int64_t offset
,
508 int count
, int *total
)
510 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
517 static int do_save_vmstate(BlockDriverState
*bs
, char *buf
, int64_t offset
,
518 int count
, int *total
)
520 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
527 #define NOT_DONE 0x7fffffff
528 static void aio_rw_done(void *opaque
, int ret
)
530 *(int *)opaque
= ret
;
533 static int do_aio_readv(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
534 int64_t offset
, int *total
)
536 int async_ret
= NOT_DONE
;
538 bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
539 aio_rw_done
, &async_ret
);
540 while (async_ret
== NOT_DONE
) {
541 main_loop_wait(false);
545 return async_ret
< 0 ? async_ret
: 1;
548 static int do_aio_writev(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
549 int64_t offset
, int *total
)
551 int async_ret
= NOT_DONE
;
553 bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
554 aio_rw_done
, &async_ret
);
555 while (async_ret
== NOT_DONE
) {
556 main_loop_wait(false);
560 return async_ret
< 0 ? async_ret
: 1;
563 struct multiwrite_async_ret
{
568 static void multiwrite_cb(void *opaque
, int ret
)
570 struct multiwrite_async_ret
*async_ret
= opaque
;
572 async_ret
->num_done
++;
574 async_ret
->error
= ret
;
578 static int do_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
* reqs
,
579 int num_reqs
, int *total
)
582 struct multiwrite_async_ret async_ret
= {
588 for (i
= 0; i
< num_reqs
; i
++) {
589 reqs
[i
].cb
= multiwrite_cb
;
590 reqs
[i
].opaque
= &async_ret
;
591 *total
+= reqs
[i
].qiov
->size
;
594 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
599 while (async_ret
.num_done
< num_reqs
) {
600 main_loop_wait(false);
603 return async_ret
.error
< 0 ? async_ret
.error
: 1;
606 static void read_help(void)
610 " reads a range of bytes from the given offset\n"
613 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
615 " Reads a segment of the currently open file, optionally dumping it to the\n"
616 " standard output stream (with -v option) for subsequent inspection.\n"
617 " -b, -- read from the VM state rather than the virtual disk\n"
618 " -C, -- report statistics in a machine parsable format\n"
619 " -l, -- length for pattern verification (only with -P)\n"
620 " -p, -- use bdrv_pread to read the file\n"
621 " -P, -- use a pattern to verify read data\n"
622 " -q, -- quiet mode, do not show I/O statistics\n"
623 " -s, -- start offset for pattern verification (only with -P)\n"
624 " -v, -- dump buffer to standard output\n"
628 static int read_f(BlockDriverState
*bs
, int argc
, char **argv
);
630 static const cmdinfo_t read_cmd
= {
636 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
637 .oneline
= "reads a number of bytes at a specified offset",
641 static int read_f(BlockDriverState
*bs
, int argc
, char **argv
)
643 struct timeval t1
, t2
;
644 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
645 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
650 /* Some compilers get confused and warn if this is not initialized. */
652 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
654 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
664 pattern_count
= cvtnum(optarg
);
665 if (pattern_count
< 0) {
666 printf("non-numeric length argument -- %s\n", optarg
);
675 pattern
= parse_pattern(optarg
);
685 pattern_offset
= cvtnum(optarg
);
686 if (pattern_offset
< 0) {
687 printf("non-numeric length argument -- %s\n", optarg
);
695 return qemuio_command_usage(&read_cmd
);
699 if (optind
!= argc
- 2) {
700 return qemuio_command_usage(&read_cmd
);
703 if (bflag
&& pflag
) {
704 printf("-b and -p cannot be specified at the same time\n");
708 offset
= cvtnum(argv
[optind
]);
710 printf("non-numeric length argument -- %s\n", argv
[optind
]);
715 count
= cvtnum(argv
[optind
]);
717 printf("non-numeric length argument -- %s\n", argv
[optind
]);
721 if (!Pflag
&& (lflag
|| sflag
)) {
722 return qemuio_command_usage(&read_cmd
);
726 pattern_count
= count
- pattern_offset
;
729 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
730 printf("pattern verification range exceeds end of read data\n");
735 if (offset
& 0x1ff) {
736 printf("offset %" PRId64
" is not sector aligned\n",
741 printf("count %d is not sector aligned\n",
747 buf
= qemu_io_alloc(bs
, count
, 0xab);
749 gettimeofday(&t1
, NULL
);
751 cnt
= do_pread(bs
, buf
, offset
, count
, &total
);
753 cnt
= do_load_vmstate(bs
, buf
, offset
, count
, &total
);
755 cnt
= do_read(bs
, buf
, offset
, count
, &total
);
757 gettimeofday(&t2
, NULL
);
760 printf("read failed: %s\n", strerror(-cnt
));
765 void *cmp_buf
= g_malloc(pattern_count
);
766 memset(cmp_buf
, pattern
, pattern_count
);
767 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
768 printf("Pattern verification failed at offset %"
769 PRId64
", %d bytes\n",
770 offset
+ pattern_offset
, pattern_count
);
780 dump_buffer(buf
, offset
, count
);
783 /* Finally, report back -- -C gives a parsable format */
785 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
793 static void readv_help(void)
797 " reads a range of bytes from the given offset into multiple buffers\n"
800 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
802 " Reads a segment of the currently open file, optionally dumping it to the\n"
803 " standard output stream (with -v option) for subsequent inspection.\n"
804 " Uses multiple iovec buffers if more than one byte range is specified.\n"
805 " -C, -- report statistics in a machine parsable format\n"
806 " -P, -- use a pattern to verify read data\n"
807 " -v, -- dump buffer to standard output\n"
808 " -q, -- quiet mode, do not show I/O statistics\n"
812 static int readv_f(BlockDriverState
*bs
, int argc
, char **argv
);
814 static const cmdinfo_t readv_cmd
= {
819 .args
= "[-Cqv] [-P pattern ] off len [len..]",
820 .oneline
= "reads a number of bytes at a specified offset",
824 static int readv_f(BlockDriverState
*bs
, int argc
, char **argv
)
826 struct timeval t1
, t2
;
827 int Cflag
= 0, qflag
= 0, vflag
= 0;
831 /* Some compilers get confused and warn if this is not initialized. */
838 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
845 pattern
= parse_pattern(optarg
);
857 return qemuio_command_usage(&readv_cmd
);
861 if (optind
> argc
- 2) {
862 return qemuio_command_usage(&readv_cmd
);
866 offset
= cvtnum(argv
[optind
]);
868 printf("non-numeric length argument -- %s\n", argv
[optind
]);
873 if (offset
& 0x1ff) {
874 printf("offset %" PRId64
" is not sector aligned\n",
879 nr_iov
= argc
- optind
;
880 buf
= create_iovec(bs
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
885 gettimeofday(&t1
, NULL
);
886 cnt
= do_aio_readv(bs
, &qiov
, offset
, &total
);
887 gettimeofday(&t2
, NULL
);
890 printf("readv failed: %s\n", strerror(-cnt
));
895 void *cmp_buf
= g_malloc(qiov
.size
);
896 memset(cmp_buf
, pattern
, qiov
.size
);
897 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
898 printf("Pattern verification failed at offset %"
899 PRId64
", %zd bytes\n", offset
, qiov
.size
);
909 dump_buffer(buf
, offset
, qiov
.size
);
912 /* Finally, report back -- -C gives a parsable format */
914 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
917 qemu_iovec_destroy(&qiov
);
922 static void write_help(void)
926 " writes a range of bytes from the given offset\n"
929 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
931 " Writes into a segment of the currently open file, using a buffer\n"
932 " filled with a set pattern (0xcdcdcdcd).\n"
933 " -b, -- write to the VM state rather than the virtual disk\n"
934 " -c, -- write compressed data with bdrv_write_compressed\n"
935 " -p, -- use bdrv_pwrite to write the file\n"
936 " -P, -- use different pattern to fill file\n"
937 " -C, -- report statistics in a machine parsable format\n"
938 " -q, -- quiet mode, do not show I/O statistics\n"
939 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
943 static int write_f(BlockDriverState
*bs
, int argc
, char **argv
);
945 static const cmdinfo_t write_cmd
= {
951 .args
= "[-bcCpqz] [-P pattern ] off len",
952 .oneline
= "writes a number of bytes at a specified offset",
956 static int write_f(BlockDriverState
*bs
, int argc
, char **argv
)
958 struct timeval t1
, t2
;
959 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
965 /* Some compilers get confused and warn if this is not initialized. */
969 while ((c
= getopt(argc
, argv
, "bcCpP:qz")) != EOF
) {
985 pattern
= parse_pattern(optarg
);
997 return qemuio_command_usage(&write_cmd
);
1001 if (optind
!= argc
- 2) {
1002 return qemuio_command_usage(&write_cmd
);
1005 if (bflag
+ pflag
+ zflag
> 1) {
1006 printf("-b, -p, or -z cannot be specified at the same time\n");
1010 if (zflag
&& Pflag
) {
1011 printf("-z and -P cannot be specified at the same time\n");
1015 offset
= cvtnum(argv
[optind
]);
1017 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1022 count
= cvtnum(argv
[optind
]);
1024 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1029 if (offset
& 0x1ff) {
1030 printf("offset %" PRId64
" is not sector aligned\n",
1035 if (count
& 0x1ff) {
1036 printf("count %d is not sector aligned\n",
1043 buf
= qemu_io_alloc(bs
, count
, pattern
);
1046 gettimeofday(&t1
, NULL
);
1048 cnt
= do_pwrite(bs
, buf
, offset
, count
, &total
);
1050 cnt
= do_save_vmstate(bs
, buf
, offset
, count
, &total
);
1052 cnt
= do_co_write_zeroes(bs
, offset
, count
, &total
);
1054 cnt
= do_write_compressed(bs
, buf
, offset
, count
, &total
);
1056 cnt
= do_write(bs
, buf
, offset
, count
, &total
);
1058 gettimeofday(&t2
, NULL
);
1061 printf("write failed: %s\n", strerror(-cnt
));
1069 /* Finally, report back -- -C gives a parsable format */
1071 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1086 " writes a range of bytes from the given offset source from multiple buffers\n"
1089 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1091 " Writes into a segment of the currently open file, using a buffer\n"
1092 " filled with a set pattern (0xcdcdcdcd).\n"
1093 " -P, -- use different pattern to fill file\n"
1094 " -C, -- report statistics in a machine parsable format\n"
1095 " -q, -- quiet mode, do not show I/O statistics\n"
1099 static int writev_f(BlockDriverState
*bs
, int argc
, char **argv
);
1101 static const cmdinfo_t writev_cmd
= {
1106 .args
= "[-Cq] [-P pattern ] off len [len..]",
1107 .oneline
= "writes a number of bytes at a specified offset",
1108 .help
= writev_help
,
1111 static int writev_f(BlockDriverState
*bs
, int argc
, char **argv
)
1113 struct timeval t1
, t2
;
1114 int Cflag
= 0, qflag
= 0;
1118 /* Some compilers get confused and warn if this is not initialized. */
1124 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1133 pattern
= parse_pattern(optarg
);
1139 return qemuio_command_usage(&writev_cmd
);
1143 if (optind
> argc
- 2) {
1144 return qemuio_command_usage(&writev_cmd
);
1147 offset
= cvtnum(argv
[optind
]);
1149 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1154 if (offset
& 0x1ff) {
1155 printf("offset %" PRId64
" is not sector aligned\n",
1160 nr_iov
= argc
- optind
;
1161 buf
= create_iovec(bs
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1166 gettimeofday(&t1
, NULL
);
1167 cnt
= do_aio_writev(bs
, &qiov
, offset
, &total
);
1168 gettimeofday(&t2
, NULL
);
1171 printf("writev failed: %s\n", strerror(-cnt
));
1179 /* Finally, report back -- -C gives a parsable format */
1181 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1183 qemu_iovec_destroy(&qiov
);
1188 static void multiwrite_help(void)
1192 " writes a range of bytes from the given offset source from multiple buffers,\n"
1193 " in a batch of requests that may be merged by qemu\n"
1196 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1197 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1199 " Writes into a segment of the currently open file, using a buffer\n"
1200 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1201 " by one for each request contained in the multiwrite command.\n"
1202 " -P, -- use different pattern to fill file\n"
1203 " -C, -- report statistics in a machine parsable format\n"
1204 " -q, -- quiet mode, do not show I/O statistics\n"
1208 static int multiwrite_f(BlockDriverState
*bs
, int argc
, char **argv
);
1210 static const cmdinfo_t multiwrite_cmd
= {
1211 .name
= "multiwrite",
1212 .cfunc
= multiwrite_f
,
1215 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1216 .oneline
= "issues multiple write requests at once",
1217 .help
= multiwrite_help
,
1220 static int multiwrite_f(BlockDriverState
*bs
, int argc
, char **argv
)
1222 struct timeval t1
, t2
;
1223 int Cflag
= 0, qflag
= 0;
1226 int64_t offset
, first_offset
= 0;
1227 /* Some compilers get confused and warn if this is not initialized. */
1232 QEMUIOVector
*qiovs
;
1236 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1245 pattern
= parse_pattern(optarg
);
1251 return qemuio_command_usage(&writev_cmd
);
1255 if (optind
> argc
- 2) {
1256 return qemuio_command_usage(&writev_cmd
);
1260 for (i
= optind
; i
< argc
; i
++) {
1261 if (!strcmp(argv
[i
], ";")) {
1266 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
1267 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
1268 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
1270 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1273 /* Read the offset of the request */
1274 offset
= cvtnum(argv
[optind
]);
1276 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
1281 if (offset
& 0x1ff) {
1282 printf("offset %lld is not sector aligned\n",
1288 first_offset
= offset
;
1291 /* Read lengths for qiov entries */
1292 for (j
= optind
; j
< argc
; j
++) {
1293 if (!strcmp(argv
[j
], ";")) {
1298 nr_iov
= j
- optind
;
1301 buf
[i
] = create_iovec(bs
, &qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1302 if (buf
[i
] == NULL
) {
1306 reqs
[i
].qiov
= &qiovs
[i
];
1307 reqs
[i
].sector
= offset
>> 9;
1308 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1315 /* If there were empty requests at the end, ignore them */
1318 gettimeofday(&t1
, NULL
);
1319 cnt
= do_aio_multiwrite(bs
, reqs
, nr_reqs
, &total
);
1320 gettimeofday(&t2
, NULL
);
1323 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1331 /* Finally, report back -- -C gives a parsable format */
1333 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1335 for (i
= 0; i
< nr_reqs
; i
++) {
1336 qemu_io_free(buf
[i
]);
1337 if (reqs
[i
].qiov
!= NULL
) {
1338 qemu_iovec_destroy(&qiovs
[i
]);
1359 static void aio_write_done(void *opaque
, int ret
)
1361 struct aio_ctx
*ctx
= opaque
;
1364 gettimeofday(&t2
, NULL
);
1368 printf("aio_write failed: %s\n", strerror(-ret
));
1376 /* Finally, report back -- -C gives a parsable format */
1377 t2
= tsub(t2
, ctx
->t1
);
1378 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1379 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1381 qemu_io_free(ctx
->buf
);
1382 qemu_iovec_destroy(&ctx
->qiov
);
1386 static void aio_read_done(void *opaque
, int ret
)
1388 struct aio_ctx
*ctx
= opaque
;
1391 gettimeofday(&t2
, NULL
);
1394 printf("readv failed: %s\n", strerror(-ret
));
1399 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1401 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1402 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1403 printf("Pattern verification failed at offset %"
1404 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1414 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1417 /* Finally, report back -- -C gives a parsable format */
1418 t2
= tsub(t2
, ctx
->t1
);
1419 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1420 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1422 qemu_io_free(ctx
->buf
);
1423 qemu_iovec_destroy(&ctx
->qiov
);
1427 static void aio_read_help(void)
1431 " asynchronously reads a range of bytes from the given offset\n"
1434 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1436 " Reads a segment of the currently open file, optionally dumping it to the\n"
1437 " standard output stream (with -v option) for subsequent inspection.\n"
1438 " The read is performed asynchronously and the aio_flush command must be\n"
1439 " used to ensure all outstanding aio requests have been completed.\n"
1440 " -C, -- report statistics in a machine parsable format\n"
1441 " -P, -- use a pattern to verify read data\n"
1442 " -v, -- dump buffer to standard output\n"
1443 " -q, -- quiet mode, do not show I/O statistics\n"
1447 static int aio_read_f(BlockDriverState
*bs
, int argc
, char **argv
);
1449 static const cmdinfo_t aio_read_cmd
= {
1451 .cfunc
= aio_read_f
,
1454 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1455 .oneline
= "asynchronously reads a number of bytes",
1456 .help
= aio_read_help
,
1459 static int aio_read_f(BlockDriverState
*bs
, int argc
, char **argv
)
1462 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1464 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1471 ctx
->pattern
= parse_pattern(optarg
);
1472 if (ctx
->pattern
< 0) {
1485 return qemuio_command_usage(&aio_read_cmd
);
1489 if (optind
> argc
- 2) {
1491 return qemuio_command_usage(&aio_read_cmd
);
1494 ctx
->offset
= cvtnum(argv
[optind
]);
1495 if (ctx
->offset
< 0) {
1496 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1502 if (ctx
->offset
& 0x1ff) {
1503 printf("offset %" PRId64
" is not sector aligned\n",
1509 nr_iov
= argc
- optind
;
1510 ctx
->buf
= create_iovec(bs
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1511 if (ctx
->buf
== NULL
) {
1516 gettimeofday(&ctx
->t1
, NULL
);
1517 bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1518 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1522 static void aio_write_help(void)
1526 " asynchronously writes a range of bytes from the given offset source\n"
1527 " from multiple buffers\n"
1530 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1532 " Writes into a segment of the currently open file, using a buffer\n"
1533 " filled with a set pattern (0xcdcdcdcd).\n"
1534 " The write is performed asynchronously and the aio_flush command must be\n"
1535 " used to ensure all outstanding aio requests have been completed.\n"
1536 " -P, -- use different pattern to fill file\n"
1537 " -C, -- report statistics in a machine parsable format\n"
1538 " -q, -- quiet mode, do not show I/O statistics\n"
1542 static int aio_write_f(BlockDriverState
*bs
, int argc
, char **argv
);
1544 static const cmdinfo_t aio_write_cmd
= {
1545 .name
= "aio_write",
1546 .cfunc
= aio_write_f
,
1549 .args
= "[-Cq] [-P pattern ] off len [len..]",
1550 .oneline
= "asynchronously writes a number of bytes",
1551 .help
= aio_write_help
,
1554 static int aio_write_f(BlockDriverState
*bs
, int argc
, char **argv
)
1558 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1560 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1569 pattern
= parse_pattern(optarg
);
1577 return qemuio_command_usage(&aio_write_cmd
);
1581 if (optind
> argc
- 2) {
1583 return qemuio_command_usage(&aio_write_cmd
);
1586 ctx
->offset
= cvtnum(argv
[optind
]);
1587 if (ctx
->offset
< 0) {
1588 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1594 if (ctx
->offset
& 0x1ff) {
1595 printf("offset %" PRId64
" is not sector aligned\n",
1601 nr_iov
= argc
- optind
;
1602 ctx
->buf
= create_iovec(bs
, &ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1603 if (ctx
->buf
== NULL
) {
1608 gettimeofday(&ctx
->t1
, NULL
);
1609 bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1610 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1614 static int aio_flush_f(BlockDriverState
*bs
, int argc
, char **argv
)
1620 static const cmdinfo_t aio_flush_cmd
= {
1621 .name
= "aio_flush",
1622 .cfunc
= aio_flush_f
,
1623 .oneline
= "completes all outstanding aio requests"
1626 static int flush_f(BlockDriverState
*bs
, int argc
, char **argv
)
1632 static const cmdinfo_t flush_cmd
= {
1636 .oneline
= "flush all in-core file state to disk",
1639 static int truncate_f(BlockDriverState
*bs
, int argc
, char **argv
)
1644 offset
= cvtnum(argv
[1]);
1646 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1650 ret
= bdrv_truncate(bs
, offset
);
1652 printf("truncate: %s\n", strerror(-ret
));
1659 static const cmdinfo_t truncate_cmd
= {
1662 .cfunc
= truncate_f
,
1666 .oneline
= "truncates the current file at the given offset",
1669 static int length_f(BlockDriverState
*bs
, int argc
, char **argv
)
1674 size
= bdrv_getlength(bs
);
1676 printf("getlength: %s\n", strerror(-size
));
1680 cvtstr(size
, s1
, sizeof(s1
));
1686 static const cmdinfo_t length_cmd
= {
1690 .oneline
= "gets the length of the current file",
1694 static int info_f(BlockDriverState
*bs
, int argc
, char **argv
)
1696 BlockDriverInfo bdi
;
1697 ImageInfoSpecific
*spec_info
;
1698 char s1
[64], s2
[64];
1701 if (bs
->drv
&& bs
->drv
->format_name
) {
1702 printf("format name: %s\n", bs
->drv
->format_name
);
1704 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1705 printf("format name: %s\n", bs
->drv
->protocol_name
);
1708 ret
= bdrv_get_info(bs
, &bdi
);
1713 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1714 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1716 printf("cluster size: %s\n", s1
);
1717 printf("vm state offset: %s\n", s2
);
1719 spec_info
= bdrv_get_specific_info(bs
);
1721 printf("Format specific information:\n");
1722 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1723 qapi_free_ImageInfoSpecific(spec_info
);
1731 static const cmdinfo_t info_cmd
= {
1735 .oneline
= "prints information about the current file",
1738 static void discard_help(void)
1742 " discards a range of bytes from the given offset\n"
1745 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1747 " Discards a segment of the currently open file.\n"
1748 " -C, -- report statistics in a machine parsable format\n"
1749 " -q, -- quiet mode, do not show I/O statistics\n"
1753 static int discard_f(BlockDriverState
*bs
, int argc
, char **argv
);
1755 static const cmdinfo_t discard_cmd
= {
1761 .args
= "[-Cq] off len",
1762 .oneline
= "discards a number of bytes at a specified offset",
1763 .help
= discard_help
,
1766 static int discard_f(BlockDriverState
*bs
, int argc
, char **argv
)
1768 struct timeval t1
, t2
;
1769 int Cflag
= 0, qflag
= 0;
1774 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1783 return qemuio_command_usage(&discard_cmd
);
1787 if (optind
!= argc
- 2) {
1788 return qemuio_command_usage(&discard_cmd
);
1791 offset
= cvtnum(argv
[optind
]);
1793 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1798 count
= cvtnum(argv
[optind
]);
1800 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1804 gettimeofday(&t1
, NULL
);
1805 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1806 count
>> BDRV_SECTOR_BITS
);
1807 gettimeofday(&t2
, NULL
);
1810 printf("discard failed: %s\n", strerror(-ret
));
1814 /* Finally, report back -- -C gives a parsable format */
1817 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1824 static int alloc_f(BlockDriverState
*bs
, int argc
, char **argv
)
1826 int64_t offset
, sector_num
;
1827 int nb_sectors
, remaining
;
1832 offset
= cvtnum(argv
[1]);
1834 printf("non-numeric offset argument -- %s\n", argv
[1]);
1836 } else if (offset
& 0x1ff) {
1837 printf("offset %" PRId64
" is not sector aligned\n",
1843 nb_sectors
= cvtnum(argv
[2]);
1844 if (nb_sectors
< 0) {
1845 printf("non-numeric length argument -- %s\n", argv
[2]);
1852 remaining
= nb_sectors
;
1854 sector_num
= offset
>> 9;
1856 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1858 printf("is_allocated failed: %s\n", strerror(-ret
));
1867 nb_sectors
-= remaining
;
1872 cvtstr(offset
, s1
, sizeof(s1
));
1874 printf("%d/%d sectors allocated at offset %s\n",
1875 sum_alloc
, nb_sectors
, s1
);
1879 static const cmdinfo_t alloc_cmd
= {
1885 .args
= "off [sectors]",
1886 .oneline
= "checks if a sector is present in the file",
1890 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1891 int64_t nb_sectors
, int64_t *pnum
)
1893 int num
, num_checked
;
1896 num_checked
= MIN(nb_sectors
, INT_MAX
);
1897 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1905 while (nb_sectors
> 0 && ret
== firstret
) {
1909 num_checked
= MIN(nb_sectors
, INT_MAX
);
1910 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1911 if (ret
== firstret
) {
1921 static int map_f(BlockDriverState
*bs
, int argc
, char **argv
)
1931 nb_sectors
= bs
->total_sectors
;
1934 ret
= map_is_allocated(bs
, offset
, nb_sectors
, &num
);
1936 error_report("Failed to get allocation status: %s", strerror(-ret
));
1940 retstr
= ret
? " allocated" : "not allocated";
1941 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1942 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1943 "at offset %s (%d)\n",
1944 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
1948 } while (offset
< bs
->total_sectors
);
1953 static const cmdinfo_t map_cmd
= {
1959 .oneline
= "prints the allocated areas of a file",
1962 static int break_f(BlockDriverState
*bs
, int argc
, char **argv
)
1966 ret
= bdrv_debug_breakpoint(bs
, argv
[1], argv
[2]);
1968 printf("Could not set breakpoint: %s\n", strerror(-ret
));
1974 static int remove_break_f(BlockDriverState
*bs
, int argc
, char **argv
)
1978 ret
= bdrv_debug_remove_breakpoint(bs
, argv
[1]);
1980 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
1986 static const cmdinfo_t break_cmd
= {
1991 .args
= "event tag",
1992 .oneline
= "sets a breakpoint on event and tags the stopped "
1996 static const cmdinfo_t remove_break_cmd
= {
1997 .name
= "remove_break",
2000 .cfunc
= remove_break_f
,
2002 .oneline
= "remove a breakpoint by tag",
2005 static int resume_f(BlockDriverState
*bs
, int argc
, char **argv
)
2009 ret
= bdrv_debug_resume(bs
, argv
[1]);
2011 printf("Could not resume request: %s\n", strerror(-ret
));
2017 static const cmdinfo_t resume_cmd
= {
2023 .oneline
= "resumes the request tagged as tag",
2026 static int wait_break_f(BlockDriverState
*bs
, int argc
, char **argv
)
2028 while (!bdrv_debug_is_suspended(bs
, argv
[1])) {
2035 static const cmdinfo_t wait_break_cmd
= {
2036 .name
= "wait_break",
2039 .cfunc
= wait_break_f
,
2041 .oneline
= "waits for the suspension of a request",
2044 static int abort_f(BlockDriverState
*bs
, int argc
, char **argv
)
2049 static const cmdinfo_t abort_cmd
= {
2052 .flags
= CMD_NOFILE_OK
,
2053 .oneline
= "simulate a program crash using abort(3)",
2056 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2061 printf("%s ", ct
->name
);
2063 printf("(or %s) ", ct
->altname
);
2068 printf("%s ", ct
->args
);
2070 printf("-- %s\n", ct
->oneline
);
2073 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2075 help_oneline(cmd
, ct
);
2081 static void help_all(void)
2083 const cmdinfo_t
*ct
;
2085 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2086 help_oneline(ct
->name
, ct
);
2088 printf("\nUse 'help commandname' for extended help.\n");
2091 static int help_f(BlockDriverState
*bs
, int argc
, char **argv
)
2093 const cmdinfo_t
*ct
;
2100 ct
= find_command(argv
[1]);
2102 printf("command %s not found\n", argv
[1]);
2106 help_onecmd(argv
[1], ct
);
2110 static const cmdinfo_t help_cmd
= {
2116 .flags
= CMD_FLAG_GLOBAL
,
2117 .args
= "[command]",
2118 .oneline
= "help for one or all commands",
2121 bool qemuio_command(BlockDriverState
*bs
, const char *cmd
)
2124 const cmdinfo_t
*ct
;
2129 input
= g_strdup(cmd
);
2130 v
= breakline(input
, &c
);
2132 ct
= find_command(v
[0]);
2134 done
= command(bs
, ct
, c
, v
);
2136 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2145 static void __attribute((constructor
)) init_qemuio_commands(void)
2147 /* initialize commands */
2148 qemuio_add_command(&help_cmd
);
2149 qemuio_add_command(&read_cmd
);
2150 qemuio_add_command(&readv_cmd
);
2151 qemuio_add_command(&write_cmd
);
2152 qemuio_add_command(&writev_cmd
);
2153 qemuio_add_command(&multiwrite_cmd
);
2154 qemuio_add_command(&aio_read_cmd
);
2155 qemuio_add_command(&aio_write_cmd
);
2156 qemuio_add_command(&aio_flush_cmd
);
2157 qemuio_add_command(&flush_cmd
);
2158 qemuio_add_command(&truncate_cmd
);
2159 qemuio_add_command(&length_cmd
);
2160 qemuio_add_command(&info_cmd
);
2161 qemuio_add_command(&discard_cmd
);
2162 qemuio_add_command(&alloc_cmd
);
2163 qemuio_add_command(&map_cmd
);
2164 qemuio_add_command(&break_cmd
);
2165 qemuio_add_command(&remove_break_cmd
);
2166 qemuio_add_command(&resume_cmd
);
2167 qemuio_add_command(&wait_break_cmd
);
2168 qemuio_add_command(&abort_cmd
);