Spaces:
Running
Running
File size: 7,492 Bytes
7bd613a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | #include "../include/afterkey.hpp"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <vector>
// g++ -std=c++17 -O3 -DNDEBUG bench/fanout.cpp -o /tmp/afterkey-fanout
// taskset -c 2 /tmp/afterkey-fanout
// The result is JSON. Timings cover the call and its consuming callbacks. Reader
// scheduling, prefill and result verification are outside each sample.
using Key = std::uint32_t;
using U64 = std::uint64_t;
using Clock = std::chrono::steady_clock;
constexpr Key capacity = 1U << 20;
constexpr Key readers = 4096;
constexpr Key warmup = 8192;
constexpr Key measured_writes = 32768;
constexpr unsigned trials = 3;
class CursorLog {
struct Row { U64 revision{}; U64 value{}; bool present{}; };
std::vector<Row> rows;
std::vector<Key> history;
void write(Key key, U64 value, bool present) {
history.push_back(key);
rows[key] = {history.size(), value, present};
}
public:
explicit CursorLog(Key n) : rows(n) { history.reserve(n + warmup + measured_writes); }
void put(Key k, U64 v) { write(k, v, true); }
void erase(Key k) { write(k, 0, false); }
template<class Sink> void since(U64 cursor, Sink&& consume) const {
for (std::size_t i = cursor; i < history.size(); ++i) {
Key k = history[i];
const auto& r = rows[k];
if (r.revision == i + 1) consume(r.revision, k, r.present ? &r.value : nullptr);
}
}
};
static U64 random_word(U64& state) {
state += 0x9e3779b97f4a7c15ULL;
U64 z = state;
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
return z ^ (z >> 31);
}
struct Write { Key key; U64 value; };
struct Trace {
std::vector<Write> writes;
std::vector<std::vector<Key>> polls;
std::size_t measured_polls = 0;
explicit Trace(bool uniform) : polls(warmup + measured_writes) {
U64 writer_rng = 0x20190d;
U64 reader_rng = 0x10074d;
for (Key i = 0; i < polls.size(); ++i) {
Key word = static_cast<Key>(random_word(writer_rng));
// Move 16 of the 64 hot keys every 512 writes. All readers see the
// same state history, with different cuts based on their own pace.
Key base = (i / 512) * 16;
Key k = uniform ? word % capacity : (base + word % 64) % capacity;
writes.push_back({k, random_word(writer_rng)});
}
for (Key reader = 0; reader < readers; ++reader) {
Key period = 256U << (reader % 6);
Key first = random_word(reader_rng) % period;
for (Key i = first; i < polls.size(); i += period) {
polls[i].push_back(reader);
measured_polls += i >= warmup;
}
}
}
};
struct Output { U64 hash, count; };
struct Samples { std::vector<U64> latency; U64 emitted = 0; };
static U64 ns(Clock::time_point start, Clock::time_point end) {
return std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
}
template<class Store>
static void run(const Trace& trace, Samples& samples, std::vector<Output>& oracle, bool first) {
Store store(capacity);
for (Key k = 0; k < capacity; ++k) store.put(k, k);
std::vector<U64> cursors(readers, capacity);
std::size_t poll_num = 0;
U64 emitted = 0;
for (std::size_t i = 0; i < trace.writes.size(); ++i) {
const auto& w = trace.writes[i];
if ((w.value & 31) == 0) store.erase(w.key);
else store.put(w.key, w.value);
for (Key reader : trace.polls[i]) {
U64 cursor = cursors[reader];
Output out{0xcbf29ce484222325ULL ^ cursor, 0};
auto consume = [&](U64 revision, Key key, const U64* value) {
out.hash = (out.hash ^ revision) * 1099511628211ULL;
out.hash = (out.hash ^ key) * 1099511628211ULL;
out.hash = (out.hash ^ (value ? *value : 0)) * 1099511628211ULL;
out.hash ^= value != nullptr;
++out.count;
};
auto start = Clock::now();
std::atomic_signal_fence(std::memory_order_seq_cst);
store.since(cursor, consume);
std::atomic_signal_fence(std::memory_order_seq_cst);
auto end = Clock::now();
cursors[reader] = capacity + i + 1;
if (i >= warmup) {
samples.latency.push_back(ns(start, end));
emitted += out.count;
if (first) oracle.push_back(out);
else if (out.count != oracle.at(poll_num).count || out.hash != oracle[poll_num].hash)
throw std::runtime_error("per-poll output mismatch");
++poll_num;
}
}
}
if (poll_num != trace.measured_polls) throw std::runtime_error("missing poll");
samples.emitted += emitted;
}
static U64 percentile(const std::vector<U64>& sorted, double fraction) {
auto rank = static_cast<std::size_t>(std::ceil(fraction * sorted.size()));
return sorted[rank - 1];
}
static void summarize(const char* name, Samples& s) {
std::sort(s.latency.begin(), s.latency.end());
std::cout << " \"" << name << "\": {\"samples\":" << s.latency.size()
<< ",\"emitted_per_trial\":" << s.emitted / trials
<< ",\"p50_ns\":" << percentile(s.latency, 0.5)
<< ",\"p99_ns\":" << percentile(s.latency, 0.99)
<< ",\"p999_ns\":" << percentile(s.latency, 0.999) << "}";
}
int main() {
std::vector<U64> floor;
for (unsigned i = 0; i < 100000; ++i) {
auto a = Clock::now();
std::atomic_signal_fence(std::memory_order_seq_cst);
auto b = Clock::now();
floor.push_back(ns(a, b));
}
std::sort(floor.begin(), floor.end());
std::cout << "{\n \"capacity\":" << capacity << ", \"readers\":" << readers
<< ", \"warmup_writes\":" << warmup << ", \"measured_writes\":" << measured_writes
<< ", \"trials\":" << trials << ", \"empty_clock_pair_median_ns\":" << percentile(floor, 0.5)
<< ",\n \"method\":\"Single-threaded synthetic closed-loop wall time per poll, including consuming callback. "
"Nearest-rank percentiles pooled across trials. Timers are unsubtracted. Requests execute serially. "
"Excludes queue wait, networking and producer time.\",\n \"scenarios\":{\n";
for (bool uniform : {false, true}) {
Trace trace(uniform);
std::vector<Output> oracle;
oracle.reserve(trace.measured_polls);
Samples index, log;
index.latency.reserve(trials * trace.measured_polls);
log.latency.reserve(trials * trace.measured_polls);
for (unsigned trial = 0; trial < trials; ++trial) {
if (trial % 2 == 0) {
run<CursorLog>(trace, log, oracle, trial == 0);
run<afterkey::Index<U64>>(trace, index, oracle, false);
} else {
run<afterkey::Index<U64>>(trace, index, oracle, false);
run<CursorLog>(trace, log, oracle, false);
}
}
std::cout << " \"" << (uniform ? "uniform" : "moving-hot-set") << "\":{\n";
summarize("afterkey", index);
std::cout << ",\n";
summarize("cursor-log", log);
std::cout << "\n }" << (uniform ? "\n" : ",\n");
}
std::cout << " }\n}\n";
}
|