blob: 1c49d447aa15f376229e32f67c853266429d3fa9 [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
Devin Nakamura43642b32011-07-11 11:22:16 -0400963 for (i = 0; i < nr_reqs; i++) {
964 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]);
970 return 0;
971 }
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);
977 return 0;
978 }
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
Devin Nakamura43642b32011-07-11 11:22:16 -04001008 gettimeofday(&t1, NULL);
1009 cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
1010 gettimeofday(&t2, NULL);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001011
Devin Nakamura43642b32011-07-11 11:22:16 -04001012 if (cnt < 0) {
1013 printf("aio_multiwrite failed: %s\n", strerror(-cnt));
1014 goto out;
1015 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001016
Devin Nakamura43642b32011-07-11 11:22:16 -04001017 if (qflag) {
1018 goto out;
1019 }
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001020
Devin Nakamura43642b32011-07-11 11:22:16 -04001021 /* Finally, report back -- -C gives a parsable format */
1022 t2 = tsub(t2, t1);
1023 print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001024out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001025 for (i = 0; i < nr_reqs; i++) {
1026 qemu_io_free(buf[i]);
Kevin Wolff2360622011-10-31 11:36:32 +01001027 if (reqs[i].qiov != NULL) {
1028 qemu_iovec_destroy(&qiovs[i]);
1029 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001030 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001031 g_free(buf);
1032 g_free(reqs);
1033 g_free(qiovs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001034 return 0;
Kevin Wolf776cbbb2010-05-21 11:37:26 +02001035}
1036
Christoph Hellwig95533d52009-06-20 21:10:44 +02001037struct aio_ctx {
Devin Nakamura43642b32011-07-11 11:22:16 -04001038 QEMUIOVector qiov;
1039 int64_t offset;
1040 char *buf;
1041 int qflag;
1042 int vflag;
1043 int Cflag;
1044 int Pflag;
1045 int pattern;
1046 struct timeval t1;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001047};
1048
Devin Nakamura43642b32011-07-11 11:22:16 -04001049static void aio_write_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001050{
Devin Nakamura43642b32011-07-11 11:22:16 -04001051 struct aio_ctx *ctx = opaque;
1052 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001053
Devin Nakamura43642b32011-07-11 11:22:16 -04001054 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001055
Christoph Hellwig95533d52009-06-20 21:10:44 +02001056
Devin Nakamura43642b32011-07-11 11:22:16 -04001057 if (ret < 0) {
1058 printf("aio_write failed: %s\n", strerror(-ret));
1059 goto out;
1060 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001061
Devin Nakamura43642b32011-07-11 11:22:16 -04001062 if (ctx->qflag) {
1063 goto out;
1064 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001065
Devin Nakamura43642b32011-07-11 11:22:16 -04001066 /* Finally, report back -- -C gives a parsable format */
1067 t2 = tsub(t2, ctx->t1);
1068 print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
1069 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001070out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001071 qemu_io_free(ctx->buf);
1072 free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001073}
1074
Devin Nakamura43642b32011-07-11 11:22:16 -04001075static void aio_read_done(void *opaque, int ret)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001076{
Devin Nakamura43642b32011-07-11 11:22:16 -04001077 struct aio_ctx *ctx = opaque;
1078 struct timeval t2;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001079
Devin Nakamura43642b32011-07-11 11:22:16 -04001080 gettimeofday(&t2, NULL);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001081
Devin Nakamura43642b32011-07-11 11:22:16 -04001082 if (ret < 0) {
1083 printf("readv failed: %s\n", strerror(-ret));
1084 goto out;
1085 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001086
Devin Nakamura43642b32011-07-11 11:22:16 -04001087 if (ctx->Pflag) {
1088 void *cmp_buf = malloc(ctx->qiov.size);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001089
Devin Nakamura43642b32011-07-11 11:22:16 -04001090 memset(cmp_buf, ctx->pattern, ctx->qiov.size);
1091 if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
1092 printf("Pattern verification failed at offset %"
1093 PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
1094 }
1095 free(cmp_buf);
1096 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001097
Devin Nakamura43642b32011-07-11 11:22:16 -04001098 if (ctx->qflag) {
1099 goto out;
1100 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001101
Devin Nakamura43642b32011-07-11 11:22:16 -04001102 if (ctx->vflag) {
1103 dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
1104 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001105
Devin Nakamura43642b32011-07-11 11:22:16 -04001106 /* Finally, report back -- -C gives a parsable format */
1107 t2 = tsub(t2, ctx->t1);
1108 print_report("read", &t2, ctx->offset, ctx->qiov.size,
1109 ctx->qiov.size, 1, ctx->Cflag);
Kevin Wolf7d8abfc2009-07-10 13:33:52 +02001110out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001111 qemu_io_free(ctx->buf);
1112 free(ctx);
Christoph Hellwig95533d52009-06-20 21:10:44 +02001113}
1114
Devin Nakamura43642b32011-07-11 11:22:16 -04001115static void aio_read_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001116{
Devin Nakamura43642b32011-07-11 11:22:16 -04001117 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001118"\n"
1119" asynchronously reads a range of bytes from the given offset\n"
1120"\n"
1121" Example:\n"
1122" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
1123"\n"
1124" Reads a segment of the currently open file, optionally dumping it to the\n"
1125" standard output stream (with -v option) for subsequent inspection.\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001126" The read is performed asynchronously and the aio_flush command must be\n"
1127" used to ensure all outstanding aio requests have been completed\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001128" -C, -- report statistics in a machine parsable format\n"
1129" -P, -- use a pattern to verify read data\n"
1130" -v, -- dump buffer to standard output\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001131" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001132"\n");
1133}
1134
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001135static int aio_read_f(int argc, char **argv);
1136
1137static const cmdinfo_t aio_read_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001138 .name = "aio_read",
1139 .cfunc = aio_read_f,
1140 .argmin = 2,
1141 .argmax = -1,
1142 .args = "[-Cqv] [-P pattern ] off len [len..]",
1143 .oneline = "asynchronously reads a number of bytes",
1144 .help = aio_read_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001145};
1146
Devin Nakamura43642b32011-07-11 11:22:16 -04001147static int aio_read_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001148{
Devin Nakamura43642b32011-07-11 11:22:16 -04001149 int nr_iov, c;
1150 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1151 BlockDriverAIOCB *acb;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001152
Devin Nakamura43642b32011-07-11 11:22:16 -04001153 while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
1154 switch (c) {
1155 case 'C':
1156 ctx->Cflag = 1;
1157 break;
1158 case 'P':
1159 ctx->Pflag = 1;
1160 ctx->pattern = parse_pattern(optarg);
1161 if (ctx->pattern < 0) {
1162 free(ctx);
1163 return 0;
1164 }
1165 break;
1166 case 'q':
1167 ctx->qflag = 1;
1168 break;
1169 case 'v':
1170 ctx->vflag = 1;
1171 break;
1172 default:
1173 free(ctx);
1174 return command_usage(&aio_read_cmd);
1175 }
1176 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001177
Devin Nakamura43642b32011-07-11 11:22:16 -04001178 if (optind > argc - 2) {
1179 free(ctx);
1180 return command_usage(&aio_read_cmd);
1181 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001182
Devin Nakamura43642b32011-07-11 11:22:16 -04001183 ctx->offset = cvtnum(argv[optind]);
1184 if (ctx->offset < 0) {
1185 printf("non-numeric length argument -- %s\n", argv[optind]);
1186 free(ctx);
1187 return 0;
1188 }
1189 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001190
Devin Nakamura43642b32011-07-11 11:22:16 -04001191 if (ctx->offset & 0x1ff) {
1192 printf("offset %" PRId64 " is not sector aligned\n",
1193 ctx->offset);
1194 free(ctx);
1195 return 0;
1196 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001197
Devin Nakamura43642b32011-07-11 11:22:16 -04001198 nr_iov = argc - optind;
1199 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
Kevin Wolff2360622011-10-31 11:36:32 +01001200 if (ctx->buf == NULL) {
1201 free(ctx);
1202 return 0;
1203 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001204
Devin Nakamura43642b32011-07-11 11:22:16 -04001205 gettimeofday(&ctx->t1, NULL);
1206 acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
1207 ctx->qiov.size >> 9, aio_read_done, ctx);
1208 if (!acb) {
1209 free(ctx->buf);
1210 free(ctx);
1211 return -EIO;
1212 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001213
Devin Nakamura43642b32011-07-11 11:22:16 -04001214 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001215}
1216
Devin Nakamura43642b32011-07-11 11:22:16 -04001217static void aio_write_help(void)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001218{
Devin Nakamura43642b32011-07-11 11:22:16 -04001219 printf(
Christoph Hellwig95533d52009-06-20 21:10:44 +02001220"\n"
Devin Nakamura43642b32011-07-11 11:22:16 -04001221" asynchronously writes a range of bytes from the given offset source\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001222" from multiple buffers\n"
1223"\n"
1224" Example:\n"
1225" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
1226"\n"
1227" Writes into a segment of the currently open file, using a buffer\n"
1228" filled with a set pattern (0xcdcdcdcd).\n"
Christoph Hellwige432cef2010-03-28 12:19:31 +02001229" The write is performed asynchronously and the aio_flush command must be\n"
1230" used to ensure all outstanding aio requests have been completed\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001231" -P, -- use different pattern to fill file\n"
1232" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001233" -q, -- quiet mode, do not show I/O statistics\n"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001234"\n");
1235}
1236
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001237static int aio_write_f(int argc, char **argv);
1238
1239static const cmdinfo_t aio_write_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001240 .name = "aio_write",
1241 .cfunc = aio_write_f,
1242 .argmin = 2,
1243 .argmax = -1,
1244 .args = "[-Cq] [-P pattern ] off len [len..]",
1245 .oneline = "asynchronously writes a number of bytes",
1246 .help = aio_write_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001247};
Christoph Hellwig95533d52009-06-20 21:10:44 +02001248
Devin Nakamura43642b32011-07-11 11:22:16 -04001249static int aio_write_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001250{
Devin Nakamura43642b32011-07-11 11:22:16 -04001251 int nr_iov, c;
1252 int pattern = 0xcd;
1253 struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));
1254 BlockDriverAIOCB *acb;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001255
Devin Nakamura43642b32011-07-11 11:22:16 -04001256 while ((c = getopt(argc, argv, "CqP:")) != EOF) {
1257 switch (c) {
1258 case 'C':
1259 ctx->Cflag = 1;
1260 break;
1261 case 'q':
1262 ctx->qflag = 1;
1263 break;
1264 case 'P':
1265 pattern = parse_pattern(optarg);
1266 if (pattern < 0) {
Alex Jiab1b1dad2011-09-28 14:57:01 +08001267 free(ctx);
Devin Nakamura43642b32011-07-11 11:22:16 -04001268 return 0;
1269 }
1270 break;
1271 default:
1272 free(ctx);
1273 return command_usage(&aio_write_cmd);
1274 }
1275 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001276
Devin Nakamura43642b32011-07-11 11:22:16 -04001277 if (optind > argc - 2) {
1278 free(ctx);
1279 return command_usage(&aio_write_cmd);
1280 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001281
Devin Nakamura43642b32011-07-11 11:22:16 -04001282 ctx->offset = cvtnum(argv[optind]);
1283 if (ctx->offset < 0) {
1284 printf("non-numeric length argument -- %s\n", argv[optind]);
1285 free(ctx);
1286 return 0;
1287 }
1288 optind++;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001289
Devin Nakamura43642b32011-07-11 11:22:16 -04001290 if (ctx->offset & 0x1ff) {
1291 printf("offset %" PRId64 " is not sector aligned\n",
1292 ctx->offset);
1293 free(ctx);
1294 return 0;
1295 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001296
Devin Nakamura43642b32011-07-11 11:22:16 -04001297 nr_iov = argc - optind;
1298 ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
Kevin Wolff2360622011-10-31 11:36:32 +01001299 if (ctx->buf == NULL) {
1300 free(ctx);
1301 return 0;
1302 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001303
Devin Nakamura43642b32011-07-11 11:22:16 -04001304 gettimeofday(&ctx->t1, NULL);
1305 acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
1306 ctx->qiov.size >> 9, aio_write_done, ctx);
1307 if (!acb) {
1308 free(ctx->buf);
1309 free(ctx);
1310 return -EIO;
1311 }
Christoph Hellwig95533d52009-06-20 21:10:44 +02001312
Devin Nakamura43642b32011-07-11 11:22:16 -04001313 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001314}
1315
Devin Nakamura43642b32011-07-11 11:22:16 -04001316static int aio_flush_f(int argc, char **argv)
Christoph Hellwig95533d52009-06-20 21:10:44 +02001317{
Devin Nakamura43642b32011-07-11 11:22:16 -04001318 qemu_aio_flush();
1319 return 0;
Christoph Hellwig95533d52009-06-20 21:10:44 +02001320}
1321
1322static const cmdinfo_t aio_flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001323 .name = "aio_flush",
1324 .cfunc = aio_flush_f,
1325 .oneline = "completes all outstanding aio requests"
Christoph Hellwig95533d52009-06-20 21:10:44 +02001326};
1327
Devin Nakamura43642b32011-07-11 11:22:16 -04001328static int flush_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001329{
Devin Nakamura43642b32011-07-11 11:22:16 -04001330 bdrv_flush(bs);
1331 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001332}
1333
1334static const cmdinfo_t flush_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001335 .name = "flush",
1336 .altname = "f",
1337 .cfunc = flush_f,
1338 .oneline = "flush all in-core file state to disk",
aliguorie3aff4f2009-04-05 19:14:04 +00001339};
1340
Devin Nakamura43642b32011-07-11 11:22:16 -04001341static int truncate_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001342{
Devin Nakamura43642b32011-07-11 11:22:16 -04001343 int64_t offset;
1344 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001345
Devin Nakamura43642b32011-07-11 11:22:16 -04001346 offset = cvtnum(argv[1]);
1347 if (offset < 0) {
1348 printf("non-numeric truncate argument -- %s\n", argv[1]);
1349 return 0;
1350 }
aliguorie3aff4f2009-04-05 19:14:04 +00001351
Devin Nakamura43642b32011-07-11 11:22:16 -04001352 ret = bdrv_truncate(bs, offset);
1353 if (ret < 0) {
1354 printf("truncate: %s\n", strerror(-ret));
1355 return 0;
1356 }
aliguorie3aff4f2009-04-05 19:14:04 +00001357
Devin Nakamura43642b32011-07-11 11:22:16 -04001358 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001359}
1360
1361static const cmdinfo_t truncate_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001362 .name = "truncate",
1363 .altname = "t",
1364 .cfunc = truncate_f,
1365 .argmin = 1,
1366 .argmax = 1,
1367 .args = "off",
1368 .oneline = "truncates the current file at the given offset",
aliguorie3aff4f2009-04-05 19:14:04 +00001369};
1370
Devin Nakamura43642b32011-07-11 11:22:16 -04001371static int length_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001372{
Devin Nakamura43642b32011-07-11 11:22:16 -04001373 int64_t size;
1374 char s1[64];
aliguorie3aff4f2009-04-05 19:14:04 +00001375
Devin Nakamura43642b32011-07-11 11:22:16 -04001376 size = bdrv_getlength(bs);
1377 if (size < 0) {
1378 printf("getlength: %s\n", strerror(-size));
1379 return 0;
1380 }
aliguorie3aff4f2009-04-05 19:14:04 +00001381
Devin Nakamura43642b32011-07-11 11:22:16 -04001382 cvtstr(size, s1, sizeof(s1));
1383 printf("%s\n", s1);
1384 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001385}
1386
1387
1388static const cmdinfo_t length_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001389 .name = "length",
1390 .altname = "l",
1391 .cfunc = length_f,
1392 .oneline = "gets the length of the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001393};
1394
1395
Devin Nakamura43642b32011-07-11 11:22:16 -04001396static int info_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001397{
Devin Nakamura43642b32011-07-11 11:22:16 -04001398 BlockDriverInfo bdi;
1399 char s1[64], s2[64];
1400 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001401
Devin Nakamura43642b32011-07-11 11:22:16 -04001402 if (bs->drv && bs->drv->format_name) {
1403 printf("format name: %s\n", bs->drv->format_name);
1404 }
1405 if (bs->drv && bs->drv->protocol_name) {
1406 printf("format name: %s\n", bs->drv->protocol_name);
1407 }
aliguorie3aff4f2009-04-05 19:14:04 +00001408
Devin Nakamura43642b32011-07-11 11:22:16 -04001409 ret = bdrv_get_info(bs, &bdi);
1410 if (ret) {
1411 return 0;
1412 }
aliguorie3aff4f2009-04-05 19:14:04 +00001413
Devin Nakamura43642b32011-07-11 11:22:16 -04001414 cvtstr(bdi.cluster_size, s1, sizeof(s1));
1415 cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
aliguorie3aff4f2009-04-05 19:14:04 +00001416
Devin Nakamura43642b32011-07-11 11:22:16 -04001417 printf("cluster size: %s\n", s1);
1418 printf("vm state offset: %s\n", s2);
aliguorie3aff4f2009-04-05 19:14:04 +00001419
Devin Nakamura43642b32011-07-11 11:22:16 -04001420 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001421}
1422
1423
1424
1425static const cmdinfo_t info_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001426 .name = "info",
1427 .altname = "i",
1428 .cfunc = info_f,
1429 .oneline = "prints information about the current file",
aliguorie3aff4f2009-04-05 19:14:04 +00001430};
1431
Devin Nakamura43642b32011-07-11 11:22:16 -04001432static void discard_help(void)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001433{
Devin Nakamura43642b32011-07-11 11:22:16 -04001434 printf(
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001435"\n"
1436" discards a range of bytes from the given offset\n"
1437"\n"
1438" Example:\n"
1439" 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
1440"\n"
1441" Discards a segment of the currently open file.\n"
1442" -C, -- report statistics in a machine parsable format\n"
Kevin Wolf095343a2010-12-17 11:55:37 +01001443" -q, -- quiet mode, do not show I/O statistics\n"
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001444"\n");
1445}
1446
1447static int discard_f(int argc, char **argv);
1448
1449static const cmdinfo_t discard_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001450 .name = "discard",
1451 .altname = "d",
1452 .cfunc = discard_f,
1453 .argmin = 2,
1454 .argmax = -1,
1455 .args = "[-Cq] off len",
1456 .oneline = "discards a number of bytes at a specified offset",
1457 .help = discard_help,
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001458};
1459
Devin Nakamura43642b32011-07-11 11:22:16 -04001460static int discard_f(int argc, char **argv)
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001461{
Devin Nakamura43642b32011-07-11 11:22:16 -04001462 struct timeval t1, t2;
1463 int Cflag = 0, qflag = 0;
1464 int c, ret;
1465 int64_t offset;
1466 int count;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001467
Devin Nakamura43642b32011-07-11 11:22:16 -04001468 while ((c = getopt(argc, argv, "Cq")) != EOF) {
1469 switch (c) {
1470 case 'C':
1471 Cflag = 1;
1472 break;
1473 case 'q':
1474 qflag = 1;
1475 break;
1476 default:
1477 return command_usage(&discard_cmd);
1478 }
1479 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001480
Devin Nakamura43642b32011-07-11 11:22:16 -04001481 if (optind != argc - 2) {
1482 return command_usage(&discard_cmd);
1483 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001484
Devin Nakamura43642b32011-07-11 11:22:16 -04001485 offset = cvtnum(argv[optind]);
1486 if (offset < 0) {
1487 printf("non-numeric length argument -- %s\n", argv[optind]);
1488 return 0;
1489 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001490
Devin Nakamura43642b32011-07-11 11:22:16 -04001491 optind++;
1492 count = cvtnum(argv[optind]);
1493 if (count < 0) {
1494 printf("non-numeric length argument -- %s\n", argv[optind]);
1495 return 0;
1496 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001497
Devin Nakamura43642b32011-07-11 11:22:16 -04001498 gettimeofday(&t1, NULL);
1499 ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
1500 count >> BDRV_SECTOR_BITS);
1501 gettimeofday(&t2, NULL);
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001502
Devin Nakamura43642b32011-07-11 11:22:16 -04001503 if (ret < 0) {
1504 printf("discard failed: %s\n", strerror(-ret));
1505 goto out;
1506 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001507
Devin Nakamura43642b32011-07-11 11:22:16 -04001508 /* Finally, report back -- -C gives a parsable format */
1509 if (!qflag) {
1510 t2 = tsub(t2, t1);
1511 print_report("discard", &t2, offset, count, count, 1, Cflag);
1512 }
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001513
1514out:
Devin Nakamura43642b32011-07-11 11:22:16 -04001515 return 0;
Stefan Hajnocziedff5db2010-12-13 09:36:26 +00001516}
1517
Devin Nakamura43642b32011-07-11 11:22:16 -04001518static int alloc_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001519{
Devin Nakamura43642b32011-07-11 11:22:16 -04001520 int64_t offset;
1521 int nb_sectors, remaining;
1522 char s1[64];
1523 int num, sum_alloc;
1524 int ret;
aliguorie3aff4f2009-04-05 19:14:04 +00001525
Devin Nakamura43642b32011-07-11 11:22:16 -04001526 offset = cvtnum(argv[1]);
1527 if (offset & 0x1ff) {
1528 printf("offset %" PRId64 " is not sector aligned\n",
1529 offset);
1530 return 0;
1531 }
aliguorie3aff4f2009-04-05 19:14:04 +00001532
Devin Nakamura43642b32011-07-11 11:22:16 -04001533 if (argc == 3) {
1534 nb_sectors = cvtnum(argv[2]);
1535 } else {
1536 nb_sectors = 1;
1537 }
aliguorie3aff4f2009-04-05 19:14:04 +00001538
Devin Nakamura43642b32011-07-11 11:22:16 -04001539 remaining = nb_sectors;
1540 sum_alloc = 0;
1541 while (remaining) {
1542 ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num);
1543 remaining -= num;
1544 if (ret) {
1545 sum_alloc += num;
1546 }
1547 }
aliguorie3aff4f2009-04-05 19:14:04 +00001548
Devin Nakamura43642b32011-07-11 11:22:16 -04001549 cvtstr(offset, s1, sizeof(s1));
aliguorie3aff4f2009-04-05 19:14:04 +00001550
Devin Nakamura43642b32011-07-11 11:22:16 -04001551 printf("%d/%d sectors allocated at offset %s\n",
1552 sum_alloc, nb_sectors, s1);
1553 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001554}
1555
1556static const cmdinfo_t alloc_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001557 .name = "alloc",
1558 .altname = "a",
1559 .argmin = 1,
1560 .argmax = 2,
1561 .cfunc = alloc_f,
1562 .args = "off [sectors]",
1563 .oneline = "checks if a sector is present in the file",
aliguorie3aff4f2009-04-05 19:14:04 +00001564};
1565
Devin Nakamura43642b32011-07-11 11:22:16 -04001566static int map_f(int argc, char **argv)
Kevin Wolf191c2892010-09-16 13:18:08 +02001567{
Devin Nakamura43642b32011-07-11 11:22:16 -04001568 int64_t offset;
1569 int64_t nb_sectors;
1570 char s1[64];
1571 int num, num_checked;
1572 int ret;
1573 const char *retstr;
Kevin Wolf191c2892010-09-16 13:18:08 +02001574
Devin Nakamura43642b32011-07-11 11:22:16 -04001575 offset = 0;
1576 nb_sectors = bs->total_sectors;
Kevin Wolf191c2892010-09-16 13:18:08 +02001577
Devin Nakamura43642b32011-07-11 11:22:16 -04001578 do {
1579 num_checked = MIN(nb_sectors, INT_MAX);
1580 ret = bdrv_is_allocated(bs, offset, num_checked, &num);
1581 retstr = ret ? " allocated" : "not allocated";
1582 cvtstr(offset << 9ULL, s1, sizeof(s1));
1583 printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
1584 offset << 9ULL, num, num_checked, retstr, s1, ret);
Kevin Wolf191c2892010-09-16 13:18:08 +02001585
Devin Nakamura43642b32011-07-11 11:22:16 -04001586 offset += num;
1587 nb_sectors -= num;
1588 } while (offset < bs->total_sectors);
Kevin Wolf191c2892010-09-16 13:18:08 +02001589
Devin Nakamura43642b32011-07-11 11:22:16 -04001590 return 0;
Kevin Wolf191c2892010-09-16 13:18:08 +02001591}
1592
1593static const cmdinfo_t map_cmd = {
1594 .name = "map",
1595 .argmin = 0,
1596 .argmax = 0,
1597 .cfunc = map_f,
1598 .args = "",
1599 .oneline = "prints the allocated areas of a file",
1600};
1601
1602
Devin Nakamura43642b32011-07-11 11:22:16 -04001603static int close_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001604{
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001605 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001606 bs = NULL;
1607 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001608}
1609
1610static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001611 .name = "close",
1612 .altname = "c",
1613 .cfunc = close_f,
1614 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +00001615};
1616
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001617static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +00001618{
Devin Nakamura43642b32011-07-11 11:22:16 -04001619 if (bs) {
1620 fprintf(stderr, "file open already, try 'help close'\n");
1621 return 1;
1622 }
aliguorie3aff4f2009-04-05 19:14:04 +00001623
Devin Nakamura43642b32011-07-11 11:22:16 -04001624 if (growable) {
1625 if (bdrv_file_open(&bs, name, flags)) {
1626 fprintf(stderr, "%s: can't open device %s\n", progname, name);
1627 return 1;
1628 }
1629 } else {
1630 bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +02001631
Devin Nakamura43642b32011-07-11 11:22:16 -04001632 if (bdrv_open(bs, name, flags, NULL) < 0) {
1633 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001634 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001635 bs = NULL;
1636 return 1;
1637 }
1638 }
Christoph Hellwig1db69472009-07-15 23:11:21 +02001639
Devin Nakamura43642b32011-07-11 11:22:16 -04001640 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001641}
1642
Devin Nakamura43642b32011-07-11 11:22:16 -04001643static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +00001644{
Devin Nakamura43642b32011-07-11 11:22:16 -04001645 printf(
aliguorie3aff4f2009-04-05 19:14:04 +00001646"\n"
1647" opens a new file in the requested mode\n"
1648"\n"
1649" Example:\n"
1650" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1651"\n"
1652" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001653" -r, -- open file read-only\n"
1654" -s, -- use snapshot file\n"
1655" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +02001656" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +00001657"\n");
1658}
1659
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001660static int open_f(int argc, char **argv);
1661
1662static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -04001663 .name = "open",
1664 .altname = "o",
1665 .cfunc = open_f,
1666 .argmin = 1,
1667 .argmax = -1,
1668 .flags = CMD_NOFILE_OK,
1669 .args = "[-Crsn] [path]",
1670 .oneline = "open the file specified by path",
1671 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +00001672};
aliguorie3aff4f2009-04-05 19:14:04 +00001673
Devin Nakamura43642b32011-07-11 11:22:16 -04001674static int open_f(int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +00001675{
Devin Nakamura43642b32011-07-11 11:22:16 -04001676 int flags = 0;
1677 int readonly = 0;
1678 int growable = 0;
1679 int c;
aliguorie3aff4f2009-04-05 19:14:04 +00001680
Devin Nakamura43642b32011-07-11 11:22:16 -04001681 while ((c = getopt(argc, argv, "snrg")) != EOF) {
1682 switch (c) {
1683 case 's':
1684 flags |= BDRV_O_SNAPSHOT;
1685 break;
1686 case 'n':
1687 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1688 break;
1689 case 'r':
1690 readonly = 1;
1691 break;
1692 case 'g':
1693 growable = 1;
1694 break;
1695 default:
1696 return command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001697 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001698 }
aliguorie3aff4f2009-04-05 19:14:04 +00001699
Devin Nakamura43642b32011-07-11 11:22:16 -04001700 if (!readonly) {
1701 flags |= BDRV_O_RDWR;
1702 }
aliguorie3aff4f2009-04-05 19:14:04 +00001703
Devin Nakamura43642b32011-07-11 11:22:16 -04001704 if (optind != argc - 1) {
1705 return command_usage(&open_cmd);
1706 }
1707
1708 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +00001709}
1710
Devin Nakamura43642b32011-07-11 11:22:16 -04001711static int init_args_command(int index)
aliguorie3aff4f2009-04-05 19:14:04 +00001712{
Devin Nakamura43642b32011-07-11 11:22:16 -04001713 /* only one device allowed so far */
1714 if (index >= 1) {
1715 return 0;
1716 }
1717 return ++index;
aliguorie3aff4f2009-04-05 19:14:04 +00001718}
1719
Devin Nakamura43642b32011-07-11 11:22:16 -04001720static int init_check_command(const cmdinfo_t *ct)
aliguorie3aff4f2009-04-05 19:14:04 +00001721{
Devin Nakamura43642b32011-07-11 11:22:16 -04001722 if (ct->flags & CMD_FLAG_GLOBAL) {
1723 return 1;
1724 }
1725 if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
1726 fprintf(stderr, "no file open, try 'help open'\n");
1727 return 0;
1728 }
1729 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +00001730}
1731
1732static void usage(const char *name)
1733{
Devin Nakamura43642b32011-07-11 11:22:16 -04001734 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +01001735"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +02001736"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001737"\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001738" -c, --cmd command to execute\n"
1739" -r, --read-only export read-only\n"
1740" -s, --snapshot use snapshot file\n"
1741" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +02001742" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001743" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001744" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
aliguorie3aff4f2009-04-05 19:14:04 +00001745" -h, --help display this help and exit\n"
1746" -V, --version output version information and exit\n"
1747"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -04001748 name);
aliguorie3aff4f2009-04-05 19:14:04 +00001749}
1750
1751
1752int main(int argc, char **argv)
1753{
Devin Nakamura43642b32011-07-11 11:22:16 -04001754 int readonly = 0;
1755 int growable = 0;
1756 const char *sopt = "hVc:rsnmgk";
1757 const struct option lopt[] = {
1758 { "help", 0, NULL, 'h' },
1759 { "version", 0, NULL, 'V' },
1760 { "offset", 1, NULL, 'o' },
1761 { "cmd", 1, NULL, 'c' },
1762 { "read-only", 0, NULL, 'r' },
1763 { "snapshot", 0, NULL, 's' },
1764 { "nocache", 0, NULL, 'n' },
1765 { "misalign", 0, NULL, 'm' },
1766 { "growable", 0, NULL, 'g' },
1767 { "native-aio", 0, NULL, 'k' },
1768 { NULL, 0, NULL, 0 }
1769 };
1770 int c;
1771 int opt_index = 0;
1772 int flags = 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001773
Devin Nakamura43642b32011-07-11 11:22:16 -04001774 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +00001775
Devin Nakamura43642b32011-07-11 11:22:16 -04001776 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
1777 switch (c) {
1778 case 's':
1779 flags |= BDRV_O_SNAPSHOT;
1780 break;
1781 case 'n':
1782 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
1783 break;
1784 case 'c':
1785 add_user_command(optarg);
1786 break;
1787 case 'r':
1788 readonly = 1;
1789 break;
1790 case 'm':
1791 misalign = 1;
1792 break;
1793 case 'g':
1794 growable = 1;
1795 break;
1796 case 'k':
1797 flags |= BDRV_O_NATIVE_AIO;
1798 break;
1799 case 'V':
1800 printf("%s version %s\n", progname, VERSION);
1801 exit(0);
1802 case 'h':
1803 usage(progname);
1804 exit(0);
1805 default:
1806 usage(progname);
1807 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +02001808 }
Devin Nakamura43642b32011-07-11 11:22:16 -04001809 }
aliguorie3aff4f2009-04-05 19:14:04 +00001810
Devin Nakamura43642b32011-07-11 11:22:16 -04001811 if ((argc - optind) > 1) {
1812 usage(progname);
1813 exit(1);
1814 }
aliguorie3aff4f2009-04-05 19:14:04 +00001815
Devin Nakamura43642b32011-07-11 11:22:16 -04001816 bdrv_init();
Christoph Hellwig95533d52009-06-20 21:10:44 +02001817
Devin Nakamura43642b32011-07-11 11:22:16 -04001818 /* initialize commands */
1819 quit_init();
1820 help_init();
1821 add_command(&open_cmd);
1822 add_command(&close_cmd);
1823 add_command(&read_cmd);
1824 add_command(&readv_cmd);
1825 add_command(&write_cmd);
1826 add_command(&writev_cmd);
1827 add_command(&multiwrite_cmd);
1828 add_command(&aio_read_cmd);
1829 add_command(&aio_write_cmd);
1830 add_command(&aio_flush_cmd);
1831 add_command(&flush_cmd);
1832 add_command(&truncate_cmd);
1833 add_command(&length_cmd);
1834 add_command(&info_cmd);
1835 add_command(&discard_cmd);
1836 add_command(&alloc_cmd);
1837 add_command(&map_cmd);
1838
1839 add_args_command(init_args_command);
1840 add_check_command(init_check_command);
1841
1842 /* open the device */
1843 if (!readonly) {
1844 flags |= BDRV_O_RDWR;
1845 }
1846
1847 if ((argc - optind) == 1) {
1848 openfile(argv[optind], flags, growable);
1849 }
1850 command_loop();
1851
1852 /*
1853 * Make sure all outstanding requests get flushed the program exits.
1854 */
1855 qemu_aio_flush();
1856
1857 if (bs) {
Stefan Hajnoczib4657852011-10-27 10:54:26 +01001858 bdrv_delete(bs);
Devin Nakamura43642b32011-07-11 11:22:16 -04001859 }
1860 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +00001861}