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