test_runner.c 57 KB

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