mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 16:14:58 +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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
// the physics logs, <folder>/<name>.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<std::string> m_failed; // the logs whose file couldn't be written, told once
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user