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
);
150 static void print_cvtnum_err(int64_t rc
, const char *arg
)
154 printf("Parsing error: non-numeric argument,"
155 " or extraneous/unrecognized suffix -- %s\n", arg
);
158 printf("Parsing error: argument too large -- %s\n", arg
);
161 printf("Parsing error: %s\n", arg
);
165 #define EXABYTES(x) ((long long)(x) << 60)
166 #define PETABYTES(x) ((long long)(x) << 50)
167 #define TERABYTES(x) ((long long)(x) << 40)
168 #define GIGABYTES(x) ((long long)(x) << 30)
169 #define MEGABYTES(x) ((long long)(x) << 20)
170 #define KILOBYTES(x) ((long long)(x) << 10)
172 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
173 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
174 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
175 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
176 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
177 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
179 static void cvtstr(double value
, char *str
, size_t size
)
184 if (value
>= EXABYTES(1)) {
186 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
187 } else if (value
>= PETABYTES(1)) {
189 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
190 } else if (value
>= TERABYTES(1)) {
192 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
193 } else if (value
>= GIGABYTES(1)) {
195 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
196 } else if (value
>= MEGABYTES(1)) {
198 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
199 } else if (value
>= KILOBYTES(1)) {
201 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
204 snprintf(str
, size
- 6, "%f", value
);
207 trim
= strstr(str
, ".000");
209 strcpy(trim
, suffix
);
217 static struct timeval
tsub(struct timeval t1
, struct timeval t2
)
219 t1
.tv_usec
-= t2
.tv_usec
;
220 if (t1
.tv_usec
< 0) {
221 t1
.tv_usec
+= 1000000;
224 t1
.tv_sec
-= t2
.tv_sec
;
228 static double tdiv(double value
, struct timeval tv
)
230 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
233 #define HOURS(sec) ((sec) / (60 * 60))
234 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
235 #define SECONDS(sec) ((sec) % 60)
239 TERSE_FIXED_TIME
= 0x1,
240 VERBOSE_FIXED_TIME
= 0x2,
243 static void timestr(struct timeval
*tv
, char *ts
, size_t size
, int format
)
245 double usec
= (double)tv
->tv_usec
/ 1000000.0;
247 if (format
& TERSE_FIXED_TIME
) {
248 if (!HOURS(tv
->tv_sec
)) {
249 snprintf(ts
, size
, "%u:%02u.%02u",
250 (unsigned int) MINUTES(tv
->tv_sec
),
251 (unsigned int) SECONDS(tv
->tv_sec
),
252 (unsigned int) (usec
* 100));
255 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
258 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
259 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
260 (unsigned int) HOURS(tv
->tv_sec
),
261 (unsigned int) MINUTES(tv
->tv_sec
),
262 (unsigned int) SECONDS(tv
->tv_sec
),
263 (unsigned int) (usec
* 100));
265 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
270 * Parse the pattern argument to various sub-commands.
272 * Because the pattern is used as an argument to memset it must evaluate
273 * to an unsigned integer that fits into a single byte.
275 static int parse_pattern(const char *arg
)
280 pattern
= strtol(arg
, &endptr
, 0);
281 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
282 printf("%s is not a valid pattern byte\n", arg
);
290 * Memory allocation helpers.
292 * Make sure memory is aligned by default, or purposefully misaligned if
293 * that is specified on the command line.
296 #define MISALIGN_OFFSET 16
297 static void *qemu_io_alloc(BlockBackend
*blk
, size_t len
, int pattern
)
301 if (qemuio_misalign
) {
302 len
+= MISALIGN_OFFSET
;
304 buf
= blk_blockalign(blk
, len
);
305 memset(buf
, pattern
, len
);
306 if (qemuio_misalign
) {
307 buf
+= MISALIGN_OFFSET
;
312 static void qemu_io_free(void *p
)
314 if (qemuio_misalign
) {
315 p
-= MISALIGN_OFFSET
;
320 static void dump_buffer(const void *buffer
, int64_t offset
, int64_t len
)
326 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
327 const uint8_t *s
= p
;
329 printf("%08" PRIx64
": ", offset
+ i
);
330 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++) {
334 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
345 static void print_report(const char *op
, struct timeval
*t
, int64_t offset
,
346 int64_t count
, int64_t total
, int cnt
, bool Cflag
)
348 char s1
[64], s2
[64], ts
[64];
350 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
352 cvtstr((double)total
, s1
, sizeof(s1
));
353 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
354 printf("%s %"PRId64
"/%"PRId64
" bytes at offset %" PRId64
"\n",
355 op
, total
, count
, offset
);
356 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
357 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
358 } else {/* bytes,ops,time,bytes/sec,ops/sec */
359 printf("%"PRId64
",%d,%s,%.3f,%.3f\n",
361 tdiv((double)total
, *t
),
362 tdiv((double)cnt
, *t
));
367 * Parse multiple length statements for vectored I/O, and construct an I/O
368 * vector matching it.
371 create_iovec(BlockBackend
*blk
, QEMUIOVector
*qiov
, char **argv
, int nr_iov
,
374 size_t *sizes
= g_new0(size_t, nr_iov
);
380 for (i
= 0; i
< nr_iov
; i
++) {
386 print_cvtnum_err(len
, arg
);
390 if (len
> BDRV_REQUEST_MAX_BYTES
) {
391 printf("Argument '%s' exceeds maximum size %" PRIu64
"\n", arg
,
392 (uint64_t)BDRV_REQUEST_MAX_BYTES
);
396 if (count
> BDRV_REQUEST_MAX_BYTES
- len
) {
397 printf("The total number of bytes exceed the maximum size %" PRIu64
398 "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES
);
406 qemu_iovec_init(qiov
, nr_iov
);
408 buf
= p
= qemu_io_alloc(blk
, count
, pattern
);
410 for (i
= 0; i
< nr_iov
; i
++) {
411 qemu_iovec_add(qiov
, p
, sizes
[i
]);
420 static int do_pread(BlockBackend
*blk
, char *buf
, int64_t offset
,
421 int64_t count
, int64_t *total
)
423 if (count
> INT_MAX
) {
427 *total
= blk_pread(blk
, offset
, (uint8_t *)buf
, count
);
434 static int do_pwrite(BlockBackend
*blk
, char *buf
, int64_t offset
,
435 int64_t count
, int flags
, int64_t *total
)
437 if (count
> INT_MAX
) {
441 *total
= blk_pwrite(blk
, offset
, (uint8_t *)buf
, count
, flags
);
458 static void coroutine_fn
co_pwrite_zeroes_entry(void *opaque
)
460 CoWriteZeroes
*data
= opaque
;
462 data
->ret
= blk_co_pwrite_zeroes(data
->blk
, data
->offset
, data
->count
,
466 *data
->total
= data
->ret
;
470 *data
->total
= data
->count
;
473 static int do_co_pwrite_zeroes(BlockBackend
*blk
, int64_t offset
,
474 int64_t count
, int flags
, int64_t *total
)
477 CoWriteZeroes data
= {
486 if (count
> INT_MAX
) {
490 co
= qemu_coroutine_create(co_pwrite_zeroes_entry
, &data
);
491 qemu_coroutine_enter(co
);
493 aio_poll(blk_get_aio_context(blk
), true);
502 static int do_write_compressed(BlockBackend
*blk
, char *buf
, int64_t offset
,
503 int64_t count
, int64_t *total
)
507 if (count
>> 9 > BDRV_REQUEST_MAX_SECTORS
) {
511 ret
= blk_pwrite_compressed(blk
, offset
, buf
, count
);
519 static int do_load_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
520 int64_t count
, int64_t *total
)
522 if (count
> INT_MAX
) {
526 *total
= blk_load_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
533 static int do_save_vmstate(BlockBackend
*blk
, char *buf
, int64_t offset
,
534 int64_t count
, int64_t *total
)
536 if (count
> INT_MAX
) {
540 *total
= blk_save_vmstate(blk
, (uint8_t *)buf
, offset
, count
);
547 #define NOT_DONE 0x7fffffff
548 static void aio_rw_done(void *opaque
, int ret
)
550 *(int *)opaque
= ret
;
553 static int do_aio_readv(BlockBackend
*blk
, QEMUIOVector
*qiov
,
554 int64_t offset
, int *total
)
556 int async_ret
= NOT_DONE
;
558 blk_aio_preadv(blk
, offset
, qiov
, 0, aio_rw_done
, &async_ret
);
559 while (async_ret
== NOT_DONE
) {
560 main_loop_wait(false);
564 return async_ret
< 0 ? async_ret
: 1;
567 static int do_aio_writev(BlockBackend
*blk
, QEMUIOVector
*qiov
,
568 int64_t offset
, int flags
, int *total
)
570 int async_ret
= NOT_DONE
;
572 blk_aio_pwritev(blk
, offset
, qiov
, flags
, aio_rw_done
, &async_ret
);
573 while (async_ret
== NOT_DONE
) {
574 main_loop_wait(false);
578 return async_ret
< 0 ? async_ret
: 1;
581 static void read_help(void)
585 " reads a range of bytes from the given offset\n"
588 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
590 " Reads a segment of the currently open file, optionally dumping it to the\n"
591 " standard output stream (with -v option) for subsequent inspection.\n"
592 " -b, -- read from the VM state rather than the virtual disk\n"
593 " -C, -- report statistics in a machine parsable format\n"
594 " -l, -- length for pattern verification (only with -P)\n"
595 " -p, -- ignored for backwards compatibility\n"
596 " -P, -- use a pattern to verify read data\n"
597 " -q, -- quiet mode, do not show I/O statistics\n"
598 " -s, -- start offset for pattern verification (only with -P)\n"
599 " -v, -- dump buffer to standard output\n"
603 static int read_f(BlockBackend
*blk
, int argc
, char **argv
);
605 static const cmdinfo_t read_cmd
= {
611 .args
= "[-abCqv] [-P pattern [-s off] [-l len]] off len",
612 .oneline
= "reads a number of bytes at a specified offset",
616 static int read_f(BlockBackend
*blk
, int argc
, char **argv
)
618 struct timeval t1
, t2
;
619 bool Cflag
= false, qflag
= false, vflag
= false;
620 bool Pflag
= false, sflag
= false, lflag
= false, bflag
= false;
625 /* Some compilers get confused and warn if this is not initialized. */
628 int64_t pattern_offset
= 0, pattern_count
= 0;
630 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != -1) {
640 pattern_count
= cvtnum(optarg
);
641 if (pattern_count
< 0) {
642 print_cvtnum_err(pattern_count
, optarg
);
647 /* Ignored for backwards compatibility */
651 pattern
= parse_pattern(optarg
);
661 pattern_offset
= cvtnum(optarg
);
662 if (pattern_offset
< 0) {
663 print_cvtnum_err(pattern_offset
, optarg
);
671 return qemuio_command_usage(&read_cmd
);
675 if (optind
!= argc
- 2) {
676 return qemuio_command_usage(&read_cmd
);
679 offset
= cvtnum(argv
[optind
]);
681 print_cvtnum_err(offset
, argv
[optind
]);
686 count
= cvtnum(argv
[optind
]);
688 print_cvtnum_err(count
, argv
[optind
]);
690 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
691 printf("length cannot exceed %" PRIu64
", given %s\n",
692 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
696 if (!Pflag
&& (lflag
|| sflag
)) {
697 return qemuio_command_usage(&read_cmd
);
701 pattern_count
= count
- pattern_offset
;
704 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
705 printf("pattern verification range exceeds end of read data\n");
710 if (offset
& 0x1ff) {
711 printf("offset %" PRId64
" is not sector aligned\n",
716 printf("count %"PRId64
" is not sector aligned\n",
722 buf
= qemu_io_alloc(blk
, count
, 0xab);
724 gettimeofday(&t1
, NULL
);
726 cnt
= do_load_vmstate(blk
, buf
, offset
, count
, &total
);
728 cnt
= do_pread(blk
, buf
, offset
, count
, &total
);
730 gettimeofday(&t2
, NULL
);
733 printf("read failed: %s\n", strerror(-cnt
));
738 void *cmp_buf
= g_malloc(pattern_count
);
739 memset(cmp_buf
, pattern
, pattern_count
);
740 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
741 printf("Pattern verification failed at offset %"
742 PRId64
", %"PRId64
" bytes\n",
743 offset
+ pattern_offset
, pattern_count
);
753 dump_buffer(buf
, offset
, count
);
756 /* Finally, report back -- -C gives a parsable format */
758 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
766 static void readv_help(void)
770 " reads a range of bytes from the given offset into multiple buffers\n"
773 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
775 " Reads a segment of the currently open file, optionally dumping it to the\n"
776 " standard output stream (with -v option) for subsequent inspection.\n"
777 " Uses multiple iovec buffers if more than one byte range is specified.\n"
778 " -C, -- report statistics in a machine parsable format\n"
779 " -P, -- use a pattern to verify read data\n"
780 " -v, -- dump buffer to standard output\n"
781 " -q, -- quiet mode, do not show I/O statistics\n"
785 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
);
787 static const cmdinfo_t readv_cmd
= {
792 .args
= "[-Cqv] [-P pattern] off len [len..]",
793 .oneline
= "reads a number of bytes at a specified offset",
797 static int readv_f(BlockBackend
*blk
, int argc
, char **argv
)
799 struct timeval t1
, t2
;
800 bool Cflag
= false, qflag
= false, vflag
= false;
804 /* Some compilers get confused and warn if this is not initialized. */
811 while ((c
= getopt(argc
, argv
, "CP:qv")) != -1) {
818 pattern
= parse_pattern(optarg
);
830 return qemuio_command_usage(&readv_cmd
);
834 if (optind
> argc
- 2) {
835 return qemuio_command_usage(&readv_cmd
);
839 offset
= cvtnum(argv
[optind
]);
841 print_cvtnum_err(offset
, argv
[optind
]);
846 nr_iov
= argc
- optind
;
847 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, 0xab);
852 gettimeofday(&t1
, NULL
);
853 cnt
= do_aio_readv(blk
, &qiov
, offset
, &total
);
854 gettimeofday(&t2
, NULL
);
857 printf("readv failed: %s\n", strerror(-cnt
));
862 void *cmp_buf
= g_malloc(qiov
.size
);
863 memset(cmp_buf
, pattern
, qiov
.size
);
864 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
865 printf("Pattern verification failed at offset %"
866 PRId64
", %zd bytes\n", offset
, qiov
.size
);
876 dump_buffer(buf
, offset
, qiov
.size
);
879 /* Finally, report back -- -C gives a parsable format */
881 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
884 qemu_iovec_destroy(&qiov
);
889 static void write_help(void)
893 " writes a range of bytes from the given offset\n"
896 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
898 " Writes into a segment of the currently open file, using a buffer\n"
899 " filled with a set pattern (0xcdcdcdcd).\n"
900 " -b, -- write to the VM state rather than the virtual disk\n"
901 " -c, -- write compressed data with blk_write_compressed\n"
902 " -f, -- use Force Unit Access semantics\n"
903 " -p, -- ignored for backwards compatibility\n"
904 " -P, -- use different pattern to fill file\n"
905 " -C, -- report statistics in a machine parsable format\n"
906 " -q, -- quiet mode, do not show I/O statistics\n"
907 " -u, -- with -z, allow unmapping\n"
908 " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
912 static int write_f(BlockBackend
*blk
, int argc
, char **argv
);
914 static const cmdinfo_t write_cmd
= {
920 .args
= "[-bcCfquz] [-P pattern] off len",
921 .oneline
= "writes a number of bytes at a specified offset",
925 static int write_f(BlockBackend
*blk
, int argc
, char **argv
)
927 struct timeval t1
, t2
;
928 bool Cflag
= false, qflag
= false, bflag
= false;
929 bool Pflag
= false, zflag
= false, cflag
= false;
935 /* Some compilers get confused and warn if this is not initialized. */
939 while ((c
= getopt(argc
, argv
, "bcCfpP:quz")) != -1) {
951 flags
|= BDRV_REQ_FUA
;
954 /* Ignored for backwards compatibility */
958 pattern
= parse_pattern(optarg
);
967 flags
|= BDRV_REQ_MAY_UNMAP
;
973 return qemuio_command_usage(&write_cmd
);
977 if (optind
!= argc
- 2) {
978 return qemuio_command_usage(&write_cmd
);
981 if (bflag
&& zflag
) {
982 printf("-b and -z cannot be specified at the same time\n");
986 if ((flags
& BDRV_REQ_FUA
) && (bflag
|| cflag
)) {
987 printf("-f and -b or -c cannot be specified at the same time\n");
991 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !zflag
) {
992 printf("-u requires -z to be specified\n");
996 if (zflag
&& Pflag
) {
997 printf("-z and -P cannot be specified at the same time\n");
1001 offset
= cvtnum(argv
[optind
]);
1003 print_cvtnum_err(offset
, argv
[optind
]);
1008 count
= cvtnum(argv
[optind
]);
1010 print_cvtnum_err(count
, argv
[optind
]);
1012 } else if (count
> BDRV_REQUEST_MAX_BYTES
) {
1013 printf("length cannot exceed %" PRIu64
", given %s\n",
1014 (uint64_t)BDRV_REQUEST_MAX_BYTES
, argv
[optind
]);
1018 if (bflag
|| cflag
) {
1019 if (offset
& 0x1ff) {
1020 printf("offset %" PRId64
" is not sector aligned\n",
1025 if (count
& 0x1ff) {
1026 printf("count %"PRId64
" is not sector aligned\n",
1033 buf
= qemu_io_alloc(blk
, count
, pattern
);
1036 gettimeofday(&t1
, NULL
);
1038 cnt
= do_save_vmstate(blk
, buf
, offset
, count
, &total
);
1040 cnt
= do_co_pwrite_zeroes(blk
, offset
, count
, flags
, &total
);
1042 cnt
= do_write_compressed(blk
, buf
, offset
, count
, &total
);
1044 cnt
= do_pwrite(blk
, buf
, offset
, count
, flags
, &total
);
1046 gettimeofday(&t2
, NULL
);
1049 printf("write failed: %s\n", strerror(-cnt
));
1057 /* Finally, report back -- -C gives a parsable format */
1059 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
1074 " writes a range of bytes from the given offset source from multiple buffers\n"
1077 " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1079 " Writes into a segment of the currently open file, using a buffer\n"
1080 " filled with a set pattern (0xcdcdcdcd).\n"
1081 " -P, -- use different pattern to fill file\n"
1082 " -C, -- report statistics in a machine parsable format\n"
1083 " -f, -- use Force Unit Access semantics\n"
1084 " -q, -- quiet mode, do not show I/O statistics\n"
1088 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
);
1090 static const cmdinfo_t writev_cmd
= {
1095 .args
= "[-Cfq] [-P pattern] off len [len..]",
1096 .oneline
= "writes a number of bytes at a specified offset",
1097 .help
= writev_help
,
1100 static int writev_f(BlockBackend
*blk
, int argc
, char **argv
)
1102 struct timeval t1
, t2
;
1103 bool Cflag
= false, qflag
= false;
1108 /* Some compilers get confused and warn if this is not initialized. */
1114 while ((c
= getopt(argc
, argv
, "CfqP:")) != -1) {
1120 flags
|= BDRV_REQ_FUA
;
1126 pattern
= parse_pattern(optarg
);
1132 return qemuio_command_usage(&writev_cmd
);
1136 if (optind
> argc
- 2) {
1137 return qemuio_command_usage(&writev_cmd
);
1140 offset
= cvtnum(argv
[optind
]);
1142 print_cvtnum_err(offset
, argv
[optind
]);
1147 nr_iov
= argc
- optind
;
1148 buf
= create_iovec(blk
, &qiov
, &argv
[optind
], nr_iov
, pattern
);
1153 gettimeofday(&t1
, NULL
);
1154 cnt
= do_aio_writev(blk
, &qiov
, offset
, flags
, &total
);
1155 gettimeofday(&t2
, NULL
);
1158 printf("writev failed: %s\n", strerror(-cnt
));
1166 /* Finally, report back -- -C gives a parsable format */
1168 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
1170 qemu_iovec_destroy(&qiov
);
1185 BlockAcctCookie acct
;
1190 static void aio_write_done(void *opaque
, int ret
)
1192 struct aio_ctx
*ctx
= opaque
;
1195 gettimeofday(&t2
, NULL
);
1199 printf("aio_write failed: %s\n", strerror(-ret
));
1200 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1204 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1210 /* Finally, report back -- -C gives a parsable format */
1211 t2
= tsub(t2
, ctx
->t1
);
1212 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1213 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1216 qemu_io_free(ctx
->buf
);
1217 qemu_iovec_destroy(&ctx
->qiov
);
1222 static void aio_read_done(void *opaque
, int ret
)
1224 struct aio_ctx
*ctx
= opaque
;
1227 gettimeofday(&t2
, NULL
);
1230 printf("readv failed: %s\n", strerror(-ret
));
1231 block_acct_failed(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1236 void *cmp_buf
= g_malloc(ctx
->qiov
.size
);
1238 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
1239 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
1240 printf("Pattern verification failed at offset %"
1241 PRId64
", %zd bytes\n", ctx
->offset
, ctx
->qiov
.size
);
1246 block_acct_done(blk_get_stats(ctx
->blk
), &ctx
->acct
);
1253 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
1256 /* Finally, report back -- -C gives a parsable format */
1257 t2
= tsub(t2
, ctx
->t1
);
1258 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
1259 ctx
->qiov
.size
, 1, ctx
->Cflag
);
1261 qemu_io_free(ctx
->buf
);
1262 qemu_iovec_destroy(&ctx
->qiov
);
1266 static void aio_read_help(void)
1270 " asynchronously reads a range of bytes from the given offset\n"
1273 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1275 " Reads a segment of the currently open file, optionally dumping it to the\n"
1276 " standard output stream (with -v option) for subsequent inspection.\n"
1277 " The read is performed asynchronously and the aio_flush command must be\n"
1278 " used to ensure all outstanding aio requests have been completed.\n"
1279 " -C, -- report statistics in a machine parsable format\n"
1280 " -P, -- use a pattern to verify read data\n"
1281 " -i, -- treat request as invalid, for exercising stats\n"
1282 " -v, -- dump buffer to standard output\n"
1283 " -q, -- quiet mode, do not show I/O statistics\n"
1287 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
);
1289 static const cmdinfo_t aio_read_cmd
= {
1291 .cfunc
= aio_read_f
,
1294 .args
= "[-Ciqv] [-P pattern] off len [len..]",
1295 .oneline
= "asynchronously reads a number of bytes",
1296 .help
= aio_read_help
,
1299 static int aio_read_f(BlockBackend
*blk
, int argc
, char **argv
)
1302 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1305 while ((c
= getopt(argc
, argv
, "CP:iqv")) != -1) {
1312 ctx
->pattern
= parse_pattern(optarg
);
1313 if (ctx
->pattern
< 0) {
1319 printf("injecting invalid read request\n");
1320 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1331 return qemuio_command_usage(&aio_read_cmd
);
1335 if (optind
> argc
- 2) {
1337 return qemuio_command_usage(&aio_read_cmd
);
1340 ctx
->offset
= cvtnum(argv
[optind
]);
1341 if (ctx
->offset
< 0) {
1342 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1348 nr_iov
= argc
- optind
;
1349 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
1350 if (ctx
->buf
== NULL
) {
1351 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_READ
);
1356 gettimeofday(&ctx
->t1
, NULL
);
1357 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1359 blk_aio_preadv(blk
, ctx
->offset
, &ctx
->qiov
, 0, aio_read_done
, ctx
);
1363 static void aio_write_help(void)
1367 " asynchronously writes a range of bytes from the given offset source\n"
1368 " from multiple buffers\n"
1371 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1373 " Writes into a segment of the currently open file, using a buffer\n"
1374 " filled with a set pattern (0xcdcdcdcd).\n"
1375 " The write is performed asynchronously and the aio_flush command must be\n"
1376 " used to ensure all outstanding aio requests have been completed.\n"
1377 " -P, -- use different pattern to fill file\n"
1378 " -C, -- report statistics in a machine parsable format\n"
1379 " -f, -- use Force Unit Access semantics\n"
1380 " -i, -- treat request as invalid, for exercising stats\n"
1381 " -q, -- quiet mode, do not show I/O statistics\n"
1382 " -u, -- with -z, allow unmapping\n"
1383 " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
1387 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
);
1389 static const cmdinfo_t aio_write_cmd
= {
1390 .name
= "aio_write",
1391 .cfunc
= aio_write_f
,
1394 .args
= "[-Cfiquz] [-P pattern] off len [len..]",
1395 .oneline
= "asynchronously writes a number of bytes",
1396 .help
= aio_write_help
,
1399 static int aio_write_f(BlockBackend
*blk
, int argc
, char **argv
)
1403 struct aio_ctx
*ctx
= g_new0(struct aio_ctx
, 1);
1407 while ((c
= getopt(argc
, argv
, "CfiqP:uz")) != -1) {
1413 flags
|= BDRV_REQ_FUA
;
1419 flags
|= BDRV_REQ_MAY_UNMAP
;
1422 pattern
= parse_pattern(optarg
);
1429 printf("injecting invalid write request\n");
1430 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1438 return qemuio_command_usage(&aio_write_cmd
);
1442 if (optind
> argc
- 2) {
1444 return qemuio_command_usage(&aio_write_cmd
);
1447 if (ctx
->zflag
&& optind
!= argc
- 2) {
1448 printf("-z supports only a single length parameter\n");
1453 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !ctx
->zflag
) {
1454 printf("-u requires -z to be specified\n");
1459 if (ctx
->zflag
&& ctx
->Pflag
) {
1460 printf("-z and -P cannot be specified at the same time\n");
1465 ctx
->offset
= cvtnum(argv
[optind
]);
1466 if (ctx
->offset
< 0) {
1467 print_cvtnum_err(ctx
->offset
, argv
[optind
]);
1474 int64_t count
= cvtnum(argv
[optind
]);
1476 print_cvtnum_err(count
, argv
[optind
]);
1481 ctx
->qiov
.size
= count
;
1482 blk_aio_pwrite_zeroes(blk
, ctx
->offset
, count
, flags
, aio_write_done
,
1485 nr_iov
= argc
- optind
;
1486 ctx
->buf
= create_iovec(blk
, &ctx
->qiov
, &argv
[optind
], nr_iov
,
1488 if (ctx
->buf
== NULL
) {
1489 block_acct_invalid(blk_get_stats(blk
), BLOCK_ACCT_WRITE
);
1494 gettimeofday(&ctx
->t1
, NULL
);
1495 block_acct_start(blk_get_stats(blk
), &ctx
->acct
, ctx
->qiov
.size
,
1498 blk_aio_pwritev(blk
, ctx
->offset
, &ctx
->qiov
, flags
, aio_write_done
,
1504 static int aio_flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1506 BlockAcctCookie cookie
;
1507 block_acct_start(blk_get_stats(blk
), &cookie
, 0, BLOCK_ACCT_FLUSH
);
1509 block_acct_done(blk_get_stats(blk
), &cookie
);
1513 static const cmdinfo_t aio_flush_cmd
= {
1514 .name
= "aio_flush",
1515 .cfunc
= aio_flush_f
,
1516 .oneline
= "completes all outstanding aio requests"
1519 static int flush_f(BlockBackend
*blk
, int argc
, char **argv
)
1525 static const cmdinfo_t flush_cmd
= {
1529 .oneline
= "flush all in-core file state to disk",
1532 static int truncate_f(BlockBackend
*blk
, int argc
, char **argv
)
1537 offset
= cvtnum(argv
[1]);
1539 print_cvtnum_err(offset
, argv
[1]);
1543 ret
= blk_truncate(blk
, offset
);
1545 printf("truncate: %s\n", strerror(-ret
));
1552 static const cmdinfo_t truncate_cmd
= {
1555 .cfunc
= truncate_f
,
1559 .oneline
= "truncates the current file at the given offset",
1562 static int length_f(BlockBackend
*blk
, int argc
, char **argv
)
1567 size
= blk_getlength(blk
);
1569 printf("getlength: %s\n", strerror(-size
));
1573 cvtstr(size
, s1
, sizeof(s1
));
1579 static const cmdinfo_t length_cmd
= {
1583 .oneline
= "gets the length of the current file",
1587 static int info_f(BlockBackend
*blk
, int argc
, char **argv
)
1589 BlockDriverState
*bs
= blk_bs(blk
);
1590 BlockDriverInfo bdi
;
1591 ImageInfoSpecific
*spec_info
;
1592 char s1
[64], s2
[64];
1595 if (bs
->drv
&& bs
->drv
->format_name
) {
1596 printf("format name: %s\n", bs
->drv
->format_name
);
1598 if (bs
->drv
&& bs
->drv
->protocol_name
) {
1599 printf("format name: %s\n", bs
->drv
->protocol_name
);
1602 ret
= bdrv_get_info(bs
, &bdi
);
1607 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1608 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1610 printf("cluster size: %s\n", s1
);
1611 printf("vm state offset: %s\n", s2
);
1613 spec_info
= bdrv_get_specific_info(bs
);
1615 printf("Format specific information:\n");
1616 bdrv_image_info_specific_dump(fprintf
, stdout
, spec_info
);
1617 qapi_free_ImageInfoSpecific(spec_info
);
1625 static const cmdinfo_t info_cmd
= {
1629 .oneline
= "prints information about the current file",
1632 static void discard_help(void)
1636 " discards a range of bytes from the given offset\n"
1639 " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1641 " Discards a segment of the currently open file.\n"
1642 " -C, -- report statistics in a machine parsable format\n"
1643 " -q, -- quiet mode, do not show I/O statistics\n"
1647 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
);
1649 static const cmdinfo_t discard_cmd
= {
1655 .args
= "[-Cq] off len",
1656 .oneline
= "discards a number of bytes at a specified offset",
1657 .help
= discard_help
,
1660 static int discard_f(BlockBackend
*blk
, int argc
, char **argv
)
1662 struct timeval t1
, t2
;
1663 bool Cflag
= false, qflag
= false;
1665 int64_t offset
, count
;
1667 while ((c
= getopt(argc
, argv
, "Cq")) != -1) {
1676 return qemuio_command_usage(&discard_cmd
);
1680 if (optind
!= argc
- 2) {
1681 return qemuio_command_usage(&discard_cmd
);
1684 offset
= cvtnum(argv
[optind
]);
1686 print_cvtnum_err(offset
, argv
[optind
]);
1691 count
= cvtnum(argv
[optind
]);
1693 print_cvtnum_err(count
, argv
[optind
]);
1695 } else if (count
>> BDRV_SECTOR_BITS
> BDRV_REQUEST_MAX_SECTORS
) {
1696 printf("length cannot exceed %"PRIu64
", given %s\n",
1697 (uint64_t)BDRV_REQUEST_MAX_SECTORS
<< BDRV_SECTOR_BITS
,
1702 gettimeofday(&t1
, NULL
);
1703 ret
= blk_pdiscard(blk
, offset
, count
);
1704 gettimeofday(&t2
, NULL
);
1707 printf("discard failed: %s\n", strerror(-ret
));
1711 /* Finally, report back -- -C gives a parsable format */
1714 print_report("discard", &t2
, offset
, count
, count
, 1, Cflag
);
1721 static int alloc_f(BlockBackend
*blk
, int argc
, char **argv
)
1723 BlockDriverState
*bs
= blk_bs(blk
);
1724 int64_t offset
, sector_num
, nb_sectors
, remaining
;
1729 offset
= cvtnum(argv
[1]);
1731 print_cvtnum_err(offset
, argv
[1]);
1733 } else if (offset
& 0x1ff) {
1734 printf("offset %" PRId64
" is not sector aligned\n",
1740 nb_sectors
= cvtnum(argv
[2]);
1741 if (nb_sectors
< 0) {
1742 print_cvtnum_err(nb_sectors
, argv
[2]);
1744 } else if (nb_sectors
> INT_MAX
) {
1745 printf("length argument cannot exceed %d, given %s\n",
1753 remaining
= nb_sectors
;
1755 sector_num
= offset
>> 9;
1757 ret
= bdrv_is_allocated(bs
, sector_num
, remaining
, &num
);
1759 printf("is_allocated failed: %s\n", strerror(-ret
));
1768 nb_sectors
-= remaining
;
1773 cvtstr(offset
, s1
, sizeof(s1
));
1775 printf("%"PRId64
"/%"PRId64
" sectors allocated at offset %s\n",
1776 sum_alloc
, nb_sectors
, s1
);
1780 static const cmdinfo_t alloc_cmd
= {
1786 .args
= "off [sectors]",
1787 .oneline
= "checks if a sector is present in the file",
1791 static int map_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
1792 int64_t nb_sectors
, int64_t *pnum
)
1794 int num
, num_checked
;
1797 num_checked
= MIN(nb_sectors
, INT_MAX
);
1798 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1806 while (nb_sectors
> 0 && ret
== firstret
) {
1810 num_checked
= MIN(nb_sectors
, INT_MAX
);
1811 ret
= bdrv_is_allocated(bs
, sector_num
, num_checked
, &num
);
1812 if (ret
== firstret
&& num
) {
1822 static int map_f(BlockBackend
*blk
, int argc
, char **argv
)
1825 int64_t nb_sectors
, total_sectors
;
1832 total_sectors
= blk_nb_sectors(blk
);
1833 if (total_sectors
< 0) {
1834 error_report("Failed to query image length: %s",
1835 strerror(-total_sectors
));
1839 nb_sectors
= total_sectors
;
1842 ret
= map_is_allocated(blk_bs(blk
), offset
, nb_sectors
, &num
);
1844 error_report("Failed to get allocation status: %s", strerror(-ret
));
1847 error_report("Unexpected end of image");
1851 retstr
= ret
? " allocated" : "not allocated";
1852 cvtstr(offset
<< 9ULL, s1
, sizeof(s1
));
1853 printf("[% 24" PRId64
"] % 8" PRId64
"/% 8" PRId64
" sectors %s "
1854 "at offset %s (%d)\n",
1855 offset
<< 9ULL, num
, nb_sectors
, retstr
, s1
, ret
);
1859 } while (offset
< total_sectors
);
1864 static const cmdinfo_t map_cmd
= {
1870 .oneline
= "prints the allocated areas of a file",
1873 static void reopen_help(void)
1877 " Changes the open options of an already opened image\n"
1880 " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
1882 " -r, -- Reopen the image read-only\n"
1883 " -c, -- Change the cache mode to the given value\n"
1884 " -o, -- Changes block driver options (cf. 'open' command)\n"
1888 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
);
1890 static QemuOptsList reopen_opts
= {
1892 .merge_lists
= true,
1893 .head
= QTAILQ_HEAD_INITIALIZER(reopen_opts
.head
),
1895 /* no elements => accept any params */
1896 { /* end of list */ }
1900 static const cmdinfo_t reopen_cmd
= {
1905 .args
= "[-r] [-c cache] [-o options]",
1906 .oneline
= "reopens an image with new options",
1907 .help
= reopen_help
,
1910 static int reopen_f(BlockBackend
*blk
, int argc
, char **argv
)
1912 BlockDriverState
*bs
= blk_bs(blk
);
1916 int flags
= bs
->open_flags
;
1917 bool writethrough
= !blk_enable_write_cache(blk
);
1919 BlockReopenQueue
*brq
;
1920 Error
*local_err
= NULL
;
1922 while ((c
= getopt(argc
, argv
, "c:o:r")) != -1) {
1925 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
1926 error_report("Invalid cache option: %s", optarg
);
1931 if (!qemu_opts_parse_noisily(&reopen_opts
, optarg
, 0)) {
1932 qemu_opts_reset(&reopen_opts
);
1937 flags
&= ~BDRV_O_RDWR
;
1940 qemu_opts_reset(&reopen_opts
);
1941 return qemuio_command_usage(&reopen_cmd
);
1945 if (optind
!= argc
) {
1946 qemu_opts_reset(&reopen_opts
);
1947 return qemuio_command_usage(&reopen_cmd
);
1950 if (writethrough
!= blk_enable_write_cache(blk
) &&
1951 blk_get_attached_dev(blk
))
1953 error_report("Cannot change cache.writeback: Device attached");
1954 qemu_opts_reset(&reopen_opts
);
1958 qopts
= qemu_opts_find(&reopen_opts
, NULL
);
1959 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
1960 qemu_opts_reset(&reopen_opts
);
1962 brq
= bdrv_reopen_queue(NULL
, bs
, opts
, flags
);
1963 bdrv_reopen_multiple(bdrv_get_aio_context(bs
), brq
, &local_err
);
1965 error_report_err(local_err
);
1967 blk_set_enable_write_cache(blk
, !writethrough
);
1973 static int break_f(BlockBackend
*blk
, int argc
, char **argv
)
1977 ret
= bdrv_debug_breakpoint(blk_bs(blk
), argv
[1], argv
[2]);
1979 printf("Could not set breakpoint: %s\n", strerror(-ret
));
1985 static int remove_break_f(BlockBackend
*blk
, int argc
, char **argv
)
1989 ret
= bdrv_debug_remove_breakpoint(blk_bs(blk
), argv
[1]);
1991 printf("Could not remove breakpoint %s: %s\n", argv
[1], strerror(-ret
));
1997 static const cmdinfo_t break_cmd
= {
2002 .args
= "event tag",
2003 .oneline
= "sets a breakpoint on event and tags the stopped "
2007 static const cmdinfo_t remove_break_cmd
= {
2008 .name
= "remove_break",
2011 .cfunc
= remove_break_f
,
2013 .oneline
= "remove a breakpoint by tag",
2016 static int resume_f(BlockBackend
*blk
, int argc
, char **argv
)
2020 ret
= bdrv_debug_resume(blk_bs(blk
), argv
[1]);
2022 printf("Could not resume request: %s\n", strerror(-ret
));
2028 static const cmdinfo_t resume_cmd
= {
2034 .oneline
= "resumes the request tagged as tag",
2037 static int wait_break_f(BlockBackend
*blk
, int argc
, char **argv
)
2039 while (!bdrv_debug_is_suspended(blk_bs(blk
), argv
[1])) {
2040 aio_poll(blk_get_aio_context(blk
), true);
2046 static const cmdinfo_t wait_break_cmd
= {
2047 .name
= "wait_break",
2050 .cfunc
= wait_break_f
,
2052 .oneline
= "waits for the suspension of a request",
2055 static int abort_f(BlockBackend
*blk
, int argc
, char **argv
)
2060 static const cmdinfo_t abort_cmd
= {
2063 .flags
= CMD_NOFILE_OK
,
2064 .oneline
= "simulate a program crash using abort(3)",
2067 static void sigraise_help(void)
2071 " raises the given signal\n"
2074 " 'sigraise %i' - raises SIGTERM\n"
2076 " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2077 " given to sigraise.\n"
2081 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
);
2083 static const cmdinfo_t sigraise_cmd
= {
2085 .cfunc
= sigraise_f
,
2088 .flags
= CMD_NOFILE_OK
,
2090 .oneline
= "raises a signal",
2091 .help
= sigraise_help
,
2094 static int sigraise_f(BlockBackend
*blk
, int argc
, char **argv
)
2096 int64_t sig
= cvtnum(argv
[1]);
2098 print_cvtnum_err(sig
, argv
[1]);
2100 } else if (sig
> NSIG
) {
2101 printf("signal argument '%s' is too large to be a valid signal\n",
2106 /* Using raise() to kill this process does not necessarily flush all open
2107 * streams. At least stdout and stderr (although the latter should be
2108 * non-buffered anyway) should be flushed, though. */
2116 static void sleep_cb(void *opaque
)
2118 bool *expired
= opaque
;
2122 static int sleep_f(BlockBackend
*blk
, int argc
, char **argv
)
2126 struct QEMUTimer
*timer
;
2127 bool expired
= false;
2129 ms
= strtol(argv
[1], &endptr
, 0);
2130 if (ms
< 0 || *endptr
!= '\0') {
2131 printf("%s is not a valid number\n", argv
[1]);
2135 timer
= timer_new_ns(QEMU_CLOCK_HOST
, sleep_cb
, &expired
);
2136 timer_mod(timer
, qemu_clock_get_ns(QEMU_CLOCK_HOST
) + SCALE_MS
* ms
);
2139 main_loop_wait(false);
2147 static const cmdinfo_t sleep_cmd
= {
2152 .flags
= CMD_NOFILE_OK
,
2153 .oneline
= "waits for the given value in milliseconds",
2156 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
)
2161 printf("%s ", ct
->name
);
2163 printf("(or %s) ", ct
->altname
);
2168 printf("%s ", ct
->args
);
2170 printf("-- %s\n", ct
->oneline
);
2173 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
)
2175 help_oneline(cmd
, ct
);
2181 static void help_all(void)
2183 const cmdinfo_t
*ct
;
2185 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
2186 help_oneline(ct
->name
, ct
);
2188 printf("\nUse 'help commandname' for extended help.\n");
2191 static int help_f(BlockBackend
*blk
, int argc
, char **argv
)
2193 const cmdinfo_t
*ct
;
2200 ct
= find_command(argv
[1]);
2202 printf("command %s not found\n", argv
[1]);
2206 help_onecmd(argv
[1], ct
);
2210 static const cmdinfo_t help_cmd
= {
2216 .flags
= CMD_FLAG_GLOBAL
,
2217 .args
= "[command]",
2218 .oneline
= "help for one or all commands",
2221 bool qemuio_command(BlockBackend
*blk
, const char *cmd
)
2225 const cmdinfo_t
*ct
;
2230 input
= g_strdup(cmd
);
2231 v
= breakline(input
, &c
);
2233 ct
= find_command(v
[0]);
2235 ctx
= blk
? blk_get_aio_context(blk
) : qemu_get_aio_context();
2236 aio_context_acquire(ctx
);
2237 done
= command(blk
, ct
, c
, v
);
2238 aio_context_release(ctx
);
2240 fprintf(stderr
, "command \"%s\" not found\n", v
[0]);
2249 static void __attribute((constructor
)) init_qemuio_commands(void)
2251 /* initialize commands */
2252 qemuio_add_command(&help_cmd
);
2253 qemuio_add_command(&read_cmd
);
2254 qemuio_add_command(&readv_cmd
);
2255 qemuio_add_command(&write_cmd
);
2256 qemuio_add_command(&writev_cmd
);
2257 qemuio_add_command(&aio_read_cmd
);
2258 qemuio_add_command(&aio_write_cmd
);
2259 qemuio_add_command(&aio_flush_cmd
);
2260 qemuio_add_command(&flush_cmd
);
2261 qemuio_add_command(&truncate_cmd
);
2262 qemuio_add_command(&length_cmd
);
2263 qemuio_add_command(&info_cmd
);
2264 qemuio_add_command(&discard_cmd
);
2265 qemuio_add_command(&alloc_cmd
);
2266 qemuio_add_command(&map_cmd
);
2267 qemuio_add_command(&reopen_cmd
);
2268 qemuio_add_command(&break_cmd
);
2269 qemuio_add_command(&remove_break_cmd
);
2270 qemuio_add_command(&resume_cmd
);
2271 qemuio_add_command(&wait_break_cmd
);
2272 qemuio_add_command(&abort_cmd
);
2273 qemuio_add_command(&sleep_cmd
);
2274 qemuio_add_command(&sigraise_cmd
);