Spaces:
Running
Running
File size: 7,870 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | // 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.
#include "sincekit_batch.h"
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
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) {
#if defined(__GNUC__) || defined(__clang__)
asm volatile("" : : "g"(&plan), "g"(plan.rows.data()), "g"(plan.prefix_lengths.data()) : "memory");
#else
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);
#endif
}
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;
}
}
|