From 043b9fb2f0143cd2d0ccc6048f02e06423d8d3fb Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 23:38:24 +0100 Subject: [PATCH] fix(physicslog): failed logs kept within 64 MB in all, cut outside the lock, never written twice; the writer knows itself when stopped The lines kept while files can't be written are cut to their share of 64 MB before the lock the simulation waits on. A log written in part is cut back to its length, so the next try doesn't write its start twice. A stop from the writer itself is told by its id even after another thread took it to join, and the watchdog's flush waits at most its time, the writing on a thread of its own. --- blackbox_doc.md | 2 +- simulation/blackboxrecorder.cpp | 5 ++- simulation/simulationtest.cpp | 4 +-- utilities/physicslog.cpp | 62 ++++++++++++++++++++++++--------- utilities/physicslog.h | 8 +++-- 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/blackbox_doc.md b/blackbox_doc.md index 33757788a..704d1e954 100644 --- a/blackbox_doc.md +++ b/blackbox_doc.md @@ -23,7 +23,7 @@ the driver of its train. So the speed recorder's tape can be read from them as b simulator runs, without a log of every vehicle. Like those of `physicslog yes`, they are written every two seconds, each file opened only for the moment it is written to; a crash loses the last two seconds of real time of them (at a higher time speed, more of the simulated time). A file another program holds -for a while doesn't lose its lines: they are written once it can be. +for a while doesn't lose its lines: they are written once it can be, the last 64 MB of all such logs at most. The recording starts once the scenery is loaded and ends when the simulator closes. Going into the editor (F11) ends it too, since the editor may take away the tracks and the vehicles it refers to; no diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index d53180744..28e453926 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -421,9 +421,8 @@ void blackbox_recorder::write_cab() { // of the wheels as they turn, which the speedometer shows: apart from that of the vehicle when they slip or skid. // those of the powered vehicle, as the speedometer has it (TTrain::DynamicSet), which in a unit driven from an // unpowered car is another one, maybe facing the other way: signed as the occupied one's speed - auto *powered{train->Dynamic()->FindPowered()}; - if (powered == nullptr) - powered = train->Dynamic(); + // the vehicle itself, if none in the unit is powered + auto const *powered{train->Dynamic()->FindPowered()}; auto const facing{powered->DirectionGet() * vehicle->DirectionGet()}; sample.wheel_speed_kmh = static_cast(facing * M_PI * powered->MoverParameters->WheelDiameter * powered->MoverParameters->nrot * 3.6); sample.acceleration = static_cast(controlling->AccS); diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 11aca23c4..d32fd0e72 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -262,8 +262,8 @@ void test_manager::check_wall_clock(double const Elapsed) // but which doesn't close ends with its result if (false == conclude(test_result::timeout, std::format("still running after {:.0f} s of real time (test.wall_timeout)", limit))) ErrorLog(std::format("test: the simulator was still running {:.0f} s of real time after the start, ended", limit)); - // what was logged last gets written, unless the log is held up by a hung thread, or the physics logs by a disk - // which doesn't answer: the run ends anyway + // what was logged last gets written, unless the log is held up by a hung thread, or the physics logs by a writer + // holding their files or a disk which doesn't answer: the run ends anyway, two seconds later at most PhysicsLogs.flush_for(std::chrono::seconds{2}); StopLogService(); std::this_thread::sleep_for(std::chrono::milliseconds(250)); diff --git a/utilities/physicslog.cpp b/utilities/physicslog.cpp index 8a9ec2434..299b74221 100644 --- a/utilities/physicslog.cpp +++ b/utilities/physicslog.cpp @@ -11,7 +11,10 @@ http://mozilla.org/MPL/2.0/. #include "utilities/physicslog.h" #include "utilities/Logs.h" +#include #include +#include +#include #include #include @@ -19,8 +22,18 @@ physicslog_writer PhysicsLogs; namespace { -// the most of a log kept while its file can't be written; the oldest lines go beyond it -constexpr std::size_t keptwhilefailing{4 * 1024 * 1024}; +// the most of all the logs kept while their files can't be written, shared by those, a log never less than the +// least: the oldest lines go beyond it +constexpr std::size_t keptwhilefailing{64 * 1024 * 1024}; +constexpr std::size_t keptwhilefailingleast{64 * 1024}; + +// the text cut to its last lines within Most +void keep_last(std::string &Text, std::size_t const Most) { + if (Text.size() <= Most) + return; + auto const cut{Text.find('\n', Text.size() - Most)}; + Text.erase(0, cut == std::string::npos ? Text.size() : cut + 1); +} } // namespace @@ -51,11 +64,15 @@ void physicslog_writer::flush() { } bool physicslog_writer::flush_for(std::chrono::milliseconds const Timeout) { - std::unique_lock files{m_filemutex, Timeout}; - if (false == files.owns_lock()) - return false; - write(); - return true; + // on a thread of its own, left behind if it doesn't end in time: a disk which doesn't answer holds it up in the + // writing, not only the lock. for the end of the process, which doesn't wait for it + auto done{std::make_shared>()}; + auto ended{done->get_future()}; + std::thread([this, done] { + flush(); + done->set_value(); + }).detach(); + return ended.wait_for(Timeout) == std::future_status::ready; } void physicslog_writer::stop() { @@ -63,15 +80,19 @@ void physicslog_writer::stop() { { std::scoped_lock lock{m_mutex}; m_stopped = true; + // stopped from the writer itself, by the end of a run which failed in it: the files may be held by it, and + // another thread may be joining it already + if (m_writer == std::this_thread::get_id()) { + if (m_thread.joinable()) { + m_thread.request_stop(); + m_thread.detach(); + } + return; + } // taken by the first to stop, so it's joined once thread = std::move(m_thread); } if (thread.joinable()) { - if (thread.get_id() == std::this_thread::get_id()) { - // stopped from the thread itself, by the end of a run which failed in it: the files may be held by it - thread.detach(); - return; - } thread.request_stop(); thread.join(); } @@ -82,6 +103,7 @@ void physicslog_writer::start() { if (m_stopped || m_thread.joinable()) return; m_thread = std::jthread([this](std::stop_token const &Stop) { run(Stop); }); + m_writer = m_thread.get_id(); } void physicslog_writer::run(std::stop_token const &Stop) { @@ -111,6 +133,10 @@ void physicslog_writer::write() { if (lines.text.empty() && false == lines.fresh) continue; auto const path{m_folder / (name + ".dat")}; + // how long it was, to be cut back to if the lines are written in part only, a disk filling up say: else + // they'd be written twice, the next time + auto const before{lines.fresh ? 0 : std::filesystem::file_size(path, error)}; + auto const existed{false == lines.fresh && false == static_cast(error)}; // in text mode, as the logs were always written: on Windows their lines, which end with "\r\n", end with "\r\r\n" std::ofstream file{path, lines.fresh ? std::ios::out | std::ios::trunc : std::ios::out | std::ios::app}; if (file.is_open()) { @@ -124,12 +150,18 @@ void physicslog_writer::write() { WriteLog("Physics log: " + path.string() + " written again"); continue; } + if (existed) + std::filesystem::resize_file(path, before, error); if (m_failed.insert(name).second) - ErrorLog("Bad physics log: can't write " + path.string() + ", its lines are kept for the next try"); + ErrorLog("Bad physics log: can't write " + path.string() + ", its last lines are kept for the next try"); failed.emplace(name, std::move(lines)); } if (failed.empty()) return; + // cut before the lock, which the simulation waits on to give its lines: only what came since is added under it + auto const most{std::max(keptwhilefailingleast, keptwhilefailing / failed.size())}; + for (auto &[name, lines] : failed) + keep_last(lines.text, most); // tried again the next time, before what came since; a file held by another program for a while doesn't lose // them, nor its header. a log begun anew since drops them std::scoped_lock lock{m_mutex}; @@ -138,10 +170,6 @@ void physicslog_writer::write() { if (now.fresh) continue; lines.text.append(now.text); - if (lines.text.size() > keptwhilefailing) { - auto const cut{lines.text.find('\n', lines.text.size() - keptwhilefailing)}; - lines.text.erase(0, cut == std::string::npos ? lines.text.size() : cut + 1); - } now = std::move(lines); } } diff --git a/utilities/physicslog.h b/utilities/physicslog.h index 024ae8fba..44d04b4c3 100644 --- a/utilities/physicslog.h +++ b/utilities/physicslog.h @@ -23,7 +23,7 @@ http://mozilla.org/MPL/2.0/. // the physics logs, /.dat. the lines given are kept, and written every two seconds by a thread of its // own, each file opened only for the moment it is written to: the logs of hundreds of vehicles hold no files open, and // the simulation doesn't wait for the disk. a crash loses the last two seconds of real time of them. lines whose file -// can't be written, held by another program say, are kept for the next try +// can't be written, held by another program say, are kept for the next try: the last 64 MB of them all at most class physicslog_writer { public: explicit physicslog_writer(std::filesystem::path Folder = "physicslog", std::chrono::milliseconds Interval = std::chrono::seconds{2}); @@ -39,7 +39,8 @@ public: void append(std::string const &Name, std::string_view Line); // writes what is kept, now void flush(); - // as flush, unless the files are being written for longer than Timeout: then false, and nothing done + // as flush, waited for no longer than Timeout: then false, and the writing goes on by itself, on a thread left + // behind. for the end of the process, the files held up or the disk not answering bool flush_for(std::chrono::milliseconds Timeout); // writes what is kept and ends the thread. what is given after is written by a flush, or at the end void stop(); @@ -64,7 +65,8 @@ private: bool m_stopped{false}; std::condition_variable_any m_wake; std::jthread m_thread; - std::timed_mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order + std::thread::id m_writer; // of the thread, kept after it is taken to be joined: the writer knows itself by it + std::mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order std::set m_failed; // the logs whose file couldn't be written, told once };