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/osdep.h"
12 #include "qapi/error.h"
14 #include "sysemu/block-backend.h"
15 #include "block/block.h"
16 #include "block/block_int.h" /* for info_f() */
17 #include "block/qapi.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/timer.h"
21 #include "sysemu/block-backend.h"
22 #include "qemu/cutils.h"
24 #define CMD_NOFILE_OK 0x01
28 static cmdinfo_t
*cmdtab
;
31 static int compare_cmdname(const void *a
, const void *b
)
33 return strcmp(((const cmdinfo_t
*)a
)->name
,
34 ((const cmdinfo_t
*)b
)->name
);
37 void qemuio_add_command(const cmdinfo_t
*ci
)
39 cmdtab
= g_renew(cmdinfo_t
, cmdtab
, ++ncmds
);
40 cmdtab
[ncmds
- 1] = *ci
;
41 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
44 int qemuio_command_usage(const cmdinfo_t
*ci
)
46 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
50 static int init_check_command(BlockBackend
*blk
, const cmdinfo_t
*ct
)
52 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
55 if (!(ct
->flags
& CMD_NOFILE_OK
) && !blk
) {
56 fprintf(stderr
, "no file open, try 'help open'\n");
62 static int command(BlockBackend
*blk
, const cmdinfo_t
*ct
, int argc
,
67 if (!init_check_command(blk
, ct
)) {
71 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
72 if (ct
->argmax
== -1) {
74 "bad argument count %d to %s, expected at least %d arguments\n",
75 argc
-1, cmd
, ct
->argmin
);
76 } else if (ct
->argmin
== ct
->argmax
) {
78 "bad argument count %d to %s, expected %d arguments\n",
79 argc
-1, cmd
, ct
->argmin
);
82 "bad argument count %d to %s, expected between %d and %d arguments\n",
83 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
88 return ct
->cfunc(blk
, argc
, argv
);
91 static const cmdinfo_t
*find_command(const char *cmd
)
95 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
96 if (strcmp(ct
->name
, cmd
) == 0 ||
97 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
99 return (const cmdinfo_t
*)ct
;
105 /* Invoke fn() for commands with a matching prefix */
106 void qemuio_complete_command(const char *input
,
107 void (*fn
)(const char *cmd
, void *opaque
),
111 size_t input_len
= strlen(input
);
113 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
114 if (strncmp(input
, ct
->name
, input_len
) == 0) {
115 fn(ct
->name
, opaque
);
120 static char **breakline(char *input
, int *count
)
124 char **rval
= g_new0(char *, 1);
126 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
131 rval
= g_renew(char *, rval
, (c
+ 1));
139 static int64_t cvtnum(const char *s
)
144 ret
= qemu_strtosz_suffix(s
, &end
, QEMU_STRTOSZ_DEFSUFFIX_B
);
146 /* Detritus at the end of the string */
152 static void print_cvtnum_err(int64_t rc
, const char *arg
)
156 printf("Parsing error: non-numeric argument,"
157 " or extraneous/unrecognized suffix -- %s\n", arg
);
160 printf("Parsing error: argument too large -- %s\n", arg
);
163 printf("Parsing error: %s\n", arg
);
167 #define EXABYTES(x) ((long long)(x) << 60)
168 #define PETABYTES(x) ((long long)(x) << 50)
169 #define TERABYTES(x) ((long long)(x) << 40)
170 #define GIGABYTES(x) ((long long)(x) << 30)
171 #define MEGABYTES(x) ((long long)(x) << 20)
172 #define KILOBYTES(x) ((long long)(x) << 10)
174 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
175 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
176 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
177 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
178 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
179 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
181 static void cvtstr(double value
, char *str
, size_t size
)
186 if (value
>= EXABYTES(1)) {
188 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
189 } else if (value
>= PETABYTES(1)) {
191 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
192 } else if (value
>= TERABYTES(1)) {
194 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
195 } else if (value
>= GIGABYTES(1)) {
197 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
198 } else if (value
>= MEGABYTES(1)) {
200 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
201 } else if (value
>= KILOBYTES(1)) {
203 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
206 snprintf(str
, size
- 6, "%f", value
);
209 trim
= strstr(str
, ".000");
211 strcpy(trim
, suffix
);
219 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
221 t1
.tv_usec
-= t2
.tv_usec
;
222 if (t1
.tv_usec
< 0) {
223 t1
.tv_usec
+= 1000000;
226 t1
.tv_sec
-= t2
.tv_sec
;
230 static double tdiv(double value
, struct timeval tv
)
232 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
235 #define HOURS(sec) ((sec) / (60 * 60))
236 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
237 #define SECONDS(sec) ((sec) % 60)
241 TERSE_FIXED_TIME
= 0x1,
242 VERBOSE_FIXED_TIME
= 0x2,
245 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
247 double usec
= (double)tv
->tv_usec
/ 1000000.0;
249 if (format
& TERSE_FIXED_TIME
) {
250 if (!HOURS(tv
->tv_sec
)) {
251 snprintf(ts
, size
, "%u:%02u.%02u",
252 (unsigned int) MINUTES(tv
->tv_sec
),
253 (unsigned int) SECONDS(tv
->tv_sec
),
254 (unsigned int) (usec
* 100));
257 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
260 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
261 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
262 (unsigned int) HOURS(tv
->tv_sec
),
263 (unsigned int) MINUTES(tv
->tv_sec
),
264 (unsigned int) SECONDS(tv
->tv_sec
),
265 (unsigned int) (usec
* 100));
267 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
272 * Parse the pattern argument to various sub-commands.
274 * Because the pattern is used as an argument to memset it must evaluate
275 * to an unsigned integer that fits into a single byte.
277 static int parse_pattern(const char *arg
)
282 pattern
= strtol(arg
, &endptr
, 0);
283 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
284 printf("%s is not a valid pattern byte\n", arg
);
292 * Memory allocation helpers.
294 * Make sure memory is aligned by default, or purposefully misaligned if
295 * that is specified on the command line.
298 #define MISALIGN_OFFSET 16
299 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
303 if (qemuio_misalign
) {
304 len
+= MISALIGN_OFFSET
;
306 buf
= blk_blockalign(blk
, len
);
307 memset(buf
, pattern
, len
);
308 if (qemuio_misalign
) {
309 buf
+= MISALIGN_OFFSET
;
314 static void qemu_io_free(void *p
)
316 if (qemuio_misalign
) {
317 p
-= MISALIGN_OFFSET
;
322 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
328 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
329 const uint8_t *s
= p
;
331 printf("%08" PRIx64
": ", offset
+ i
);
332 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
336 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
347 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
348 int64_t count
, int64_t total
, int cnt
, int Cflag
)
350 char s1
[64], s2
[64], ts
[64];
352 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
354 cvtstr((double)total
, s1
, sizeof(s1
));
355 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
356 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
357 op
, total
, count
, offset
);
358 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
359 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
360 } else {/* bytes,ops,time,bytes/sec,ops/sec */
361 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
363 tdiv((double)total
, *t
),
364 tdiv((double)cnt
, *t
));
369 * Parse multiple length statements for vectored I/O, and construct an I/O
370 * vector matching it.
373 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
376 size_t *sizes
= g_new0(size_t, nr_iov
);
382 for (i
= 0; i
< nr_iov
; i
++) {
388 print_cvtnum_err(len
, arg
);
392 /* should be SIZE_T_MAX, but that doesn't exist */
394 printf("Argument '%s' exceeds maximum size %d\n", arg
, INT_MAX
);
399 printf("length argument %" PRId64
400 " is not sector aligned\n", len
);
408 qemu_iovec_init(qiov
, nr_iov
);
410 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
412 for (i
= 0; i
< nr_iov
; i
++) {
413 qemu_iovec_add(qiov
, p
, sizes
[i
]);
422 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
423 int64_t count
, int64_t *total
)
425 if (count
> INT_MAX
) {
429 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, count
);
436 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
437 int64_t count
, int64_t *total
)
439 if (count
> INT_MAX
) {
443 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, count
, 0);
459 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
461 CoWriteZeroes
*data
= opaque
;
463 data
->ret
= blk_co_write_zeroes(data
->blk
, data
->offset
, data
->count
, 0);
466 *data
->total
= data
->ret
;
470 *data
->total
= data
->count
;
473 static int do_co_write_zeroes(BlockBackend
*blk
, int64_t offset
, int64_t count
,
477 CoWriteZeroes data
= {
485 if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
489 co
= qemu_coroutine_create(co_write_zeroes_entry
);
490 qemu_coroutine_enter(co
, &data
);
492 aio_poll(blk_get_aio_context(blk
), true);
501 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
502 int64_t count
, int64_t *total
)
506 if (count
>> 9 > INT_MAX
) {
510 ret
= blk_write_compressed(blk
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
518 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
519 int64_t count
, int64_t *total
)
521 if (count
> INT_MAX
) {
525 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
532 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
533 int64_t count
, int64_t *total
)
535 if (count
> INT_MAX
) {
539 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
546 #define NOT_DONE 0x7fffffff
547 static void aio_rw_done(void *opaque
, int ret
)
549 *(int *)opaque
= ret
;
552 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
553 int64_t offset
, int *total
)
555 int async_ret
= NOT_DONE
;
557 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
558 while (async_ret
== NOT_DONE
) {
559 main_loop_wait(false);
563 return async_ret
< 0 ? async_ret
: 1;
566 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
567 int64_t offset
, int *total
)
569 int async_ret
= NOT_DONE
;
571 blk_aio_pwritev(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
572 while (async_ret
== NOT_DONE
) {
573 main_loop_wait(false);
577 return async_ret
< 0 ? async_ret
: 1;
580 struct multiwrite_async_ret
{
585 static void multiwrite_cb(void *opaque
, int ret
)
587 struct multiwrite_async_ret
*async_ret
= opaque
;
589 async_ret
->num_done
++;
591 async_ret
->error
= ret
;
595 static int do_aio_multiwrite(BlockBackend
*blk
, BlockRequest
* reqs
,
596 int num_reqs
, int *total
)
599 struct multiwrite_async_ret async_ret
= {
605 for (i
= 0; i
< num_reqs
; i
++) {
606 reqs
[i
].cb
= multiwrite_cb
;
607 reqs
[i
].opaque
= &async_ret
;
608 *total
+= reqs
[i
].qiov
->size
;
611 ret
= blk_aio_multiwrite(blk
, reqs
, num_reqs
);
616 while (async_ret
.num_done
< num_reqs
) {
617 main_loop_wait(false);
620 return async_ret
.error
< 0 ? async_ret
.error
: 1;
623 static void read_help(void)
627 " reads a range of bytes from the given offset\n"
630 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
632 " Reads a segment of the currently open file, optionally dumping it to the\n"
633 " standard output stream (with -v option) for subsequent inspection.\n"
634 " -b, -- read from the VM state rather than the virtual disk\n"
635 " -C, -- report statistics in a machine parsable format\n"
636 " -l, -- length for pattern verification (only with -P)\n"
637 " -p, -- allow unaligned access\n"
638 " -P, -- use a pattern to verify read data\n"
639 " -q, -- quiet mode, do not show I/O statistics\n"
640 " -s, -- start offset for pattern verification (only with -P)\n"
641 " -v, -- dump buffer to standard output\n"
645 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
647 static const cmdinfo_t read_cmd
= {
653 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
654 .oneline
= "reads a number of bytes at a specified offset",
658 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
660 struct timeval t1
, t2
;
661 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
662 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
667 /* Some compilers get confused and warn if this is not initialized. */
670 int64_t pattern_offset
= 0, pattern_count
= 0;
672 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
682 pattern_count
= cvtnum(optarg
);
683 if (pattern_count
< 0) {
684 print_cvtnum_err(pattern_count
, optarg
);
693 pattern
= parse_pattern(optarg
);
703 pattern_offset
= cvtnum(optarg
);
704 if (pattern_offset
< 0) {
705 print_cvtnum_err(pattern_offset
, optarg
);
713 return qemuio_command_usage(&read_cmd
);
717 if (optind
!= argc
- 2) {
718 return qemuio_command_usage(&read_cmd
);
721 if (bflag
&& pflag
) {
722 printf("-b and -p cannot be specified at the same time\n");
726 offset
= cvtnum(argv
[optind
]);
728 print_cvtnum_err(offset
, argv
[optind
]);
733 count
= cvtnum(argv
[optind
]);
735 print_cvtnum_err(count
, argv
[optind
]);
737 } else if (count
> SIZE_MAX
) {
738 printf("length cannot exceed %" PRIu64
", given %s\n",
739 (uint64_t) SIZE_MAX
, argv
[optind
]);
743 if (!Pflag
&& (lflag
|| sflag
)) {
744 return qemuio_command_usage(&read_cmd
);
748 pattern_count
= count
- pattern_offset
;
751 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
752 printf("pattern verification range exceeds end of read data\n");
757 if (offset
& 0x1ff) {
758 printf("offset %" PRId64
" is not sector aligned\n",
763 printf("count %"PRId64
" is not sector aligned\n",
769 buf
= qemu_io_alloc(blk
, count
, 0xab);
771 gettimeofday(&t1
, NULL
);
773 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
775 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
777 gettimeofday(&t2
, NULL
);
780 printf("read failed: %s\n", strerror(-cnt
));
785 void *cmp_buf
= g_malloc(pattern_count
);
786 memset(cmp_buf
, pattern
, pattern_count
);
787 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
788 printf("Pattern verification failed at offset %"
789 PRId64
", %"PRId64
" bytes\n",
790 offset
+ pattern_offset
, pattern_count
);
800 dump_buffer(buf
, offset
, count
);
803 /* Finally, report back -- -C gives a parsable format */
805 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
813 static void readv_help(void)
817 " reads a range of bytes from the given offset into multiple buffers\n"
820 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
822 " Reads a segment of the currently open file, optionally dumping it to the\n"
823 " standard output stream (with -v option) for subsequent inspection.\n"
824 " Uses multiple iovec buffers if more than one byte range is specified.\n"
825 " -C, -- report statistics in a machine parsable format\n"
826 " -P, -- use a pattern to verify read data\n"
827 " -v, -- dump buffer to standard output\n"
828 " -q, -- quiet mode, do not show I/O statistics\n"
832 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
834 static const cmdinfo_t readv_cmd
= {
839 .args
= "[-Cqv] [-P pattern ] off len [len..]",
840 .oneline
= "reads a number of bytes at a specified offset",
844 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
846 struct timeval t1
, t2
;
847 int Cflag
= 0, qflag
= 0, vflag
= 0;
851 /* Some compilers get confused and warn if this is not initialized. */
858 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
865 pattern
= parse_pattern(optarg
);
877 return qemuio_command_usage(&readv_cmd
);
881 if (optind
> argc
- 2) {
882 return qemuio_command_usage(&readv_cmd
);
886 offset
= cvtnum(argv
[optind
]);
888 print_cvtnum_err(offset
, argv
[optind
]);
893 if (offset
& 0x1ff) {
894 printf("offset %" PRId64
" is not sector aligned\n",
899 nr_iov
= argc
- optind
;
900 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
905 gettimeofday(&t1
, NULL
);
906 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
907 gettimeofday(&t2
, NULL
);
910 printf("readv failed: %s\n", strerror(-cnt
));
915 void *cmp_buf
= g_malloc(qiov
.size
);
916 memset(cmp_buf
, pattern
, qiov
.size
);
917 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
918 printf("Pattern verification failed at offset %"
919 PRId64
", %zd bytes\n", offset
, qiov
.size
);
929 dump_buffer(buf
, offset
, qiov
.size
);
932 /* Finally, report back -- -C gives a parsable format */
934 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
937 qemu_iovec_destroy(&qiov
);
942 static void write_help(void)
946 " writes a range of bytes from the given offset\n"
949 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
951 " Writes into a segment of the currently open file, using a buffer\n"
952 " filled with a set pattern (0xcdcdcdcd).\n"
953 " -b, -- write to the VM state rather than the virtual disk\n"
954 " -c, -- write compressed data with blk_write_compressed\n"
955 " -p, -- allow unaligned access\n"
956 " -P, -- use different pattern to fill file\n"
957 " -C, -- report statistics in a machine parsable format\n"
958 " -q, -- quiet mode, do not show I/O statistics\n"
959 " -z, -- write zeroes using blk_co_write_zeroes\n"
963 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
965 static const cmdinfo_t write_cmd
= {
971 .args
= "[-bcCpqz] [-P pattern ] off len",
972 .oneline
= "writes a number of bytes at a specified offset",
976 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
978 struct timeval t1
, t2
;
979 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0, Pflag
= 0, zflag
= 0;
985 /* Some compilers get confused and warn if this is not initialized. */
989 while ((c
= getopt(argc
, argv
, "bcCpP:qz")) != -1) {
1005 pattern
= parse_pattern(optarg
);
1017 return qemuio_command_usage(&write_cmd
);
1021 if (optind
!= argc
- 2) {
1022 return qemuio_command_usage(&write_cmd
);
1025 if (bflag
+ pflag
+ zflag
> 1) {
1026 printf("-b, -p, or -z cannot be specified at the same time\n");
1030 if (zflag
&& Pflag
) {
1031 printf("-z and -P cannot be specified at the same time\n");
1035 offset
= cvtnum(argv
[optind
]);
1037 print_cvtnum_err(offset
, argv
[optind
]);
1042 count
= cvtnum(argv
[optind
]);
1044 print_cvtnum_err(count
, argv
[optind
]);
1046 } else if (count
> SIZE_MAX
) {
1047 printf("length cannot exceed %" PRIu64
", given %s\n",
1048 (uint64_t) SIZE_MAX
, argv
[optind
]);
1053 if (offset
& 0x1ff) {
1054 printf("offset %" PRId64
" is not sector aligned\n",
1059 if (count
& 0x1ff) {
1060 printf("count %"PRId64
" is not sector aligned\n",
1067 buf
= qemu_io_alloc(blk
, count
, pattern
);
1070 gettimeofday(&t1
, NULL
);
1072 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1074 cnt
= do_co_write_zeroes(blk
, offset
, count
, &total
);
1076 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1078 cnt
= do_pwrite(blk
, buf
, offset
, count
, &total
);
1080 gettimeofday(&t2
, NULL
);
1083 printf("write failed: %s\n", strerror(-cnt
));
1091 /* Finally, report back -- -C gives a parsable format */
1093 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1108 " writes a range of bytes from the given offset source from multiple buffers\n"
1111 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1113 " Writes into a segment of the currently open file, using a buffer\n"
1114 " filled with a set pattern (0xcdcdcdcd).\n"
1115 " -P, -- use different pattern to fill file\n"
1116 " -C, -- report statistics in a machine parsable format\n"
1117 " -q, -- quiet mode, do not show I/O statistics\n"
1121 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1123 static const cmdinfo_t writev_cmd
= {
1128 .args
= "[-Cq] [-P pattern ] off len [len..]",
1129 .oneline
= "writes a number of bytes at a specified offset",
1130 .help
= writev_help
,
1133 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1135 struct timeval t1
, t2
;
1136 int Cflag
= 0, qflag
= 0;
1140 /* Some compilers get confused and warn if this is not initialized. */
1146 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1155 pattern
= parse_pattern(optarg
);
1161 return qemuio_command_usage(&writev_cmd
);
1165 if (optind
> argc
- 2) {
1166 return qemuio_command_usage(&writev_cmd
);
1169 offset
= cvtnum(argv
[optind
]);
1171 print_cvtnum_err(offset
, argv
[optind
]);
1176 if (offset
& 0x1ff) {
1177 printf("offset %" PRId64
" is not sector aligned\n",
1182 nr_iov
= argc
- optind
;
1183 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1188 gettimeofday(&t1
, NULL
);
1189 cnt
= do_aio_writev(blk
, &qiov
, offset
, &total
);
1190 gettimeofday(&t2
, NULL
);
1193 printf("writev failed: %s\n", strerror(-cnt
));
1201 /* Finally, report back -- -C gives a parsable format */
1203 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1205 qemu_iovec_destroy(&qiov
);
1210 static void multiwrite_help(void)
1214 " writes a range of bytes from the given offset source from multiple buffers,\n"
1215 " in a batch of requests that may be merged by qemu\n"
1218 " 'multiwrite 512 1k 1k ; 4k 1k'\n"
1219 " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1221 " Writes into a segment of the currently open file, using a buffer\n"
1222 " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1223 " by one for each request contained in the multiwrite command.\n"
1224 " -P, -- use different pattern to fill file\n"
1225 " -C, -- report statistics in a machine parsable format\n"
1226 " -q, -- quiet mode, do not show I/O statistics\n"
1230 static int multiwrite_f(BlockBackend
*blk
, int argc
, char **argv
);
1232 static const cmdinfo_t multiwrite_cmd
= {
1233 .name
= "multiwrite",
1234 .cfunc
= multiwrite_f
,
1237 .args
= "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1238 .oneline
= "issues multiple write requests at once",
1239 .help
= multiwrite_help
,
1242 static int multiwrite_f(BlockBackend
*blk
, int argc
, char **argv
)
1244 struct timeval t1
, t2
;
1245 int Cflag
= 0, qflag
= 0;
1248 int64_t offset
, first_offset
= 0;
1249 /* Some compilers get confused and warn if this is not initialized. */
1254 QEMUIOVector
*qiovs
;
1258 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1267 pattern
= parse_pattern(optarg
);
1273 return qemuio_command_usage(&writev_cmd
);
1277 if (optind
> argc
- 2) {
1278 return qemuio_command_usage(&writev_cmd
);
1282 for (i
= optind
; i
< argc
; i
++) {
1283 if (!strcmp(argv
[i
], ";")) {
1288 reqs
= g_new0(BlockRequest
, nr_reqs
);
1289 buf
= g_new0(char *, nr_reqs
);
1290 qiovs
= g_new(QEMUIOVector
, nr_reqs
);
1292 for (i
= 0; i
< nr_reqs
&& optind
< argc
; i
++) {
1295 /* Read the offset of the request */
1296 offset
= cvtnum(argv
[optind
]);
1298 print_cvtnum_err(offset
, argv
[optind
]);
1303 if (offset
& 0x1ff) {
1304 printf("offset %lld is not sector aligned\n",
1310 first_offset
= offset
;
1313 /* Read lengths for qiov entries */
1314 for (j
= optind
; j
< argc
; j
++) {
1315 if (!strcmp(argv
[j
], ";")) {
1320 nr_iov
= j
- optind
;
1323 buf
[i
] = create_iovec(blk
, &qiovs
[i
], &argv
[optind
], nr_iov
, pattern
);
1324 if (buf
[i
] == NULL
) {
1328 reqs
[i
].qiov
= &qiovs
[i
];
1329 reqs
[i
].sector
= offset
>> 9;
1330 reqs
[i
].nb_sectors
= reqs
[i
].qiov
->size
>> 9;
1337 /* If there were empty requests at the end, ignore them */
1340 gettimeofday(&t1
, NULL
);
1341 cnt
= do_aio_multiwrite(blk
, reqs
, nr_reqs
, &total
);
1342 gettimeofday(&t2
, NULL
);
1345 printf("aio_multiwrite failed: %s\n", strerror(-cnt
));
1353 /* Finally, report back -- -C gives a parsable format */
1355 print_report("wrote", &t2
, first_offset
, total
, total
, cnt
, Cflag
);
1357 for (i
= 0; i
< nr_reqs
; i
++) {
1358 qemu_io_free(buf
[i
]);
1359 if (reqs
[i
].qiov
!= NULL
) {
1360 qemu_iovec_destroy(&qiovs
[i
]);
1379 BlockAcctCookie acct
;
1384 static void aio_write_done(void *opaque
, int ret
)
1386 struct aio_ctx
*ctx
= opaque
;
1389 gettimeofday(&t2
, NULL
);
1393 printf("aio_write failed: %s\n", strerror(-ret
));
1394 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1398 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1404 /* Finally, report back -- -C gives a parsable format */
1405 t2
= tsub(t2
, ctx
->t1
);
1406 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1407 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1410 qemu_io_free(ctx
->buf
);
1411 qemu_iovec_destroy(&ctx
->qiov
);
1416 static void aio_read_done(void *opaque
, int ret
)
1418 struct aio_ctx
*ctx
= opaque
;
1421 gettimeofday(&t2
, NULL
);
1424 printf("readv failed: %s\n", strerror(-ret
));
1425 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1430 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1432 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1433 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1434 printf("Pattern verification failed at offset %"
1435 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1440 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1447 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1450 /* Finally, report back -- -C gives a parsable format */
1451 t2
= tsub(t2
, ctx
->t1
);
1452 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1453 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1455 qemu_io_free(ctx
->buf
);
1456 qemu_iovec_destroy(&ctx
->qiov
);
1460 static void aio_read_help(void)
1464 " asynchronously reads a range of bytes from the given offset\n"
1467 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1469 " Reads a segment of the currently open file, optionally dumping it to the\n"
1470 " standard output stream (with -v option) for subsequent inspection.\n"
1471 " The read is performed asynchronously and the aio_flush command must be\n"
1472 " used to ensure all outstanding aio requests have been completed.\n"
1473 " -C, -- report statistics in a machine parsable format\n"
1474 " -P, -- use a pattern to verify read data\n"
1475 " -v, -- dump buffer to standard output\n"
1476 " -q, -- quiet mode, do not show I/O statistics\n"
1480 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1482 static const cmdinfo_t aio_read_cmd
= {
1484 .cfunc
= aio_read_f
,
1487 .args
= "[-Cqv] [-P pattern ] off len [len..]",
1488 .oneline
= "asynchronously reads a number of bytes",
1489 .help
= aio_read_help
,
1492 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1495 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1498 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
1505 ctx
->pattern
= parse_pattern(optarg
);
1506 if (ctx
->pattern
< 0) {
1519 return qemuio_command_usage(&aio_read_cmd
);
1523 if (optind
> argc
- 2) {
1525 return qemuio_command_usage(&aio_read_cmd
);
1528 ctx
->offset
= cvtnum(argv
[optind
]);
1529 if (ctx
->offset
< 0) {
1530 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1536 if (ctx
->offset
& 0x1ff) {
1537 printf("offset %" PRId64
" is not sector aligned\n",
1539 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1544 nr_iov
= argc
- optind
;
1545 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1546 if (ctx
->buf
== NULL
) {
1547 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1552 gettimeofday(&ctx
->t1
, NULL
);
1553 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1555 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1559 static void aio_write_help(void)
1563 " asynchronously writes a range of bytes from the given offset source\n"
1564 " from multiple buffers\n"
1567 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1569 " Writes into a segment of the currently open file, using a buffer\n"
1570 " filled with a set pattern (0xcdcdcdcd).\n"
1571 " The write is performed asynchronously and the aio_flush command must be\n"
1572 " used to ensure all outstanding aio requests have been completed.\n"
1573 " -P, -- use different pattern to fill file\n"
1574 " -C, -- report statistics in a machine parsable format\n"
1575 " -q, -- quiet mode, do not show I/O statistics\n"
1576 " -z, -- write zeroes using blk_aio_write_zeroes\n"
1580 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1582 static const cmdinfo_t aio_write_cmd
= {
1583 .name
= "aio_write",
1584 .cfunc
= aio_write_f
,
1587 .args
= "[-Cqz] [-P pattern ] off len [len..]",
1588 .oneline
= "asynchronously writes a number of bytes",
1589 .help
= aio_write_help
,
1592 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1596 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1599 while ((c
= getopt(argc
, argv
, "CqP:z")) != -1) {
1608 pattern
= parse_pattern(optarg
);
1619 return qemuio_command_usage(&aio_write_cmd
);
1623 if (optind
> argc
- 2) {
1625 return qemuio_command_usage(&aio_write_cmd
);
1628 if (ctx
->zflag
&& optind
!= argc
- 2) {
1629 printf("-z supports only a single length parameter\n");
1634 if (ctx
->zflag
&& ctx
->Pflag
) {
1635 printf("-z and -P cannot be specified at the same time\n");
1640 ctx
->offset
= cvtnum(argv
[optind
]);
1641 if (ctx
->offset
< 0) {
1642 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1648 if (ctx
->offset
& 0x1ff) {
1649 printf("offset %" PRId64
" is not sector aligned\n",
1651 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1657 int64_t count
= cvtnum(argv
[optind
]);
1659 print_cvtnum_err(count
, argv
[optind
]);
1664 ctx
->qiov
.size
= count
;
1665 blk_aio_write_zeroes(blk
, ctx
->offset
, count
, 0, aio_write_done
, ctx
);
1667 nr_iov
= argc
- optind
;
1668 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1670 if (ctx
->buf
== NULL
) {
1671 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1676 gettimeofday(&ctx
->t1
, NULL
);
1677 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1680 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_write_done
, ctx
);
1685 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1687 BlockAcctCookie cookie
;
1688 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1690 block_acct_done(blk_get_stats(blk
), &cookie
);
1694 static const cmdinfo_t aio_flush_cmd
= {
1695 .name
= "aio_flush",
1696 .cfunc
= aio_flush_f
,
1697 .oneline
= "completes all outstanding aio requests"
1700 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1706 static const cmdinfo_t flush_cmd
= {
1710 .oneline
= "flush all in-core file state to disk",
1713 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1718 offset
= cvtnum(argv
[1]);
1720 print_cvtnum_err(offset
, argv
[1]);
1724 ret
= blk_truncate(blk
, offset
);
1726 printf("truncate: %s\n", strerror(-ret
));
1733 static const cmdinfo_t truncate_cmd
= {
1736 .cfunc
= truncate_f
,
1740 .oneline
= "truncates the current file at the given offset",
1743 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1748 size
= blk_getlength(blk
);
1750 printf("getlength: %s\n", strerror(-size
));
1754 cvtstr(size
, s1
, sizeof(s1
));
1760 static const cmdinfo_t length_cmd
= {
1764 .oneline
= "gets the length of the current file",
1768 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1770 BlockDriverState
*bs
= blk_bs(blk
);
1771 BlockDriverInfo bdi
;
1772 ImageInfoSpecific
*spec_info
;
1773 char s1
[64], s2
[64];
1776 if (bs
->drv
&& bs
->drv
->format_name
) {
1777 printf("format name: %s\n", bs
->drv
->format_name
);
1779 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1780 printf("format name: %s\n", bs
->drv
->protocol_name
);
1783 ret
= bdrv_get_info(bs
, &bdi
);
1788 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1789 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1791 printf("cluster size: %s\n", s1
);
1792 printf("vm state offset: %s\n", s2
);
1794 spec_info
= bdrv_get_specific_info(bs
);
1796 printf("Format specific information:\n");
1797 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1798 qapi_free_ImageInfoSpecific(spec_info
);
1806 static const cmdinfo_t info_cmd
= {
1810 .oneline
= "prints information about the current file",
1813 static void discard_help(void)
1817 " discards a range of bytes from the given offset\n"
1820 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1822 " Discards a segment of the currently open file.\n"
1823 " -C, -- report statistics in a machine parsable format\n"
1824 " -q, -- quiet mode, do not show I/O statistics\n"
1828 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1830 static const cmdinfo_t discard_cmd
= {
1836 .args
= "[-Cq] off len",
1837 .oneline
= "discards a number of bytes at a specified offset",
1838 .help
= discard_help
,
1841 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1843 struct timeval t1
, t2
;
1844 int Cflag
= 0, qflag
= 0;
1846 int64_t offset
, count
;
1848 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1857 return qemuio_command_usage(&discard_cmd
);
1861 if (optind
!= argc
- 2) {
1862 return qemuio_command_usage(&discard_cmd
);
1865 offset
= cvtnum(argv
[optind
]);
1867 print_cvtnum_err(offset
, argv
[optind
]);
1872 count
= cvtnum(argv
[optind
]);
1874 print_cvtnum_err(count
, argv
[optind
]);
1876 } else if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
1877 printf("length cannot exceed %"PRIu64
", given %s\n",
1878 (uint64_t)INT_MAX
<< BDRV_SECTOR_BITS
,
1883 gettimeofday(&t1
, NULL
);
1884 ret
= blk_discard(blk
, offset
>> BDRV_SECTOR_BITS
,
1885 count
>> BDRV_SECTOR_BITS
);
1886 gettimeofday(&t2
, NULL
);
1889 printf("discard failed: %s\n", strerror(-ret
));
1893 /* Finally, report back -- -C gives a parsable format */
1896 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1903 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1905 BlockDriverState
*bs
= blk_bs(blk
);
1906 int64_t offset
, sector_num
, nb_sectors
, remaining
;
1911 offset
= cvtnum(argv
[1]);
1913 print_cvtnum_err(offset
, argv
[1]);
1915 } else if (offset
& 0x1ff) {
1916 printf("offset %" PRId64
" is not sector aligned\n",
1922 nb_sectors
= cvtnum(argv
[2]);
1923 if (nb_sectors
< 0) {
1924 print_cvtnum_err(nb_sectors
, argv
[2]);
1926 } else if (nb_sectors
> INT_MAX
) {
1927 printf("length argument cannot exceed %d, given %s\n",
1935 remaining
= nb_sectors
;
1937 sector_num
= offset
>> 9;
1939 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1941 printf("is_allocated failed: %s\n", strerror(-ret
));
1950 nb_sectors
-= remaining
;
1955 cvtstr(offset
, s1
, sizeof(s1
));
1957 printf("%"PRId64
"/%"PRId64
" sectors allocated at offset %s\n",
1958 sum_alloc
, nb_sectors
, s1
);
1962 static const cmdinfo_t alloc_cmd
= {
1968 .args
= "off [sectors]",
1969 .oneline
= "checks if a sector is present in the file",
1973 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1974 int64_t nb_sectors
, int64_t *pnum
)
1976 int num
, num_checked
;
1979 num_checked
= MIN(nb_sectors
, INT_MAX
);
1980 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1988 while (nb_sectors
> 0 && ret
== firstret
) {
1992 num_checked
= MIN(nb_sectors
, INT_MAX
);
1993 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1994 if (ret
== firstret
&& num
) {
2004 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
2007 int64_t nb_sectors
, total_sectors
;
2014 total_sectors
= blk_nb_sectors(blk
);
2015 if (total_sectors
< 0) {
2016 error_report("Failed to query image length: %s",
2017 strerror(-total_sectors
));
2021 nb_sectors
= total_sectors
;
2024 ret
= map_is_allocated(blk_bs(blk
), offset
, nb_sectors
, &num
);
2026 error_report("Failed to get allocation status: %s", strerror(-ret
));
2029 error_report("Unexpected end of image");
2033 retstr
= ret
? " allocated" : "not allocated";
2034 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
2035 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
2036 "at offset %s (%d)\n",
2037 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
2041 } while (offset
< total_sectors
);
2046 static const cmdinfo_t map_cmd
= {
2052 .oneline
= "prints the allocated areas of a file",
2055 static void reopen_help(void)
2059 " Changes the open options of an already opened image\n"
2062 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
2064 " -r, -- Reopen the image read-only\n"
2065 " -c, -- Change the cache mode to the given value\n"
2066 " -o, -- Changes block driver options (cf. 'open' command)\n"
2070 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
2072 static QemuOptsList reopen_opts
= {
2074 .merge_lists
= true,
2075 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
2077 /* no elements => accept any params */
2078 { /* end of list */ }
2082 static const cmdinfo_t reopen_cmd
= {
2087 .args
= "[-r] [-c cache] [-o options]",
2088 .oneline
= "reopens an image with new options",
2089 .help
= reopen_help
,
2092 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
2094 BlockDriverState
*bs
= blk_bs(blk
);
2098 int flags
= bs
->open_flags
;
2099 bool writethrough
= !blk_enable_write_cache(blk
);
2101 BlockReopenQueue
*brq
;
2102 Error
*local_err
= NULL
;
2104 while ((c
= getopt(argc
, argv
, "c:o:r")) != -1) {
2107 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
2108 error_report("Invalid cache option: %s", optarg
);
2113 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
2114 qemu_opts_reset(&reopen_opts
);
2119 flags
&= ~BDRV_O_RDWR
;
2122 qemu_opts_reset(&reopen_opts
);
2123 return qemuio_command_usage(&reopen_cmd
);
2127 if (optind
!= argc
) {
2128 qemu_opts_reset(&reopen_opts
);
2129 return qemuio_command_usage(&reopen_cmd
);
2132 if (writethrough
!= blk_enable_write_cache(blk
) &&
2133 blk_get_attached_dev(blk
))
2135 error_report("Cannot change cache.writeback: Device attached");
2136 qemu_opts_reset(&reopen_opts
);
2140 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
2141 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
2142 qemu_opts_reset(&reopen_opts
);
2144 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
2145 bdrv_reopen_multiple(brq
, &local_err
);
2147 error_report_err(local_err
);
2149 blk_set_enable_write_cache(blk
, !writethrough
);
2155 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
2159 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
2161 printf("Could not set breakpoint: %s\n", strerror(-ret
));
2167 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2171 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
2173 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2179 static const cmdinfo_t break_cmd
= {
2184 .args
= "event tag",
2185 .oneline
= "sets a breakpoint on event and tags the stopped "
2189 static const cmdinfo_t remove_break_cmd
= {
2190 .name
= "remove_break",
2193 .cfunc
= remove_break_f
,
2195 .oneline
= "remove a breakpoint by tag",
2198 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2202 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2204 printf("Could not resume request: %s\n", strerror(-ret
));
2210 static const cmdinfo_t resume_cmd
= {
2216 .oneline
= "resumes the request tagged as tag",
2219 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2221 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2222 aio_poll(blk_get_aio_context(blk
), true);
2228 static const cmdinfo_t wait_break_cmd
= {
2229 .name
= "wait_break",
2232 .cfunc
= wait_break_f
,
2234 .oneline
= "waits for the suspension of a request",
2237 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2242 static const cmdinfo_t abort_cmd
= {
2245 .flags
= CMD_NOFILE_OK
,
2246 .oneline
= "simulate a program crash using abort(3)",
2249 static void sigraise_help(void)
2253 " raises the given signal\n"
2256 " 'sigraise %i' - raises SIGTERM\n"
2258 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2259 " given to sigraise.\n"
2263 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2265 static const cmdinfo_t sigraise_cmd
= {
2267 .cfunc
= sigraise_f
,
2270 .flags
= CMD_NOFILE_OK
,
2272 .oneline
= "raises a signal",
2273 .help
= sigraise_help
,
2276 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2278 int64_t sig
= cvtnum(argv
[1]);
2280 print_cvtnum_err(sig
, argv
[1]);
2282 } else if (sig
> NSIG
) {
2283 printf("signal argument '%s' is too large to be a valid signal\n",
2288 /* Using raise() to kill this process does not necessarily flush all open
2289 * streams. At least stdout and stderr (although the latter should be
2290 * non-buffered anyway) should be flushed, though. */
2298 static void sleep_cb(void *opaque
)
2300 bool *expired
= opaque
;
2304 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2308 struct QEMUTimer
*timer
;
2309 bool expired
= false;
2311 ms
= strtol(argv
[1], &endptr
, 0);
2312 if (ms
< 0 || *endptr
!= '\0') {
2313 printf("%s is not a valid number\n", argv
[1]);
2317 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2318 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2321 main_loop_wait(false);
2329 static const cmdinfo_t sleep_cmd
= {
2334 .flags
= CMD_NOFILE_OK
,
2335 .oneline
= "waits for the given value in milliseconds",
2338 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2343 printf("%s ", ct
->name
);
2345 printf("(or %s) ", ct
->altname
);
2350 printf("%s ", ct
->args
);
2352 printf("-- %s\n", ct
->oneline
);
2355 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2357 help_oneline(cmd
, ct
);
2363 static void help_all(void)
2365 const cmdinfo_t
*ct
;
2367 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2368 help_oneline(ct
->name
, ct
);
2370 printf("\nUse 'help commandname' for extended help.\n");
2373 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2375 const cmdinfo_t
*ct
;
2382 ct
= find_command(argv
[1]);
2384 printf("command %s not found\n", argv
[1]);
2388 help_onecmd(argv
[1], ct
);
2392 static const cmdinfo_t help_cmd
= {
2398 .flags
= CMD_FLAG_GLOBAL
,
2399 .args
= "[command]",
2400 .oneline
= "help for one or all commands",
2403 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2406 const cmdinfo_t
*ct
;
2411 input
= g_strdup(cmd
);
2412 v
= breakline(input
, &c
);
2414 ct
= find_command(v
[0]);
2416 done
= command(blk
, ct
, c
, v
);
2418 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2427 static void __attribute((constructor
)) init_qemuio_commands(void)
2429 /* initialize commands */
2430 qemuio_add_command(&help_cmd
);
2431 qemuio_add_command(&read_cmd
);
2432 qemuio_add_command(&readv_cmd
);
2433 qemuio_add_command(&write_cmd
);
2434 qemuio_add_command(&writev_cmd
);
2435 qemuio_add_command(&multiwrite_cmd
);
2436 qemuio_add_command(&aio_read_cmd
);
2437 qemuio_add_command(&aio_write_cmd
);
2438 qemuio_add_command(&aio_flush_cmd
);
2439 qemuio_add_command(&flush_cmd
);
2440 qemuio_add_command(&truncate_cmd
);
2441 qemuio_add_command(&length_cmd
);
2442 qemuio_add_command(&info_cmd
);
2443 qemuio_add_command(&discard_cmd
);
2444 qemuio_add_command(&alloc_cmd
);
2445 qemuio_add_command(&map_cmd
);
2446 qemuio_add_command(&reopen_cmd
);
2447 qemuio_add_command(&break_cmd
);
2448 qemuio_add_command(&remove_break_cmd
);
2449 qemuio_add_command(&resume_cmd
);
2450 qemuio_add_command(&wait_break_cmd
);
2451 qemuio_add_command(&abort_cmd
);
2452 qemuio_add_command(&sleep_cmd
);
2453 qemuio_add_command(&sigraise_cmd
);