2015-12-11 11:09:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-14 11:03:04 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-12-11 11:09:10 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-14 11:03:04 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-12-11 11:09:10 +01:00
|
|
|
**
|
2016-01-14 11:03:04 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-12-11 11:09:10 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "flamegraphmodel.h"
|
|
|
|
|
|
2016-04-28 16:02:54 +02:00
|
|
|
#include "qmlprofilermodelmanager.h"
|
|
|
|
|
#include "qmlprofilerdatamodel.h"
|
2015-12-11 11:09:10 +01:00
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QVector>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QQueue>
|
|
|
|
|
#include <QSet>
|
|
|
|
|
|
2016-04-26 10:21:00 +02:00
|
|
|
namespace QmlProfiler {
|
2015-12-11 11:09:10 +01:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2016-04-26 10:21:00 +02:00
|
|
|
FlameGraphModel::FlameGraphModel(QmlProfilerModelManager *modelManager,
|
2015-12-11 11:09:10 +01:00
|
|
|
QObject *parent) : QAbstractItemModel(parent)
|
|
|
|
|
{
|
|
|
|
|
m_modelManager = modelManager;
|
2016-04-26 16:38:56 +02:00
|
|
|
m_callStack.append(QmlEvent());
|
|
|
|
|
m_stackTop = &m_stackBottom;
|
|
|
|
|
connect(modelManager, &QmlProfilerModelManager::stateChanged,
|
|
|
|
|
this, &FlameGraphModel::onModelManagerStateChanged);
|
2015-12-11 11:09:10 +01:00
|
|
|
connect(modelManager->notesModel(), &Timeline::TimelineNotesModel::changed,
|
|
|
|
|
this, [this](int typeId, int, int){loadNotes(typeId, true);});
|
|
|
|
|
m_modelId = modelManager->registerModelProxy();
|
|
|
|
|
|
2016-04-28 16:19:17 +02:00
|
|
|
modelManager->announceFeatures(Constants::QML_JS_RANGE_FEATURES,
|
|
|
|
|
[this](const QmlEvent &event, const QmlEventType &type) {
|
|
|
|
|
loadEvent(event, type);
|
|
|
|
|
}, [this](){
|
|
|
|
|
finalize();
|
|
|
|
|
});
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlameGraphModel::clear()
|
|
|
|
|
{
|
2016-04-26 16:38:56 +02:00
|
|
|
beginResetModel();
|
2016-04-28 16:13:16 +02:00
|
|
|
m_stackBottom = FlameGraphData(0, -1, 1);
|
2016-04-26 16:38:56 +02:00
|
|
|
m_callStack.clear();
|
|
|
|
|
m_callStack.append(QmlEvent());
|
|
|
|
|
m_stackTop = &m_stackBottom;
|
2015-12-11 11:09:10 +01:00
|
|
|
m_typeIdsWithNotes.clear();
|
2016-04-26 16:38:56 +02:00
|
|
|
endResetModel();
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlameGraphModel::loadNotes(int typeIndex, bool emitSignal)
|
|
|
|
|
{
|
|
|
|
|
QSet<int> changedNotes;
|
|
|
|
|
Timeline::TimelineNotesModel *notes = m_modelManager->notesModel();
|
|
|
|
|
if (typeIndex == -1) {
|
|
|
|
|
changedNotes = m_typeIdsWithNotes;
|
|
|
|
|
m_typeIdsWithNotes.clear();
|
|
|
|
|
for (int i = 0; i < notes->count(); ++i)
|
|
|
|
|
m_typeIdsWithNotes.insert(notes->typeId(i));
|
|
|
|
|
changedNotes += m_typeIdsWithNotes;
|
|
|
|
|
} else {
|
|
|
|
|
changedNotes.insert(typeIndex);
|
|
|
|
|
if (notes->byTypeId(typeIndex).isEmpty())
|
|
|
|
|
m_typeIdsWithNotes.remove(typeIndex);
|
|
|
|
|
else
|
|
|
|
|
m_typeIdsWithNotes.insert(typeIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 11:12:12 +01:00
|
|
|
if (emitSignal)
|
2016-05-02 12:18:57 +02:00
|
|
|
emit dataChanged(QModelIndex(), QModelIndex(), QVector<int>() << NoteRole);
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-26 16:38:56 +02:00
|
|
|
void FlameGraphModel::loadEvent(const QmlEvent &event, const QmlEventType &type)
|
2015-12-11 11:09:10 +01:00
|
|
|
{
|
2016-05-31 14:50:16 +02:00
|
|
|
Q_UNUSED(type);
|
2016-04-26 16:38:56 +02:00
|
|
|
|
|
|
|
|
if (m_stackBottom.children.isEmpty())
|
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
|
|
const QmlEvent *potentialParent = &(m_callStack.top());
|
2016-04-28 16:13:16 +02:00
|
|
|
if (event.rangeStage() == RangeEnd) {
|
|
|
|
|
m_stackTop->duration += event.timestamp() - potentialParent->timestamp();
|
2016-04-26 16:38:56 +02:00
|
|
|
m_callStack.pop();
|
|
|
|
|
m_stackTop = m_stackTop->parent;
|
|
|
|
|
potentialParent = &(m_callStack.top());
|
2016-04-28 16:13:16 +02:00
|
|
|
} else {
|
|
|
|
|
QTC_ASSERT(event.rangeStage() == RangeStart, return);
|
|
|
|
|
m_callStack.push(event);
|
|
|
|
|
m_stackTop = pushChild(m_stackTop, event);
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
2016-04-26 16:38:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlameGraphModel::finalize()
|
|
|
|
|
{
|
|
|
|
|
foreach (FlameGraphData *child, m_stackBottom.children)
|
|
|
|
|
m_stackBottom.duration += child->duration;
|
|
|
|
|
|
|
|
|
|
loadNotes(-1, false);
|
|
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlameGraphModel::onModelManagerStateChanged()
|
|
|
|
|
{
|
|
|
|
|
if (m_modelManager->state() == QmlProfilerModelManager::ClearingData)
|
|
|
|
|
clear();
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
2016-05-02 12:18:57 +02:00
|
|
|
static QString nameForType(RangeType typeNumber)
|
2015-12-11 11:09:10 +01:00
|
|
|
{
|
|
|
|
|
switch (typeNumber) {
|
2016-05-02 12:18:57 +02:00
|
|
|
case Compiling: return FlameGraphModel::tr("Compile");
|
|
|
|
|
case Creating: return FlameGraphModel::tr("Create");
|
|
|
|
|
case Binding: return FlameGraphModel::tr("Binding");
|
|
|
|
|
case HandlingSignal: return FlameGraphModel::tr("Signal");
|
|
|
|
|
case Javascript: return FlameGraphModel::tr("JavaScript");
|
2016-05-31 14:50:16 +02:00
|
|
|
default: Q_UNREACHABLE();
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant FlameGraphModel::lookup(const FlameGraphData &stats, int role) const
|
|
|
|
|
{
|
|
|
|
|
switch (role) {
|
2016-05-02 12:18:57 +02:00
|
|
|
case TypeIdRole: return stats.typeIndex;
|
|
|
|
|
case NoteRole: {
|
2015-12-11 11:09:10 +01:00
|
|
|
QString ret;
|
|
|
|
|
if (!m_typeIdsWithNotes.contains(stats.typeIndex))
|
|
|
|
|
return ret;
|
2016-04-26 10:21:00 +02:00
|
|
|
QmlProfilerNotesModel *notes = m_modelManager->notesModel();
|
2015-12-11 11:09:10 +01:00
|
|
|
foreach (const QVariant &item, notes->byTypeId(stats.typeIndex)) {
|
|
|
|
|
if (ret.isEmpty())
|
2016-03-02 11:12:12 +01:00
|
|
|
ret = notes->text(item.toInt());
|
2015-12-11 11:09:10 +01:00
|
|
|
else
|
2016-03-02 11:12:12 +01:00
|
|
|
ret += QChar::LineFeed + notes->text(item.toInt());
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2016-05-02 12:18:57 +02:00
|
|
|
case DurationRole: return stats.duration;
|
|
|
|
|
case CallCountRole: return stats.calls;
|
|
|
|
|
case TimePerCallRole: return stats.duration / stats.calls;
|
|
|
|
|
case TimeInPercentRole: return stats.duration * 100 / m_stackBottom.duration;
|
2015-12-11 11:09:10 +01:00
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stats.typeIndex != -1) {
|
2016-04-26 11:50:59 +02:00
|
|
|
const QVector<QmlEventType> &typeList = m_modelManager->qmlModel()->eventTypes();
|
|
|
|
|
const QmlEventType &type = typeList[stats.typeIndex];
|
2015-12-11 11:09:10 +01:00
|
|
|
|
|
|
|
|
switch (role) {
|
2016-06-06 19:51:55 +02:00
|
|
|
case FilenameRole: return type.location().filename();
|
|
|
|
|
case LineRole: return type.location().line();
|
|
|
|
|
case ColumnRole: return type.location().column();
|
|
|
|
|
case TypeRole: return nameForType(type.rangeType());
|
|
|
|
|
case RangeTypeRole: return type.rangeType();
|
|
|
|
|
case DetailsRole: return type.data().isEmpty() ?
|
|
|
|
|
FlameGraphModel::tr("Source code not available") : type.data();
|
|
|
|
|
case LocationRole: return type.displayName();
|
2015-12-11 11:09:10 +01:00
|
|
|
default: return QVariant();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FlameGraphData::FlameGraphData(FlameGraphData *parent, int typeIndex, qint64 duration) :
|
|
|
|
|
duration(duration), calls(1), typeIndex(typeIndex), parent(parent) {}
|
|
|
|
|
|
|
|
|
|
FlameGraphData::~FlameGraphData()
|
|
|
|
|
{
|
|
|
|
|
qDeleteAll(children);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 16:38:56 +02:00
|
|
|
FlameGraphData *FlameGraphModel::pushChild(FlameGraphData *parent, const QmlEvent &data)
|
2015-12-11 11:09:10 +01:00
|
|
|
{
|
|
|
|
|
foreach (FlameGraphData *child, parent->children) {
|
2016-04-26 16:38:56 +02:00
|
|
|
if (child->typeIndex == data.typeIndex()) {
|
2015-12-11 11:09:10 +01:00
|
|
|
++child->calls;
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 16:13:16 +02:00
|
|
|
FlameGraphData *child = new FlameGraphData(parent, data.typeIndex());
|
2015-12-11 11:09:10 +01:00
|
|
|
parent->children.append(child);
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex FlameGraphModel::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
if (parent.isValid()) {
|
|
|
|
|
FlameGraphData *parentData = static_cast<FlameGraphData *>(parent.internalPointer());
|
|
|
|
|
return createIndex(row, column, parentData->children[row]);
|
|
|
|
|
} else {
|
|
|
|
|
return createIndex(row, column, row >= 0 ? m_stackBottom.children[row] : 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex FlameGraphModel::parent(const QModelIndex &child) const
|
|
|
|
|
{
|
|
|
|
|
if (child.isValid()) {
|
|
|
|
|
FlameGraphData *childData = static_cast<FlameGraphData *>(child.internalPointer());
|
2016-05-30 17:38:45 +02:00
|
|
|
return childData->parent == &m_stackBottom ? QModelIndex() :
|
|
|
|
|
createIndex(0, 0, childData->parent);
|
2015-12-11 11:09:10 +01:00
|
|
|
} else {
|
|
|
|
|
return QModelIndex();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FlameGraphModel::rowCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
if (parent.isValid()) {
|
|
|
|
|
FlameGraphData *parentData = static_cast<FlameGraphData *>(parent.internalPointer());
|
|
|
|
|
return parentData->children.count();
|
|
|
|
|
} else {
|
|
|
|
|
return m_stackBottom.children.count();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FlameGraphModel::columnCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant FlameGraphModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
FlameGraphData *data = static_cast<FlameGraphData *>(index.internalPointer());
|
|
|
|
|
return lookup(data ? *data : m_stackBottom, role);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QHash<int, QByteArray> FlameGraphModel::roleNames() const
|
|
|
|
|
{
|
|
|
|
|
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
|
2016-05-02 12:18:57 +02:00
|
|
|
names[TypeIdRole] = "typeId";
|
|
|
|
|
names[TypeRole] = "type";
|
|
|
|
|
names[DurationRole] = "duration";
|
|
|
|
|
names[CallCountRole] = "callCount";
|
|
|
|
|
names[DetailsRole] = "details";
|
|
|
|
|
names[FilenameRole] = "filename";
|
|
|
|
|
names[LineRole] = "line";
|
|
|
|
|
names[ColumnRole] = "column";
|
|
|
|
|
names[NoteRole] = "note";
|
|
|
|
|
names[TimePerCallRole] = "timePerCall";
|
|
|
|
|
names[TimeInPercentRole] = "timeInPercent";
|
|
|
|
|
names[RangeTypeRole] = "rangeType";
|
2015-12-11 11:09:10 +01:00
|
|
|
return names;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 16:13:16 +02:00
|
|
|
QmlProfilerModelManager *FlameGraphModel::modelManager() const
|
|
|
|
|
{
|
|
|
|
|
return m_modelManager;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-11 11:09:10 +01:00
|
|
|
} // namespace Internal
|
2016-04-26 10:21:00 +02:00
|
|
|
} // namespace QmlProfiler
|