test_runner.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. #ifndef _POSIX_C_SOURCE
  2. #define _POSIX_C_SOURCE 199309L
  3. #endif
  4. #include "runners/test_runner.h"
  5. #include "bd/lfs_testbd.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. // test suites in a custom ld section
  15. extern struct test_suite __start__test_suites;
  16. extern struct test_suite __stop__test_suites;
  17. const struct test_suite *test_suites = &__start__test_suites;
  18. #define TEST_SUITE_COUNT \
  19. ((size_t)(&__stop__test_suites - &__start__test_suites))
  20. // test geometries
  21. struct test_geometry {
  22. const char *name;
  23. intmax_t defines[TEST_GEOMETRY_DEFINE_COUNT];
  24. };
  25. const struct test_geometry test_geometries[TEST_GEOMETRY_COUNT]
  26. = TEST_GEOMETRIES;
  27. // test define lookup and management
  28. const intmax_t *test_override_defines;
  29. intmax_t (*const *test_case_defines)(void);
  30. const intmax_t *test_geometry_defines;
  31. const intmax_t test_default_defines[TEST_PREDEFINE_COUNT]
  32. = TEST_DEFAULTS;
  33. uint8_t test_override_predefine_map[TEST_PREDEFINE_COUNT];
  34. uint8_t test_override_define_map[256];
  35. uint8_t test_case_predefine_map[TEST_PREDEFINE_COUNT];
  36. const char *const *test_override_names;
  37. size_t test_override_count;
  38. const char *const test_predefine_names[TEST_PREDEFINE_COUNT]
  39. = TEST_PREDEFINE_NAMES;
  40. const char *const *test_define_names;
  41. size_t test_define_count;
  42. intmax_t test_predefine(size_t define) {
  43. if (test_override_defines
  44. && test_override_predefine_map[define] != 0xff) {
  45. return test_override_defines[test_override_predefine_map[define]];
  46. } else if (test_case_defines
  47. && test_case_predefine_map[define] != 0xff
  48. && test_case_defines[test_case_predefine_map[define]]) {
  49. return test_case_defines[test_case_predefine_map[define]]();
  50. } else if (define < TEST_GEOMETRY_DEFINE_COUNT) {
  51. return test_geometry_defines[define];
  52. } else {
  53. return test_default_defines[define-TEST_GEOMETRY_DEFINE_COUNT];
  54. }
  55. }
  56. intmax_t test_define(size_t define) {
  57. if (test_override_defines
  58. && test_override_define_map[define] != 0xff) {
  59. return test_override_defines[test_override_define_map[define]];
  60. } else if (test_case_defines
  61. && test_case_defines[define]) {
  62. return test_case_defines[define]();
  63. }
  64. fprintf(stderr, "error: undefined define %s\n",
  65. test_define_names[define]);
  66. assert(false);
  67. exit(-1);
  68. }
  69. static void define_geometry(const struct test_geometry *geometry) {
  70. test_geometry_defines = geometry->defines;
  71. }
  72. static void test_define_overrides(
  73. const char *const *override_names,
  74. const intmax_t *override_defines,
  75. size_t override_count) {
  76. test_override_defines = override_defines;
  77. test_override_names = override_names;
  78. test_override_count = override_count;
  79. // map any override predefines
  80. memset(test_override_predefine_map, 0xff, TEST_PREDEFINE_COUNT);
  81. for (size_t i = 0; i < test_override_count; i++) {
  82. for (size_t j = 0; j < TEST_PREDEFINE_COUNT; j++) {
  83. if (strcmp(test_override_names[i], test_predefine_names[j]) == 0) {
  84. test_override_predefine_map[j] = i;
  85. }
  86. }
  87. }
  88. }
  89. static void define_suite(const struct test_suite *suite) {
  90. test_define_names = suite->define_names;
  91. test_define_count = suite->define_count;
  92. // map any override defines
  93. memset(test_override_define_map, 0xff, suite->define_count);
  94. for (size_t i = 0; i < test_override_count; i++) {
  95. for (size_t j = 0; j < suite->define_count; j++) {
  96. if (strcmp(test_override_names[i], suite->define_names[j]) == 0) {
  97. test_override_define_map[j] = i;
  98. }
  99. }
  100. }
  101. // map any suite/case predefines
  102. memset(test_case_predefine_map, 0xff, TEST_PREDEFINE_COUNT);
  103. for (size_t i = 0; i < suite->define_count; i++) {
  104. for (size_t j = 0; j < TEST_PREDEFINE_COUNT; j++) {
  105. if (strcmp(suite->define_names[i], test_predefine_names[j]) == 0) {
  106. test_case_predefine_map[j] = i;
  107. }
  108. }
  109. }
  110. }
  111. static void define_perm(
  112. const struct test_suite *suite,
  113. const struct test_case *case_,
  114. size_t perm) {
  115. (void)suite;
  116. if (case_->defines) {
  117. test_case_defines = case_->defines[perm];
  118. } else {
  119. test_case_defines = NULL;
  120. }
  121. }
  122. // a quick encoding scheme for sequences of power-loss
  123. static void leb16_print(
  124. const lfs_testbd_powercycles_t *cycles,
  125. size_t cycle_count) {
  126. for (size_t i = 0; i < cycle_count; i++) {
  127. lfs_testbd_powercycles_t x = cycles[i];
  128. while (true) {
  129. lfs_testbd_powercycles_t nibble = (x & 0xf) | (x > 0xf ? 0x10 : 0);
  130. printf("%c", (nibble < 10) ? '0'+nibble : 'a'+nibble-10);
  131. if (x <= 0xf) {
  132. break;
  133. }
  134. x >>= 4;
  135. }
  136. }
  137. }
  138. static size_t leb16_parse(const char *s, char **tail,
  139. lfs_testbd_powercycles_t **cycles) {
  140. // first lets count how many number we're dealing with
  141. size_t count = 0;
  142. size_t len = 0;
  143. for (size_t i = 0;; i++) {
  144. if ((s[i] >= '0' && s[i] <= '9')
  145. || (s[i] >= 'a' && s[i] <= 'f')) {
  146. len = i+1;
  147. count += 1;
  148. } else if ((s[i] >= 'g' && s[i] <= 'v')) {
  149. // do nothing
  150. } else {
  151. break;
  152. }
  153. }
  154. // then parse
  155. lfs_testbd_powercycles_t *cycles_ = malloc(
  156. count * sizeof(lfs_testbd_powercycles_t));
  157. size_t i = 0;
  158. lfs_testbd_powercycles_t x = 0;
  159. size_t k = 0;
  160. for (size_t j = 0; j < len; j++) {
  161. lfs_testbd_powercycles_t nibble = s[j];
  162. nibble = (nibble < 'a') ? nibble-'0' : nibble-'a'+10;
  163. x |= (nibble & 0xf) << (4*k);
  164. k += 1;
  165. if (!(nibble & 0x10)) {
  166. cycles_[i] = x;
  167. i += 1;
  168. x = 0;
  169. k = 0;
  170. }
  171. }
  172. if (tail) {
  173. *tail = (char*)s + len;
  174. }
  175. *cycles = cycles_;
  176. return count;
  177. }
  178. // test state
  179. typedef struct test_powerloss {
  180. char short_name;
  181. const char *long_name;
  182. void (*run)(
  183. const struct test_suite *suite,
  184. const struct test_case *case_,
  185. size_t perm,
  186. const lfs_testbd_powercycles_t *cycles,
  187. size_t cycle_count);
  188. const lfs_testbd_powercycles_t *cycles;
  189. size_t cycle_count;
  190. } test_powerloss_t;
  191. static void run_powerloss_none(
  192. const struct test_suite *suite,
  193. const struct test_case *case_,
  194. size_t perm,
  195. const lfs_testbd_powercycles_t *cycles,
  196. size_t cycle_count);
  197. const test_powerloss_t *test_powerlosses = (const test_powerloss_t[]){
  198. {'0', "none", run_powerloss_none, NULL, 0},
  199. };
  200. size_t test_powerloss_count = 1;
  201. typedef struct test_id {
  202. const char *suite;
  203. const char *case_;
  204. size_t perm;
  205. const lfs_testbd_powercycles_t *cycles;
  206. size_t cycle_count;
  207. } test_id_t;
  208. const test_id_t *test_ids = (const test_id_t[]) {
  209. {NULL, NULL, -1, NULL, 0},
  210. };
  211. size_t test_id_count = 1;
  212. const char *test_geometry = NULL;
  213. size_t test_start = 0;
  214. size_t test_stop = -1;
  215. size_t test_step = 1;
  216. const char *test_disk_path = NULL;
  217. const char *test_trace_path = NULL;
  218. FILE *test_trace_file = NULL;
  219. uint32_t test_trace_cycles = 0;
  220. lfs_testbd_delay_t test_read_delay = 0.0;
  221. lfs_testbd_delay_t test_prog_delay = 0.0;
  222. lfs_testbd_delay_t test_erase_delay = 0.0;
  223. // trace printing
  224. void test_trace(const char *fmt, ...) {
  225. if (test_trace_path) {
  226. if (!test_trace_file) {
  227. // Tracing output is heavy and trying to open every trace
  228. // call is slow, so we only try to open the trace file every
  229. // so often. Note this doesn't affect successfully opened files
  230. if (test_trace_cycles % 128 != 0) {
  231. test_trace_cycles += 1;
  232. return;
  233. }
  234. test_trace_cycles += 1;
  235. int fd;
  236. if (strcmp(test_trace_path, "-") == 0) {
  237. fd = dup(1);
  238. } else {
  239. fd = open(
  240. test_trace_path,
  241. O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK,
  242. 0666);
  243. }
  244. if (fd < 0) {
  245. return;
  246. }
  247. FILE *f = fdopen(fd, "a");
  248. assert(f);
  249. int err = setvbuf(f, NULL, _IOLBF, BUFSIZ);
  250. assert(!err);
  251. test_trace_file = f;
  252. }
  253. va_list va;
  254. va_start(va, fmt);
  255. int res = vfprintf(test_trace_file, fmt, va);
  256. if (res < 0) {
  257. fclose(test_trace_file);
  258. test_trace_file = NULL;
  259. }
  260. va_end(va);
  261. }
  262. }
  263. // how many permutations are there actually in a test case
  264. static void count_perms(
  265. const struct test_suite *suite,
  266. const struct test_case *case_,
  267. size_t perm,
  268. const lfs_testbd_powercycles_t *cycles,
  269. size_t cycle_count,
  270. size_t *perms,
  271. size_t *filtered) {
  272. (void)cycle_count;
  273. size_t perms_ = 0;
  274. size_t filtered_ = 0;
  275. for (size_t p = 0; p < (cycles ? 1 : test_powerloss_count); p++) {
  276. if (!cycles
  277. && test_powerlosses[p].short_name != '0'
  278. && !(case_->flags & TEST_REENTRANT)) {
  279. continue;
  280. }
  281. size_t perm_ = 0;
  282. for (size_t g = 0; g < TEST_GEOMETRY_COUNT; g++) {
  283. if (test_geometry && strcmp(
  284. test_geometries[g].name, test_geometry) != 0) {
  285. continue;
  286. }
  287. for (size_t k = 0; k < case_->permutations; k++) {
  288. perm_ += 1;
  289. if (perm != (size_t)-1 && perm_ != perm) {
  290. continue;
  291. }
  292. perms_ += 1;
  293. // setup defines
  294. define_perm(suite, case_, k);
  295. define_geometry(&test_geometries[g]);
  296. if (case_->filter && !case_->filter()) {
  297. continue;
  298. }
  299. filtered_ += 1;
  300. }
  301. }
  302. }
  303. *perms += perms_;
  304. *filtered += filtered_;
  305. }
  306. // operations we can do
  307. static void summary(void) {
  308. printf("%-36s %7s %7s %7s %11s\n",
  309. "", "flags", "suites", "cases", "perms");
  310. size_t cases = 0;
  311. test_flags_t flags = 0;
  312. size_t perms = 0;
  313. size_t filtered = 0;
  314. for (size_t t = 0; t < test_id_count; t++) {
  315. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  316. if (test_ids[t].suite && strcmp(
  317. test_suites[i].name, test_ids[t].suite) != 0) {
  318. continue;
  319. }
  320. define_suite(&test_suites[i]);
  321. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  322. if (test_ids[t].case_ && strcmp(
  323. test_suites[i].cases[j].name, test_ids[t].case_) != 0) {
  324. continue;
  325. }
  326. count_perms(&test_suites[i], &test_suites[i].cases[j],
  327. test_ids[t].perm,
  328. test_ids[t].cycles,
  329. test_ids[t].cycle_count,
  330. &perms, &filtered);
  331. }
  332. cases += test_suites[i].case_count;
  333. flags |= test_suites[i].flags;
  334. }
  335. }
  336. char perm_buf[64];
  337. sprintf(perm_buf, "%zu/%zu", filtered, perms);
  338. char flag_buf[64];
  339. sprintf(flag_buf, "%s%s",
  340. (flags & TEST_REENTRANT) ? "r" : "",
  341. (!flags) ? "-" : "");
  342. printf("%-36s %7s %7zu %7zu %11s\n",
  343. "TOTAL",
  344. flag_buf,
  345. TEST_SUITE_COUNT,
  346. cases,
  347. perm_buf);
  348. }
  349. static void list_suites(void) {
  350. printf("%-36s %7s %7s %11s\n", "suite", "flags", "cases", "perms");
  351. for (size_t t = 0; t < test_id_count; t++) {
  352. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  353. if (test_ids[t].suite && strcmp(
  354. test_suites[i].name, test_ids[t].suite) != 0) {
  355. continue;
  356. }
  357. define_suite(&test_suites[i]);
  358. size_t perms = 0;
  359. size_t filtered = 0;
  360. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  361. if (test_ids[t].case_ && strcmp(
  362. test_suites[i].cases[j].name, test_ids[t].case_) != 0) {
  363. continue;
  364. }
  365. count_perms(&test_suites[i], &test_suites[i].cases[j],
  366. test_ids[t].perm,
  367. test_ids[t].cycles,
  368. test_ids[t].cycle_count,
  369. &perms, &filtered);
  370. }
  371. char perm_buf[64];
  372. sprintf(perm_buf, "%zu/%zu", filtered, perms);
  373. char flag_buf[64];
  374. sprintf(flag_buf, "%s%s",
  375. (test_suites[i].flags & TEST_REENTRANT) ? "r" : "",
  376. (!test_suites[i].flags) ? "-" : "");
  377. printf("%-36s %7s %7zu %11s\n",
  378. test_suites[i].id,
  379. flag_buf,
  380. test_suites[i].case_count,
  381. perm_buf);
  382. }
  383. }
  384. }
  385. static void list_cases(void) {
  386. printf("%-36s %7s %11s\n", "case", "flags", "perms");
  387. for (size_t t = 0; t < test_id_count; t++) {
  388. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  389. if (test_ids[t].suite && strcmp(
  390. test_suites[i].name, test_ids[t].suite) != 0) {
  391. continue;
  392. }
  393. define_suite(&test_suites[i]);
  394. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  395. if (test_ids[t].case_ && strcmp(
  396. test_suites[i].cases[j].name, test_ids[t].case_) != 0) {
  397. continue;
  398. }
  399. size_t perms = 0;
  400. size_t filtered = 0;
  401. count_perms(&test_suites[i], &test_suites[i].cases[j],
  402. test_ids[t].perm,
  403. test_ids[t].cycles,
  404. test_ids[t].cycle_count,
  405. &perms, &filtered);
  406. char perm_buf[64];
  407. sprintf(perm_buf, "%zu/%zu", filtered, perms);
  408. char flag_buf[64];
  409. sprintf(flag_buf, "%s%s",
  410. (test_suites[i].cases[j].flags & TEST_REENTRANT)
  411. ? "r" : "",
  412. (!test_suites[i].cases[j].flags)
  413. ? "-" : "");
  414. printf("%-36s %7s %11s\n",
  415. test_suites[i].cases[j].id,
  416. flag_buf,
  417. perm_buf);
  418. }
  419. }
  420. }
  421. }
  422. static void list_paths(void) {
  423. for (size_t t = 0; t < test_id_count; t++) {
  424. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  425. if (test_ids[t].suite && strcmp(
  426. test_suites[i].name, test_ids[t].suite) != 0) {
  427. continue;
  428. }
  429. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  430. if (test_ids[t].case_ && strcmp(
  431. test_suites[i].cases[j].name, test_ids[t].case_) != 0) {
  432. continue;
  433. }
  434. printf("%-36s %-36s\n",
  435. test_suites[i].cases[j].id,
  436. test_suites[i].cases[j].path);
  437. }
  438. }
  439. }
  440. }
  441. static void list_defines(void) {
  442. for (size_t t = 0; t < test_id_count; t++) {
  443. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  444. if (test_ids[t].suite && strcmp(
  445. test_suites[i].name, test_ids[t].suite) != 0) {
  446. continue;
  447. }
  448. define_suite(&test_suites[i]);
  449. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  450. if (test_ids[t].case_ && strcmp(
  451. test_suites[i].cases[j].name, test_ids[t].case_) != 0) {
  452. continue;
  453. }
  454. for (size_t p = 0;
  455. p < (test_ids[t].cycles ? 1 : test_powerloss_count);
  456. p++) {
  457. if (!test_ids[t].cycles
  458. && test_powerlosses[p].short_name != '0'
  459. && !(test_suites[i].cases[j].flags
  460. & TEST_REENTRANT)) {
  461. continue;
  462. }
  463. size_t perm_ = 0;
  464. for (size_t g = 0; g < TEST_GEOMETRY_COUNT; g++) {
  465. if (test_geometry && strcmp(
  466. test_geometries[g].name, test_geometry) != 0) {
  467. continue;
  468. }
  469. for (size_t k = 0;
  470. k < test_suites[i].cases[j].permutations;
  471. k++) {
  472. perm_ += 1;
  473. if (test_ids[t].perm != (size_t)-1
  474. && perm_ != test_ids[t].perm) {
  475. continue;
  476. }
  477. // setup defines
  478. define_perm(&test_suites[i],
  479. &test_suites[i].cases[j],
  480. k);
  481. define_geometry(&test_geometries[g]);
  482. // print the case
  483. char id_buf[256];
  484. sprintf(id_buf, "%s#%zu",
  485. test_suites[i].cases[j].id, perm_);
  486. printf("%-36s ", id_buf);
  487. // special case for the current geometry
  488. printf("GEOMETRY=%s ", test_geometries[g].name);
  489. // print each define
  490. for (size_t l = 0;
  491. l < test_suites[i].define_count;
  492. l++) {
  493. if (test_suites[i].cases[j].defines
  494. && test_suites[i].cases[j]
  495. .defines[k][l]) {
  496. printf("%s=%jd ",
  497. test_suites[i].define_names[l],
  498. test_define(l));
  499. }
  500. }
  501. printf("\n");
  502. }
  503. }
  504. }
  505. }
  506. }
  507. }
  508. }
  509. static void list_geometries(void) {
  510. for (size_t i = 0; i < TEST_GEOMETRY_COUNT; i++) {
  511. if (test_geometry && strcmp(
  512. test_geometries[i].name,
  513. test_geometry) != 0) {
  514. continue;
  515. }
  516. define_geometry(&test_geometries[i]);
  517. printf("%-36s ", test_geometries[i].name);
  518. // print each define
  519. for (size_t k = 0; k < TEST_GEOMETRY_DEFINE_COUNT; k++) {
  520. printf("%s=%jd ",
  521. test_predefine_names[k],
  522. test_predefine(k));
  523. }
  524. printf("\n");
  525. }
  526. }
  527. static void list_defaults(void) {
  528. printf("%-36s ", "defaults");
  529. // print each define
  530. for (size_t k = 0; k < TEST_DEFAULT_DEFINE_COUNT; k++) {
  531. printf("%s=%jd ",
  532. test_predefine_names[k+TEST_GEOMETRY_DEFINE_COUNT],
  533. test_predefine(k+TEST_GEOMETRY_DEFINE_COUNT));
  534. }
  535. printf("\n");
  536. }
  537. // scenarios to run tests under power-loss
  538. static void run_powerloss_none(
  539. const struct test_suite *suite,
  540. const struct test_case *case_,
  541. size_t perm,
  542. const lfs_testbd_powercycles_t *cycles,
  543. size_t cycle_count) {
  544. (void)cycles;
  545. (void)cycle_count;
  546. (void)suite;
  547. // create block device and configuration
  548. lfs_testbd_t bd;
  549. struct lfs_config cfg = {
  550. .context = &bd,
  551. .read = lfs_testbd_read,
  552. .prog = lfs_testbd_prog,
  553. .erase = lfs_testbd_erase,
  554. .sync = lfs_testbd_sync,
  555. .read_size = READ_SIZE,
  556. .prog_size = PROG_SIZE,
  557. .block_size = BLOCK_SIZE,
  558. .block_count = BLOCK_COUNT,
  559. .block_cycles = BLOCK_CYCLES,
  560. .cache_size = CACHE_SIZE,
  561. .lookahead_size = LOOKAHEAD_SIZE,
  562. };
  563. struct lfs_testbd_config bdcfg = {
  564. .erase_value = ERASE_VALUE,
  565. .erase_cycles = ERASE_CYCLES,
  566. .badblock_behavior = BADBLOCK_BEHAVIOR,
  567. .disk_path = test_disk_path,
  568. .read_delay = test_read_delay,
  569. .prog_delay = test_prog_delay,
  570. .erase_delay = test_erase_delay,
  571. };
  572. int err = lfs_testbd_createcfg(&cfg, test_disk_path, &bdcfg);
  573. if (err) {
  574. fprintf(stderr, "error: could not create block device: %d\n", err);
  575. exit(-1);
  576. }
  577. // run the test
  578. printf("running %s#%zu\n", case_->id, perm);
  579. case_->run(&cfg);
  580. printf("finished %s#%zu\n", case_->id, perm);
  581. // cleanup
  582. err = lfs_testbd_destroy(&cfg);
  583. if (err) {
  584. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  585. exit(-1);
  586. }
  587. }
  588. static void powerloss_longjmp(void *c) {
  589. jmp_buf *powerloss_jmp = c;
  590. longjmp(*powerloss_jmp, 1);
  591. }
  592. static void run_powerloss_linear(
  593. const struct test_suite *suite,
  594. const struct test_case *case_,
  595. size_t perm,
  596. const lfs_testbd_powercycles_t *cycles,
  597. size_t cycle_count) {
  598. (void)cycles;
  599. (void)cycle_count;
  600. (void)suite;
  601. // create block device and configuration
  602. lfs_testbd_t bd;
  603. jmp_buf powerloss_jmp;
  604. volatile lfs_testbd_powercycles_t i = 1;
  605. struct lfs_config cfg = {
  606. .context = &bd,
  607. .read = lfs_testbd_read,
  608. .prog = lfs_testbd_prog,
  609. .erase = lfs_testbd_erase,
  610. .sync = lfs_testbd_sync,
  611. .read_size = READ_SIZE,
  612. .prog_size = PROG_SIZE,
  613. .block_size = BLOCK_SIZE,
  614. .block_count = BLOCK_COUNT,
  615. .block_cycles = BLOCK_CYCLES,
  616. .cache_size = CACHE_SIZE,
  617. .lookahead_size = LOOKAHEAD_SIZE,
  618. };
  619. struct lfs_testbd_config bdcfg = {
  620. .erase_value = ERASE_VALUE,
  621. .erase_cycles = ERASE_CYCLES,
  622. .badblock_behavior = BADBLOCK_BEHAVIOR,
  623. .disk_path = test_disk_path,
  624. .read_delay = test_read_delay,
  625. .prog_delay = test_prog_delay,
  626. .erase_delay = test_erase_delay,
  627. .power_cycles = i,
  628. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  629. .powerloss_cb = powerloss_longjmp,
  630. .powerloss_data = &powerloss_jmp,
  631. };
  632. int err = lfs_testbd_createcfg(&cfg, test_disk_path, &bdcfg);
  633. if (err) {
  634. fprintf(stderr, "error: could not create block device: %d\n", err);
  635. exit(-1);
  636. }
  637. // run the test, increasing power-cycles as power-loss events occur
  638. printf("running %s#%zu\n", case_->id, perm);
  639. while (true) {
  640. if (!setjmp(powerloss_jmp)) {
  641. // run the test
  642. case_->run(&cfg);
  643. break;
  644. }
  645. // power-loss!
  646. printf("powerloss %s#%zu#", case_->id, perm);
  647. for (lfs_testbd_powercycles_t j = 1; j <= i; j++) {
  648. leb16_print(&j, 1);
  649. }
  650. printf("\n");
  651. i += 1;
  652. lfs_testbd_setpowercycles(&cfg, i);
  653. }
  654. printf("finished %s#%zu\n", case_->id, perm);
  655. // cleanup
  656. err = lfs_testbd_destroy(&cfg);
  657. if (err) {
  658. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  659. exit(-1);
  660. }
  661. }
  662. static void run_powerloss_exponential(
  663. const struct test_suite *suite,
  664. const struct test_case *case_,
  665. size_t perm,
  666. const lfs_testbd_powercycles_t *cycles,
  667. size_t cycle_count) {
  668. (void)cycles;
  669. (void)cycle_count;
  670. (void)suite;
  671. // create block device and configuration
  672. lfs_testbd_t bd;
  673. jmp_buf powerloss_jmp;
  674. volatile lfs_testbd_powercycles_t i = 1;
  675. struct lfs_config cfg = {
  676. .context = &bd,
  677. .read = lfs_testbd_read,
  678. .prog = lfs_testbd_prog,
  679. .erase = lfs_testbd_erase,
  680. .sync = lfs_testbd_sync,
  681. .read_size = READ_SIZE,
  682. .prog_size = PROG_SIZE,
  683. .block_size = BLOCK_SIZE,
  684. .block_count = BLOCK_COUNT,
  685. .block_cycles = BLOCK_CYCLES,
  686. .cache_size = CACHE_SIZE,
  687. .lookahead_size = LOOKAHEAD_SIZE,
  688. };
  689. struct lfs_testbd_config bdcfg = {
  690. .erase_value = ERASE_VALUE,
  691. .erase_cycles = ERASE_CYCLES,
  692. .badblock_behavior = BADBLOCK_BEHAVIOR,
  693. .disk_path = test_disk_path,
  694. .read_delay = test_read_delay,
  695. .prog_delay = test_prog_delay,
  696. .erase_delay = test_erase_delay,
  697. .power_cycles = i,
  698. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  699. .powerloss_cb = powerloss_longjmp,
  700. .powerloss_data = &powerloss_jmp,
  701. };
  702. int err = lfs_testbd_createcfg(&cfg, test_disk_path, &bdcfg);
  703. if (err) {
  704. fprintf(stderr, "error: could not create block device: %d\n", err);
  705. exit(-1);
  706. }
  707. // run the test, increasing power-cycles as power-loss events occur
  708. printf("running %s#%zu\n", case_->id, perm);
  709. while (true) {
  710. if (!setjmp(powerloss_jmp)) {
  711. // run the test
  712. case_->run(&cfg);
  713. break;
  714. }
  715. // power-loss!
  716. printf("powerloss %s#%zu#", case_->id, perm);
  717. for (lfs_testbd_powercycles_t j = 1; j <= i; j *= 2) {
  718. leb16_print(&j, 1);
  719. }
  720. printf("\n");
  721. i *= 2;
  722. lfs_testbd_setpowercycles(&cfg, i);
  723. }
  724. printf("finished %s#%zu\n", case_->id, perm);
  725. // cleanup
  726. err = lfs_testbd_destroy(&cfg);
  727. if (err) {
  728. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  729. exit(-1);
  730. }
  731. }
  732. static void run_powerloss_cycles(
  733. const struct test_suite *suite,
  734. const struct test_case *case_,
  735. size_t perm,
  736. const lfs_testbd_powercycles_t *cycles,
  737. size_t cycle_count) {
  738. (void)suite;
  739. // create block device and configuration
  740. lfs_testbd_t bd;
  741. jmp_buf powerloss_jmp;
  742. volatile size_t i = 0;
  743. struct lfs_config cfg = {
  744. .context = &bd,
  745. .read = lfs_testbd_read,
  746. .prog = lfs_testbd_prog,
  747. .erase = lfs_testbd_erase,
  748. .sync = lfs_testbd_sync,
  749. .read_size = READ_SIZE,
  750. .prog_size = PROG_SIZE,
  751. .block_size = BLOCK_SIZE,
  752. .block_count = BLOCK_COUNT,
  753. .block_cycles = BLOCK_CYCLES,
  754. .cache_size = CACHE_SIZE,
  755. .lookahead_size = LOOKAHEAD_SIZE,
  756. };
  757. struct lfs_testbd_config bdcfg = {
  758. .erase_value = ERASE_VALUE,
  759. .erase_cycles = ERASE_CYCLES,
  760. .badblock_behavior = BADBLOCK_BEHAVIOR,
  761. .disk_path = test_disk_path,
  762. .read_delay = test_read_delay,
  763. .prog_delay = test_prog_delay,
  764. .erase_delay = test_erase_delay,
  765. .power_cycles = (i < cycle_count) ? cycles[i] : 0,
  766. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  767. .powerloss_cb = powerloss_longjmp,
  768. .powerloss_data = &powerloss_jmp,
  769. };
  770. int err = lfs_testbd_createcfg(&cfg, test_disk_path, &bdcfg);
  771. if (err) {
  772. fprintf(stderr, "error: could not create block device: %d\n", err);
  773. exit(-1);
  774. }
  775. // run the test, increasing power-cycles as power-loss events occur
  776. printf("running %s#%zu\n", case_->id, perm);
  777. while (true) {
  778. if (!setjmp(powerloss_jmp)) {
  779. // run the test
  780. case_->run(&cfg);
  781. break;
  782. }
  783. // power-loss!
  784. assert(i <= cycle_count);
  785. printf("powerloss %s#%zu#", case_->id, perm);
  786. leb16_print(cycles, i+1);
  787. printf("\n");
  788. i += 1;
  789. lfs_testbd_setpowercycles(&cfg,
  790. (i < cycle_count) ? cycles[i] : 0);
  791. }
  792. printf("finished %s#%zu\n", case_->id, perm);
  793. // cleanup
  794. err = lfs_testbd_destroy(&cfg);
  795. if (err) {
  796. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  797. exit(-1);
  798. }
  799. }
  800. struct powerloss_exhaustive_state {
  801. struct lfs_config *cfg;
  802. lfs_testbd_t *branches;
  803. size_t branch_count;
  804. size_t branch_capacity;
  805. };
  806. struct powerloss_exhaustive_cycles {
  807. lfs_testbd_powercycles_t *cycles;
  808. size_t cycle_count;
  809. size_t cycle_capacity;
  810. };
  811. static void powerloss_exhaustive_branch(void *c) {
  812. // append to branches
  813. struct powerloss_exhaustive_state *state = c;
  814. state->branch_count += 1;
  815. if (state->branch_count > state->branch_capacity) {
  816. state->branch_capacity = (2*state->branch_capacity > 4)
  817. ? 2*state->branch_capacity
  818. : 4;
  819. state->branches = realloc(state->branches,
  820. state->branch_capacity * sizeof(lfs_testbd_t));
  821. if (!state->branches) {
  822. fprintf(stderr, "error: exhaustive: out of memory\n");
  823. exit(-1);
  824. }
  825. }
  826. // create copy-on-write copy
  827. int err = lfs_testbd_copy(state->cfg,
  828. &state->branches[state->branch_count-1]);
  829. if (err) {
  830. fprintf(stderr, "error: exhaustive: could not create bd copy\n");
  831. exit(-1);
  832. }
  833. // also trigger on next power cycle
  834. lfs_testbd_setpowercycles(state->cfg, 1);
  835. }
  836. static void run_powerloss_exhaustive_layer(
  837. const struct test_suite *suite,
  838. const struct test_case *case_,
  839. size_t perm,
  840. struct lfs_config *cfg,
  841. struct lfs_testbd_config *bdcfg,
  842. size_t depth,
  843. struct powerloss_exhaustive_cycles *cycles) {
  844. (void)suite;
  845. struct powerloss_exhaustive_state state = {
  846. .cfg = cfg,
  847. .branches = NULL,
  848. .branch_count = 0,
  849. .branch_capacity = 0,
  850. };
  851. // run through the test without additional powerlosses, collecting possible
  852. // branches as we do so
  853. lfs_testbd_setpowercycles(state.cfg, depth > 0 ? 1 : 0);
  854. bdcfg->powerloss_data = &state;
  855. // run the tests
  856. case_->run(cfg);
  857. // aggressively clean up memory here to try to keep our memory usage low
  858. int err = lfs_testbd_destroy(cfg);
  859. if (err) {
  860. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  861. exit(-1);
  862. }
  863. // recurse into each branch
  864. for (size_t i = 0; i < state.branch_count; i++) {
  865. // first push and print the branch
  866. cycles->cycle_count += 1;
  867. if (cycles->cycle_count > cycles->cycle_capacity) {
  868. cycles->cycle_capacity = (2*cycles->cycle_capacity > 4)
  869. ? 2*cycles->cycle_capacity
  870. : 4;
  871. cycles->cycles = realloc(cycles->cycles,
  872. cycles->cycle_capacity * sizeof(lfs_testbd_powercycles_t));
  873. if (!cycles->cycles) {
  874. fprintf(stderr, "error: exhaustive: out of memory\n");
  875. exit(-1);
  876. }
  877. }
  878. cycles->cycles[cycles->cycle_count-1] = i;
  879. printf("powerloss %s#%zu#", case_->id, perm);
  880. leb16_print(cycles->cycles, cycles->cycle_count);
  881. printf("\n");
  882. // now recurse
  883. cfg->context = &state.branches[i];
  884. run_powerloss_exhaustive_layer(suite, case_, perm,
  885. cfg, bdcfg, depth-1, cycles);
  886. // pop the cycle
  887. cycles->cycle_count -= 1;
  888. }
  889. // clean up memory
  890. free(state.branches);
  891. }
  892. static void run_powerloss_exhaustive(
  893. const struct test_suite *suite,
  894. const struct test_case *case_,
  895. size_t perm,
  896. const lfs_testbd_powercycles_t *cycles,
  897. size_t cycle_count) {
  898. (void)cycles;
  899. (void)suite;
  900. // create block device and configuration
  901. lfs_testbd_t bd;
  902. struct lfs_config cfg = {
  903. .context = &bd,
  904. .read = lfs_testbd_read,
  905. .prog = lfs_testbd_prog,
  906. .erase = lfs_testbd_erase,
  907. .sync = lfs_testbd_sync,
  908. .read_size = READ_SIZE,
  909. .prog_size = PROG_SIZE,
  910. .block_size = BLOCK_SIZE,
  911. .block_count = BLOCK_COUNT,
  912. .block_cycles = BLOCK_CYCLES,
  913. .cache_size = CACHE_SIZE,
  914. .lookahead_size = LOOKAHEAD_SIZE,
  915. };
  916. struct lfs_testbd_config bdcfg = {
  917. .erase_value = ERASE_VALUE,
  918. .erase_cycles = ERASE_CYCLES,
  919. .badblock_behavior = BADBLOCK_BEHAVIOR,
  920. .disk_path = test_disk_path,
  921. .read_delay = test_read_delay,
  922. .prog_delay = test_prog_delay,
  923. .erase_delay = test_erase_delay,
  924. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  925. .powerloss_cb = powerloss_exhaustive_branch,
  926. .powerloss_data = NULL,
  927. };
  928. int err = lfs_testbd_createcfg(&cfg, test_disk_path, &bdcfg);
  929. if (err) {
  930. fprintf(stderr, "error: could not create block device: %d\n", err);
  931. exit(-1);
  932. }
  933. // run the test, increasing power-cycles as power-loss events occur
  934. printf("running %s#%zu\n", case_->id, perm);
  935. // recursively exhaust each layer of powerlosses
  936. run_powerloss_exhaustive_layer(suite, case_, perm,
  937. &cfg, &bdcfg, cycle_count,
  938. &(struct powerloss_exhaustive_cycles){NULL, 0, 0});
  939. printf("finished %s#%zu\n", case_->id, perm);
  940. }
  941. const test_powerloss_t builtin_powerlosses[] = {
  942. {'0', "none", run_powerloss_none, NULL, 0},
  943. {'e', "exponential", run_powerloss_exponential, NULL, 0},
  944. {'l', "linear", run_powerloss_linear, NULL, 0},
  945. {'x', "exhaustive", run_powerloss_exhaustive, NULL, SIZE_MAX},
  946. {0, NULL, NULL, NULL, 0},
  947. };
  948. const char *const builtin_powerlosses_help[] = {
  949. "Run with no power-losses.",
  950. "Run with linearly-decreasing power-losses.",
  951. "Run with exponentially-decreasing power-losses.",
  952. "Run a all permutations of power-losses, this may take a while.",
  953. "Run a all permutations of n power-losses.",
  954. "Run a custom comma-separated set of power-losses.",
  955. "Run a custom leb16-encoded set of power-losses.",
  956. };
  957. static void list_powerlosses(void) {
  958. printf("%-24s %s\n", "scenario", "description");
  959. size_t i = 0;
  960. for (; builtin_powerlosses[i].long_name; i++) {
  961. printf("%c,%-22s %s\n",
  962. builtin_powerlosses[i].short_name,
  963. builtin_powerlosses[i].long_name,
  964. builtin_powerlosses_help[i]);
  965. }
  966. // a couple more options with special parsing
  967. printf("%-24s %s\n", "1,2,3", builtin_powerlosses_help[i+0]);
  968. printf("%-24s %s\n", "{1,2,3}", builtin_powerlosses_help[i+1]);
  969. printf("%-24s %s\n", "#1248g1", builtin_powerlosses_help[i+2]);
  970. }
  971. // global test step count
  972. static size_t step = 0;
  973. // run the tests
  974. static void run_perms(
  975. const struct test_suite *suite,
  976. const struct test_case *case_,
  977. size_t perm,
  978. const lfs_testbd_powercycles_t *cycles,
  979. size_t cycle_count) {
  980. for (size_t p = 0; p < (cycles ? 1 : test_powerloss_count); p++) {
  981. if (!cycles
  982. && test_powerlosses[p].short_name != '0'
  983. && !(case_->flags & TEST_REENTRANT)) {
  984. continue;
  985. }
  986. size_t perm_ = 0;
  987. for (size_t g = 0; g < TEST_GEOMETRY_COUNT; g++) {
  988. if (test_geometry && strcmp(
  989. test_geometries[g].name, test_geometry) != 0) {
  990. continue;
  991. }
  992. for (size_t k = 0; k < case_->permutations; k++) {
  993. perm_ += 1;
  994. if (perm != (size_t)-1 && perm_ != perm) {
  995. continue;
  996. }
  997. if (!(step >= test_start
  998. && step < test_stop
  999. && (step-test_start) % test_step == 0)) {
  1000. step += 1;
  1001. continue;
  1002. }
  1003. step += 1;
  1004. // setup defines
  1005. define_perm(suite, case_, k);
  1006. define_geometry(&test_geometries[g]);
  1007. // filter?
  1008. if (case_->filter && !case_->filter()) {
  1009. printf("skipped %s#%zu\n", case_->id, perm_);
  1010. continue;
  1011. }
  1012. if (cycles) {
  1013. run_powerloss_cycles(
  1014. suite, case_, perm_,
  1015. cycles,
  1016. cycle_count);
  1017. } else {
  1018. test_powerlosses[p].run(
  1019. suite, case_, perm_,
  1020. test_powerlosses[p].cycles,
  1021. test_powerlosses[p].cycle_count);
  1022. }
  1023. }
  1024. }
  1025. }
  1026. }
  1027. static void run(void) {
  1028. // ignore disconnected pipes
  1029. signal(SIGPIPE, SIG_IGN);
  1030. for (size_t t = 0; t < test_id_count; t++) {
  1031. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  1032. if (test_ids[t].suite && strcmp(
  1033. test_suites[i].name, test_ids[t].suite) != 0) {
  1034. continue;
  1035. }
  1036. define_suite(&test_suites[i]);
  1037. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  1038. if (test_ids[t].case_ && strcmp(
  1039. test_suites[i].cases[j].name, test_ids[t].case_) != 0) {
  1040. continue;
  1041. }
  1042. run_perms(&test_suites[i], &test_suites[i].cases[j],
  1043. test_ids[t].perm,
  1044. test_ids[t].cycles,
  1045. test_ids[t].cycle_count);
  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_PATHS = 1,
  1057. OPT_LIST_DEFINES = 2,
  1058. OPT_LIST_GEOMETRIES = 3,
  1059. OPT_LIST_DEFAULTS = 4,
  1060. OPT_LIST_POWERLOSSES = 5,
  1061. OPT_DEFINE = 'D',
  1062. OPT_GEOMETRY = 'G',
  1063. OPT_POWERLOSS = 'p',
  1064. OPT_START = 6,
  1065. OPT_STEP = 7,
  1066. OPT_STOP = 8,
  1067. OPT_DISK = 'd',
  1068. OPT_TRACE = 't',
  1069. OPT_READ_DELAY = 9,
  1070. OPT_PROG_DELAY = 10,
  1071. OPT_ERASE_DELAY = 11,
  1072. };
  1073. const char *short_opts = "hYlLD:G:p:nrVd:t:";
  1074. const struct option long_opts[] = {
  1075. {"help", no_argument, NULL, OPT_HELP},
  1076. {"summary", no_argument, NULL, OPT_SUMMARY},
  1077. {"list-suites", no_argument, NULL, OPT_LIST_SUITES},
  1078. {"list-cases", no_argument, NULL, OPT_LIST_CASES},
  1079. {"list-paths", no_argument, NULL, OPT_LIST_PATHS},
  1080. {"list-defines", no_argument, NULL, OPT_LIST_DEFINES},
  1081. {"list-geometries", no_argument, NULL, OPT_LIST_GEOMETRIES},
  1082. {"list-defaults", no_argument, NULL, OPT_LIST_DEFAULTS},
  1083. {"list-powerlosses", no_argument, NULL, OPT_LIST_POWERLOSSES},
  1084. {"define", required_argument, NULL, OPT_DEFINE},
  1085. {"geometry", required_argument, NULL, OPT_GEOMETRY},
  1086. {"powerloss", required_argument, NULL, OPT_POWERLOSS},
  1087. {"start", required_argument, NULL, OPT_START},
  1088. {"stop", required_argument, NULL, OPT_STOP},
  1089. {"step", required_argument, NULL, OPT_STEP},
  1090. {"disk", required_argument, NULL, OPT_DISK},
  1091. {"trace", required_argument, NULL, OPT_TRACE},
  1092. {"read-delay", required_argument, NULL, OPT_READ_DELAY},
  1093. {"prog-delay", required_argument, NULL, OPT_PROG_DELAY},
  1094. {"erase-delay", required_argument, NULL, OPT_ERASE_DELAY},
  1095. {NULL, 0, NULL, 0},
  1096. };
  1097. const char *const help_text[] = {
  1098. "Show this help message.",
  1099. "Show quick summary.",
  1100. "List test suites.",
  1101. "List test cases.",
  1102. "List the path for each test case.",
  1103. "List the defines for each test permutation.",
  1104. "List the disk geometries used for testing.",
  1105. "List the default defines in this test-runner.",
  1106. "List the available power-loss scenarios.",
  1107. "Override a test define.",
  1108. "Filter by geometry.",
  1109. "Comma-separated list of power-loss scenarios to test. Defaults to 0,l.",
  1110. "Start at the nth test.",
  1111. "Stop before the nth test.",
  1112. "Only run every n tests, calculated after --start and --stop.",
  1113. "Redirect block device operations to this file.",
  1114. "Redirect trace output to this file.",
  1115. "Artificial read delay in seconds.",
  1116. "Artificial prog delay in seconds.",
  1117. "Artificial erase delay in seconds.",
  1118. };
  1119. int main(int argc, char **argv) {
  1120. void (*op)(void) = run;
  1121. const char **override_names = NULL;
  1122. intmax_t *override_defines = NULL;
  1123. size_t override_count = 0;
  1124. size_t override_capacity = 0;
  1125. size_t test_powerloss_capacity = 0;
  1126. size_t test_id_capacity = 0;
  1127. // parse options
  1128. while (true) {
  1129. int c = getopt_long(argc, argv, short_opts, long_opts, NULL);
  1130. switch (c) {
  1131. // generate help message
  1132. case OPT_HELP: {
  1133. printf("usage: %s [options] [test_id]\n", argv[0]);
  1134. printf("\n");
  1135. printf("options:\n");
  1136. size_t i = 0;
  1137. while (long_opts[i].name) {
  1138. size_t indent;
  1139. if (long_opts[i].has_arg == no_argument) {
  1140. if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
  1141. indent = printf(" -%c, --%s ",
  1142. long_opts[i].val,
  1143. long_opts[i].name);
  1144. } else {
  1145. indent = printf(" --%s ",
  1146. long_opts[i].name);
  1147. }
  1148. } else {
  1149. if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
  1150. indent = printf(" -%c %s, --%s %s ",
  1151. long_opts[i].val,
  1152. long_opts[i].name,
  1153. long_opts[i].name,
  1154. long_opts[i].name);
  1155. } else {
  1156. indent = printf(" --%s %s ",
  1157. long_opts[i].name,
  1158. long_opts[i].name);
  1159. }
  1160. }
  1161. // a quick, hacky, byte-level method for text wrapping
  1162. size_t len = strlen(help_text[i]);
  1163. size_t j = 0;
  1164. if (indent < 24) {
  1165. printf("%*s %.80s\n",
  1166. (int)(24-1-indent),
  1167. "",
  1168. &help_text[i][j]);
  1169. j += 80;
  1170. } else {
  1171. printf("\n");
  1172. }
  1173. while (j < len) {
  1174. printf("%24s%.80s\n", "", &help_text[i][j]);
  1175. j += 80;
  1176. }
  1177. i += 1;
  1178. }
  1179. printf("\n");
  1180. exit(0);
  1181. }
  1182. // summary/list flags
  1183. case OPT_SUMMARY:
  1184. op = summary;
  1185. break;
  1186. case OPT_LIST_SUITES:
  1187. op = list_suites;
  1188. break;
  1189. case OPT_LIST_CASES:
  1190. op = list_cases;
  1191. break;
  1192. case OPT_LIST_PATHS:
  1193. op = list_paths;
  1194. break;
  1195. case OPT_LIST_DEFINES:
  1196. op = list_defines;
  1197. break;
  1198. case OPT_LIST_GEOMETRIES:
  1199. op = list_geometries;
  1200. break;
  1201. case OPT_LIST_DEFAULTS:
  1202. op = list_defaults;
  1203. break;
  1204. case OPT_LIST_POWERLOSSES:
  1205. op = list_powerlosses;
  1206. break;
  1207. // configuration
  1208. case OPT_DEFINE: {
  1209. // special case for -DGEOMETRY=<name>, we treat this the same
  1210. // as --geometry=<name>
  1211. if (strncmp(optarg, "GEOMETRY=", strlen("GEOMETRY=")) == 0) {
  1212. test_geometry = &optarg[strlen("GEOMETRY=")];
  1213. break;
  1214. }
  1215. // realloc if necessary
  1216. override_count += 1;
  1217. if (override_count > override_capacity) {
  1218. override_capacity = (2*override_capacity > 4)
  1219. ? 2*override_capacity
  1220. : 4;
  1221. override_names = realloc(override_names,
  1222. override_capacity * sizeof(const char *));
  1223. override_defines = realloc(override_defines,
  1224. override_capacity * sizeof(intmax_t));
  1225. }
  1226. // parse into string key/intmax_t value, cannibalizing the
  1227. // arg in the process
  1228. char *sep = strchr(optarg, '=');
  1229. char *parsed = NULL;
  1230. if (!sep) {
  1231. goto invalid_define;
  1232. }
  1233. override_defines[override_count-1]
  1234. = strtoumax(sep+1, &parsed, 0);
  1235. if (parsed == sep+1) {
  1236. goto invalid_define;
  1237. }
  1238. override_names[override_count-1] = optarg;
  1239. *sep = '\0';
  1240. break;
  1241. invalid_define:
  1242. fprintf(stderr, "error: invalid define: %s\n", optarg);
  1243. exit(-1);
  1244. }
  1245. case OPT_GEOMETRY:
  1246. test_geometry = optarg;
  1247. break;
  1248. case OPT_POWERLOSS: {
  1249. // reset our powerloss scenarios
  1250. if (test_powerloss_capacity > 0) {
  1251. free((test_powerloss_t*)test_powerlosses);
  1252. }
  1253. test_powerlosses = NULL;
  1254. test_powerloss_count = 0;
  1255. test_powerloss_capacity = 0;
  1256. // parse the comma separated list of power-loss scenarios
  1257. while (*optarg) {
  1258. // allocate space
  1259. test_powerloss_count += 1;
  1260. if (test_powerloss_count > test_powerloss_capacity) {
  1261. test_powerloss_capacity
  1262. = (2*test_powerloss_capacity > 4)
  1263. ? 2*test_powerloss_capacity
  1264. : 4;
  1265. test_powerlosses = realloc(
  1266. (test_powerloss_t*)test_powerlosses,
  1267. test_powerloss_capacity
  1268. * sizeof(test_powerloss_t));
  1269. }
  1270. // parse the power-loss scenario
  1271. optarg += strspn(optarg, " ");
  1272. // named power-loss scenario
  1273. size_t len = strcspn(optarg, " ,");
  1274. for (size_t i = 0; builtin_powerlosses[i].long_name; i++) {
  1275. if ((len == 1
  1276. && *optarg == builtin_powerlosses[i].short_name)
  1277. || (len == strlen(
  1278. builtin_powerlosses[i].long_name)
  1279. && memcmp(optarg,
  1280. builtin_powerlosses[i].long_name,
  1281. len) == 0)) {
  1282. ((test_powerloss_t*)test_powerlosses)[
  1283. test_powerloss_count-1]
  1284. = builtin_powerlosses[i];
  1285. optarg += len;
  1286. goto powerloss_next;
  1287. }
  1288. }
  1289. // comma-separated permutation
  1290. if (*optarg == '{') {
  1291. // how many cycles?
  1292. size_t count = 1;
  1293. for (size_t i = 0; optarg[i]; i++) {
  1294. if (optarg[i] == ',') {
  1295. count += 1;
  1296. }
  1297. }
  1298. // parse cycles
  1299. lfs_testbd_powercycles_t *cycles = malloc(
  1300. count * sizeof(lfs_testbd_powercycles_t));
  1301. size_t i = 0;
  1302. char *s = optarg + 1;
  1303. while (true) {
  1304. char *parsed = NULL;
  1305. cycles[i] = strtoumax(s, &parsed, 0);
  1306. if (parsed == s) {
  1307. count -= 1;
  1308. i -= 1;
  1309. }
  1310. i += 1;
  1311. s = parsed + strspn(parsed, " ");
  1312. if (*s == ',') {
  1313. s += 1;
  1314. continue;
  1315. } else if (*s == '}') {
  1316. s += 1;
  1317. break;
  1318. } else {
  1319. goto powerloss_unknown;
  1320. }
  1321. }
  1322. ((test_powerloss_t*)test_powerlosses)[
  1323. test_powerloss_count-1] = (test_powerloss_t){
  1324. .run = run_powerloss_cycles,
  1325. .cycles = cycles,
  1326. .cycle_count = count,
  1327. };
  1328. optarg = s;
  1329. goto powerloss_next;
  1330. }
  1331. // leb16-encoded permutation
  1332. if (*optarg == '#') {
  1333. lfs_testbd_powercycles_t *cycles;
  1334. char *parsed = NULL;
  1335. size_t count = leb16_parse(optarg+1, &parsed, &cycles);
  1336. if (parsed == optarg+1) {
  1337. goto powerloss_unknown;
  1338. }
  1339. ((test_powerloss_t*)test_powerlosses)[
  1340. test_powerloss_count-1] = (test_powerloss_t){
  1341. .run = run_powerloss_cycles,
  1342. .cycles = cycles,
  1343. .cycle_count = count,
  1344. };
  1345. optarg = (char*)parsed;
  1346. goto powerloss_next;
  1347. }
  1348. // exhaustive permutations
  1349. {
  1350. char *parsed = NULL;
  1351. size_t count = strtoumax(optarg, &parsed, 0);
  1352. if (parsed == optarg) {
  1353. goto powerloss_unknown;
  1354. }
  1355. ((test_powerloss_t*)test_powerlosses)[
  1356. test_powerloss_count-1] = (test_powerloss_t){
  1357. .run = run_powerloss_exhaustive,
  1358. .cycles = NULL,
  1359. .cycle_count = count,
  1360. };
  1361. optarg = (char*)parsed;
  1362. goto powerloss_next;
  1363. }
  1364. powerloss_unknown:
  1365. // unknown scenario?
  1366. fprintf(stderr, "error: "
  1367. "unknown power-loss scenario: %s\n",
  1368. optarg);
  1369. exit(-1);
  1370. powerloss_next:
  1371. optarg += strcspn(optarg, ",");
  1372. if (*optarg == ',') {
  1373. optarg += 1;
  1374. }
  1375. }
  1376. break;
  1377. }
  1378. case OPT_START: {
  1379. char *parsed = NULL;
  1380. test_start = strtoumax(optarg, &parsed, 0);
  1381. if (parsed == optarg) {
  1382. fprintf(stderr, "error: invalid skip: %s\n", optarg);
  1383. exit(-1);
  1384. }
  1385. break;
  1386. }
  1387. case OPT_STOP: {
  1388. char *parsed = NULL;
  1389. test_stop = strtoumax(optarg, &parsed, 0);
  1390. if (parsed == optarg) {
  1391. fprintf(stderr, "error: invalid count: %s\n", optarg);
  1392. exit(-1);
  1393. }
  1394. break;
  1395. }
  1396. case OPT_STEP: {
  1397. char *parsed = NULL;
  1398. test_step = strtoumax(optarg, &parsed, 0);
  1399. if (parsed == optarg) {
  1400. fprintf(stderr, "error: invalid every: %s\n", optarg);
  1401. exit(-1);
  1402. }
  1403. break;
  1404. }
  1405. case OPT_DISK:
  1406. test_disk_path = optarg;
  1407. break;
  1408. case OPT_TRACE:
  1409. test_trace_path = optarg;
  1410. break;
  1411. case OPT_READ_DELAY: {
  1412. char *parsed = NULL;
  1413. double read_delay = strtod(optarg, &parsed);
  1414. if (parsed == optarg) {
  1415. fprintf(stderr, "error: invalid read-delay: %s\n", optarg);
  1416. exit(-1);
  1417. }
  1418. test_read_delay = read_delay*1.0e9;
  1419. break;
  1420. }
  1421. case OPT_PROG_DELAY: {
  1422. char *parsed = NULL;
  1423. double prog_delay = strtod(optarg, &parsed);
  1424. if (parsed == optarg) {
  1425. fprintf(stderr, "error: invalid prog-delay: %s\n", optarg);
  1426. exit(-1);
  1427. }
  1428. test_prog_delay = prog_delay*1.0e9;
  1429. break;
  1430. }
  1431. case OPT_ERASE_DELAY: {
  1432. char *parsed = NULL;
  1433. double erase_delay = strtod(optarg, &parsed);
  1434. if (parsed == optarg) {
  1435. fprintf(stderr, "error: invalid erase-delay: %s\n", optarg);
  1436. exit(-1);
  1437. }
  1438. test_erase_delay = erase_delay*1.0e9;
  1439. break;
  1440. }
  1441. // done parsing
  1442. case -1:
  1443. goto getopt_done;
  1444. // unknown arg, getopt prints a message for us
  1445. default:
  1446. exit(-1);
  1447. }
  1448. }
  1449. getopt_done: ;
  1450. if (argc > optind) {
  1451. // reset our test identifier list
  1452. test_ids = NULL;
  1453. test_id_count = 0;
  1454. test_id_capacity = 0;
  1455. }
  1456. // parse test identifier, if any, cannibalizing the arg in the process
  1457. for (; argc > optind; optind++) {
  1458. // parse suite
  1459. char *suite = argv[optind];
  1460. char *case_ = strchr(suite, '#');
  1461. size_t perm = -1;
  1462. lfs_testbd_powercycles_t *cycles = NULL;
  1463. size_t cycle_count = 0;
  1464. if (case_) {
  1465. *case_ = '\0';
  1466. case_ += 1;
  1467. // parse case
  1468. char *perm_ = strchr(case_, '#');
  1469. if (perm_) {
  1470. *perm_ = '\0';
  1471. perm_ += 1;
  1472. // parse power cycles
  1473. char *cycles_ = strchr(perm_, '#');
  1474. if (cycles_) {
  1475. *cycles_ = '\0';
  1476. cycles_ += 1;
  1477. char *parsed = NULL;
  1478. cycle_count = leb16_parse(cycles_, &parsed, &cycles);
  1479. if (parsed == cycles_) {
  1480. fprintf(stderr, "error: "
  1481. "could not parse test cycles: %s\n", cycles_);
  1482. exit(-1);
  1483. }
  1484. }
  1485. char *parsed = NULL;
  1486. perm = strtoumax(perm_, &parsed, 10);
  1487. if (parsed == perm_) {
  1488. fprintf(stderr, "error: "
  1489. "could not parse test permutation: %s\n", perm_);
  1490. exit(-1);
  1491. }
  1492. }
  1493. }
  1494. // remove optional path and .toml suffix
  1495. char *slash = strrchr(suite, '/');
  1496. if (slash) {
  1497. suite = slash+1;
  1498. }
  1499. size_t suite_len = strlen(suite);
  1500. if (suite_len > 5 && strcmp(&suite[suite_len-5], ".toml") == 0) {
  1501. suite[suite_len-5] = '\0';
  1502. }
  1503. // append to identifier list
  1504. test_id_count += 1;
  1505. if (test_id_count > test_id_capacity) {
  1506. test_id_capacity = (2*test_id_capacity > 4)
  1507. ? 2*test_id_capacity
  1508. : 4;
  1509. test_ids = realloc((test_id_t*)test_ids,
  1510. test_id_capacity * sizeof(test_id_t));
  1511. }
  1512. ((test_id_t*)test_ids)[test_id_count-1] = (test_id_t){
  1513. .suite = suite,
  1514. .case_ = case_,
  1515. .perm = perm,
  1516. .cycles = cycles,
  1517. .cycle_count = cycle_count,
  1518. };
  1519. }
  1520. // register overrides
  1521. test_define_overrides(override_names, override_defines, override_count);
  1522. // do the thing
  1523. op();
  1524. // cleanup (need to be done for valgrind testing)
  1525. free(override_names);
  1526. free(override_defines);
  1527. if (test_powerloss_capacity) {
  1528. for (size_t i = 0; i < test_powerloss_count; i++) {
  1529. free((lfs_testbd_powercycles_t*)test_powerlosses[i].cycles);
  1530. }
  1531. free((test_powerloss_t*)test_powerlosses);
  1532. }
  1533. if (test_id_capacity) {
  1534. for (size_t i = 0; i < test_id_count; i++) {
  1535. free((lfs_testbd_powercycles_t*)test_ids[i].cycles);
  1536. }
  1537. free((test_id_t*)test_ids);
  1538. }
  1539. }