okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bd0e878f4
main
1// ple_issue_bench: host allocations and CPU time of PleReader::issue() (PR #4910), before/after, on a synthetic table. 2// CPU and a temp file only, no model, no GPU. Not a ctest target; build it against the reader under test: 3// 4// g++ -O2 -std=c++20 -pthread -Iinclude tests/ngram/ple_issue_bench.cpp src/ngram/ple_reader.cpp \ 5// src/platform/direct_file.cpp src/platform/memory.cpp -o /tmp/ple_issue_bench 6// /tmp/ple_issue_bench [--dir D] [--calls N] [--threaded 0|1] 7// 8// For each batch size it issues `calls` tickets of random rows (no row cache, so every row misses), collecting each before 9// the next, and prints: heap allocations made by the calling thread inside issue() per call, thread CPU time inside issue() 10// per call, and dedup_rows per call (the same on both builds: the rows that share a page). Allocation counting replaces the 11// global operator new, so the numbers are exact for this thread. 12#include "strata/ngram/ple_reader.hpp" 13 14#include <time.h> 15 16#include <algorithm> 17#include <atomic> 18#include <cstdint> 19#include <cstdio> 20#include <cstdlib> 21#include <cstring> 22#include <filesystem> 23#include <fstream> 24#include <new> 25#include <random> 26#include <string> 27#include <vector> 28 29namespace ng= strata::ngram; 30 31static thread_localuint64_t t_allocs = 0 ; 32void * operatornew (size_t n ) { 33++ t_allocs ; 34if (void * p = std::malloc (n ?n :1 ))return p ; 35throw std::bad_alloc (); 36} 37void * operatornew [](size_t n ) {return operatornew (n ); } 38void operatordelete (void * p )noexcept { std::free (p ); } 39void operatordelete [](void * p )noexcept { std::free (p ); } 40void operatordelete (void * p ,size_t )noexcept { std::free (p ); } 41void operatordelete [](void * p ,size_t )noexcept { std::free (p ); } 42 43static double thread_cpu_us () { 44timespec ts {}; 45clock_gettime (CLOCK_THREAD_CPUTIME_ID ,& ts ); 46return (double )ts .tv_sec * 1e6 + (double )ts .tv_nsec /1e3 ; 47} 48 49int main (int argc ,char ** argv ) { 50 std::string dir = std::filesystem::temp_directory_path ().string (); 51int calls = 2000 ; 52bool threaded = true; 53for (int i = 1 ;i < argc ;++ i ) { 54const std::string a = argv [i ]; 55if (a == "--dir" && i + 1 < argc )dir = argv [++ i ]; 56else if (a == "--calls" && i + 1 < argc )calls = std::atoi (argv [++ i ]); 57else if (a == "--threaded" && i + 1 < argc )threaded = std::atoi (argv [++ i ])!= 0 ; 58 } 59const uint32_t N = 300000 ,rb = 90 ; 60const uint64_t header = 192 ; 61const std::string path = dir + "/ple_issue_bench.bin" ; 62 { 63 std::ofstream f (path , std::ios::binary ); 64 std::vector < uint8_t > h (header ,0xAB ),r (rb ); 65f .write ((const char * )h .data (), (std::streamsize )h .size ()); 66for (uint32_t i = 0 ;i < N ;++ i ) { 67for (uint32_t b = 0 ;b < rb ;++ b )r [b ]= (uint8_t ) ((i * 2654435761u + b * 97u ) >>7 ); 68 std::memcpy (r .data (),& i ,4 ); 69f .write ((const char * )r .data (),rb ); 70 } 71 } 72 ng::PleReader rd ; 73 std::string err ; 74if (!rd .open (path ,header ,N ,64 ,0 ,err ,threaded ,rb )) { std::fprintf (stderr ,"open: %s\n" ,err .c_str ());return 2 ; } 75 std::mt19937 rng (4910 ); 76 std::printf ("threaded=%d calls=%d\n" ,threaded ,calls ); 77// three row patterns: a 4000-row window (many rows share a page: decode neighbours), the whole table (a chunk of 78// distinct n-grams: nearly every row its own page), and a skewed draw (a few hot rows repeat, the rest spread) 79for (const char * pattern : {"window" ,"wide" ,"skewed" }) { 80 std::printf ("pattern %s\n%8s %14s %14s %12s\n" ,pattern ,"rows" ,"allocs/issue" ,"cpu us/issue" ,"dedup/issue" ); 81for (size_t n : {(size_t )1 , (size_t )4 , (size_t )16 , (size_t )64 , (size_t )65 , (size_t )256 , (size_t )3072 , (size_t )49152 }) { 82 std::vector < uint32_t > rows (n ); 83 std::vector < uint8_t > out (n * rb ); 84double cpu = 0 ; 85uint64_t allocs = 0 ; 86const uint64_t dedup0 = rd .snapshot ().dedup_rows ; 87const int reps = n >=3072 ? std::max (20 ,calls /30 ) :calls ; 88for (int c = 0 ;c < reps ;++ c ) { 89const uint32_t base = (uint32_t ) (rng () % (N - 4000 )); 90for (auto & r :rows ) { 91if (pattern [0 ]== 'w' && pattern [1 ]== 'i' )r = base + (uint32_t ) (rng () %4000 ); 92else if (pattern [0 ]== 'w' )r = (uint32_t ) (rng () %N ); 93else {const uint32_t u = (uint32_t ) (rng () %1000 );r = u < 400 ? (uint32_t ) (rng () %64 )* 4099u %N : (uint32_t ) (rng () %N ); } 94 } 95const uint64_t a0 = t_allocs ; 96const double t0 = thread_cpu_us (); 97const auto t = rd .issue (rows .data (),n ,out .data ()); 98cpu += thread_cpu_us ()- t0 ; 99allocs += t_allocs - a0 ; 100if (!rd .collect (t ,err )) { std::fprintf (stderr ,"collect: %s\n" ,err .c_str ());return 1 ; } 101 } 102 std::printf ("%8zu %14.2f %14.3f %12.2f\n" ,n , (double )allocs /reps ,cpu /reps , 103 (double ) (rd .snapshot ().dedup_rows - dedup0 ) /reps ); 104 } 105 } 106rd .close (); 107 std::filesystem::remove (path ); 108return 0 ; 109}