Commit 1a82bedc authored by hjk's avatar hjk
Browse files

QmlProfiler: Merge LocalQmlProfilerRunner and QmlProfilerRunner



Also streamline code paths in the result.

Change-Id: Id7d96343a8f778ba8f415b1a850cc78576afa475
Reviewed-by: Christian Stenger's avatarChristian Stenger <christian.stenger@qt.io>
parent 8116cb39
/****************************************************************************
**
** 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 "localqmlprofilerrunner.h"
#include "qmlprofilerplugin.h"
#include "qmlprofilerruncontrol.h"
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/environmentaspect.h>
#include <projectexplorer/devicesupport/idevice.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/target.h>
#include <qmldebug/qmldebugcommandlinearguments.h>
#include <utils/temporaryfile.h>
#include <QTcpServer>
#include <QTemporaryFile>
using namespace ProjectExplorer;
namespace QmlProfiler {
QString LocalQmlProfilerRunner::findFreeSocket()
{
Utils::TemporaryFile file("qmlprofiler-freesocket");
if (file.open()) {
return file.fileName();
} else {
qWarning() << "Could not open a temporary file to find a debug socket.";
return QString();
}
}
Utils::Port LocalQmlProfilerRunner::findFreePort(QString &host)
{
QTcpServer server;
if (!server.listen(QHostAddress::LocalHost)
&& !server.listen(QHostAddress::LocalHostIPv6)) {
qWarning() << "Cannot open port on host for QML profiling.";
return Utils::Port();
}
host = server.serverAddress().toString();
return Utils::Port(server.serverPort());
}
LocalQmlProfilerRunner::LocalQmlProfilerRunner(const Configuration &configuration,
QmlProfilerRunner *runner) :
QObject(runner->runControl()),
m_runControl(runner->runControl()),
m_configuration(configuration)
{
auto runControl = runner->runControl();
connect(&m_launcher, &ApplicationLauncher::appendMessage,
this, &LocalQmlProfilerRunner::appendMessage);
connect(this, &LocalQmlProfilerRunner::stopped,
runner, &QmlProfilerRunner::notifyRemoteFinished);
connect(this, &LocalQmlProfilerRunner::appendMessage,
runControl, &RunControl::appendMessage);
connect(runControl, &RunControl::starting,
this, &LocalQmlProfilerRunner::start);
connect(runControl, &RunControl::finished,
this, &LocalQmlProfilerRunner::stop);
m_outputParser.setNoOutputText(ApplicationLauncher::msgWinCannotRetrieveDebuggingOutput());
connect(runControl, &RunControl::appendMessageRequested,
this, [this](RunControl *runControl, const QString &msg, Utils::OutputFormat format) {
Q_UNUSED(runControl);
Q_UNUSED(format);
m_outputParser.processOutput(msg);
});
connect(&m_outputParser, &QmlDebug::QmlOutputParser::waitingForConnectionOnPort,
runControl, [this, runControl, runner](Utils::Port port) {
runControl->notifyRemoteSetupDone(port);
runner->notifyRemoteSetupDone(port);
});
connect(&m_outputParser, &QmlDebug::QmlOutputParser::noOutputMessage,
runControl, [this, runControl, runner]() {
runControl->notifyRemoteSetupDone(Utils::Port());
runner->notifyRemoteSetupDone(Utils::Port());
});
connect(&m_outputParser, &QmlDebug::QmlOutputParser::connectingToSocketMessage,
runControl, [this, runControl, runner]() {
runControl->notifyRemoteSetupDone(Utils::Port());
runner->notifyRemoteSetupDone(Utils::Port());
});
connect(&m_outputParser, &QmlDebug::QmlOutputParser::errorMessage,
runControl, [this, runControl, runner](const QString &message) {
runControl->notifyRemoteSetupFailed(message);
runner->notifyRemoteSetupFailed(message);
});
}
void LocalQmlProfilerRunner::start()
{
QTC_ASSERT(!m_configuration.socket.isEmpty() || m_configuration.port.isValid(), return);
StandardRunnable runnable = m_configuration.debuggee;
QString arguments = m_configuration.socket.isEmpty() ?
QmlDebug::qmlDebugTcpArguments(QmlDebug::QmlProfilerServices,
m_configuration.port) :
QmlDebug::qmlDebugLocalArguments(QmlDebug::QmlProfilerServices,
m_configuration.socket);
if (!m_configuration.debuggee.commandLineArguments.isEmpty())
arguments += QLatin1Char(' ') + m_configuration.debuggee.commandLineArguments;
runnable.commandLineArguments = arguments;
runnable.runMode = ApplicationLauncher::Gui;
// queue this, as the process can already die in the call to start().
// We want the started() signal to be emitted before the stopped() signal.
connect(&m_launcher, &ApplicationLauncher::processExited,
this, &LocalQmlProfilerRunner::spontaneousStop,
Qt::QueuedConnection);
m_launcher.start(runnable);
emit started();
}
void LocalQmlProfilerRunner::spontaneousStop(int exitCode, QProcess::ExitStatus status)
{
Q_UNUSED(exitCode);
Q_UNUSED(status);
disconnect(&m_launcher, &ApplicationLauncher::processExited,
this, &LocalQmlProfilerRunner::spontaneousStop);
emit stopped();
}
void LocalQmlProfilerRunner::stop()
{
if (m_launcher.isRunning())
m_launcher.stop();
}
} // namespace QmlProfiler
/****************************************************************************
**
** 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.
**
****************************************************************************/
#pragma once
#include "qmlprofiler_global.h"
#include <utils/environment.h>
#include <utils/port.h>
#include <projectexplorer/applicationlauncher.h>
#include <projectexplorer/runnables.h>
#include <qmldebug/qmloutputparser.h>
namespace QmlProfiler {
class QmlProfilerRunner;
class QMLPROFILER_EXPORT LocalQmlProfilerRunner : public QObject
{
Q_OBJECT
public:
struct Configuration {
ProjectExplorer::StandardRunnable debuggee;
Utils::Port port;
QString socket;
};
LocalQmlProfilerRunner(const Configuration &configuration,
QmlProfilerRunner *runner);
static Utils::Port findFreePort(QString &host);
static QString findFreeSocket();
signals:
void started();
void stopped();
void appendMessage(const QString &message, Utils::OutputFormat format);
private:
void spontaneousStop(int exitCode, QProcess::ExitStatus status);
void start();
void stop();
ProjectExplorer::RunControl *m_runControl;
Configuration m_configuration;
ProjectExplorer::ApplicationLauncher m_launcher;
QmlDebug::QmlOutputParser m_outputParser;
};
} // namespace QmlProfiler
...@@ -9,7 +9,6 @@ SOURCES += \ ...@@ -9,7 +9,6 @@ SOURCES += \
flamegraphmodel.cpp \ flamegraphmodel.cpp \
flamegraphview.cpp \ flamegraphview.cpp \
inputeventsmodel.cpp \ inputeventsmodel.cpp \
localqmlprofilerrunner.cpp \
memoryusagemodel.cpp \ memoryusagemodel.cpp \
pixmapcachemodel.cpp \ pixmapcachemodel.cpp \
qmlevent.cpp \ qmlevent.cpp \
...@@ -50,7 +49,6 @@ HEADERS += \ ...@@ -50,7 +49,6 @@ HEADERS += \
flamegraphmodel.h \ flamegraphmodel.h \
flamegraphview.h \ flamegraphview.h \
inputeventsmodel.h \ inputeventsmodel.h \
localqmlprofilerrunner.h \
memoryusagemodel.h \ memoryusagemodel.h \
pixmapcachemodel.h \ pixmapcachemodel.h \
qmlevent.h \ qmlevent.h \
......
...@@ -24,7 +24,6 @@ QtcPlugin { ...@@ -24,7 +24,6 @@ QtcPlugin {
"flamegraphmodel.cpp", "flamegraphmodel.h", "flamegraphmodel.cpp", "flamegraphmodel.h",
"flamegraphview.cpp", "flamegraphview.h", "flamegraphview.cpp", "flamegraphview.h",
"inputeventsmodel.cpp", "inputeventsmodel.h", "inputeventsmodel.cpp", "inputeventsmodel.h",
"localqmlprofilerrunner.cpp", "localqmlprofilerrunner.h",
"memoryusagemodel.cpp", "memoryusagemodel.h", "memoryusagemodel.cpp", "memoryusagemodel.h",
"pixmapcachemodel.cpp", "pixmapcachemodel.h", "pixmapcachemodel.cpp", "pixmapcachemodel.h",
"qmlevent.cpp", "qmlevent.h", "qmlevent.cpp", "qmlevent.h",
......
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
#include <QObject> #include <QObject>
#include <functional> #include <functional>
namespace ProjectExplorer { class RunConfiguration; }
namespace QmlProfiler { namespace QmlProfiler {
class QmlProfilerModelManager; class QmlProfilerModelManager;
class QmlProfilerNotesModel; class QmlProfilerNotesModel;
......
...@@ -23,9 +23,10 @@ ...@@ -23,9 +23,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include "qmlprofilerclientmanager.h"
#include "qmlprofilerruncontrol.h" #include "qmlprofilerruncontrol.h"
#include "localqmlprofilerrunner.h"
#include "qmlprofilertool.h" #include "qmlprofilertool.h"
#include "qmlprofilerplugin.h"
#include <debugger/analyzer/analyzermanager.h> #include <debugger/analyzer/analyzermanager.h>
#include <debugger/analyzer/analyzerstartparameters.h> #include <debugger/analyzer/analyzerstartparameters.h>
...@@ -33,30 +34,38 @@ ...@@ -33,30 +34,38 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/helpmanager.h> #include <coreplugin/helpmanager.h>
#include <projectexplorer/devicesupport/idevice.h>
#include <projectexplorer/environmentaspect.h> #include <projectexplorer/environmentaspect.h>
#include <projectexplorer/kitinformation.h> #include <projectexplorer/kitinformation.h>
#include <projectexplorer/localapplicationruncontrol.h> #include <projectexplorer/localapplicationruncontrol.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorerconstants.h> #include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectexplorericons.h> #include <projectexplorer/projectexplorericons.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/runnables.h> #include <projectexplorer/runnables.h>
#include <projectexplorer/target.h> #include <projectexplorer/target.h>
#include <qtsupport/qtsupportconstants.h> #include <qtsupport/qtsupportconstants.h>
#include <qmldebug/qmloutputparser.h> #include <qmldebug/qmloutputparser.h>
#include <qmldebug/qmldebugcommandlinearguments.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/temporaryfile.h>
#include <QApplication>
#include <QMainWindow> #include <QMainWindow>
#include <QMessageBox> #include <QMessageBox>
#include <QTimer>
#include <QTcpServer>
#include <QApplication>
#include <QMessageBox> #include <QMessageBox>
#include <QPushButton> #include <QPushButton>
#include <QTcpServer>
#include <QTemporaryFile>
#include <QTimer>
using namespace Debugger; using namespace Debugger;
using namespace Core; using namespace Core;
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace QmlProfiler::Internal;
namespace QmlProfiler { namespace QmlProfiler {
...@@ -69,6 +78,10 @@ class QmlProfilerRunner::QmlProfilerRunnerPrivate ...@@ -69,6 +78,10 @@ class QmlProfilerRunner::QmlProfilerRunnerPrivate
public: public:
QmlProfilerStateManager *m_profilerState = 0; QmlProfilerStateManager *m_profilerState = 0;
QTimer m_noDebugOutputTimer; QTimer m_noDebugOutputTimer;
bool m_isLocal = false;
QmlProfilerRunner::Configuration m_configuration;
ProjectExplorer::ApplicationLauncher m_launcher;
QmlDebug::QmlOutputParser m_outputParser;
}; };
// //
...@@ -100,20 +113,48 @@ QmlProfilerRunner::~QmlProfilerRunner() ...@@ -100,20 +113,48 @@ QmlProfilerRunner::~QmlProfilerRunner()
void QmlProfilerRunner::start() void QmlProfilerRunner::start()
{ {
runControl()->reportApplicationStart();
Internal::QmlProfilerTool::instance()->finalizeRunControl(this); Internal::QmlProfilerTool::instance()->finalizeRunControl(this);
QTC_ASSERT(d->m_profilerState, runControl()->reportApplicationStop(); return); QTC_ASSERT(d->m_profilerState, return);
QTC_ASSERT(connection().is<AnalyzerConnection>(), runControl()->reportApplicationStop(); return); QTC_ASSERT(connection().is<AnalyzerConnection>(), return);
auto conn = connection().as<AnalyzerConnection>(); auto conn = connection().as<AnalyzerConnection>();
if (conn.analyzerPort.isValid()) if (conn.analyzerPort.isValid()) {
emit processRunning(conn.analyzerPort); auto clientManager = Internal::QmlProfilerTool::clientManager();
clientManager->setTcpConnection(conn.analyzerHost, conn.analyzerPort);
clientManager->connectToTcpServer();
}
else if (conn.analyzerSocket.isEmpty()) else if (conn.analyzerSocket.isEmpty())
d->m_noDebugOutputTimer.start(); d->m_noDebugOutputTimer.start();
d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppRunning); d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppRunning);
emit runControl()->starting();
if (d->m_isLocal) {
QTC_ASSERT(!d->m_configuration.socket.isEmpty() || d->m_configuration.port.isValid(), return);
StandardRunnable debuggee = runnable().as<StandardRunnable>();
QString arguments = d->m_configuration.socket.isEmpty() ?
QmlDebug::qmlDebugTcpArguments(QmlDebug::QmlProfilerServices,
d->m_configuration.port) :
QmlDebug::qmlDebugLocalArguments(QmlDebug::QmlProfilerServices,
d->m_configuration.socket);
if (!debuggee.commandLineArguments.isEmpty())
arguments += ' ' + debuggee.commandLineArguments;
debuggee.commandLineArguments = arguments;
debuggee.runMode = ApplicationLauncher::Gui;
// queue this, as the process can already die in the call to start().
// We want the started() signal to be emitted before the stopped() signal.
connect(&d->m_launcher, &ApplicationLauncher::processExited,
this, &QmlProfilerRunner::spontaneousStop,
Qt::QueuedConnection);
d->m_launcher.start(debuggee);
emit localRunnerStarted();
}
} }
void QmlProfilerRunner::stop() void QmlProfilerRunner::stop()
...@@ -148,7 +189,6 @@ void QmlProfilerRunner::notifyRemoteFinished() ...@@ -148,7 +189,6 @@ void QmlProfilerRunner::notifyRemoteFinished()
switch (d->m_profilerState->currentState()) { switch (d->m_profilerState->currentState()) {
case QmlProfilerStateManager::AppRunning: case QmlProfilerStateManager::AppRunning:
d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying); d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
runControl()->reportApplicationStop();
break; break;
case QmlProfilerStateManager::Idle: case QmlProfilerStateManager::Idle:
break; break;
...@@ -177,7 +217,7 @@ void QmlProfilerRunner::cancelProcess() ...@@ -177,7 +217,7 @@ void QmlProfilerRunner::cancelProcess()
return; return;
} }
} }
runControl()->reportApplicationStop(); runControl()->initiateStop();
} }
void QmlProfilerRunner::notifyRemoteSetupFailed(const QString &errorMessage) void QmlProfilerRunner::notifyRemoteSetupFailed(const QString &errorMessage)
...@@ -200,7 +240,6 @@ void QmlProfilerRunner::notifyRemoteSetupFailed(const QString &errorMessage) ...@@ -200,7 +240,6 @@ void QmlProfilerRunner::notifyRemoteSetupFailed(const QString &errorMessage)
// KILL // KILL
d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying); d->m_profilerState->setCurrentState(QmlProfilerStateManager::AppDying);
d->m_noDebugOutputTimer.stop(); d->m_noDebugOutputTimer.stop();
runControl()->reportApplicationStop();
} }
void QmlProfilerRunner::wrongSetupMessageBoxFinished(int button) void QmlProfilerRunner::wrongSetupMessageBoxFinished(int button)
...@@ -219,8 +258,11 @@ void QmlProfilerRunner::notifyRemoteSetupDone(Utils::Port port) ...@@ -219,8 +258,11 @@ void QmlProfilerRunner::notifyRemoteSetupDone(Utils::Port port)
QTC_ASSERT(connection().is<AnalyzerConnection>(), return); QTC_ASSERT(connection().is<AnalyzerConnection>(), return);
port = connection().as<AnalyzerConnection>().analyzerPort; port = connection().as<AnalyzerConnection>().analyzerPort;
} }
if (port.isValid()) if (port.isValid()) {
emit processRunning(port); auto clientManager = Internal::QmlProfilerTool::clientManager();
clientManager->setTcpConnection(connection().as<AnalyzerConnection>().analyzerHost, port);
clientManager->connectToTcpServer();
}
} }
void QmlProfilerRunner::registerProfilerStateManager( QmlProfilerStateManager *profilerState ) void QmlProfilerRunner::registerProfilerStateManager( QmlProfilerStateManager *profilerState )
...@@ -242,7 +284,6 @@ void QmlProfilerRunner::profilerStateChanged() ...@@ -242,7 +284,6 @@ void QmlProfilerRunner::profilerStateChanged()
{ {
switch (d->m_profilerState->currentState()) { switch (d->m_profilerState->currentState()) {
case QmlProfilerStateManager::Idle: case QmlProfilerStateManager::Idle:
runControl()->reportApplicationStop();
d->m_noDebugOutputTimer.stop(); d->m_noDebugOutputTimer.stop();
break; break;
default: default:
...@@ -250,4 +291,82 @@ void QmlProfilerRunner::profilerStateChanged() ...@@ -250,4 +291,82 @@ void QmlProfilerRunner::profilerStateChanged()
} }
} }
QString QmlProfilerRunner::findFreeSocket()
{
Utils::TemporaryFile file("qmlprofiler-freesocket");
if (file.open()) {
return file.fileName();
} else {
qWarning() << "Could not open a temporary file to find a debug socket.";
return QString();
}
}
Utils::Port QmlProfilerRunner::findFreePort(QString &host)
{
QTcpServer server;
if (!server.listen(QHostAddress::LocalHost)
&& !server.listen(QHostAddress::LocalHostIPv6)) {
qWarning() << "Cannot open port on host for QML profiling.";
return Utils::Port();
}
host = server.serverAddress().toString();
return Utils::Port(server.serverPort());
}
void QmlProfilerRunner::setLocalConfiguration(const Configuration &configuration)
{
d->m_isLocal = true;
d->m_configuration = configuration;
connect(&d->m_launcher, &ApplicationLauncher::appendMessage,
this, &QmlProfilerRunner::appendMessage);
connect(this, &QmlProfilerRunner::localRunnerStopped,
this, &QmlProfilerRunner::notifyRemoteFinished);
connect(runControl(), &RunControl::finished,
this, &QmlProfilerRunner::stopLocalRunner);
d->m_outputParser.setNoOutputText(ApplicationLauncher::msgWinCannotRetrieveDebuggingOutput());
connect(runControl(), &RunControl::appendMessageRequested,
this, [this](RunControl *, const QString &msg, Utils::OutputFormat) {
d->m_outputParser.processOutput(msg);
});
connect(&d->m_outputParser, &QmlDebug::QmlOutputParser::waitingForConnectionOnPort,
this, [this](Utils::Port port) {
notifyRemoteSetupDone(port);
});
connect(&d->m_outputParser, &QmlDebug::QmlOutputParser::noOutputMessage,
this, [this] {
notifyRemoteSetupDone(Utils::Port());
});
connect(&d->m_outputParser, &QmlDebug::QmlOutputParser::connectingToSocketMessage,
this, [this] {
notifyRemoteSetupDone(Utils::Port());
});
connect(&d->m_outputParser, &QmlDebug::QmlOutputParser::errorMessage,
this, [this](const QString &message) {
notifyRemoteSetupFailed(message);
});
}
void QmlProfilerRunner::spontaneousStop(int exitCode, QProcess::ExitStatus status)
{
Q_UNUSED(exitCode);
Q_UNUSED(status);
disconnect(&d->m_launcher, &ApplicationLauncher::processExited,
this, &QmlProfilerRunner::spontaneousStop);
emit localRunnerStopped();
}