Fix non-ACPI Timer Interrupt Routing - v3
[qemu/aliguori-queue.git] / qemu-io.c
blob076984a9470905e62e5c39937cdf4e5a23f31c7d
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/types.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13 #include <getopt.h>
15 #include "qemu-common.h"
16 #include "block_int.h"
17 #include "cmd.h"
19 #define VERSION "0.0.1"
21 #define CMD_NOFILE_OK 0x01
23 char *progname;
24 static BlockDriverState *bs;
26 static int misalign;
29 * Memory allocation helpers.
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len, int pattern)
38 void *buf;
40 if (misalign)
41 len += MISALIGN_OFFSET;
42 buf = qemu_memalign(512, len);
43 memset(buf, pattern, len);
44 if (misalign)
45 buf += MISALIGN_OFFSET;
46 return buf;
49 static void qemu_io_free(void *p)
51 if (misalign)
52 p -= MISALIGN_OFFSET;
53 qemu_vfree(p);
56 static void
57 dump_buffer(const void *buffer, int64_t offset, int len)
59 int i, j;
60 const uint8_t *p;
62 for (i = 0, p = buffer; i < len; i += 16) {
63 const uint8_t *s = p;
65 printf("%08llx: ", (unsigned long long)offset + i);
66 for (j = 0; j < 16 && i + j < len; j++, p++)
67 printf("%02x ", *p);
68 printf(" ");
69 for (j = 0; j < 16 && i + j < len; j++, s++) {
70 if (isalnum(*s))
71 printf("%c", *s);
72 else
73 printf(".");
75 printf("\n");
79 static void
80 print_report(const char *op, struct timeval *t, int64_t offset,
81 int count, int total, int cnt, int Cflag)
83 char s1[64], s2[64], ts[64];
85 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
86 if (!Cflag) {
87 cvtstr((double)total, s1, sizeof(s1));
88 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
89 printf("%s %d/%d bytes at offset %lld\n",
90 op, total, count, (long long)offset);
91 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
92 s1, cnt, ts, s2, tdiv((double)cnt, *t));
93 } else {/* bytes,ops,time,bytes/sec,ops/sec */
94 printf("%d,%d,%s,%.3f,%.3f\n",
95 total, cnt, ts,
96 tdiv((double)total, *t),
97 tdiv((double)cnt, *t));
102 * Parse multiple length statements for vectored I/O, and construct an I/O
103 * vector matching it.
105 static void *
106 create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
108 size_t *sizes = calloc(nr_iov, sizeof(size_t));
109 size_t count = 0;
110 void *buf, *p;
111 int i;
113 for (i = 0; i < nr_iov; i++) {
114 char *arg = argv[i];
115 long long len;
117 len = cvtnum(arg);
118 if (len < 0) {
119 printf("non-numeric length argument -- %s\n", arg);
120 return NULL;
123 /* should be SIZE_T_MAX, but that doesn't exist */
124 if (len > UINT_MAX) {
125 printf("too large length argument -- %s\n", arg);
126 return NULL;
129 if (len & 0x1ff) {
130 printf("length argument %lld is not sector aligned\n",
131 len);
132 return NULL;
135 sizes[i] = len;
136 count += len;
139 qemu_iovec_init(qiov, nr_iov);
141 buf = p = qemu_io_alloc(count, pattern);
143 for (i = 0; i < nr_iov; i++) {
144 qemu_iovec_add(qiov, p, sizes[i]);
145 p += sizes[i];
148 free(sizes);
149 return buf;
152 static int do_read(char *buf, int64_t offset, int count, int *total)
154 int ret;
156 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
157 if (ret < 0)
158 return ret;
159 *total = count;
160 return 1;
163 static int do_write(char *buf, int64_t offset, int count, int *total)
165 int ret;
167 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
168 if (ret < 0)
169 return ret;
170 *total = count;
171 return 1;
174 static int do_pread(char *buf, int64_t offset, int count, int *total)
176 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
177 if (*total < 0)
178 return *total;
179 return 1;
182 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
184 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
185 if (*total < 0)
186 return *total;
187 return 1;
190 #define NOT_DONE 0x7fffffff
191 static void aio_rw_done(void *opaque, int ret)
193 *(int *)opaque = ret;
196 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
198 BlockDriverAIOCB *acb;
199 int async_ret = NOT_DONE;
201 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
202 aio_rw_done, &async_ret);
203 if (!acb)
204 return -EIO;
206 while (async_ret == NOT_DONE)
207 qemu_aio_wait();
209 *total = qiov->size;
210 return async_ret < 0 ? async_ret : 1;
213 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
215 BlockDriverAIOCB *acb;
216 int async_ret = NOT_DONE;
218 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
219 aio_rw_done, &async_ret);
220 if (!acb)
221 return -EIO;
223 while (async_ret == NOT_DONE)
224 qemu_aio_wait();
226 *total = qiov->size;
227 return async_ret < 0 ? async_ret : 1;
231 static const cmdinfo_t read_cmd;
233 static void
234 read_help(void)
236 printf(
237 "\n"
238 " reads a range of bytes from the given offset\n"
239 "\n"
240 " Example:\n"
241 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
242 "\n"
243 " Reads a segment of the currently open file, optionally dumping it to the\n"
244 " standard output stream (with -v option) for subsequent inspection.\n"
245 " -C, -- report statistics in a machine parsable format\n"
246 " -l, -- length for pattern verification (only with -P)\n"
247 " -p, -- use bdrv_pread to read the file\n"
248 " -P, -- use a pattern to verify read data\n"
249 " -q, -- quite mode, do not show I/O statistics\n"
250 " -s, -- start offset for pattern verification (only with -P)\n"
251 " -v, -- dump buffer to standard output\n"
252 "\n");
255 static int
256 read_f(int argc, char **argv)
258 struct timeval t1, t2;
259 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
260 int Pflag = 0, sflag = 0, lflag = 0;
261 int c, cnt;
262 char *buf;
263 int64_t offset;
264 int count;
265 /* Some compilers get confused and warn if this is not initialized. */
266 int total = 0;
267 int pattern = 0, pattern_offset = 0, pattern_count = 0;
269 while ((c = getopt(argc, argv, "Cl:pP:qs:v")) != EOF) {
270 switch (c) {
271 case 'C':
272 Cflag = 1;
273 break;
274 case 'l':
275 lflag = 1;
276 pattern_count = cvtnum(optarg);
277 if (pattern_count < 0) {
278 printf("non-numeric length argument -- %s\n", optarg);
279 return 0;
281 break;
282 case 'p':
283 pflag = 1;
284 break;
285 case 'P':
286 Pflag = 1;
287 pattern = atoi(optarg);
288 break;
289 case 'q':
290 qflag = 1;
291 break;
292 case 's':
293 sflag = 1;
294 pattern_offset = cvtnum(optarg);
295 if (pattern_offset < 0) {
296 printf("non-numeric length argument -- %s\n", optarg);
297 return 0;
299 break;
300 case 'v':
301 vflag = 1;
302 break;
303 default:
304 return command_usage(&read_cmd);
308 if (optind != argc - 2)
309 return command_usage(&read_cmd);
311 offset = cvtnum(argv[optind]);
312 if (offset < 0) {
313 printf("non-numeric length argument -- %s\n", argv[optind]);
314 return 0;
317 optind++;
318 count = cvtnum(argv[optind]);
319 if (count < 0) {
320 printf("non-numeric length argument -- %s\n", argv[optind]);
321 return 0;
324 if (!Pflag && (lflag || sflag)) {
325 return command_usage(&read_cmd);
328 if (!lflag) {
329 pattern_count = count - pattern_offset;
332 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
333 printf("pattern verfication range exceeds end of read data\n");
334 return 0;
337 if (!pflag)
338 if (offset & 0x1ff) {
339 printf("offset %lld is not sector aligned\n",
340 (long long)offset);
341 return 0;
343 if (count & 0x1ff) {
344 printf("count %d is not sector aligned\n",
345 count);
346 return 0;
350 buf = qemu_io_alloc(count, 0xab);
352 gettimeofday(&t1, NULL);
353 if (pflag)
354 cnt = do_pread(buf, offset, count, &total);
355 else
356 cnt = do_read(buf, offset, count, &total);
357 gettimeofday(&t2, NULL);
359 if (cnt < 0) {
360 printf("read failed: %s\n", strerror(-cnt));
361 goto out;
364 if (Pflag) {
365 void* cmp_buf = malloc(pattern_count);
366 memset(cmp_buf, pattern, pattern_count);
367 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
368 printf("Pattern verification failed at offset %lld, "
369 "%d bytes\n",
370 (long long) offset + pattern_offset, pattern_count);
372 free(cmp_buf);
375 if (qflag)
376 goto out;
378 if (vflag)
379 dump_buffer(buf, offset, count);
381 /* Finally, report back -- -C gives a parsable format */
382 t2 = tsub(t2, t1);
383 print_report("read", &t2, offset, count, total, cnt, Cflag);
385 out:
386 qemu_io_free(buf);
388 return 0;
391 static const cmdinfo_t read_cmd = {
392 .name = "read",
393 .altname = "r",
394 .cfunc = read_f,
395 .argmin = 2,
396 .argmax = -1,
397 .args = "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
398 .oneline = "reads a number of bytes at a specified offset",
399 .help = read_help,
402 static const cmdinfo_t readv_cmd;
404 static void
405 readv_help(void)
407 printf(
408 "\n"
409 " reads a range of bytes from the given offset into multiple buffers\n"
410 "\n"
411 " Example:\n"
412 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
413 "\n"
414 " Reads a segment of the currently open file, optionally dumping it to the\n"
415 " standard output stream (with -v option) for subsequent inspection.\n"
416 " Uses multiple iovec buffers if more than one byte range is specified.\n"
417 " -C, -- report statistics in a machine parsable format\n"
418 " -P, -- use a pattern to verify read data\n"
419 " -v, -- dump buffer to standard output\n"
420 " -q, -- quite mode, do not show I/O statistics\n"
421 "\n");
424 static int
425 readv_f(int argc, char **argv)
427 struct timeval t1, t2;
428 int Cflag = 0, qflag = 0, vflag = 0;
429 int c, cnt;
430 char *buf;
431 int64_t offset;
432 int total;
433 int nr_iov;
434 QEMUIOVector qiov;
435 int pattern = 0;
436 int Pflag = 0;
438 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
439 switch (c) {
440 case 'C':
441 Cflag = 1;
442 break;
443 case 'P':
444 Pflag = 1;
445 pattern = atoi(optarg);
446 break;
447 case 'q':
448 qflag = 1;
449 break;
450 case 'v':
451 vflag = 1;
452 break;
453 default:
454 return command_usage(&readv_cmd);
458 if (optind > argc - 2)
459 return command_usage(&readv_cmd);
462 offset = cvtnum(argv[optind]);
463 if (offset < 0) {
464 printf("non-numeric length argument -- %s\n", argv[optind]);
465 return 0;
467 optind++;
469 if (offset & 0x1ff) {
470 printf("offset %lld is not sector aligned\n",
471 (long long)offset);
472 return 0;
475 nr_iov = argc - optind;
476 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
478 gettimeofday(&t1, NULL);
479 cnt = do_aio_readv(&qiov, offset, &total);
480 gettimeofday(&t2, NULL);
482 if (cnt < 0) {
483 printf("readv failed: %s\n", strerror(-cnt));
484 goto out;
487 if (Pflag) {
488 void* cmp_buf = malloc(qiov.size);
489 memset(cmp_buf, pattern, qiov.size);
490 if (memcmp(buf, cmp_buf, qiov.size)) {
491 printf("Pattern verification failed at offset %lld, "
492 "%zd bytes\n",
493 (long long) offset, qiov.size);
495 free(cmp_buf);
498 if (qflag)
499 goto out;
501 if (vflag)
502 dump_buffer(buf, offset, qiov.size);
504 /* Finally, report back -- -C gives a parsable format */
505 t2 = tsub(t2, t1);
506 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
508 out:
509 qemu_io_free(buf);
510 return 0;
513 static const cmdinfo_t readv_cmd = {
514 .name = "readv",
515 .cfunc = readv_f,
516 .argmin = 2,
517 .argmax = -1,
518 .args = "[-Cqv] [-P pattern ] off len [len..]",
519 .oneline = "reads a number of bytes at a specified offset",
520 .help = readv_help,
523 static const cmdinfo_t write_cmd;
525 static void
526 write_help(void)
528 printf(
529 "\n"
530 " writes a range of bytes from the given offset\n"
531 "\n"
532 " Example:\n"
533 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
534 "\n"
535 " Writes into a segment of the currently open file, using a buffer\n"
536 " filled with a set pattern (0xcdcdcdcd).\n"
537 " -p, -- use bdrv_pwrite to write the file\n"
538 " -P, -- use different pattern to fill file\n"
539 " -C, -- report statistics in a machine parsable format\n"
540 " -q, -- quite mode, do not show I/O statistics\n"
541 "\n");
544 static int
545 write_f(int argc, char **argv)
547 struct timeval t1, t2;
548 int Cflag = 0, pflag = 0, qflag = 0;
549 int c, cnt;
550 char *buf;
551 int64_t offset;
552 int count;
553 /* Some compilers get confused and warn if this is not initialized. */
554 int total = 0;
555 int pattern = 0xcd;
557 while ((c = getopt(argc, argv, "CpP:q")) != EOF) {
558 switch (c) {
559 case 'C':
560 Cflag = 1;
561 break;
562 case 'p':
563 pflag = 1;
564 break;
565 case 'P':
566 pattern = atoi(optarg);
567 break;
568 case 'q':
569 qflag = 1;
570 break;
571 default:
572 return command_usage(&write_cmd);
576 if (optind != argc - 2)
577 return command_usage(&write_cmd);
579 offset = cvtnum(argv[optind]);
580 if (offset < 0) {
581 printf("non-numeric length argument -- %s\n", argv[optind]);
582 return 0;
585 optind++;
586 count = cvtnum(argv[optind]);
587 if (count < 0) {
588 printf("non-numeric length argument -- %s\n", argv[optind]);
589 return 0;
592 if (!pflag) {
593 if (offset & 0x1ff) {
594 printf("offset %lld is not sector aligned\n",
595 (long long)offset);
596 return 0;
599 if (count & 0x1ff) {
600 printf("count %d is not sector aligned\n",
601 count);
602 return 0;
606 buf = qemu_io_alloc(count, pattern);
608 gettimeofday(&t1, NULL);
609 if (pflag)
610 cnt = do_pwrite(buf, offset, count, &total);
611 else
612 cnt = do_write(buf, offset, count, &total);
613 gettimeofday(&t2, NULL);
615 if (cnt < 0) {
616 printf("write failed: %s\n", strerror(-cnt));
617 goto out;
620 if (qflag)
621 goto out;
623 /* Finally, report back -- -C gives a parsable format */
624 t2 = tsub(t2, t1);
625 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
627 out:
628 qemu_io_free(buf);
630 return 0;
633 static const cmdinfo_t write_cmd = {
634 .name = "write",
635 .altname = "w",
636 .cfunc = write_f,
637 .argmin = 2,
638 .argmax = -1,
639 .args = "[-aCpq] [-P pattern ] off len",
640 .oneline = "writes a number of bytes at a specified offset",
641 .help = write_help,
644 static const cmdinfo_t writev_cmd;
646 static void
647 writev_help(void)
649 printf(
650 "\n"
651 " writes a range of bytes from the given offset source from multiple buffers\n"
652 "\n"
653 " Example:\n"
654 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
655 "\n"
656 " Writes into a segment of the currently open file, using a buffer\n"
657 " filled with a set pattern (0xcdcdcdcd).\n"
658 " -P, -- use different pattern to fill file\n"
659 " -C, -- report statistics in a machine parsable format\n"
660 " -q, -- quite mode, do not show I/O statistics\n"
661 "\n");
664 static int
665 writev_f(int argc, char **argv)
667 struct timeval t1, t2;
668 int Cflag = 0, qflag = 0;
669 int c, cnt;
670 char *buf;
671 int64_t offset;
672 int total;
673 int nr_iov;
674 int pattern = 0xcd;
675 QEMUIOVector qiov;
677 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
678 switch (c) {
679 case 'C':
680 Cflag = 1;
681 break;
682 case 'q':
683 qflag = 1;
684 break;
685 case 'P':
686 pattern = atoi(optarg);
687 break;
688 default:
689 return command_usage(&writev_cmd);
693 if (optind > argc - 2)
694 return command_usage(&writev_cmd);
696 offset = cvtnum(argv[optind]);
697 if (offset < 0) {
698 printf("non-numeric length argument -- %s\n", argv[optind]);
699 return 0;
701 optind++;
703 if (offset & 0x1ff) {
704 printf("offset %lld is not sector aligned\n",
705 (long long)offset);
706 return 0;
709 nr_iov = argc - optind;
710 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
712 gettimeofday(&t1, NULL);
713 cnt = do_aio_writev(&qiov, offset, &total);
714 gettimeofday(&t2, NULL);
716 if (cnt < 0) {
717 printf("writev failed: %s\n", strerror(-cnt));
718 goto out;
721 if (qflag)
722 goto out;
724 /* Finally, report back -- -C gives a parsable format */
725 t2 = tsub(t2, t1);
726 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
727 out:
728 qemu_io_free(buf);
729 return 0;
732 static const cmdinfo_t writev_cmd = {
733 .name = "writev",
734 .cfunc = writev_f,
735 .argmin = 2,
736 .argmax = -1,
737 .args = "[-Cq] [-P pattern ] off len [len..]",
738 .oneline = "writes a number of bytes at a specified offset",
739 .help = writev_help,
742 struct aio_ctx {
743 QEMUIOVector qiov;
744 int64_t offset;
745 char *buf;
746 int qflag;
747 int vflag;
748 int Cflag;
749 int Pflag;
750 int pattern;
751 struct timeval t1;
754 static void
755 aio_write_done(void *opaque, int ret)
757 struct aio_ctx *ctx = opaque;
758 struct timeval t2;
760 gettimeofday(&t2, NULL);
763 if (ret < 0) {
764 printf("aio_write failed: %s\n", strerror(-ret));
765 goto out;
768 if (ctx->qflag) {
769 goto out;
772 /* Finally, report back -- -C gives a parsable format */
773 t2 = tsub(t2, ctx->t1);
774 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
775 ctx->qiov.size, 1, ctx->Cflag);
776 out:
777 qemu_io_free(ctx->buf);
778 free(ctx);
781 static const cmdinfo_t aio_read_cmd;
783 static void
784 aio_read_done(void *opaque, int ret)
786 struct aio_ctx *ctx = opaque;
787 struct timeval t2;
789 gettimeofday(&t2, NULL);
791 if (ret < 0) {
792 printf("readv failed: %s\n", strerror(-ret));
793 goto out;
796 if (ctx->Pflag) {
797 void *cmp_buf = malloc(ctx->qiov.size);
799 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
800 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
801 printf("Pattern verification failed at offset %lld, "
802 "%zd bytes\n",
803 (long long) ctx->offset, ctx->qiov.size);
805 free(cmp_buf);
808 if (ctx->qflag) {
809 goto out;
812 if (ctx->vflag) {
813 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
816 /* Finally, report back -- -C gives a parsable format */
817 t2 = tsub(t2, ctx->t1);
818 print_report("read", &t2, ctx->offset, ctx->qiov.size,
819 ctx->qiov.size, 1, ctx->Cflag);
820 out:
821 qemu_io_free(ctx->buf);
822 free(ctx);
825 static void
826 aio_read_help(void)
828 printf(
829 "\n"
830 " asynchronously reads a range of bytes from the given offset\n"
831 "\n"
832 " Example:\n"
833 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
834 "\n"
835 " Reads a segment of the currently open file, optionally dumping it to the\n"
836 " standard output stream (with -v option) for subsequent inspection.\n"
837 " The read is performed asynchronously and should the aio_flush command \n"
838 " should be used to ensure all outstanding aio requests have been completed\n"
839 " -C, -- report statistics in a machine parsable format\n"
840 " -P, -- use a pattern to verify read data\n"
841 " -v, -- dump buffer to standard output\n"
842 " -q, -- quite mode, do not show I/O statistics\n"
843 "\n");
846 static int
847 aio_read_f(int argc, char **argv)
849 int nr_iov, c;
850 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
851 BlockDriverAIOCB *acb;
853 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
854 switch (c) {
855 case 'C':
856 ctx->Cflag = 1;
857 break;
858 case 'P':
859 ctx->Pflag = 1;
860 ctx->pattern = atoi(optarg);
861 break;
862 case 'q':
863 ctx->qflag = 1;
864 break;
865 case 'v':
866 ctx->vflag = 1;
867 break;
868 default:
869 free(ctx);
870 return command_usage(&aio_read_cmd);
874 if (optind > argc - 2) {
875 free(ctx);
876 return command_usage(&aio_read_cmd);
879 ctx->offset = cvtnum(argv[optind]);
880 if (ctx->offset < 0) {
881 printf("non-numeric length argument -- %s\n", argv[optind]);
882 free(ctx);
883 return 0;
885 optind++;
887 if (ctx->offset & 0x1ff) {
888 printf("offset %lld is not sector aligned\n",
889 (long long)ctx->offset);
890 free(ctx);
891 return 0;
894 nr_iov = argc - optind;
895 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
897 gettimeofday(&ctx->t1, NULL);
898 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
899 ctx->qiov.size >> 9, aio_read_done, ctx);
900 if (!acb) {
901 free(ctx->buf);
902 free(ctx);
903 return -EIO;
906 return 0;
909 static const cmdinfo_t aio_read_cmd = {
910 .name = "aio_read",
911 .cfunc = aio_read_f,
912 .argmin = 2,
913 .argmax = -1,
914 .args = "[-Cqv] [-P pattern ] off len [len..]",
915 .oneline = "asynchronously reads a number of bytes",
916 .help = aio_read_help,
919 static const cmdinfo_t aio_write_cmd;
921 static void
922 aio_write_help(void)
924 printf(
925 "\n"
926 " asynchronously writes a range of bytes from the given offset source \n"
927 " from multiple buffers\n"
928 "\n"
929 " Example:\n"
930 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
931 "\n"
932 " Writes into a segment of the currently open file, using a buffer\n"
933 " filled with a set pattern (0xcdcdcdcd).\n"
934 " The write is performed asynchronously and should the aio_flush command \n"
935 " should be used to ensure all outstanding aio requests have been completed\n"
936 " -P, -- use different pattern to fill file\n"
937 " -C, -- report statistics in a machine parsable format\n"
938 " -q, -- quite mode, do not show I/O statistics\n"
939 "\n");
943 static int
944 aio_write_f(int argc, char **argv)
946 int nr_iov, c;
947 int pattern = 0xcd;
948 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
949 BlockDriverAIOCB *acb;
951 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
952 switch (c) {
953 case 'C':
954 ctx->Cflag = 1;
955 break;
956 case 'q':
957 ctx->qflag = 1;
958 break;
959 case 'P':
960 pattern = atoi(optarg);
961 break;
962 default:
963 free(ctx);
964 return command_usage(&aio_write_cmd);
968 if (optind > argc - 2) {
969 free(ctx);
970 return command_usage(&aio_write_cmd);
973 ctx->offset = cvtnum(argv[optind]);
974 if (ctx->offset < 0) {
975 printf("non-numeric length argument -- %s\n", argv[optind]);
976 free(ctx);
977 return 0;
979 optind++;
981 if (ctx->offset & 0x1ff) {
982 printf("offset %lld is not sector aligned\n",
983 (long long)ctx->offset);
984 free(ctx);
985 return 0;
988 nr_iov = argc - optind;
989 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
991 gettimeofday(&ctx->t1, NULL);
992 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
993 ctx->qiov.size >> 9, aio_write_done, ctx);
994 if (!acb) {
995 free(ctx->buf);
996 free(ctx);
997 return -EIO;
1000 return 0;
1003 static const cmdinfo_t aio_write_cmd = {
1004 .name = "aio_write",
1005 .cfunc = aio_write_f,
1006 .argmin = 2,
1007 .argmax = -1,
1008 .args = "[-Cq] [-P pattern ] off len [len..]",
1009 .oneline = "asynchronously writes a number of bytes",
1010 .help = aio_write_help,
1013 static int
1014 aio_flush_f(int argc, char **argv)
1016 qemu_aio_flush();
1017 return 0;
1020 static const cmdinfo_t aio_flush_cmd = {
1021 .name = "aio_flush",
1022 .cfunc = aio_flush_f,
1023 .oneline = "completes all outstanding aio requets"
1026 static int
1027 flush_f(int argc, char **argv)
1029 bdrv_flush(bs);
1030 return 0;
1033 static const cmdinfo_t flush_cmd = {
1034 .name = "flush",
1035 .altname = "f",
1036 .cfunc = flush_f,
1037 .oneline = "flush all in-core file state to disk",
1040 static int
1041 truncate_f(int argc, char **argv)
1043 int64_t offset;
1044 int ret;
1046 offset = cvtnum(argv[1]);
1047 if (offset < 0) {
1048 printf("non-numeric truncate argument -- %s\n", argv[1]);
1049 return 0;
1052 ret = bdrv_truncate(bs, offset);
1053 if (ret < 0) {
1054 printf("truncate: %s", strerror(ret));
1055 return 0;
1058 return 0;
1061 static const cmdinfo_t truncate_cmd = {
1062 .name = "truncate",
1063 .altname = "t",
1064 .cfunc = truncate_f,
1065 .argmin = 1,
1066 .argmax = 1,
1067 .args = "off",
1068 .oneline = "truncates the current file at the given offset",
1071 static int
1072 length_f(int argc, char **argv)
1074 int64_t size;
1075 char s1[64];
1077 size = bdrv_getlength(bs);
1078 if (size < 0) {
1079 printf("getlength: %s", strerror(size));
1080 return 0;
1083 cvtstr(size, s1, sizeof(s1));
1084 printf("%s\n", s1);
1085 return 0;
1089 static const cmdinfo_t length_cmd = {
1090 .name = "length",
1091 .altname = "l",
1092 .cfunc = length_f,
1093 .oneline = "gets the length of the current file",
1097 static int
1098 info_f(int argc, char **argv)
1100 BlockDriverInfo bdi;
1101 char s1[64], s2[64];
1102 int ret;
1104 if (bs->drv && bs->drv->format_name)
1105 printf("format name: %s\n", bs->drv->format_name);
1106 if (bs->drv && bs->drv->protocol_name)
1107 printf("format name: %s\n", bs->drv->protocol_name);
1109 ret = bdrv_get_info(bs, &bdi);
1110 if (ret)
1111 return 0;
1113 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1114 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1116 printf("cluster size: %s\n", s1);
1117 printf("vm state offset: %s\n", s2);
1119 return 0;
1124 static const cmdinfo_t info_cmd = {
1125 .name = "info",
1126 .altname = "i",
1127 .cfunc = info_f,
1128 .oneline = "prints information about the current file",
1131 static int
1132 alloc_f(int argc, char **argv)
1134 int64_t offset;
1135 int nb_sectors;
1136 char s1[64];
1137 int num;
1138 int ret;
1139 const char *retstr;
1141 offset = cvtnum(argv[1]);
1142 if (offset & 0x1ff) {
1143 printf("offset %lld is not sector aligned\n",
1144 (long long)offset);
1145 return 0;
1148 if (argc == 3)
1149 nb_sectors = cvtnum(argv[2]);
1150 else
1151 nb_sectors = 1;
1153 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1155 cvtstr(offset, s1, sizeof(s1));
1157 retstr = ret ? "allocated" : "not allocated";
1158 if (nb_sectors == 1)
1159 printf("sector %s at offset %s\n", retstr, s1);
1160 else
1161 printf("%d/%d sectors %s at offset %s\n",
1162 num, nb_sectors, retstr, s1);
1163 return 0;
1166 static const cmdinfo_t alloc_cmd = {
1167 .name = "alloc",
1168 .altname = "a",
1169 .argmin = 1,
1170 .argmax = 2,
1171 .cfunc = alloc_f,
1172 .args = "off [sectors]",
1173 .oneline = "checks if a sector is present in the file",
1176 static int
1177 close_f(int argc, char **argv)
1179 bdrv_close(bs);
1180 bs = NULL;
1181 return 0;
1184 static const cmdinfo_t close_cmd = {
1185 .name = "close",
1186 .altname = "c",
1187 .cfunc = close_f,
1188 .oneline = "close the current open file",
1191 static int openfile(char *name, int flags, int growable)
1193 if (bs) {
1194 fprintf(stderr, "file open already, try 'help close'\n");
1195 return 1;
1198 bs = bdrv_new("hda");
1199 if (!bs)
1200 return 1;
1202 if (bdrv_open(bs, name, flags) == -1) {
1203 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1204 bs = NULL;
1205 return 1;
1209 if (growable) {
1210 if (!bs->drv || !bs->drv->protocol_name) {
1211 fprintf(stderr,
1212 "%s: only protocols can be opened growable\n",
1213 progname);
1214 return 1;
1216 bs->growable = 1;
1219 return 0;
1222 static void
1223 open_help(void)
1225 printf(
1226 "\n"
1227 " opens a new file in the requested mode\n"
1228 "\n"
1229 " Example:\n"
1230 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1231 "\n"
1232 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1233 " -C, -- create new file if it doesn't exist\n"
1234 " -r, -- open file read-only\n"
1235 " -s, -- use snapshot file\n"
1236 " -n, -- disable host cache\n"
1237 " -g, -- allow file to grow (only applies to protocols)"
1238 "\n");
1241 static const cmdinfo_t open_cmd;
1243 static int
1244 open_f(int argc, char **argv)
1246 int flags = 0;
1247 int readonly = 0;
1248 int growable = 0;
1249 int c;
1251 while ((c = getopt(argc, argv, "snCrg")) != EOF) {
1252 switch (c) {
1253 case 's':
1254 flags |= BDRV_O_SNAPSHOT;
1255 break;
1256 case 'n':
1257 flags |= BDRV_O_NOCACHE;
1258 break;
1259 case 'C':
1260 flags |= BDRV_O_CREAT;
1261 break;
1262 case 'r':
1263 readonly = 1;
1264 break;
1265 case 'g':
1266 growable = 1;
1267 break;
1268 default:
1269 return command_usage(&open_cmd);
1273 if (readonly)
1274 flags |= BDRV_O_RDONLY;
1275 else
1276 flags |= BDRV_O_RDWR;
1278 if (optind != argc - 1)
1279 return command_usage(&open_cmd);
1281 return openfile(argv[optind], flags, growable);
1284 static const cmdinfo_t open_cmd = {
1285 .name = "open",
1286 .altname = "o",
1287 .cfunc = open_f,
1288 .argmin = 1,
1289 .argmax = -1,
1290 .flags = CMD_NOFILE_OK,
1291 .args = "[-Crsn] [path]",
1292 .oneline = "open the file specified by path",
1293 .help = open_help,
1296 static int
1297 init_args_command(
1298 int index)
1300 /* only one device allowed so far */
1301 if (index >= 1)
1302 return 0;
1303 return ++index;
1306 static int
1307 init_check_command(
1308 const cmdinfo_t *ct)
1310 if (ct->flags & CMD_FLAG_GLOBAL)
1311 return 1;
1312 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1313 fprintf(stderr, "no file open, try 'help open'\n");
1314 return 0;
1316 return 1;
1319 static void usage(const char *name)
1321 printf(
1322 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1323 "QEMU Disk exerciser\n"
1324 "\n"
1325 " -C, --create create new file if it doesn't exist\n"
1326 " -c, --cmd command to execute\n"
1327 " -r, --read-only export read-only\n"
1328 " -s, --snapshot use snapshot file\n"
1329 " -n, --nocache disable host cache\n"
1330 " -m, --misalign misalign allocations for O_DIRECT\n"
1331 " -h, --help display this help and exit\n"
1332 " -V, --version output version information and exit\n"
1333 "\n",
1334 name);
1338 int main(int argc, char **argv)
1340 int readonly = 0;
1341 int growable = 0;
1342 const char *sopt = "hVc:Crsnmg";
1343 struct option lopt[] = {
1344 { "help", 0, 0, 'h' },
1345 { "version", 0, 0, 'V' },
1346 { "offset", 1, 0, 'o' },
1347 { "cmd", 1, 0, 'c' },
1348 { "create", 0, 0, 'C' },
1349 { "read-only", 0, 0, 'r' },
1350 { "snapshot", 0, 0, 's' },
1351 { "nocache", 0, 0, 'n' },
1352 { "misalign", 0, 0, 'm' },
1353 { "growable", 0, 0, 'g' },
1354 { NULL, 0, 0, 0 }
1356 int c;
1357 int opt_index = 0;
1358 int flags = 0;
1360 progname = basename(argv[0]);
1362 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1363 switch (c) {
1364 case 's':
1365 flags |= BDRV_O_SNAPSHOT;
1366 break;
1367 case 'n':
1368 flags |= BDRV_O_NOCACHE;
1369 break;
1370 case 'c':
1371 add_user_command(optarg);
1372 break;
1373 case 'C':
1374 flags |= BDRV_O_CREAT;
1375 break;
1376 case 'r':
1377 readonly = 1;
1378 break;
1379 case 'm':
1380 misalign = 1;
1381 break;
1382 case 'g':
1383 growable = 1;
1384 break;
1385 case 'V':
1386 printf("%s version %s\n", progname, VERSION);
1387 exit(0);
1388 case 'h':
1389 usage(progname);
1390 exit(0);
1391 default:
1392 usage(progname);
1393 exit(1);
1397 if ((argc - optind) > 1) {
1398 usage(progname);
1399 exit(1);
1402 bdrv_init();
1404 /* initialize commands */
1405 quit_init();
1406 help_init();
1407 add_command(&open_cmd);
1408 add_command(&close_cmd);
1409 add_command(&read_cmd);
1410 add_command(&readv_cmd);
1411 add_command(&write_cmd);
1412 add_command(&writev_cmd);
1413 add_command(&aio_read_cmd);
1414 add_command(&aio_write_cmd);
1415 add_command(&aio_flush_cmd);
1416 add_command(&flush_cmd);
1417 add_command(&truncate_cmd);
1418 add_command(&length_cmd);
1419 add_command(&info_cmd);
1420 add_command(&alloc_cmd);
1422 add_args_command(init_args_command);
1423 add_check_command(init_check_command);
1425 /* open the device */
1426 if (readonly)
1427 flags |= BDRV_O_RDONLY;
1428 else
1429 flags |= BDRV_O_RDWR;
1431 if ((argc - optind) == 1)
1432 openfile(argv[optind], flags, growable);
1433 command_loop();
1436 * Make sure all outstanding requests get flushed the program exits.
1438 qemu_aio_flush();
1440 if (bs)
1441 bdrv_close(bs);
1442 return 0;