properly save kvm system time msr registers
[qemu-kvm/fedora.git] / qemu-io.c
blobcd9c9599caf0ecfa348f87af2facea5bfdf753d0
1 /*
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.
9 */
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <libgen.h>
17 #include "qemu-common.h"
18 #include "block_int.h"
19 #include "cmd.h"
21 #define VERSION "0.0.1"
23 #define CMD_NOFILE_OK 0x01
25 char *progname;
26 static BlockDriverState *bs;
28 static int misalign;
31 * Memory allocation helpers.
33 * Make sure memory is aligned by default, or purposefully misaligned if
34 * that is specified on the command line.
37 #define MISALIGN_OFFSET 16
38 static void *qemu_io_alloc(size_t len, int pattern)
40 void *buf;
42 if (misalign)
43 len += MISALIGN_OFFSET;
44 buf = qemu_memalign(512, len);
45 memset(buf, pattern, len);
46 if (misalign)
47 buf += MISALIGN_OFFSET;
48 return buf;
51 static void qemu_io_free(void *p)
53 if (misalign)
54 p -= MISALIGN_OFFSET;
55 qemu_vfree(p);
58 static void
59 dump_buffer(const void *buffer, int64_t offset, int len)
61 int i, j;
62 const uint8_t *p;
64 for (i = 0, p = buffer; i < len; i += 16) {
65 const uint8_t *s = p;
67 printf("%08llx: ", (unsigned long long)offset + i);
68 for (j = 0; j < 16 && i + j < len; j++, p++)
69 printf("%02x ", *p);
70 printf(" ");
71 for (j = 0; j < 16 && i + j < len; j++, s++) {
72 if (isalnum(*s))
73 printf("%c", *s);
74 else
75 printf(".");
77 printf("\n");
81 static void
82 print_report(const char *op, struct timeval *t, int64_t offset,
83 int count, int total, int cnt, int Cflag)
85 char s1[64], s2[64], ts[64];
87 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
88 if (!Cflag) {
89 cvtstr((double)total, s1, sizeof(s1));
90 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
91 printf("%s %d/%d bytes at offset %lld\n",
92 op, total, count, (long long)offset);
93 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
94 s1, cnt, ts, s2, tdiv((double)cnt, *t));
95 } else {/* bytes,ops,time,bytes/sec,ops/sec */
96 printf("%d,%d,%s,%.3f,%.3f\n",
97 total, cnt, ts,
98 tdiv((double)total, *t),
99 tdiv((double)cnt, *t));
104 * Parse multiple length statements for vectored I/O, and construct an I/O
105 * vector matching it.
107 static void *
108 create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
110 size_t *sizes = calloc(nr_iov, sizeof(size_t));
111 size_t count = 0;
112 void *buf, *p;
113 int i;
115 for (i = 0; i < nr_iov; i++) {
116 char *arg = argv[i];
117 long long len;
119 len = cvtnum(arg);
120 if (len < 0) {
121 printf("non-numeric length argument -- %s\n", arg);
122 return NULL;
125 /* should be SIZE_T_MAX, but that doesn't exist */
126 if (len > UINT_MAX) {
127 printf("too large length argument -- %s\n", arg);
128 return NULL;
131 if (len & 0x1ff) {
132 printf("length argument %lld is not sector aligned\n",
133 len);
134 return NULL;
137 sizes[i] = len;
138 count += len;
141 qemu_iovec_init(qiov, nr_iov);
143 buf = p = qemu_io_alloc(count, pattern);
145 for (i = 0; i < nr_iov; i++) {
146 qemu_iovec_add(qiov, p, sizes[i]);
147 p += sizes[i];
150 free(sizes);
151 return buf;
154 static int do_read(char *buf, int64_t offset, int count, int *total)
156 int ret;
158 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
159 if (ret < 0)
160 return ret;
161 *total = count;
162 return 1;
165 static int do_write(char *buf, int64_t offset, int count, int *total)
167 int ret;
169 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
170 if (ret < 0)
171 return ret;
172 *total = count;
173 return 1;
176 static int do_pread(char *buf, int64_t offset, int count, int *total)
178 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
179 if (*total < 0)
180 return *total;
181 return 1;
184 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
186 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
187 if (*total < 0)
188 return *total;
189 return 1;
192 static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
194 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
195 if (*total < 0)
196 return *total;
197 return 1;
200 static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
202 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
203 if (*total < 0)
204 return *total;
205 return 1;
208 #define NOT_DONE 0x7fffffff
209 static void aio_rw_done(void *opaque, int ret)
211 *(int *)opaque = ret;
214 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
216 BlockDriverAIOCB *acb;
217 int async_ret = NOT_DONE;
219 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
220 aio_rw_done, &async_ret);
221 if (!acb)
222 return -EIO;
224 while (async_ret == NOT_DONE)
225 qemu_aio_wait();
227 *total = qiov->size;
228 return async_ret < 0 ? async_ret : 1;
231 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
233 BlockDriverAIOCB *acb;
234 int async_ret = NOT_DONE;
236 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
237 aio_rw_done, &async_ret);
238 if (!acb)
239 return -EIO;
241 while (async_ret == NOT_DONE)
242 qemu_aio_wait();
244 *total = qiov->size;
245 return async_ret < 0 ? async_ret : 1;
249 static const cmdinfo_t read_cmd;
251 static void
252 read_help(void)
254 printf(
255 "\n"
256 " reads a range of bytes from the given offset\n"
257 "\n"
258 " Example:\n"
259 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
260 "\n"
261 " Reads a segment of the currently open file, optionally dumping it to the\n"
262 " standard output stream (with -v option) for subsequent inspection.\n"
263 " -b, -- read from the VM state rather than the virtual disk\n"
264 " -C, -- report statistics in a machine parsable format\n"
265 " -l, -- length for pattern verification (only with -P)\n"
266 " -p, -- use bdrv_pread to read the file\n"
267 " -P, -- use a pattern to verify read data\n"
268 " -q, -- quite mode, do not show I/O statistics\n"
269 " -s, -- start offset for pattern verification (only with -P)\n"
270 " -v, -- dump buffer to standard output\n"
271 "\n");
274 static int
275 read_f(int argc, char **argv)
277 struct timeval t1, t2;
278 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
279 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
280 int c, cnt;
281 char *buf;
282 int64_t offset;
283 int count;
284 /* Some compilers get confused and warn if this is not initialized. */
285 int total = 0;
286 int pattern = 0, pattern_offset = 0, pattern_count = 0;
288 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
289 switch (c) {
290 case 'b':
291 bflag = 1;
292 break;
293 case 'C':
294 Cflag = 1;
295 break;
296 case 'l':
297 lflag = 1;
298 pattern_count = cvtnum(optarg);
299 if (pattern_count < 0) {
300 printf("non-numeric length argument -- %s\n", optarg);
301 return 0;
303 break;
304 case 'p':
305 pflag = 1;
306 break;
307 case 'P':
308 Pflag = 1;
309 pattern = atoi(optarg);
310 break;
311 case 'q':
312 qflag = 1;
313 break;
314 case 's':
315 sflag = 1;
316 pattern_offset = cvtnum(optarg);
317 if (pattern_offset < 0) {
318 printf("non-numeric length argument -- %s\n", optarg);
319 return 0;
321 break;
322 case 'v':
323 vflag = 1;
324 break;
325 default:
326 return command_usage(&read_cmd);
330 if (optind != argc - 2)
331 return command_usage(&read_cmd);
333 if (bflag && pflag) {
334 printf("-b and -p cannot be specified at the same time\n");
335 return 0;
338 offset = cvtnum(argv[optind]);
339 if (offset < 0) {
340 printf("non-numeric length argument -- %s\n", argv[optind]);
341 return 0;
344 optind++;
345 count = cvtnum(argv[optind]);
346 if (count < 0) {
347 printf("non-numeric length argument -- %s\n", argv[optind]);
348 return 0;
351 if (!Pflag && (lflag || sflag)) {
352 return command_usage(&read_cmd);
355 if (!lflag) {
356 pattern_count = count - pattern_offset;
359 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
360 printf("pattern verfication range exceeds end of read data\n");
361 return 0;
364 if (!pflag)
365 if (offset & 0x1ff) {
366 printf("offset %lld is not sector aligned\n",
367 (long long)offset);
368 return 0;
370 if (count & 0x1ff) {
371 printf("count %d is not sector aligned\n",
372 count);
373 return 0;
377 buf = qemu_io_alloc(count, 0xab);
379 gettimeofday(&t1, NULL);
380 if (pflag)
381 cnt = do_pread(buf, offset, count, &total);
382 else if (bflag)
383 cnt = do_load_vmstate(buf, offset, count, &total);
384 else
385 cnt = do_read(buf, offset, count, &total);
386 gettimeofday(&t2, NULL);
388 if (cnt < 0) {
389 printf("read failed: %s\n", strerror(-cnt));
390 goto out;
393 if (Pflag) {
394 void* cmp_buf = malloc(pattern_count);
395 memset(cmp_buf, pattern, pattern_count);
396 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
397 printf("Pattern verification failed at offset %lld, "
398 "%d bytes\n",
399 (long long) offset + pattern_offset, pattern_count);
401 free(cmp_buf);
404 if (qflag)
405 goto out;
407 if (vflag)
408 dump_buffer(buf, offset, count);
410 /* Finally, report back -- -C gives a parsable format */
411 t2 = tsub(t2, t1);
412 print_report("read", &t2, offset, count, total, cnt, Cflag);
414 out:
415 qemu_io_free(buf);
417 return 0;
420 static const cmdinfo_t read_cmd = {
421 .name = "read",
422 .altname = "r",
423 .cfunc = read_f,
424 .argmin = 2,
425 .argmax = -1,
426 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
427 .oneline = "reads a number of bytes at a specified offset",
428 .help = read_help,
431 static const cmdinfo_t readv_cmd;
433 static void
434 readv_help(void)
436 printf(
437 "\n"
438 " reads a range of bytes from the given offset into multiple buffers\n"
439 "\n"
440 " Example:\n"
441 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
442 "\n"
443 " Reads a segment of the currently open file, optionally dumping it to the\n"
444 " standard output stream (with -v option) for subsequent inspection.\n"
445 " Uses multiple iovec buffers if more than one byte range is specified.\n"
446 " -C, -- report statistics in a machine parsable format\n"
447 " -P, -- use a pattern to verify read data\n"
448 " -v, -- dump buffer to standard output\n"
449 " -q, -- quite mode, do not show I/O statistics\n"
450 "\n");
453 static int
454 readv_f(int argc, char **argv)
456 struct timeval t1, t2;
457 int Cflag = 0, qflag = 0, vflag = 0;
458 int c, cnt;
459 char *buf;
460 int64_t offset;
461 int total;
462 int nr_iov;
463 QEMUIOVector qiov;
464 int pattern = 0;
465 int Pflag = 0;
467 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
468 switch (c) {
469 case 'C':
470 Cflag = 1;
471 break;
472 case 'P':
473 Pflag = 1;
474 pattern = atoi(optarg);
475 break;
476 case 'q':
477 qflag = 1;
478 break;
479 case 'v':
480 vflag = 1;
481 break;
482 default:
483 return command_usage(&readv_cmd);
487 if (optind > argc - 2)
488 return command_usage(&readv_cmd);
491 offset = cvtnum(argv[optind]);
492 if (offset < 0) {
493 printf("non-numeric length argument -- %s\n", argv[optind]);
494 return 0;
496 optind++;
498 if (offset & 0x1ff) {
499 printf("offset %lld is not sector aligned\n",
500 (long long)offset);
501 return 0;
504 nr_iov = argc - optind;
505 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
507 gettimeofday(&t1, NULL);
508 cnt = do_aio_readv(&qiov, offset, &total);
509 gettimeofday(&t2, NULL);
511 if (cnt < 0) {
512 printf("readv failed: %s\n", strerror(-cnt));
513 goto out;
516 if (Pflag) {
517 void* cmp_buf = malloc(qiov.size);
518 memset(cmp_buf, pattern, qiov.size);
519 if (memcmp(buf, cmp_buf, qiov.size)) {
520 printf("Pattern verification failed at offset %lld, "
521 "%zd bytes\n",
522 (long long) offset, qiov.size);
524 free(cmp_buf);
527 if (qflag)
528 goto out;
530 if (vflag)
531 dump_buffer(buf, offset, qiov.size);
533 /* Finally, report back -- -C gives a parsable format */
534 t2 = tsub(t2, t1);
535 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
537 out:
538 qemu_io_free(buf);
539 return 0;
542 static const cmdinfo_t readv_cmd = {
543 .name = "readv",
544 .cfunc = readv_f,
545 .argmin = 2,
546 .argmax = -1,
547 .args = "[-Cqv] [-P pattern ] off len [len..]",
548 .oneline = "reads a number of bytes at a specified offset",
549 .help = readv_help,
552 static const cmdinfo_t write_cmd;
554 static void
555 write_help(void)
557 printf(
558 "\n"
559 " writes a range of bytes from the given offset\n"
560 "\n"
561 " Example:\n"
562 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
563 "\n"
564 " Writes into a segment of the currently open file, using a buffer\n"
565 " filled with a set pattern (0xcdcdcdcd).\n"
566 " -b, -- write to the VM state rather than the virtual disk\n"
567 " -p, -- use bdrv_pwrite to write the file\n"
568 " -P, -- use different pattern to fill file\n"
569 " -C, -- report statistics in a machine parsable format\n"
570 " -q, -- quite mode, do not show I/O statistics\n"
571 "\n");
574 static int
575 write_f(int argc, char **argv)
577 struct timeval t1, t2;
578 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
579 int c, cnt;
580 char *buf;
581 int64_t offset;
582 int count;
583 /* Some compilers get confused and warn if this is not initialized. */
584 int total = 0;
585 int pattern = 0xcd;
587 while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
588 switch (c) {
589 case 'b':
590 bflag = 1;
591 break;
592 case 'C':
593 Cflag = 1;
594 break;
595 case 'p':
596 pflag = 1;
597 break;
598 case 'P':
599 pattern = atoi(optarg);
600 break;
601 case 'q':
602 qflag = 1;
603 break;
604 default:
605 return command_usage(&write_cmd);
609 if (optind != argc - 2)
610 return command_usage(&write_cmd);
612 if (bflag && pflag) {
613 printf("-b and -p cannot be specified at the same time\n");
614 return 0;
617 offset = cvtnum(argv[optind]);
618 if (offset < 0) {
619 printf("non-numeric length argument -- %s\n", argv[optind]);
620 return 0;
623 optind++;
624 count = cvtnum(argv[optind]);
625 if (count < 0) {
626 printf("non-numeric length argument -- %s\n", argv[optind]);
627 return 0;
630 if (!pflag) {
631 if (offset & 0x1ff) {
632 printf("offset %lld is not sector aligned\n",
633 (long long)offset);
634 return 0;
637 if (count & 0x1ff) {
638 printf("count %d is not sector aligned\n",
639 count);
640 return 0;
644 buf = qemu_io_alloc(count, pattern);
646 gettimeofday(&t1, NULL);
647 if (pflag)
648 cnt = do_pwrite(buf, offset, count, &total);
649 else if (bflag)
650 cnt = do_save_vmstate(buf, offset, count, &total);
651 else
652 cnt = do_write(buf, offset, count, &total);
653 gettimeofday(&t2, NULL);
655 if (cnt < 0) {
656 printf("write failed: %s\n", strerror(-cnt));
657 goto out;
660 if (qflag)
661 goto out;
663 /* Finally, report back -- -C gives a parsable format */
664 t2 = tsub(t2, t1);
665 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
667 out:
668 qemu_io_free(buf);
670 return 0;
673 static const cmdinfo_t write_cmd = {
674 .name = "write",
675 .altname = "w",
676 .cfunc = write_f,
677 .argmin = 2,
678 .argmax = -1,
679 .args = "[-abCpq] [-P pattern ] off len",
680 .oneline = "writes a number of bytes at a specified offset",
681 .help = write_help,
684 static const cmdinfo_t writev_cmd;
686 static void
687 writev_help(void)
689 printf(
690 "\n"
691 " writes a range of bytes from the given offset source from multiple buffers\n"
692 "\n"
693 " Example:\n"
694 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
695 "\n"
696 " Writes into a segment of the currently open file, using a buffer\n"
697 " filled with a set pattern (0xcdcdcdcd).\n"
698 " -P, -- use different pattern to fill file\n"
699 " -C, -- report statistics in a machine parsable format\n"
700 " -q, -- quite mode, do not show I/O statistics\n"
701 "\n");
704 static int
705 writev_f(int argc, char **argv)
707 struct timeval t1, t2;
708 int Cflag = 0, qflag = 0;
709 int c, cnt;
710 char *buf;
711 int64_t offset;
712 int total;
713 int nr_iov;
714 int pattern = 0xcd;
715 QEMUIOVector qiov;
717 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
718 switch (c) {
719 case 'C':
720 Cflag = 1;
721 break;
722 case 'q':
723 qflag = 1;
724 break;
725 case 'P':
726 pattern = atoi(optarg);
727 break;
728 default:
729 return command_usage(&writev_cmd);
733 if (optind > argc - 2)
734 return command_usage(&writev_cmd);
736 offset = cvtnum(argv[optind]);
737 if (offset < 0) {
738 printf("non-numeric length argument -- %s\n", argv[optind]);
739 return 0;
741 optind++;
743 if (offset & 0x1ff) {
744 printf("offset %lld is not sector aligned\n",
745 (long long)offset);
746 return 0;
749 nr_iov = argc - optind;
750 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
752 gettimeofday(&t1, NULL);
753 cnt = do_aio_writev(&qiov, offset, &total);
754 gettimeofday(&t2, NULL);
756 if (cnt < 0) {
757 printf("writev failed: %s\n", strerror(-cnt));
758 goto out;
761 if (qflag)
762 goto out;
764 /* Finally, report back -- -C gives a parsable format */
765 t2 = tsub(t2, t1);
766 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
767 out:
768 qemu_io_free(buf);
769 return 0;
772 static const cmdinfo_t writev_cmd = {
773 .name = "writev",
774 .cfunc = writev_f,
775 .argmin = 2,
776 .argmax = -1,
777 .args = "[-Cq] [-P pattern ] off len [len..]",
778 .oneline = "writes a number of bytes at a specified offset",
779 .help = writev_help,
782 struct aio_ctx {
783 QEMUIOVector qiov;
784 int64_t offset;
785 char *buf;
786 int qflag;
787 int vflag;
788 int Cflag;
789 int Pflag;
790 int pattern;
791 struct timeval t1;
794 static void
795 aio_write_done(void *opaque, int ret)
797 struct aio_ctx *ctx = opaque;
798 struct timeval t2;
800 gettimeofday(&t2, NULL);
803 if (ret < 0) {
804 printf("aio_write failed: %s\n", strerror(-ret));
805 goto out;
808 if (ctx->qflag) {
809 goto out;
812 /* Finally, report back -- -C gives a parsable format */
813 t2 = tsub(t2, ctx->t1);
814 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
815 ctx->qiov.size, 1, ctx->Cflag);
816 out:
817 qemu_io_free(ctx->buf);
818 free(ctx);
821 static const cmdinfo_t aio_read_cmd;
823 static void
824 aio_read_done(void *opaque, int ret)
826 struct aio_ctx *ctx = opaque;
827 struct timeval t2;
829 gettimeofday(&t2, NULL);
831 if (ret < 0) {
832 printf("readv failed: %s\n", strerror(-ret));
833 goto out;
836 if (ctx->Pflag) {
837 void *cmp_buf = malloc(ctx->qiov.size);
839 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
840 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
841 printf("Pattern verification failed at offset %lld, "
842 "%zd bytes\n",
843 (long long) ctx->offset, ctx->qiov.size);
845 free(cmp_buf);
848 if (ctx->qflag) {
849 goto out;
852 if (ctx->vflag) {
853 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
856 /* Finally, report back -- -C gives a parsable format */
857 t2 = tsub(t2, ctx->t1);
858 print_report("read", &t2, ctx->offset, ctx->qiov.size,
859 ctx->qiov.size, 1, ctx->Cflag);
860 out:
861 qemu_io_free(ctx->buf);
862 free(ctx);
865 static void
866 aio_read_help(void)
868 printf(
869 "\n"
870 " asynchronously reads a range of bytes from the given offset\n"
871 "\n"
872 " Example:\n"
873 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
874 "\n"
875 " Reads a segment of the currently open file, optionally dumping it to the\n"
876 " standard output stream (with -v option) for subsequent inspection.\n"
877 " The read is performed asynchronously and should the aio_flush command \n"
878 " should be used to ensure all outstanding aio requests have been completed\n"
879 " -C, -- report statistics in a machine parsable format\n"
880 " -P, -- use a pattern to verify read data\n"
881 " -v, -- dump buffer to standard output\n"
882 " -q, -- quite mode, do not show I/O statistics\n"
883 "\n");
886 static int
887 aio_read_f(int argc, char **argv)
889 int nr_iov, c;
890 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
891 BlockDriverAIOCB *acb;
893 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
894 switch (c) {
895 case 'C':
896 ctx->Cflag = 1;
897 break;
898 case 'P':
899 ctx->Pflag = 1;
900 ctx->pattern = atoi(optarg);
901 break;
902 case 'q':
903 ctx->qflag = 1;
904 break;
905 case 'v':
906 ctx->vflag = 1;
907 break;
908 default:
909 free(ctx);
910 return command_usage(&aio_read_cmd);
914 if (optind > argc - 2) {
915 free(ctx);
916 return command_usage(&aio_read_cmd);
919 ctx->offset = cvtnum(argv[optind]);
920 if (ctx->offset < 0) {
921 printf("non-numeric length argument -- %s\n", argv[optind]);
922 free(ctx);
923 return 0;
925 optind++;
927 if (ctx->offset & 0x1ff) {
928 printf("offset %lld is not sector aligned\n",
929 (long long)ctx->offset);
930 free(ctx);
931 return 0;
934 nr_iov = argc - optind;
935 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
937 gettimeofday(&ctx->t1, NULL);
938 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
939 ctx->qiov.size >> 9, aio_read_done, ctx);
940 if (!acb) {
941 free(ctx->buf);
942 free(ctx);
943 return -EIO;
946 return 0;
949 static const cmdinfo_t aio_read_cmd = {
950 .name = "aio_read",
951 .cfunc = aio_read_f,
952 .argmin = 2,
953 .argmax = -1,
954 .args = "[-Cqv] [-P pattern ] off len [len..]",
955 .oneline = "asynchronously reads a number of bytes",
956 .help = aio_read_help,
959 static const cmdinfo_t aio_write_cmd;
961 static void
962 aio_write_help(void)
964 printf(
965 "\n"
966 " asynchronously writes a range of bytes from the given offset source \n"
967 " from multiple buffers\n"
968 "\n"
969 " Example:\n"
970 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
971 "\n"
972 " Writes into a segment of the currently open file, using a buffer\n"
973 " filled with a set pattern (0xcdcdcdcd).\n"
974 " The write is performed asynchronously and should the aio_flush command \n"
975 " should be used to ensure all outstanding aio requests have been completed\n"
976 " -P, -- use different pattern to fill file\n"
977 " -C, -- report statistics in a machine parsable format\n"
978 " -q, -- quite mode, do not show I/O statistics\n"
979 "\n");
983 static int
984 aio_write_f(int argc, char **argv)
986 int nr_iov, c;
987 int pattern = 0xcd;
988 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
989 BlockDriverAIOCB *acb;
991 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
992 switch (c) {
993 case 'C':
994 ctx->Cflag = 1;
995 break;
996 case 'q':
997 ctx->qflag = 1;
998 break;
999 case 'P':
1000 pattern = atoi(optarg);
1001 break;
1002 default:
1003 free(ctx);
1004 return command_usage(&aio_write_cmd);
1008 if (optind > argc - 2) {
1009 free(ctx);
1010 return command_usage(&aio_write_cmd);
1013 ctx->offset = cvtnum(argv[optind]);
1014 if (ctx->offset < 0) {
1015 printf("non-numeric length argument -- %s\n", argv[optind]);
1016 free(ctx);
1017 return 0;
1019 optind++;
1021 if (ctx->offset & 0x1ff) {
1022 printf("offset %lld is not sector aligned\n",
1023 (long long)ctx->offset);
1024 free(ctx);
1025 return 0;
1028 nr_iov = argc - optind;
1029 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
1031 gettimeofday(&ctx->t1, NULL);
1032 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1033 ctx->qiov.size >> 9, aio_write_done, ctx);
1034 if (!acb) {
1035 free(ctx->buf);
1036 free(ctx);
1037 return -EIO;
1040 return 0;
1043 static const cmdinfo_t aio_write_cmd = {
1044 .name = "aio_write",
1045 .cfunc = aio_write_f,
1046 .argmin = 2,
1047 .argmax = -1,
1048 .args = "[-Cq] [-P pattern ] off len [len..]",
1049 .oneline = "asynchronously writes a number of bytes",
1050 .help = aio_write_help,
1053 static int
1054 aio_flush_f(int argc, char **argv)
1056 qemu_aio_flush();
1057 return 0;
1060 static const cmdinfo_t aio_flush_cmd = {
1061 .name = "aio_flush",
1062 .cfunc = aio_flush_f,
1063 .oneline = "completes all outstanding aio requets"
1066 static int
1067 flush_f(int argc, char **argv)
1069 bdrv_flush(bs);
1070 return 0;
1073 static const cmdinfo_t flush_cmd = {
1074 .name = "flush",
1075 .altname = "f",
1076 .cfunc = flush_f,
1077 .oneline = "flush all in-core file state to disk",
1080 static int
1081 truncate_f(int argc, char **argv)
1083 int64_t offset;
1084 int ret;
1086 offset = cvtnum(argv[1]);
1087 if (offset < 0) {
1088 printf("non-numeric truncate argument -- %s\n", argv[1]);
1089 return 0;
1092 ret = bdrv_truncate(bs, offset);
1093 if (ret < 0) {
1094 printf("truncate: %s", strerror(ret));
1095 return 0;
1098 return 0;
1101 static const cmdinfo_t truncate_cmd = {
1102 .name = "truncate",
1103 .altname = "t",
1104 .cfunc = truncate_f,
1105 .argmin = 1,
1106 .argmax = 1,
1107 .args = "off",
1108 .oneline = "truncates the current file at the given offset",
1111 static int
1112 length_f(int argc, char **argv)
1114 int64_t size;
1115 char s1[64];
1117 size = bdrv_getlength(bs);
1118 if (size < 0) {
1119 printf("getlength: %s", strerror(size));
1120 return 0;
1123 cvtstr(size, s1, sizeof(s1));
1124 printf("%s\n", s1);
1125 return 0;
1129 static const cmdinfo_t length_cmd = {
1130 .name = "length",
1131 .altname = "l",
1132 .cfunc = length_f,
1133 .oneline = "gets the length of the current file",
1137 static int
1138 info_f(int argc, char **argv)
1140 BlockDriverInfo bdi;
1141 char s1[64], s2[64];
1142 int ret;
1144 if (bs->drv && bs->drv->format_name)
1145 printf("format name: %s\n", bs->drv->format_name);
1146 if (bs->drv && bs->drv->protocol_name)
1147 printf("format name: %s\n", bs->drv->protocol_name);
1149 ret = bdrv_get_info(bs, &bdi);
1150 if (ret)
1151 return 0;
1153 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1154 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1156 printf("cluster size: %s\n", s1);
1157 printf("vm state offset: %s\n", s2);
1159 return 0;
1164 static const cmdinfo_t info_cmd = {
1165 .name = "info",
1166 .altname = "i",
1167 .cfunc = info_f,
1168 .oneline = "prints information about the current file",
1171 static int
1172 alloc_f(int argc, char **argv)
1174 int64_t offset;
1175 int nb_sectors;
1176 char s1[64];
1177 int num;
1178 int ret;
1179 const char *retstr;
1181 offset = cvtnum(argv[1]);
1182 if (offset & 0x1ff) {
1183 printf("offset %lld is not sector aligned\n",
1184 (long long)offset);
1185 return 0;
1188 if (argc == 3)
1189 nb_sectors = cvtnum(argv[2]);
1190 else
1191 nb_sectors = 1;
1193 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1195 cvtstr(offset, s1, sizeof(s1));
1197 retstr = ret ? "allocated" : "not allocated";
1198 if (nb_sectors == 1)
1199 printf("sector %s at offset %s\n", retstr, s1);
1200 else
1201 printf("%d/%d sectors %s at offset %s\n",
1202 num, nb_sectors, retstr, s1);
1203 return 0;
1206 static const cmdinfo_t alloc_cmd = {
1207 .name = "alloc",
1208 .altname = "a",
1209 .argmin = 1,
1210 .argmax = 2,
1211 .cfunc = alloc_f,
1212 .args = "off [sectors]",
1213 .oneline = "checks if a sector is present in the file",
1216 static int
1217 close_f(int argc, char **argv)
1219 bdrv_close(bs);
1220 bs = NULL;
1221 return 0;
1224 static const cmdinfo_t close_cmd = {
1225 .name = "close",
1226 .altname = "c",
1227 .cfunc = close_f,
1228 .oneline = "close the current open file",
1231 static int openfile(char *name, int flags, int growable)
1233 if (bs) {
1234 fprintf(stderr, "file open already, try 'help close'\n");
1235 return 1;
1238 bs = bdrv_new("hda");
1239 if (!bs)
1240 return 1;
1242 if (growable) {
1243 flags |= BDRV_O_FILE;
1246 if (bdrv_open(bs, name, flags) == -1) {
1247 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1248 bs = NULL;
1249 return 1;
1252 if (growable) {
1253 bs->growable = 1;
1255 return 0;
1258 static void
1259 open_help(void)
1261 printf(
1262 "\n"
1263 " opens a new file in the requested mode\n"
1264 "\n"
1265 " Example:\n"
1266 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1267 "\n"
1268 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1269 " -C, -- create new file if it doesn't exist\n"
1270 " -r, -- open file read-only\n"
1271 " -s, -- use snapshot file\n"
1272 " -n, -- disable host cache\n"
1273 " -g, -- allow file to grow (only applies to protocols)"
1274 "\n");
1277 static const cmdinfo_t open_cmd;
1279 static int
1280 open_f(int argc, char **argv)
1282 int flags = 0;
1283 int readonly = 0;
1284 int growable = 0;
1285 int c;
1287 while ((c = getopt(argc, argv, "snCrg")) != EOF) {
1288 switch (c) {
1289 case 's':
1290 flags |= BDRV_O_SNAPSHOT;
1291 break;
1292 case 'n':
1293 flags |= BDRV_O_NOCACHE;
1294 break;
1295 case 'C':
1296 flags |= BDRV_O_CREAT;
1297 break;
1298 case 'r':
1299 readonly = 1;
1300 break;
1301 case 'g':
1302 growable = 1;
1303 break;
1304 default:
1305 return command_usage(&open_cmd);
1309 if (readonly)
1310 flags |= BDRV_O_RDONLY;
1311 else
1312 flags |= BDRV_O_RDWR;
1314 if (optind != argc - 1)
1315 return command_usage(&open_cmd);
1317 return openfile(argv[optind], flags, growable);
1320 static const cmdinfo_t open_cmd = {
1321 .name = "open",
1322 .altname = "o",
1323 .cfunc = open_f,
1324 .argmin = 1,
1325 .argmax = -1,
1326 .flags = CMD_NOFILE_OK,
1327 .args = "[-Crsn] [path]",
1328 .oneline = "open the file specified by path",
1329 .help = open_help,
1332 static int
1333 init_args_command(
1334 int index)
1336 /* only one device allowed so far */
1337 if (index >= 1)
1338 return 0;
1339 return ++index;
1342 static int
1343 init_check_command(
1344 const cmdinfo_t *ct)
1346 if (ct->flags & CMD_FLAG_GLOBAL)
1347 return 1;
1348 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1349 fprintf(stderr, "no file open, try 'help open'\n");
1350 return 0;
1352 return 1;
1355 static void usage(const char *name)
1357 printf(
1358 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1359 "QEMU Disk exerciser\n"
1360 "\n"
1361 " -C, --create create new file if it doesn't exist\n"
1362 " -c, --cmd command to execute\n"
1363 " -r, --read-only export read-only\n"
1364 " -s, --snapshot use snapshot file\n"
1365 " -n, --nocache disable host cache\n"
1366 " -g, --growable allow file to grow (only applies to protocols)\n"
1367 " -m, --misalign misalign allocations for O_DIRECT\n"
1368 " -h, --help display this help and exit\n"
1369 " -V, --version output version information and exit\n"
1370 "\n",
1371 name);
1375 int main(int argc, char **argv)
1377 int readonly = 0;
1378 int growable = 0;
1379 const char *sopt = "hVc:Crsnmg";
1380 struct option lopt[] = {
1381 { "help", 0, NULL, 'h' },
1382 { "version", 0, NULL, 'V' },
1383 { "offset", 1, NULL, 'o' },
1384 { "cmd", 1, NULL, 'c' },
1385 { "create", 0, NULL, 'C' },
1386 { "read-only", 0, NULL, 'r' },
1387 { "snapshot", 0, NULL, 's' },
1388 { "nocache", 0, NULL, 'n' },
1389 { "misalign", 0, NULL, 'm' },
1390 { "growable", 0, NULL, 'g' },
1391 { NULL, 0, NULL, 0 }
1393 int c;
1394 int opt_index = 0;
1395 int flags = 0;
1397 progname = basename(argv[0]);
1399 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1400 switch (c) {
1401 case 's':
1402 flags |= BDRV_O_SNAPSHOT;
1403 break;
1404 case 'n':
1405 flags |= BDRV_O_NOCACHE;
1406 break;
1407 case 'c':
1408 add_user_command(optarg);
1409 break;
1410 case 'C':
1411 flags |= BDRV_O_CREAT;
1412 break;
1413 case 'r':
1414 readonly = 1;
1415 break;
1416 case 'm':
1417 misalign = 1;
1418 break;
1419 case 'g':
1420 growable = 1;
1421 break;
1422 case 'V':
1423 printf("%s version %s\n", progname, VERSION);
1424 exit(0);
1425 case 'h':
1426 usage(progname);
1427 exit(0);
1428 default:
1429 usage(progname);
1430 exit(1);
1434 if ((argc - optind) > 1) {
1435 usage(progname);
1436 exit(1);
1439 bdrv_init();
1441 /* initialize commands */
1442 quit_init();
1443 help_init();
1444 add_command(&open_cmd);
1445 add_command(&close_cmd);
1446 add_command(&read_cmd);
1447 add_command(&readv_cmd);
1448 add_command(&write_cmd);
1449 add_command(&writev_cmd);
1450 add_command(&aio_read_cmd);
1451 add_command(&aio_write_cmd);
1452 add_command(&aio_flush_cmd);
1453 add_command(&flush_cmd);
1454 add_command(&truncate_cmd);
1455 add_command(&length_cmd);
1456 add_command(&info_cmd);
1457 add_command(&alloc_cmd);
1459 add_args_command(init_args_command);
1460 add_check_command(init_check_command);
1462 /* open the device */
1463 if (readonly)
1464 flags |= BDRV_O_RDONLY;
1465 else
1466 flags |= BDRV_O_RDWR;
1468 if ((argc - optind) == 1)
1469 openfile(argv[optind], flags, growable);
1470 command_loop();
1473 * Make sure all outstanding requests get flushed the program exits.
1475 qemu_aio_flush();
1477 if (bs)
1478 bdrv_close(bs);
1479 return 0;