Spaces:
Running
Running
File size: 6,051 Bytes
ef8334d | 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 | #include "sincekit_batch.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <vector>
namespace {
struct Event {
std::uint64_t ms;
std::uint32_t id;
};
struct Trace {
std::vector<Event> events;
std::size_t slots = 0;
};
Trace load(const char* path) {
std::ifstream in(path);
if (!in) throw std::runtime_error("cannot open trace");
Trace trace;
Event event{};
while (in >> event.ms) {
if (!(in >> event.id) || (!trace.events.empty() && event.ms < trace.events.back().ms)) {
throw std::runtime_error("invalid event or time order");
}
trace.events.push_back(event);
trace.slots = std::max(trace.slots, std::size_t{event.id} + 1);
}
if (!in.eof() || trace.events.empty()) throw std::runtime_error("invalid trace");
return trace;
}
// Rebuild each delta independently from the recorded write history. The first
// encounter in a reverse scan supplies the latest revision of that session.
class HistoryOracle {
public:
explicit HistoryOracle(const Trace& trace) : trace_(trace), seen_(trace.slots) {}
template<class Visit>
std::size_t visit(std::size_t cursor, std::size_t end, Visit&& visit_row) {
++tag_;
std::size_t count = 0;
for (auto rev = end; rev > cursor; --rev) {
const auto id = trace_.events[rev - 1].id;
if (seen_[id] == tag_) continue;
seen_[id] = tag_;
visit_row(count++, id, rev);
}
return count;
}
private:
const Trace& trace_;
std::vector<std::uint64_t> seen_;
std::uint64_t tag_ = 0;
};
template<class Query>
void play(const Trace& trace, std::uint64_t period_ms, Query&& query) {
sincekit::Index index(trace.slots);
std::size_t read = 0;
for (std::uint64_t tick = 1; read < trace.events.size(); ++tick) {
const auto deadline = tick * period_ms;
while (read < trace.events.size() && trace.events[read].ms <= deadline) {
index.touch(trace.events[read++].id);
}
query(index, tick, read, read == trace.events.size());
}
}
void check_row(const sincekit::BatchRow& row, std::uint32_t id, std::size_t rev) {
if (row.id != id || row.revision != rev || row.deleted) {
throw std::runtime_error("delta disagrees with event history");
}
}
void single(const Trace& trace, unsigned seconds) {
HistoryOracle oracle(trace);
std::size_t cursor = 0, polls = 0, idle = 0, emitted = 0;
std::vector<sincekit::BatchRow> rows;
play(trace, 1000ULL * seconds, [&](const auto& index, auto, auto read, bool) {
rows.clear();
const auto next = index.since(cursor, [&](auto id, auto rev, auto deleted) {
rows.push_back({rev, id, deleted});
});
const auto count = oracle.visit(cursor, read, [&](auto i, auto id, auto rev) {
check_row(rows.at(i), id, rev);
});
if (count != rows.size() || next != read) throw std::runtime_error("cursor or count mismatch");
++polls;
if (rows.empty()) ++idle;
emitted += rows.size();
cursor = next;
});
std::cout << " {\"period_s\":" << seconds << ",\"polls\":" << polls
<< ",\"empty_polls\":" << idle << ",\"rows\":" << emitted
<< ",\"intermediate_writes_coalesced\":" << trace.events.size() - emitted << "}";
}
void batch(const Trace& trace) {
constexpr std::size_t readers = 64;
HistoryOracle oracle(trace);
std::vector<std::uint64_t> acknowledged(readers);
std::uint64_t plans = 0, shared_rows = 0, logical_rows = 0, prefix_entries = 0;
play(trace, 60000, [&](const auto& index, auto minute, auto read, bool final_tick) {
std::vector<std::size_t> due;
std::vector<std::uint64_t> cursors;
for (std::size_t reader = 0; reader < readers; ++reader) {
if (final_tick || minute % (reader + 1) == 0) {
due.push_back(reader);
cursors.push_back(acknowledged[reader]);
}
}
const auto plan = sincekit::since_many(index, cursors);
if (plan.revision != read || plan.prefix_lengths.size() != due.size()) {
throw std::runtime_error("incorrect batch revision or reader count");
}
std::size_t longest = 0;
for (std::size_t i = 0; i < due.size(); ++i) {
const auto count = oracle.visit(cursors[i], read, [&](auto j, auto id, auto rev) {
check_row(plan.rows.at(j), id, rev);
});
if (count != plan.prefix_lengths[i]) throw std::runtime_error("incorrect prefix length");
acknowledged[due[i]] = read;
longest = std::max(longest, count);
logical_rows += count;
}
if (longest != plan.rows.size()) throw std::runtime_error("unexpected batch tail");
++plans;
prefix_entries += due.size();
shared_rows += plan.rows.size();
});
std::cout << " \"simulated_fanout\": {\"readers\":" << readers
<< ",\"plans\":" << plans << ",\"prefix_lengths\":" << prefix_entries
<< ",\"independent_rows\":" << logical_rows << ",\"shared_rows\":" << shared_rows << "}\n";
}
} // namespace
int main(int argc, char** argv) {
try {
if (argc != 2) throw std::runtime_error("usage: replay <activity.tsv>");
const auto trace = load(argv[1]);
std::cout << "{\n \"writes\":" << trace.events.size() << ",\"sessions\":" << trace.slots
<< ",\"span_ms\":" << trace.events.back().ms << ",\n \"simulated_single_reader\":[\n";
for (const auto seconds : {1U, 10U, 60U, 300U, 3600U}) {
single(trace, seconds);
std::cout << (seconds == 3600 ? "\n" : ",\n");
}
std::cout << " ],\n";
batch(trace);
std::cout << "}\n";
} catch (const std::exception& ex) {
std::cerr << ex.what() << '\n';
return 1;
}
}
|