light/modules/debug/public/instrumentor.hpp

78 lines
1.7 KiB
C++
Raw Normal View History

2022-03-08 21:19:19 +03:30
#pragma once
#include <chrono>
#include <fstream>
2025-07-11 00:05:48 +03:30
namespace lt {
2022-03-08 21:19:19 +03:30
struct ScopeProfileResult
{
std::string name;
long long start, duration;
uint32_t threadID;
};
2025-07-05 16:07:51 +03:30
class Instrumentor
2022-03-08 21:19:19 +03:30
{
public:
2025-07-06 17:23:28 +03:30
static auto instance() -> Instrumentor &
{
static auto instance = Instrumentor {};
return instance;
}
2022-03-08 21:19:19 +03:30
2025-07-06 14:02:50 +03:30
static void begin_session(const std::string &outputPath)
2025-07-05 13:28:41 +03:30
{
2025-07-06 17:23:28 +03:30
instance().begin_session_impl(outputPath);
2025-07-05 13:28:41 +03:30
}
2025-07-06 14:02:50 +03:30
static void end_session()
2025-07-05 13:28:41 +03:30
{
2025-07-06 17:23:28 +03:30
instance().end_session_impl();
2025-07-05 13:28:41 +03:30
}
2022-03-08 21:19:19 +03:30
2025-07-06 14:02:50 +03:30
static void submit_scope_profile(const ScopeProfileResult &profileResult)
2025-07-05 13:28:41 +03:30
{
2025-07-06 17:23:28 +03:30
instance().submit_scope_profile_impl(profileResult);
2025-07-05 13:28:41 +03:30
}
2022-03-08 21:19:19 +03:30
private:
2025-07-05 16:07:51 +03:30
std::ofstream m_output_file_stream;
2025-07-06 17:23:28 +03:30
unsigned int m_current_session_count { 0u };
2025-07-05 16:07:51 +03:30
2025-07-06 17:23:28 +03:30
Instrumentor() = default;
2022-03-08 21:19:19 +03:30
void begin_session_impl(const std::string &outputPath);
2025-07-05 16:07:51 +03:30
void end_session_impl();
2022-03-08 21:19:19 +03:30
void submit_scope_profile_impl(const ScopeProfileResult &profileResult);
2022-03-08 21:19:19 +03:30
};
class InstrumentorTimer
{
public:
2025-07-05 13:28:41 +03:30
InstrumentorTimer(const std::string &scopeName);
2025-07-05 16:07:51 +03:30
2022-03-08 21:19:19 +03:30
~InstrumentorTimer();
2025-07-05 16:07:51 +03:30
private:
ScopeProfileResult m_result;
std::chrono::time_point<std::chrono::steady_clock> m_start;
2022-03-08 21:19:19 +03:30
};
2025-07-11 00:05:48 +03:30
} // namespace lt
2022-03-08 21:19:19 +03:30
/* scope */
#define lt_profile_scope(name) lt_profile_scope_no_redifinition(name, __LINE__)
#define lt_profile_scope_no_redifinition(name, line) lt_profile_scope_no_redifinition2(name, line)
#define lt_profile_scope_no_redifinition2(name, line) InstrumentorTimer timer##line(name)
2022-03-08 21:19:19 +03:30
/* function */
#define LT_PROFILE_FUNCTION lt_profile_scope(__FUNCSIG__)
2022-03-08 21:19:19 +03:30
/* session */
2025-07-11 00:05:48 +03:30
#define lt_profile_begin_session(outputPath) ::lt::Instrumentor::begin_session(outputPath)
#define lt_profile_end_session() ::lt::Instrumentor::end_session()