2016-02-16 23:21:12 +03:00
|
|
|
/**
|
|
|
|
Lightweight profiler library for c++
|
2016-08-11 23:52:33 +03:00
|
|
|
Copyright(C) 2016 Sergey Yagovtsev, Victor Zarubkin
|
2016-02-16 23:21:12 +03:00
|
|
|
|
|
|
|
This program is free software : you can redistribute it and / or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program.If not, see <http://www.gnu.org/licenses/>.
|
2016-02-17 23:43:37 +03:00
|
|
|
**/
|
2016-02-16 23:21:12 +03:00
|
|
|
|
|
|
|
#ifndef ___PROFILER____MANAGER____H______
|
|
|
|
#define ___PROFILER____MANAGER____H______
|
|
|
|
|
|
|
|
#include "profiler/profiler.h"
|
|
|
|
|
2016-02-18 19:27:17 +03:00
|
|
|
#include "spin_lock.h"
|
|
|
|
|
|
|
|
#include <stack>
|
|
|
|
#include <map>
|
2016-02-20 05:24:12 +03:00
|
|
|
#include <list>
|
2016-07-31 22:12:11 +03:00
|
|
|
#include <set>
|
2016-08-11 23:52:33 +03:00
|
|
|
#include <vector>
|
2016-02-20 05:24:12 +03:00
|
|
|
|
2016-06-21 00:13:45 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <thread>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
inline uint32_t getCurrentThreadId()
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
return (uint32_t)::GetCurrentThreadId();
|
|
|
|
#else
|
2016-06-22 23:57:11 +03:00
|
|
|
thread_local static uint32_t _id = (uint32_t)std::hash<std::thread::id>()(std::this_thread::get_id());
|
|
|
|
return _id;
|
2016-06-21 00:13:45 +03:00
|
|
|
#endif
|
|
|
|
}
|
2016-06-20 23:21:54 +03:00
|
|
|
|
2016-02-16 23:21:12 +03:00
|
|
|
class ProfileManager
|
|
|
|
{
|
|
|
|
ProfileManager();
|
|
|
|
ProfileManager(const ProfileManager& p) = delete;
|
|
|
|
ProfileManager& operator=(const ProfileManager&) = delete;
|
2016-02-17 18:18:06 +03:00
|
|
|
static ProfileManager m_profileManager;
|
2016-02-16 23:21:12 +03:00
|
|
|
|
|
|
|
bool m_isEnabled = false;
|
2016-02-18 19:27:17 +03:00
|
|
|
|
|
|
|
typedef std::stack<profiler::Block*> stack_of_blocks_t;
|
|
|
|
typedef std::map<size_t, stack_of_blocks_t> map_of_threads_stacks;
|
2016-07-31 22:12:11 +03:00
|
|
|
typedef std::set<size_t> set_of_thread_id;
|
2016-08-11 23:52:33 +03:00
|
|
|
typedef std::vector<profiler::SourceBlock> sources;
|
2016-02-18 19:27:17 +03:00
|
|
|
|
|
|
|
map_of_threads_stacks m_openedBracketsMap;
|
|
|
|
|
|
|
|
profiler::spin_lock m_spin;
|
2016-02-20 05:24:12 +03:00
|
|
|
profiler::spin_lock m_storedSpin;
|
2016-02-18 19:27:17 +03:00
|
|
|
typedef profiler::guard_lock<profiler::spin_lock> guard_lock_t;
|
2016-02-20 05:24:12 +03:00
|
|
|
|
2016-08-19 00:35:31 +03:00
|
|
|
void _internalInsertBlock(profiler::Block &_block);
|
2016-02-20 05:24:12 +03:00
|
|
|
|
2016-08-11 23:52:33 +03:00
|
|
|
sources m_sources;
|
|
|
|
|
2016-08-07 21:40:23 +03:00
|
|
|
typedef std::list<profiler::SerializedBlock*> serialized_list_t;
|
2016-02-20 05:24:12 +03:00
|
|
|
serialized_list_t m_blocks;
|
2016-06-20 23:10:14 +03:00
|
|
|
|
2016-07-31 22:12:11 +03:00
|
|
|
set_of_thread_id m_namedThreades;
|
2016-08-11 23:52:33 +03:00
|
|
|
|
2016-08-14 22:22:44 +03:00
|
|
|
uint64_t m_blocksMemorySize = 0;
|
|
|
|
|
2016-02-16 23:21:12 +03:00
|
|
|
public:
|
2016-08-11 23:52:33 +03:00
|
|
|
|
2016-02-17 18:18:06 +03:00
|
|
|
static ProfileManager& instance();
|
2016-02-20 05:24:12 +03:00
|
|
|
~ProfileManager();
|
2016-08-11 23:52:33 +03:00
|
|
|
|
|
|
|
void beginBlock(profiler::Block* _block);
|
2016-02-16 23:21:12 +03:00
|
|
|
void endBlock();
|
|
|
|
void setEnabled(bool isEnable);
|
2016-07-04 22:53:02 +03:00
|
|
|
unsigned int dumpBlocksToFile(const char* filename);
|
2016-07-31 22:12:11 +03:00
|
|
|
void setThreadName(const char* name);
|
2016-08-11 23:52:33 +03:00
|
|
|
unsigned int addSource(const char* _filename, int _line);
|
2016-02-16 23:21:12 +03:00
|
|
|
};
|
|
|
|
|
2016-02-16 23:25:12 +03:00
|
|
|
#endif
|