okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// Host test for include/strata/prefill/copy_events.hpp (#4005): the copied-event rules of the streamed prompt walk. 2#include "strata/prefill/copy_events.hpp" 3 4#include <cstdio> 5#include <cstdlib> 6#include <vector> 7 8using namespace strata::prefill ; 9 10static int g_fail = 0 ; 11static void check (bool ok ,const char * what ) { 12if (!ok ) { std::printf ("FAIL: %s\n" ,what );++ g_fail ; } 13} 14 15int main () { 16check (copy_event_every_from (0 )== 1 && copy_event_every_from (1 )== 1 && copy_event_every_from (-5 )== 1 ,"unset / 0 / 1 / negative: every entry" ); 17check (copy_event_every_from (2 )== 2 && copy_event_every_from (16 )== 16 && copy_event_every_from (64 )== 64 ,"2..64 as given" ); 18check (copy_event_every_from (65 )== 64 && copy_event_every_from (100000 )== 64 ,"above 64 is 64" ); 19 20// default: every entry records (the walk then waits on copied[slot], as before the switch) 21for (size_t total : {1u ,2u ,7u ,100u }) 22for (size_t i = 0 ;i < total ;++ i )check (copy_event_due (i ,total ,1 ),"every == 1 records every entry" ); 23 24// sparse: the walk's issuer/consumer as a model. The issuer records per copy_event_due (and, when it must wait for 25// the consumer, the entry before the one it is about to issue); the consumer for entry k waits on 26// copy_event_entry(k, recorded, ...). Every wait must name a recorded entry at or after k, and the ring must 27// never need an event that a later entry has overwritten. 28 std::srand (4005 ); 29for (int every : {2 ,3 ,5 ,16 ,64 }) 30for (int ring : {2 ,4 ,8 ,96 }) 31for (size_t total : {1u ,5u ,33u ,300u }) { 32 std::vector < long long > rec_of ((size_t )ring ,-1 ); 33size_t recorded = 0 ,issued = 0 ,consumed = 0 ; 34long long last_rec = -1 ; 35bool ok = true; 36auto record = [& ](size_t i ) { 37if (rec_of [i % (size_t )ring ] >=0 && (size_t )rec_of [i % (size_t )ring ] >=consumed )ok = false;// overwrote a live event 38rec_of [i % (size_t )ring ]= (long long )i ; 39recorded = i + 1 ; 40last_rec = (long long )i ; 41 }; 42auto cover = [& ](size_t idx ) {if (idx > 0 && last_rec < (long long )idx - 1 )record (idx - 1 ); }; 43size_t guard = 0 ; 44while (consumed < total && ok && ++ guard < 100000 ) { 45const bool can_issue = issued < total && issued < consumed + (size_t )ring ; 46const bool can_consume = consumed < issued ; 47const bool pick_issue = can_issue && (!can_consume || std::rand () %2 ); 48if (pick_issue ) { 49const size_t idx = issued ; 50if (std::rand () %4 == 0 )cover (idx );// the issuer had to wait (ring full or stager) 51if (copy_event_due (idx ,total ,every ))record (idx ); 52++ issued ; 53 }else if (can_consume ) { 54if (issued == total || true) { 55// a consumer needs recorded > k; when the issuer is blocked it has covered what it issued 56if (recorded <=consumed ) { 57if (issued < total && !(issued < consumed + (size_t )ring ))cover (issued ); 58if (issued == total )cover (issued ); 59 } 60if (recorded <=consumed ) {continue ; }// would spin until the issuer records 61const size_t r = copy_event_entry (consumed ,recorded , [& ](size_t e ) {return rec_of [e % (size_t )ring ]== (long long )e ; }); 62if (r < consumed || r >=recorded || rec_of [r % (size_t )ring ]!= (long long )r )ok = false; 63++ consumed ; 64 } 65 }else { 66break ; 67 } 68 } 69char what [128 ]; 70 std::snprintf (what ,sizeof what ,"sparse every=%d ring=%d total=%zu: every wait names a live recorded entry" ,every ,ring ,total ); 71check (ok && consumed == total ,what ); 72 } 73 74// copy_wait_dedupe_ok: skipping the copy stream's wait on a slot's release event must never leave a slot's refill 75// without a wait on a record at or after the flush that released it. Two walks of random length with random group 76// sizes (1..group), the issuer and the consumer interleaved at random. A "record" is a counter tick of the event. 77auto simulate = [& ](int ring ,int group ,bool original_rule ,unsigned seed )-> bool { 78 std::srand (seed ); 79 std::vector < int > used_of ((size_t )ring ); 80 std::vector < long long > rec_time ((size_t )ring ,0 ),last_flush ((size_t )ring ,0 ); 81for (int i = 0 ;i < ring ;++ i )used_of [(size_t )i ]= i ; 82long long tick = 0 ; 83for (int walk = 0 ;walk < 3 ;++ walk ) { 84const size_t n = (size_t )ring * (size_t ) (2 + std::rand () %3 )+ (size_t ) (std::rand () % (ring + 1 )); 85 std::vector < size_t > group_end ;// the last entry of each group, in order 86for (size_t s = 0 ;s < n ;) { 87size_t len = (size_t ) (1 + std::rand () %group ); 88if (s + len > n )len = n - s ; 89s += len ; 90group_end .push_back (s - 1 ); 91 } 92size_t issued = 0 ,consumed = 0 ,g = 0 ; 93const int issue_pct = std::vector < int > {50 ,90 ,99 }[(size_t ) (std::rand () %3 )];// how far ahead the issuer runs 94int prev_rel = -1 ; 95long long prev_cap = 0 ; 96 std::vector < bool > live ((size_t )ring ,walk > 0 );// the first walk starts with empty slots 97size_t guard = 0 ; 98while (consumed < n && ++ guard < 1000000 ) { 99const bool can_issue = issued < n && issued < consumed + (size_t )ring ; 100const bool can_flush = g < group_end .size ()&& group_end [g ]< issued ; 101if (can_issue && (!can_flush || std::rand () %100 < issue_pct )) { 102const size_t idx = issued ++ ; 103const int sl = (int ) (idx % (size_t )ring ); 104if (live [(size_t )sl ]) { 105const int rel = used_of [(size_t )sl ]; 106const bool skip = original_rule ? (idx >= (size_t )ring && rel == prev_rel ) 107 :copy_wait_dedupe_ok (idx ,ring ,group ,rel ,prev_rel ); 108const long long cap = skip ?prev_cap :rec_time [(size_t )rel ]; 109if (cap < last_flush [(size_t )sl ])return false;// the refill would not wait for its release 110prev_cap = cap ; 111prev_rel = rel ; 112 }else { 113prev_rel = -1 ; 114 } 115live [(size_t )sl ]= true; 116 }else if (can_flush ) { 117size_t first = consumed ; 118const size_t last = group_end [g ++ ]; 119const int rel = (int ) (last % (size_t )ring ); 120rec_time [(size_t )rel ]= ++ tick ; 121for (size_t e = first ;e <=last ;++ e ) { 122used_of [e % (size_t )ring ]= rel ; 123last_flush [e % (size_t )ring ]= tick ; 124 } 125consumed = last + 1 ; 126 }else { 127return false;// stuck: the model is wrong 128 } 129 } 130 } 131return true; 132 }; 133for (int ring : {17 ,18 ,24 ,32 ,96 ,1024 }) 134for (unsigned seed = 1 ;seed <=300 ;++ seed ) { 135char what [96 ]; 136 std::snprintf (what ,sizeof what ,"dedupe never skips a needed wait (ring %d, seed %u)" ,ring ,seed ); 137if (!simulate (ring ,16 , false,seed )) {check (false,what );break ; } 138 } 139 { 140bool rule_ok = true;// the rule refuses a ring that a group can fill 141for (size_t idx : {20u ,1000u })rule_ok = rule_ok && !copy_wait_dedupe_ok (idx ,16 ,16 ,3 ,3 ); 142check (rule_ok ,"a ring of one group never dedupes" ); 143check (!copy_wait_dedupe_ok (32 ,32 ,16 ,3 ,3 )&& copy_wait_dedupe_ok (33 ,32 ,16 ,3 ,3 ),"the first round never dedupes" ); 144check (!copy_wait_dedupe_ok (40 ,32 ,16 ,3 ,4 )&& !copy_wait_dedupe_ok (40 ,32 ,16 ,3 ,-1 ),"different events or none: wait" ); 145// the control: the PR's first rule (idx >= ring) is caught by this model 146bool caught = false; 147for (int ring : {17 ,18 ,20 ,24 ,32 })for (unsigned seed = 1 ;seed <=600 && !caught ;++ seed )caught = !simulate (ring ,16 , true,seed ); 148check (caught ,"the model finds the stale-event race in the rule 'idx >= ring'" ); 149 } 150 151if (g_fail == 0 ) std::printf ("copy_events_test: all checks passed\n" ); 152return g_fail == 0 ?0 :1 ; 153}