Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In Instrumentor::WriteProfie(), hold lock for less time
Browse files Browse the repository at this point in the history
0xworks committed Dec 5, 2019
1 parent 54ae187 commit 3d73b18
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions Hazel/src/Hazel/Debug/Instrumentor.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <string>
#include <chrono>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <fstream>

#include <string>
#include <thread>

namespace Hazel {
@@ -24,6 +24,7 @@ namespace Hazel {
{
private:
std::mutex m_Mutex;
std::atomic<bool> m_HasSession;
InstrumentationSession* m_CurrentSession;
std::ofstream m_OutputStream;
int m_ProfileCount;
@@ -36,7 +37,7 @@ namespace Hazel {
void BeginSession(const std::string& name, const std::string& filepath = "results.json")
{
std::lock_guard lock(m_Mutex);
if (m_CurrentSession) {
if (m_HasSession) {
// If there is already a current session, then close it before beginning new one.
// Subsequent profiling output meant for the original session will end up in the
// newly opened session instead. That's better than having badly formatted
@@ -48,6 +49,7 @@ namespace Hazel {
}
m_OutputStream.open(filepath);
if (m_OutputStream.is_open()) {
m_HasSession = true;
m_CurrentSession = new InstrumentationSession({name});
WriteHeader();
} else {
@@ -63,24 +65,26 @@ namespace Hazel {

void WriteProfile(const ProfileResult& result)
{
std::lock_guard lock(m_Mutex);
if (m_CurrentSession) {
if (m_HasSession) {
std::stringstream json;
if (m_ProfileCount++ > 0)

This comment has been minimized.

Copy link
@reductor

reductor Dec 5, 2019

this m_ProfileCount might want to be under the lock, otherwise just make the stream always have a starting item and always add a comma

m_OutputStream << ",";
json << ",";

std::string name = result.Name;
std::replace(name.begin(), name.end(), '"', '\'');

m_OutputStream << "{";
m_OutputStream << "\"cat\":\"function\",";
m_OutputStream << "\"dur\":" << (result.End - result.Start) << ',';
m_OutputStream << "\"name\":\"" << name << "\",";
m_OutputStream << "\"ph\":\"X\",";
m_OutputStream << "\"pid\":0,";
m_OutputStream << "\"tid\":" << result.ThreadID << ",";
m_OutputStream << "\"ts\":" << result.Start;
m_OutputStream << "}";

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

std::lock_guard lock(m_Mutex);
m_OutputStream << json.str();
m_OutputStream.flush();
}
}
@@ -112,6 +116,7 @@ namespace Hazel {
m_OutputStream.close();
delete m_CurrentSession;
m_CurrentSession = nullptr;
m_HasSession = 0;
m_ProfileCount = 0;
}
}

0 comments on commit 3d73b18

Please sign in to comment.