2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
10 #include <sys/types.h>
15 #include "qemu-common.h"
16 #include "block_int.h"
19 #define VERSION "0.0.1"
21 #define CMD_NOFILE_OK 0x01
24 static BlockDriverState
*bs
;
29 * Parse the pattern argument to various sub-commands.
31 * Because the pattern is used as an argument to memset it must evaluate
32 * to an unsigned integer that fits into a single byte.
34 static int parse_pattern(const char *arg
)
39 pattern
= strtol(arg
, &endptr
, 0);
40 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
41 printf("%s is not a valid pattern byte\n", arg
);
49 * Memory allocation helpers.
51 * Make sure memory is aligned by default, or purposefully misaligned if
52 * that is specified on the command line.
55 #define MISALIGN_OFFSET 16
56 static void *qemu_io_alloc(size_t len
, int pattern
)
61 len
+= MISALIGN_OFFSET
;
62 buf
= qemu_memalign(512, len
);
63 memset(buf
, pattern
, len
);
65 buf
+= MISALIGN_OFFSET
;
69 static void qemu_io_free(void *p
)
77 dump_buffer(const void *buffer
, int64_t offset
, int len
)
82 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
85 printf("%08llx: ", (unsigned long long)offset
+ i
);
86 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
89 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
100 print_report(const char *op
, struct timeval
*t
, int64_t offset
,
101 int count
, int total
, int cnt
, int Cflag
)
103 char s1
[64], s2
[64], ts
[64];
105 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
107 cvtstr((double)total
, s1
, sizeof(s1
));
108 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
109 printf("%s %d/%d bytes at offset %lld\n",
110 op
, total
, count
, (long long)offset
);
111 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
112 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
113 } else {/* bytes,ops,time,bytes/sec,ops/sec */
114 printf("%d,%d,%s,%.3f,%.3f\n",
116 tdiv((double)total
, *t
),
117 tdiv((double)cnt
, *t
));
122 * Parse multiple length statements for vectored I/O, and construct an I/O
123 * vector matching it.
126 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
128 size_t *sizes
= calloc(nr_iov
, sizeof(size_t));
133 for (i
= 0; i
< nr_iov
; i
++) {
139 printf("non-numeric length argument -- %s\n", arg
);
143 /* should be SIZE_T_MAX, but that doesn't exist */
144 if (len
> UINT_MAX
) {
145 printf("too large length argument -- %s\n", arg
);
150 printf("length argument %lld is not sector aligned\n",
159 qemu_iovec_init(qiov
, nr_iov
);
161 buf
= p
= qemu_io_alloc(count
, pattern
);
163 for (i
= 0; i
< nr_iov
; i
++) {
164 qemu_iovec_add(qiov
, p
, sizes
[i
]);
172 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
176 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
183 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
187 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
194 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
196 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
202 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
204 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
210 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
212 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
218 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
220 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
226 #define NOT_DONE 0x7fffffff
227 static void aio_rw_done(void *opaque
, int ret
)
229 *(int *)opaque
= ret
;
232 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
234 BlockDriverAIOCB
*acb
;
235 int async_ret
= NOT_DONE
;
237 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
238 aio_rw_done
, &async_ret
);
242 while (async_ret
== NOT_DONE
)
246 return async_ret
< 0 ? async_ret
: 1;
249 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
251 BlockDriverAIOCB
*acb
;
252 int async_ret
= NOT_DONE
;
254 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
255 aio_rw_done
, &async_ret
);
259 while (async_ret
== NOT_DONE
)
263 return async_ret
< 0 ? async_ret
: 1;
267 static const cmdinfo_t read_cmd
;
274 " reads a range of bytes from the given offset\n"
277 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
279 " Reads a segment of the currently open file, optionally dumping it to the\n"
280 " standard output stream (with -v option) for subsequent inspection.\n"
281 " -b, -- read from the VM state rather than the virtual disk\n"
282 " -C, -- report statistics in a machine parsable format\n"
283 " -l, -- length for pattern verification (only with -P)\n"
284 " -p, -- use bdrv_pread to read the file\n"
285 " -P, -- use a pattern to verify read data\n"
286 " -q, -- quite mode, do not show I/O statistics\n"
287 " -s, -- start offset for pattern verification (only with -P)\n"
288 " -v, -- dump buffer to standard output\n"
293 read_f(int argc
, char **argv
)
295 struct timeval t1
, t2
;
296 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
297 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
302 /* Some compilers get confused and warn if this is not initialized. */
304 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
306 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
316 pattern_count
= cvtnum(optarg
);
317 if (pattern_count
< 0) {
318 printf("non-numeric length argument -- %s\n", optarg
);
327 pattern
= parse_pattern(optarg
);
336 pattern_offset
= cvtnum(optarg
);
337 if (pattern_offset
< 0) {
338 printf("non-numeric length argument -- %s\n", optarg
);
346 return command_usage(&read_cmd
);
350 if (optind
!= argc
- 2)
351 return command_usage(&read_cmd
);
353 if (bflag
&& pflag
) {
354 printf("-b and -p cannot be specified at the same time\n");
358 offset
= cvtnum(argv
[optind
]);
360 printf("non-numeric length argument -- %s\n", argv
[optind
]);
365 count
= cvtnum(argv
[optind
]);
367 printf("non-numeric length argument -- %s\n", argv
[optind
]);
371 if (!Pflag
&& (lflag
|| sflag
)) {
372 return command_usage(&read_cmd
);
376 pattern_count
= count
- pattern_offset
;
379 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
380 printf("pattern verfication range exceeds end of read data\n");
385 if (offset
& 0x1ff) {
386 printf("offset %lld is not sector aligned\n",
391 printf("count %d is not sector aligned\n",
397 buf
= qemu_io_alloc(count
, 0xab);
399 gettimeofday(&t1
, NULL
);
401 cnt
= do_pread(buf
, offset
, count
, &total
);
403 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
405 cnt
= do_read(buf
, offset
, count
, &total
);
406 gettimeofday(&t2
, NULL
);
409 printf("read failed: %s\n", strerror(-cnt
));
414 void* cmp_buf
= malloc(pattern_count
);
415 memset(cmp_buf
, pattern
, pattern_count
);
416 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
417 printf("Pattern verification failed at offset %lld, "
419 (long long) offset
+ pattern_offset
, pattern_count
);
428 dump_buffer(buf
, offset
, count
);
430 /* Finally, report back -- -C gives a parsable format */
432 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
440 static const cmdinfo_t read_cmd
= {
446 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
447 .oneline
= "reads a number of bytes at a specified offset",
451 static const cmdinfo_t readv_cmd
;
458 " reads a range of bytes from the given offset into multiple buffers\n"
461 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
463 " Reads a segment of the currently open file, optionally dumping it to the\n"
464 " standard output stream (with -v option) for subsequent inspection.\n"
465 " Uses multiple iovec buffers if more than one byte range is specified.\n"
466 " -C, -- report statistics in a machine parsable format\n"
467 " -P, -- use a pattern to verify read data\n"
468 " -v, -- dump buffer to standard output\n"
469 " -q, -- quite mode, do not show I/O statistics\n"
474 readv_f(int argc
, char **argv
)
476 struct timeval t1
, t2
;
477 int Cflag
= 0, qflag
= 0, vflag
= 0;
487 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
494 pattern
= parse_pattern(optarg
);
505 return command_usage(&readv_cmd
);
509 if (optind
> argc
- 2)
510 return command_usage(&readv_cmd
);
513 offset
= cvtnum(argv
[optind
]);
515 printf("non-numeric length argument -- %s\n", argv
[optind
]);
520 if (offset
& 0x1ff) {
521 printf("offset %lld is not sector aligned\n",
526 nr_iov
= argc
- optind
;
527 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
529 gettimeofday(&t1
, NULL
);
530 cnt
= do_aio_readv(&qiov
, offset
, &total
);
531 gettimeofday(&t2
, NULL
);
534 printf("readv failed: %s\n", strerror(-cnt
));
539 void* cmp_buf
= malloc(qiov
.size
);
540 memset(cmp_buf
, pattern
, qiov
.size
);
541 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
542 printf("Pattern verification failed at offset %lld, "
544 (long long) offset
, qiov
.size
);
553 dump_buffer(buf
, offset
, qiov
.size
);
555 /* Finally, report back -- -C gives a parsable format */
557 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
564 static const cmdinfo_t readv_cmd
= {
569 .args
= "[-Cqv] [-P pattern ] off len [len..]",
570 .oneline
= "reads a number of bytes at a specified offset",
574 static const cmdinfo_t write_cmd
;
581 " writes a range of bytes from the given offset\n"
584 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
586 " Writes into a segment of the currently open file, using a buffer\n"
587 " filled with a set pattern (0xcdcdcdcd).\n"
588 " -b, -- write to the VM state rather than the virtual disk\n"
589 " -p, -- use bdrv_pwrite to write the file\n"
590 " -P, -- use different pattern to fill file\n"
591 " -C, -- report statistics in a machine parsable format\n"
592 " -q, -- quite mode, do not show I/O statistics\n"
597 write_f(int argc
, char **argv
)
599 struct timeval t1
, t2
;
600 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0;
605 /* Some compilers get confused and warn if this is not initialized. */
609 while ((c
= getopt(argc
, argv
, "bCpP:q")) != EOF
) {
621 pattern
= parse_pattern(optarg
);
629 return command_usage(&write_cmd
);
633 if (optind
!= argc
- 2)
634 return command_usage(&write_cmd
);
636 if (bflag
&& pflag
) {
637 printf("-b and -p cannot be specified at the same time\n");
641 offset
= cvtnum(argv
[optind
]);
643 printf("non-numeric length argument -- %s\n", argv
[optind
]);
648 count
= cvtnum(argv
[optind
]);
650 printf("non-numeric length argument -- %s\n", argv
[optind
]);
655 if (offset
& 0x1ff) {
656 printf("offset %lld is not sector aligned\n",
662 printf("count %d is not sector aligned\n",
668 buf
= qemu_io_alloc(count
, pattern
);
670 gettimeofday(&t1
, NULL
);
672 cnt
= do_pwrite(buf
, offset
, count
, &total
);
674 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
676 cnt
= do_write(buf
, offset
, count
, &total
);
677 gettimeofday(&t2
, NULL
);
680 printf("write failed: %s\n", strerror(-cnt
));
687 /* Finally, report back -- -C gives a parsable format */
689 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
697 static const cmdinfo_t write_cmd
= {
703 .args
= "[-abCpq] [-P pattern ] off len",
704 .oneline
= "writes a number of bytes at a specified offset",
708 static const cmdinfo_t writev_cmd
;
715 " writes a range of bytes from the given offset source from multiple buffers\n"
718 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
720 " Writes into a segment of the currently open file, using a buffer\n"
721 " filled with a set pattern (0xcdcdcdcd).\n"
722 " -P, -- use different pattern to fill file\n"
723 " -C, -- report statistics in a machine parsable format\n"
724 " -q, -- quite mode, do not show I/O statistics\n"
729 writev_f(int argc
, char **argv
)
731 struct timeval t1
, t2
;
732 int Cflag
= 0, qflag
= 0;
741 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
750 pattern
= parse_pattern(optarg
);
755 return command_usage(&writev_cmd
);
759 if (optind
> argc
- 2)
760 return command_usage(&writev_cmd
);
762 offset
= cvtnum(argv
[optind
]);
764 printf("non-numeric length argument -- %s\n", argv
[optind
]);
769 if (offset
& 0x1ff) {
770 printf("offset %lld is not sector aligned\n",
775 nr_iov
= argc
- optind
;
776 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
778 gettimeofday(&t1
, NULL
);
779 cnt
= do_aio_writev(&qiov
, offset
, &total
);
780 gettimeofday(&t2
, NULL
);
783 printf("writev failed: %s\n", strerror(-cnt
));
790 /* Finally, report back -- -C gives a parsable format */
792 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
798 static const cmdinfo_t writev_cmd
= {
803 .args
= "[-Cq] [-P pattern ] off len [len..]",
804 .oneline
= "writes a number of bytes at a specified offset",
821 aio_write_done(void *opaque
, int ret
)
823 struct aio_ctx
*ctx
= opaque
;
826 gettimeofday(&t2
, NULL
);
830 printf("aio_write failed: %s\n", strerror(-ret
));
838 /* Finally, report back -- -C gives a parsable format */
839 t2
= tsub(t2
, ctx
->t1
);
840 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
841 ctx
->qiov
.size
, 1, ctx
->Cflag
);
843 qemu_io_free(ctx
->buf
);
847 static const cmdinfo_t aio_read_cmd
;
850 aio_read_done(void *opaque
, int ret
)
852 struct aio_ctx
*ctx
= opaque
;
855 gettimeofday(&t2
, NULL
);
858 printf("readv failed: %s\n", strerror(-ret
));
863 void *cmp_buf
= malloc(ctx
->qiov
.size
);
865 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
866 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
867 printf("Pattern verification failed at offset %lld, "
869 (long long) ctx
->offset
, ctx
->qiov
.size
);
879 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
882 /* Finally, report back -- -C gives a parsable format */
883 t2
= tsub(t2
, ctx
->t1
);
884 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
885 ctx
->qiov
.size
, 1, ctx
->Cflag
);
887 qemu_io_free(ctx
->buf
);
896 " asynchronously reads a range of bytes from the given offset\n"
899 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
901 " Reads a segment of the currently open file, optionally dumping it to the\n"
902 " standard output stream (with -v option) for subsequent inspection.\n"
903 " The read is performed asynchronously and should the aio_flush command \n"
904 " should be used to ensure all outstanding aio requests have been completed\n"
905 " -C, -- report statistics in a machine parsable format\n"
906 " -P, -- use a pattern to verify read data\n"
907 " -v, -- dump buffer to standard output\n"
908 " -q, -- quite mode, do not show I/O statistics\n"
913 aio_read_f(int argc
, char **argv
)
916 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
917 BlockDriverAIOCB
*acb
;
919 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
926 ctx
->pattern
= parse_pattern(optarg
);
927 if (ctx
->pattern
< 0)
938 return command_usage(&aio_read_cmd
);
942 if (optind
> argc
- 2) {
944 return command_usage(&aio_read_cmd
);
947 ctx
->offset
= cvtnum(argv
[optind
]);
948 if (ctx
->offset
< 0) {
949 printf("non-numeric length argument -- %s\n", argv
[optind
]);
955 if (ctx
->offset
& 0x1ff) {
956 printf("offset %lld is not sector aligned\n",
957 (long long)ctx
->offset
);
962 nr_iov
= argc
- optind
;
963 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
965 gettimeofday(&ctx
->t1
, NULL
);
966 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
967 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
977 static const cmdinfo_t aio_read_cmd
= {
982 .args
= "[-Cqv] [-P pattern ] off len [len..]",
983 .oneline
= "asynchronously reads a number of bytes",
984 .help
= aio_read_help
,
987 static const cmdinfo_t aio_write_cmd
;
994 " asynchronously writes a range of bytes from the given offset source \n"
995 " from multiple buffers\n"
998 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1000 " Writes into a segment of the currently open file, using a buffer\n"
1001 " filled with a set pattern (0xcdcdcdcd).\n"
1002 " The write is performed asynchronously and should the aio_flush command \n"
1003 " should be used to ensure all outstanding aio requests have been completed\n"
1004 " -P, -- use different pattern to fill file\n"
1005 " -C, -- report statistics in a machine parsable format\n"
1006 " -q, -- quite mode, do not show I/O statistics\n"
1012 aio_write_f(int argc
, char **argv
)
1016 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
1017 BlockDriverAIOCB
*acb
;
1019 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1028 pattern
= parse_pattern(optarg
);
1034 return command_usage(&aio_write_cmd
);
1038 if (optind
> argc
- 2) {
1040 return command_usage(&aio_write_cmd
);
1043 ctx
->offset
= cvtnum(argv
[optind
]);
1044 if (ctx
->offset
< 0) {
1045 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1051 if (ctx
->offset
& 0x1ff) {
1052 printf("offset %lld is not sector aligned\n",
1053 (long long)ctx
->offset
);
1058 nr_iov
= argc
- optind
;
1059 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1061 gettimeofday(&ctx
->t1
, NULL
);
1062 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1063 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1073 static const cmdinfo_t aio_write_cmd
= {
1074 .name
= "aio_write",
1075 .cfunc
= aio_write_f
,
1078 .args
= "[-Cq] [-P pattern ] off len [len..]",
1079 .oneline
= "asynchronously writes a number of bytes",
1080 .help
= aio_write_help
,
1084 aio_flush_f(int argc
, char **argv
)
1090 static const cmdinfo_t aio_flush_cmd
= {
1091 .name
= "aio_flush",
1092 .cfunc
= aio_flush_f
,
1093 .oneline
= "completes all outstanding aio requets"
1097 flush_f(int argc
, char **argv
)
1103 static const cmdinfo_t flush_cmd
= {
1107 .oneline
= "flush all in-core file state to disk",
1111 truncate_f(int argc
, char **argv
)
1116 offset
= cvtnum(argv
[1]);
1118 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1122 ret
= bdrv_truncate(bs
, offset
);
1124 printf("truncate: %s", strerror(ret
));
1131 static const cmdinfo_t truncate_cmd
= {
1134 .cfunc
= truncate_f
,
1138 .oneline
= "truncates the current file at the given offset",
1142 length_f(int argc
, char **argv
)
1147 size
= bdrv_getlength(bs
);
1149 printf("getlength: %s", strerror(size
));
1153 cvtstr(size
, s1
, sizeof(s1
));
1159 static const cmdinfo_t length_cmd
= {
1163 .oneline
= "gets the length of the current file",
1168 info_f(int argc
, char **argv
)
1170 BlockDriverInfo bdi
;
1171 char s1
[64], s2
[64];
1174 if (bs
->drv
&& bs
->drv
->format_name
)
1175 printf("format name: %s\n", bs
->drv
->format_name
);
1176 if (bs
->drv
&& bs
->drv
->protocol_name
)
1177 printf("format name: %s\n", bs
->drv
->protocol_name
);
1179 ret
= bdrv_get_info(bs
, &bdi
);
1183 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1184 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1186 printf("cluster size: %s\n", s1
);
1187 printf("vm state offset: %s\n", s2
);
1194 static const cmdinfo_t info_cmd
= {
1198 .oneline
= "prints information about the current file",
1202 alloc_f(int argc
, char **argv
)
1205 int nb_sectors
, remaining
;
1210 offset
= cvtnum(argv
[1]);
1211 if (offset
& 0x1ff) {
1212 printf("offset %lld is not sector aligned\n",
1218 nb_sectors
= cvtnum(argv
[2]);
1222 remaining
= nb_sectors
;
1225 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1232 cvtstr(offset
, s1
, sizeof(s1
));
1234 if (nb_sectors
== 1)
1235 printf("sector allocated at offset %s\n", s1
);
1237 printf("%d/%d sectors allocated at offset %s\n",
1238 sum_alloc
, nb_sectors
, s1
);
1242 static const cmdinfo_t alloc_cmd
= {
1248 .args
= "off [sectors]",
1249 .oneline
= "checks if a sector is present in the file",
1253 close_f(int argc
, char **argv
)
1260 static const cmdinfo_t close_cmd
= {
1264 .oneline
= "close the current open file",
1267 static int openfile(char *name
, int flags
, int growable
)
1270 fprintf(stderr
, "file open already, try 'help close'\n");
1274 bs
= bdrv_new("hda");
1279 flags
|= BDRV_O_FILE
;
1282 if (bdrv_open(bs
, name
, flags
) == -1) {
1283 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1299 " opens a new file in the requested mode\n"
1302 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1304 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1305 " -C, -- create new file if it doesn't exist\n"
1306 " -r, -- open file read-only\n"
1307 " -s, -- use snapshot file\n"
1308 " -n, -- disable host cache\n"
1309 " -g, -- allow file to grow (only applies to protocols)"
1313 static const cmdinfo_t open_cmd
;
1316 open_f(int argc
, char **argv
)
1323 while ((c
= getopt(argc
, argv
, "snCrg")) != EOF
) {
1326 flags
|= BDRV_O_SNAPSHOT
;
1329 flags
|= BDRV_O_NOCACHE
;
1332 flags
|= BDRV_O_CREAT
;
1341 return command_usage(&open_cmd
);
1346 flags
|= BDRV_O_RDONLY
;
1348 flags
|= BDRV_O_RDWR
;
1350 if (optind
!= argc
- 1)
1351 return command_usage(&open_cmd
);
1353 return openfile(argv
[optind
], flags
, growable
);
1356 static const cmdinfo_t open_cmd
= {
1362 .flags
= CMD_NOFILE_OK
,
1363 .args
= "[-Crsn] [path]",
1364 .oneline
= "open the file specified by path",
1372 /* only one device allowed so far */
1380 const cmdinfo_t
*ct
)
1382 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1384 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1385 fprintf(stderr
, "no file open, try 'help open'\n");
1391 static void usage(const char *name
)
1394 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1395 "QEMU Disk exerciser\n"
1397 " -C, --create create new file if it doesn't exist\n"
1398 " -c, --cmd command to execute\n"
1399 " -r, --read-only export read-only\n"
1400 " -s, --snapshot use snapshot file\n"
1401 " -n, --nocache disable host cache\n"
1402 " -g, --growable allow file to grow (only applies to protocols)\n"
1403 " -m, --misalign misalign allocations for O_DIRECT\n"
1404 " -h, --help display this help and exit\n"
1405 " -V, --version output version information and exit\n"
1411 int main(int argc
, char **argv
)
1415 const char *sopt
= "hVc:Crsnmg";
1416 struct option lopt
[] = {
1417 { "help", 0, 0, 'h' },
1418 { "version", 0, 0, 'V' },
1419 { "offset", 1, 0, 'o' },
1420 { "cmd", 1, 0, 'c' },
1421 { "create", 0, 0, 'C' },
1422 { "read-only", 0, 0, 'r' },
1423 { "snapshot", 0, 0, 's' },
1424 { "nocache", 0, 0, 'n' },
1425 { "misalign", 0, 0, 'm' },
1426 { "growable", 0, 0, 'g' },
1433 progname
= basename(argv
[0]);
1435 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1438 flags
|= BDRV_O_SNAPSHOT
;
1441 flags
|= BDRV_O_NOCACHE
;
1444 add_user_command(optarg
);
1447 flags
|= BDRV_O_CREAT
;
1459 printf("%s version %s\n", progname
, VERSION
);
1470 if ((argc
- optind
) > 1) {
1477 /* initialize commands */
1480 add_command(&open_cmd
);
1481 add_command(&close_cmd
);
1482 add_command(&read_cmd
);
1483 add_command(&readv_cmd
);
1484 add_command(&write_cmd
);
1485 add_command(&writev_cmd
);
1486 add_command(&aio_read_cmd
);
1487 add_command(&aio_write_cmd
);
1488 add_command(&aio_flush_cmd
);
1489 add_command(&flush_cmd
);
1490 add_command(&truncate_cmd
);
1491 add_command(&length_cmd
);
1492 add_command(&info_cmd
);
1493 add_command(&alloc_cmd
);
1495 add_args_command(init_args_command
);
1496 add_check_command(init_check_command
);
1498 /* open the device */
1500 flags
|= BDRV_O_RDONLY
;
1502 flags
|= BDRV_O_RDWR
;
1504 if ((argc
- optind
) == 1)
1505 openfile(argv
[optind
], flags
, growable
);
1509 * Make sure all outstanding requests get flushed the program exits.