From dd2deaf36f7221a51a16b1b99670d6ff5ebbd9ec Mon Sep 17 00:00:00 2001 From: Sergey Yagovtsev Date: Sun, 31 Jul 2016 22:12:11 +0300 Subject: [PATCH] Add macro for specifying thread name --- include/profiler/profiler.h | 8 ++++++++ include/profiler/reader.h | 2 ++ src/block.cpp | 2 +- src/profile_manager.cpp | 16 ++++++++++++++++ src/profile_manager.h | 5 ++++- src/reader.cpp | 4 ++++ 6 files changed, 35 insertions(+), 2 deletions(-) diff --git a/include/profiler/profiler.h b/include/profiler/profiler.h index 2358799..ea54c7a 100644 --- a/include/profiler/profiler.h +++ b/include/profiler/profiler.h @@ -146,6 +146,10 @@ void foo() */ #define PROFILER_DISABLE profiler::setEnabled(false); +#define PROFILER_SET_THREAD_NAME(name) profiler::setThreadName(name); + +#define PROFILER_SET_MAIN_THREAD PROFILER_SET_THREAD_NAME("Main") + #else #define PROFILER_ADD_MARK(name) #define PROFILER_ADD_MARK_GROUPED(name,block_group) @@ -158,6 +162,8 @@ void foo() #define PROFILER_DISABLE profiler::setEnabled(false); #define PROFILER_ADD_EVENT(name) #define PROFILER_ADD_EVENT_GROUPED(name,block_group) +#define PROFILER_SET_THREAD_NAME(name) +#define PROFILER_SET_MAIN_THREAD #endif #include @@ -227,6 +233,7 @@ namespace profiler void PROFILER_API endBlock(); void PROFILER_API setEnabled(bool isEnable); unsigned int PROFILER_API dumpBlocksToFile(const char* filename); + void PROFILER_API setThreadName(const char* name); } typedef uint8_t block_type_t; @@ -235,6 +242,7 @@ namespace profiler const block_type_t BLOCK_TYPE_EVENT = 1; const block_type_t BLOCK_TYPE_BLOCK = 2; + const block_type_t BLOCK_TYPE_THREAD_SIGN = 3; #pragma pack(push,1) class PROFILER_API BaseBlockData diff --git a/include/profiler/reader.h b/include/profiler/reader.h index f0ee606..bbfffc4 100644 --- a/include/profiler/reader.h +++ b/include/profiler/reader.h @@ -99,6 +99,7 @@ struct BlocksTree ::profiler::SerilizedBlock* node; ::profiler::BlockStatistics* frame_statistics; ///< Pointer to statistics for this block within the parent (may be nullptr for top-level blocks) ::profiler::BlockStatistics* total_statistics; ///< Pointer to statistics for this block within the bounds of all frames per current thread + const char* thread_name; #ifdef PROFILER_COUNT_TOTAL_CHILDREN_NUMBER unsigned int total_children_number; ///< Number of all children including number of grandchildren (and so on) @@ -118,6 +119,7 @@ struct BlocksTree #ifdef PROFILER_COUNT_DEPTH , depth(0) #endif + , thread_name("") { } diff --git a/src/block.cpp b/src/block.cpp index 244f22c..cc32783 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -21,7 +21,7 @@ Block::Block(const char* _name, color_t _color, block_type_t _type) : name(_name) { tick(begin); - if (this->type == BLOCK_TYPE_EVENT) + if (BLOCK_TYPE_BLOCK != this->type) { end = begin; } diff --git a/src/profile_manager.cpp b/src/profile_manager.cpp index ff78cd3..dec111b 100644 --- a/src/profile_manager.cpp +++ b/src/profile_manager.cpp @@ -27,6 +27,11 @@ extern "C"{ { return ProfileManager::instance().dumpBlocksToFile(filename); } + + void PROFILER_API setThreadName(const char* name) + { + return ProfileManager::instance().setThreadName(name); + } } SerilizedBlock::SerilizedBlock(Block* block): @@ -164,3 +169,14 @@ unsigned int ProfileManager::dumpBlocksToFile(const char* filename) return size; } + +void ProfileManager::setThreadName(const char* name) +{ + profiler::Block block(name, 0, profiler::BLOCK_TYPE_THREAD_SIGN); + auto find_it = m_namedThreades.find(block.getThreadId()); + if (find_it != m_namedThreades.end()) + return; + + _internalInsertBlock(&block); + m_namedThreades.insert(block.getThreadId()); +} diff --git a/src/profile_manager.h b/src/profile_manager.h index 6819e10..a97b7ab 100644 --- a/src/profile_manager.h +++ b/src/profile_manager.h @@ -26,6 +26,7 @@ along with this program.If not, see . #include #include #include +#include #ifdef WIN32 #include @@ -54,6 +55,7 @@ class ProfileManager typedef std::stack stack_of_blocks_t; typedef std::map map_of_threads_stacks; + typedef std::set set_of_thread_id; map_of_threads_stacks m_openedBracketsMap; @@ -66,7 +68,7 @@ class ProfileManager typedef std::list serialized_list_t; serialized_list_t m_blocks; - + set_of_thread_id m_namedThreades; public: static ProfileManager& instance(); ~ProfileManager(); @@ -74,6 +76,7 @@ public: void endBlock(); void setEnabled(bool isEnable); unsigned int dumpBlocksToFile(const char* filename); + void setThreadName(const char* name); }; #endif diff --git a/src/reader.cpp b/src/reader.cpp index afae061..058b3d5 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -239,6 +239,10 @@ extern "C"{ tree.node = new profiler::SerilizedBlock(sz, data); ++blocks_counter; + if (::profiler::BLOCK_TYPE_THREAD_SIGN == baseData->getType()){ + root.thread_name = tree.node->getBlockName(); + } + if (!root.children.empty()) { BlocksTree& back = root.children.back();