qemu-io.c 48 KB

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