#include "kar_expert_cache.hpp" #include #include #include #include using namespace strata::kar; namespace { void require(bool ok,const char* what) { if(!ok) throw std::runtime_error(what); } ExpertCacheKey key(uint32_t expert,uint64_t offset) { ExpertCacheKey k{}; k.mapping=0x1234; k.archive_bytes=4096; k.expert=expert; k.level=2; for (auto& t:k.tensors) { t.books_offset=offset; t.n_dims=3; t.dims[0]=64; t.dims[1]=32; t.dims[2]=8; } return k; } void upload(ExpertLruIndex& cache,const ExpertCacheKey& k) { size_t slot=0; require(cache.begin_upload(slot),"begin upload rejected"); require(cache.publish_upload(slot,k),"publish rejected"); } } int main() try { const auto a=key(0,100), b=key(1,200), c=key(2,300); ExpertLruIndex cache(2); upload(cache,a); upload(cache,b); size_t slot=99; require(cache.lookup(a,slot) && slot==0,"repeat hit/mru failed"); size_t victim=99; require(cache.begin_upload(victim) && victim==1,"LRU victim was not expert B"); require(cache.publish_upload(victim,c),"replacement publish failed"); require(!cache.lookup(b,slot),"evicted key still hit"); require(cache.lookup(a,slot) && slot==0,"resident key lost after eviction"); require(cache.capacity()==2 && cache.stats().uploads==3 && cache.stats().hits==2 && cache.stats().misses==1 && cache.stats().evictions==1,"budget or counters wrong"); require(cache.begin_run(),"run ownership acquire failed"); require(!cache.begin_upload(victim) && !cache.lookup(a,slot) && !cache.begin_run(), "pending run allowed cache mutation or overlapping owner"); require(cache.finish_run() && !cache.finish_run(),"run ownership release invalid"); require(cache.begin_upload(victim),"upload after run completion rejected"); cache.fail_upload(); require(cache.failed() && !cache.begin_run() && !cache.begin_upload(victim) && !cache.lookup(a,slot), "failed upload did not fail closed"); ExpertLruIndex geometry(1); upload(geometry,a); auto wrong=key(0,101); require(!geometry.lookup(wrong,slot),"different slice identity hit cached expert"); struct ReferenceEntry { ExpertCacheKey key{}; uint64_t age=0; bool valid=false; }; constexpr size_t slots=5, operations=10000; ExpertLruIndex actual(slots); std::array reference{}; uint64_t clock=0, hits=0, misses=0, uploads=0, evictions=0; uint32_t random=0x5eed1234u; for(size_t n=0;n>8)%13; const uint64_t slice=100+((random>>20)%3)*4096; const auto request=key(expert,slice); size_t expected=slots; for(size_t i=0;i