main.cpp
1#include "itch/app/options.hpp"2#include "itch/common/file_utils.hpp"3#include "itch/feed/moldudp/live.hpp"4#include "itch/output/staged_output.hpp"5#include "itch/replay/replay.hpp"67#include <algorithm>8#include <cstddef>9#include <cstdlib>10#include <filesystem>11#include <iostream>12#include <optional>13#include <stdexcept>14#include <string>15#include <sys/resource.h>1617int main(int argc, char** argv) {18 try {19 const itch::Options options = itch::parse_options(argc, argv);20 if (options.live) {21 std::optional<itch::StagedOutput> staged;22 if (!options.output.path.empty()) {23 staged.emplace(options.output.path, "");24 }25 itch::live(options.network, options.book.target, staged ? staged->stream() : std::cout,26 options.book.max_depth, options.book.check_book);27 auto& output = staged ? staged->stream() : std::cout;28 output.flush();29 if (!output) {30 throw std::runtime_error("output write failed");31 }32 if (staged) {33 staged->commit();34 }35 return 0;36 }37 itch::reject_same_file(options.replay.path, options.checkpoint.path);38 itch::reject_same_file(options.replay.path, options.checkpoint.resume_path);39 itch::reject_same_file(options.output.path, options.checkpoint.path);40 itch::reject_same_file(options.output.path, options.checkpoint.resume_path);41 // Replacing the loaded checkpoint is allowed after successful continuation.42 std::optional<itch::Checkpoint> resume;43 if (!options.checkpoint.resume_path.empty()) {44 resume = itch::load_checkpoint(options.checkpoint.resume_path);45 }46 if (resume && !resume->csv_path.empty()) {47 for (const auto& path : {options.replay.path, options.output.path,48 options.checkpoint.path, options.checkpoint.resume_path}) {49 itch::reject_same_file(path, resume->csv_path);50 }51 }52 itch::Checkpoint checkpoint;53 itch::ReplayControl control;54 control.resume = resume ? &*resume : nullptr;55 control.stop_after_frame = options.checkpoint.stop_after_frame;56 control.checkpoint = options.checkpoint.path.empty() && !options.checkpoint.resume_csv57 ? nullptr58 : &checkpoint;59 std::optional<itch::StagedOutput> staged;60 if (!options.output.path.empty()) {61 staged.emplace(options.output.path, options.replay.path);62 }63 std::optional<itch::StagedOutput> journal;64 const bool recoverable_csv =65 !options.output.path.empty() &&66 (options.checkpoint.resume_csv || (!options.checkpoint.path.empty() && !resume));67 if (recoverable_csv) {68 journal.emplace(options.output.path + ".recovery", options.replay.path);69 if (options.checkpoint.resume_csv) {70 itch::copy_checkpoint_csv(*resume, journal->stream());71 }72 control.omit_header = options.checkpoint.resume_csv;73 }74 auto bind_csv = [&](const itch::Checkpoint& state) {75 auto saved = state;76 if (journal) {77 journal->sync();78 const auto position = journal->stream().tellp();79 if (position < 0) {80 throw std::runtime_error("cannot determine CSV prefix length");81 }82 saved.csv_path = std::filesystem::absolute(journal->temporary_path()).string();83 saved.csv_bytes = static_cast<uint64_t>(position);84 }85 return saved;86 };87 auto publish_checkpoint = [&](const itch::Checkpoint& state) {88 itch::save_checkpoint(bind_csv(state), options.checkpoint.path, options.replay.path);89 // Only retain a journal once a successfully published checkpoint references it.90 if (journal) {91 journal->preserve_temporary();92 }93 };94 if (options.checkpoint.every_frames) {95 control.checkpoint_every_frames = options.checkpoint.every_frames;96 control.on_checkpoint = publish_checkpoint;97 }9899 const itch::Result first =100 itch::replay(options.replay.path, options.book.target,101 journal ? &journal->stream()102 : staged ? &staged->stream()103 : &std::cout,104 options.book.max_depth, options.book.check_book,105 options.replay.allow_implicit_eof, options.replay.with_offset, control);106107 if (options.replay.verify) {108 itch::Checkpoint second_checkpoint;109 itch::ReplayControl second_control;110 second_control.resume = control.resume;111 second_control.stop_after_frame = control.stop_after_frame;112 second_control.checkpoint = control.checkpoint ? &second_checkpoint : nullptr;113 const itch::Result second = itch::replay(114 options.replay.path, options.book.target, nullptr, options.book.max_depth,115 options.book.check_book, options.replay.allow_implicit_eof,116 options.replay.with_offset, second_control);117 if (first.hash != second.hash || first.selected != second.selected ||118 first.frames != second.frames) {119 throw std::runtime_error("deterministic replay mismatch");120 }121 if (control.checkpoint &&122 itch::encode_checkpoint(checkpoint) != itch::encode_checkpoint(second_checkpoint)) {123 throw std::runtime_error("deterministic checkpoint mismatch");124 }125 }126127 if (journal) {128 itch::copy_checkpoint_csv(bind_csv(checkpoint), staged->stream());129 }130 std::ostream& output = staged ? staged->stream() : std::cout;131 output.flush();132 if (!output) {133 throw std::runtime_error("output write failed");134 }135 if (!options.checkpoint.path.empty()) {136 publish_checkpoint(checkpoint);137 }138 if (staged) {139 staged->commit();140 }141142 rusage usage{};143 getrusage(RUSAGE_SELF, &usage);144#ifdef __APPLE__145 const uint64_t peak_bytes = static_cast<uint64_t>(usage.ru_maxrss);146#else147 const uint64_t peak_bytes = static_cast<uint64_t>(usage.ru_maxrss) * 1024;148#endif149150 std::cerr << "frames=" << first.frames << " selected=" << first.selected151 << " hash=" << std::hex << first.hash << std::dec152 << " verified=" << (options.replay.verify ? "yes" : "no") << " scope="153 << (resume && !options.checkpoint.resume_csv ? "suffix"154 : first.stopped ? "prefix"155 : "full")156 << " complete=" << (first.stopped ? "no" : "yes") << " emitted=" << first.emitted157 << " elapsed_ms=" << first.elapsed_ns / 1e6 << " frames_per_s="158 << (first.elapsed_ns ? (first.frames - (resume ? resume->next_frame - 1 : 0)) *159 1e9 / first.elapsed_ns160 : 0)161 << " mean_selected_event_ns="162 << (first.emitted ? first.event_ns / first.emitted : 0)163 << " max_selected_event_ns=" << first.max_event_ns164 << " peak_rss_bytes=" << peak_bytes << '\n';165 return 0;166 } catch (const std::exception& error) {167 std::cerr << "error: " << error.what() << '\n';168 return 1;169 }170}