okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cd9745050
main
1// #5718 follow-up: the stall report names the GPU streams a host may be waiting on and says, from a non-blocking query, 2// whether their work is finished. No GPU: the query is faked per stream; this checks the list (RAII, slots), the line 3// for each state, and that nothing is printed when no stream is watched. 4#include "strata/core/stream_watch.hpp" 5 6#include <cstdint> 7#include <cstdio> 8#include <cstring> 9#include <string> 10 11using namespace strata::core ; 12 13namespace { 14int failed = 0 ; 15#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 16 17std::string report_text () { 18 std::FILE * f = std::tmpfile (); 19if (!f )return "<no tmpfile>" ; 20stream_watch ().report (f , [](void * s ,int , std::string & why ) { 21const auto id = (std::uintptr_t )s ; 22if (id == 1 )return StreamState ::finished ; 23if (id == 2 )return StreamState ::working ; 24why = "invalid resource handle" ; 25return StreamState ::failed ; 26 }); 27 std::string out ; 28 std::rewind (f ); 29char buf [512 ]; 30while (std::fgets (buf ,sizeof buf ,f ))out += buf ; 31 std::fclose (f ); 32return out ; 33} 34}// namespace 35 36int main () { 37CHECK (report_text ().empty ());// nothing watched: no line 38 { 39WatchedStream a ("the prompt's GPU work" , (void * )1 ,0 ); 40CHECK (stream_watch ().count ()== 1 ); 41 { 42WatchedStream b ("the prompt's GPU work" , (void * )2 ,1 ); 43WatchedStream c ("the prompt's GPU work" , (void * )3 ,0 ); 44const std::string t = report_text (); 45 std::printf ("%s" ,t .c_str ()); 46CHECK (t .find (" the prompt's GPU work (GPU 0) had finished, but the engine was still waiting for it\n" ) 47!= std::string::npos ); 48CHECK (t .find (" the prompt's GPU work (GPU 1) was still running\n" )!= std::string::npos ); 49CHECK (t .find (" the state of the prompt's GPU work could not be read: invalid resource handle\n" ) 50!= std::string::npos );CHECK (stream_watch ().count ()== 3 ); 51 } 52CHECK (stream_watch ().count ()== 1 );// b and c left with their scopes 53const std::string t = report_text (); 54CHECK (t .find ("(GPU 1)" )== std::string::npos ); 55 } 56CHECK (stream_watch ().count ()== 0 ); 57CHECK (report_text ().empty ()); 58 {// all slots taken: the extra one is simply not listed, and nothing breaks when it goes 59WatchedStream * many [StreamWatch ::kSlots + 2 ]; 60for (auto & w :many )w = new WatchedStream ("s" , (void * )1 ,0 ); 61CHECK (stream_watch ().count ()== StreamWatch ::kSlots ); 62for (auto & w :many )delete w ; 63CHECK (stream_watch ().count ()== 0 ); 64 } 65if (failed ) { std::fprintf (stderr ,"stream_watch_test: %d failed\n" ,failed );return 1 ; } 66 std::printf ("stream_watch_test: ok\n" ); 67return 0 ; 68}