0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-27 08:41:02 +08:00
easy_profiler/src/profile_manager.h

163 lines
4.0 KiB
C
Raw Normal View History

2016-02-16 23:21:12 +03:00
/**
Lightweight profiler library for c++
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>
#include <list>
2016-07-31 22:12:11 +03:00
#include <set>
#include <vector>
#include <functional>
//////////////////////////////////////////////////////////////////////////
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
namespace profiler { class SerializedBlock; }
//////////////////////////////////////////////////////////////////////////
template <class T, uint16_t N>
class chunk_allocator final
2016-02-16 23:21:12 +03:00
{
struct chunk { T data[N]; };
2016-02-16 23:21:12 +03:00
std::list<chunk> m_chunks;
uint16_t m_size;
2016-02-18 19:27:17 +03:00
public:
2016-02-18 19:27:17 +03:00
chunk_allocator() : m_size(0)
{
m_chunks.emplace_back();
}
T* allocate(uint16_t n)
{
if (m_size + n <= N)
{
T* data = m_chunks.back().data + m_size;
m_size += n;
return data;
}
m_size = 0;
m_chunks.emplace_back();
return m_chunks.back().data;
}
void clear()
{
m_size = 0;
m_chunks.clear();
m_chunks.emplace_back();
}
};
2016-02-18 19:27:17 +03:00
//////////////////////////////////////////////////////////////////////////
class ThreadStorage
{
typedef std::stack<std::reference_wrapper<profiler::Block> > stack_of_blocks_t;
typedef std::vector<profiler::SerializedBlock*> serialized_list_t;
chunk_allocator<char, 1024> m_allocator;
public:
2016-06-20 23:10:14 +03:00
stack_of_blocks_t openedList;
serialized_list_t closedList;
uint64_t usedMemorySize = 0;
bool named = false;
void store(const profiler::Block& _block);
void clearClosed();
};
//////////////////////////////////////////////////////////////////////////
class ProfileManager
{
friend profiler::StaticBlockDescriptor;
ProfileManager();
ProfileManager(const ProfileManager& p) = delete;
ProfileManager& operator=(const ProfileManager&) = delete;
typedef profiler::guard_lock<profiler::spin_lock> guard_lock_t;
typedef std::map<profiler::thread_id_t, ThreadStorage> map_of_threads_stacks;
typedef std::vector<profiler::BlockDescriptor> block_descriptors_t;
map_of_threads_stacks m_threads;
block_descriptors_t m_descriptors;
uint64_t m_usedMemorySize = 0;
profiler::spin_lock m_spin;
profiler::spin_lock m_storedSpin;
bool m_isEnabled = false;
2016-02-16 23:21:12 +03:00
public:
static ProfileManager& instance();
~ProfileManager();
void beginBlock(profiler::Block& _block);
2016-02-16 23:21:12 +03:00
void endBlock();
void setEnabled(bool isEnable);
uint32_t dumpBlocksToFile(const char* filename);
2016-07-31 22:12:11 +03:00
void setThreadName(const char* name);
private:
template <class ... TArgs>
uint32_t addBlockDescriptor(TArgs ... _args)
{
guard_lock_t lock(m_storedSpin);
const auto id = static_cast<uint32_t>(m_descriptors.size());
m_descriptors.emplace_back(m_usedMemorySize, _args...);
return id;
}
ThreadStorage& threadStorage(profiler::thread_id_t _thread_id)
{
guard_lock_t lock(m_spin);
return m_threads[_thread_id];
}
2016-02-16 23:21:12 +03:00
};
2016-02-16 23:25:12 +03:00
#endif