test_runner.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566
  1. #ifndef _POSIX_C_SOURCE
  2. #define _POSIX_C_SOURCE 199309L
  3. #endif
  4. #include "runners/test_runner.h"
  5. #include "bd/lfs_emubd.h"
  6. #include <getopt.h>
  7. #include <sys/types.h>
  8. #include <errno.h>
  9. #include <setjmp.h>
  10. #include <fcntl.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. // some helpers
  15. // append to an array with amortized doubling
  16. void *mappend(void **p,
  17. size_t size,
  18. size_t *count,
  19. size_t *capacity) {
  20. uint8_t *p_ = *p;
  21. size_t count_ = *count;
  22. size_t capacity_ = *capacity;
  23. count_ += 1;
  24. if (count_ > capacity_) {
  25. capacity_ = (2*capacity_ < 4) ? 4 : 2*capacity_;
  26. p_ = realloc(p_, capacity_*size);
  27. if (!p_) {
  28. return NULL;
  29. }
  30. }
  31. *p = p_;
  32. *count = count_;
  33. *capacity = capacity_;
  34. return &p_[(count_-1)*size];
  35. }
  36. // a quick self-terminating text-safe varint scheme
  37. static void leb16_print(uintmax_t x) {
  38. while (true) {
  39. char nibble = (x & 0xf) | (x > 0xf ? 0x10 : 0);
  40. printf("%c", (nibble < 10) ? '0'+nibble : 'a'+nibble-10);
  41. if (x <= 0xf) {
  42. break;
  43. }
  44. x >>= 4;
  45. }
  46. }
  47. static uintmax_t leb16_parse(const char *s, char **tail) {
  48. uintmax_t x = 0;
  49. size_t i = 0;
  50. while (true) {
  51. uintmax_t nibble = s[i];
  52. if (nibble >= '0' && nibble <= '9') {
  53. nibble = nibble - '0';
  54. } else if (nibble >= 'a' && nibble <= 'v') {
  55. nibble = nibble - 'a' + 10;
  56. } else {
  57. // invalid?
  58. if (tail) {
  59. *tail = (char*)s;
  60. }
  61. return 0;
  62. }
  63. x |= (nibble & 0xf) << (4*i);
  64. i += 1;
  65. if (!(nibble & 0x10)) {
  66. break;
  67. }
  68. }
  69. if (tail) {
  70. *tail = (char*)s + i;
  71. }
  72. return x;
  73. }
  74. // test_runner types
  75. typedef struct test_geometry {
  76. char short_name;
  77. const char *long_name;
  78. test_define_t defines[TEST_GEOMETRY_DEFINE_COUNT];
  79. } test_geometry_t;
  80. typedef struct test_powerloss {
  81. char short_name;
  82. const char *long_name;
  83. void (*run)(
  84. const lfs_emubd_powercycles_t *cycles,
  85. size_t cycle_count,
  86. const struct test_suite *suite,
  87. const struct test_case *case_);
  88. const lfs_emubd_powercycles_t *cycles;
  89. size_t cycle_count;
  90. } test_powerloss_t;
  91. typedef struct test_id {
  92. const char *name;
  93. const test_define_t *defines;
  94. size_t define_count;
  95. const lfs_emubd_powercycles_t *cycles;
  96. size_t cycle_count;
  97. } test_id_t;
  98. // test suites are linked into a custom ld section
  99. extern struct test_suite __start__test_suites;
  100. extern struct test_suite __stop__test_suites;
  101. const struct test_suite *test_suites = &__start__test_suites;
  102. #define TEST_SUITE_COUNT \
  103. ((size_t)(&__stop__test_suites - &__start__test_suites))
  104. // test define management
  105. typedef struct test_define_map {
  106. const test_define_t *defines;
  107. size_t count;
  108. } test_define_map_t;
  109. typedef struct test_define_names {
  110. const char *const *names;
  111. size_t count;
  112. } test_define_names_t;
  113. intmax_t test_define_lit(void *data) {
  114. return (intmax_t)data;
  115. }
  116. #define TEST_CONST(x) {test_define_lit, (void*)(uintptr_t)(x)}
  117. #define TEST_LIT(x) ((test_define_t)TEST_CONST(x))
  118. #define TEST_DEF(k, v) \
  119. intmax_t test_define_##k(void *data) { \
  120. (void)data; \
  121. return v; \
  122. }
  123. TEST_IMPLICIT_DEFINES
  124. #undef TEST_DEF
  125. #define TEST_DEFINE_MAP_EXPLICIT 0
  126. #define TEST_DEFINE_MAP_OVERRIDE 1
  127. #define TEST_DEFINE_MAP_PERMUTATION 2
  128. #define TEST_DEFINE_MAP_GEOMETRY 3
  129. #define TEST_DEFINE_MAP_IMPLICIT 4
  130. #define TEST_DEFINE_MAP_COUNT 5
  131. test_define_map_t test_define_maps[TEST_DEFINE_MAP_COUNT] = {
  132. [TEST_DEFINE_MAP_IMPLICIT] = {
  133. (const test_define_t[TEST_IMPLICIT_DEFINE_COUNT]) {
  134. #define TEST_DEF(k, v) \
  135. [k##_i] = {test_define_##k, NULL},
  136. TEST_IMPLICIT_DEFINES
  137. #undef TEST_DEF
  138. },
  139. TEST_IMPLICIT_DEFINE_COUNT,
  140. },
  141. };
  142. #define TEST_DEFINE_NAMES_SUITE 0
  143. #define TEST_DEFINE_NAMES_IMPLICIT 1
  144. #define TEST_DEFINE_NAMES_COUNT 2
  145. test_define_names_t test_define_names[TEST_DEFINE_NAMES_COUNT] = {
  146. [TEST_DEFINE_NAMES_IMPLICIT] = {
  147. (const char *const[TEST_IMPLICIT_DEFINE_COUNT]){
  148. #define TEST_DEF(k, v) \
  149. [k##_i] = #k,
  150. TEST_IMPLICIT_DEFINES
  151. #undef TEST_DEF
  152. },
  153. TEST_IMPLICIT_DEFINE_COUNT,
  154. },
  155. };
  156. intmax_t *test_define_cache;
  157. size_t test_define_cache_count;
  158. unsigned *test_define_cache_mask;
  159. const char *test_define_name(size_t define) {
  160. // lookup in our test names
  161. for (size_t i = 0; i < TEST_DEFINE_NAMES_COUNT; i++) {
  162. if (define < test_define_names[i].count
  163. && test_define_names[i].names
  164. && test_define_names[i].names[define]) {
  165. return test_define_names[i].names[define];
  166. }
  167. }
  168. return NULL;
  169. }
  170. bool test_define_ispermutation(size_t define) {
  171. // is this define specific to the permutation?
  172. for (size_t i = 0; i < TEST_DEFINE_MAP_IMPLICIT; i++) {
  173. if (define < test_define_maps[i].count
  174. && test_define_maps[i].defines[define].cb) {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. intmax_t test_define(size_t define) {
  181. // is the define in our cache?
  182. if (define < test_define_cache_count
  183. && (test_define_cache_mask[define/(8*sizeof(unsigned))]
  184. & (1 << (define%(8*sizeof(unsigned)))))) {
  185. return test_define_cache[define];
  186. }
  187. // lookup in our test defines
  188. for (size_t i = 0; i < TEST_DEFINE_MAP_COUNT; i++) {
  189. if (define < test_define_maps[i].count
  190. && test_define_maps[i].defines[define].cb) {
  191. intmax_t v = test_define_maps[i].defines[define].cb(
  192. test_define_maps[i].defines[define].data);
  193. // insert into cache!
  194. test_define_cache[define] = v;
  195. test_define_cache_mask[define / (8*sizeof(unsigned))]
  196. |= 1 << (define%(8*sizeof(unsigned)));
  197. return v;
  198. }
  199. }
  200. return 0;
  201. // not found?
  202. const char *name = test_define_name(define);
  203. fprintf(stderr, "error: undefined define %s (%zd)\n",
  204. name ? name : "(unknown)",
  205. define);
  206. assert(false);
  207. exit(-1);
  208. }
  209. void test_define_flush(void) {
  210. // clear cache between permutations
  211. memset(test_define_cache_mask, 0,
  212. sizeof(unsigned)*(
  213. (test_define_cache_count+(8*sizeof(unsigned))-1)
  214. / (8*sizeof(unsigned))));
  215. }
  216. // geometry updates
  217. const test_geometry_t *test_geometry = NULL;
  218. void test_define_geometry(const test_geometry_t *geometry) {
  219. test_define_maps[TEST_DEFINE_MAP_GEOMETRY] = (test_define_map_t){
  220. geometry->defines, TEST_GEOMETRY_DEFINE_COUNT};
  221. }
  222. // override updates
  223. typedef struct test_override {
  224. const char *name;
  225. const intmax_t *defines;
  226. size_t permutations;
  227. } test_override_t;
  228. const test_override_t *test_overrides = NULL;
  229. size_t test_override_count = 0;
  230. test_define_t *test_override_defines = NULL;
  231. size_t test_override_define_count = 0;
  232. size_t test_override_define_permutations = 1;
  233. size_t test_override_define_capacity = 0;
  234. // suite/perm updates
  235. void test_define_suite(const struct test_suite *suite) {
  236. test_define_names[TEST_DEFINE_NAMES_SUITE] = (test_define_names_t){
  237. suite->define_names, suite->define_count};
  238. // make sure our cache is large enough
  239. if (lfs_max(suite->define_count, TEST_IMPLICIT_DEFINE_COUNT)
  240. > test_define_cache_count) {
  241. // align to power of two to avoid any superlinear growth
  242. size_t ncount = 1 << lfs_npw2(
  243. lfs_max(suite->define_count, TEST_IMPLICIT_DEFINE_COUNT));
  244. test_define_cache = realloc(test_define_cache, ncount*sizeof(intmax_t));
  245. test_define_cache_mask = realloc(test_define_cache_mask,
  246. sizeof(unsigned)*(
  247. (ncount+(8*sizeof(unsigned))-1)
  248. / (8*sizeof(unsigned))));
  249. test_define_cache_count = ncount;
  250. }
  251. // map any overrides
  252. if (test_override_count > 0) {
  253. // first figure out the total size of override permutations
  254. size_t count = 0;
  255. size_t permutations = 1;
  256. for (size_t i = 0; i < test_override_count; i++) {
  257. for (size_t d = 0;
  258. d < lfs_max(
  259. suite->define_count,
  260. TEST_IMPLICIT_DEFINE_COUNT);
  261. d++) {
  262. // define name match?
  263. const char *name = test_define_name(d);
  264. if (name && strcmp(name, test_overrides[i].name) == 0) {
  265. count = lfs_max(count, d+1);
  266. permutations *= test_overrides[i].permutations;
  267. break;
  268. }
  269. }
  270. }
  271. test_override_define_count = count;
  272. test_override_define_permutations = permutations;
  273. // make sure our override arrays are big enough
  274. if (count * permutations > test_override_define_capacity) {
  275. // align to power of two to avoid any superlinear growth
  276. size_t ncapacity = 1 << lfs_npw2(count * permutations);
  277. test_override_defines = realloc(
  278. test_override_defines,
  279. sizeof(test_define_t)*ncapacity);
  280. test_override_define_capacity = ncapacity;
  281. }
  282. // zero unoverridden defines
  283. memset(test_override_defines, 0,
  284. sizeof(test_define_t) * count * permutations);
  285. // compute permutations
  286. size_t p = 1;
  287. for (size_t i = 0; i < test_override_count; i++) {
  288. for (size_t d = 0;
  289. d < lfs_max(
  290. suite->define_count,
  291. TEST_IMPLICIT_DEFINE_COUNT);
  292. d++) {
  293. // define name match?
  294. const char *name = test_define_name(d);
  295. if (name && strcmp(name, test_overrides[i].name) == 0) {
  296. // scatter the define permutations based on already
  297. // seen permutations
  298. for (size_t j = 0; j < permutations; j++) {
  299. test_override_defines[j*count + d] = TEST_LIT(
  300. test_overrides[i].defines[(j/p)
  301. % test_overrides[i].permutations]);
  302. }
  303. // keep track of how many permutations we've seen so far
  304. p *= test_overrides[i].permutations;
  305. break;
  306. }
  307. }
  308. }
  309. }
  310. }
  311. void test_define_perm(
  312. const struct test_suite *suite,
  313. const struct test_case *case_,
  314. size_t perm) {
  315. if (case_->defines) {
  316. test_define_maps[TEST_DEFINE_MAP_PERMUTATION] = (test_define_map_t){
  317. case_->defines + perm*suite->define_count,
  318. suite->define_count};
  319. } else {
  320. test_define_maps[TEST_DEFINE_MAP_PERMUTATION] = (test_define_map_t){
  321. NULL, 0};
  322. }
  323. }
  324. void test_define_override(size_t perm) {
  325. test_define_maps[TEST_DEFINE_MAP_OVERRIDE] = (test_define_map_t){
  326. test_override_defines + perm*test_override_define_count,
  327. test_override_define_count};
  328. }
  329. void test_define_explicit(
  330. const test_define_t *defines,
  331. size_t define_count) {
  332. test_define_maps[TEST_DEFINE_MAP_EXPLICIT] = (test_define_map_t){
  333. defines, define_count};
  334. }
  335. void test_define_cleanup(void) {
  336. // test define management can allocate a few things
  337. free(test_define_cache);
  338. free(test_define_cache_mask);
  339. free(test_override_defines);
  340. }
  341. // test state
  342. extern const test_geometry_t *test_geometries;
  343. extern size_t test_geometry_count;
  344. extern const test_powerloss_t *test_powerlosses;
  345. extern size_t test_powerloss_count;
  346. const test_id_t *test_ids = (const test_id_t[]) {
  347. {NULL, NULL, 0, NULL, 0},
  348. };
  349. size_t test_id_count = 1;
  350. size_t test_step_start = 0;
  351. size_t test_step_stop = -1;
  352. size_t test_step_step = 1;
  353. const char *test_disk_path = NULL;
  354. const char *test_trace_path = NULL;
  355. FILE *test_trace_file = NULL;
  356. uint32_t test_trace_cycles = 0;
  357. lfs_emubd_sleep_t test_read_sleep = 0.0;
  358. lfs_emubd_sleep_t test_prog_sleep = 0.0;
  359. lfs_emubd_sleep_t test_erase_sleep = 0.0;
  360. // trace printing
  361. void test_trace(const char *fmt, ...) {
  362. if (test_trace_path) {
  363. if (!test_trace_file) {
  364. // Tracing output is heavy and trying to open every trace
  365. // call is slow, so we only try to open the trace file every
  366. // so often. Note this doesn't affect successfully opened files
  367. if (test_trace_cycles % 128 != 0) {
  368. test_trace_cycles += 1;
  369. return;
  370. }
  371. test_trace_cycles += 1;
  372. int fd;
  373. if (strcmp(test_trace_path, "-") == 0) {
  374. fd = dup(1);
  375. if (fd < 0) {
  376. return;
  377. }
  378. } else {
  379. fd = open(
  380. test_trace_path,
  381. O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK,
  382. 0666);
  383. if (fd < 0) {
  384. return;
  385. }
  386. int err = fcntl(fd, F_SETFL, O_WRONLY | O_CREAT | O_APPEND);
  387. assert(!err);
  388. }
  389. FILE *f = fdopen(fd, "a");
  390. assert(f);
  391. int err = setvbuf(f, NULL, _IOLBF, BUFSIZ);
  392. assert(!err);
  393. test_trace_file = f;
  394. }
  395. va_list va;
  396. va_start(va, fmt);
  397. int res = vfprintf(test_trace_file, fmt, va);
  398. if (res < 0) {
  399. fclose(test_trace_file);
  400. test_trace_file = NULL;
  401. }
  402. va_end(va);
  403. }
  404. }
  405. // encode our permutation into a reusable id
  406. static void perm_printid(
  407. const struct test_suite *suite,
  408. const struct test_case *case_,
  409. const lfs_emubd_powercycles_t *cycles,
  410. size_t cycle_count) {
  411. (void)suite;
  412. // case[:permutation[:powercycles]]
  413. printf("%s:", case_->name);
  414. for (size_t d = 0;
  415. d < lfs_max(
  416. suite->define_count,
  417. TEST_IMPLICIT_DEFINE_COUNT);
  418. d++) {
  419. if (test_define_ispermutation(d)) {
  420. leb16_print(d);
  421. leb16_print(TEST_DEFINE(d));
  422. }
  423. }
  424. // only print power-cycles if any occured
  425. if (cycles) {
  426. printf(":");
  427. for (size_t i = 0; i < cycle_count; i++) {
  428. leb16_print(cycles[i]);
  429. }
  430. }
  431. }
  432. // a quick trie for keeping track of permutations we've seen
  433. typedef struct test_seen {
  434. struct test_seen_branch *branches;
  435. size_t branch_count;
  436. size_t branch_capacity;
  437. } test_seen_t;
  438. struct test_seen_branch {
  439. intmax_t define;
  440. struct test_seen branch;
  441. };
  442. bool test_seen_insert(
  443. test_seen_t *seen,
  444. const struct test_suite *suite,
  445. const struct test_case *case_) {
  446. (void)case_;
  447. bool was_seen = true;
  448. // use the currently set defines
  449. for (size_t d = 0;
  450. d < lfs_max(
  451. suite->define_count,
  452. TEST_IMPLICIT_DEFINE_COUNT);
  453. d++) {
  454. // treat unpermuted defines the same as 0
  455. intmax_t define = test_define_ispermutation(d) ? TEST_DEFINE(d) : 0;
  456. // already seen?
  457. struct test_seen_branch *branch = NULL;
  458. for (size_t i = 0; i < seen->branch_count; i++) {
  459. if (seen->branches[i].define == define) {
  460. branch = &seen->branches[i];
  461. break;
  462. }
  463. }
  464. // need to create a new node
  465. if (!branch) {
  466. was_seen = false;
  467. branch = mappend(
  468. (void**)&seen->branches,
  469. sizeof(struct test_seen_branch),
  470. &seen->branch_count,
  471. &seen->branch_capacity);
  472. branch->define = define;
  473. branch->branch = (test_seen_t){NULL, 0, 0};
  474. }
  475. seen = &branch->branch;
  476. }
  477. return was_seen;
  478. }
  479. void test_seen_cleanup(test_seen_t *seen) {
  480. for (size_t i = 0; i < seen->branch_count; i++) {
  481. test_seen_cleanup(&seen->branches[i].branch);
  482. }
  483. free(seen->branches);
  484. }
  485. static void run_powerloss_cycles(
  486. const lfs_emubd_powercycles_t *cycles,
  487. size_t cycle_count,
  488. const struct test_suite *suite,
  489. const struct test_case *case_);
  490. // iterate through permutations in a test case
  491. static void case_forperm(
  492. const struct test_suite *suite,
  493. const struct test_case *case_,
  494. const test_define_t *defines,
  495. size_t define_count,
  496. const lfs_emubd_powercycles_t *cycles,
  497. size_t cycle_count,
  498. void (*cb)(
  499. void *data,
  500. const struct test_suite *suite,
  501. const struct test_case *case_,
  502. const test_powerloss_t *powerloss),
  503. void *data) {
  504. if (defines) {
  505. test_define_explicit(defines, define_count);
  506. test_define_flush();
  507. if (cycles) {
  508. cb(data, suite, case_, &(test_powerloss_t){
  509. .run=run_powerloss_cycles,
  510. .cycles=cycles,
  511. .cycle_count=cycle_count});
  512. } else {
  513. for (size_t p = 0; p < test_powerloss_count; p++) {
  514. // skip non-reentrant tests when powerloss testing
  515. if (test_powerlosses[p].short_name != '0'
  516. && !(case_->flags & TEST_REENTRANT)) {
  517. continue;
  518. }
  519. cb(data, suite, case_, &test_powerlosses[p]);
  520. }
  521. }
  522. return;
  523. }
  524. test_seen_t seen = {NULL, 0, 0};
  525. for (size_t k = 0; k < case_->permutations; k++) {
  526. // define permutation
  527. test_define_perm(suite, case_, k);
  528. for (size_t v = 0; v < test_override_define_permutations; v++) {
  529. // define override permutation
  530. test_define_override(v);
  531. for (size_t g = 0; g < test_geometry_count; g++) {
  532. // define geometry
  533. test_define_geometry(&test_geometries[g]);
  534. test_define_flush();
  535. // have we seen this permutation before?
  536. bool was_seen = test_seen_insert(&seen, suite, case_);
  537. if (!(k == 0 && v == 0 && g == 0) && was_seen) {
  538. continue;
  539. }
  540. if (cycles) {
  541. cb(data, suite, case_, &(test_powerloss_t){
  542. .run=run_powerloss_cycles,
  543. .cycles=cycles,
  544. .cycle_count=cycle_count});
  545. } else {
  546. for (size_t p = 0; p < test_powerloss_count; p++) {
  547. // skip non-reentrant tests when powerloss testing
  548. if (test_powerlosses[p].short_name != '0'
  549. && !(case_->flags & TEST_REENTRANT)) {
  550. continue;
  551. }
  552. cb(data, suite, case_, &test_powerlosses[p]);
  553. }
  554. }
  555. }
  556. }
  557. }
  558. test_seen_cleanup(&seen);
  559. }
  560. // how many permutations are there actually in a test case
  561. struct perm_count_state {
  562. size_t total;
  563. size_t filtered;
  564. };
  565. void perm_count(
  566. void *data,
  567. const struct test_suite *suite,
  568. const struct test_case *case_,
  569. const test_powerloss_t *powerloss) {
  570. struct perm_count_state *state = data;
  571. (void)suite;
  572. (void)case_;
  573. (void)powerloss;
  574. state->total += 1;
  575. if (case_->filter && !case_->filter()) {
  576. return;
  577. }
  578. state->filtered += 1;
  579. }
  580. // operations we can do
  581. static void summary(void) {
  582. printf("%-36s %7s %7s %7s %11s\n",
  583. "", "flags", "suites", "cases", "perms");
  584. size_t suites = 0;
  585. size_t cases = 0;
  586. test_flags_t flags = 0;
  587. struct perm_count_state perms = {0, 0};
  588. for (size_t t = 0; t < test_id_count; t++) {
  589. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  590. test_define_suite(&test_suites[i]);
  591. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  592. // does neither suite nor case name match?
  593. if (test_ids[t].name && !(
  594. strcmp(test_ids[t].name,
  595. test_suites[i].name) == 0
  596. || strcmp(test_ids[t].name,
  597. test_suites[i].cases[j].name) == 0)) {
  598. continue;
  599. }
  600. cases += 1;
  601. case_forperm(
  602. &test_suites[i],
  603. &test_suites[i].cases[j],
  604. test_ids[t].defines,
  605. test_ids[t].define_count,
  606. test_ids[t].cycles,
  607. test_ids[t].cycle_count,
  608. perm_count,
  609. &perms);
  610. }
  611. suites += 1;
  612. flags |= test_suites[i].flags;
  613. }
  614. }
  615. char perm_buf[64];
  616. sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
  617. char flag_buf[64];
  618. sprintf(flag_buf, "%s%s",
  619. (flags & TEST_REENTRANT) ? "r" : "",
  620. (!flags) ? "-" : "");
  621. printf("%-36s %7s %7zu %7zu %11s\n",
  622. "TOTAL",
  623. flag_buf,
  624. suites,
  625. cases,
  626. perm_buf);
  627. }
  628. static void list_suites(void) {
  629. printf("%-36s %7s %7s %11s\n", "suite", "flags", "cases", "perms");
  630. for (size_t t = 0; t < test_id_count; t++) {
  631. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  632. test_define_suite(&test_suites[i]);
  633. size_t cases = 0;
  634. struct perm_count_state perms = {0, 0};
  635. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  636. // does neither suite nor case name match?
  637. if (test_ids[t].name && !(
  638. strcmp(test_ids[t].name,
  639. test_suites[i].name) == 0
  640. || strcmp(test_ids[t].name,
  641. test_suites[i].cases[j].name) == 0)) {
  642. continue;
  643. }
  644. cases += 1;
  645. case_forperm(
  646. &test_suites[i],
  647. &test_suites[i].cases[j],
  648. test_ids[t].defines,
  649. test_ids[t].define_count,
  650. test_ids[t].cycles,
  651. test_ids[t].cycle_count,
  652. perm_count,
  653. &perms);
  654. }
  655. // no tests found?
  656. if (!cases) {
  657. continue;
  658. }
  659. char perm_buf[64];
  660. sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
  661. char flag_buf[64];
  662. sprintf(flag_buf, "%s%s",
  663. (test_suites[i].flags & TEST_REENTRANT) ? "r" : "",
  664. (!test_suites[i].flags) ? "-" : "");
  665. printf("%-36s %7s %7zu %11s\n",
  666. test_suites[i].name,
  667. flag_buf,
  668. cases,
  669. perm_buf);
  670. }
  671. }
  672. }
  673. static void list_cases(void) {
  674. printf("%-36s %7s %11s\n", "case", "flags", "perms");
  675. for (size_t t = 0; t < test_id_count; t++) {
  676. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  677. test_define_suite(&test_suites[i]);
  678. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  679. // does neither suite nor case name match?
  680. if (test_ids[t].name && !(
  681. strcmp(test_ids[t].name,
  682. test_suites[i].name) == 0
  683. || strcmp(test_ids[t].name,
  684. test_suites[i].cases[j].name) == 0)) {
  685. continue;
  686. }
  687. struct perm_count_state perms = {0, 0};
  688. case_forperm(
  689. &test_suites[i],
  690. &test_suites[i].cases[j],
  691. test_ids[t].defines,
  692. test_ids[t].define_count,
  693. test_ids[t].cycles,
  694. test_ids[t].cycle_count,
  695. perm_count,
  696. &perms);
  697. char perm_buf[64];
  698. sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
  699. char flag_buf[64];
  700. sprintf(flag_buf, "%s%s",
  701. (test_suites[i].cases[j].flags & TEST_REENTRANT)
  702. ? "r" : "",
  703. (!test_suites[i].cases[j].flags)
  704. ? "-" : "");
  705. printf("%-36s %7s %11s\n",
  706. test_suites[i].cases[j].name,
  707. flag_buf,
  708. perm_buf);
  709. }
  710. }
  711. }
  712. }
  713. static void list_suite_paths(void) {
  714. printf("%-36s %s\n", "suite", "path");
  715. for (size_t t = 0; t < test_id_count; t++) {
  716. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  717. size_t cases = 0;
  718. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  719. // does neither suite nor case name match?
  720. if (test_ids[t].name && !(
  721. strcmp(test_ids[t].name,
  722. test_suites[i].name) == 0
  723. || strcmp(test_ids[t].name,
  724. test_suites[i].cases[j].name) == 0)) {
  725. continue;
  726. }
  727. }
  728. // no tests found?
  729. if (!cases) {
  730. continue;
  731. }
  732. printf("%-36s %s\n",
  733. test_suites[i].name,
  734. test_suites[i].path);
  735. }
  736. }
  737. }
  738. static void list_case_paths(void) {
  739. printf("%-36s %s\n", "case", "path");
  740. for (size_t t = 0; t < test_id_count; t++) {
  741. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  742. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  743. // does neither suite nor case name match?
  744. if (test_ids[t].name && !(
  745. strcmp(test_ids[t].name,
  746. test_suites[i].name) == 0
  747. || strcmp(test_ids[t].name,
  748. test_suites[i].cases[j].name) == 0)) {
  749. continue;
  750. }
  751. printf("%-36s %s\n",
  752. test_suites[i].cases[j].name,
  753. test_suites[i].cases[j].path);
  754. }
  755. }
  756. }
  757. }
  758. struct list_defines_define {
  759. const char *name;
  760. intmax_t *values;
  761. size_t value_count;
  762. size_t value_capacity;
  763. };
  764. struct list_defines_defines {
  765. struct list_defines_define *defines;
  766. size_t define_count;
  767. size_t define_capacity;
  768. };
  769. static void list_defines_add(
  770. struct list_defines_defines *defines,
  771. size_t d) {
  772. const char *name = test_define_name(d);
  773. intmax_t value = TEST_DEFINE(d);
  774. // define already in defines?
  775. for (size_t i = 0; i < defines->define_count; i++) {
  776. if (strcmp(defines->defines[i].name, name) == 0) {
  777. // value already in values?
  778. for (size_t j = 0; j < defines->defines[i].value_count; j++) {
  779. if (defines->defines[i].values[j] == value) {
  780. return;
  781. }
  782. }
  783. *(intmax_t*)mappend(
  784. (void**)&defines->defines[i].values,
  785. sizeof(intmax_t),
  786. &defines->defines[i].value_count,
  787. &defines->defines[i].value_capacity) = value;
  788. return;
  789. }
  790. }
  791. // new define?
  792. struct list_defines_define *define = mappend(
  793. (void**)&defines->defines,
  794. sizeof(struct list_defines_define),
  795. &defines->define_count,
  796. &defines->define_capacity);
  797. define->name = name;
  798. define->values = malloc(sizeof(intmax_t));
  799. define->values[0] = value;
  800. define->value_count = 1;
  801. define->value_capacity = 1;
  802. }
  803. void perm_list_defines(
  804. void *data,
  805. const struct test_suite *suite,
  806. const struct test_case *case_,
  807. const test_powerloss_t *powerloss) {
  808. struct list_defines_defines *defines = data;
  809. (void)suite;
  810. (void)case_;
  811. (void)powerloss;
  812. // collect defines
  813. for (size_t d = 0;
  814. d < lfs_max(suite->define_count,
  815. TEST_IMPLICIT_DEFINE_COUNT);
  816. d++) {
  817. if (d < TEST_IMPLICIT_DEFINE_COUNT
  818. || test_define_ispermutation(d)) {
  819. list_defines_add(defines, d);
  820. }
  821. }
  822. }
  823. void perm_list_permutation_defines(
  824. void *data,
  825. const struct test_suite *suite,
  826. const struct test_case *case_,
  827. const test_powerloss_t *powerloss) {
  828. struct list_defines_defines *defines = data;
  829. (void)suite;
  830. (void)case_;
  831. (void)powerloss;
  832. // collect permutation_defines
  833. for (size_t d = 0;
  834. d < lfs_max(suite->define_count,
  835. TEST_IMPLICIT_DEFINE_COUNT);
  836. d++) {
  837. if (test_define_ispermutation(d)) {
  838. list_defines_add(defines, d);
  839. }
  840. }
  841. }
  842. extern const test_geometry_t builtin_geometries[];
  843. static void list_defines(void) {
  844. struct list_defines_defines defines = {NULL, 0, 0};
  845. // add defines
  846. for (size_t t = 0; t < test_id_count; t++) {
  847. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  848. test_define_suite(&test_suites[i]);
  849. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  850. // does neither suite nor case name match?
  851. if (test_ids[t].name && !(
  852. strcmp(test_ids[t].name,
  853. test_suites[i].name) == 0
  854. || strcmp(test_ids[t].name,
  855. test_suites[i].cases[j].name) == 0)) {
  856. continue;
  857. }
  858. case_forperm(
  859. &test_suites[i],
  860. &test_suites[i].cases[j],
  861. test_ids[t].defines,
  862. test_ids[t].define_count,
  863. test_ids[t].cycles,
  864. test_ids[t].cycle_count,
  865. perm_list_defines,
  866. &defines);
  867. }
  868. }
  869. }
  870. for (size_t i = 0; i < defines.define_count; i++) {
  871. printf("%s=", defines.defines[i].name);
  872. for (size_t j = 0; j < defines.defines[i].value_count; j++) {
  873. printf("%jd", defines.defines[i].values[j]);
  874. if (j != defines.defines[i].value_count-1) {
  875. printf(",");
  876. }
  877. }
  878. printf("\n");
  879. }
  880. for (size_t i = 0; i < defines.define_count; i++) {
  881. free(defines.defines[i].values);
  882. }
  883. free(defines.defines);
  884. }
  885. static void list_permutation_defines(void) {
  886. struct list_defines_defines defines = {NULL, 0, 0};
  887. // add permutation defines
  888. for (size_t t = 0; t < test_id_count; t++) {
  889. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  890. test_define_suite(&test_suites[i]);
  891. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  892. // does neither suite nor case name match?
  893. if (test_ids[t].name && !(
  894. strcmp(test_ids[t].name,
  895. test_suites[i].name) == 0
  896. || strcmp(test_ids[t].name,
  897. test_suites[i].cases[j].name) == 0)) {
  898. continue;
  899. }
  900. case_forperm(
  901. &test_suites[i],
  902. &test_suites[i].cases[j],
  903. test_ids[t].defines,
  904. test_ids[t].define_count,
  905. test_ids[t].cycles,
  906. test_ids[t].cycle_count,
  907. perm_list_permutation_defines,
  908. &defines);
  909. }
  910. }
  911. }
  912. for (size_t i = 0; i < defines.define_count; i++) {
  913. printf("%s=", defines.defines[i].name);
  914. for (size_t j = 0; j < defines.defines[i].value_count; j++) {
  915. printf("%jd", defines.defines[i].values[j]);
  916. if (j != defines.defines[i].value_count-1) {
  917. printf(",");
  918. }
  919. }
  920. printf("\n");
  921. }
  922. for (size_t i = 0; i < defines.define_count; i++) {
  923. free(defines.defines[i].values);
  924. }
  925. free(defines.defines);
  926. }
  927. static void list_implicit_defines(void) {
  928. struct list_defines_defines defines = {NULL, 0, 0};
  929. // yes we do need to define a suite, this does a bit of bookeeping
  930. // such as setting up the define cache
  931. test_define_suite(&(const struct test_suite){0});
  932. // make sure to include builtin geometries here
  933. extern const test_geometry_t builtin_geometries[];
  934. for (size_t g = 0; builtin_geometries[g].long_name; g++) {
  935. test_define_geometry(&builtin_geometries[g]);
  936. test_define_flush();
  937. // add implicit defines
  938. for (size_t d = 0; d < TEST_IMPLICIT_DEFINE_COUNT; d++) {
  939. list_defines_add(&defines, d);
  940. }
  941. }
  942. for (size_t i = 0; i < defines.define_count; i++) {
  943. printf("%s=", defines.defines[i].name);
  944. for (size_t j = 0; j < defines.defines[i].value_count; j++) {
  945. printf("%jd", defines.defines[i].values[j]);
  946. if (j != defines.defines[i].value_count-1) {
  947. printf(",");
  948. }
  949. }
  950. printf("\n");
  951. }
  952. for (size_t i = 0; i < defines.define_count; i++) {
  953. free(defines.defines[i].values);
  954. }
  955. free(defines.defines);
  956. }
  957. // geometries to test
  958. const test_geometry_t builtin_geometries[] = {
  959. {'d', "default", {{NULL}, TEST_CONST(16), TEST_CONST(512), {NULL}}},
  960. {'e', "eeprom", {{NULL}, TEST_CONST(1), TEST_CONST(512), {NULL}}},
  961. {'E', "emmc", {{NULL}, {NULL}, TEST_CONST(512), {NULL}}},
  962. {'n', "nor", {{NULL}, TEST_CONST(1), TEST_CONST(4096), {NULL}}},
  963. {'N', "nand", {{NULL}, TEST_CONST(4096), TEST_CONST(32768), {NULL}}},
  964. {0, NULL, {{NULL}, {NULL}, {NULL}, {NULL}}},
  965. };
  966. const test_geometry_t *test_geometries = builtin_geometries;
  967. size_t test_geometry_count = 5;
  968. static void list_geometries(void) {
  969. // yes we do need to define a suite, this does a bit of bookeeping
  970. // such as setting up the define cache
  971. test_define_suite(&(const struct test_suite){0});
  972. printf("%-24s %7s %7s %7s %7s %11s\n",
  973. "geometry", "read", "prog", "erase", "count", "size");
  974. for (size_t g = 0; builtin_geometries[g].long_name; g++) {
  975. test_define_geometry(&builtin_geometries[g]);
  976. test_define_flush();
  977. printf("%c,%-22s %7ju %7ju %7ju %7ju %11ju\n",
  978. builtin_geometries[g].short_name,
  979. builtin_geometries[g].long_name,
  980. READ_SIZE,
  981. PROG_SIZE,
  982. BLOCK_SIZE,
  983. BLOCK_COUNT,
  984. BLOCK_SIZE*BLOCK_COUNT);
  985. }
  986. }
  987. // scenarios to run tests under power-loss
  988. static void run_powerloss_none(
  989. const lfs_emubd_powercycles_t *cycles,
  990. size_t cycle_count,
  991. const struct test_suite *suite,
  992. const struct test_case *case_) {
  993. (void)cycles;
  994. (void)cycle_count;
  995. (void)suite;
  996. // create block device and configuration
  997. lfs_emubd_t bd;
  998. struct lfs_config cfg = {
  999. .context = &bd,
  1000. .read = lfs_emubd_read,
  1001. .prog = lfs_emubd_prog,
  1002. .erase = lfs_emubd_erase,
  1003. .sync = lfs_emubd_sync,
  1004. .read_size = READ_SIZE,
  1005. .prog_size = PROG_SIZE,
  1006. .block_size = BLOCK_SIZE,
  1007. .block_count = BLOCK_COUNT,
  1008. .block_cycles = BLOCK_CYCLES,
  1009. .cache_size = CACHE_SIZE,
  1010. .lookahead_size = LOOKAHEAD_SIZE,
  1011. };
  1012. struct lfs_emubd_config bdcfg = {
  1013. .erase_value = ERASE_VALUE,
  1014. .erase_cycles = ERASE_CYCLES,
  1015. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1016. .disk_path = test_disk_path,
  1017. .read_sleep = test_read_sleep,
  1018. .prog_sleep = test_prog_sleep,
  1019. .erase_sleep = test_erase_sleep,
  1020. };
  1021. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1022. if (err) {
  1023. fprintf(stderr, "error: could not create block device: %d\n", err);
  1024. exit(-1);
  1025. }
  1026. // run the test
  1027. printf("running ");
  1028. perm_printid(suite, case_, NULL, 0);
  1029. printf("\n");
  1030. case_->run(&cfg);
  1031. printf("finished ");
  1032. perm_printid(suite, case_, NULL, 0);
  1033. printf("\n");
  1034. // cleanup
  1035. err = lfs_emubd_destroy(&cfg);
  1036. if (err) {
  1037. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1038. exit(-1);
  1039. }
  1040. }
  1041. static void powerloss_longjmp(void *c) {
  1042. jmp_buf *powerloss_jmp = c;
  1043. longjmp(*powerloss_jmp, 1);
  1044. }
  1045. static void run_powerloss_linear(
  1046. const lfs_emubd_powercycles_t *cycles,
  1047. size_t cycle_count,
  1048. const struct test_suite *suite,
  1049. const struct test_case *case_) {
  1050. (void)cycles;
  1051. (void)cycle_count;
  1052. (void)suite;
  1053. // create block device and configuration
  1054. lfs_emubd_t bd;
  1055. jmp_buf powerloss_jmp;
  1056. volatile lfs_emubd_powercycles_t i = 1;
  1057. struct lfs_config cfg = {
  1058. .context = &bd,
  1059. .read = lfs_emubd_read,
  1060. .prog = lfs_emubd_prog,
  1061. .erase = lfs_emubd_erase,
  1062. .sync = lfs_emubd_sync,
  1063. .read_size = READ_SIZE,
  1064. .prog_size = PROG_SIZE,
  1065. .block_size = BLOCK_SIZE,
  1066. .block_count = BLOCK_COUNT,
  1067. .block_cycles = BLOCK_CYCLES,
  1068. .cache_size = CACHE_SIZE,
  1069. .lookahead_size = LOOKAHEAD_SIZE,
  1070. };
  1071. struct lfs_emubd_config bdcfg = {
  1072. .erase_value = ERASE_VALUE,
  1073. .erase_cycles = ERASE_CYCLES,
  1074. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1075. .disk_path = test_disk_path,
  1076. .read_sleep = test_read_sleep,
  1077. .prog_sleep = test_prog_sleep,
  1078. .erase_sleep = test_erase_sleep,
  1079. .power_cycles = i,
  1080. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  1081. .powerloss_cb = powerloss_longjmp,
  1082. .powerloss_data = &powerloss_jmp,
  1083. };
  1084. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1085. if (err) {
  1086. fprintf(stderr, "error: could not create block device: %d\n", err);
  1087. exit(-1);
  1088. }
  1089. // run the test, increasing power-cycles as power-loss events occur
  1090. printf("running ");
  1091. perm_printid(suite, case_, NULL, 0);
  1092. printf("\n");
  1093. while (true) {
  1094. if (!setjmp(powerloss_jmp)) {
  1095. // run the test
  1096. case_->run(&cfg);
  1097. break;
  1098. }
  1099. // power-loss!
  1100. printf("powerloss ");
  1101. perm_printid(suite, case_, NULL, 0);
  1102. printf(":");
  1103. for (lfs_emubd_powercycles_t j = 1; j <= i; j++) {
  1104. leb16_print(j);
  1105. }
  1106. printf("\n");
  1107. i += 1;
  1108. lfs_emubd_setpowercycles(&cfg, i);
  1109. }
  1110. printf("finished ");
  1111. perm_printid(suite, case_, NULL, 0);
  1112. printf("\n");
  1113. // cleanup
  1114. err = lfs_emubd_destroy(&cfg);
  1115. if (err) {
  1116. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1117. exit(-1);
  1118. }
  1119. }
  1120. static void run_powerloss_exponential(
  1121. const lfs_emubd_powercycles_t *cycles,
  1122. size_t cycle_count,
  1123. const struct test_suite *suite,
  1124. const struct test_case *case_) {
  1125. (void)cycles;
  1126. (void)cycle_count;
  1127. (void)suite;
  1128. // create block device and configuration
  1129. lfs_emubd_t bd;
  1130. jmp_buf powerloss_jmp;
  1131. volatile lfs_emubd_powercycles_t i = 1;
  1132. struct lfs_config cfg = {
  1133. .context = &bd,
  1134. .read = lfs_emubd_read,
  1135. .prog = lfs_emubd_prog,
  1136. .erase = lfs_emubd_erase,
  1137. .sync = lfs_emubd_sync,
  1138. .read_size = READ_SIZE,
  1139. .prog_size = PROG_SIZE,
  1140. .block_size = BLOCK_SIZE,
  1141. .block_count = BLOCK_COUNT,
  1142. .block_cycles = BLOCK_CYCLES,
  1143. .cache_size = CACHE_SIZE,
  1144. .lookahead_size = LOOKAHEAD_SIZE,
  1145. };
  1146. struct lfs_emubd_config bdcfg = {
  1147. .erase_value = ERASE_VALUE,
  1148. .erase_cycles = ERASE_CYCLES,
  1149. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1150. .disk_path = test_disk_path,
  1151. .read_sleep = test_read_sleep,
  1152. .prog_sleep = test_prog_sleep,
  1153. .erase_sleep = test_erase_sleep,
  1154. .power_cycles = i,
  1155. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  1156. .powerloss_cb = powerloss_longjmp,
  1157. .powerloss_data = &powerloss_jmp,
  1158. };
  1159. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1160. if (err) {
  1161. fprintf(stderr, "error: could not create block device: %d\n", err);
  1162. exit(-1);
  1163. }
  1164. // run the test, increasing power-cycles as power-loss events occur
  1165. printf("running ");
  1166. perm_printid(suite, case_, NULL, 0);
  1167. printf("\n");
  1168. while (true) {
  1169. if (!setjmp(powerloss_jmp)) {
  1170. // run the test
  1171. case_->run(&cfg);
  1172. break;
  1173. }
  1174. // power-loss!
  1175. printf("powerloss ");
  1176. perm_printid(suite, case_, NULL, 0);
  1177. printf(":");
  1178. for (lfs_emubd_powercycles_t j = 1; j <= i; j *= 2) {
  1179. leb16_print(j);
  1180. }
  1181. printf("\n");
  1182. i *= 2;
  1183. lfs_emubd_setpowercycles(&cfg, i);
  1184. }
  1185. printf("finished ");
  1186. perm_printid(suite, case_, NULL, 0);
  1187. printf("\n");
  1188. // cleanup
  1189. err = lfs_emubd_destroy(&cfg);
  1190. if (err) {
  1191. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1192. exit(-1);
  1193. }
  1194. }
  1195. static void run_powerloss_cycles(
  1196. const lfs_emubd_powercycles_t *cycles,
  1197. size_t cycle_count,
  1198. const struct test_suite *suite,
  1199. const struct test_case *case_) {
  1200. (void)suite;
  1201. // create block device and configuration
  1202. lfs_emubd_t bd;
  1203. jmp_buf powerloss_jmp;
  1204. volatile size_t i = 0;
  1205. struct lfs_config cfg = {
  1206. .context = &bd,
  1207. .read = lfs_emubd_read,
  1208. .prog = lfs_emubd_prog,
  1209. .erase = lfs_emubd_erase,
  1210. .sync = lfs_emubd_sync,
  1211. .read_size = READ_SIZE,
  1212. .prog_size = PROG_SIZE,
  1213. .block_size = BLOCK_SIZE,
  1214. .block_count = BLOCK_COUNT,
  1215. .block_cycles = BLOCK_CYCLES,
  1216. .cache_size = CACHE_SIZE,
  1217. .lookahead_size = LOOKAHEAD_SIZE,
  1218. };
  1219. struct lfs_emubd_config bdcfg = {
  1220. .erase_value = ERASE_VALUE,
  1221. .erase_cycles = ERASE_CYCLES,
  1222. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1223. .disk_path = test_disk_path,
  1224. .read_sleep = test_read_sleep,
  1225. .prog_sleep = test_prog_sleep,
  1226. .erase_sleep = test_erase_sleep,
  1227. .power_cycles = (i < cycle_count) ? cycles[i] : 0,
  1228. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  1229. .powerloss_cb = powerloss_longjmp,
  1230. .powerloss_data = &powerloss_jmp,
  1231. };
  1232. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1233. if (err) {
  1234. fprintf(stderr, "error: could not create block device: %d\n", err);
  1235. exit(-1);
  1236. }
  1237. // run the test, increasing power-cycles as power-loss events occur
  1238. printf("running ");
  1239. perm_printid(suite, case_, NULL, 0);
  1240. printf("\n");
  1241. while (true) {
  1242. if (!setjmp(powerloss_jmp)) {
  1243. // run the test
  1244. case_->run(&cfg);
  1245. break;
  1246. }
  1247. // power-loss!
  1248. assert(i <= cycle_count);
  1249. printf("powerloss ");
  1250. perm_printid(suite, case_, cycles, i+1);
  1251. printf("\n");
  1252. i += 1;
  1253. lfs_emubd_setpowercycles(&cfg,
  1254. (i < cycle_count) ? cycles[i] : 0);
  1255. }
  1256. printf("finished ");
  1257. perm_printid(suite, case_, NULL, 0);
  1258. printf("\n");
  1259. // cleanup
  1260. err = lfs_emubd_destroy(&cfg);
  1261. if (err) {
  1262. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1263. exit(-1);
  1264. }
  1265. }
  1266. struct powerloss_exhaustive_state {
  1267. struct lfs_config *cfg;
  1268. lfs_emubd_t *branches;
  1269. size_t branch_count;
  1270. size_t branch_capacity;
  1271. };
  1272. struct powerloss_exhaustive_cycles {
  1273. lfs_emubd_powercycles_t *cycles;
  1274. size_t cycle_count;
  1275. size_t cycle_capacity;
  1276. };
  1277. static void powerloss_exhaustive_branch(void *c) {
  1278. struct powerloss_exhaustive_state *state = c;
  1279. // append to branches
  1280. lfs_emubd_t *branch = mappend(
  1281. (void**)&state->branches,
  1282. sizeof(lfs_emubd_t),
  1283. &state->branch_count,
  1284. &state->branch_capacity);
  1285. if (!branch) {
  1286. fprintf(stderr, "error: exhaustive: out of memory\n");
  1287. exit(-1);
  1288. }
  1289. // create copy-on-write copy
  1290. int err = lfs_emubd_copy(state->cfg, branch);
  1291. if (err) {
  1292. fprintf(stderr, "error: exhaustive: could not create bd copy\n");
  1293. exit(-1);
  1294. }
  1295. // also trigger on next power cycle
  1296. lfs_emubd_setpowercycles(state->cfg, 1);
  1297. }
  1298. static void run_powerloss_exhaustive_layer(
  1299. struct powerloss_exhaustive_cycles *cycles,
  1300. const struct test_suite *suite,
  1301. const struct test_case *case_,
  1302. struct lfs_config *cfg,
  1303. struct lfs_emubd_config *bdcfg,
  1304. size_t depth) {
  1305. (void)suite;
  1306. struct powerloss_exhaustive_state state = {
  1307. .cfg = cfg,
  1308. .branches = NULL,
  1309. .branch_count = 0,
  1310. .branch_capacity = 0,
  1311. };
  1312. // run through the test without additional powerlosses, collecting possible
  1313. // branches as we do so
  1314. lfs_emubd_setpowercycles(state.cfg, depth > 0 ? 1 : 0);
  1315. bdcfg->powerloss_data = &state;
  1316. // run the tests
  1317. case_->run(cfg);
  1318. // aggressively clean up memory here to try to keep our memory usage low
  1319. int err = lfs_emubd_destroy(cfg);
  1320. if (err) {
  1321. fprintf(stderr, "error: could not destroy block device: %d\n", err);
  1322. exit(-1);
  1323. }
  1324. // recurse into each branch
  1325. for (size_t i = 0; i < state.branch_count; i++) {
  1326. // first push and print the branch
  1327. lfs_emubd_powercycles_t *cycle = mappend(
  1328. (void**)&cycles->cycles,
  1329. sizeof(lfs_emubd_powercycles_t),
  1330. &cycles->cycle_count,
  1331. &cycles->cycle_capacity);
  1332. if (!cycle) {
  1333. fprintf(stderr, "error: exhaustive: out of memory\n");
  1334. exit(-1);
  1335. }
  1336. *cycle = i;
  1337. printf("powerloss ");
  1338. perm_printid(suite, case_, cycles->cycles, cycles->cycle_count);
  1339. printf("\n");
  1340. // now recurse
  1341. cfg->context = &state.branches[i];
  1342. run_powerloss_exhaustive_layer(cycles,
  1343. suite, case_,
  1344. cfg, bdcfg, depth-1);
  1345. // pop the cycle
  1346. cycles->cycle_count -= 1;
  1347. }
  1348. // clean up memory
  1349. free(state.branches);
  1350. }
  1351. static void run_powerloss_exhaustive(
  1352. const lfs_emubd_powercycles_t *cycles,
  1353. size_t cycle_count,
  1354. const struct test_suite *suite,
  1355. const struct test_case *case_) {
  1356. (void)cycles;
  1357. (void)suite;
  1358. // create block device and configuration
  1359. lfs_emubd_t bd;
  1360. struct lfs_config cfg = {
  1361. .context = &bd,
  1362. .read = lfs_emubd_read,
  1363. .prog = lfs_emubd_prog,
  1364. .erase = lfs_emubd_erase,
  1365. .sync = lfs_emubd_sync,
  1366. .read_size = READ_SIZE,
  1367. .prog_size = PROG_SIZE,
  1368. .block_size = BLOCK_SIZE,
  1369. .block_count = BLOCK_COUNT,
  1370. .block_cycles = BLOCK_CYCLES,
  1371. .cache_size = CACHE_SIZE,
  1372. .lookahead_size = LOOKAHEAD_SIZE,
  1373. };
  1374. struct lfs_emubd_config bdcfg = {
  1375. .erase_value = ERASE_VALUE,
  1376. .erase_cycles = ERASE_CYCLES,
  1377. .badblock_behavior = BADBLOCK_BEHAVIOR,
  1378. .disk_path = test_disk_path,
  1379. .read_sleep = test_read_sleep,
  1380. .prog_sleep = test_prog_sleep,
  1381. .erase_sleep = test_erase_sleep,
  1382. .powerloss_behavior = POWERLOSS_BEHAVIOR,
  1383. .powerloss_cb = powerloss_exhaustive_branch,
  1384. .powerloss_data = NULL,
  1385. };
  1386. int err = lfs_emubd_createcfg(&cfg, test_disk_path, &bdcfg);
  1387. if (err) {
  1388. fprintf(stderr, "error: could not create block device: %d\n", err);
  1389. exit(-1);
  1390. }
  1391. // run the test, increasing power-cycles as power-loss events occur
  1392. printf("running ");
  1393. perm_printid(suite, case_, NULL, 0);
  1394. printf("\n");
  1395. // recursively exhaust each layer of powerlosses
  1396. run_powerloss_exhaustive_layer(
  1397. &(struct powerloss_exhaustive_cycles){NULL, 0, 0},
  1398. suite, case_,
  1399. &cfg, &bdcfg, cycle_count);
  1400. printf("finished ");
  1401. perm_printid(suite, case_, NULL, 0);
  1402. printf("\n");
  1403. }
  1404. const test_powerloss_t builtin_powerlosses[] = {
  1405. {'0', "none", run_powerloss_none, NULL, 0},
  1406. {'e', "exponential", run_powerloss_exponential, NULL, 0},
  1407. {'l', "linear", run_powerloss_linear, NULL, 0},
  1408. {'x', "exhaustive", run_powerloss_exhaustive, NULL, SIZE_MAX},
  1409. {0, NULL, NULL, NULL, 0},
  1410. };
  1411. const char *const builtin_powerlosses_help[] = {
  1412. "Run with no power-losses.",
  1413. "Run with exponentially-decreasing power-losses.",
  1414. "Run with linearly-decreasing power-losses.",
  1415. "Run a all permutations of power-losses, this may take a while.",
  1416. "Run a all permutations of n power-losses.",
  1417. "Run a custom comma-separated set of power-losses.",
  1418. "Run a custom leb16-encoded set of power-losses.",
  1419. };
  1420. const test_powerloss_t *test_powerlosses = (const test_powerloss_t[]){
  1421. {'0', "none", run_powerloss_none, NULL, 0},
  1422. };
  1423. size_t test_powerloss_count = 1;
  1424. static void list_powerlosses(void) {
  1425. printf("%-24s %s\n", "scenario", "description");
  1426. size_t i = 0;
  1427. for (; builtin_powerlosses[i].long_name; i++) {
  1428. printf("%c,%-22s %s\n",
  1429. builtin_powerlosses[i].short_name,
  1430. builtin_powerlosses[i].long_name,
  1431. builtin_powerlosses_help[i]);
  1432. }
  1433. // a couple more options with special parsing
  1434. printf("%-24s %s\n", "1,2,3", builtin_powerlosses_help[i+0]);
  1435. printf("%-24s %s\n", "{1,2,3}", builtin_powerlosses_help[i+1]);
  1436. printf("%-24s %s\n", ":1248g1", builtin_powerlosses_help[i+2]);
  1437. }
  1438. // global test step count
  1439. size_t test_step = 0;
  1440. void perm_run(
  1441. void *data,
  1442. const struct test_suite *suite,
  1443. const struct test_case *case_,
  1444. const test_powerloss_t *powerloss) {
  1445. (void)data;
  1446. // skip this step?
  1447. if (!(test_step >= test_step_start
  1448. && test_step < test_step_stop
  1449. && (test_step-test_step_start) % test_step_step == 0)) {
  1450. test_step += 1;
  1451. return;
  1452. }
  1453. test_step += 1;
  1454. // filter?
  1455. if (case_->filter && !case_->filter()) {
  1456. printf("skipped ");
  1457. perm_printid(suite, case_, NULL, 0);
  1458. printf("\n");
  1459. return;
  1460. }
  1461. powerloss->run(
  1462. powerloss->cycles, powerloss->cycle_count,
  1463. suite, case_);
  1464. }
  1465. static void run(void) {
  1466. // ignore disconnected pipes
  1467. signal(SIGPIPE, SIG_IGN);
  1468. for (size_t t = 0; t < test_id_count; t++) {
  1469. for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
  1470. test_define_suite(&test_suites[i]);
  1471. for (size_t j = 0; j < test_suites[i].case_count; j++) {
  1472. // does neither suite nor case name match?
  1473. if (test_ids[t].name && !(
  1474. strcmp(test_ids[t].name,
  1475. test_suites[i].name) == 0
  1476. || strcmp(test_ids[t].name,
  1477. test_suites[i].cases[j].name) == 0)) {
  1478. continue;
  1479. }
  1480. case_forperm(
  1481. &test_suites[i],
  1482. &test_suites[i].cases[j],
  1483. test_ids[t].defines,
  1484. test_ids[t].define_count,
  1485. test_ids[t].cycles,
  1486. test_ids[t].cycle_count,
  1487. perm_run,
  1488. NULL);
  1489. }
  1490. }
  1491. }
  1492. }
  1493. // option handling
  1494. enum opt_flags {
  1495. OPT_HELP = 'h',
  1496. OPT_SUMMARY = 'Y',
  1497. OPT_LIST_SUITES = 'l',
  1498. OPT_LIST_CASES = 'L',
  1499. OPT_LIST_SUITE_PATHS = 1,
  1500. OPT_LIST_CASE_PATHS = 2,
  1501. OPT_LIST_DEFINES = 3,
  1502. OPT_LIST_PERMUTATION_DEFINES = 4,
  1503. OPT_LIST_IMPLICIT_DEFINES = 5,
  1504. OPT_LIST_GEOMETRIES = 6,
  1505. OPT_LIST_POWERLOSSES = 7,
  1506. OPT_DEFINE = 'D',
  1507. OPT_GEOMETRY = 'g',
  1508. OPT_POWERLOSS = 'p',
  1509. OPT_STEP = 's',
  1510. OPT_DISK = 'd',
  1511. OPT_TRACE = 't',
  1512. OPT_READ_SLEEP = 8,
  1513. OPT_PROG_SLEEP = 9,
  1514. OPT_ERASE_SLEEP = 10,
  1515. };
  1516. const char *short_opts = "hYlLD:g:p:s:d:t:";
  1517. const struct option long_opts[] = {
  1518. {"help", no_argument, NULL, OPT_HELP},
  1519. {"summary", no_argument, NULL, OPT_SUMMARY},
  1520. {"list-suites", no_argument, NULL, OPT_LIST_SUITES},
  1521. {"list-cases", no_argument, NULL, OPT_LIST_CASES},
  1522. {"list-suite-paths", no_argument, NULL, OPT_LIST_SUITE_PATHS},
  1523. {"list-case-paths", no_argument, NULL, OPT_LIST_CASE_PATHS},
  1524. {"list-defines", no_argument, NULL, OPT_LIST_DEFINES},
  1525. {"list-permutation-defines",
  1526. no_argument, NULL, OPT_LIST_PERMUTATION_DEFINES},
  1527. {"list-implicit-defines",
  1528. no_argument, NULL, OPT_LIST_IMPLICIT_DEFINES},
  1529. {"list-geometries", no_argument, NULL, OPT_LIST_GEOMETRIES},
  1530. {"list-powerlosses", no_argument, NULL, OPT_LIST_POWERLOSSES},
  1531. {"define", required_argument, NULL, OPT_DEFINE},
  1532. {"geometry", required_argument, NULL, OPT_GEOMETRY},
  1533. {"powerloss", required_argument, NULL, OPT_POWERLOSS},
  1534. {"step", required_argument, NULL, OPT_STEP},
  1535. {"disk", required_argument, NULL, OPT_DISK},
  1536. {"trace", required_argument, NULL, OPT_TRACE},
  1537. {"read-sleep", required_argument, NULL, OPT_READ_SLEEP},
  1538. {"prog-sleep", required_argument, NULL, OPT_PROG_SLEEP},
  1539. {"erase-sleep", required_argument, NULL, OPT_ERASE_SLEEP},
  1540. {NULL, 0, NULL, 0},
  1541. };
  1542. const char *const help_text[] = {
  1543. "Show this help message.",
  1544. "Show quick summary.",
  1545. "List test suites.",
  1546. "List test cases.",
  1547. "List the path for each test suite.",
  1548. "List the path and line number for each test case.",
  1549. "List all defines in this test-runner.",
  1550. "List explicit defines in this test-runner.",
  1551. "List implicit defines in this test-runner.",
  1552. "List the available disk geometries.",
  1553. "List the available power-loss scenarios.",
  1554. "Override a test define.",
  1555. "Comma-separated list of disk geometries to test. Defaults to d,e,E,n,N.",
  1556. "Comma-separated list of power-loss scenarios to test. Defaults to 0,l.",
  1557. "Comma-separated range of test permutations to run (start,stop,step).",
  1558. "Redirect block device operations to this file.",
  1559. "Redirect trace output to this file.",
  1560. "Artificial read delay in seconds.",
  1561. "Artificial prog delay in seconds.",
  1562. "Artificial erase delay in seconds.",
  1563. };
  1564. int main(int argc, char **argv) {
  1565. void (*op)(void) = run;
  1566. size_t test_override_capacity = 0;
  1567. size_t test_geometry_capacity = 0;
  1568. size_t test_powerloss_capacity = 0;
  1569. size_t test_id_capacity = 0;
  1570. // parse options
  1571. while (true) {
  1572. int c = getopt_long(argc, argv, short_opts, long_opts, NULL);
  1573. switch (c) {
  1574. // generate help message
  1575. case OPT_HELP: {
  1576. printf("usage: %s [options] [test_id]\n", argv[0]);
  1577. printf("\n");
  1578. printf("options:\n");
  1579. size_t i = 0;
  1580. while (long_opts[i].name) {
  1581. size_t indent;
  1582. if (long_opts[i].has_arg == no_argument) {
  1583. if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
  1584. indent = printf(" -%c, --%s ",
  1585. long_opts[i].val,
  1586. long_opts[i].name);
  1587. } else {
  1588. indent = printf(" --%s ",
  1589. long_opts[i].name);
  1590. }
  1591. } else {
  1592. if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
  1593. indent = printf(" -%c %s, --%s %s ",
  1594. long_opts[i].val,
  1595. long_opts[i].name,
  1596. long_opts[i].name,
  1597. long_opts[i].name);
  1598. } else {
  1599. indent = printf(" --%s %s ",
  1600. long_opts[i].name,
  1601. long_opts[i].name);
  1602. }
  1603. }
  1604. // a quick, hacky, byte-level method for text wrapping
  1605. size_t len = strlen(help_text[i]);
  1606. size_t j = 0;
  1607. if (indent < 24) {
  1608. printf("%*s %.80s\n",
  1609. (int)(24-1-indent),
  1610. "",
  1611. &help_text[i][j]);
  1612. j += 80;
  1613. } else {
  1614. printf("\n");
  1615. }
  1616. while (j < len) {
  1617. printf("%24s%.80s\n", "", &help_text[i][j]);
  1618. j += 80;
  1619. }
  1620. i += 1;
  1621. }
  1622. printf("\n");
  1623. exit(0);
  1624. }
  1625. // summary/list flags
  1626. case OPT_SUMMARY:
  1627. op = summary;
  1628. break;
  1629. case OPT_LIST_SUITES:
  1630. op = list_suites;
  1631. break;
  1632. case OPT_LIST_CASES:
  1633. op = list_cases;
  1634. break;
  1635. case OPT_LIST_SUITE_PATHS:
  1636. op = list_suite_paths;
  1637. break;
  1638. case OPT_LIST_CASE_PATHS:
  1639. op = list_case_paths;
  1640. break;
  1641. case OPT_LIST_DEFINES:
  1642. op = list_defines;
  1643. break;
  1644. case OPT_LIST_PERMUTATION_DEFINES:
  1645. op = list_permutation_defines;
  1646. break;
  1647. case OPT_LIST_IMPLICIT_DEFINES:
  1648. op = list_implicit_defines;
  1649. break;
  1650. case OPT_LIST_GEOMETRIES:
  1651. op = list_geometries;
  1652. break;
  1653. case OPT_LIST_POWERLOSSES:
  1654. op = list_powerlosses;
  1655. break;
  1656. // configuration
  1657. case OPT_DEFINE: {
  1658. // allocate space
  1659. test_override_t *override = mappend(
  1660. (void**)&test_overrides,
  1661. sizeof(test_override_t),
  1662. &test_override_count,
  1663. &test_override_capacity);
  1664. // parse into string key/intmax_t value, cannibalizing the
  1665. // arg in the process
  1666. char *sep = strchr(optarg, '=');
  1667. char *parsed = NULL;
  1668. if (!sep) {
  1669. goto invalid_define;
  1670. }
  1671. *sep = '\0';
  1672. override->name = optarg;
  1673. optarg = sep+1;
  1674. // parse comma-separated permutations
  1675. {
  1676. override->defines = NULL;
  1677. override->permutations = 0;
  1678. size_t override_capacity = 0;
  1679. while (true) {
  1680. optarg += strspn(optarg, " ");
  1681. if (strncmp(optarg, "range", strlen("range")) == 0) {
  1682. // range of values
  1683. optarg += strlen("range");
  1684. optarg += strspn(optarg, " ");
  1685. if (*optarg != '(') {
  1686. goto invalid_define;
  1687. }
  1688. optarg += 1;
  1689. intmax_t start = strtoumax(optarg, &parsed, 0);
  1690. intmax_t stop = -1;
  1691. intmax_t step = 1;
  1692. // allow empty string for start=0
  1693. if (parsed == optarg) {
  1694. start = 0;
  1695. }
  1696. optarg = parsed + strspn(parsed, " ");
  1697. if (*optarg != ',' && *optarg != ')') {
  1698. goto invalid_define;
  1699. }
  1700. if (*optarg == ',') {
  1701. optarg += 1;
  1702. stop = strtoumax(optarg, &parsed, 0);
  1703. // allow empty string for stop=end
  1704. if (parsed == optarg) {
  1705. stop = -1;
  1706. }
  1707. optarg = parsed + strspn(parsed, " ");
  1708. if (*optarg != ',' && *optarg != ')') {
  1709. goto invalid_define;
  1710. }
  1711. if (*optarg == ',') {
  1712. optarg += 1;
  1713. step = strtoumax(optarg, &parsed, 0);
  1714. // allow empty string for stop=1
  1715. if (parsed == optarg) {
  1716. step = 1;
  1717. }
  1718. optarg = parsed + strspn(parsed, " ");
  1719. if (*optarg != ')') {
  1720. goto invalid_define;
  1721. }
  1722. }
  1723. } else {
  1724. // single value = stop only
  1725. stop = start;
  1726. start = 0;
  1727. }
  1728. if (*optarg != ')') {
  1729. goto invalid_define;
  1730. }
  1731. optarg += 1;
  1732. // calculate the range of values
  1733. assert(step != 0);
  1734. for (intmax_t i = start;
  1735. (step < 0)
  1736. ? i > stop
  1737. : (uintmax_t)i < (uintmax_t)stop;
  1738. i += step) {
  1739. *(intmax_t*)mappend(
  1740. (void**)&override->defines,
  1741. sizeof(intmax_t),
  1742. &override->permutations,
  1743. &override_capacity) = i;
  1744. }
  1745. } else if (*optarg != '\0') {
  1746. // single value
  1747. intmax_t define = strtoimax(optarg, &parsed, 0);
  1748. if (parsed == optarg) {
  1749. goto invalid_define;
  1750. }
  1751. optarg = parsed + strspn(parsed, " ");
  1752. *(intmax_t*)mappend(
  1753. (void**)&override->defines,
  1754. sizeof(intmax_t),
  1755. &override->permutations,
  1756. &override_capacity) = define;
  1757. } else {
  1758. break;
  1759. }
  1760. if (*optarg == ',') {
  1761. optarg += 1;
  1762. }
  1763. }
  1764. }
  1765. assert(override->permutations > 0);
  1766. break;
  1767. invalid_define:
  1768. fprintf(stderr, "error: invalid define: %s\n", optarg);
  1769. exit(-1);
  1770. }
  1771. case OPT_GEOMETRY: {
  1772. // reset our geometry scenarios
  1773. if (test_geometry_capacity > 0) {
  1774. free((test_geometry_t*)test_geometries);
  1775. }
  1776. test_geometries = NULL;
  1777. test_geometry_count = 0;
  1778. test_geometry_capacity = 0;
  1779. // parse the comma separated list of disk geometries
  1780. while (*optarg) {
  1781. // allocate space
  1782. test_geometry_t *geometry = mappend(
  1783. (void**)&test_geometries,
  1784. sizeof(test_geometry_t),
  1785. &test_geometry_count,
  1786. &test_geometry_capacity);
  1787. // parse the disk geometry
  1788. optarg += strspn(optarg, " ");
  1789. // named disk geometry
  1790. size_t len = strcspn(optarg, " ,");
  1791. for (size_t i = 0; builtin_geometries[i].long_name; i++) {
  1792. if ((len == 1
  1793. && *optarg == builtin_geometries[i].short_name)
  1794. || (len == strlen(
  1795. builtin_geometries[i].long_name)
  1796. && memcmp(optarg,
  1797. builtin_geometries[i].long_name,
  1798. len) == 0)) {
  1799. *geometry = builtin_geometries[i];
  1800. optarg += len;
  1801. goto geometry_next;
  1802. }
  1803. }
  1804. // comma-separated read/prog/erase/count
  1805. if (*optarg == '{') {
  1806. lfs_size_t sizes[4];
  1807. size_t count = 0;
  1808. char *s = optarg + 1;
  1809. while (count < 4) {
  1810. char *parsed = NULL;
  1811. sizes[count] = strtoumax(s, &parsed, 0);
  1812. count += 1;
  1813. s = parsed + strspn(parsed, " ");
  1814. if (*s == ',') {
  1815. s += 1;
  1816. continue;
  1817. } else if (*s == '}') {
  1818. s += 1;
  1819. break;
  1820. } else {
  1821. goto geometry_unknown;
  1822. }
  1823. }
  1824. // allow implicit r=p and p=e for common geometries
  1825. memset(geometry, 0, sizeof(test_geometry_t));
  1826. if (count >= 3) {
  1827. geometry->defines[READ_SIZE_i]
  1828. = TEST_LIT(sizes[0]);
  1829. geometry->defines[PROG_SIZE_i]
  1830. = TEST_LIT(sizes[1]);
  1831. geometry->defines[BLOCK_SIZE_i]
  1832. = TEST_LIT(sizes[2]);
  1833. } else if (count >= 2) {
  1834. geometry->defines[PROG_SIZE_i]
  1835. = TEST_LIT(sizes[0]);
  1836. geometry->defines[BLOCK_SIZE_i]
  1837. = TEST_LIT(sizes[1]);
  1838. } else {
  1839. geometry->defines[BLOCK_SIZE_i]
  1840. = TEST_LIT(sizes[0]);
  1841. }
  1842. if (count >= 4) {
  1843. geometry->defines[BLOCK_COUNT_i]
  1844. = TEST_LIT(sizes[3]);
  1845. }
  1846. optarg = s;
  1847. goto geometry_next;
  1848. }
  1849. // leb16-encoded read/prog/erase/count
  1850. if (*optarg == ':') {
  1851. lfs_size_t sizes[4];
  1852. size_t count = 0;
  1853. char *s = optarg + 1;
  1854. while (true) {
  1855. char *parsed = NULL;
  1856. uintmax_t x = leb16_parse(s, &parsed);
  1857. if (parsed == s || count >= 4) {
  1858. break;
  1859. }
  1860. sizes[count] = x;
  1861. count += 1;
  1862. s = parsed;
  1863. }
  1864. // allow implicit r=p and p=e for common geometries
  1865. memset(geometry, 0, sizeof(test_geometry_t));
  1866. if (count >= 3) {
  1867. geometry->defines[READ_SIZE_i]
  1868. = TEST_LIT(sizes[0]);
  1869. geometry->defines[PROG_SIZE_i]
  1870. = TEST_LIT(sizes[1]);
  1871. geometry->defines[BLOCK_SIZE_i]
  1872. = TEST_LIT(sizes[2]);
  1873. } else if (count >= 2) {
  1874. geometry->defines[PROG_SIZE_i]
  1875. = TEST_LIT(sizes[0]);
  1876. geometry->defines[BLOCK_SIZE_i]
  1877. = TEST_LIT(sizes[1]);
  1878. } else {
  1879. geometry->defines[BLOCK_SIZE_i]
  1880. = TEST_LIT(sizes[0]);
  1881. }
  1882. if (count >= 4) {
  1883. geometry->defines[BLOCK_COUNT_i]
  1884. = TEST_LIT(sizes[3]);
  1885. }
  1886. optarg = s;
  1887. goto geometry_next;
  1888. }
  1889. geometry_unknown:
  1890. // unknown scenario?
  1891. fprintf(stderr, "error: unknown disk geometry: %s\n",
  1892. optarg);
  1893. exit(-1);
  1894. geometry_next:
  1895. optarg += strspn(optarg, " ");
  1896. if (*optarg == ',') {
  1897. optarg += 1;
  1898. } else if (*optarg == '\0') {
  1899. break;
  1900. } else {
  1901. goto geometry_unknown;
  1902. }
  1903. }
  1904. break;
  1905. }
  1906. case OPT_POWERLOSS: {
  1907. // reset our powerloss scenarios
  1908. if (test_powerloss_capacity > 0) {
  1909. free((test_powerloss_t*)test_powerlosses);
  1910. }
  1911. test_powerlosses = NULL;
  1912. test_powerloss_count = 0;
  1913. test_powerloss_capacity = 0;
  1914. // parse the comma separated list of power-loss scenarios
  1915. while (*optarg) {
  1916. // allocate space
  1917. test_powerloss_t *powerloss = mappend(
  1918. (void**)&test_powerlosses,
  1919. sizeof(test_powerloss_t),
  1920. &test_powerloss_count,
  1921. &test_powerloss_capacity);
  1922. // parse the power-loss scenario
  1923. optarg += strspn(optarg, " ");
  1924. // named power-loss scenario
  1925. size_t len = strcspn(optarg, " ,");
  1926. for (size_t i = 0; builtin_powerlosses[i].long_name; i++) {
  1927. if ((len == 1
  1928. && *optarg == builtin_powerlosses[i].short_name)
  1929. || (len == strlen(
  1930. builtin_powerlosses[i].long_name)
  1931. && memcmp(optarg,
  1932. builtin_powerlosses[i].long_name,
  1933. len) == 0)) {
  1934. *powerloss = builtin_powerlosses[i];
  1935. optarg += len;
  1936. goto powerloss_next;
  1937. }
  1938. }
  1939. // comma-separated permutation
  1940. if (*optarg == '{') {
  1941. lfs_emubd_powercycles_t *cycles = NULL;
  1942. size_t cycle_count = 0;
  1943. size_t cycle_capacity = 0;
  1944. char *s = optarg + 1;
  1945. while (true) {
  1946. char *parsed = NULL;
  1947. *(lfs_emubd_powercycles_t*)mappend(
  1948. (void**)&cycles,
  1949. sizeof(lfs_emubd_powercycles_t),
  1950. &cycle_count,
  1951. &cycle_capacity)
  1952. = strtoumax(s, &parsed, 0);
  1953. s = parsed + strspn(parsed, " ");
  1954. if (*s == ',') {
  1955. s += 1;
  1956. continue;
  1957. } else if (*s == '}') {
  1958. s += 1;
  1959. break;
  1960. } else {
  1961. goto powerloss_unknown;
  1962. }
  1963. }
  1964. *powerloss = (test_powerloss_t){
  1965. .run = run_powerloss_cycles,
  1966. .cycles = cycles,
  1967. .cycle_count = cycle_count,
  1968. };
  1969. optarg = s;
  1970. goto powerloss_next;
  1971. }
  1972. // leb16-encoded permutation
  1973. if (*optarg == ':') {
  1974. lfs_emubd_powercycles_t *cycles = NULL;
  1975. size_t cycle_count = 0;
  1976. size_t cycle_capacity = 0;
  1977. char *s = optarg + 1;
  1978. while (true) {
  1979. char *parsed = NULL;
  1980. uintmax_t x = leb16_parse(s, &parsed);
  1981. if (parsed == s) {
  1982. break;
  1983. }
  1984. *(lfs_emubd_powercycles_t*)mappend(
  1985. (void**)&cycles,
  1986. sizeof(lfs_emubd_powercycles_t),
  1987. &cycle_count,
  1988. &cycle_capacity) = x;
  1989. s = parsed;
  1990. }
  1991. *powerloss = (test_powerloss_t){
  1992. .run = run_powerloss_cycles,
  1993. .cycles = cycles,
  1994. .cycle_count = cycle_count,
  1995. };
  1996. optarg = s;
  1997. goto powerloss_next;
  1998. }
  1999. // exhaustive permutations
  2000. {
  2001. char *parsed = NULL;
  2002. size_t count = strtoumax(optarg, &parsed, 0);
  2003. if (parsed == optarg) {
  2004. goto powerloss_unknown;
  2005. }
  2006. *powerloss = (test_powerloss_t){
  2007. .run = run_powerloss_exhaustive,
  2008. .cycles = NULL,
  2009. .cycle_count = count,
  2010. };
  2011. optarg = (char*)parsed;
  2012. goto powerloss_next;
  2013. }
  2014. powerloss_unknown:
  2015. // unknown scenario?
  2016. fprintf(stderr, "error: unknown power-loss scenario: %s\n",
  2017. optarg);
  2018. exit(-1);
  2019. powerloss_next:
  2020. optarg += strspn(optarg, " ");
  2021. if (*optarg == ',') {
  2022. optarg += 1;
  2023. } else if (*optarg == '\0') {
  2024. break;
  2025. } else {
  2026. goto powerloss_unknown;
  2027. }
  2028. }
  2029. break;
  2030. }
  2031. case OPT_STEP: {
  2032. char *parsed = NULL;
  2033. test_step_start = strtoumax(optarg, &parsed, 0);
  2034. test_step_stop = -1;
  2035. test_step_step = 1;
  2036. // allow empty string for start=0
  2037. if (parsed == optarg) {
  2038. test_step_start = 0;
  2039. }
  2040. optarg = parsed + strspn(parsed, " ");
  2041. if (*optarg != ',' && *optarg != '\0') {
  2042. goto step_unknown;
  2043. }
  2044. if (*optarg == ',') {
  2045. optarg += 1;
  2046. test_step_stop = strtoumax(optarg, &parsed, 0);
  2047. // allow empty string for stop=end
  2048. if (parsed == optarg) {
  2049. test_step_stop = -1;
  2050. }
  2051. optarg = parsed + strspn(parsed, " ");
  2052. if (*optarg != ',' && *optarg != '\0') {
  2053. goto step_unknown;
  2054. }
  2055. if (*optarg == ',') {
  2056. optarg += 1;
  2057. test_step_step = strtoumax(optarg, &parsed, 0);
  2058. // allow empty string for stop=1
  2059. if (parsed == optarg) {
  2060. test_step_step = 1;
  2061. }
  2062. optarg = parsed + strspn(parsed, " ");
  2063. if (*optarg != '\0') {
  2064. goto step_unknown;
  2065. }
  2066. }
  2067. } else {
  2068. // single value = stop only
  2069. test_step_stop = test_step_start;
  2070. test_step_start = 0;
  2071. }
  2072. break;
  2073. step_unknown:
  2074. fprintf(stderr, "error: invalid step: %s\n", optarg);
  2075. exit(-1);
  2076. }
  2077. case OPT_DISK:
  2078. test_disk_path = optarg;
  2079. break;
  2080. case OPT_TRACE:
  2081. test_trace_path = optarg;
  2082. break;
  2083. case OPT_READ_SLEEP: {
  2084. char *parsed = NULL;
  2085. double read_sleep = strtod(optarg, &parsed);
  2086. if (parsed == optarg) {
  2087. fprintf(stderr, "error: invalid read-sleep: %s\n", optarg);
  2088. exit(-1);
  2089. }
  2090. test_read_sleep = read_sleep*1.0e9;
  2091. break;
  2092. }
  2093. case OPT_PROG_SLEEP: {
  2094. char *parsed = NULL;
  2095. double prog_sleep = strtod(optarg, &parsed);
  2096. if (parsed == optarg) {
  2097. fprintf(stderr, "error: invalid prog-sleep: %s\n", optarg);
  2098. exit(-1);
  2099. }
  2100. test_prog_sleep = prog_sleep*1.0e9;
  2101. break;
  2102. }
  2103. case OPT_ERASE_SLEEP: {
  2104. char *parsed = NULL;
  2105. double erase_sleep = strtod(optarg, &parsed);
  2106. if (parsed == optarg) {
  2107. fprintf(stderr, "error: invalid erase-sleep: %s\n", optarg);
  2108. exit(-1);
  2109. }
  2110. test_erase_sleep = erase_sleep*1.0e9;
  2111. break;
  2112. }
  2113. // done parsing
  2114. case -1:
  2115. goto getopt_done;
  2116. // unknown arg, getopt prints a message for us
  2117. default:
  2118. exit(-1);
  2119. }
  2120. }
  2121. getopt_done: ;
  2122. if (argc > optind) {
  2123. // reset our test identifier list
  2124. test_ids = NULL;
  2125. test_id_count = 0;
  2126. test_id_capacity = 0;
  2127. }
  2128. // parse test identifier, if any, cannibalizing the arg in the process
  2129. for (; argc > optind; optind++) {
  2130. test_define_t *defines = NULL;
  2131. size_t define_count = 0;
  2132. lfs_emubd_powercycles_t *cycles = NULL;
  2133. size_t cycle_count = 0;
  2134. // parse name, can be suite or case
  2135. char *name = argv[optind];
  2136. char *defines_ = strchr(name, ':');
  2137. if (defines_) {
  2138. *defines_ = '\0';
  2139. defines_ += 1;
  2140. }
  2141. // remove optional path and .toml suffix
  2142. char *slash = strrchr(name, '/');
  2143. if (slash) {
  2144. name = slash+1;
  2145. }
  2146. size_t name_len = strlen(name);
  2147. if (name_len > 5 && strcmp(&name[name_len-5], ".toml") == 0) {
  2148. name[name_len-5] = '\0';
  2149. }
  2150. if (defines_) {
  2151. // parse defines
  2152. char *cycles_ = strchr(defines_, ':');
  2153. if (cycles_) {
  2154. *cycles_ = '\0';
  2155. cycles_ += 1;
  2156. }
  2157. while (true) {
  2158. char *parsed;
  2159. size_t d = leb16_parse(defines_, &parsed);
  2160. intmax_t v = leb16_parse(parsed, &parsed);
  2161. if (parsed == defines_) {
  2162. break;
  2163. }
  2164. defines_ = parsed;
  2165. if (d >= define_count) {
  2166. // align to power of two to avoid any superlinear growth
  2167. size_t ncount = 1 << lfs_npw2(d+1);
  2168. defines = realloc(defines,
  2169. ncount*sizeof(test_define_t));
  2170. memset(defines+define_count, 0,
  2171. (ncount-define_count)*sizeof(test_define_t));
  2172. define_count = ncount;
  2173. }
  2174. defines[d] = TEST_LIT(v);
  2175. }
  2176. if (cycles_) {
  2177. // parse power cycles
  2178. size_t cycle_capacity = 0;
  2179. while (*cycles_ != '\0') {
  2180. char *parsed = NULL;
  2181. *(lfs_emubd_powercycles_t*)mappend(
  2182. (void**)&cycles,
  2183. sizeof(lfs_emubd_powercycles_t),
  2184. &cycle_count,
  2185. &cycle_capacity)
  2186. = leb16_parse(cycles_, &parsed);
  2187. if (parsed == cycles_) {
  2188. fprintf(stderr, "error: "
  2189. "could not parse test cycles: %s\n",
  2190. cycles_);
  2191. exit(-1);
  2192. }
  2193. cycles_ = parsed;
  2194. }
  2195. }
  2196. }
  2197. // append to identifier list
  2198. *(test_id_t*)mappend(
  2199. (void**)&test_ids,
  2200. sizeof(test_id_t),
  2201. &test_id_count,
  2202. &test_id_capacity) = (test_id_t){
  2203. .name = name,
  2204. .defines = defines,
  2205. .define_count = define_count,
  2206. .cycles = cycles,
  2207. .cycle_count = cycle_count,
  2208. };
  2209. }
  2210. // do the thing
  2211. op();
  2212. // cleanup (need to be done for valgrind testing)
  2213. test_define_cleanup();
  2214. if (test_overrides) {
  2215. for (size_t i = 0; i < test_override_count; i++) {
  2216. free((void*)test_overrides[i].defines);
  2217. }
  2218. free((void*)test_overrides);
  2219. }
  2220. if (test_geometry_capacity) {
  2221. free((void*)test_geometries);
  2222. }
  2223. if (test_powerloss_capacity) {
  2224. for (size_t i = 0; i < test_powerloss_count; i++) {
  2225. free((void*)test_powerlosses[i].cycles);
  2226. }
  2227. free((void*)test_powerlosses);
  2228. }
  2229. if (test_id_capacity) {
  2230. for (size_t i = 0; i < test_id_count; i++) {
  2231. free((void*)test_ids[i].defines);
  2232. free((void*)test_ids[i].cycles);
  2233. }
  2234. free((void*)test_ids);
  2235. }
  2236. }