bench_runner.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  1. #ifndef _POSIX_C_SOURCE
  2. #define _POSIX_C_SOURCE 199309L
  3. #endif
  4. #include "runners/bench_runner.h"
  5. #include "bd/lfs_emubd.h"
  6. #include <getopt.h>
  7. #include <sys/types.h>
  8. #include <errno.h>
  9. #include <setjmp.h>
  10. #include <fcntl.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. // some helpers
  15. // append to an array with amortized doubling
  16. void *mappend(void **p,
  17. size_t size,
  18. size_t *count,
  19. size_t *capacity) {
  20. uint8_t *p_ = *p;
  21. size_t count_ = *count;
  22. size_t capacity_ = *capacity;
  23. count_ += 1;
  24. if (count_ > capacity_) {
  25. capacity_ = (2*capacity_ < 4) ? 4 : 2*capacity_;
  26. p_ = realloc(p_, capacity_*size);
  27. if (!p_) {
  28. return NULL;
  29. }
  30. }
  31. *p = p_;
  32. *count = count_;
  33. *capacity = capacity_;
  34. return &p_[(count_-1)*size];
  35. }
  36. // a quick self-terminating text-safe varint scheme
  37. static void leb16_print(uintmax_t x) {
  38. while (true) {
  39. char nibble = (x & 0xf) | (x > 0xf ? 0x10 : 0);
  40. printf("%c", (nibble < 10) ? '0'+nibble : 'a'+nibble-10);
  41. if (x <= 0xf) {
  42. break;
  43. }
  44. x >>= 4;
  45. }
  46. }
  47. static uintmax_t leb16_parse(const char *s, char **tail) {
  48. uintmax_t x = 0;
  49. size_t i = 0;
  50. while (true) {
  51. uintmax_t nibble = s[i];
  52. if (nibble >= '0' && nibble <= '9') {
  53. nibble = nibble - '0';
  54. } else if (nibble >= 'a' && nibble <= 'v') {
  55. nibble = nibble - 'a' + 10;
  56. } else {
  57. // invalid?
  58. if (tail) {
  59. *tail = (char*)s;
  60. }
  61. return 0;
  62. }
  63. x |= (nibble & 0xf) << (4*i);
  64. i += 1;
  65. if (!(nibble & 0x10)) {
  66. break;
  67. }
  68. }
  69. if (tail) {
  70. *tail = (char*)s + i;
  71. }
  72. return x;
  73. }
  74. // bench_runner types
  75. typedef struct bench_geometry {
  76. char short_name;
  77. const char *long_name;
  78. bench_define_t defines[BENCH_GEOMETRY_DEFINE_COUNT];
  79. } bench_geometry_t;
  80. typedef struct bench_id {
  81. const char *name;
  82. const bench_define_t *defines;
  83. size_t define_count;
  84. } bench_id_t;
  85. // bench suites are linked into a custom ld section
  86. extern struct bench_suite __start__bench_suites;
  87. extern struct bench_suite __stop__bench_suites;
  88. const struct bench_suite *bench_suites = &__start__bench_suites;
  89. #define BENCH_SUITE_COUNT \
  90. ((size_t)(&__stop__bench_suites - &__start__bench_suites))
  91. // bench define management
  92. typedef struct bench_define_map {
  93. const bench_define_t *defines;
  94. size_t count;
  95. } bench_define_map_t;
  96. typedef struct bench_define_names {
  97. const char *const *names;
  98. size_t count;
  99. } bench_define_names_t;
  100. intmax_t bench_define_lit(void *data) {
  101. return (intmax_t)data;
  102. }
  103. #define BENCH_CONST(x) {bench_define_lit, (void*)(uintptr_t)(x)}
  104. #define BENCH_LIT(x) ((bench_define_t)BENCH_CONST(x))
  105. #define BENCH_DEF(k, v) \
  106. intmax_t bench_define_##k(void *data) { \
  107. (void)data; \
  108. return v; \
  109. }
  110. BENCH_IMPLICIT_DEFINES
  111. #undef BENCH_DEF
  112. #define BENCH_DEFINE_MAP_EXPLICIT 0
  113. #define BENCH_DEFINE_MAP_OVERRIDE 1
  114. #define BENCH_DEFINE_MAP_PERMUTATION 2
  115. #define BENCH_DEFINE_MAP_GEOMETRY 3
  116. #define BENCH_DEFINE_MAP_IMPLICIT 4
  117. #define BENCH_DEFINE_MAP_COUNT 5
  118. bench_define_map_t bench_define_maps[BENCH_DEFINE_MAP_COUNT] = {
  119. [BENCH_DEFINE_MAP_IMPLICIT] = {
  120. (const bench_define_t[BENCH_IMPLICIT_DEFINE_COUNT]) {
  121. #define BENCH_DEF(k, v) \
  122. [k##_i] = {bench_define_##k, NULL},
  123. BENCH_IMPLICIT_DEFINES
  124. #undef BENCH_DEF
  125. },
  126. BENCH_IMPLICIT_DEFINE_COUNT,
  127. },
  128. };
  129. #define BENCH_DEFINE_NAMES_SUITE 0
  130. #define BENCH_DEFINE_NAMES_IMPLICIT 1
  131. #define BENCH_DEFINE_NAMES_COUNT 2
  132. bench_define_names_t bench_define_names[BENCH_DEFINE_NAMES_COUNT] = {
  133. [BENCH_DEFINE_NAMES_IMPLICIT] = {
  134. (const char *const[BENCH_IMPLICIT_DEFINE_COUNT]){
  135. #define BENCH_DEF(k, v) \
  136. [k##_i] = #k,
  137. BENCH_IMPLICIT_DEFINES
  138. #undef BENCH_DEF
  139. },
  140. BENCH_IMPLICIT_DEFINE_COUNT,
  141. },
  142. };
  143. intmax_t *bench_define_cache;
  144. size_t bench_define_cache_count;
  145. unsigned *bench_define_cache_mask;
  146. const char *bench_define_name(size_t define) {
  147. // lookup in our bench names
  148. for (size_t i = 0; i < BENCH_DEFINE_NAMES_COUNT; i++) {
  149. if (define < bench_define_names[i].count
  150. && bench_define_names[i].names
  151. && bench_define_names[i].names[define]) {
  152. return bench_define_names[i].names[define];
  153. }
  154. }
  155. return NULL;
  156. }
  157. bool bench_define_ispermutation(size_t define) {
  158. // is this define specific to the permutation?
  159. for (size_t i = 0; i < BENCH_DEFINE_MAP_IMPLICIT; i++) {
  160. if (define < bench_define_maps[i].count
  161. && bench_define_maps[i].defines[define].cb) {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. intmax_t bench_define(size_t define) {
  168. // is the define in our cache?
  169. if (define < bench_define_cache_count
  170. && (bench_define_cache_mask[define/(8*sizeof(unsigned))]
  171. & (1 << (define%(8*sizeof(unsigned)))))) {
  172. return bench_define_cache[define];
  173. }
  174. // lookup in our bench defines
  175. for (size_t i = 0; i < BENCH_DEFINE_MAP_COUNT; i++) {
  176. if (define < bench_define_maps[i].count
  177. && bench_define_maps[i].defines[define].cb) {
  178. intmax_t v = bench_define_maps[i].defines[define].cb(
  179. bench_define_maps[i].defines[define].data);
  180. // insert into cache!
  181. bench_define_cache[define] = v;
  182. bench_define_cache_mask[define / (8*sizeof(unsigned))]
  183. |= 1 << (define%(8*sizeof(unsigned)));
  184. return v;
  185. }
  186. }
  187. return 0;
  188. // not found?
  189. const char *name = bench_define_name(define);
  190. fprintf(stderr, "error: undefined define %s (%zd)\n",
  191. name ? name : "(unknown)",
  192. define);
  193. assert(false);
  194. exit(-1);
  195. }
  196. void bench_define_flush(void) {
  197. // clear cache between permutations
  198. memset(bench_define_cache_mask, 0,
  199. sizeof(unsigned)*(
  200. (bench_define_cache_count+(8*sizeof(unsigned))-1)
  201. / (8*sizeof(unsigned))));
  202. }
  203. // geometry updates
  204. const bench_geometry_t *bench_geometry = NULL;
  205. void bench_define_geometry(const bench_geometry_t *geometry) {
  206. bench_define_maps[BENCH_DEFINE_MAP_GEOMETRY] = (bench_define_map_t){
  207. geometry->defines, BENCH_GEOMETRY_DEFINE_COUNT};
  208. }
  209. // override updates
  210. typedef struct bench_override {
  211. const char *name;
  212. const intmax_t *defines;
  213. size_t permutations;
  214. } bench_override_t;
  215. const bench_override_t *bench_overrides = NULL;
  216. size_t bench_override_count = 0;
  217. bench_define_t *bench_override_defines = NULL;
  218. size_t bench_override_define_count = 0;
  219. size_t bench_override_define_permutations = 1;
  220. size_t bench_override_define_capacity = 0;
  221. // suite/perm updates
  222. void bench_define_suite(const struct bench_suite *suite) {
  223. bench_define_names[BENCH_DEFINE_NAMES_SUITE] = (bench_define_names_t){
  224. suite->define_names, suite->define_count};
  225. // make sure our cache is large enough
  226. if (lfs_max(suite->define_count, BENCH_IMPLICIT_DEFINE_COUNT)
  227. > bench_define_cache_count) {
  228. // align to power of two to avoid any superlinear growth
  229. size_t ncount = 1 << lfs_npw2(
  230. lfs_max(suite->define_count, BENCH_IMPLICIT_DEFINE_COUNT));
  231. bench_define_cache = realloc(bench_define_cache, ncount*sizeof(intmax_t));
  232. bench_define_cache_mask = realloc(bench_define_cache_mask,
  233. sizeof(unsigned)*(
  234. (ncount+(8*sizeof(unsigned))-1)
  235. / (8*sizeof(unsigned))));
  236. bench_define_cache_count = ncount;
  237. }
  238. // map any overrides
  239. if (bench_override_count > 0) {
  240. // first figure out the total size of override permutations
  241. size_t count = 0;
  242. size_t permutations = 1;
  243. for (size_t i = 0; i < bench_override_count; i++) {
  244. for (size_t d = 0;
  245. d < lfs_max(
  246. suite->define_count,
  247. BENCH_IMPLICIT_DEFINE_COUNT);
  248. d++) {
  249. // define name match?
  250. const char *name = bench_define_name(d);
  251. if (name && strcmp(name, bench_overrides[i].name) == 0) {
  252. count = lfs_max(count, d+1);
  253. permutations *= bench_overrides[i].permutations;
  254. break;
  255. }
  256. }
  257. }
  258. bench_override_define_count = count;
  259. bench_override_define_permutations = permutations;
  260. // make sure our override arrays are big enough
  261. if (count * permutations > bench_override_define_capacity) {
  262. // align to power of two to avoid any superlinear growth
  263. size_t ncapacity = 1 << lfs_npw2(count * permutations);
  264. bench_override_defines = realloc(
  265. bench_override_defines,
  266. sizeof(bench_define_t)*ncapacity);
  267. bench_override_define_capacity = ncapacity;
  268. }
  269. // zero unoverridden defines
  270. memset(bench_override_defines, 0,
  271. sizeof(bench_define_t) * count * permutations);
  272. // compute permutations
  273. size_t p = 1;
  274. for (size_t i = 0; i < bench_override_count; i++) {
  275. for (size_t d = 0;
  276. d < lfs_max(
  277. suite->define_count,
  278. BENCH_IMPLICIT_DEFINE_COUNT);
  279. d++) {
  280. // define name match?
  281. const char *name = bench_define_name(d);
  282. if (name && strcmp(name, bench_overrides[i].name) == 0) {
  283. // scatter the define permutations based on already
  284. // seen permutations
  285. for (size_t j = 0; j < permutations; j++) {
  286. bench_override_defines[j*count + d] = BENCH_LIT(
  287. bench_overrides[i].defines[(j/p)
  288. % bench_overrides[i].permutations]);
  289. }
  290. // keep track of how many permutations we've seen so far
  291. p *= bench_overrides[i].permutations;
  292. break;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. void bench_define_perm(
  299. const struct bench_suite *suite,
  300. const struct bench_case *case_,
  301. size_t perm) {
  302. if (case_->defines) {
  303. bench_define_maps[BENCH_DEFINE_MAP_PERMUTATION] = (bench_define_map_t){
  304. case_->defines + perm*suite->define_count,
  305. suite->define_count};
  306. } else {
  307. bench_define_maps[BENCH_DEFINE_MAP_PERMUTATION] = (bench_define_map_t){
  308. NULL, 0};
  309. }
  310. }
  311. void bench_define_override(size_t perm) {
  312. bench_define_maps[BENCH_DEFINE_MAP_OVERRIDE] = (bench_define_map_t){
  313. bench_override_defines + perm*bench_override_define_count,
  314. bench_override_define_count};
  315. }
  316. void bench_define_explicit(
  317. const bench_define_t *defines,
  318. size_t define_count) {
  319. bench_define_maps[BENCH_DEFINE_MAP_EXPLICIT] = (bench_define_map_t){
  320. defines, define_count};
  321. }
  322. void bench_define_cleanup(void) {
  323. // bench define management can allocate a few things
  324. free(bench_define_cache);
  325. free(bench_define_cache_mask);
  326. free(bench_override_defines);
  327. }
  328. // bench state
  329. extern const bench_geometry_t *bench_geometries;
  330. extern size_t bench_geometry_count;
  331. const bench_id_t *bench_ids = (const bench_id_t[]) {
  332. {NULL, NULL, 0},
  333. };
  334. size_t bench_id_count = 1;
  335. size_t bench_step_start = 0;
  336. size_t bench_step_stop = -1;
  337. size_t bench_step_step = 1;
  338. const char *bench_disk_path = NULL;
  339. const char *bench_trace_path = NULL;
  340. FILE *bench_trace_file = NULL;
  341. uint32_t bench_trace_cycles = 0;
  342. lfs_emubd_sleep_t bench_read_sleep = 0.0;
  343. lfs_emubd_sleep_t bench_prog_sleep = 0.0;
  344. lfs_emubd_sleep_t bench_erase_sleep = 0.0;
  345. // trace printing
  346. void bench_trace(const char *fmt, ...) {
  347. if (bench_trace_path) {
  348. if (!bench_trace_file) {
  349. // Tracing output is heavy and trying to open every trace
  350. // call is slow, so we only try to open the trace file every
  351. // so often. Note this doesn't affect successfully opened files
  352. if (bench_trace_cycles % 128 != 0) {
  353. bench_trace_cycles += 1;
  354. return;
  355. }
  356. bench_trace_cycles += 1;
  357. int fd;
  358. if (strcmp(bench_trace_path, "-") == 0) {
  359. fd = dup(1);
  360. if (fd < 0) {
  361. return;
  362. }
  363. } else {
  364. fd = open(
  365. bench_trace_path,
  366. O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK,
  367. 0666);
  368. if (fd < 0) {
  369. return;
  370. }
  371. int err = fcntl(fd, F_SETFL, O_WRONLY | O_CREAT | O_APPEND);
  372. assert(!err);
  373. }
  374. FILE *f = fdopen(fd, "a");
  375. assert(f);
  376. int err = setvbuf(f, NULL, _IOLBF, BUFSIZ);
  377. assert(!err);
  378. bench_trace_file = f;
  379. }
  380. va_list va;
  381. va_start(va, fmt);
  382. int res = vfprintf(bench_trace_file, fmt, va);
  383. if (res < 0) {
  384. fclose(bench_trace_file);
  385. bench_trace_file = NULL;
  386. }
  387. va_end(va);
  388. }
  389. }
  390. // bench recording state
  391. static struct lfs_config *bench_cfg = NULL;
  392. static lfs_emubd_io_t bench_last_read = 0;
  393. static lfs_emubd_io_t bench_last_prog = 0;
  394. static lfs_emubd_io_t bench_last_erased = 0;
  395. lfs_emubd_io_t bench_read = 0;
  396. lfs_emubd_io_t bench_prog = 0;
  397. lfs_emubd_io_t bench_erased = 0;
  398. void bench_reset(void) {
  399. bench_read = 0;
  400. bench_prog = 0;
  401. bench_erased = 0;
  402. bench_last_read = 0;
  403. bench_last_prog = 0;
  404. bench_last_erased = 0;
  405. }
  406. void bench_start(void) {
  407. assert(bench_cfg);
  408. lfs_emubd_sio_t read = lfs_emubd_getread(bench_cfg);
  409. assert(read >= 0);
  410. lfs_emubd_sio_t prog = lfs_emubd_getprog(bench_cfg);
  411. assert(prog >= 0);
  412. lfs_emubd_sio_t erased = lfs_emubd_geterased(bench_cfg);
  413. assert(erased >= 0);
  414. bench_last_read = read;
  415. bench_last_prog = prog;
  416. bench_last_erased = erased;
  417. }
  418. void bench_stop(void) {
  419. assert(bench_cfg);
  420. lfs_emubd_sio_t read = lfs_emubd_getread(bench_cfg);
  421. assert(read >= 0);
  422. lfs_emubd_sio_t prog = lfs_emubd_getprog(bench_cfg);
  423. assert(prog >= 0);
  424. lfs_emubd_sio_t erased = lfs_emubd_geterased(bench_cfg);
  425. assert(erased >= 0);
  426. bench_read += read - bench_last_read;
  427. bench_prog += prog - bench_last_prog;
  428. bench_erased += erased - bench_last_erased;
  429. }
  430. // encode our permutation into a reusable id
  431. static void perm_printid(
  432. const struct bench_suite *suite,
  433. const struct bench_case *case_) {
  434. (void)suite;
  435. // case[:permutation]
  436. printf("%s:", case_->name);
  437. for (size_t d = 0;
  438. d < lfs_max(
  439. suite->define_count,
  440. BENCH_IMPLICIT_DEFINE_COUNT);
  441. d++) {
  442. if (bench_define_ispermutation(d)) {
  443. leb16_print(d);
  444. leb16_print(BENCH_DEFINE(d));
  445. }
  446. }
  447. }
  448. // a quick trie for keeping track of permutations we've seen
  449. typedef struct bench_seen {
  450. struct bench_seen_branch *branches;
  451. size_t branch_count;
  452. size_t branch_capacity;
  453. } bench_seen_t;
  454. struct bench_seen_branch {
  455. intmax_t define;
  456. struct bench_seen branch;
  457. };
  458. bool bench_seen_insert(
  459. bench_seen_t *seen,
  460. const struct bench_suite *suite,
  461. const struct bench_case *case_) {
  462. (void)case_;
  463. bool was_seen = true;
  464. // use the currently set defines
  465. for (size_t d = 0;
  466. d < lfs_max(
  467. suite->define_count,
  468. BENCH_IMPLICIT_DEFINE_COUNT);
  469. d++) {
  470. // treat unpermuted defines the same as 0
  471. intmax_t define = bench_define_ispermutation(d) ? BENCH_DEFINE(d) : 0;
  472. // already seen?
  473. struct bench_seen_branch *branch = NULL;
  474. for (size_t i = 0; i < seen->branch_count; i++) {
  475. if (seen->branches[i].define == define) {
  476. branch = &seen->branches[i];
  477. break;
  478. }
  479. }
  480. // need to create a new node
  481. if (!branch) {
  482. was_seen = false;
  483. branch = mappend(
  484. (void**)&seen->branches,
  485. sizeof(struct bench_seen_branch),
  486. &seen->branch_count,
  487. &seen->branch_capacity);
  488. branch->define = define;
  489. branch->branch = (bench_seen_t){NULL, 0, 0};
  490. }
  491. seen = &branch->branch;
  492. }
  493. return was_seen;
  494. }
  495. void bench_seen_cleanup(bench_seen_t *seen) {
  496. for (size_t i = 0; i < seen->branch_count; i++) {
  497. bench_seen_cleanup(&seen->branches[i].branch);
  498. }
  499. free(seen->branches);
  500. }
  501. // iterate through permutations in a bench case
  502. static void case_forperm(
  503. const struct bench_suite *suite,
  504. const struct bench_case *case_,
  505. const bench_define_t *defines,
  506. size_t define_count,
  507. void (*cb)(
  508. void *data,
  509. const struct bench_suite *suite,
  510. const struct bench_case *case_),
  511. void *data) {
  512. if (defines) {
  513. bench_define_explicit(defines, define_count);
  514. bench_define_flush();
  515. cb(data, suite, case_);
  516. return;
  517. }
  518. bench_seen_t seen = {NULL, 0, 0};
  519. for (size_t k = 0; k < case_->permutations; k++) {
  520. // define permutation
  521. bench_define_perm(suite, case_, k);
  522. for (size_t v = 0; v < bench_override_define_permutations; v++) {
  523. // define override permutation
  524. bench_define_override(v);
  525. for (size_t g = 0; g < bench_geometry_count; g++) {
  526. // define geometry
  527. bench_define_geometry(&bench_geometries[g]);
  528. bench_define_flush();
  529. // have we seen this permutation before?
  530. bool was_seen = bench_seen_insert(&seen, suite, case_);
  531. if (!(k == 0 && v == 0 && g == 0) && was_seen) {
  532. continue;
  533. }
  534. cb(data, suite, case_);
  535. }
  536. }
  537. }
  538. bench_seen_cleanup(&seen);
  539. }
  540. // how many permutations are there actually in a bench case
  541. struct perm_count_state {
  542. size_t total;
  543. size_t filtered;
  544. };
  545. void perm_count(
  546. void *data,
  547. const struct bench_suite *suite,
  548. const struct bench_case *case_) {
  549. struct perm_count_state *state = data;
  550. (void)suite;
  551. (void)case_;
  552. state->total += 1;
  553. if (case_->filter && !case_->filter()) {
  554. return;
  555. }
  556. state->filtered += 1;
  557. }
  558. // operations we can do
  559. static void summary(void) {
  560. printf("%-36s %7s %7s %7s %11s\n",
  561. "", "flags", "suites", "cases", "perms");
  562. size_t suites = 0;
  563. size_t cases = 0;
  564. bench_flags_t flags = 0;
  565. struct perm_count_state perms = {0, 0};
  566. for (size_t t = 0; t < bench_id_count; t++) {
  567. for (size_t i = 0; i < BENCH_SUITE_COUNT; i++) {
  568. bench_define_suite(&bench_suites[i]);
  569. for (size_t j = 0; j < bench_suites[i].case_count; j++) {
  570. // does neither suite nor case name match?
  571. if (bench_ids[t].name && !(
  572. strcmp(bench_ids[t].name,
  573. bench_suites[i].name) == 0
  574. || strcmp(bench_ids[t].name,
  575. bench_suites[i].cases[j].name) == 0)) {
  576. continue;
  577. }
  578. cases += 1;
  579. case_forperm(
  580. &bench_suites[i],
  581. &bench_suites[i].cases[j],
  582. bench_ids[t].defines,
  583. bench_ids[t].define_count,
  584. perm_count,
  585. &perms);
  586. }
  587. suites += 1;
  588. flags |= bench_suites[i].flags;
  589. }
  590. }
  591. char perm_buf[64];
  592. sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
  593. char flag_buf[64];
  594. sprintf(flag_buf, "%s%s",
  595. (flags & BENCH_REENTRANT) ? "r" : "",
  596. (!flags) ? "-" : "");
  597. printf("%-36s %7s %7zu %7zu %11s\n",
  598. "TOTAL",
  599. flag_buf,
  600. suites,
  601. cases,
  602. perm_buf);
  603. }
  604. static void list_suites(void) {
  605. printf("%-36s %7s %7s %11s\n", "suite", "flags", "cases", "perms");
  606. for (size_t t = 0; t < bench_id_count; t++) {
  607. for (size_t i = 0; i < BENCH_SUITE_COUNT; i++) {
  608. bench_define_suite(&bench_suites[i]);
  609. size_t cases = 0;
  610. struct perm_count_state perms = {0, 0};
  611. for (size_t j = 0; j < bench_suites[i].case_count; j++) {
  612. // does neither suite nor case name match?
  613. if (bench_ids[t].name && !(
  614. strcmp(bench_ids[t].name,
  615. bench_suites[i].name) == 0
  616. || strcmp(bench_ids[t].name,
  617. bench_suites[i].cases[j].name) == 0)) {
  618. continue;
  619. }
  620. cases += 1;
  621. case_forperm(
  622. &bench_suites[i],
  623. &bench_suites[i].cases[j],
  624. bench_ids[t].defines,
  625. bench_ids[t].define_count,
  626. perm_count,
  627. &perms);
  628. }
  629. // no benches found?
  630. if (!cases) {
  631. continue;
  632. }
  633. char perm_buf[64];
  634. sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
  635. char flag_buf[64];
  636. sprintf(flag_buf, "%s%s",
  637. (bench_suites[i].flags & BENCH_REENTRANT) ? "r" : "",
  638. (!bench_suites[i].flags) ? "-" : "");
  639. printf("%-36s %7s %7zu %11s\n",
  640. bench_suites[i].name,
  641. flag_buf,
  642. cases,
  643. perm_buf);
  644. }
  645. }
  646. }
  647. static void list_cases(void) {
  648. printf("%-36s %7s %11s\n", "case", "flags", "perms");
  649. for (size_t t = 0; t < bench_id_count; t++) {
  650. for (size_t i = 0; i < BENCH_SUITE_COUNT; i++) {
  651. bench_define_suite(&bench_suites[i]);
  652. for (size_t j = 0; j < bench_suites[i].case_count; j++) {
  653. // does neither suite nor case name match?
  654. if (bench_ids[t].name && !(
  655. strcmp(bench_ids[t].name,
  656. bench_suites[i].name) == 0
  657. || strcmp(bench_ids[t].name,
  658. bench_suites[i].cases[j].name) == 0)) {
  659. continue;
  660. }
  661. struct perm_count_state perms = {0, 0};
  662. case_forperm(
  663. &bench_suites[i],
  664. &bench_suites[i].cases[j],
  665. bench_ids[t].defines,
  666. bench_ids[t].define_count,
  667. perm_count,
  668. &perms);
  669. char perm_buf[64];
  670. sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
  671. char flag_buf[64];
  672. sprintf(flag_buf, "%s%s",
  673. (bench_suites[i].cases[j].flags & BENCH_REENTRANT)
  674. ? "r" : "",
  675. (!bench_suites[i].cases[j].flags)
  676. ? "-" : "");
  677. printf("%-36s %7s %11s\n",
  678. bench_suites[i].cases[j].name,
  679. flag_buf,
  680. perm_buf);
  681. }
  682. }
  683. }
  684. }
  685. static void list_suite_paths(void) {
  686. printf("%-36s %s\n", "suite", "path");
  687. for (size_t t = 0; t < bench_id_count; t++) {
  688. for (size_t i = 0; i < BENCH_SUITE_COUNT; i++) {
  689. size_t cases = 0;
  690. for (size_t j = 0; j < bench_suites[i].case_count; j++) {
  691. // does neither suite nor case name match?
  692. if (bench_ids[t].name && !(
  693. strcmp(bench_ids[t].name,
  694. bench_suites[i].name) == 0
  695. || strcmp(bench_ids[t].name,
  696. bench_suites[i].cases[j].name) == 0)) {
  697. continue;
  698. }
  699. }
  700. // no benches found?
  701. if (!cases) {
  702. continue;
  703. }
  704. printf("%-36s %s\n",
  705. bench_suites[i].name,
  706. bench_suites[i].path);
  707. }
  708. }
  709. }
  710. static void list_case_paths(void) {
  711. printf("%-36s %s\n", "case", "path");
  712. for (size_t t = 0; t < bench_id_count; t++) {
  713. for (size_t i = 0; i < BENCH_SUITE_COUNT; i++) {
  714. for (size_t j = 0; j < bench_suites[i].case_count; j++) {
  715. // does neither suite nor case name match?
  716. if (bench_ids[t].name && !(
  717. strcmp(bench_ids[t].name,
  718. bench_suites[i].name) == 0
  719. || strcmp(bench_ids[t].name,
  720. bench_suites[i].cases[j].name) == 0)) {
  721. continue;
  722. }
  723. printf("%-36s %s\n",
  724. bench_suites[i].cases[j].name,
  725. bench_suites[i].cases[j].path);
  726. }
  727. }
  728. }
  729. }
  730. struct list_defines_define {
  731. const char *name;
  732. intmax_t *values;
  733. size_t value_count;
  734. size_t value_capacity;
  735. };
  736. struct list_defines_defines {
  737. struct list_defines_define *defines;
  738. size_t define_count;
  739. size_t define_capacity;
  740. };
  741. static void list_defines_add(
  742. struct list_defines_defines *defines,
  743. size_t d) {
  744. const char *name = bench_define_name(d);
  745. intmax_t value = BENCH_DEFINE(d);
  746. // define already in defines?
  747. for (size_t i = 0; i < defines->define_count; i++) {
  748. if (strcmp(defines->defines[i].name, name) == 0) {
  749. // value already in values?
  750. for (size_t j = 0; j < defines->defines[i].value_count; j++) {
  751. if (defines->defines[i].values[j] == value) {
  752. return;
  753. }
  754. }
  755. *(intmax_t*)mappend(
  756. (void**)&defines->defines[i].values,
  757. sizeof(intmax_t),
  758. &defines->defines[i].value_count,
  759. &defines->defines[i].value_capacity) = value;
  760. return;
  761. }
  762. }
  763. // new define?
  764. struct list_defines_define *define = mappend(
  765. (void**)&defines->defines,
  766. sizeof(struct list_defines_define),
  767. &defines->define_count,
  768. &defines->define_capacity);
  769. define->name = name;
  770. define->values = malloc(sizeof(intmax_t));
  771. define->values[0] = value;
  772. define->value_count = 1;
  773. define->value_capacity = 1;
  774. }
  775. void perm_list_defines(
  776. void *data,
  777. const struct bench_suite *suite,
  778. const struct bench_case *case_) {
  779. struct list_defines_defines *defines = data;
  780. (void)suite;
  781. (void)case_;
  782. // collect defines
  783. for (size_t d = 0;
  784. d < lfs_max(suite->define_count,
  785. BENCH_IMPLICIT_DEFINE_COUNT);
  786. d++) {
  787. if (d < BENCH_IMPLICIT_DEFINE_COUNT
  788. || bench_define_ispermutation(d)) {
  789. list_defines_add(defines, d);
  790. }
  791. }
  792. }
  793. void perm_list_permutation_defines(
  794. void *data,
  795. const struct bench_suite *suite,
  796. const struct bench_case *case_) {
  797. struct list_defines_defines *defines = data;
  798. (void)suite;
  799. (void)case_;
  800. // collect permutation_defines
  801. for (size_t d = 0;
  802. d < lfs_max(suite->define_count,
  803. BENCH_IMPLICIT_DEFINE_COUNT);
  804. d++) {
  805. if (bench_define_ispermutation(d)) {
  806. list_defines_add(defines, d);
  807. }
  808. }
  809. }
  810. extern const bench_geometry_t builtin_geometries[];
  811. static void list_defines(void) {
  812. struct list_defines_defines defines = {NULL, 0, 0};
  813. // add defines
  814. for (size_t t = 0; t < bench_id_count; t++) {
  815. for (size_t i = 0; i < BENCH_SUITE_COUNT; i++) {
  816. bench_define_suite(&bench_suites[i]);
  817. for (size_t j = 0; j < bench_suites[i].case_count; j++) {
  818. // does neither suite nor case name match?
  819. if (bench_ids[t].name && !(
  820. strcmp(bench_ids[t].name,
  821. bench_suites[i].name) == 0
  822. || strcmp(bench_ids[t].name,
  823. bench_suites[i].cases[j].name) == 0)) {
  824. continue;
  825. }
  826. case_forperm(
  827. &bench_suites[i],
  828. &bench_suites[i].cases[j],
  829. bench_ids[t].defines,
  830. bench_ids[t].define_count,
  831. perm_list_defines,
  832. &defines);
  833. }
  834. }
  835. }
  836. for (size_t i = 0; i < defines.define_count; i++) {
  837. printf("%s=", defines.defines[i].name);
  838. for (size_t j = 0; j < defines.defines[i].value_count; j++) {
  839. printf("%jd", defines.defines[i].values[j]);
  840. if (j != defines.defines[i].value_count-1) {
  841. printf(",");
  842. }
  843. }
  844. printf("\n");
  845. }
  846. for (size_t i = 0; i < defines.define_count; i++) {
  847. free(defines.defines[i].values);
  848. }
  849. free(defines.defines);
  850. }
  851. static void list_permutation_defines(void) {
  852. struct list_defines_defines defines = {NULL, 0, 0};
  853. // add permutation defines
  854. for (size_t t = 0; t < bench_id_count; t++) {
  855. for (size_t i = 0; i < BENCH_SUITE_COUNT; i++) {
  856. bench_define_suite(&bench_suites[i]);
  857. for (size_t j = 0; j < bench_suites[i].case_count; j++) {
  858. // does neither suite nor case name match?
  859. if (bench_ids[t].name && !(
  860. strcmp(bench_ids[t].name,
  861. bench_suites[i].name) == 0
  862. || strcmp(bench_ids[t].name,
  863. bench_suites[i].cases[j].name) == 0)) {
  864. continue;
  865. }
  866. case_forperm(
  867. &bench_suites[i],
  868. &bench_suites[i].cases[j],
  869. bench_ids[t].defines,
  870. bench_ids[t].define_count,
  871. perm_list_permutation_defines,
  872. &defines);
  873. }
  874. }
  875. }
  876. for (size_t i = 0; i < defines.define_count; i++) {
  877. printf("%s=", defines.defines[i].name);
  878. for (size_t j = 0; j < defines.defines[i].value_count; j++) {
  879. printf("%jd", defines.defines[i].values[j]);
  880. if (j != defines.defines[i].value_count-1) {
  881. printf(",");
  882. }
  883. }
  884. printf("\n");
  885. }
  886. for (size_t i = 0; i < defines.define_count; i++) {
  887. free(defines.defines[i].values);
  888. }
  889. free(defines.defines);
  890. }
  891. static void list_implicit_defines(void) {
  892. struct list_defines_defines defines = {NULL, 0, 0};
  893. // yes we do need to define a suite, this does a bit of bookeeping
  894. // such as setting up the define cache
  895. bench_define_suite(&(const struct bench_suite){0});
  896. // make sure to include builtin geometries here
  897. extern const bench_geometry_t builtin_geometries[];
  898. for (size_t g = 0; builtin_geometries[g].long_name; g++) {
  899. bench_define_geometry(&builtin_geometries[g]);
  900. bench_define_flush();
  901. // add implicit defines
  902. for (size_t d = 0; d < BENCH_IMPLICIT_DEFINE_COUNT; d++) {
  903. list_defines_add(&defines, d);
  904. }
  905. }
  906. for (size_t i = 0; i < defines.define_count; i++) {
  907. printf("%s=", defines.defines[i].name);
  908. for (size_t j = 0; j < defines.defines[i].value_count; j++) {
  909. printf("%jd", defines.defines[i].values[j]);
  910. if (j != defines.defines[i].value_count-1) {
  911. printf(",");
  912. }
  913. }
  914. printf("\n");
  915. }
  916. for (size_t i = 0; i < defines.define_count; i++) {
  917. free(defines.defines[i].values);
  918. }
  919. free(defines.defines);
  920. }
  921. // geometries to bench
  922. const bench_geometry_t builtin_geometries[] = {
  923. {'d', "default", {{NULL}, BENCH_CONST(16), BENCH_CONST(512), {NULL}}},
  924. {'e', "eeprom", {{NULL}, BENCH_CONST(1), BENCH_CONST(512), {NULL}}},
  925. {'E', "emmc", {{NULL}, {NULL}, BENCH_CONST(512), {NULL}}},
  926. {'n', "nor", {{NULL}, BENCH_CONST(1), BENCH_CONST(4096), {NULL}}},
  927. {'N', "nand", {{NULL}, BENCH_CONST(4096), BENCH_CONST(32768), {NULL}}},
  928. {0, NULL, {{NULL}, {NULL}, {NULL}, {NULL}}},
  929. };
  930. const bench_geometry_t *bench_geometries = builtin_geometries;
  931. size_t bench_geometry_count = 5;
  932. static void list_geometries(void) {
  933. // yes we do need to define a suite, this does a bit of bookeeping
  934. // such as setting up the define cache
  935. bench_define_suite(&(const struct bench_suite){0});
  936. printf("%-24s %7s %7s %7s %7s %11s\n",
  937. "geometry", "read", "prog", "erase", "count", "size");
  938. for (size_t g = 0; builtin_geometries[g].long_name; g++) {
  939. bench_define_geometry(&builtin_geometries[g]);
  940. bench_define_flush();
  941. printf("%c,%-22s %7ju %7ju %7ju %7ju %11ju\n",
  942. builtin_geometries[g].short_name,
  943. builtin_geometries[g].long_name,
  944. READ_SIZE,
  945. PROG_SIZE,
  946. BLOCK_SIZE,
  947. BLOCK_COUNT,
  948. BLOCK_SIZE*BLOCK_COUNT);
  949. }
  950. }
  951. // global bench step count
  952. size_t bench_step = 0;
  953. void perm_run(
  954. void *data,
  955. const struct bench_suite *suite,
  956. const struct bench_case *case_) {
  957. (void)data;
  958. // skip this step?
  959. if (!(bench_step >= bench_step_start
  960. && bench_step < bench_step_stop
  961. && (bench_step-bench_step_start) % bench_step_step == 0)) {
  962. bench_step += 1;
  963. return;
  964. }
  965. bench_step += 1;
  966. // filter?
  967. if (case_->filter && !case_->filter()) {
  968. printf("skipped ");
  969. perm_printid(suite, case_);
  970. printf("\n");
  971. return;
  972. }
  973. // create block device and configuration
  974. lfs_emubd_t bd;
  975. struct lfs_config cfg = {
  976. .context = &bd,
  977. .read = lfs_emubd_read,
  978. .prog = lfs_emubd_prog,
  979. .erase = lfs_emubd_erase,
  980. .sync = lfs_emubd_sync,
  981. .read_size = READ_SIZE,
  982. .prog_size = PROG_SIZE,
  983. .block_size = BLOCK_SIZE,
  984. .block_count = BLOCK_COUNT,
  985. .block_cycles = BLOCK_CYCLES,
  986. .cache_size = CACHE_SIZE,
  987. .lookahead_size = LOOKAHEAD_SIZE,
  988. };
  989. struct lfs_emubd_config bdcfg = {
  990. .erase_value = ERASE_VALUE,
  991. .erase_cycles = ERASE_CYCLES,
  992. .badblock_behavior = BADBLOCK_BEHAVIOR,
  993. .disk_path = bench_disk_path,
  994. .read_sleep = bench_read_sleep,
  995. .prog_sleep = bench_prog_sleep,
  996. .erase_sleep = bench_erase_sleep,
  997. };
  998. int err = lfs_emubd_createcfg(&cfg, bench_disk_path, &bdcfg);
  999. if (err) {
  1000. fprintf(stderr, "error: could not create block device: %d\n", err);
  1001. exit(-1);
  1002. }
  1003. // run the bench
  1004. bench_cfg = &cfg;
  1005. bench_reset();
  1006. printf("running ");
  1007. perm_printid(suite, case_);
  1008. printf("\n");
  1009. case_->run(&cfg);
  1010. printf("finished ");
  1011. perm_printid(suite, case_);
  1012. printf(" %"PRIu64" %"PRIu64" %"PRIu64,
  1013. bench_read,
  1014. bench_prog,
  1015. bench_erased);
  1016. printf("\n");
  1017. // cleanup
  1018. err = lfs_emubd_destroy(&cfg);
  1019. if (err) {
  1020. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1021. exit(-1);
  1022. }
  1023. }
  1024. static void run(void) {
  1025. // ignore disconnected pipes
  1026. signal(SIGPIPE, SIG_IGN);
  1027. for (size_t t = 0; t < bench_id_count; t++) {
  1028. for (size_t i = 0; i < BENCH_SUITE_COUNT; i++) {
  1029. bench_define_suite(&bench_suites[i]);
  1030. for (size_t j = 0; j < bench_suites[i].case_count; j++) {
  1031. // does neither suite nor case name match?
  1032. if (bench_ids[t].name && !(
  1033. strcmp(bench_ids[t].name,
  1034. bench_suites[i].name) == 0
  1035. || strcmp(bench_ids[t].name,
  1036. bench_suites[i].cases[j].name) == 0)) {
  1037. continue;
  1038. }
  1039. case_forperm(
  1040. &bench_suites[i],
  1041. &bench_suites[i].cases[j],
  1042. bench_ids[t].defines,
  1043. bench_ids[t].define_count,
  1044. perm_run,
  1045. NULL);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. // option handling
  1051. enum opt_flags {
  1052. OPT_HELP = 'h',
  1053. OPT_SUMMARY = 'Y',
  1054. OPT_LIST_SUITES = 'l',
  1055. OPT_LIST_CASES = 'L',
  1056. OPT_LIST_SUITE_PATHS = 1,
  1057. OPT_LIST_CASE_PATHS = 2,
  1058. OPT_LIST_DEFINES = 3,
  1059. OPT_LIST_PERMUTATION_DEFINES = 4,
  1060. OPT_LIST_IMPLICIT_DEFINES = 5,
  1061. OPT_LIST_GEOMETRIES = 6,
  1062. OPT_DEFINE = 'D',
  1063. OPT_GEOMETRY = 'g',
  1064. OPT_STEP = 's',
  1065. OPT_DISK = 'd',
  1066. OPT_TRACE = 't',
  1067. OPT_READ_SLEEP = 7,
  1068. OPT_PROG_SLEEP = 8,
  1069. OPT_ERASE_SLEEP = 9,
  1070. };
  1071. const char *short_opts = "hYlLD:g:s:d:t:";
  1072. const struct option long_opts[] = {
  1073. {"help", no_argument, NULL, OPT_HELP},
  1074. {"summary", no_argument, NULL, OPT_SUMMARY},
  1075. {"list-suites", no_argument, NULL, OPT_LIST_SUITES},
  1076. {"list-cases", no_argument, NULL, OPT_LIST_CASES},
  1077. {"list-suite-paths", no_argument, NULL, OPT_LIST_SUITE_PATHS},
  1078. {"list-case-paths", no_argument, NULL, OPT_LIST_CASE_PATHS},
  1079. {"list-defines", no_argument, NULL, OPT_LIST_DEFINES},
  1080. {"list-permutation-defines",
  1081. no_argument, NULL, OPT_LIST_PERMUTATION_DEFINES},
  1082. {"list-implicit-defines",
  1083. no_argument, NULL, OPT_LIST_IMPLICIT_DEFINES},
  1084. {"list-geometries", no_argument, NULL, OPT_LIST_GEOMETRIES},
  1085. {"define", required_argument, NULL, OPT_DEFINE},
  1086. {"geometry", required_argument, NULL, OPT_GEOMETRY},
  1087. {"step", required_argument, NULL, OPT_STEP},
  1088. {"disk", required_argument, NULL, OPT_DISK},
  1089. {"trace", required_argument, NULL, OPT_TRACE},
  1090. {"read-sleep", required_argument, NULL, OPT_READ_SLEEP},
  1091. {"prog-sleep", required_argument, NULL, OPT_PROG_SLEEP},
  1092. {"erase-sleep", required_argument, NULL, OPT_ERASE_SLEEP},
  1093. {NULL, 0, NULL, 0},
  1094. };
  1095. const char *const help_text[] = {
  1096. "Show this help message.",
  1097. "Show quick summary.",
  1098. "List bench suites.",
  1099. "List bench cases.",
  1100. "List the path for each bench suite.",
  1101. "List the path and line number for each bench case.",
  1102. "List all defines in this bench-runner.",
  1103. "List explicit defines in this bench-runner.",
  1104. "List implicit defines in this bench-runner.",
  1105. "List the available disk geometries.",
  1106. "Override a bench define.",
  1107. "Comma-separated list of disk geometries to bench. Defaults to d,e,E,n,N.",
  1108. "Comma-separated range of bench permutations to run (start,stop,step).",
  1109. "Redirect block device operations to this file.",
  1110. "Redirect trace output to this file.",
  1111. "Artificial read delay in seconds.",
  1112. "Artificial prog delay in seconds.",
  1113. "Artificial erase delay in seconds.",
  1114. };
  1115. int main(int argc, char **argv) {
  1116. void (*op)(void) = run;
  1117. size_t bench_override_capacity = 0;
  1118. size_t bench_geometry_capacity = 0;
  1119. size_t bench_id_capacity = 0;
  1120. // parse options
  1121. while (true) {
  1122. int c = getopt_long(argc, argv, short_opts, long_opts, NULL);
  1123. switch (c) {
  1124. // generate help message
  1125. case OPT_HELP: {
  1126. printf("usage: %s [options] [bench_id]\n", argv[0]);
  1127. printf("\n");
  1128. printf("options:\n");
  1129. size_t i = 0;
  1130. while (long_opts[i].name) {
  1131. size_t indent;
  1132. if (long_opts[i].has_arg == no_argument) {
  1133. if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
  1134. indent = printf(" -%c, --%s ",
  1135. long_opts[i].val,
  1136. long_opts[i].name);
  1137. } else {
  1138. indent = printf(" --%s ",
  1139. long_opts[i].name);
  1140. }
  1141. } else {
  1142. if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
  1143. indent = printf(" -%c %s, --%s %s ",
  1144. long_opts[i].val,
  1145. long_opts[i].name,
  1146. long_opts[i].name,
  1147. long_opts[i].name);
  1148. } else {
  1149. indent = printf(" --%s %s ",
  1150. long_opts[i].name,
  1151. long_opts[i].name);
  1152. }
  1153. }
  1154. // a quick, hacky, byte-level method for text wrapping
  1155. size_t len = strlen(help_text[i]);
  1156. size_t j = 0;
  1157. if (indent < 24) {
  1158. printf("%*s %.80s\n",
  1159. (int)(24-1-indent),
  1160. "",
  1161. &help_text[i][j]);
  1162. j += 80;
  1163. } else {
  1164. printf("\n");
  1165. }
  1166. while (j < len) {
  1167. printf("%24s%.80s\n", "", &help_text[i][j]);
  1168. j += 80;
  1169. }
  1170. i += 1;
  1171. }
  1172. printf("\n");
  1173. exit(0);
  1174. }
  1175. // summary/list flags
  1176. case OPT_SUMMARY:
  1177. op = summary;
  1178. break;
  1179. case OPT_LIST_SUITES:
  1180. op = list_suites;
  1181. break;
  1182. case OPT_LIST_CASES:
  1183. op = list_cases;
  1184. break;
  1185. case OPT_LIST_SUITE_PATHS:
  1186. op = list_suite_paths;
  1187. break;
  1188. case OPT_LIST_CASE_PATHS:
  1189. op = list_case_paths;
  1190. break;
  1191. case OPT_LIST_DEFINES:
  1192. op = list_defines;
  1193. break;
  1194. case OPT_LIST_PERMUTATION_DEFINES:
  1195. op = list_permutation_defines;
  1196. break;
  1197. case OPT_LIST_IMPLICIT_DEFINES:
  1198. op = list_implicit_defines;
  1199. break;
  1200. case OPT_LIST_GEOMETRIES:
  1201. op = list_geometries;
  1202. break;
  1203. // configuration
  1204. case OPT_DEFINE: {
  1205. // allocate space
  1206. bench_override_t *override = mappend(
  1207. (void**)&bench_overrides,
  1208. sizeof(bench_override_t),
  1209. &bench_override_count,
  1210. &bench_override_capacity);
  1211. // parse into string key/intmax_t value, cannibalizing the
  1212. // arg in the process
  1213. char *sep = strchr(optarg, '=');
  1214. char *parsed = NULL;
  1215. if (!sep) {
  1216. goto invalid_define;
  1217. }
  1218. *sep = '\0';
  1219. override->name = optarg;
  1220. optarg = sep+1;
  1221. // parse comma-separated permutations
  1222. {
  1223. override->defines = NULL;
  1224. override->permutations = 0;
  1225. size_t override_capacity = 0;
  1226. while (true) {
  1227. optarg += strspn(optarg, " ");
  1228. if (strncmp(optarg, "range", strlen("range")) == 0) {
  1229. // range of values
  1230. optarg += strlen("range");
  1231. optarg += strspn(optarg, " ");
  1232. if (*optarg != '(') {
  1233. goto invalid_define;
  1234. }
  1235. optarg += 1;
  1236. intmax_t start = strtoumax(optarg, &parsed, 0);
  1237. intmax_t stop = -1;
  1238. intmax_t step = 1;
  1239. // allow empty string for start=0
  1240. if (parsed == optarg) {
  1241. start = 0;
  1242. }
  1243. optarg = parsed + strspn(parsed, " ");
  1244. if (*optarg != ',' && *optarg != ')') {
  1245. goto invalid_define;
  1246. }
  1247. if (*optarg == ',') {
  1248. optarg += 1;
  1249. stop = strtoumax(optarg, &parsed, 0);
  1250. // allow empty string for stop=end
  1251. if (parsed == optarg) {
  1252. stop = -1;
  1253. }
  1254. optarg = parsed + strspn(parsed, " ");
  1255. if (*optarg != ',' && *optarg != ')') {
  1256. goto invalid_define;
  1257. }
  1258. if (*optarg == ',') {
  1259. optarg += 1;
  1260. step = strtoumax(optarg, &parsed, 0);
  1261. // allow empty string for stop=1
  1262. if (parsed == optarg) {
  1263. step = 1;
  1264. }
  1265. optarg = parsed + strspn(parsed, " ");
  1266. if (*optarg != ')') {
  1267. goto invalid_define;
  1268. }
  1269. }
  1270. } else {
  1271. // single value = stop only
  1272. stop = start;
  1273. start = 0;
  1274. }
  1275. if (*optarg != ')') {
  1276. goto invalid_define;
  1277. }
  1278. optarg += 1;
  1279. // calculate the range of values
  1280. assert(step != 0);
  1281. for (intmax_t i = start;
  1282. (step < 0)
  1283. ? i > stop
  1284. : (uintmax_t)i < (uintmax_t)stop;
  1285. i += step) {
  1286. *(intmax_t*)mappend(
  1287. (void**)&override->defines,
  1288. sizeof(intmax_t),
  1289. &override->permutations,
  1290. &override_capacity) = i;
  1291. }
  1292. } else if (*optarg != '\0') {
  1293. // single value
  1294. intmax_t define = strtoimax(optarg, &parsed, 0);
  1295. if (parsed == optarg) {
  1296. goto invalid_define;
  1297. }
  1298. optarg = parsed + strspn(parsed, " ");
  1299. *(intmax_t*)mappend(
  1300. (void**)&override->defines,
  1301. sizeof(intmax_t),
  1302. &override->permutations,
  1303. &override_capacity) = define;
  1304. } else {
  1305. break;
  1306. }
  1307. if (*optarg == ',') {
  1308. optarg += 1;
  1309. }
  1310. }
  1311. }
  1312. assert(override->permutations > 0);
  1313. break;
  1314. invalid_define:
  1315. fprintf(stderr, "error: invalid define: %s\n", optarg);
  1316. exit(-1);
  1317. }
  1318. case OPT_GEOMETRY: {
  1319. // reset our geometry scenarios
  1320. if (bench_geometry_capacity > 0) {
  1321. free((bench_geometry_t*)bench_geometries);
  1322. }
  1323. bench_geometries = NULL;
  1324. bench_geometry_count = 0;
  1325. bench_geometry_capacity = 0;
  1326. // parse the comma separated list of disk geometries
  1327. while (*optarg) {
  1328. // allocate space
  1329. bench_geometry_t *geometry = mappend(
  1330. (void**)&bench_geometries,
  1331. sizeof(bench_geometry_t),
  1332. &bench_geometry_count,
  1333. &bench_geometry_capacity);
  1334. // parse the disk geometry
  1335. optarg += strspn(optarg, " ");
  1336. // named disk geometry
  1337. size_t len = strcspn(optarg, " ,");
  1338. for (size_t i = 0; builtin_geometries[i].long_name; i++) {
  1339. if ((len == 1
  1340. && *optarg == builtin_geometries[i].short_name)
  1341. || (len == strlen(
  1342. builtin_geometries[i].long_name)
  1343. && memcmp(optarg,
  1344. builtin_geometries[i].long_name,
  1345. len) == 0)) {
  1346. *geometry = builtin_geometries[i];
  1347. optarg += len;
  1348. goto geometry_next;
  1349. }
  1350. }
  1351. // comma-separated read/prog/erase/count
  1352. if (*optarg == '{') {
  1353. lfs_size_t sizes[4];
  1354. size_t count = 0;
  1355. char *s = optarg + 1;
  1356. while (count < 4) {
  1357. char *parsed = NULL;
  1358. sizes[count] = strtoumax(s, &parsed, 0);
  1359. count += 1;
  1360. s = parsed + strspn(parsed, " ");
  1361. if (*s == ',') {
  1362. s += 1;
  1363. continue;
  1364. } else if (*s == '}') {
  1365. s += 1;
  1366. break;
  1367. } else {
  1368. goto geometry_unknown;
  1369. }
  1370. }
  1371. // allow implicit r=p and p=e for common geometries
  1372. memset(geometry, 0, sizeof(bench_geometry_t));
  1373. if (count >= 3) {
  1374. geometry->defines[READ_SIZE_i]
  1375. = BENCH_LIT(sizes[0]);
  1376. geometry->defines[PROG_SIZE_i]
  1377. = BENCH_LIT(sizes[1]);
  1378. geometry->defines[BLOCK_SIZE_i]
  1379. = BENCH_LIT(sizes[2]);
  1380. } else if (count >= 2) {
  1381. geometry->defines[PROG_SIZE_i]
  1382. = BENCH_LIT(sizes[0]);
  1383. geometry->defines[BLOCK_SIZE_i]
  1384. = BENCH_LIT(sizes[1]);
  1385. } else {
  1386. geometry->defines[BLOCK_SIZE_i]
  1387. = BENCH_LIT(sizes[0]);
  1388. }
  1389. if (count >= 4) {
  1390. geometry->defines[BLOCK_COUNT_i]
  1391. = BENCH_LIT(sizes[3]);
  1392. }
  1393. optarg = s;
  1394. goto geometry_next;
  1395. }
  1396. // leb16-encoded read/prog/erase/count
  1397. if (*optarg == ':') {
  1398. lfs_size_t sizes[4];
  1399. size_t count = 0;
  1400. char *s = optarg + 1;
  1401. while (true) {
  1402. char *parsed = NULL;
  1403. uintmax_t x = leb16_parse(s, &parsed);
  1404. if (parsed == s || count >= 4) {
  1405. break;
  1406. }
  1407. sizes[count] = x;
  1408. count += 1;
  1409. s = parsed;
  1410. }
  1411. // allow implicit r=p and p=e for common geometries
  1412. memset(geometry, 0, sizeof(bench_geometry_t));
  1413. if (count >= 3) {
  1414. geometry->defines[READ_SIZE_i]
  1415. = BENCH_LIT(sizes[0]);
  1416. geometry->defines[PROG_SIZE_i]
  1417. = BENCH_LIT(sizes[1]);
  1418. geometry->defines[BLOCK_SIZE_i]
  1419. = BENCH_LIT(sizes[2]);
  1420. } else if (count >= 2) {
  1421. geometry->defines[PROG_SIZE_i]
  1422. = BENCH_LIT(sizes[0]);
  1423. geometry->defines[BLOCK_SIZE_i]
  1424. = BENCH_LIT(sizes[1]);
  1425. } else {
  1426. geometry->defines[BLOCK_SIZE_i]
  1427. = BENCH_LIT(sizes[0]);
  1428. }
  1429. if (count >= 4) {
  1430. geometry->defines[BLOCK_COUNT_i]
  1431. = BENCH_LIT(sizes[3]);
  1432. }
  1433. optarg = s;
  1434. goto geometry_next;
  1435. }
  1436. geometry_unknown:
  1437. // unknown scenario?
  1438. fprintf(stderr, "error: unknown disk geometry: %s\n",
  1439. optarg);
  1440. exit(-1);
  1441. geometry_next:
  1442. optarg += strspn(optarg, " ");
  1443. if (*optarg == ',') {
  1444. optarg += 1;
  1445. } else if (*optarg == '\0') {
  1446. break;
  1447. } else {
  1448. goto geometry_unknown;
  1449. }
  1450. }
  1451. break;
  1452. }
  1453. case OPT_STEP: {
  1454. char *parsed = NULL;
  1455. bench_step_start = strtoumax(optarg, &parsed, 0);
  1456. bench_step_stop = -1;
  1457. bench_step_step = 1;
  1458. // allow empty string for start=0
  1459. if (parsed == optarg) {
  1460. bench_step_start = 0;
  1461. }
  1462. optarg = parsed + strspn(parsed, " ");
  1463. if (*optarg != ',' && *optarg != '\0') {
  1464. goto step_unknown;
  1465. }
  1466. if (*optarg == ',') {
  1467. optarg += 1;
  1468. bench_step_stop = strtoumax(optarg, &parsed, 0);
  1469. // allow empty string for stop=end
  1470. if (parsed == optarg) {
  1471. bench_step_stop = -1;
  1472. }
  1473. optarg = parsed + strspn(parsed, " ");
  1474. if (*optarg != ',' && *optarg != '\0') {
  1475. goto step_unknown;
  1476. }
  1477. if (*optarg == ',') {
  1478. optarg += 1;
  1479. bench_step_step = strtoumax(optarg, &parsed, 0);
  1480. // allow empty string for stop=1
  1481. if (parsed == optarg) {
  1482. bench_step_step = 1;
  1483. }
  1484. optarg = parsed + strspn(parsed, " ");
  1485. if (*optarg != '\0') {
  1486. goto step_unknown;
  1487. }
  1488. }
  1489. } else {
  1490. // single value = stop only
  1491. bench_step_stop = bench_step_start;
  1492. bench_step_start = 0;
  1493. }
  1494. break;
  1495. step_unknown:
  1496. fprintf(stderr, "error: invalid step: %s\n", optarg);
  1497. exit(-1);
  1498. }
  1499. case OPT_DISK:
  1500. bench_disk_path = optarg;
  1501. break;
  1502. case OPT_TRACE:
  1503. bench_trace_path = optarg;
  1504. break;
  1505. case OPT_READ_SLEEP: {
  1506. char *parsed = NULL;
  1507. double read_sleep = strtod(optarg, &parsed);
  1508. if (parsed == optarg) {
  1509. fprintf(stderr, "error: invalid read-sleep: %s\n", optarg);
  1510. exit(-1);
  1511. }
  1512. bench_read_sleep = read_sleep*1.0e9;
  1513. break;
  1514. }
  1515. case OPT_PROG_SLEEP: {
  1516. char *parsed = NULL;
  1517. double prog_sleep = strtod(optarg, &parsed);
  1518. if (parsed == optarg) {
  1519. fprintf(stderr, "error: invalid prog-sleep: %s\n", optarg);
  1520. exit(-1);
  1521. }
  1522. bench_prog_sleep = prog_sleep*1.0e9;
  1523. break;
  1524. }
  1525. case OPT_ERASE_SLEEP: {
  1526. char *parsed = NULL;
  1527. double erase_sleep = strtod(optarg, &parsed);
  1528. if (parsed == optarg) {
  1529. fprintf(stderr, "error: invalid erase-sleep: %s\n", optarg);
  1530. exit(-1);
  1531. }
  1532. bench_erase_sleep = erase_sleep*1.0e9;
  1533. break;
  1534. }
  1535. // done parsing
  1536. case -1:
  1537. goto getopt_done;
  1538. // unknown arg, getopt prints a message for us
  1539. default:
  1540. exit(-1);
  1541. }
  1542. }
  1543. getopt_done: ;
  1544. if (argc > optind) {
  1545. // reset our bench identifier list
  1546. bench_ids = NULL;
  1547. bench_id_count = 0;
  1548. bench_id_capacity = 0;
  1549. }
  1550. // parse bench identifier, if any, cannibalizing the arg in the process
  1551. for (; argc > optind; optind++) {
  1552. bench_define_t *defines = NULL;
  1553. size_t define_count = 0;
  1554. // parse name, can be suite or case
  1555. char *name = argv[optind];
  1556. char *defines_ = strchr(name, ':');
  1557. if (defines_) {
  1558. *defines_ = '\0';
  1559. defines_ += 1;
  1560. }
  1561. // remove optional path and .toml suffix
  1562. char *slash = strrchr(name, '/');
  1563. if (slash) {
  1564. name = slash+1;
  1565. }
  1566. size_t name_len = strlen(name);
  1567. if (name_len > 5 && strcmp(&name[name_len-5], ".toml") == 0) {
  1568. name[name_len-5] = '\0';
  1569. }
  1570. if (defines_) {
  1571. // parse defines
  1572. while (true) {
  1573. char *parsed;
  1574. size_t d = leb16_parse(defines_, &parsed);
  1575. intmax_t v = leb16_parse(parsed, &parsed);
  1576. if (parsed == defines_) {
  1577. break;
  1578. }
  1579. defines_ = parsed;
  1580. if (d >= define_count) {
  1581. // align to power of two to avoid any superlinear growth
  1582. size_t ncount = 1 << lfs_npw2(d+1);
  1583. defines = realloc(defines,
  1584. ncount*sizeof(bench_define_t));
  1585. memset(defines+define_count, 0,
  1586. (ncount-define_count)*sizeof(bench_define_t));
  1587. define_count = ncount;
  1588. }
  1589. defines[d] = BENCH_LIT(v);
  1590. }
  1591. }
  1592. // append to identifier list
  1593. *(bench_id_t*)mappend(
  1594. (void**)&bench_ids,
  1595. sizeof(bench_id_t),
  1596. &bench_id_count,
  1597. &bench_id_capacity) = (bench_id_t){
  1598. .name = name,
  1599. .defines = defines,
  1600. .define_count = define_count,
  1601. };
  1602. }
  1603. // do the thing
  1604. op();
  1605. // cleanup (need to be done for valgrind benching)
  1606. bench_define_cleanup();
  1607. if (bench_overrides) {
  1608. for (size_t i = 0; i < bench_override_count; i++) {
  1609. free((void*)bench_overrides[i].defines);
  1610. }
  1611. free((void*)bench_overrides);
  1612. }
  1613. if (bench_geometry_capacity) {
  1614. free((void*)bench_geometries);
  1615. }
  1616. if (bench_id_capacity) {
  1617. for (size_t i = 0; i < bench_id_count; i++) {
  1618. free((void*)bench_ids[i].defines);
  1619. }
  1620. free((void*)bench_ids);
  1621. }
  1622. }