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 "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
, bool 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
);
402 qemu_iovec_init(qiov
, nr_iov
);
404 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
406 for (i
= 0; i
< nr_iov
; i
++) {
407 qemu_iovec_add(qiov
, p
, sizes
[i
]);
416 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
417 int64_t count
, int64_t *total
)
419 if (count
> INT_MAX
) {
423 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, count
);
430 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
431 int64_t count
, int flags
, int64_t *total
)
433 if (count
> INT_MAX
) {
437 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, count
, flags
);
454 static void coroutine_fn
co_write_zeroes_entry(void *opaque
)
456 CoWriteZeroes
*data
= opaque
;
458 data
->ret
= blk_co_write_zeroes(data
->blk
, data
->offset
, data
->count
,
462 *data
->total
= data
->ret
;
466 *data
->total
= data
->count
;
469 static int do_co_write_zeroes(BlockBackend
*blk
, int64_t offset
, int64_t count
,
470 int flags
, int64_t *total
)
473 CoWriteZeroes data
= {
482 if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
486 co
= qemu_coroutine_create(co_write_zeroes_entry
);
487 qemu_coroutine_enter(co
, &data
);
489 aio_poll(blk_get_aio_context(blk
), true);
498 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
499 int64_t count
, int64_t *total
)
503 if (count
>> 9 > INT_MAX
) {
507 ret
= blk_write_compressed(blk
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
515 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
516 int64_t count
, int64_t *total
)
518 if (count
> INT_MAX
) {
522 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
529 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
530 int64_t count
, int64_t *total
)
532 if (count
> INT_MAX
) {
536 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
543 #define NOT_DONE 0x7fffffff
544 static void aio_rw_done(void *opaque
, int ret
)
546 *(int *)opaque
= ret
;
549 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
550 int64_t offset
, int *total
)
552 int async_ret
= NOT_DONE
;
554 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
555 while (async_ret
== NOT_DONE
) {
556 main_loop_wait(false);
560 return async_ret
< 0 ? async_ret
: 1;
563 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
564 int64_t offset
, int flags
, int *total
)
566 int async_ret
= NOT_DONE
;
568 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
569 while (async_ret
== NOT_DONE
) {
570 main_loop_wait(false);
574 return async_ret
< 0 ? async_ret
: 1;
577 static void read_help(void)
581 " reads a range of bytes from the given offset\n"
584 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
586 " Reads a segment of the currently open file, optionally dumping it to the\n"
587 " standard output stream (with -v option) for subsequent inspection.\n"
588 " -b, -- read from the VM state rather than the virtual disk\n"
589 " -C, -- report statistics in a machine parsable format\n"
590 " -l, -- length for pattern verification (only with -P)\n"
591 " -p, -- ignored for backwards compatibility\n"
592 " -P, -- use a pattern to verify read data\n"
593 " -q, -- quiet mode, do not show I/O statistics\n"
594 " -s, -- start offset for pattern verification (only with -P)\n"
595 " -v, -- dump buffer to standard output\n"
599 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
601 static const cmdinfo_t read_cmd
= {
607 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
608 .oneline
= "reads a number of bytes at a specified offset",
612 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
614 struct timeval t1
, t2
;
615 bool Cflag
= false, qflag
= false, vflag
= false;
616 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
621 /* Some compilers get confused and warn if this is not initialized. */
624 int64_t pattern_offset
= 0, pattern_count
= 0;
626 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
636 pattern_count
= cvtnum(optarg
);
637 if (pattern_count
< 0) {
638 print_cvtnum_err(pattern_count
, optarg
);
643 /* Ignored for backwards compatibility */
647 pattern
= parse_pattern(optarg
);
657 pattern_offset
= cvtnum(optarg
);
658 if (pattern_offset
< 0) {
659 print_cvtnum_err(pattern_offset
, optarg
);
667 return qemuio_command_usage(&read_cmd
);
671 if (optind
!= argc
- 2) {
672 return qemuio_command_usage(&read_cmd
);
675 offset
= cvtnum(argv
[optind
]);
677 print_cvtnum_err(offset
, argv
[optind
]);
682 count
= cvtnum(argv
[optind
]);
684 print_cvtnum_err(count
, argv
[optind
]);
686 } else if (count
> SIZE_MAX
) {
687 printf("length cannot exceed %" PRIu64
", given %s\n",
688 (uint64_t) SIZE_MAX
, argv
[optind
]);
692 if (!Pflag
&& (lflag
|| sflag
)) {
693 return qemuio_command_usage(&read_cmd
);
697 pattern_count
= count
- pattern_offset
;
700 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
701 printf("pattern verification range exceeds end of read data\n");
706 if (offset
& 0x1ff) {
707 printf("offset %" PRId64
" is not sector aligned\n",
712 printf("count %"PRId64
" is not sector aligned\n",
718 buf
= qemu_io_alloc(blk
, count
, 0xab);
720 gettimeofday(&t1
, NULL
);
722 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
724 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
726 gettimeofday(&t2
, NULL
);
729 printf("read failed: %s\n", strerror(-cnt
));
734 void *cmp_buf
= g_malloc(pattern_count
);
735 memset(cmp_buf
, pattern
, pattern_count
);
736 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
737 printf("Pattern verification failed at offset %"
738 PRId64
", %"PRId64
" bytes\n",
739 offset
+ pattern_offset
, pattern_count
);
749 dump_buffer(buf
, offset
, count
);
752 /* Finally, report back -- -C gives a parsable format */
754 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
762 static void readv_help(void)
766 " reads a range of bytes from the given offset into multiple buffers\n"
769 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
771 " Reads a segment of the currently open file, optionally dumping it to the\n"
772 " standard output stream (with -v option) for subsequent inspection.\n"
773 " Uses multiple iovec buffers if more than one byte range is specified.\n"
774 " -C, -- report statistics in a machine parsable format\n"
775 " -P, -- use a pattern to verify read data\n"
776 " -v, -- dump buffer to standard output\n"
777 " -q, -- quiet mode, do not show I/O statistics\n"
781 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
783 static const cmdinfo_t readv_cmd
= {
788 .args
= "[-Cqv] [-P pattern] off len [len..]",
789 .oneline
= "reads a number of bytes at a specified offset",
793 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
795 struct timeval t1
, t2
;
796 bool Cflag
= false, qflag
= false, vflag
= false;
800 /* Some compilers get confused and warn if this is not initialized. */
807 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
814 pattern
= parse_pattern(optarg
);
826 return qemuio_command_usage(&readv_cmd
);
830 if (optind
> argc
- 2) {
831 return qemuio_command_usage(&readv_cmd
);
835 offset
= cvtnum(argv
[optind
]);
837 print_cvtnum_err(offset
, argv
[optind
]);
842 nr_iov
= argc
- optind
;
843 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
848 gettimeofday(&t1
, NULL
);
849 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
850 gettimeofday(&t2
, NULL
);
853 printf("readv failed: %s\n", strerror(-cnt
));
858 void *cmp_buf
= g_malloc(qiov
.size
);
859 memset(cmp_buf
, pattern
, qiov
.size
);
860 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
861 printf("Pattern verification failed at offset %"
862 PRId64
", %zd bytes\n", offset
, qiov
.size
);
872 dump_buffer(buf
, offset
, qiov
.size
);
875 /* Finally, report back -- -C gives a parsable format */
877 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
880 qemu_iovec_destroy(&qiov
);
885 static void write_help(void)
889 " writes a range of bytes from the given offset\n"
892 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
894 " Writes into a segment of the currently open file, using a buffer\n"
895 " filled with a set pattern (0xcdcdcdcd).\n"
896 " -b, -- write to the VM state rather than the virtual disk\n"
897 " -c, -- write compressed data with blk_write_compressed\n"
898 " -f, -- use Force Unit Access semantics\n"
899 " -p, -- ignored for backwards compatibility\n"
900 " -P, -- use different pattern to fill file\n"
901 " -C, -- report statistics in a machine parsable format\n"
902 " -q, -- quiet mode, do not show I/O statistics\n"
903 " -u, -- with -z, allow unmapping\n"
904 " -z, -- write zeroes using blk_co_write_zeroes\n"
908 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
910 static const cmdinfo_t write_cmd
= {
916 .args
= "[-bcCfquz] [-P pattern] off len",
917 .oneline
= "writes a number of bytes at a specified offset",
921 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
923 struct timeval t1
, t2
;
924 bool Cflag
= false, qflag
= false, bflag
= false;
925 bool Pflag
= false, zflag
= false, cflag
= false;
931 /* Some compilers get confused and warn if this is not initialized. */
935 while ((c
= getopt(argc
, argv
, "bcCfpP:quz")) != -1) {
947 flags
|= BDRV_REQ_FUA
;
950 /* Ignored for backwards compatibility */
954 pattern
= parse_pattern(optarg
);
963 flags
|= BDRV_REQ_MAY_UNMAP
;
969 return qemuio_command_usage(&write_cmd
);
973 if (optind
!= argc
- 2) {
974 return qemuio_command_usage(&write_cmd
);
977 if (bflag
&& zflag
) {
978 printf("-b and -z cannot be specified at the same time\n");
982 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
983 printf("-f and -b or -c cannot be specified at the same time\n");
987 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
988 printf("-u requires -z to be specified\n");
992 if (zflag
&& Pflag
) {
993 printf("-z and -P cannot be specified at the same time\n");
997 offset
= cvtnum(argv
[optind
]);
999 print_cvtnum_err(offset
, argv
[optind
]);
1004 count
= cvtnum(argv
[optind
]);
1006 print_cvtnum_err(count
, argv
[optind
]);
1008 } else if (count
> SIZE_MAX
) {
1009 printf("length cannot exceed %" PRIu64
", given %s\n",
1010 (uint64_t) SIZE_MAX
, argv
[optind
]);
1014 if (bflag
|| cflag
) {
1015 if (offset
& 0x1ff) {
1016 printf("offset %" PRId64
" is not sector aligned\n",
1021 if (count
& 0x1ff) {
1022 printf("count %"PRId64
" is not sector aligned\n",
1029 buf
= qemu_io_alloc(blk
, count
, pattern
);
1032 gettimeofday(&t1
, NULL
);
1034 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1036 cnt
= do_co_write_zeroes(blk
, offset
, count
, flags
, &total
);
1038 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1040 cnt
= do_pwrite(blk
, buf
, offset
, count
, flags
, &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 " 'writev 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 " -f, -- use Force Unit Access semantics\n"
1080 " -q, -- quiet mode, do not show I/O statistics\n"
1084 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1086 static const cmdinfo_t writev_cmd
= {
1091 .args
= "[-Cfq] [-P pattern] off len [len..]",
1092 .oneline
= "writes a number of bytes at a specified offset",
1093 .help
= writev_help
,
1096 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1098 struct timeval t1
, t2
;
1099 bool Cflag
= false, qflag
= false;
1104 /* Some compilers get confused and warn if this is not initialized. */
1110 while ((c
= getopt(argc
, argv
, "CqP:")) != -1) {
1116 flags
|= BDRV_REQ_FUA
;
1122 pattern
= parse_pattern(optarg
);
1128 return qemuio_command_usage(&writev_cmd
);
1132 if (optind
> argc
- 2) {
1133 return qemuio_command_usage(&writev_cmd
);
1136 offset
= cvtnum(argv
[optind
]);
1138 print_cvtnum_err(offset
, argv
[optind
]);
1143 nr_iov
= argc
- optind
;
1144 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1149 gettimeofday(&t1
, NULL
);
1150 cnt
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1151 gettimeofday(&t2
, NULL
);
1154 printf("writev failed: %s\n", strerror(-cnt
));
1162 /* Finally, report back -- -C gives a parsable format */
1164 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1166 qemu_iovec_destroy(&qiov
);
1181 BlockAcctCookie acct
;
1186 static void aio_write_done(void *opaque
, int ret
)
1188 struct aio_ctx
*ctx
= opaque
;
1191 gettimeofday(&t2
, NULL
);
1195 printf("aio_write failed: %s\n", strerror(-ret
));
1196 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1200 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1206 /* Finally, report back -- -C gives a parsable format */
1207 t2
= tsub(t2
, ctx
->t1
);
1208 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1209 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1212 qemu_io_free(ctx
->buf
);
1213 qemu_iovec_destroy(&ctx
->qiov
);
1218 static void aio_read_done(void *opaque
, int ret
)
1220 struct aio_ctx
*ctx
= opaque
;
1223 gettimeofday(&t2
, NULL
);
1226 printf("readv failed: %s\n", strerror(-ret
));
1227 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1232 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1234 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1235 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1236 printf("Pattern verification failed at offset %"
1237 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1242 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1249 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1252 /* Finally, report back -- -C gives a parsable format */
1253 t2
= tsub(t2
, ctx
->t1
);
1254 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1255 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1257 qemu_io_free(ctx
->buf
);
1258 qemu_iovec_destroy(&ctx
->qiov
);
1262 static void aio_read_help(void)
1266 " asynchronously reads a range of bytes from the given offset\n"
1269 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1271 " Reads a segment of the currently open file, optionally dumping it to the\n"
1272 " standard output stream (with -v option) for subsequent inspection.\n"
1273 " The read is performed asynchronously and the aio_flush command must be\n"
1274 " used to ensure all outstanding aio requests have been completed.\n"
1275 " -C, -- report statistics in a machine parsable format\n"
1276 " -P, -- use a pattern to verify read data\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
= "[-Cqv] [-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:qv")) != -1) {
1307 ctx
->pattern
= parse_pattern(optarg
);
1308 if (ctx
->pattern
< 0) {
1321 return qemuio_command_usage(&aio_read_cmd
);
1325 if (optind
> argc
- 2) {
1327 return qemuio_command_usage(&aio_read_cmd
);
1330 ctx
->offset
= cvtnum(argv
[optind
]);
1331 if (ctx
->offset
< 0) {
1332 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1338 nr_iov
= argc
- optind
;
1339 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1340 if (ctx
->buf
== NULL
) {
1341 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1346 gettimeofday(&ctx
->t1
, NULL
);
1347 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1349 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1353 static void aio_write_help(void)
1357 " asynchronously writes a range of bytes from the given offset source\n"
1358 " from multiple buffers\n"
1361 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1363 " Writes into a segment of the currently open file, using a buffer\n"
1364 " filled with a set pattern (0xcdcdcdcd).\n"
1365 " The write is performed asynchronously and the aio_flush command must be\n"
1366 " used to ensure all outstanding aio requests have been completed.\n"
1367 " -P, -- use different pattern to fill file\n"
1368 " -C, -- report statistics in a machine parsable format\n"
1369 " -f, -- use Force Unit Access semantics\n"
1370 " -q, -- quiet mode, do not show I/O statistics\n"
1371 " -u, -- with -z, allow unmapping\n"
1372 " -z, -- write zeroes using blk_aio_write_zeroes\n"
1376 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1378 static const cmdinfo_t aio_write_cmd
= {
1379 .name
= "aio_write",
1380 .cfunc
= aio_write_f
,
1383 .args
= "[-Cfquz] [-P pattern] off len [len..]",
1384 .oneline
= "asynchronously writes a number of bytes",
1385 .help
= aio_write_help
,
1388 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1392 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1396 while ((c
= getopt(argc
, argv
, "CfqP:z")) != -1) {
1402 flags
|= BDRV_REQ_FUA
;
1408 flags
|= BDRV_REQ_MAY_UNMAP
;
1411 pattern
= parse_pattern(optarg
);
1422 return qemuio_command_usage(&aio_write_cmd
);
1426 if (optind
> argc
- 2) {
1428 return qemuio_command_usage(&aio_write_cmd
);
1431 if (ctx
->zflag
&& optind
!= argc
- 2) {
1432 printf("-z supports only a single length parameter\n");
1437 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1438 printf("-u requires -z to be specified\n");
1442 if (ctx
->zflag
&& ctx
->Pflag
) {
1443 printf("-z and -P cannot be specified at the same time\n");
1448 ctx
->offset
= cvtnum(argv
[optind
]);
1449 if (ctx
->offset
< 0) {
1450 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1457 int64_t count
= cvtnum(argv
[optind
]);
1459 print_cvtnum_err(count
, argv
[optind
]);
1464 ctx
->qiov
.size
= count
;
1465 blk_aio_write_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1468 nr_iov
= argc
- optind
;
1469 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1471 if (ctx
->buf
== NULL
) {
1472 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1477 gettimeofday(&ctx
->t1
, NULL
);
1478 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1481 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1487 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1489 BlockAcctCookie cookie
;
1490 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1492 block_acct_done(blk_get_stats(blk
), &cookie
);
1496 static const cmdinfo_t aio_flush_cmd
= {
1497 .name
= "aio_flush",
1498 .cfunc
= aio_flush_f
,
1499 .oneline
= "completes all outstanding aio requests"
1502 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1508 static const cmdinfo_t flush_cmd
= {
1512 .oneline
= "flush all in-core file state to disk",
1515 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1520 offset
= cvtnum(argv
[1]);
1522 print_cvtnum_err(offset
, argv
[1]);
1526 ret
= blk_truncate(blk
, offset
);
1528 printf("truncate: %s\n", strerror(-ret
));
1535 static const cmdinfo_t truncate_cmd
= {
1538 .cfunc
= truncate_f
,
1542 .oneline
= "truncates the current file at the given offset",
1545 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1550 size
= blk_getlength(blk
);
1552 printf("getlength: %s\n", strerror(-size
));
1556 cvtstr(size
, s1
, sizeof(s1
));
1562 static const cmdinfo_t length_cmd
= {
1566 .oneline
= "gets the length of the current file",
1570 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1572 BlockDriverState
*bs
= blk_bs(blk
);
1573 BlockDriverInfo bdi
;
1574 ImageInfoSpecific
*spec_info
;
1575 char s1
[64], s2
[64];
1578 if (bs
->drv
&& bs
->drv
->format_name
) {
1579 printf("format name: %s\n", bs
->drv
->format_name
);
1581 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1582 printf("format name: %s\n", bs
->drv
->protocol_name
);
1585 ret
= bdrv_get_info(bs
, &bdi
);
1590 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1591 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1593 printf("cluster size: %s\n", s1
);
1594 printf("vm state offset: %s\n", s2
);
1596 spec_info
= bdrv_get_specific_info(bs
);
1598 printf("Format specific information:\n");
1599 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1600 qapi_free_ImageInfoSpecific(spec_info
);
1608 static const cmdinfo_t info_cmd
= {
1612 .oneline
= "prints information about the current file",
1615 static void discard_help(void)
1619 " discards a range of bytes from the given offset\n"
1622 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1624 " Discards a segment of the currently open file.\n"
1625 " -C, -- report statistics in a machine parsable format\n"
1626 " -q, -- quiet mode, do not show I/O statistics\n"
1630 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1632 static const cmdinfo_t discard_cmd
= {
1638 .args
= "[-Cq] off len",
1639 .oneline
= "discards a number of bytes at a specified offset",
1640 .help
= discard_help
,
1643 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1645 struct timeval t1
, t2
;
1646 bool Cflag
= false, qflag
= false;
1648 int64_t offset
, count
;
1650 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1659 return qemuio_command_usage(&discard_cmd
);
1663 if (optind
!= argc
- 2) {
1664 return qemuio_command_usage(&discard_cmd
);
1667 offset
= cvtnum(argv
[optind
]);
1669 print_cvtnum_err(offset
, argv
[optind
]);
1674 count
= cvtnum(argv
[optind
]);
1676 print_cvtnum_err(count
, argv
[optind
]);
1678 } else if (count
>> BDRV_SECTOR_BITS
> INT_MAX
) {
1679 printf("length cannot exceed %"PRIu64
", given %s\n",
1680 (uint64_t)INT_MAX
<< BDRV_SECTOR_BITS
,
1685 gettimeofday(&t1
, NULL
);
1686 ret
= blk_discard(blk
, offset
>> BDRV_SECTOR_BITS
,
1687 count
>> BDRV_SECTOR_BITS
);
1688 gettimeofday(&t2
, NULL
);
1691 printf("discard failed: %s\n", strerror(-ret
));
1695 /* Finally, report back -- -C gives a parsable format */
1698 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1705 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1707 BlockDriverState
*bs
= blk_bs(blk
);
1708 int64_t offset
, sector_num
, nb_sectors
, remaining
;
1713 offset
= cvtnum(argv
[1]);
1715 print_cvtnum_err(offset
, argv
[1]);
1717 } else if (offset
& 0x1ff) {
1718 printf("offset %" PRId64
" is not sector aligned\n",
1724 nb_sectors
= cvtnum(argv
[2]);
1725 if (nb_sectors
< 0) {
1726 print_cvtnum_err(nb_sectors
, argv
[2]);
1728 } else if (nb_sectors
> INT_MAX
) {
1729 printf("length argument cannot exceed %d, given %s\n",
1737 remaining
= nb_sectors
;
1739 sector_num
= offset
>> 9;
1741 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1743 printf("is_allocated failed: %s\n", strerror(-ret
));
1752 nb_sectors
-= remaining
;
1757 cvtstr(offset
, s1
, sizeof(s1
));
1759 printf("%"PRId64
"/%"PRId64
" sectors allocated at offset %s\n",
1760 sum_alloc
, nb_sectors
, s1
);
1764 static const cmdinfo_t alloc_cmd
= {
1770 .args
= "off [sectors]",
1771 .oneline
= "checks if a sector is present in the file",
1775 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1776 int64_t nb_sectors
, int64_t *pnum
)
1778 int num
, num_checked
;
1781 num_checked
= MIN(nb_sectors
, INT_MAX
);
1782 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1790 while (nb_sectors
> 0 && ret
== firstret
) {
1794 num_checked
= MIN(nb_sectors
, INT_MAX
);
1795 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1796 if (ret
== firstret
&& num
) {
1806 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1809 int64_t nb_sectors
, total_sectors
;
1816 total_sectors
= blk_nb_sectors(blk
);
1817 if (total_sectors
< 0) {
1818 error_report("Failed to query image length: %s",
1819 strerror(-total_sectors
));
1823 nb_sectors
= total_sectors
;
1826 ret
= map_is_allocated(blk_bs(blk
), offset
, nb_sectors
, &num
);
1828 error_report("Failed to get allocation status: %s", strerror(-ret
));
1831 error_report("Unexpected end of image");
1835 retstr
= ret
? " allocated" : "not allocated";
1836 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1837 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1838 "at offset %s (%d)\n",
1839 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
1843 } while (offset
< total_sectors
);
1848 static const cmdinfo_t map_cmd
= {
1854 .oneline
= "prints the allocated areas of a file",
1857 static void reopen_help(void)
1861 " Changes the open options of an already opened image\n"
1864 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1866 " -r, -- Reopen the image read-only\n"
1867 " -c, -- Change the cache mode to the given value\n"
1868 " -o, -- Changes block driver options (cf. 'open' command)\n"
1872 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
1874 static QemuOptsList reopen_opts
= {
1876 .merge_lists
= true,
1877 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
1879 /* no elements => accept any params */
1880 { /* end of list */ }
1884 static const cmdinfo_t reopen_cmd
= {
1889 .args
= "[-r] [-c cache] [-o options]",
1890 .oneline
= "reopens an image with new options",
1891 .help
= reopen_help
,
1894 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
1896 BlockDriverState
*bs
= blk_bs(blk
);
1900 int flags
= bs
->open_flags
;
1901 bool writethrough
= !blk_enable_write_cache(blk
);
1903 BlockReopenQueue
*brq
;
1904 Error
*local_err
= NULL
;
1906 while ((c
= getopt(argc
, argv
, "c:o:r")) != -1) {
1909 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
1910 error_report("Invalid cache option: %s", optarg
);
1915 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
1916 qemu_opts_reset(&reopen_opts
);
1921 flags
&= ~BDRV_O_RDWR
;
1924 qemu_opts_reset(&reopen_opts
);
1925 return qemuio_command_usage(&reopen_cmd
);
1929 if (optind
!= argc
) {
1930 qemu_opts_reset(&reopen_opts
);
1931 return qemuio_command_usage(&reopen_cmd
);
1934 if (writethrough
!= blk_enable_write_cache(blk
) &&
1935 blk_get_attached_dev(blk
))
1937 error_report("Cannot change cache.writeback: Device attached");
1938 qemu_opts_reset(&reopen_opts
);
1942 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
1943 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
1944 qemu_opts_reset(&reopen_opts
);
1946 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
1947 bdrv_reopen_multiple(brq
, &local_err
);
1949 error_report_err(local_err
);
1951 blk_set_enable_write_cache(blk
, !writethrough
);
1957 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
1961 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
1963 printf("Could not set breakpoint: %s\n", strerror(-ret
));
1969 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
1973 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
1975 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
1981 static const cmdinfo_t break_cmd
= {
1986 .args
= "event tag",
1987 .oneline
= "sets a breakpoint on event and tags the stopped "
1991 static const cmdinfo_t remove_break_cmd
= {
1992 .name
= "remove_break",
1995 .cfunc
= remove_break_f
,
1997 .oneline
= "remove a breakpoint by tag",
2000 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2004 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2006 printf("Could not resume request: %s\n", strerror(-ret
));
2012 static const cmdinfo_t resume_cmd
= {
2018 .oneline
= "resumes the request tagged as tag",
2021 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2023 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2024 aio_poll(blk_get_aio_context(blk
), true);
2030 static const cmdinfo_t wait_break_cmd
= {
2031 .name
= "wait_break",
2034 .cfunc
= wait_break_f
,
2036 .oneline
= "waits for the suspension of a request",
2039 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2044 static const cmdinfo_t abort_cmd
= {
2047 .flags
= CMD_NOFILE_OK
,
2048 .oneline
= "simulate a program crash using abort(3)",
2051 static void sigraise_help(void)
2055 " raises the given signal\n"
2058 " 'sigraise %i' - raises SIGTERM\n"
2060 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2061 " given to sigraise.\n"
2065 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2067 static const cmdinfo_t sigraise_cmd
= {
2069 .cfunc
= sigraise_f
,
2072 .flags
= CMD_NOFILE_OK
,
2074 .oneline
= "raises a signal",
2075 .help
= sigraise_help
,
2078 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2080 int64_t sig
= cvtnum(argv
[1]);
2082 print_cvtnum_err(sig
, argv
[1]);
2084 } else if (sig
> NSIG
) {
2085 printf("signal argument '%s' is too large to be a valid signal\n",
2090 /* Using raise() to kill this process does not necessarily flush all open
2091 * streams. At least stdout and stderr (although the latter should be
2092 * non-buffered anyway) should be flushed, though. */
2100 static void sleep_cb(void *opaque
)
2102 bool *expired
= opaque
;
2106 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2110 struct QEMUTimer
*timer
;
2111 bool expired
= false;
2113 ms
= strtol(argv
[1], &endptr
, 0);
2114 if (ms
< 0 || *endptr
!= '\0') {
2115 printf("%s is not a valid number\n", argv
[1]);
2119 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2120 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2123 main_loop_wait(false);
2131 static const cmdinfo_t sleep_cmd
= {
2136 .flags
= CMD_NOFILE_OK
,
2137 .oneline
= "waits for the given value in milliseconds",
2140 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2145 printf("%s ", ct
->name
);
2147 printf("(or %s) ", ct
->altname
);
2152 printf("%s ", ct
->args
);
2154 printf("-- %s\n", ct
->oneline
);
2157 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2159 help_oneline(cmd
, ct
);
2165 static void help_all(void)
2167 const cmdinfo_t
*ct
;
2169 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2170 help_oneline(ct
->name
, ct
);
2172 printf("\nUse 'help commandname' for extended help.\n");
2175 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2177 const cmdinfo_t
*ct
;
2184 ct
= find_command(argv
[1]);
2186 printf("command %s not found\n", argv
[1]);
2190 help_onecmd(argv
[1], ct
);
2194 static const cmdinfo_t help_cmd
= {
2200 .flags
= CMD_FLAG_GLOBAL
,
2201 .args
= "[command]",
2202 .oneline
= "help for one or all commands",
2205 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2208 const cmdinfo_t
*ct
;
2213 input
= g_strdup(cmd
);
2214 v
= breakline(input
, &c
);
2216 ct
= find_command(v
[0]);
2218 done
= command(blk
, ct
, c
, v
);
2220 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2229 static void __attribute((constructor
)) init_qemuio_commands(void)
2231 /* initialize commands */
2232 qemuio_add_command(&help_cmd
);
2233 qemuio_add_command(&read_cmd
);
2234 qemuio_add_command(&readv_cmd
);
2235 qemuio_add_command(&write_cmd
);
2236 qemuio_add_command(&writev_cmd
);
2237 qemuio_add_command(&aio_read_cmd
);
2238 qemuio_add_command(&aio_write_cmd
);
2239 qemuio_add_command(&aio_flush_cmd
);
2240 qemuio_add_command(&flush_cmd
);
2241 qemuio_add_command(&truncate_cmd
);
2242 qemuio_add_command(&length_cmd
);
2243 qemuio_add_command(&info_cmd
);
2244 qemuio_add_command(&discard_cmd
);
2245 qemuio_add_command(&alloc_cmd
);
2246 qemuio_add_command(&map_cmd
);
2247 qemuio_add_command(&reopen_cmd
);
2248 qemuio_add_command(&break_cmd
);
2249 qemuio_add_command(&remove_break_cmd
);
2250 qemuio_add_command(&resume_cmd
);
2251 qemuio_add_command(&wait_break_cmd
);
2252 qemuio_add_command(&abort_cmd
);
2253 qemuio_add_command(&sleep_cmd
);
2254 qemuio_add_command(&sigraise_cmd
);