Bicameral Engine
GPU-resident engine for a magic-research open-world game
Loading...
Searching...
No Matches
trace_capture.cpp
Go to the documentation of this file.
1// trace_capture.cpp — 連鎖のトレースを集めてファイルに書く(T-0087・T-0088)。使い方は trace_capture.h。
3
4#include <algorithm>
5#include <format>
6#include <limits>
7#include <system_error>
8
9#include "core/log.h"
10#include "core/unicode.h"
11#include "sim/probe_trace.h"
12
13namespace bicameral::frame {
14
15 void TraceCapture::Add(std::span<const gpu::GraphTraceRecord> records, uint32_t droppedCount) {
16 m_droppedCount += droppedCount;
17
18 const size_t room = MAX_RECORDS - std::min(m_records.size(), MAX_RECORDS);
19 const size_t taken = std::min(room, records.size());
20 m_records.insert(m_records.end(), records.begin(), records.begin() + static_cast<ptrdiff_t>(taken));
21 m_droppedCount += records.size() - taken;
22 }
23
24 bool TraceCapture::IsComplete(uint64_t latestStateTick) const {
25 if (m_filter.tickEnd == std::numeric_limits<uint64_t>::max())
26 return false;
27
28 return latestStateTick >= m_filter.tickEnd; // S(tickEnd) を読んだ = 刻み tickEnd − 1 まで終わった
29 }
30
31 std::expected<void, std::string> TraceCapture::Write() {
32 const std::string pathText = ToUtf8(m_path.wstring());
33 if (m_path.has_parent_path()) {
34 std::error_code error;
35 fs::create_directories(m_path.parent_path(), error);
36 if (error)
37 return std::unexpected(std::format("トレースのフォルダを作れない: {}", pathText));
38 }
39
40 const size_t recordCount = m_records.size();
41 const auto written = sim::WriteProbeTraceFile(m_path, std::move(m_records), m_filter, m_droppedCount);
42 m_records = {};
43 if (!written)
44 return std::unexpected(std::format("{}: {}", written.error(), pathText));
45
46 Log(Channel::Sim, m_droppedCount > 0 ? Level::Warning : Level::Info, "トレース: {}(記録 {} 件{})", pathText,
47 recordCount,
48 m_droppedCount > 0 ? std::format("、落とした {} 件。範囲を狭めると欠けない", m_droppedCount)
49 : std::string());
50
51 return {};
52 }
53
54} // namespace bicameral::frame
gpu::GraphTraceFilter m_filter
std::vector< gpu::GraphTraceRecord > m_records
void Add(std::span< const gpu::GraphTraceRecord > records, uint32_t droppedCount)
std::expected< void, std::string > Write()
static constexpr size_t MAX_RECORDS
bool IsComplete(uint64_t latestStateTick) const
std::expected< void, std::string > WriteProbeTraceFile(const fs::path &path, std::vector< gpu::GraphTraceRecord > records, const gpu::GraphTraceFilter &filter, uint64_t droppedCount)
void Log(Channel channel, Level level, FormatWithLocation< std::type_identity_t< Args >... > pattern, Args &&... args)
Definition log.h:140
std::string ToUtf8(std::wstring_view text)
Definition unicode.cpp:10