Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/Profiler2.cpp
/* Copyright (C) 2018 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* | * | ||||
* Permission is hereby granted, free of charge, to any person obtaining | * Permission is hereby granted, free of charge, to any person obtaining | ||||
* a copy of this software and associated documentation files (the | * a copy of this software and associated documentation files (the | ||||
* "Software"), to deal in the Software without restriction, including | * "Software"), to deal in the Software without restriction, including | ||||
* without limitation the rights to use, copy, modify, merge, publish, | * without limitation the rights to use, copy, modify, merge, publish, | ||||
* distribute, sublicense, and/or sell copies of the Software, and to | * distribute, sublicense, and/or sell copies of the Software, and to | ||||
* permit persons to whom the Software is furnished to do so, subject to | * permit persons to whom the Software is furnished to do so, subject to | ||||
* the following conditions: | * the following conditions: | ||||
▲ Show 20 Lines • Show All 282 Lines • ▼ Show 20 Lines | void CProfiler2::RegisterCurrentThread(const std::string& name) | ||||
RecordSyncMarker(); | RecordSyncMarker(); | ||||
RecordEvent("thread start"); | RecordEvent("thread start"); | ||||
AddThreadStorage(storage); | AddThreadStorage(storage); | ||||
} | } | ||||
void CProfiler2::AddThreadStorage(ThreadStorage* storage) | void CProfiler2::AddThreadStorage(ThreadStorage* storage) | ||||
{ | { | ||||
CScopeLock lock(m_Mutex); | std::lock_guard<std::mutex> lock(m_Mutex); | ||||
m_Threads.push_back(storage); | m_Threads.push_back(storage); | ||||
} | } | ||||
void CProfiler2::RemoveThreadStorage(ThreadStorage* storage) | void CProfiler2::RemoveThreadStorage(ThreadStorage* storage) | ||||
{ | { | ||||
CScopeLock lock(m_Mutex); | std::lock_guard<std::mutex> lock(m_Mutex); | ||||
m_Threads.erase(std::find(m_Threads.begin(), m_Threads.end(), storage)); | m_Threads.erase(std::find(m_Threads.begin(), m_Threads.end(), storage)); | ||||
} | } | ||||
CProfiler2::ThreadStorage::ThreadStorage(CProfiler2& profiler, const std::string& name) : | CProfiler2::ThreadStorage::ThreadStorage(CProfiler2& profiler, const std::string& name) : | ||||
m_Profiler(profiler), m_Name(name), m_BufferPos0(0), m_BufferPos1(0), m_LastTime(timer_Time()), m_HeldDepth(0) | m_Profiler(profiler), m_Name(name), m_BufferPos0(0), m_BufferPos1(0), m_LastTime(timer_Time()), m_HeldDepth(0) | ||||
{ | { | ||||
m_Buffer = new u8[BUFFER_SIZE]; | m_Buffer = new u8[BUFFER_SIZE]; | ||||
memset(m_Buffer, ITEM_NOP, BUFFER_SIZE); | memset(m_Buffer, ITEM_NOP, BUFFER_SIZE); | ||||
▲ Show 20 Lines • Show All 404 Lines • ▼ Show 20 Lines | if (!m_HeldDepth) | ||||
return; | return; | ||||
m_HeldDepth--; | m_HeldDepth--; | ||||
} | } | ||||
void CProfiler2::ConstructJSONOverview(std::ostream& stream) | void CProfiler2::ConstructJSONOverview(std::ostream& stream) | ||||
{ | { | ||||
TIMER(L"profile2 overview"); | TIMER(L"profile2 overview"); | ||||
CScopeLock lock(m_Mutex); | std::lock_guard<std::mutex> lock(m_Mutex); | ||||
stream << "{\"threads\":["; | stream << "{\"threads\":["; | ||||
for (size_t i = 0; i < m_Threads.size(); ++i) | for (size_t i = 0; i < m_Threads.size(); ++i) | ||||
{ | { | ||||
if (i != 0) | if (i != 0) | ||||
stream << ","; | stream << ","; | ||||
stream << "{\"name\":\"" << CStr(m_Threads[i]->GetName()).EscapeToPrintableASCII() << "\"}"; | stream << "{\"name\":\"" << CStr(m_Threads[i]->GetName()).EscapeToPrintableASCII() << "\"}"; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
TIMER(L"profile2 query"); | TIMER(L"profile2 query"); | ||||
std::string buffer; | std::string buffer; | ||||
{ | { | ||||
TIMER(L"profile2 get buffer"); | TIMER(L"profile2 get buffer"); | ||||
CScopeLock lock(m_Mutex); // lock against changes to m_Threads or deletions of ThreadStorage | std::lock_guard<std::mutex> lock(m_Mutex); // lock against changes to m_Threads or deletions of ThreadStorage | ||||
ThreadStorage* storage = NULL; | ThreadStorage* storage = NULL; | ||||
for (size_t i = 0; i < m_Threads.size(); ++i) | for (size_t i = 0; i < m_Threads.size(); ++i) | ||||
{ | { | ||||
if (m_Threads[i]->GetName() == thread) | if (m_Threads[i]->GetName() == thread) | ||||
{ | { | ||||
storage = m_Threads[i]; | storage = m_Threads[i]; | ||||
break; | break; | ||||
Show All 21 Lines | |||||
{ | { | ||||
OsPath path = psLogDir()/"profile2.jsonp"; | OsPath path = psLogDir()/"profile2.jsonp"; | ||||
std::ofstream stream(OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); | std::ofstream stream(OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); | ||||
ENSURE(stream.good()); | ENSURE(stream.good()); | ||||
std::vector<ThreadStorage*> threads; | std::vector<ThreadStorage*> threads; | ||||
{ | { | ||||
CScopeLock lock(m_Mutex); | std::lock_guard<std::mutex> lock(m_Mutex); | ||||
threads = m_Threads; | threads = m_Threads; | ||||
} | } | ||||
stream << "profileDataCB({\"threads\": [\n"; | stream << "profileDataCB({\"threads\": [\n"; | ||||
for (size_t i = 0; i < threads.size(); ++i) | for (size_t i = 0; i < threads.size(); ++i) | ||||
{ | { | ||||
if (i != 0) | if (i != 0) | ||||
stream << ",\n"; | stream << ",\n"; | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator