2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2014-05-27 16:30:48 +02:00
|
|
|
|
|
|
|
|
#include "memoryusagemodel.h"
|
2016-05-02 12:18:57 +02:00
|
|
|
#include "qmlprofilereventtypes.h"
|
2022-08-30 20:37:31 +02:00
|
|
|
#include "qmlprofilermodelmanager.h"
|
|
|
|
|
#include "qmlprofilertr.h"
|
2014-05-27 16:30:48 +02:00
|
|
|
|
2016-06-03 15:50:32 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2016-04-26 10:21:00 +02:00
|
|
|
namespace QmlProfiler {
|
2014-05-27 16:30:48 +02:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2018-03-28 15:40:13 +02:00
|
|
|
MemoryUsageModel::MemoryUsageModel(QmlProfilerModelManager *manager,
|
|
|
|
|
Timeline::TimelineModelAggregator *parent) :
|
2022-09-19 15:34:52 +03:00
|
|
|
QmlProfilerTimelineModel(manager, MemoryAllocation, UndefinedRangeType, ProfileMemory, parent)
|
2014-05-27 16:30:48 +02:00
|
|
|
{
|
2018-04-05 09:47:33 +02:00
|
|
|
// Register additional features. The base class already registers the main feature.
|
|
|
|
|
// Don't register initializer, finalizer, or clearer as the base class has done so already.
|
|
|
|
|
modelManager()->registerFeatures(Constants::QML_JS_RANGE_FEATURES ^ (1 << ProfileCompiling),
|
|
|
|
|
std::bind(&QmlProfilerTimelineModel::loadEvent, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2014-09-08 18:33:02 +02:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 13:31:58 +01:00
|
|
|
qint64 MemoryUsageModel::rowMaxValue(int rowNumber) const
|
2014-06-24 11:53:19 +02:00
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(rowNumber)
|
2014-10-27 19:54:23 +01:00
|
|
|
return m_maxSize;
|
2014-06-24 11:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-17 13:30:53 +01:00
|
|
|
int MemoryUsageModel::expandedRow(int index) const
|
2014-05-27 16:30:48 +02:00
|
|
|
{
|
2014-10-28 14:30:04 +01:00
|
|
|
int type = selectionId(index);
|
2016-05-02 12:18:57 +02:00
|
|
|
return (type == HeapPage || type == LargeItem) ? 1 : 2;
|
2014-11-17 13:30:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int MemoryUsageModel::collapsedRow(int index) const
|
|
|
|
|
{
|
|
|
|
|
return expandedRow(index);
|
2014-05-27 16:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-28 14:30:04 +01:00
|
|
|
int MemoryUsageModel::typeId(int index) const
|
2014-05-27 16:30:48 +02:00
|
|
|
{
|
2014-10-28 14:30:04 +01:00
|
|
|
return m_data[index].typeId;
|
2014-05-27 16:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-08 18:11:36 +01:00
|
|
|
QRgb MemoryUsageModel::color(int index) const
|
2014-05-27 16:30:48 +02:00
|
|
|
{
|
2014-08-29 17:12:11 +02:00
|
|
|
return colorBySelectionId(index);
|
2014-05-27 16:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-02 12:30:40 +02:00
|
|
|
float MemoryUsageModel::relativeHeight(int index) const
|
2014-05-27 16:30:48 +02:00
|
|
|
{
|
2014-10-27 19:54:23 +01:00
|
|
|
return qMin(1.0f, (float)m_data[index].size / (float)m_maxSize);
|
2014-05-27 16:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-08 14:53:47 +02:00
|
|
|
QVariantMap MemoryUsageModel::location(int index) const
|
2014-06-24 11:53:47 +02:00
|
|
|
{
|
2016-06-03 14:02:51 +02:00
|
|
|
return locationFromTypeId(index);
|
2014-06-24 11:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-08 14:53:47 +02:00
|
|
|
QVariantList MemoryUsageModel::labels() const
|
2014-05-27 16:30:48 +02:00
|
|
|
{
|
|
|
|
|
QVariantList result;
|
|
|
|
|
|
2014-10-29 10:42:22 +01:00
|
|
|
QVariantMap element;
|
2022-08-30 20:37:31 +02:00
|
|
|
element.insert(QLatin1String("description"), Tr::tr("Memory Allocation"));
|
2016-06-03 14:02:51 +02:00
|
|
|
element.insert(QLatin1String("id"), HeapPage);
|
2014-10-29 10:42:22 +01:00
|
|
|
result << element;
|
|
|
|
|
|
|
|
|
|
element.clear();
|
2022-08-30 20:37:31 +02:00
|
|
|
element.insert(QLatin1String("description"), Tr::tr("Memory Usage"));
|
2016-06-03 14:02:51 +02:00
|
|
|
element.insert(QLatin1String("id"), SmallItem);
|
2014-10-29 10:42:22 +01:00
|
|
|
result << element;
|
2014-05-27 16:30:48 +02:00
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 18:02:14 +02:00
|
|
|
static int toSameSignedInt(qint64 number)
|
|
|
|
|
{
|
|
|
|
|
if (number > std::numeric_limits<int>::max())
|
|
|
|
|
return std::numeric_limits<int>::max();
|
|
|
|
|
if (number < std::numeric_limits<int>::min())
|
|
|
|
|
return std::numeric_limits<int>::min();
|
|
|
|
|
return static_cast<int>(number);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-07 14:41:44 +02:00
|
|
|
QVariantMap MemoryUsageModel::details(int index) const
|
2014-05-27 16:30:48 +02:00
|
|
|
{
|
2014-07-07 14:41:44 +02:00
|
|
|
QVariantMap result;
|
2018-05-09 09:27:07 +02:00
|
|
|
const Item *ev = &m_data[index];
|
2014-05-27 16:30:48 +02:00
|
|
|
|
2014-07-11 11:01:47 +02:00
|
|
|
if (ev->allocated >= -ev->deallocated)
|
2022-08-30 20:37:31 +02:00
|
|
|
result.insert(QLatin1String("displayName"), Tr::tr("Memory Allocated"));
|
2014-07-11 10:09:08 +02:00
|
|
|
else
|
2022-08-30 20:37:31 +02:00
|
|
|
result.insert(QLatin1String("displayName"), Tr::tr("Memory Freed"));
|
2014-05-27 16:30:48 +02:00
|
|
|
|
2022-08-30 20:37:31 +02:00
|
|
|
result.insert(Tr::tr("Total"), Tr::tr("%n byte(s)", nullptr, toSameSignedInt(ev->size)));
|
2014-07-11 11:01:47 +02:00
|
|
|
if (ev->allocations > 0) {
|
2022-08-30 20:37:31 +02:00
|
|
|
result.insert(Tr::tr("Allocated"), Tr::tr("%n byte(s)", nullptr, toSameSignedInt(ev->allocated)));
|
|
|
|
|
result.insert(Tr::tr("Allocations"), ev->allocations);
|
2014-07-11 11:01:47 +02:00
|
|
|
}
|
|
|
|
|
if (ev->deallocations > 0) {
|
2022-08-30 20:37:31 +02:00
|
|
|
result.insert(Tr::tr("Deallocated"),
|
|
|
|
|
Tr::tr("%n byte(s)", nullptr, toSameSignedInt(-ev->deallocated)));
|
|
|
|
|
result.insert(Tr::tr("Deallocations"), ev->deallocations);
|
2014-07-11 11:01:47 +02:00
|
|
|
}
|
2016-06-03 14:02:51 +02:00
|
|
|
QString memoryTypeName;
|
|
|
|
|
switch (selectionId(index)) {
|
2022-08-30 20:37:31 +02:00
|
|
|
case HeapPage: memoryTypeName = Tr::tr("Heap Allocation"); break;
|
|
|
|
|
case LargeItem: memoryTypeName = Tr::tr("Large Item Allocation"); break;
|
|
|
|
|
case SmallItem: memoryTypeName = Tr::tr("Heap Usage"); break;
|
2016-06-03 14:02:51 +02:00
|
|
|
default: Q_UNREACHABLE();
|
|
|
|
|
}
|
2022-08-30 20:37:31 +02:00
|
|
|
result.insert(Tr::tr("Type"), memoryTypeName);
|
2014-07-07 14:41:44 +02:00
|
|
|
|
2022-08-30 20:37:31 +02:00
|
|
|
result.insert(Tr::tr("Location"), modelManager()->eventType(ev->typeId).displayName());
|
2014-05-27 16:30:48 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 13:23:35 +02:00
|
|
|
void MemoryUsageModel::loadEvent(const QmlEvent &event, const QmlEventType &type)
|
|
|
|
|
{
|
2016-06-06 19:51:55 +02:00
|
|
|
if (type.message() != MemoryAllocation) {
|
2022-09-19 15:34:52 +03:00
|
|
|
if (type.rangeType() != UndefinedRangeType) {
|
2017-03-20 16:19:48 +01:00
|
|
|
m_continuation = ContinueNothing;
|
2016-04-28 16:13:16 +02:00
|
|
|
if (event.rangeStage() == RangeStart)
|
|
|
|
|
m_rangeStack.push(RangeStackFrame(event.typeIndex(), event.timestamp()));
|
2017-03-20 16:19:48 +01:00
|
|
|
else if (event.rangeStage() == RangeEnd) {
|
|
|
|
|
QTC_ASSERT(!m_rangeStack.isEmpty(), return);
|
|
|
|
|
QTC_ASSERT(m_rangeStack.top().originTypeIndex == event.typeIndex(), return);
|
2016-04-28 16:13:16 +02:00
|
|
|
m_rangeStack.pop();
|
2017-03-20 16:19:48 +01:00
|
|
|
}
|
2014-06-24 11:53:47 +02:00
|
|
|
}
|
2016-04-26 13:23:35 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2014-05-27 16:30:48 +02:00
|
|
|
|
2016-06-03 15:50:32 +02:00
|
|
|
auto canContinue = [&](EventContinuation continuation) {
|
|
|
|
|
QTC_ASSERT(continuation != ContinueNothing, return false);
|
|
|
|
|
if ((m_continuation & continuation) == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
int currentIndex = (continuation == ContinueAllocation ? m_currentJSHeapIndex :
|
|
|
|
|
m_currentUsageIndex);
|
|
|
|
|
|
|
|
|
|
if (m_rangeStack.isEmpty()) {
|
|
|
|
|
qint64 amount = event.number<qint64>(0);
|
|
|
|
|
// outside of ranges show monotonous allocation or deallocation
|
|
|
|
|
return (amount >= 0 && m_data[currentIndex].allocated >= 0)
|
|
|
|
|
|| (amount < 0 && m_data[currentIndex].deallocated > 0);
|
|
|
|
|
} else {
|
|
|
|
|
return m_data[currentIndex].typeId == m_rangeStack.top().originTypeIndex
|
|
|
|
|
&& m_rangeStack.top().startTime < startTime(currentIndex);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-06 19:51:55 +02:00
|
|
|
if (type.detailType() == SmallItem || type.detailType() == LargeItem) {
|
2016-06-03 15:50:32 +02:00
|
|
|
if (canContinue(ContinueUsage)) {
|
2016-04-26 13:23:35 +02:00
|
|
|
m_data[m_currentUsageIndex].update(event.number<qint64>(0));
|
|
|
|
|
m_currentUsage = m_data[m_currentUsageIndex].size;
|
|
|
|
|
} else {
|
2018-05-09 09:27:07 +02:00
|
|
|
Item allocation(
|
2016-05-26 09:27:59 +02:00
|
|
|
m_rangeStack.empty() ? event.typeIndex() :
|
|
|
|
|
m_rangeStack.top().originTypeIndex,
|
|
|
|
|
m_currentUsage);
|
2016-04-26 13:23:35 +02:00
|
|
|
allocation.update(event.number<qint64>(0));
|
|
|
|
|
m_currentUsage = allocation.size;
|
|
|
|
|
|
|
|
|
|
if (m_currentUsageIndex != -1) {
|
2016-07-05 13:04:40 +02:00
|
|
|
qint64 duration = event.timestamp() - startTime(m_currentUsageIndex);
|
|
|
|
|
if (duration > 0) {
|
|
|
|
|
insertEnd(m_currentUsageIndex, duration - 1);
|
|
|
|
|
m_currentUsageIndex = insertStart(event.timestamp(), SmallItem);
|
|
|
|
|
m_data.insert(m_currentUsageIndex, allocation);
|
|
|
|
|
} else {
|
|
|
|
|
// Ignore ranges of 0 duration. We only need to keep track of the sizes.
|
|
|
|
|
m_data[m_currentUsageIndex] = allocation;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m_currentUsageIndex = insertStart(event.timestamp(), SmallItem);
|
|
|
|
|
m_data.insert(m_currentUsageIndex, allocation);
|
2014-05-27 16:30:48 +02:00
|
|
|
}
|
2016-06-03 15:50:32 +02:00
|
|
|
m_continuation = m_continuation | ContinueUsage;
|
2014-05-27 16:30:48 +02:00
|
|
|
}
|
2016-04-26 13:23:35 +02:00
|
|
|
}
|
2014-05-27 16:30:48 +02:00
|
|
|
|
2016-06-06 19:51:55 +02:00
|
|
|
if (type.detailType() == HeapPage || type.detailType() == LargeItem) {
|
2016-06-03 15:50:32 +02:00
|
|
|
if (canContinue(ContinueAllocation)
|
2016-06-06 19:51:55 +02:00
|
|
|
&& type.detailType() == selectionId(m_currentJSHeapIndex)) {
|
2016-04-26 13:23:35 +02:00
|
|
|
m_data[m_currentJSHeapIndex].update(event.number<qint64>(0));
|
|
|
|
|
m_currentSize = m_data[m_currentJSHeapIndex].size;
|
|
|
|
|
} else {
|
2018-05-09 09:27:07 +02:00
|
|
|
Item allocation(
|
2016-05-26 09:27:59 +02:00
|
|
|
m_rangeStack.empty() ? event.typeIndex() :
|
|
|
|
|
m_rangeStack.top().originTypeIndex,
|
|
|
|
|
m_currentSize);
|
2016-04-26 13:23:35 +02:00
|
|
|
allocation.update(event.number<qint64>(0));
|
|
|
|
|
m_currentSize = allocation.size;
|
|
|
|
|
|
|
|
|
|
if (m_currentSize > m_maxSize)
|
|
|
|
|
m_maxSize = m_currentSize;
|
2016-07-05 13:04:40 +02:00
|
|
|
|
|
|
|
|
if (m_currentJSHeapIndex != -1) {
|
|
|
|
|
qint64 duration = event.timestamp() - startTime(m_currentJSHeapIndex);
|
|
|
|
|
if (duration > 0){
|
|
|
|
|
insertEnd(m_currentJSHeapIndex, duration - 1);
|
|
|
|
|
m_currentJSHeapIndex = insertStart(event.timestamp(), type.detailType());
|
|
|
|
|
m_data.insert(m_currentJSHeapIndex, allocation);
|
|
|
|
|
} else {
|
|
|
|
|
// Ignore ranges of 0 duration. We only need to keep track of the sizes.
|
|
|
|
|
m_data[m_currentJSHeapIndex] = allocation;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m_currentJSHeapIndex = insertStart(event.timestamp(), type.detailType());
|
|
|
|
|
m_data.insert(m_currentJSHeapIndex, allocation);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-03 15:50:32 +02:00
|
|
|
m_continuation = m_continuation | ContinueAllocation;
|
2014-05-27 16:30:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-26 13:23:35 +02:00
|
|
|
}
|
2014-05-27 16:30:48 +02:00
|
|
|
|
2016-04-26 13:23:35 +02:00
|
|
|
void MemoryUsageModel::finalize()
|
|
|
|
|
{
|
2018-03-28 08:57:46 +02:00
|
|
|
if (m_currentJSHeapIndex != -1) {
|
|
|
|
|
insertEnd(m_currentJSHeapIndex,
|
|
|
|
|
modelManager()->traceEnd() - startTime(m_currentJSHeapIndex));
|
|
|
|
|
}
|
2016-04-26 13:23:35 +02:00
|
|
|
if (m_currentUsageIndex != -1)
|
2018-03-28 08:57:46 +02:00
|
|
|
insertEnd(m_currentUsageIndex, modelManager()->traceEnd() - startTime(m_currentUsageIndex));
|
2014-05-27 16:30:48 +02:00
|
|
|
|
|
|
|
|
|
2014-08-26 12:57:29 +02:00
|
|
|
computeNesting();
|
2014-10-27 19:54:23 +01:00
|
|
|
setExpandedRowCount(3);
|
|
|
|
|
setCollapsedRowCount(3);
|
2018-04-05 09:47:33 +02:00
|
|
|
QmlProfilerTimelineModel::finalize();
|
2014-05-27 16:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MemoryUsageModel::clear()
|
|
|
|
|
{
|
2014-10-27 19:54:23 +01:00
|
|
|
m_data.clear();
|
|
|
|
|
m_maxSize = 1;
|
2016-04-26 13:23:35 +02:00
|
|
|
m_currentSize = 0;
|
|
|
|
|
m_currentUsage = 0;
|
|
|
|
|
m_currentUsageIndex = -1;
|
|
|
|
|
m_currentJSHeapIndex = -1;
|
2016-06-03 15:50:32 +02:00
|
|
|
m_continuation = ContinueNothing;
|
2016-04-26 13:23:35 +02:00
|
|
|
m_rangeStack.clear();
|
2014-10-29 10:31:28 +01:00
|
|
|
QmlProfilerTimelineModel::clear();
|
2014-05-27 16:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-09 10:34:39 +02:00
|
|
|
bool MemoryUsageModel::handlesTypeId(int typeId) const
|
|
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(typeId)
|
2016-09-09 10:34:39 +02:00
|
|
|
// We don't want the memory ranges allocated by some QML/JS function to be highlighted when
|
|
|
|
|
// propagating a typeId selection to the timeline. The actual range should be highlighted.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 09:27:07 +02:00
|
|
|
MemoryUsageModel::Item::Item(int typeId, qint64 baseAmount) :
|
2016-05-26 09:27:59 +02:00
|
|
|
size(baseAmount), allocated(0), deallocated(0), allocations(0), deallocations(0),
|
|
|
|
|
typeId(typeId)
|
2014-07-11 11:01:47 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 09:27:07 +02:00
|
|
|
void MemoryUsageModel::Item::update(qint64 amount)
|
2014-07-11 11:01:47 +02:00
|
|
|
{
|
|
|
|
|
size += amount;
|
|
|
|
|
if (amount < 0) {
|
|
|
|
|
deallocated += amount;
|
|
|
|
|
++deallocations;
|
|
|
|
|
} else {
|
|
|
|
|
allocated += amount;
|
|
|
|
|
++allocations;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 16:30:48 +02:00
|
|
|
|
|
|
|
|
} // namespace Internal
|
2016-04-26 10:21:00 +02:00
|
|
|
} // namespace QmlProfiler
|