summaryrefslogtreecommitdiff
path: root/src/plugins/qmlprofiler/qmlprofilerdatamodel.cpp
blob: 870fcd7f4a3b1336a65b20323e70cb67014b5974 (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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
** 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.
**
** 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.
**
****************************************************************************/

#include "qmlprofilerdatamodel.h"
#include "qmlprofilermodelmanager.h"
#include "qmlprofilernotesmodel.h"
#include "qmlprofilerdetailsrewriter.h"
#include "qmlprofilereventtypes.h"

#include <utils/qtcassert.h>
#include <QUrl>
#include <QDebug>
#include <algorithm>

namespace QmlProfiler {

class QmlProfilerDataModel::QmlProfilerDataModelPrivate
{
public:
    QVector<QmlEventType> eventTypes;
    QVector<QmlEvent> eventList;
    QHash<QmlEventType, int> eventTypeIds;

    QmlProfilerModelManager *modelManager;
    int modelId;
    Internal::QmlProfilerDetailsRewriter *detailsRewriter;
};

QString getDisplayName(const QmlEventType &event)
{
    if (event.location.filename.isEmpty()) {
        return QmlProfilerDataModel::tr("<bytecode>");
    } else {
        const QString filePath = QUrl(event.location.filename).path();
        return filePath.mid(filePath.lastIndexOf(QLatin1Char('/')) + 1) + QLatin1Char(':') +
                QString::number(event.location.line);
    }
}

QString getInitialDetails(const QmlEventType &event)
{
    QString details;
    // generate details string
    if (!event.data.isEmpty()) {
        details = event.data;
        details = details.replace(QLatin1Char('\n'),QLatin1Char(' ')).simplified();
        if (details.isEmpty()) {
            if (event.rangeType == Javascript)
                details = QmlProfilerDataModel::tr("anonymous function");
        } else {
            QRegExp rewrite(QLatin1String("\\(function \\$(\\w+)\\(\\) \\{ (return |)(.+) \\}\\)"));
            bool match = rewrite.exactMatch(details);
            if (match)
                details = rewrite.cap(1) + QLatin1String(": ") + rewrite.cap(3);
            if (details.startsWith(QLatin1String("file://")) ||
                    details.startsWith(QLatin1String("qrc:/")))
                details = details.mid(details.lastIndexOf(QLatin1Char('/')) + 1);
        }
    } else if (event.rangeType == Painting) {
        // QtQuick1 animations always run in GUI thread.
        details = QmlProfilerDataModel::tr("GUI Thread");
    }

    return details;
}

QString QmlProfilerDataModel::formatTime(qint64 timestamp)
{
    if (timestamp < 1e6)
        return QString::number(timestamp/1e3f,'f',3) + trUtf8(" \xc2\xb5s");
    if (timestamp < 1e9)
        return QString::number(timestamp/1e6f,'f',3) + tr(" ms");

    return QString::number(timestamp/1e9f,'f',3) + tr(" s");
}

QmlProfilerDataModel::QmlProfilerDataModel(Utils::FileInProjectFinder *fileFinder,
                                           QmlProfilerModelManager *parent) :
    QObject(parent), d_ptr(new QmlProfilerDataModelPrivate)
{
    Q_D(QmlProfilerDataModel);
    Q_ASSERT(parent);
    d->modelManager = parent;
    d->detailsRewriter = new QmlProfilerDetailsRewriter(this, fileFinder);
    d->modelId = d->modelManager->registerModelProxy();
    connect(d->detailsRewriter, &QmlProfilerDetailsRewriter::rewriteDetailsString,
            this, &QmlProfilerDataModel::detailsChanged);
    connect(d->detailsRewriter, &QmlProfilerDetailsRewriter::eventDetailsChanged,
            this, &QmlProfilerDataModel::detailsDone);
    connect(this, &QmlProfilerDataModel::requestReload,
            d->detailsRewriter, &QmlProfilerDetailsRewriter::reloadDocuments);
}

QmlProfilerDataModel::~QmlProfilerDataModel()
{
    Q_D(QmlProfilerDataModel);
    delete d->detailsRewriter;
    delete d;
}

const QVector<QmlEvent> &QmlProfilerDataModel::events() const
{
    Q_D(const QmlProfilerDataModel);
    return d->eventList;
}

const QVector<QmlEventType> &QmlProfilerDataModel::eventTypes() const
{
    Q_D(const QmlProfilerDataModel);
    return d->eventTypes;
}

void QmlProfilerDataModel::setData(qint64 traceStart, qint64 traceEnd,
                                   const QVector<QmlEventType> &types,
                                   const QVector<QmlEvent> &events)
{
    Q_D(QmlProfilerDataModel);
    d->modelManager->traceTime()->setTime(traceStart, traceEnd);
    d->eventList = events;
    d->eventTypes = types;
    for (int id = 0; id < types.count(); ++id)
        d->eventTypeIds[types[id]] = id;
}

int QmlProfilerDataModel::count() const
{
    Q_D(const QmlProfilerDataModel);
    return d->eventList.count();
}

void QmlProfilerDataModel::clear()
{
    Q_D(QmlProfilerDataModel);
    d->eventList.clear();
    d->eventTypes.clear();
    d->eventTypeIds.clear();
    d->detailsRewriter->clearRequests();
}

bool QmlProfilerDataModel::isEmpty() const
{
    Q_D(const QmlProfilerDataModel);
    return d->eventList.isEmpty();
}

inline static bool operator<(const QmlEvent &t1, const QmlEvent &t2)
{
    return t1.timestamp() < t2.timestamp();
}

inline static uint qHash(const QmlEventType &type)
{
    return qHash(type.location.filename) ^
            ((type.location.line & 0xfff) |             // 12 bits of line number
            ((type.message << 12) & 0xf000) |           // 4 bits of message
            ((type.location.column << 16) & 0xff0000) | // 8 bits of column
            ((type.rangeType << 24) & 0xf000000) |      // 4 bits of rangeType
            ((type.detailType << 28) & 0xf0000000));    // 4 bits of detailType
}

inline static bool operator==(const QmlEventType &type1,
                              const QmlEventType &type2)
{
    return type1.message == type2.message && type1.rangeType == type2.rangeType &&
            type1.detailType == type2.detailType && type1.location.line == type2.location.line &&
            type1.location.column == type2.location.column &&
            // compare filename last as it's expensive.
            type1.location.filename == type2.location.filename;
}

void QmlProfilerDataModel::processData()
{
    Q_D(QmlProfilerDataModel);
    // post-processing

    // sort events by start time, using above operator<
    std::sort(d->eventList.begin(), d->eventList.end());

    // rewrite strings
    int n = d->eventTypes.count();
    for (int i = 0; i < n; i++) {
        QmlEventType *event = &d->eventTypes[i];
        event->displayName = getDisplayName(*event);
        event->data = getInitialDetails(*event);

        //
        // request further details from files
        //

        if (event->rangeType != Binding && event->rangeType != HandlingSignal)
            continue;

        // This skips anonymous bindings in Qt4.8 (we don't have valid location data for them)
        if (event->location.filename.isEmpty())
            continue;

        // Skip non-anonymous bindings from Qt4.8 (we already have correct details for them)
        if (event->location.column == -1)
            continue;

        d->detailsRewriter->requestDetailsForLocation(i, event->location);
    }

    emit requestReload();
}

void QmlProfilerDataModel::addEvent(Message message, RangeType rangeType, int detailType,
                                    qint64 startTime, qint64 duration, const QString &data,
                                    const QmlEventLocation &location, qint64 ndata1, qint64 ndata2,
                                    qint64 ndata3, qint64 ndata4, qint64 ndata5)
{
    Q_D(QmlProfilerDataModel);
    QString displayName;

    QmlEventType typeData(displayName, location, message, rangeType, detailType,
                              message == DebugMessage ? QString() : data);
    QmlEvent eventData = (message == DebugMessage) ?
                QmlEvent(startTime, duration, -1, data) :
                QmlEvent(startTime, duration, -1, {ndata1, ndata2, ndata3, ndata4, ndata5});

    QHash<QmlEventType, int>::Iterator it = d->eventTypeIds.find(typeData);
    if (it != d->eventTypeIds.end()) {
        eventData.setTypeIndex(it.value());
    } else {
        eventData.setTypeIndex(d->eventTypes.size());
        d->eventTypeIds[typeData] = eventData.typeIndex();
        d->eventTypes.append(typeData);
    }

    d->eventList.append(eventData);
}

qint64 QmlProfilerDataModel::lastTimeMark() const
{
    Q_D(const QmlProfilerDataModel);
    if (d->eventList.isEmpty())
        return 0;

    return d->eventList.last().timestamp() + d->eventList.last().duration();
}

void QmlProfilerDataModel::detailsChanged(int requestId, const QString &newString)
{
    Q_D(QmlProfilerDataModel);
    QTC_ASSERT(requestId < d->eventTypes.count(), return);

    QmlEventType *event = &d->eventTypes[requestId];
    event->data = newString;
}

void QmlProfilerDataModel::detailsDone()
{
    Q_D(QmlProfilerDataModel);
    d->modelManager->processingDone();
}

} // namespace QmlProfiler