okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cd9745050
main
1// #5697: an evicted parked conversation must give its RAM back to the system, because the physical-RAM gate that 2// evicted it reads MemAvailable right after. With glibc the K/V segments of a parked conversation land in the heap 3// once the dynamic mmap threshold has risen (it does at the first freed segment), and free() hands back only the top of 4// the heap: an entry below a newer one stayed resident, so the gate evicted everything and still refused. 5// 6// The test parks three conversations, evicts the oldest (the one below the others) and reads the process RSS from 7// /proc/self/statm before and after. Linux with glibc only; elsewhere it says SKIPPED. It also prints what the 8// eviction costs in time. 9#include "strata/core/conversation_cache.hpp" 10 11#include <chrono> 12#include <cstdio> 13#include <cstdlib> 14#include <cstring> 15#include <vector> 16 17#if defined(__linux__ )&& defined(__GLIBC__ ) 18#include <unistd.h> 19 20using namespace strata::core ; 21 22namespace { 23constexpr size_t MiB = size_t (1 ) <<20 ; 24constexpr size_t layers = 32 ,part = 8 * MiB ;// 32 layers x (K + V) x 8 MiB = 512 MiB per conversation 25 26size_t rss_bytes () { 27 std::FILE * f = std::fopen ("/proc/self/statm" ,"r" ); 28if (!f )return 0 ; 29unsigned long size = 0 ,resident = 0 ; 30const int n = std::fscanf (f ,"%lu %lu" ,& size ,& resident ); 31 std::fclose (f ); 32return n == 2 ?size_t (resident )* size_t (sysconf (_SC_PAGESIZE )) :0 ; 33} 34 35SavedConversation conversation (int32_t id ,uint8_t fill ) { 36SavedConversation s ; 37s .live .ids = {id ,id + 1 ,id + 2 }; 38s .live .gdn .resize (64 ,fill ); 39s .kv .resize (layers ); 40for (auto & layer :s .kv ) { 41layer .k .resize (part ,fill );// written: the pages are resident, as a parked image's are 42layer .v .resize (part ,fill ); 43 } 44return s ; 45} 46} 47 48int main () { 49// What the engine's first freed segment does: a freed mmapped block of this size raises glibc's mmap threshold, 50// so the segments allocated after it come from the heap. 51 { 52 std::vector < uint8_t > warm (16 * MiB ); 53 std::memset (warm .data (),1 ,warm .size ()); 54 } 55ConversationCache cache (size_t (8 ) <<30 ,4 ); 56for (int32_t i = 0 ;i < 3 ;++ i ) 57if (!cache .put (conversation (100 * (i + 1 ),uint8_t (i + 1 )))) { std::fprintf (stderr ,"FAIL: park %d\n" ,i );return 1 ; } 58const size_t entry = cache .bytes () /3 ; 59const size_t before = rss_bytes (); 60const auto t0 = std::chrono::steady_clock::now (); 61const bool evicted = cache .evict_oldest (); 62const double ms = std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count (); 63const size_t after = rss_bytes (); 64const size_t returned = before > after ?before - after :0 ; 65 std::printf ("conversation_heap_test: parked 3 x %zu MiB, RSS %zu MiB -> %zu MiB after evicting the oldest " 66"(%zu MiB returned to the system), eviction took %.2f ms\n" , 67entry /MiB ,before /MiB ,after /MiB ,returned /MiB ,ms ); 68if (!evicted || cache .size ()!= 2 ) { std::fprintf (stderr ,"FAIL: evict_oldest\n" );return 1 ; } 69if (returned < entry * 8 /10 ) { 70 std::fprintf (stderr ,"FAIL: the evicted conversation's RAM stayed with the process (%zu of %zu MiB returned)\n" , 71returned /MiB ,entry /MiB ); 72return 1 ; 73 } 74// the second eviction: the heap holds the third conversation above it, nothing else freed 75const auto t1 = std::chrono::steady_clock::now (); 76cache .evict_oldest (); 77 std::printf ("conversation_heap_test: the second eviction took %.2f ms, RSS now %zu MiB\n" , 78 std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t1 ).count (),rss_bytes () /MiB ); 79 std::printf ("conversation_heap_test: ok\n" ); 80return 0 ; 81} 82#else 83int main () { 84 std::printf ("SKIPPED: conversation_heap_test needs Linux with glibc\n" ); 85return 0 ; 86} 87#endif