test_runner.c 56 KB

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