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 | */ |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 10 | #include <sys/time.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 11 | #include <sys/types.h> |
| 12 | #include <stdarg.h> |
| 13 | #include <stdio.h> |
| 14 | #include <getopt.h> |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 15 | #include <libgen.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 16 | |
| 17 | #include "qemu-common.h" |
| 18 | #include "block_int.h" |
| 19 | #include "cmd.h" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 20 | #include "trace/control.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 21 | |
| 22 | #define VERSION "0.0.1" |
| 23 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 24 | #define CMD_NOFILE_OK 0x01 |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 25 | |
| 26 | char *progname; |
| 27 | static BlockDriverState *bs; |
| 28 | |
| 29 | static int misalign; |
| 30 | |
| 31 | /* |
Christoph Hellwig | cf070d7 | 2009-07-20 01:19:25 +0200 | [diff] [blame] | 32 | * Parse the pattern argument to various sub-commands. |
| 33 | * |
| 34 | * Because the pattern is used as an argument to memset it must evaluate |
| 35 | * to an unsigned integer that fits into a single byte. |
| 36 | */ |
| 37 | static int parse_pattern(const char *arg) |
| 38 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 39 | char *endptr = NULL; |
| 40 | long pattern; |
Christoph Hellwig | cf070d7 | 2009-07-20 01:19:25 +0200 | [diff] [blame] | 41 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 42 | pattern = strtol(arg, &endptr, 0); |
| 43 | if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') { |
| 44 | printf("%s is not a valid pattern byte\n", arg); |
| 45 | return -1; |
| 46 | } |
Christoph Hellwig | cf070d7 | 2009-07-20 01:19:25 +0200 | [diff] [blame] | 47 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 48 | return pattern; |
Christoph Hellwig | cf070d7 | 2009-07-20 01:19:25 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 52 | * Memory allocation helpers. |
| 53 | * |
| 54 | * Make sure memory is aligned by default, or purposefully misaligned if |
| 55 | * that is specified on the command line. |
| 56 | */ |
| 57 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 58 | #define MISALIGN_OFFSET 16 |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 59 | static void *qemu_io_alloc(size_t len, int pattern) |
| 60 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 61 | void *buf; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 62 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 63 | if (misalign) { |
| 64 | len += MISALIGN_OFFSET; |
| 65 | } |
| 66 | buf = qemu_blockalign(bs, len); |
| 67 | memset(buf, pattern, len); |
| 68 | if (misalign) { |
| 69 | buf += MISALIGN_OFFSET; |
| 70 | } |
| 71 | return buf; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void qemu_io_free(void *p) |
| 75 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 76 | if (misalign) { |
| 77 | p -= MISALIGN_OFFSET; |
| 78 | } |
| 79 | qemu_vfree(p); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 82 | static void dump_buffer(const void *buffer, int64_t offset, int len) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 83 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 84 | int i, j; |
| 85 | const uint8_t *p; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 86 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 87 | for (i = 0, p = buffer; i < len; i += 16) { |
| 88 | const uint8_t *s = p; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 89 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 90 | printf("%08" PRIx64 ": ", offset + i); |
| 91 | for (j = 0; j < 16 && i + j < len; j++, p++) { |
| 92 | printf("%02x ", *p); |
| 93 | } |
| 94 | printf(" "); |
| 95 | for (j = 0; j < 16 && i + j < len; j++, s++) { |
| 96 | if (isalnum(*s)) { |
| 97 | printf("%c", *s); |
| 98 | } else { |
| 99 | printf("."); |
| 100 | } |
| 101 | } |
| 102 | printf("\n"); |
| 103 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 106 | static void print_report(const char *op, struct timeval *t, int64_t offset, |
| 107 | int count, int total, int cnt, int Cflag) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 108 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 109 | char s1[64], s2[64], ts[64]; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 110 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 111 | timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0); |
| 112 | if (!Cflag) { |
| 113 | cvtstr((double)total, s1, sizeof(s1)); |
| 114 | cvtstr(tdiv((double)total, *t), s2, sizeof(s2)); |
| 115 | printf("%s %d/%d bytes at offset %" PRId64 "\n", |
| 116 | op, total, count, offset); |
| 117 | printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n", |
| 118 | s1, cnt, ts, s2, tdiv((double)cnt, *t)); |
| 119 | } else {/* bytes,ops,time,bytes/sec,ops/sec */ |
| 120 | printf("%d,%d,%s,%.3f,%.3f\n", |
| 121 | total, cnt, ts, |
| 122 | tdiv((double)total, *t), |
| 123 | tdiv((double)cnt, *t)); |
| 124 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 127 | /* |
| 128 | * Parse multiple length statements for vectored I/O, and construct an I/O |
| 129 | * vector matching it. |
| 130 | */ |
| 131 | static void * |
| 132 | create_iovec(QEMUIOVector *qiov, char **argv, int nr_iov, int pattern) |
| 133 | { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 134 | size_t *sizes = g_new0(size_t, nr_iov); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 135 | size_t count = 0; |
| 136 | void *buf = NULL; |
| 137 | void *p; |
| 138 | int i; |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 139 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 140 | for (i = 0; i < nr_iov; i++) { |
| 141 | char *arg = argv[i]; |
| 142 | int64_t len; |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 143 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 144 | len = cvtnum(arg); |
| 145 | if (len < 0) { |
| 146 | printf("non-numeric length argument -- %s\n", arg); |
| 147 | goto fail; |
| 148 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 149 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 150 | /* should be SIZE_T_MAX, but that doesn't exist */ |
| 151 | if (len > INT_MAX) { |
| 152 | printf("too large length argument -- %s\n", arg); |
| 153 | goto fail; |
| 154 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 155 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 156 | if (len & 0x1ff) { |
| 157 | printf("length argument %" PRId64 |
| 158 | " is not sector aligned\n", len); |
| 159 | goto fail; |
| 160 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 161 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 162 | sizes[i] = len; |
| 163 | count += len; |
| 164 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 165 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 166 | qemu_iovec_init(qiov, nr_iov); |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 167 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 168 | buf = p = qemu_io_alloc(count, pattern); |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 169 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 170 | for (i = 0; i < nr_iov; i++) { |
| 171 | qemu_iovec_add(qiov, p, sizes[i]); |
| 172 | p += sizes[i]; |
| 173 | } |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 174 | |
Kevin Wolf | 40a0d7c | 2009-11-18 10:42:59 +0100 | [diff] [blame] | 175 | fail: |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 176 | g_free(sizes); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 177 | return buf; |
Christoph Hellwig | cf57298 | 2009-07-10 13:33:42 +0200 | [diff] [blame] | 178 | } |
| 179 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 180 | static int do_read(char *buf, int64_t offset, int count, int *total) |
| 181 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 182 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 183 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 184 | ret = bdrv_read(bs, offset >> 9, (uint8_t *)buf, count >> 9); |
| 185 | if (ret < 0) { |
| 186 | return ret; |
| 187 | } |
| 188 | *total = count; |
| 189 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int do_write(char *buf, int64_t offset, int count, int *total) |
| 193 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 194 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 195 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 196 | ret = bdrv_write(bs, offset >> 9, (uint8_t *)buf, count >> 9); |
| 197 | if (ret < 0) { |
| 198 | return ret; |
| 199 | } |
| 200 | *total = count; |
| 201 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int do_pread(char *buf, int64_t offset, int count, int *total) |
| 205 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 206 | *total = bdrv_pread(bs, offset, (uint8_t *)buf, count); |
| 207 | if (*total < 0) { |
| 208 | return *total; |
| 209 | } |
| 210 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static int do_pwrite(char *buf, int64_t offset, int count, int *total) |
| 214 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 215 | *total = bdrv_pwrite(bs, offset, (uint8_t *)buf, count); |
| 216 | if (*total < 0) { |
| 217 | return *total; |
| 218 | } |
| 219 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 222 | typedef struct { |
| 223 | int64_t offset; |
| 224 | int count; |
| 225 | int *total; |
| 226 | int ret; |
| 227 | bool done; |
| 228 | } CoWriteZeroes; |
| 229 | |
| 230 | static void coroutine_fn co_write_zeroes_entry(void *opaque) |
| 231 | { |
| 232 | CoWriteZeroes *data = opaque; |
| 233 | |
| 234 | data->ret = bdrv_co_write_zeroes(bs, data->offset / BDRV_SECTOR_SIZE, |
| 235 | data->count / BDRV_SECTOR_SIZE); |
| 236 | data->done = true; |
| 237 | if (data->ret < 0) { |
| 238 | *data->total = data->ret; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | *data->total = data->count; |
| 243 | } |
| 244 | |
| 245 | static int do_co_write_zeroes(int64_t offset, int count, int *total) |
| 246 | { |
| 247 | Coroutine *co; |
| 248 | CoWriteZeroes data = { |
| 249 | .offset = offset, |
| 250 | .count = count, |
| 251 | .total = total, |
| 252 | .done = false, |
| 253 | }; |
| 254 | |
| 255 | co = qemu_coroutine_create(co_write_zeroes_entry); |
| 256 | qemu_coroutine_enter(co, &data); |
| 257 | while (!data.done) { |
| 258 | qemu_aio_wait(); |
| 259 | } |
| 260 | if (data.ret < 0) { |
| 261 | return data.ret; |
| 262 | } else { |
| 263 | return 1; |
| 264 | } |
| 265 | } |
| 266 | |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 267 | static int do_load_vmstate(char *buf, int64_t offset, int count, int *total) |
| 268 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 269 | *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count); |
| 270 | if (*total < 0) { |
| 271 | return *total; |
| 272 | } |
| 273 | return 1; |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static int do_save_vmstate(char *buf, int64_t offset, int count, int *total) |
| 277 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 278 | *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count); |
| 279 | if (*total < 0) { |
| 280 | return *total; |
| 281 | } |
| 282 | return 1; |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 283 | } |
| 284 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 285 | #define NOT_DONE 0x7fffffff |
| 286 | static void aio_rw_done(void *opaque, int ret) |
| 287 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 288 | *(int *)opaque = ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int do_aio_readv(QEMUIOVector *qiov, int64_t offset, int *total) |
| 292 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 293 | int async_ret = NOT_DONE; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 294 | |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 295 | bdrv_aio_readv(bs, offset >> 9, qiov, qiov->size >> 9, |
| 296 | aio_rw_done, &async_ret); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 297 | while (async_ret == NOT_DONE) { |
| 298 | qemu_aio_wait(); |
| 299 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 300 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 301 | *total = qiov->size; |
| 302 | return async_ret < 0 ? async_ret : 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static int do_aio_writev(QEMUIOVector *qiov, int64_t offset, int *total) |
| 306 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 307 | int async_ret = NOT_DONE; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 308 | |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 309 | bdrv_aio_writev(bs, offset >> 9, qiov, qiov->size >> 9, |
| 310 | aio_rw_done, &async_ret); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 311 | while (async_ret == NOT_DONE) { |
| 312 | qemu_aio_wait(); |
| 313 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 314 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 315 | *total = qiov->size; |
| 316 | return async_ret < 0 ? async_ret : 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 319 | struct multiwrite_async_ret { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 320 | int num_done; |
| 321 | int error; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | static void multiwrite_cb(void *opaque, int ret) |
| 325 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 326 | struct multiwrite_async_ret *async_ret = opaque; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 327 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 328 | async_ret->num_done++; |
| 329 | if (ret < 0) { |
| 330 | async_ret->error = ret; |
| 331 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static int do_aio_multiwrite(BlockRequest* reqs, int num_reqs, int *total) |
| 335 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 336 | int i, ret; |
| 337 | struct multiwrite_async_ret async_ret = { |
| 338 | .num_done = 0, |
| 339 | .error = 0, |
| 340 | }; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 341 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 342 | *total = 0; |
| 343 | for (i = 0; i < num_reqs; i++) { |
| 344 | reqs[i].cb = multiwrite_cb; |
| 345 | reqs[i].opaque = &async_ret; |
| 346 | *total += reqs[i].qiov->size; |
| 347 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 348 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 349 | ret = bdrv_aio_multiwrite(bs, reqs, num_reqs); |
| 350 | if (ret < 0) { |
| 351 | return ret; |
| 352 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 353 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 354 | while (async_ret.num_done < num_reqs) { |
| 355 | qemu_aio_wait(); |
| 356 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 357 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 358 | return async_ret.error < 0 ? async_ret.error : 1; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 359 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 360 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 361 | static void read_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 362 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 363 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 364 | "\n" |
| 365 | " reads a range of bytes from the given offset\n" |
| 366 | "\n" |
| 367 | " Example:\n" |
| 368 | " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n" |
| 369 | "\n" |
| 370 | " Reads a segment of the currently open file, optionally dumping it to the\n" |
| 371 | " standard output stream (with -v option) for subsequent inspection.\n" |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 372 | " -b, -- read from the VM state rather than the virtual disk\n" |
Kevin Wolf | d9654a5 | 2009-04-23 15:11:13 +0200 | [diff] [blame] | 373 | " -C, -- report statistics in a machine parsable format\n" |
| 374 | " -l, -- length for pattern verification (only with -P)\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 375 | " -p, -- use bdrv_pread to read the file\n" |
aliguori | c48101a | 2009-04-18 15:36:23 +0000 | [diff] [blame] | 376 | " -P, -- use a pattern to verify read data\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 377 | " -q, -- quiet mode, do not show I/O statistics\n" |
Kevin Wolf | d9654a5 | 2009-04-23 15:11:13 +0200 | [diff] [blame] | 378 | " -s, -- start offset for pattern verification (only with -P)\n" |
| 379 | " -v, -- dump buffer to standard output\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 380 | "\n"); |
| 381 | } |
| 382 | |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 383 | static int read_f(int argc, char **argv); |
| 384 | |
| 385 | static const cmdinfo_t read_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 386 | .name = "read", |
| 387 | .altname = "r", |
| 388 | .cfunc = read_f, |
| 389 | .argmin = 2, |
| 390 | .argmax = -1, |
| 391 | .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len", |
| 392 | .oneline = "reads a number of bytes at a specified offset", |
| 393 | .help = read_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 394 | }; |
| 395 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 396 | static int read_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 397 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 398 | struct timeval t1, t2; |
| 399 | int Cflag = 0, pflag = 0, qflag = 0, vflag = 0; |
| 400 | int Pflag = 0, sflag = 0, lflag = 0, bflag = 0; |
| 401 | int c, cnt; |
| 402 | char *buf; |
| 403 | int64_t offset; |
| 404 | int count; |
| 405 | /* Some compilers get confused and warn if this is not initialized. */ |
| 406 | int total = 0; |
| 407 | int pattern = 0, pattern_offset = 0, pattern_count = 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 408 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 409 | while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) { |
| 410 | switch (c) { |
| 411 | case 'b': |
| 412 | bflag = 1; |
| 413 | break; |
| 414 | case 'C': |
| 415 | Cflag = 1; |
| 416 | break; |
| 417 | case 'l': |
| 418 | lflag = 1; |
| 419 | pattern_count = cvtnum(optarg); |
| 420 | if (pattern_count < 0) { |
| 421 | printf("non-numeric length argument -- %s\n", optarg); |
| 422 | return 0; |
| 423 | } |
| 424 | break; |
| 425 | case 'p': |
| 426 | pflag = 1; |
| 427 | break; |
| 428 | case 'P': |
| 429 | Pflag = 1; |
| 430 | pattern = parse_pattern(optarg); |
| 431 | if (pattern < 0) { |
| 432 | return 0; |
| 433 | } |
| 434 | break; |
| 435 | case 'q': |
| 436 | qflag = 1; |
| 437 | break; |
| 438 | case 's': |
| 439 | sflag = 1; |
| 440 | pattern_offset = cvtnum(optarg); |
| 441 | if (pattern_offset < 0) { |
| 442 | printf("non-numeric length argument -- %s\n", optarg); |
| 443 | return 0; |
| 444 | } |
| 445 | break; |
| 446 | case 'v': |
| 447 | vflag = 1; |
| 448 | break; |
| 449 | default: |
| 450 | return command_usage(&read_cmd); |
| 451 | } |
| 452 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 453 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 454 | if (optind != argc - 2) { |
| 455 | return command_usage(&read_cmd); |
| 456 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 457 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 458 | if (bflag && pflag) { |
| 459 | printf("-b and -p cannot be specified at the same time\n"); |
| 460 | return 0; |
| 461 | } |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 462 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 463 | offset = cvtnum(argv[optind]); |
| 464 | if (offset < 0) { |
| 465 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 466 | return 0; |
| 467 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 468 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 469 | optind++; |
| 470 | count = cvtnum(argv[optind]); |
| 471 | if (count < 0) { |
| 472 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 473 | return 0; |
| 474 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 475 | |
Kevin Wolf | d9654a5 | 2009-04-23 15:11:13 +0200 | [diff] [blame] | 476 | if (!Pflag && (lflag || sflag)) { |
| 477 | return command_usage(&read_cmd); |
| 478 | } |
| 479 | |
| 480 | if (!lflag) { |
| 481 | pattern_count = count - pattern_offset; |
| 482 | } |
| 483 | |
| 484 | if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) { |
Dong Xu Wang | 07f3507 | 2011-11-22 18:06:26 +0800 | [diff] [blame] | 485 | printf("pattern verification range exceeds end of read data\n"); |
Kevin Wolf | d9654a5 | 2009-04-23 15:11:13 +0200 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
Devin Nakamura | 5afc8b3 | 2011-07-11 11:20:25 -0400 | [diff] [blame] | 489 | if (!pflag) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 490 | if (offset & 0x1ff) { |
| 491 | printf("offset %" PRId64 " is not sector aligned\n", |
| 492 | offset); |
| 493 | return 0; |
| 494 | } |
| 495 | if (count & 0x1ff) { |
| 496 | printf("count %d is not sector aligned\n", |
| 497 | count); |
| 498 | return 0; |
| 499 | } |
Devin Nakamura | 5afc8b3 | 2011-07-11 11:20:25 -0400 | [diff] [blame] | 500 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 501 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 502 | buf = qemu_io_alloc(count, 0xab); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 503 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 504 | gettimeofday(&t1, NULL); |
| 505 | if (pflag) { |
| 506 | cnt = do_pread(buf, offset, count, &total); |
| 507 | } else if (bflag) { |
| 508 | cnt = do_load_vmstate(buf, offset, count, &total); |
| 509 | } else { |
| 510 | cnt = do_read(buf, offset, count, &total); |
| 511 | } |
| 512 | gettimeofday(&t2, NULL); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 513 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 514 | if (cnt < 0) { |
| 515 | printf("read failed: %s\n", strerror(-cnt)); |
| 516 | goto out; |
| 517 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 518 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 519 | if (Pflag) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 520 | void *cmp_buf = g_malloc(pattern_count); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 521 | memset(cmp_buf, pattern, pattern_count); |
| 522 | if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) { |
| 523 | printf("Pattern verification failed at offset %" |
| 524 | PRId64 ", %d bytes\n", |
| 525 | offset + pattern_offset, pattern_count); |
| 526 | } |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 527 | g_free(cmp_buf); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 528 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 529 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 530 | if (qflag) { |
| 531 | goto out; |
| 532 | } |
aliguori | c48101a | 2009-04-18 15:36:23 +0000 | [diff] [blame] | 533 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 534 | if (vflag) { |
| 535 | dump_buffer(buf, offset, count); |
| 536 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 537 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 538 | /* Finally, report back -- -C gives a parsable format */ |
| 539 | t2 = tsub(t2, t1); |
| 540 | print_report("read", &t2, offset, count, total, cnt, Cflag); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 541 | |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 542 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 543 | qemu_io_free(buf); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 544 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 545 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 548 | static void readv_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 549 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 550 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 551 | "\n" |
| 552 | " reads a range of bytes from the given offset into multiple buffers\n" |
| 553 | "\n" |
| 554 | " Example:\n" |
| 555 | " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" |
| 556 | "\n" |
| 557 | " Reads a segment of the currently open file, optionally dumping it to the\n" |
| 558 | " standard output stream (with -v option) for subsequent inspection.\n" |
| 559 | " Uses multiple iovec buffers if more than one byte range is specified.\n" |
| 560 | " -C, -- report statistics in a machine parsable format\n" |
aliguori | c48101a | 2009-04-18 15:36:23 +0000 | [diff] [blame] | 561 | " -P, -- use a pattern to verify read data\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 562 | " -v, -- dump buffer to standard output\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 563 | " -q, -- quiet mode, do not show I/O statistics\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 564 | "\n"); |
| 565 | } |
| 566 | |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 567 | static int readv_f(int argc, char **argv); |
| 568 | |
| 569 | static const cmdinfo_t readv_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 570 | .name = "readv", |
| 571 | .cfunc = readv_f, |
| 572 | .argmin = 2, |
| 573 | .argmax = -1, |
| 574 | .args = "[-Cqv] [-P pattern ] off len [len..]", |
| 575 | .oneline = "reads a number of bytes at a specified offset", |
| 576 | .help = readv_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 577 | }; |
| 578 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 579 | static int readv_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 580 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 581 | struct timeval t1, t2; |
| 582 | int Cflag = 0, qflag = 0, vflag = 0; |
| 583 | int c, cnt; |
| 584 | char *buf; |
| 585 | int64_t offset; |
| 586 | /* Some compilers get confused and warn if this is not initialized. */ |
| 587 | int total = 0; |
| 588 | int nr_iov; |
| 589 | QEMUIOVector qiov; |
| 590 | int pattern = 0; |
| 591 | int Pflag = 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 592 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 593 | while ((c = getopt(argc, argv, "CP:qv")) != EOF) { |
| 594 | switch (c) { |
| 595 | case 'C': |
| 596 | Cflag = 1; |
| 597 | break; |
| 598 | case 'P': |
| 599 | Pflag = 1; |
| 600 | pattern = parse_pattern(optarg); |
| 601 | if (pattern < 0) { |
| 602 | return 0; |
| 603 | } |
| 604 | break; |
| 605 | case 'q': |
| 606 | qflag = 1; |
| 607 | break; |
| 608 | case 'v': |
| 609 | vflag = 1; |
| 610 | break; |
| 611 | default: |
| 612 | return command_usage(&readv_cmd); |
| 613 | } |
| 614 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 615 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 616 | if (optind > argc - 2) { |
| 617 | return command_usage(&readv_cmd); |
| 618 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 619 | |
| 620 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 621 | offset = cvtnum(argv[optind]); |
| 622 | if (offset < 0) { |
| 623 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 624 | return 0; |
| 625 | } |
| 626 | optind++; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 627 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 628 | if (offset & 0x1ff) { |
| 629 | printf("offset %" PRId64 " is not sector aligned\n", |
| 630 | offset); |
| 631 | return 0; |
| 632 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 633 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 634 | nr_iov = argc - optind; |
| 635 | buf = create_iovec(&qiov, &argv[optind], nr_iov, 0xab); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 636 | if (buf == NULL) { |
| 637 | return 0; |
| 638 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 639 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 640 | gettimeofday(&t1, NULL); |
| 641 | cnt = do_aio_readv(&qiov, offset, &total); |
| 642 | gettimeofday(&t2, NULL); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 643 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 644 | if (cnt < 0) { |
| 645 | printf("readv failed: %s\n", strerror(-cnt)); |
| 646 | goto out; |
| 647 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 648 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 649 | if (Pflag) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 650 | void *cmp_buf = g_malloc(qiov.size); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 651 | memset(cmp_buf, pattern, qiov.size); |
| 652 | if (memcmp(buf, cmp_buf, qiov.size)) { |
| 653 | printf("Pattern verification failed at offset %" |
| 654 | PRId64 ", %zd bytes\n", offset, qiov.size); |
| 655 | } |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 656 | g_free(cmp_buf); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 657 | } |
aliguori | c48101a | 2009-04-18 15:36:23 +0000 | [diff] [blame] | 658 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 659 | if (qflag) { |
| 660 | goto out; |
| 661 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 662 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 663 | if (vflag) { |
| 664 | dump_buffer(buf, offset, qiov.size); |
| 665 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 666 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 667 | /* Finally, report back -- -C gives a parsable format */ |
| 668 | t2 = tsub(t2, t1); |
| 669 | print_report("read", &t2, offset, qiov.size, total, cnt, Cflag); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 670 | |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 671 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 672 | qemu_io_free(buf); |
| 673 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 676 | static void write_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 677 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 678 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 679 | "\n" |
| 680 | " writes a range of bytes from the given offset\n" |
| 681 | "\n" |
| 682 | " Example:\n" |
| 683 | " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n" |
| 684 | "\n" |
| 685 | " Writes into a segment of the currently open file, using a buffer\n" |
| 686 | " filled with a set pattern (0xcdcdcdcd).\n" |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 687 | " -b, -- write to the VM state rather than the virtual disk\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 688 | " -p, -- use bdrv_pwrite to write the file\n" |
| 689 | " -P, -- use different pattern to fill file\n" |
| 690 | " -C, -- report statistics in a machine parsable format\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 691 | " -q, -- quiet mode, do not show I/O statistics\n" |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 692 | " -z, -- write zeroes using bdrv_co_write_zeroes\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 693 | "\n"); |
| 694 | } |
| 695 | |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 696 | static int write_f(int argc, char **argv); |
| 697 | |
| 698 | static const cmdinfo_t write_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 699 | .name = "write", |
| 700 | .altname = "w", |
| 701 | .cfunc = write_f, |
| 702 | .argmin = 2, |
| 703 | .argmax = -1, |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 704 | .args = "[-bCpqz] [-P pattern ] off len", |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 705 | .oneline = "writes a number of bytes at a specified offset", |
| 706 | .help = write_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 707 | }; |
| 708 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 709 | static int write_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 710 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 711 | struct timeval t1, t2; |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 712 | int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 713 | int c, cnt; |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 714 | char *buf = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 715 | int64_t offset; |
| 716 | int count; |
| 717 | /* Some compilers get confused and warn if this is not initialized. */ |
| 718 | int total = 0; |
| 719 | int pattern = 0xcd; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 720 | |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 721 | while ((c = getopt(argc, argv, "bCpP:qz")) != EOF) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 722 | switch (c) { |
| 723 | case 'b': |
| 724 | bflag = 1; |
| 725 | break; |
| 726 | case 'C': |
| 727 | Cflag = 1; |
| 728 | break; |
| 729 | case 'p': |
| 730 | pflag = 1; |
| 731 | break; |
| 732 | case 'P': |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 733 | Pflag = 1; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 734 | pattern = parse_pattern(optarg); |
| 735 | if (pattern < 0) { |
| 736 | return 0; |
| 737 | } |
| 738 | break; |
| 739 | case 'q': |
| 740 | qflag = 1; |
| 741 | break; |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 742 | case 'z': |
| 743 | zflag = 1; |
| 744 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 745 | default: |
| 746 | return command_usage(&write_cmd); |
| 747 | } |
| 748 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 749 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 750 | if (optind != argc - 2) { |
| 751 | return command_usage(&write_cmd); |
| 752 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 753 | |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 754 | if (bflag + pflag + zflag > 1) { |
| 755 | printf("-b, -p, or -z cannot be specified at the same time\n"); |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | if (zflag && Pflag) { |
| 760 | printf("-z and -P cannot be specified at the same time\n"); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 761 | return 0; |
| 762 | } |
Kevin Wolf | ca94dbc | 2009-07-15 12:06:58 +0200 | [diff] [blame] | 763 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 764 | offset = cvtnum(argv[optind]); |
| 765 | if (offset < 0) { |
| 766 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 767 | return 0; |
| 768 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 769 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 770 | optind++; |
| 771 | count = cvtnum(argv[optind]); |
| 772 | if (count < 0) { |
| 773 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 774 | return 0; |
| 775 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 776 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 777 | if (!pflag) { |
| 778 | if (offset & 0x1ff) { |
| 779 | printf("offset %" PRId64 " is not sector aligned\n", |
| 780 | offset); |
| 781 | return 0; |
| 782 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 783 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 784 | if (count & 0x1ff) { |
| 785 | printf("count %d is not sector aligned\n", |
| 786 | count); |
| 787 | return 0; |
| 788 | } |
| 789 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 790 | |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 791 | if (!zflag) { |
| 792 | buf = qemu_io_alloc(count, pattern); |
| 793 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 794 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 795 | gettimeofday(&t1, NULL); |
| 796 | if (pflag) { |
| 797 | cnt = do_pwrite(buf, offset, count, &total); |
| 798 | } else if (bflag) { |
| 799 | cnt = do_save_vmstate(buf, offset, count, &total); |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 800 | } else if (zflag) { |
| 801 | cnt = do_co_write_zeroes(offset, count, &total); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 802 | } else { |
| 803 | cnt = do_write(buf, offset, count, &total); |
| 804 | } |
| 805 | gettimeofday(&t2, NULL); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 806 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 807 | if (cnt < 0) { |
| 808 | printf("write failed: %s\n", strerror(-cnt)); |
| 809 | goto out; |
| 810 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 811 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 812 | if (qflag) { |
| 813 | goto out; |
| 814 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 815 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 816 | /* Finally, report back -- -C gives a parsable format */ |
| 817 | t2 = tsub(t2, t1); |
| 818 | print_report("wrote", &t2, offset, count, total, cnt, Cflag); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 819 | |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 820 | out: |
Stefan Hajnoczi | 71b58b8 | 2012-02-07 13:27:29 +0000 | [diff] [blame] | 821 | if (!zflag) { |
| 822 | qemu_io_free(buf); |
| 823 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 824 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 825 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 826 | } |
| 827 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 828 | static void |
| 829 | writev_help(void) |
| 830 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 831 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 832 | "\n" |
| 833 | " writes a range of bytes from the given offset source from multiple buffers\n" |
| 834 | "\n" |
| 835 | " Example:\n" |
| 836 | " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" |
| 837 | "\n" |
| 838 | " Writes into a segment of the currently open file, using a buffer\n" |
| 839 | " filled with a set pattern (0xcdcdcdcd).\n" |
| 840 | " -P, -- use different pattern to fill file\n" |
| 841 | " -C, -- report statistics in a machine parsable format\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 842 | " -q, -- quiet mode, do not show I/O statistics\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 843 | "\n"); |
| 844 | } |
| 845 | |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 846 | static int writev_f(int argc, char **argv); |
| 847 | |
| 848 | static const cmdinfo_t writev_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 849 | .name = "writev", |
| 850 | .cfunc = writev_f, |
| 851 | .argmin = 2, |
| 852 | .argmax = -1, |
| 853 | .args = "[-Cq] [-P pattern ] off len [len..]", |
| 854 | .oneline = "writes a number of bytes at a specified offset", |
| 855 | .help = writev_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 856 | }; |
| 857 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 858 | static int writev_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 859 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 860 | struct timeval t1, t2; |
| 861 | int Cflag = 0, qflag = 0; |
| 862 | int c, cnt; |
| 863 | char *buf; |
| 864 | int64_t offset; |
| 865 | /* Some compilers get confused and warn if this is not initialized. */ |
| 866 | int total = 0; |
| 867 | int nr_iov; |
| 868 | int pattern = 0xcd; |
| 869 | QEMUIOVector qiov; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 870 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 871 | while ((c = getopt(argc, argv, "CqP:")) != EOF) { |
| 872 | switch (c) { |
| 873 | case 'C': |
| 874 | Cflag = 1; |
| 875 | break; |
| 876 | case 'q': |
| 877 | qflag = 1; |
| 878 | break; |
| 879 | case 'P': |
| 880 | pattern = parse_pattern(optarg); |
| 881 | if (pattern < 0) { |
| 882 | return 0; |
| 883 | } |
| 884 | break; |
| 885 | default: |
| 886 | return command_usage(&writev_cmd); |
| 887 | } |
| 888 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 889 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 890 | if (optind > argc - 2) { |
| 891 | return command_usage(&writev_cmd); |
| 892 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 893 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 894 | offset = cvtnum(argv[optind]); |
| 895 | if (offset < 0) { |
| 896 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 897 | return 0; |
| 898 | } |
| 899 | optind++; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 900 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 901 | if (offset & 0x1ff) { |
| 902 | printf("offset %" PRId64 " is not sector aligned\n", |
| 903 | offset); |
| 904 | return 0; |
| 905 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 906 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 907 | nr_iov = argc - optind; |
| 908 | buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 909 | if (buf == NULL) { |
| 910 | return 0; |
| 911 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 912 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 913 | gettimeofday(&t1, NULL); |
| 914 | cnt = do_aio_writev(&qiov, offset, &total); |
| 915 | gettimeofday(&t2, NULL); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 916 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 917 | if (cnt < 0) { |
| 918 | printf("writev failed: %s\n", strerror(-cnt)); |
| 919 | goto out; |
| 920 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 921 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 922 | if (qflag) { |
| 923 | goto out; |
| 924 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 925 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 926 | /* Finally, report back -- -C gives a parsable format */ |
| 927 | t2 = tsub(t2, t1); |
| 928 | print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag); |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 929 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 930 | qemu_io_free(buf); |
| 931 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 934 | static void multiwrite_help(void) |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 935 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 936 | printf( |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 937 | "\n" |
| 938 | " writes a range of bytes from the given offset source from multiple buffers,\n" |
| 939 | " in a batch of requests that may be merged by qemu\n" |
| 940 | "\n" |
| 941 | " Example:\n" |
Stefan Weil | b2bedb2 | 2011-09-12 22:33:01 +0200 | [diff] [blame] | 942 | " 'multiwrite 512 1k 1k ; 4k 1k'\n" |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 943 | " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n" |
| 944 | "\n" |
| 945 | " Writes into a segment of the currently open file, using a buffer\n" |
| 946 | " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n" |
| 947 | " by one for each request contained in the multiwrite command.\n" |
| 948 | " -P, -- use different pattern to fill file\n" |
| 949 | " -C, -- report statistics in a machine parsable format\n" |
| 950 | " -q, -- quiet mode, do not show I/O statistics\n" |
| 951 | "\n"); |
| 952 | } |
| 953 | |
| 954 | static int multiwrite_f(int argc, char **argv); |
| 955 | |
| 956 | static const cmdinfo_t multiwrite_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 957 | .name = "multiwrite", |
| 958 | .cfunc = multiwrite_f, |
| 959 | .argmin = 2, |
| 960 | .argmax = -1, |
| 961 | .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]", |
| 962 | .oneline = "issues multiple write requests at once", |
| 963 | .help = multiwrite_help, |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 964 | }; |
| 965 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 966 | static int multiwrite_f(int argc, char **argv) |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 967 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 968 | struct timeval t1, t2; |
| 969 | int Cflag = 0, qflag = 0; |
| 970 | int c, cnt; |
| 971 | char **buf; |
| 972 | int64_t offset, first_offset = 0; |
| 973 | /* Some compilers get confused and warn if this is not initialized. */ |
| 974 | int total = 0; |
| 975 | int nr_iov; |
| 976 | int nr_reqs; |
| 977 | int pattern = 0xcd; |
| 978 | QEMUIOVector *qiovs; |
| 979 | int i; |
| 980 | BlockRequest *reqs; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 981 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 982 | while ((c = getopt(argc, argv, "CqP:")) != EOF) { |
| 983 | switch (c) { |
| 984 | case 'C': |
| 985 | Cflag = 1; |
| 986 | break; |
| 987 | case 'q': |
| 988 | qflag = 1; |
| 989 | break; |
| 990 | case 'P': |
| 991 | pattern = parse_pattern(optarg); |
| 992 | if (pattern < 0) { |
| 993 | return 0; |
| 994 | } |
| 995 | break; |
| 996 | default: |
| 997 | return command_usage(&writev_cmd); |
| 998 | } |
| 999 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1000 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1001 | if (optind > argc - 2) { |
| 1002 | return command_usage(&writev_cmd); |
| 1003 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1004 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1005 | nr_reqs = 1; |
| 1006 | for (i = optind; i < argc; i++) { |
| 1007 | if (!strcmp(argv[i], ";")) { |
| 1008 | nr_reqs++; |
| 1009 | } |
| 1010 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1011 | |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1012 | reqs = g_malloc0(nr_reqs * sizeof(*reqs)); |
| 1013 | buf = g_malloc0(nr_reqs * sizeof(*buf)); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1014 | qiovs = g_malloc(nr_reqs * sizeof(*qiovs)); |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1015 | |
Kevin Wolf | 67403db | 2011-10-31 11:49:21 +0100 | [diff] [blame] | 1016 | for (i = 0; i < nr_reqs && optind < argc; i++) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1017 | int j; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1018 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1019 | /* Read the offset of the request */ |
| 1020 | offset = cvtnum(argv[optind]); |
| 1021 | if (offset < 0) { |
| 1022 | printf("non-numeric offset argument -- %s\n", argv[optind]); |
Kevin Wolf | 67403db | 2011-10-31 11:49:21 +0100 | [diff] [blame] | 1023 | goto out; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1024 | } |
| 1025 | optind++; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1026 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1027 | if (offset & 0x1ff) { |
| 1028 | printf("offset %lld is not sector aligned\n", |
| 1029 | (long long)offset); |
Kevin Wolf | 67403db | 2011-10-31 11:49:21 +0100 | [diff] [blame] | 1030 | goto out; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1031 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1032 | |
| 1033 | if (i == 0) { |
| 1034 | first_offset = offset; |
| 1035 | } |
| 1036 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1037 | /* Read lengths for qiov entries */ |
| 1038 | for (j = optind; j < argc; j++) { |
| 1039 | if (!strcmp(argv[j], ";")) { |
| 1040 | break; |
| 1041 | } |
| 1042 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1043 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1044 | nr_iov = j - optind; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1045 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1046 | /* Build request */ |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1047 | buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern); |
| 1048 | if (buf[i] == NULL) { |
| 1049 | goto out; |
| 1050 | } |
| 1051 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1052 | reqs[i].qiov = &qiovs[i]; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1053 | reqs[i].sector = offset >> 9; |
| 1054 | reqs[i].nb_sectors = reqs[i].qiov->size >> 9; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1055 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1056 | optind = j + 1; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1057 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1058 | pattern++; |
| 1059 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1060 | |
Kevin Wolf | 67403db | 2011-10-31 11:49:21 +0100 | [diff] [blame] | 1061 | /* If there were empty requests at the end, ignore them */ |
| 1062 | nr_reqs = i; |
| 1063 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1064 | gettimeofday(&t1, NULL); |
| 1065 | cnt = do_aio_multiwrite(reqs, nr_reqs, &total); |
| 1066 | gettimeofday(&t2, NULL); |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1067 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1068 | if (cnt < 0) { |
| 1069 | printf("aio_multiwrite failed: %s\n", strerror(-cnt)); |
| 1070 | goto out; |
| 1071 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1072 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1073 | if (qflag) { |
| 1074 | goto out; |
| 1075 | } |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1076 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1077 | /* Finally, report back -- -C gives a parsable format */ |
| 1078 | t2 = tsub(t2, t1); |
| 1079 | print_report("wrote", &t2, first_offset, total, total, cnt, Cflag); |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1080 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1081 | for (i = 0; i < nr_reqs; i++) { |
| 1082 | qemu_io_free(buf[i]); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1083 | if (reqs[i].qiov != NULL) { |
| 1084 | qemu_iovec_destroy(&qiovs[i]); |
| 1085 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1086 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1087 | g_free(buf); |
| 1088 | g_free(reqs); |
| 1089 | g_free(qiovs); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1090 | return 0; |
Kevin Wolf | 776cbbb | 2010-05-21 11:37:26 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1093 | struct aio_ctx { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1094 | QEMUIOVector qiov; |
| 1095 | int64_t offset; |
| 1096 | char *buf; |
| 1097 | int qflag; |
| 1098 | int vflag; |
| 1099 | int Cflag; |
| 1100 | int Pflag; |
| 1101 | int pattern; |
| 1102 | struct timeval t1; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1103 | }; |
| 1104 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1105 | static void aio_write_done(void *opaque, int ret) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1106 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1107 | struct aio_ctx *ctx = opaque; |
| 1108 | struct timeval t2; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1109 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1110 | gettimeofday(&t2, NULL); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1111 | |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1112 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1113 | if (ret < 0) { |
| 1114 | printf("aio_write failed: %s\n", strerror(-ret)); |
| 1115 | goto out; |
| 1116 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1117 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1118 | if (ctx->qflag) { |
| 1119 | goto out; |
| 1120 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1121 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1122 | /* Finally, report back -- -C gives a parsable format */ |
| 1123 | t2 = tsub(t2, ctx->t1); |
| 1124 | print_report("wrote", &t2, ctx->offset, ctx->qiov.size, |
| 1125 | ctx->qiov.size, 1, ctx->Cflag); |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 1126 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1127 | qemu_io_free(ctx->buf); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1128 | g_free(ctx); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1131 | static void aio_read_done(void *opaque, int ret) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1132 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1133 | struct aio_ctx *ctx = opaque; |
| 1134 | struct timeval t2; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1135 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1136 | gettimeofday(&t2, NULL); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1137 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1138 | if (ret < 0) { |
| 1139 | printf("readv failed: %s\n", strerror(-ret)); |
| 1140 | goto out; |
| 1141 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1142 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1143 | if (ctx->Pflag) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1144 | void *cmp_buf = g_malloc(ctx->qiov.size); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1145 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1146 | memset(cmp_buf, ctx->pattern, ctx->qiov.size); |
| 1147 | if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) { |
| 1148 | printf("Pattern verification failed at offset %" |
| 1149 | PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size); |
| 1150 | } |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1151 | g_free(cmp_buf); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1152 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1153 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1154 | if (ctx->qflag) { |
| 1155 | goto out; |
| 1156 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1157 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1158 | if (ctx->vflag) { |
| 1159 | dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size); |
| 1160 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1161 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1162 | /* Finally, report back -- -C gives a parsable format */ |
| 1163 | t2 = tsub(t2, ctx->t1); |
| 1164 | print_report("read", &t2, ctx->offset, ctx->qiov.size, |
| 1165 | ctx->qiov.size, 1, ctx->Cflag); |
Kevin Wolf | 7d8abfc | 2009-07-10 13:33:52 +0200 | [diff] [blame] | 1166 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1167 | qemu_io_free(ctx->buf); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1168 | g_free(ctx); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1171 | static void aio_read_help(void) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1172 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1173 | printf( |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1174 | "\n" |
| 1175 | " asynchronously reads a range of bytes from the given offset\n" |
| 1176 | "\n" |
| 1177 | " Example:\n" |
| 1178 | " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" |
| 1179 | "\n" |
| 1180 | " Reads a segment of the currently open file, optionally dumping it to the\n" |
| 1181 | " standard output stream (with -v option) for subsequent inspection.\n" |
Christoph Hellwig | e432cef | 2010-03-28 12:19:31 +0200 | [diff] [blame] | 1182 | " The read is performed asynchronously and the aio_flush command must be\n" |
Laszlo Ersek | 96bab41 | 2012-01-24 21:13:28 +0100 | [diff] [blame] | 1183 | " used to ensure all outstanding aio requests have been completed.\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1184 | " -C, -- report statistics in a machine parsable format\n" |
| 1185 | " -P, -- use a pattern to verify read data\n" |
| 1186 | " -v, -- dump buffer to standard output\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 1187 | " -q, -- quiet mode, do not show I/O statistics\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1188 | "\n"); |
| 1189 | } |
| 1190 | |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1191 | static int aio_read_f(int argc, char **argv); |
| 1192 | |
| 1193 | static const cmdinfo_t aio_read_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1194 | .name = "aio_read", |
| 1195 | .cfunc = aio_read_f, |
| 1196 | .argmin = 2, |
| 1197 | .argmax = -1, |
| 1198 | .args = "[-Cqv] [-P pattern ] off len [len..]", |
| 1199 | .oneline = "asynchronously reads a number of bytes", |
| 1200 | .help = aio_read_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1201 | }; |
| 1202 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1203 | static int aio_read_f(int argc, char **argv) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1204 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1205 | int nr_iov, c; |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1206 | struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1207 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1208 | while ((c = getopt(argc, argv, "CP:qv")) != EOF) { |
| 1209 | switch (c) { |
| 1210 | case 'C': |
| 1211 | ctx->Cflag = 1; |
| 1212 | break; |
| 1213 | case 'P': |
| 1214 | ctx->Pflag = 1; |
| 1215 | ctx->pattern = parse_pattern(optarg); |
| 1216 | if (ctx->pattern < 0) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1217 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1218 | return 0; |
| 1219 | } |
| 1220 | break; |
| 1221 | case 'q': |
| 1222 | ctx->qflag = 1; |
| 1223 | break; |
| 1224 | case 'v': |
| 1225 | ctx->vflag = 1; |
| 1226 | break; |
| 1227 | default: |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1228 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1229 | return command_usage(&aio_read_cmd); |
| 1230 | } |
| 1231 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1232 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1233 | if (optind > argc - 2) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1234 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1235 | return command_usage(&aio_read_cmd); |
| 1236 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1237 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1238 | ctx->offset = cvtnum(argv[optind]); |
| 1239 | if (ctx->offset < 0) { |
| 1240 | printf("non-numeric length argument -- %s\n", argv[optind]); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1241 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1242 | return 0; |
| 1243 | } |
| 1244 | optind++; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1245 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1246 | if (ctx->offset & 0x1ff) { |
| 1247 | printf("offset %" PRId64 " is not sector aligned\n", |
| 1248 | ctx->offset); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1249 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1252 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1253 | nr_iov = argc - optind; |
| 1254 | ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1255 | if (ctx->buf == NULL) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1256 | g_free(ctx); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1257 | return 0; |
| 1258 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1259 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1260 | gettimeofday(&ctx->t1, NULL); |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 1261 | bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov, |
| 1262 | ctx->qiov.size >> 9, aio_read_done, ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1263 | return 0; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1264 | } |
| 1265 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1266 | static void aio_write_help(void) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1267 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1268 | printf( |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1269 | "\n" |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1270 | " asynchronously writes a range of bytes from the given offset source\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1271 | " from multiple buffers\n" |
| 1272 | "\n" |
| 1273 | " Example:\n" |
| 1274 | " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" |
| 1275 | "\n" |
| 1276 | " Writes into a segment of the currently open file, using a buffer\n" |
| 1277 | " filled with a set pattern (0xcdcdcdcd).\n" |
Christoph Hellwig | e432cef | 2010-03-28 12:19:31 +0200 | [diff] [blame] | 1278 | " The write is performed asynchronously and the aio_flush command must be\n" |
Laszlo Ersek | 96bab41 | 2012-01-24 21:13:28 +0100 | [diff] [blame] | 1279 | " used to ensure all outstanding aio requests have been completed.\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1280 | " -P, -- use different pattern to fill file\n" |
| 1281 | " -C, -- report statistics in a machine parsable format\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 1282 | " -q, -- quiet mode, do not show I/O statistics\n" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1283 | "\n"); |
| 1284 | } |
| 1285 | |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1286 | static int aio_write_f(int argc, char **argv); |
| 1287 | |
| 1288 | static const cmdinfo_t aio_write_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1289 | .name = "aio_write", |
| 1290 | .cfunc = aio_write_f, |
| 1291 | .argmin = 2, |
| 1292 | .argmax = -1, |
| 1293 | .args = "[-Cq] [-P pattern ] off len [len..]", |
| 1294 | .oneline = "asynchronously writes a number of bytes", |
| 1295 | .help = aio_write_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1296 | }; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1297 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1298 | static int aio_write_f(int argc, char **argv) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1299 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1300 | int nr_iov, c; |
| 1301 | int pattern = 0xcd; |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1302 | struct aio_ctx *ctx = g_new0(struct aio_ctx, 1); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1303 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1304 | while ((c = getopt(argc, argv, "CqP:")) != EOF) { |
| 1305 | switch (c) { |
| 1306 | case 'C': |
| 1307 | ctx->Cflag = 1; |
| 1308 | break; |
| 1309 | case 'q': |
| 1310 | ctx->qflag = 1; |
| 1311 | break; |
| 1312 | case 'P': |
| 1313 | pattern = parse_pattern(optarg); |
| 1314 | if (pattern < 0) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1315 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1316 | return 0; |
| 1317 | } |
| 1318 | break; |
| 1319 | default: |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1320 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1321 | return command_usage(&aio_write_cmd); |
| 1322 | } |
| 1323 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1324 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1325 | if (optind > argc - 2) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1326 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1327 | return command_usage(&aio_write_cmd); |
| 1328 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1329 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1330 | ctx->offset = cvtnum(argv[optind]); |
| 1331 | if (ctx->offset < 0) { |
| 1332 | printf("non-numeric length argument -- %s\n", argv[optind]); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1333 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1334 | return 0; |
| 1335 | } |
| 1336 | optind++; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1337 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1338 | if (ctx->offset & 0x1ff) { |
| 1339 | printf("offset %" PRId64 " is not sector aligned\n", |
| 1340 | ctx->offset); |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1341 | g_free(ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1342 | return 0; |
| 1343 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1344 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1345 | nr_iov = argc - optind; |
| 1346 | ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1347 | if (ctx->buf == NULL) { |
Stefan Hajnoczi | 031380d | 2012-01-16 09:28:06 +0000 | [diff] [blame] | 1348 | g_free(ctx); |
Kevin Wolf | f236062 | 2011-10-31 11:36:32 +0100 | [diff] [blame] | 1349 | return 0; |
| 1350 | } |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1351 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1352 | gettimeofday(&ctx->t1, NULL); |
Paolo Bonzini | ad54ae8 | 2011-11-30 09:12:30 +0100 | [diff] [blame] | 1353 | bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov, |
| 1354 | ctx->qiov.size >> 9, aio_write_done, ctx); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1355 | return 0; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1356 | } |
| 1357 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1358 | static int aio_flush_f(int argc, char **argv) |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1359 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1360 | qemu_aio_flush(); |
| 1361 | return 0; |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | static const cmdinfo_t aio_flush_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1365 | .name = "aio_flush", |
| 1366 | .cfunc = aio_flush_f, |
| 1367 | .oneline = "completes all outstanding aio requests" |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1368 | }; |
| 1369 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1370 | static int flush_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1371 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1372 | bdrv_flush(bs); |
| 1373 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | static const cmdinfo_t flush_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1377 | .name = "flush", |
| 1378 | .altname = "f", |
| 1379 | .cfunc = flush_f, |
| 1380 | .oneline = "flush all in-core file state to disk", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1381 | }; |
| 1382 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1383 | static int truncate_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1384 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1385 | int64_t offset; |
| 1386 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1387 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1388 | offset = cvtnum(argv[1]); |
| 1389 | if (offset < 0) { |
| 1390 | printf("non-numeric truncate argument -- %s\n", argv[1]); |
| 1391 | return 0; |
| 1392 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1393 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1394 | ret = bdrv_truncate(bs, offset); |
| 1395 | if (ret < 0) { |
| 1396 | printf("truncate: %s\n", strerror(-ret)); |
| 1397 | return 0; |
| 1398 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1399 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1400 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | static const cmdinfo_t truncate_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1404 | .name = "truncate", |
| 1405 | .altname = "t", |
| 1406 | .cfunc = truncate_f, |
| 1407 | .argmin = 1, |
| 1408 | .argmax = 1, |
| 1409 | .args = "off", |
| 1410 | .oneline = "truncates the current file at the given offset", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1411 | }; |
| 1412 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1413 | static int length_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1414 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1415 | int64_t size; |
| 1416 | char s1[64]; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1417 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1418 | size = bdrv_getlength(bs); |
| 1419 | if (size < 0) { |
| 1420 | printf("getlength: %s\n", strerror(-size)); |
| 1421 | return 0; |
| 1422 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1423 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1424 | cvtstr(size, s1, sizeof(s1)); |
| 1425 | printf("%s\n", s1); |
| 1426 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | |
| 1430 | static const cmdinfo_t length_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1431 | .name = "length", |
| 1432 | .altname = "l", |
| 1433 | .cfunc = length_f, |
| 1434 | .oneline = "gets the length of the current file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1435 | }; |
| 1436 | |
| 1437 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1438 | static int info_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1439 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1440 | BlockDriverInfo bdi; |
| 1441 | char s1[64], s2[64]; |
| 1442 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1443 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1444 | if (bs->drv && bs->drv->format_name) { |
| 1445 | printf("format name: %s\n", bs->drv->format_name); |
| 1446 | } |
| 1447 | if (bs->drv && bs->drv->protocol_name) { |
| 1448 | printf("format name: %s\n", bs->drv->protocol_name); |
| 1449 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1450 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1451 | ret = bdrv_get_info(bs, &bdi); |
| 1452 | if (ret) { |
| 1453 | return 0; |
| 1454 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1455 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1456 | cvtstr(bdi.cluster_size, s1, sizeof(s1)); |
| 1457 | cvtstr(bdi.vm_state_offset, s2, sizeof(s2)); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1458 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1459 | printf("cluster size: %s\n", s1); |
| 1460 | printf("vm state offset: %s\n", s2); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1461 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1462 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | static const cmdinfo_t info_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1468 | .name = "info", |
| 1469 | .altname = "i", |
| 1470 | .cfunc = info_f, |
| 1471 | .oneline = "prints information about the current file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1472 | }; |
| 1473 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1474 | static void discard_help(void) |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1475 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1476 | printf( |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1477 | "\n" |
| 1478 | " discards a range of bytes from the given offset\n" |
| 1479 | "\n" |
| 1480 | " Example:\n" |
| 1481 | " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n" |
| 1482 | "\n" |
| 1483 | " Discards a segment of the currently open file.\n" |
| 1484 | " -C, -- report statistics in a machine parsable format\n" |
Kevin Wolf | 095343a | 2010-12-17 11:55:37 +0100 | [diff] [blame] | 1485 | " -q, -- quiet mode, do not show I/O statistics\n" |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1486 | "\n"); |
| 1487 | } |
| 1488 | |
| 1489 | static int discard_f(int argc, char **argv); |
| 1490 | |
| 1491 | static const cmdinfo_t discard_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1492 | .name = "discard", |
| 1493 | .altname = "d", |
| 1494 | .cfunc = discard_f, |
| 1495 | .argmin = 2, |
| 1496 | .argmax = -1, |
| 1497 | .args = "[-Cq] off len", |
| 1498 | .oneline = "discards a number of bytes at a specified offset", |
| 1499 | .help = discard_help, |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1500 | }; |
| 1501 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1502 | static int discard_f(int argc, char **argv) |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1503 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1504 | struct timeval t1, t2; |
| 1505 | int Cflag = 0, qflag = 0; |
| 1506 | int c, ret; |
| 1507 | int64_t offset; |
| 1508 | int count; |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1509 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1510 | while ((c = getopt(argc, argv, "Cq")) != EOF) { |
| 1511 | switch (c) { |
| 1512 | case 'C': |
| 1513 | Cflag = 1; |
| 1514 | break; |
| 1515 | case 'q': |
| 1516 | qflag = 1; |
| 1517 | break; |
| 1518 | default: |
| 1519 | return command_usage(&discard_cmd); |
| 1520 | } |
| 1521 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1522 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1523 | if (optind != argc - 2) { |
| 1524 | return command_usage(&discard_cmd); |
| 1525 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1526 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1527 | offset = cvtnum(argv[optind]); |
| 1528 | if (offset < 0) { |
| 1529 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 1530 | return 0; |
| 1531 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1532 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1533 | optind++; |
| 1534 | count = cvtnum(argv[optind]); |
| 1535 | if (count < 0) { |
| 1536 | printf("non-numeric length argument -- %s\n", argv[optind]); |
| 1537 | return 0; |
| 1538 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1539 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1540 | gettimeofday(&t1, NULL); |
| 1541 | ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS, |
| 1542 | count >> BDRV_SECTOR_BITS); |
| 1543 | gettimeofday(&t2, NULL); |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1544 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1545 | if (ret < 0) { |
| 1546 | printf("discard failed: %s\n", strerror(-ret)); |
| 1547 | goto out; |
| 1548 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1549 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1550 | /* Finally, report back -- -C gives a parsable format */ |
| 1551 | if (!qflag) { |
| 1552 | t2 = tsub(t2, t1); |
| 1553 | print_report("discard", &t2, offset, count, count, 1, Cflag); |
| 1554 | } |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1555 | |
| 1556 | out: |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1557 | return 0; |
Stefan Hajnoczi | edff5db | 2010-12-13 09:36:26 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1560 | static int alloc_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1561 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1562 | int64_t offset; |
| 1563 | int nb_sectors, remaining; |
| 1564 | char s1[64]; |
| 1565 | int num, sum_alloc; |
| 1566 | int ret; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1567 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1568 | offset = cvtnum(argv[1]); |
| 1569 | if (offset & 0x1ff) { |
| 1570 | printf("offset %" PRId64 " is not sector aligned\n", |
| 1571 | offset); |
| 1572 | return 0; |
| 1573 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1574 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1575 | if (argc == 3) { |
| 1576 | nb_sectors = cvtnum(argv[2]); |
| 1577 | } else { |
| 1578 | nb_sectors = 1; |
| 1579 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1580 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1581 | remaining = nb_sectors; |
| 1582 | sum_alloc = 0; |
| 1583 | while (remaining) { |
| 1584 | ret = bdrv_is_allocated(bs, offset >> 9, nb_sectors, &num); |
| 1585 | remaining -= num; |
| 1586 | if (ret) { |
| 1587 | sum_alloc += num; |
| 1588 | } |
| 1589 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1590 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1591 | cvtstr(offset, s1, sizeof(s1)); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1592 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1593 | printf("%d/%d sectors allocated at offset %s\n", |
| 1594 | sum_alloc, nb_sectors, s1); |
| 1595 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | static const cmdinfo_t alloc_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1599 | .name = "alloc", |
| 1600 | .altname = "a", |
| 1601 | .argmin = 1, |
| 1602 | .argmax = 2, |
| 1603 | .cfunc = alloc_f, |
| 1604 | .args = "off [sectors]", |
| 1605 | .oneline = "checks if a sector is present in the file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1606 | }; |
| 1607 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1608 | static int map_f(int argc, char **argv) |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1609 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1610 | int64_t offset; |
| 1611 | int64_t nb_sectors; |
| 1612 | char s1[64]; |
| 1613 | int num, num_checked; |
| 1614 | int ret; |
| 1615 | const char *retstr; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1616 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1617 | offset = 0; |
| 1618 | nb_sectors = bs->total_sectors; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1619 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1620 | do { |
| 1621 | num_checked = MIN(nb_sectors, INT_MAX); |
| 1622 | ret = bdrv_is_allocated(bs, offset, num_checked, &num); |
| 1623 | retstr = ret ? " allocated" : "not allocated"; |
| 1624 | cvtstr(offset << 9ULL, s1, sizeof(s1)); |
| 1625 | printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n", |
| 1626 | offset << 9ULL, num, num_checked, retstr, s1, ret); |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1627 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1628 | offset += num; |
| 1629 | nb_sectors -= num; |
| 1630 | } while (offset < bs->total_sectors); |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1631 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1632 | return 0; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | static const cmdinfo_t map_cmd = { |
| 1636 | .name = "map", |
| 1637 | .argmin = 0, |
| 1638 | .argmax = 0, |
| 1639 | .cfunc = map_f, |
| 1640 | .args = "", |
| 1641 | .oneline = "prints the allocated areas of a file", |
| 1642 | }; |
| 1643 | |
| 1644 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1645 | static int close_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1646 | { |
Stefan Hajnoczi | b465785 | 2011-10-27 10:54:26 +0100 | [diff] [blame] | 1647 | bdrv_delete(bs); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1648 | bs = NULL; |
| 1649 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | static const cmdinfo_t close_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1653 | .name = "close", |
| 1654 | .altname = "c", |
| 1655 | .cfunc = close_f, |
| 1656 | .oneline = "close the current open file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1657 | }; |
| 1658 | |
Christoph Hellwig | 9c4bab2 | 2009-07-10 13:33:47 +0200 | [diff] [blame] | 1659 | static int openfile(char *name, int flags, int growable) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1660 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1661 | if (bs) { |
| 1662 | fprintf(stderr, "file open already, try 'help close'\n"); |
| 1663 | return 1; |
| 1664 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1665 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1666 | if (growable) { |
| 1667 | if (bdrv_file_open(&bs, name, flags)) { |
| 1668 | fprintf(stderr, "%s: can't open device %s\n", progname, name); |
| 1669 | return 1; |
| 1670 | } |
| 1671 | } else { |
| 1672 | bs = bdrv_new("hda"); |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 1673 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1674 | if (bdrv_open(bs, name, flags, NULL) < 0) { |
| 1675 | fprintf(stderr, "%s: can't open device %s\n", progname, name); |
Stefan Hajnoczi | b465785 | 2011-10-27 10:54:26 +0100 | [diff] [blame] | 1676 | bdrv_delete(bs); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1677 | bs = NULL; |
| 1678 | return 1; |
| 1679 | } |
| 1680 | } |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 1681 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1682 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1685 | static void open_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1686 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1687 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1688 | "\n" |
| 1689 | " opens a new file in the requested mode\n" |
| 1690 | "\n" |
| 1691 | " Example:\n" |
| 1692 | " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n" |
| 1693 | "\n" |
| 1694 | " Opens a file for subsequent use by all of the other qemu-io commands.\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1695 | " -r, -- open file read-only\n" |
| 1696 | " -s, -- use snapshot file\n" |
| 1697 | " -n, -- disable host cache\n" |
Christoph Hellwig | 9c4bab2 | 2009-07-10 13:33:47 +0200 | [diff] [blame] | 1698 | " -g, -- allow file to grow (only applies to protocols)" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1699 | "\n"); |
| 1700 | } |
| 1701 | |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1702 | static int open_f(int argc, char **argv); |
| 1703 | |
| 1704 | static const cmdinfo_t open_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1705 | .name = "open", |
| 1706 | .altname = "o", |
| 1707 | .cfunc = open_f, |
| 1708 | .argmin = 1, |
| 1709 | .argmax = -1, |
| 1710 | .flags = CMD_NOFILE_OK, |
| 1711 | .args = "[-Crsn] [path]", |
| 1712 | .oneline = "open the file specified by path", |
| 1713 | .help = open_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 1714 | }; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1715 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1716 | static int open_f(int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1717 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1718 | int flags = 0; |
| 1719 | int readonly = 0; |
| 1720 | int growable = 0; |
| 1721 | int c; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1722 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1723 | while ((c = getopt(argc, argv, "snrg")) != EOF) { |
| 1724 | switch (c) { |
| 1725 | case 's': |
| 1726 | flags |= BDRV_O_SNAPSHOT; |
| 1727 | break; |
| 1728 | case 'n': |
| 1729 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 1730 | break; |
| 1731 | case 'r': |
| 1732 | readonly = 1; |
| 1733 | break; |
| 1734 | case 'g': |
| 1735 | growable = 1; |
| 1736 | break; |
| 1737 | default: |
| 1738 | return command_usage(&open_cmd); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 1739 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1740 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1741 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1742 | if (!readonly) { |
| 1743 | flags |= BDRV_O_RDWR; |
| 1744 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1745 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1746 | if (optind != argc - 1) { |
| 1747 | return command_usage(&open_cmd); |
| 1748 | } |
| 1749 | |
| 1750 | return openfile(argv[optind], flags, growable); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1753 | static int init_args_command(int index) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1754 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1755 | /* only one device allowed so far */ |
| 1756 | if (index >= 1) { |
| 1757 | return 0; |
| 1758 | } |
| 1759 | return ++index; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1762 | static int init_check_command(const cmdinfo_t *ct) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1763 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1764 | if (ct->flags & CMD_FLAG_GLOBAL) { |
| 1765 | return 1; |
| 1766 | } |
| 1767 | if (!(ct->flags & CMD_NOFILE_OK) && !bs) { |
| 1768 | fprintf(stderr, "no file open, try 'help open'\n"); |
| 1769 | return 0; |
| 1770 | } |
| 1771 | return 1; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | static void usage(const char *name) |
| 1775 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1776 | printf( |
Christoph Hellwig | 9a2d77a | 2010-01-20 18:13:42 +0100 | [diff] [blame] | 1777 | "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n" |
Stefan Weil | 84844a2 | 2009-06-22 15:08:47 +0200 | [diff] [blame] | 1778 | "QEMU Disk exerciser\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1779 | "\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1780 | " -c, --cmd command to execute\n" |
| 1781 | " -r, --read-only export read-only\n" |
| 1782 | " -s, --snapshot use snapshot file\n" |
| 1783 | " -n, --nocache disable host cache\n" |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 1784 | " -g, --growable allow file to grow (only applies to protocols)\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1785 | " -m, --misalign misalign allocations for O_DIRECT\n" |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 1786 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 1787 | " -T, --trace FILE enable trace events listed in the given file\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1788 | " -h, --help display this help and exit\n" |
| 1789 | " -V, --version output version information and exit\n" |
| 1790 | "\n", |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1791 | name); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | int main(int argc, char **argv) |
| 1796 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1797 | int readonly = 0; |
| 1798 | int growable = 0; |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 1799 | const char *sopt = "hVc:rsnmgkT:"; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1800 | const struct option lopt[] = { |
| 1801 | { "help", 0, NULL, 'h' }, |
| 1802 | { "version", 0, NULL, 'V' }, |
| 1803 | { "offset", 1, NULL, 'o' }, |
| 1804 | { "cmd", 1, NULL, 'c' }, |
| 1805 | { "read-only", 0, NULL, 'r' }, |
| 1806 | { "snapshot", 0, NULL, 's' }, |
| 1807 | { "nocache", 0, NULL, 'n' }, |
| 1808 | { "misalign", 0, NULL, 'm' }, |
| 1809 | { "growable", 0, NULL, 'g' }, |
| 1810 | { "native-aio", 0, NULL, 'k' }, |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 1811 | { "trace", 1, NULL, 'T' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1812 | { NULL, 0, NULL, 0 } |
| 1813 | }; |
| 1814 | int c; |
| 1815 | int opt_index = 0; |
| 1816 | int flags = 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1817 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1818 | progname = basename(argv[0]); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1819 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1820 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
| 1821 | switch (c) { |
| 1822 | case 's': |
| 1823 | flags |= BDRV_O_SNAPSHOT; |
| 1824 | break; |
| 1825 | case 'n': |
| 1826 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 1827 | break; |
| 1828 | case 'c': |
| 1829 | add_user_command(optarg); |
| 1830 | break; |
| 1831 | case 'r': |
| 1832 | readonly = 1; |
| 1833 | break; |
| 1834 | case 'm': |
| 1835 | misalign = 1; |
| 1836 | break; |
| 1837 | case 'g': |
| 1838 | growable = 1; |
| 1839 | break; |
| 1840 | case 'k': |
| 1841 | flags |= BDRV_O_NATIVE_AIO; |
| 1842 | break; |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 1843 | case 'T': |
| 1844 | if (!trace_backend_init(optarg, NULL)) { |
| 1845 | exit(1); /* error message will have been printed */ |
| 1846 | } |
| 1847 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1848 | case 'V': |
| 1849 | printf("%s version %s\n", progname, VERSION); |
| 1850 | exit(0); |
| 1851 | case 'h': |
| 1852 | usage(progname); |
| 1853 | exit(0); |
| 1854 | default: |
| 1855 | usage(progname); |
| 1856 | exit(1); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 1857 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1858 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1859 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1860 | if ((argc - optind) > 1) { |
| 1861 | usage(progname); |
| 1862 | exit(1); |
| 1863 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1864 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1865 | bdrv_init(); |
Christoph Hellwig | 95533d5 | 2009-06-20 21:10:44 +0200 | [diff] [blame] | 1866 | |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 1867 | qemu_init_main_loop(); |
| 1868 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1869 | /* initialize commands */ |
| 1870 | quit_init(); |
| 1871 | help_init(); |
| 1872 | add_command(&open_cmd); |
| 1873 | add_command(&close_cmd); |
| 1874 | add_command(&read_cmd); |
| 1875 | add_command(&readv_cmd); |
| 1876 | add_command(&write_cmd); |
| 1877 | add_command(&writev_cmd); |
| 1878 | add_command(&multiwrite_cmd); |
| 1879 | add_command(&aio_read_cmd); |
| 1880 | add_command(&aio_write_cmd); |
| 1881 | add_command(&aio_flush_cmd); |
| 1882 | add_command(&flush_cmd); |
| 1883 | add_command(&truncate_cmd); |
| 1884 | add_command(&length_cmd); |
| 1885 | add_command(&info_cmd); |
| 1886 | add_command(&discard_cmd); |
| 1887 | add_command(&alloc_cmd); |
| 1888 | add_command(&map_cmd); |
| 1889 | |
| 1890 | add_args_command(init_args_command); |
| 1891 | add_check_command(init_check_command); |
| 1892 | |
| 1893 | /* open the device */ |
| 1894 | if (!readonly) { |
| 1895 | flags |= BDRV_O_RDWR; |
| 1896 | } |
| 1897 | |
| 1898 | if ((argc - optind) == 1) { |
| 1899 | openfile(argv[optind], flags, growable); |
| 1900 | } |
| 1901 | command_loop(); |
| 1902 | |
| 1903 | /* |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 1904 | * Make sure all outstanding requests complete before the program exits. |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1905 | */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 1906 | bdrv_drain_all(); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1907 | |
| 1908 | if (bs) { |
Stefan Hajnoczi | b465785 | 2011-10-27 10:54:26 +0100 | [diff] [blame] | 1909 | bdrv_delete(bs); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 1910 | } |
| 1911 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1912 | } |