0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-27 16:51:22 +08:00

added tree widget view for profiled blocks

This commit is contained in:
Victor Zarubkin 2016-06-26 13:14:00 +03:00
parent a8df818b33
commit 6b93a6635f
3 changed files with 337 additions and 17 deletions

View File

@ -8,5 +8,5 @@ set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets REQUIRED)
add_executable(${PROJECT_NAME} main.cpp treemodel.h treemodel.cpp treeitem.h treeitem.cpp graphics_view.h)# widgets_view.h)
add_executable(${PROJECT_NAME} main.cpp treemodel.h treemodel.cpp treeitem.h treeitem.cpp graphics_view.h tree_view.h)# widgets_view.h)
target_link_libraries(${PROJECT_NAME} Qt5::Widgets easy_profiler)

View File

@ -3,6 +3,7 @@
#include <QFileSystemModel>
#include "treemodel.h"
#include "graphics_view.h"
#include "tree_view.h"
#include "profiler/reader.h"
int main(int argc, char **argv)
@ -30,6 +31,12 @@ int main(int argc, char **argv)
//
// tree->show();
int mode = 1;
switch (mode)
{
case 0:
{
const bool test = false;
if (test)
@ -46,10 +53,25 @@ int main(int argc, char **argv)
}
thread_blocks_tree_t threaded_trees;
int blocks_counter = fillTreesFromFile("test.prof", threaded_trees, false);
int blocks_counter = fillTreesFromFile("../test.prof", threaded_trees, true);
MyGraphicsView gview(threaded_trees);
gview.show();
return app.exec();
}
case 1:
{
thread_blocks_tree_t threaded_trees;
int blocks_counter = fillTreesFromFile("../test.prof", threaded_trees, true);
MyTreeWidget view(threaded_trees);
view.show();
return app.exec();
}
}
return -1;
}

298
profiler_gui/tree_view.h Normal file
View File

@ -0,0 +1,298 @@
#ifndef MY____TREE___VIEW_H
#define MY____TREE___VIEW_H
#include <QTreeWidget>
#include <QMenu>
#include <QAction>
#include <stdlib.h>
#include <unordered_map>
#include "profiler/reader.h"
//////////////////////////////////////////////////////////////////////////
template <const size_t SIZEOF_T>
struct no_hasher {
template <class T> inline size_t operator () (const T& _data) const {
return (size_t)_data;
}
};
#ifdef _WIN64
template <> struct no_hasher<8> {
template <class T> inline size_t operator () (T _data) const {
return (size_t)_data;
}
};
#endif
template <> struct no_hasher<4> {
template <class T> inline size_t operator () (T _data) const {
return (size_t)_data;
}
};
template <> struct no_hasher<2> {
template <class T> inline size_t operator () (T _data) const {
return (size_t)_data;
}
};
template <> struct no_hasher<1> {
template <class T> inline size_t operator () (T _data) const {
return (size_t)_data;
}
};
template <class T>
struct do_no_hash {
typedef no_hasher<sizeof(T)> hasher_t;
};
//////////////////////////////////////////////////////////////////////////
class MyTreeItem : public QTreeWidgetItem
{
//Q_OBJECT
const BlocksTree* m_block;
public:
MyTreeItem(const BlocksTree* _block, QTreeWidgetItem* _parent = nullptr) : QTreeWidgetItem(_parent), m_block(_block)
{
}
const BlocksTree* block() const
{
return m_block;
}
inline bool operator < (const QTreeWidgetItem& _other) const
{
const auto col = treeWidget()->sortColumn();
//switch (col)
//{
// case 1:
// case 2:
// case 3:
if (col > 0 && col < 7)
{
#ifndef _DEBUG
return data(col, Qt::UserRole).toULongLong() < _other.data(col, Qt::UserRole).toULongLong();
#else
const auto selfdata = data(col, Qt::UserRole).toULongLong();
const auto otherdata = _other.data(col, Qt::UserRole).toULongLong();
return selfdata < otherdata;
#endif
}
//}
return QTreeWidgetItem::operator < (_other);
}
void setTimeSmart(int _column, const ::profiler::timestamp_t& _time)
{
setData(_column, Qt::UserRole, _time);
setToolTip(_column, QString("%1 ns").arg(_time));
if (_time < 1e3)
{
setText(_column, QString("%1 ns").arg(_time));
}
else if (_time < 1e6)
{
setText(_column, QString("%1 us").arg(double(_time) * 1e-3, 0, 'f', 3));
}
else if (_time < 1e9)
{
setText(_column, QString("%1 ms").arg(double(_time) * 1e-6, 0, 'f', 3));
}
else
{
setText(_column, QString("%1 s").arg(double(_time) * 1e-9, 0, 'f', 3));
}
}
void setTimeMs(int _column, const ::profiler::timestamp_t& _time)
{
setData(_column, Qt::UserRole, _time);
setToolTip(_column, QString("%1 ns").arg(_time));
setText(_column, QString::number(double(_time) * 1e-6, 'g', 9));
}
};
//////////////////////////////////////////////////////////////////////////
class ItemAction : public QAction
{
Q_OBJECT
private:
MyTreeItem* m_item;
public:
ItemAction(const char* _label, MyTreeItem* _item) : QAction(_label, nullptr), m_item(_item)
{
connect(this, &QAction::triggered, this, &ItemAction::onToggle);
}
private:
void onToggle(bool)
{
emit clicked(m_item);
}
signals:
void clicked(MyTreeItem* _item);
};
//////////////////////////////////////////////////////////////////////////
class MyTreeWidget : public QTreeWidget
{
Q_OBJECT
protected:
typedef ::std::unordered_map<const ::profiler::SerilizedBlock*, MyTreeItem*, do_no_hash<const ::profiler::SerilizedBlock*>::hasher_t> BlockItemMap;
BlockItemMap m_itemblocks;
::profiler::timestamp_t m_beginTime;
public:
MyTreeWidget(const thread_blocks_tree_t& _blocksTree, QWidget* _parent = nullptr) : QTreeWidget(_parent), m_beginTime(-1)
{
setAlternatingRowColors(true);
setItemsExpandable(true);
setAnimated(true);
setSortingEnabled(true);
setColumnCount(10);
auto header = new QTreeWidgetItem();
header->setText(0, "Name");
header->setText(1, "Duration");
header->setText(2, "Begin, ms");
header->setText(3, "End, ms");
header->setText(4, "Min dur.");
header->setText(5, "Max dur.");
header->setText(6, "Average dur.");
header->setText(7, "N Calls");
setHeaderItem(header);
setTreeInternal(_blocksTree);
setBaseSize(640, 480);
}
void setTree(const thread_blocks_tree_t& _blocksTree)
{
m_itemblocks.clear();
clear();
setTreeInternal(_blocksTree);
}
protected:
void setTreeInternal(const thread_blocks_tree_t& _blocksTree)
{
for (const auto& threadTree : _blocksTree)
{
const auto beginTime = threadTree.second.children.front().node->block()->getBegin();
if (m_beginTime > beginTime)
{
m_beginTime = beginTime;
}
}
for (const auto& threadTree : _blocksTree)
{
auto item = new MyTreeItem(&threadTree.second);
item->setText(0, QString("Thread %1").arg(threadTree.first));
setTreeInternal(threadTree.second.children, item);
addTopLevelItem(item);
}
}
void setTreeInternal(const BlocksTree::children_t& _children, MyTreeItem* _parent)
{
for (const auto& child : _children)
{
const auto duration = child.node->block()->duration();
const auto beginTime = child.node->block()->getBegin() - m_beginTime;
const auto endTime = child.node->block()->getEnd() - m_beginTime;
auto item = new MyTreeItem(&child, _parent);
item->setText(0, child.node->getBlockName());
item->setTimeSmart(1, child.node->block()->duration());
item->setTimeMs(2, child.node->block()->getBegin() - m_beginTime);
item->setTimeMs(3, child.node->block()->getEnd() - m_beginTime);
if (child.total_statistics)
{
item->setTimeSmart(4, child.total_statistics->min_duration);
item->setTimeSmart(5, child.total_statistics->max_duration);
item->setTimeSmart(6, child.total_statistics->average_duration());
item->setText(7, QString::number(child.total_statistics->calls_number));
}
m_itemblocks[child.node] = item;
if (!child.children.empty())
{
setTreeInternal(child.children, item);
}
}
}
void contextMenuEvent(QContextMenuEvent* event)
{
const auto col = currentColumn();
auto item = static_cast<MyTreeItem*>(currentItem());
if (item && col > 0)
{
QMenu menu;
switch (col)
{
case 5:
{
auto action = new ItemAction("Jump to item with max duration", item);
connect(action, &ItemAction::clicked, this, &MyTreeWidget::onJumpToMaxItemClicked);
menu.addAction(action);
break;
}
}
menu.exec(QCursor::pos());
event->accept();
}
}
private slots:
void onJumpToMaxItemClicked(MyTreeItem* _item)
{
auto it = m_itemblocks.find(_item->block()->total_statistics->max_duration_block);
if (it != m_itemblocks.end())
{
setCurrentItem(it->second);
scrollToItem(it->second);
}
}
};
//////////////////////////////////////////////////////////////////////////
#endif // MY____TREE___VIEW_H