Spaces:
Running
Running
Download trace/replay.cpp from pgg-engineering/sincekit: direct link, hf CLI and curl.
- Browser
- Download file 6.05 kB
-
https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/trace/replay.cpp
- Command line
-
hf download hf://spaces/pgg-engineering/sincekit/trace/replay.cpp
-
curl -L -o replay.cpp https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/trace/replay.cpp
6.05 kB
| 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; | |
| } | |
| } | |