test_runner.c 91 KB

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