]> git.proxmox.com Git - qemu.git/blame - qemu-io.c
Handle vga= in -append
[qemu.git] / qemu-io.c
CommitLineData
e3aff4f6
AL
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
23char *progname;
24static BlockDriverState *bs;
25
26static 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
36static 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
49static void qemu_io_free(void *p)
50{
51 if (misalign)
52 p -= MISALIGN_OFFSET;
53 qemu_vfree(p);
54}
55
56static void
3abcdf49 57dump_buffer(const void *buffer, int64_t offset, int len)
e3aff4f6
AL
58{
59 int i, j;
3abcdf49 60 const uint8_t *p;
e3aff4f6
AL
61
62 for (i = 0, p = buffer; i < len; i += 16) {
3abcdf49 63 const uint8_t *s = p;
e3aff4f6
AL
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++) {
3abcdf49 70 if (isalnum(*s))
e3aff4f6
AL
71 printf("%c", *s);
72 else
73 printf(".");
74 }
75 printf("\n");
76 }
77}
78
79static void
80print_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
cf57298a
CH
101/*
102 * Parse multiple length statements for vectored I/O, and construct an I/O
103 * vector matching it.
104 */
105static void *
106create_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
e3aff4f6
AL
152static 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
163static 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
174static 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
182static 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#define NOT_DONE 0x7fffffff
191static void aio_rw_done(void *opaque, int ret)
192{
193 *(int *)opaque = ret;
194}
195
196static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
197{
198 BlockDriverAIOCB *acb;
199 int async_ret = NOT_DONE;
200
201 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
202 aio_rw_done, &async_ret);
203 if (!acb)
204 return -EIO;
205
206 while (async_ret == NOT_DONE)
207 qemu_aio_wait();
208
209 *total = qiov->size;
210 return async_ret < 0 ? async_ret : 1;
211}
212
213static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
214{
215 BlockDriverAIOCB *acb;
216 int async_ret = NOT_DONE;
217
218 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
219 aio_rw_done, &async_ret);
220 if (!acb)
221 return -EIO;
222
223 while (async_ret == NOT_DONE)
224 qemu_aio_wait();
225
7e9bbc9f 226 *total = qiov->size;
e3aff4f6
AL
227 return async_ret < 0 ? async_ret : 1;
228}
229
230
231static const cmdinfo_t read_cmd;
232
233static void
234read_help(void)
235{
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"
d9654a58
KW
245" -C, -- report statistics in a machine parsable format\n"
246" -l, -- length for pattern verification (only with -P)\n"
e3aff4f6 247" -p, -- use bdrv_pread to read the file\n"
c48101ae 248" -P, -- use a pattern to verify read data\n"
e3aff4f6 249" -q, -- quite mode, do not show I/O statistics\n"
d9654a58
KW
250" -s, -- start offset for pattern verification (only with -P)\n"
251" -v, -- dump buffer to standard output\n"
e3aff4f6
AL
252"\n");
253}
254
255static int
256read_f(int argc, char **argv)
257{
258 struct timeval t1, t2;
259 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
d9654a58 260 int Pflag = 0, sflag = 0, lflag = 0;
e3aff4f6
AL
261 int c, cnt;
262 char *buf;
263 int64_t offset;
d4ec5228
PB
264 int count;
265 /* Some compilers get confused and warn if this is not initialized. */
266 int total = 0;
d9654a58 267 int pattern = 0, pattern_offset = 0, pattern_count = 0;
e3aff4f6 268
d9654a58 269 while ((c = getopt(argc, argv, "Cl:pP:qs:v")) != EOF) {
e3aff4f6
AL
270 switch (c) {
271 case 'C':
272 Cflag = 1;
273 break;
d9654a58
KW
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;
280 }
281 break;
e3aff4f6
AL
282 case 'p':
283 pflag = 1;
284 break;
c48101ae
AL
285 case 'P':
286 Pflag = 1;
287 pattern = atoi(optarg);
288 break;
e3aff4f6
AL
289 case 'q':
290 qflag = 1;
291 break;
d9654a58
KW
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;
298 }
299 break;
e3aff4f6
AL
300 case 'v':
301 vflag = 1;
302 break;
303 default:
304 return command_usage(&read_cmd);
305 }
306 }
307
308 if (optind != argc - 2)
309 return command_usage(&read_cmd);
310
311 offset = cvtnum(argv[optind]);
312 if (offset < 0) {
313 printf("non-numeric length argument -- %s\n", argv[optind]);
314 return 0;
315 }
316
317 optind++;
318 count = cvtnum(argv[optind]);
319 if (count < 0) {
320 printf("non-numeric length argument -- %s\n", argv[optind]);
321 return 0;
322 }
323
d9654a58
KW
324 if (!Pflag && (lflag || sflag)) {
325 return command_usage(&read_cmd);
326 }
327
328 if (!lflag) {
329 pattern_count = count - pattern_offset;
330 }
331
332 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
333 printf("pattern verfication range exceeds end of read data\n");
334 return 0;
335 }
336
e3aff4f6
AL
337 if (!pflag)
338 if (offset & 0x1ff) {
339 printf("offset %lld is not sector aligned\n",
340 (long long)offset);
341 return 0;
342
343 if (count & 0x1ff) {
344 printf("count %d is not sector aligned\n",
345 count);
346 return 0;
347 }
348 }
349
350 buf = qemu_io_alloc(count, 0xab);
351
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);
358
359 if (cnt < 0) {
360 printf("read failed: %s\n", strerror(-cnt));
7d8abfcb 361 goto out;
e3aff4f6
AL
362 }
363
c48101ae 364 if (Pflag) {
d9654a58
KW
365 void* cmp_buf = malloc(pattern_count);
366 memset(cmp_buf, pattern, pattern_count);
367 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
c48101ae
AL
368 printf("Pattern verification failed at offset %lld, "
369 "%d bytes\n",
d9654a58 370 (long long) offset + pattern_offset, pattern_count);
c48101ae
AL
371 }
372 free(cmp_buf);
373 }
374
e3aff4f6 375 if (qflag)
7d8abfcb 376 goto out;
e3aff4f6
AL
377
378 if (vflag)
379 dump_buffer(buf, offset, count);
380
381 /* Finally, report back -- -C gives a parsable format */
382 t2 = tsub(t2, t1);
383 print_report("read", &t2, offset, count, total, cnt, Cflag);
384
7d8abfcb 385out:
e3aff4f6
AL
386 qemu_io_free(buf);
387
388 return 0;
389}
390
391static const cmdinfo_t read_cmd = {
392 .name = "read",
393 .altname = "r",
394 .cfunc = read_f,
395 .argmin = 2,
396 .argmax = -1,
d9654a58 397 .args = "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
e3aff4f6
AL
398 .oneline = "reads a number of bytes at a specified offset",
399 .help = read_help,
400};
401
402static const cmdinfo_t readv_cmd;
403
404static void
405readv_help(void)
406{
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"
c48101ae 418" -P, -- use a pattern to verify read data\n"
e3aff4f6
AL
419" -v, -- dump buffer to standard output\n"
420" -q, -- quite mode, do not show I/O statistics\n"
421"\n");
422}
423
424static int
425readv_f(int argc, char **argv)
426{
427 struct timeval t1, t2;
428 int Cflag = 0, qflag = 0, vflag = 0;
429 int c, cnt;
cf57298a 430 char *buf;
e3aff4f6 431 int64_t offset;
cf57298a
CH
432 int total;
433 int nr_iov;
e3aff4f6 434 QEMUIOVector qiov;
c48101ae
AL
435 int pattern = 0;
436 int Pflag = 0;
e3aff4f6 437
c48101ae 438 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
e3aff4f6
AL
439 switch (c) {
440 case 'C':
441 Cflag = 1;
442 break;
c48101ae
AL
443 case 'P':
444 Pflag = 1;
445 pattern = atoi(optarg);
446 break;
e3aff4f6
AL
447 case 'q':
448 qflag = 1;
449 break;
450 case 'v':
451 vflag = 1;
452 break;
453 default:
454 return command_usage(&readv_cmd);
455 }
456 }
457
458 if (optind > argc - 2)
459 return command_usage(&readv_cmd);
460
461
462 offset = cvtnum(argv[optind]);
463 if (offset < 0) {
464 printf("non-numeric length argument -- %s\n", argv[optind]);
465 return 0;
466 }
467 optind++;
468
469 if (offset & 0x1ff) {
470 printf("offset %lld is not sector aligned\n",
471 (long long)offset);
472 return 0;
473 }
474
e3aff4f6 475 nr_iov = argc - optind;
cf57298a 476 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
e3aff4f6
AL
477
478 gettimeofday(&t1, NULL);
479 cnt = do_aio_readv(&qiov, offset, &total);
480 gettimeofday(&t2, NULL);
481
482 if (cnt < 0) {
483 printf("readv failed: %s\n", strerror(-cnt));
7d8abfcb 484 goto out;
e3aff4f6
AL
485 }
486
c48101ae 487 if (Pflag) {
cf57298a
CH
488 void* cmp_buf = malloc(qiov.size);
489 memset(cmp_buf, pattern, qiov.size);
490 if (memcmp(buf, cmp_buf, qiov.size)) {
c48101ae 491 printf("Pattern verification failed at offset %lld, "
cf57298a
CH
492 "%zd bytes\n",
493 (long long) offset, qiov.size);
c48101ae
AL
494 }
495 free(cmp_buf);
496 }
497
e3aff4f6 498 if (qflag)
7d8abfcb 499 goto out;
e3aff4f6
AL
500
501 if (vflag)
502 dump_buffer(buf, offset, qiov.size);
503
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);
507
7d8abfcb 508out:
e3aff4f6 509 qemu_io_free(buf);
e3aff4f6
AL
510 return 0;
511}
512
513static const cmdinfo_t readv_cmd = {
514 .name = "readv",
515 .cfunc = readv_f,
516 .argmin = 2,
517 .argmax = -1,
c48101ae 518 .args = "[-Cqv] [-P pattern ] off len [len..]",
e3aff4f6
AL
519 .oneline = "reads a number of bytes at a specified offset",
520 .help = readv_help,
521};
522
523static const cmdinfo_t write_cmd;
524
525static void
526write_help(void)
527{
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");
542}
543
544static int
545write_f(int argc, char **argv)
546{
547 struct timeval t1, t2;
548 int Cflag = 0, pflag = 0, qflag = 0;
549 int c, cnt;
550 char *buf;
551 int64_t offset;
d4ec5228
PB
552 int count;
553 /* Some compilers get confused and warn if this is not initialized. */
554 int total = 0;
e3aff4f6
AL
555 int pattern = 0xcd;
556
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);
573 }
574 }
575
576 if (optind != argc - 2)
577 return command_usage(&write_cmd);
578
579 offset = cvtnum(argv[optind]);
580 if (offset < 0) {
581 printf("non-numeric length argument -- %s\n", argv[optind]);
582 return 0;
583 }
584
585 optind++;
586 count = cvtnum(argv[optind]);
587 if (count < 0) {
588 printf("non-numeric length argument -- %s\n", argv[optind]);
589 return 0;
590 }
591
592 if (!pflag) {
593 if (offset & 0x1ff) {
594 printf("offset %lld is not sector aligned\n",
595 (long long)offset);
596 return 0;
597 }
598
599 if (count & 0x1ff) {
600 printf("count %d is not sector aligned\n",
601 count);
602 return 0;
603 }
604 }
605
606 buf = qemu_io_alloc(count, pattern);
607
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);
614
615 if (cnt < 0) {
616 printf("write failed: %s\n", strerror(-cnt));
7d8abfcb 617 goto out;
e3aff4f6
AL
618 }
619
620 if (qflag)
7d8abfcb 621 goto out;
e3aff4f6
AL
622
623 /* Finally, report back -- -C gives a parsable format */
624 t2 = tsub(t2, t1);
625 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
626
7d8abfcb 627out:
e3aff4f6
AL
628 qemu_io_free(buf);
629
630 return 0;
631}
632
633static 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,
642};
643
644static const cmdinfo_t writev_cmd;
645
646static void
647writev_help(void)
648{
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");
662}
663
664static int
665writev_f(int argc, char **argv)
666{
667 struct timeval t1, t2;
668 int Cflag = 0, qflag = 0;
669 int c, cnt;
cf57298a 670 char *buf;
e3aff4f6 671 int64_t offset;
cf57298a
CH
672 int total;
673 int nr_iov;
e3aff4f6
AL
674 int pattern = 0xcd;
675 QEMUIOVector qiov;
676
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);
690 }
691 }
692
693 if (optind > argc - 2)
694 return command_usage(&writev_cmd);
695
696 offset = cvtnum(argv[optind]);
697 if (offset < 0) {
698 printf("non-numeric length argument -- %s\n", argv[optind]);
699 return 0;
700 }
701 optind++;
702
703 if (offset & 0x1ff) {
704 printf("offset %lld is not sector aligned\n",
705 (long long)offset);
706 return 0;
707 }
708
e3aff4f6 709 nr_iov = argc - optind;
cf57298a 710 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
e3aff4f6
AL
711
712 gettimeofday(&t1, NULL);
713 cnt = do_aio_writev(&qiov, offset, &total);
714 gettimeofday(&t2, NULL);
715
716 if (cnt < 0) {
717 printf("writev failed: %s\n", strerror(-cnt));
7d8abfcb 718 goto out;
e3aff4f6
AL
719 }
720
721 if (qflag)
7d8abfcb 722 goto out;
e3aff4f6
AL
723
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);
7d8abfcb 727out:
e3aff4f6 728 qemu_io_free(buf);
e3aff4f6
AL
729 return 0;
730}
731
732static 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,
740};
741
95533d5f
CH
742struct 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;
752};
753
754static void
755aio_write_done(void *opaque, int ret)
756{
757 struct aio_ctx *ctx = opaque;
758 struct timeval t2;
95533d5f
CH
759
760 gettimeofday(&t2, NULL);
761
95533d5f
CH
762
763 if (ret < 0) {
764 printf("aio_write failed: %s\n", strerror(-ret));
7d8abfcb 765 goto out;
95533d5f
CH
766 }
767
230d4fa4 768 if (ctx->qflag) {
7d8abfcb 769 goto out;
230d4fa4 770 }
95533d5f
CH
771
772 /* Finally, report back -- -C gives a parsable format */
773 t2 = tsub(t2, ctx->t1);
230d4fa4
CH
774 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
775 ctx->qiov.size, 1, ctx->Cflag);
7d8abfcb 776out:
95533d5f
CH
777 qemu_io_free(ctx->buf);
778 free(ctx);
779}
780
781static const cmdinfo_t aio_read_cmd;
782
783static void
784aio_read_done(void *opaque, int ret)
785{
786 struct aio_ctx *ctx = opaque;
787 struct timeval t2;
95533d5f
CH
788
789 gettimeofday(&t2, NULL);
790
95533d5f
CH
791 if (ret < 0) {
792 printf("readv failed: %s\n", strerror(-ret));
7d8abfcb 793 goto out;
95533d5f
CH
794 }
795
796 if (ctx->Pflag) {
230d4fa4 797 void *cmp_buf = malloc(ctx->qiov.size);
95533d5f 798
230d4fa4
CH
799 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
800 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
95533d5f 801 printf("Pattern verification failed at offset %lld, "
230d4fa4
CH
802 "%zd bytes\n",
803 (long long) ctx->offset, ctx->qiov.size);
95533d5f
CH
804 }
805 free(cmp_buf);
806 }
807
230d4fa4 808 if (ctx->qflag) {
7d8abfcb 809 goto out;
230d4fa4 810 }
95533d5f 811
230d4fa4
CH
812 if (ctx->vflag) {
813 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
814 }
95533d5f
CH
815
816 /* Finally, report back -- -C gives a parsable format */
817 t2 = tsub(t2, ctx->t1);
230d4fa4
CH
818 print_report("read", &t2, ctx->offset, ctx->qiov.size,
819 ctx->qiov.size, 1, ctx->Cflag);
7d8abfcb 820out:
95533d5f
CH
821 qemu_io_free(ctx->buf);
822 free(ctx);
95533d5f
CH
823}
824
825static void
826aio_read_help(void)
827{
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");
844}
845
846static int
847aio_read_f(int argc, char **argv)
848{
cf57298a 849 int nr_iov, c;
95533d5f
CH
850 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
851 BlockDriverAIOCB *acb;
852
95533d5f
CH
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:
7d8abfcb 869 free(ctx);
95533d5f
CH
870 return command_usage(&aio_read_cmd);
871 }
872 }
873
7d8abfcb
KW
874 if (optind > argc - 2) {
875 free(ctx);
95533d5f 876 return command_usage(&aio_read_cmd);
7d8abfcb 877 }
95533d5f
CH
878
879 ctx->offset = cvtnum(argv[optind]);
880 if (ctx->offset < 0) {
881 printf("non-numeric length argument -- %s\n", argv[optind]);
7d8abfcb 882 free(ctx);
95533d5f
CH
883 return 0;
884 }
885 optind++;
886
887 if (ctx->offset & 0x1ff) {
888 printf("offset %lld is not sector aligned\n",
889 (long long)ctx->offset);
7d8abfcb 890 free(ctx);
95533d5f
CH
891 return 0;
892 }
893
95533d5f 894 nr_iov = argc - optind;
cf57298a 895 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
95533d5f
CH
896
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);
7d8abfcb
KW
900 if (!acb) {
901 free(ctx->buf);
902 free(ctx);
95533d5f 903 return -EIO;
7d8abfcb 904 }
95533d5f
CH
905
906 return 0;
907}
908
909static 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,
917};
918
919static const cmdinfo_t aio_write_cmd;
920
921static void
922aio_write_help(void)
923{
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");
940}
941
942
943static int
944aio_write_f(int argc, char **argv)
945{
cf57298a 946 int nr_iov, c;
95533d5f
CH
947 int pattern = 0xcd;
948 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
949 BlockDriverAIOCB *acb;
950
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:
7d8abfcb 963 free(ctx);
95533d5f
CH
964 return command_usage(&aio_write_cmd);
965 }
966 }
967
7d8abfcb
KW
968 if (optind > argc - 2) {
969 free(ctx);
95533d5f 970 return command_usage(&aio_write_cmd);
7d8abfcb 971 }
95533d5f
CH
972
973 ctx->offset = cvtnum(argv[optind]);
974 if (ctx->offset < 0) {
975 printf("non-numeric length argument -- %s\n", argv[optind]);
7d8abfcb 976 free(ctx);
95533d5f
CH
977 return 0;
978 }
979 optind++;
980
981 if (ctx->offset & 0x1ff) {
982 printf("offset %lld is not sector aligned\n",
983 (long long)ctx->offset);
7d8abfcb 984 free(ctx);
95533d5f
CH
985 return 0;
986 }
987
95533d5f 988 nr_iov = argc - optind;
cf57298a 989 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
95533d5f
CH
990
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);
7d8abfcb
KW
994 if (!acb) {
995 free(ctx->buf);
996 free(ctx);
95533d5f 997 return -EIO;
7d8abfcb 998 }
95533d5f
CH
999
1000 return 0;
1001}
1002
1003static 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,
1011};
1012
1013static int
1014aio_flush_f(int argc, char **argv)
1015{
1016 qemu_aio_flush();
1017 return 0;
1018}
1019
1020static const cmdinfo_t aio_flush_cmd = {
1021 .name = "aio_flush",
1022 .cfunc = aio_flush_f,
1023 .oneline = "completes all outstanding aio requets"
1024};
1025
e3aff4f6
AL
1026static int
1027flush_f(int argc, char **argv)
1028{
1029 bdrv_flush(bs);
1030 return 0;
1031}
1032
1033static 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",
1038};
1039
1040static int
1041truncate_f(int argc, char **argv)
1042{
1043 int64_t offset;
1044 int ret;
1045
1046 offset = cvtnum(argv[1]);
1047 if (offset < 0) {
1048 printf("non-numeric truncate argument -- %s\n", argv[1]);
1049 return 0;
1050 }
1051
1052 ret = bdrv_truncate(bs, offset);
1053 if (ret < 0) {
1054 printf("truncate: %s", strerror(ret));
1055 return 0;
1056 }
1057
1058 return 0;
1059}
1060
1061static 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",
1069};
1070
1071static int
1072length_f(int argc, char **argv)
1073{
1074 int64_t size;
1075 char s1[64];
1076
1077 size = bdrv_getlength(bs);
1078 if (size < 0) {
1079 printf("getlength: %s", strerror(size));
1080 return 0;
1081 }
1082
1083 cvtstr(size, s1, sizeof(s1));
1084 printf("%s\n", s1);
1085 return 0;
1086}
1087
1088
1089static const cmdinfo_t length_cmd = {
1090 .name = "length",
1091 .altname = "l",
1092 .cfunc = length_f,
1093 .oneline = "gets the length of the current file",
1094};
1095
1096
1097static int
1098info_f(int argc, char **argv)
1099{
1100 BlockDriverInfo bdi;
1101 char s1[64], s2[64];
1102 int ret;
1103
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);
1108
1109 ret = bdrv_get_info(bs, &bdi);
1110 if (ret)
1111 return 0;
1112
1113 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1114 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1115
1116 printf("cluster size: %s\n", s1);
1117 printf("vm state offset: %s\n", s2);
1118
1119 return 0;
1120}
1121
1122
1123
1124static const cmdinfo_t info_cmd = {
1125 .name = "info",
1126 .altname = "i",
1127 .cfunc = info_f,
1128 .oneline = "prints information about the current file",
1129};
1130
1131static int
1132alloc_f(int argc, char **argv)
1133{
1134 int64_t offset;
1135 int nb_sectors;
1136 char s1[64];
1137 int num;
1138 int ret;
838ab728 1139 const char *retstr;
e3aff4f6
AL
1140
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;
1146 }
1147
1148 if (argc == 3)
1149 nb_sectors = cvtnum(argv[2]);
1150 else
1151 nb_sectors = 1;
1152
1153 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
e3aff4f6
AL
1154
1155 cvtstr(offset, s1, sizeof(s1));
1156
838ab728 1157 retstr = ret ? "allocated" : "not allocated";
e3aff4f6 1158 if (nb_sectors == 1)
838ab728 1159 printf("sector %s at offset %s\n", retstr, s1);
e3aff4f6 1160 else
838ab728
AL
1161 printf("%d/%d sectors %s at offset %s\n",
1162 num, nb_sectors, retstr, s1);
e3aff4f6
AL
1163 return 0;
1164}
1165
1166static 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",
1174};
1175
1176static int
1177close_f(int argc, char **argv)
1178{
1179 bdrv_close(bs);
1180 bs = NULL;
1181 return 0;
1182}
1183
1184static const cmdinfo_t close_cmd = {
1185 .name = "close",
1186 .altname = "c",
1187 .cfunc = close_f,
1188 .oneline = "close the current open file",
1189};
1190
9c4bab26 1191static int openfile(char *name, int flags, int growable)
e3aff4f6
AL
1192{
1193 if (bs) {
1194 fprintf(stderr, "file open already, try 'help close'\n");
1195 return 1;
1196 }
1197
1198 bs = bdrv_new("hda");
1199 if (!bs)
1200 return 1;
1201
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;
1206 }
1207
9c4bab26
CH
1208
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;
1215 }
1216 bs->growable = 1;
1217 }
1218
e3aff4f6
AL
1219 return 0;
1220}
1221
1222static void
1223open_help(void)
1224{
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"
9c4bab26 1237" -g, -- allow file to grow (only applies to protocols)"
e3aff4f6
AL
1238"\n");
1239}
1240
1241static const cmdinfo_t open_cmd;
1242
1243static int
1244open_f(int argc, char **argv)
1245{
1246 int flags = 0;
1247 int readonly = 0;
9c4bab26 1248 int growable = 0;
e3aff4f6
AL
1249 int c;
1250
9c4bab26 1251 while ((c = getopt(argc, argv, "snCrg")) != EOF) {
e3aff4f6
AL
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;
9c4bab26
CH
1265 case 'g':
1266 growable = 1;
1267 break;
e3aff4f6
AL
1268 default:
1269 return command_usage(&open_cmd);
1270 }
1271 }
1272
1273 if (readonly)
1274 flags |= BDRV_O_RDONLY;
1275 else
1276 flags |= BDRV_O_RDWR;
1277
1278 if (optind != argc - 1)
1279 return command_usage(&open_cmd);
1280
9c4bab26 1281 return openfile(argv[optind], flags, growable);
e3aff4f6
AL
1282}
1283
1284static 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,
1294};
1295
1296static int
1297init_args_command(
1298 int index)
1299{
1300 /* only one device allowed so far */
1301 if (index >= 1)
1302 return 0;
1303 return ++index;
1304}
1305
1306static int
1307init_check_command(
1308 const cmdinfo_t *ct)
1309{
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;
1315 }
1316 return 1;
1317}
1318
1319static void usage(const char *name)
1320{
1321 printf(
1322"Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
84844a20 1323"QEMU Disk exerciser\n"
e3aff4f6
AL
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);
1335}
1336
1337
1338int main(int argc, char **argv)
1339{
1340 int readonly = 0;
9c4bab26
CH
1341 int growable = 0;
1342 const char *sopt = "hVc:Crsnmg";
e3aff4f6
AL
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' },
9c4bab26 1353 { "growable", 0, 0, 'g' },
e3aff4f6
AL
1354 { NULL, 0, 0, 0 }
1355 };
1356 int c;
1357 int opt_index = 0;
1358 int flags = 0;
1359
1360 progname = basename(argv[0]);
1361
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;
9c4bab26
CH
1382 case 'g':
1383 growable = 1;
1384 break;
e3aff4f6
AL
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);
1394 }
1395 }
1396
1397 if ((argc - optind) > 1) {
1398 usage(progname);
1399 exit(1);
1400 }
1401
1402 bdrv_init();
1403
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);
95533d5f
CH
1413 add_command(&aio_read_cmd);
1414 add_command(&aio_write_cmd);
1415 add_command(&aio_flush_cmd);
e3aff4f6
AL
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);
1421
1422 add_args_command(init_args_command);
1423 add_check_command(init_check_command);
1424
1425 /* open the device */
1426 if (readonly)
1427 flags |= BDRV_O_RDONLY;
1428 else
1429 flags |= BDRV_O_RDWR;
1430
1431 if ((argc - optind) == 1)
9c4bab26 1432 openfile(argv[optind], flags, growable);
e3aff4f6
AL
1433 command_loop();
1434
95533d5f
CH
1435 /*
1436 * Make sure all outstanding requests get flushed the program exits.
1437 */
1438 qemu_aio_flush();
1439
e3aff4f6
AL
1440 if (bs)
1441 bdrv_close(bs);
1442 return 0;
1443}