| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818 |
- /*
- * Runner for littlefs tests
- *
- * Copyright (c) 2022, The littlefs authors.
- * SPDX-License-Identifier: BSD-3-Clause
- */
- #ifndef _POSIX_C_SOURCE
- #define _POSIX_C_SOURCE 199309L
- #endif
- #include "runners/test_runner.h"
- #include "bd/lfs_emubd.h"
- #include <getopt.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <setjmp.h>
- #include <fcntl.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <time.h>
- #include <execinfo.h>
- // some helpers
- // append to an array with amortized doubling
- void *mappend(void **p,
- size_t size,
- size_t *count,
- size_t *capacity) {
- uint8_t *p_ = *p;
- size_t count_ = *count;
- size_t capacity_ = *capacity;
- count_ += 1;
- if (count_ > capacity_) {
- capacity_ = (2*capacity_ < 4) ? 4 : 2*capacity_;
- p_ = realloc(p_, capacity_*size);
- if (!p_) {
- return NULL;
- }
- }
- *p = p_;
- *count = count_;
- *capacity = capacity_;
- return &p_[(count_-1)*size];
- }
- // a quick self-terminating text-safe varint scheme
- static void leb16_print(uintmax_t x) {
- // allow 'w' to indicate negative numbers
- if ((intmax_t)x < 0) {
- printf("w");
- x = -x;
- }
- while (true) {
- char nibble = (x & 0xf) | (x > 0xf ? 0x10 : 0);
- printf("%c", (nibble < 10) ? '0'+nibble : 'a'+nibble-10);
- if (x <= 0xf) {
- break;
- }
- x >>= 4;
- }
- }
- static uintmax_t leb16_parse(const char *s, char **tail) {
- bool neg = false;
- uintmax_t x = 0;
- if (tail) {
- *tail = (char*)s;
- }
- if (s[0] == 'w') {
- neg = true;
- s = s+1;
- }
- size_t i = 0;
- while (true) {
- uintmax_t nibble = s[i];
- if (nibble >= '0' && nibble <= '9') {
- nibble = nibble - '0';
- } else if (nibble >= 'a' && nibble <= 'v') {
- nibble = nibble - 'a' + 10;
- } else {
- // invalid?
- return 0;
- }
- x |= (nibble & 0xf) << (4*i);
- i += 1;
- if (!(nibble & 0x10)) {
- s = s + i;
- break;
- }
- }
- if (tail) {
- *tail = (char*)s;
- }
- return neg ? -x : x;
- }
- // test_runner types
- typedef struct test_geometry {
- const char *name;
- test_define_t defines[TEST_GEOMETRY_DEFINE_COUNT];
- } test_geometry_t;
- typedef struct test_powerloss {
- const char *name;
- void (*run)(
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count,
- const struct test_suite *suite,
- const struct test_case *case_);
- const lfs_emubd_powercycles_t *cycles;
- size_t cycle_count;
- } test_powerloss_t;
- typedef struct test_id {
- const char *name;
- const test_define_t *defines;
- size_t define_count;
- const lfs_emubd_powercycles_t *cycles;
- size_t cycle_count;
- } test_id_t;
- // test suites are linked into a custom ld section
- #if defined(__APPLE__)
- extern struct test_suite __start__test_suites __asm("section$start$__DATA$_test_suites");
- extern struct test_suite __stop__test_suites __asm("section$end$__DATA$_test_suites");
- #else
- extern struct test_suite __start__test_suites;
- extern struct test_suite __stop__test_suites;
- #endif
- const struct test_suite *test_suites = &__start__test_suites;
- #define TEST_SUITE_COUNT \
- ((size_t)(&__stop__test_suites - &__start__test_suites))
- // test define management
- typedef struct test_define_map {
- const test_define_t *defines;
- size_t count;
- } test_define_map_t;
- typedef struct test_define_names {
- const char *const *names;
- size_t count;
- } test_define_names_t;
- intmax_t test_define_lit(void *data) {
- return (intptr_t)data;
- }
- #define TEST_CONST(x) {test_define_lit, (void*)(uintptr_t)(x)}
- #define TEST_LIT(x) ((test_define_t)TEST_CONST(x))
- #define TEST_DEF(k, v) \
- intmax_t test_define_##k(void *data) { \
- (void)data; \
- return v; \
- }
- TEST_IMPLICIT_DEFINES
- #undef TEST_DEF
- #define TEST_DEFINE_MAP_OVERRIDE 0
- #define TEST_DEFINE_MAP_EXPLICIT 1
- #define TEST_DEFINE_MAP_PERMUTATION 2
- #define TEST_DEFINE_MAP_GEOMETRY 3
- #define TEST_DEFINE_MAP_IMPLICIT 4
- #define TEST_DEFINE_MAP_COUNT 5
- test_define_map_t test_define_maps[TEST_DEFINE_MAP_COUNT] = {
- [TEST_DEFINE_MAP_IMPLICIT] = {
- (const test_define_t[TEST_IMPLICIT_DEFINE_COUNT]) {
- #define TEST_DEF(k, v) \
- [k##_i] = {test_define_##k, NULL},
- TEST_IMPLICIT_DEFINES
- #undef TEST_DEF
- },
- TEST_IMPLICIT_DEFINE_COUNT,
- },
- };
- #define TEST_DEFINE_NAMES_SUITE 0
- #define TEST_DEFINE_NAMES_IMPLICIT 1
- #define TEST_DEFINE_NAMES_COUNT 2
- test_define_names_t test_define_names[TEST_DEFINE_NAMES_COUNT] = {
- [TEST_DEFINE_NAMES_IMPLICIT] = {
- (const char *const[TEST_IMPLICIT_DEFINE_COUNT]){
- #define TEST_DEF(k, v) \
- [k##_i] = #k,
- TEST_IMPLICIT_DEFINES
- #undef TEST_DEF
- },
- TEST_IMPLICIT_DEFINE_COUNT,
- },
- };
- intmax_t *test_define_cache;
- size_t test_define_cache_count;
- unsigned *test_define_cache_mask;
- const char *test_define_name(size_t define) {
- // lookup in our test names
- for (size_t i = 0; i < TEST_DEFINE_NAMES_COUNT; i++) {
- if (define < test_define_names[i].count
- && test_define_names[i].names
- && test_define_names[i].names[define]) {
- return test_define_names[i].names[define];
- }
- }
- return NULL;
- }
- bool test_define_ispermutation(size_t define) {
- // is this define specific to the permutation?
- for (size_t i = 0; i < TEST_DEFINE_MAP_IMPLICIT; i++) {
- if (define < test_define_maps[i].count
- && test_define_maps[i].defines[define].cb) {
- return true;
- }
- }
- return false;
- }
- intmax_t test_define(size_t define) {
- // is the define in our cache?
- if (define < test_define_cache_count
- && (test_define_cache_mask[define/(8*sizeof(unsigned))]
- & (1 << (define%(8*sizeof(unsigned)))))) {
- return test_define_cache[define];
- }
- // lookup in our test defines
- for (size_t i = 0; i < TEST_DEFINE_MAP_COUNT; i++) {
- if (define < test_define_maps[i].count
- && test_define_maps[i].defines[define].cb) {
- intmax_t v = test_define_maps[i].defines[define].cb(
- test_define_maps[i].defines[define].data);
- // insert into cache!
- test_define_cache[define] = v;
- test_define_cache_mask[define / (8*sizeof(unsigned))]
- |= 1 << (define%(8*sizeof(unsigned)));
- return v;
- }
- }
- return 0;
- // not found?
- const char *name = test_define_name(define);
- fprintf(stderr, "error: undefined define %s (%zd)\n",
- name ? name : "(unknown)",
- define);
- assert(false);
- exit(-1);
- }
- void test_define_flush(void) {
- // clear cache between permutations
- memset(test_define_cache_mask, 0,
- sizeof(unsigned)*(
- (test_define_cache_count+(8*sizeof(unsigned))-1)
- / (8*sizeof(unsigned))));
- }
- // geometry updates
- const test_geometry_t *test_geometry = NULL;
- void test_define_geometry(const test_geometry_t *geometry) {
- test_define_maps[TEST_DEFINE_MAP_GEOMETRY] = (test_define_map_t){
- geometry->defines, TEST_GEOMETRY_DEFINE_COUNT};
- }
- // override updates
- typedef struct test_override {
- const char *name;
- const intmax_t *defines;
- size_t permutations;
- } test_override_t;
- const test_override_t *test_overrides = NULL;
- size_t test_override_count = 0;
- test_define_t *test_override_defines = NULL;
- size_t test_override_define_count = 0;
- size_t test_override_define_permutations = 1;
- size_t test_override_define_capacity = 0;
- // suite/perm updates
- void test_define_suite(const struct test_suite *suite) {
- test_define_names[TEST_DEFINE_NAMES_SUITE] = (test_define_names_t){
- suite->define_names, suite->define_count};
- // make sure our cache is large enough
- if (lfs_max(suite->define_count, TEST_IMPLICIT_DEFINE_COUNT)
- > test_define_cache_count) {
- // align to power of two to avoid any superlinear growth
- size_t ncount = 1 << lfs_npw2(
- lfs_max(suite->define_count, TEST_IMPLICIT_DEFINE_COUNT));
- test_define_cache = realloc(test_define_cache, ncount*sizeof(intmax_t));
- test_define_cache_mask = realloc(test_define_cache_mask,
- sizeof(unsigned)*(
- (ncount+(8*sizeof(unsigned))-1)
- / (8*sizeof(unsigned))));
- test_define_cache_count = ncount;
- }
- // map any overrides
- if (test_override_count > 0) {
- // first figure out the total size of override permutations
- size_t count = 0;
- size_t permutations = 1;
- for (size_t i = 0; i < test_override_count; i++) {
- for (size_t d = 0;
- d < lfs_max(
- suite->define_count,
- TEST_IMPLICIT_DEFINE_COUNT);
- d++) {
- // define name match?
- const char *name = test_define_name(d);
- if (name && strcmp(name, test_overrides[i].name) == 0) {
- count = lfs_max(count, d+1);
- permutations *= test_overrides[i].permutations;
- break;
- }
- }
- }
- test_override_define_count = count;
- test_override_define_permutations = permutations;
- // make sure our override arrays are big enough
- if (count * permutations > test_override_define_capacity) {
- // align to power of two to avoid any superlinear growth
- size_t ncapacity = 1 << lfs_npw2(count * permutations);
- test_override_defines = realloc(
- test_override_defines,
- sizeof(test_define_t)*ncapacity);
- test_override_define_capacity = ncapacity;
- }
- // zero unoverridden defines
- memset(test_override_defines, 0,
- sizeof(test_define_t) * count * permutations);
- // compute permutations
- size_t p = 1;
- for (size_t i = 0; i < test_override_count; i++) {
- for (size_t d = 0;
- d < lfs_max(
- suite->define_count,
- TEST_IMPLICIT_DEFINE_COUNT);
- d++) {
- // define name match?
- const char *name = test_define_name(d);
- if (name && strcmp(name, test_overrides[i].name) == 0) {
- // scatter the define permutations based on already
- // seen permutations
- for (size_t j = 0; j < permutations; j++) {
- test_override_defines[j*count + d] = TEST_LIT(
- test_overrides[i].defines[(j/p)
- % test_overrides[i].permutations]);
- }
- // keep track of how many permutations we've seen so far
- p *= test_overrides[i].permutations;
- break;
- }
- }
- }
- }
- }
- void test_define_perm(
- const struct test_suite *suite,
- const struct test_case *case_,
- size_t perm) {
- if (case_->defines) {
- test_define_maps[TEST_DEFINE_MAP_PERMUTATION] = (test_define_map_t){
- case_->defines + perm*suite->define_count,
- suite->define_count};
- } else {
- test_define_maps[TEST_DEFINE_MAP_PERMUTATION] = (test_define_map_t){
- NULL, 0};
- }
- }
- void test_define_override(size_t perm) {
- test_define_maps[TEST_DEFINE_MAP_OVERRIDE] = (test_define_map_t){
- test_override_defines + perm*test_override_define_count,
- test_override_define_count};
- }
- void test_define_explicit(
- const test_define_t *defines,
- size_t define_count) {
- test_define_maps[TEST_DEFINE_MAP_EXPLICIT] = (test_define_map_t){
- defines, define_count};
- }
- void test_define_cleanup(void) {
- // test define management can allocate a few things
- free(test_define_cache);
- free(test_define_cache_mask);
- free(test_override_defines);
- }
- // test state
- extern const test_geometry_t *test_geometries;
- extern size_t test_geometry_count;
- extern const test_powerloss_t *test_powerlosses;
- extern size_t test_powerloss_count;
- const test_id_t *test_ids = (const test_id_t[]) {
- {NULL, NULL, 0, NULL, 0},
- };
- size_t test_id_count = 1;
- size_t test_step_start = 0;
- size_t test_step_stop = -1;
- size_t test_step_step = 1;
- const char *test_disk_path = NULL;
- const char *test_trace_path = NULL;
- bool test_trace_backtrace = false;
- uint32_t test_trace_period = 0;
- uint32_t test_trace_freq = 0;
- FILE *test_trace_file = NULL;
- uint32_t test_trace_cycles = 0;
- uint64_t test_trace_time = 0;
- uint64_t test_trace_open_time = 0;
- lfs_emubd_sleep_t test_read_sleep = 0.0;
- lfs_emubd_sleep_t test_prog_sleep = 0.0;
- lfs_emubd_sleep_t test_erase_sleep = 0.0;
- // this determines both the backtrace buffer and the trace printf buffer, if
- // trace ends up interleaved or truncated this may need to be increased
- #ifndef TEST_TRACE_BACKTRACE_BUFFER_SIZE
- #define TEST_TRACE_BACKTRACE_BUFFER_SIZE 8192
- #endif
- void *test_trace_backtrace_buffer[
- TEST_TRACE_BACKTRACE_BUFFER_SIZE / sizeof(void*)];
- // trace printing
- void test_trace(const char *fmt, ...) {
- if (test_trace_path) {
- // sample at a specific period?
- if (test_trace_period) {
- if (test_trace_cycles % test_trace_period != 0) {
- test_trace_cycles += 1;
- return;
- }
- test_trace_cycles += 1;
- }
- // sample at a specific frequency?
- if (test_trace_freq) {
- struct timespec t;
- clock_gettime(CLOCK_MONOTONIC, &t);
- uint64_t now = (uint64_t)t.tv_sec*1000*1000*1000
- + (uint64_t)t.tv_nsec;
- if (now - test_trace_time < (1000*1000*1000) / test_trace_freq) {
- return;
- }
- test_trace_time = now;
- }
- if (!test_trace_file) {
- // Tracing output is heavy and trying to open every trace
- // call is slow, so we only try to open the trace file every
- // so often. Note this doesn't affect successfully opened files
- struct timespec t;
- clock_gettime(CLOCK_MONOTONIC, &t);
- uint64_t now = (uint64_t)t.tv_sec*1000*1000*1000
- + (uint64_t)t.tv_nsec;
- if (now - test_trace_open_time < 100*1000*1000) {
- return;
- }
- test_trace_open_time = now;
- // try to open the trace file
- int fd;
- if (strcmp(test_trace_path, "-") == 0) {
- fd = dup(1);
- if (fd < 0) {
- return;
- }
- } else {
- fd = open(
- test_trace_path,
- O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK,
- 0666);
- if (fd < 0) {
- return;
- }
- int err = fcntl(fd, F_SETFL, O_WRONLY | O_CREAT | O_APPEND);
- assert(!err);
- }
- FILE *f = fdopen(fd, "a");
- assert(f);
- int err = setvbuf(f, NULL, _IOFBF,
- TEST_TRACE_BACKTRACE_BUFFER_SIZE);
- assert(!err);
- test_trace_file = f;
- }
- // print trace
- va_list va;
- va_start(va, fmt);
- int res = vfprintf(test_trace_file, fmt, va);
- va_end(va);
- if (res < 0) {
- fclose(test_trace_file);
- test_trace_file = NULL;
- return;
- }
- if (test_trace_backtrace) {
- // print backtrace
- size_t count = backtrace(
- test_trace_backtrace_buffer,
- TEST_TRACE_BACKTRACE_BUFFER_SIZE);
- // note we skip our own stack frame
- for (size_t i = 1; i < count; i++) {
- res = fprintf(test_trace_file, "\tat %p\n",
- test_trace_backtrace_buffer[i]);
- if (res < 0) {
- fclose(test_trace_file);
- test_trace_file = NULL;
- return;
- }
- }
- }
- // flush immediately
- fflush(test_trace_file);
- }
- }
- // test prng
- uint32_t test_prng(uint32_t *state) {
- // A simple xorshift32 generator, easily reproducible. Keep in mind
- // determinism is much more important than actual randomness here.
- uint32_t x = *state;
- x ^= x << 13;
- x ^= x >> 17;
- x ^= x << 5;
- *state = x;
- return x;
- }
- // encode our permutation into a reusable id
- static void perm_printid(
- const struct test_suite *suite,
- const struct test_case *case_,
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count) {
- (void)suite;
- // case[:permutation[:powercycles]]
- printf("%s:", case_->name);
- for (size_t d = 0;
- d < lfs_max(
- suite->define_count,
- TEST_IMPLICIT_DEFINE_COUNT);
- d++) {
- if (test_define_ispermutation(d)) {
- leb16_print(d);
- leb16_print(TEST_DEFINE(d));
- }
- }
- // only print power-cycles if any occured
- if (cycles) {
- printf(":");
- for (size_t i = 0; i < cycle_count; i++) {
- leb16_print(cycles[i]);
- }
- }
- }
- // a quick trie for keeping track of permutations we've seen
- typedef struct test_seen {
- struct test_seen_branch *branches;
- size_t branch_count;
- size_t branch_capacity;
- } test_seen_t;
- struct test_seen_branch {
- intmax_t define;
- struct test_seen branch;
- };
- bool test_seen_insert(
- test_seen_t *seen,
- const struct test_suite *suite,
- const struct test_case *case_) {
- (void)case_;
- bool was_seen = true;
- // use the currently set defines
- for (size_t d = 0;
- d < lfs_max(
- suite->define_count,
- TEST_IMPLICIT_DEFINE_COUNT);
- d++) {
- // treat unpermuted defines the same as 0
- intmax_t define = test_define_ispermutation(d) ? TEST_DEFINE(d) : 0;
- // already seen?
- struct test_seen_branch *branch = NULL;
- for (size_t i = 0; i < seen->branch_count; i++) {
- if (seen->branches[i].define == define) {
- branch = &seen->branches[i];
- break;
- }
- }
- // need to create a new node
- if (!branch) {
- was_seen = false;
- branch = mappend(
- (void**)&seen->branches,
- sizeof(struct test_seen_branch),
- &seen->branch_count,
- &seen->branch_capacity);
- branch->define = define;
- branch->branch = (test_seen_t){NULL, 0, 0};
- }
- seen = &branch->branch;
- }
- return was_seen;
- }
- void test_seen_cleanup(test_seen_t *seen) {
- for (size_t i = 0; i < seen->branch_count; i++) {
- test_seen_cleanup(&seen->branches[i].branch);
- }
- free(seen->branches);
- }
- static void run_powerloss_none(
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count,
- const struct test_suite *suite,
- const struct test_case *case_);
- static void run_powerloss_cycles(
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count,
- const struct test_suite *suite,
- const struct test_case *case_);
- // iterate through permutations in a test case
- static void case_forperm(
- const struct test_suite *suite,
- const struct test_case *case_,
- const test_define_t *defines,
- size_t define_count,
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count,
- void (*cb)(
- void *data,
- const struct test_suite *suite,
- const struct test_case *case_,
- const test_powerloss_t *powerloss),
- void *data) {
- // explicit permutation?
- if (defines) {
- test_define_explicit(defines, define_count);
- for (size_t v = 0; v < test_override_define_permutations; v++) {
- // define override permutation
- test_define_override(v);
- test_define_flush();
- // explicit powerloss cycles?
- if (cycles) {
- cb(data, suite, case_, &(test_powerloss_t){
- .run=run_powerloss_cycles,
- .cycles=cycles,
- .cycle_count=cycle_count});
- } else {
- for (size_t p = 0; p < test_powerloss_count; p++) {
- // skip non-reentrant tests when powerloss testing
- if (test_powerlosses[p].run != run_powerloss_none
- && !(case_->flags & TEST_REENTRANT)) {
- continue;
- }
- cb(data, suite, case_, &test_powerlosses[p]);
- }
- }
- }
- return;
- }
- test_seen_t seen = {NULL, 0, 0};
- for (size_t k = 0; k < case_->permutations; k++) {
- // define permutation
- test_define_perm(suite, case_, k);
- for (size_t v = 0; v < test_override_define_permutations; v++) {
- // define override permutation
- test_define_override(v);
- for (size_t g = 0; g < test_geometry_count; g++) {
- // define geometry
- test_define_geometry(&test_geometries[g]);
- test_define_flush();
- // have we seen this permutation before?
- bool was_seen = test_seen_insert(&seen, suite, case_);
- if (!(k == 0 && v == 0 && g == 0) && was_seen) {
- continue;
- }
- if (cycles) {
- cb(data, suite, case_, &(test_powerloss_t){
- .run=run_powerloss_cycles,
- .cycles=cycles,
- .cycle_count=cycle_count});
- } else {
- for (size_t p = 0; p < test_powerloss_count; p++) {
- // skip non-reentrant tests when powerloss testing
- if (test_powerlosses[p].run != run_powerloss_none
- && !(case_->flags & TEST_REENTRANT)) {
- continue;
- }
- cb(data, suite, case_, &test_powerlosses[p]);
- }
- }
- }
- }
- }
- test_seen_cleanup(&seen);
- }
- // how many permutations are there actually in a test case
- struct perm_count_state {
- size_t total;
- size_t filtered;
- };
- void perm_count(
- void *data,
- const struct test_suite *suite,
- const struct test_case *case_,
- const test_powerloss_t *powerloss) {
- struct perm_count_state *state = data;
- (void)suite;
- (void)case_;
- (void)powerloss;
- state->total += 1;
- if (case_->filter && !case_->filter()) {
- return;
- }
- state->filtered += 1;
- }
- // operations we can do
- static void summary(void) {
- printf("%-23s %7s %7s %7s %11s\n",
- "", "flags", "suites", "cases", "perms");
- size_t suites = 0;
- size_t cases = 0;
- test_flags_t flags = 0;
- struct perm_count_state perms = {0, 0};
- for (size_t t = 0; t < test_id_count; t++) {
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- test_define_suite(&test_suites[i]);
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- // does neither suite nor case name match?
- if (test_ids[t].name && !(
- strcmp(test_ids[t].name,
- test_suites[i].name) == 0
- || strcmp(test_ids[t].name,
- test_suites[i].cases[j].name) == 0)) {
- continue;
- }
- cases += 1;
- case_forperm(
- &test_suites[i],
- &test_suites[i].cases[j],
- test_ids[t].defines,
- test_ids[t].define_count,
- test_ids[t].cycles,
- test_ids[t].cycle_count,
- perm_count,
- &perms);
- }
- suites += 1;
- flags |= test_suites[i].flags;
- }
- }
- char perm_buf[64];
- sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
- char flag_buf[64];
- sprintf(flag_buf, "%s%s",
- (flags & TEST_REENTRANT) ? "r" : "",
- (!flags) ? "-" : "");
- printf("%-23s %7s %7zu %7zu %11s\n",
- "TOTAL",
- flag_buf,
- suites,
- cases,
- perm_buf);
- }
- static void list_suites(void) {
- // at least size so that names fit
- unsigned name_width = 23;
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- size_t len = strlen(test_suites[i].name);
- if (len > name_width) {
- name_width = len;
- }
- }
- name_width = 4*((name_width+1+4-1)/4)-1;
- printf("%-*s %7s %7s %11s\n",
- name_width, "suite", "flags", "cases", "perms");
- for (size_t t = 0; t < test_id_count; t++) {
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- test_define_suite(&test_suites[i]);
- size_t cases = 0;
- struct perm_count_state perms = {0, 0};
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- // does neither suite nor case name match?
- if (test_ids[t].name && !(
- strcmp(test_ids[t].name,
- test_suites[i].name) == 0
- || strcmp(test_ids[t].name,
- test_suites[i].cases[j].name) == 0)) {
- continue;
- }
- cases += 1;
- case_forperm(
- &test_suites[i],
- &test_suites[i].cases[j],
- test_ids[t].defines,
- test_ids[t].define_count,
- test_ids[t].cycles,
- test_ids[t].cycle_count,
- perm_count,
- &perms);
- }
- // no tests found?
- if (!cases) {
- continue;
- }
- char perm_buf[64];
- sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
- char flag_buf[64];
- sprintf(flag_buf, "%s%s",
- (test_suites[i].flags & TEST_REENTRANT) ? "r" : "",
- (!test_suites[i].flags) ? "-" : "");
- printf("%-*s %7s %7zu %11s\n",
- name_width,
- test_suites[i].name,
- flag_buf,
- cases,
- perm_buf);
- }
- }
- }
- static void list_cases(void) {
- // at least size so that names fit
- unsigned name_width = 23;
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- size_t len = strlen(test_suites[i].cases[j].name);
- if (len > name_width) {
- name_width = len;
- }
- }
- }
- name_width = 4*((name_width+1+4-1)/4)-1;
- printf("%-*s %7s %11s\n", name_width, "case", "flags", "perms");
- for (size_t t = 0; t < test_id_count; t++) {
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- test_define_suite(&test_suites[i]);
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- // does neither suite nor case name match?
- if (test_ids[t].name && !(
- strcmp(test_ids[t].name,
- test_suites[i].name) == 0
- || strcmp(test_ids[t].name,
- test_suites[i].cases[j].name) == 0)) {
- continue;
- }
- struct perm_count_state perms = {0, 0};
- case_forperm(
- &test_suites[i],
- &test_suites[i].cases[j],
- test_ids[t].defines,
- test_ids[t].define_count,
- test_ids[t].cycles,
- test_ids[t].cycle_count,
- perm_count,
- &perms);
- char perm_buf[64];
- sprintf(perm_buf, "%zu/%zu", perms.filtered, perms.total);
- char flag_buf[64];
- sprintf(flag_buf, "%s%s",
- (test_suites[i].cases[j].flags & TEST_REENTRANT)
- ? "r" : "",
- (!test_suites[i].cases[j].flags)
- ? "-" : "");
- printf("%-*s %7s %11s\n",
- name_width,
- test_suites[i].cases[j].name,
- flag_buf,
- perm_buf);
- }
- }
- }
- }
- static void list_suite_paths(void) {
- // at least size so that names fit
- unsigned name_width = 23;
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- size_t len = strlen(test_suites[i].name);
- if (len > name_width) {
- name_width = len;
- }
- }
- name_width = 4*((name_width+1+4-1)/4)-1;
- printf("%-*s %s\n", name_width, "suite", "path");
- for (size_t t = 0; t < test_id_count; t++) {
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- size_t cases = 0;
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- // does neither suite nor case name match?
- if (test_ids[t].name && !(
- strcmp(test_ids[t].name,
- test_suites[i].name) == 0
- || strcmp(test_ids[t].name,
- test_suites[i].cases[j].name) == 0)) {
- continue;
- }
- cases += 1;
- }
- // no tests found?
- if (!cases) {
- continue;
- }
- printf("%-*s %s\n",
- name_width,
- test_suites[i].name,
- test_suites[i].path);
- }
- }
- }
- static void list_case_paths(void) {
- // at least size so that names fit
- unsigned name_width = 23;
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- size_t len = strlen(test_suites[i].cases[j].name);
- if (len > name_width) {
- name_width = len;
- }
- }
- }
- name_width = 4*((name_width+1+4-1)/4)-1;
- printf("%-*s %s\n", name_width, "case", "path");
- for (size_t t = 0; t < test_id_count; t++) {
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- // does neither suite nor case name match?
- if (test_ids[t].name && !(
- strcmp(test_ids[t].name,
- test_suites[i].name) == 0
- || strcmp(test_ids[t].name,
- test_suites[i].cases[j].name) == 0)) {
- continue;
- }
- printf("%-*s %s\n",
- name_width,
- test_suites[i].cases[j].name,
- test_suites[i].cases[j].path);
- }
- }
- }
- }
- struct list_defines_define {
- const char *name;
- intmax_t *values;
- size_t value_count;
- size_t value_capacity;
- };
- struct list_defines_defines {
- struct list_defines_define *defines;
- size_t define_count;
- size_t define_capacity;
- };
- static void list_defines_add(
- struct list_defines_defines *defines,
- size_t d) {
- const char *name = test_define_name(d);
- intmax_t value = TEST_DEFINE(d);
- // define already in defines?
- for (size_t i = 0; i < defines->define_count; i++) {
- if (strcmp(defines->defines[i].name, name) == 0) {
- // value already in values?
- for (size_t j = 0; j < defines->defines[i].value_count; j++) {
- if (defines->defines[i].values[j] == value) {
- return;
- }
- }
- *(intmax_t*)mappend(
- (void**)&defines->defines[i].values,
- sizeof(intmax_t),
- &defines->defines[i].value_count,
- &defines->defines[i].value_capacity) = value;
- return;
- }
- }
- // new define?
- struct list_defines_define *define = mappend(
- (void**)&defines->defines,
- sizeof(struct list_defines_define),
- &defines->define_count,
- &defines->define_capacity);
- define->name = name;
- define->values = malloc(sizeof(intmax_t));
- define->values[0] = value;
- define->value_count = 1;
- define->value_capacity = 1;
- }
- void perm_list_defines(
- void *data,
- const struct test_suite *suite,
- const struct test_case *case_,
- const test_powerloss_t *powerloss) {
- struct list_defines_defines *defines = data;
- (void)suite;
- (void)case_;
- (void)powerloss;
- // collect defines
- for (size_t d = 0;
- d < lfs_max(suite->define_count,
- TEST_IMPLICIT_DEFINE_COUNT);
- d++) {
- if (d < TEST_IMPLICIT_DEFINE_COUNT
- || test_define_ispermutation(d)) {
- list_defines_add(defines, d);
- }
- }
- }
- void perm_list_permutation_defines(
- void *data,
- const struct test_suite *suite,
- const struct test_case *case_,
- const test_powerloss_t *powerloss) {
- struct list_defines_defines *defines = data;
- (void)suite;
- (void)case_;
- (void)powerloss;
- // collect permutation_defines
- for (size_t d = 0;
- d < lfs_max(suite->define_count,
- TEST_IMPLICIT_DEFINE_COUNT);
- d++) {
- if (test_define_ispermutation(d)) {
- list_defines_add(defines, d);
- }
- }
- }
- extern const test_geometry_t builtin_geometries[];
- static void list_defines(void) {
- struct list_defines_defines defines = {NULL, 0, 0};
- // add defines
- for (size_t t = 0; t < test_id_count; t++) {
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- test_define_suite(&test_suites[i]);
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- // does neither suite nor case name match?
- if (test_ids[t].name && !(
- strcmp(test_ids[t].name,
- test_suites[i].name) == 0
- || strcmp(test_ids[t].name,
- test_suites[i].cases[j].name) == 0)) {
- continue;
- }
- case_forperm(
- &test_suites[i],
- &test_suites[i].cases[j],
- test_ids[t].defines,
- test_ids[t].define_count,
- test_ids[t].cycles,
- test_ids[t].cycle_count,
- perm_list_defines,
- &defines);
- }
- }
- }
- for (size_t i = 0; i < defines.define_count; i++) {
- printf("%s=", defines.defines[i].name);
- for (size_t j = 0; j < defines.defines[i].value_count; j++) {
- printf("%jd", defines.defines[i].values[j]);
- if (j != defines.defines[i].value_count-1) {
- printf(",");
- }
- }
- printf("\n");
- }
- for (size_t i = 0; i < defines.define_count; i++) {
- free(defines.defines[i].values);
- }
- free(defines.defines);
- }
- static void list_permutation_defines(void) {
- struct list_defines_defines defines = {NULL, 0, 0};
- // add permutation defines
- for (size_t t = 0; t < test_id_count; t++) {
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- test_define_suite(&test_suites[i]);
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- // does neither suite nor case name match?
- if (test_ids[t].name && !(
- strcmp(test_ids[t].name,
- test_suites[i].name) == 0
- || strcmp(test_ids[t].name,
- test_suites[i].cases[j].name) == 0)) {
- continue;
- }
- case_forperm(
- &test_suites[i],
- &test_suites[i].cases[j],
- test_ids[t].defines,
- test_ids[t].define_count,
- test_ids[t].cycles,
- test_ids[t].cycle_count,
- perm_list_permutation_defines,
- &defines);
- }
- }
- }
- for (size_t i = 0; i < defines.define_count; i++) {
- printf("%s=", defines.defines[i].name);
- for (size_t j = 0; j < defines.defines[i].value_count; j++) {
- printf("%jd", defines.defines[i].values[j]);
- if (j != defines.defines[i].value_count-1) {
- printf(",");
- }
- }
- printf("\n");
- }
- for (size_t i = 0; i < defines.define_count; i++) {
- free(defines.defines[i].values);
- }
- free(defines.defines);
- }
- static void list_implicit_defines(void) {
- struct list_defines_defines defines = {NULL, 0, 0};
- // yes we do need to define a suite, this does a bit of bookeeping
- // such as setting up the define cache
- test_define_suite(&(const struct test_suite){0});
- // make sure to include builtin geometries here
- extern const test_geometry_t builtin_geometries[];
- for (size_t g = 0; builtin_geometries[g].name; g++) {
- test_define_geometry(&builtin_geometries[g]);
- test_define_flush();
- // add implicit defines
- for (size_t d = 0; d < TEST_IMPLICIT_DEFINE_COUNT; d++) {
- list_defines_add(&defines, d);
- }
- }
- for (size_t i = 0; i < defines.define_count; i++) {
- printf("%s=", defines.defines[i].name);
- for (size_t j = 0; j < defines.defines[i].value_count; j++) {
- printf("%jd", defines.defines[i].values[j]);
- if (j != defines.defines[i].value_count-1) {
- printf(",");
- }
- }
- printf("\n");
- }
- for (size_t i = 0; i < defines.define_count; i++) {
- free(defines.defines[i].values);
- }
- free(defines.defines);
- }
- // geometries to test
- const test_geometry_t builtin_geometries[] = {
- {"default", {{0}, TEST_CONST(16), TEST_CONST(512), {0}}},
- {"eeprom", {{0}, TEST_CONST(1), TEST_CONST(512), {0}}},
- {"emmc", {{0}, {0}, TEST_CONST(512), {0}}},
- {"nor", {{0}, TEST_CONST(1), TEST_CONST(4096), {0}}},
- {"nand", {{0}, TEST_CONST(4096), TEST_CONST(32768), {0}}},
- {NULL, {{0}, {0}, {0}, {0}}},
- };
- const test_geometry_t *test_geometries = builtin_geometries;
- size_t test_geometry_count = 5;
- static void list_geometries(void) {
- // at least size so that names fit
- unsigned name_width = 23;
- for (size_t g = 0; builtin_geometries[g].name; g++) {
- size_t len = strlen(builtin_geometries[g].name);
- if (len > name_width) {
- name_width = len;
- }
- }
- name_width = 4*((name_width+1+4-1)/4)-1;
- // yes we do need to define a suite, this does a bit of bookeeping
- // such as setting up the define cache
- test_define_suite(&(const struct test_suite){0});
- printf("%-*s %7s %7s %7s %7s %11s\n",
- name_width, "geometry", "read", "prog", "erase", "count", "size");
- for (size_t g = 0; builtin_geometries[g].name; g++) {
- test_define_geometry(&builtin_geometries[g]);
- test_define_flush();
- printf("%-*s %7ju %7ju %7ju %7ju %11ju\n",
- name_width,
- builtin_geometries[g].name,
- READ_SIZE,
- PROG_SIZE,
- ERASE_SIZE,
- ERASE_COUNT,
- ERASE_SIZE*ERASE_COUNT);
- }
- }
- // scenarios to run tests under power-loss
- static void run_powerloss_none(
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count,
- const struct test_suite *suite,
- const struct test_case *case_) {
- (void)cycles;
- (void)cycle_count;
- (void)suite;
- // create block device and configuration
- lfs_emubd_t bd;
- struct lfs_config cfg = {
- .context = &bd,
- .read = lfs_emubd_read,
- .prog = lfs_emubd_prog,
- .erase = lfs_emubd_erase,
- .sync = lfs_emubd_sync,
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .block_size = BLOCK_SIZE,
- .block_count = BLOCK_COUNT,
- .block_cycles = BLOCK_CYCLES,
- .cache_size = CACHE_SIZE,
- .lookahead_size = LOOKAHEAD_SIZE,
- .compact_thresh = COMPACT_THRESH,
- .metadata_max = METADATA_MAX,
- .inline_max = INLINE_MAX,
- #ifdef LFS_MULTIVERSION
- .disk_version = DISK_VERSION,
- #endif
- };
- struct lfs_emubd_config bdcfg = {
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .erase_size = ERASE_SIZE,
- .erase_count = ERASE_COUNT,
- .erase_value = ERASE_VALUE,
- .erase_cycles = ERASE_CYCLES,
- .badblock_behavior = BADBLOCK_BEHAVIOR,
- .disk_path = test_disk_path,
- .read_sleep = test_read_sleep,
- .prog_sleep = test_prog_sleep,
- .erase_sleep = test_erase_sleep,
- };
- int err = lfs_emubd_create(&cfg, &bdcfg);
- if (err) {
- fprintf(stderr, "error: could not create block device: %d\n", err);
- exit(-1);
- }
- // run the test
- printf("running ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- case_->run(&cfg);
- printf("finished ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- // cleanup
- err = lfs_emubd_destroy(&cfg);
- if (err) {
- fprintf(stderr, "error: could not destroy block device: %d\n", err);
- exit(-1);
- }
- }
- static void powerloss_longjmp(void *c) {
- jmp_buf *powerloss_jmp = c;
- longjmp(*powerloss_jmp, 1);
- }
- static void run_powerloss_linear(
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count,
- const struct test_suite *suite,
- const struct test_case *case_) {
- (void)cycles;
- (void)cycle_count;
- (void)suite;
- // create block device and configuration
- lfs_emubd_t bd;
- jmp_buf powerloss_jmp;
- volatile lfs_emubd_powercycles_t i = 1;
- struct lfs_config cfg = {
- .context = &bd,
- .read = lfs_emubd_read,
- .prog = lfs_emubd_prog,
- .erase = lfs_emubd_erase,
- .sync = lfs_emubd_sync,
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .block_size = BLOCK_SIZE,
- .block_count = BLOCK_COUNT,
- .block_cycles = BLOCK_CYCLES,
- .cache_size = CACHE_SIZE,
- .lookahead_size = LOOKAHEAD_SIZE,
- .compact_thresh = COMPACT_THRESH,
- .metadata_max = METADATA_MAX,
- .inline_max = INLINE_MAX,
- #ifdef LFS_MULTIVERSION
- .disk_version = DISK_VERSION,
- #endif
- };
- struct lfs_emubd_config bdcfg = {
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .erase_size = ERASE_SIZE,
- .erase_count = ERASE_COUNT,
- .erase_value = ERASE_VALUE,
- .erase_cycles = ERASE_CYCLES,
- .badblock_behavior = BADBLOCK_BEHAVIOR,
- .disk_path = test_disk_path,
- .read_sleep = test_read_sleep,
- .prog_sleep = test_prog_sleep,
- .erase_sleep = test_erase_sleep,
- .power_cycles = i,
- .powerloss_behavior = POWERLOSS_BEHAVIOR,
- .powerloss_cb = powerloss_longjmp,
- .powerloss_data = &powerloss_jmp,
- };
- int err = lfs_emubd_create(&cfg, &bdcfg);
- if (err) {
- fprintf(stderr, "error: could not create block device: %d\n", err);
- exit(-1);
- }
- // run the test, increasing power-cycles as power-loss events occur
- printf("running ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- while (true) {
- if (!setjmp(powerloss_jmp)) {
- // run the test
- case_->run(&cfg);
- break;
- }
- // power-loss!
- printf("powerloss ");
- perm_printid(suite, case_, NULL, 0);
- printf(":");
- for (lfs_emubd_powercycles_t j = 1; j <= i; j++) {
- leb16_print(j);
- }
- printf("\n");
- i += 1;
- lfs_emubd_setpowercycles(&cfg, i);
- }
- printf("finished ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- // cleanup
- err = lfs_emubd_destroy(&cfg);
- if (err) {
- fprintf(stderr, "error: could not destroy block device: %d\n", err);
- exit(-1);
- }
- }
- static void run_powerloss_log(
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count,
- const struct test_suite *suite,
- const struct test_case *case_) {
- (void)cycles;
- (void)cycle_count;
- (void)suite;
- // create block device and configuration
- lfs_emubd_t bd;
- jmp_buf powerloss_jmp;
- volatile lfs_emubd_powercycles_t i = 1;
- struct lfs_config cfg = {
- .context = &bd,
- .read = lfs_emubd_read,
- .prog = lfs_emubd_prog,
- .erase = lfs_emubd_erase,
- .sync = lfs_emubd_sync,
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .block_size = BLOCK_SIZE,
- .block_count = BLOCK_COUNT,
- .block_cycles = BLOCK_CYCLES,
- .cache_size = CACHE_SIZE,
- .lookahead_size = LOOKAHEAD_SIZE,
- .compact_thresh = COMPACT_THRESH,
- .metadata_max = METADATA_MAX,
- .inline_max = INLINE_MAX,
- #ifdef LFS_MULTIVERSION
- .disk_version = DISK_VERSION,
- #endif
- };
- struct lfs_emubd_config bdcfg = {
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .erase_size = ERASE_SIZE,
- .erase_count = ERASE_COUNT,
- .erase_value = ERASE_VALUE,
- .erase_cycles = ERASE_CYCLES,
- .badblock_behavior = BADBLOCK_BEHAVIOR,
- .disk_path = test_disk_path,
- .read_sleep = test_read_sleep,
- .prog_sleep = test_prog_sleep,
- .erase_sleep = test_erase_sleep,
- .power_cycles = i,
- .powerloss_behavior = POWERLOSS_BEHAVIOR,
- .powerloss_cb = powerloss_longjmp,
- .powerloss_data = &powerloss_jmp,
- };
- int err = lfs_emubd_create(&cfg, &bdcfg);
- if (err) {
- fprintf(stderr, "error: could not create block device: %d\n", err);
- exit(-1);
- }
- // run the test, increasing power-cycles as power-loss events occur
- printf("running ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- while (true) {
- if (!setjmp(powerloss_jmp)) {
- // run the test
- case_->run(&cfg);
- break;
- }
- // power-loss!
- printf("powerloss ");
- perm_printid(suite, case_, NULL, 0);
- printf(":");
- for (lfs_emubd_powercycles_t j = 1; j <= i; j *= 2) {
- leb16_print(j);
- }
- printf("\n");
- i *= 2;
- lfs_emubd_setpowercycles(&cfg, i);
- }
- printf("finished ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- // cleanup
- err = lfs_emubd_destroy(&cfg);
- if (err) {
- fprintf(stderr, "error: could not destroy block device: %d\n", err);
- exit(-1);
- }
- }
- static void run_powerloss_cycles(
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count,
- const struct test_suite *suite,
- const struct test_case *case_) {
- (void)suite;
- // create block device and configuration
- lfs_emubd_t bd;
- jmp_buf powerloss_jmp;
- volatile size_t i = 0;
- struct lfs_config cfg = {
- .context = &bd,
- .read = lfs_emubd_read,
- .prog = lfs_emubd_prog,
- .erase = lfs_emubd_erase,
- .sync = lfs_emubd_sync,
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .block_size = BLOCK_SIZE,
- .block_count = BLOCK_COUNT,
- .block_cycles = BLOCK_CYCLES,
- .cache_size = CACHE_SIZE,
- .lookahead_size = LOOKAHEAD_SIZE,
- .compact_thresh = COMPACT_THRESH,
- .metadata_max = METADATA_MAX,
- .inline_max = INLINE_MAX,
- #ifdef LFS_MULTIVERSION
- .disk_version = DISK_VERSION,
- #endif
- };
- struct lfs_emubd_config bdcfg = {
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .erase_size = ERASE_SIZE,
- .erase_count = ERASE_COUNT,
- .erase_value = ERASE_VALUE,
- .erase_cycles = ERASE_CYCLES,
- .badblock_behavior = BADBLOCK_BEHAVIOR,
- .disk_path = test_disk_path,
- .read_sleep = test_read_sleep,
- .prog_sleep = test_prog_sleep,
- .erase_sleep = test_erase_sleep,
- .power_cycles = (i < cycle_count) ? cycles[i] : 0,
- .powerloss_behavior = POWERLOSS_BEHAVIOR,
- .powerloss_cb = powerloss_longjmp,
- .powerloss_data = &powerloss_jmp,
- };
- int err = lfs_emubd_create(&cfg, &bdcfg);
- if (err) {
- fprintf(stderr, "error: could not create block device: %d\n", err);
- exit(-1);
- }
- // run the test, increasing power-cycles as power-loss events occur
- printf("running ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- while (true) {
- if (!setjmp(powerloss_jmp)) {
- // run the test
- case_->run(&cfg);
- break;
- }
- // power-loss!
- assert(i <= cycle_count);
- printf("powerloss ");
- perm_printid(suite, case_, cycles, i+1);
- printf("\n");
- i += 1;
- lfs_emubd_setpowercycles(&cfg,
- (i < cycle_count) ? cycles[i] : 0);
- }
- printf("finished ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- // cleanup
- err = lfs_emubd_destroy(&cfg);
- if (err) {
- fprintf(stderr, "error: could not destroy block device: %d\n", err);
- exit(-1);
- }
- }
- struct powerloss_exhaustive_state {
- struct lfs_config *cfg;
- lfs_emubd_t *branches;
- size_t branch_count;
- size_t branch_capacity;
- };
- struct powerloss_exhaustive_cycles {
- lfs_emubd_powercycles_t *cycles;
- size_t cycle_count;
- size_t cycle_capacity;
- };
- static void powerloss_exhaustive_branch(void *c) {
- struct powerloss_exhaustive_state *state = c;
- // append to branches
- lfs_emubd_t *branch = mappend(
- (void**)&state->branches,
- sizeof(lfs_emubd_t),
- &state->branch_count,
- &state->branch_capacity);
- if (!branch) {
- fprintf(stderr, "error: exhaustive: out of memory\n");
- exit(-1);
- }
- // create copy-on-write copy
- int err = lfs_emubd_copy(state->cfg, branch);
- if (err) {
- fprintf(stderr, "error: exhaustive: could not create bd copy\n");
- exit(-1);
- }
- // also trigger on next power cycle
- lfs_emubd_setpowercycles(state->cfg, 1);
- }
- static void run_powerloss_exhaustive_layer(
- struct powerloss_exhaustive_cycles *cycles,
- const struct test_suite *suite,
- const struct test_case *case_,
- struct lfs_config *cfg,
- struct lfs_emubd_config *bdcfg,
- size_t depth) {
- (void)suite;
- struct powerloss_exhaustive_state state = {
- .cfg = cfg,
- .branches = NULL,
- .branch_count = 0,
- .branch_capacity = 0,
- };
- // run through the test without additional powerlosses, collecting possible
- // branches as we do so
- lfs_emubd_setpowercycles(state.cfg, depth > 0 ? 1 : 0);
- bdcfg->powerloss_data = &state;
- // run the tests
- case_->run(cfg);
- // aggressively clean up memory here to try to keep our memory usage low
- int err = lfs_emubd_destroy(cfg);
- if (err) {
- fprintf(stderr, "error: could not destroy block device: %d\n", err);
- exit(-1);
- }
- // recurse into each branch
- for (size_t i = 0; i < state.branch_count; i++) {
- // first push and print the branch
- lfs_emubd_powercycles_t *cycle = mappend(
- (void**)&cycles->cycles,
- sizeof(lfs_emubd_powercycles_t),
- &cycles->cycle_count,
- &cycles->cycle_capacity);
- if (!cycle) {
- fprintf(stderr, "error: exhaustive: out of memory\n");
- exit(-1);
- }
- *cycle = i+1;
- printf("powerloss ");
- perm_printid(suite, case_, cycles->cycles, cycles->cycle_count);
- printf("\n");
- // now recurse
- cfg->context = &state.branches[i];
- run_powerloss_exhaustive_layer(cycles,
- suite, case_,
- cfg, bdcfg, depth-1);
- // pop the cycle
- cycles->cycle_count -= 1;
- }
- // clean up memory
- free(state.branches);
- }
- static void run_powerloss_exhaustive(
- const lfs_emubd_powercycles_t *cycles,
- size_t cycle_count,
- const struct test_suite *suite,
- const struct test_case *case_) {
- (void)cycles;
- (void)suite;
- // create block device and configuration
- lfs_emubd_t bd;
- struct lfs_config cfg = {
- .context = &bd,
- .read = lfs_emubd_read,
- .prog = lfs_emubd_prog,
- .erase = lfs_emubd_erase,
- .sync = lfs_emubd_sync,
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .block_size = BLOCK_SIZE,
- .block_count = BLOCK_COUNT,
- .block_cycles = BLOCK_CYCLES,
- .cache_size = CACHE_SIZE,
- .lookahead_size = LOOKAHEAD_SIZE,
- .compact_thresh = COMPACT_THRESH,
- .metadata_max = METADATA_MAX,
- .inline_max = INLINE_MAX,
- #ifdef LFS_MULTIVERSION
- .disk_version = DISK_VERSION,
- #endif
- };
- struct lfs_emubd_config bdcfg = {
- .read_size = READ_SIZE,
- .prog_size = PROG_SIZE,
- .erase_size = ERASE_SIZE,
- .erase_count = ERASE_COUNT,
- .erase_value = ERASE_VALUE,
- .erase_cycles = ERASE_CYCLES,
- .badblock_behavior = BADBLOCK_BEHAVIOR,
- .disk_path = test_disk_path,
- .read_sleep = test_read_sleep,
- .prog_sleep = test_prog_sleep,
- .erase_sleep = test_erase_sleep,
- .powerloss_behavior = POWERLOSS_BEHAVIOR,
- .powerloss_cb = powerloss_exhaustive_branch,
- .powerloss_data = NULL,
- };
- int err = lfs_emubd_create(&cfg, &bdcfg);
- if (err) {
- fprintf(stderr, "error: could not create block device: %d\n", err);
- exit(-1);
- }
- // run the test, increasing power-cycles as power-loss events occur
- printf("running ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- // recursively exhaust each layer of powerlosses
- run_powerloss_exhaustive_layer(
- &(struct powerloss_exhaustive_cycles){NULL, 0, 0},
- suite, case_,
- &cfg, &bdcfg, cycle_count);
- printf("finished ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- }
- const test_powerloss_t builtin_powerlosses[] = {
- {"none", run_powerloss_none, NULL, 0},
- {"log", run_powerloss_log, NULL, 0},
- {"linear", run_powerloss_linear, NULL, 0},
- {"exhaustive", run_powerloss_exhaustive, NULL, SIZE_MAX},
- {NULL, NULL, NULL, 0},
- };
- const char *const builtin_powerlosses_help[] = {
- "Run with no power-losses.",
- "Run with exponentially-decreasing power-losses.",
- "Run with linearly-decreasing power-losses.",
- "Run a all permutations of power-losses, this may take a while.",
- "Run a all permutations of n power-losses.",
- "Run a custom comma-separated set of power-losses.",
- "Run a custom leb16-encoded set of power-losses.",
- };
- // default to -Pnone,linear, which provides a good heuristic while still
- // running quickly
- const test_powerloss_t *test_powerlosses = (const test_powerloss_t[]){
- {"none", run_powerloss_none, NULL, 0},
- {"linear", run_powerloss_linear, NULL, 0},
- };
- size_t test_powerloss_count = 2;
- static void list_powerlosses(void) {
- // at least size so that names fit
- unsigned name_width = 23;
- for (size_t i = 0; builtin_powerlosses[i].name; i++) {
- size_t len = strlen(builtin_powerlosses[i].name);
- if (len > name_width) {
- name_width = len;
- }
- }
- name_width = 4*((name_width+1+4-1)/4)-1;
- printf("%-*s %s\n", name_width, "scenario", "description");
- size_t i = 0;
- for (; builtin_powerlosses[i].name; i++) {
- printf("%-*s %s\n",
- name_width,
- builtin_powerlosses[i].name,
- builtin_powerlosses_help[i]);
- }
- // a couple more options with special parsing
- printf("%-*s %s\n", name_width, "1,2,3", builtin_powerlosses_help[i+0]);
- printf("%-*s %s\n", name_width, "{1,2,3}", builtin_powerlosses_help[i+1]);
- printf("%-*s %s\n", name_width, ":1248g1", builtin_powerlosses_help[i+2]);
- }
- // global test step count
- size_t test_step = 0;
- void perm_run(
- void *data,
- const struct test_suite *suite,
- const struct test_case *case_,
- const test_powerloss_t *powerloss) {
- (void)data;
- // skip this step?
- if (!(test_step >= test_step_start
- && test_step < test_step_stop
- && (test_step-test_step_start) % test_step_step == 0)) {
- test_step += 1;
- return;
- }
- test_step += 1;
- // filter?
- if (case_->filter && !case_->filter()) {
- printf("skipped ");
- perm_printid(suite, case_, NULL, 0);
- printf("\n");
- return;
- }
- powerloss->run(
- powerloss->cycles, powerloss->cycle_count,
- suite, case_);
- }
- static void run(void) {
- // ignore disconnected pipes
- signal(SIGPIPE, SIG_IGN);
- for (size_t t = 0; t < test_id_count; t++) {
- for (size_t i = 0; i < TEST_SUITE_COUNT; i++) {
- test_define_suite(&test_suites[i]);
- for (size_t j = 0; j < test_suites[i].case_count; j++) {
- // does neither suite nor case name match?
- if (test_ids[t].name && !(
- strcmp(test_ids[t].name,
- test_suites[i].name) == 0
- || strcmp(test_ids[t].name,
- test_suites[i].cases[j].name) == 0)) {
- continue;
- }
- case_forperm(
- &test_suites[i],
- &test_suites[i].cases[j],
- test_ids[t].defines,
- test_ids[t].define_count,
- test_ids[t].cycles,
- test_ids[t].cycle_count,
- perm_run,
- NULL);
- }
- }
- }
- }
- // option handling
- enum opt_flags {
- OPT_HELP = 'h',
- OPT_SUMMARY = 'Y',
- OPT_LIST_SUITES = 'l',
- OPT_LIST_CASES = 'L',
- OPT_LIST_SUITE_PATHS = 1,
- OPT_LIST_CASE_PATHS = 2,
- OPT_LIST_DEFINES = 3,
- OPT_LIST_PERMUTATION_DEFINES = 4,
- OPT_LIST_IMPLICIT_DEFINES = 5,
- OPT_LIST_GEOMETRIES = 6,
- OPT_LIST_POWERLOSSES = 7,
- OPT_DEFINE = 'D',
- OPT_GEOMETRY = 'G',
- OPT_POWERLOSS = 'P',
- OPT_STEP = 's',
- OPT_DISK = 'd',
- OPT_TRACE = 't',
- OPT_TRACE_BACKTRACE = 8,
- OPT_TRACE_PERIOD = 9,
- OPT_TRACE_FREQ = 10,
- OPT_READ_SLEEP = 11,
- OPT_PROG_SLEEP = 12,
- OPT_ERASE_SLEEP = 13,
- };
- const char *short_opts = "hYlLD:G:P:s:d:t:";
- const struct option long_opts[] = {
- {"help", no_argument, NULL, OPT_HELP},
- {"summary", no_argument, NULL, OPT_SUMMARY},
- {"list-suites", no_argument, NULL, OPT_LIST_SUITES},
- {"list-cases", no_argument, NULL, OPT_LIST_CASES},
- {"list-suite-paths", no_argument, NULL, OPT_LIST_SUITE_PATHS},
- {"list-case-paths", no_argument, NULL, OPT_LIST_CASE_PATHS},
- {"list-defines", no_argument, NULL, OPT_LIST_DEFINES},
- {"list-permutation-defines",
- no_argument, NULL, OPT_LIST_PERMUTATION_DEFINES},
- {"list-implicit-defines",
- no_argument, NULL, OPT_LIST_IMPLICIT_DEFINES},
- {"list-geometries", no_argument, NULL, OPT_LIST_GEOMETRIES},
- {"list-powerlosses", no_argument, NULL, OPT_LIST_POWERLOSSES},
- {"define", required_argument, NULL, OPT_DEFINE},
- {"geometry", required_argument, NULL, OPT_GEOMETRY},
- {"powerloss", required_argument, NULL, OPT_POWERLOSS},
- {"step", required_argument, NULL, OPT_STEP},
- {"disk", required_argument, NULL, OPT_DISK},
- {"trace", required_argument, NULL, OPT_TRACE},
- {"trace-backtrace", no_argument, NULL, OPT_TRACE_BACKTRACE},
- {"trace-period", required_argument, NULL, OPT_TRACE_PERIOD},
- {"trace-freq", required_argument, NULL, OPT_TRACE_FREQ},
- {"read-sleep", required_argument, NULL, OPT_READ_SLEEP},
- {"prog-sleep", required_argument, NULL, OPT_PROG_SLEEP},
- {"erase-sleep", required_argument, NULL, OPT_ERASE_SLEEP},
- {NULL, 0, NULL, 0},
- };
- const char *const help_text[] = {
- "Show this help message.",
- "Show quick summary.",
- "List test suites.",
- "List test cases.",
- "List the path for each test suite.",
- "List the path and line number for each test case.",
- "List all defines in this test-runner.",
- "List explicit defines in this test-runner.",
- "List implicit defines in this test-runner.",
- "List the available disk geometries.",
- "List the available power-loss scenarios.",
- "Override a test define.",
- "Comma-separated list of disk geometries to test.",
- "Comma-separated list of power-loss scenarios to test.",
- "Comma-separated range of test permutations to run (start,stop,step).",
- "Direct block device operations to this file.",
- "Direct trace output to this file.",
- "Include a backtrace with every trace statement.",
- "Sample trace output at this period in cycles.",
- "Sample trace output at this frequency in hz.",
- "Artificial read delay in seconds.",
- "Artificial prog delay in seconds.",
- "Artificial erase delay in seconds.",
- };
- int main(int argc, char **argv) {
- void (*op)(void) = run;
- size_t test_override_capacity = 0;
- size_t test_geometry_capacity = 0;
- size_t test_powerloss_capacity = 0;
- size_t test_id_capacity = 0;
- // parse options
- while (true) {
- int c = getopt_long(argc, argv, short_opts, long_opts, NULL);
- switch (c) {
- // generate help message
- case OPT_HELP: {
- printf("usage: %s [options] [test_id]\n", argv[0]);
- printf("\n");
- printf("options:\n");
- size_t i = 0;
- while (long_opts[i].name) {
- size_t indent;
- if (long_opts[i].has_arg == no_argument) {
- if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
- indent = printf(" -%c, --%s ",
- long_opts[i].val,
- long_opts[i].name);
- } else {
- indent = printf(" --%s ",
- long_opts[i].name);
- }
- } else {
- if (long_opts[i].val >= '0' && long_opts[i].val < 'z') {
- indent = printf(" -%c %s, --%s %s ",
- long_opts[i].val,
- long_opts[i].name,
- long_opts[i].name,
- long_opts[i].name);
- } else {
- indent = printf(" --%s %s ",
- long_opts[i].name,
- long_opts[i].name);
- }
- }
- // a quick, hacky, byte-level method for text wrapping
- size_t len = strlen(help_text[i]);
- size_t j = 0;
- if (indent < 24) {
- printf("%*s %.80s\n",
- (int)(24-1-indent),
- "",
- &help_text[i][j]);
- j += 80;
- } else {
- printf("\n");
- }
- while (j < len) {
- printf("%24s%.80s\n", "", &help_text[i][j]);
- j += 80;
- }
- i += 1;
- }
- printf("\n");
- exit(0);
- }
- // summary/list flags
- case OPT_SUMMARY:
- op = summary;
- break;
- case OPT_LIST_SUITES:
- op = list_suites;
- break;
- case OPT_LIST_CASES:
- op = list_cases;
- break;
- case OPT_LIST_SUITE_PATHS:
- op = list_suite_paths;
- break;
- case OPT_LIST_CASE_PATHS:
- op = list_case_paths;
- break;
- case OPT_LIST_DEFINES:
- op = list_defines;
- break;
- case OPT_LIST_PERMUTATION_DEFINES:
- op = list_permutation_defines;
- break;
- case OPT_LIST_IMPLICIT_DEFINES:
- op = list_implicit_defines;
- break;
- case OPT_LIST_GEOMETRIES:
- op = list_geometries;
- break;
- case OPT_LIST_POWERLOSSES:
- op = list_powerlosses;
- break;
- // configuration
- case OPT_DEFINE: {
- // allocate space
- test_override_t *override = mappend(
- (void**)&test_overrides,
- sizeof(test_override_t),
- &test_override_count,
- &test_override_capacity);
- // parse into string key/intmax_t value, cannibalizing the
- // arg in the process
- char *sep = strchr(optarg, '=');
- char *parsed = NULL;
- if (!sep) {
- goto invalid_define;
- }
- *sep = '\0';
- override->name = optarg;
- optarg = sep+1;
- // parse comma-separated permutations
- {
- override->defines = NULL;
- override->permutations = 0;
- size_t override_capacity = 0;
- while (true) {
- optarg += strspn(optarg, " ");
- if (strncmp(optarg, "range", strlen("range")) == 0) {
- // range of values
- optarg += strlen("range");
- optarg += strspn(optarg, " ");
- if (*optarg != '(') {
- goto invalid_define;
- }
- optarg += 1;
- intmax_t start = strtoumax(optarg, &parsed, 0);
- intmax_t stop = -1;
- intmax_t step = 1;
- // allow empty string for start=0
- if (parsed == optarg) {
- start = 0;
- }
- optarg = parsed + strspn(parsed, " ");
- if (*optarg != ',' && *optarg != ')') {
- goto invalid_define;
- }
- if (*optarg == ',') {
- optarg += 1;
- stop = strtoumax(optarg, &parsed, 0);
- // allow empty string for stop=end
- if (parsed == optarg) {
- stop = -1;
- }
- optarg = parsed + strspn(parsed, " ");
- if (*optarg != ',' && *optarg != ')') {
- goto invalid_define;
- }
- if (*optarg == ',') {
- optarg += 1;
- step = strtoumax(optarg, &parsed, 0);
- // allow empty string for stop=1
- if (parsed == optarg) {
- step = 1;
- }
- optarg = parsed + strspn(parsed, " ");
- if (*optarg != ')') {
- goto invalid_define;
- }
- }
- } else {
- // single value = stop only
- stop = start;
- start = 0;
- }
-
- if (*optarg != ')') {
- goto invalid_define;
- }
- optarg += 1;
- // calculate the range of values
- assert(step != 0);
- for (intmax_t i = start;
- (step < 0)
- ? i > stop
- : (uintmax_t)i < (uintmax_t)stop;
- i += step) {
- *(intmax_t*)mappend(
- (void**)&override->defines,
- sizeof(intmax_t),
- &override->permutations,
- &override_capacity) = i;
- }
- } else if (*optarg != '\0') {
- // single value
- intmax_t define = strtoimax(optarg, &parsed, 0);
- if (parsed == optarg) {
- goto invalid_define;
- }
- optarg = parsed + strspn(parsed, " ");
- *(intmax_t*)mappend(
- (void**)&override->defines,
- sizeof(intmax_t),
- &override->permutations,
- &override_capacity) = define;
- } else {
- break;
- }
- if (*optarg == ',') {
- optarg += 1;
- }
- }
- }
- assert(override->permutations > 0);
- break;
- invalid_define:
- fprintf(stderr, "error: invalid define: %s\n", optarg);
- exit(-1);
- }
- case OPT_GEOMETRY: {
- // reset our geometry scenarios
- if (test_geometry_capacity > 0) {
- free((test_geometry_t*)test_geometries);
- }
- test_geometries = NULL;
- test_geometry_count = 0;
- test_geometry_capacity = 0;
- // parse the comma separated list of disk geometries
- while (*optarg) {
- // allocate space
- test_geometry_t *geometry = mappend(
- (void**)&test_geometries,
- sizeof(test_geometry_t),
- &test_geometry_count,
- &test_geometry_capacity);
- // parse the disk geometry
- optarg += strspn(optarg, " ");
- // named disk geometry
- size_t len = strcspn(optarg, " ,");
- for (size_t i = 0; builtin_geometries[i].name; i++) {
- if (len == strlen(builtin_geometries[i].name)
- && memcmp(optarg,
- builtin_geometries[i].name,
- len) == 0) {
- *geometry = builtin_geometries[i];
- optarg += len;
- goto geometry_next;
- }
- }
- // comma-separated read/prog/erase/count
- if (*optarg == '{') {
- lfs_size_t sizes[4];
- size_t count = 0;
- char *s = optarg + 1;
- while (count < 4) {
- char *parsed = NULL;
- sizes[count] = strtoumax(s, &parsed, 0);
- count += 1;
- s = parsed + strspn(parsed, " ");
- if (*s == ',') {
- s += 1;
- continue;
- } else if (*s == '}') {
- s += 1;
- break;
- } else {
- goto geometry_unknown;
- }
- }
- // allow implicit r=p and p=e for common geometries
- memset(geometry, 0, sizeof(test_geometry_t));
- if (count >= 3) {
- geometry->defines[READ_SIZE_i]
- = TEST_LIT(sizes[0]);
- geometry->defines[PROG_SIZE_i]
- = TEST_LIT(sizes[1]);
- geometry->defines[ERASE_SIZE_i]
- = TEST_LIT(sizes[2]);
- } else if (count >= 2) {
- geometry->defines[PROG_SIZE_i]
- = TEST_LIT(sizes[0]);
- geometry->defines[ERASE_SIZE_i]
- = TEST_LIT(sizes[1]);
- } else {
- geometry->defines[ERASE_SIZE_i]
- = TEST_LIT(sizes[0]);
- }
- if (count >= 4) {
- geometry->defines[ERASE_COUNT_i]
- = TEST_LIT(sizes[3]);
- }
- optarg = s;
- goto geometry_next;
- }
- // leb16-encoded read/prog/erase/count
- if (*optarg == ':') {
- lfs_size_t sizes[4];
- size_t count = 0;
- char *s = optarg + 1;
- while (true) {
- char *parsed = NULL;
- uintmax_t x = leb16_parse(s, &parsed);
- if (parsed == s || count >= 4) {
- break;
- }
- sizes[count] = x;
- count += 1;
- s = parsed;
- }
- // allow implicit r=p and p=e for common geometries
- memset(geometry, 0, sizeof(test_geometry_t));
- if (count >= 3) {
- geometry->defines[READ_SIZE_i]
- = TEST_LIT(sizes[0]);
- geometry->defines[PROG_SIZE_i]
- = TEST_LIT(sizes[1]);
- geometry->defines[ERASE_SIZE_i]
- = TEST_LIT(sizes[2]);
- } else if (count >= 2) {
- geometry->defines[PROG_SIZE_i]
- = TEST_LIT(sizes[0]);
- geometry->defines[ERASE_SIZE_i]
- = TEST_LIT(sizes[1]);
- } else {
- geometry->defines[ERASE_SIZE_i]
- = TEST_LIT(sizes[0]);
- }
- if (count >= 4) {
- geometry->defines[ERASE_COUNT_i]
- = TEST_LIT(sizes[3]);
- }
- optarg = s;
- goto geometry_next;
- }
- geometry_unknown:
- // unknown scenario?
- fprintf(stderr, "error: unknown disk geometry: %s\n",
- optarg);
- exit(-1);
- geometry_next:
- optarg += strspn(optarg, " ");
- if (*optarg == ',') {
- optarg += 1;
- } else if (*optarg == '\0') {
- break;
- } else {
- goto geometry_unknown;
- }
- }
- break;
- }
- case OPT_POWERLOSS: {
- // reset our powerloss scenarios
- if (test_powerloss_capacity > 0) {
- free((test_powerloss_t*)test_powerlosses);
- }
- test_powerlosses = NULL;
- test_powerloss_count = 0;
- test_powerloss_capacity = 0;
- // parse the comma separated list of power-loss scenarios
- while (*optarg) {
- // allocate space
- test_powerloss_t *powerloss = mappend(
- (void**)&test_powerlosses,
- sizeof(test_powerloss_t),
- &test_powerloss_count,
- &test_powerloss_capacity);
- // parse the power-loss scenario
- optarg += strspn(optarg, " ");
- // named power-loss scenario
- size_t len = strcspn(optarg, " ,");
- for (size_t i = 0; builtin_powerlosses[i].name; i++) {
- if (len == strlen(builtin_powerlosses[i].name)
- && memcmp(optarg,
- builtin_powerlosses[i].name,
- len) == 0) {
- *powerloss = builtin_powerlosses[i];
- optarg += len;
- goto powerloss_next;
- }
- }
- // comma-separated permutation
- if (*optarg == '{') {
- lfs_emubd_powercycles_t *cycles = NULL;
- size_t cycle_count = 0;
- size_t cycle_capacity = 0;
- char *s = optarg + 1;
- while (true) {
- char *parsed = NULL;
- *(lfs_emubd_powercycles_t*)mappend(
- (void**)&cycles,
- sizeof(lfs_emubd_powercycles_t),
- &cycle_count,
- &cycle_capacity)
- = strtoumax(s, &parsed, 0);
- s = parsed + strspn(parsed, " ");
- if (*s == ',') {
- s += 1;
- continue;
- } else if (*s == '}') {
- s += 1;
- break;
- } else {
- goto powerloss_unknown;
- }
- }
- *powerloss = (test_powerloss_t){
- .run = run_powerloss_cycles,
- .cycles = cycles,
- .cycle_count = cycle_count,
- };
- optarg = s;
- goto powerloss_next;
- }
- // leb16-encoded permutation
- if (*optarg == ':') {
- lfs_emubd_powercycles_t *cycles = NULL;
- size_t cycle_count = 0;
- size_t cycle_capacity = 0;
- char *s = optarg + 1;
- while (true) {
- char *parsed = NULL;
- uintmax_t x = leb16_parse(s, &parsed);
- if (parsed == s) {
- break;
- }
- *(lfs_emubd_powercycles_t*)mappend(
- (void**)&cycles,
- sizeof(lfs_emubd_powercycles_t),
- &cycle_count,
- &cycle_capacity) = x;
- s = parsed;
- }
- *powerloss = (test_powerloss_t){
- .run = run_powerloss_cycles,
- .cycles = cycles,
- .cycle_count = cycle_count,
- };
- optarg = s;
- goto powerloss_next;
- }
- // exhaustive permutations
- {
- char *parsed = NULL;
- size_t count = strtoumax(optarg, &parsed, 0);
- if (parsed == optarg) {
- goto powerloss_unknown;
- }
- *powerloss = (test_powerloss_t){
- .run = run_powerloss_exhaustive,
- .cycles = NULL,
- .cycle_count = count,
- };
- optarg = (char*)parsed;
- goto powerloss_next;
- }
- powerloss_unknown:
- // unknown scenario?
- fprintf(stderr, "error: unknown power-loss scenario: %s\n",
- optarg);
- exit(-1);
- powerloss_next:
- optarg += strspn(optarg, " ");
- if (*optarg == ',') {
- optarg += 1;
- } else if (*optarg == '\0') {
- break;
- } else {
- goto powerloss_unknown;
- }
- }
- break;
- }
- case OPT_STEP: {
- char *parsed = NULL;
- test_step_start = strtoumax(optarg, &parsed, 0);
- test_step_stop = -1;
- test_step_step = 1;
- // allow empty string for start=0
- if (parsed == optarg) {
- test_step_start = 0;
- }
- optarg = parsed + strspn(parsed, " ");
- if (*optarg != ',' && *optarg != '\0') {
- goto step_unknown;
- }
- if (*optarg == ',') {
- optarg += 1;
- test_step_stop = strtoumax(optarg, &parsed, 0);
- // allow empty string for stop=end
- if (parsed == optarg) {
- test_step_stop = -1;
- }
- optarg = parsed + strspn(parsed, " ");
- if (*optarg != ',' && *optarg != '\0') {
- goto step_unknown;
- }
- if (*optarg == ',') {
- optarg += 1;
- test_step_step = strtoumax(optarg, &parsed, 0);
- // allow empty string for stop=1
- if (parsed == optarg) {
- test_step_step = 1;
- }
- optarg = parsed + strspn(parsed, " ");
- if (*optarg != '\0') {
- goto step_unknown;
- }
- }
- } else {
- // single value = stop only
- test_step_stop = test_step_start;
- test_step_start = 0;
- }
- break;
- step_unknown:
- fprintf(stderr, "error: invalid step: %s\n", optarg);
- exit(-1);
- }
- case OPT_DISK:
- test_disk_path = optarg;
- break;
- case OPT_TRACE:
- test_trace_path = optarg;
- break;
- case OPT_TRACE_BACKTRACE:
- test_trace_backtrace = true;
- break;
- case OPT_TRACE_PERIOD: {
- char *parsed = NULL;
- test_trace_period = strtoumax(optarg, &parsed, 0);
- if (parsed == optarg) {
- fprintf(stderr, "error: invalid trace-period: %s\n", optarg);
- exit(-1);
- }
- break;
- }
- case OPT_TRACE_FREQ: {
- char *parsed = NULL;
- test_trace_freq = strtoumax(optarg, &parsed, 0);
- if (parsed == optarg) {
- fprintf(stderr, "error: invalid trace-freq: %s\n", optarg);
- exit(-1);
- }
- break;
- }
- case OPT_READ_SLEEP: {
- char *parsed = NULL;
- double read_sleep = strtod(optarg, &parsed);
- if (parsed == optarg) {
- fprintf(stderr, "error: invalid read-sleep: %s\n", optarg);
- exit(-1);
- }
- test_read_sleep = read_sleep*1.0e9;
- break;
- }
- case OPT_PROG_SLEEP: {
- char *parsed = NULL;
- double prog_sleep = strtod(optarg, &parsed);
- if (parsed == optarg) {
- fprintf(stderr, "error: invalid prog-sleep: %s\n", optarg);
- exit(-1);
- }
- test_prog_sleep = prog_sleep*1.0e9;
- break;
- }
- case OPT_ERASE_SLEEP: {
- char *parsed = NULL;
- double erase_sleep = strtod(optarg, &parsed);
- if (parsed == optarg) {
- fprintf(stderr, "error: invalid erase-sleep: %s\n", optarg);
- exit(-1);
- }
- test_erase_sleep = erase_sleep*1.0e9;
- break;
- }
- // done parsing
- case -1:
- goto getopt_done;
- // unknown arg, getopt prints a message for us
- default:
- exit(-1);
- }
- }
- getopt_done: ;
- if (argc > optind) {
- // reset our test identifier list
- test_ids = NULL;
- test_id_count = 0;
- test_id_capacity = 0;
- }
- // parse test identifier, if any, cannibalizing the arg in the process
- for (; argc > optind; optind++) {
- test_define_t *defines = NULL;
- size_t define_count = 0;
- lfs_emubd_powercycles_t *cycles = NULL;
- size_t cycle_count = 0;
- // parse name, can be suite or case
- char *name = argv[optind];
- char *defines_ = strchr(name, ':');
- if (defines_) {
- *defines_ = '\0';
- defines_ += 1;
- }
- // remove optional path and .toml suffix
- char *slash = strrchr(name, '/');
- if (slash) {
- name = slash+1;
- }
- size_t name_len = strlen(name);
- if (name_len > 5 && strcmp(&name[name_len-5], ".toml") == 0) {
- name[name_len-5] = '\0';
- }
- if (defines_) {
- // parse defines
- char *cycles_ = strchr(defines_, ':');
- if (cycles_) {
- *cycles_ = '\0';
- cycles_ += 1;
- }
- while (true) {
- char *parsed;
- size_t d = leb16_parse(defines_, &parsed);
- intmax_t v = leb16_parse(parsed, &parsed);
- if (parsed == defines_) {
- break;
- }
- defines_ = parsed;
- if (d >= define_count) {
- // align to power of two to avoid any superlinear growth
- size_t ncount = 1 << lfs_npw2(d+1);
- defines = realloc(defines,
- ncount*sizeof(test_define_t));
- memset(defines+define_count, 0,
- (ncount-define_count)*sizeof(test_define_t));
- define_count = ncount;
- }
- defines[d] = TEST_LIT(v);
- }
- if (cycles_) {
- // parse power cycles
- size_t cycle_capacity = 0;
- while (*cycles_ != '\0') {
- char *parsed = NULL;
- *(lfs_emubd_powercycles_t*)mappend(
- (void**)&cycles,
- sizeof(lfs_emubd_powercycles_t),
- &cycle_count,
- &cycle_capacity)
- = leb16_parse(cycles_, &parsed);
- if (parsed == cycles_) {
- fprintf(stderr, "error: "
- "could not parse test cycles: %s\n",
- cycles_);
- exit(-1);
- }
- cycles_ = parsed;
- }
- }
- }
- // append to identifier list
- *(test_id_t*)mappend(
- (void**)&test_ids,
- sizeof(test_id_t),
- &test_id_count,
- &test_id_capacity) = (test_id_t){
- .name = name,
- .defines = defines,
- .define_count = define_count,
- .cycles = cycles,
- .cycle_count = cycle_count,
- };
- }
- // do the thing
- op();
- // cleanup (need to be done for valgrind testing)
- test_define_cleanup();
- if (test_overrides) {
- for (size_t i = 0; i < test_override_count; i++) {
- free((void*)test_overrides[i].defines);
- }
- free((void*)test_overrides);
- }
- if (test_geometry_capacity) {
- free((void*)test_geometries);
- }
- if (test_powerloss_capacity) {
- for (size_t i = 0; i < test_powerloss_count; i++) {
- free((void*)test_powerlosses[i].cycles);
- }
- free((void*)test_powerlosses);
- }
- if (test_id_capacity) {
- for (size_t i = 0; i < test_id_count; i++) {
- free((void*)test_ids[i].defines);
- free((void*)test_ids[i].cycles);
- }
- free((void*)test_ids);
- }
- }
|