okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// tests/program/gpu_report_test.cpp - #3797: the out-of-memory report at start (pure text; the NVML query is only 2// checked to be safe to call: it must return, not throw, and leave the list empty when it answers false). 3#include "strata/program/gpu_report.hpp" 4 5#include <cstdio> 6#include <string> 7 8using namespace strata::program::gpu_report ; 9 10namespace { 11int g_fail = 0 ; 12void check (bool ok ,const char * what ,const std::string & got = {}) { 13if ( ok ) return ; 14std:: fprintf ( stderr , "FAIL: %s%s%s\n" , what , got . empty () ? "" : "\n got: " , got . c_str ()); 15++ g_fail ; 16} 17bool has ( const std::string & s , const char * part ) { return s . find ( part ) != std::string:: npos ; } 18} // namespace 19 20int main () { 21{ // the case of #3797: 320 MiB free of a 22 GB card, another program holds the rest 22Card c ; 23c . index = 1 ; 24c . name = "NVIDIA GeForce RTX 2080 Ti" ; 25c . free_mib = 320 ; 26c . total_mib = 21998 ; 27c . processes_known = true; 28c . processes = {{ 4242 , "/usr/bin/python3.12" , 20100 }, { 777 , "/opt/strata/strata" , 900 }, { 1 , "Xorg" , 12 }, { 5 , "" , 0 }}; 29const std::string r = format_gpu_memory_report ({ c }, 777 ); 30check ( has ( r , "CUDA1 (NVIDIA GeForce RTX 2080 Ti): 320 MiB free of 21998 MiB" ), "free and total" , r ); 31check ( has ( r , "python3.12 (pid 4242) 19.6 GiB" ), "the largest other program, by file name" , r ); 32check (! has ( r , "strata (pid 777)" ), "this process is not listed as another program" , r ); 33check ( has ( r , "Xorg (pid 1) 12 MiB" ), "a small one is still named" , r ); 34check ( has ( r , "1 program whose memory the driver does not report" ), "a process of unknown size is counted" , r ); 35check ( r . find ( "python3.12" ) < r . find ( "Xorg" ), "largest first" , r ); 36} 37{ // more than three: the rest is counted 38Card c ; 39c . index = 0 ; 40c . free_mib = 100 ; 41c . total_mib = 12288 ; 42c . processes_known = true; 43for (uint32_t i = 1 ; i <= 6 ; ++ i ) c . processes . push_back ({ i , "p" + std:: to_string ( i ), 100u * i }); 44const std::string r = format_gpu_memory_report ({ c }, 0 ); 45check ( has ( r , "p6 (pid 6) 600 MiB, p5 (pid 5) 500 MiB, p4 (pid 4) 400 MiB, and 3 more" ), "three named, three counted" , r ); 46check (! has ( r , "p3 (pid 3)" ), "the fourth is not named" , r ); 47} 48{ // the driver would not say: no claim about other programs 49Card c ; 50c . index = 0 ; 51c . free_mib = 512 ; 52c . total_mib = 8192 ; 53c . processes_known = false; 54const std::string r = format_gpu_memory_report ({ c }, 1 ); 55check ( has ( r , "512 MiB free of 8192 MiB; the driver does not say which programs use it here" ), "unknown processes" , r ); 56check (! has ( r , "other programs on it" ), "no list" , r ); 57} 58{ // the driver answered with nothing but ourselves: said carefully (Windows often lists no program at all) 59Card c ; 60c . index = 0 ; 61c . free_mib = 700 ; 62c . total_mib = 12288 ; 63c . processes_known = true; 64c . processes = {{ 99 , "strata.exe" , 11000 }}; 65const std::string r = format_gpu_memory_report ({ c }, 99 ); 66check ( has ( r , "the driver lists no other program using it" ), "only ourselves" , r ); 67} 68{ // two cards, a pid with no name, a Windows path 69Card a , b ; 70a . index = 0 ; a . free_mib = 2000 ; a . total_mib = 16000 ; a . processes_known = true; 71b . index = 1 ; b . free_mib = 50 ; b . total_mib = 12000 ; b . processes_known = true; 72b . processes = {{ 31 , "" , 11000 }, { 32 , "C:\\Program Files\\Obs\\obs64.exe" , 300 }}; 73const std::string r = format_gpu_memory_report ({ a , b }, 5 ); 74check ( has ( r , "CUDA0: 2000 MiB free of 16000 MiB; the driver lists no other program using it\n" ), "first card line" , r ); 75check ( has ( r , "CUDA1: 50 MiB free of 12000 MiB; other programs on it: pid 31 10.7 GiB, obs64.exe (pid 32) 300 MiB" ), "second card line" , r ); 76} 77check ( format_gpu_memory_report ({}, 1 ). empty (), "no cards, no text" ); 78{ // planned versus free 79check ( planned_vs_free_text ( 796 , 68 ) == 80"the layer split planned to keep 796 MiB free here after loading; 68 MiB are free, so 728 MiB went to something the plan did not count" , 81"planned versus free" , planned_vs_free_text ( 796 , 68 )); 82check ( planned_vs_free_text ( 796 , 796 ). empty () && planned_vs_free_text ( 796 , 900 ). empty (), "enough free: nothing to say" ); 83check ( planned_vs_free_text ( 0 , 10 ). empty () && planned_vs_free_text ( -5 , 10 ). empty (), "no plan, no text" ); 84check ( has ( planned_vs_free_text ( 300 , -4 ), "0 MiB are free, so 300 MiB" ), "a negative free count is read as 0" ); 85} 86{ // the query never throws and never leaves a half-filled list behind a false 87std::vector < Process > p {{ 1 , "x" , 1 }}; 88const bool ok = query_other_processes ( "" , p ); 89check (! ok && p . empty (), "an empty bus id is refused" ); 90std::vector < Process > q {{ 1 , "x" , 1 }}; 91const bool r = query_other_processes ( "ffff:ff:ff.f" , q ); // no such card: false, or true with a list; both are fine 92check ( r || q . empty (), "a failed query leaves the list empty" ); 93} 94std:: printf ( g_fail ? "FAIL\n" : "PASS\n" ); 95return g_fail ? 1 : 0 ; 96}