0
0
mirror of https://github.com/yse/easy_profiler.git synced 2025-01-14 00:27:55 +08:00

(profiler Optimizations) PROFILER_SET_THREAD_NAME uses static variable, so profiler::setThreadName() will be invoked only once;

(profiler Optimizations) removed unnecessary profiler::Block creation from profiler::setThreadName();
(profiler Optimizations) removed statics from getCurrentTime() function - more correct static usage.
This commit is contained in:
Victor Zarubkin 2016-08-02 21:18:04 +03:00
parent 6445be6e41
commit fd67a3d81b
3 changed files with 42 additions and 28 deletions

View File

@ -146,7 +146,7 @@ void foo()
*/
#define PROFILER_DISABLE profiler::setEnabled(false);
#define PROFILER_SET_THREAD_NAME(name) profiler::setThreadName(name);
#define PROFILER_SET_THREAD_NAME(name) static const profiler::ThreadNameSetter TOKEN_CONCATENATE(unique_profiler_thread_name_setter_,__LINE__)(name);
#define PROFILER_SET_MAIN_THREAD PROFILER_SET_THREAD_NAME("Main")
@ -258,7 +258,7 @@ namespace profiler
void tick(timestamp_t& stamp);
public:
BaseBlockData(color_t _color, block_type_t _type);
BaseBlockData(color_t _color, block_type_t _type, thread_id_t _thread_id = 0);
inline block_type_t getType() const { return type; }
inline color_t getColor() const { return color; }
@ -283,7 +283,8 @@ namespace profiler
const char *name;
public:
Block(const char* _name, color_t _color = 0, block_type_t _type = BLOCK_TYPE_EVENT);
Block(const char* _name, color_t _color, block_type_t _type);
Block(const char* _name, thread_id_t _thread_id, color_t _color, block_type_t _type);
~Block();
inline const char* getName() const { return name; }
@ -307,7 +308,14 @@ namespace profiler
const char* getBlockName() const;
};
struct PROFILER_API ThreadNameSetter
{
ThreadNameSetter(const char* _name)
{
setThreadName(_name);
}
};
}
} // END of namespace profiler.
#endif

View File

@ -6,42 +6,47 @@
using namespace profiler;
BaseBlockData::BaseBlockData(color_t _color, block_type_t _type) :
type(_type),
color(_color),
begin(0),
end(0),
thread_id(0)
#ifdef WIN32
struct ProfPerformanceFrequency {
LARGE_INTEGER frequency;
ProfPerformanceFrequency() { QueryPerformanceFrequency(&frequency); }
} const WINDOWS_CPU_INFO;
#endif
BaseBlockData::BaseBlockData(color_t _color, block_type_t _type, thread_id_t _thread_id) : type(_type)
, color(_color)
, begin(0)
, end(0)
, thread_id(_thread_id)
{
}
Block::Block(const char* _name, color_t _color, block_type_t _type) :
BaseBlockData(_color,_type),
name(_name)
Block::Block(const char* _name, color_t _color, block_type_t _type)
: Block(_name, getCurrentThreadId(), _color, _type)
{
tick(begin);
if (BLOCK_TYPE_BLOCK != this->type)
{
end = begin;
}
thread_id = getCurrentThreadId();
}
Block::Block(const char* _name, thread_id_t _thread_id, color_t _color, block_type_t _type)
: BaseBlockData(_color, _type, _thread_id)
, name(_name)
{
tick(begin);
if (BLOCK_TYPE_BLOCK != this->type)
{
end = begin;
}
}
inline timestamp_t getCurrentTime()
{
#ifdef WIN32
//see https://msdn.microsoft.com/library/windows/desktop/dn553408(v=vs.85).aspx
static LARGE_INTEGER frequency;
static bool first=true;
if (first)
QueryPerformanceFrequency(&frequency);
first = false;
LARGE_INTEGER elapsedMicroseconds;
if (!QueryPerformanceCounter(&elapsedMicroseconds))
return 0;
elapsedMicroseconds.QuadPart *= 1000000000LL;
elapsedMicroseconds.QuadPart /= frequency.QuadPart;
elapsedMicroseconds.QuadPart /= WINDOWS_CPU_INFO.frequency.QuadPart;
return (timestamp_t)elapsedMicroseconds.QuadPart;
#else
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_point;

View File

@ -172,11 +172,12 @@ unsigned int ProfileManager::dumpBlocksToFile(const char* filename)
void ProfileManager::setThreadName(const char* name)
{
profiler::Block block(name, 0, profiler::BLOCK_TYPE_THREAD_SIGN);
auto find_it = m_namedThreades.find(block.getThreadId());
auto current_thread_id = getCurrentThreadId();
auto find_it = m_namedThreades.find(current_thread_id);
if (find_it != m_namedThreades.end())
return;
profiler::Block block(name, current_thread_id, 0, profiler::BLOCK_TYPE_THREAD_SIGN);
_internalInsertBlock(&block);
m_namedThreades.insert(block.getThreadId());
m_namedThreades.insert(current_thread_id);
}