too tired to think
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
#ifndef INSTRUMENTOR_HPP
|
||||
#define INSTRUMENTOR_HPP
|
||||
|
||||
#include "open_engine/logging.hpp"
|
||||
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <algorithm>
|
||||
@@ -8,11 +12,15 @@
|
||||
#include <thread>
|
||||
|
||||
namespace OpenEngine {
|
||||
|
||||
using FloatingPointMicroseconds = std::chrono::duration<double, std::micro>;
|
||||
|
||||
struct ProfileResult
|
||||
{
|
||||
std::string name;
|
||||
long long start, end;
|
||||
uint32_t thread_id;
|
||||
FloatingPointMicroseconds start;
|
||||
std::chrono::microseconds elapsed_time;
|
||||
std::thread::id thread_id;
|
||||
};
|
||||
|
||||
struct InstrumentationSession
|
||||
@@ -30,45 +38,53 @@ namespace OpenEngine {
|
||||
|
||||
void BeginSession(const char* name, const std::string& filepath = "results.json")
|
||||
{
|
||||
std::lock_guard lock(mutex);
|
||||
if (current_session) {
|
||||
if (Logger::GetCoreLogger())
|
||||
OE_CORE_ERROR("Instrumentor::BeginSession({}), when session {} already exists", name, current_session->name);
|
||||
InternalEndSession();
|
||||
}
|
||||
|
||||
output_stream.open(filepath);
|
||||
WriteHeader();
|
||||
if(output_stream.is_open()) {
|
||||
current_session = new InstrumentationSession(name);
|
||||
WriteHeader();
|
||||
} else {
|
||||
if (Logger::GetCoreLogger())
|
||||
OE_CORE_ERROR("Instrumentor could not open results file: {}", filepath);
|
||||
}
|
||||
};
|
||||
|
||||
void EndSession()
|
||||
{
|
||||
WriteFooter();
|
||||
output_stream.flush();
|
||||
output_stream.close();
|
||||
profile_count = 0;
|
||||
std::lock_guard lock(mutex);
|
||||
InternalEndSession();
|
||||
};
|
||||
|
||||
void WriteProfile(const ProfileResult& result)
|
||||
{
|
||||
if (profile_count++ > 0)
|
||||
output_stream << ",";
|
||||
std::stringstream json;
|
||||
|
||||
std::string name = result.name;
|
||||
std::replace(name.begin(), name.end(), '"', '\'');
|
||||
|
||||
output_stream << "{";
|
||||
output_stream << "\"cat\":\"function\",";
|
||||
output_stream << "\"dur\":" << (result.end - result.start) << ',';
|
||||
output_stream << "\"name\":\"" << name << "\",";
|
||||
output_stream << "\"ph\":\"X\",";
|
||||
output_stream << "\"pid\":0,";
|
||||
output_stream << "\"tid\":" << result.thread_id << ",";
|
||||
output_stream << "\"ts\":" << result.start;
|
||||
output_stream << "}";
|
||||
};
|
||||
json << std::setprecision(3) << std::fixed;
|
||||
json << ",{";
|
||||
json << "\"cat\":\"function\",";
|
||||
json << "\"dur\":" << (result.elapsed_time.count()) << ',';
|
||||
json << "\"name\":\"" << name << "\",";
|
||||
json << "\"ph\":\"X\",";
|
||||
json << "\"pid\":0,";
|
||||
json << "\"tid\":" << result.thread_id << ",";
|
||||
json << "\"ts\":" << result.start.count();
|
||||
json << "}";
|
||||
|
||||
void WriteHeader()
|
||||
{
|
||||
output_stream << "{\"otherData\": {},\"traceEvents\":[";
|
||||
};
|
||||
std::lock_guard lock(mutex);
|
||||
|
||||
void WriteFooter()
|
||||
{
|
||||
output_stream << "]}";
|
||||
if (current_session) {
|
||||
output_stream << json.str();
|
||||
output_stream.flush();
|
||||
}
|
||||
};
|
||||
|
||||
static Instrumentor& Get()
|
||||
@@ -78,11 +94,32 @@ namespace OpenEngine {
|
||||
};
|
||||
|
||||
private:
|
||||
void WriteHeader()
|
||||
{
|
||||
output_stream << "{\"otherData\": {},\"traceEvents\":[{}";
|
||||
};
|
||||
|
||||
void WriteFooter()
|
||||
{
|
||||
output_stream << "]}";
|
||||
};
|
||||
|
||||
void InternalEndSession() {
|
||||
if (current_session) {
|
||||
WriteFooter();
|
||||
output_stream.close();
|
||||
delete current_session;
|
||||
current_session = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Instrumentor()
|
||||
{
|
||||
};
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
InstrumentationSession* current_session = nullptr;
|
||||
std::ofstream output_stream;
|
||||
int profile_count = 0;
|
||||
};
|
||||
@@ -106,11 +143,12 @@ namespace OpenEngine {
|
||||
{
|
||||
auto end_timepoint = std::chrono::high_resolution_clock::now();
|
||||
|
||||
long long start = std::chrono::time_point_cast<std::chrono::microseconds>(start_timepoint).time_since_epoch().count();
|
||||
long long end = std::chrono::time_point_cast<std::chrono::microseconds>(end_timepoint).time_since_epoch().count();
|
||||
auto start = FloatingPointMicroseconds{start_timepoint.time_since_epoch()};
|
||||
auto elapsed_time = std::chrono::time_point_cast<std::chrono::microseconds>(end_timepoint).time_since_epoch()
|
||||
- std::chrono::time_point_cast<std::chrono::microseconds>(start_timepoint).time_since_epoch();
|
||||
|
||||
uint32_t thread_id = std::hash<std::thread::id>{}(std::this_thread::get_id());
|
||||
Instrumentor::Get().WriteProfile({ name, start, end, thread_id });
|
||||
Instrumentor::Get().WriteProfile({ name, start, elapsed_time, std::this_thread::get_id() });
|
||||
|
||||
stopped = true;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user