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 "qemu/main-loop.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(BlockDriverState
*bs
, const cmdinfo_t
*ct
, int argc
,
58 if (!init_check_command(bs
, ct
)) {
62 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
63 if (ct
->argmax
== -1) {
65 "bad argument count %d to %s, expected at least %d arguments\n",
66 argc
-1, cmd
, ct
->argmin
);
67 } else if (ct
->argmin
== ct
->argmax
) {
69 "bad argument count %d to %s, expected %d arguments\n",
70 argc
-1, cmd
, ct
->argmin
);
73 "bad argument count %d to %s, expected between %d and %d arguments\n",
74 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
79 return ct
->cfunc(bs
, argc
, argv
);
82 static const cmdinfo_t
*find_command(const char *cmd
)
86 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
87 if (strcmp(ct
->name
, cmd
) == 0 ||
88 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
90 return (const cmdinfo_t
*)ct
;
96 static char **breakline(char *input
, int *count
)
100 char **rval
= g_malloc0(sizeof(char *));
103 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
108 tmp
= g_realloc(rval
, sizeof(*rval
) * (c
+ 1));
124 static int64_t cvtnum(const char *s
)
127 return strtosz_suffix(s
, &end
, STRTOSZ_DEFSUFFIX_B
);
130 #define EXABYTES(x) ((long long)(x) << 60)
131 #define PETABYTES(x) ((long long)(x) << 50)
132 #define TERABYTES(x) ((long long)(x) << 40)
133 #define GIGABYTES(x) ((long long)(x) << 30)
134 #define MEGABYTES(x) ((long long)(x) << 20)
135 #define KILOBYTES(x) ((long long)(x) << 10)
137 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
138 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
139 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
140 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
141 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
142 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
144 static void cvtstr(double value
, char *str
, size_t size
)
149 if (value
>= EXABYTES(1)) {
151 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
152 } else if (value
>= PETABYTES(1)) {
154 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
155 } else if (value
>= TERABYTES(1)) {
157 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
158 } else if (value
>= GIGABYTES(1)) {
160 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
161 } else if (value
>= MEGABYTES(1)) {
163 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
164 } else if (value
>= KILOBYTES(1)) {
166 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
169 snprintf(str
, size
- 6, "%f", value
);
172 trim
= strstr(str
, ".000");
174 strcpy(trim
, suffix
);
182 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
184 t1
.tv_usec
-= t2
.tv_usec
;
185 if (t1
.tv_usec
< 0) {
186 t1
.tv_usec
+= 1000000;
189 t1
.tv_sec
-= t2
.tv_sec
;
193 static double tdiv(double value
, struct timeval tv
)
195 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
198 #define HOURS(sec) ((sec) / (60 * 60))
199 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
200 #define SECONDS(sec) ((sec) % 60)
204 TERSE_FIXED_TIME
= 0x1,
205 VERBOSE_FIXED_TIME
= 0x2,
208 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
210 double usec
= (double)tv
->tv_usec
/ 1000000.0;
212 if (format
& TERSE_FIXED_TIME
) {
213 if (!HOURS(tv
->tv_sec
)) {
214 snprintf(ts
, size
, "%u:%02u.%02u",
215 (unsigned int) MINUTES(tv
->tv_sec
),
216 (unsigned int) SECONDS(tv
->tv_sec
),
217 (unsigned int) (usec
* 100));
220 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
223 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
224 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
225 (unsigned int) HOURS(tv
->tv_sec
),
226 (unsigned int) MINUTES(tv
->tv_sec
),
227 (unsigned int) SECONDS(tv
->tv_sec
),
228 (unsigned int) (usec
* 100));
230 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
235 * Parse the pattern argument to various sub-commands.
237 * Because the pattern is used as an argument to memset it must evaluate
238 * to an unsigned integer that fits into a single byte.
240 static int parse_pattern(const char *arg
)
245 pattern
= strtol(arg
, &endptr
, 0);
246 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
247 printf("%s is not a valid pattern byte\n", arg
);
255 * Memory allocation helpers.
257 * Make sure memory is aligned by default, or purposefully misaligned if
258 * that is specified on the command line.
261 #define MISALIGN_OFFSET 16
262 static void *qemu_io_alloc(BlockDriverState
*bs
, size_t len
, int pattern
)
266 if (qemuio_misalign
) {
267 len
+= MISALIGN_OFFSET
;
269 buf
= qemu_blockalign(bs
, len
);
270 memset(buf
, pattern
, len
);
271 if (qemuio_misalign
) {
272 buf
+= MISALIGN_OFFSET
;
277 static void qemu_io_free(void *p
)
279 if (qemuio_misalign
) {
280 p
-= MISALIGN_OFFSET
;
285 static void dump_buffer(const void *buffer
, int64_t offset
, int len
)
290 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
291 const uint8_t *s
= p
;
293 printf("%08" PRIx64
": ", offset
+ i
);
294 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
298 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
309 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
310 int count
, int total
, int cnt
, int Cflag
)
312 char s1
[64], s2
[64], ts
[64];
314 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
316 cvtstr((double)total
, s1
, sizeof(s1
));
317 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
318 printf("%s %d/%d bytes at offset %" PRId64
"\n",
319 op
, total
, count
, offset
);
320 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
321 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
322 } else {/* bytes,ops,time,bytes/sec,ops/sec */
323 printf("%d,%d,%s,%.3f,%.3f\n",
325 tdiv((double)total
, *t
),
326 tdiv((double)cnt
, *t
));
331 * Parse multiple length statements for vectored I/O, and construct an I/O
332 * vector matching it.
335 create_iovec(BlockDriverState
*bs
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
338 size_t *sizes
= g_new0(size_t, nr_iov
);
344 for (i
= 0; i
< nr_iov
; i
++) {
350 printf("non-numeric length argument -- %s\n", arg
);
354 /* should be SIZE_T_MAX, but that doesn't exist */
356 printf("too large length argument -- %s\n", arg
);
361 printf("length argument %" PRId64
362 " is not sector aligned\n", len
);
370 qemu_iovec_init(qiov
, nr_iov
);
372 buf
= p
= qemu_io_alloc(bs
, count
, pattern
);
374 for (i
= 0; i
< nr_iov
; i
++) {
375 qemu_iovec_add(qiov
, p
, sizes
[i
]);
384 static int do_read(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
389 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
397 static int do_write(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
402 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
410 static int do_pread(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
413 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
420 static int do_pwrite(BlockDriverState
*bs
, char *buf
, int64_t offset
, int count
,
423 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
431 BlockDriverState
*bs
;
439 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
441 CoWriteZeroes
*data
= opaque
;
443 data
->ret
= bdrv_co_write_zeroes(data
->bs
, data
->offset
/ BDRV_SECTOR_SIZE
,
444 data
->count
/ BDRV_SECTOR_SIZE
);
447 *data
->total
= data
->ret
;
451 *data
->total
= data
->count
;
454 static int do_co_write_zeroes(BlockDriverState
*bs
, int64_t offset
, int count
,
458 CoWriteZeroes data
= {
466 co
= qemu_coroutine_create(co_write_zeroes_entry
);
467 qemu_coroutine_enter(co
, &data
);
478 static int do_write_compressed(BlockDriverState
*bs
, char *buf
, int64_t offset
,
479 int count
, int *total
)
483 ret
= bdrv_write_compressed(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
491 static int do_load_vmstate(BlockDriverState
*bs
, char *buf
, int64_t offset
,
492 int count
, int *total
)
494 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
501 static int do_save_vmstate(BlockDriverState
*bs
, char *buf
, int64_t offset
,
502 int count
, int *total
)
504 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
511 #define NOT_DONE 0x7fffffff
512 static void aio_rw_done(void *opaque
, int ret
)
514 *(int *)opaque
= ret
;
517 static int do_aio_readv(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
518 int64_t offset
, int *total
)
520 int async_ret
= NOT_DONE
;
522 bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
523 aio_rw_done
, &async_ret
);
524 while (async_ret
== NOT_DONE
) {
525 main_loop_wait(false);
529 return async_ret
< 0 ? async_ret
: 1;
532 static int do_aio_writev(BlockDriverState
*bs
, QEMUIOVector
*qiov
,
533 int64_t offset
, int *total
)
535 int async_ret
= NOT_DONE
;
537 bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
538 aio_rw_done
, &async_ret
);
539 while (async_ret
== NOT_DONE
) {
540 main_loop_wait(false);
544 return async_ret
< 0 ? async_ret
: 1;
547 struct multiwrite_async_ret
{
552 static void multiwrite_cb(void *opaque
, int ret
)
554 struct multiwrite_async_ret
*async_ret
= opaque
;
556 async_ret
->num_done
++;
558 async_ret
->error
= ret
;
562 static int do_aio_multiwrite(BlockDriverState
*bs
, BlockRequest
* reqs
,
563 int num_reqs
, int *total
)
566 struct multiwrite_async_ret async_ret
= {
572 for (i
= 0; i
< num_reqs
; i
++) {
573 reqs
[i
].cb
= multiwrite_cb
;
574 reqs
[i
].opaque
= &async_ret
;
575 *total
+= reqs
[i
].qiov
->size
;
578 ret
= bdrv_aio_multiwrite(bs
, reqs
, num_reqs
);
583 while (async_ret
.num_done
< num_reqs
) {
584 main_loop_wait(false);
587 return async_ret
.error
< 0 ? async_ret
.error
: 1;
590 static void read_help(void)
594 " reads a range of bytes from the given offset\n"
597 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
599 " Reads a segment of the currently open file, optionally dumping it to the\n"
600 " standard output stream (with -v option) for subsequent inspection.\n"
601 " -b, -- read from the VM state rather than the virtual disk\n"
602 " -C, -- report statistics in a machine parsable format\n"
603 " -l, -- length for pattern verification (only with -P)\n"
604 " -p, -- use bdrv_pread to read the file\n"
605 " -P, -- use a pattern to verify read data\n"
606 " -q, -- quiet mode, do not show I/O statistics\n"
607 " -s, -- start offset for pattern verification (only with -P)\n"
608 " -v, -- dump buffer to standard output\n"
612 static int read_f(BlockDriverState
*bs
, int argc
, char **argv
);
614 static const cmdinfo_t read_cmd
= {
620 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
621 .oneline
= "reads a number of bytes at a specified offset",
625 static int read_f(BlockDriverState
*bs
, int argc
, char **argv
)
627 struct timeval t1
, t2
;
628 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
629 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
634 /* Some compilers get confused and warn if this is not initialized. */
636 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
638 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
648 pattern_count
= cvtnum(optarg
);
649 if (pattern_count
< 0) {
650 printf("non-numeric length argument -- %s\n", optarg
);
659 pattern
= parse_pattern(optarg
);
669 pattern_offset
= cvtnum(optarg
);
670 if (pattern_offset
< 0) {
671 printf("non-numeric length argument -- %s\n", optarg
);
679 return qemuio_command_usage(&read_cmd
);
683 if (optind
!= argc
- 2) {
684 return qemuio_command_usage(&read_cmd
);
687 if (bflag
&& pflag
) {
688 printf("-b and -p cannot be specified at the same time\n");
692 offset
= cvtnum(argv
[optind
]);
694 printf("non-numeric length argument -- %s\n", argv
[optind
]);
699 count
= cvtnum(argv
[optind
]);
701 printf("non-numeric length argument -- %s\n", argv
[optind
]);
705 if (!Pflag
&& (lflag
|| sflag
)) {
706 return qemuio_command_usage(&read_cmd
);
710 pattern_count
= count
- pattern_offset
;
713 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
714 printf("pattern verification range exceeds end of read data\n");
719 if (offset
& 0x1ff) {
720 printf("offset %" PRId64
" is not sector aligned\n",
725 printf("count %d is not sector aligned\n",
731 buf
= qemu_io_alloc(bs
, count
, 0xab);
733 gettimeofday(&t1
, NULL
);
735 cnt
= do_pread(bs
, buf
, offset
, count
, &total
);
737 cnt
= do_load_vmstate(bs
, buf
, offset
, count
, &total
);
739 cnt
= do_read(bs
, buf
, offset
, count
, &total
);
741 gettimeofday(&t2
, NULL
);
744 printf("read failed: %s\n", strerror(-cnt
));
749 void *cmp_buf
= g_malloc(pattern_count
);
750 memset(cmp_buf
, pattern
, pattern_count
);
751 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
752 printf("Pattern verification failed at offset %"
753 PRId64
", %d bytes\n",
754 offset
+ pattern_offset
, pattern_count
);
764 dump_buffer(buf
, offset
, count
);
767 /* Finally, report back -- -C gives a parsable format */
769 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
777 static void readv_help(void)
781 " reads a range of bytes from the given offset into multiple buffers\n"
784 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
786 " Reads a segment of the currently open file, optionally dumping it to the\n"
787 " standard output stream (with -v option) for subsequent inspection.\n"
788 " Uses multiple iovec buffers if more than one byte range is specified.\n"
789 " -C, -- report statistics in a machine parsable format\n"
790 " -P, -- use a pattern to verify read data\n"
791 " -v, -- dump buffer to standard output\n"
792 " -q, -- quiet mode, do not show I/O statistics\n"
796 static int readv_f(BlockDriverState
*bs
, int argc
, char **argv
);
798 static const cmdinfo_t readv_cmd
= {
803 .args
= "[-Cqv] [-P pattern ] off len [len..]",
804 .oneline
= "reads a number of bytes at a specified offset",
808 static int readv_f(BlockDriverState
*bs
, int argc
, char **argv
)
810 struct timeval t1
, t2
;
811 int Cflag
= 0, qflag
= 0, vflag
= 0;
815 /* Some compilers get confused and warn if this is not initialized. */
822 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
829 pattern
= parse_pattern(optarg
);
841 return qemuio_command_usage(&readv_cmd
);
845 if (optind
> argc
- 2) {
846 return qemuio_command_usage(&readv_cmd
);
850 offset
= cvtnum(argv
[optind
]);
852 printf("non-numeric length argument -- %s\n", argv
[optind
]);
857 if (offset
& 0x1ff) {
858 printf("offset %" PRId64
" is not sector aligned\n",
863 nr_iov
= argc
- optind
;
864 buf
= create_iovec(bs
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
869 gettimeofday(&t1
, NULL
);
870 cnt
= do_aio_readv(bs
, &qiov
, offset
, &total
);
871 gettimeofday(&t2
, NULL
);
874 printf("readv failed: %s\n", strerror(-cnt
));
879 void *cmp_buf
= g_malloc(qiov
.size
);
880 memset(cmp_buf
, pattern
, qiov
.size
);
881 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
882 printf("Pattern verification failed at offset %"
883 PRId64
", %zd bytes\n", offset
, qiov
.size
);
893 dump_buffer(buf
, offset
, qiov
.size
);
896 /* Finally, report back -- -C gives a parsable format */
898 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
901 qemu_iovec_destroy(&qiov
);
906 static void write_help(void)
910 " writes a range of bytes from the given offset\n"
913 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
915 " Writes into a segment of the currently open file, using a buffer\n"
916 " filled with a set pattern (0xcdcdcdcd).\n"
917 " -b, -- write to the VM state rather than the virtual disk\n"
918 " -c, -- write compressed data with bdrv_write_compressed\n"
919 " -p, -- use bdrv_pwrite to write the file\n"
920 " -P, -- use different pattern to fill file\n"
921 " -C, -- report statistics in a machine parsable format\n"
922 " -q, -- quiet mode, do not show I/O statistics\n"
923 " -z, -- write zeroes using bdrv_co_write_zeroes\n"
927 static int write_f(BlockDriverState
*bs
, int argc
, char **argv
);
929 static const cmdinfo_t write_cmd
= {
935 .args
= "[-bcCpqz] [-P pattern ] off len",
936 .oneline
= "writes a number of bytes at a specified offset",
940 static int write_f(BlockDriverState
*bs
, int argc
, char **argv
)
942 struct timeval t1
, t2
;
943 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
949 /* Some compilers get confused and warn if this is not initialized. */
953 while ((c
= getopt(argc
, argv
, "bcCpP:qz")) != EOF
) {
969 pattern
= parse_pattern(optarg
);
981 return qemuio_command_usage(&write_cmd
);
985 if (optind
!= argc
- 2) {
986 return qemuio_command_usage(&write_cmd
);
989 if (bflag
+ pflag
+ zflag
> 1) {
990 printf("-b, -p, or -z cannot be specified at the same time\n");
994 if (zflag
&& Pflag
) {
995 printf("-z and -P cannot be specified at the same time\n");
999 offset
= cvtnum(argv
[optind
]);
1001 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1006 count
= cvtnum(argv
[optind
]);
1008 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1013 if (offset
& 0x1ff) {
1014 printf("offset %" PRId64
" is not sector aligned\n",
1019 if (count
& 0x1ff) {
1020 printf("count %d is not sector aligned\n",
1027 buf
= qemu_io_alloc(bs
, count
, pattern
);
1030 gettimeofday(&t1
, NULL
);
1032 cnt
= do_pwrite(bs
, buf
, offset
, count
, &total
);
1034 cnt
= do_save_vmstate(bs
, buf
, offset
, count
, &total
);
1036 cnt
= do_co_write_zeroes(bs
, offset
, count
, &total
);
1038 cnt
= do_write_compressed(bs
, buf
, offset
, count
, &total
);
1040 cnt
= do_write(bs
, buf
, offset
, count
, &total
);
1042 gettimeofday(&t2
, NULL
);
1045 printf("write failed: %s\n", strerror(-cnt
));
1053 /* Finally, report back -- -C gives a parsable format */
1055 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1070 " writes a range of bytes from the given offset source from multiple buffers\n"
1073 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1075 " Writes into a segment of the currently open file, using a buffer\n"
1076 " filled with a set pattern (0xcdcdcdcd).\n"
1077 " -P, -- use different pattern to fill file\n"
1078 " -C, -- report statistics in a machine parsable format\n"
1079 " -q, -- quiet mode, do not show I/O statistics\n"
1083 static int writev_f(BlockDriverState
*bs
, int argc
, char **argv
);
1085 static const cmdinfo_t writev_cmd
= {
1090 .args
= "[-Cq] [-P pattern ] off len [len..]",
1091 .oneline
= "writes a number of bytes at a specified offset",
1092 .help
= writev_help
,
1095 static int writev_f(BlockDriverState
*bs
, int argc
, char **argv
)
1097 struct timeval t1
, t2
;
1098 int Cflag
= 0, qflag
= 0;
1102 /* Some compilers get confused and warn if this is not initialized. */
1108 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1117 pattern
= parse_pattern(optarg
);
1123 return qemuio_command_usage(&writev_cmd
);
1127 if (optind
> argc
- 2) {
1128 return qemuio_command_usage(&writev_cmd
);
1131 offset
= cvtnum(argv
[optind
]);
1133 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1138 if (offset
& 0x1ff) {
1139 printf("offset %" PRId64
" is not sector aligned\n",
1144 nr_iov
= argc
- optind
;
1145 buf
= create_iovec(bs
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1150 gettimeofday(&t1
, NULL
);
1151 cnt
= do_aio_writev(bs
, &qiov
, offset
, &total
);
1152 gettimeofday(&t2
, NULL
);
1155 printf("writev failed: %s\n", strerror(-cnt
));
1163 /* Finally, report back -- -C gives a parsable format */
1165 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1167 qemu_iovec_destroy(&qiov
);
1172 static void multiwrite_help(void)
1176 " writes a range of bytes from the given offset source from multiple buffers,\n"
1177 " in a batch of requests that may be merged by qemu\n"
1180 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1181 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1183 " Writes into a segment of the currently open file, using a buffer\n"
1184 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1185 " by one for each request contained in the multiwrite command.\n"
1186 " -P, -- use different pattern to fill file\n"
1187 " -C, -- report statistics in a machine parsable format\n"
1188 " -q, -- quiet mode, do not show I/O statistics\n"
1192 static int multiwrite_f(BlockDriverState
*bs
, int argc
, char **argv
);
1194 static const cmdinfo_t multiwrite_cmd
= {
1195 .name
= "multiwrite",
1196 .cfunc
= multiwrite_f
,
1199 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1200 .oneline
= "issues multiple write requests at once",
1201 .help
= multiwrite_help
,
1204 static int multiwrite_f(BlockDriverState
*bs
, int argc
, char **argv
)
1206 struct timeval t1
, t2
;
1207 int Cflag
= 0, qflag
= 0;
1210 int64_t offset
, first_offset
= 0;
1211 /* Some compilers get confused and warn if this is not initialized. */
1216 QEMUIOVector
*qiovs
;
1220 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1229 pattern
= parse_pattern(optarg
);
1235 return qemuio_command_usage(&writev_cmd
);
1239 if (optind
> argc
- 2) {
1240 return qemuio_command_usage(&writev_cmd
);
1244 for (i
= optind
; i
< argc
; i
++) {
1245 if (!strcmp(argv
[i
], ";")) {
1250 reqs
= g_malloc0(nr_reqs
* sizeof(*reqs
));
1251 buf
= g_malloc0(nr_reqs
* sizeof(*buf
));
1252 qiovs
= g_malloc(nr_reqs
* sizeof(*qiovs
));
1254 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1257 /* Read the offset of the request */
1258 offset
= cvtnum(argv
[optind
]);
1260 printf("non-numeric offset argument -- %s\n", argv
[optind
]);
1265 if (offset
& 0x1ff) {
1266 printf("offset %lld is not sector aligned\n",
1272 first_offset
= offset
;
1275 /* Read lengths for qiov entries */
1276 for (j
= optind
; j
< argc
; j
++) {
1277 if (!strcmp(argv
[j
], ";")) {
1282 nr_iov
= j
- optind
;
1285 buf
[i
] = create_iovec(bs
, &qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1286 if (buf
[i
] == NULL
) {
1290 reqs
[i
].qiov
= &qiovs
[i
];
1291 reqs
[i
].sector
= offset
>> 9;
1292 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1299 /* If there were empty requests at the end, ignore them */
1302 gettimeofday(&t1
, NULL
);
1303 cnt
= do_aio_multiwrite(bs
, reqs
, nr_reqs
, &total
);
1304 gettimeofday(&t2
, NULL
);
1307 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1315 /* Finally, report back -- -C gives a parsable format */
1317 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1319 for (i
= 0; i
< nr_reqs
; i
++) {
1320 qemu_io_free(buf
[i
]);
1321 if (reqs
[i
].qiov
!= NULL
) {
1322 qemu_iovec_destroy(&qiovs
[i
]);
1343 static void aio_write_done(void *opaque
, int ret
)
1345 struct aio_ctx
*ctx
= opaque
;
1348 gettimeofday(&t2
, NULL
);
1352 printf("aio_write failed: %s\n", strerror(-ret
));
1360 /* Finally, report back -- -C gives a parsable format */
1361 t2
= tsub(t2
, ctx
->t1
);
1362 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1363 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1365 qemu_io_free(ctx
->buf
);
1366 qemu_iovec_destroy(&ctx
->qiov
);
1370 static void aio_read_done(void *opaque
, int ret
)
1372 struct aio_ctx
*ctx
= opaque
;
1375 gettimeofday(&t2
, NULL
);
1378 printf("readv failed: %s\n", strerror(-ret
));
1383 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1385 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1386 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1387 printf("Pattern verification failed at offset %"
1388 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1398 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1401 /* Finally, report back -- -C gives a parsable format */
1402 t2
= tsub(t2
, ctx
->t1
);
1403 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1404 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1406 qemu_io_free(ctx
->buf
);
1407 qemu_iovec_destroy(&ctx
->qiov
);
1411 static void aio_read_help(void)
1415 " asynchronously reads a range of bytes from the given offset\n"
1418 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1420 " Reads a segment of the currently open file, optionally dumping it to the\n"
1421 " standard output stream (with -v option) for subsequent inspection.\n"
1422 " The read is performed asynchronously and the aio_flush command must be\n"
1423 " used to ensure all outstanding aio requests have been completed.\n"
1424 " -C, -- report statistics in a machine parsable format\n"
1425 " -P, -- use a pattern to verify read data\n"
1426 " -v, -- dump buffer to standard output\n"
1427 " -q, -- quiet mode, do not show I/O statistics\n"
1431 static int aio_read_f(BlockDriverState
*bs
, int argc
, char **argv
);
1433 static const cmdinfo_t aio_read_cmd
= {
1435 .cfunc
= aio_read_f
,
1438 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1439 .oneline
= "asynchronously reads a number of bytes",
1440 .help
= aio_read_help
,
1443 static int aio_read_f(BlockDriverState
*bs
, int argc
, char **argv
)
1446 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1448 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
1455 ctx
->pattern
= parse_pattern(optarg
);
1456 if (ctx
->pattern
< 0) {
1469 return qemuio_command_usage(&aio_read_cmd
);
1473 if (optind
> argc
- 2) {
1475 return qemuio_command_usage(&aio_read_cmd
);
1478 ctx
->offset
= cvtnum(argv
[optind
]);
1479 if (ctx
->offset
< 0) {
1480 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1486 if (ctx
->offset
& 0x1ff) {
1487 printf("offset %" PRId64
" is not sector aligned\n",
1493 nr_iov
= argc
- optind
;
1494 ctx
->buf
= create_iovec(bs
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1495 if (ctx
->buf
== NULL
) {
1500 gettimeofday(&ctx
->t1
, NULL
);
1501 bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1502 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
1506 static void aio_write_help(void)
1510 " asynchronously writes a range of bytes from the given offset source\n"
1511 " from multiple buffers\n"
1514 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1516 " Writes into a segment of the currently open file, using a buffer\n"
1517 " filled with a set pattern (0xcdcdcdcd).\n"
1518 " The write is performed asynchronously and the aio_flush command must be\n"
1519 " used to ensure all outstanding aio requests have been completed.\n"
1520 " -P, -- use different pattern to fill file\n"
1521 " -C, -- report statistics in a machine parsable format\n"
1522 " -q, -- quiet mode, do not show I/O statistics\n"
1526 static int aio_write_f(BlockDriverState
*bs
, int argc
, char **argv
);
1528 static const cmdinfo_t aio_write_cmd
= {
1529 .name
= "aio_write",
1530 .cfunc
= aio_write_f
,
1533 .args
= "[-Cq] [-P pattern ] off len [len..]",
1534 .oneline
= "asynchronously writes a number of bytes",
1535 .help
= aio_write_help
,
1538 static int aio_write_f(BlockDriverState
*bs
, int argc
, char **argv
)
1542 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1544 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1553 pattern
= parse_pattern(optarg
);
1561 return qemuio_command_usage(&aio_write_cmd
);
1565 if (optind
> argc
- 2) {
1567 return qemuio_command_usage(&aio_write_cmd
);
1570 ctx
->offset
= cvtnum(argv
[optind
]);
1571 if (ctx
->offset
< 0) {
1572 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1578 if (ctx
->offset
& 0x1ff) {
1579 printf("offset %" PRId64
" is not sector aligned\n",
1585 nr_iov
= argc
- optind
;
1586 ctx
->buf
= create_iovec(bs
, &ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1587 if (ctx
->buf
== NULL
) {
1592 gettimeofday(&ctx
->t1
, NULL
);
1593 bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1594 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1598 static int aio_flush_f(BlockDriverState
*bs
, int argc
, char **argv
)
1604 static const cmdinfo_t aio_flush_cmd
= {
1605 .name
= "aio_flush",
1606 .cfunc
= aio_flush_f
,
1607 .oneline
= "completes all outstanding aio requests"
1610 static int flush_f(BlockDriverState
*bs
, int argc
, char **argv
)
1616 static const cmdinfo_t flush_cmd
= {
1620 .oneline
= "flush all in-core file state to disk",
1623 static int truncate_f(BlockDriverState
*bs
, int argc
, char **argv
)
1628 offset
= cvtnum(argv
[1]);
1630 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1634 ret
= bdrv_truncate(bs
, offset
);
1636 printf("truncate: %s\n", strerror(-ret
));
1643 static const cmdinfo_t truncate_cmd
= {
1646 .cfunc
= truncate_f
,
1650 .oneline
= "truncates the current file at the given offset",
1653 static int length_f(BlockDriverState
*bs
, int argc
, char **argv
)
1658 size
= bdrv_getlength(bs
);
1660 printf("getlength: %s\n", strerror(-size
));
1664 cvtstr(size
, s1
, sizeof(s1
));
1670 static const cmdinfo_t length_cmd
= {
1674 .oneline
= "gets the length of the current file",
1678 static int info_f(BlockDriverState
*bs
, int argc
, char **argv
)
1680 BlockDriverInfo bdi
;
1681 char s1
[64], s2
[64];
1684 if (bs
->drv
&& bs
->drv
->format_name
) {
1685 printf("format name: %s\n", bs
->drv
->format_name
);
1687 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1688 printf("format name: %s\n", bs
->drv
->protocol_name
);
1691 ret
= bdrv_get_info(bs
, &bdi
);
1696 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1697 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1699 printf("cluster size: %s\n", s1
);
1700 printf("vm state offset: %s\n", s2
);
1707 static const cmdinfo_t info_cmd
= {
1711 .oneline
= "prints information about the current file",
1714 static void discard_help(void)
1718 " discards a range of bytes from the given offset\n"
1721 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1723 " Discards a segment of the currently open file.\n"
1724 " -C, -- report statistics in a machine parsable format\n"
1725 " -q, -- quiet mode, do not show I/O statistics\n"
1729 static int discard_f(BlockDriverState
*bs
, int argc
, char **argv
);
1731 static const cmdinfo_t discard_cmd
= {
1737 .args
= "[-Cq] off len",
1738 .oneline
= "discards a number of bytes at a specified offset",
1739 .help
= discard_help
,
1742 static int discard_f(BlockDriverState
*bs
, int argc
, char **argv
)
1744 struct timeval t1
, t2
;
1745 int Cflag
= 0, qflag
= 0;
1750 while ((c
= getopt(argc
, argv
, "Cq")) != EOF
) {
1759 return qemuio_command_usage(&discard_cmd
);
1763 if (optind
!= argc
- 2) {
1764 return qemuio_command_usage(&discard_cmd
);
1767 offset
= cvtnum(argv
[optind
]);
1769 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1774 count
= cvtnum(argv
[optind
]);
1776 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1780 gettimeofday(&t1
, NULL
);
1781 ret
= bdrv_discard(bs
, offset
>> BDRV_SECTOR_BITS
,
1782 count
>> BDRV_SECTOR_BITS
);
1783 gettimeofday(&t2
, NULL
);
1786 printf("discard failed: %s\n", strerror(-ret
));
1790 /* Finally, report back -- -C gives a parsable format */
1793 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1800 static int alloc_f(BlockDriverState
*bs
, int argc
, char **argv
)
1802 int64_t offset
, sector_num
;
1803 int nb_sectors
, remaining
;
1808 offset
= cvtnum(argv
[1]);
1810 printf("non-numeric offset argument -- %s\n", argv
[1]);
1812 } else if (offset
& 0x1ff) {
1813 printf("offset %" PRId64
" is not sector aligned\n",
1819 nb_sectors
= cvtnum(argv
[2]);
1820 if (nb_sectors
< 0) {
1821 printf("non-numeric length argument -- %s\n", argv
[2]);
1828 remaining
= nb_sectors
;
1830 sector_num
= offset
>> 9;
1832 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1839 nb_sectors
-= remaining
;
1844 cvtstr(offset
, s1
, sizeof(s1
));
1846 printf("%d/%d sectors allocated at offset %s\n",
1847 sum_alloc
, nb_sectors
, s1
);
1851 static const cmdinfo_t alloc_cmd
= {
1857 .args
= "off [sectors]",
1858 .oneline
= "checks if a sector is present in the file",
1862 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1863 int64_t nb_sectors
, int64_t *pnum
)
1865 int num
, num_checked
;
1868 num_checked
= MIN(nb_sectors
, INT_MAX
);
1869 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1877 while (nb_sectors
> 0 && ret
== firstret
) {
1881 num_checked
= MIN(nb_sectors
, INT_MAX
);
1882 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1883 if (ret
== firstret
) {
1893 static int map_f(BlockDriverState
*bs
, int argc
, char **argv
)
1903 nb_sectors
= bs
->total_sectors
;
1906 ret
= map_is_allocated(bs
, offset
, nb_sectors
, &num
);
1908 error_report("Failed to get allocation status: %s", strerror(-ret
));
1912 retstr
= ret
? " allocated" : "not allocated";
1913 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1914 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1915 "at offset %s (%d)\n",
1916 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
1920 } while (offset
< bs
->total_sectors
);
1925 static const cmdinfo_t map_cmd
= {
1931 .oneline
= "prints the allocated areas of a file",
1934 static int break_f(BlockDriverState
*bs
, int argc
, char **argv
)
1938 ret
= bdrv_debug_breakpoint(bs
, argv
[1], argv
[2]);
1940 printf("Could not set breakpoint: %s\n", strerror(-ret
));
1946 static const cmdinfo_t break_cmd
= {
1951 .args
= "event tag",
1952 .oneline
= "sets a breakpoint on event and tags the stopped "
1956 static int resume_f(BlockDriverState
*bs
, int argc
, char **argv
)
1960 ret
= bdrv_debug_resume(bs
, argv
[1]);
1962 printf("Could not resume request: %s\n", strerror(-ret
));
1968 static const cmdinfo_t resume_cmd
= {
1974 .oneline
= "resumes the request tagged as tag",
1977 static int wait_break_f(BlockDriverState
*bs
, int argc
, char **argv
)
1979 while (!bdrv_debug_is_suspended(bs
, argv
[1])) {
1986 static const cmdinfo_t wait_break_cmd
= {
1987 .name
= "wait_break",
1990 .cfunc
= wait_break_f
,
1992 .oneline
= "waits for the suspension of a request",
1995 static int abort_f(BlockDriverState
*bs
, int argc
, char **argv
)
2000 static const cmdinfo_t abort_cmd
= {
2003 .flags
= CMD_NOFILE_OK
,
2004 .oneline
= "simulate a program crash using abort(3)",
2007 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2012 printf("%s ", ct
->name
);
2014 printf("(or %s) ", ct
->altname
);
2019 printf("%s ", ct
->args
);
2021 printf("-- %s\n", ct
->oneline
);
2024 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2026 help_oneline(cmd
, ct
);
2032 static void help_all(void)
2034 const cmdinfo_t
*ct
;
2036 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2037 help_oneline(ct
->name
, ct
);
2039 printf("\nUse 'help commandname' for extended help.\n");
2042 static int help_f(BlockDriverState
*bs
, int argc
, char **argv
)
2044 const cmdinfo_t
*ct
;
2051 ct
= find_command(argv
[1]);
2053 printf("command %s not found\n", argv
[1]);
2057 help_onecmd(argv
[1], ct
);
2061 static const cmdinfo_t help_cmd
= {
2067 .flags
= CMD_FLAG_GLOBAL
,
2068 .args
= "[command]",
2069 .oneline
= "help for one or all commands",
2072 bool qemuio_command(BlockDriverState
*bs
, const char *cmd
)
2075 const cmdinfo_t
*ct
;
2080 input
= g_strdup(cmd
);
2081 v
= breakline(input
, &c
);
2083 ct
= find_command(v
[0]);
2085 done
= command(bs
, ct
, c
, v
);
2087 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2096 static void __attribute((constructor
)) init_qemuio_commands(void)
2098 /* initialize commands */
2099 qemuio_add_command(&help_cmd
);
2100 qemuio_add_command(&read_cmd
);
2101 qemuio_add_command(&readv_cmd
);
2102 qemuio_add_command(&write_cmd
);
2103 qemuio_add_command(&writev_cmd
);
2104 qemuio_add_command(&multiwrite_cmd
);
2105 qemuio_add_command(&aio_read_cmd
);
2106 qemuio_add_command(&aio_write_cmd
);
2107 qemuio_add_command(&aio_flush_cmd
);
2108 qemuio_add_command(&flush_cmd
);
2109 qemuio_add_command(&truncate_cmd
);
2110 qemuio_add_command(&length_cmd
);
2111 qemuio_add_command(&info_cmd
);
2112 qemuio_add_command(&discard_cmd
);
2113 qemuio_add_command(&alloc_cmd
);
2114 qemuio_add_command(&map_cmd
);
2115 qemuio_add_command(&break_cmd
);
2116 qemuio_add_command(&resume_cmd
);
2117 qemuio_add_command(&wait_break_cmd
);
2118 qemuio_add_command(&abort_cmd
);