Spaces:
Running
Running
Download trace/time_plans.cpp from pgg-engineering/sincekit: direct link, hf CLI and curl.
- Browser
- Download file 7.87 kB
-
https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/trace/time_plans.cpp
- Command line
-
hf download hf://spaces/pgg-engineering/sincekit/trace/time_plans.cpp
-
curl -L -o time_plans.cpp https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/trace/time_plans.cpp
7.87 kB
| // Run from the repository root on Linux: | |
| // g++ -O3 -DNDEBUG -std=c++17 -Iinclude trace/time_plans.cpp -o /tmp/time_plans | |
| // taskset -c 1 /tmp/time_plans trace/activity.tsv > trace/timing.csv | |
| // Each sample times 8 full write + plan + destruction replays. Capture steady | |
| // wall time and process CPU time to show descheduling on a shared host. | |
| // Input parsing, deterministic reader scheduling, full checksums and I/O are | |
| // outside timing. No payload reads, socket writes, receiver work or disk I/O. | |
| namespace { | |
| using sincekit::BatchPlan; | |
| using sincekit::BatchRow; | |
| using sincekit::Index; | |
| using Cursors = std::vector<std::uint64_t>; | |
| using MakePlan = BatchPlan (*)(const Index&, const Cursors&); | |
| struct Event { std::uint64_t ms; std::uint32_t id; }; | |
| struct Round { std::size_t end; Cursors cursors; }; | |
| std::vector<Event> load(const char* path) { | |
| std::ifstream in(path); | |
| if (!in) throw std::runtime_error("cannot open activity.tsv"); | |
| std::vector<Event> events; | |
| Event event{}; | |
| while (in >> event.ms) { | |
| if (!(in >> event.id) || (!events.empty() && event.ms < events.back().ms)) { | |
| throw std::runtime_error("invalid trace"); | |
| } | |
| events.push_back(event); | |
| } | |
| if (!in.eof() || events.empty()) throw std::runtime_error("invalid trace"); | |
| return events; | |
| } | |
| // Reader i acknowledges every i+1 minutes; all acknowledge at the final tick. | |
| // Checkpoints depend only on the given event timings and reader schedule. | |
| std::vector<Round> schedule(const std::vector<Event>& events, std::size_t readers) { | |
| Cursors acks(readers); | |
| std::vector<Round> rounds; | |
| std::size_t end = 0; | |
| for (std::uint64_t tick = 1; end != events.size(); ++tick) { | |
| while (end < events.size() && events[end].ms <= tick * 60000) ++end; | |
| rounds.push_back({end, {}}); | |
| for (std::size_t i = 0; i < readers; ++i) { | |
| if (end == events.size() || tick % (i + 1) == 0) { | |
| rounds.back().cursors.push_back(acks[i]); | |
| acks[i] = end; | |
| } | |
| } | |
| } | |
| return rounds; | |
| } | |
| // Comparable owned plan, capacity reservation, cursor validation and equal- | |
| // cursor shortcut. The sweep also exploits naturally ordered reader cursors. | |
| template<bool TrySweep> | |
| BatchPlan reference(const Index& index, const Cursors& cursors) { | |
| BatchPlan plan; | |
| plan.revision = index.revision(); | |
| if (cursors.empty()) return plan; | |
| const auto bounds = std::minmax_element(cursors.begin(), cursors.end()); | |
| if (*bounds.second > plan.revision) throw std::out_of_range("future cursor"); | |
| plan.rows.reserve(sincekit::batch_detail::initial_rows(index, *bounds.first, cursors.size())); | |
| index.since(*bounds.first, [&](auto id, auto rev, auto deleted) { | |
| plan.rows.push_back({rev, id, deleted}); | |
| }); | |
| if (*bounds.first == *bounds.second || plan.rows.empty()) { | |
| plan.prefix_lengths.assign(cursors.size(), plan.rows.size()); | |
| return plan; | |
| } | |
| plan.prefix_lengths.resize(cursors.size()); | |
| if (TrySweep && std::is_sorted(cursors.begin(), cursors.end(), std::greater<>{})) { | |
| std::size_t rank = 0; | |
| for (std::size_t i = 0; i < cursors.size(); ++i) { | |
| while (rank < plan.rows.size() && plan.rows[rank].revision > cursors[i]) ++rank; | |
| plan.prefix_lengths[i] = rank; | |
| } | |
| return plan; | |
| } | |
| for (std::size_t i = 0; i < cursors.size(); ++i) { | |
| auto it = std::lower_bound(plan.rows.begin(), plan.rows.end(), cursors[i], | |
| [](const BatchRow& row, auto cursor) { return row.revision > cursor; }); | |
| plan.prefix_lengths[i] = static_cast<std::size_t>(it - plan.rows.begin()); | |
| } | |
| return plan; | |
| } | |
| inline void consume(const BatchPlan& plan) { | |
| asm volatile("" : : "g"(&plan), "g"(plan.rows.data()), "g"(plan.prefix_lengths.data()) : "memory"); | |
| static const void* volatile observed[3]; | |
| observed[0] = &plan; | |
| observed[1] = plan.rows.data(); | |
| observed[2] = plan.prefix_lengths.data(); | |
| std::atomic_signal_fence(std::memory_order_seq_cst); | |
| } | |
| template<bool Verify> | |
| std::uint64_t replay(const std::vector<Event>& events, std::size_t slots, | |
| const std::vector<Round>& rounds, MakePlan make_plan) { | |
| Index index(slots); | |
| std::size_t written = 0; | |
| std::uint64_t hash = 14695981039346656037ULL; | |
| const auto mix = [&](std::uint64_t v) { hash = (hash ^ v) * 1099511628211ULL; }; | |
| for (const auto& r : rounds) { | |
| while (written != r.end) index.touch(events[written++].id); | |
| const auto plan = make_plan(index, r.cursors); | |
| if constexpr (Verify) { | |
| mix(plan.revision); mix(plan.rows.size()); mix(plan.prefix_lengths.size()); | |
| for (const auto& row : plan.rows) { | |
| mix(row.id); mix(row.revision); mix(row.deleted); | |
| } | |
| for (const auto length : plan.prefix_lengths) mix(length); | |
| } else { | |
| consume(plan); | |
| } | |
| } | |
| return hash; | |
| } | |
| } // namespace | |
| int main(int argc, char** argv) { | |
| try { | |
| if (argc != 2) throw std::runtime_error("usage: time_plans activity.tsv"); | |
| const auto events = load(argv[1]); | |
| std::size_t slots = 0; | |
| for (const auto& e : events) slots = std::max(slots, std::size_t(e.id) + 1); | |
| const std::array<MakePlan, 3> methods{ | |
| sincekit::since_many, reference<false>, reference<true>}; | |
| const std::array<const char*, 3> names{"since_many", "lower_bound", "ordered_sweep"}; | |
| constexpr int repeats = 8; | |
| std::cout << "readers,plans,trial,position,method,replays,elapsed_ns,cpu_ns,checksum\n"; | |
| for (const unsigned readers : {64U, 2048U}) { | |
| const auto rounds = schedule(events, readers); | |
| const auto expected = replay<true>(events, slots, rounds, methods[1]); | |
| for (const auto method : methods) { | |
| if (replay<true>(events, slots, rounds, method) != expected) { | |
| throw std::runtime_error("plan checksum differs"); | |
| } | |
| } | |
| for (unsigned trial = 0; trial <= 12; ++trial) { | |
| for (unsigned pos = 0; pos < methods.size(); ++pos) { | |
| // Rotate all three through each position; reverse on alternate blocks. | |
| const auto j = (trial + (trial / 3 % 2 ? 2 - pos : pos)) % methods.size(); | |
| const auto start = std::chrono::steady_clock::now(); | |
| const auto cpu_start = std::clock(); | |
| for (int i = 0; i < repeats; ++i) replay<false>(events, slots, rounds, methods[j]); | |
| const auto cpu_end = std::clock(); | |
| const auto elapsed = std::chrono::steady_clock::now() - start; | |
| const auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count(); | |
| if (cpu_start == -1 || cpu_end == -1) throw std::runtime_error("CPU clock unavailable"); | |
| const auto cpu_ns = static_cast<std::uint64_t>( | |
| (cpu_end - cpu_start) * (1.0e9 / CLOCKS_PER_SEC)); | |
| if (trial != 0) { | |
| std::cout << readers << ',' << rounds.size() << ',' << trial << ',' << pos << ',' | |
| << names[j] << ',' << repeats << ',' << ns << ',' << cpu_ns << ',' | |
| << expected << '\n'; | |
| } | |
| } | |
| } | |
| } | |
| } catch (const std::exception& e) { | |
| std::cerr << e.what() << '\n'; | |
| return 1; | |
| } | |
| } | |