qemu-io.c 46 KB

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