qmlprofilertool.cpp 32 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Kai Koehne's avatar
Kai Koehne committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
Kai Koehne's avatar
Kai Koehne committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Kai Koehne's avatar
Kai Koehne committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
Kai Koehne's avatar
Kai Koehne committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
Kai Koehne's avatar
Kai Koehne committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Kai Koehne's avatar
Kai Koehne committed
30

Christiaan Janssen's avatar
Christiaan Janssen committed
31
#include "qmlprofilertool.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
32
#include "qmlprofilerstatemanager.h"
33
#include "qmlprofilerruncontrol.h"
34 35
#include "qmlprofilerconstants.h"
#include "qmlprofilerattachdialog.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
36 37
#include "qmlprofilerviewmanager.h"
#include "qmlprofilerclientmanager.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
38
#include "qmlprofilermodelmanager.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
39
#include "qmlprofilerdetailsrewriter.h"
40
#include "qmlprofilernotesmodel.h"
41 42
#include "qmlprofilerrunconfigurationaspect.h"
#include "qmlprofilersettings.h"
43
#include "qmlprofilerplugin.h"
Christiaan Janssen's avatar
Christiaan Janssen committed
44 45

#include <analyzerbase/analyzermanager.h>
46
#include <analyzerbase/analyzerruncontrol.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
47

48
#include <utils/fancymainwindow.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
49
#include <utils/fileinprojectfinder.h>
50
#include <utils/qtcassert.h>
51
#include <projectexplorer/environmentaspect.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
52 53
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/project.h>
54
#include <projectexplorer/target.h>
55
#include <projectexplorer/session.h>
56
#include <projectexplorer/kitinformation.h>
57
#include <projectexplorer/localapplicationrunconfiguration.h>
58
#include <texteditor/texteditor.h>
59

60
#include <coreplugin/coreconstants.h>
61
#include <coreplugin/coreicons.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
62
#include <coreplugin/editormanager/editormanager.h>
63
#include <coreplugin/find/findplugin.h>
64
#include <coreplugin/icore.h>
65
#include <coreplugin/messagemanager.h>
66
#include <coreplugin/helpmanager.h>
67 68 69 70 71
#include <coreplugin/modemanager.h>
#include <coreplugin/imode.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
72

Tobias Hunger's avatar
Tobias Hunger committed
73
#include <qtsupport/qtkitinformation.h>
Tobias Hunger's avatar
Tobias Hunger committed
74

75
#include <QApplication>
hjk's avatar
hjk committed
76
#include <QFileDialog>
77 78
#include <QHBoxLayout>
#include <QLabel>
79
#include <QLineEdit>
80
#include <QMenu>
hjk's avatar
hjk committed
81
#include <QMessageBox>
82
#include <QTcpServer>
hjk's avatar
hjk committed
83 84 85
#include <QTime>
#include <QTimer>
#include <QToolButton>
86

87 88
using namespace Core;
using namespace Core::Constants;
Christiaan Janssen's avatar
Christiaan Janssen committed
89
using namespace Analyzer;
90
using namespace Analyzer::Constants;
Christiaan Janssen's avatar
Christiaan Janssen committed
91
using namespace QmlProfiler::Constants;
92
using namespace QmlDebug;
93
using namespace ProjectExplorer;
Christiaan Janssen's avatar
Christiaan Janssen committed
94

95 96 97
namespace QmlProfiler {
namespace Internal {

Christiaan Janssen's avatar
Christiaan Janssen committed
98 99 100
class QmlProfilerTool::QmlProfilerToolPrivate
{
public:
Christiaan Janssen's avatar
Christiaan Janssen committed
101 102
    QmlProfilerStateManager *m_profilerState;
    QmlProfilerClientManager *m_profilerConnections;
Christiaan Janssen's avatar
Christiaan Janssen committed
103
    QmlProfilerModelManager *m_profilerModelManager;
Christiaan Janssen's avatar
Christiaan Janssen committed
104 105

    QmlProfilerViewManager *m_viewContainer;
106
    Utils::FileInProjectFinder m_projectFinder;
107
    QToolButton *m_recordButton;
108
    QMenu *m_recordFeaturesMenu;
109

110
    QToolButton *m_clearButton;
Christiaan Janssen's avatar
Christiaan Janssen committed
111 112 113 114 115 116

    // elapsed time display
    QTimer m_recordingTimer;
    QTime m_recordingElapsedTime;
    QLabel *m_timeLabel;

117
    // open search
118 119
    QToolButton *m_searchButton;

120 121 122 123
    // hide and show categories
    QToolButton *m_displayFeaturesButton;
    QMenu *m_displayFeaturesMenu;

Christiaan Janssen's avatar
Christiaan Janssen committed
124
    // save and load actions
125
    QAction *m_saveQmlTrace;
Christiaan Janssen's avatar
Christiaan Janssen committed
126
    QAction *m_loadQmlTrace;
Christiaan Janssen's avatar
Christiaan Janssen committed
127 128
};

129
QmlProfilerTool::QmlProfilerTool(QObject *parent)
hjk's avatar
hjk committed
130
    : QObject(parent), d(new QmlProfilerToolPrivate)
Christiaan Janssen's avatar
Christiaan Janssen committed
131
{
132
    setObjectName(QLatin1String("QmlProfilerTool"));
hjk's avatar
hjk committed
133

Christiaan Janssen's avatar
Christiaan Janssen committed
134 135
    d->m_profilerState = 0;
    d->m_viewContainer = 0;
136
    d->m_recordButton = 0;
137
    d->m_recordFeaturesMenu = 0;
138 139
    d->m_clearButton = 0;
    d->m_timeLabel = 0;
140
    d->m_searchButton = 0;
141 142
    d->m_displayFeaturesButton = 0;
    d->m_displayFeaturesMenu = 0;
143

Christiaan Janssen's avatar
Christiaan Janssen committed
144
    d->m_profilerState = new QmlProfilerStateManager(this);
145 146 147 148 149 150
    connect(d->m_profilerState, &QmlProfilerStateManager::stateChanged,
            this, &QmlProfilerTool::profilerStateChanged);
    connect(d->m_profilerState, &QmlProfilerStateManager::clientRecordingChanged,
            this, &QmlProfilerTool::clientRecordingChanged);
    connect(d->m_profilerState, &QmlProfilerStateManager::serverRecordingChanged,
            this, &QmlProfilerTool::serverRecordingChanged);
151 152
    connect(d->m_profilerState, &QmlProfilerStateManager::recordedFeaturesChanged,
            this, &QmlProfilerTool::setRecordedFeatures);
Christiaan Janssen's avatar
Christiaan Janssen committed
153 154 155

    d->m_profilerConnections = new QmlProfilerClientManager(this);
    d->m_profilerConnections->registerProfilerStateManager(d->m_profilerState);
156 157
    connect(d->m_profilerConnections, &QmlProfilerClientManager::connectionClosed,
            this, &QmlProfilerTool::clientsDisconnected);
Christiaan Janssen's avatar
Christiaan Janssen committed
158

Christiaan Janssen's avatar
Christiaan Janssen committed
159
    d->m_profilerModelManager = new QmlProfilerModelManager(&d->m_projectFinder, this);
160 161 162 163
    connect(d->m_profilerModelManager, &QmlProfilerModelManager::stateChanged,
            this, &QmlProfilerTool::profilerDataModelStateChanged);
    connect(d->m_profilerModelManager, &QmlProfilerModelManager::error,
            this, &QmlProfilerTool::showErrorDialog);
164 165
    connect(d->m_profilerModelManager, &QmlProfilerModelManager::availableFeaturesChanged,
            this, &QmlProfilerTool::setAvailableFeatures);
166
    connect(d->m_profilerModelManager, &QmlProfilerModelManager::saveFinished,
167 168 169
            this, &QmlProfilerTool::onLoadSaveFinished);
    connect(d->m_profilerModelManager, &QmlProfilerModelManager::loadFinished,
            this, &QmlProfilerTool::onLoadSaveFinished);
170

Christiaan Janssen's avatar
Christiaan Janssen committed
171
    d->m_profilerConnections->setModelManager(d->m_profilerModelManager);
172 173
    Command *command = 0;

hjk's avatar
hjk committed
174 175
    ActionContainer *menu = ActionManager::actionContainer(M_DEBUG_ANALYZER);
    ActionContainer *options = ActionManager::createMenu(M_DEBUG_ANALYZER_QML_OPTIONS);
176 177 178 179
    options->menu()->setTitle(tr("QML Profiler Options"));
    menu->addMenu(options, G_ANALYZER_OPTIONS);
    options->menu()->setEnabled(true);

Christiaan Janssen's avatar
Christiaan Janssen committed
180
    QAction *act = d->m_loadQmlTrace = new QAction(tr("Load QML Trace"), options);
181
    command = ActionManager::registerAction(act, Constants::QmlProfilerLoadActionId);
182
    connect(act, &QAction::triggered, this, &QmlProfilerTool::showLoadDialog);
183 184 185 186
    options->addAction(command);

    act = d->m_saveQmlTrace = new QAction(tr("Save QML Trace"), options);
    d->m_saveQmlTrace->setEnabled(false);
187
    command = ActionManager::registerAction(act, Constants::QmlProfilerSaveActionId);
188
    connect(act, &QAction::triggered, this, &QmlProfilerTool::showSaveDialog);
189
    options->addAction(command);
Christiaan Janssen's avatar
Christiaan Janssen committed
190 191

    d->m_recordingTimer.setInterval(100);
192
    connect(&d->m_recordingTimer, &QTimer::timeout, this, &QmlProfilerTool::updateTimeDisplay);
Christiaan Janssen's avatar
Christiaan Janssen committed
193 194 195 196 197 198 199
}

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

200 201 202 203 204 205 206 207 208
static QString sysroot(RunConfiguration *runConfig)
{
    QTC_ASSERT(runConfig, return QString());
    Kit *k = runConfig->target()->kit();
    if (k && SysRootKitInformation::hasSysRoot(k))
        return SysRootKitInformation::sysRoot(runConfig->target()->kit()).toString();
    return QString();
}

209
AnalyzerRunControl *QmlProfilerTool::createRunControl(const AnalyzerStartParameters &sp,
210
    RunConfiguration *runConfiguration)
Christiaan Janssen's avatar
Christiaan Janssen committed
211
{
212 213 214 215 216 217 218 219 220
    QmlProfilerRunConfigurationAspect *aspect = static_cast<QmlProfilerRunConfigurationAspect *>(
                runConfiguration->extraAspect(Constants::SETTINGS));
    QTC_ASSERT(aspect, return 0);

    QmlProfilerSettings *settings = static_cast<QmlProfilerSettings *>(aspect->currentSettings());
    QTC_ASSERT(settings, return 0);

    d->m_profilerConnections->setFlushInterval(settings->flushEnabled() ?
                                                   settings->flushInterval() : 0);
221
    d->m_profilerConnections->setAggregateTraces(settings->aggregateTraces());
222

223
    QmlProfilerRunControl *engine = new QmlProfilerRunControl(sp, runConfiguration);
224

Christiaan Janssen's avatar
Christiaan Janssen committed
225 226
    engine->registerProfilerStateManager(d->m_profilerState);

227
    // FIXME: Check that there's something sensible in sp.connParams
228 229 230
    if (!sp.analyzerSocket.isEmpty())
        d->m_profilerConnections->setLocalSocket(sp.analyzerSocket);
    else
231
        d->m_profilerConnections->setTcpConnection(sp.analyzerHost, sp.analyzerPort);
232

233 234 235 236 237
    //
    // Initialize m_projectFinder
    //

    QString projectDirectory;
238 239
    if (runConfiguration) {
        Project *project = runConfiguration->target()->project();
240
        projectDirectory = project->projectDirectory().toString();
241
    }
242

243
    populateFileFinder(projectDirectory, sysroot(runConfiguration));
244

245 246 247
    if (sp.analyzerSocket.isEmpty())
        connect(engine, &QmlProfilerRunControl::processRunning,
                d->m_profilerConnections, &QmlProfilerClientManager::connectTcpClient);
248 249
    connect(d->m_profilerConnections, &QmlProfilerClientManager::connectionFailed,
            engine, &QmlProfilerRunControl::cancelProcess);
Christiaan Janssen's avatar
Christiaan Janssen committed
250 251 252 253

    return engine;
}

254 255
QWidget *QmlProfilerTool::createWidgets()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
256
    QTC_ASSERT(!d->m_viewContainer, return 0);
257

Christiaan Janssen's avatar
Christiaan Janssen committed
258

Christiaan Janssen's avatar
Christiaan Janssen committed
259
    d->m_viewContainer = new QmlProfilerViewManager(this,
Christiaan Janssen's avatar
Christiaan Janssen committed
260
                                                    d->m_profilerModelManager,
Christiaan Janssen's avatar
Christiaan Janssen committed
261
                                                    d->m_profilerState);
262 263
    connect(d->m_viewContainer, &QmlProfilerViewManager::gotoSourceLocation,
            this, &QmlProfilerTool::gotoSourceLocation);
264

265 266 267
    //
    // Toolbar
    //
Christiaan Janssen's avatar
Christiaan Janssen committed
268 269 270 271 272 273 274
    QWidget *toolbarWidget = new QWidget;
    toolbarWidget->setObjectName(QLatin1String("QmlProfilerToolBarWidget"));

    QHBoxLayout *layout = new QHBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);

275 276 277
    d->m_recordButton = new QToolButton(toolbarWidget);
    d->m_recordButton->setCheckable(true);

278 279
    connect(d->m_recordButton,&QAbstractButton::clicked,
            this, &QmlProfilerTool::recordingButtonChanged);
280
    d->m_recordButton->setChecked(true);
281 282
    d->m_recordFeaturesMenu = new QMenu(d->m_recordButton);
    d->m_recordButton->setMenu(d->m_recordFeaturesMenu);
283
    d->m_recordButton->setPopupMode(QToolButton::MenuButtonPopup);
284 285
    connect(d->m_recordFeaturesMenu, &QMenu::triggered,
            this, &QmlProfilerTool::toggleRequestedFeature);
286

Christiaan Janssen's avatar
Christiaan Janssen committed
287
    setRecording(d->m_profilerState->clientRecording());
288 289
    layout->addWidget(d->m_recordButton);

290
    d->m_clearButton = new QToolButton(toolbarWidget);
291
    d->m_clearButton->setIcon(Icons::CLEAN_PANE.icon());
Christiaan Janssen's avatar
Christiaan Janssen committed
292
    d->m_clearButton->setToolTip(tr("Discard data"));
Christiaan Janssen's avatar
Christiaan Janssen committed
293

294 295 296 297
    connect(d->m_clearButton, &QAbstractButton::clicked, [this](){
        if (checkForUnsavedNotes())
            clearData();
    });
Christiaan Janssen's avatar
Christiaan Janssen committed
298

299 300
    layout->addWidget(d->m_clearButton);

301
    d->m_searchButton = new QToolButton;
302
    d->m_searchButton->setIcon(Icons::ZOOM.icon());
303
    d->m_searchButton->setToolTip(tr("Search timeline event notes."));
304 305
    layout->addWidget(d->m_searchButton);

306
    connect(d->m_searchButton, &QToolButton::clicked, this, &QmlProfilerTool::showTimeLineSearch);
307

308
    d->m_displayFeaturesButton = new QToolButton;
309
    d->m_displayFeaturesButton->setIcon(Icons::FILTER.icon());
310 311
    d->m_displayFeaturesButton->setToolTip(tr("Hide or show event categories."));
    d->m_displayFeaturesButton->setPopupMode(QToolButton::InstantPopup);
312
    d->m_displayFeaturesButton->setProperty("noArrow", true);
313 314 315 316 317 318
    d->m_displayFeaturesMenu = new QMenu(d->m_displayFeaturesButton);
    d->m_displayFeaturesButton->setMenu(d->m_displayFeaturesMenu);
    connect(d->m_displayFeaturesMenu, &QMenu::triggered,
            this, &QmlProfilerTool::toggleVisibleFeature);
    layout->addWidget(d->m_displayFeaturesButton);

Ulf Hermann's avatar
Ulf Hermann committed
319 320 321 322 323 324 325 326
    d->m_timeLabel = new QLabel();
    QPalette palette;
    palette.setColor(QPalette::WindowText, Qt::white);
    d->m_timeLabel->setPalette(palette);
    d->m_timeLabel->setIndent(10);
    updateTimeDisplay();
    layout->addWidget(d->m_timeLabel);

327
    layout->addStretch();
328

Christiaan Janssen's avatar
Christiaan Janssen committed
329
    toolbarWidget->setLayout(layout);
330 331
    setAvailableFeatures(d->m_profilerModelManager->availableFeatures());
    setRecordedFeatures(0);
Christiaan Janssen's avatar
Christiaan Janssen committed
332

333 334 335 336
    // When the widgets are requested we assume that the session data
    // is available, then we can populate the file finder
    populateFileFinder();

Christiaan Janssen's avatar
Christiaan Janssen committed
337
    return toolbarWidget;
Christiaan Janssen's avatar
Christiaan Janssen committed
338 339
}

340 341 342 343
void QmlProfilerTool::populateFileFinder(QString projectDirectory, QString activeSysroot)
{
    // Initialize filefinder with some sensible default
    QStringList sourceFiles;
hjk's avatar
hjk committed
344 345
    QList<Project *> projects = SessionManager::projects();
    if (Project *startupProject = SessionManager::startupProject()) {
346
        // startup project first
hjk's avatar
hjk committed
347
        projects.removeOne(startupProject);
348 349 350 351 352 353
        projects.insert(0, startupProject);
    }
    foreach (Project *project, projects)
        sourceFiles << project->files(Project::ExcludeGeneratedFiles);

    if (!projects.isEmpty()) {
354
        if (projectDirectory.isEmpty())
355
            projectDirectory = projects.first()->projectDirectory().toString();
356 357 358 359 360 361 362 363 364 365 366 367 368

        if (activeSysroot.isEmpty()) {
            if (Target *target = projects.first()->activeTarget())
                if (RunConfiguration *rc = target->activeRunConfiguration())
                    activeSysroot = sysroot(rc);
        }
    }

    d->m_projectFinder.setProjectDirectory(projectDirectory);
    d->m_projectFinder.setProjectFiles(sourceFiles);
    d->m_projectFinder.setSysroot(activeSysroot);
}

369
void QmlProfilerTool::recordingButtonChanged(bool recording)
370
{
371 372 373
    // clientRecording is our intention for new sessions. That may differ from the state of the
    // current session, as indicated by the button. To synchronize it, toggle once.

374 375
    if (recording && d->m_profilerState->currentState() == QmlProfilerStateManager::AppRunning) {
        if (checkForUnsavedNotes()) {
376 377 378
            if (!d->m_profilerConnections->aggregateTraces() ||
                    d->m_profilerModelManager->state() == QmlProfilerModelManager::Done)
                clearData(); // clear before the recording starts, unless we aggregate recordings
379 380
            if (d->m_profilerState->clientRecording())
                d->m_profilerState->setClientRecording(false);
381 382 383 384 385
            d->m_profilerState->setClientRecording(true);
        } else {
            d->m_recordButton->setChecked(false);
        }
    } else {
386 387
        if (d->m_profilerState->clientRecording() == recording)
            d->m_profilerState->setClientRecording(!recording);
388 389
        d->m_profilerState->setClientRecording(recording);
    }
390 391 392 393
}

void QmlProfilerTool::setRecording(bool recording)
{
Christiaan Janssen's avatar
Christiaan Janssen committed
394
    // update display
395 396 397 398 399
    d->m_recordButton->setToolTip( recording ? tr("Disable profiling") : tr("Enable profiling"));
    d->m_recordButton->setIcon(QIcon(recording ? QLatin1String(":/qmlprofiler/recordOn.png") :
                                                 QLatin1String(":/qmlprofiler/recordOff.png")));

    d->m_recordButton->setChecked(recording);
Christiaan Janssen's avatar
Christiaan Janssen committed
400

Christiaan Janssen's avatar
Christiaan Janssen committed
401 402 403 404 405 406 407 408 409
    // manage timer
    if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppRunning) {
        if (recording) {
            d->m_recordingTimer.start();
            d->m_recordingElapsedTime.start();
        } else {
            d->m_recordingTimer.stop();
        }
    }
410 411
}

412
void QmlProfilerTool::gotoSourceLocation(const QString &fileUrl, int lineNumber, int columnNumber)
Christiaan Janssen's avatar
Christiaan Janssen committed
413
{
Kai Koehne's avatar
Kai Koehne committed
414
    if (lineNumber < 0 || fileUrl.isEmpty())
Christiaan Janssen's avatar
Christiaan Janssen committed
415 416
        return;

417
    const QString projectFileName = d->m_projectFinder.findFile(fileUrl);
Christiaan Janssen's avatar
Christiaan Janssen committed
418

419 420 421 422
    QFileInfo fileInfo(projectFileName);
    if (!fileInfo.exists() || !fileInfo.isReadable())
        return;

423 424 425 426
    // The text editors count columns starting with 0, but the ASTs store the
    // location starting with 1, therefore the -1.
    EditorManager::openEditorAt(projectFileName, lineNumber, columnNumber - 1, Id(),
                                EditorManager::DoNotSwitchToDesignMode);
Christiaan Janssen's avatar
Christiaan Janssen committed
427
}
Christiaan Janssen's avatar
Christiaan Janssen committed
428

Christiaan Janssen's avatar
Christiaan Janssen committed
429
void QmlProfilerTool::updateTimeDisplay()
430
{
Christiaan Janssen's avatar
Christiaan Janssen committed
431
    double seconds = 0;
432 433 434 435 436 437
    switch (d->m_profilerState->currentState()) {
    case QmlProfilerStateManager::AppStopRequested:
    case QmlProfilerStateManager::AppDying:
        return; // Transitional state: don't update the display.
    case QmlProfilerStateManager::AppRunning:
        if (d->m_profilerState->serverRecording()) {
Christiaan Janssen's avatar
Christiaan Janssen committed
438
            seconds = d->m_recordingElapsedTime.elapsed() / 1000.0;
439 440 441 442 443 444 445
            break;
        } // else fall through
    case QmlProfilerStateManager::Idle:
        if (d->m_profilerModelManager->state() != QmlProfilerModelManager::Empty &&
               d->m_profilerModelManager->state() != QmlProfilerModelManager::ClearingData)
            seconds = d->m_profilerModelManager->traceTime()->duration() / 1.0e9;
        break;
Christiaan Janssen's avatar
Christiaan Janssen committed
446
    }
447
    QString timeString = QString::number(seconds,'f',1);
Christiaan Janssen's avatar
Christiaan Janssen committed
448 449
    QString profilerTimeStr = QmlProfilerTool::tr("%1 s").arg(timeString, 6);
    d->m_timeLabel->setText(tr("Elapsed: %1").arg(profilerTimeStr));
450 451
}

452
void QmlProfilerTool::showTimeLineSearch()
453
{
454 455
    d->m_viewContainer->raiseTimeline();
    Core::FindPlugin::instance()->openFindToolBar(Core::FindPlugin::FindForwardDirection);
456 457
}

Christiaan Janssen's avatar
Christiaan Janssen committed
458
void QmlProfilerTool::clearData()
459
{
Christiaan Janssen's avatar
Christiaan Janssen committed
460
    d->m_profilerModelManager->clear();
461
    d->m_profilerConnections->discardPendingData();
462
    setRecordedFeatures(0);
Christiaan Janssen's avatar
Christiaan Janssen committed
463
}
464

465 466
void QmlProfilerTool::clearDisplay()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
467 468 469
    d->m_profilerConnections->clearBufferedData();
    d->m_viewContainer->clear();
    updateTimeDisplay();
470 471
}

472 473 474 475 476 477 478 479
void QmlProfilerTool::setButtonsEnabled(bool enable)
{
    d->m_clearButton->setEnabled(enable);
    d->m_displayFeaturesButton->setEnabled(enable);
    d->m_searchButton->setEnabled(enable);
    d->m_recordFeaturesMenu->setEnabled(enable);
}

480
bool QmlProfilerTool::prepareTool()
481
{
482
    if (d->m_recordButton->isChecked()) {
483 484 485 486 487 488
        if (checkForUnsavedNotes()) {
            clearData(); // clear right away to suppress second warning on server recording change
            return true;
        } else {
            return false;
        }
489
    }
490
    return true;
491 492 493 494 495 496
}

void QmlProfilerTool::startRemoteTool()
{
    AnalyzerManager::showMode();

497
    Id kitId;
498
    quint16 port;
499
    Kit *kit = 0;
500 501

    {
hjk's avatar
hjk committed
502
        QSettings *settings = ICore::settings();
503

504 505
        kitId = Id::fromSetting(settings->value(QLatin1String("AnalyzerQmlAttachDialog/kitId")));
        port = settings->value(QLatin1String("AnalyzerQmlAttachDialog/port"), 3768).toUInt();
506 507 508

        QmlProfilerAttachDialog dialog;

509
        dialog.setKitId(kitId);
510 511 512 513 514
        dialog.setPort(port);

        if (dialog.exec() != QDialog::Accepted)
            return;

515
        kit = dialog.kit();
516 517
        port = dialog.port();

518
        settings->setValue(QLatin1String("AnalyzerQmlAttachDialog/kitId"), kit->id().toSetting());
519 520
        settings->setValue(QLatin1String("AnalyzerQmlAttachDialog/port"), port);
    }
521

522
    AnalyzerStartParameters sp;
523 524 525 526

    IDevice::ConstPtr device = DeviceKitInformation::device(kit);
    if (device) {
        sp.connParams = device->sshParameters();
hjk's avatar
hjk committed
527
        sp.analyzerHost = device->qmlProfilerHost();
528
    }
529
    sp.analyzerPort = port;
530

531
    AnalyzerRunControl *rc = createRunControl(sp, 0);
532
    ProjectExplorerPlugin::startRunControl(rc, ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
533
}
534

535
void QmlProfilerTool::logState(const QString &msg)
536
{
hjk's avatar
hjk committed
537
    MessageManager::write(msg, MessageManager::Flash);
538 539 540 541
}

void QmlProfilerTool::logError(const QString &msg)
{
hjk's avatar
hjk committed
542
    MessageManager::write(msg);
543
}
544

Christiaan Janssen's avatar
Christiaan Janssen committed
545 546
void QmlProfilerTool::showErrorDialog(const QString &error)
{
hjk's avatar
hjk committed
547
    QMessageBox *errorDialog = new QMessageBox(ICore::mainWindow());
Christiaan Janssen's avatar
Christiaan Janssen committed
548 549 550 551 552 553 554 555 556
    errorDialog->setIcon(QMessageBox::Warning);
    errorDialog->setWindowTitle(tr("QML Profiler"));
    errorDialog->setText(error);
    errorDialog->setStandardButtons(QMessageBox::Ok);
    errorDialog->setDefaultButton(QMessageBox::Ok);
    errorDialog->setModal(false);
    errorDialog->show();
}

557 558 559 560 561
void QmlProfilerTool::showLoadOption()
{
    d->m_loadQmlTrace->setEnabled(!d->m_profilerState->serverRecording());
}

562 563
void QmlProfilerTool::showSaveOption()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
564
    d->m_saveQmlTrace->setEnabled(!d->m_profilerModelManager->isEmpty());
565 566
}

567 568 569 570 571 572 573 574 575
void saveLastTraceFile(const QString &filename)
{
    QmlProfilerSettings *settings = QmlProfilerPlugin::globalSettings();
    if (filename != settings->lastTraceFile()) {
        settings->setLastTraceFile(filename);
        settings->writeGlobalSettings();
    }
}

576 577
void QmlProfilerTool::showSaveDialog()
{
578 579 580 581
    QString filename = QFileDialog::getSaveFileName(
                ICore::mainWindow(), tr("Save QML Trace"),
                QmlProfilerPlugin::globalSettings()->lastTraceFile(),
                tr("QML traces (*%1)").arg(QLatin1String(TraceFileExtension)));
582
    if (!filename.isEmpty()) {
583 584
        if (!filename.endsWith(QLatin1String(TraceFileExtension)))
            filename += QLatin1String(TraceFileExtension);
585
        saveLastTraceFile(filename);
586
        AnalyzerManager::mainWindow()->setEnabled(false);
Christiaan Janssen's avatar
Christiaan Janssen committed
587
        d->m_profilerModelManager->save(filename);
588 589 590 591 592
    }
}

void QmlProfilerTool::showLoadDialog()
{
593 594 595
    if (!checkForUnsavedNotes())
        return;

596
    if (ModeManager::currentMode()->id() != MODE_ANALYZE)
597 598
        AnalyzerManager::showMode();

hjk's avatar
hjk committed
599
    AnalyzerManager::selectAction(QmlProfilerRemoteActionId);
600

601 602 603 604
    QString filename = QFileDialog::getOpenFileName(
                ICore::mainWindow(), tr("Load QML Trace"),
                QmlProfilerPlugin::globalSettings()->lastTraceFile(),
                tr("QML traces (*%1)").arg(QLatin1String(TraceFileExtension)));
605 606

    if (!filename.isEmpty()) {
607
        saveLastTraceFile(filename);
608
        AnalyzerManager::mainWindow()->setEnabled(false);
609 610
        connect(d->m_profilerModelManager, &QmlProfilerModelManager::recordedFeaturesChanged,
                this, &QmlProfilerTool::setRecordedFeatures);
611
        d->m_profilerModelManager->load(filename);
612 613
    }
}
614

615 616
void QmlProfilerTool::onLoadSaveFinished()
{
617 618
    disconnect(d->m_profilerModelManager, &QmlProfilerModelManager::recordedFeaturesChanged,
               this, &QmlProfilerTool::setRecordedFeatures);
619 620 621
    AnalyzerManager::mainWindow()->setEnabled(true);
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
/*!
    Checks if we have unsaved notes. If so, shows a warning dialog. Returns true if we can continue
    with a potentially destructive operation and discard the warnings, or false if not. We don't
    want to show a save/discard dialog here because that will often result in a confusing series of
    different dialogs: first "save" and then immediately "load" or "connect".
 */
bool QmlProfilerTool::checkForUnsavedNotes()
{
    if (!d->m_profilerModelManager->notesModel()->isModified())
        return true;
    return QMessageBox::warning(QApplication::activeWindow(), tr("QML Profiler"),
                                tr("You are about to discard the profiling data, including unsaved "
                                   "notes. Do you want to continue?"),
                                QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes;
}

638 639 640 641 642 643 644 645 646 647 648 649
void QmlProfilerTool::restoreFeatureVisibility()
{
    // Restore the shown/hidden state of features to what the user selected. When clearing data the
    // the model manager sets its features to 0, and models get automatically shown, for the mockup.
    quint64 features = 0;
    foreach (const QAction *action, d->m_displayFeaturesMenu->actions()) {
        if (action->isChecked())
            features |= (1ULL << action->data().toUInt());
    }
    d->m_profilerModelManager->setVisibleFeatures(features);
}

650 651 652
void QmlProfilerTool::clientsDisconnected()
{
    // If the application stopped by itself, check if we have all the data
653 654
    if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppDying ||
            d->m_profilerState->currentState() == QmlProfilerStateManager::Idle) {
655
        if (d->m_profilerModelManager->state() == QmlProfilerModelManager::AcquiringData) {
656 657 658 659 660 661 662
            if (d->m_profilerConnections->aggregateTraces()) {
                d->m_profilerModelManager->acquiringDone();
            } else {
                showNonmodalWarning(tr("Application finished before loading profiled data.\n"
                                       "Please use the stop button instead."));
                d->m_profilerModelManager->clear();
            }
663
        }
664 665

        // ... and return to the "base" state
666 667
        if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppDying)
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::Idle);
668 669 670 671
    }
    // If the connection is closed while the app is still running, no special action is needed
}

672 673 674 675 676 677 678 679 680
void addFeatureToMenu(QMenu *menu, ProfileFeature feature, quint64 enabledFeatures)
{
    QAction *action =
            menu->addAction(QmlProfilerTool::tr(QmlProfilerModelManager::featureName(feature)));
    action->setCheckable(true);
    action->setData(static_cast<uint>(feature));
    action->setChecked(enabledFeatures & (1ULL << (feature)));
}

681
template<ProfileFeature feature>
682
void QmlProfilerTool::updateFeatures(quint64 features)
683
{
684
    if (features & (1ULL << (feature))) {
685 686 687 688
        addFeatureToMenu(d->m_recordFeaturesMenu, feature,
                         d->m_profilerState->requestedFeatures());
        addFeatureToMenu(d->m_displayFeaturesMenu, feature,
                         d->m_profilerModelManager->visibleFeatures());
689
    }
690
    updateFeatures<static_cast<ProfileFeature>(feature + 1)>(features);
691 692 693
}

template<>
694
void QmlProfilerTool::updateFeatures<MaximumProfileFeature>(quint64 features)
695 696 697 698 699 700 701
{
    Q_UNUSED(features);
    return;
}

void QmlProfilerTool::setAvailableFeatures(quint64 features)
{
702 703 704 705 706 707
    if (features != d->m_profilerState->requestedFeatures())
        d->m_profilerState->setRequestedFeatures(features); // by default, enable them all.
    if (d->m_recordFeaturesMenu && d->m_displayFeaturesMenu) {
        d->m_recordFeaturesMenu->clear();
        d->m_displayFeaturesMenu->clear();
        updateFeatures<static_cast<ProfileFeature>(0)>(features);
708 709 710
    }
}

711 712 713 714 715 716
void QmlProfilerTool::setRecordedFeatures(quint64 features)
{
    foreach (QAction *action, d->m_displayFeaturesMenu->actions())
        action->setEnabled(features & (1ULL << action->data().toUInt()));
}

Christiaan Janssen's avatar
Christiaan Janssen committed
717
void QmlProfilerTool::profilerDataModelStateChanged()
718
{
Christiaan Janssen's avatar
Christiaan Janssen committed
719
    switch (d->m_profilerModelManager->state()) {
720
    case QmlProfilerModelManager::Empty :
721 722
        d->m_recordButton->setEnabled(true);
        setButtonsEnabled(true);
723
        break;
724
    case QmlProfilerModelManager::ClearingData :
725 726
        d->m_recordButton->setEnabled(false);
        setButtonsEnabled(false);
Christiaan Janssen's avatar
Christiaan Janssen committed
727 728
        clearDisplay();
        break;
729
    case QmlProfilerModelManager::AcquiringData :
730 731 732
        d->m_recordButton->setEnabled(true); // Press recording button to stop recording
        setButtonsEnabled(false);            // Other buttons disabled
        break;
733
    case QmlProfilerModelManager::ProcessingData :
734 735
        d->m_recordButton->setEnabled(false);
        setButtonsEnabled(false);
Christiaan Janssen's avatar
Christiaan Janssen committed
736
        break;
737
    case QmlProfilerModelManager::Done :
Christiaan Janssen's avatar
Christiaan Janssen committed
738
        if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppStopRequested)
739
            d->m_profilerState->setCurrentState(QmlProfilerStateManager::Idle);
Christiaan Janssen's avatar
Christiaan Janssen committed
740 741
        showSaveOption();
        updateTimeDisplay();
742
        restoreFeatureVisibility();
743 744
        d->m_recordButton->setEnabled(true);
        setButtonsEnabled(true);
Christiaan Janssen's avatar
Christiaan Janssen committed
745 746 747 748 749 750
    break;
    default:
        break;
    }
}

751
QList <QAction *> QmlProfilerTool::profilerContextMenuActions()
Christiaan Janssen's avatar
Christiaan Janssen committed
752 753
{
    QList <QAction *> commonActions;
754 755 756 757 758 759 760 761 762
    ActionManager *manager = ActionManager::instance();
    if (manager) {
        Command *command = manager->command(Constants::QmlProfilerLoadActionId);
        if (command)
            commonActions << command->action();
        command = manager->command(Constants::QmlProfilerSaveActionId);
        if (command)
            commonActions << command->action();
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
763 764 765 766 767
    return commonActions;
}

void QmlProfilerTool::showNonmodalWarning(const QString &warningMsg)
{
hjk's avatar
hjk committed
768
    QMessageBox *noExecWarning = new QMessageBox(ICore::mainWindow());
Christiaan Janssen's avatar
Christiaan Janssen committed
769 770 771 772 773 774 775 776 777 778 779
    noExecWarning->setIcon(QMessageBox::Warning);
    noExecWarning->setWindowTitle(tr("QML Profiler"));
    noExecWarning->setText(warningMsg);
    noExecWarning->setStandardButtons(QMessageBox::Ok);
    noExecWarning->setDefaultButton(QMessageBox::Ok);
    noExecWarning->setModal(false);
    noExecWarning->show();
}

QMessageBox *QmlProfilerTool::requestMessageBox()
{
hjk's avatar
hjk committed
780
    return new QMessageBox(ICore::mainWindow());
Christiaan Janssen's avatar
Christiaan Janssen committed
781 782 783 784
}

void QmlProfilerTool::handleHelpRequest(const QString &link)
{
785
    HelpManager::handleHelpRequest(link);
786
}
787

Christiaan Janssen's avatar
Christiaan Janssen committed
788
void QmlProfilerTool::profilerStateChanged()
789
{
Christiaan Janssen's avatar
Christiaan Janssen committed
790
    switch (d->m_profilerState->currentState()) {
791 792 793
    case QmlProfilerStateManager::AppDying : {
        // If already disconnected when dying, check again that all data was read
        if (!d->m_profilerConnections->isConnected())
794
            QTimer::singleShot(0, this, &QmlProfilerTool::clientsDisconnected);
795 796
        break;
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
797 798 799 800
    case QmlProfilerStateManager::Idle :
        // when the app finishes, set recording display to client status
        setRecording(d->m_profilerState->clientRecording());
        break;
801 802 803 804 805
    case QmlProfilerStateManager::AppStopRequested:
        // Don't allow toggling the recording while data is loaded when application quits
        if (d->m_profilerState->serverRecording())
            d->m_recordButton->setEnabled(false);
        break;
Christiaan Janssen's avatar
Christiaan Janssen committed
806 807
    default:
        // no special action needed for other states
808 809
        break;
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
810 811 812 813 814 815
}

void QmlProfilerTool::clientRecordingChanged()
{
    // if application is running, display server record changes
    // if application is stopped, display client record changes
816
    if (d->m_profilerState->currentState() != QmlProfilerStateManager::AppRunning)
Christiaan Janssen's avatar
Christiaan Janssen committed
817
        setRecording(d->m_profilerState->clientRecording());
818
}
819

Christiaan Janssen's avatar
Christiaan Janssen committed
820
void QmlProfilerTool::serverRecordingChanged()
821
{
822
    showLoadOption();
Christiaan Janssen's avatar
Christiaan Janssen committed
823 824 825
    if (d->m_profilerState->currentState() == QmlProfilerStateManager::AppRunning) {
        // clear the old data each time we start a new profiling session
        if (d->m_profilerState->serverRecording()) {
826 827 828 829 830 831 832 833 834 835 836 837
            // We cannot stop it here, so we cannot give the usual yes/no dialog. Show a dialog
            // offering to immediately save the data instead.
            if (d->m_profilerModelManager->notesModel()->isModified() &&
                    QMessageBox::warning(QApplication::activeWindow(), tr("QML Profiler"),
                                         tr("Starting a new profiling session will discard the "
                                            "previous data, including unsaved notes.\nDo you want "
                                            "to save the data first?"),
                                         QMessageBox::Save, QMessageBox::Discard) ==
                    QMessageBox::Save)
                showSaveDialog();

            setRecording(true);
838 839 840
            if (!d->m_profilerConnections->aggregateTraces() ||
                    d->m_profilerModelManager->state() == QmlProfilerModelManager::Done)
                clearData();
Christiaan Janssen's avatar
Christiaan Janssen committed
841 842
            d->m_profilerModelManager->prepareForWriting();
        } else {
843
            setRecording(false);
844 845

            // changes back once loading is finished, see profilerDataModelStateChanged()
846 847
            if (!d->m_profilerConnections->aggregateTraces())
                d->m_recordButton->setEnabled(false);
Christiaan Janssen's avatar
Christiaan Janssen committed
848
        }
849 850
    }
}
851

852
void QmlProfilerTool::toggleRequestedFeature(QAction *action)
853 854 855
{
    uint feature = action->data().toUInt();
    if (action->isChecked())
856 857
        d->m_profilerState->setRequestedFeatures(
                    d->m_profilerState->requestedFeatures() | (1ULL << feature));