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 err
= qemu_strtosz(s
, NULL
, &value
);
147 if (value
> INT64_MAX
) {
153 static void print_cvtnum_err(int64_t rc
, const char *arg
)
157 printf("Parsing error: non-numeric argument,"
158 " or extraneous/unrecognized suffix -- %s\n", arg
);
161 printf("Parsing error: argument too large -- %s\n", arg
);
164 printf("Parsing error: %s\n", arg
);
168 #define EXABYTES(x) ((long long)(x) << 60)
169 #define PETABYTES(x) ((long long)(x) << 50)
170 #define TERABYTES(x) ((long long)(x) << 40)
171 #define GIGABYTES(x) ((long long)(x) << 30)
172 #define MEGABYTES(x) ((long long)(x) << 20)
173 #define KILOBYTES(x) ((long long)(x) << 10)
175 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
176 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
177 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
178 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
179 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
180 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
182 static void cvtstr(double value
, char *str
, size_t size
)
187 if (value
>= EXABYTES(1)) {
189 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
190 } else if (value
>= PETABYTES(1)) {
192 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
193 } else if (value
>= TERABYTES(1)) {
195 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
196 } else if (value
>= GIGABYTES(1)) {
198 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
199 } else if (value
>= MEGABYTES(1)) {
201 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
202 } else if (value
>= KILOBYTES(1)) {
204 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
207 snprintf(str
, size
- 6, "%f", value
);
210 trim
= strstr(str
, ".000");
212 strcpy(trim
, suffix
);
220 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
222 t1
.tv_usec
-= t2
.tv_usec
;
223 if (t1
.tv_usec
< 0) {
224 t1
.tv_usec
+= 1000000;
227 t1
.tv_sec
-= t2
.tv_sec
;
231 static double tdiv(double value
, struct timeval tv
)
233 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
236 #define HOURS(sec) ((sec) / (60 * 60))
237 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
238 #define SECONDS(sec) ((sec) % 60)
242 TERSE_FIXED_TIME
= 0x1,
243 VERBOSE_FIXED_TIME
= 0x2,
246 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
248 double usec
= (double)tv
->tv_usec
/ 1000000.0;
250 if (format
& TERSE_FIXED_TIME
) {
251 if (!HOURS(tv
->tv_sec
)) {
252 snprintf(ts
, size
, "%u:%02u.%02u",
253 (unsigned int) MINUTES(tv
->tv_sec
),
254 (unsigned int) SECONDS(tv
->tv_sec
),
255 (unsigned int) (usec
* 100));
258 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
261 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
262 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
263 (unsigned int) HOURS(tv
->tv_sec
),
264 (unsigned int) MINUTES(tv
->tv_sec
),
265 (unsigned int) SECONDS(tv
->tv_sec
),
266 (unsigned int) (usec
* 100));
268 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
273 * Parse the pattern argument to various sub-commands.
275 * Because the pattern is used as an argument to memset it must evaluate
276 * to an unsigned integer that fits into a single byte.
278 static int parse_pattern(const char *arg
)
283 pattern
= strtol(arg
, &endptr
, 0);
284 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
285 printf("%s is not a valid pattern byte\n", arg
);
293 * Memory allocation helpers.
295 * Make sure memory is aligned by default, or purposefully misaligned if
296 * that is specified on the command line.
299 #define MISALIGN_OFFSET 16
300 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
304 if (qemuio_misalign
) {
305 len
+= MISALIGN_OFFSET
;
307 buf
= blk_blockalign(blk
, len
);
308 memset(buf
, pattern
, len
);
309 if (qemuio_misalign
) {
310 buf
+= MISALIGN_OFFSET
;
315 static void qemu_io_free(void *p
)
317 if (qemuio_misalign
) {
318 p
-= MISALIGN_OFFSET
;
323 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
329 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
330 const uint8_t *s
= p
;
332 printf("%08" PRIx64
": ", offset
+ i
);
333 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
337 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
348 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
349 int64_t count
, int64_t total
, int cnt
, bool Cflag
)
351 char s1
[64], s2
[64], ts
[64];
353 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
355 cvtstr((double)total
, s1
, sizeof(s1
));
356 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
357 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
358 op
, total
, count
, offset
);
359 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
360 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
361 } else {/* bytes,ops,time,bytes/sec,ops/sec */
362 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
364 tdiv((double)total
, *t
),
365 tdiv((double)cnt
, *t
));
370 * Parse multiple length statements for vectored I/O, and construct an I/O
371 * vector matching it.
374 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
377 size_t *sizes
= g_new0(size_t, nr_iov
);
383 for (i
= 0; i
< nr_iov
; i
++) {
389 print_cvtnum_err(len
, arg
);
393 if (len
> BDRV_REQUEST_MAX_BYTES
) {
394 printf("Argument '%s' exceeds maximum size %" PRIu64
"\n", arg
,
395 (uint64_t)BDRV_REQUEST_MAX_BYTES
);
399 if (count
> BDRV_REQUEST_MAX_BYTES
- len
) {
400 printf("The total number of bytes exceed the maximum size %" PRIu64
401 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES
);
409 qemu_iovec_init(qiov
, nr_iov
);
411 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
413 for (i
= 0; i
< nr_iov
; i
++) {
414 qemu_iovec_add(qiov
, p
, sizes
[i
]);
423 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
424 int64_t count
, int64_t *total
)
426 if (count
> INT_MAX
) {
430 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, count
);
437 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
438 int64_t count
, int flags
, int64_t *total
)
440 if (count
> INT_MAX
) {
444 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, count
, flags
);
461 static void coroutine_fn
co_pwrite_zeroes_entry(void *opaque
)
463 CoWriteZeroes
*data
= opaque
;
465 data
->ret
= blk_co_pwrite_zeroes(data
->blk
, data
->offset
, data
->count
,
469 *data
->total
= data
->ret
;
473 *data
->total
= data
->count
;
476 static int do_co_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
477 int64_t count
, int flags
, int64_t *total
)
480 CoWriteZeroes data
= {
489 if (count
> INT_MAX
) {
493 co
= qemu_coroutine_create(co_pwrite_zeroes_entry
, &data
);
494 qemu_coroutine_enter(co
);
496 aio_poll(blk_get_aio_context(blk
), true);
505 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
506 int64_t count
, int64_t *total
)
510 if (count
>> 9 > BDRV_REQUEST_MAX_SECTORS
) {
514 ret
= blk_pwrite_compressed(blk
, offset
, buf
, count
);
522 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
523 int64_t count
, int64_t *total
)
525 if (count
> INT_MAX
) {
529 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
536 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
537 int64_t count
, int64_t *total
)
539 if (count
> INT_MAX
) {
543 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
550 #define NOT_DONE 0x7fffffff
551 static void aio_rw_done(void *opaque
, int ret
)
553 *(int *)opaque
= ret
;
556 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
557 int64_t offset
, int *total
)
559 int async_ret
= NOT_DONE
;
561 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
562 while (async_ret
== NOT_DONE
) {
563 main_loop_wait(false);
567 return async_ret
< 0 ? async_ret
: 1;
570 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
571 int64_t offset
, int flags
, int *total
)
573 int async_ret
= NOT_DONE
;
575 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
576 while (async_ret
== NOT_DONE
) {
577 main_loop_wait(false);
581 return async_ret
< 0 ? async_ret
: 1;
584 static void read_help(void)
588 " reads a range of bytes from the given offset\n"
591 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
593 " Reads a segment of the currently open file, optionally dumping it to the\n"
594 " standard output stream (with -v option) for subsequent inspection.\n"
595 " -b, -- read from the VM state rather than the virtual disk\n"
596 " -C, -- report statistics in a machine parsable format\n"
597 " -l, -- length for pattern verification (only with -P)\n"
598 " -p, -- ignored for backwards compatibility\n"
599 " -P, -- use a pattern to verify read data\n"
600 " -q, -- quiet mode, do not show I/O statistics\n"
601 " -s, -- start offset for pattern verification (only with -P)\n"
602 " -v, -- dump buffer to standard output\n"
606 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
608 static const cmdinfo_t read_cmd
= {
614 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
615 .oneline
= "reads a number of bytes at a specified offset",
619 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
621 struct timeval t1
, t2
;
622 bool Cflag
= false, qflag
= false, vflag
= false;
623 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
628 /* Some compilers get confused and warn if this is not initialized. */
631 int64_t pattern_offset
= 0, pattern_count
= 0;
633 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
643 pattern_count
= cvtnum(optarg
);
644 if (pattern_count
< 0) {
645 print_cvtnum_err(pattern_count
, optarg
);
650 /* Ignored for backwards compatibility */
654 pattern
= parse_pattern(optarg
);
664 pattern_offset
= cvtnum(optarg
);
665 if (pattern_offset
< 0) {
666 print_cvtnum_err(pattern_offset
, optarg
);
674 return qemuio_command_usage(&read_cmd
);
678 if (optind
!= argc
- 2) {
679 return qemuio_command_usage(&read_cmd
);
682 offset
= cvtnum(argv
[optind
]);
684 print_cvtnum_err(offset
, argv
[optind
]);
689 count
= cvtnum(argv
[optind
]);
691 print_cvtnum_err(count
, argv
[optind
]);
693 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
694 printf("length cannot exceed %" PRIu64
", given %s\n",
695 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
699 if (!Pflag
&& (lflag
|| sflag
)) {
700 return qemuio_command_usage(&read_cmd
);
704 pattern_count
= count
- pattern_offset
;
707 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
708 printf("pattern verification range exceeds end of read data\n");
713 if (offset
& 0x1ff) {
714 printf("offset %" PRId64
" is not sector aligned\n",
719 printf("count %"PRId64
" is not sector aligned\n",
725 buf
= qemu_io_alloc(blk
, count
, 0xab);
727 gettimeofday(&t1
, NULL
);
729 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
731 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
733 gettimeofday(&t2
, NULL
);
736 printf("read failed: %s\n", strerror(-cnt
));
741 void *cmp_buf
= g_malloc(pattern_count
);
742 memset(cmp_buf
, pattern
, pattern_count
);
743 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
744 printf("Pattern verification failed at offset %"
745 PRId64
", %"PRId64
" bytes\n",
746 offset
+ pattern_offset
, pattern_count
);
756 dump_buffer(buf
, offset
, count
);
759 /* Finally, report back -- -C gives a parsable format */
761 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
769 static void readv_help(void)
773 " reads a range of bytes from the given offset into multiple buffers\n"
776 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
778 " Reads a segment of the currently open file, optionally dumping it to the\n"
779 " standard output stream (with -v option) for subsequent inspection.\n"
780 " Uses multiple iovec buffers if more than one byte range is specified.\n"
781 " -C, -- report statistics in a machine parsable format\n"
782 " -P, -- use a pattern to verify read data\n"
783 " -v, -- dump buffer to standard output\n"
784 " -q, -- quiet mode, do not show I/O statistics\n"
788 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
790 static const cmdinfo_t readv_cmd
= {
795 .args
= "[-Cqv] [-P pattern] off len [len..]",
796 .oneline
= "reads a number of bytes at a specified offset",
800 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
802 struct timeval t1
, t2
;
803 bool Cflag
= false, qflag
= false, vflag
= false;
807 /* Some compilers get confused and warn if this is not initialized. */
814 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
821 pattern
= parse_pattern(optarg
);
833 return qemuio_command_usage(&readv_cmd
);
837 if (optind
> argc
- 2) {
838 return qemuio_command_usage(&readv_cmd
);
842 offset
= cvtnum(argv
[optind
]);
844 print_cvtnum_err(offset
, argv
[optind
]);
849 nr_iov
= argc
- optind
;
850 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
855 gettimeofday(&t1
, NULL
);
856 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
857 gettimeofday(&t2
, NULL
);
860 printf("readv failed: %s\n", strerror(-cnt
));
865 void *cmp_buf
= g_malloc(qiov
.size
);
866 memset(cmp_buf
, pattern
, qiov
.size
);
867 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
868 printf("Pattern verification failed at offset %"
869 PRId64
", %zd bytes\n", offset
, qiov
.size
);
879 dump_buffer(buf
, offset
, qiov
.size
);
882 /* Finally, report back -- -C gives a parsable format */
884 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
887 qemu_iovec_destroy(&qiov
);
892 static void write_help(void)
896 " writes a range of bytes from the given offset\n"
899 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
901 " Writes into a segment of the currently open file, using a buffer\n"
902 " filled with a set pattern (0xcdcdcdcd).\n"
903 " -b, -- write to the VM state rather than the virtual disk\n"
904 " -c, -- write compressed data with blk_write_compressed\n"
905 " -f, -- use Force Unit Access semantics\n"
906 " -p, -- ignored for backwards compatibility\n"
907 " -P, -- use different pattern to fill file\n"
908 " -C, -- report statistics in a machine parsable format\n"
909 " -q, -- quiet mode, do not show I/O statistics\n"
910 " -u, -- with -z, allow unmapping\n"
911 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
915 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
917 static const cmdinfo_t write_cmd
= {
923 .args
= "[-bcCfquz] [-P pattern] off len",
924 .oneline
= "writes a number of bytes at a specified offset",
928 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
930 struct timeval t1
, t2
;
931 bool Cflag
= false, qflag
= false, bflag
= false;
932 bool Pflag
= false, zflag
= false, cflag
= false;
938 /* Some compilers get confused and warn if this is not initialized. */
942 while ((c
= getopt(argc
, argv
, "bcCfpP:quz")) != -1) {
954 flags
|= BDRV_REQ_FUA
;
957 /* Ignored for backwards compatibility */
961 pattern
= parse_pattern(optarg
);
970 flags
|= BDRV_REQ_MAY_UNMAP
;
976 return qemuio_command_usage(&write_cmd
);
980 if (optind
!= argc
- 2) {
981 return qemuio_command_usage(&write_cmd
);
984 if (bflag
&& zflag
) {
985 printf("-b and -z cannot be specified at the same time\n");
989 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
990 printf("-f and -b or -c cannot be specified at the same time\n");
994 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
995 printf("-u requires -z to be specified\n");
999 if (zflag
&& Pflag
) {
1000 printf("-z and -P cannot be specified at the same time\n");
1004 offset
= cvtnum(argv
[optind
]);
1006 print_cvtnum_err(offset
, argv
[optind
]);
1011 count
= cvtnum(argv
[optind
]);
1013 print_cvtnum_err(count
, argv
[optind
]);
1015 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
1016 printf("length cannot exceed %" PRIu64
", given %s\n",
1017 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1021 if (bflag
|| cflag
) {
1022 if (offset
& 0x1ff) {
1023 printf("offset %" PRId64
" is not sector aligned\n",
1028 if (count
& 0x1ff) {
1029 printf("count %"PRId64
" is not sector aligned\n",
1036 buf
= qemu_io_alloc(blk
, count
, pattern
);
1039 gettimeofday(&t1
, NULL
);
1041 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1043 cnt
= do_co_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1045 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1047 cnt
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1049 gettimeofday(&t2
, NULL
);
1052 printf("write failed: %s\n", strerror(-cnt
));
1060 /* Finally, report back -- -C gives a parsable format */
1062 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1077 " writes a range of bytes from the given offset source from multiple buffers\n"
1080 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1082 " Writes into a segment of the currently open file, using a buffer\n"
1083 " filled with a set pattern (0xcdcdcdcd).\n"
1084 " -P, -- use different pattern to fill file\n"
1085 " -C, -- report statistics in a machine parsable format\n"
1086 " -f, -- use Force Unit Access semantics\n"
1087 " -q, -- quiet mode, do not show I/O statistics\n"
1091 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1093 static const cmdinfo_t writev_cmd
= {
1098 .args
= "[-Cfq] [-P pattern] off len [len..]",
1099 .oneline
= "writes a number of bytes at a specified offset",
1100 .help
= writev_help
,
1103 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1105 struct timeval t1
, t2
;
1106 bool Cflag
= false, qflag
= false;
1111 /* Some compilers get confused and warn if this is not initialized. */
1117 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1123 flags
|= BDRV_REQ_FUA
;
1129 pattern
= parse_pattern(optarg
);
1135 return qemuio_command_usage(&writev_cmd
);
1139 if (optind
> argc
- 2) {
1140 return qemuio_command_usage(&writev_cmd
);
1143 offset
= cvtnum(argv
[optind
]);
1145 print_cvtnum_err(offset
, argv
[optind
]);
1150 nr_iov
= argc
- optind
;
1151 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1156 gettimeofday(&t1
, NULL
);
1157 cnt
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1158 gettimeofday(&t2
, NULL
);
1161 printf("writev failed: %s\n", strerror(-cnt
));
1169 /* Finally, report back -- -C gives a parsable format */
1171 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1173 qemu_iovec_destroy(&qiov
);
1188 BlockAcctCookie acct
;
1193 static void aio_write_done(void *opaque
, int ret
)
1195 struct aio_ctx
*ctx
= opaque
;
1198 gettimeofday(&t2
, NULL
);
1202 printf("aio_write failed: %s\n", strerror(-ret
));
1203 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1207 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1213 /* Finally, report back -- -C gives a parsable format */
1214 t2
= tsub(t2
, ctx
->t1
);
1215 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1216 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1219 qemu_io_free(ctx
->buf
);
1220 qemu_iovec_destroy(&ctx
->qiov
);
1225 static void aio_read_done(void *opaque
, int ret
)
1227 struct aio_ctx
*ctx
= opaque
;
1230 gettimeofday(&t2
, NULL
);
1233 printf("readv failed: %s\n", strerror(-ret
));
1234 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1239 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1241 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1242 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1243 printf("Pattern verification failed at offset %"
1244 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1249 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1256 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1259 /* Finally, report back -- -C gives a parsable format */
1260 t2
= tsub(t2
, ctx
->t1
);
1261 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1262 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1264 qemu_io_free(ctx
->buf
);
1265 qemu_iovec_destroy(&ctx
->qiov
);
1269 static void aio_read_help(void)
1273 " asynchronously reads a range of bytes from the given offset\n"
1276 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1278 " Reads a segment of the currently open file, optionally dumping it to the\n"
1279 " standard output stream (with -v option) for subsequent inspection.\n"
1280 " The read is performed asynchronously and the aio_flush command must be\n"
1281 " used to ensure all outstanding aio requests have been completed.\n"
1282 " -C, -- report statistics in a machine parsable format\n"
1283 " -P, -- use a pattern to verify read data\n"
1284 " -i, -- treat request as invalid, for exercising stats\n"
1285 " -v, -- dump buffer to standard output\n"
1286 " -q, -- quiet mode, do not show I/O statistics\n"
1290 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1292 static const cmdinfo_t aio_read_cmd
= {
1294 .cfunc
= aio_read_f
,
1297 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1298 .oneline
= "asynchronously reads a number of bytes",
1299 .help
= aio_read_help
,
1302 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1305 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1308 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1315 ctx
->pattern
= parse_pattern(optarg
);
1316 if (ctx
->pattern
< 0) {
1322 printf("injecting invalid read request\n");
1323 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1334 return qemuio_command_usage(&aio_read_cmd
);
1338 if (optind
> argc
- 2) {
1340 return qemuio_command_usage(&aio_read_cmd
);
1343 ctx
->offset
= cvtnum(argv
[optind
]);
1344 if (ctx
->offset
< 0) {
1345 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1351 nr_iov
= argc
- optind
;
1352 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1353 if (ctx
->buf
== NULL
) {
1354 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1359 gettimeofday(&ctx
->t1
, NULL
);
1360 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1362 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1366 static void aio_write_help(void)
1370 " asynchronously writes a range of bytes from the given offset source\n"
1371 " from multiple buffers\n"
1374 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1376 " Writes into a segment of the currently open file, using a buffer\n"
1377 " filled with a set pattern (0xcdcdcdcd).\n"
1378 " The write is performed asynchronously and the aio_flush command must be\n"
1379 " used to ensure all outstanding aio requests have been completed.\n"
1380 " -P, -- use different pattern to fill file\n"
1381 " -C, -- report statistics in a machine parsable format\n"
1382 " -f, -- use Force Unit Access semantics\n"
1383 " -i, -- treat request as invalid, for exercising stats\n"
1384 " -q, -- quiet mode, do not show I/O statistics\n"
1385 " -u, -- with -z, allow unmapping\n"
1386 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1390 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1392 static const cmdinfo_t aio_write_cmd
= {
1393 .name
= "aio_write",
1394 .cfunc
= aio_write_f
,
1397 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1398 .oneline
= "asynchronously writes a number of bytes",
1399 .help
= aio_write_help
,
1402 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1406 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1410 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1416 flags
|= BDRV_REQ_FUA
;
1422 flags
|= BDRV_REQ_MAY_UNMAP
;
1425 pattern
= parse_pattern(optarg
);
1432 printf("injecting invalid write request\n");
1433 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1441 return qemuio_command_usage(&aio_write_cmd
);
1445 if (optind
> argc
- 2) {
1447 return qemuio_command_usage(&aio_write_cmd
);
1450 if (ctx
->zflag
&& optind
!= argc
- 2) {
1451 printf("-z supports only a single length parameter\n");
1456 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1457 printf("-u requires -z to be specified\n");
1462 if (ctx
->zflag
&& ctx
->Pflag
) {
1463 printf("-z and -P cannot be specified at the same time\n");
1468 ctx
->offset
= cvtnum(argv
[optind
]);
1469 if (ctx
->offset
< 0) {
1470 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1477 int64_t count
= cvtnum(argv
[optind
]);
1479 print_cvtnum_err(count
, argv
[optind
]);
1484 ctx
->qiov
.size
= count
;
1485 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1488 nr_iov
= argc
- optind
;
1489 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1491 if (ctx
->buf
== NULL
) {
1492 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1497 gettimeofday(&ctx
->t1
, NULL
);
1498 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1501 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1507 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1509 BlockAcctCookie cookie
;
1510 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1512 block_acct_done(blk_get_stats(blk
), &cookie
);
1516 static const cmdinfo_t aio_flush_cmd
= {
1517 .name
= "aio_flush",
1518 .cfunc
= aio_flush_f
,
1519 .oneline
= "completes all outstanding aio requests"
1522 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1528 static const cmdinfo_t flush_cmd
= {
1532 .oneline
= "flush all in-core file state to disk",
1535 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1540 offset
= cvtnum(argv
[1]);
1542 print_cvtnum_err(offset
, argv
[1]);
1546 ret
= blk_truncate(blk
, offset
);
1548 printf("truncate: %s\n", strerror(-ret
));
1555 static const cmdinfo_t truncate_cmd
= {
1558 .cfunc
= truncate_f
,
1562 .oneline
= "truncates the current file at the given offset",
1565 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1570 size
= blk_getlength(blk
);
1572 printf("getlength: %s\n", strerror(-size
));
1576 cvtstr(size
, s1
, sizeof(s1
));
1582 static const cmdinfo_t length_cmd
= {
1586 .oneline
= "gets the length of the current file",
1590 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1592 BlockDriverState
*bs
= blk_bs(blk
);
1593 BlockDriverInfo bdi
;
1594 ImageInfoSpecific
*spec_info
;
1595 char s1
[64], s2
[64];
1598 if (bs
->drv
&& bs
->drv
->format_name
) {
1599 printf("format name: %s\n", bs
->drv
->format_name
);
1601 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1602 printf("format name: %s\n", bs
->drv
->protocol_name
);
1605 ret
= bdrv_get_info(bs
, &bdi
);
1610 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1611 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1613 printf("cluster size: %s\n", s1
);
1614 printf("vm state offset: %s\n", s2
);
1616 spec_info
= bdrv_get_specific_info(bs
);
1618 printf("Format specific information:\n");
1619 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1620 qapi_free_ImageInfoSpecific(spec_info
);
1628 static const cmdinfo_t info_cmd
= {
1632 .oneline
= "prints information about the current file",
1635 static void discard_help(void)
1639 " discards a range of bytes from the given offset\n"
1642 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1644 " Discards a segment of the currently open file.\n"
1645 " -C, -- report statistics in a machine parsable format\n"
1646 " -q, -- quiet mode, do not show I/O statistics\n"
1650 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1652 static const cmdinfo_t discard_cmd
= {
1658 .args
= "[-Cq] off len",
1659 .oneline
= "discards a number of bytes at a specified offset",
1660 .help
= discard_help
,
1663 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1665 struct timeval t1
, t2
;
1666 bool Cflag
= false, qflag
= false;
1668 int64_t offset
, count
;
1670 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1679 return qemuio_command_usage(&discard_cmd
);
1683 if (optind
!= argc
- 2) {
1684 return qemuio_command_usage(&discard_cmd
);
1687 offset
= cvtnum(argv
[optind
]);
1689 print_cvtnum_err(offset
, argv
[optind
]);
1694 count
= cvtnum(argv
[optind
]);
1696 print_cvtnum_err(count
, argv
[optind
]);
1698 } else if (count
>> BDRV_SECTOR_BITS
> BDRV_REQUEST_MAX_SECTORS
) {
1699 printf("length cannot exceed %"PRIu64
", given %s\n",
1700 (uint64_t)BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
,
1705 gettimeofday(&t1
, NULL
);
1706 ret
= blk_pdiscard(blk
, offset
, count
);
1707 gettimeofday(&t2
, NULL
);
1710 printf("discard failed: %s\n", strerror(-ret
));
1714 /* Finally, report back -- -C gives a parsable format */
1717 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1724 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1726 BlockDriverState
*bs
= blk_bs(blk
);
1727 int64_t offset
, sector_num
, nb_sectors
, remaining
;
1732 offset
= cvtnum(argv
[1]);
1734 print_cvtnum_err(offset
, argv
[1]);
1736 } else if (offset
& 0x1ff) {
1737 printf("offset %" PRId64
" is not sector aligned\n",
1743 nb_sectors
= cvtnum(argv
[2]);
1744 if (nb_sectors
< 0) {
1745 print_cvtnum_err(nb_sectors
, argv
[2]);
1747 } else if (nb_sectors
> INT_MAX
) {
1748 printf("length argument cannot exceed %d, given %s\n",
1756 remaining
= nb_sectors
;
1758 sector_num
= offset
>> 9;
1760 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1762 printf("is_allocated failed: %s\n", strerror(-ret
));
1771 nb_sectors
-= remaining
;
1776 cvtstr(offset
, s1
, sizeof(s1
));
1778 printf("%"PRId64
"/%"PRId64
" sectors allocated at offset %s\n",
1779 sum_alloc
, nb_sectors
, s1
);
1783 static const cmdinfo_t alloc_cmd
= {
1789 .args
= "off [sectors]",
1790 .oneline
= "checks if a sector is present in the file",
1794 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1795 int64_t nb_sectors
, int64_t *pnum
)
1797 int num
, num_checked
;
1800 num_checked
= MIN(nb_sectors
, INT_MAX
);
1801 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1809 while (nb_sectors
> 0 && ret
== firstret
) {
1813 num_checked
= MIN(nb_sectors
, INT_MAX
);
1814 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1815 if (ret
== firstret
&& num
) {
1825 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1828 int64_t nb_sectors
, total_sectors
;
1835 total_sectors
= blk_nb_sectors(blk
);
1836 if (total_sectors
< 0) {
1837 error_report("Failed to query image length: %s",
1838 strerror(-total_sectors
));
1842 nb_sectors
= total_sectors
;
1845 ret
= map_is_allocated(blk_bs(blk
), offset
, nb_sectors
, &num
);
1847 error_report("Failed to get allocation status: %s", strerror(-ret
));
1850 error_report("Unexpected end of image");
1854 retstr
= ret
? " allocated" : "not allocated";
1855 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1856 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1857 "at offset %s (%d)\n",
1858 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
1862 } while (offset
< total_sectors
);
1867 static const cmdinfo_t map_cmd
= {
1873 .oneline
= "prints the allocated areas of a file",
1876 static void reopen_help(void)
1880 " Changes the open options of an already opened image\n"
1883 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1885 " -r, -- Reopen the image read-only\n"
1886 " -c, -- Change the cache mode to the given value\n"
1887 " -o, -- Changes block driver options (cf. 'open' command)\n"
1891 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
1893 static QemuOptsList reopen_opts
= {
1895 .merge_lists
= true,
1896 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
1898 /* no elements => accept any params */
1899 { /* end of list */ }
1903 static const cmdinfo_t reopen_cmd
= {
1908 .args
= "[-r] [-c cache] [-o options]",
1909 .oneline
= "reopens an image with new options",
1910 .help
= reopen_help
,
1913 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
1915 BlockDriverState
*bs
= blk_bs(blk
);
1919 int flags
= bs
->open_flags
;
1920 bool writethrough
= !blk_enable_write_cache(blk
);
1922 BlockReopenQueue
*brq
;
1923 Error
*local_err
= NULL
;
1925 while ((c
= getopt(argc
, argv
, "c:o:r")) != -1) {
1928 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
1929 error_report("Invalid cache option: %s", optarg
);
1934 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
1935 qemu_opts_reset(&reopen_opts
);
1940 flags
&= ~BDRV_O_RDWR
;
1943 qemu_opts_reset(&reopen_opts
);
1944 return qemuio_command_usage(&reopen_cmd
);
1948 if (optind
!= argc
) {
1949 qemu_opts_reset(&reopen_opts
);
1950 return qemuio_command_usage(&reopen_cmd
);
1953 if (writethrough
!= blk_enable_write_cache(blk
) &&
1954 blk_get_attached_dev(blk
))
1956 error_report("Cannot change cache.writeback: Device attached");
1957 qemu_opts_reset(&reopen_opts
);
1961 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
1962 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
1963 qemu_opts_reset(&reopen_opts
);
1965 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
1966 bdrv_reopen_multiple(bdrv_get_aio_context(bs
), brq
, &local_err
);
1968 error_report_err(local_err
);
1970 blk_set_enable_write_cache(blk
, !writethrough
);
1976 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
1980 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
1982 printf("Could not set breakpoint: %s\n", strerror(-ret
));
1988 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
1992 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
1994 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
2000 static const cmdinfo_t break_cmd
= {
2005 .args
= "event tag",
2006 .oneline
= "sets a breakpoint on event and tags the stopped "
2010 static const cmdinfo_t remove_break_cmd
= {
2011 .name
= "remove_break",
2014 .cfunc
= remove_break_f
,
2016 .oneline
= "remove a breakpoint by tag",
2019 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2023 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2025 printf("Could not resume request: %s\n", strerror(-ret
));
2031 static const cmdinfo_t resume_cmd
= {
2037 .oneline
= "resumes the request tagged as tag",
2040 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2042 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2043 aio_poll(blk_get_aio_context(blk
), true);
2049 static const cmdinfo_t wait_break_cmd
= {
2050 .name
= "wait_break",
2053 .cfunc
= wait_break_f
,
2055 .oneline
= "waits for the suspension of a request",
2058 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2063 static const cmdinfo_t abort_cmd
= {
2066 .flags
= CMD_NOFILE_OK
,
2067 .oneline
= "simulate a program crash using abort(3)",
2070 static void sigraise_help(void)
2074 " raises the given signal\n"
2077 " 'sigraise %i' - raises SIGTERM\n"
2079 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2080 " given to sigraise.\n"
2084 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2086 static const cmdinfo_t sigraise_cmd
= {
2088 .cfunc
= sigraise_f
,
2091 .flags
= CMD_NOFILE_OK
,
2093 .oneline
= "raises a signal",
2094 .help
= sigraise_help
,
2097 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2099 int64_t sig
= cvtnum(argv
[1]);
2101 print_cvtnum_err(sig
, argv
[1]);
2103 } else if (sig
> NSIG
) {
2104 printf("signal argument '%s' is too large to be a valid signal\n",
2109 /* Using raise() to kill this process does not necessarily flush all open
2110 * streams. At least stdout and stderr (although the latter should be
2111 * non-buffered anyway) should be flushed, though. */
2119 static void sleep_cb(void *opaque
)
2121 bool *expired
= opaque
;
2125 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2129 struct QEMUTimer
*timer
;
2130 bool expired
= false;
2132 ms
= strtol(argv
[1], &endptr
, 0);
2133 if (ms
< 0 || *endptr
!= '\0') {
2134 printf("%s is not a valid number\n", argv
[1]);
2138 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2139 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2142 main_loop_wait(false);
2150 static const cmdinfo_t sleep_cmd
= {
2155 .flags
= CMD_NOFILE_OK
,
2156 .oneline
= "waits for the given value in milliseconds",
2159 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2164 printf("%s ", ct
->name
);
2166 printf("(or %s) ", ct
->altname
);
2171 printf("%s ", ct
->args
);
2173 printf("-- %s\n", ct
->oneline
);
2176 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2178 help_oneline(cmd
, ct
);
2184 static void help_all(void)
2186 const cmdinfo_t
*ct
;
2188 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2189 help_oneline(ct
->name
, ct
);
2191 printf("\nUse 'help commandname' for extended help.\n");
2194 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2196 const cmdinfo_t
*ct
;
2203 ct
= find_command(argv
[1]);
2205 printf("command %s not found\n", argv
[1]);
2209 help_onecmd(argv
[1], ct
);
2213 static const cmdinfo_t help_cmd
= {
2219 .flags
= CMD_FLAG_GLOBAL
,
2220 .args
= "[command]",
2221 .oneline
= "help for one or all commands",
2224 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2228 const cmdinfo_t
*ct
;
2233 input
= g_strdup(cmd
);
2234 v
= breakline(input
, &c
);
2236 ct
= find_command(v
[0]);
2238 ctx
= blk
? blk_get_aio_context(blk
) : qemu_get_aio_context();
2239 aio_context_acquire(ctx
);
2240 done
= command(blk
, ct
, c
, v
);
2241 aio_context_release(ctx
);
2243 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2252 static void __attribute((constructor
)) init_qemuio_commands(void)
2254 /* initialize commands */
2255 qemuio_add_command(&help_cmd
);
2256 qemuio_add_command(&read_cmd
);
2257 qemuio_add_command(&readv_cmd
);
2258 qemuio_add_command(&write_cmd
);
2259 qemuio_add_command(&writev_cmd
);
2260 qemuio_add_command(&aio_read_cmd
);
2261 qemuio_add_command(&aio_write_cmd
);
2262 qemuio_add_command(&aio_flush_cmd
);
2263 qemuio_add_command(&flush_cmd
);
2264 qemuio_add_command(&truncate_cmd
);
2265 qemuio_add_command(&length_cmd
);
2266 qemuio_add_command(&info_cmd
);
2267 qemuio_add_command(&discard_cmd
);
2268 qemuio_add_command(&alloc_cmd
);
2269 qemuio_add_command(&map_cmd
);
2270 qemuio_add_command(&reopen_cmd
);
2271 qemuio_add_command(&break_cmd
);
2272 qemuio_add_command(&remove_break_cmd
);
2273 qemuio_add_command(&resume_cmd
);
2274 qemuio_add_command(&wait_break_cmd
);
2275 qemuio_add_command(&abort_cmd
);
2276 qemuio_add_command(&sleep_cmd
);
2277 qemuio_add_command(&sigraise_cmd
);