0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-28 01:04:41 +08:00

(Core) Add main thread check

This commit is contained in:
Victor Zarubkin 2017-04-09 10:23:59 +03:00 committed by Sergey Yagovtsev
parent f00d731da4
commit f17aa956cd
2 changed files with 48 additions and 23 deletions

View File

@ -658,6 +658,7 @@ ProfileManager::ProfileManager() :
m_isAlreadyListening = ATOMIC_VAR_INIT(false); m_isAlreadyListening = ATOMIC_VAR_INIT(false);
m_stopListen = ATOMIC_VAR_INIT(false); m_stopListen = ATOMIC_VAR_INIT(false);
m_mainThreadId = ATOMIC_VAR_INIT(0);
m_frameMax = ATOMIC_VAR_INIT(0); m_frameMax = ATOMIC_VAR_INIT(0);
m_frameAvg = ATOMIC_VAR_INIT(0); m_frameAvg = ATOMIC_VAR_INIT(0);
m_frameCur = ATOMIC_VAR_INIT(0); m_frameCur = ATOMIC_VAR_INIT(0);
@ -1304,6 +1305,8 @@ uint32_t ProfileManager::dumpBlocksToStream(profiler::OStream& _outputStream, bo
} }
#endif #endif
bool mainThreadExpired = false;
// Calculate used memory total size and total blocks number // Calculate used memory total size and total blocks number
uint64_t usedMemorySize = 0; uint64_t usedMemorySize = 0;
uint32_t blocks_number = 0; uint32_t blocks_number = 0;
@ -1315,6 +1318,9 @@ uint32_t ProfileManager::dumpBlocksToStream(profiler::OStream& _outputStream, bo
const char expired = checkThreadExpired(t); const char expired = checkThreadExpired(t);
if (num == 0 && (expired != 0 || !t.guarded)) { if (num == 0 && (expired != 0 || !t.guarded)) {
// Remove thread if it contains no profiled information and has been finished or is not guarded. // Remove thread if it contains no profiled information and has been finished or is not guarded.
profiler::thread_id_t id = it->first;
if (!mainThreadExpired && m_mainThreadId.compare_exchange_weak(id, 0, std::memory_order_release, std::memory_order_acquire))
mainThreadExpired = true;
m_threads.erase(it++); m_threads.erase(it++);
continue; continue;
} }
@ -1399,9 +1405,17 @@ uint32_t ProfileManager::dumpBlocksToStream(profiler::OStream& _outputStream, bo
t.sync.openedList.clear(); t.sync.openedList.clear();
if (t.expired.load(std::memory_order_acquire) != 0) if (t.expired.load(std::memory_order_acquire) != 0)
m_threads.erase(it++); // Remove expired thread after writing all profiled information {
// Remove expired thread after writing all profiled information
profiler::thread_id_t id = it->first;
if (!mainThreadExpired && m_mainThreadId.compare_exchange_weak(id, 0, std::memory_order_release, std::memory_order_acquire))
mainThreadExpired = true;
m_threads.erase(it++);
}
else else
{
++it; ++it;
}
} }
m_storedSpin.unlock(); m_storedSpin.unlock();
@ -1450,11 +1464,16 @@ const char* ProfileManager::registerThread(const char* name, ThreadGuard& thread
THIS_THREAD = &threadStorage(getCurrentThreadId()); THIS_THREAD = &threadStorage(getCurrentThreadId());
THIS_THREAD->guarded = true; THIS_THREAD->guarded = true;
if (!THIS_THREAD->named) { if (!THIS_THREAD->named)
{
THIS_THREAD->named = true; THIS_THREAD->named = true;
THIS_THREAD->name = name; THIS_THREAD->name = name;
if (THIS_THREAD->name == "Main") if (THIS_THREAD->name == "Main")
THIS_THREAD_IS_MAIN = true; {
profiler::thread_id_t id = 0;
THIS_THREAD_IS_MAIN = m_mainThreadId.compare_exchange_weak(id, THIS_THREAD->id, std::memory_order_release, std::memory_order_acquire);
}
} }
threadGuard.m_id = THIS_THREAD->id; threadGuard.m_id = THIS_THREAD->id;
@ -1467,11 +1486,16 @@ const char* ProfileManager::registerThread(const char* name)
if (THIS_THREAD == nullptr) if (THIS_THREAD == nullptr)
THIS_THREAD = &threadStorage(getCurrentThreadId()); THIS_THREAD = &threadStorage(getCurrentThreadId());
if (!THIS_THREAD->named) { if (!THIS_THREAD->named)
{
THIS_THREAD->named = true; THIS_THREAD->named = true;
THIS_THREAD->name = name; THIS_THREAD->name = name;
if (THIS_THREAD->name == "Main") if (THIS_THREAD->name == "Main")
THIS_THREAD_IS_MAIN = true; {
profiler::thread_id_t id = 0;
THIS_THREAD_IS_MAIN = m_mainThreadId.compare_exchange_weak(id, THIS_THREAD->id, std::memory_order_release, std::memory_order_acquire);
}
} }
return THIS_THREAD->name.c_str(); return THIS_THREAD->name.c_str();

View File

@ -371,25 +371,26 @@ class ProfileManager
typedef std::unordered_map<profiler::hashed_stdstring, profiler::block_id_t> descriptors_map_t; typedef std::unordered_map<profiler::hashed_stdstring, profiler::block_id_t> descriptors_map_t;
#endif #endif
const processid_t m_processId; const processid_t m_processId;
map_of_threads_stacks m_threads; map_of_threads_stacks m_threads;
block_descriptors_t m_descriptors; block_descriptors_t m_descriptors;
descriptors_map_t m_descriptorsMap; descriptors_map_t m_descriptorsMap;
uint64_t m_usedMemorySize; uint64_t m_usedMemorySize;
profiler::timestamp_t m_beginTime; profiler::timestamp_t m_beginTime;
profiler::timestamp_t m_endTime; profiler::timestamp_t m_endTime;
std::atomic<profiler::timestamp_t> m_frameMax; std::atomic<profiler::timestamp_t> m_frameMax;
std::atomic<profiler::timestamp_t> m_frameAvg; std::atomic<profiler::timestamp_t> m_frameAvg;
std::atomic<profiler::timestamp_t> m_frameCur; std::atomic<profiler::timestamp_t> m_frameCur;
profiler::spin_lock m_spin; profiler::spin_lock m_spin;
profiler::spin_lock m_storedSpin; profiler::spin_lock m_storedSpin;
profiler::spin_lock m_dumpSpin; profiler::spin_lock m_dumpSpin;
std::atomic<char> m_profilerStatus; std::atomic<profiler::thread_id_t> m_mainThreadId;
std::atomic_bool m_isEventTracingEnabled; std::atomic<char> m_profilerStatus;
std::atomic_bool m_isAlreadyListening; std::atomic_bool m_isEventTracingEnabled;
std::atomic_bool m_frameMaxReset; std::atomic_bool m_isAlreadyListening;
std::atomic_bool m_frameAvgReset; std::atomic_bool m_frameMaxReset;
std::atomic_bool m_frameAvgReset;
std::string m_csInfoFilename = "/tmp/cs_profiling_info.log"; std::string m_csInfoFilename = "/tmp/cs_profiling_info.log";