Skip to content
Permalink
Browse files

Fixed Instrumentor generating same start time (#185)

  • Loading branch information
Penuvil authored and LovelySanta committed Jan 13, 2020
1 parent 9095bed commit 0716731c8e274f2e3d83aefef2222fe189a33728
Showing with 17 additions and 10 deletions.
  1. +17 −10 Hazel/src/Hazel/Debug/Instrumentor.h
@@ -3,14 +3,20 @@
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <string>
#include <thread>

namespace Hazel {

using FloatingPointMicroseconds = std::chrono::duration<double, std::micro>;

struct ProfileResult
{
std::string Name;
long long Start, End;

FloatingPointMicroseconds Start;
std::chrono::microseconds ElapsedTime;
std::thread::id ThreadID;
};

@@ -45,6 +51,7 @@ namespace Hazel {
InternalEndSession();
}
m_OutputStream.open(filepath);

if (m_OutputStream.is_open()) {
m_CurrentSession = new InstrumentationSession({name});
WriteHeader();
@@ -68,14 +75,15 @@ namespace Hazel {
std::string name = result.Name;
std::replace(name.begin(), name.end(), '"', '\'');

json << std::setprecision(3) << std::fixed;
json << ",{";
json << "\"cat\":\"function\",";
json << "\"dur\":" << (result.End - result.Start) << ',';
json << "\"dur\":" << (result.ElapsedTime.count()) << ',';
json << "\"name\":\"" << name << "\",";
json << "\"ph\":\"X\",";
json << "\"pid\":0,";
json << "\"tid\":" << result.ThreadID << ",";
json << "\"ts\":" << result.Start;
json << "\"ts\":" << result.Start.count();
json << "}";

std::lock_guard lock(m_Mutex);
@@ -123,7 +131,7 @@ namespace Hazel {
InstrumentationTimer(const char* name)
: m_Name(name), m_Stopped(false)
{
m_StartTimepoint = std::chrono::high_resolution_clock::now();
m_StartTimepoint = std::chrono::steady_clock::now();
}

~InstrumentationTimer()
@@ -134,18 +142,17 @@ namespace Hazel {

void Stop()
{
auto endTimepoint = std::chrono::high_resolution_clock::now();

long long start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();
long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();
auto endTimepoint = std::chrono::steady_clock::now();
auto highResStart = FloatingPointMicroseconds{ m_StartTimepoint.time_since_epoch() };
auto elapsedTime = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch() - std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch();

Instrumentor::Get().WriteProfile({ m_Name, start, end, std::this_thread::get_id()});
Instrumentor::Get().WriteProfile({ m_Name, highResStart, elapsedTime, std::this_thread::get_id() });

m_Stopped = true;
}
private:
const char* m_Name;
std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepoint;
std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;
bool m_Stopped;
};
}

0 comments on commit 0716731

Please sign in to comment.
You can’t perform that action at this time.