2
0

qemu-io.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  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_iovec_destroy(&qiov);
  579. qemu_io_free(buf);
  580. return 0;
  581. }
  582. static void write_help(void)
  583. {
  584. printf(
  585. "\n"
  586. " writes a range of bytes from the given offset\n"
  587. "\n"
  588. " Example:\n"
  589. " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
  590. "\n"
  591. " Writes into a segment of the currently open file, using a buffer\n"
  592. " filled with a set pattern (0xcdcdcdcd).\n"
  593. " -b, -- write to the VM state rather than the virtual disk\n"
  594. " -p, -- use bdrv_pwrite to write the file\n"
  595. " -P, -- use different pattern to fill file\n"
  596. " -C, -- report statistics in a machine parsable format\n"
  597. " -q, -- quiet mode, do not show I/O statistics\n"
  598. " -z, -- write zeroes using bdrv_co_write_zeroes\n"
  599. "\n");
  600. }
  601. static int write_f(int argc, char **argv);
  602. static const cmdinfo_t write_cmd = {
  603. .name = "write",
  604. .altname = "w",
  605. .cfunc = write_f,
  606. .argmin = 2,
  607. .argmax = -1,
  608. .args = "[-bCpqz] [-P pattern ] off len",
  609. .oneline = "writes a number of bytes at a specified offset",
  610. .help = write_help,
  611. };
  612. static int write_f(int argc, char **argv)
  613. {
  614. struct timeval t1, t2;
  615. int Cflag = 0, pflag = 0, qflag = 0, bflag = 0, Pflag = 0, zflag = 0;
  616. int c, cnt;
  617. char *buf = NULL;
  618. int64_t offset;
  619. int count;
  620. /* Some compilers get confused and warn if this is not initialized. */
  621. int total = 0;
  622. int pattern = 0xcd;
  623. while ((c = getopt(argc, argv, "bCpP:qz")) != EOF) {
  624. switch (c) {
  625. case 'b':
  626. bflag = 1;
  627. break;
  628. case 'C':
  629. Cflag = 1;
  630. break;
  631. case 'p':
  632. pflag = 1;
  633. break;
  634. case 'P':
  635. Pflag = 1;
  636. pattern = parse_pattern(optarg);
  637. if (pattern < 0) {
  638. return 0;
  639. }
  640. break;
  641. case 'q':
  642. qflag = 1;
  643. break;
  644. case 'z':
  645. zflag = 1;
  646. break;
  647. default:
  648. return command_usage(&write_cmd);
  649. }
  650. }
  651. if (optind != argc - 2) {
  652. return command_usage(&write_cmd);
  653. }
  654. if (bflag + pflag + zflag > 1) {
  655. printf("-b, -p, or -z cannot be specified at the same time\n");
  656. return 0;
  657. }
  658. if (zflag && Pflag) {
  659. printf("-z and -P cannot be specified at the same time\n");
  660. return 0;
  661. }
  662. offset = cvtnum(argv[optind]);
  663. if (offset < 0) {
  664. printf("non-numeric length argument -- %s\n", argv[optind]);
  665. return 0;
  666. }
  667. optind++;
  668. count = cvtnum(argv[optind]);
  669. if (count < 0) {
  670. printf("non-numeric length argument -- %s\n", argv[optind]);
  671. return 0;
  672. }
  673. if (!pflag) {
  674. if (offset & 0x1ff) {
  675. printf("offset %" PRId64 " is not sector aligned\n",
  676. offset);
  677. return 0;
  678. }
  679. if (count & 0x1ff) {
  680. printf("count %d is not sector aligned\n",
  681. count);
  682. return 0;
  683. }
  684. }
  685. if (!zflag) {
  686. buf = qemu_io_alloc(count, pattern);
  687. }
  688. gettimeofday(&t1, NULL);
  689. if (pflag) {
  690. cnt = do_pwrite(buf, offset, count, &total);
  691. } else if (bflag) {
  692. cnt = do_save_vmstate(buf, offset, count, &total);
  693. } else if (zflag) {
  694. cnt = do_co_write_zeroes(offset, count, &total);
  695. } else {
  696. cnt = do_write(buf, offset, count, &total);
  697. }
  698. gettimeofday(&t2, NULL);
  699. if (cnt < 0) {
  700. printf("write failed: %s\n", strerror(-cnt));
  701. goto out;
  702. }
  703. if (qflag) {
  704. goto out;
  705. }
  706. /* Finally, report back -- -C gives a parsable format */
  707. t2 = tsub(t2, t1);
  708. print_report("wrote", &t2, offset, count, total, cnt, Cflag);
  709. out:
  710. if (!zflag) {
  711. qemu_io_free(buf);
  712. }
  713. return 0;
  714. }
  715. static void
  716. writev_help(void)
  717. {
  718. printf(
  719. "\n"
  720. " writes a range of bytes from the given offset source from multiple buffers\n"
  721. "\n"
  722. " Example:\n"
  723. " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
  724. "\n"
  725. " Writes into a segment of the currently open file, using a buffer\n"
  726. " filled with a set pattern (0xcdcdcdcd).\n"
  727. " -P, -- use different pattern to fill file\n"
  728. " -C, -- report statistics in a machine parsable format\n"
  729. " -q, -- quiet mode, do not show I/O statistics\n"
  730. "\n");
  731. }
  732. static int writev_f(int argc, char **argv);
  733. static const cmdinfo_t writev_cmd = {
  734. .name = "writev",
  735. .cfunc = writev_f,
  736. .argmin = 2,
  737. .argmax = -1,
  738. .args = "[-Cq] [-P pattern ] off len [len..]",
  739. .oneline = "writes a number of bytes at a specified offset",
  740. .help = writev_help,
  741. };
  742. static int writev_f(int argc, char **argv)
  743. {
  744. struct timeval t1, t2;
  745. int Cflag = 0, qflag = 0;
  746. int c, cnt;
  747. char *buf;
  748. int64_t offset;
  749. /* Some compilers get confused and warn if this is not initialized. */
  750. int total = 0;
  751. int nr_iov;
  752. int pattern = 0xcd;
  753. QEMUIOVector qiov;
  754. while ((c = getopt(argc, argv, "CqP:")) != EOF) {
  755. switch (c) {
  756. case 'C':
  757. Cflag = 1;
  758. break;
  759. case 'q':
  760. qflag = 1;
  761. break;
  762. case 'P':
  763. pattern = parse_pattern(optarg);
  764. if (pattern < 0) {
  765. return 0;
  766. }
  767. break;
  768. default:
  769. return command_usage(&writev_cmd);
  770. }
  771. }
  772. if (optind > argc - 2) {
  773. return command_usage(&writev_cmd);
  774. }
  775. offset = cvtnum(argv[optind]);
  776. if (offset < 0) {
  777. printf("non-numeric length argument -- %s\n", argv[optind]);
  778. return 0;
  779. }
  780. optind++;
  781. if (offset & 0x1ff) {
  782. printf("offset %" PRId64 " is not sector aligned\n",
  783. offset);
  784. return 0;
  785. }
  786. nr_iov = argc - optind;
  787. buf = create_iovec(&qiov, &argv[optind], nr_iov, pattern);
  788. if (buf == NULL) {
  789. return 0;
  790. }
  791. gettimeofday(&t1, NULL);
  792. cnt = do_aio_writev(&qiov, offset, &total);
  793. gettimeofday(&t2, NULL);
  794. if (cnt < 0) {
  795. printf("writev failed: %s\n", strerror(-cnt));
  796. goto out;
  797. }
  798. if (qflag) {
  799. goto out;
  800. }
  801. /* Finally, report back -- -C gives a parsable format */
  802. t2 = tsub(t2, t1);
  803. print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
  804. out:
  805. qemu_iovec_destroy(&qiov);
  806. qemu_io_free(buf);
  807. return 0;
  808. }
  809. static void multiwrite_help(void)
  810. {
  811. printf(
  812. "\n"
  813. " writes a range of bytes from the given offset source from multiple buffers,\n"
  814. " in a batch of requests that may be merged by qemu\n"
  815. "\n"
  816. " Example:\n"
  817. " 'multiwrite 512 1k 1k ; 4k 1k'\n"
  818. " writes 2 kB at 512 bytes and 1 kB at 4 kB into the open file\n"
  819. "\n"
  820. " Writes into a segment of the currently open file, using a buffer\n"
  821. " filled with a set pattern (0xcdcdcdcd). The pattern byte is increased\n"
  822. " by one for each request contained in the multiwrite command.\n"
  823. " -P, -- use different pattern to fill file\n"
  824. " -C, -- report statistics in a machine parsable format\n"
  825. " -q, -- quiet mode, do not show I/O statistics\n"
  826. "\n");
  827. }
  828. static int multiwrite_f(int argc, char **argv);
  829. static const cmdinfo_t multiwrite_cmd = {
  830. .name = "multiwrite",
  831. .cfunc = multiwrite_f,
  832. .argmin = 2,
  833. .argmax = -1,
  834. .args = "[-Cq] [-P pattern ] off len [len..] [; off len [len..]..]",
  835. .oneline = "issues multiple write requests at once",
  836. .help = multiwrite_help,
  837. };
  838. static int multiwrite_f(int argc, char **argv)
  839. {
  840. struct timeval t1, t2;
  841. int Cflag = 0, qflag = 0;
  842. int c, cnt;
  843. char **buf;
  844. int64_t offset, first_offset = 0;
  845. /* Some compilers get confused and warn if this is not initialized. */
  846. int total = 0;
  847. int nr_iov;
  848. int nr_reqs;
  849. int pattern = 0xcd;
  850. QEMUIOVector *qiovs;
  851. int i;
  852. BlockRequest *reqs;
  853. while ((c = getopt(argc, argv, "CqP:")) != EOF) {
  854. switch (c) {
  855. case 'C':
  856. Cflag = 1;
  857. break;
  858. case 'q':
  859. qflag = 1;
  860. break;
  861. case 'P':
  862. pattern = parse_pattern(optarg);
  863. if (pattern < 0) {
  864. return 0;
  865. }
  866. break;
  867. default:
  868. return command_usage(&writev_cmd);
  869. }
  870. }
  871. if (optind > argc - 2) {
  872. return command_usage(&writev_cmd);
  873. }
  874. nr_reqs = 1;
  875. for (i = optind; i < argc; i++) {
  876. if (!strcmp(argv[i], ";")) {
  877. nr_reqs++;
  878. }
  879. }
  880. reqs = g_malloc0(nr_reqs * sizeof(*reqs));
  881. buf = g_malloc0(nr_reqs * sizeof(*buf));
  882. qiovs = g_malloc(nr_reqs * sizeof(*qiovs));
  883. for (i = 0; i < nr_reqs && optind < argc; i++) {
  884. int j;
  885. /* Read the offset of the request */
  886. offset = cvtnum(argv[optind]);
  887. if (offset < 0) {
  888. printf("non-numeric offset argument -- %s\n", argv[optind]);
  889. goto out;
  890. }
  891. optind++;
  892. if (offset & 0x1ff) {
  893. printf("offset %lld is not sector aligned\n",
  894. (long long)offset);
  895. goto out;
  896. }
  897. if (i == 0) {
  898. first_offset = offset;
  899. }
  900. /* Read lengths for qiov entries */
  901. for (j = optind; j < argc; j++) {
  902. if (!strcmp(argv[j], ";")) {
  903. break;
  904. }
  905. }
  906. nr_iov = j - optind;
  907. /* Build request */
  908. buf[i] = create_iovec(&qiovs[i], &argv[optind], nr_iov, pattern);
  909. if (buf[i] == NULL) {
  910. goto out;
  911. }
  912. reqs[i].qiov = &qiovs[i];
  913. reqs[i].sector = offset >> 9;
  914. reqs[i].nb_sectors = reqs[i].qiov->size >> 9;
  915. optind = j + 1;
  916. pattern++;
  917. }
  918. /* If there were empty requests at the end, ignore them */
  919. nr_reqs = i;
  920. gettimeofday(&t1, NULL);
  921. cnt = do_aio_multiwrite(reqs, nr_reqs, &total);
  922. gettimeofday(&t2, NULL);
  923. if (cnt < 0) {
  924. printf("aio_multiwrite failed: %s\n", strerror(-cnt));
  925. goto out;
  926. }
  927. if (qflag) {
  928. goto out;
  929. }
  930. /* Finally, report back -- -C gives a parsable format */
  931. t2 = tsub(t2, t1);
  932. print_report("wrote", &t2, first_offset, total, total, cnt, Cflag);
  933. out:
  934. for (i = 0; i < nr_reqs; i++) {
  935. qemu_io_free(buf[i]);
  936. if (reqs[i].qiov != NULL) {
  937. qemu_iovec_destroy(&qiovs[i]);
  938. }
  939. }
  940. g_free(buf);
  941. g_free(reqs);
  942. g_free(qiovs);
  943. return 0;
  944. }
  945. struct aio_ctx {
  946. QEMUIOVector qiov;
  947. int64_t offset;
  948. char *buf;
  949. int qflag;
  950. int vflag;
  951. int Cflag;
  952. int Pflag;
  953. int pattern;
  954. struct timeval t1;
  955. };
  956. static void aio_write_done(void *opaque, int ret)
  957. {
  958. struct aio_ctx *ctx = opaque;
  959. struct timeval t2;
  960. gettimeofday(&t2, NULL);
  961. if (ret < 0) {
  962. printf("aio_write failed: %s\n", strerror(-ret));
  963. goto out;
  964. }
  965. if (ctx->qflag) {
  966. goto out;
  967. }
  968. /* Finally, report back -- -C gives a parsable format */
  969. t2 = tsub(t2, ctx->t1);
  970. print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
  971. ctx->qiov.size, 1, ctx->Cflag);
  972. out:
  973. qemu_io_free(ctx->buf);
  974. qemu_iovec_destroy(&ctx->qiov);
  975. g_free(ctx);
  976. }
  977. static void aio_read_done(void *opaque, int ret)
  978. {
  979. struct aio_ctx *ctx = opaque;
  980. struct timeval t2;
  981. gettimeofday(&t2, NULL);
  982. if (ret < 0) {
  983. printf("readv failed: %s\n", strerror(-ret));
  984. goto out;
  985. }
  986. if (ctx->Pflag) {
  987. void *cmp_buf = g_malloc(ctx->qiov.size);
  988. memset(cmp_buf, ctx->pattern, ctx->qiov.size);
  989. if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
  990. printf("Pattern verification failed at offset %"
  991. PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
  992. }
  993. g_free(cmp_buf);
  994. }
  995. if (ctx->qflag) {
  996. goto out;
  997. }
  998. if (ctx->vflag) {
  999. dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
  1000. }
  1001. /* Finally, report back -- -C gives a parsable format */
  1002. t2 = tsub(t2, ctx->t1);
  1003. print_report("read", &t2, ctx->offset, ctx->qiov.size,
  1004. ctx->qiov.size, 1, ctx->Cflag);
  1005. out:
  1006. qemu_io_free(ctx->buf);
  1007. qemu_iovec_destroy(&ctx->qiov);
  1008. g_free(ctx);
  1009. }
  1010. static void aio_read_help(void)
  1011. {
  1012. printf(
  1013. "\n"
  1014. " asynchronously reads a range of bytes from the given offset\n"
  1015. "\n"
  1016. " Example:\n"
  1017. " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
  1018. "\n"
  1019. " Reads a segment of the currently open file, optionally dumping it to the\n"
  1020. " standard output stream (with -v option) for subsequent inspection.\n"
  1021. " The read is performed asynchronously and the aio_flush command must be\n"
  1022. " used to ensure all outstanding aio requests have been completed.\n"
  1023. " -C, -- report statistics in a machine parsable format\n"
  1024. " -P, -- use a pattern to verify read data\n"
  1025. " -v, -- dump buffer to standard output\n"
  1026. " -q, -- quiet mode, do not show I/O statistics\n"
  1027. "\n");
  1028. }
  1029. static int aio_read_f(int argc, char **argv);
  1030. static const cmdinfo_t aio_read_cmd = {
  1031. .name = "aio_read",
  1032. .cfunc = aio_read_f,
  1033. .argmin = 2,
  1034. .argmax = -1,
  1035. .args = "[-Cqv] [-P pattern ] off len [len..]",
  1036. .oneline = "asynchronously reads a number of bytes",
  1037. .help = aio_read_help,
  1038. };
  1039. static int aio_read_f(int argc, char **argv)
  1040. {
  1041. int nr_iov, c;
  1042. struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
  1043. while ((c = getopt(argc, argv, "CP:qv")) != EOF) {
  1044. switch (c) {
  1045. case 'C':
  1046. ctx->Cflag = 1;
  1047. break;
  1048. case 'P':
  1049. ctx->Pflag = 1;
  1050. ctx->pattern = parse_pattern(optarg);
  1051. if (ctx->pattern < 0) {
  1052. g_free(ctx);
  1053. return 0;
  1054. }
  1055. break;
  1056. case 'q':
  1057. ctx->qflag = 1;
  1058. break;
  1059. case 'v':
  1060. ctx->vflag = 1;
  1061. break;
  1062. default:
  1063. g_free(ctx);
  1064. return command_usage(&aio_read_cmd);
  1065. }
  1066. }
  1067. if (optind > argc - 2) {
  1068. g_free(ctx);
  1069. return command_usage(&aio_read_cmd);
  1070. }
  1071. ctx->offset = cvtnum(argv[optind]);
  1072. if (ctx->offset < 0) {
  1073. printf("non-numeric length argument -- %s\n", argv[optind]);
  1074. g_free(ctx);
  1075. return 0;
  1076. }
  1077. optind++;
  1078. if (ctx->offset & 0x1ff) {
  1079. printf("offset %" PRId64 " is not sector aligned\n",
  1080. ctx->offset);
  1081. g_free(ctx);
  1082. return 0;
  1083. }
  1084. nr_iov = argc - optind;
  1085. ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, 0xab);
  1086. if (ctx->buf == NULL) {
  1087. g_free(ctx);
  1088. return 0;
  1089. }
  1090. gettimeofday(&ctx->t1, NULL);
  1091. bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
  1092. ctx->qiov.size >> 9, aio_read_done, ctx);
  1093. return 0;
  1094. }
  1095. static void aio_write_help(void)
  1096. {
  1097. printf(
  1098. "\n"
  1099. " asynchronously writes a range of bytes from the given offset source\n"
  1100. " from multiple buffers\n"
  1101. "\n"
  1102. " Example:\n"
  1103. " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
  1104. "\n"
  1105. " Writes into a segment of the currently open file, using a buffer\n"
  1106. " filled with a set pattern (0xcdcdcdcd).\n"
  1107. " The write is performed asynchronously and the aio_flush command must be\n"
  1108. " used to ensure all outstanding aio requests have been completed.\n"
  1109. " -P, -- use different pattern to fill file\n"
  1110. " -C, -- report statistics in a machine parsable format\n"
  1111. " -q, -- quiet mode, do not show I/O statistics\n"
  1112. "\n");
  1113. }
  1114. static int aio_write_f(int argc, char **argv);
  1115. static const cmdinfo_t aio_write_cmd = {
  1116. .name = "aio_write",
  1117. .cfunc = aio_write_f,
  1118. .argmin = 2,
  1119. .argmax = -1,
  1120. .args = "[-Cq] [-P pattern ] off len [len..]",
  1121. .oneline = "asynchronously writes a number of bytes",
  1122. .help = aio_write_help,
  1123. };
  1124. static int aio_write_f(int argc, char **argv)
  1125. {
  1126. int nr_iov, c;
  1127. int pattern = 0xcd;
  1128. struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
  1129. while ((c = getopt(argc, argv, "CqP:")) != EOF) {
  1130. switch (c) {
  1131. case 'C':
  1132. ctx->Cflag = 1;
  1133. break;
  1134. case 'q':
  1135. ctx->qflag = 1;
  1136. break;
  1137. case 'P':
  1138. pattern = parse_pattern(optarg);
  1139. if (pattern < 0) {
  1140. g_free(ctx);
  1141. return 0;
  1142. }
  1143. break;
  1144. default:
  1145. g_free(ctx);
  1146. return command_usage(&aio_write_cmd);
  1147. }
  1148. }
  1149. if (optind > argc - 2) {
  1150. g_free(ctx);
  1151. return command_usage(&aio_write_cmd);
  1152. }
  1153. ctx->offset = cvtnum(argv[optind]);
  1154. if (ctx->offset < 0) {
  1155. printf("non-numeric length argument -- %s\n", argv[optind]);
  1156. g_free(ctx);
  1157. return 0;
  1158. }
  1159. optind++;
  1160. if (ctx->offset & 0x1ff) {
  1161. printf("offset %" PRId64 " is not sector aligned\n",
  1162. ctx->offset);
  1163. g_free(ctx);
  1164. return 0;
  1165. }
  1166. nr_iov = argc - optind;
  1167. ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);
  1168. if (ctx->buf == NULL) {
  1169. g_free(ctx);
  1170. return 0;
  1171. }
  1172. gettimeofday(&ctx->t1, NULL);
  1173. bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
  1174. ctx->qiov.size >> 9, aio_write_done, ctx);
  1175. return 0;
  1176. }
  1177. static int aio_flush_f(int argc, char **argv)
  1178. {
  1179. qemu_aio_flush();
  1180. return 0;
  1181. }
  1182. static const cmdinfo_t aio_flush_cmd = {
  1183. .name = "aio_flush",
  1184. .cfunc = aio_flush_f,
  1185. .oneline = "completes all outstanding aio requests"
  1186. };
  1187. static int flush_f(int argc, char **argv)
  1188. {
  1189. bdrv_flush(bs);
  1190. return 0;
  1191. }
  1192. static const cmdinfo_t flush_cmd = {
  1193. .name = "flush",
  1194. .altname = "f",
  1195. .cfunc = flush_f,
  1196. .oneline = "flush all in-core file state to disk",
  1197. };
  1198. static int truncate_f(int argc, char **argv)
  1199. {
  1200. int64_t offset;
  1201. int ret;
  1202. offset = cvtnum(argv[1]);
  1203. if (offset < 0) {
  1204. printf("non-numeric truncate argument -- %s\n", argv[1]);
  1205. return 0;
  1206. }
  1207. ret = bdrv_truncate(bs, offset);
  1208. if (ret < 0) {
  1209. printf("truncate: %s\n", strerror(-ret));
  1210. return 0;
  1211. }
  1212. return 0;
  1213. }
  1214. static const cmdinfo_t truncate_cmd = {
  1215. .name = "truncate",
  1216. .altname = "t",
  1217. .cfunc = truncate_f,
  1218. .argmin = 1,
  1219. .argmax = 1,
  1220. .args = "off",
  1221. .oneline = "truncates the current file at the given offset",
  1222. };
  1223. static int length_f(int argc, char **argv)
  1224. {
  1225. int64_t size;
  1226. char s1[64];
  1227. size = bdrv_getlength(bs);
  1228. if (size < 0) {
  1229. printf("getlength: %s\n", strerror(-size));
  1230. return 0;
  1231. }
  1232. cvtstr(size, s1, sizeof(s1));
  1233. printf("%s\n", s1);
  1234. return 0;
  1235. }
  1236. static const cmdinfo_t length_cmd = {
  1237. .name = "length",
  1238. .altname = "l",
  1239. .cfunc = length_f,
  1240. .oneline = "gets the length of the current file",
  1241. };
  1242. static int info_f(int argc, char **argv)
  1243. {
  1244. BlockDriverInfo bdi;
  1245. char s1[64], s2[64];
  1246. int ret;
  1247. if (bs->drv && bs->drv->format_name) {
  1248. printf("format name: %s\n", bs->drv->format_name);
  1249. }
  1250. if (bs->drv && bs->drv->protocol_name) {
  1251. printf("format name: %s\n", bs->drv->protocol_name);
  1252. }
  1253. ret = bdrv_get_info(bs, &bdi);
  1254. if (ret) {
  1255. return 0;
  1256. }
  1257. cvtstr(bdi.cluster_size, s1, sizeof(s1));
  1258. cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
  1259. printf("cluster size: %s\n", s1);
  1260. printf("vm state offset: %s\n", s2);
  1261. return 0;
  1262. }
  1263. static const cmdinfo_t info_cmd = {
  1264. .name = "info",
  1265. .altname = "i",
  1266. .cfunc = info_f,
  1267. .oneline = "prints information about the current file",
  1268. };
  1269. static void discard_help(void)
  1270. {
  1271. printf(
  1272. "\n"
  1273. " discards a range of bytes from the given offset\n"
  1274. "\n"
  1275. " Example:\n"
  1276. " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
  1277. "\n"
  1278. " Discards a segment of the currently open file.\n"
  1279. " -C, -- report statistics in a machine parsable format\n"
  1280. " -q, -- quiet mode, do not show I/O statistics\n"
  1281. "\n");
  1282. }
  1283. static int discard_f(int argc, char **argv);
  1284. static const cmdinfo_t discard_cmd = {
  1285. .name = "discard",
  1286. .altname = "d",
  1287. .cfunc = discard_f,
  1288. .argmin = 2,
  1289. .argmax = -1,
  1290. .args = "[-Cq] off len",
  1291. .oneline = "discards a number of bytes at a specified offset",
  1292. .help = discard_help,
  1293. };
  1294. static int discard_f(int argc, char **argv)
  1295. {
  1296. struct timeval t1, t2;
  1297. int Cflag = 0, qflag = 0;
  1298. int c, ret;
  1299. int64_t offset;
  1300. int count;
  1301. while ((c = getopt(argc, argv, "Cq")) != EOF) {
  1302. switch (c) {
  1303. case 'C':
  1304. Cflag = 1;
  1305. break;
  1306. case 'q':
  1307. qflag = 1;
  1308. break;
  1309. default:
  1310. return command_usage(&discard_cmd);
  1311. }
  1312. }
  1313. if (optind != argc - 2) {
  1314. return command_usage(&discard_cmd);
  1315. }
  1316. offset = cvtnum(argv[optind]);
  1317. if (offset < 0) {
  1318. printf("non-numeric length argument -- %s\n", argv[optind]);
  1319. return 0;
  1320. }
  1321. optind++;
  1322. count = cvtnum(argv[optind]);
  1323. if (count < 0) {
  1324. printf("non-numeric length argument -- %s\n", argv[optind]);
  1325. return 0;
  1326. }
  1327. gettimeofday(&t1, NULL);
  1328. ret = bdrv_discard(bs, offset >> BDRV_SECTOR_BITS,
  1329. count >> BDRV_SECTOR_BITS);
  1330. gettimeofday(&t2, NULL);
  1331. if (ret < 0) {
  1332. printf("discard failed: %s\n", strerror(-ret));
  1333. goto out;
  1334. }
  1335. /* Finally, report back -- -C gives a parsable format */
  1336. if (!qflag) {
  1337. t2 = tsub(t2, t1);
  1338. print_report("discard", &t2, offset, count, count, 1, Cflag);
  1339. }
  1340. out:
  1341. return 0;
  1342. }
  1343. static int alloc_f(int argc, char **argv)
  1344. {
  1345. int64_t offset, sector_num;
  1346. int nb_sectors, remaining;
  1347. char s1[64];
  1348. int num, sum_alloc;
  1349. int ret;
  1350. offset = cvtnum(argv[1]);
  1351. if (offset & 0x1ff) {
  1352. printf("offset %" PRId64 " is not sector aligned\n",
  1353. offset);
  1354. return 0;
  1355. }
  1356. if (argc == 3) {
  1357. nb_sectors = cvtnum(argv[2]);
  1358. } else {
  1359. nb_sectors = 1;
  1360. }
  1361. remaining = nb_sectors;
  1362. sum_alloc = 0;
  1363. sector_num = offset >> 9;
  1364. while (remaining) {
  1365. ret = bdrv_is_allocated(bs, sector_num, remaining, &num);
  1366. sector_num += num;
  1367. remaining -= num;
  1368. if (ret) {
  1369. sum_alloc += num;
  1370. }
  1371. if (num == 0) {
  1372. nb_sectors -= remaining;
  1373. remaining = 0;
  1374. }
  1375. }
  1376. cvtstr(offset, s1, sizeof(s1));
  1377. printf("%d/%d sectors allocated at offset %s\n",
  1378. sum_alloc, nb_sectors, s1);
  1379. return 0;
  1380. }
  1381. static const cmdinfo_t alloc_cmd = {
  1382. .name = "alloc",
  1383. .altname = "a",
  1384. .argmin = 1,
  1385. .argmax = 2,
  1386. .cfunc = alloc_f,
  1387. .args = "off [sectors]",
  1388. .oneline = "checks if a sector is present in the file",
  1389. };
  1390. static int map_f(int argc, char **argv)
  1391. {
  1392. int64_t offset;
  1393. int64_t nb_sectors;
  1394. char s1[64];
  1395. int num, num_checked;
  1396. int ret;
  1397. const char *retstr;
  1398. offset = 0;
  1399. nb_sectors = bs->total_sectors;
  1400. do {
  1401. num_checked = MIN(nb_sectors, INT_MAX);
  1402. ret = bdrv_is_allocated(bs, offset, num_checked, &num);
  1403. retstr = ret ? " allocated" : "not allocated";
  1404. cvtstr(offset << 9ULL, s1, sizeof(s1));
  1405. printf("[% 24" PRId64 "] % 8d/% 8d sectors %s at offset %s (%d)\n",
  1406. offset << 9ULL, num, num_checked, retstr, s1, ret);
  1407. offset += num;
  1408. nb_sectors -= num;
  1409. } while (offset < bs->total_sectors);
  1410. return 0;
  1411. }
  1412. static const cmdinfo_t map_cmd = {
  1413. .name = "map",
  1414. .argmin = 0,
  1415. .argmax = 0,
  1416. .cfunc = map_f,
  1417. .args = "",
  1418. .oneline = "prints the allocated areas of a file",
  1419. };
  1420. static int abort_f(int argc, char **argv)
  1421. {
  1422. abort();
  1423. }
  1424. static const cmdinfo_t abort_cmd = {
  1425. .name = "abort",
  1426. .cfunc = abort_f,
  1427. .flags = CMD_NOFILE_OK,
  1428. .oneline = "simulate a program crash using abort(3)",
  1429. };
  1430. static int close_f(int argc, char **argv)
  1431. {
  1432. bdrv_delete(bs);
  1433. bs = NULL;
  1434. return 0;
  1435. }
  1436. static const cmdinfo_t close_cmd = {
  1437. .name = "close",
  1438. .altname = "c",
  1439. .cfunc = close_f,
  1440. .oneline = "close the current open file",
  1441. };
  1442. static int openfile(char *name, int flags, int growable)
  1443. {
  1444. if (bs) {
  1445. fprintf(stderr, "file open already, try 'help close'\n");
  1446. return 1;
  1447. }
  1448. if (growable) {
  1449. if (bdrv_file_open(&bs, name, flags)) {
  1450. fprintf(stderr, "%s: can't open device %s\n", progname, name);
  1451. return 1;
  1452. }
  1453. } else {
  1454. bs = bdrv_new("hda");
  1455. if (bdrv_open(bs, name, flags, NULL) < 0) {
  1456. fprintf(stderr, "%s: can't open device %s\n", progname, name);
  1457. bdrv_delete(bs);
  1458. bs = NULL;
  1459. return 1;
  1460. }
  1461. }
  1462. return 0;
  1463. }
  1464. static void open_help(void)
  1465. {
  1466. printf(
  1467. "\n"
  1468. " opens a new file in the requested mode\n"
  1469. "\n"
  1470. " Example:\n"
  1471. " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
  1472. "\n"
  1473. " Opens a file for subsequent use by all of the other qemu-io commands.\n"
  1474. " -r, -- open file read-only\n"
  1475. " -s, -- use snapshot file\n"
  1476. " -n, -- disable host cache\n"
  1477. " -g, -- allow file to grow (only applies to protocols)"
  1478. "\n");
  1479. }
  1480. static int open_f(int argc, char **argv);
  1481. static const cmdinfo_t open_cmd = {
  1482. .name = "open",
  1483. .altname = "o",
  1484. .cfunc = open_f,
  1485. .argmin = 1,
  1486. .argmax = -1,
  1487. .flags = CMD_NOFILE_OK,
  1488. .args = "[-Crsn] [path]",
  1489. .oneline = "open the file specified by path",
  1490. .help = open_help,
  1491. };
  1492. static int open_f(int argc, char **argv)
  1493. {
  1494. int flags = 0;
  1495. int readonly = 0;
  1496. int growable = 0;
  1497. int c;
  1498. while ((c = getopt(argc, argv, "snrg")) != EOF) {
  1499. switch (c) {
  1500. case 's':
  1501. flags |= BDRV_O_SNAPSHOT;
  1502. break;
  1503. case 'n':
  1504. flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
  1505. break;
  1506. case 'r':
  1507. readonly = 1;
  1508. break;
  1509. case 'g':
  1510. growable = 1;
  1511. break;
  1512. default:
  1513. return command_usage(&open_cmd);
  1514. }
  1515. }
  1516. if (!readonly) {
  1517. flags |= BDRV_O_RDWR;
  1518. }
  1519. if (optind != argc - 1) {
  1520. return command_usage(&open_cmd);
  1521. }
  1522. return openfile(argv[optind], flags, growable);
  1523. }
  1524. static int init_args_command(int index)
  1525. {
  1526. /* only one device allowed so far */
  1527. if (index >= 1) {
  1528. return 0;
  1529. }
  1530. return ++index;
  1531. }
  1532. static int init_check_command(const cmdinfo_t *ct)
  1533. {
  1534. if (ct->flags & CMD_FLAG_GLOBAL) {
  1535. return 1;
  1536. }
  1537. if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
  1538. fprintf(stderr, "no file open, try 'help open'\n");
  1539. return 0;
  1540. }
  1541. return 1;
  1542. }
  1543. static void usage(const char *name)
  1544. {
  1545. printf(
  1546. "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
  1547. "QEMU Disk exerciser\n"
  1548. "\n"
  1549. " -c, --cmd command to execute\n"
  1550. " -r, --read-only export read-only\n"
  1551. " -s, --snapshot use snapshot file\n"
  1552. " -n, --nocache disable host cache\n"
  1553. " -g, --growable allow file to grow (only applies to protocols)\n"
  1554. " -m, --misalign misalign allocations for O_DIRECT\n"
  1555. " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
  1556. " -t, --cache=MODE use the given cache mode for the image\n"
  1557. " -T, --trace FILE enable trace events listed in the given file\n"
  1558. " -h, --help display this help and exit\n"
  1559. " -V, --version output version information and exit\n"
  1560. "\n",
  1561. name);
  1562. }
  1563. int main(int argc, char **argv)
  1564. {
  1565. int readonly = 0;
  1566. int growable = 0;
  1567. const char *sopt = "hVc:rsnmgkt:T:";
  1568. const struct option lopt[] = {
  1569. { "help", 0, NULL, 'h' },
  1570. { "version", 0, NULL, 'V' },
  1571. { "offset", 1, NULL, 'o' },
  1572. { "cmd", 1, NULL, 'c' },
  1573. { "read-only", 0, NULL, 'r' },
  1574. { "snapshot", 0, NULL, 's' },
  1575. { "nocache", 0, NULL, 'n' },
  1576. { "misalign", 0, NULL, 'm' },
  1577. { "growable", 0, NULL, 'g' },
  1578. { "native-aio", 0, NULL, 'k' },
  1579. { "cache", 1, NULL, 't' },
  1580. { "trace", 1, NULL, 'T' },
  1581. { NULL, 0, NULL, 0 }
  1582. };
  1583. int c;
  1584. int opt_index = 0;
  1585. int flags = 0;
  1586. progname = basename(argv[0]);
  1587. while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
  1588. switch (c) {
  1589. case 's':
  1590. flags |= BDRV_O_SNAPSHOT;
  1591. break;
  1592. case 'n':
  1593. flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
  1594. break;
  1595. case 'c':
  1596. add_user_command(optarg);
  1597. break;
  1598. case 'r':
  1599. readonly = 1;
  1600. break;
  1601. case 'm':
  1602. misalign = 1;
  1603. break;
  1604. case 'g':
  1605. growable = 1;
  1606. break;
  1607. case 'k':
  1608. flags |= BDRV_O_NATIVE_AIO;
  1609. break;
  1610. case 't':
  1611. if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
  1612. error_report("Invalid cache option: %s", optarg);
  1613. exit(1);
  1614. }
  1615. break;
  1616. case 'T':
  1617. if (!trace_backend_init(optarg, NULL)) {
  1618. exit(1); /* error message will have been printed */
  1619. }
  1620. break;
  1621. case 'V':
  1622. printf("%s version %s\n", progname, VERSION);
  1623. exit(0);
  1624. case 'h':
  1625. usage(progname);
  1626. exit(0);
  1627. default:
  1628. usage(progname);
  1629. exit(1);
  1630. }
  1631. }
  1632. if ((argc - optind) > 1) {
  1633. usage(progname);
  1634. exit(1);
  1635. }
  1636. qemu_init_main_loop();
  1637. bdrv_init();
  1638. /* initialize commands */
  1639. quit_init();
  1640. help_init();
  1641. add_command(&open_cmd);
  1642. add_command(&close_cmd);
  1643. add_command(&read_cmd);
  1644. add_command(&readv_cmd);
  1645. add_command(&write_cmd);
  1646. add_command(&writev_cmd);
  1647. add_command(&multiwrite_cmd);
  1648. add_command(&aio_read_cmd);
  1649. add_command(&aio_write_cmd);
  1650. add_command(&aio_flush_cmd);
  1651. add_command(&flush_cmd);
  1652. add_command(&truncate_cmd);
  1653. add_command(&length_cmd);
  1654. add_command(&info_cmd);
  1655. add_command(&discard_cmd);
  1656. add_command(&alloc_cmd);
  1657. add_command(&map_cmd);
  1658. add_command(&abort_cmd);
  1659. add_args_command(init_args_command);
  1660. add_check_command(init_check_command);
  1661. /* open the device */
  1662. if (!readonly) {
  1663. flags |= BDRV_O_RDWR;
  1664. }
  1665. if ((argc - optind) == 1) {
  1666. openfile(argv[optind], flags, growable);
  1667. }
  1668. command_loop();
  1669. /*
  1670. * Make sure all outstanding requests complete before the program exits.
  1671. */
  1672. bdrv_drain_all();
  1673. if (bs) {
  1674. bdrv_delete(bs);
  1675. }
  1676. return 0;
  1677. }