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-09-08 21:03:05 +03:00
|
|
|
#include "profiler/serialized_block.h"
|
2016-02-16 23:21:12 +03:00
|
|
|
|
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-08-11 23:52:33 +03:00
|
|
|
#include <vector>
|
2016-09-06 23:03:05 +03:00
|
|
|
#include <string>
|
2016-08-21 14:46:16 +03:00
|
|
|
#include <functional>
|
2016-09-08 21:03:05 +03:00
|
|
|
#include <sstream>
|
|
|
|
#include <string.h>
|
2016-09-09 06:14:34 +03:00
|
|
|
#include <thread>
|
2016-09-08 23:15:01 +03:00
|
|
|
#include <atomic>
|
2016-09-08 21:03:05 +03:00
|
|
|
|
2016-02-20 05:24:12 +03:00
|
|
|
|
2016-08-28 02:41:02 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-01 22:22:58 +03:00
|
|
|
#ifdef _WIN32
|
2016-09-09 06:14:34 +03:00
|
|
|
//#include <Windows.h>
|
2016-06-21 00:13:45 +03:00
|
|
|
#else
|
|
|
|
#include <thread>
|
2016-09-05 22:02:32 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/syscall.h>
|
2016-06-21 00:13:45 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
inline uint32_t getCurrentThreadId()
|
|
|
|
{
|
2016-09-01 22:22:58 +03:00
|
|
|
#ifdef _WIN32
|
2016-09-06 23:03:05 +03:00
|
|
|
return (uint32_t)::GetCurrentThreadId();
|
2016-06-21 00:13:45 +03:00
|
|
|
#else
|
2016-09-06 23:03:05 +03:00
|
|
|
EASY_THREAD_LOCAL static const pid_t x = syscall(__NR_gettid);
|
|
|
|
EASY_THREAD_LOCAL static const uint32_t _id = (uint32_t)x;//std::hash<std::thread::id>()(std::this_thread::get_id());
|
2016-09-05 22:02:32 +03:00
|
|
|
return _id;
|
2016-06-21 00:13:45 +03:00
|
|
|
#endif
|
|
|
|
}
|
2016-06-20 23:21:54 +03:00
|
|
|
|
2016-08-28 02:41:02 +03:00
|
|
|
namespace profiler { class SerializedBlock; }
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-04 14:48:35 +03:00
|
|
|
template <class T, const uint16_t N>
|
2016-08-28 02:41:02 +03:00
|
|
|
class chunk_allocator final
|
2016-02-16 23:21:12 +03:00
|
|
|
{
|
2016-08-28 21:06:23 +03:00
|
|
|
struct chunk { T data[N]; };
|
2016-02-16 23:21:12 +03:00
|
|
|
|
2016-08-28 02:41:02 +03:00
|
|
|
std::list<chunk> m_chunks;
|
|
|
|
uint16_t m_size;
|
2016-02-18 19:27:17 +03:00
|
|
|
|
2016-08-28 02:41:02 +03:00
|
|
|
public:
|
2016-02-18 19:27:17 +03:00
|
|
|
|
2016-08-28 02:41:02 +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;
|
|
|
|
}
|
|
|
|
|
2016-09-04 19:35:58 +03:00
|
|
|
m_size = n;
|
2016-08-28 02:41:02 +03:00
|
|
|
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
|
|
|
|
2016-08-28 02:41:02 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2016-02-20 05:24:12 +03:00
|
|
|
|
2016-09-04 19:35:58 +03:00
|
|
|
const uint16_t SIZEOF_CSWITCH = sizeof(profiler::BaseBlockData) + 1;
|
|
|
|
|
2016-09-07 21:50:42 +03:00
|
|
|
typedef std::vector<profiler::SerializedBlock*> serialized_list_t;
|
|
|
|
|
|
|
|
template <class T, const uint16_t N>
|
|
|
|
struct BlocksList final
|
2016-08-28 02:41:02 +03:00
|
|
|
{
|
2016-09-07 21:50:42 +03:00
|
|
|
BlocksList() = default;
|
2016-02-20 05:24:12 +03:00
|
|
|
|
2016-09-07 21:50:42 +03:00
|
|
|
class Stack final {
|
|
|
|
//std::stack<T> m_stack;
|
|
|
|
std::vector<T> m_stack;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
inline void clear() { m_stack.clear(); }
|
|
|
|
inline bool empty() const { return m_stack.empty(); }
|
|
|
|
|
|
|
|
inline void emplace(profiler::Block& _block) {
|
|
|
|
//m_stack.emplace(_block);
|
|
|
|
m_stack.emplace_back(_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ... TArgs> inline void emplace(TArgs ... _args) {
|
|
|
|
//m_stack.emplace(_args);
|
|
|
|
m_stack.emplace_back(_args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T& top() {
|
|
|
|
//return m_stack.top();
|
|
|
|
return m_stack.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void pop() {
|
|
|
|
//m_stack.pop();
|
|
|
|
m_stack.pop_back();
|
2016-09-04 14:48:35 +03:00
|
|
|
}
|
|
|
|
};
|
2016-08-11 23:52:33 +03:00
|
|
|
|
2016-09-07 21:50:42 +03:00
|
|
|
chunk_allocator<char, N> alloc;
|
|
|
|
Stack openedList;
|
|
|
|
serialized_list_t closedList;
|
|
|
|
uint64_t usedMemorySize = 0;
|
|
|
|
|
|
|
|
void clearClosed() {
|
|
|
|
serialized_list_t().swap(closedList);
|
|
|
|
alloc.clear();
|
|
|
|
usedMemorySize = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadStorage final
|
|
|
|
{
|
2016-08-28 02:41:02 +03:00
|
|
|
public:
|
2016-06-20 23:10:14 +03:00
|
|
|
|
2016-09-04 19:35:58 +03:00
|
|
|
BlocksList<std::reference_wrapper<profiler::Block>, SIZEOF_CSWITCH * (uint16_t)1024U> blocks;
|
|
|
|
BlocksList<profiler::Block, SIZEOF_CSWITCH * (uint16_t)128U> sync;
|
2016-09-06 23:03:05 +03:00
|
|
|
std::string name;
|
2016-09-04 14:48:35 +03:00
|
|
|
bool named = false;
|
2016-08-11 23:52:33 +03:00
|
|
|
|
2016-09-04 14:48:35 +03:00
|
|
|
void storeBlock(const profiler::Block& _block);
|
|
|
|
void storeCSwitch(const profiler::Block& _block);
|
2016-08-28 02:41:02 +03:00
|
|
|
void clearClosed();
|
2016-09-07 21:40:40 +03:00
|
|
|
|
|
|
|
ThreadStorage() = default;
|
2016-08-28 02:41:02 +03:00
|
|
|
};
|
|
|
|
|
2016-09-08 21:03:05 +03:00
|
|
|
class StreamWriter final
|
|
|
|
{
|
|
|
|
std::stringstream m_stream;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
explicit StreamWriter() : m_stream(std::ios_base::out | std::ios_base::binary) { }
|
|
|
|
|
|
|
|
template <typename T> void write(const char* _data, T _size) {
|
|
|
|
m_stream.write(_data, _size);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T> void write(const T& _data) {
|
|
|
|
m_stream.write((const char*)&_data, sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeBlock(const profiler::SerializedBlock* _block)
|
|
|
|
{
|
|
|
|
auto sz = static_cast<uint16_t>(sizeof(profiler::BaseBlockData) + strlen(_block->name()) + 1);
|
|
|
|
write(sz);
|
|
|
|
write(_block->data(), sz);
|
|
|
|
}
|
|
|
|
const std::stringstream& stream() const {return m_stream;}
|
|
|
|
};
|
|
|
|
|
2016-08-28 02:41:02 +03:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-04 14:48:35 +03:00
|
|
|
class ProfileManager final
|
2016-08-28 02:41:02 +03:00
|
|
|
{
|
|
|
|
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-08-14 22:22:44 +03:00
|
|
|
|
2016-09-05 22:11:03 +03:00
|
|
|
std::string m_csInfoFilename = "/tmp/cs_profiling_info.log";
|
|
|
|
|
2016-09-08 21:03:05 +03:00
|
|
|
std::thread m_listenThread;
|
|
|
|
bool m_isAlreadyListened = false;
|
|
|
|
void startListen();
|
|
|
|
|
|
|
|
int m_socket = 0;//TODO crossplatform
|
|
|
|
|
|
|
|
uint32_t dumpBlocksToStream(StreamWriter& _stream);
|
2016-09-08 23:15:01 +03:00
|
|
|
std::atomic_bool m_stopListen;
|
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-09-07 21:32:14 +03:00
|
|
|
~ProfileManager();
|
2016-08-11 23:52:33 +03:00
|
|
|
|
2016-09-06 23:03:05 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-21 14:46:16 +03:00
|
|
|
void beginBlock(profiler::Block& _block);
|
2016-09-07 21:32:14 +03:00
|
|
|
void endBlock();
|
|
|
|
void setEnabled(bool isEnable);
|
2016-08-28 02:41:02 +03:00
|
|
|
uint32_t dumpBlocksToFile(const char* filename);
|
2016-09-06 23:03:05 +03:00
|
|
|
const char* setThreadName(const char* name, const char* filename, const char* _funcname, int line);
|
2016-08-28 02:41:02 +03:00
|
|
|
|
2016-09-05 22:11:03 +03:00
|
|
|
void setContextSwitchLogFilename(const char* name)
|
|
|
|
{
|
|
|
|
m_csInfoFilename = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* getContextSwitchLogFilename() const
|
|
|
|
{
|
|
|
|
return m_csInfoFilename.c_str();
|
|
|
|
}
|
|
|
|
|
2016-09-06 23:03:05 +03:00
|
|
|
void beginContextSwitch(profiler::thread_id_t _thread_id, profiler::timestamp_t _time, profiler::block_id_t _id);
|
|
|
|
void storeContextSwitch(profiler::thread_id_t _thread_id, profiler::timestamp_t _time, profiler::block_id_t _id);
|
|
|
|
void endContextSwitch(profiler::thread_id_t _thread_id, profiler::timestamp_t _endtime);
|
2016-09-04 14:48:35 +03:00
|
|
|
|
2016-09-08 21:03:05 +03:00
|
|
|
void startListenSignalToCapture();
|
|
|
|
void stopListenSignalToCapture();
|
|
|
|
|
2016-08-28 02:41:02 +03:00
|
|
|
private:
|
|
|
|
|
|
|
|
ThreadStorage& threadStorage(profiler::thread_id_t _thread_id)
|
|
|
|
{
|
|
|
|
guard_lock_t lock(m_spin);
|
|
|
|
return m_threads[_thread_id];
|
|
|
|
}
|
2016-09-04 14:48:35 +03:00
|
|
|
|
2016-09-06 23:03:05 +03:00
|
|
|
ThreadStorage* findThreadStorage(profiler::thread_id_t _thread_id)
|
2016-09-04 14:48:35 +03:00
|
|
|
{
|
|
|
|
guard_lock_t lock(m_spin);
|
|
|
|
auto it = m_threads.find(_thread_id);
|
|
|
|
return it != m_threads.end() ? &it->second : nullptr;
|
|
|
|
}
|
2016-02-16 23:21:12 +03:00
|
|
|
};
|
|
|
|
|
2016-02-16 23:25:12 +03:00
|
|
|
#endif
|