qmlprofilerdatamodel.cpp 8.64 KB
Newer Older
Christiaan Janssen's avatar
Christiaan Janssen committed
1
2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
Christiaan Janssen's avatar
Christiaan Janssen committed
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
** Contact: http://www.qt-project.org/legal
**
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

30
#include "qmlprofilerdatamodel.h"
31
#include "qmlprofilerbasemodel_p.h"
32
#include "qmlprofilermodelmanager.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
33
34
35
36
37
38
39
#include <qmldebug/qmlprofilereventtypes.h>
#include <utils/qtcassert.h>
#include <QUrl>
#include <QDebug>

namespace QmlProfiler {

40
41
42
43
44
45
46
47
48
49
class QmlProfilerDataModel::QmlProfilerDataModelPrivate :
        public QmlProfilerBaseModel::QmlProfilerBaseModelPrivate
{
public:
    QmlProfilerDataModelPrivate(QmlProfilerDataModel *qq) : QmlProfilerBaseModelPrivate(qq) {}
    QVector<QmlEventData> eventList;
private:
    Q_DECLARE_PUBLIC(QmlProfilerDataModel)
};

50
51
52
QmlDebug::QmlEventLocation getLocation(const QmlProfilerDataModel::QmlEventData &event);
QString getDisplayName(const QmlProfilerDataModel::QmlEventData &event);
QString getInitialDetails(const QmlProfilerDataModel::QmlEventData &event);
Christiaan Janssen's avatar
Christiaan Janssen committed
53

54
QmlDebug::QmlEventLocation getLocation(const QmlProfilerDataModel::QmlEventData &event)
Christiaan Janssen's avatar
Christiaan Janssen committed
55
56
57
58
59
60
61
62
63
64
65
{
    QmlDebug::QmlEventLocation eventLocation = event.location;
    if ((event.eventType == QmlDebug::Creating || event.eventType == QmlDebug::Compiling)
            && eventLocation.filename.isEmpty()) {
        eventLocation.filename = getInitialDetails(event);
        eventLocation.line = 1;
        eventLocation.column = 1;
    }
    return eventLocation;
}

66
QString getDisplayName(const QmlProfilerDataModel::QmlEventData &event)
Christiaan Janssen's avatar
Christiaan Janssen committed
67
68
69
70
71
72
{
    const QmlDebug::QmlEventLocation eventLocation = getLocation(event);
    QString displayName;

    // generate hash
    if (eventLocation.filename.isEmpty()) {
73
        displayName = QmlProfilerDataModel::tr("<bytecode>");
Christiaan Janssen's avatar
Christiaan Janssen committed
74
75
76
77
78
79
80
81
82
    } else {
        const QString filePath = QUrl(eventLocation.filename).path();
        displayName = filePath.mid(filePath.lastIndexOf(QLatin1Char('/')) + 1) + QLatin1Char(':') +
                QString::number(eventLocation.line);
    }

    return displayName;
}

83
QString getInitialDetails(const QmlProfilerDataModel::QmlEventData &event)
Christiaan Janssen's avatar
Christiaan Janssen committed
84
85
86
87
{
    QString details;
    // generate details string
    if (event.data.isEmpty())
88
        details = QmlProfilerDataModel::tr("Source code not available.");
Christiaan Janssen's avatar
Christiaan Janssen committed
89
90
    else {
        details = event.data.join(QLatin1String(" ")).replace(QLatin1Char('\n'),QLatin1Char(' ')).simplified();
91
        if (details.isEmpty()) {
92
            details = QmlProfilerDataModel::tr("anonymous function");
93
94
95
96
97
        } else {
            QRegExp rewrite(QLatin1String("\\(function \\$(\\w+)\\(\\) \\{ (return |)(.+) \\}\\)"));
            bool match = rewrite.exactMatch(details);
            if (match)
                details = rewrite.cap(1) + QLatin1String(": ") + rewrite.cap(3);
98
99
            if (details.startsWith(QLatin1String("file://")) ||
                    details.startsWith(QLatin1String("qrc:/")))
100
101
                details = details.mid(details.lastIndexOf(QLatin1Char('/')) + 1);
        }
Christiaan Janssen's avatar
Christiaan Janssen committed
102
103
104
105
106
107
    }

    return details;
}


108
bool compareStartTimes(const QmlProfilerDataModel::QmlEventData &t1, const QmlProfilerDataModel::QmlEventData &t2)
Christiaan Janssen's avatar
Christiaan Janssen committed
109
110
111
112
113
114
{
    return t1.startTime < t2.startTime;
}

//////////////////////////////////////////////////////////////////////////////

115
116
QmlProfilerDataModel::QmlProfilerDataModel(Utils::FileInProjectFinder *fileFinder,
                                                     QmlProfilerModelManager *parent)
117
    : QmlProfilerBaseModel(fileFinder, parent, new QmlProfilerDataModelPrivate(this))
Christiaan Janssen's avatar
Christiaan Janssen committed
118
{
119
    Q_D(QmlProfilerDataModel);
120
    // The document loading is very expensive.
121
    d->modelManager->setProxyCountWeight(d->modelId, 4);
122
}
123

124
125
const QVector<QmlProfilerDataModel::QmlEventData> &QmlProfilerDataModel::getEvents() const
{
126
127
    Q_D(const QmlProfilerDataModel);
    return d->eventList;
Christiaan Janssen's avatar
Christiaan Janssen committed
128
129
}

130
int QmlProfilerDataModel::count() const
Christiaan Janssen's avatar
Christiaan Janssen committed
131
{
132
133
    Q_D(const QmlProfilerDataModel);
    return d->eventList.count();
Christiaan Janssen's avatar
Christiaan Janssen committed
134
135
}

136
void QmlProfilerDataModel::clear()
Christiaan Janssen's avatar
Christiaan Janssen committed
137
{
138
139
    Q_D(QmlProfilerDataModel);
    d->eventList.clear();
140
    // This call emits changed(). Don't emit it again here.
141
142
143
144
145
    QmlProfilerBaseModel::clear();
}

bool QmlProfilerDataModel::isEmpty() const
{
146
147
    Q_D(const QmlProfilerDataModel);
    return d->eventList.isEmpty();
Christiaan Janssen's avatar
Christiaan Janssen committed
148
149
}

150
void QmlProfilerDataModel::complete()
Christiaan Janssen's avatar
Christiaan Janssen committed
151
{
152
    Q_D(QmlProfilerDataModel);
Christiaan Janssen's avatar
Christiaan Janssen committed
153
154
155
    // post-processing

    // sort events by start time
156
    qSort(d->eventList.begin(), d->eventList.end(), compareStartTimes);
Christiaan Janssen's avatar
Christiaan Janssen committed
157
158

    // rewrite strings
159
    int n = d->eventList.count();
Christiaan Janssen's avatar
Christiaan Janssen committed
160
    for (int i = 0; i < n; i++) {
161
        QmlEventData *event = &d->eventList[i];
Christiaan Janssen's avatar
Christiaan Janssen committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
        event->location = getLocation(*event);
        event->displayName = getDisplayName(*event);
        event->data = QStringList() << getInitialDetails(*event);

        //
        // request further details from files
        //

        if (event->eventType != QmlDebug::Binding && event->eventType != QmlDebug::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;

181
182
        d->detailsRewriter->requestDetailsForLocation(i, event->location);
        d->modelManager->modelProxyCountUpdated(d->modelId, i + n, n * 2);
Christiaan Janssen's avatar
Christiaan Janssen committed
183
184
    }

185
    // Allow changed() event only after documents have been reloaded to avoid
186
    // unnecessary updates of child models.
187
    QmlProfilerBaseModel::complete();
Christiaan Janssen's avatar
Christiaan Janssen committed
188
189
}

190
191
192
193
194
void QmlProfilerDataModel::addQmlEvent(int type, int bindingType, qint64 startTime,
                                            qint64 duration, const QStringList &data,
                                            const QmlDebug::QmlEventLocation &location,
                                            qint64 ndata1, qint64 ndata2, qint64 ndata3,
                                            qint64 ndata4, qint64 ndata5)
Christiaan Janssen's avatar
Christiaan Janssen committed
195
{
196
    Q_D(QmlProfilerDataModel);
197
198
199
200
201
202
203
204
    QString displayName;
    if (type == QmlDebug::Painting && bindingType == QmlDebug::AnimationFrame) {
        displayName = tr("Animations");
    } else {
        displayName = QString::fromLatin1("%1:%2").arg(
                location.filename,
                QString::number(location.line));
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
205

206
207
    QmlEventData eventData = {displayName, type, bindingType, startTime, duration, data, location,
                              ndata1, ndata2, ndata3, ndata4, ndata5};
208
    d->eventList.append(eventData);
209

210
211
    d->modelManager->modelProxyCountUpdated(d->modelId, startTime,
                                            d->modelManager->estimatedProfilingTime() * 2);
Christiaan Janssen's avatar
Christiaan Janssen committed
212
213
}

214
QString QmlProfilerDataModel::getHashString(const QmlProfilerDataModel::QmlEventData &event)
Christiaan Janssen's avatar
Christiaan Janssen committed
215
{
216
217
218
219
220
221
    return QString::fromLatin1("%1:%2:%3:%4:%5").arg(
                event.location.filename,
                QString::number(event.location.line),
                QString::number(event.location.column),
                QString::number(event.eventType),
                QString::number(event.bindingType));
Christiaan Janssen's avatar
Christiaan Janssen committed
222
223
}

224
225
qint64 QmlProfilerDataModel::lastTimeMark() const
{
226
227
    Q_D(const QmlProfilerDataModel);
    if (d->eventList.isEmpty())
228
229
        return 0;

230
    return d->eventList.last().startTime + d->eventList.last().duration;
Christiaan Janssen's avatar
Christiaan Janssen committed
231
}
232
233
234

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

238
    QmlEventData *event = &d->eventList[requestId];
239
240
241
    event->data = QStringList(newString);
}

Christiaan Janssen's avatar
Christiaan Janssen committed
242
}