mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 22:44:59 +01:00
fix(physicslog): a log whose file can't be written keeps its lines and header, and the writer is stopped once on every way out
A failed write put its lines back, ahead of those since, so a file held by another program for a while gets its header and every line once it can be written; up to 4 MB kept per log. Stopping is joined once, and not from the writer's own thread. main stops the logs after an error it caught too, and the test watchdog flushes with a time limit, so a disk that doesn't answer doesn't keep the run from ending.
This commit is contained in:
3
EU07.cpp
3
EU07.cpp
@@ -242,6 +242,9 @@ int main(int argc, char *argv[])
|
|||||||
MessageBoxA(nullptr, msg.c_str(), "Simulator crashed :(", MB_ICONERROR);
|
MessageBoxA(nullptr, msg.c_str(), "Simulator crashed :(", MB_ICONERROR);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
// the last two seconds of the physics logs, also after an error caught above; Application.exit() stops them
|
||||||
|
// otherwise, and a second stop only writes what came since
|
||||||
|
PhysicsLogs.stop();
|
||||||
}
|
}
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|||||||
@@ -262,8 +262,9 @@ void test_manager::check_wall_clock(double const Elapsed)
|
|||||||
// but which doesn't close ends with its result
|
// 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)))
|
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));
|
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
|
// what was logged last gets written, unless the log is held up by a hung thread, or the physics logs by a disk
|
||||||
PhysicsLogs.flush();
|
// which doesn't answer: the run ends anyway
|
||||||
|
PhysicsLogs.flush_for(std::chrono::seconds{2});
|
||||||
StopLogService();
|
StopLogService();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
std::_Exit(exit_code());
|
std::_Exit(exit_code());
|
||||||
|
|||||||
@@ -83,7 +83,8 @@ TEST_CASE("physicslog: no file is kept open between the writings, and a log not
|
|||||||
|
|
||||||
logs.begin("SM42-001", header);
|
logs.begin("SM42-001", header);
|
||||||
logs.flush();
|
logs.flush();
|
||||||
// Windows doesn't let a file open in a program be removed
|
// Windows doesn't let a file open in a program be removed. Linux does, so there it checks nothing; the log is
|
||||||
|
// written again below anyway
|
||||||
std::error_code error;
|
std::error_code error;
|
||||||
CHECK(std::filesystem::remove(folder.path() / "SM42-001.dat", error));
|
CHECK(std::filesystem::remove(folder.path() / "SM42-001.dat", error));
|
||||||
CHECK_FALSE(error);
|
CHECK_FALSE(error);
|
||||||
@@ -120,6 +121,63 @@ TEST_CASE("physicslog: stopping writes what is kept at once, and what comes afte
|
|||||||
CHECK(folder.read("EN57-1001") == std::string{header} + "0.0500 0.0000\r\n0.1000 0.0000\r\n");
|
CHECK(folder.read("EN57-1001") == std::string{header} + "0.0500 0.0000\r\n0.1000 0.0000\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("physicslog: the lines of a log whose file can't be written are kept, with its header, for the next try") {
|
||||||
|
log_folder folder{"failed"};
|
||||||
|
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
|
||||||
|
|
||||||
|
// a folder in the way of the file: it can't be opened, as one held by another program
|
||||||
|
std::filesystem::create_directories(folder.path() / "EU07-005.dat");
|
||||||
|
logs.begin("EU07-005", header);
|
||||||
|
logs.append("EU07-005", "0.0500 0.0000\r\n");
|
||||||
|
logs.flush();
|
||||||
|
logs.append("EU07-005", "0.1000 0.5000\r\n");
|
||||||
|
logs.flush();
|
||||||
|
CHECK(std::filesystem::is_directory(folder.path() / "EU07-005.dat"));
|
||||||
|
|
||||||
|
std::filesystem::remove(folder.path() / "EU07-005.dat");
|
||||||
|
logs.append("EU07-005", "0.1500 1.0000\r\n");
|
||||||
|
logs.flush();
|
||||||
|
CHECK(folder.read("EU07-005") == std::string{header} + "0.0500 0.0000\r\n0.1000 0.5000\r\n0.1500 1.0000\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("physicslog: a log begun anew while its file can't be written drops what was kept for it") {
|
||||||
|
log_folder folder{"failedbegun"};
|
||||||
|
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
|
||||||
|
|
||||||
|
std::filesystem::create_directories(folder.path() / "SU45-001.dat");
|
||||||
|
logs.begin("SU45-001", header);
|
||||||
|
logs.append("SU45-001", "0.0500 0.0000\r\n");
|
||||||
|
logs.flush();
|
||||||
|
std::filesystem::remove(folder.path() / "SU45-001.dat");
|
||||||
|
logs.begin("SU45-001", header);
|
||||||
|
logs.append("SU45-001", "0.0500 2.0000\r\n");
|
||||||
|
logs.flush();
|
||||||
|
CHECK(folder.read("SU45-001") == std::string{header} + "0.0500 2.0000\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("physicslog: stopping twice, from two threads at once, writes once and ends") {
|
||||||
|
log_folder folder{"stoptwice"};
|
||||||
|
physicslog_writer logs{folder.path(), std::chrono::milliseconds{20}};
|
||||||
|
|
||||||
|
logs.begin("EP09-001", header);
|
||||||
|
logs.append("EP09-001", "0.0500 0.0000\r\n");
|
||||||
|
{
|
||||||
|
std::jthread other{[&logs] { logs.stop(); }};
|
||||||
|
logs.stop();
|
||||||
|
}
|
||||||
|
CHECK(folder.read("EP09-001") == std::string{header} + "0.0500 0.0000\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("physicslog: a flush with a time limit writes, as flush does") {
|
||||||
|
log_folder folder{"flushfor"};
|
||||||
|
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
|
||||||
|
|
||||||
|
logs.begin("ET41-001", header);
|
||||||
|
logs.append("ET41-001", "0.0500 0.0000\r\n");
|
||||||
|
CHECK(logs.flush_for(std::chrono::seconds{1}));
|
||||||
|
CHECK(folder.read("ET41-001") == std::string{header} + "0.0500 0.0000\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("physicslog: what is kept is written when the writer goes") {
|
TEST_CASE("physicslog: what is kept is written when the writer goes") {
|
||||||
log_folder folder{"end"};
|
log_folder folder{"end"};
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,6 +17,13 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
|
|
||||||
physicslog_writer PhysicsLogs;
|
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};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
physicslog_writer::physicslog_writer(std::filesystem::path Folder, std::chrono::milliseconds const Interval) : m_folder{std::move(Folder)}, m_interval{Interval} {}
|
physicslog_writer::physicslog_writer(std::filesystem::path Folder, std::chrono::milliseconds const Interval) : m_folder{std::move(Folder)}, m_interval{Interval} {}
|
||||||
|
|
||||||
physicslog_writer::~physicslog_writer() {
|
physicslog_writer::~physicslog_writer() {
|
||||||
@@ -27,7 +34,8 @@ void physicslog_writer::begin(std::string const &Name, std::string_view const He
|
|||||||
std::scoped_lock lock{m_mutex};
|
std::scoped_lock lock{m_mutex};
|
||||||
auto &lines{m_pending[Name]};
|
auto &lines{m_pending[Name]};
|
||||||
lines.fresh = true;
|
lines.fresh = true;
|
||||||
lines.text.assign(Header);
|
lines.header.assign(Header);
|
||||||
|
lines.text.clear();
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,20 +46,36 @@ void physicslog_writer::append(std::string const &Name, std::string_view const L
|
|||||||
}
|
}
|
||||||
|
|
||||||
void physicslog_writer::flush() {
|
void physicslog_writer::flush() {
|
||||||
|
std::scoped_lock files{m_filemutex};
|
||||||
write();
|
write();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
void physicslog_writer::stop() {
|
void physicslog_writer::stop() {
|
||||||
|
std::jthread thread;
|
||||||
{
|
{
|
||||||
std::scoped_lock lock{m_mutex};
|
std::scoped_lock lock{m_mutex};
|
||||||
m_stopped = true;
|
m_stopped = true;
|
||||||
|
// taken by the first to stop, so it's joined once
|
||||||
|
thread = std::move(m_thread);
|
||||||
}
|
}
|
||||||
// started only before m_stopped was set, so no one else touches it now
|
if (thread.joinable()) {
|
||||||
if (m_thread.joinable()) {
|
if (thread.get_id() == std::this_thread::get_id()) {
|
||||||
m_thread.request_stop();
|
// stopped from the thread itself, by the end of a run which failed in it: the files may be held by it
|
||||||
m_thread.join();
|
thread.detach();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
thread.request_stop();
|
||||||
|
thread.join();
|
||||||
}
|
}
|
||||||
write();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void physicslog_writer::start() {
|
void physicslog_writer::start() {
|
||||||
@@ -68,12 +92,11 @@ void physicslog_writer::run(std::stop_token const &Stop) {
|
|||||||
if (m_wake.wait_for(lock, Stop, m_interval, [] { return false; }) || Stop.stop_requested())
|
if (m_wake.wait_for(lock, Stop, m_interval, [] { return false; }) || Stop.stop_requested())
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
write();
|
flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void physicslog_writer::write() {
|
void physicslog_writer::write() {
|
||||||
std::scoped_lock files{m_filemutex};
|
|
||||||
std::map<std::string, pending> taken;
|
std::map<std::string, pending> taken;
|
||||||
{
|
{
|
||||||
std::scoped_lock lock{m_mutex};
|
std::scoped_lock lock{m_mutex};
|
||||||
@@ -83,16 +106,42 @@ void physicslog_writer::write() {
|
|||||||
return;
|
return;
|
||||||
std::error_code error;
|
std::error_code error;
|
||||||
std::filesystem::create_directories(m_folder, error);
|
std::filesystem::create_directories(m_folder, error);
|
||||||
for (auto const &[name, lines] : taken) {
|
std::map<std::string, pending> failed;
|
||||||
|
for (auto &[name, lines] : taken) {
|
||||||
if (lines.text.empty() && false == lines.fresh)
|
if (lines.text.empty() && false == lines.fresh)
|
||||||
continue;
|
continue;
|
||||||
auto const path{m_folder / (name + ".dat")};
|
auto const path{m_folder / (name + ".dat")};
|
||||||
// in text mode, as the logs were always written: on Windows their lines, which end with "\r\n", end with "\r\r\n"
|
// 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};
|
std::ofstream file{path, lines.fresh ? std::ios::out | std::ios::trunc : std::ios::out | std::ios::app};
|
||||||
if (file.is_open())
|
if (file.is_open()) {
|
||||||
|
if (lines.fresh)
|
||||||
|
file << lines.header;
|
||||||
file << lines.text;
|
file << lines.text;
|
||||||
|
}
|
||||||
file.close();
|
file.close();
|
||||||
if (file.fail() && m_failed.insert(name).second)
|
if (false == file.fail()) {
|
||||||
ErrorLog("Bad physics log: can't write " + path.string());
|
if (m_failed.erase(name) > 0)
|
||||||
|
WriteLog("Physics log: " + path.string() + " written again");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (m_failed.insert(name).second)
|
||||||
|
ErrorLog("Bad physics log: can't write " + path.string() + ", its lines are kept for the next try");
|
||||||
|
failed.emplace(name, std::move(lines));
|
||||||
|
}
|
||||||
|
if (failed.empty())
|
||||||
|
return;
|
||||||
|
// 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};
|
||||||
|
for (auto &[name, lines] : failed) {
|
||||||
|
auto &now{m_pending[name]};
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ 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
|
// 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
|
// 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 them
|
// 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
|
||||||
class physicslog_writer {
|
class physicslog_writer {
|
||||||
public:
|
public:
|
||||||
explicit physicslog_writer(std::filesystem::path Folder = "physicslog", std::chrono::milliseconds Interval = std::chrono::seconds{2});
|
explicit physicslog_writer(std::filesystem::path Folder = "physicslog", std::chrono::milliseconds Interval = std::chrono::seconds{2});
|
||||||
@@ -38,18 +39,22 @@ public:
|
|||||||
void append(std::string const &Name, std::string_view Line);
|
void append(std::string const &Name, std::string_view Line);
|
||||||
// writes what is kept, now
|
// writes what is kept, now
|
||||||
void flush();
|
void flush();
|
||||||
|
// as flush, unless the files are being written for longer than Timeout: then false, and nothing done
|
||||||
|
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
|
// writes what is kept and ends the thread. what is given after is written by a flush, or at the end
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct pending {
|
struct pending {
|
||||||
bool fresh{false}; // its file is to be emptied first
|
bool fresh{false}; // its file is to be emptied first, and the header written into it
|
||||||
|
std::string header;
|
||||||
std::string text;
|
std::string text;
|
||||||
};
|
};
|
||||||
|
|
||||||
// the thread, on the first line given, unless stopped. called with m_mutex held
|
// the thread, on the first line given, unless stopped. called with m_mutex held
|
||||||
void start();
|
void start();
|
||||||
void run(std::stop_token const &Stop);
|
void run(std::stop_token const &Stop);
|
||||||
|
// called with m_filemutex held
|
||||||
void write();
|
void write();
|
||||||
|
|
||||||
std::filesystem::path const m_folder;
|
std::filesystem::path const m_folder;
|
||||||
@@ -59,7 +64,7 @@ private:
|
|||||||
bool m_stopped{false};
|
bool m_stopped{false};
|
||||||
std::condition_variable_any m_wake;
|
std::condition_variable_any m_wake;
|
||||||
std::jthread m_thread;
|
std::jthread m_thread;
|
||||||
std::mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order
|
std::timed_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
|
std::set<std::string> m_failed; // the logs whose file couldn't be written, told once
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user