18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 15:04:58 +01:00

feat(physicslog): the logs written every two seconds by a thread of their own, no file kept open; the black box's tapes as it goes

"physicslog yes" kept the .dat of every vehicle with a driver open for the whole run and flushed it twenty
times a second from the simulation's thread: some 580 open files on Tomaszewo. The lines are kept now and a
thread of their own writes them every two seconds, each file opened only for the moment it is written to.
The files, their lines and their text mode stay as they were.

- The black box, with the physics logs off, writes the tapes of the user's vehicles through the same
  writer as it goes, not at the end. A log begins when the user is first in the vehicle and goes on
  while the user looks around outside, until another vehicle is taken; back in it, the log goes on after
  the gap. The vehicle's own driver keeps the log only if it is the primary one, as a passive one's clock
  stands still; otherwise the driver of its train does.
- What is kept is written when the simulator closes, after the black box, at the end of a test run, and
  when the test watchdog ends a run. A crash loses the last two seconds.
- On Tomaszewo with the black box alone, the one tape grows every two seconds. With "physicslog yes", the
  581 logs grow and the simulator holds as many handles as without them; writing them takes the thread
  about 1.9 s, which the closing waits for once.
This commit is contained in:
maj00r
2026-11-14 00:48:17 +01:00
parent 5ca2f7e38f
commit 03a3b4c09a
12 changed files with 347 additions and 61 deletions

View File

@@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/.
#include "world/Track.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/physicslog.h"
#include "utilities/utilities.h"
#include <algorithm>
@@ -280,7 +281,6 @@ void blackbox_recorder::close() {
m_writer->end(m_time);
m_writer.reset();
m_file.close();
write_tapes();
using milliseconds = std::chrono::duration<double, std::milli>;
WriteLog(std::format("Black box: recording closed; {} looks around, {:.3f} ms each on average, the longest {:.3f} ms", m_looks,
m_looks > 0 ? milliseconds{m_looktime}.count() / m_looks : 0.0, milliseconds{m_looklongest}.count()));
@@ -492,30 +492,16 @@ void blackbox_recorder::keep_tape_line() {
return;
auto const *vehicle{train->Dynamic()};
// the driver of the vehicle, whose log it would be: it keeps the name of its first vehicle, so a change of the cab
// in a unit goes on in the same log. in a vehicle without one, the driver of its train
auto const *driver{vehicle->Mechanik != nullptr ? vehicle->Mechanik : vehicle->ctOwner};
// in a unit goes on in the same log. a passive one stands still (its clock too), so then the driver of the train
auto const *driver{vehicle->Mechanik != nullptr && vehicle->Mechanik->primary() ? vehicle->Mechanik : vehicle->ctOwner};
if (driver == nullptr)
return;
m_tapes[driver->physicslog_name()] += driver->physicslog_line();
}
void blackbox_recorder::write_tapes() {
for (auto const &[name, lines] : m_tapes) {
// opened as the driver opens its log, so the file is the same to the byte
auto const filename{"physicslog/" + name + ".dat"};
std::fstream file{filename, std::ios::in | std::ios::out | std::ios::trunc};
if (false == file.is_open()) {
ErrorLog(std::format("Black box: can't create the physics log \"{}\"", filename));
continue;
}
file << TController::physicslog_header() << lines;
file.close();
if (file.fail())
ErrorLog(std::format("Black box: the physics log \"{}\" wasn't written whole", filename));
else
WriteLog(std::format("Black box: the physics log \"{}\" written from the recording", filename));
auto const &name{driver->physicslog_name()};
if (m_tapes.insert(name).second) {
PhysicsLogs.begin(name, TController::physicslog_header());
WriteLog(std::format("Black box: the physics log \"physicslog/{}.dat\" begun", name));
}
m_tapes.clear();
PhysicsLogs.append(name, driver->physicslog_line());
}
void blackbox_recorder::look_around(bool const Keyframe) {

View File

@@ -15,9 +15,9 @@ http://mozilla.org/MPL/2.0/.
#include <chrono>
#include <cstdint>
#include <fstream>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
@@ -87,10 +87,10 @@ private:
void write_map();
static std::vector<std::pair<glm::dvec3, float>> stop_point_headings();
void write_cab();
// with the physics logs off, the line of the log of the driver the user is with, kept for the end
// with the physics logs off, the line of the log of the driver the user is with, to its log, physicslog/<vehicle>.dat,
// as the physics logs would have it. a log is begun at the first line of the recording, and goes on while the user
// looks around outside, until the user takes another vehicle; back in the vehicle, it goes on after the gap
void keep_tape_line();
// the kept lines, each driver's to its log, physicslog/<vehicle>.dat, as the physics logs would have it
void write_tapes();
// the clock, the user's vehicle, the vehicles, the switches and the signals
void look_around(bool Keyframe);
void look_at_vehicles(bool Keyframe);
@@ -115,8 +115,8 @@ private:
double m_time{0.0};
double m_cabdue{0.0};
double m_tapedue{0.0};
// the lines of the physics log by the name of its file, of the times the user was with its driver
std::map<std::string, std::string> m_tapes;
// the names of the logs begun by the recording
std::set<std::string> m_tapes;
double m_lookdue{0.0};
double m_keyframedue{0.0};
std::unordered_map<TDynamicObject const *, vehicle_entry> m_vehicles;

View File

@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
#include "simulation/simulation.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/physicslog.h"
#include "utilities/utilities.h"
#include "vehicle/DynObj.h"
#include "world/Event.h"
@@ -262,6 +263,7 @@ void test_manager::check_wall_clock(double const Elapsed)
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
PhysicsLogs.flush();
StopLogService();
std::this_thread::sleep_for(std::chrono::milliseconds(250));
std::_Exit(exit_code());