okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0216-core into f-0216-allbd0e878f4

main
5.0 KiB109 linesraw
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_local uint64_t t_allocs = 0;
32void* operator new(size_t n) {
33    ++t_allocs;
34    if (void* p = std::malloc(n ? n : 1)) return p;
35    throw std::bad_alloc();
36}
37void* operator new[](size_t n) { return operator new(n); }
38void operator delete(void* p) noexcept { std::free(p); }
39void operator delete[](void* p) noexcept { std::free(p); }
40void operator delete(void* p, size_t) noexcept { std::free(p); }
41void operator delete[](void* p, size_t) noexcept { std::free(p); }
42
43static double thread_cpu_us() {
44    timespec ts{};
45    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
46    return (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();
51    int calls = 2000;
52    bool threaded = true;
53    for (int i = 1; i < argc; ++i) {
54        const std::string a = argv[i];
55        if (a == "--dir" && i + 1 < argc) dir = argv[++i];
56        else if (a == "--calls" && i + 1 < argc) calls = std::atoi(argv[++i]);
57        else if (a == "--threaded" && i + 1 < argc) threaded = std::atoi(argv[++i]) != 0;
58    }
59    const uint32_t N = 300000, rb = 90;
60    const uint64_t header = 192;
61    const 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);
65        f.write((const char*) h.data(), (std::streamsize) h.size());
66        for (uint32_t i = 0; i < N; ++i) {
67            for (uint32_t b = 0; b < rb; ++b) r[b] = (uint8_t) ((i * 2654435761u + b * 97u) >> 7);
68            std::memcpy(r.data(), &i, 4);
69            f.write((const char*) r.data(), rb);
70        }
71    }
72    ng::PleReader rd;
73    std::string err;
74    if (!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)
79    for (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");
81        for (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);
84            double cpu = 0;
85            uint64_t allocs = 0;
86            const uint64_t dedup0 = rd.snapshot().dedup_rows;
87            const int reps = n >= 3072 ? std::max(20, calls / 30) : calls;
88            for (int c = 0; c < reps; ++c) {
89                const uint32_t base = (uint32_t) (rng() % (N - 4000));
90                for (auto& r : rows) {
91                    if (pattern[0] == 'w' && pattern[1] == 'i') r = base + (uint32_t) (rng() % 4000);
92                    else if (pattern[0] == 'w') r = (uint32_t) (rng() % N);
93                    else { const uint32_t u = (uint32_t) (rng() % 1000); r = u < 400 ? (uint32_t) (rng() % 64) * 4099u % N : (uint32_t) (rng() % N); }
94                }
95                const uint64_t a0 = t_allocs;
96                const double t0 = thread_cpu_us();
97                const auto t = rd.issue(rows.data(), n, out.data());
98                cpu += thread_cpu_us() - t0;
99                allocs += t_allocs - a0;
100                if (!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    }
106    rd.close();
107    std::filesystem::remove(path);
108    return 0;
109}