aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/qmlprofilerextension/memoryusagemodel.cpp
blob: 5085b0bfbe238467ab4b247e924331d4ebbb6a91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com <http://qt.digia.com/>
**
** This file is part of the Qt Enterprise Qt Quick Profiler Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com <http://qt.digia.com/>
**
****************************************************************************/

#include "memoryusagemodel.h"
#include "qmldebug/qmlprofilereventtypes.h"
#include "qmlprofiler/qmlprofilermodelmanager.h"
#include "qmlprofiler/sortedtimelinemodel.h"
#include "qmlprofiler/abstracttimelinemodel_p.h"

#include <QStack>

namespace QmlProfilerExtension {
namespace Internal {

using namespace QmlProfiler;

class MemoryUsageModel::MemoryUsageModelPrivate :
        public SortedTimelineModel<MemoryAllocation,
                                   AbstractTimelineModel::AbstractTimelineModelPrivate>
{
public:
    static QString memoryTypeName(int type);

    qint64 maxSize;
private:
    Q_DECLARE_PUBLIC(MemoryUsageModel)
};

MemoryUsageModel::MemoryUsageModel(QObject *parent)
    : AbstractTimelineModel(new MemoryUsageModelPrivate(),
                                  QLatin1String("MemoryUsageTimelineModel"),
                                  QLatin1String("Memory Usage"), QmlDebug::MemoryAllocation,
                                  QmlDebug::MaximumRangeType, parent)
{
}

int MemoryUsageModel::rowCount() const
{
    return isEmpty() ? 1 : 3;
}

int MemoryUsageModel::rowMaxValue(int rowNumber) const
{
    Q_D(const MemoryUsageModel);
    Q_UNUSED(rowNumber);
    return d->maxSize;
}

int MemoryUsageModel::getEventRow(int index) const
{
    Q_D(const MemoryUsageModel);
    QmlDebug::MemoryType type = d->range(index).type;
    if (type == QmlDebug::HeapPage || type == QmlDebug::LargeItem)
        return 1;
    else
        return 2;
}

int MemoryUsageModel::getEventId(int index) const
{
    Q_D(const MemoryUsageModel);
    return d->range(index).type;
}

QColor MemoryUsageModel::getColor(int index) const
{
    return getEventColor(index);
}

float MemoryUsageModel::getHeight(int index) const
{
    Q_D(const MemoryUsageModel);
    return qMin(1.0f, (float)d->range(index).size / (float)d->maxSize);
}

const QVariantMap MemoryUsageModel::getEventLocation(int index) const
{
    static const QLatin1String file("file");
    static const QLatin1String line("line");
    static const QLatin1String column("column");

    Q_D(const MemoryUsageModel);
    QVariantMap result;

    int originType = d->range(index).originTypeIndex;
    if (originType > -1) {
        const QmlDebug::QmlEventLocation &location =
                d->modelManager->qmlModel()->getEventTypes().at(originType).location;

        result.insert(file, location.filename);
        result.insert(line, location.line);
        result.insert(column, location.column);
    }

    return result;
}

const QVariantList MemoryUsageModel::getLabels() const
{
    Q_D(const MemoryUsageModel);
    QVariantList result;

    if (d->expanded && !isEmpty()) {
        {
            QVariantMap element;
            element.insert(QLatin1String("description"), QVariant(tr("Memory Allocation")));

            element.insert(QLatin1String("id"), QVariant(QmlDebug::HeapPage));
            result << element;
        }

        {
            QVariantMap element;
            element.insert(QLatin1String("description"), QVariant(tr("Memory Usage")));

            element.insert(QLatin1String("id"), QVariant(QmlDebug::SmallItem));
            result << element;
        }
    }

    return result;
}

const QVariantList MemoryUsageModel::getEventDetails(int index) const
{
    Q_D(const MemoryUsageModel);
    static QString title = QStringLiteral("title");

    QVariantList result;
    const MemoryUsageModelPrivate::Range *ev = &d->range(index);

    QVariantMap res;
    if (ev->allocated >= -ev->deallocated)
        res.insert(title, tr("Memory Allocated"));
    else
        res.insert(title, tr("Memory Freed"));
    result << res;
    res.clear();

    res.insert(tr("Total"), QVariant(QString::fromLatin1("%1 bytes").arg(ev->size)));
    result << res;
    res.clear();

    if (ev->allocations > 0) {
        res.insert(tr("Allocated"), QString::fromLatin1("%1 bytes").arg(ev->allocated));
        res.insert(tr("Allocations"), QString::number(ev->allocations));
    }
    if (ev->deallocations > 0) {
        res.insert(tr("Deallocated"), QString::fromLatin1("%1 bytes").arg(-ev->deallocated));
        res.insert(tr("Deallocations"), QString::number(ev->deallocations));
    }
    res.insert(tr("Type"), QVariant(MemoryUsageModelPrivate::memoryTypeName(ev->type)));
    if (ev->originTypeIndex != -1) {
        res.insert(tr("Location"),
                d->modelManager->qmlModel()->getEventTypes().at(ev->originTypeIndex).displayName);
    }
    result << res;
    return result;
}

struct RangeStackFrame {
    RangeStackFrame() : originTypeIndex(-1), startTime(-1), endTime(-1) {}
    RangeStackFrame(int originTypeIndex, qint64 startTime, qint64 endTime) :
        originTypeIndex(originTypeIndex), startTime(startTime), endTime(endTime) {}
    int originTypeIndex;
    qint64 startTime;
    qint64 endTime;
};

void MemoryUsageModel::loadData()
{
    Q_D(MemoryUsageModel);
    clear();
    QmlProfilerDataModel *simpleModel = d->modelManager->qmlModel();
    if (simpleModel->isEmpty())
        return;

    qint64 currentSize = 0;
    qint64 currentUsage = 0;
    int currentUsageIndex = -1;
    int currentJSHeapIndex = -1;

    QStack<RangeStackFrame> rangeStack;
    MemoryAllocation dummy;

    const QVector<QmlProfilerDataModel::QmlEventTypeData> &types = simpleModel->getEventTypes();
    foreach (const QmlProfilerDataModel::QmlEventData &event, simpleModel->getEvents()) {
        const QmlProfilerDataModel::QmlEventTypeData &type = types[event.typeIndex];
        while (!rangeStack.empty() && rangeStack.top().endTime < event.startTime)
            rangeStack.pop();
        if (!eventAccepted(type)) {
            if (type.rangeType != QmlDebug::MaximumRangeType) {
                rangeStack.push(RangeStackFrame(event.typeIndex, event.startTime,
                                                event.startTime + event.duration));
            }
            continue;
        }

        if (type.detailType == QmlDebug::SmallItem || type.detailType == QmlDebug::LargeItem) {
            MemoryAllocation &last = currentUsageIndex > -1 ? d->data(currentUsageIndex) : dummy;
            if (!rangeStack.empty() && type.detailType == last.type &&
                    last.originTypeIndex == rangeStack.top().originTypeIndex &&
                    rangeStack.top().startTime < d->range(currentUsageIndex).start) {
                last.update(event.numericData1);
                currentUsage = last.size;
            } else {
                MemoryAllocation allocation(QmlDebug::SmallItem, currentUsage,
                        rangeStack.empty() ? -1 : rangeStack.top().originTypeIndex);
                allocation.update(event.numericData1);
                currentUsage = allocation.size;

                if (currentUsageIndex != -1) {
                    d->insertEnd(currentUsageIndex,
                                 event.startTime - d->range(currentUsageIndex).start - 1);
                }
                currentUsageIndex = d->insertStart(event.startTime, allocation);
            }
        }

        if (type.detailType == QmlDebug::HeapPage || type.detailType == QmlDebug::LargeItem) {
            MemoryAllocation &last = currentJSHeapIndex > -1 ? d->data(currentJSHeapIndex) : dummy;
            if (!rangeStack.empty() && type.detailType == last.type &&
                    last.originTypeIndex == rangeStack.top().originTypeIndex &&
                    rangeStack.top().startTime < d->range(currentJSHeapIndex).start) {
                last.update(event.numericData1);
                currentSize = last.size;
            } else {
                MemoryAllocation allocation((QmlDebug::MemoryType)type.detailType, currentSize,
                        rangeStack.empty() ? -1 : rangeStack.top().originTypeIndex);
                allocation.update(event.numericData1);
                currentSize = allocation.size;

                if (currentSize > d->maxSize)
                    d->maxSize = currentSize;
                if (currentJSHeapIndex != -1)
                    d->insertEnd(currentJSHeapIndex,
                                 event.startTime - d->range(currentJSHeapIndex).start - 1);
                currentJSHeapIndex = d->insertStart(event.startTime, allocation);
            }
        }

        d->modelManager->modelProxyCountUpdated(d->modelId, d->count(), simpleModel->getEvents().count());
    }

    if (currentJSHeapIndex != -1)
        d->insertEnd(currentJSHeapIndex, traceEndTime() -
                     d->range(currentJSHeapIndex).start - 1);
    if (currentUsageIndex != -1)
        d->insertEnd(currentUsageIndex, traceEndTime() -
                     d->range(currentUsageIndex).start - 1);


    d->computeNesting();
    d->modelManager->modelProxyCountUpdated(d->modelId, 1, 1);
}

void MemoryUsageModel::clear()
{
    Q_D(MemoryUsageModel);
    d->SortedTimelineModel::clear();
    d->expanded = false;
    d->maxSize = 1;

    d->modelManager->modelProxyCountUpdated(d->modelId, 0, 1);
}

QString MemoryUsageModel::MemoryUsageModelPrivate::memoryTypeName(int type)
{
    switch (type) {
    case QmlDebug::HeapPage: return tr("Heap Allocation");
    case QmlDebug::LargeItem: return tr("Large Item Allocation");
    case QmlDebug::SmallItem: return tr("Heap Usage");
    case QmlDebug::MaximumMemoryType: return tr("Total");
    default: return tr("Unknown");
    }
}

MemoryUsageModel::MemoryAllocation::MemoryAllocation(QmlDebug::MemoryType type, qint64 baseAmount,
                                                     int originTypeIndex) :
    type(type), size(baseAmount), allocated(0), deallocated(0), allocations(0), deallocations(0),
    originTypeIndex(originTypeIndex)
{
}

void MemoryUsageModel::MemoryAllocation::update(qint64 amount)
{
    size += amount;
    if (amount < 0) {
        deallocated += amount;
        ++deallocations;
    } else {
        allocated += amount;
        ++allocations;
    }
}


} // namespace Internal
} // namespace QmlProfilerExtension