okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
90a5d082b
main
1// pin_budget_test: the pinned-memory headroom (include/strata/core/pin_budget.hpp) with the simulated ceiling. 2// Needs a GPU for the pinned calls; without one it prints SKIP and passes. 3#include "strata/core/pin_budget.hpp" 4 5#include <cstdio> 6#include <cstdlib> 7#include <cstring> 8 9static int g_fail = 0 ; 10#define CHECK (c ) \ 11 do { \ 12 if (!(c)) { std::fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++g_fail; } \ 13 } while (0) 14 15#ifdef _WIN32 16#define SETENV (k ,v ) _putenv_s(k, v) 17#else 18#define SETENV (k ,v ) setenv(k, v, 1) 19#endif 20 21int main () { 22// the singleton reads its environment on first use: set everything first 23SETENV ("STRATA_TEST_PIN_BUDGET_MIB" ,"100" ); 24SETENV ("STRATA_PIN_RESERVE_MIB" ,"32" );// soft 32 MiB, hard 64 MiB (a quarter of 32 is below the 64 MiB floor) 25using namespace strata::core ; 26if (cudaFree (0 )!= cudaSuccess ) { 27 std::printf ("pin_budget_test: SKIP (no GPU)\n" ); 28return 0 ; 29 } 30 (void )cudaGetLastError (); 31 32// 1. a registration beyond the ceiling is refused, one inside it works and is charged 33 { 34void * big = std::malloc (160u <<20 ); 35void * small = std::malloc (8u <<20 ); 36CHECK (pin_register (big ,160u <<20 ,cudaHostRegisterPortable )== cudaErrorMemoryAllocation ); 37CHECK (pin_register (small ,8u <<20 ,cudaHostRegisterPortable )== cudaSuccess ); 38CHECK (pin_detail::st ().sim_used == (8u <<20 )); 39CHECK (pin_unregister (small )== cudaSuccess ); 40CHECK (pin_detail::st ().sim_used == 0 ); 41 std::free (big ); 42 std::free (small ); 43 } 44// 2. without a reserve a refused guarded allocation stays refused 45 { 46void * p = nullptr ; 47CHECK (host_alloc_guarded (& p ,200u <<20 ,cudaHostAllocMapped )!= cudaSuccess ); 48 (void )cudaGetLastError (); 49 } 50// 3. the reserves are held, the arena (here: a registration) takes what is left, a required buffer is then 51// refused, the held headroom is handed back and the retry works 52 { 53pin_reserve_take (); 54CHECK (pin_detail::st ().hard != nullptr && pin_detail::st ().hard_bytes == (64u <<20 )); 55CHECK (pin_detail::st ().soft != nullptr && pin_detail::st ().soft_bytes == (32u <<20 )); 56CHECK (pin_detail::st ().sim_used == (96u <<20 )); 57void * arena = std::malloc (4u <<20 ); 58CHECK (pin_register (arena ,4u <<20 ,cudaHostRegisterPortable )== cudaSuccess );// 100 MiB used: the ceiling 59pin_reserve_release_soft (); 60CHECK (pin_detail::st ().soft == nullptr ); 61CHECK (pin_detail::st ().sim_used == (68u <<20 )); 62void * a = nullptr ; 63CHECK (host_alloc_guarded (& a ,16u <<20 ,cudaHostAllocMapped )== cudaSuccess );// fits beside the hard reserve 64void * b = nullptr ; 65CHECK (host_alloc_guarded (& b ,40u <<20 ,cudaHostAllocMapped )== cudaSuccess );// refused first, then the hard reserve goes 66CHECK (pin_detail::st ().hard == nullptr ); 67CHECK (pin_detail::st ().hard_retries == 1 ); 68void * c = nullptr ; 69CHECK (host_alloc_guarded (& c ,60u <<20 ,cudaHostAllocMapped )!= cudaSuccess );// truly out: reported, not hidden 70 (void )cudaGetLastError (); 71CHECK (pin_exhausted_hint ().find ("[pinned-memory-exhausted]" )!= std::string::npos ); 72 (void )pin_unregister (arena ); 73 std::free (arena ); 74 } 75 std::printf (g_fail == 0 ?"pin_budget_test: OK\n" :"pin_budget_test: %d FAILED\n" ,g_fail ); 76return g_fail == 0 ?0 :1 ; 77}