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"
|
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 {
|
|
|
|
|
|
2017-08-11 11:12:49 +02:00
|
|
|
static inline quint64 supportedFeatures()
|
|
|
|
|
{
|
|
|
|
|
return Constants::QML_JS_RANGE_FEATURES | (1ULL << ProfileMemory);
|
|
|
|
|
}
|
|
|
|
|
|
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());
|
2016-09-09 10:25:46 +02:00
|
|
|
m_compileStack.append(QmlEvent());
|
|
|
|
|
m_callStackTop = &m_stackBottom;
|
|
|
|
|
m_compileStackTop = &m_stackBottom;
|
2018-03-27 15:58:43 +02:00
|
|
|
connect(modelManager, &QmlProfilerModelManager::typeDetailsFinished,
|
|
|
|
|
this, &FlameGraphModel::onTypeDetailsFinished);
|
2015-12-11 11:09:10 +01:00
|
|
|
connect(modelManager->notesModel(), &Timeline::TimelineNotesModel::changed,
|
|
|
|
|
this, [this](int typeId, int, int){loadNotes(typeId, true);});
|
2017-08-11 11:12:49 +02:00
|
|
|
m_acceptedFeatures = supportedFeatures();
|
2015-12-11 11:09:10 +01:00
|
|
|
|
2018-04-05 09:47:33 +02:00
|
|
|
modelManager->registerFeatures(m_acceptedFeatures,
|
|
|
|
|
std::bind(&FlameGraphModel::loadEvent, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2),
|
|
|
|
|
std::bind(&FlameGraphModel::beginResetModel, this),
|
|
|
|
|
std::bind(&FlameGraphModel::finalize, this),
|
|
|
|
|
std::bind(&FlameGraphModel::clear, this));
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlameGraphModel::clear()
|
|
|
|
|
{
|
2016-04-26 16:38:56 +02:00
|
|
|
beginResetModel();
|
2018-04-12 10:04:55 +02:00
|
|
|
m_stackBottom = FlameGraphData(nullptr, -1, 0);
|
2016-04-26 16:38:56 +02:00
|
|
|
m_callStack.clear();
|
2016-09-09 10:25:46 +02:00
|
|
|
m_compileStack.clear();
|
2016-04-26 16:38:56 +02:00
|
|
|
m_callStack.append(QmlEvent());
|
2016-09-09 10:25:46 +02:00
|
|
|
m_compileStack.append(QmlEvent());
|
|
|
|
|
m_callStackTop = &m_stackBottom;
|
|
|
|
|
m_compileStackTop = &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
|
|
|
{
|
2017-08-11 11:12:49 +02:00
|
|
|
if (!(m_acceptedFeatures & (1ULL << type.feature())))
|
|
|
|
|
return;
|
2016-04-26 16:38:56 +02:00
|
|
|
|
2016-09-09 10:25:46 +02:00
|
|
|
const bool isCompiling = (type.rangeType() == Compiling);
|
|
|
|
|
QStack<QmlEvent> &stack = isCompiling ? m_compileStack : m_callStack;
|
|
|
|
|
FlameGraphData *&stackTop = isCompiling ? m_compileStackTop : m_callStackTop;
|
2017-03-20 16:19:48 +01:00
|
|
|
QTC_ASSERT(stackTop, return);
|
2016-09-09 10:25:46 +02:00
|
|
|
|
2016-07-19 18:21:01 +02:00
|
|
|
if (type.message() == MemoryAllocation) {
|
|
|
|
|
if (type.detailType() == HeapPage)
|
|
|
|
|
return; // We're only interested in actual allocations, not heap pages being mmap'd
|
|
|
|
|
|
|
|
|
|
qint64 amount = event.number<qint64>(0);
|
|
|
|
|
if (amount < 0)
|
|
|
|
|
return; // We're not interested in GC runs here
|
|
|
|
|
|
2016-09-22 11:01:16 +03:00
|
|
|
for (FlameGraphData *data = stackTop; data; data = data->parent) {
|
2016-07-19 18:21:01 +02:00
|
|
|
++data->allocations;
|
|
|
|
|
data->memory += amount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (event.rangeStage() == RangeEnd) {
|
2017-03-20 16:19:48 +01:00
|
|
|
QTC_ASSERT(stackTop != &m_stackBottom, return);
|
|
|
|
|
QTC_ASSERT(stackTop->typeIndex == event.typeIndex(), return);
|
2017-02-21 11:42:32 +01:00
|
|
|
stackTop->duration += event.timestamp() - stack.top().timestamp();
|
2016-09-09 10:25:46 +02:00
|
|
|
stack.pop();
|
|
|
|
|
stackTop = stackTop->parent;
|
2016-04-28 16:13:16 +02:00
|
|
|
} else {
|
|
|
|
|
QTC_ASSERT(event.rangeStage() == RangeStart, return);
|
2016-09-09 10:25:46 +02:00
|
|
|
stack.push(event);
|
|
|
|
|
stackTop = pushChild(stackTop, event);
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
2017-03-20 16:19:48 +01:00
|
|
|
QTC_CHECK(stackTop);
|
2016-04-26 16:38:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlameGraphModel::finalize()
|
|
|
|
|
{
|
2017-02-21 10:42:18 +01:00
|
|
|
for (FlameGraphData *child : m_stackBottom.children)
|
2016-04-26 16:38:56 +02:00
|
|
|
m_stackBottom.duration += child->duration;
|
|
|
|
|
|
|
|
|
|
loadNotes(-1, false);
|
|
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 15:58:43 +02:00
|
|
|
void FlameGraphModel::onTypeDetailsFinished()
|
|
|
|
|
{
|
|
|
|
|
emit dataChanged(QModelIndex(), QModelIndex(), QVector<int>(1, DetailsRole));
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-11 11:12:49 +02:00
|
|
|
void FlameGraphModel::restrictToFeatures(quint64 visibleFeatures)
|
|
|
|
|
{
|
|
|
|
|
visibleFeatures = visibleFeatures & supportedFeatures();
|
|
|
|
|
if (visibleFeatures == m_acceptedFeatures)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_acceptedFeatures = visibleFeatures;
|
|
|
|
|
|
|
|
|
|
clear();
|
2018-04-05 09:47:33 +02:00
|
|
|
|
|
|
|
|
QFutureInterface<void> future;
|
2018-05-07 17:08:12 +02:00
|
|
|
const auto filter = m_modelManager->rangeFilter(m_modelManager->traceStart(),
|
|
|
|
|
m_modelManager->traceEnd());
|
|
|
|
|
m_modelManager->replayQmlEvents(filter(std::bind(&FlameGraphModel::loadEvent, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2)),
|
2018-04-19 13:10:49 +02:00
|
|
|
std::bind(&FlameGraphModel::beginResetModel, this),
|
|
|
|
|
std::bind(&FlameGraphModel::finalize, this),
|
|
|
|
|
[this](const QString &message) {
|
2018-05-07 13:26:05 +02:00
|
|
|
if (!message.isEmpty()) {
|
|
|
|
|
emit m_modelManager->error(tr("Could not re-read events from temporary trace file: %1")
|
|
|
|
|
.arg(message));
|
|
|
|
|
}
|
2017-08-11 11:12:49 +02:00
|
|
|
endResetModel();
|
|
|
|
|
clear();
|
2018-04-05 09:47:33 +02:00
|
|
|
}, future);
|
2017-08-11 11:12:49 +02: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;
|
2018-04-05 09:47:33 +02:00
|
|
|
Timeline::TimelineNotesModel *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;
|
2016-07-19 18:21:01 +02:00
|
|
|
case AllocationsRole: return stats.allocations;
|
|
|
|
|
case MemoryRole: return stats.memory;
|
2015-12-11 11:09:10 +01:00
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stats.typeIndex != -1) {
|
2018-03-28 09:42:28 +02:00
|
|
|
const QmlEventType &type = m_modelManager->eventType(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) :
|
2016-07-19 18:21:01 +02:00
|
|
|
duration(duration), calls(1), memory(0), allocations(0), typeIndex(typeIndex), parent(parent) {}
|
2015-12-11 11:09:10 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-02-21 10:42:18 +01:00
|
|
|
QVector<FlameGraphData *> &siblings = parent->children;
|
|
|
|
|
|
|
|
|
|
for (auto it = siblings.begin(), end = siblings.end(); it != end; ++it) {
|
|
|
|
|
FlameGraphData *child = *it;
|
2016-04-26 16:38:56 +02:00
|
|
|
if (child->typeIndex == data.typeIndex()) {
|
2015-12-11 11:09:10 +01:00
|
|
|
++child->calls;
|
2017-02-21 10:42:18 +01:00
|
|
|
for (auto back = it, front = siblings.begin(); back != front;) {
|
|
|
|
|
--back;
|
|
|
|
|
if ((*back)->calls >= (*it)->calls)
|
|
|
|
|
break;
|
|
|
|
|
qSwap(*it, *back);
|
|
|
|
|
it = back;
|
|
|
|
|
}
|
2015-12-11 11:09:10 +01:00
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 12:14:44 +01:00
|
|
|
auto 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()) {
|
2018-11-24 12:14:44 +01:00
|
|
|
auto parentData = static_cast<const FlameGraphData *>(parent.internalPointer());
|
2015-12-11 11:09:10 +01:00
|
|
|
return createIndex(row, column, parentData->children[row]);
|
|
|
|
|
} else {
|
2018-04-12 10:04:55 +02:00
|
|
|
return createIndex(row, column, row >= 0 ? m_stackBottom.children[row] : nullptr);
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex FlameGraphModel::parent(const QModelIndex &child) const
|
|
|
|
|
{
|
|
|
|
|
if (child.isValid()) {
|
2018-11-24 12:14:44 +01:00
|
|
|
auto childData = static_cast<const 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()) {
|
2018-11-24 12:14:44 +01:00
|
|
|
auto parentData = static_cast<const FlameGraphData *>(parent.internalPointer());
|
2015-12-11 11:09:10 +01:00
|
|
|
return parentData->children.count();
|
|
|
|
|
} else {
|
|
|
|
|
return m_stackBottom.children.count();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FlameGraphModel::columnCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(parent)
|
2015-12-11 11:09:10 +01:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant FlameGraphModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
2018-11-24 12:14:44 +01:00
|
|
|
auto data = static_cast<const FlameGraphData *>(index.internalPointer());
|
2015-12-11 11:09:10 +01:00
|
|
|
return lookup(data ? *data : m_stackBottom, role);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QHash<int, QByteArray> FlameGraphModel::roleNames() const
|
|
|
|
|
{
|
2016-06-17 17:33:02 +02:00
|
|
|
static QHash<int, QByteArray> extraRoles{
|
|
|
|
|
{TypeIdRole, "typeId"},
|
|
|
|
|
{TypeRole, "type"},
|
|
|
|
|
{DurationRole, "duration"},
|
|
|
|
|
{CallCountRole, "callCount"},
|
|
|
|
|
{DetailsRole, "details"},
|
|
|
|
|
{FilenameRole, "filename"},
|
|
|
|
|
{LineRole, "line"},
|
|
|
|
|
{ColumnRole, "column"},
|
|
|
|
|
{NoteRole, "note"},
|
|
|
|
|
{TimePerCallRole, "timePerCall"},
|
2016-07-19 18:21:01 +02:00
|
|
|
{RangeTypeRole, "rangeType"},
|
|
|
|
|
{LocationRole, "location" },
|
|
|
|
|
{AllocationsRole, "allocations" },
|
|
|
|
|
{MemoryRole, "memory" }
|
2016-06-17 17:33:02 +02:00
|
|
|
};
|
|
|
|
|
return QAbstractItemModel::roleNames().unite(extraRoles);
|
2015-12-11 11:09:10 +01:00
|
|
|
}
|
|
|
|
|
|
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
|