]> Git Repo - qemu.git/blame - qemu-io-cmds.c
qemu-io: Account IO by aio_read and aio_write
[qemu.git] / qemu-io-cmds.c
CommitLineData
797ac58c
KW
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
3d21994f 11#include "qemu-io.h"
797ac58c 12#include "block/block_int.h"
a8d8ecb7 13#include "block/qapi.h"
6a1751b7 14#include "qemu/main-loop.h"
cd33d02a 15#include "qemu/timer.h"
a91f9584 16#include "sysemu/block-backend.h"
797ac58c
KW
17
18#define CMD_NOFILE_OK 0x01
19
f9883880 20bool qemuio_misalign;
797ac58c 21
c2cdf5c5
KW
22static cmdinfo_t *cmdtab;
23static int ncmds;
24
25static int compare_cmdname(const void *a, const void *b)
26{
27 return strcmp(((const cmdinfo_t *)a)->name,
28 ((const cmdinfo_t *)b)->name);
29}
30
31void qemuio_add_command(const cmdinfo_t *ci)
32{
02c4f26b 33 cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
c2cdf5c5
KW
34 cmdtab[ncmds - 1] = *ci;
35 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
36}
37
38int qemuio_command_usage(const cmdinfo_t *ci)
39{
40 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
41 return 0;
42}
43
44static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
45{
46 if (ct->flags & CMD_FLAG_GLOBAL) {
47 return 1;
48 }
49 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
50 fprintf(stderr, "no file open, try 'help open'\n");
51 return 0;
52 }
53 return 1;
54}
55
3d21994f
KW
56static int command(BlockDriverState *bs, const cmdinfo_t *ct, int argc,
57 char **argv)
c2cdf5c5
KW
58{
59 char *cmd = argv[0];
60
3d21994f 61 if (!init_check_command(bs, ct)) {
c2cdf5c5
KW
62 return 0;
63 }
64
65 if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
66 if (ct->argmax == -1) {
67 fprintf(stderr,
68 "bad argument count %d to %s, expected at least %d arguments\n",
69 argc-1, cmd, ct->argmin);
70 } else if (ct->argmin == ct->argmax) {
71 fprintf(stderr,
72 "bad argument count %d to %s, expected %d arguments\n",
73 argc-1, cmd, ct->argmin);
74 } else {
75 fprintf(stderr,
76 "bad argument count %d to %s, expected between %d and %d arguments\n",
77 argc-1, cmd, ct->argmin, ct->argmax);
78 }
79 return 0;
80 }
81 optind = 0;
3d21994f 82 return ct->cfunc(bs, argc, argv);
c2cdf5c5
KW
83}
84
85static const cmdinfo_t *find_command(const char *cmd)
86{
87 cmdinfo_t *ct;
88
89 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
90 if (strcmp(ct->name, cmd) == 0 ||
91 (ct->altname && strcmp(ct->altname, cmd) == 0))
92 {
93 return (const cmdinfo_t *)ct;
94 }
95 }
96 return NULL;
97}
98
4694020d
SH
99/* Invoke fn() for commands with a matching prefix */
100void qemuio_complete_command(const char *input,
101 void (*fn)(const char *cmd, void *opaque),
102 void *opaque)
103{
104 cmdinfo_t *ct;
105 size_t input_len = strlen(input);
106
107 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
108 if (strncmp(input, ct->name, input_len) == 0) {
109 fn(ct->name, opaque);
110 }
111 }
112}
113
c2cdf5c5
KW
114static char **breakline(char *input, int *count)
115{
116 int c = 0;
117 char *p;
5839e53b 118 char **rval = g_new0(char *, 1);
c2cdf5c5
KW
119
120 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
121 if (!*p) {
122 continue;
123 }
124 c++;
08193dd5 125 rval = g_renew(char *, rval, (c + 1));
c2cdf5c5
KW
126 rval[c - 1] = p;
127 rval[c] = NULL;
128 }
129 *count = c;
130 return rval;
131}
132
797ac58c
KW
133static int64_t cvtnum(const char *s)
134{
135 char *end;
136 return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
137}
138
0b613881
KW
139#define EXABYTES(x) ((long long)(x) << 60)
140#define PETABYTES(x) ((long long)(x) << 50)
141#define TERABYTES(x) ((long long)(x) << 40)
142#define GIGABYTES(x) ((long long)(x) << 30)
143#define MEGABYTES(x) ((long long)(x) << 20)
144#define KILOBYTES(x) ((long long)(x) << 10)
145
146#define TO_EXABYTES(x) ((x) / EXABYTES(1))
147#define TO_PETABYTES(x) ((x) / PETABYTES(1))
148#define TO_TERABYTES(x) ((x) / TERABYTES(1))
149#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
150#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
151#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
152
153static void cvtstr(double value, char *str, size_t size)
154{
155 char *trim;
156 const char *suffix;
157
158 if (value >= EXABYTES(1)) {
159 suffix = " EiB";
160 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
161 } else if (value >= PETABYTES(1)) {
162 suffix = " PiB";
163 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
164 } else if (value >= TERABYTES(1)) {
165 suffix = " TiB";
166 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
167 } else if (value >= GIGABYTES(1)) {
168 suffix = " GiB";
169 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
170 } else if (value >= MEGABYTES(1)) {
171 suffix = " MiB";
172 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
173 } else if (value >= KILOBYTES(1)) {
174 suffix = " KiB";
175 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
176 } else {
177 suffix = " bytes";
178 snprintf(str, size - 6, "%f", value);
179 }
180
181 trim = strstr(str, ".000");
182 if (trim) {
183 strcpy(trim, suffix);
184 } else {
185 strcat(str, suffix);
186 }
187}
188
189
190
191static struct timeval tsub(struct timeval t1, struct timeval t2)
192{
193 t1.tv_usec -= t2.tv_usec;
194 if (t1.tv_usec < 0) {
195 t1.tv_usec += 1000000;
196 t1.tv_sec--;
197 }
198 t1.tv_sec -= t2.tv_sec;
199 return t1;
200}
201
202static double tdiv(double value, struct timeval tv)
203{
204 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
205}
206
207#define HOURS(sec) ((sec) / (60 * 60))
208#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
209#define SECONDS(sec) ((sec) % 60)
210
211enum {
212 DEFAULT_TIME = 0x0,
213 TERSE_FIXED_TIME = 0x1,
214 VERBOSE_FIXED_TIME = 0x2,
215};
216
217static void timestr(struct timeval *tv, char *ts, size_t size, int format)
218{
219 double usec = (double)tv->tv_usec / 1000000.0;
220
221 if (format & TERSE_FIXED_TIME) {
222 if (!HOURS(tv->tv_sec)) {
223 snprintf(ts, size, "%u:%02u.%02u",
224 (unsigned int) MINUTES(tv->tv_sec),
225 (unsigned int) SECONDS(tv->tv_sec),
226 (unsigned int) (usec * 100));
227 return;
228 }
229 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
230 }
231
232 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
233 snprintf(ts, size, "%u:%02u:%02u.%02u",
234 (unsigned int) HOURS(tv->tv_sec),
235 (unsigned int) MINUTES(tv->tv_sec),
236 (unsigned int) SECONDS(tv->tv_sec),
237 (unsigned int) (usec * 100));
238 } else {
239 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
240 }
241}
242
797ac58c
KW
243/*
244 * Parse the pattern argument to various sub-commands.
245 *
246 * Because the pattern is used as an argument to memset it must evaluate
247 * to an unsigned integer that fits into a single byte.
248 */
249static int parse_pattern(const char *arg)
250{
251 char *endptr = NULL;
252 long pattern;
253
254 pattern = strtol(arg, &endptr, 0);
255 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
256 printf("%s is not a valid pattern byte\n", arg);
257 return -1;
258 }
259
260 return pattern;
261}
262
263/*
264 * Memory allocation helpers.
265 *
266 * Make sure memory is aligned by default, or purposefully misaligned if
267 * that is specified on the command line.
268 */
269
270#define MISALIGN_OFFSET 16
271static void *qemu_io_alloc(BlockDriverState *bs, size_t len, int pattern)
272{
273 void *buf;
274
275 if (qemuio_misalign) {
276 len += MISALIGN_OFFSET;
277 }
278 buf = qemu_blockalign(bs, len);
279 memset(buf, pattern, len);
280 if (qemuio_misalign) {
281 buf += MISALIGN_OFFSET;
282 }
283 return buf;
284}
285
286static void qemu_io_free(void *p)
287{
288 if (qemuio_misalign) {
289 p -= MISALIGN_OFFSET;
290 }
291 qemu_vfree(p);
292}
293
294static void dump_buffer(const void *buffer, int64_t offset, int len)
295{
296 int i, j;
297 const uint8_t *p;
298
299 for (i = 0, p = buffer; i < len; i += 16) {
300 const uint8_t *s = p;
301
302 printf("%08" PRIx64 ": ", offset + i);
303 for (j = 0; j < 16 && i + j < len; j++, p++) {
304 printf("%02x ", *p);
305 }
306 printf(" ");
307 for (j = 0; j < 16 && i + j < len; j++, s++) {
308 if (isalnum(*s)) {
309 printf("%c", *s);
310 } else {
311 printf(".");
312 }
313 }
314 printf("\n");
315 }
316}
317
318static void print_report(const char *op, struct timeval *t, int64_t offset,
319 int count, int total, int cnt, int Cflag)
320{
321 char s1[64], s2[64], ts[64];
322
323 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
324 if (!Cflag) {
325 cvtstr((double)total, s1, sizeof(s1));
326 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
327 printf("%s %d/%d bytes at offset %" PRId64 "\n",
328 op, total, count, offset);
329 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
330 s1, cnt, ts, s2, tdiv((double)cnt, *t));
331 } else {/* bytes,ops,time,bytes/sec,ops/sec */
332 printf("%d,%d,%s,%.3f,%.3f\n",
333 total, cnt, ts,
334 tdiv((double)total, *t),
335 tdiv((double)cnt, *t));
336 }
337}
338
339/*
340 * Parse multiple length statements for vectored I/O, and construct an I/O
341 * vector matching it.
342 */
343static void *
344create_iovec(BlockDriverState *bs, QEMUIOVector *qiov, char **argv, int nr_iov,
345 int pattern)
346{
347 size_t *sizes = g_new0(size_t, nr_iov);
348 size_t count = 0;
349 void *buf = NULL;
350 void *p;
351 int i;
352
353 for (i = 0; i < nr_iov; i++) {
354 char *arg = argv[i];
355 int64_t len;
356
357 len = cvtnum(arg);
358 if (len < 0) {
359 printf("non-numeric length argument -- %s\n", arg);
360 goto fail;
361 }
362
363 /* should be SIZE_T_MAX, but that doesn't exist */
364 if (len > INT_MAX) {
365 printf("too large length argument -- %s\n", arg);
366 goto fail;
367 }
368
369 if (len & 0x1ff) {
370 printf("length argument %" PRId64
371 " is not sector aligned\n", len);
372 goto fail;
373 }
374
375 sizes[i] = len;
376 count += len;
377 }
378
379 qemu_iovec_init(qiov, nr_iov);
380
381 buf = p = qemu_io_alloc(bs, count, pattern);
382
383 for (i = 0; i < nr_iov; i++) {
384 qemu_iovec_add(qiov, p, sizes[i]);
385 p += sizes[i];
386 }
387
388fail:
389 g_free(sizes);
390 return buf;
391}
392
393static int do_read(BlockDriverState *bs, char *buf, int64_t offset, int count,
394 int *total)
395{
396 int ret;
397
398 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
399 if (ret < 0) {
400 return ret;
401 }
402 *total = count;
403 return 1;
404}
405
406static int do_write(BlockDriverState *bs, char *buf, int64_t offset, int count,
407 int *total)
408{
409 int ret;
410
411 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
412 if (ret < 0) {
413 return ret;
414 }
415 *total = count;
416 return 1;
417}
418
419static int do_pread(BlockDriverState *bs, char *buf, int64_t offset, int count,
420 int *total)
421{
422 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
423 if (*total < 0) {
424 return *total;
425 }
426 return 1;
427}
428
429static int do_pwrite(BlockDriverState *bs, char *buf, int64_t offset, int count,
430 int *total)
431{
432 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
433 if (*total < 0) {
434 return *total;
435 }
436 return 1;
437}
438
439typedef struct {
440 BlockDriverState *bs;
441 int64_t offset;
442 int count;
443 int *total;
444 int ret;
445 bool done;
446} CoWriteZeroes;
447
448static void coroutine_fn co_write_zeroes_entry(void *opaque)
449{
450 CoWriteZeroes *data = opaque;
451
452 data->ret = bdrv_co_write_zeroes(data->bs, data->offset / BDRV_SECTOR_SIZE,
aa7bfbff 453 data->count / BDRV_SECTOR_SIZE, 0);
797ac58c
KW
454 data->done = true;
455 if (data->ret < 0) {
456 *data->total = data->ret;
457 return;
458 }
459
460 *data->total = data->count;
461}
462
463static int do_co_write_zeroes(BlockDriverState *bs, int64_t offset, int count,
464 int *total)
465{
466 Coroutine *co;
467 CoWriteZeroes data = {
468 .bs = bs,
469 .offset = offset,
470 .count = count,
471 .total = total,
472 .done = false,
473 };
474
475 co = qemu_coroutine_create(co_write_zeroes_entry);
476 qemu_coroutine_enter(co, &data);
477 while (!data.done) {
b47ec2c4 478 aio_poll(bdrv_get_aio_context(bs), true);
797ac58c
KW
479 }
480 if (data.ret < 0) {
481 return data.ret;
482 } else {
483 return 1;
484 }
485}
486
487static int do_write_compressed(BlockDriverState *bs, char *buf, int64_t offset,
488 int count, int *total)
489{
490 int ret;
491
492 ret = bdrv_write_compressed(bs, offset >> 9, (uint8_t *)buf, count >> 9);
493 if (ret < 0) {
494 return ret;
495 }
496 *total = count;
497 return 1;
498}
499
500static int do_load_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
501 int count, int *total)
502{
503 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
504 if (*total < 0) {
505 return *total;
506 }
507 return 1;
508}
509
510static int do_save_vmstate(BlockDriverState *bs, char *buf, int64_t offset,
511 int count, int *total)
512{
513 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
514 if (*total < 0) {
515 return *total;
516 }
517 return 1;
518}
519
520#define NOT_DONE 0x7fffffff
521static void aio_rw_done(void *opaque, int ret)
522{
523 *(int *)opaque = ret;
524}
525
526static int do_aio_readv(BlockDriverState *bs, QEMUIOVector *qiov,
527 int64_t offset, int *total)
528{
529 int async_ret = NOT_DONE;
530
531 bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
532 aio_rw_done, &async_ret);
533 while (async_ret == NOT_DONE) {
534 main_loop_wait(false);
535 }
536
537 *total = qiov->size;
538 return async_ret < 0 ? async_ret : 1;
539}
540
541static int do_aio_writev(BlockDriverState *bs, QEMUIOVector *qiov,
542 int64_t offset, int *total)
543{
544 int async_ret = NOT_DONE;
545
546 bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
547 aio_rw_done, &async_ret);
548 while (async_ret == NOT_DONE) {
549 main_loop_wait(false);
550 }
551
552 *total = qiov->size;
553 return async_ret < 0 ? async_ret : 1;
554}
555
556struct multiwrite_async_ret {
557 int num_done;
558 int error;
559};
560
561static void multiwrite_cb(void *opaque, int ret)
562{
563 struct multiwrite_async_ret *async_ret = opaque;
564
565 async_ret->num_done++;
566 if (ret < 0) {
567 async_ret->error = ret;
568 }
569}
570
571static int do_aio_multiwrite(BlockDriverState *bs, BlockRequest* reqs,
572 int num_reqs, int *total)
573{
574 int i, ret;
575 struct multiwrite_async_ret async_ret = {
576 .num_done = 0,
577 .error = 0,
578 };
579
580 *total = 0;
581 for (i = 0; i < num_reqs; i++) {
582 reqs[i].cb = multiwrite_cb;
583 reqs[i].opaque = &async_ret;
584 *total += reqs[i].qiov->size;
585 }
586
587 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
588 if (ret < 0) {
589 return ret;
590 }
591
592 while (async_ret.num_done < num_reqs) {
593 main_loop_wait(false);
594 }
595
596 return async_ret.error < 0 ? async_ret.error : 1;
597}
598
599static void read_help(void)
600{
601 printf(
602"\n"
603" reads a range of bytes from the given offset\n"
604"\n"
605" Example:\n"
606" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
607"\n"
608" Reads a segment of the currently open file, optionally dumping it to the\n"
609" standard output stream (with -v option) for subsequent inspection.\n"
610" -b, -- read from the VM state rather than the virtual disk\n"
611" -C, -- report statistics in a machine parsable format\n"
612" -l, -- length for pattern verification (only with -P)\n"
613" -p, -- use bdrv_pread to read the file\n"
614" -P, -- use a pattern to verify read data\n"
615" -q, -- quiet mode, do not show I/O statistics\n"
616" -s, -- start offset for pattern verification (only with -P)\n"
617" -v, -- dump buffer to standard output\n"
618"\n");
619}
620
621static int read_f(BlockDriverState *bs, int argc, char **argv);
622
623static const cmdinfo_t read_cmd = {
624 .name = "read",
625 .altname = "r",
626 .cfunc = read_f,
627 .argmin = 2,
628 .argmax = -1,
629 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
630 .oneline = "reads a number of bytes at a specified offset",
631 .help = read_help,
632};
633
634static int read_f(BlockDriverState *bs, int argc, char **argv)
635{
636 struct timeval t1, t2;
637 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
638 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
639 int c, cnt;
640 char *buf;
641 int64_t offset;
642 int count;
643 /* Some compilers get confused and warn if this is not initialized. */
644 int total = 0;
645 int pattern = 0, pattern_offset = 0, pattern_count = 0;
646
647 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
648 switch (c) {
649 case 'b':
650 bflag = 1;
651 break;
652 case 'C':
653 Cflag = 1;
654 break;
655 case 'l':
656 lflag = 1;
657 pattern_count = cvtnum(optarg);
658 if (pattern_count < 0) {
659 printf("non-numeric length argument -- %s\n", optarg);
660 return 0;
661 }
662 break;
663 case 'p':
664 pflag = 1;
665 break;
666 case 'P':
667 Pflag = 1;
668 pattern = parse_pattern(optarg);
669 if (pattern < 0) {
670 return 0;
671 }
672 break;
673 case 'q':
674 qflag = 1;
675 break;
676 case 's':
677 sflag = 1;
678 pattern_offset = cvtnum(optarg);
679 if (pattern_offset < 0) {
680 printf("non-numeric length argument -- %s\n", optarg);
681 return 0;
682 }
683 break;
684 case 'v':
685 vflag = 1;
686 break;
687 default:
c2cdf5c5 688 return qemuio_command_usage(&read_cmd);
797ac58c
KW
689 }
690 }
691
692 if (optind != argc - 2) {
c2cdf5c5 693 return qemuio_command_usage(&read_cmd);
797ac58c
KW
694 }
695
696 if (bflag && pflag) {
697 printf("-b and -p cannot be specified at the same time\n");
698 return 0;
699 }
700
701 offset = cvtnum(argv[optind]);
702 if (offset < 0) {
703 printf("non-numeric length argument -- %s\n", argv[optind]);
704 return 0;
705 }
706
707 optind++;
708 count = cvtnum(argv[optind]);
709 if (count < 0) {
710 printf("non-numeric length argument -- %s\n", argv[optind]);
711 return 0;
712 }
713
714 if (!Pflag && (lflag || sflag)) {
c2cdf5c5 715 return qemuio_command_usage(&read_cmd);
797ac58c
KW
716 }
717
718 if (!lflag) {
719 pattern_count = count - pattern_offset;
720 }
721
722 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
723 printf("pattern verification range exceeds end of read data\n");
724 return 0;
725 }
726
727 if (!pflag) {
728 if (offset & 0x1ff) {
729 printf("offset %" PRId64 " is not sector aligned\n",
730 offset);
731 return 0;
732 }
733 if (count & 0x1ff) {
734 printf("count %d is not sector aligned\n",
735 count);
736 return 0;
737 }
738 }
739
740 buf = qemu_io_alloc(bs, count, 0xab);
741
742 gettimeofday(&t1, NULL);
743 if (pflag) {
744 cnt = do_pread(bs, buf, offset, count, &total);
745 } else if (bflag) {
746 cnt = do_load_vmstate(bs, buf, offset, count, &total);
747 } else {
748 cnt = do_read(bs, buf, offset, count, &total);
749 }
750 gettimeofday(&t2, NULL);
751
752 if (cnt < 0) {
753 printf("read failed: %s\n", strerror(-cnt));
754 goto out;
755 }
756
757 if (Pflag) {
758 void *cmp_buf = g_malloc(pattern_count);
759 memset(cmp_buf, pattern, pattern_count);
760 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
761 printf("Pattern verification failed at offset %"
762 PRId64 ", %d bytes\n",
763 offset + pattern_offset, pattern_count);
764 }
765 g_free(cmp_buf);
766 }
767
768 if (qflag) {
769 goto out;
770 }
771
772 if (vflag) {
773 dump_buffer(buf, offset, count);
774 }
775
776 /* Finally, report back -- -C gives a parsable format */
777 t2 = tsub(t2, t1);
778 print_report("read", &t2, offset, count, total, cnt, Cflag);
779
780out:
781 qemu_io_free(buf);
782
783 return 0;
784}
785
786static void readv_help(void)
787{
788 printf(
789"\n"
790" reads a range of bytes from the given offset into multiple buffers\n"
791"\n"
792" Example:\n"
793" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
794"\n"
795" Reads a segment of the currently open file, optionally dumping it to the\n"
796" standard output stream (with -v option) for subsequent inspection.\n"
797" Uses multiple iovec buffers if more than one byte range is specified.\n"
798" -C, -- report statistics in a machine parsable format\n"
799" -P, -- use a pattern to verify read data\n"
800" -v, -- dump buffer to standard output\n"
801" -q, -- quiet mode, do not show I/O statistics\n"
802"\n");
803}
804
805static int readv_f(BlockDriverState *bs, int argc, char **argv);
806
807static const cmdinfo_t readv_cmd = {
808 .name = "readv",
809 .cfunc = readv_f,
810 .argmin = 2,
811 .argmax = -1,
812 .args = "[-Cqv] [-P pattern ] off len [len..]",
813 .oneline = "reads a number of bytes at a specified offset",
814 .help = readv_help,
815};
816
817static int readv_f(BlockDriverState *bs, int argc, char **argv)
818{
819 struct timeval t1, t2;
820 int Cflag = 0, qflag = 0, vflag = 0;
821 int c, cnt;
822 char *buf;
823 int64_t offset;
824 /* Some compilers get confused and warn if this is not initialized. */
825 int total = 0;
826 int nr_iov;
827 QEMUIOVector qiov;
828 int pattern = 0;
829 int Pflag = 0;
830
831 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
832 switch (c) {
833 case 'C':
834 Cflag = 1;
835 break;
836 case 'P':
837 Pflag = 1;
838 pattern = parse_pattern(optarg);
839 if (pattern < 0) {
840 return 0;
841 }
842 break;
843 case 'q':
844 qflag = 1;
845 break;
846 case 'v':
847 vflag = 1;
848 break;
849 default:
c2cdf5c5 850 return qemuio_command_usage(&readv_cmd);
797ac58c
KW
851 }
852 }
853
854 if (optind > argc - 2) {
c2cdf5c5 855 return qemuio_command_usage(&readv_cmd);
797ac58c
KW
856 }
857
858
859 offset = cvtnum(argv[optind]);
860 if (offset < 0) {
861 printf("non-numeric length argument -- %s\n", argv[optind]);
862 return 0;
863 }
864 optind++;
865
866 if (offset & 0x1ff) {
867 printf("offset %" PRId64 " is not sector aligned\n",
868 offset);
869 return 0;
870 }
871
872 nr_iov = argc - optind;
873 buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, 0xab);
874 if (buf == NULL) {
875 return 0;
876 }
877
878 gettimeofday(&t1, NULL);
879 cnt = do_aio_readv(bs, &qiov, offset, &total);
880 gettimeofday(&t2, NULL);
881
882 if (cnt < 0) {
883 printf("readv failed: %s\n", strerror(-cnt));
884 goto out;
885 }
886
887 if (Pflag) {
888 void *cmp_buf = g_malloc(qiov.size);
889 memset(cmp_buf, pattern, qiov.size);
890 if (memcmp(buf, cmp_buf, qiov.size)) {
891 printf("Pattern verification failed at offset %"
892 PRId64 ", %zd bytes\n", offset, qiov.size);
893 }
894 g_free(cmp_buf);
895 }
896
897 if (qflag) {
898 goto out;
899 }
900
901 if (vflag) {
902 dump_buffer(buf, offset, qiov.size);
903 }
904
905 /* Finally, report back -- -C gives a parsable format */
906 t2 = tsub(t2, t1);
907 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
908
909out:
910 qemu_iovec_destroy(&qiov);
911 qemu_io_free(buf);
912 return 0;
913}
914
915static void write_help(void)
916{
917 printf(
918"\n"
919" writes a range of bytes from the given offset\n"
920"\n"
921" Example:\n"
922" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
923"\n"
924" Writes into a segment of the currently open file, using a buffer\n"
925" filled with a set pattern (0xcdcdcdcd).\n"
926" -b, -- write to the VM state rather than the virtual disk\n"
927" -c, -- write compressed data with bdrv_write_compressed\n"
928" -p, -- use bdrv_pwrite to write the file\n"
929" -P, -- use different pattern to fill file\n"
930" -C, -- report statistics in a machine parsable format\n"
931" -q, -- quiet mode, do not show I/O statistics\n"
932" -z, -- write zeroes using bdrv_co_write_zeroes\n"
933"\n");
934}
935
936static int write_f(BlockDriverState *bs, int argc, char **argv);
937
938static const cmdinfo_t write_cmd = {
939 .name = "write",
940 .altname = "w",
941 .cfunc = write_f,
942 .argmin = 2,
943 .argmax = -1,
944 .args = "[-bcCpqz] [-P pattern ] off len",
945 .oneline = "writes a number of bytes at a specified offset",
946 .help = write_help,
947};
948
949static int write_f(BlockDriverState *bs, int argc, char **argv)
950{
951 struct timeval t1, t2;
952 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
953 int cflag = 0;
954 int c, cnt;
955 char *buf = NULL;
956 int64_t offset;
957 int count;
958 /* Some compilers get confused and warn if this is not initialized. */
959 int total = 0;
960 int pattern = 0xcd;
961
962 while ((c = getopt(argc, argv, "bcCpP:qz")) != EOF) {
963 switch (c) {
964 case 'b':
965 bflag = 1;
966 break;
967 case 'c':
968 cflag = 1;
969 break;
970 case 'C':
971 Cflag = 1;
972 break;
973 case 'p':
974 pflag = 1;
975 break;
976 case 'P':
977 Pflag = 1;
978 pattern = parse_pattern(optarg);
979 if (pattern < 0) {
980 return 0;
981 }
982 break;
983 case 'q':
984 qflag = 1;
985 break;
986 case 'z':
987 zflag = 1;
988 break;
989 default:
c2cdf5c5 990 return qemuio_command_usage(&write_cmd);
797ac58c
KW
991 }
992 }
993
994 if (optind != argc - 2) {
c2cdf5c5 995 return qemuio_command_usage(&write_cmd);
797ac58c
KW
996 }
997
998 if (bflag + pflag + zflag > 1) {
999 printf("-b, -p, or -z cannot be specified at the same time\n");
1000 return 0;
1001 }
1002
1003 if (zflag && Pflag) {
1004 printf("-z and -P cannot be specified at the same time\n");
1005 return 0;
1006 }
1007
1008 offset = cvtnum(argv[optind]);
1009 if (offset < 0) {
1010 printf("non-numeric length argument -- %s\n", argv[optind]);
1011 return 0;
1012 }
1013
1014 optind++;
1015 count = cvtnum(argv[optind]);
1016 if (count < 0) {
1017 printf("non-numeric length argument -- %s\n", argv[optind]);
1018 return 0;
1019 }
1020
1021 if (!pflag) {
1022 if (offset & 0x1ff) {
1023 printf("offset %" PRId64 " is not sector aligned\n",
1024 offset);
1025 return 0;
1026 }
1027
1028 if (count & 0x1ff) {
1029 printf("count %d is not sector aligned\n",
1030 count);
1031 return 0;
1032 }
1033 }
1034
1035 if (!zflag) {
1036 buf = qemu_io_alloc(bs, count, pattern);
1037 }
1038
1039 gettimeofday(&t1, NULL);
1040 if (pflag) {
1041 cnt = do_pwrite(bs, buf, offset, count, &total);
1042 } else if (bflag) {
1043 cnt = do_save_vmstate(bs, buf, offset, count, &total);
1044 } else if (zflag) {
1045 cnt = do_co_write_zeroes(bs, offset, count, &total);
1046 } else if (cflag) {
1047 cnt = do_write_compressed(bs, buf, offset, count, &total);
1048 } else {
1049 cnt = do_write(bs, buf, offset, count, &total);
1050 }
1051 gettimeofday(&t2, NULL);
1052
1053 if (cnt < 0) {
1054 printf("write failed: %s\n", strerror(-cnt));
1055 goto out;
1056 }
1057
1058 if (qflag) {
1059 goto out;
1060 }
1061
1062 /* Finally, report back -- -C gives a parsable format */
1063 t2 = tsub(t2, t1);
1064 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
1065
1066out:
1067 if (!zflag) {
1068 qemu_io_free(buf);
1069 }
1070
1071 return 0;
1072}
1073
1074static void
1075writev_help(void)
1076{
1077 printf(
1078"\n"
1079" writes a range of bytes from the given offset source from multiple buffers\n"
1080"\n"
1081" Example:\n"
6e6507c0 1082" 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
797ac58c
KW
1083"\n"
1084" Writes into a segment of the currently open file, using a buffer\n"
1085" filled with a set pattern (0xcdcdcdcd).\n"
1086" -P, -- use different pattern to fill file\n"
1087" -C, -- report statistics in a machine parsable format\n"
1088" -q, -- quiet mode, do not show I/O statistics\n"
1089"\n");
1090}
1091
1092static int writev_f(BlockDriverState *bs, int argc, char **argv);
1093
1094static const cmdinfo_t writev_cmd = {
1095 .name = "writev",
1096 .cfunc = writev_f,
1097 .argmin = 2,
1098 .argmax = -1,
1099 .args = "[-Cq] [-P pattern ] off len [len..]",
1100 .oneline = "writes a number of bytes at a specified offset",
1101 .help = writev_help,
1102};
1103
1104static int writev_f(BlockDriverState *bs, int argc, char **argv)
1105{
1106 struct timeval t1, t2;
1107 int Cflag = 0, qflag = 0;
1108 int c, cnt;
1109 char *buf;
1110 int64_t offset;
1111 /* Some compilers get confused and warn if this is not initialized. */
1112 int total = 0;
1113 int nr_iov;
1114 int pattern = 0xcd;
1115 QEMUIOVector qiov;
1116
1117 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1118 switch (c) {
1119 case 'C':
1120 Cflag = 1;
1121 break;
1122 case 'q':
1123 qflag = 1;
1124 break;
1125 case 'P':
1126 pattern = parse_pattern(optarg);
1127 if (pattern < 0) {
1128 return 0;
1129 }
1130 break;
1131 default:
c2cdf5c5 1132 return qemuio_command_usage(&writev_cmd);
797ac58c
KW
1133 }
1134 }
1135
1136 if (optind > argc - 2) {
c2cdf5c5 1137 return qemuio_command_usage(&writev_cmd);
797ac58c
KW
1138 }
1139
1140 offset = cvtnum(argv[optind]);
1141 if (offset < 0) {
1142 printf("non-numeric length argument -- %s\n", argv[optind]);
1143 return 0;
1144 }
1145 optind++;
1146
1147 if (offset & 0x1ff) {
1148 printf("offset %" PRId64 " is not sector aligned\n",
1149 offset);
1150 return 0;
1151 }
1152
1153 nr_iov = argc - optind;
1154 buf = create_iovec(bs, &qiov, &argv[optind], nr_iov, pattern);
1155 if (buf == NULL) {
1156 return 0;
1157 }
1158
1159 gettimeofday(&t1, NULL);
1160 cnt = do_aio_writev(bs, &qiov, offset, &total);
1161 gettimeofday(&t2, NULL);
1162
1163 if (cnt < 0) {
1164 printf("writev failed: %s\n", strerror(-cnt));
1165 goto out;
1166 }
1167
1168 if (qflag) {
1169 goto out;
1170 }
1171
1172 /* Finally, report back -- -C gives a parsable format */
1173 t2 = tsub(t2, t1);
1174 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
1175out:
1176 qemu_iovec_destroy(&qiov);
1177 qemu_io_free(buf);
1178 return 0;
1179}
1180
1181static void multiwrite_help(void)
1182{
1183 printf(
1184"\n"
1185" writes a range of bytes from the given offset source from multiple buffers,\n"
1186" in a batch of requests that may be merged by qemu\n"
1187"\n"
1188" Example:\n"
1189" 'multiwrite 512 1k 1k ; 4k 1k'\n"
1190" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
1191"\n"
1192" Writes into a segment of the currently open file, using a buffer\n"
1193" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
1194" by one for each request contained in the multiwrite command.\n"
1195" -P, -- use different pattern to fill file\n"
1196" -C, -- report statistics in a machine parsable format\n"
1197" -q, -- quiet mode, do not show I/O statistics\n"
1198"\n");
1199}
1200
1201static int multiwrite_f(BlockDriverState *bs, int argc, char **argv);
1202
1203static const cmdinfo_t multiwrite_cmd = {
1204 .name = "multiwrite",
1205 .cfunc = multiwrite_f,
1206 .argmin = 2,
1207 .argmax = -1,
1208 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
1209 .oneline = "issues multiple write requests at once",
1210 .help = multiwrite_help,
1211};
1212
1213static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
1214{
1215 struct timeval t1, t2;
1216 int Cflag = 0, qflag = 0;
1217 int c, cnt;
1218 char **buf;
1219 int64_t offset, first_offset = 0;
1220 /* Some compilers get confused and warn if this is not initialized. */
1221 int total = 0;
1222 int nr_iov;
1223 int nr_reqs;
1224 int pattern = 0xcd;
1225 QEMUIOVector *qiovs;
1226 int i;
1227 BlockRequest *reqs;
1228
1229 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1230 switch (c) {
1231 case 'C':
1232 Cflag = 1;
1233 break;
1234 case 'q':
1235 qflag = 1;
1236 break;
1237 case 'P':
1238 pattern = parse_pattern(optarg);
1239 if (pattern < 0) {
1240 return 0;
1241 }
1242 break;
1243 default:
c2cdf5c5 1244 return qemuio_command_usage(&writev_cmd);
797ac58c
KW
1245 }
1246 }
1247
1248 if (optind > argc - 2) {
c2cdf5c5 1249 return qemuio_command_usage(&writev_cmd);
797ac58c
KW
1250 }
1251
1252 nr_reqs = 1;
1253 for (i = optind; i < argc; i++) {
1254 if (!strcmp(argv[i], ";")) {
1255 nr_reqs++;
1256 }
1257 }
1258
02c4f26b
MA
1259 reqs = g_new0(BlockRequest, nr_reqs);
1260 buf = g_new0(char *, nr_reqs);
1261 qiovs = g_new(QEMUIOVector, nr_reqs);
797ac58c
KW
1262
1263 for (i = 0; i < nr_reqs && optind < argc; i++) {
1264 int j;
1265
1266 /* Read the offset of the request */
1267 offset = cvtnum(argv[optind]);
1268 if (offset < 0) {
1269 printf("non-numeric offset argument -- %s\n", argv[optind]);
1270 goto out;
1271 }
1272 optind++;
1273
1274 if (offset & 0x1ff) {
1275 printf("offset %lld is not sector aligned\n",
1276 (long long)offset);
1277 goto out;
1278 }
1279
1280 if (i == 0) {
1281 first_offset = offset;
1282 }
1283
1284 /* Read lengths for qiov entries */
1285 for (j = optind; j < argc; j++) {
1286 if (!strcmp(argv[j], ";")) {
1287 break;
1288 }
1289 }
1290
1291 nr_iov = j - optind;
1292
1293 /* Build request */
1294 buf[i] = create_iovec(bs, &qiovs[i], &argv[optind], nr_iov, pattern);
1295 if (buf[i] == NULL) {
1296 goto out;
1297 }
1298
1299 reqs[i].qiov = &qiovs[i];
1300 reqs[i].sector = offset >> 9;
1301 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
1302
1303 optind = j + 1;
1304
1305 pattern++;
1306 }
1307
1308 /* If there were empty requests at the end, ignore them */
1309 nr_reqs = i;
1310
1311 gettimeofday(&t1, NULL);
1312 cnt = do_aio_multiwrite(bs, reqs, nr_reqs, &total);
1313 gettimeofday(&t2, NULL);
1314
1315 if (cnt < 0) {
1316 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1317 goto out;
1318 }
1319
1320 if (qflag) {
1321 goto out;
1322 }
1323
1324 /* Finally, report back -- -C gives a parsable format */
1325 t2 = tsub(t2, t1);
1326 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
1327out:
1328 for (i = 0; i < nr_reqs; i++) {
1329 qemu_io_free(buf[i]);
1330 if (reqs[i].qiov != NULL) {
1331 qemu_iovec_destroy(&qiovs[i]);
1332 }
1333 }
1334 g_free(buf);
1335 g_free(reqs);
1336 g_free(qiovs);
1337 return 0;
1338}
1339
1340struct aio_ctx {
a91f9584 1341 BlockDriverState *bs;
797ac58c
KW
1342 QEMUIOVector qiov;
1343 int64_t offset;
1344 char *buf;
1345 int qflag;
1346 int vflag;
1347 int Cflag;
1348 int Pflag;
a91f9584 1349 BlockAcctCookie acct;
797ac58c
KW
1350 int pattern;
1351 struct timeval t1;
1352};
1353
1354static void aio_write_done(void *opaque, int ret)
1355{
1356 struct aio_ctx *ctx = opaque;
1357 struct timeval t2;
1358
1359 gettimeofday(&t2, NULL);
1360
1361
1362 if (ret < 0) {
1363 printf("aio_write failed: %s\n", strerror(-ret));
1364 goto out;
1365 }
1366
a91f9584
FZ
1367 block_acct_done(&ctx->bs->stats, &ctx->acct);
1368
797ac58c
KW
1369 if (ctx->qflag) {
1370 goto out;
1371 }
1372
1373 /* Finally, report back -- -C gives a parsable format */
1374 t2 = tsub(t2, ctx->t1);
1375 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1376 ctx->qiov.size, 1, ctx->Cflag);
1377out:
1378 qemu_io_free(ctx->buf);
1379 qemu_iovec_destroy(&ctx->qiov);
1380 g_free(ctx);
1381}
1382
1383static void aio_read_done(void *opaque, int ret)
1384{
1385 struct aio_ctx *ctx = opaque;
1386 struct timeval t2;
1387
1388 gettimeofday(&t2, NULL);
1389
1390 if (ret < 0) {
1391 printf("readv failed: %s\n", strerror(-ret));
1392 goto out;
1393 }
1394
1395 if (ctx->Pflag) {
1396 void *cmp_buf = g_malloc(ctx->qiov.size);
1397
1398 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1399 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1400 printf("Pattern verification failed at offset %"
1401 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1402 }
1403 g_free(cmp_buf);
1404 }
1405
a91f9584
FZ
1406 block_acct_done(&ctx->bs->stats, &ctx->acct);
1407
797ac58c
KW
1408 if (ctx->qflag) {
1409 goto out;
1410 }
1411
1412 if (ctx->vflag) {
1413 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1414 }
1415
1416 /* Finally, report back -- -C gives a parsable format */
1417 t2 = tsub(t2, ctx->t1);
1418 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1419 ctx->qiov.size, 1, ctx->Cflag);
1420out:
1421 qemu_io_free(ctx->buf);
1422 qemu_iovec_destroy(&ctx->qiov);
1423 g_free(ctx);
1424}
1425
1426static void aio_read_help(void)
1427{
1428 printf(
1429"\n"
1430" asynchronously reads a range of bytes from the given offset\n"
1431"\n"
1432" Example:\n"
1433" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1434"\n"
1435" Reads a segment of the currently open file, optionally dumping it to the\n"
1436" standard output stream (with -v option) for subsequent inspection.\n"
1437" The read is performed asynchronously and the aio_flush command must be\n"
1438" used to ensure all outstanding aio requests have been completed.\n"
1439" -C, -- report statistics in a machine parsable format\n"
1440" -P, -- use a pattern to verify read data\n"
1441" -v, -- dump buffer to standard output\n"
1442" -q, -- quiet mode, do not show I/O statistics\n"
1443"\n");
1444}
1445
1446static int aio_read_f(BlockDriverState *bs, int argc, char **argv);
1447
1448static const cmdinfo_t aio_read_cmd = {
1449 .name = "aio_read",
1450 .cfunc = aio_read_f,
1451 .argmin = 2,
1452 .argmax = -1,
1453 .args = "[-Cqv] [-P pattern ] off len [len..]",
1454 .oneline = "asynchronously reads a number of bytes",
1455 .help = aio_read_help,
1456};
1457
1458static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
1459{
1460 int nr_iov, c;
1461 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1462
a91f9584 1463 ctx->bs = bs;
797ac58c
KW
1464 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1465 switch (c) {
1466 case 'C':
1467 ctx->Cflag = 1;
1468 break;
1469 case 'P':
1470 ctx->Pflag = 1;
1471 ctx->pattern = parse_pattern(optarg);
1472 if (ctx->pattern < 0) {
1473 g_free(ctx);
1474 return 0;
1475 }
1476 break;
1477 case 'q':
1478 ctx->qflag = 1;
1479 break;
1480 case 'v':
1481 ctx->vflag = 1;
1482 break;
1483 default:
1484 g_free(ctx);
c2cdf5c5 1485 return qemuio_command_usage(&aio_read_cmd);
797ac58c
KW
1486 }
1487 }
1488
1489 if (optind > argc - 2) {
1490 g_free(ctx);
c2cdf5c5 1491 return qemuio_command_usage(&aio_read_cmd);
797ac58c
KW
1492 }
1493
1494 ctx->offset = cvtnum(argv[optind]);
1495 if (ctx->offset < 0) {
1496 printf("non-numeric length argument -- %s\n", argv[optind]);
1497 g_free(ctx);
1498 return 0;
1499 }
1500 optind++;
1501
1502 if (ctx->offset & 0x1ff) {
1503 printf("offset %" PRId64 " is not sector aligned\n",
1504 ctx->offset);
1505 g_free(ctx);
1506 return 0;
1507 }
1508
1509 nr_iov = argc - optind;
1510 ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, 0xab);
1511 if (ctx->buf == NULL) {
1512 g_free(ctx);
1513 return 0;
1514 }
1515
1516 gettimeofday(&ctx->t1, NULL);
a91f9584 1517 block_acct_start(&bs->stats, &ctx->acct, ctx->qiov.size, BLOCK_ACCT_READ);
797ac58c
KW
1518 bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1519 ctx->qiov.size >> 9, aio_read_done, ctx);
1520 return 0;
1521}
1522
1523static void aio_write_help(void)
1524{
1525 printf(
1526"\n"
1527" asynchronously writes a range of bytes from the given offset source\n"
1528" from multiple buffers\n"
1529"\n"
1530" Example:\n"
1531" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1532"\n"
1533" Writes into a segment of the currently open file, using a buffer\n"
1534" filled with a set pattern (0xcdcdcdcd).\n"
1535" The write is performed asynchronously and the aio_flush command must be\n"
1536" used to ensure all outstanding aio requests have been completed.\n"
1537" -P, -- use different pattern to fill file\n"
1538" -C, -- report statistics in a machine parsable format\n"
1539" -q, -- quiet mode, do not show I/O statistics\n"
1540"\n");
1541}
1542
1543static int aio_write_f(BlockDriverState *bs, int argc, char **argv);
1544
1545static const cmdinfo_t aio_write_cmd = {
1546 .name = "aio_write",
1547 .cfunc = aio_write_f,
1548 .argmin = 2,
1549 .argmax = -1,
1550 .args = "[-Cq] [-P pattern ] off len [len..]",
1551 .oneline = "asynchronously writes a number of bytes",
1552 .help = aio_write_help,
1553};
1554
1555static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
1556{
1557 int nr_iov, c;
1558 int pattern = 0xcd;
1559 struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
1560
a91f9584 1561 ctx->bs = bs;
797ac58c
KW
1562 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1563 switch (c) {
1564 case 'C':
1565 ctx->Cflag = 1;
1566 break;
1567 case 'q':
1568 ctx->qflag = 1;
1569 break;
1570 case 'P':
1571 pattern = parse_pattern(optarg);
1572 if (pattern < 0) {
1573 g_free(ctx);
1574 return 0;
1575 }
1576 break;
1577 default:
1578 g_free(ctx);
c2cdf5c5 1579 return qemuio_command_usage(&aio_write_cmd);
797ac58c
KW
1580 }
1581 }
1582
1583 if (optind > argc - 2) {
1584 g_free(ctx);
c2cdf5c5 1585 return qemuio_command_usage(&aio_write_cmd);
797ac58c
KW
1586 }
1587
1588 ctx->offset = cvtnum(argv[optind]);
1589 if (ctx->offset < 0) {
1590 printf("non-numeric length argument -- %s\n", argv[optind]);
1591 g_free(ctx);
1592 return 0;
1593 }
1594 optind++;
1595
1596 if (ctx->offset & 0x1ff) {
1597 printf("offset %" PRId64 " is not sector aligned\n",
1598 ctx->offset);
1599 g_free(ctx);
1600 return 0;
1601 }
1602
1603 nr_iov = argc - optind;
1604 ctx->buf = create_iovec(bs, &ctx->qiov, &argv[optind], nr_iov, pattern);
1605 if (ctx->buf == NULL) {
1606 g_free(ctx);
1607 return 0;
1608 }
1609
1610 gettimeofday(&ctx->t1, NULL);
a91f9584 1611 block_acct_start(&bs->stats, &ctx->acct, ctx->qiov.size, BLOCK_ACCT_WRITE);
797ac58c
KW
1612 bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1613 ctx->qiov.size >> 9, aio_write_done, ctx);
1614 return 0;
1615}
1616
1617static int aio_flush_f(BlockDriverState *bs, int argc, char **argv)
1618{
1619 bdrv_drain_all();
1620 return 0;
1621}
1622
1623static const cmdinfo_t aio_flush_cmd = {
1624 .name = "aio_flush",
1625 .cfunc = aio_flush_f,
1626 .oneline = "completes all outstanding aio requests"
1627};
1628
1629static int flush_f(BlockDriverState *bs, int argc, char **argv)
1630{
1631 bdrv_flush(bs);
1632 return 0;
1633}
1634
1635static const cmdinfo_t flush_cmd = {
1636 .name = "flush",
1637 .altname = "f",
1638 .cfunc = flush_f,
1639 .oneline = "flush all in-core file state to disk",
1640};
1641
1642static int truncate_f(BlockDriverState *bs, int argc, char **argv)
1643{
1644 int64_t offset;
1645 int ret;
1646
1647 offset = cvtnum(argv[1]);
1648 if (offset < 0) {
1649 printf("non-numeric truncate argument -- %s\n", argv[1]);
1650 return 0;
1651 }
1652
1653 ret = bdrv_truncate(bs, offset);
1654 if (ret < 0) {
1655 printf("truncate: %s\n", strerror(-ret));
1656 return 0;
1657 }
1658
1659 return 0;
1660}
1661
1662static const cmdinfo_t truncate_cmd = {
1663 .name = "truncate",
1664 .altname = "t",
1665 .cfunc = truncate_f,
1666 .argmin = 1,
1667 .argmax = 1,
1668 .args = "off",
1669 .oneline = "truncates the current file at the given offset",
1670};
1671
1672static int length_f(BlockDriverState *bs, int argc, char **argv)
1673{
1674 int64_t size;
1675 char s1[64];
1676
1677 size = bdrv_getlength(bs);
1678 if (size < 0) {
1679 printf("getlength: %s\n", strerror(-size));
1680 return 0;
1681 }
1682
1683 cvtstr(size, s1, sizeof(s1));
1684 printf("%s\n", s1);
1685 return 0;
1686}
1687
1688
1689static const cmdinfo_t length_cmd = {
1690 .name = "length",
1691 .altname = "l",
1692 .cfunc = length_f,
1693 .oneline = "gets the length of the current file",
1694};
1695
1696
1697static int info_f(BlockDriverState *bs, int argc, char **argv)
1698{
1699 BlockDriverInfo bdi;
a8d8ecb7 1700 ImageInfoSpecific *spec_info;
797ac58c
KW
1701 char s1[64], s2[64];
1702 int ret;
1703
1704 if (bs->drv && bs->drv->format_name) {
1705 printf("format name: %s\n", bs->drv->format_name);
1706 }
1707 if (bs->drv && bs->drv->protocol_name) {
1708 printf("format name: %s\n", bs->drv->protocol_name);
1709 }
1710
1711 ret = bdrv_get_info(bs, &bdi);
1712 if (ret) {
1713 return 0;
1714 }
1715
1716 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1717 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
1718
1719 printf("cluster size: %s\n", s1);
1720 printf("vm state offset: %s\n", s2);
1721
a8d8ecb7
HR
1722 spec_info = bdrv_get_specific_info(bs);
1723 if (spec_info) {
1724 printf("Format specific information:\n");
1725 bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
1726 qapi_free_ImageInfoSpecific(spec_info);
1727 }
1728
797ac58c
KW
1729 return 0;
1730}
1731
1732
1733
1734static const cmdinfo_t info_cmd = {
1735 .name = "info",
1736 .altname = "i",
1737 .cfunc = info_f,
1738 .oneline = "prints information about the current file",
1739};
1740
1741static void discard_help(void)
1742{
1743 printf(
1744"\n"
1745" discards a range of bytes from the given offset\n"
1746"\n"
1747" Example:\n"
1748" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1749"\n"
1750" Discards a segment of the currently open file.\n"
1751" -C, -- report statistics in a machine parsable format\n"
1752" -q, -- quiet mode, do not show I/O statistics\n"
1753"\n");
1754}
1755
1756static int discard_f(BlockDriverState *bs, int argc, char **argv);
1757
1758static const cmdinfo_t discard_cmd = {
1759 .name = "discard",
1760 .altname = "d",
1761 .cfunc = discard_f,
1762 .argmin = 2,
1763 .argmax = -1,
1764 .args = "[-Cq] off len",
1765 .oneline = "discards a number of bytes at a specified offset",
1766 .help = discard_help,
1767};
1768
1769static int discard_f(BlockDriverState *bs, int argc, char **argv)
1770{
1771 struct timeval t1, t2;
1772 int Cflag = 0, qflag = 0;
1773 int c, ret;
1774 int64_t offset;
1775 int count;
1776
1777 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1778 switch (c) {
1779 case 'C':
1780 Cflag = 1;
1781 break;
1782 case 'q':
1783 qflag = 1;
1784 break;
1785 default:
c2cdf5c5 1786 return qemuio_command_usage(&discard_cmd);
797ac58c
KW
1787 }
1788 }
1789
1790 if (optind != argc - 2) {
c2cdf5c5 1791 return qemuio_command_usage(&discard_cmd);
797ac58c
KW
1792 }
1793
1794 offset = cvtnum(argv[optind]);
1795 if (offset < 0) {
1796 printf("non-numeric length argument -- %s\n", argv[optind]);
1797 return 0;
1798 }
1799
1800 optind++;
1801 count = cvtnum(argv[optind]);
1802 if (count < 0) {
1803 printf("non-numeric length argument -- %s\n", argv[optind]);
1804 return 0;
1805 }
1806
1807 gettimeofday(&t1, NULL);
1808 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1809 count >> BDRV_SECTOR_BITS);
1810 gettimeofday(&t2, NULL);
1811
1812 if (ret < 0) {
1813 printf("discard failed: %s\n", strerror(-ret));
1814 goto out;
1815 }
1816
1817 /* Finally, report back -- -C gives a parsable format */
1818 if (!qflag) {
1819 t2 = tsub(t2, t1);
1820 print_report("discard", &t2, offset, count, count, 1, Cflag);
1821 }
1822
1823out:
1824 return 0;
1825}
1826
1827static int alloc_f(BlockDriverState *bs, int argc, char **argv)
1828{
1829 int64_t offset, sector_num;
1830 int nb_sectors, remaining;
1831 char s1[64];
1832 int num, sum_alloc;
1833 int ret;
1834
1835 offset = cvtnum(argv[1]);
1836 if (offset < 0) {
1837 printf("non-numeric offset argument -- %s\n", argv[1]);
1838 return 0;
1839 } else if (offset & 0x1ff) {
1840 printf("offset %" PRId64 " is not sector aligned\n",
1841 offset);
1842 return 0;
1843 }
1844
1845 if (argc == 3) {
1846 nb_sectors = cvtnum(argv[2]);
1847 if (nb_sectors < 0) {
1848 printf("non-numeric length argument -- %s\n", argv[2]);
1849 return 0;
1850 }
1851 } else {
1852 nb_sectors = 1;
1853 }
1854
1855 remaining = nb_sectors;
1856 sum_alloc = 0;
1857 sector_num = offset >> 9;
1858 while (remaining) {
1859 ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
d663640c
PB
1860 if (ret < 0) {
1861 printf("is_allocated failed: %s\n", strerror(-ret));
1862 return 0;
1863 }
797ac58c
KW
1864 sector_num += num;
1865 remaining -= num;
1866 if (ret) {
1867 sum_alloc += num;
1868 }
1869 if (num == 0) {
1870 nb_sectors -= remaining;
1871 remaining = 0;
1872 }
1873 }
1874
1875 cvtstr(offset, s1, sizeof(s1));
1876
1877 printf("%d/%d sectors allocated at offset %s\n",
1878 sum_alloc, nb_sectors, s1);
1879 return 0;
1880}
1881
1882static const cmdinfo_t alloc_cmd = {
1883 .name = "alloc",
1884 .altname = "a",
1885 .argmin = 1,
1886 .argmax = 2,
1887 .cfunc = alloc_f,
1888 .args = "off [sectors]",
1889 .oneline = "checks if a sector is present in the file",
1890};
1891
1892
1893static int map_is_allocated(BlockDriverState *bs, int64_t sector_num,
1894 int64_t nb_sectors, int64_t *pnum)
1895{
1896 int num, num_checked;
1897 int ret, firstret;
1898
1899 num_checked = MIN(nb_sectors, INT_MAX);
1900 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
1901 if (ret < 0) {
1902 return ret;
1903 }
1904
1905 firstret = ret;
1906 *pnum = num;
1907
1908 while (nb_sectors > 0 && ret == firstret) {
1909 sector_num += num;
1910 nb_sectors -= num;
1911
1912 num_checked = MIN(nb_sectors, INT_MAX);
1913 ret = bdrv_is_allocated(bs, sector_num, num_checked, &num);
4b25bbc4 1914 if (ret == firstret && num) {
797ac58c
KW
1915 *pnum += num;
1916 } else {
1917 break;
1918 }
1919 }
1920
1921 return firstret;
1922}
1923
1924static int map_f(BlockDriverState *bs, int argc, char **argv)
1925{
1926 int64_t offset;
1927 int64_t nb_sectors;
1928 char s1[64];
1929 int64_t num;
1930 int ret;
1931 const char *retstr;
1932
1933 offset = 0;
1934 nb_sectors = bs->total_sectors;
1935
1936 do {
1937 ret = map_is_allocated(bs, offset, nb_sectors, &num);
1938 if (ret < 0) {
1939 error_report("Failed to get allocation status: %s", strerror(-ret));
1940 return 0;
4b25bbc4
HR
1941 } else if (!num) {
1942 error_report("Unexpected end of image");
1943 return 0;
797ac58c
KW
1944 }
1945
1946 retstr = ret ? " allocated" : "not allocated";
1947 cvtstr(offset << 9ULL, s1, sizeof(s1));
1948 printf("[% 24" PRId64 "] % 8" PRId64 "/% 8" PRId64 " sectors %s "
1949 "at offset %s (%d)\n",
1950 offset << 9ULL, num, nb_sectors, retstr, s1, ret);
1951
1952 offset += num;
1953 nb_sectors -= num;
1954 } while (offset < bs->total_sectors);
1955
1956 return 0;
1957}
1958
1959static const cmdinfo_t map_cmd = {
1960 .name = "map",
1961 .argmin = 0,
1962 .argmax = 0,
1963 .cfunc = map_f,
1964 .args = "",
1965 .oneline = "prints the allocated areas of a file",
1966};
1967
1968static int break_f(BlockDriverState *bs, int argc, char **argv)
1969{
1970 int ret;
1971
1972 ret = bdrv_debug_breakpoint(bs, argv[1], argv[2]);
1973 if (ret < 0) {
1974 printf("Could not set breakpoint: %s\n", strerror(-ret));
1975 }
1976
1977 return 0;
1978}
1979
4cc70e93
FZ
1980static int remove_break_f(BlockDriverState *bs, int argc, char **argv)
1981{
1982 int ret;
1983
1984 ret = bdrv_debug_remove_breakpoint(bs, argv[1]);
1985 if (ret < 0) {
1986 printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
1987 }
1988
1989 return 0;
1990}
1991
797ac58c
KW
1992static const cmdinfo_t break_cmd = {
1993 .name = "break",
1994 .argmin = 2,
1995 .argmax = 2,
1996 .cfunc = break_f,
1997 .args = "event tag",
1998 .oneline = "sets a breakpoint on event and tags the stopped "
1999 "request as tag",
2000};
2001
4cc70e93
FZ
2002static const cmdinfo_t remove_break_cmd = {
2003 .name = "remove_break",
2004 .argmin = 1,
2005 .argmax = 1,
2006 .cfunc = remove_break_f,
2007 .args = "tag",
2008 .oneline = "remove a breakpoint by tag",
2009};
2010
797ac58c
KW
2011static int resume_f(BlockDriverState *bs, int argc, char **argv)
2012{
2013 int ret;
2014
2015 ret = bdrv_debug_resume(bs, argv[1]);
2016 if (ret < 0) {
2017 printf("Could not resume request: %s\n", strerror(-ret));
2018 }
2019
2020 return 0;
2021}
2022
2023static const cmdinfo_t resume_cmd = {
2024 .name = "resume",
2025 .argmin = 1,
2026 .argmax = 1,
2027 .cfunc = resume_f,
2028 .args = "tag",
2029 .oneline = "resumes the request tagged as tag",
2030};
2031
2032static int wait_break_f(BlockDriverState *bs, int argc, char **argv)
2033{
2034 while (!bdrv_debug_is_suspended(bs, argv[1])) {
b47ec2c4 2035 aio_poll(bdrv_get_aio_context(bs), true);
797ac58c
KW
2036 }
2037
2038 return 0;
2039}
2040
2041static const cmdinfo_t wait_break_cmd = {
2042 .name = "wait_break",
2043 .argmin = 1,
2044 .argmax = 1,
2045 .cfunc = wait_break_f,
2046 .args = "tag",
2047 .oneline = "waits for the suspension of a request",
2048};
2049
2050static int abort_f(BlockDriverState *bs, int argc, char **argv)
2051{
2052 abort();
2053}
2054
2055static const cmdinfo_t abort_cmd = {
2056 .name = "abort",
2057 .cfunc = abort_f,
2058 .flags = CMD_NOFILE_OK,
2059 .oneline = "simulate a program crash using abort(3)",
2060};
2061
0e82dc7b
HR
2062static void sigraise_help(void)
2063{
2064 printf(
2065"\n"
2066" raises the given signal\n"
2067"\n"
2068" Example:\n"
2069" 'sigraise %i' - raises SIGTERM\n"
2070"\n"
2071" Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
2072" given to sigraise.\n"
2073"\n", SIGTERM);
2074}
2075
2076static int sigraise_f(BlockDriverState *bs, int argc, char **argv);
2077
2078static const cmdinfo_t sigraise_cmd = {
2079 .name = "sigraise",
2080 .cfunc = sigraise_f,
2081 .argmin = 1,
2082 .argmax = 1,
2083 .flags = CMD_NOFILE_OK,
2084 .args = "signal",
2085 .oneline = "raises a signal",
2086 .help = sigraise_help,
2087};
2088
2089static int sigraise_f(BlockDriverState *bs, int argc, char **argv)
2090{
2091 int sig = cvtnum(argv[1]);
2092 if (sig < 0) {
2093 printf("non-numeric signal number argument -- %s\n", argv[1]);
2094 return 0;
2095 }
2096
2097 /* Using raise() to kill this process does not necessarily flush all open
2098 * streams. At least stdout and stderr (although the latter should be
2099 * non-buffered anyway) should be flushed, though. */
2100 fflush(stdout);
2101 fflush(stderr);
2102
2103 raise(sig);
2104 return 0;
2105}
2106
cd33d02a
KW
2107static void sleep_cb(void *opaque)
2108{
2109 bool *expired = opaque;
2110 *expired = true;
2111}
2112
2113static int sleep_f(BlockDriverState *bs, int argc, char **argv)
2114{
2115 char *endptr;
2116 long ms;
2117 struct QEMUTimer *timer;
2118 bool expired = false;
2119
2120 ms = strtol(argv[1], &endptr, 0);
2121 if (ms < 0 || *endptr != '\0') {
2122 printf("%s is not a valid number\n", argv[1]);
2123 return 0;
2124 }
2125
2126 timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
2127 timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
2128
2129 while (!expired) {
2130 main_loop_wait(false);
2131 }
2132
2133 timer_free(timer);
2134
2135 return 0;
2136}
2137
2138static const cmdinfo_t sleep_cmd = {
2139 .name = "sleep",
2140 .argmin = 1,
2141 .argmax = 1,
2142 .cfunc = sleep_f,
2143 .flags = CMD_NOFILE_OK,
2144 .oneline = "waits for the given value in milliseconds",
2145};
2146
f18a834a
KW
2147static void help_oneline(const char *cmd, const cmdinfo_t *ct)
2148{
2149 if (cmd) {
2150 printf("%s ", cmd);
2151 } else {
2152 printf("%s ", ct->name);
2153 if (ct->altname) {
2154 printf("(or %s) ", ct->altname);
2155 }
2156 }
2157
2158 if (ct->args) {
2159 printf("%s ", ct->args);
2160 }
2161 printf("-- %s\n", ct->oneline);
2162}
2163
2164static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
2165{
2166 help_oneline(cmd, ct);
2167 if (ct->help) {
2168 ct->help();
2169 }
2170}
2171
2172static void help_all(void)
2173{
2174 const cmdinfo_t *ct;
2175
2176 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
2177 help_oneline(ct->name, ct);
2178 }
2179 printf("\nUse 'help commandname' for extended help.\n");
2180}
2181
2182static int help_f(BlockDriverState *bs, int argc, char **argv)
2183{
2184 const cmdinfo_t *ct;
2185
2186 if (argc == 1) {
2187 help_all();
2188 return 0;
2189 }
2190
2191 ct = find_command(argv[1]);
2192 if (ct == NULL) {
2193 printf("command %s not found\n", argv[1]);
2194 return 0;
2195 }
2196
2197 help_onecmd(argv[1], ct);
2198 return 0;
2199}
2200
2201static const cmdinfo_t help_cmd = {
2202 .name = "help",
2203 .altname = "?",
2204 .cfunc = help_f,
2205 .argmin = 0,
2206 .argmax = 1,
2207 .flags = CMD_FLAG_GLOBAL,
2208 .args = "[command]",
2209 .oneline = "help for one or all commands",
2210};
2211
3d21994f 2212bool qemuio_command(BlockDriverState *bs, const char *cmd)
dd583296
KW
2213{
2214 char *input;
2215 const cmdinfo_t *ct;
2216 char **v;
2217 int c;
2218 bool done = false;
2219
2220 input = g_strdup(cmd);
2221 v = breakline(input, &c);
2222 if (c) {
2223 ct = find_command(v[0]);
2224 if (ct) {
3d21994f 2225 done = command(bs, ct, c, v);
dd583296
KW
2226 } else {
2227 fprintf(stderr, "command \"%s\" not found\n", v[0]);
2228 }
2229 }
2230 g_free(input);
2231 g_free(v);
2232
2233 return done;
2234}
2235
797ac58c
KW
2236static void __attribute((constructor)) init_qemuio_commands(void)
2237{
2238 /* initialize commands */
c2cdf5c5
KW
2239 qemuio_add_command(&help_cmd);
2240 qemuio_add_command(&read_cmd);
2241 qemuio_add_command(&readv_cmd);
2242 qemuio_add_command(&write_cmd);
2243 qemuio_add_command(&writev_cmd);
2244 qemuio_add_command(&multiwrite_cmd);
2245 qemuio_add_command(&aio_read_cmd);
2246 qemuio_add_command(&aio_write_cmd);
2247 qemuio_add_command(&aio_flush_cmd);
2248 qemuio_add_command(&flush_cmd);
2249 qemuio_add_command(&truncate_cmd);
2250 qemuio_add_command(&length_cmd);
2251 qemuio_add_command(&info_cmd);
2252 qemuio_add_command(&discard_cmd);
2253 qemuio_add_command(&alloc_cmd);
2254 qemuio_add_command(&map_cmd);
2255 qemuio_add_command(&break_cmd);
4cc70e93 2256 qemuio_add_command(&remove_break_cmd);
c2cdf5c5
KW
2257 qemuio_add_command(&resume_cmd);
2258 qemuio_add_command(&wait_break_cmd);
2259 qemuio_add_command(&abort_cmd);
cd33d02a 2260 qemuio_add_command(&sleep_cmd);
0e82dc7b 2261 qemuio_add_command(&sigraise_cmd);
797ac58c 2262}
This page took 0.348635 seconds and 4 git commands to generate.