okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8f70a52cf
main
1// alias_scan: allocate device buffers of assorted sizes, keep them, and run strata::arena_alias_check on each: where does 2// the xe page table alias pages (a write to one 2 MiB page showing at another)? Prints each buffer's address mod 1 GiB. 3// alias_scan [n=40] [seed=1] [max_mib=6000] 4#include <sycl/sycl.hpp> 5#include <cstdio> 6#include <cstdlib> 7#include <random> 8#include <vector> 9#include "strata/sycl_queue.hpp" 10 11int main (int argc ,char ** argv ) { 12const int n = argc > 1 ? std::atoi (argv [1 ]) :40 ; 13const unsigned seed = argc > 2 ? (unsigned ) std::atoi (argv [2 ]) :1 ; 14const size_t max_mib = argc > 3 ? (size_t ) std::atoll (argv [3 ]) :6000 ; 15 sycl::queue q {sycl::gpu_selector_v , sycl::property::queue::in_order {}}; 16 std::mt19937_64 rng (seed ); 17 std::vector < void *> keep ; 18size_t total = 0 ; 19int bad_bufs = 0 ; 20for (int i = 0 ;i < n ;++ i ) { 21size_t mib = (rng () %4 == 0 ) ?1 + rng () %8 :1 + rng () %max_mib ; 22if (total + mib > 24000 )break ; 23const size_t bytes = mib <<20 ; 24void * p = sycl::malloc_device (bytes ,q ); 25if (!p ) { std::printf ("alloc %zu MiB failed\n" ,mib );break ; } 26keep .push_back (p ); 27total += mib ; 28size_t first = 0 ; 29const uint64_t bad = strata::arena_alias_check (q ,p ,bytes ,& first ); 30 std::printf ("#%02d %6zu MiB at %p (mod 1GiB %5zu MiB, mod 2MiB %zu): %s" ,i ,mib ,p , (size_t ) (((uintptr_t )p & ((1u <<30 )- 1 )) >>20 ), 31 (size_t ) ((uintptr_t )p & ((2u <<20 )- 1 )),bad ?"ALIAS" :"ok" ); 32if (bad ) {++ bad_bufs ; std::printf (" %llu blocks, first at +%zu MiB" , (unsigned long long )bad ,first >>20 ); } 33 std::printf ("\n" ); 34 } 35 std::printf ("%d of %zu buffers aliased\n" ,bad_bufs ,keep .size ()); 36for (void * p :keep ) sycl::free (p ,q ); 37return bad_bufs ?3 :0 ; 38}