18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 05:44:59 +01:00

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.
This commit is contained in:
maj00r
2026-11-15 23:38:24 +01:00
parent 5d0bf6ed00
commit 043b9fb2f0
5 changed files with 55 additions and 26 deletions

View File

@@ -11,7 +11,10 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/physicslog.h"
#include "utilities/Logs.h"
#include <algorithm>
#include <fstream>
#include <future>
#include <memory>
#include <system_error>
#include <utility>
@@ -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<std::promise<void>>()};
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<bool>(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);
}
}