blob: de26422fcfb976a693fd40abaf4191a50463a790 [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
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 */
Stefan Weilc32d7662009-08-31 22:16:16 +020010#include <sys/time.h>
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020015#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000016
17#include "qemu-common.h"
18#include "block_int.h"
19#include "cmd.h"
20
21#define VERSION "0.0.1"
22
Devin Nakamura43642b32011-07-11 11:22:16 -040023#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000024
25char *progname;
26static BlockDriverState *bs;
27
28static int misalign;
29
30/*
Christoph Hellwigcf070d72009-07-20 01:19:25 +020031 * Parse the pattern argument to various sub-commands.
32 *
33 * Because the pattern is used as an argument to memset it must evaluate
34 * to an unsigned integer that fits into a single byte.
35 */
36static int parse_pattern(const char *arg)
37{
Devin Nakamura43642b32011-07-11 11:22:16 -040038 char *endptr = NULL;
39 long pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020040
Devin Nakamura43642b32011-07-11 11:22:16 -040041 pattern = strtol(arg, &endptr, 0);
42 if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
43 printf("%s is not a valid pattern byte\n", arg);
44 return -1;
45 }
Christoph Hellwigcf070d72009-07-20 01:19:25 +020046
Devin Nakamura43642b32011-07-11 11:22:16 -040047 return pattern;
Christoph Hellwigcf070d72009-07-20 01:19:25 +020048}
49
50/*
aliguorie3aff4f2009-04-05 19:14:04 +000051 * Memory allocation helpers.
52 *
53 * Make sure memory is aligned by default, or purposefully misaligned if
54 * that is specified on the command line.
55 */
56
Devin Nakamura43642b32011-07-11 11:22:16 -040057#define MISALIGN_OFFSET 16
aliguorie3aff4f2009-04-05 19:14:04 +000058static void *qemu_io_alloc(size_t len, int pattern)
59{
Devin Nakamura43642b32011-07-11 11:22:16 -040060 void *buf;
aliguorie3aff4f2009-04-05 19:14:04 +000061
Devin Nakamura43642b32011-07-11 11:22:16 -040062 if (misalign) {
63 len += MISALIGN_OFFSET;
64 }
65 buf = qemu_blockalign(bs, len);
66 memset(buf, pattern, len);
67 if (misalign) {
68 buf += MISALIGN_OFFSET;
69 }
70 return buf;
aliguorie3aff4f2009-04-05 19:14:04 +000071}
72
73static void qemu_io_free(void *p)
74{
Devin Nakamura43642b32011-07-11 11:22:16 -040075 if (misalign) {
76 p -= MISALIGN_OFFSET;
77 }
78 qemu_vfree(p);
aliguorie3aff4f2009-04-05 19:14:04 +000079}
80
Devin Nakamura43642b32011-07-11 11:22:16 -040081static void dump_buffer(const void *buffer, int64_t offset, int len)
aliguorie3aff4f2009-04-05 19:14:04 +000082{
Devin Nakamura43642b32011-07-11 11:22:16 -040083 int i, j;
84 const uint8_t *p;
aliguorie3aff4f2009-04-05 19:14:04 +000085
Devin Nakamura43642b32011-07-11 11:22:16 -040086 for (i = 0, p = buffer; i < len; i += 16) {
87 const uint8_t *s = p;
aliguorie3aff4f2009-04-05 19:14:04 +000088
Devin Nakamura43642b32011-07-11 11:22:16 -040089 printf("%08" PRIx64 ": ", offset + i);
90 for (j = 0; j < 16 && i + j < len; j++, p++) {
91 printf("%02x ", *p);
92 }
93 printf(" ");
94 for (j = 0; j < 16 && i + j < len; j++, s++) {
95 if (isalnum(*s)) {
96 printf("%c", *s);
97 } else {
98 printf(".");
99 }
100 }
101 printf("\n");
102 }
aliguorie3aff4f2009-04-05 19:14:04 +0000103}
104
Devin Nakamura43642b32011-07-11 11:22:16 -0400105static void print_report(const char *op, struct timeval *t, int64_t offset,
106 int count, int total, int cnt, int Cflag)
aliguorie3aff4f2009-04-05 19:14:04 +0000107{
Devin Nakamura43642b32011-07-11 11:22:16 -0400108 char s1[64], s2[64], ts[64];
aliguorie3aff4f2009-04-05 19:14:04 +0000109
Devin Nakamura43642b32011-07-11 11:22:16 -0400110 timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
111 if (!Cflag) {
112 cvtstr((double)total, s1, sizeof(s1));
113 cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
114 printf("%s %d/%d bytes at offset %" PRId64 "\n",
115 op, total, count, offset);
116 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
117 s1, cnt, ts, s2, tdiv((double)cnt, *t));
118 } else {/* bytes,ops,time,bytes/sec,ops/sec */
119 printf("%d,%d,%s,%.3f,%.3f\n",
120 total, cnt, ts,
121 tdiv((double)total, *t),
122 tdiv((double)cnt, *t));
123 }
aliguorie3aff4f2009-04-05 19:14:04 +0000124}
125
Christoph Hellwigcf572982009-07-10 13:33:42 +0200126/*
127 * Parse multiple length statements for vectored I/O, and construct an I/O
128 * vector matching it.
129 */
130static void *
131create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern)
132{
Devin Nakamura43642b32011-07-11 11:22:16 -0400133 size_t *sizes = calloc(nr_iov, sizeof(size_t));
134 size_t count = 0;
135 void *buf = NULL;
136 void *p;
137 int i;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200138
Devin Nakamura43642b32011-07-11 11:22:16 -0400139 for (i = 0; i < nr_iov; i++) {
140 char *arg = argv[i];
141 int64_t len;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200142
Devin Nakamura43642b32011-07-11 11:22:16 -0400143 len = cvtnum(arg);
144 if (len < 0) {
145 printf("non-numeric length argument -- %s\n", arg);
146 goto fail;
147 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200148
Devin Nakamura43642b32011-07-11 11:22:16 -0400149 /* should be SIZE_T_MAX, but that doesn't exist */
150 if (len > INT_MAX) {
151 printf("too large length argument -- %s\n", arg);
152 goto fail;
153 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200154
Devin Nakamura43642b32011-07-11 11:22:16 -0400155 if (len & 0x1ff) {
156 printf("length argument %" PRId64
157 " is not sector aligned\n", len);
158 goto fail;
159 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200160
Devin Nakamura43642b32011-07-11 11:22:16 -0400161 sizes[i] = len;
162 count += len;
163 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200164
Devin Nakamura43642b32011-07-11 11:22:16 -0400165 qemu_iovec_init(qiov, nr_iov);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200166
Devin Nakamura43642b32011-07-11 11:22:16 -0400167 buf = p = qemu_io_alloc(count, pattern);
Christoph Hellwigcf572982009-07-10 13:33:42 +0200168
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 for (i = 0; i < nr_iov; i++) {
170 qemu_iovec_add(qiov, p, sizes[i]);
171 p += sizes[i];
172 }
Christoph Hellwigcf572982009-07-10 13:33:42 +0200173
Kevin Wolf40a0d7c2009-11-18 10:42:59 +0100174fail:
Devin Nakamura43642b32011-07-11 11:22:16 -0400175 free(sizes);
176 return buf;
Christoph Hellwigcf572982009-07-10 13:33:42 +0200177}
178
aliguorie3aff4f2009-04-05 19:14:04 +0000179static int do_read(char *buf, int64_t offset, int count, int *total)
180{
Devin Nakamura43642b32011-07-11 11:22:16 -0400181 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000182
Devin Nakamura43642b32011-07-11 11:22:16 -0400183 ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9);
184 if (ret < 0) {
185 return ret;
186 }
187 *total = count;
188 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000189}
190
191static int do_write(char *buf, int64_t offset, int count, int *total)
192{
Devin Nakamura43642b32011-07-11 11:22:16 -0400193 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000194
Devin Nakamura43642b32011-07-11 11:22:16 -0400195 ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9);
196 if (ret < 0) {
197 return ret;
198 }
199 *total = count;
200 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000201}
202
203static int do_pread(char *buf, int64_t offset, int count, int *total)
204{
Devin Nakamura43642b32011-07-11 11:22:16 -0400205 *total = bdrv_pread(bs, offset, (uint8_t *)buf, count);
206 if (*total < 0) {
207 return *total;
208 }
209 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000210}
211
212static int do_pwrite(char *buf, int64_t offset, int count, int *total)
213{
Devin Nakamura43642b32011-07-11 11:22:16 -0400214 *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count);
215 if (*total < 0) {
216 return *total;
217 }
218 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000219}
220
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200221static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
222{
Devin Nakamura43642b32011-07-11 11:22:16 -0400223 *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
224 if (*total < 0) {
225 return *total;
226 }
227 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200228}
229
230static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
231{
Devin Nakamura43642b32011-07-11 11:22:16 -0400232 *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
233 if (*total < 0) {
234 return *total;
235 }
236 return 1;
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200237}
238
aliguorie3aff4f2009-04-05 19:14:04 +0000239#define NOT_DONE 0x7fffffff
240static void aio_rw_done(void *opaque, int ret)
241{
Devin Nakamura43642b32011-07-11 11:22:16 -0400242 *(int *)opaque = ret;
aliguorie3aff4f2009-04-05 19:14:04 +0000243}
244
245static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total)
246{
Devin Nakamura43642b32011-07-11 11:22:16 -0400247 BlockDriverAIOCB *acb;
248 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000249
Devin Nakamura43642b32011-07-11 11:22:16 -0400250 acb = bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9,
251 aio_rw_done, &async_ret);
252 if (!acb) {
253 return -EIO;
254 }
255 while (async_ret == NOT_DONE) {
256 qemu_aio_wait();
257 }
aliguorie3aff4f2009-04-05 19:14:04 +0000258
Devin Nakamura43642b32011-07-11 11:22:16 -0400259 *total = qiov->size;
260 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000261}
262
263static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total)
264{
Devin Nakamura43642b32011-07-11 11:22:16 -0400265 BlockDriverAIOCB *acb;
266 int async_ret = NOT_DONE;
aliguorie3aff4f2009-04-05 19:14:04 +0000267
Devin Nakamura43642b32011-07-11 11:22:16 -0400268 acb = bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9,
269 aio_rw_done, &async_ret);
270 if (!acb) {
271 return -EIO;
272 }
aliguorie3aff4f2009-04-05 19:14:04 +0000273
Devin Nakamura43642b32011-07-11 11:22:16 -0400274 while (async_ret == NOT_DONE) {
275 qemu_aio_wait();
276 }
aliguorie3aff4f2009-04-05 19:14:04 +0000277
Devin Nakamura43642b32011-07-11 11:22:16 -0400278 *total = qiov->size;
279 return async_ret < 0 ? async_ret : 1;
aliguorie3aff4f2009-04-05 19:14:04 +0000280}
281
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200282struct multiwrite_async_ret {
Devin Nakamura43642b32011-07-11 11:22:16 -0400283 int num_done;
284 int error;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200285};
286
287static void multiwrite_cb(void *opaque, int ret)
288{
Devin Nakamura43642b32011-07-11 11:22:16 -0400289 struct multiwrite_async_ret *async_ret = opaque;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200290
Devin Nakamura43642b32011-07-11 11:22:16 -0400291 async_ret->num_done++;
292 if (ret < 0) {
293 async_ret->error = ret;
294 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200295}
296
297static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total)
298{
Devin Nakamura43642b32011-07-11 11:22:16 -0400299 int i, ret;
300 struct multiwrite_async_ret async_ret = {
301 .num_done = 0,
302 .error = 0,
303 };
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200304
Devin Nakamura43642b32011-07-11 11:22:16 -0400305 *total = 0;
306 for (i = 0; i < num_reqs; i++) {
307 reqs[i].cb = multiwrite_cb;
308 reqs[i].opaque = &async_ret;
309 *total += reqs[i].qiov->size;
310 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200311
Devin Nakamura43642b32011-07-11 11:22:16 -0400312 ret = bdrv_aio_multiwrite(bs, reqs, num_reqs);
313 if (ret < 0) {
314 return ret;
315 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200316
Devin Nakamura43642b32011-07-11 11:22:16 -0400317 while (async_ret.num_done < num_reqs) {
318 qemu_aio_wait();
319 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200320
Devin Nakamura43642b32011-07-11 11:22:16 -0400321 return async_ret.error < 0 ? async_ret.error : 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200322}
aliguorie3aff4f2009-04-05 19:14:04 +0000323
Devin Nakamura43642b32011-07-11 11:22:16 -0400324static void read_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000325{
Devin Nakamura43642b32011-07-11 11:22:16 -0400326 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000327"\n"
328" reads a range of bytes from the given offset\n"
329"\n"
330" Example:\n"
331" 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
332"\n"
333" Reads a segment of the currently open file, optionally dumping it to the\n"
334" standard output stream (with -v option) for subsequent inspection.\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200335" -b, -- read from the VM state rather than the virtual disk\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200336" -C, -- report statistics in a machine parsable format\n"
337" -l, -- length for pattern verification (only with -P)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000338" -p, -- use bdrv_pread to read the file\n"
aliguoric48101a2009-04-18 15:36:23 +0000339" -P, -- use a pattern to verify read data\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100340" -q, -- quiet mode, do not show I/O statistics\n"
Kevin Wolfd9654a52009-04-23 15:11:13 +0200341" -s, -- start offset for pattern verification (only with -P)\n"
342" -v, -- dump buffer to standard output\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000343"\n");
344}
345
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000346static int read_f(int argc, char **argv);
347
348static const cmdinfo_t read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400349 .name = "read",
350 .altname = "r",
351 .cfunc = read_f,
352 .argmin = 2,
353 .argmax = -1,
354 .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
355 .oneline = "reads a number of bytes at a specified offset",
356 .help = read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000357};
358
Devin Nakamura43642b32011-07-11 11:22:16 -0400359static int read_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000360{
Devin Nakamura43642b32011-07-11 11:22:16 -0400361 struct timeval t1, t2;
362 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
363 int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
364 int c, cnt;
365 char *buf;
366 int64_t offset;
367 int count;
368 /* Some compilers get confused and warn if this is not initialized. */
369 int total = 0;
370 int pattern = 0, pattern_offset = 0, pattern_count = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000371
Devin Nakamura43642b32011-07-11 11:22:16 -0400372 while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
373 switch (c) {
374 case 'b':
375 bflag = 1;
376 break;
377 case 'C':
378 Cflag = 1;
379 break;
380 case 'l':
381 lflag = 1;
382 pattern_count = cvtnum(optarg);
383 if (pattern_count < 0) {
384 printf("non-numeric length argument -- %s\n", optarg);
385 return 0;
386 }
387 break;
388 case 'p':
389 pflag = 1;
390 break;
391 case 'P':
392 Pflag = 1;
393 pattern = parse_pattern(optarg);
394 if (pattern < 0) {
395 return 0;
396 }
397 break;
398 case 'q':
399 qflag = 1;
400 break;
401 case 's':
402 sflag = 1;
403 pattern_offset = cvtnum(optarg);
404 if (pattern_offset < 0) {
405 printf("non-numeric length argument -- %s\n", optarg);
406 return 0;
407 }
408 break;
409 case 'v':
410 vflag = 1;
411 break;
412 default:
413 return command_usage(&read_cmd);
414 }
415 }
aliguorie3aff4f2009-04-05 19:14:04 +0000416
Devin Nakamura43642b32011-07-11 11:22:16 -0400417 if (optind != argc - 2) {
418 return command_usage(&read_cmd);
419 }
aliguorie3aff4f2009-04-05 19:14:04 +0000420
Devin Nakamura43642b32011-07-11 11:22:16 -0400421 if (bflag && pflag) {
422 printf("-b and -p cannot be specified at the same time\n");
423 return 0;
424 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200425
Devin Nakamura43642b32011-07-11 11:22:16 -0400426 offset = cvtnum(argv[optind]);
427 if (offset < 0) {
428 printf("non-numeric length argument -- %s\n", argv[optind]);
429 return 0;
430 }
aliguorie3aff4f2009-04-05 19:14:04 +0000431
Devin Nakamura43642b32011-07-11 11:22:16 -0400432 optind++;
433 count = cvtnum(argv[optind]);
434 if (count < 0) {
435 printf("non-numeric length argument -- %s\n", argv[optind]);
436 return 0;
437 }
aliguorie3aff4f2009-04-05 19:14:04 +0000438
Kevin Wolfd9654a52009-04-23 15:11:13 +0200439 if (!Pflag && (lflag || sflag)) {
440 return command_usage(&read_cmd);
441 }
442
443 if (!lflag) {
444 pattern_count = count - pattern_offset;
445 }
446
447 if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
448 printf("pattern verfication range exceeds end of read data\n");
449 return 0;
450 }
451
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400452 if (!pflag) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400453 if (offset & 0x1ff) {
454 printf("offset %" PRId64 " is not sector aligned\n",
455 offset);
456 return 0;
457 }
458 if (count & 0x1ff) {
459 printf("count %d is not sector aligned\n",
460 count);
461 return 0;
462 }
Devin Nakamura5afc8b32011-07-11 11:20:25 -0400463 }
aliguorie3aff4f2009-04-05 19:14:04 +0000464
Devin Nakamura43642b32011-07-11 11:22:16 -0400465 buf = qemu_io_alloc(count, 0xab);
aliguorie3aff4f2009-04-05 19:14:04 +0000466
Devin Nakamura43642b32011-07-11 11:22:16 -0400467 gettimeofday(&t1, NULL);
468 if (pflag) {
469 cnt = do_pread(buf, offset, count, &total);
470 } else if (bflag) {
471 cnt = do_load_vmstate(buf, offset, count, &total);
472 } else {
473 cnt = do_read(buf, offset, count, &total);
474 }
475 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000476
Devin Nakamura43642b32011-07-11 11:22:16 -0400477 if (cnt < 0) {
478 printf("read failed: %s\n", strerror(-cnt));
479 goto out;
480 }
aliguorie3aff4f2009-04-05 19:14:04 +0000481
Devin Nakamura43642b32011-07-11 11:22:16 -0400482 if (Pflag) {
483 void *cmp_buf = malloc(pattern_count);
484 memset(cmp_buf, pattern, pattern_count);
485 if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
486 printf("Pattern verification failed at offset %"
487 PRId64 ", %d bytes\n",
488 offset + pattern_offset, pattern_count);
489 }
490 free(cmp_buf);
491 }
aliguorie3aff4f2009-04-05 19:14:04 +0000492
Devin Nakamura43642b32011-07-11 11:22:16 -0400493 if (qflag) {
494 goto out;
495 }
aliguoric48101a2009-04-18 15:36:23 +0000496
Devin Nakamura43642b32011-07-11 11:22:16 -0400497 if (vflag) {
498 dump_buffer(buf, offset, count);
499 }
aliguorie3aff4f2009-04-05 19:14:04 +0000500
Devin Nakamura43642b32011-07-11 11:22:16 -0400501 /* Finally, report back -- -C gives a parsable format */
502 t2 = tsub(t2, t1);
503 print_report("read", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000504
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200505out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400506 qemu_io_free(buf);
aliguorie3aff4f2009-04-05 19:14:04 +0000507
Devin Nakamura43642b32011-07-11 11:22:16 -0400508 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000509}
510
Devin Nakamura43642b32011-07-11 11:22:16 -0400511static void readv_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000512{
Devin Nakamura43642b32011-07-11 11:22:16 -0400513 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000514"\n"
515" reads a range of bytes from the given offset into multiple buffers\n"
516"\n"
517" Example:\n"
518" 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
519"\n"
520" Reads a segment of the currently open file, optionally dumping it to the\n"
521" standard output stream (with -v option) for subsequent inspection.\n"
522" Uses multiple iovec buffers if more than one byte range is specified.\n"
523" -C, -- report statistics in a machine parsable format\n"
aliguoric48101a2009-04-18 15:36:23 +0000524" -P, -- use a pattern to verify read data\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000525" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100526" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000527"\n");
528}
529
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000530static int readv_f(int argc, char **argv);
531
532static const cmdinfo_t readv_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400533 .name = "readv",
534 .cfunc = readv_f,
535 .argmin = 2,
536 .argmax = -1,
537 .args = "[-Cqv] [-P pattern ] off len [len..]",
538 .oneline = "reads a number of bytes at a specified offset",
539 .help = readv_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000540};
541
Devin Nakamura43642b32011-07-11 11:22:16 -0400542static int readv_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000543{
Devin Nakamura43642b32011-07-11 11:22:16 -0400544 struct timeval t1, t2;
545 int Cflag = 0, qflag = 0, vflag = 0;
546 int c, cnt;
547 char *buf;
548 int64_t offset;
549 /* Some compilers get confused and warn if this is not initialized. */
550 int total = 0;
551 int nr_iov;
552 QEMUIOVector qiov;
553 int pattern = 0;
554 int Pflag = 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000555
Devin Nakamura43642b32011-07-11 11:22:16 -0400556 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
557 switch (c) {
558 case 'C':
559 Cflag = 1;
560 break;
561 case 'P':
562 Pflag = 1;
563 pattern = parse_pattern(optarg);
564 if (pattern < 0) {
565 return 0;
566 }
567 break;
568 case 'q':
569 qflag = 1;
570 break;
571 case 'v':
572 vflag = 1;
573 break;
574 default:
575 return command_usage(&readv_cmd);
576 }
577 }
aliguorie3aff4f2009-04-05 19:14:04 +0000578
Devin Nakamura43642b32011-07-11 11:22:16 -0400579 if (optind > argc - 2) {
580 return command_usage(&readv_cmd);
581 }
aliguorie3aff4f2009-04-05 19:14:04 +0000582
583
Devin Nakamura43642b32011-07-11 11:22:16 -0400584 offset = cvtnum(argv[optind]);
585 if (offset < 0) {
586 printf("non-numeric length argument -- %s\n", argv[optind]);
587 return 0;
588 }
589 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000590
Devin Nakamura43642b32011-07-11 11:22:16 -0400591 if (offset & 0x1ff) {
592 printf("offset %" PRId64 " is not sector aligned\n",
593 offset);
594 return 0;
595 }
aliguorie3aff4f2009-04-05 19:14:04 +0000596
Devin Nakamura43642b32011-07-11 11:22:16 -0400597 nr_iov = argc - optind;
598 buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +0100599 if (buf == NULL) {
600 return 0;
601 }
aliguorie3aff4f2009-04-05 19:14:04 +0000602
Devin Nakamura43642b32011-07-11 11:22:16 -0400603 gettimeofday(&t1, NULL);
604 cnt = do_aio_readv(&qiov, offset, &total);
605 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000606
Devin Nakamura43642b32011-07-11 11:22:16 -0400607 if (cnt < 0) {
608 printf("readv failed: %s\n", strerror(-cnt));
609 goto out;
610 }
aliguorie3aff4f2009-04-05 19:14:04 +0000611
Devin Nakamura43642b32011-07-11 11:22:16 -0400612 if (Pflag) {
613 void *cmp_buf = malloc(qiov.size);
614 memset(cmp_buf, pattern, qiov.size);
615 if (memcmp(buf, cmp_buf, qiov.size)) {
616 printf("Pattern verification failed at offset %"
617 PRId64 ", %zd bytes\n", offset, qiov.size);
618 }
619 free(cmp_buf);
620 }
aliguoric48101a2009-04-18 15:36:23 +0000621
Devin Nakamura43642b32011-07-11 11:22:16 -0400622 if (qflag) {
623 goto out;
624 }
aliguorie3aff4f2009-04-05 19:14:04 +0000625
Devin Nakamura43642b32011-07-11 11:22:16 -0400626 if (vflag) {
627 dump_buffer(buf, offset, qiov.size);
628 }
aliguorie3aff4f2009-04-05 19:14:04 +0000629
Devin Nakamura43642b32011-07-11 11:22:16 -0400630 /* Finally, report back -- -C gives a parsable format */
631 t2 = tsub(t2, t1);
632 print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000633
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200634out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400635 qemu_io_free(buf);
636 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000637}
638
Devin Nakamura43642b32011-07-11 11:22:16 -0400639static void write_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000640{
Devin Nakamura43642b32011-07-11 11:22:16 -0400641 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000642"\n"
643" writes a range of bytes from the given offset\n"
644"\n"
645" Example:\n"
646" 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
647"\n"
648" Writes into a segment of the currently open file, using a buffer\n"
649" filled with a set pattern (0xcdcdcdcd).\n"
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200650" -b, -- write to the VM state rather than the virtual disk\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000651" -p, -- use bdrv_pwrite to write the file\n"
652" -P, -- use different pattern to fill file\n"
653" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100654" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000655"\n");
656}
657
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000658static int write_f(int argc, char **argv);
659
660static const cmdinfo_t write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400661 .name = "write",
662 .altname = "w",
663 .cfunc = write_f,
664 .argmin = 2,
665 .argmax = -1,
666 .args = "[-abCpq] [-P pattern ] off len",
667 .oneline = "writes a number of bytes at a specified offset",
668 .help = write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000669};
670
Devin Nakamura43642b32011-07-11 11:22:16 -0400671static int write_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000672{
Devin Nakamura43642b32011-07-11 11:22:16 -0400673 struct timeval t1, t2;
674 int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
675 int c, cnt;
676 char *buf;
677 int64_t offset;
678 int count;
679 /* Some compilers get confused and warn if this is not initialized. */
680 int total = 0;
681 int pattern = 0xcd;
aliguorie3aff4f2009-04-05 19:14:04 +0000682
Devin Nakamura43642b32011-07-11 11:22:16 -0400683 while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
684 switch (c) {
685 case 'b':
686 bflag = 1;
687 break;
688 case 'C':
689 Cflag = 1;
690 break;
691 case 'p':
692 pflag = 1;
693 break;
694 case 'P':
695 pattern = parse_pattern(optarg);
696 if (pattern < 0) {
697 return 0;
698 }
699 break;
700 case 'q':
701 qflag = 1;
702 break;
703 default:
704 return command_usage(&write_cmd);
705 }
706 }
aliguorie3aff4f2009-04-05 19:14:04 +0000707
Devin Nakamura43642b32011-07-11 11:22:16 -0400708 if (optind != argc - 2) {
709 return command_usage(&write_cmd);
710 }
aliguorie3aff4f2009-04-05 19:14:04 +0000711
Devin Nakamura43642b32011-07-11 11:22:16 -0400712 if (bflag && pflag) {
713 printf("-b and -p cannot be specified at the same time\n");
714 return 0;
715 }
Kevin Wolfca94dbc2009-07-15 12:06:58 +0200716
Devin Nakamura43642b32011-07-11 11:22:16 -0400717 offset = cvtnum(argv[optind]);
718 if (offset < 0) {
719 printf("non-numeric length argument -- %s\n", argv[optind]);
720 return 0;
721 }
aliguorie3aff4f2009-04-05 19:14:04 +0000722
Devin Nakamura43642b32011-07-11 11:22:16 -0400723 optind++;
724 count = cvtnum(argv[optind]);
725 if (count < 0) {
726 printf("non-numeric length argument -- %s\n", argv[optind]);
727 return 0;
728 }
aliguorie3aff4f2009-04-05 19:14:04 +0000729
Devin Nakamura43642b32011-07-11 11:22:16 -0400730 if (!pflag) {
731 if (offset & 0x1ff) {
732 printf("offset %" PRId64 " is not sector aligned\n",
733 offset);
734 return 0;
735 }
aliguorie3aff4f2009-04-05 19:14:04 +0000736
Devin Nakamura43642b32011-07-11 11:22:16 -0400737 if (count & 0x1ff) {
738 printf("count %d is not sector aligned\n",
739 count);
740 return 0;
741 }
742 }
aliguorie3aff4f2009-04-05 19:14:04 +0000743
Devin Nakamura43642b32011-07-11 11:22:16 -0400744 buf = qemu_io_alloc(count, pattern);
aliguorie3aff4f2009-04-05 19:14:04 +0000745
Devin Nakamura43642b32011-07-11 11:22:16 -0400746 gettimeofday(&t1, NULL);
747 if (pflag) {
748 cnt = do_pwrite(buf, offset, count, &total);
749 } else if (bflag) {
750 cnt = do_save_vmstate(buf, offset, count, &total);
751 } else {
752 cnt = do_write(buf, offset, count, &total);
753 }
754 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000755
Devin Nakamura43642b32011-07-11 11:22:16 -0400756 if (cnt < 0) {
757 printf("write failed: %s\n", strerror(-cnt));
758 goto out;
759 }
aliguorie3aff4f2009-04-05 19:14:04 +0000760
Devin Nakamura43642b32011-07-11 11:22:16 -0400761 if (qflag) {
762 goto out;
763 }
aliguorie3aff4f2009-04-05 19:14:04 +0000764
Devin Nakamura43642b32011-07-11 11:22:16 -0400765 /* Finally, report back -- -C gives a parsable format */
766 t2 = tsub(t2, t1);
767 print_report("wrote", &t2, offset, count, total, cnt, Cflag);
aliguorie3aff4f2009-04-05 19:14:04 +0000768
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200769out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400770 qemu_io_free(buf);
aliguorie3aff4f2009-04-05 19:14:04 +0000771
Devin Nakamura43642b32011-07-11 11:22:16 -0400772 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000773}
774
aliguorie3aff4f2009-04-05 19:14:04 +0000775static void
776writev_help(void)
777{
Devin Nakamura43642b32011-07-11 11:22:16 -0400778 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000779"\n"
780" writes a range of bytes from the given offset source from multiple buffers\n"
781"\n"
782" Example:\n"
783" 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
784"\n"
785" Writes into a segment of the currently open file, using a buffer\n"
786" filled with a set pattern (0xcdcdcdcd).\n"
787" -P, -- use different pattern to fill file\n"
788" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +0100789" -q, -- quiet mode, do not show I/O statistics\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000790"\n");
791}
792
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000793static int writev_f(int argc, char **argv);
794
795static const cmdinfo_t writev_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400796 .name = "writev",
797 .cfunc = writev_f,
798 .argmin = 2,
799 .argmax = -1,
800 .args = "[-Cq] [-P pattern ] off len [len..]",
801 .oneline = "writes a number of bytes at a specified offset",
802 .help = writev_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000803};
804
Devin Nakamura43642b32011-07-11 11:22:16 -0400805static int writev_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000806{
Devin Nakamura43642b32011-07-11 11:22:16 -0400807 struct timeval t1, t2;
808 int Cflag = 0, qflag = 0;
809 int c, cnt;
810 char *buf;
811 int64_t offset;
812 /* Some compilers get confused and warn if this is not initialized. */
813 int total = 0;
814 int nr_iov;
815 int pattern = 0xcd;
816 QEMUIOVector qiov;
aliguorie3aff4f2009-04-05 19:14:04 +0000817
Devin Nakamura43642b32011-07-11 11:22:16 -0400818 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
819 switch (c) {
820 case 'C':
821 Cflag = 1;
822 break;
823 case 'q':
824 qflag = 1;
825 break;
826 case 'P':
827 pattern = parse_pattern(optarg);
828 if (pattern < 0) {
829 return 0;
830 }
831 break;
832 default:
833 return command_usage(&writev_cmd);
834 }
835 }
aliguorie3aff4f2009-04-05 19:14:04 +0000836
Devin Nakamura43642b32011-07-11 11:22:16 -0400837 if (optind > argc - 2) {
838 return command_usage(&writev_cmd);
839 }
aliguorie3aff4f2009-04-05 19:14:04 +0000840
Devin Nakamura43642b32011-07-11 11:22:16 -0400841 offset = cvtnum(argv[optind]);
842 if (offset < 0) {
843 printf("non-numeric length argument -- %s\n", argv[optind]);
844 return 0;
845 }
846 optind++;
aliguorie3aff4f2009-04-05 19:14:04 +0000847
Devin Nakamura43642b32011-07-11 11:22:16 -0400848 if (offset & 0x1ff) {
849 printf("offset %" PRId64 " is not sector aligned\n",
850 offset);
851 return 0;
852 }
aliguorie3aff4f2009-04-05 19:14:04 +0000853
Devin Nakamura43642b32011-07-11 11:22:16 -0400854 nr_iov = argc - optind;
855 buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +0100856 if (buf == NULL) {
857 return 0;
858 }
aliguorie3aff4f2009-04-05 19:14:04 +0000859
Devin Nakamura43642b32011-07-11 11:22:16 -0400860 gettimeofday(&t1, NULL);
861 cnt = do_aio_writev(&qiov, offset, &total);
862 gettimeofday(&t2, NULL);
aliguorie3aff4f2009-04-05 19:14:04 +0000863
Devin Nakamura43642b32011-07-11 11:22:16 -0400864 if (cnt < 0) {
865 printf("writev failed: %s\n", strerror(-cnt));
866 goto out;
867 }
aliguorie3aff4f2009-04-05 19:14:04 +0000868
Devin Nakamura43642b32011-07-11 11:22:16 -0400869 if (qflag) {
870 goto out;
871 }
aliguorie3aff4f2009-04-05 19:14:04 +0000872
Devin Nakamura43642b32011-07-11 11:22:16 -0400873 /* Finally, report back -- -C gives a parsable format */
874 t2 = tsub(t2, t1);
875 print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +0200876out:
Devin Nakamura43642b32011-07-11 11:22:16 -0400877 qemu_io_free(buf);
878 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000879}
880
Devin Nakamura43642b32011-07-11 11:22:16 -0400881static void multiwrite_help(void)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200882{
Devin Nakamura43642b32011-07-11 11:22:16 -0400883 printf(
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200884"\n"
885" writes a range of bytes from the given offset source from multiple buffers,\n"
886" in a batch of requests that may be merged by qemu\n"
887"\n"
888" Example:\n"
Stefan Weilb2bedb22011-09-12 22:33:01 +0200889" 'multiwrite 512 1k 1k ; 4k 1k'\n"
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200890" writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
891"\n"
892" Writes into a segment of the currently open file, using a buffer\n"
893" filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
894" by one for each request contained in the multiwrite command.\n"
895" -P, -- use different pattern to fill file\n"
896" -C, -- report statistics in a machine parsable format\n"
897" -q, -- quiet mode, do not show I/O statistics\n"
898"\n");
899}
900
901static int multiwrite_f(int argc, char **argv);
902
903static const cmdinfo_t multiwrite_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400904 .name = "multiwrite",
905 .cfunc = multiwrite_f,
906 .argmin = 2,
907 .argmax = -1,
908 .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
909 .oneline = "issues multiple write requests at once",
910 .help = multiwrite_help,
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200911};
912
Devin Nakamura43642b32011-07-11 11:22:16 -0400913static int multiwrite_f(int argc, char **argv)
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200914{
Devin Nakamura43642b32011-07-11 11:22:16 -0400915 struct timeval t1, t2;
916 int Cflag = 0, qflag = 0;
917 int c, cnt;
918 char **buf;
919 int64_t offset, first_offset = 0;
920 /* Some compilers get confused and warn if this is not initialized. */
921 int total = 0;
922 int nr_iov;
923 int nr_reqs;
924 int pattern = 0xcd;
925 QEMUIOVector *qiovs;
926 int i;
927 BlockRequest *reqs;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200928
Devin Nakamura43642b32011-07-11 11:22:16 -0400929 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
930 switch (c) {
931 case 'C':
932 Cflag = 1;
933 break;
934 case 'q':
935 qflag = 1;
936 break;
937 case 'P':
938 pattern = parse_pattern(optarg);
939 if (pattern < 0) {
940 return 0;
941 }
942 break;
943 default:
944 return command_usage(&writev_cmd);
945 }
946 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200947
Devin Nakamura43642b32011-07-11 11:22:16 -0400948 if (optind > argc - 2) {
949 return command_usage(&writev_cmd);
950 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200951
Devin Nakamura43642b32011-07-11 11:22:16 -0400952 nr_reqs = 1;
953 for (i = optind; i < argc; i++) {
954 if (!strcmp(argv[i], ";")) {
955 nr_reqs++;
956 }
957 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200958
Kevin Wolff2360622011-10-31 11:36:32 +0100959 reqs = g_malloc0(nr_reqs * sizeof(*reqs));
960 buf = g_malloc0(nr_reqs * sizeof(*buf));
Anthony Liguori7267c092011-08-20 22:09:37 -0500961 qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200962
Kevin Wolf67403db2011-10-31 11:49:21 +0100963 for (i = 0; i < nr_reqs && optind < argc; i++) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400964 int j;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200965
Devin Nakamura43642b32011-07-11 11:22:16 -0400966 /* Read the offset of the request */
967 offset = cvtnum(argv[optind]);
968 if (offset < 0) {
969 printf("non-numeric offset argument -- %s\n", argv[optind]);
Kevin Wolf67403db2011-10-31 11:49:21 +0100970 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -0400971 }
972 optind++;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200973
Devin Nakamura43642b32011-07-11 11:22:16 -0400974 if (offset & 0x1ff) {
975 printf("offset %lld is not sector aligned\n",
976 (long long)offset);
Kevin Wolf67403db2011-10-31 11:49:21 +0100977 goto out;
Devin Nakamura43642b32011-07-11 11:22:16 -0400978 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200979
980 if (i == 0) {
981 first_offset = offset;
982 }
983
Devin Nakamura43642b32011-07-11 11:22:16 -0400984 /* Read lengths for qiov entries */
985 for (j = optind; j < argc; j++) {
986 if (!strcmp(argv[j], ";")) {
987 break;
988 }
989 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200990
Devin Nakamura43642b32011-07-11 11:22:16 -0400991 nr_iov = j - optind;
Kevin Wolf776cbbb2010-05-21 11:37:26 +0200992
Devin Nakamura43642b32011-07-11 11:22:16 -0400993 /* Build request */
Kevin Wolff2360622011-10-31 11:36:32 +0100994 buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
995 if (buf[i] == NULL) {
996 goto out;
997 }
998
Devin Nakamura43642b32011-07-11 11:22:16 -0400999 reqs[i].qiov = &qiovs[i];
Devin Nakamura43642b32011-07-11 11:22:16 -04001000 reqs[i].sector = offset >> 9;
1001 reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001002
Devin Nakamura43642b32011-07-11 11:22:16 -04001003 optind = j + 1;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001004
Devin Nakamura43642b32011-07-11 11:22:16 -04001005 pattern++;
1006 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001007
Kevin Wolf67403db2011-10-31 11:49:21 +01001008 /* If there were empty requests at the end, ignore them */
1009 nr_reqs = i;
1010
Devin Nakamura43642b32011-07-11 11:22:16 -04001011 gettimeofday(&t1, NULL);
1012 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1013 gettimeofday(&t2, NULL);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001014
Devin Nakamura43642b32011-07-11 11:22:16 -04001015 if (cnt < 0) {
1016 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1017 goto out;
1018 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001019
Devin Nakamura43642b32011-07-11 11:22:16 -04001020 if (qflag) {
1021 goto out;
1022 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001023
Devin Nakamura43642b32011-07-11 11:22:16 -04001024 /* Finally, report back -- -C gives a parsable format */
1025 t2 = tsub(t2, t1);
1026 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001027out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001028 for (i = 0; i < nr_reqs; i++) {
1029 qemu_io_free(buf[i]);
Kevin Wolff2360622011-10-31 11:36:32 +01001030 if (reqs[i].qiov != NULL) {
1031 qemu_iovec_destroy(&qiovs[i]);
1032 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001033 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001034 g_free(buf);
1035 g_free(reqs);
1036 g_free(qiovs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001037 return 0;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001038}
1039
Christoph Hellwig95533d52009-06-20 21:10:44 +02001040struct aio_ctx {
Devin Nakamura43642b32011-07-11 11:22:16 -04001041 QEMUIOVector qiov;
1042 int64_t offset;
1043 char *buf;
1044 int qflag;
1045 int vflag;
1046 int Cflag;
1047 int Pflag;
1048 int pattern;
1049 struct timeval t1;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001050};
1051
Devin Nakamura43642b32011-07-11 11:22:16 -04001052static void aio_write_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001053{
Devin Nakamura43642b32011-07-11 11:22:16 -04001054 struct aio_ctx *ctx = opaque;
1055 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001056
Devin Nakamura43642b32011-07-11 11:22:16 -04001057 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001058
Christoph Hellwig95533d52009-06-20 21:10:44 +02001059
Devin Nakamura43642b32011-07-11 11:22:16 -04001060 if (ret < 0) {
1061 printf("aio_write failed: %s\n", strerror(-ret));
1062 goto out;
1063 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001064
Devin Nakamura43642b32011-07-11 11:22:16 -04001065 if (ctx->qflag) {
1066 goto out;
1067 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001068
Devin Nakamura43642b32011-07-11 11:22:16 -04001069 /* Finally, report back -- -C gives a parsable format */
1070 t2 = tsub(t2, ctx->t1);
1071 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1072 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001073out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001074 qemu_io_free(ctx->buf);
1075 free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001076}
1077
Devin Nakamura43642b32011-07-11 11:22:16 -04001078static void aio_read_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001079{
Devin Nakamura43642b32011-07-11 11:22:16 -04001080 struct aio_ctx *ctx = opaque;
1081 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001082
Devin Nakamura43642b32011-07-11 11:22:16 -04001083 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001084
Devin Nakamura43642b32011-07-11 11:22:16 -04001085 if (ret < 0) {
1086 printf("readv failed: %s\n", strerror(-ret));
1087 goto out;
1088 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001089
Devin Nakamura43642b32011-07-11 11:22:16 -04001090 if (ctx->Pflag) {
1091 void *cmp_buf = malloc(ctx->qiov.size);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001092
Devin Nakamura43642b32011-07-11 11:22:16 -04001093 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1094 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1095 printf("Pattern verification failed at offset %"
1096 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1097 }
1098 free(cmp_buf);
1099 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001100
Devin Nakamura43642b32011-07-11 11:22:16 -04001101 if (ctx->qflag) {
1102 goto out;
1103 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001104
Devin Nakamura43642b32011-07-11 11:22:16 -04001105 if (ctx->vflag) {
1106 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1107 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001108
Devin Nakamura43642b32011-07-11 11:22:16 -04001109 /* Finally, report back -- -C gives a parsable format */
1110 t2 = tsub(t2, ctx->t1);
1111 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1112 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001113out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001114 qemu_io_free(ctx->buf);
1115 free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001116}
1117
Devin Nakamura43642b32011-07-11 11:22:16 -04001118static void aio_read_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001119{
Devin Nakamura43642b32011-07-11 11:22:16 -04001120 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001121"\n"
1122" asynchronously reads a range of bytes from the given offset\n"
1123"\n"
1124" Example:\n"
1125" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1126"\n"
1127" Reads a segment of the currently open file, optionally dumping it to the\n"
1128" standard output stream (with -v option) for subsequent inspection.\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001129" The read is performed asynchronously and the aio_flush command must be\n"
1130" used to ensure all outstanding aio requests have been completed\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001131" -C, -- report statistics in a machine parsable format\n"
1132" -P, -- use a pattern to verify read data\n"
1133" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001134" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001135"\n");
1136}
1137
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001138static int aio_read_f(int argc, char **argv);
1139
1140static const cmdinfo_t aio_read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001141 .name = "aio_read",
1142 .cfunc = aio_read_f,
1143 .argmin = 2,
1144 .argmax = -1,
1145 .args = "[-Cqv] [-P pattern ] off len [len..]",
1146 .oneline = "asynchronously reads a number of bytes",
1147 .help = aio_read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001148};
1149
Devin Nakamura43642b32011-07-11 11:22:16 -04001150static int aio_read_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001151{
Devin Nakamura43642b32011-07-11 11:22:16 -04001152 int nr_iov, c;
1153 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1154 BlockDriverAIOCB *acb;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001155
Devin Nakamura43642b32011-07-11 11:22:16 -04001156 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1157 switch (c) {
1158 case 'C':
1159 ctx->Cflag = 1;
1160 break;
1161 case 'P':
1162 ctx->Pflag = 1;
1163 ctx->pattern = parse_pattern(optarg);
1164 if (ctx->pattern < 0) {
1165 free(ctx);
1166 return 0;
1167 }
1168 break;
1169 case 'q':
1170 ctx->qflag = 1;
1171 break;
1172 case 'v':
1173 ctx->vflag = 1;
1174 break;
1175 default:
1176 free(ctx);
1177 return command_usage(&aio_read_cmd);
1178 }
1179 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001180
Devin Nakamura43642b32011-07-11 11:22:16 -04001181 if (optind > argc - 2) {
1182 free(ctx);
1183 return command_usage(&aio_read_cmd);
1184 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001185
Devin Nakamura43642b32011-07-11 11:22:16 -04001186 ctx->offset = cvtnum(argv[optind]);
1187 if (ctx->offset < 0) {
1188 printf("non-numeric length argument -- %s\n", argv[optind]);
1189 free(ctx);
1190 return 0;
1191 }
1192 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001193
Devin Nakamura43642b32011-07-11 11:22:16 -04001194 if (ctx->offset & 0x1ff) {
1195 printf("offset %" PRId64 " is not sector aligned\n",
1196 ctx->offset);
1197 free(ctx);
1198 return 0;
1199 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001200
Devin Nakamura43642b32011-07-11 11:22:16 -04001201 nr_iov = argc - optind;
1202 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +01001203 if (ctx->buf == NULL) {
1204 free(ctx);
1205 return 0;
1206 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001207
Devin Nakamura43642b32011-07-11 11:22:16 -04001208 gettimeofday(&ctx->t1, NULL);
1209 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1210 ctx->qiov.size >> 9, aio_read_done, ctx);
1211 if (!acb) {
1212 free(ctx->buf);
1213 free(ctx);
1214 return -EIO;
1215 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001216
Devin Nakamura43642b32011-07-11 11:22:16 -04001217 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001218}
1219
Devin Nakamura43642b32011-07-11 11:22:16 -04001220static void aio_write_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001221{
Devin Nakamura43642b32011-07-11 11:22:16 -04001222 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001223"\n"
Devin Nakamura43642b32011-07-11 11:22:16 -04001224" asynchronously writes a range of bytes from the given offset source\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001225" from multiple buffers\n"
1226"\n"
1227" Example:\n"
1228" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1229"\n"
1230" Writes into a segment of the currently open file, using a buffer\n"
1231" filled with a set pattern (0xcdcdcdcd).\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001232" The write is performed asynchronously and the aio_flush command must be\n"
1233" used to ensure all outstanding aio requests have been completed\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001234" -P, -- use different pattern to fill file\n"
1235" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001236" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001237"\n");
1238}
1239
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001240static int aio_write_f(int argc, char **argv);
1241
1242static const cmdinfo_t aio_write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001243 .name = "aio_write",
1244 .cfunc = aio_write_f,
1245 .argmin = 2,
1246 .argmax = -1,
1247 .args = "[-Cq] [-P pattern ] off len [len..]",
1248 .oneline = "asynchronously writes a number of bytes",
1249 .help = aio_write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001250};
Christoph Hellwig95533d52009-06-20 21:10:44 +02001251
Devin Nakamura43642b32011-07-11 11:22:16 -04001252static int aio_write_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001253{
Devin Nakamura43642b32011-07-11 11:22:16 -04001254 int nr_iov, c;
1255 int pattern = 0xcd;
1256 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1257 BlockDriverAIOCB *acb;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001258
Devin Nakamura43642b32011-07-11 11:22:16 -04001259 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1260 switch (c) {
1261 case 'C':
1262 ctx->Cflag = 1;
1263 break;
1264 case 'q':
1265 ctx->qflag = 1;
1266 break;
1267 case 'P':
1268 pattern = parse_pattern(optarg);
1269 if (pattern < 0) {
Alex Jiab1b1dad2011-09-28 14:57:01 +08001270 free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001271 return 0;
1272 }
1273 break;
1274 default:
1275 free(ctx);
1276 return command_usage(&aio_write_cmd);
1277 }
1278 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001279
Devin Nakamura43642b32011-07-11 11:22:16 -04001280 if (optind > argc - 2) {
1281 free(ctx);
1282 return command_usage(&aio_write_cmd);
1283 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001284
Devin Nakamura43642b32011-07-11 11:22:16 -04001285 ctx->offset = cvtnum(argv[optind]);
1286 if (ctx->offset < 0) {
1287 printf("non-numeric length argument -- %s\n", argv[optind]);
1288 free(ctx);
1289 return 0;
1290 }
1291 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001292
Devin Nakamura43642b32011-07-11 11:22:16 -04001293 if (ctx->offset & 0x1ff) {
1294 printf("offset %" PRId64 " is not sector aligned\n",
1295 ctx->offset);
1296 free(ctx);
1297 return 0;
1298 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001299
Devin Nakamura43642b32011-07-11 11:22:16 -04001300 nr_iov = argc - optind;
1301 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +01001302 if (ctx->buf == NULL) {
1303 free(ctx);
1304 return 0;
1305 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001306
Devin Nakamura43642b32011-07-11 11:22:16 -04001307 gettimeofday(&ctx->t1, NULL);
1308 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1309 ctx->qiov.size >> 9, aio_write_done, ctx);
1310 if (!acb) {
1311 free(ctx->buf);
1312 free(ctx);
1313 return -EIO;
1314 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001315
Devin Nakamura43642b32011-07-11 11:22:16 -04001316 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001317}
1318
Devin Nakamura43642b32011-07-11 11:22:16 -04001319static int aio_flush_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001320{
Devin Nakamura43642b32011-07-11 11:22:16 -04001321 qemu_aio_flush();
1322 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001323}
1324
1325static const cmdinfo_t aio_flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001326 .name = "aio_flush",
1327 .cfunc = aio_flush_f,
1328 .oneline = "completes all outstanding aio requests"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001329};
1330
Devin Nakamura43642b32011-07-11 11:22:16 -04001331static int flush_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001332{
Devin Nakamura43642b32011-07-11 11:22:16 -04001333 bdrv_flush(bs);
1334 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001335}
1336
1337static const cmdinfo_t flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001338 .name = "flush",
1339 .altname = "f",
1340 .cfunc = flush_f,
1341 .oneline = "flush all in-core file state to disk",
aliguorie3aff4f2009-04-05 19:14:04 +00001342};
1343
Devin Nakamura43642b32011-07-11 11:22:16 -04001344static int truncate_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001345{
Devin Nakamura43642b32011-07-11 11:22:16 -04001346 int64_t offset;
1347 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001348
Devin Nakamura43642b32011-07-11 11:22:16 -04001349 offset = cvtnum(argv[1]);
1350 if (offset < 0) {
1351 printf("non-numeric truncate argument -- %s\n", argv[1]);
1352 return 0;
1353 }
aliguorie3aff4f2009-04-05 19:14:04 +00001354
Devin Nakamura43642b32011-07-11 11:22:16 -04001355 ret = bdrv_truncate(bs, offset);
1356 if (ret < 0) {
1357 printf("truncate: %s\n", strerror(-ret));
1358 return 0;
1359 }
aliguorie3aff4f2009-04-05 19:14:04 +00001360
Devin Nakamura43642b32011-07-11 11:22:16 -04001361 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001362}
1363
1364static const cmdinfo_t truncate_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001365 .name = "truncate",
1366 .altname = "t",
1367 .cfunc = truncate_f,
1368 .argmin = 1,
1369 .argmax = 1,
1370 .args = "off",
1371 .oneline = "truncates the current file at the given offset",
aliguorie3aff4f2009-04-05 19:14:04 +00001372};
1373
Devin Nakamura43642b32011-07-11 11:22:16 -04001374static int length_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001375{
Devin Nakamura43642b32011-07-11 11:22:16 -04001376 int64_t size;
1377 char s1[64];
aliguorie3aff4f2009-04-05 19:14:04 +00001378
Devin Nakamura43642b32011-07-11 11:22:16 -04001379 size = bdrv_getlength(bs);
1380 if (size < 0) {
1381 printf("getlength: %s\n", strerror(-size));
1382 return 0;
1383 }
aliguorie3aff4f2009-04-05 19:14:04 +00001384
Devin Nakamura43642b32011-07-11 11:22:16 -04001385 cvtstr(size, s1, sizeof(s1));
1386 printf("%s\n", s1);
1387 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001388}
1389
1390
1391static const cmdinfo_t length_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001392 .name = "length",
1393 .altname = "l",
1394 .cfunc = length_f,
1395 .oneline = "gets the length of the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001396};
1397
1398
Devin Nakamura43642b32011-07-11 11:22:16 -04001399static int info_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001400{
Devin Nakamura43642b32011-07-11 11:22:16 -04001401 BlockDriverInfo bdi;
1402 char s1[64], s2[64];
1403 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001404
Devin Nakamura43642b32011-07-11 11:22:16 -04001405 if (bs->drv && bs->drv->format_name) {
1406 printf("format name: %s\n", bs->drv->format_name);
1407 }
1408 if (bs->drv && bs->drv->protocol_name) {
1409 printf("format name: %s\n", bs->drv->protocol_name);
1410 }
aliguorie3aff4f2009-04-05 19:14:04 +00001411
Devin Nakamura43642b32011-07-11 11:22:16 -04001412 ret = bdrv_get_info(bs, &bdi);
1413 if (ret) {
1414 return 0;
1415 }
aliguorie3aff4f2009-04-05 19:14:04 +00001416
Devin Nakamura43642b32011-07-11 11:22:16 -04001417 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1418 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
aliguorie3aff4f2009-04-05 19:14:04 +00001419
Devin Nakamura43642b32011-07-11 11:22:16 -04001420 printf("cluster size: %s\n", s1);
1421 printf("vm state offset: %s\n", s2);
aliguorie3aff4f2009-04-05 19:14:04 +00001422
Devin Nakamura43642b32011-07-11 11:22:16 -04001423 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001424}
1425
1426
1427
1428static const cmdinfo_t info_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001429 .name = "info",
1430 .altname = "i",
1431 .cfunc = info_f,
1432 .oneline = "prints information about the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001433};
1434
Devin Nakamura43642b32011-07-11 11:22:16 -04001435static void discard_help(void)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001436{
Devin Nakamura43642b32011-07-11 11:22:16 -04001437 printf(
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001438"\n"
1439" discards a range of bytes from the given offset\n"
1440"\n"
1441" Example:\n"
1442" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1443"\n"
1444" Discards a segment of the currently open file.\n"
1445" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001446" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001447"\n");
1448}
1449
1450static int discard_f(int argc, char **argv);
1451
1452static const cmdinfo_t discard_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001453 .name = "discard",
1454 .altname = "d",
1455 .cfunc = discard_f,
1456 .argmin = 2,
1457 .argmax = -1,
1458 .args = "[-Cq] off len",
1459 .oneline = "discards a number of bytes at a specified offset",
1460 .help = discard_help,
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001461};
1462
Devin Nakamura43642b32011-07-11 11:22:16 -04001463static int discard_f(int argc, char **argv)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001464{
Devin Nakamura43642b32011-07-11 11:22:16 -04001465 struct timeval t1, t2;
1466 int Cflag = 0, qflag = 0;
1467 int c, ret;
1468 int64_t offset;
1469 int count;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001470
Devin Nakamura43642b32011-07-11 11:22:16 -04001471 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1472 switch (c) {
1473 case 'C':
1474 Cflag = 1;
1475 break;
1476 case 'q':
1477 qflag = 1;
1478 break;
1479 default:
1480 return command_usage(&discard_cmd);
1481 }
1482 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001483
Devin Nakamura43642b32011-07-11 11:22:16 -04001484 if (optind != argc - 2) {
1485 return command_usage(&discard_cmd);
1486 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001487
Devin Nakamura43642b32011-07-11 11:22:16 -04001488 offset = cvtnum(argv[optind]);
1489 if (offset < 0) {
1490 printf("non-numeric length argument -- %s\n", argv[optind]);
1491 return 0;
1492 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001493
Devin Nakamura43642b32011-07-11 11:22:16 -04001494 optind++;
1495 count = cvtnum(argv[optind]);
1496 if (count < 0) {
1497 printf("non-numeric length argument -- %s\n", argv[optind]);
1498 return 0;
1499 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001500
Devin Nakamura43642b32011-07-11 11:22:16 -04001501 gettimeofday(&t1, NULL);
1502 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1503 count >> BDRV_SECTOR_BITS);
1504 gettimeofday(&t2, NULL);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001505
Devin Nakamura43642b32011-07-11 11:22:16 -04001506 if (ret < 0) {
1507 printf("discard failed: %s\n", strerror(-ret));
1508 goto out;
1509 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001510
Devin Nakamura43642b32011-07-11 11:22:16 -04001511 /* Finally, report back -- -C gives a parsable format */
1512 if (!qflag) {
1513 t2 = tsub(t2, t1);
1514 print_report("discard", &t2, offset, count, count, 1, Cflag);
1515 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001516
1517out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001518 return 0;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001519}
1520
Devin Nakamura43642b32011-07-11 11:22:16 -04001521static int alloc_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001522{
Devin Nakamura43642b32011-07-11 11:22:16 -04001523 int64_t offset;
1524 int nb_sectors, remaining;
1525 char s1[64];
1526 int num, sum_alloc;
1527 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001528
Devin Nakamura43642b32011-07-11 11:22:16 -04001529 offset = cvtnum(argv[1]);
1530 if (offset & 0x1ff) {
1531 printf("offset %" PRId64 " is not sector aligned\n",
1532 offset);
1533 return 0;
1534 }
aliguorie3aff4f2009-04-05 19:14:04 +00001535
Devin Nakamura43642b32011-07-11 11:22:16 -04001536 if (argc == 3) {
1537 nb_sectors = cvtnum(argv[2]);
1538 } else {
1539 nb_sectors = 1;
1540 }
aliguorie3aff4f2009-04-05 19:14:04 +00001541
Devin Nakamura43642b32011-07-11 11:22:16 -04001542 remaining = nb_sectors;
1543 sum_alloc = 0;
1544 while (remaining) {
1545 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1546 remaining -= num;
1547 if (ret) {
1548 sum_alloc += num;
1549 }
1550 }
aliguorie3aff4f2009-04-05 19:14:04 +00001551
Devin Nakamura43642b32011-07-11 11:22:16 -04001552 cvtstr(offset, s1, sizeof(s1));
aliguorie3aff4f2009-04-05 19:14:04 +00001553
Devin Nakamura43642b32011-07-11 11:22:16 -04001554 printf("%d/%d sectors allocated at offset %s\n",
1555 sum_alloc, nb_sectors, s1);
1556 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001557}
1558
1559static const cmdinfo_t alloc_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001560 .name = "alloc",
1561 .altname = "a",
1562 .argmin = 1,
1563 .argmax = 2,
1564 .cfunc = alloc_f,
1565 .args = "off [sectors]",
1566 .oneline = "checks if a sector is present in the file",
aliguorie3aff4f2009-04-05 19:14:04 +00001567};
1568
Devin Nakamura43642b32011-07-11 11:22:16 -04001569static int map_f(int argc, char **argv)
Kevin Wolf191c2892010-09-16 13:18:08 +02001570{
Devin Nakamura43642b32011-07-11 11:22:16 -04001571 int64_t offset;
1572 int64_t nb_sectors;
1573 char s1[64];
1574 int num, num_checked;
1575 int ret;
1576 const char *retstr;
Kevin Wolf191c2892010-09-16 13:18:08 +02001577
Devin Nakamura43642b32011-07-11 11:22:16 -04001578 offset = 0;
1579 nb_sectors = bs->total_sectors;
Kevin Wolf191c2892010-09-16 13:18:08 +02001580
Devin Nakamura43642b32011-07-11 11:22:16 -04001581 do {
1582 num_checked = MIN(nb_sectors, INT_MAX);
1583 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1584 retstr = ret ? " allocated" : "not allocated";
1585 cvtstr(offset << 9ULL, s1, sizeof(s1));
1586 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1587 offset << 9ULL, num, num_checked, retstr, s1, ret);
Kevin Wolf191c2892010-09-16 13:18:08 +02001588
Devin Nakamura43642b32011-07-11 11:22:16 -04001589 offset += num;
1590 nb_sectors -= num;
1591 } while (offset < bs->total_sectors);
Kevin Wolf191c2892010-09-16 13:18:08 +02001592
Devin Nakamura43642b32011-07-11 11:22:16 -04001593 return 0;
Kevin Wolf191c2892010-09-16 13:18:08 +02001594}
1595
1596static const cmdinfo_t map_cmd = {
1597 .name = "map",
1598 .argmin = 0,
1599 .argmax = 0,
1600 .cfunc = map_f,
1601 .args = "",
1602 .oneline = "prints the allocated areas of a file",
1603};
1604
1605
Devin Nakamura43642b32011-07-11 11:22:16 -04001606static int close_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001607{
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001608 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001609 bs = NULL;
1610 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001611}
1612
1613static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001614 .name = "close",
1615 .altname = "c",
1616 .cfunc = close_f,
1617 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +00001618};
1619
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001620static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +00001621{
Devin Nakamura43642b32011-07-11 11:22:16 -04001622 if (bs) {
1623 fprintf(stderr, "file open already, try 'help close'\n");
1624 return 1;
1625 }
aliguorie3aff4f2009-04-05 19:14:04 +00001626
Devin Nakamura43642b32011-07-11 11:22:16 -04001627 if (growable) {
1628 if (bdrv_file_open(&bs, name, flags)) {
1629 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1630 return 1;
1631 }
1632 } else {
1633 bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +02001634
Devin Nakamura43642b32011-07-11 11:22:16 -04001635 if (bdrv_open(bs, name, flags, NULL) < 0) {
1636 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001637 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001638 bs = NULL;
1639 return 1;
1640 }
1641 }
Christoph Hellwig1db69472009-07-15 23:11:21 +02001642
Devin Nakamura43642b32011-07-11 11:22:16 -04001643 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001644}
1645
Devin Nakamura43642b32011-07-11 11:22:16 -04001646static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +00001647{
Devin Nakamura43642b32011-07-11 11:22:16 -04001648 printf(
aliguorie3aff4f2009-04-05 19:14:04 +00001649"\n"
1650" opens a new file in the requested mode\n"
1651"\n"
1652" Example:\n"
1653" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1654"\n"
1655" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001656" -r, -- open file read-only\n"
1657" -s, -- use snapshot file\n"
1658" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001659" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +00001660"\n");
1661}
1662
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001663static int open_f(int argc, char **argv);
1664
1665static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001666 .name = "open",
1667 .altname = "o",
1668 .cfunc = open_f,
1669 .argmin = 1,
1670 .argmax = -1,
1671 .flags = CMD_NOFILE_OK,
1672 .args = "[-Crsn] [path]",
1673 .oneline = "open the file specified by path",
1674 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001675};
aliguorie3aff4f2009-04-05 19:14:04 +00001676
Devin Nakamura43642b32011-07-11 11:22:16 -04001677static int open_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001678{
Devin Nakamura43642b32011-07-11 11:22:16 -04001679 int flags = 0;
1680 int readonly = 0;
1681 int growable = 0;
1682 int c;
aliguorie3aff4f2009-04-05 19:14:04 +00001683
Devin Nakamura43642b32011-07-11 11:22:16 -04001684 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1685 switch (c) {
1686 case 's':
1687 flags |= BDRV_O_SNAPSHOT;
1688 break;
1689 case 'n':
1690 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1691 break;
1692 case 'r':
1693 readonly = 1;
1694 break;
1695 case 'g':
1696 growable = 1;
1697 break;
1698 default:
1699 return command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001700 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001701 }
aliguorie3aff4f2009-04-05 19:14:04 +00001702
Devin Nakamura43642b32011-07-11 11:22:16 -04001703 if (!readonly) {
1704 flags |= BDRV_O_RDWR;
1705 }
aliguorie3aff4f2009-04-05 19:14:04 +00001706
Devin Nakamura43642b32011-07-11 11:22:16 -04001707 if (optind != argc - 1) {
1708 return command_usage(&open_cmd);
1709 }
1710
1711 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +00001712}
1713
Devin Nakamura43642b32011-07-11 11:22:16 -04001714static int init_args_command(int index)
aliguorie3aff4f2009-04-05 19:14:04 +00001715{
Devin Nakamura43642b32011-07-11 11:22:16 -04001716 /* only one device allowed so far */
1717 if (index >= 1) {
1718 return 0;
1719 }
1720 return ++index;
aliguorie3aff4f2009-04-05 19:14:04 +00001721}
1722
Devin Nakamura43642b32011-07-11 11:22:16 -04001723static int init_check_command(const cmdinfo_t *ct)
aliguorie3aff4f2009-04-05 19:14:04 +00001724{
Devin Nakamura43642b32011-07-11 11:22:16 -04001725 if (ct->flags & CMD_FLAG_GLOBAL) {
1726 return 1;
1727 }
1728 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1729 fprintf(stderr, "no file open, try 'help open'\n");
1730 return 0;
1731 }
1732 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +00001733}
1734
1735static void usage(const char *name)
1736{
Devin Nakamura43642b32011-07-11 11:22:16 -04001737 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +01001738"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +02001739"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001740"\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001741" -c, --cmd command to execute\n"
1742" -r, --read-only export read-only\n"
1743" -s, --snapshot use snapshot file\n"
1744" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +02001745" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001746" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001747" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001748" -h, --help display this help and exit\n"
1749" -V, --version output version information and exit\n"
1750"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -04001751 name);
aliguorie3aff4f2009-04-05 19:14:04 +00001752}
1753
1754
1755int main(int argc, char **argv)
1756{
Devin Nakamura43642b32011-07-11 11:22:16 -04001757 int readonly = 0;
1758 int growable = 0;
1759 const char *sopt = "hVc:rsnmgk";
1760 const struct option lopt[] = {
1761 { "help", 0, NULL, 'h' },
1762 { "version", 0, NULL, 'V' },
1763 { "offset", 1, NULL, 'o' },
1764 { "cmd", 1, NULL, 'c' },
1765 { "read-only", 0, NULL, 'r' },
1766 { "snapshot", 0, NULL, 's' },
1767 { "nocache", 0, NULL, 'n' },
1768 { "misalign", 0, NULL, 'm' },
1769 { "growable", 0, NULL, 'g' },
1770 { "native-aio", 0, NULL, 'k' },
1771 { NULL, 0, NULL, 0 }
1772 };
1773 int c;
1774 int opt_index = 0;
1775 int flags = 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001776
Devin Nakamura43642b32011-07-11 11:22:16 -04001777 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +00001778
Devin Nakamura43642b32011-07-11 11:22:16 -04001779 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1780 switch (c) {
1781 case 's':
1782 flags |= BDRV_O_SNAPSHOT;
1783 break;
1784 case 'n':
1785 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1786 break;
1787 case 'c':
1788 add_user_command(optarg);
1789 break;
1790 case 'r':
1791 readonly = 1;
1792 break;
1793 case 'm':
1794 misalign = 1;
1795 break;
1796 case 'g':
1797 growable = 1;
1798 break;
1799 case 'k':
1800 flags |= BDRV_O_NATIVE_AIO;
1801 break;
1802 case 'V':
1803 printf("%s version %s\n", progname, VERSION);
1804 exit(0);
1805 case 'h':
1806 usage(progname);
1807 exit(0);
1808 default:
1809 usage(progname);
1810 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001811 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001812 }
aliguorie3aff4f2009-04-05 19:14:04 +00001813
Devin Nakamura43642b32011-07-11 11:22:16 -04001814 if ((argc - optind) > 1) {
1815 usage(progname);
1816 exit(1);
1817 }
aliguorie3aff4f2009-04-05 19:14:04 +00001818
Devin Nakamura43642b32011-07-11 11:22:16 -04001819 bdrv_init();
Christoph Hellwig95533d52009-06-20 21:10:44 +02001820
Devin Nakamura43642b32011-07-11 11:22:16 -04001821 /* initialize commands */
1822 quit_init();
1823 help_init();
1824 add_command(&open_cmd);
1825 add_command(&close_cmd);
1826 add_command(&read_cmd);
1827 add_command(&readv_cmd);
1828 add_command(&write_cmd);
1829 add_command(&writev_cmd);
1830 add_command(&multiwrite_cmd);
1831 add_command(&aio_read_cmd);
1832 add_command(&aio_write_cmd);
1833 add_command(&aio_flush_cmd);
1834 add_command(&flush_cmd);
1835 add_command(&truncate_cmd);
1836 add_command(&length_cmd);
1837 add_command(&info_cmd);
1838 add_command(&discard_cmd);
1839 add_command(&alloc_cmd);
1840 add_command(&map_cmd);
1841
1842 add_args_command(init_args_command);
1843 add_check_command(init_check_command);
1844
1845 /* open the device */
1846 if (!readonly) {
1847 flags |= BDRV_O_RDWR;
1848 }
1849
1850 if ((argc - optind) == 1) {
1851 openfile(argv[optind], flags, growable);
1852 }
1853 command_loop();
1854
1855 /*
1856 * Make sure all outstanding requests get flushed the program exits.
1857 */
1858 qemu_aio_flush();
1859
1860 if (bs) {
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001861 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001862 }
1863 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001864}