lmdd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. char *id = "$Id: lmdd.c,v 1.23 1997/12/01 23:47:59 lm Exp $\n";
  2. /*
  3. * defaults:
  4. * bs=8k
  5. * count=forever
  6. * if=internal
  7. * of=internal
  8. * ipat=0
  9. * opat=0
  10. * mismatch=0
  11. * rusage=0
  12. * flush=0
  13. * rand=0
  14. * print=0
  15. * direct=0
  16. * rt=0
  17. * rtmax=0
  18. * wtmax=0
  19. * rtmin=0
  20. * wtmin=0
  21. * label=""
  22. * shorthands:
  23. * k, m, g are 2^10, 2^20, 2^30 multipliers.
  24. * K, M, G are 10^3, 10^6, 10^9 multipliers.
  25. * recognizes "internal" as an internal /dev/zero /dev/null file.
  26. *
  27. * Copyright (c) 1994-1998 by Larry McVoy. All rights reserved.
  28. * See the file COPYING for the licensing terms.
  29. *
  30. * TODO - rewrite this entire thing from scratch. This is disgusting code.
  31. */
  32. #ifndef __Lynx__
  33. #define FLUSH
  34. #endif
  35. #include <fcntl.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <signal.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <sys/types.h>
  42. #include <sys/wait.h>
  43. #include <sys/time.h>
  44. #include "bench.h"
  45. #undef ALIGN
  46. #define ALIGN(x, bs) ((x + (bs - 1)) & ~(bs - 1))
  47. #ifdef FLUSH
  48. #include <sys/mman.h>
  49. #include <sys/stat.h>
  50. void flush(void);
  51. #endif
  52. #define USE_VALLOC
  53. #ifdef USE_VALLOC
  54. #define VALLOC valloc
  55. #else
  56. #define VALLOC malloc
  57. #endif
  58. #ifdef __sgi
  59. # define LSEEK(a,b,c) (uint64)lseek64(a, (off64_t)b, c)
  60. # define ATOL(s) atoll(s)
  61. #endif
  62. #ifdef linux
  63. # define LSEEK(a,b,c) (uint64)lseek64(a, (uint64)b, (uint64)c)
  64. # define ATOL(s) atoll(s)
  65. #endif
  66. #if !defined(linux) && !defined(__sgi)
  67. # define LSEEK(a,b,c) (uint64)lseek(a, b, c)
  68. # define ATOL(s) atol(s)
  69. #endif
  70. int awrite, poff, out, Print, Fsync, Sync, Flush, Bsize, ru;
  71. uint64 Start, End, Rand, int_count;
  72. int hash;
  73. int Realtime, Notrunc;
  74. int Rtmax, Rtmin, Wtmax, Wtmin;
  75. int rthist[12]; /* histogram of read times */
  76. int wthist[12]; /* histogram of write times */
  77. char *Label;
  78. uint64 *norepeat;
  79. int norepeats = -1;
  80. #ifdef USE_BDS
  81. bds_msg *m1, *m2;
  82. #endif
  83. uint64 getarg();
  84. int been_there(uint64 off);
  85. int getfile(char *s, int ac, char **av);
  86. char *cmds[] = {
  87. "bs", /* block size */
  88. "bufs", /* use this many buffers round robin */
  89. "count", /* number of blocks */
  90. #ifdef DBG
  91. "debug", /* set external variable "dbg" */
  92. #endif
  93. #ifdef O_DIRECT
  94. "direct", /* direct I/O on input and output */
  95. "idirect", /* direct I/O on input */
  96. "odirect", /* direct I/O on output */
  97. #endif
  98. #ifdef FLUSH
  99. "flush", /* map in out and invalidate (flush) */
  100. #endif
  101. "fork", /* fork to do write I/O */
  102. "fsync", /* fsync output before exit */
  103. "if", /* input file */
  104. "ipat", /* check input for pattern */
  105. "label", /* prefix print out with this */
  106. "mismatch", /* stop at first mismatch */
  107. "move", /* instead of count, limit transfer to this */
  108. "of", /* output file */
  109. "opat", /* generate pattern on output */
  110. "print", /* report type */
  111. "rand", /* do randoms over the specified size */
  112. /* must be power of two, not checked */
  113. "poff", /* Print the offsets as we do the io. */
  114. #ifdef RUSAGE
  115. "rusage", /* dump rusage stats */
  116. #endif
  117. "skip", /* skip this number of blocks */
  118. "sync", /* sync output before exit */
  119. "touch", /* touch each buffer after the I/O */
  120. #if !defined(hpux)
  121. "usleep", /* sleep this many usecs between I/O */
  122. #endif
  123. "hash", /* hash marks like FTP */
  124. "append", /* O_APPEND */
  125. "rtmax", /* read latency histogram max in mills */
  126. "wtmax", /* write latency histogram max in mills */
  127. "rtmin", /* read latency histogram max in mills */
  128. "wtmin", /* write latency histogram max in mills */
  129. "realtime", /* create files as XFS realtime files */
  130. "notrunc", /* overwrite rather than truncing out file */
  131. "end", /* limit randoms to this size near the
  132. * Rand endpoints. */
  133. "start", /* Add this to Rand */
  134. "time", /* Run for this many seconds only. */
  135. "srand", /* Seed the random number generator */
  136. "padin", /* Pad an extra untimed block_size read */
  137. #ifdef USE_BDS
  138. "awrite", /* use async writes and pipeline them. */
  139. #endif
  140. "norepeat", /* don't ever do the same I/O twice */
  141. #ifdef sgi
  142. "mpin", /* pin the buffer */
  143. #endif
  144. "timeopen", /* include open time in results */
  145. "nocreate", /* just open for writing, don't create/trunc it */
  146. #ifdef O_SYNC
  147. "osync", /* O_SYNC */
  148. #endif
  149. 0,
  150. };
  151. void error(char *);
  152. void done();
  153. #ifdef DBG
  154. extern int dbg;
  155. #endif
  156. int
  157. main(int ac, char **av)
  158. {
  159. uint *buf;
  160. uint *bufs[10];
  161. int nbufs, nextbuf = 0;
  162. int Fork, misses, mismatch, outpat, inpat, in, timeopen, gotcnt;
  163. int slp;
  164. uint64 skip, size, count;
  165. void chkarg();
  166. int i;
  167. uint64 off = 0;
  168. int touch;
  169. int time;
  170. int mills;
  171. int pad_in;
  172. int pid = 0;
  173. struct timeval start_tv;
  174. struct timeval stop_tv;
  175. if (sizeof(int) != 4) {
  176. fprintf(stderr, "sizeof(int) != 4\n");
  177. exit(1);
  178. }
  179. for (i = 1; i < ac; ++i) {
  180. chkarg(av[i]);
  181. }
  182. signal(SIGINT, done);
  183. signal(SIGALRM, done);
  184. misses = mismatch = getarg("mismatch=", ac, av);
  185. inpat = getarg("ipat=", ac, av);
  186. outpat = getarg("opat=", ac, av);
  187. Bsize = getarg("bs=", ac, av);
  188. if (Bsize < 0)
  189. Bsize = 8192;
  190. #if !defined(hpux)
  191. slp = getarg("usleep=", ac, av);
  192. #endif
  193. Fork = getarg("fork=", ac, av);
  194. Fsync = getarg("fsync=", ac, av);
  195. Sync = getarg("sync=", ac, av);
  196. Rand = getarg("rand=", ac, av);
  197. Start = getarg("start=", ac, av);
  198. End = getarg("end=", ac, av);
  199. time = getarg("time=", ac, av);
  200. if ((End != -1) && (Rand != -1) && (End > Rand)) {
  201. End = Rand;
  202. }
  203. if (getarg("srand=", ac, av) != -1) {
  204. srand48((long)getarg("srand=", ac, av));
  205. }
  206. poff = getarg("poff=", ac, av) != -1;
  207. Print = getarg("print=", ac, av);
  208. nbufs = getarg("bufs=", ac, av);
  209. Realtime = getarg("realtime=", ac, av);
  210. Rtmax = getarg("rtmax=", ac, av);
  211. if ((Rtmax != -1) && (Rtmax < 10))
  212. Rtmax = 10;
  213. Rtmin = getarg("rtmin=", ac, av);
  214. if ((Rtmax != -1) && (Rtmin == -1)) {
  215. Rtmin = 0;
  216. }
  217. Wtmax = getarg("wtmax=", ac, av);
  218. if ((Wtmax != -1) && (Wtmax < 10))
  219. Wtmax = 10;
  220. Wtmin = getarg("wtmin=", ac, av);
  221. if ((Wtmax != -1) && (Wtmin == -1)) {
  222. Wtmin = 0;
  223. }
  224. if ((Rtmin && !Rtmax) || (Wtmin && !Wtmax)) {
  225. fprintf(stderr, "Need a max to go with that min.\n");
  226. exit(1);
  227. }
  228. if ((Rtmin > Rtmax) || (Wtmin > Wtmax)) {
  229. fprintf(stderr,
  230. "min has to be less than max, R=%d,%d W=%d,%d\n",
  231. Rtmax, Rtmin, Wtmax, Wtmin);
  232. exit(1);
  233. }
  234. timeopen = getarg("timeopen=", ac, av);
  235. pad_in = getarg("padin=", ac, av);
  236. if (pad_in == -1) pad_in = 0;
  237. if (nbufs == -1) nbufs = 1;
  238. if (nbufs > 10) { printf("Too many bufs\n"); exit(1); }
  239. #ifdef DBG
  240. dbg = getarg("debug=", ac, av) != -1;
  241. #endif
  242. #ifdef RUSAGE
  243. ru = getarg("rusage=", ac, av);
  244. #endif
  245. touch = getarg("touch=", ac, av) != -1;
  246. hash = getarg("hash=", ac, av) != (uint64)-1;
  247. Label = (char *)getarg("label=", ac, av);
  248. count = getarg("count=", ac, av);
  249. size = getarg("move=", ac, av);
  250. if (size != (uint64)-1)
  251. count = size / Bsize;
  252. if (Rand != -1) {
  253. size = Rand - Bsize;
  254. size = ALIGN(size, Bsize);
  255. }
  256. #ifdef FLUSH
  257. Flush = getarg("flush=", ac, av);
  258. #endif
  259. if (count == (uint64)-1)
  260. gotcnt = 0;
  261. else
  262. gotcnt = 1;
  263. int_count = 0;
  264. skip = getarg("skip=", ac, av);
  265. if (getarg("norepeat=", ac, av) != -1) {
  266. if (gotcnt) {
  267. norepeat = (uint64*)calloc(count, sizeof(uint64));
  268. } else {
  269. norepeat = (uint64*)calloc(10<<10, sizeof(uint64));
  270. }
  271. }
  272. if ((inpat != -1 || outpat != -1) && (Bsize & 3)) {
  273. fprintf(stderr, "Block size 0x%x must be word aligned\n", Bsize);
  274. exit(1);
  275. }
  276. if ((Bsize >> 2) == 0) {
  277. fprintf(stderr, "Block size must be at least 4.\n");
  278. exit(1);
  279. }
  280. for (i = 0; i < nbufs; i++) {
  281. if (!(bufs[i] = (uint *) VALLOC((unsigned) Bsize))) {
  282. perror("VALLOC");
  283. exit(1);
  284. }
  285. bzero((char *) bufs[i], Bsize);
  286. #ifdef sgi
  287. if (getarg("mpin=", ac, av) != -1) {
  288. if (mpin((void *)bufs[i], (size_t)Bsize)) {
  289. perror("mpin for adam");
  290. }
  291. }
  292. #endif
  293. }
  294. if (time != -1) {
  295. alarm(time);
  296. }
  297. if (timeopen != -1) {
  298. start(NULL);
  299. }
  300. in = getfile("if=", ac, av);
  301. out = getfile("of=", ac, av);
  302. if (timeopen == -1) {
  303. start(NULL);
  304. }
  305. if ((Rtmax != -1) && in < 0) {
  306. fprintf(stderr, "I think you wanted wtmax, not rtmax\n");
  307. exit(1);
  308. }
  309. if ((Wtmax != -1) && out < 0) {
  310. fprintf(stderr, "I think you wanted rtmax, not wtmax\n");
  311. exit(1);
  312. }
  313. if (skip != (uint64)-1) {
  314. off = skip;
  315. off *= Bsize;
  316. if (in >= 0) {
  317. LSEEK(in, off, 0);
  318. }
  319. if (out >= 0) {
  320. LSEEK(out, off, 0);
  321. }
  322. if (poff) {
  323. fprintf(stderr, "%s ", p64sz(off));
  324. }
  325. }
  326. for (;;) {
  327. register int moved;
  328. if (gotcnt && count-- <= 0) {
  329. done();
  330. }
  331. /*
  332. * If End is set, it means alternate back and forth
  333. * between the end points of Rand, doing randoms within
  334. * the area 0..End and Rand-End..Rand
  335. */
  336. if (End != -1) {
  337. static uint64 start = 0;
  338. start = start ? 0 : Rand - End;
  339. do {
  340. off = drand48() * End;
  341. off = ALIGN(off, Bsize);
  342. off += start;
  343. if (Start != -1) {
  344. off += Start;
  345. }
  346. } while (norepeat && been_there(off));
  347. if (norepeat) {
  348. norepeat[norepeats++] = off;
  349. if (!gotcnt && (norepeats == 10<<10)) {
  350. norepeats = 0;
  351. }
  352. }
  353. if (in >= 0) {
  354. LSEEK(in, off, 0);
  355. }
  356. if (out >= 0) {
  357. LSEEK(out, off, 0);
  358. }
  359. }
  360. /*
  361. * Set the seek pointer if doing randoms
  362. */
  363. else if (Rand != -1) {
  364. do {
  365. off = drand48() * (size - Bsize);
  366. if (Start != -1) {
  367. off += Start;
  368. }
  369. off = ALIGN(off, Bsize);
  370. } while (norepeat && been_there(off));
  371. if (norepeat) {
  372. norepeat[norepeats++] = off;
  373. }
  374. if (!gotcnt && (norepeats == 10<<10)) {
  375. norepeats = 0;
  376. }
  377. if (in >= 0) {
  378. LSEEK(in, off, 0);
  379. }
  380. if (out >= 0) {
  381. LSEEK(out, off, 0);
  382. }
  383. }
  384. if (poff) {
  385. fprintf(stderr, "%s ", p64sz(off));
  386. }
  387. buf = bufs[nextbuf];
  388. if (++nextbuf == nbufs) nextbuf = 0;
  389. if (in >= 0) {
  390. if ((Rtmax != -1) || (Rtmin != -1)) {
  391. start(&start_tv);
  392. }
  393. moved = read(in, buf, Bsize);
  394. if (pad_in) { /* ignore this run, restart clock */
  395. pad_in = 0;
  396. count++;
  397. start(NULL);
  398. continue;
  399. }
  400. if ((Rtmax != -1) || (Rtmin != -1)) {
  401. int mics = stop(&start_tv, &stop_tv);
  402. mills = mics / 1000;
  403. if ((mills > Rtmax) || (mills < Rtmin)) {
  404. fprintf(stderr,
  405. "READ: %.02f milliseconds offset %s\n",
  406. ((float)mics) / 1000,
  407. p64sz(LSEEK(in, 0, SEEK_CUR)));
  408. }
  409. /*
  410. * Put this read time in the histogram.
  411. * The buckets are each 1/10th of Rtmax.
  412. */
  413. if (mills >= Rtmax) {
  414. rthist[11]++;
  415. } else if (mills < Rtmin) {
  416. rthist[0]++;
  417. } else {
  418. int step = (Rtmax - Rtmin) / 10;
  419. int i;
  420. for (i = 1; i <= 10; ++i) {
  421. if (mills < i * step + Rtmin) {
  422. rthist[i]++;
  423. break;
  424. }
  425. }
  426. }
  427. }
  428. } else {
  429. moved = Bsize;
  430. }
  431. if (moved == -1) {
  432. perror("read");
  433. }
  434. if (moved <= 0) {
  435. done();
  436. }
  437. if (inpat != -1) {
  438. register int foo, cnt;
  439. for (foo = 0, cnt = moved/sizeof(int); cnt--; foo++) {
  440. if (buf[foo] != (uint) (off + foo*sizeof(int))) {
  441. fprintf(stderr,
  442. "off=%u want=%x got=%x\n",
  443. (uint)off,
  444. (uint)(off + foo*sizeof(int)),
  445. buf[foo]);
  446. if (mismatch != -1 && --misses == 0) {
  447. done();
  448. }
  449. }
  450. }
  451. }
  452. if ((in >= 0) && touch) {
  453. int i;
  454. for (i = 0; i < moved; i += 4096) {
  455. ((char *)buf)[i] = 0;
  456. }
  457. }
  458. if (out >= 0) {
  459. int moved2;
  460. if (Fork != -1) {
  461. if (pid) {
  462. waitpid(pid, 0, 0);
  463. }
  464. if ((pid = fork())) {
  465. off += moved;
  466. int_count += (moved >> 2);
  467. continue;
  468. }
  469. }
  470. if (outpat != -1) {
  471. register int foo, cnt;
  472. for (foo = 0, cnt = moved/sizeof(int);
  473. cnt--; foo++) {
  474. buf[foo] =
  475. (uint)(off + foo*sizeof(int));
  476. }
  477. }
  478. if ((Wtmax != -1) || (Wtmin != -1)) {
  479. start(&start_tv);
  480. }
  481. #ifdef USE_BDS
  482. /*
  483. * The first time through, m1 & m2 are null.
  484. * The Nth time through, we start the I/O into
  485. * m2, and wait on m1, then switch.
  486. */
  487. if (awrite) {
  488. if (m1) {
  489. m2 = bds_awrite(out, buf, moved);
  490. moved2 = bds_adone(out, m1);
  491. m1 = m2;
  492. } else {
  493. m1 = bds_awrite(out, buf, moved);
  494. goto writedone;
  495. }
  496. } else {
  497. moved2 = write(out, buf, moved);
  498. }
  499. #else
  500. moved2 = write(out, buf, moved);
  501. #endif
  502. if (moved2 == -1) {
  503. perror("write");
  504. }
  505. if (moved2 != moved) {
  506. fprintf(stderr, "write: wanted=%d got=%d\n",
  507. moved, moved2);
  508. done();
  509. }
  510. if ((Wtmax != -1) || (Wtmin != -1)) {
  511. int mics = stop(&start_tv, &stop_tv);
  512. mills = mics / 1000;
  513. if ((mills > Wtmax) || (mills < Wtmin)) {
  514. fprintf(stderr,
  515. "WRITE: %.02f milliseconds offset %s\n",
  516. ((float)mics) / 1000,
  517. p64sz(LSEEK(out, 0, SEEK_CUR)));
  518. }
  519. /*
  520. * Put this write time in the histogram.
  521. * The buckets are each 1/10th of Wtmax.
  522. */
  523. if (mills >= Wtmax) {
  524. wthist[11]++;
  525. } else if (mills < Wtmin) {
  526. wthist[0]++;
  527. } else {
  528. int step = (Wtmax - Wtmin) / 10;
  529. int i;
  530. for (i = 1; i <= 10; ++i) {
  531. if (mills < i * step + Wtmin) {
  532. wthist[i]++;
  533. break;
  534. }
  535. }
  536. }
  537. }
  538. if (moved2 == -1) {
  539. perror("write");
  540. }
  541. if (moved2 != moved) {
  542. done();
  543. }
  544. if (touch) {
  545. int i;
  546. for (i = 0; i < moved; i += 4096) {
  547. ((char *)buf)[i] = 0;
  548. }
  549. }
  550. }
  551. #ifdef USE_BDS
  552. writedone: /* for the first async write */
  553. #endif
  554. off += moved;
  555. int_count += (moved >> 2);
  556. #if !defined(hpux)
  557. if (slp != -1) {
  558. usleep(slp);
  559. }
  560. #endif
  561. if (hash) {
  562. fprintf(stderr, "#");
  563. }
  564. if (Fork != -1) {
  565. exit(0);
  566. }
  567. }
  568. }
  569. int
  570. been_there(uint64 off)
  571. {
  572. register int i;
  573. for (i = 0; i <= norepeats; ++i) {
  574. if (off == norepeat[i]) {
  575. fprintf(stderr, "norepeat on %u\n", (uint)off);
  576. return (1);
  577. }
  578. }
  579. return (0);
  580. }
  581. void
  582. chkarg(char *arg)
  583. {
  584. int i;
  585. char *a, *b;
  586. for (i = 0; cmds[i]; ++i) {
  587. for (a = arg, b = cmds[i]; *a && *b && *a == *b; a++, b++)
  588. ;
  589. if (*a == '=')
  590. return;
  591. }
  592. fprintf(stderr, "Bad arg: %s, possible arguments are: ", arg);
  593. for (i = 0; cmds[i]; ++i) {
  594. fprintf(stderr, "%s ", cmds[i]);
  595. }
  596. fprintf(stderr, "\n");
  597. exit(1);
  598. /*NOTREACHED*/
  599. }
  600. void
  601. done(void)
  602. {
  603. int i;
  604. int step;
  605. int size;
  606. #ifdef USE_BDS
  607. if (awrite && m1) {
  608. bds_adone(out, m1);
  609. }
  610. #endif
  611. if (Sync > 0)
  612. sync();
  613. if (Fsync > 0)
  614. fsync(out);
  615. #ifdef FLUSH
  616. if (Flush > 0)
  617. flush();
  618. #endif
  619. stop(NULL, NULL);
  620. #ifdef RUSAGE
  621. if (ru != -1)
  622. rusage();
  623. #endif
  624. if (hash || poff) {
  625. fprintf(stderr, "\n");
  626. }
  627. if ((long)Label != -1) {
  628. fprintf(stderr, "%s", Label);
  629. }
  630. int_count <<= 2;
  631. switch (Print) {
  632. case 0: /* no print out */
  633. break;
  634. case 1: /* latency type print out */
  635. latency((uint64)(int_count / Bsize), (uint64)Bsize);
  636. break;
  637. case 2: /* microsecond per op print out */
  638. micro("", (uint64)(int_count / Bsize));
  639. break;
  640. case 3: /* kb / sec print out */
  641. kb(int_count);
  642. break;
  643. case 4: /* mb / sec print out */
  644. mb(int_count);
  645. break;
  646. case 5: /* Xgraph output */
  647. bandwidth(int_count, 1, 0);
  648. break;
  649. default: /* bandwidth print out */
  650. bandwidth(int_count, 1, 1);
  651. break;
  652. }
  653. if (Rtmax != -1) {
  654. printf("READ operation latencies\n");
  655. step = (Rtmax - Rtmin) / 10;
  656. if (rthist[0]) {
  657. printf("%d- ms: %d\n", Rtmin, rthist[0]);
  658. }
  659. for (i = 1, size = Rtmin; i <= 10; i++, size += step) {
  660. if (!rthist[i])
  661. continue;
  662. printf("%d to %d ms: %d\n",
  663. size, size + step - 1, rthist[i]);
  664. }
  665. if (rthist[11]) {
  666. printf("%d+ ms: %d\n", Rtmax, rthist[11]);
  667. }
  668. }
  669. if (Wtmax != -1) {
  670. printf("WRITE operation latencies\n");
  671. step = (Wtmax - Wtmin) / 10;
  672. if (wthist[0]) {
  673. printf("%d- ms: %d\n", Wtmin, wthist[0]);
  674. }
  675. for (i = 1, size = Wtmin; i <= 10; i++, size += step) {
  676. if (!wthist[i])
  677. continue;
  678. printf("%d to %d ms: %d\n",
  679. size, size + step - 1, wthist[i]);
  680. }
  681. if (wthist[11]) {
  682. printf("%d+ ms: %d\n", Wtmax, wthist[11]);
  683. }
  684. }
  685. exit(0);
  686. }
  687. uint64
  688. getarg(char *s, int ac, char **av)
  689. {
  690. register uint64 len, i;
  691. len = strlen(s);
  692. for (i = 1; i < ac; ++i) {
  693. if (!strncmp(av[i], s, len)) {
  694. register uint64 bs = ATOL(&av[i][len]);
  695. switch (av[i][strlen(av[i]) - 1]) {
  696. case 'K': bs *= 1000; break;
  697. case 'k': bs <<= 10; break;
  698. case 'M': bs *= 1000000; break;
  699. case 'm': bs <<= 20; break;
  700. case 'G': bs *= 1000000000L; break;
  701. case 'g': bs <<= 30; break;
  702. }
  703. if (!strncmp(av[i], "label", 5)) {
  704. return (uint64)(&av[i][len]); /* HACK */
  705. }
  706. if (!strncmp(av[i], "bs=", 3)) {
  707. return (uint64)(bs);
  708. }
  709. return (bs);
  710. }
  711. }
  712. return ((uint64)-1);
  713. }
  714. char *output;
  715. int
  716. getfile(char *s, int ac, char **av)
  717. {
  718. register int ret, len, i;
  719. int append = getarg("append=", ac, av) != -1;
  720. int notrunc = getarg("notrunc=", ac, av) != -1;
  721. int nocreate = getarg("nocreate=", ac, av) != -1;
  722. #ifdef O_SYNC
  723. int osync = getarg("osync=", ac, av) != -1;
  724. #endif
  725. int oflags;
  726. len = strlen(s);
  727. for (i = 1; i < ac; ++i) {
  728. if (!strncmp(av[i], s, len)) {
  729. if (av[i][0] == 'o') {
  730. if (!strcmp("of=internal", av[i]))
  731. return (-2);
  732. if (!strcmp("of=stdout", av[i]))
  733. return (1);
  734. if (!strcmp("of=1", av[i]))
  735. return (1);
  736. if (!strcmp("of=-", av[i]))
  737. return (1);
  738. if (!strcmp("of=stderr", av[i]))
  739. return (2);
  740. if (!strcmp("of=2", av[i]))
  741. return (2);
  742. oflags = O_WRONLY;
  743. oflags |= (notrunc || append) ? 0 : O_TRUNC;
  744. oflags |= nocreate ? 0 : O_CREAT;
  745. oflags |= append ? O_APPEND : 0;
  746. #ifdef O_SYNC
  747. oflags |= osync ? O_SYNC : 0;
  748. #endif
  749. ret = open(&av[i][len], oflags,0644);
  750. #ifdef O_DIRECT
  751. if ((getarg("odirect=", ac, av) != -1) ||
  752. (getarg("direct=", ac, av) != -1)) {
  753. close(ret);
  754. ret = open(&av[i][len], oflags|O_DIRECT);
  755. awrite =
  756. getarg("awrite=", ac, av) != -1;
  757. }
  758. #endif
  759. if (ret == -1)
  760. error(&av[i][len]);
  761. #ifdef F_FSSETXATTR
  762. if (Realtime == 1) {
  763. struct fsxattr fsxattr;
  764. bzero(&fsxattr,sizeof(struct fsxattr));
  765. fsxattr.fsx_xflags = 0x1;
  766. if (fcntl(ret,F_FSSETXATTR,&fsxattr)){
  767. printf("WARNING: Could not make %s a real time file\n",
  768. &av[i][len]);
  769. }
  770. }
  771. #endif
  772. output = &av[i][len];
  773. return (ret);
  774. } else {
  775. if (!strcmp("if=internal", av[i]))
  776. return (-2);
  777. if (!strcmp("if=stdin", av[i]))
  778. return (0);
  779. if (!strcmp("if=0", av[i]))
  780. return (0);
  781. if (!strcmp("if=-", av[i]))
  782. return (0);
  783. ret = open(&av[i][len], 0);
  784. #ifdef O_DIRECT
  785. if ((getarg("idirect=", ac, av) != -1) ||
  786. (getarg("direct=", ac, av) != -1)) {
  787. close(ret);
  788. ret = open(&av[i][len], O_RDONLY|O_DIRECT);
  789. }
  790. #endif
  791. if (ret == -1)
  792. error(&av[i][len]);
  793. return (ret);
  794. }
  795. }
  796. }
  797. return (-2);
  798. }
  799. #ifdef FLUSH
  800. int
  801. warning(char *s)
  802. {
  803. if ((long)Label != -1) {
  804. fprintf(stderr, "%s: ", Label);
  805. }
  806. perror(s);
  807. return (-1);
  808. }
  809. void
  810. flush(void)
  811. {
  812. int fd;
  813. struct stat sb;
  814. caddr_t where;
  815. if (output == NULL || (fd = open(output, 2)) == -1) {
  816. warning("No output file");
  817. return;
  818. }
  819. if (fstat(fd, &sb) == -1 || sb.st_size == 0) {
  820. warning(output);
  821. return;
  822. }
  823. where = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  824. msync(where, sb.st_size, MS_INVALIDATE);
  825. munmap(where, sb.st_size);
  826. }
  827. #endif
  828. void
  829. error(char *s)
  830. {
  831. if ((long)Label != -1) {
  832. fprintf(stderr, "%s: ", Label);
  833. }
  834. perror(s);
  835. exit(1);
  836. }