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