replay.cpp
1#include "itch/replay/replay.hpp"23#include "itch/book/book.hpp"4#include "itch/feed/binaryfile/reader.hpp"5#include "itch/feed/itch/decoder.hpp"6#include "itch/output/csv.hpp"78#include <algorithm>9#include <chrono>10#include <fstream>11#include <ostream>12#include <stdexcept>13#include <string>1415namespace itch {16namespace {1718using Clock = std::chrono::steady_clock;1920} // namespace2122Result replay(const std::string& path, const std::string& target, std::ostream* output,23 size_t max_depth, bool check_book, bool allow_implicit_eof, bool with_offset,24 const ReplayControl& control) {25 const auto begin = Clock::now();26 std::optional<Fingerprint> input;27 if (control.resume || control.checkpoint || control.on_checkpoint) {28 input = fingerprint(path);29 }30 std::ifstream in(path, std::ios::binary);31 if (!in) {32 throw std::runtime_error("cannot open input: " + path);33 }34 Book book(target);35 Result result;36 uint64_t byte_offset = 0;37 bool last_was_end_of_messages = false;38 if (control.resume) {39 const auto& c = *control.resume;40 if (c.target != target) {41 throw std::runtime_error("checkpoint symbol mismatch");42 }43 if (c.depth != max_depth || c.with_offset != with_offset ||44 c.allow_implicit_eof != allow_implicit_eof) {45 throw std::runtime_error(46 "checkpoint replay options mismatch (depth/with-offset/allow-implicit-eof)");47 }48 if (c.input.size != input->size || c.input.digest != input->digest) {49 throw std::runtime_error("checkpoint input SHA-256 mismatch");50 }51 validate_boundary(c, path);52 book.restore(c.book, c.next_frame - 1);53 result.frames = c.next_frame - 1;54 result.selected = c.selected;55 result.hash = c.hash;56 byte_offset = c.offset;57 last_was_end_of_messages = c.last_end_messages;58 in.seekg(static_cast<std::streamoff>(byte_offset));59 if (!in) {60 throw std::runtime_error("checkpoint input seek failed");61 }62 }63 if (control.stop_after_frame && *control.stop_after_frame < result.frames) {64 throw std::runtime_error("--stop-after-frame precedes checkpoint");65 }66 if (output && !control.omit_header) {67 *output << csv_header(with_offset) << '\n';68 }6970 auto checkpoint_state = [&]() {71 return Checkpoint{*input,72 target,73 result.frames + 1,74 byte_offset,75 result.selected,76 result.hash,77 max_depth,78 with_offset,79 allow_implicit_eof,80 last_was_end_of_messages,81 book.state(),82 {},83 0};84 };85 const uint64_t starting_frame = result.frames;8687 uint64_t attempted_offset = byte_offset;88 const uint64_t starting_offset = byte_offset;89 try {90 while (true) {91 if (control.on_checkpoint && control.checkpoint_every_frames &&92 result.frames > starting_frame &&93 result.frames % control.checkpoint_every_frames == 0) {94 control.on_checkpoint(checkpoint_state());95 }96 if (control.stop_after_frame && result.frames == *control.stop_after_frame) {97 result.stopped = true;98 break;99 }100 const uint64_t frame_offset = byte_offset;101 attempted_offset = frame_offset;102 auto payload = read_binary_frame(in, result.frames, byte_offset,103 last_was_end_of_messages, allow_implicit_eof);104 if (!payload) {105 break;106 }107 auto event = decode(*payload, result.frames);108 if (!event) {109 continue;110 }111 const auto event_begin = Clock::now();112 auto snapshot = book.apply(*event, max_depth, check_book);113 if (!snapshot) {114 continue;115 }116 snapshot->byte_offset = frame_offset;117 snapshot->byte_end_offset = byte_offset;118 const std::string line = csv_line(*snapshot, with_offset);119 const uint64_t event_ns =120 std::chrono::duration_cast<std::chrono::nanoseconds>(Clock::now() - event_begin)121 .count();122 ++result.selected;123 ++result.emitted;124 result.event_ns += event_ns;125 result.max_event_ns = std::max(result.max_event_ns, event_ns);126 result.hash = hash_csv_line(result.hash, line);127 if (output) {128 *output << line << '\n';129 }130 }131 } catch (const std::exception& error) {132 throw std::runtime_error(133 std::string(error.what()) +134 "; first untrusted byte_offset=" + std::to_string(attempted_offset) +135 "; replay started at byte_offset=" + std::to_string(starting_offset));136 }137138 if (control.stop_after_frame && !result.stopped) {139 throw std::runtime_error("--stop-after-frame exceeds input frame count");140 }141 if (input) {142 const auto final_input = fingerprint(path);143 if (final_input.size != input->size || final_input.digest != input->digest) {144 throw std::runtime_error("input changed during replay (SHA-256 mismatch)");145 }146 }147 if (control.checkpoint) {148 *control.checkpoint = checkpoint_state();149 }150151 result.elapsed_ns =152 std::chrono::duration_cast<std::chrono::nanoseconds>(Clock::now() - begin).count();153 return result;154}155156} // namespace itch