]> git.proxmox.com Git - qemu.git/blob - qemu-io.c
monitor: Make pci_add device options truely optional
[qemu.git] / qemu-io.c
1 /*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
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>
14
15 #include "qemu-common.h"
16 #include "block_int.h"
17 #include "cmd.h"
18
19 #define VERSION "0.0.1"
20
21 #define CMD_NOFILE_OK 0x01
22
23 char *progname;
24 static BlockDriverState *bs;
25
26 static int misalign;
27
28 /*
29 * Memory allocation helpers.
30 *
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
33 */
34
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len, int pattern)
37 {
38 void *buf;
39
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;
47 }
48
49 static void qemu_io_free(void *p)
50 {
51 if (misalign)
52 p -= MISALIGN_OFFSET;
53 qemu_vfree(p);
54 }
55
56 static void
57 dump_buffer(const void *buffer, int64_t offset, int len)
58 {
59 int i, j;
60 const uint8_t *p;
61
62 for (i = 0, p = buffer; i < len; i += 16) {
63 const uint8_t *s = p;
64
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(".");
74 }
75 printf("\n");
76 }
77 }
78
79 static void
80 print_report(const char *op, struct timeval *t, int64_t offset,
81 int count, int total, int cnt, int Cflag)
82 {
83 char s1[64], s2[64], ts[64];
84
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));
98 }
99 }
100
101 static int do_read(char *buf, int64_t offset, int count, int *total)
102 {
103 int ret;
104
105 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
106 if (ret < 0)
107 return ret;
108 *total = count;
109 return 1;
110 }
111
112 static int do_write(char *buf, int64_t offset, int count, int *total)
113 {
114 int ret;
115
116 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
117 if (ret < 0)
118 return ret;
119 *total = count;
120 return 1;
121 }
122
123 static int do_pread(char *buf, int64_t offset, int count, int *total)
124 {
125 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
126 if (*total < 0)
127 return *total;
128 return 1;
129 }
130
131 static int do_pwrite(char *buf, int64_t offset, int count, int *total)
132 {
133 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
134 if (*total < 0)
135 return *total;
136 return 1;
137 }
138
139 #define NOT_DONE 0x7fffffff
140 static void aio_rw_done(void *opaque, int ret)
141 {
142 *(int *)opaque = ret;
143 }
144
145 static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
146 {
147 BlockDriverAIOCB *acb;
148 int async_ret = NOT_DONE;
149
150 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
151 aio_rw_done, &async_ret);
152 if (!acb)
153 return -EIO;
154
155 while (async_ret == NOT_DONE)
156 qemu_aio_wait();
157
158 *total = qiov->size;
159 return async_ret < 0 ? async_ret : 1;
160 }
161
162 static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
163 {
164 BlockDriverAIOCB *acb;
165 int async_ret = NOT_DONE;
166
167 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
168 aio_rw_done, &async_ret);
169 if (!acb)
170 return -EIO;
171
172 while (async_ret == NOT_DONE)
173 qemu_aio_wait();
174
175 *total = qiov->size;
176 return async_ret < 0 ? async_ret : 1;
177 }
178
179
180 static const cmdinfo_t read_cmd;
181
182 static void
183 read_help(void)
184 {
185 printf(
186 "\n"
187 " reads a range of bytes from the given offset\n"
188 "\n"
189 " Example:\n"
190 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
191 "\n"
192 " Reads a segment of the currently open file, optionally dumping it to the\n"
193 " standard output stream (with -v option) for subsequent inspection.\n"
194 " -C, -- report statistics in a machine parsable format\n"
195 " -l, -- length for pattern verification (only with -P)\n"
196 " -p, -- use bdrv_pread to read the file\n"
197 " -P, -- use a pattern to verify read data\n"
198 " -q, -- quite mode, do not show I/O statistics\n"
199 " -s, -- start offset for pattern verification (only with -P)\n"
200 " -v, -- dump buffer to standard output\n"
201 "\n");
202 }
203
204 static int
205 read_f(int argc, char **argv)
206 {
207 struct timeval t1, t2;
208 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
209 int Pflag = 0, sflag = 0, lflag = 0;
210 int c, cnt;
211 char *buf;
212 int64_t offset;
213 int count;
214 /* Some compilers get confused and warn if this is not initialized. */
215 int total = 0;
216 int pattern = 0, pattern_offset = 0, pattern_count = 0;
217
218 while ((c = getopt(argc, argv, "Cl:pP:qs:v")) != EOF) {
219 switch (c) {
220 case 'C':
221 Cflag = 1;
222 break;
223 case 'l':
224 lflag = 1;
225 pattern_count = cvtnum(optarg);
226 if (pattern_count < 0) {
227 printf("non-numeric length argument -- %s\n", optarg);
228 return 0;
229 }
230 break;
231 case 'p':
232 pflag = 1;
233 break;
234 case 'P':
235 Pflag = 1;
236 pattern = atoi(optarg);
237 break;
238 case 'q':
239 qflag = 1;
240 break;
241 case 's':
242 sflag = 1;
243 pattern_offset = cvtnum(optarg);
244 if (pattern_offset < 0) {
245 printf("non-numeric length argument -- %s\n", optarg);
246 return 0;
247 }
248 break;
249 case 'v':
250 vflag = 1;
251 break;
252 default:
253 return command_usage(&read_cmd);
254 }
255 }
256
257 if (optind != argc - 2)
258 return command_usage(&read_cmd);
259
260 offset = cvtnum(argv[optind]);
261 if (offset < 0) {
262 printf("non-numeric length argument -- %s\n", argv[optind]);
263 return 0;
264 }
265
266 optind++;
267 count = cvtnum(argv[optind]);
268 if (count < 0) {
269 printf("non-numeric length argument -- %s\n", argv[optind]);
270 return 0;
271 }
272
273 if (!Pflag && (lflag || sflag)) {
274 return command_usage(&read_cmd);
275 }
276
277 if (!lflag) {
278 pattern_count = count - pattern_offset;
279 }
280
281 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
282 printf("pattern verfication range exceeds end of read data\n");
283 return 0;
284 }
285
286 if (!pflag)
287 if (offset & 0x1ff) {
288 printf("offset %lld is not sector aligned\n",
289 (long long)offset);
290 return 0;
291
292 if (count & 0x1ff) {
293 printf("count %d is not sector aligned\n",
294 count);
295 return 0;
296 }
297 }
298
299 buf = qemu_io_alloc(count, 0xab);
300
301 gettimeofday(&t1, NULL);
302 if (pflag)
303 cnt = do_pread(buf, offset, count, &total);
304 else
305 cnt = do_read(buf, offset, count, &total);
306 gettimeofday(&t2, NULL);
307
308 if (cnt < 0) {
309 printf("read failed: %s\n", strerror(-cnt));
310 return 0;
311 }
312
313 if (Pflag) {
314 void* cmp_buf = malloc(pattern_count);
315 memset(cmp_buf, pattern, pattern_count);
316 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
317 printf("Pattern verification failed at offset %lld, "
318 "%d bytes\n",
319 (long long) offset + pattern_offset, pattern_count);
320 }
321 free(cmp_buf);
322 }
323
324 if (qflag)
325 return 0;
326
327 if (vflag)
328 dump_buffer(buf, offset, count);
329
330 /* Finally, report back -- -C gives a parsable format */
331 t2 = tsub(t2, t1);
332 print_report("read", &t2, offset, count, total, cnt, Cflag);
333
334 qemu_io_free(buf);
335
336 return 0;
337 }
338
339 static const cmdinfo_t read_cmd = {
340 .name = "read",
341 .altname = "r",
342 .cfunc = read_f,
343 .argmin = 2,
344 .argmax = -1,
345 .args = "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
346 .oneline = "reads a number of bytes at a specified offset",
347 .help = read_help,
348 };
349
350 static const cmdinfo_t readv_cmd;
351
352 static void
353 readv_help(void)
354 {
355 printf(
356 "\n"
357 " reads a range of bytes from the given offset into multiple buffers\n"
358 "\n"
359 " Example:\n"
360 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
361 "\n"
362 " Reads a segment of the currently open file, optionally dumping it to the\n"
363 " standard output stream (with -v option) for subsequent inspection.\n"
364 " Uses multiple iovec buffers if more than one byte range is specified.\n"
365 " -C, -- report statistics in a machine parsable format\n"
366 " -P, -- use a pattern to verify read data\n"
367 " -v, -- dump buffer to standard output\n"
368 " -q, -- quite mode, do not show I/O statistics\n"
369 "\n");
370 }
371
372 static int
373 readv_f(int argc, char **argv)
374 {
375 struct timeval t1, t2;
376 int Cflag = 0, qflag = 0, vflag = 0;
377 int c, cnt;
378 char *buf, *p;
379 int64_t offset;
380 int count = 0, total;
381 int nr_iov, i;
382 QEMUIOVector qiov;
383 int pattern = 0;
384 int Pflag = 0;
385
386 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
387 switch (c) {
388 case 'C':
389 Cflag = 1;
390 break;
391 case 'P':
392 Pflag = 1;
393 pattern = atoi(optarg);
394 break;
395 case 'q':
396 qflag = 1;
397 break;
398 case 'v':
399 vflag = 1;
400 break;
401 default:
402 return command_usage(&readv_cmd);
403 }
404 }
405
406 if (optind > argc - 2)
407 return command_usage(&readv_cmd);
408
409
410 offset = cvtnum(argv[optind]);
411 if (offset < 0) {
412 printf("non-numeric length argument -- %s\n", argv[optind]);
413 return 0;
414 }
415 optind++;
416
417 if (offset & 0x1ff) {
418 printf("offset %lld is not sector aligned\n",
419 (long long)offset);
420 return 0;
421 }
422
423 if (count & 0x1ff) {
424 printf("count %d is not sector aligned\n",
425 count);
426 return 0;
427 }
428
429 for (i = optind; i < argc; i++) {
430 size_t len;
431
432 len = cvtnum(argv[i]);
433 if (len < 0) {
434 printf("non-numeric length argument -- %s\n", argv[i]);
435 return 0;
436 }
437 count += len;
438 }
439
440 nr_iov = argc - optind;
441 qemu_iovec_init(&qiov, nr_iov);
442 buf = p = qemu_io_alloc(count, 0xab);
443 for (i = 0; i < nr_iov; i++) {
444 size_t len;
445
446 len = cvtnum(argv[optind]);
447 if (len < 0) {
448 printf("non-numeric length argument -- %s\n",
449 argv[optind]);
450 return 0;
451 }
452
453 qemu_iovec_add(&qiov, p, len);
454 p += len;
455 optind++;
456 }
457
458 gettimeofday(&t1, NULL);
459 cnt = do_aio_readv(&qiov, offset, &total);
460 gettimeofday(&t2, NULL);
461
462 if (cnt < 0) {
463 printf("readv failed: %s\n", strerror(-cnt));
464 return 0;
465 }
466
467 if (Pflag) {
468 void* cmp_buf = malloc(count);
469 memset(cmp_buf, pattern, count);
470 if (memcmp(buf, cmp_buf, count)) {
471 printf("Pattern verification failed at offset %lld, "
472 "%d bytes\n",
473 (long long) offset, count);
474 }
475 free(cmp_buf);
476 }
477
478 if (qflag)
479 return 0;
480
481 if (vflag)
482 dump_buffer(buf, offset, qiov.size);
483
484 /* Finally, report back -- -C gives a parsable format */
485 t2 = tsub(t2, t1);
486 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
487
488 qemu_io_free(buf);
489
490 return 0;
491 }
492
493 static const cmdinfo_t readv_cmd = {
494 .name = "readv",
495 .cfunc = readv_f,
496 .argmin = 2,
497 .argmax = -1,
498 .args = "[-Cqv] [-P pattern ] off len [len..]",
499 .oneline = "reads a number of bytes at a specified offset",
500 .help = readv_help,
501 };
502
503 static const cmdinfo_t write_cmd;
504
505 static void
506 write_help(void)
507 {
508 printf(
509 "\n"
510 " writes a range of bytes from the given offset\n"
511 "\n"
512 " Example:\n"
513 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
514 "\n"
515 " Writes into a segment of the currently open file, using a buffer\n"
516 " filled with a set pattern (0xcdcdcdcd).\n"
517 " -p, -- use bdrv_pwrite to write the file\n"
518 " -P, -- use different pattern to fill file\n"
519 " -C, -- report statistics in a machine parsable format\n"
520 " -q, -- quite mode, do not show I/O statistics\n"
521 "\n");
522 }
523
524 static int
525 write_f(int argc, char **argv)
526 {
527 struct timeval t1, t2;
528 int Cflag = 0, pflag = 0, qflag = 0;
529 int c, cnt;
530 char *buf;
531 int64_t offset;
532 int count;
533 /* Some compilers get confused and warn if this is not initialized. */
534 int total = 0;
535 int pattern = 0xcd;
536
537 while ((c = getopt(argc, argv, "CpP:q")) != EOF) {
538 switch (c) {
539 case 'C':
540 Cflag = 1;
541 break;
542 case 'p':
543 pflag = 1;
544 break;
545 case 'P':
546 pattern = atoi(optarg);
547 break;
548 case 'q':
549 qflag = 1;
550 break;
551 default:
552 return command_usage(&write_cmd);
553 }
554 }
555
556 if (optind != argc - 2)
557 return command_usage(&write_cmd);
558
559 offset = cvtnum(argv[optind]);
560 if (offset < 0) {
561 printf("non-numeric length argument -- %s\n", argv[optind]);
562 return 0;
563 }
564
565 optind++;
566 count = cvtnum(argv[optind]);
567 if (count < 0) {
568 printf("non-numeric length argument -- %s\n", argv[optind]);
569 return 0;
570 }
571
572 if (!pflag) {
573 if (offset & 0x1ff) {
574 printf("offset %lld is not sector aligned\n",
575 (long long)offset);
576 return 0;
577 }
578
579 if (count & 0x1ff) {
580 printf("count %d is not sector aligned\n",
581 count);
582 return 0;
583 }
584 }
585
586 buf = qemu_io_alloc(count, pattern);
587
588 gettimeofday(&t1, NULL);
589 if (pflag)
590 cnt = do_pwrite(buf, offset, count, &total);
591 else
592 cnt = do_write(buf, offset, count, &total);
593 gettimeofday(&t2, NULL);
594
595 if (cnt < 0) {
596 printf("write failed: %s\n", strerror(-cnt));
597 return 0;
598 }
599
600 if (qflag)
601 return 0;
602
603 /* Finally, report back -- -C gives a parsable format */
604 t2 = tsub(t2, t1);
605 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
606
607 qemu_io_free(buf);
608
609 return 0;
610 }
611
612 static const cmdinfo_t write_cmd = {
613 .name = "write",
614 .altname = "w",
615 .cfunc = write_f,
616 .argmin = 2,
617 .argmax = -1,
618 .args = "[-aCpq] [-P pattern ] off len",
619 .oneline = "writes a number of bytes at a specified offset",
620 .help = write_help,
621 };
622
623 static const cmdinfo_t writev_cmd;
624
625 static void
626 writev_help(void)
627 {
628 printf(
629 "\n"
630 " writes a range of bytes from the given offset source from multiple buffers\n"
631 "\n"
632 " Example:\n"
633 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
634 "\n"
635 " Writes into a segment of the currently open file, using a buffer\n"
636 " filled with a set pattern (0xcdcdcdcd).\n"
637 " -P, -- use different pattern to fill file\n"
638 " -C, -- report statistics in a machine parsable format\n"
639 " -q, -- quite mode, do not show I/O statistics\n"
640 "\n");
641 }
642
643 static int
644 writev_f(int argc, char **argv)
645 {
646 struct timeval t1, t2;
647 int Cflag = 0, qflag = 0;
648 int c, cnt;
649 char *buf, *p;
650 int64_t offset;
651 int count = 0, total;
652 int nr_iov, i;
653 int pattern = 0xcd;
654 QEMUIOVector qiov;
655
656 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
657 switch (c) {
658 case 'C':
659 Cflag = 1;
660 break;
661 case 'q':
662 qflag = 1;
663 break;
664 case 'P':
665 pattern = atoi(optarg);
666 break;
667 default:
668 return command_usage(&writev_cmd);
669 }
670 }
671
672 if (optind > argc - 2)
673 return command_usage(&writev_cmd);
674
675 offset = cvtnum(argv[optind]);
676 if (offset < 0) {
677 printf("non-numeric length argument -- %s\n", argv[optind]);
678 return 0;
679 }
680 optind++;
681
682 if (offset & 0x1ff) {
683 printf("offset %lld is not sector aligned\n",
684 (long long)offset);
685 return 0;
686 }
687
688 if (count & 0x1ff) {
689 printf("count %d is not sector aligned\n",
690 count);
691 return 0;
692 }
693
694
695 for (i = optind; i < argc; i++) {
696 size_t len;
697
698 len = cvtnum(argv[optind]);
699 if (len < 0) {
700 printf("non-numeric length argument -- %s\n", argv[i]);
701 return 0;
702 }
703 count += len;
704 }
705
706 nr_iov = argc - optind;
707 qemu_iovec_init(&qiov, nr_iov);
708 buf = p = qemu_io_alloc(count, pattern);
709 for (i = 0; i < nr_iov; i++) {
710 size_t len;
711
712 len = cvtnum(argv[optind]);
713 if (len < 0) {
714 printf("non-numeric length argument -- %s\n",
715 argv[optind]);
716 return 0;
717 }
718
719 qemu_iovec_add(&qiov, p, len);
720 p += len;
721 optind++;
722 }
723
724 gettimeofday(&t1, NULL);
725 cnt = do_aio_writev(&qiov, offset, &total);
726 gettimeofday(&t2, NULL);
727
728 if (cnt < 0) {
729 printf("writev failed: %s\n", strerror(-cnt));
730 return 0;
731 }
732
733 if (qflag)
734 return 0;
735
736 /* Finally, report back -- -C gives a parsable format */
737 t2 = tsub(t2, t1);
738 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
739
740 qemu_io_free(buf);
741
742 return 0;
743 }
744
745 static const cmdinfo_t writev_cmd = {
746 .name = "writev",
747 .cfunc = writev_f,
748 .argmin = 2,
749 .argmax = -1,
750 .args = "[-Cq] [-P pattern ] off len [len..]",
751 .oneline = "writes a number of bytes at a specified offset",
752 .help = writev_help,
753 };
754
755 struct aio_ctx {
756 QEMUIOVector qiov;
757 int64_t offset;
758 char *buf;
759 int qflag;
760 int vflag;
761 int Cflag;
762 int Pflag;
763 int pattern;
764 struct timeval t1;
765 };
766
767 static void
768 aio_write_done(void *opaque, int ret)
769 {
770 struct aio_ctx *ctx = opaque;
771 struct timeval t2;
772 int total;
773 int cnt = 1;
774
775 gettimeofday(&t2, NULL);
776
777 total = ctx->qiov.size;
778
779 if (ret < 0) {
780 printf("aio_write failed: %s\n", strerror(-ret));
781 return;
782 }
783
784 if (ctx->qflag)
785 return;
786
787 /* Finally, report back -- -C gives a parsable format */
788 t2 = tsub(t2, ctx->t1);
789 print_report("wrote", &t2, ctx->offset, ctx->qiov.size, total, cnt,
790 ctx->Cflag);
791
792 qemu_io_free(ctx->buf);
793 free(ctx);
794 }
795
796 static const cmdinfo_t aio_read_cmd;
797
798 static void
799 aio_read_done(void *opaque, int ret)
800 {
801 struct aio_ctx *ctx = opaque;
802 struct timeval t2;
803 int total;
804 int cnt = 1;
805
806 gettimeofday(&t2, NULL);
807
808 total = ctx->qiov.size;
809
810 if (ret < 0) {
811 printf("readv failed: %s\n", strerror(-ret));
812 return;
813 }
814
815 if (ctx->Pflag) {
816 void *cmp_buf = malloc(total);
817
818 memset(cmp_buf, ctx->pattern, total);
819 if (memcmp(ctx->buf, cmp_buf, total)) {
820 printf("Pattern verification failed at offset %lld, "
821 "%d bytes\n",
822 (long long) ctx->offset, total);
823 }
824 free(cmp_buf);
825 }
826
827 if (ctx->qflag)
828 return;
829
830 if (ctx->vflag)
831 dump_buffer(ctx->buf, ctx->offset, total);
832
833 /* Finally, report back -- -C gives a parsable format */
834 t2 = tsub(t2, ctx->t1);
835 print_report("read", &t2, ctx->offset, ctx->qiov.size, total, cnt,
836 ctx->Cflag);
837
838 qemu_io_free(ctx->buf);
839 free(ctx);
840
841 }
842
843 static void
844 aio_read_help(void)
845 {
846 printf(
847 "\n"
848 " asynchronously reads a range of bytes from the given offset\n"
849 "\n"
850 " Example:\n"
851 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
852 "\n"
853 " Reads a segment of the currently open file, optionally dumping it to the\n"
854 " standard output stream (with -v option) for subsequent inspection.\n"
855 " The read is performed asynchronously and should the aio_flush command \n"
856 " should be used to ensure all outstanding aio requests have been completed\n"
857 " -C, -- report statistics in a machine parsable format\n"
858 " -P, -- use a pattern to verify read data\n"
859 " -v, -- dump buffer to standard output\n"
860 " -q, -- quite mode, do not show I/O statistics\n"
861 "\n");
862 }
863
864 static int
865 aio_read_f(int argc, char **argv)
866 {
867 char *p;
868 int count = 0;
869 int nr_iov, i, c;
870 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
871 BlockDriverAIOCB *acb;
872
873 ctx->pattern = 0xcd;
874
875 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
876 switch (c) {
877 case 'C':
878 ctx->Cflag = 1;
879 break;
880 case 'P':
881 ctx->Pflag = 1;
882 ctx->pattern = atoi(optarg);
883 break;
884 case 'q':
885 ctx->qflag = 1;
886 break;
887 case 'v':
888 ctx->vflag = 1;
889 break;
890 default:
891 return command_usage(&aio_read_cmd);
892 }
893 }
894
895 if (optind > argc - 2)
896 return command_usage(&aio_read_cmd);
897
898
899 ctx->offset = cvtnum(argv[optind]);
900 if (ctx->offset < 0) {
901 printf("non-numeric length argument -- %s\n", argv[optind]);
902 return 0;
903 }
904 optind++;
905
906 if (ctx->offset & 0x1ff) {
907 printf("offset %lld is not sector aligned\n",
908 (long long)ctx->offset);
909 return 0;
910 }
911
912 if (count & 0x1ff) {
913 printf("count %d is not sector aligned\n",
914 count);
915 return 0;
916 }
917
918 for (i = optind; i < argc; i++) {
919 size_t len;
920
921 len = cvtnum(argv[i]);
922 if (len < 0) {
923 printf("non-numeric length argument -- %s\n", argv[i]);
924 return 0;
925 }
926 count += len;
927 }
928
929 nr_iov = argc - optind;
930 qemu_iovec_init(&ctx->qiov, nr_iov);
931 ctx->buf = p = qemu_io_alloc(count, 0xab);
932 for (i = 0; i < nr_iov; i++) {
933 size_t len;
934
935 len = cvtnum(argv[optind]);
936 if (len < 0) {
937 printf("non-numeric length argument -- %s\n",
938 argv[optind]);
939 return 0;
940 }
941
942 qemu_iovec_add(&ctx->qiov, p, len);
943 p += len;
944 optind++;
945 }
946
947 gettimeofday(&ctx->t1, NULL);
948 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
949 ctx->qiov.size >> 9, aio_read_done, ctx);
950 if (!acb)
951 return -EIO;
952
953 return 0;
954 }
955
956 static const cmdinfo_t aio_read_cmd = {
957 .name = "aio_read",
958 .cfunc = aio_read_f,
959 .argmin = 2,
960 .argmax = -1,
961 .args = "[-Cqv] [-P pattern ] off len [len..]",
962 .oneline = "asynchronously reads a number of bytes",
963 .help = aio_read_help,
964 };
965
966 static const cmdinfo_t aio_write_cmd;
967
968 static void
969 aio_write_help(void)
970 {
971 printf(
972 "\n"
973 " asynchronously writes a range of bytes from the given offset source \n"
974 " from multiple buffers\n"
975 "\n"
976 " Example:\n"
977 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
978 "\n"
979 " Writes into a segment of the currently open file, using a buffer\n"
980 " filled with a set pattern (0xcdcdcdcd).\n"
981 " The write is performed asynchronously and should the aio_flush command \n"
982 " should be used to ensure all outstanding aio requests have been completed\n"
983 " -P, -- use different pattern to fill file\n"
984 " -C, -- report statistics in a machine parsable format\n"
985 " -q, -- quite mode, do not show I/O statistics\n"
986 "\n");
987 }
988
989
990 static int
991 aio_write_f(int argc, char **argv)
992 {
993 char *p;
994 int count = 0;
995 int nr_iov, i, c;
996 int pattern = 0xcd;
997 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
998 BlockDriverAIOCB *acb;
999
1000 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1001 switch (c) {
1002 case 'C':
1003 ctx->Cflag = 1;
1004 break;
1005 case 'q':
1006 ctx->qflag = 1;
1007 break;
1008 case 'P':
1009 pattern = atoi(optarg);
1010 break;
1011 default:
1012 return command_usage(&aio_write_cmd);
1013 }
1014 }
1015
1016 if (optind > argc - 2)
1017 return command_usage(&aio_write_cmd);
1018
1019 ctx->offset = cvtnum(argv[optind]);
1020 if (ctx->offset < 0) {
1021 printf("non-numeric length argument -- %s\n", argv[optind]);
1022 return 0;
1023 }
1024 optind++;
1025
1026 if (ctx->offset & 0x1ff) {
1027 printf("offset %lld is not sector aligned\n",
1028 (long long)ctx->offset);
1029 return 0;
1030 }
1031
1032 if (count & 0x1ff) {
1033 printf("count %d is not sector aligned\n",
1034 count);
1035 return 0;
1036 }
1037
1038
1039 for (i = optind; i < argc; i++) {
1040 size_t len;
1041
1042 len = cvtnum(argv[optind]);
1043 if (len < 0) {
1044 printf("non-numeric length argument -- %s\n", argv[i]);
1045 return 0;
1046 }
1047 count += len;
1048 }
1049
1050 nr_iov = argc - optind;
1051 qemu_iovec_init(&ctx->qiov, nr_iov);
1052 ctx->buf = p = qemu_io_alloc(count, pattern);
1053 for (i = 0; i < nr_iov; i++) {
1054 size_t len;
1055
1056 len = cvtnum(argv[optind]);
1057 if (len < 0) {
1058 printf("non-numeric length argument -- %s\n",
1059 argv[optind]);
1060 return 0;
1061 }
1062
1063 qemu_iovec_add(&ctx->qiov, p, len);
1064 p += len;
1065 optind++;
1066 }
1067
1068 gettimeofday(&ctx->t1, NULL);
1069 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1070 ctx->qiov.size >> 9, aio_write_done, ctx);
1071 if (!acb)
1072 return -EIO;
1073
1074 return 0;
1075 }
1076
1077 static const cmdinfo_t aio_write_cmd = {
1078 .name = "aio_write",
1079 .cfunc = aio_write_f,
1080 .argmin = 2,
1081 .argmax = -1,
1082 .args = "[-Cq] [-P pattern ] off len [len..]",
1083 .oneline = "asynchronously writes a number of bytes",
1084 .help = aio_write_help,
1085 };
1086
1087 static int
1088 aio_flush_f(int argc, char **argv)
1089 {
1090 qemu_aio_flush();
1091 return 0;
1092 }
1093
1094 static const cmdinfo_t aio_flush_cmd = {
1095 .name = "aio_flush",
1096 .cfunc = aio_flush_f,
1097 .oneline = "completes all outstanding aio requets"
1098 };
1099
1100 static int
1101 flush_f(int argc, char **argv)
1102 {
1103 bdrv_flush(bs);
1104 return 0;
1105 }
1106
1107 static const cmdinfo_t flush_cmd = {
1108 .name = "flush",
1109 .altname = "f",
1110 .cfunc = flush_f,
1111 .oneline = "flush all in-core file state to disk",
1112 };
1113
1114 static int
1115 truncate_f(int argc, char **argv)
1116 {
1117 int64_t offset;
1118 int ret;
1119
1120 offset = cvtnum(argv[1]);
1121 if (offset < 0) {
1122 printf("non-numeric truncate argument -- %s\n", argv[1]);
1123 return 0;
1124 }
1125
1126 ret = bdrv_truncate(bs, offset);
1127 if (ret < 0) {
1128 printf("truncate: %s", strerror(ret));
1129 return 0;
1130 }
1131
1132 return 0;
1133 }
1134
1135 static const cmdinfo_t truncate_cmd = {
1136 .name = "truncate",
1137 .altname = "t",
1138 .cfunc = truncate_f,
1139 .argmin = 1,
1140 .argmax = 1,
1141 .args = "off",
1142 .oneline = "truncates the current file at the given offset",
1143 };
1144
1145 static int
1146 length_f(int argc, char **argv)
1147 {
1148 int64_t size;
1149 char s1[64];
1150
1151 size = bdrv_getlength(bs);
1152 if (size < 0) {
1153 printf("getlength: %s", strerror(size));
1154 return 0;
1155 }
1156
1157 cvtstr(size, s1, sizeof(s1));
1158 printf("%s\n", s1);
1159 return 0;
1160 }
1161
1162
1163 static const cmdinfo_t length_cmd = {
1164 .name = "length",
1165 .altname = "l",
1166 .cfunc = length_f,
1167 .oneline = "gets the length of the current file",
1168 };
1169
1170
1171 static int
1172 info_f(int argc, char **argv)
1173 {
1174 BlockDriverInfo bdi;
1175 char s1[64], s2[64];
1176 int ret;
1177
1178 if (bs->drv && bs->drv->format_name)
1179 printf("format name: %s\n", bs->drv->format_name);
1180 if (bs->drv && bs->drv->protocol_name)
1181 printf("format name: %s\n", bs->drv->protocol_name);
1182
1183 ret = bdrv_get_info(bs, &bdi);
1184 if (ret)
1185 return 0;
1186
1187 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1188 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1189
1190 printf("cluster size: %s\n", s1);
1191 printf("vm state offset: %s\n", s2);
1192
1193 return 0;
1194 }
1195
1196
1197
1198 static const cmdinfo_t info_cmd = {
1199 .name = "info",
1200 .altname = "i",
1201 .cfunc = info_f,
1202 .oneline = "prints information about the current file",
1203 };
1204
1205 static int
1206 alloc_f(int argc, char **argv)
1207 {
1208 int64_t offset;
1209 int nb_sectors;
1210 char s1[64];
1211 int num;
1212 int ret;
1213 const char *retstr;
1214
1215 offset = cvtnum(argv[1]);
1216 if (offset & 0x1ff) {
1217 printf("offset %lld is not sector aligned\n",
1218 (long long)offset);
1219 return 0;
1220 }
1221
1222 if (argc == 3)
1223 nb_sectors = cvtnum(argv[2]);
1224 else
1225 nb_sectors = 1;
1226
1227 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1228
1229 cvtstr(offset, s1, sizeof(s1));
1230
1231 retstr = ret ? "allocated" : "not allocated";
1232 if (nb_sectors == 1)
1233 printf("sector %s at offset %s\n", retstr, s1);
1234 else
1235 printf("%d/%d sectors %s at offset %s\n",
1236 num, nb_sectors, retstr, s1);
1237 return 0;
1238 }
1239
1240 static const cmdinfo_t alloc_cmd = {
1241 .name = "alloc",
1242 .altname = "a",
1243 .argmin = 1,
1244 .argmax = 2,
1245 .cfunc = alloc_f,
1246 .args = "off [sectors]",
1247 .oneline = "checks if a sector is present in the file",
1248 };
1249
1250 static int
1251 close_f(int argc, char **argv)
1252 {
1253 bdrv_close(bs);
1254 bs = NULL;
1255 return 0;
1256 }
1257
1258 static const cmdinfo_t close_cmd = {
1259 .name = "close",
1260 .altname = "c",
1261 .cfunc = close_f,
1262 .oneline = "close the current open file",
1263 };
1264
1265 static int openfile(char *name, int flags)
1266 {
1267 if (bs) {
1268 fprintf(stderr, "file open already, try 'help close'\n");
1269 return 1;
1270 }
1271
1272 bs = bdrv_new("hda");
1273 if (!bs)
1274 return 1;
1275
1276 if (bdrv_open(bs, name, flags) == -1) {
1277 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1278 bs = NULL;
1279 return 1;
1280 }
1281
1282 return 0;
1283 }
1284
1285 static void
1286 open_help(void)
1287 {
1288 printf(
1289 "\n"
1290 " opens a new file in the requested mode\n"
1291 "\n"
1292 " Example:\n"
1293 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1294 "\n"
1295 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1296 " -C, -- create new file if it doesn't exist\n"
1297 " -r, -- open file read-only\n"
1298 " -s, -- use snapshot file\n"
1299 " -n, -- disable host cache\n"
1300 "\n");
1301 }
1302
1303 static const cmdinfo_t open_cmd;
1304
1305 static int
1306 open_f(int argc, char **argv)
1307 {
1308 int flags = 0;
1309 int readonly = 0;
1310 int c;
1311
1312 while ((c = getopt(argc, argv, "snCr")) != EOF) {
1313 switch (c) {
1314 case 's':
1315 flags |= BDRV_O_SNAPSHOT;
1316 break;
1317 case 'n':
1318 flags |= BDRV_O_NOCACHE;
1319 break;
1320 case 'C':
1321 flags |= BDRV_O_CREAT;
1322 break;
1323 case 'r':
1324 readonly = 1;
1325 break;
1326 default:
1327 return command_usage(&open_cmd);
1328 }
1329 }
1330
1331 if (readonly)
1332 flags |= BDRV_O_RDONLY;
1333 else
1334 flags |= BDRV_O_RDWR;
1335
1336 if (optind != argc - 1)
1337 return command_usage(&open_cmd);
1338
1339 return openfile(argv[optind], flags);
1340 }
1341
1342 static const cmdinfo_t open_cmd = {
1343 .name = "open",
1344 .altname = "o",
1345 .cfunc = open_f,
1346 .argmin = 1,
1347 .argmax = -1,
1348 .flags = CMD_NOFILE_OK,
1349 .args = "[-Crsn] [path]",
1350 .oneline = "open the file specified by path",
1351 .help = open_help,
1352 };
1353
1354 static int
1355 init_args_command(
1356 int index)
1357 {
1358 /* only one device allowed so far */
1359 if (index >= 1)
1360 return 0;
1361 return ++index;
1362 }
1363
1364 static int
1365 init_check_command(
1366 const cmdinfo_t *ct)
1367 {
1368 if (ct->flags & CMD_FLAG_GLOBAL)
1369 return 1;
1370 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1371 fprintf(stderr, "no file open, try 'help open'\n");
1372 return 0;
1373 }
1374 return 1;
1375 }
1376
1377 static void usage(const char *name)
1378 {
1379 printf(
1380 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1381 "QEMU Disk exerciser\n"
1382 "\n"
1383 " -C, --create create new file if it doesn't exist\n"
1384 " -c, --cmd command to execute\n"
1385 " -r, --read-only export read-only\n"
1386 " -s, --snapshot use snapshot file\n"
1387 " -n, --nocache disable host cache\n"
1388 " -m, --misalign misalign allocations for O_DIRECT\n"
1389 " -h, --help display this help and exit\n"
1390 " -V, --version output version information and exit\n"
1391 "\n",
1392 name);
1393 }
1394
1395
1396 int main(int argc, char **argv)
1397 {
1398 int readonly = 0;
1399 const char *sopt = "hVc:Crsnm";
1400 struct option lopt[] = {
1401 { "help", 0, 0, 'h' },
1402 { "version", 0, 0, 'V' },
1403 { "offset", 1, 0, 'o' },
1404 { "cmd", 1, 0, 'c' },
1405 { "create", 0, 0, 'C' },
1406 { "read-only", 0, 0, 'r' },
1407 { "snapshot", 0, 0, 's' },
1408 { "nocache", 0, 0, 'n' },
1409 { "misalign", 0, 0, 'm' },
1410 { NULL, 0, 0, 0 }
1411 };
1412 int c;
1413 int opt_index = 0;
1414 int flags = 0;
1415
1416 progname = basename(argv[0]);
1417
1418 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1419 switch (c) {
1420 case 's':
1421 flags |= BDRV_O_SNAPSHOT;
1422 break;
1423 case 'n':
1424 flags |= BDRV_O_NOCACHE;
1425 break;
1426 case 'c':
1427 add_user_command(optarg);
1428 break;
1429 case 'C':
1430 flags |= BDRV_O_CREAT;
1431 break;
1432 case 'r':
1433 readonly = 1;
1434 break;
1435 case 'm':
1436 misalign = 1;
1437 break;
1438 case 'V':
1439 printf("%s version %s\n", progname, VERSION);
1440 exit(0);
1441 case 'h':
1442 usage(progname);
1443 exit(0);
1444 default:
1445 usage(progname);
1446 exit(1);
1447 }
1448 }
1449
1450 if ((argc - optind) > 1) {
1451 usage(progname);
1452 exit(1);
1453 }
1454
1455 bdrv_init();
1456
1457 /* initialize commands */
1458 quit_init();
1459 help_init();
1460 add_command(&open_cmd);
1461 add_command(&close_cmd);
1462 add_command(&read_cmd);
1463 add_command(&readv_cmd);
1464 add_command(&write_cmd);
1465 add_command(&writev_cmd);
1466 add_command(&aio_read_cmd);
1467 add_command(&aio_write_cmd);
1468 add_command(&aio_flush_cmd);
1469 add_command(&flush_cmd);
1470 add_command(&truncate_cmd);
1471 add_command(&length_cmd);
1472 add_command(&info_cmd);
1473 add_command(&alloc_cmd);
1474
1475 add_args_command(init_args_command);
1476 add_check_command(init_check_command);
1477
1478 /* open the device */
1479 if (readonly)
1480 flags |= BDRV_O_RDONLY;
1481 else
1482 flags |= BDRV_O_RDWR;
1483
1484 if ((argc - optind) == 1)
1485 openfile(argv[optind], flags);
1486 command_loop();
1487
1488 /*
1489 * Make sure all outstanding requests get flushed the program exits.
1490 */
1491 qemu_aio_flush();
1492
1493 if (bs)
1494 bdrv_close(bs);
1495 return 0;
1496 }