qmlprofilermodelmanager.cpp 17.4 KB
Newer Older
Christiaan Janssen's avatar
Christiaan Janssen committed
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Christiaan Janssen's avatar
Christiaan Janssen committed
5 6 7 8 9 10 11
**
** 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
12 13 14
** 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.
Christiaan Janssen's avatar
Christiaan Janssen committed
15
**
16 17 18 19 20 21 22
** 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.
Christiaan Janssen's avatar
Christiaan Janssen committed
23 24 25 26
**
****************************************************************************/

#include "qmlprofilermodelmanager.h"
27
#include "qmlprofilerconstants.h"
28
#include "qmlprofilerdatamodel.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
29
#include "qmlprofilertracefile.h"
30
#include "qmlprofilernotesmodel.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
31

32 33
#include <coreplugin/progressmanager/progressmanager.h>
#include <utils/runextensions.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
34 35 36 37
#include <utils/qtcassert.h>

#include <QDebug>
#include <QFile>
38
#include <QMessageBox>
Christiaan Janssen's avatar
Christiaan Janssen committed
39

40 41
#include <functional>

Christiaan Janssen's avatar
Christiaan Janssen committed
42 43 44
namespace QmlProfiler {
namespace Internal {

45
static const char *ProfileFeatureNames[] = {
46 47 48 49 50 51 52 53 54 55
    QT_TRANSLATE_NOOP("MainView", "JavaScript"),
    QT_TRANSLATE_NOOP("MainView", "Memory Usage"),
    QT_TRANSLATE_NOOP("MainView", "Pixmap Cache"),
    QT_TRANSLATE_NOOP("MainView", "Scene Graph"),
    QT_TRANSLATE_NOOP("MainView", "Animations"),
    QT_TRANSLATE_NOOP("MainView", "Painting"),
    QT_TRANSLATE_NOOP("MainView", "Compiling"),
    QT_TRANSLATE_NOOP("MainView", "Creating"),
    QT_TRANSLATE_NOOP("MainView", "Binding"),
    QT_TRANSLATE_NOOP("MainView", "Handling Signal"),
56 57
    QT_TRANSLATE_NOOP("MainView", "Input Events"),
    QT_TRANSLATE_NOOP("MainView", "Debug Messages")
58
};
Christiaan Janssen's avatar
Christiaan Janssen committed
59

60
Q_STATIC_ASSERT(sizeof(ProfileFeatureNames) == sizeof(char *) * MaximumProfileFeature);
61

Christiaan Janssen's avatar
Christiaan Janssen committed
62
/////////////////////////////////////////////////////////////////////
63
QmlProfilerTraceTime::QmlProfilerTraceTime(QObject *parent) :
64 65
    QObject(parent), m_startTime(-1), m_endTime(-1),
    m_restrictedStartTime(-1), m_restrictedEndTime(-1)
Christiaan Janssen's avatar
Christiaan Janssen committed
66 67 68 69 70
{
}

qint64 QmlProfilerTraceTime::startTime() const
{
71
    return m_restrictedStartTime != -1 ? m_restrictedStartTime : m_startTime;
Christiaan Janssen's avatar
Christiaan Janssen committed
72 73 74 75
}

qint64 QmlProfilerTraceTime::endTime() const
{
76
    return m_restrictedEndTime != -1 ? m_restrictedEndTime : m_endTime;
Christiaan Janssen's avatar
Christiaan Janssen committed
77 78 79 80 81 82 83
}

qint64 QmlProfilerTraceTime::duration() const
{
    return endTime() - startTime();
}

84 85 86 87 88
bool QmlProfilerTraceTime::isRestrictedToRange() const
{
    return m_restrictedStartTime != -1 || m_restrictedEndTime != -1;
}

Christiaan Janssen's avatar
Christiaan Janssen committed
89 90
void QmlProfilerTraceTime::clear()
{
91
    restrictToRange(-1, -1);
92
    setTime(-1, -1);
Christiaan Janssen's avatar
Christiaan Janssen committed
93 94
}

95
void QmlProfilerTraceTime::setTime(qint64 startTime, qint64 endTime)
Christiaan Janssen's avatar
Christiaan Janssen committed
96
{
97 98 99
    QTC_ASSERT(startTime <= endTime, endTime = startTime);
    m_startTime = startTime;
    m_endTime = endTime;
Christiaan Janssen's avatar
Christiaan Janssen committed
100 101
}

Ulf Hermann's avatar
Ulf Hermann committed
102 103
void QmlProfilerTraceTime::decreaseStartTime(qint64 time)
{
104
    if (m_startTime > time || m_startTime == -1) {
105
        m_startTime = time;
106 107 108 109
        if (m_endTime == -1)
            m_endTime = m_startTime;
        else
            QTC_ASSERT(m_endTime >= m_startTime, m_endTime = m_startTime);
110
    }
Ulf Hermann's avatar
Ulf Hermann committed
111 112 113 114
}

void QmlProfilerTraceTime::increaseEndTime(qint64 time)
{
115
    if (m_endTime < time || m_endTime == -1) {
116
        m_endTime = time;
117 118 119 120
        if (m_startTime == -1)
            m_startTime = m_endTime;
        else
            QTC_ASSERT(m_endTime >= m_startTime, m_startTime = m_endTime);
121
    }
Ulf Hermann's avatar
Ulf Hermann committed
122 123
}

124 125 126 127 128 129 130
void QmlProfilerTraceTime::restrictToRange(qint64 startTime, qint64 endTime)
{
    QTC_ASSERT(endTime == -1 || startTime <= endTime, endTime = startTime);
    m_restrictedStartTime = startTime;
    m_restrictedEndTime = endTime;
}

Christiaan Janssen's avatar
Christiaan Janssen committed
131 132 133 134 135 136 137 138

} // namespace Internal

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

class QmlProfilerModelManager::QmlProfilerModelManagerPrivate
{
public:
139
    QmlProfilerDataModel *model;
140
    QmlProfilerNotesModel *notesModel;
141
    QmlProfilerTextMarkModel *textMarkModel;
142

143
    QmlProfilerModelManager::State state;
Christiaan Janssen's avatar
Christiaan Janssen committed
144 145
    QmlProfilerTraceTime *traceTime;

146
    int numRegisteredModels;
147 148 149
    int numFinishedFinalizers;

    uint numLoadedEvents;
150 151 152
    quint64 availableFeatures;
    quint64 visibleFeatures;
    quint64 recordedFeatures;
153
    bool aggregateTraces;
154 155 156

    QHash<ProfileFeature, QVector<EventLoader> > eventLoaders;
    QVector<Finalizer> finalizers;
157 158

    void dispatch(const QmlEvent &event, const QmlEventType &type);
Christiaan Janssen's avatar
Christiaan Janssen committed
159 160 161
};


162
QmlProfilerModelManager::QmlProfilerModelManager(QObject *parent) :
163
    QObject(parent), d(new QmlProfilerModelManagerPrivate)
Christiaan Janssen's avatar
Christiaan Janssen committed
164
{
165
    d->numRegisteredModels = 0;
166 167
    d->numFinishedFinalizers = 0;
    d->numLoadedEvents = 0;
168 169 170
    d->availableFeatures = 0;
    d->visibleFeatures = 0;
    d->recordedFeatures = 0;
171
    d->aggregateTraces = false;
172
    d->model = new QmlProfilerDataModel(this);
173
    d->state = Empty;
Christiaan Janssen's avatar
Christiaan Janssen committed
174
    d->traceTime = new QmlProfilerTraceTime(this);
175
    d->notesModel = new QmlProfilerNotesModel(this);
176
    d->textMarkModel = new QmlProfilerTextMarkModel(this);
177 178
    connect(d->model, &QmlProfilerDataModel::allTypesLoaded,
            this, &QmlProfilerModelManager::processingDone);
179 180 181 182
    connect(d->model, &QmlProfilerDataModel::traceFileError,
            this, [this]() {
        emit error(tr("Could not open a temporary file for storing QML traces."));
    });
Christiaan Janssen's avatar
Christiaan Janssen committed
183 184 185 186 187 188 189 190 191 192 193 194
}

QmlProfilerModelManager::~QmlProfilerModelManager()
{
    delete d;
}

QmlProfilerTraceTime *QmlProfilerModelManager::traceTime() const
{
    return d->traceTime;
}

195
QmlProfilerNotesModel *QmlProfilerModelManager::notesModel() const
196 197 198 199
{
    return d->notesModel;
}

200 201 202 203 204
QmlProfilerTextMarkModel *QmlProfilerModelManager::textMarkModel() const
{
    return d->textMarkModel;
}

Christiaan Janssen's avatar
Christiaan Janssen committed
205 206
bool QmlProfilerModelManager::isEmpty() const
{
Joerg Bornemann's avatar
Joerg Bornemann committed
207
    return d->model->isEmpty();
Christiaan Janssen's avatar
Christiaan Janssen committed
208 209
}

210 211 212 213 214
uint QmlProfilerModelManager::numLoadedEvents() const
{
    return d->numLoadedEvents;
}

215 216 217 218 219
uint QmlProfilerModelManager::numLoadedEventTypes() const
{
    return d->model->eventTypes().count();
}

Christiaan Janssen's avatar
Christiaan Janssen committed
220 221
int QmlProfilerModelManager::registerModelProxy()
{
222
    return d->numRegisteredModels++;
Christiaan Janssen's avatar
Christiaan Janssen committed
223 224
}

225 226 227 228 229 230 231 232 233 234
int QmlProfilerModelManager::numFinishedFinalizers() const
{
    return d->numFinishedFinalizers;
}

int QmlProfilerModelManager::numRegisteredFinalizers() const
{
    return d->finalizers.count();
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
void QmlProfilerModelManager::addEvents(const QVector<QmlEvent> &events)
{
    d->model->addEvents(events);
    const QVector<QmlEventType> &types = d->model->eventTypes();
    for (const QmlEvent &event : events)
        d->dispatch(event, types[event.typeIndex()]);
}

void QmlProfilerModelManager::addEvent(const QmlEvent &event)
{
    d->model->addEvent(event);
    d->dispatch(event, d->model->eventType(event.typeIndex()));
}

void QmlProfilerModelManager::addEventTypes(const QVector<QmlEventType> &types)
{
251
    const int firstTypeId = d->model->eventTypes().count();
252
    d->model->addEventTypes(types);
253 254 255 256 257 258 259 260 261
    for (int i = 0, end = types.length(); i < end; ++i) {
        const QmlEventLocation &location = types[i].location();
        if (location.isValid()) {
            d->textMarkModel->addTextMarkId(
                        firstTypeId + i, QmlEventLocation(
                            d->model->findLocalFile(location.filename()), location.line(),
                            location.column()));
        }
    }
262 263 264 265
}

void QmlProfilerModelManager::addEventType(const QmlEventType &type)
{
266
    const int typeId = d->model->eventTypes().count();
267
    d->model->addEventType(type);
268 269 270 271 272 273
    const QmlEventLocation &location = type.location();
    if (location.isValid()) {
        d->textMarkModel->addTextMarkId(
                    typeId, QmlEventLocation(d->model->findLocalFile(location.filename()),
                                             location.line(), location.column()));
    }
274 275
}

276 277 278 279 280 281 282 283 284 285 286
const QVector<QmlEventType> &QmlProfilerModelManager::eventTypes() const
{
    return d->model->eventTypes();
}

bool QmlProfilerModelManager::replayEvents(qint64 startTime, qint64 endTime,
                                           EventLoader loader) const
{
    return d->model->replayEvents(startTime, endTime, loader);
}

287 288
void QmlProfilerModelManager::QmlProfilerModelManagerPrivate::dispatch(const QmlEvent &event,
                                                                       const QmlEventType &type)
289
{
290
    foreach (const EventLoader &loader, eventLoaders[type.feature()])
291
        loader(event, type);
292
    ++numLoadedEvents;
293 294 295 296
}

void QmlProfilerModelManager::announceFeatures(quint64 features, EventLoader eventLoader,
                                               Finalizer finalizer)
297
{
298 299 300
    if ((features & d->availableFeatures) != features) {
        d->availableFeatures |= features;
        emit availableFeaturesChanged(d->availableFeatures);
301
    }
302 303 304 305
    if ((features & d->visibleFeatures) != features) {
        d->visibleFeatures |= features;
        emit visibleFeaturesChanged(d->visibleFeatures);
    }
306 307

    for (int feature = 0; feature != MaximumProfileFeature; ++feature) {
308
        if (features & (1ULL << feature))
309 310 311 312
            d->eventLoaders[static_cast<ProfileFeature>(feature)].append(eventLoader);
    }

    d->finalizers.append(finalizer);
313 314 315 316 317
}

quint64 QmlProfilerModelManager::availableFeatures() const
{
    return d->availableFeatures;
318 319
}

320
quint64 QmlProfilerModelManager::visibleFeatures() const
321
{
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    return d->visibleFeatures;
}

void QmlProfilerModelManager::setVisibleFeatures(quint64 features)
{
    if (d->visibleFeatures != features) {
        d->visibleFeatures = features;
        emit visibleFeaturesChanged(d->visibleFeatures);
    }
}

quint64 QmlProfilerModelManager::recordedFeatures() const
{
    return d->recordedFeatures;
}

void QmlProfilerModelManager::setRecordedFeatures(quint64 features)
{
    if (d->recordedFeatures != features) {
        d->recordedFeatures = features;
        emit recordedFeaturesChanged(d->recordedFeatures);
    }
344 345
}

346 347 348 349 350 351 352 353 354 355 356
bool QmlProfilerModelManager::aggregateTraces() const
{
    return d->aggregateTraces;
}

void QmlProfilerModelManager::setAggregateTraces(bool aggregateTraces)
{
    d->aggregateTraces = aggregateTraces;
}


357
const char *QmlProfilerModelManager::featureName(ProfileFeature feature)
358 359 360 361
{
    return ProfileFeatureNames[feature];
}

362
void QmlProfilerModelManager::acquiringDone()
Christiaan Janssen's avatar
Christiaan Janssen committed
363
{
364 365
    QTC_ASSERT(state() == AcquiringData, /**/);
    setState(ProcessingData);
366
    d->model->finalize();
367 368 369 370 371
}

void QmlProfilerModelManager::processingDone()
{
    QTC_ASSERT(state() == ProcessingData, /**/);
372 373
    // Load notes after the timeline models have been initialized ...
    // which happens on stateChanged(Done).
374

375
    foreach (const Finalizer &finalizer, d->finalizers) {
376
        finalizer();
377 378
        ++d->numFinishedFinalizers;
    }
379

380
    d->notesModel->loadData();
381
    setState(Done);
Christiaan Janssen's avatar
Christiaan Janssen committed
382 383
}

384 385 386 387 388 389 390 391 392 393
void QmlProfilerModelManager::populateFileFinder(const ProjectExplorer::RunConfiguration *runConfiguration)
{
    d->model->populateFileFinder(runConfiguration);
}

QString QmlProfilerModelManager::findLocalFile(const QString &remoteFile)
{
    return d->model->findLocalFile(remoteFile);
}

Christiaan Janssen's avatar
Christiaan Janssen committed
394 395
void QmlProfilerModelManager::save(const QString &filename)
{
396 397
    QFile *file = new QFile(filename);
    if (!file->open(QIODevice::WriteOnly)) {
Christiaan Janssen's avatar
Christiaan Janssen committed
398
        emit error(tr("Could not open %1 for writing.").arg(filename));
399 400
        delete file;
        emit saveFinished();
Christiaan Janssen's avatar
Christiaan Janssen committed
401 402 403
        return;
    }

404
    d->notesModel->saveData();
405

406 407 408
    QmlProfilerFileWriter *writer = new QmlProfilerFileWriter(this);
    writer->setTraceTime(traceTime()->startTime(), traceTime()->endTime(),
                        traceTime()->duration());
409
    writer->setData(this);
410
    writer->setNotes(d->notesModel->notes());
411 412 413 414

    connect(writer, &QObject::destroyed, this, &QmlProfilerModelManager::saveFinished,
            Qt::QueuedConnection);

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    connect(writer, &QmlProfilerFileWriter::error, this, [this, file](const QString &message) {
        file->close();
        file->remove();
        delete file;
        emit error(message);
    }, Qt::QueuedConnection);

    connect(writer, &QmlProfilerFileWriter::success, this, [this, file]() {
        file->close();
        delete file;
    }, Qt::QueuedConnection);

    connect(writer, &QmlProfilerFileWriter::canceled, this, [this, file]() {
        file->close();
        file->remove();
        delete file;
    }, Qt::QueuedConnection);

433
    QFuture<void> result = Utils::runAsync([file, writer] (QFutureInterface<void> &future) {
434
        writer->setFuture(&future);
435 436 437 438
        if (file->fileName().endsWith(QLatin1String(Constants::QtdFileExtension)))
            writer->saveQtd(file);
        else
            writer->saveQzt(file);
439
        writer->deleteLater();
440 441 442 443
    });

    Core::ProgressManager::addTask(result, tr("Saving Trace Data"), Constants::TASK_SAVE,
                                   Core::ProgressManager::ShowInApplicationIcon);
Christiaan Janssen's avatar
Christiaan Janssen committed
444 445 446 447
}

void QmlProfilerModelManager::load(const QString &filename)
{
448
    bool isQtd = filename.endsWith(QLatin1String(Constants::QtdFileExtension));
449
    QFile *file = new QFile(filename, this);
450
    if (!file->open(isQtd ? (QIODevice::ReadOnly | QIODevice::Text) : QIODevice::ReadOnly)) {
451
        emit error(tr("Could not open %1 for reading.").arg(filename));
452 453
        delete file;
        emit loadFinished();
Christiaan Janssen's avatar
Christiaan Janssen committed
454 455 456 457
        return;
    }

    clear();
458
    setState(AcquiringData);
459 460
    QmlProfilerFileReader *reader = new QmlProfilerFileReader(this);

461 462
    connect(reader, &QObject::destroyed, this, &QmlProfilerModelManager::loadFinished,
            Qt::QueuedConnection);
463

464
    connect(reader, &QmlProfilerFileReader::typesLoaded,
465
            this, &QmlProfilerModelManager::addEventTypes);
466 467 468 469

    connect(reader, &QmlProfilerFileReader::notesLoaded,
            d->notesModel, &QmlProfilerNotesModel::setNotes);

470
    connect(reader, &QmlProfilerFileReader::qmlEventsLoaded,
471
            this, &QmlProfilerModelManager::addEvents);
472

473
    connect(reader, &QmlProfilerFileReader::success, this, [this, reader]() {
474
        d->traceTime->setTime(reader->traceStart(), reader->traceEnd());
475 476 477 478
        setRecordedFeatures(reader->loadedFeatures());
        delete reader;
        acquiringDone();
    }, Qt::QueuedConnection);
Christiaan Janssen's avatar
Christiaan Janssen committed
479

480 481 482 483 484 485 486 487 488 489 490
    connect(reader, &QmlProfilerFileReader::error, this, [this, reader](const QString &message) {
        clear();
        delete reader;
        emit error(message);
    }, Qt::QueuedConnection);

    connect(reader, &QmlProfilerFileReader::canceled, this, [this, reader]() {
        clear();
        delete reader;
    }, Qt::QueuedConnection);

491
    QFuture<void> result = Utils::runAsync([isQtd, file, reader] (QFutureInterface<void> &future) {
492
        reader->setFuture(&future);
493 494 495 496
        if (isQtd)
            reader->loadQtd(file);
        else
            reader->loadQzt(file);
497 498 499
        file->close();
        file->deleteLater();
    });
Christiaan Janssen's avatar
Christiaan Janssen committed
500

501
    Core::ProgressManager::addTask(result, tr("Loading Trace Data"), Constants::TASK_LOAD);
Christiaan Janssen's avatar
Christiaan Janssen committed
502 503
}

504
void QmlProfilerModelManager::setState(QmlProfilerModelManager::State state)
Christiaan Janssen's avatar
Christiaan Janssen committed
505
{
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
    // It's not an error, we are continuously calling "AcquiringData" for example
    if (d->state == state)
        return;

    switch (state) {
        case ClearingData:
            QTC_ASSERT(d->state == Done || d->state == Empty || d->state == AcquiringData, /**/);
        break;
        case Empty:
            // if it's not empty, complain but go on
            QTC_ASSERT(isEmpty(), /**/);
        break;
        case AcquiringData:
            // we're not supposed to receive new data while processing older data
            QTC_ASSERT(d->state != ProcessingData, return);
        break;
        case ProcessingData:
            QTC_ASSERT(d->state == AcquiringData, return);
        break;
        case Done:
            QTC_ASSERT(d->state == ProcessingData || d->state == Empty, return);
        break;
        default:
            emit error(tr("Trying to set unknown state in events list."));
        break;
    }

    d->state = state;
    emit stateChanged();
Christiaan Janssen's avatar
Christiaan Janssen committed
535 536
}

537
QmlProfilerModelManager::State QmlProfilerModelManager::state() const
Christiaan Janssen's avatar
Christiaan Janssen committed
538
{
539
    return d->state;
Christiaan Janssen's avatar
Christiaan Janssen committed
540 541 542 543
}

void QmlProfilerModelManager::clear()
{
544
    setState(ClearingData);
545 546
    d->numLoadedEvents = 0;
    d->numFinishedFinalizers = 0;
Christiaan Janssen's avatar
Christiaan Janssen committed
547 548
    d->model->clear();
    d->traceTime->clear();
549
    d->notesModel->clear();
550 551
    setVisibleFeatures(0);
    setRecordedFeatures(0);
Christiaan Janssen's avatar
Christiaan Janssen committed
552

553
    setState(Empty);
Christiaan Janssen's avatar
Christiaan Janssen committed
554 555
}

556 557
void QmlProfilerModelManager::restrictToRange(qint64 startTime, qint64 endTime)
{
558
    d->notesModel->saveData();
559 560 561
    const QVector<QmlNote> notes = d->notesModel->notes();
    d->notesModel->clear();

562
    setState(ClearingData);
563 564 565
    setVisibleFeatures(0);

    startAcquiring();
566 567 568 569 570 571 572 573 574 575 576
    if (!d->model->replayEvents(startTime, endTime,
                                std::bind(&QmlProfilerModelManagerPrivate::dispatch, d,
                                          std::placeholders::_1, std::placeholders::_2))) {
        emit error(tr("Could not re-read events from temporary trace file. "
                      "The trace data is lost."));
        clear();
    } else {
        d->notesModel->setNotes(notes);
        d->traceTime->restrictToRange(startTime, endTime);
        acquiringDone();
    }
577 578 579 580 581 582 583
}

bool QmlProfilerModelManager::isRestrictedToRange() const
{
    return d->traceTime->isRestrictedToRange();
}

584
void QmlProfilerModelManager::startAcquiring()
Christiaan Janssen's avatar
Christiaan Janssen committed
585
{
586
    setState(AcquiringData);
Christiaan Janssen's avatar
Christiaan Janssen committed
587 588 589
}

} // namespace QmlProfiler