2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009-2016 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 "qemu/cutils.h"
23 #define CMD_NOFILE_OK 0x01
27 static cmdinfo_t
*cmdtab
;
30 static int compare_cmdname(const void *a
, const void *b
)
32 return strcmp(((const cmdinfo_t
*)a
)->name
,
33 ((const cmdinfo_t
*)b
)->name
);
36 void qemuio_add_command(const cmdinfo_t
*ci
)
38 cmdtab
= g_renew(cmdinfo_t
, cmdtab
, ++ncmds
);
39 cmdtab
[ncmds
- 1] = *ci
;
40 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare_cmdname
);
43 int qemuio_command_usage(const cmdinfo_t
*ci
)
45 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
49 static int init_check_command(BlockBackend
*blk
, const cmdinfo_t
*ct
)
51 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
54 if (!(ct
->flags
& CMD_NOFILE_OK
) && !blk
) {
55 fprintf(stderr
, "no file open, try 'help open'\n");
61 static int command(BlockBackend
*blk
, const cmdinfo_t
*ct
, int argc
,
66 if (!init_check_command(blk
, ct
)) {
70 if (argc
- 1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
- 1 > ct
->argmax
)) {
71 if (ct
->argmax
== -1) {
73 "bad argument count %d to %s, expected at least %d arguments\n",
74 argc
-1, cmd
, ct
->argmin
);
75 } else if (ct
->argmin
== ct
->argmax
) {
77 "bad argument count %d to %s, expected %d arguments\n",
78 argc
-1, cmd
, ct
->argmin
);
81 "bad argument count %d to %s, expected between %d and %d arguments\n",
82 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
87 return ct
->cfunc(blk
, argc
, argv
);
90 static const cmdinfo_t
*find_command(const char *cmd
)
94 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
95 if (strcmp(ct
->name
, cmd
) == 0 ||
96 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
98 return (const cmdinfo_t
*)ct
;
104 /* Invoke fn() for commands with a matching prefix */
105 void qemuio_complete_command(const char *input
,
106 void (*fn
)(const char *cmd
, void *opaque
),
110 size_t input_len
= strlen(input
);
112 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
113 if (strncmp(input
, ct
->name
, input_len
) == 0) {
114 fn(ct
->name
, opaque
);
119 static char **breakline(char *input
, int *count
)
123 char **rval
= g_new0(char *, 1);
125 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
130 rval
= g_renew(char *, rval
, (c
+ 1));
138 static int64_t cvtnum(const char *s
)
143 ret
= qemu_strtosz_suffix(s
, &end
, QEMU_STRTOSZ_DEFSUFFIX_B
);
145 /* Detritus at the end of the string */
151 static void print_cvtnum_err(int64_t rc
, const char *arg
)
155 printf("Parsing error: non-numeric argument,"
156 " or extraneous/unrecognized suffix -- %s\n", arg
);
159 printf("Parsing error: argument too large -- %s\n", arg
);
162 printf("Parsing error: %s\n", arg
);
166 #define EXABYTES(x) ((long long)(x) << 60)
167 #define PETABYTES(x) ((long long)(x) << 50)
168 #define TERABYTES(x) ((long long)(x) << 40)
169 #define GIGABYTES(x) ((long long)(x) << 30)
170 #define MEGABYTES(x) ((long long)(x) << 20)
171 #define KILOBYTES(x) ((long long)(x) << 10)
173 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
174 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
175 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
176 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
177 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
178 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
180 static void cvtstr(double value
, char *str
, size_t size
)
185 if (value
>= EXABYTES(1)) {
187 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
188 } else if (value
>= PETABYTES(1)) {
190 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
191 } else if (value
>= TERABYTES(1)) {
193 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
194 } else if (value
>= GIGABYTES(1)) {
196 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
197 } else if (value
>= MEGABYTES(1)) {
199 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
200 } else if (value
>= KILOBYTES(1)) {
202 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
205 snprintf(str
, size
- 6, "%f", value
);
208 trim
= strstr(str
, ".000");
210 strcpy(trim
, suffix
);
218 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
220 t1
.tv_usec
-= t2
.tv_usec
;
221 if (t1
.tv_usec
< 0) {
222 t1
.tv_usec
+= 1000000;
225 t1
.tv_sec
-= t2
.tv_sec
;
229 static double tdiv(double value
, struct timeval tv
)
231 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
234 #define HOURS(sec) ((sec) / (60 * 60))
235 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
236 #define SECONDS(sec) ((sec) % 60)
240 TERSE_FIXED_TIME
= 0x1,
241 VERBOSE_FIXED_TIME
= 0x2,
244 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
246 double usec
= (double)tv
->tv_usec
/ 1000000.0;
248 if (format
& TERSE_FIXED_TIME
) {
249 if (!HOURS(tv
->tv_sec
)) {
250 snprintf(ts
, size
, "%u:%02u.%02u",
251 (unsigned int) MINUTES(tv
->tv_sec
),
252 (unsigned int) SECONDS(tv
->tv_sec
),
253 (unsigned int) (usec
* 100));
256 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
259 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
260 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
261 (unsigned int) HOURS(tv
->tv_sec
),
262 (unsigned int) MINUTES(tv
->tv_sec
),
263 (unsigned int) SECONDS(tv
->tv_sec
),
264 (unsigned int) (usec
* 100));
266 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
271 * Parse the pattern argument to various sub-commands.
273 * Because the pattern is used as an argument to memset it must evaluate
274 * to an unsigned integer that fits into a single byte.
276 static int parse_pattern(const char *arg
)
281 pattern
= strtol(arg
, &endptr
, 0);
282 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
283 printf("%s is not a valid pattern byte\n", arg
);
291 * Memory allocation helpers.
293 * Make sure memory is aligned by default, or purposefully misaligned if
294 * that is specified on the command line.
297 #define MISALIGN_OFFSET 16
298 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
302 if (qemuio_misalign
) {
303 len
+= MISALIGN_OFFSET
;
305 buf
= blk_blockalign(blk
, len
);
306 memset(buf
, pattern
, len
);
307 if (qemuio_misalign
) {
308 buf
+= MISALIGN_OFFSET
;
313 static void qemu_io_free(void *p
)
315 if (qemuio_misalign
) {
316 p
-= MISALIGN_OFFSET
;
321 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
327 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
328 const uint8_t *s
= p
;
330 printf("%08" PRIx64
": ", offset
+ i
);
331 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
335 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
346 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
347 int64_t count
, int64_t total
, int cnt
, bool Cflag
)
349 char s1
[64], s2
[64], ts
[64];
351 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
353 cvtstr((double)total
, s1
, sizeof(s1
));
354 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
355 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
356 op
, total
, count
, offset
);
357 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
358 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
359 } else {/* bytes,ops,time,bytes/sec,ops/sec */
360 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
362 tdiv((double)total
, *t
),
363 tdiv((double)cnt
, *t
));
368 * Parse multiple length statements for vectored I/O, and construct an I/O
369 * vector matching it.
372 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
375 size_t *sizes
= g_new0(size_t, nr_iov
);
381 for (i
= 0; i
< nr_iov
; i
++) {
387 print_cvtnum_err(len
, arg
);
391 if (len
> SIZE_MAX
) {
392 printf("Argument '%s' exceeds maximum size %llu\n", arg
,
393 (unsigned long long)SIZE_MAX
);
401 qemu_iovec_init(qiov
, nr_iov
);
403 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
405 for (i
= 0; i
< nr_iov
; i
++) {
406 qemu_iovec_add(qiov
, p
, sizes
[i
]);
415 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
416 int64_t count
, int64_t *total
)
418 if (count
> INT_MAX
) {
422 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, count
);
429 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
430 int64_t count
, int flags
, int64_t *total
)
432 if (count
> INT_MAX
) {
436 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, count
, flags
);
453 static void coroutine_fn
co_pwrite_zeroes_entry(void *opaque
)
455 CoWriteZeroes
*data
= opaque
;
457 data
->ret
= blk_co_pwrite_zeroes(data
->blk
, data
->offset
, data
->count
,
461 *data
->total
= data
->ret
;
465 *data
->total
= data
->count
;
468 static int do_co_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
469 int64_t count
, int flags
, int64_t *total
)
472 CoWriteZeroes data
= {
481 if (count
> INT_MAX
) {
485 co
= qemu_coroutine_create(co_pwrite_zeroes_entry
, &data
);
486 qemu_coroutine_enter(co
);
488 aio_poll(blk_get_aio_context(blk
), true);
497 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
498 int64_t count
, int64_t *total
)
502 if (count
>> 9 > BDRV_REQUEST_MAX_SECTORS
) {
506 ret
= blk_pwrite_compressed(blk
, offset
, buf
, count
);
514 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
515 int64_t count
, int64_t *total
)
517 if (count
> INT_MAX
) {
521 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
528 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
529 int64_t count
, int64_t *total
)
531 if (count
> INT_MAX
) {
535 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
542 #define NOT_DONE 0x7fffffff
543 static void aio_rw_done(void *opaque
, int ret
)
545 *(int *)opaque
= ret
;
548 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
549 int64_t offset
, int *total
)
551 int async_ret
= NOT_DONE
;
553 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
554 while (async_ret
== NOT_DONE
) {
555 main_loop_wait(false);
559 return async_ret
< 0 ? async_ret
: 1;
562 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
563 int64_t offset
, int flags
, int *total
)
565 int async_ret
= NOT_DONE
;
567 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
568 while (async_ret
== NOT_DONE
) {
569 main_loop_wait(false);
573 return async_ret
< 0 ? async_ret
: 1;
576 static void read_help(void)
580 " reads a range of bytes from the given offset\n"
583 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
585 " Reads a segment of the currently open file, optionally dumping it to the\n"
586 " standard output stream (with -v option) for subsequent inspection.\n"
587 " -b, -- read from the VM state rather than the virtual disk\n"
588 " -C, -- report statistics in a machine parsable format\n"
589 " -l, -- length for pattern verification (only with -P)\n"
590 " -p, -- ignored for backwards compatibility\n"
591 " -P, -- use a pattern to verify read data\n"
592 " -q, -- quiet mode, do not show I/O statistics\n"
593 " -s, -- start offset for pattern verification (only with -P)\n"
594 " -v, -- dump buffer to standard output\n"
598 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
600 static const cmdinfo_t read_cmd
= {
606 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
607 .oneline
= "reads a number of bytes at a specified offset",
611 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
613 struct timeval t1
, t2
;
614 bool Cflag
= false, qflag
= false, vflag
= false;
615 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
620 /* Some compilers get confused and warn if this is not initialized. */
623 int64_t pattern_offset
= 0, pattern_count
= 0;
625 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
635 pattern_count
= cvtnum(optarg
);
636 if (pattern_count
< 0) {
637 print_cvtnum_err(pattern_count
, optarg
);
642 /* Ignored for backwards compatibility */
646 pattern
= parse_pattern(optarg
);
656 pattern_offset
= cvtnum(optarg
);
657 if (pattern_offset
< 0) {
658 print_cvtnum_err(pattern_offset
, optarg
);
666 return qemuio_command_usage(&read_cmd
);
670 if (optind
!= argc
- 2) {
671 return qemuio_command_usage(&read_cmd
);
674 offset
= cvtnum(argv
[optind
]);
676 print_cvtnum_err(offset
, argv
[optind
]);
681 count
= cvtnum(argv
[optind
]);
683 print_cvtnum_err(count
, argv
[optind
]);
685 } else if (count
> SIZE_MAX
) {
686 printf("length cannot exceed %" PRIu64
", given %s\n",
687 (uint64_t) SIZE_MAX
, argv
[optind
]);
691 if (!Pflag
&& (lflag
|| sflag
)) {
692 return qemuio_command_usage(&read_cmd
);
696 pattern_count
= count
- pattern_offset
;
699 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
700 printf("pattern verification range exceeds end of read data\n");
705 if (offset
& 0x1ff) {
706 printf("offset %" PRId64
" is not sector aligned\n",
711 printf("count %"PRId64
" is not sector aligned\n",
717 buf
= qemu_io_alloc(blk
, count
, 0xab);
719 gettimeofday(&t1
, NULL
);
721 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
723 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
725 gettimeofday(&t2
, NULL
);
728 printf("read failed: %s\n", strerror(-cnt
));
733 void *cmp_buf
= g_malloc(pattern_count
);
734 memset(cmp_buf
, pattern
, pattern_count
);
735 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
736 printf("Pattern verification failed at offset %"
737 PRId64
", %"PRId64
" bytes\n",
738 offset
+ pattern_offset
, pattern_count
);
748 dump_buffer(buf
, offset
, count
);
751 /* Finally, report back -- -C gives a parsable format */
753 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
761 static void readv_help(void)
765 " reads a range of bytes from the given offset into multiple buffers\n"
768 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
770 " Reads a segment of the currently open file, optionally dumping it to the\n"
771 " standard output stream (with -v option) for subsequent inspection.\n"
772 " Uses multiple iovec buffers if more than one byte range is specified.\n"
773 " -C, -- report statistics in a machine parsable format\n"
774 " -P, -- use a pattern to verify read data\n"
775 " -v, -- dump buffer to standard output\n"
776 " -q, -- quiet mode, do not show I/O statistics\n"
780 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
782 static const cmdinfo_t readv_cmd
= {
787 .args
= "[-Cqv] [-P pattern] off len [len..]",
788 .oneline
= "reads a number of bytes at a specified offset",
792 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
794 struct timeval t1
, t2
;
795 bool Cflag
= false, qflag
= false, vflag
= false;
799 /* Some compilers get confused and warn if this is not initialized. */
806 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
813 pattern
= parse_pattern(optarg
);
825 return qemuio_command_usage(&readv_cmd
);
829 if (optind
> argc
- 2) {
830 return qemuio_command_usage(&readv_cmd
);
834 offset
= cvtnum(argv
[optind
]);
836 print_cvtnum_err(offset
, argv
[optind
]);
841 nr_iov
= argc
- optind
;
842 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
847 gettimeofday(&t1
, NULL
);
848 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
849 gettimeofday(&t2
, NULL
);
852 printf("readv failed: %s\n", strerror(-cnt
));
857 void *cmp_buf
= g_malloc(qiov
.size
);
858 memset(cmp_buf
, pattern
, qiov
.size
);
859 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
860 printf("Pattern verification failed at offset %"
861 PRId64
", %zd bytes\n", offset
, qiov
.size
);
871 dump_buffer(buf
, offset
, qiov
.size
);
874 /* Finally, report back -- -C gives a parsable format */
876 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
879 qemu_iovec_destroy(&qiov
);
884 static void write_help(void)
888 " writes a range of bytes from the given offset\n"
891 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
893 " Writes into a segment of the currently open file, using a buffer\n"
894 " filled with a set pattern (0xcdcdcdcd).\n"
895 " -b, -- write to the VM state rather than the virtual disk\n"
896 " -c, -- write compressed data with blk_write_compressed\n"
897 " -f, -- use Force Unit Access semantics\n"
898 " -p, -- ignored for backwards compatibility\n"
899 " -P, -- use different pattern to fill file\n"
900 " -C, -- report statistics in a machine parsable format\n"
901 " -q, -- quiet mode, do not show I/O statistics\n"
902 " -u, -- with -z, allow unmapping\n"
903 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
907 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
909 static const cmdinfo_t write_cmd
= {
915 .args
= "[-bcCfquz] [-P pattern] off len",
916 .oneline
= "writes a number of bytes at a specified offset",
920 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
922 struct timeval t1
, t2
;
923 bool Cflag
= false, qflag
= false, bflag
= false;
924 bool Pflag
= false, zflag
= false, cflag
= false;
930 /* Some compilers get confused and warn if this is not initialized. */
934 while ((c
= getopt(argc
, argv
, "bcCfpP:quz")) != -1) {
946 flags
|= BDRV_REQ_FUA
;
949 /* Ignored for backwards compatibility */
953 pattern
= parse_pattern(optarg
);
962 flags
|= BDRV_REQ_MAY_UNMAP
;
968 return qemuio_command_usage(&write_cmd
);
972 if (optind
!= argc
- 2) {
973 return qemuio_command_usage(&write_cmd
);
976 if (bflag
&& zflag
) {
977 printf("-b and -z cannot be specified at the same time\n");
981 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
982 printf("-f and -b or -c cannot be specified at the same time\n");
986 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
987 printf("-u requires -z to be specified\n");
991 if (zflag
&& Pflag
) {
992 printf("-z and -P cannot be specified at the same time\n");
996 offset
= cvtnum(argv
[optind
]);
998 print_cvtnum_err(offset
, argv
[optind
]);
1003 count
= cvtnum(argv
[optind
]);
1005 print_cvtnum_err(count
, argv
[optind
]);
1007 } else if (count
> SIZE_MAX
) {
1008 printf("length cannot exceed %" PRIu64
", given %s\n",
1009 (uint64_t) SIZE_MAX
, argv
[optind
]);
1013 if (bflag
|| cflag
) {
1014 if (offset
& 0x1ff) {
1015 printf("offset %" PRId64
" is not sector aligned\n",
1020 if (count
& 0x1ff) {
1021 printf("count %"PRId64
" is not sector aligned\n",
1028 buf
= qemu_io_alloc(blk
, count
, pattern
);
1031 gettimeofday(&t1
, NULL
);
1033 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1035 cnt
= do_co_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1037 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1039 cnt
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1041 gettimeofday(&t2
, NULL
);
1044 printf("write failed: %s\n", strerror(-cnt
));
1052 /* Finally, report back -- -C gives a parsable format */
1054 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1069 " writes a range of bytes from the given offset source from multiple buffers\n"
1072 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1074 " Writes into a segment of the currently open file, using a buffer\n"
1075 " filled with a set pattern (0xcdcdcdcd).\n"
1076 " -P, -- use different pattern to fill file\n"
1077 " -C, -- report statistics in a machine parsable format\n"
1078 " -f, -- use Force Unit Access semantics\n"
1079 " -q, -- quiet mode, do not show I/O statistics\n"
1083 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1085 static const cmdinfo_t writev_cmd
= {
1090 .args
= "[-Cfq] [-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(BlockBackend
*blk
, int argc
, char **argv
)
1097 struct timeval t1
, t2
;
1098 bool Cflag
= false, qflag
= false;
1103 /* Some compilers get confused and warn if this is not initialized. */
1109 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1115 flags
|= BDRV_REQ_FUA
;
1121 pattern
= parse_pattern(optarg
);
1127 return qemuio_command_usage(&writev_cmd
);
1131 if (optind
> argc
- 2) {
1132 return qemuio_command_usage(&writev_cmd
);
1135 offset
= cvtnum(argv
[optind
]);
1137 print_cvtnum_err(offset
, argv
[optind
]);
1142 nr_iov
= argc
- optind
;
1143 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1148 gettimeofday(&t1
, NULL
);
1149 cnt
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1150 gettimeofday(&t2
, NULL
);
1153 printf("writev failed: %s\n", strerror(-cnt
));
1161 /* Finally, report back -- -C gives a parsable format */
1163 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1165 qemu_iovec_destroy(&qiov
);
1180 BlockAcctCookie acct
;
1185 static void aio_write_done(void *opaque
, int ret
)
1187 struct aio_ctx
*ctx
= opaque
;
1190 gettimeofday(&t2
, NULL
);
1194 printf("aio_write failed: %s\n", strerror(-ret
));
1195 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1199 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1205 /* Finally, report back -- -C gives a parsable format */
1206 t2
= tsub(t2
, ctx
->t1
);
1207 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1208 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1211 qemu_io_free(ctx
->buf
);
1212 qemu_iovec_destroy(&ctx
->qiov
);
1217 static void aio_read_done(void *opaque
, int ret
)
1219 struct aio_ctx
*ctx
= opaque
;
1222 gettimeofday(&t2
, NULL
);
1225 printf("readv failed: %s\n", strerror(-ret
));
1226 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1231 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1233 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1234 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1235 printf("Pattern verification failed at offset %"
1236 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1241 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1248 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1251 /* Finally, report back -- -C gives a parsable format */
1252 t2
= tsub(t2
, ctx
->t1
);
1253 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1254 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1256 qemu_io_free(ctx
->buf
);
1257 qemu_iovec_destroy(&ctx
->qiov
);
1261 static void aio_read_help(void)
1265 " asynchronously reads a range of bytes from the given offset\n"
1268 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1270 " Reads a segment of the currently open file, optionally dumping it to the\n"
1271 " standard output stream (with -v option) for subsequent inspection.\n"
1272 " The read is performed asynchronously and the aio_flush command must be\n"
1273 " used to ensure all outstanding aio requests have been completed.\n"
1274 " -C, -- report statistics in a machine parsable format\n"
1275 " -P, -- use a pattern to verify read data\n"
1276 " -i, -- treat request as invalid, for exercising stats\n"
1277 " -v, -- dump buffer to standard output\n"
1278 " -q, -- quiet mode, do not show I/O statistics\n"
1282 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1284 static const cmdinfo_t aio_read_cmd
= {
1286 .cfunc
= aio_read_f
,
1289 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1290 .oneline
= "asynchronously reads a number of bytes",
1291 .help
= aio_read_help
,
1294 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1297 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1300 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1307 ctx
->pattern
= parse_pattern(optarg
);
1308 if (ctx
->pattern
< 0) {
1314 printf("injecting invalid read request\n");
1315 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1326 return qemuio_command_usage(&aio_read_cmd
);
1330 if (optind
> argc
- 2) {
1332 return qemuio_command_usage(&aio_read_cmd
);
1335 ctx
->offset
= cvtnum(argv
[optind
]);
1336 if (ctx
->offset
< 0) {
1337 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1343 nr_iov
= argc
- optind
;
1344 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1345 if (ctx
->buf
== NULL
) {
1346 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1351 gettimeofday(&ctx
->t1
, NULL
);
1352 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1354 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1358 static void aio_write_help(void)
1362 " asynchronously writes a range of bytes from the given offset source\n"
1363 " from multiple buffers\n"
1366 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1368 " Writes into a segment of the currently open file, using a buffer\n"
1369 " filled with a set pattern (0xcdcdcdcd).\n"
1370 " The write is performed asynchronously and the aio_flush command must be\n"
1371 " used to ensure all outstanding aio requests have been completed.\n"
1372 " -P, -- use different pattern to fill file\n"
1373 " -C, -- report statistics in a machine parsable format\n"
1374 " -f, -- use Force Unit Access semantics\n"
1375 " -i, -- treat request as invalid, for exercising stats\n"
1376 " -q, -- quiet mode, do not show I/O statistics\n"
1377 " -u, -- with -z, allow unmapping\n"
1378 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1382 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1384 static const cmdinfo_t aio_write_cmd
= {
1385 .name
= "aio_write",
1386 .cfunc
= aio_write_f
,
1389 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1390 .oneline
= "asynchronously writes a number of bytes",
1391 .help
= aio_write_help
,
1394 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1398 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1402 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1408 flags
|= BDRV_REQ_FUA
;
1414 flags
|= BDRV_REQ_MAY_UNMAP
;
1417 pattern
= parse_pattern(optarg
);
1424 printf("injecting invalid write request\n");
1425 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1433 return qemuio_command_usage(&aio_write_cmd
);
1437 if (optind
> argc
- 2) {
1439 return qemuio_command_usage(&aio_write_cmd
);
1442 if (ctx
->zflag
&& optind
!= argc
- 2) {
1443 printf("-z supports only a single length parameter\n");
1448 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1449 printf("-u requires -z to be specified\n");
1454 if (ctx
->zflag
&& ctx
->Pflag
) {
1455 printf("-z and -P cannot be specified at the same time\n");
1460 ctx
->offset
= cvtnum(argv
[optind
]);
1461 if (ctx
->offset
< 0) {
1462 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1469 int64_t count
= cvtnum(argv
[optind
]);
1471 print_cvtnum_err(count
, argv
[optind
]);
1476 ctx
->qiov
.size
= count
;
1477 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1480 nr_iov
= argc
- optind
;
1481 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1483 if (ctx
->buf
== NULL
) {
1484 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1489 gettimeofday(&ctx
->t1
, NULL
);
1490 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1493 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1499 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1501 BlockAcctCookie cookie
;
1502 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1504 block_acct_done(blk_get_stats(blk
), &cookie
);
1508 static const cmdinfo_t aio_flush_cmd
= {
1509 .name
= "aio_flush",
1510 .cfunc
= aio_flush_f
,
1511 .oneline
= "completes all outstanding aio requests"
1514 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1520 static const cmdinfo_t flush_cmd
= {
1524 .oneline
= "flush all in-core file state to disk",
1527 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1532 offset
= cvtnum(argv
[1]);
1534 print_cvtnum_err(offset
, argv
[1]);
1538 ret
= blk_truncate(blk
, offset
);
1540 printf("truncate: %s\n", strerror(-ret
));
1547 static const cmdinfo_t truncate_cmd
= {
1550 .cfunc
= truncate_f
,
1554 .oneline
= "truncates the current file at the given offset",
1557 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1562 size
= blk_getlength(blk
);
1564 printf("getlength: %s\n", strerror(-size
));
1568 cvtstr(size
, s1
, sizeof(s1
));
1574 static const cmdinfo_t length_cmd
= {
1578 .oneline
= "gets the length of the current file",
1582 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1584 BlockDriverState
*bs
= blk_bs(blk
);
1585 BlockDriverInfo bdi
;
1586 ImageInfoSpecific
*spec_info
;
1587 char s1
[64], s2
[64];
1590 if (bs
->drv
&& bs
->drv
->format_name
) {
1591 printf("format name: %s\n", bs
->drv
->format_name
);
1593 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1594 printf("format name: %s\n", bs
->drv
->protocol_name
);
1597 ret
= bdrv_get_info(bs
, &bdi
);
1602 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1603 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1605 printf("cluster size: %s\n", s1
);
1606 printf("vm state offset: %s\n", s2
);
1608 spec_info
= bdrv_get_specific_info(bs
);
1610 printf("Format specific information:\n");
1611 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1612 qapi_free_ImageInfoSpecific(spec_info
);
1620 static const cmdinfo_t info_cmd
= {
1624 .oneline
= "prints information about the current file",
1627 static void discard_help(void)
1631 " discards a range of bytes from the given offset\n"
1634 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1636 " Discards a segment of the currently open file.\n"
1637 " -C, -- report statistics in a machine parsable format\n"
1638 " -q, -- quiet mode, do not show I/O statistics\n"
1642 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1644 static const cmdinfo_t discard_cmd
= {
1650 .args
= "[-Cq] off len",
1651 .oneline
= "discards a number of bytes at a specified offset",
1652 .help
= discard_help
,
1655 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1657 struct timeval t1
, t2
;
1658 bool Cflag
= false, qflag
= false;
1660 int64_t offset
, count
;
1662 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1671 return qemuio_command_usage(&discard_cmd
);
1675 if (optind
!= argc
- 2) {
1676 return qemuio_command_usage(&discard_cmd
);
1679 offset
= cvtnum(argv
[optind
]);
1681 print_cvtnum_err(offset
, argv
[optind
]);
1686 count
= cvtnum(argv
[optind
]);
1688 print_cvtnum_err(count
, argv
[optind
]);
1690 } else if (count
>> BDRV_SECTOR_BITS
> BDRV_REQUEST_MAX_SECTORS
) {
1691 printf("length cannot exceed %"PRIu64
", given %s\n",
1692 (uint64_t)BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
,
1697 gettimeofday(&t1
, NULL
);
1698 ret
= blk_pdiscard(blk
, offset
, count
);
1699 gettimeofday(&t2
, NULL
);
1702 printf("discard failed: %s\n", strerror(-ret
));
1706 /* Finally, report back -- -C gives a parsable format */
1709 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1716 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1718 BlockDriverState
*bs
= blk_bs(blk
);
1719 int64_t offset
, sector_num
, nb_sectors
, remaining
;
1724 offset
= cvtnum(argv
[1]);
1726 print_cvtnum_err(offset
, argv
[1]);
1728 } else if (offset
& 0x1ff) {
1729 printf("offset %" PRId64
" is not sector aligned\n",
1735 nb_sectors
= cvtnum(argv
[2]);
1736 if (nb_sectors
< 0) {
1737 print_cvtnum_err(nb_sectors
, argv
[2]);
1739 } else if (nb_sectors
> INT_MAX
) {
1740 printf("length argument cannot exceed %d, given %s\n",
1748 remaining
= nb_sectors
;
1750 sector_num
= offset
>> 9;
1752 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1754 printf("is_allocated failed: %s\n", strerror(-ret
));
1763 nb_sectors
-= remaining
;
1768 cvtstr(offset
, s1
, sizeof(s1
));
1770 printf("%"PRId64
"/%"PRId64
" sectors allocated at offset %s\n",
1771 sum_alloc
, nb_sectors
, s1
);
1775 static const cmdinfo_t alloc_cmd
= {
1781 .args
= "off [sectors]",
1782 .oneline
= "checks if a sector is present in the file",
1786 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1787 int64_t nb_sectors
, int64_t *pnum
)
1789 int num
, num_checked
;
1792 num_checked
= MIN(nb_sectors
, INT_MAX
);
1793 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1801 while (nb_sectors
> 0 && ret
== firstret
) {
1805 num_checked
= MIN(nb_sectors
, INT_MAX
);
1806 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1807 if (ret
== firstret
&& num
) {
1817 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1820 int64_t nb_sectors
, total_sectors
;
1827 total_sectors
= blk_nb_sectors(blk
);
1828 if (total_sectors
< 0) {
1829 error_report("Failed to query image length: %s",
1830 strerror(-total_sectors
));
1834 nb_sectors
= total_sectors
;
1837 ret
= map_is_allocated(blk_bs(blk
), offset
, nb_sectors
, &num
);
1839 error_report("Failed to get allocation status: %s", strerror(-ret
));
1842 error_report("Unexpected end of image");
1846 retstr
= ret
? " allocated" : "not allocated";
1847 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1848 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1849 "at offset %s (%d)\n",
1850 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
1854 } while (offset
< total_sectors
);
1859 static const cmdinfo_t map_cmd
= {
1865 .oneline
= "prints the allocated areas of a file",
1868 static void reopen_help(void)
1872 " Changes the open options of an already opened image\n"
1875 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1877 " -r, -- Reopen the image read-only\n"
1878 " -c, -- Change the cache mode to the given value\n"
1879 " -o, -- Changes block driver options (cf. 'open' command)\n"
1883 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
1885 static QemuOptsList reopen_opts
= {
1887 .merge_lists
= true,
1888 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
1890 /* no elements => accept any params */
1891 { /* end of list */ }
1895 static const cmdinfo_t reopen_cmd
= {
1900 .args
= "[-r] [-c cache] [-o options]",
1901 .oneline
= "reopens an image with new options",
1902 .help
= reopen_help
,
1905 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
1907 BlockDriverState
*bs
= blk_bs(blk
);
1911 int flags
= bs
->open_flags
;
1912 bool writethrough
= !blk_enable_write_cache(blk
);
1914 BlockReopenQueue
*brq
;
1915 Error
*local_err
= NULL
;
1917 while ((c
= getopt(argc
, argv
, "c:o:r")) != -1) {
1920 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
1921 error_report("Invalid cache option: %s", optarg
);
1926 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
1927 qemu_opts_reset(&reopen_opts
);
1932 flags
&= ~BDRV_O_RDWR
;
1935 qemu_opts_reset(&reopen_opts
);
1936 return qemuio_command_usage(&reopen_cmd
);
1940 if (optind
!= argc
) {
1941 qemu_opts_reset(&reopen_opts
);
1942 return qemuio_command_usage(&reopen_cmd
);
1945 if (writethrough
!= blk_enable_write_cache(blk
) &&
1946 blk_get_attached_dev(blk
))
1948 error_report("Cannot change cache.writeback: Device attached");
1949 qemu_opts_reset(&reopen_opts
);
1953 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
1954 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
1955 qemu_opts_reset(&reopen_opts
);
1957 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
1958 bdrv_reopen_multiple(bdrv_get_aio_context(bs
), brq
, &local_err
);
1960 error_report_err(local_err
);
1962 blk_set_enable_write_cache(blk
, !writethrough
);
1968 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
1972 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
1974 printf("Could not set breakpoint: %s\n", strerror(-ret
));
1980 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
1984 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
1986 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
1992 static const cmdinfo_t break_cmd
= {
1997 .args
= "event tag",
1998 .oneline
= "sets a breakpoint on event and tags the stopped "
2002 static const cmdinfo_t remove_break_cmd
= {
2003 .name
= "remove_break",
2006 .cfunc
= remove_break_f
,
2008 .oneline
= "remove a breakpoint by tag",
2011 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2015 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2017 printf("Could not resume request: %s\n", strerror(-ret
));
2023 static const cmdinfo_t resume_cmd
= {
2029 .oneline
= "resumes the request tagged as tag",
2032 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2034 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2035 aio_poll(blk_get_aio_context(blk
), true);
2041 static const cmdinfo_t wait_break_cmd
= {
2042 .name
= "wait_break",
2045 .cfunc
= wait_break_f
,
2047 .oneline
= "waits for the suspension of a request",
2050 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2055 static const cmdinfo_t abort_cmd
= {
2058 .flags
= CMD_NOFILE_OK
,
2059 .oneline
= "simulate a program crash using abort(3)",
2062 static void sigraise_help(void)
2066 " raises the given signal\n"
2069 " 'sigraise %i' - raises SIGTERM\n"
2071 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2072 " given to sigraise.\n"
2076 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2078 static const cmdinfo_t sigraise_cmd
= {
2080 .cfunc
= sigraise_f
,
2083 .flags
= CMD_NOFILE_OK
,
2085 .oneline
= "raises a signal",
2086 .help
= sigraise_help
,
2089 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2091 int64_t sig
= cvtnum(argv
[1]);
2093 print_cvtnum_err(sig
, argv
[1]);
2095 } else if (sig
> NSIG
) {
2096 printf("signal argument '%s' is too large to be a valid signal\n",
2101 /* Using raise() to kill this process does not necessarily flush all open
2102 * streams. At least stdout and stderr (although the latter should be
2103 * non-buffered anyway) should be flushed, though. */
2111 static void sleep_cb(void *opaque
)
2113 bool *expired
= opaque
;
2117 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2121 struct QEMUTimer
*timer
;
2122 bool expired
= false;
2124 ms
= strtol(argv
[1], &endptr
, 0);
2125 if (ms
< 0 || *endptr
!= '\0') {
2126 printf("%s is not a valid number\n", argv
[1]);
2130 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2131 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2134 main_loop_wait(false);
2142 static const cmdinfo_t sleep_cmd
= {
2147 .flags
= CMD_NOFILE_OK
,
2148 .oneline
= "waits for the given value in milliseconds",
2151 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2156 printf("%s ", ct
->name
);
2158 printf("(or %s) ", ct
->altname
);
2163 printf("%s ", ct
->args
);
2165 printf("-- %s\n", ct
->oneline
);
2168 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2170 help_oneline(cmd
, ct
);
2176 static void help_all(void)
2178 const cmdinfo_t
*ct
;
2180 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2181 help_oneline(ct
->name
, ct
);
2183 printf("\nUse 'help commandname' for extended help.\n");
2186 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2188 const cmdinfo_t
*ct
;
2195 ct
= find_command(argv
[1]);
2197 printf("command %s not found\n", argv
[1]);
2201 help_onecmd(argv
[1], ct
);
2205 static const cmdinfo_t help_cmd
= {
2211 .flags
= CMD_FLAG_GLOBAL
,
2212 .args
= "[command]",
2213 .oneline
= "help for one or all commands",
2216 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2220 const cmdinfo_t
*ct
;
2225 input
= g_strdup(cmd
);
2226 v
= breakline(input
, &c
);
2228 ct
= find_command(v
[0]);
2230 ctx
= blk
? blk_get_aio_context(blk
) : qemu_get_aio_context();
2231 aio_context_acquire(ctx
);
2232 done
= command(blk
, ct
, c
, v
);
2233 aio_context_release(ctx
);
2235 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2244 static void __attribute((constructor
)) init_qemuio_commands(void)
2246 /* initialize commands */
2247 qemuio_add_command(&help_cmd
);
2248 qemuio_add_command(&read_cmd
);
2249 qemuio_add_command(&readv_cmd
);
2250 qemuio_add_command(&write_cmd
);
2251 qemuio_add_command(&writev_cmd
);
2252 qemuio_add_command(&aio_read_cmd
);
2253 qemuio_add_command(&aio_write_cmd
);
2254 qemuio_add_command(&aio_flush_cmd
);
2255 qemuio_add_command(&flush_cmd
);
2256 qemuio_add_command(&truncate_cmd
);
2257 qemuio_add_command(&length_cmd
);
2258 qemuio_add_command(&info_cmd
);
2259 qemuio_add_command(&discard_cmd
);
2260 qemuio_add_command(&alloc_cmd
);
2261 qemuio_add_command(&map_cmd
);
2262 qemuio_add_command(&reopen_cmd
);
2263 qemuio_add_command(&break_cmd
);
2264 qemuio_add_command(&remove_break_cmd
);
2265 qemuio_add_command(&resume_cmd
);
2266 qemuio_add_command(&wait_break_cmd
);
2267 qemuio_add_command(&abort_cmd
);
2268 qemuio_add_command(&sleep_cmd
);
2269 qemuio_add_command(&sigraise_cmd
);