2025-07-05 13:28:41 +03:30
|
|
|
#include <engine/debug/instrumentor.hpp>
|
2021-07-12 15:20:47 +04:30
|
|
|
|
|
|
|
namespace Light {
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
Instrumentor *Instrumentor::s_Context = nullptr;
|
2022-03-07 21:57:00 +03:30
|
|
|
|
|
|
|
Scope<Instrumentor> Instrumentor::Create()
|
|
|
|
{
|
|
|
|
return MakeScope<Instrumentor>(new Instrumentor);
|
|
|
|
}
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
Instrumentor::Instrumentor(): m_CurrentSessionCount(0u)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
// #todo: maintenance
|
2025-07-05 13:28:41 +03:30
|
|
|
ASSERT(
|
|
|
|
!s_Context,
|
|
|
|
"An instance of 'Instrumentor' already exists, do not construct this class!"
|
|
|
|
);
|
2022-03-07 21:57:00 +03:30
|
|
|
s_Context = this;
|
|
|
|
}
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
void Instrumentor::BeginSessionImpl(const std::string &outputPath)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
std::filesystem::create_directory(outputPath.substr(0, outputPath.find_last_of('/') + 1));
|
|
|
|
|
|
|
|
m_OutputFileStream.open(outputPath);
|
|
|
|
m_OutputFileStream << "{\"traceEvents\":[";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instrumentor::EndSessionImpl()
|
|
|
|
{
|
|
|
|
if (m_CurrentSessionCount == 0u)
|
|
|
|
LOG(warn, "0 profiling for the ended session");
|
|
|
|
|
|
|
|
m_CurrentSessionCount = 0u;
|
|
|
|
|
|
|
|
m_OutputFileStream << "]}";
|
|
|
|
m_OutputFileStream.flush();
|
|
|
|
m_OutputFileStream.close();
|
|
|
|
}
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
void Instrumentor::SubmitScopeProfileImpl(const ScopeProfileResult &profileResult)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
if (m_CurrentSessionCount++ == 0u)
|
|
|
|
m_OutputFileStream << "{";
|
|
|
|
else
|
|
|
|
m_OutputFileStream << ",{";
|
|
|
|
|
|
|
|
m_OutputFileStream << "\"name\":\"" << profileResult.name << "\",";
|
|
|
|
m_OutputFileStream << "\"cat\": \"scope\",";
|
|
|
|
m_OutputFileStream << "\"ph\": \"X\",";
|
|
|
|
m_OutputFileStream << "\"ts\":" << profileResult.start << ",";
|
|
|
|
m_OutputFileStream << "\"dur\":" << profileResult.duration << ",";
|
|
|
|
m_OutputFileStream << "\"pid\":0,";
|
|
|
|
m_OutputFileStream << "\"tid\":" << profileResult.threadID << "";
|
|
|
|
m_OutputFileStream << "}";
|
|
|
|
}
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
InstrumentorTimer::InstrumentorTimer(const std::string &scopeName)
|
|
|
|
: m_Result({ scopeName, 0, 0, 0 })
|
|
|
|
, m_Start(std::chrono::steady_clock::now())
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InstrumentorTimer::~InstrumentorTimer()
|
|
|
|
{
|
|
|
|
auto end = std::chrono::steady_clock::now();
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
m_Result.start = std::chrono::time_point_cast<std::chrono::microseconds>(m_Start)
|
|
|
|
.time_since_epoch()
|
|
|
|
.count();
|
|
|
|
m_Result.duration = std::chrono::time_point_cast<std::chrono::microseconds>(end)
|
|
|
|
.time_since_epoch()
|
|
|
|
.count()
|
|
|
|
- m_Result.start;
|
2022-03-07 21:57:00 +03:30
|
|
|
|
|
|
|
Instrumentor::SubmitScopeProfile(m_Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Light
|