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.
11 #include <sys/types.h>
17 #include "qemu-common.h"
18 #include "block_int.h"
21 #define VERSION "0.0.1"
23 #define CMD_NOFILE_OK 0x01
26 static BlockDriverState
*bs
;
31 * Parse the pattern argument to various sub-commands.
33 * Because the pattern is used as an argument to memset it must evaluate
34 * to an unsigned integer that fits into a single byte.
36 static int parse_pattern(const char *arg
)
41 pattern
= strtol(arg
, &endptr
, 0);
42 if (pattern
< 0 || pattern
> UCHAR_MAX
|| *endptr
!= '\0') {
43 printf("%s is not a valid pattern byte\n", arg
);
51 * Memory allocation helpers.
53 * Make sure memory is aligned by default, or purposefully misaligned if
54 * that is specified on the command line.
57 #define MISALIGN_OFFSET 16
58 static void *qemu_io_alloc(size_t len
, int pattern
)
63 len
+= MISALIGN_OFFSET
;
64 buf
= qemu_memalign(512, len
);
65 memset(buf
, pattern
, len
);
67 buf
+= MISALIGN_OFFSET
;
71 static void qemu_io_free(void *p
)
79 dump_buffer(const void *buffer
, int64_t offset
, int len
)
84 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
87 printf("%08llx: ", (unsigned long long)offset
+ i
);
88 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
91 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
102 print_report(const char *op
, struct timeval
*t
, int64_t offset
,
103 int count
, int total
, int cnt
, int Cflag
)
105 char s1
[64], s2
[64], ts
[64];
107 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
109 cvtstr((double)total
, s1
, sizeof(s1
));
110 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
111 printf("%s %d/%d bytes at offset %lld\n",
112 op
, total
, count
, (long long)offset
);
113 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
114 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
115 } else {/* bytes,ops,time,bytes/sec,ops/sec */
116 printf("%d,%d,%s,%.3f,%.3f\n",
118 tdiv((double)total
, *t
),
119 tdiv((double)cnt
, *t
));
124 * Parse multiple length statements for vectored I/O, and construct an I/O
125 * vector matching it.
128 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
130 size_t *sizes
= calloc(nr_iov
, sizeof(size_t));
135 for (i
= 0; i
< nr_iov
; i
++) {
141 printf("non-numeric length argument -- %s\n", arg
);
145 /* should be SIZE_T_MAX, but that doesn't exist */
146 if (len
> UINT_MAX
) {
147 printf("too large length argument -- %s\n", arg
);
152 printf("length argument %lld is not sector aligned\n",
161 qemu_iovec_init(qiov
, nr_iov
);
163 buf
= p
= qemu_io_alloc(count
, pattern
);
165 for (i
= 0; i
< nr_iov
; i
++) {
166 qemu_iovec_add(qiov
, p
, sizes
[i
]);
174 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
178 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
185 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
189 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
196 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
198 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
204 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
206 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
212 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
214 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
220 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
222 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
228 #define NOT_DONE 0x7fffffff
229 static void aio_rw_done(void *opaque
, int ret
)
231 *(int *)opaque
= ret
;
234 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
236 BlockDriverAIOCB
*acb
;
237 int async_ret
= NOT_DONE
;
239 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
240 aio_rw_done
, &async_ret
);
244 while (async_ret
== NOT_DONE
)
248 return async_ret
< 0 ? async_ret
: 1;
251 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
253 BlockDriverAIOCB
*acb
;
254 int async_ret
= NOT_DONE
;
256 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
257 aio_rw_done
, &async_ret
);
261 while (async_ret
== NOT_DONE
)
265 return async_ret
< 0 ? async_ret
: 1;
269 static const cmdinfo_t read_cmd
;
276 " reads a range of bytes from the given offset\n"
279 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
281 " Reads a segment of the currently open file, optionally dumping it to the\n"
282 " standard output stream (with -v option) for subsequent inspection.\n"
283 " -b, -- read from the VM state rather than the virtual disk\n"
284 " -C, -- report statistics in a machine parsable format\n"
285 " -l, -- length for pattern verification (only with -P)\n"
286 " -p, -- use bdrv_pread to read the file\n"
287 " -P, -- use a pattern to verify read data\n"
288 " -q, -- quite mode, do not show I/O statistics\n"
289 " -s, -- start offset for pattern verification (only with -P)\n"
290 " -v, -- dump buffer to standard output\n"
295 read_f(int argc
, char **argv
)
297 struct timeval t1
, t2
;
298 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
299 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
304 /* Some compilers get confused and warn if this is not initialized. */
306 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
308 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
318 pattern_count
= cvtnum(optarg
);
319 if (pattern_count
< 0) {
320 printf("non-numeric length argument -- %s\n", optarg
);
329 pattern
= parse_pattern(optarg
);
338 pattern_offset
= cvtnum(optarg
);
339 if (pattern_offset
< 0) {
340 printf("non-numeric length argument -- %s\n", optarg
);
348 return command_usage(&read_cmd
);
352 if (optind
!= argc
- 2)
353 return command_usage(&read_cmd
);
355 if (bflag
&& pflag
) {
356 printf("-b and -p cannot be specified at the same time\n");
360 offset
= cvtnum(argv
[optind
]);
362 printf("non-numeric length argument -- %s\n", argv
[optind
]);
367 count
= cvtnum(argv
[optind
]);
369 printf("non-numeric length argument -- %s\n", argv
[optind
]);
373 if (!Pflag
&& (lflag
|| sflag
)) {
374 return command_usage(&read_cmd
);
378 pattern_count
= count
- pattern_offset
;
381 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
382 printf("pattern verfication range exceeds end of read data\n");
387 if (offset
& 0x1ff) {
388 printf("offset %lld is not sector aligned\n",
393 printf("count %d is not sector aligned\n",
399 buf
= qemu_io_alloc(count
, 0xab);
401 gettimeofday(&t1
, NULL
);
403 cnt
= do_pread(buf
, offset
, count
, &total
);
405 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
407 cnt
= do_read(buf
, offset
, count
, &total
);
408 gettimeofday(&t2
, NULL
);
411 printf("read failed: %s\n", strerror(-cnt
));
416 void* cmp_buf
= malloc(pattern_count
);
417 memset(cmp_buf
, pattern
, pattern_count
);
418 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
419 printf("Pattern verification failed at offset %lld, "
421 (long long) offset
+ pattern_offset
, pattern_count
);
430 dump_buffer(buf
, offset
, count
);
432 /* Finally, report back -- -C gives a parsable format */
434 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
442 static const cmdinfo_t read_cmd
= {
448 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
449 .oneline
= "reads a number of bytes at a specified offset",
453 static const cmdinfo_t readv_cmd
;
460 " reads a range of bytes from the given offset into multiple buffers\n"
463 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
465 " Reads a segment of the currently open file, optionally dumping it to the\n"
466 " standard output stream (with -v option) for subsequent inspection.\n"
467 " Uses multiple iovec buffers if more than one byte range is specified.\n"
468 " -C, -- report statistics in a machine parsable format\n"
469 " -P, -- use a pattern to verify read data\n"
470 " -v, -- dump buffer to standard output\n"
471 " -q, -- quite mode, do not show I/O statistics\n"
476 readv_f(int argc
, char **argv
)
478 struct timeval t1
, t2
;
479 int Cflag
= 0, qflag
= 0, vflag
= 0;
489 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
496 pattern
= parse_pattern(optarg
);
507 return command_usage(&readv_cmd
);
511 if (optind
> argc
- 2)
512 return command_usage(&readv_cmd
);
515 offset
= cvtnum(argv
[optind
]);
517 printf("non-numeric length argument -- %s\n", argv
[optind
]);
522 if (offset
& 0x1ff) {
523 printf("offset %lld is not sector aligned\n",
528 nr_iov
= argc
- optind
;
529 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
531 gettimeofday(&t1
, NULL
);
532 cnt
= do_aio_readv(&qiov
, offset
, &total
);
533 gettimeofday(&t2
, NULL
);
536 printf("readv failed: %s\n", strerror(-cnt
));
541 void* cmp_buf
= malloc(qiov
.size
);
542 memset(cmp_buf
, pattern
, qiov
.size
);
543 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
544 printf("Pattern verification failed at offset %lld, "
546 (long long) offset
, qiov
.size
);
555 dump_buffer(buf
, offset
, qiov
.size
);
557 /* Finally, report back -- -C gives a parsable format */
559 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
566 static const cmdinfo_t readv_cmd
= {
571 .args
= "[-Cqv] [-P pattern ] off len [len..]",
572 .oneline
= "reads a number of bytes at a specified offset",
576 static const cmdinfo_t write_cmd
;
583 " writes a range of bytes from the given offset\n"
586 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
588 " Writes into a segment of the currently open file, using a buffer\n"
589 " filled with a set pattern (0xcdcdcdcd).\n"
590 " -b, -- write to the VM state rather than the virtual disk\n"
591 " -p, -- use bdrv_pwrite to write the file\n"
592 " -P, -- use different pattern to fill file\n"
593 " -C, -- report statistics in a machine parsable format\n"
594 " -q, -- quite mode, do not show I/O statistics\n"
599 write_f(int argc
, char **argv
)
601 struct timeval t1
, t2
;
602 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0;
607 /* Some compilers get confused and warn if this is not initialized. */
611 while ((c
= getopt(argc
, argv
, "bCpP:q")) != EOF
) {
623 pattern
= parse_pattern(optarg
);
631 return command_usage(&write_cmd
);
635 if (optind
!= argc
- 2)
636 return command_usage(&write_cmd
);
638 if (bflag
&& pflag
) {
639 printf("-b and -p cannot be specified at the same time\n");
643 offset
= cvtnum(argv
[optind
]);
645 printf("non-numeric length argument -- %s\n", argv
[optind
]);
650 count
= cvtnum(argv
[optind
]);
652 printf("non-numeric length argument -- %s\n", argv
[optind
]);
657 if (offset
& 0x1ff) {
658 printf("offset %lld is not sector aligned\n",
664 printf("count %d is not sector aligned\n",
670 buf
= qemu_io_alloc(count
, pattern
);
672 gettimeofday(&t1
, NULL
);
674 cnt
= do_pwrite(buf
, offset
, count
, &total
);
676 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
678 cnt
= do_write(buf
, offset
, count
, &total
);
679 gettimeofday(&t2
, NULL
);
682 printf("write failed: %s\n", strerror(-cnt
));
689 /* Finally, report back -- -C gives a parsable format */
691 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
699 static const cmdinfo_t write_cmd
= {
705 .args
= "[-abCpq] [-P pattern ] off len",
706 .oneline
= "writes a number of bytes at a specified offset",
710 static const cmdinfo_t writev_cmd
;
717 " writes a range of bytes from the given offset source from multiple buffers\n"
720 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
722 " Writes into a segment of the currently open file, using a buffer\n"
723 " filled with a set pattern (0xcdcdcdcd).\n"
724 " -P, -- use different pattern to fill file\n"
725 " -C, -- report statistics in a machine parsable format\n"
726 " -q, -- quite mode, do not show I/O statistics\n"
731 writev_f(int argc
, char **argv
)
733 struct timeval t1
, t2
;
734 int Cflag
= 0, qflag
= 0;
743 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
752 pattern
= parse_pattern(optarg
);
757 return command_usage(&writev_cmd
);
761 if (optind
> argc
- 2)
762 return command_usage(&writev_cmd
);
764 offset
= cvtnum(argv
[optind
]);
766 printf("non-numeric length argument -- %s\n", argv
[optind
]);
771 if (offset
& 0x1ff) {
772 printf("offset %lld is not sector aligned\n",
777 nr_iov
= argc
- optind
;
778 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
780 gettimeofday(&t1
, NULL
);
781 cnt
= do_aio_writev(&qiov
, offset
, &total
);
782 gettimeofday(&t2
, NULL
);
785 printf("writev failed: %s\n", strerror(-cnt
));
792 /* Finally, report back -- -C gives a parsable format */
794 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
800 static const cmdinfo_t writev_cmd
= {
805 .args
= "[-Cq] [-P pattern ] off len [len..]",
806 .oneline
= "writes a number of bytes at a specified offset",
823 aio_write_done(void *opaque
, int ret
)
825 struct aio_ctx
*ctx
= opaque
;
828 gettimeofday(&t2
, NULL
);
832 printf("aio_write failed: %s\n", strerror(-ret
));
840 /* Finally, report back -- -C gives a parsable format */
841 t2
= tsub(t2
, ctx
->t1
);
842 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
843 ctx
->qiov
.size
, 1, ctx
->Cflag
);
845 qemu_io_free(ctx
->buf
);
849 static const cmdinfo_t aio_read_cmd
;
852 aio_read_done(void *opaque
, int ret
)
854 struct aio_ctx
*ctx
= opaque
;
857 gettimeofday(&t2
, NULL
);
860 printf("readv failed: %s\n", strerror(-ret
));
865 void *cmp_buf
= malloc(ctx
->qiov
.size
);
867 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
868 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
869 printf("Pattern verification failed at offset %lld, "
871 (long long) ctx
->offset
, ctx
->qiov
.size
);
881 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
884 /* Finally, report back -- -C gives a parsable format */
885 t2
= tsub(t2
, ctx
->t1
);
886 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
887 ctx
->qiov
.size
, 1, ctx
->Cflag
);
889 qemu_io_free(ctx
->buf
);
898 " asynchronously reads a range of bytes from the given offset\n"
901 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
903 " Reads a segment of the currently open file, optionally dumping it to the\n"
904 " standard output stream (with -v option) for subsequent inspection.\n"
905 " The read is performed asynchronously and should the aio_flush command \n"
906 " should be used to ensure all outstanding aio requests have been completed\n"
907 " -C, -- report statistics in a machine parsable format\n"
908 " -P, -- use a pattern to verify read data\n"
909 " -v, -- dump buffer to standard output\n"
910 " -q, -- quite mode, do not show I/O statistics\n"
915 aio_read_f(int argc
, char **argv
)
918 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
919 BlockDriverAIOCB
*acb
;
921 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
928 ctx
->pattern
= parse_pattern(optarg
);
929 if (ctx
->pattern
< 0)
940 return command_usage(&aio_read_cmd
);
944 if (optind
> argc
- 2) {
946 return command_usage(&aio_read_cmd
);
949 ctx
->offset
= cvtnum(argv
[optind
]);
950 if (ctx
->offset
< 0) {
951 printf("non-numeric length argument -- %s\n", argv
[optind
]);
957 if (ctx
->offset
& 0x1ff) {
958 printf("offset %lld is not sector aligned\n",
959 (long long)ctx
->offset
);
964 nr_iov
= argc
- optind
;
965 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
967 gettimeofday(&ctx
->t1
, NULL
);
968 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
969 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
979 static const cmdinfo_t aio_read_cmd
= {
984 .args
= "[-Cqv] [-P pattern ] off len [len..]",
985 .oneline
= "asynchronously reads a number of bytes",
986 .help
= aio_read_help
,
989 static const cmdinfo_t aio_write_cmd
;
996 " asynchronously writes a range of bytes from the given offset source \n"
997 " from multiple buffers\n"
1000 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1002 " Writes into a segment of the currently open file, using a buffer\n"
1003 " filled with a set pattern (0xcdcdcdcd).\n"
1004 " The write is performed asynchronously and should the aio_flush command \n"
1005 " should be used to ensure all outstanding aio requests have been completed\n"
1006 " -P, -- use different pattern to fill file\n"
1007 " -C, -- report statistics in a machine parsable format\n"
1008 " -q, -- quite mode, do not show I/O statistics\n"
1014 aio_write_f(int argc
, char **argv
)
1018 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
1019 BlockDriverAIOCB
*acb
;
1021 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1030 pattern
= parse_pattern(optarg
);
1036 return command_usage(&aio_write_cmd
);
1040 if (optind
> argc
- 2) {
1042 return command_usage(&aio_write_cmd
);
1045 ctx
->offset
= cvtnum(argv
[optind
]);
1046 if (ctx
->offset
< 0) {
1047 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1053 if (ctx
->offset
& 0x1ff) {
1054 printf("offset %lld is not sector aligned\n",
1055 (long long)ctx
->offset
);
1060 nr_iov
= argc
- optind
;
1061 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1063 gettimeofday(&ctx
->t1
, NULL
);
1064 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1065 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1075 static const cmdinfo_t aio_write_cmd
= {
1076 .name
= "aio_write",
1077 .cfunc
= aio_write_f
,
1080 .args
= "[-Cq] [-P pattern ] off len [len..]",
1081 .oneline
= "asynchronously writes a number of bytes",
1082 .help
= aio_write_help
,
1086 aio_flush_f(int argc
, char **argv
)
1092 static const cmdinfo_t aio_flush_cmd
= {
1093 .name
= "aio_flush",
1094 .cfunc
= aio_flush_f
,
1095 .oneline
= "completes all outstanding aio requets"
1099 flush_f(int argc
, char **argv
)
1105 static const cmdinfo_t flush_cmd
= {
1109 .oneline
= "flush all in-core file state to disk",
1113 truncate_f(int argc
, char **argv
)
1118 offset
= cvtnum(argv
[1]);
1120 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1124 ret
= bdrv_truncate(bs
, offset
);
1126 printf("truncate: %s", strerror(ret
));
1133 static const cmdinfo_t truncate_cmd
= {
1136 .cfunc
= truncate_f
,
1140 .oneline
= "truncates the current file at the given offset",
1144 length_f(int argc
, char **argv
)
1149 size
= bdrv_getlength(bs
);
1151 printf("getlength: %s", strerror(size
));
1155 cvtstr(size
, s1
, sizeof(s1
));
1161 static const cmdinfo_t length_cmd
= {
1165 .oneline
= "gets the length of the current file",
1170 info_f(int argc
, char **argv
)
1172 BlockDriverInfo bdi
;
1173 char s1
[64], s2
[64];
1176 if (bs
->drv
&& bs
->drv
->format_name
)
1177 printf("format name: %s\n", bs
->drv
->format_name
);
1178 if (bs
->drv
&& bs
->drv
->protocol_name
)
1179 printf("format name: %s\n", bs
->drv
->protocol_name
);
1181 ret
= bdrv_get_info(bs
, &bdi
);
1185 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1186 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1188 printf("cluster size: %s\n", s1
);
1189 printf("vm state offset: %s\n", s2
);
1196 static const cmdinfo_t info_cmd
= {
1200 .oneline
= "prints information about the current file",
1204 alloc_f(int argc
, char **argv
)
1207 int nb_sectors
, remaining
;
1212 offset
= cvtnum(argv
[1]);
1213 if (offset
& 0x1ff) {
1214 printf("offset %lld is not sector aligned\n",
1220 nb_sectors
= cvtnum(argv
[2]);
1224 remaining
= nb_sectors
;
1227 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1234 cvtstr(offset
, s1
, sizeof(s1
));
1236 if (nb_sectors
== 1)
1237 printf("sector allocated at offset %s\n", s1
);
1239 printf("%d/%d sectors allocated at offset %s\n",
1240 sum_alloc
, nb_sectors
, s1
);
1244 static const cmdinfo_t alloc_cmd
= {
1250 .args
= "off [sectors]",
1251 .oneline
= "checks if a sector is present in the file",
1255 close_f(int argc
, char **argv
)
1262 static const cmdinfo_t close_cmd
= {
1266 .oneline
= "close the current open file",
1269 static int openfile(char *name
, int flags
, int growable
)
1272 fprintf(stderr
, "file open already, try 'help close'\n");
1276 bs
= bdrv_new("hda");
1281 flags
|= BDRV_O_FILE
;
1284 if (bdrv_open(bs
, name
, flags
) == -1) {
1285 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1301 " opens a new file in the requested mode\n"
1304 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1306 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1307 " -C, -- create new file if it doesn't exist\n"
1308 " -r, -- open file read-only\n"
1309 " -s, -- use snapshot file\n"
1310 " -n, -- disable host cache\n"
1311 " -g, -- allow file to grow (only applies to protocols)"
1315 static const cmdinfo_t open_cmd
;
1318 open_f(int argc
, char **argv
)
1325 while ((c
= getopt(argc
, argv
, "snCrg")) != EOF
) {
1328 flags
|= BDRV_O_SNAPSHOT
;
1331 flags
|= BDRV_O_NOCACHE
;
1334 flags
|= BDRV_O_CREAT
;
1343 return command_usage(&open_cmd
);
1348 flags
|= BDRV_O_RDONLY
;
1350 flags
|= BDRV_O_RDWR
;
1352 if (optind
!= argc
- 1)
1353 return command_usage(&open_cmd
);
1355 return openfile(argv
[optind
], flags
, growable
);
1358 static const cmdinfo_t open_cmd
= {
1364 .flags
= CMD_NOFILE_OK
,
1365 .args
= "[-Crsn] [path]",
1366 .oneline
= "open the file specified by path",
1374 /* only one device allowed so far */
1382 const cmdinfo_t
*ct
)
1384 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1386 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1387 fprintf(stderr
, "no file open, try 'help open'\n");
1393 static void usage(const char *name
)
1396 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1397 "QEMU Disk exerciser\n"
1399 " -C, --create create new file if it doesn't exist\n"
1400 " -c, --cmd command to execute\n"
1401 " -r, --read-only export read-only\n"
1402 " -s, --snapshot use snapshot file\n"
1403 " -n, --nocache disable host cache\n"
1404 " -g, --growable allow file to grow (only applies to protocols)\n"
1405 " -m, --misalign misalign allocations for O_DIRECT\n"
1406 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
1407 " -h, --help display this help and exit\n"
1408 " -V, --version output version information and exit\n"
1414 int main(int argc
, char **argv
)
1418 const char *sopt
= "hVc:Crsnmgk";
1419 struct option lopt
[] = {
1420 { "help", 0, NULL
, 'h' },
1421 { "version", 0, NULL
, 'V' },
1422 { "offset", 1, NULL
, 'o' },
1423 { "cmd", 1, NULL
, 'c' },
1424 { "create", 0, NULL
, 'C' },
1425 { "read-only", 0, NULL
, 'r' },
1426 { "snapshot", 0, NULL
, 's' },
1427 { "nocache", 0, NULL
, 'n' },
1428 { "misalign", 0, NULL
, 'm' },
1429 { "growable", 0, NULL
, 'g' },
1430 { "native-aio", 0, NULL
, 'k' },
1431 { NULL
, 0, NULL
, 0 }
1437 progname
= basename(argv
[0]);
1439 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1442 flags
|= BDRV_O_SNAPSHOT
;
1445 flags
|= BDRV_O_NOCACHE
;
1448 add_user_command(optarg
);
1451 flags
|= BDRV_O_CREAT
;
1463 flags
|= BDRV_O_NATIVE_AIO
;
1466 printf("%s version %s\n", progname
, VERSION
);
1477 if ((argc
- optind
) > 1) {
1484 /* initialize commands */
1487 add_command(&open_cmd
);
1488 add_command(&close_cmd
);
1489 add_command(&read_cmd
);
1490 add_command(&readv_cmd
);
1491 add_command(&write_cmd
);
1492 add_command(&writev_cmd
);
1493 add_command(&aio_read_cmd
);
1494 add_command(&aio_write_cmd
);
1495 add_command(&aio_flush_cmd
);
1496 add_command(&flush_cmd
);
1497 add_command(&truncate_cmd
);
1498 add_command(&length_cmd
);
1499 add_command(&info_cmd
);
1500 add_command(&alloc_cmd
);
1502 add_args_command(init_args_command
);
1503 add_check_command(init_check_command
);
1505 /* open the device */
1507 flags
|= BDRV_O_RDONLY
;
1509 flags
|= BDRV_O_RDWR
;
1511 if ((argc
- optind
) == 1)
1512 openfile(argv
[optind
], flags
, growable
);
1516 * Make sure all outstanding requests get flushed the program exits.