2016-06-19 23:01:49 +03:00
|
|
|
/**
|
|
|
|
Lightweight profiler library for c++
|
|
|
|
Copyright(C) 2016 Sergey Yagovtsev
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#ifndef PROFILER_READER____H
|
|
|
|
#define PROFILER_READER____H
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-26 00:58:40 +03:00
|
|
|
#include <list>
|
2016-06-19 23:46:42 +03:00
|
|
|
#include <map>
|
2016-06-19 23:57:58 +03:00
|
|
|
#include "profiler/profiler.h"
|
2016-06-19 23:01:49 +03:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-07-31 18:48:41 +03:00
|
|
|
#define PROFILER_COUNT_TOTAL_CHILDREN_NUMBER
|
2016-07-27 21:50:11 +03:00
|
|
|
#define PROFILER_COUNT_DEPTH
|
|
|
|
|
2016-06-25 17:24:29 +03:00
|
|
|
namespace profiler {
|
|
|
|
|
|
|
|
typedef uint32_t calls_number_t;
|
|
|
|
|
|
|
|
struct BlockStatistics
|
|
|
|
{
|
2016-06-26 02:15:39 +03:00
|
|
|
::profiler::timestamp_t total_duration; ///< Summary duration of all block calls
|
|
|
|
::profiler::timestamp_t min_duration; ///< Cached block->duration() value. TODO: Remove this if memory consumption will be too high
|
|
|
|
::profiler::timestamp_t max_duration; ///< Cached block->duration() value. TODO: Remove this if memory consumption will be too high
|
|
|
|
const ::profiler::SerilizedBlock* min_duration_block; ///< Will be used in GUI to jump to the block with min duration
|
|
|
|
const ::profiler::SerilizedBlock* max_duration_block; ///< Will be used in GUI to jump to the block with max duration
|
|
|
|
::profiler::calls_number_t calls_number; ///< Block calls number
|
|
|
|
|
|
|
|
// TODO: It is better to replace SerilizedBlock* with BlocksTree*, but this requires to store pointers in children list.
|
|
|
|
|
|
|
|
BlockStatistics()
|
|
|
|
: total_duration(0)
|
|
|
|
, min_duration(0)
|
|
|
|
, max_duration(0)
|
|
|
|
, min_duration_block(nullptr)
|
|
|
|
, max_duration_block(nullptr)
|
|
|
|
, calls_number(1)
|
2016-06-26 00:56:24 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-26 02:15:39 +03:00
|
|
|
BlockStatistics(::profiler::timestamp_t _duration, const ::profiler::SerilizedBlock* _block)
|
2016-06-26 00:56:24 +03:00
|
|
|
: total_duration(_duration)
|
|
|
|
, min_duration(_duration)
|
|
|
|
, max_duration(_duration)
|
2016-06-26 02:15:39 +03:00
|
|
|
, min_duration_block(_block)
|
|
|
|
, max_duration_block(_block)
|
2016-06-26 00:56:24 +03:00
|
|
|
, calls_number(1)
|
2016-06-25 17:24:29 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-26 02:15:39 +03:00
|
|
|
inline ::profiler::timestamp_t average_duration() const
|
|
|
|
{
|
|
|
|
return total_duration / calls_number;
|
|
|
|
}
|
|
|
|
|
2016-06-25 17:24:29 +03:00
|
|
|
}; // END of struct BlockStatistics.
|
|
|
|
|
2016-06-26 00:56:24 +03:00
|
|
|
inline void release(BlockStatistics*& _stats)
|
|
|
|
{
|
|
|
|
if (!_stats)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--_stats->calls_number == 0)
|
|
|
|
{
|
|
|
|
delete _stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
_stats = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-25 17:24:29 +03:00
|
|
|
} // END of namespace profiler.
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-19 23:01:49 +03:00
|
|
|
struct BlocksTree
|
|
|
|
{
|
2016-06-26 00:58:40 +03:00
|
|
|
typedef ::std::list<BlocksTree> children_t;
|
2016-06-19 23:57:58 +03:00
|
|
|
|
2016-06-25 17:24:29 +03:00
|
|
|
children_t children;
|
|
|
|
::profiler::SerilizedBlock* node;
|
|
|
|
::profiler::BlockStatistics* frame_statistics; ///< Pointer to statistics for this block within the parent (may be nullptr for top-level blocks)
|
2016-06-27 23:22:12 +03:00
|
|
|
::profiler::BlockStatistics* total_statistics; ///< Pointer to statistics for this block within the bounds of all frames per current thread
|
2016-08-02 21:20:38 +03:00
|
|
|
const char* thread_name;
|
2016-06-19 23:01:49 +03:00
|
|
|
|
2016-07-27 21:50:11 +03:00
|
|
|
#ifdef PROFILER_COUNT_TOTAL_CHILDREN_NUMBER
|
|
|
|
unsigned int total_children_number; ///< Number of all children including number of grandchildren (and so on)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PROFILER_COUNT_DEPTH
|
|
|
|
unsigned short depth; ///< Maximum number of sublevels (maximum children depth)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BlocksTree()
|
|
|
|
: node(nullptr)
|
|
|
|
, frame_statistics(nullptr)
|
|
|
|
, total_statistics(nullptr)
|
|
|
|
#ifdef PROFILER_COUNT_TOTAL_CHILDREN_NUMBER
|
|
|
|
, total_children_number(0)
|
|
|
|
#endif
|
|
|
|
#ifdef PROFILER_COUNT_DEPTH
|
|
|
|
, depth(0)
|
|
|
|
#endif
|
2016-07-31 22:12:11 +03:00
|
|
|
, thread_name("")
|
2016-06-19 23:46:42 +03:00
|
|
|
{
|
|
|
|
|
2016-06-19 23:01:49 +03:00
|
|
|
}
|
|
|
|
|
2016-06-25 17:30:39 +03:00
|
|
|
BlocksTree(BlocksTree&& that) : BlocksTree()
|
2016-06-19 23:01:49 +03:00
|
|
|
{
|
2016-06-25 17:24:29 +03:00
|
|
|
makeMove(::std::forward<BlocksTree&&>(that));
|
2016-06-19 23:01:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BlocksTree& operator=(BlocksTree&& that)
|
|
|
|
{
|
2016-06-25 17:24:29 +03:00
|
|
|
makeMove(::std::forward<BlocksTree&&>(that));
|
2016-06-19 23:01:49 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~BlocksTree()
|
|
|
|
{
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
delete node;
|
|
|
|
}
|
2016-06-26 00:56:24 +03:00
|
|
|
|
|
|
|
release(total_statistics);
|
|
|
|
release(frame_statistics);
|
2016-06-19 23:01:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator < (const BlocksTree& other) const
|
|
|
|
{
|
|
|
|
if (!node || !other.node)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return node->block()->getBegin() < other.node->block()->getBegin();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-06-25 17:24:29 +03:00
|
|
|
|
2016-06-19 23:01:49 +03:00
|
|
|
void makeMove(BlocksTree&& that)
|
|
|
|
{
|
2016-06-25 17:30:39 +03:00
|
|
|
if (node && node != that.node)
|
|
|
|
{
|
|
|
|
delete node;
|
|
|
|
}
|
|
|
|
|
2016-06-26 00:56:24 +03:00
|
|
|
if (total_statistics != that.total_statistics)
|
|
|
|
{
|
|
|
|
release(total_statistics);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_statistics != that.frame_statistics)
|
|
|
|
{
|
|
|
|
release(frame_statistics);
|
|
|
|
}
|
|
|
|
|
2016-06-25 17:24:29 +03:00
|
|
|
children = ::std::move(that.children);
|
2016-06-25 17:27:36 +03:00
|
|
|
node = that.node;
|
|
|
|
frame_statistics = that.frame_statistics;
|
|
|
|
total_statistics = that.total_statistics;
|
2016-08-02 21:20:38 +03:00
|
|
|
thread_name = that.thread_name;
|
2016-07-27 21:50:11 +03:00
|
|
|
|
|
|
|
#ifdef PROFILER_COUNT_TOTAL_CHILDREN_NUMBER
|
2016-06-29 20:31:17 +03:00
|
|
|
total_children_number = that.total_children_number;
|
2016-07-27 21:50:11 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PROFILER_COUNT_DEPTH
|
|
|
|
depth = that.depth;
|
|
|
|
#endif
|
2016-06-19 23:01:49 +03:00
|
|
|
|
|
|
|
that.node = nullptr;
|
2016-06-25 17:27:36 +03:00
|
|
|
that.frame_statistics = nullptr;
|
|
|
|
that.total_statistics = nullptr;
|
2016-06-19 23:01:49 +03:00
|
|
|
}
|
|
|
|
|
2016-06-25 17:24:29 +03:00
|
|
|
}; // END of struct BlocksTree.
|
2016-06-19 23:01:49 +03:00
|
|
|
|
2016-06-19 23:46:42 +03:00
|
|
|
|
2016-06-25 17:24:29 +03:00
|
|
|
typedef ::std::map<::profiler::thread_id_t, BlocksTree> thread_blocks_tree_t;
|
2016-06-19 23:46:42 +03:00
|
|
|
|
|
|
|
extern "C"{
|
2016-06-27 23:22:12 +03:00
|
|
|
unsigned int PROFILER_API fillTreesFromFile(const char* filename, thread_blocks_tree_t& threaded_trees, bool gather_statistics = false);
|
2016-06-19 23:46:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-19 23:01:49 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#endif // PROFILER_READER____H
|