1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 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 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "qmlprofilerapplication.h"
#include "constants.h"
#include <QtCore/QStringList>
#include <QtCore/QProcess>
#include <QtCore/QTimer>
#include <QtCore/QDateTime>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
#include <QtCore/QCommandLineParser>
#include <QtCore/QTemporaryFile>
#include <QtCore/QLibraryInfo>
#include <iostream>
static const char commandTextC[] =
"The following commands are available:\n"
"'r', 'record'\n"
" Switch recording on or off.\n"
"'o [file]', 'output [file]'\n"
" Output profiling data to <file>. If no <file>\n"
" parameter is given, output to whatever was given\n"
" with --output, or standard output.\n"
"'c', 'clear'\n"
" Clear profiling data recorded so far from memory.\n"
"'f [file]', 'flush [file]'\n"
" Stop recording if it is running, then output the\n"
" data, and finally clear it from memory.\n"
"'q', 'quit'\n"
" Terminate the target process if started from\n"
" qmlprofiler, and qmlprofiler itself.";
static const char *features[] = {
"javascript",
"memory",
"pixmapcache",
"scenegraph",
"animations",
"painting",
"compiling",
"creating",
"binding",
"handlingsignal",
"inputevents",
"debugmessages"
};
Q_STATIC_ASSERT(sizeof(features) == MaximumProfileFeature * sizeof(char *));
QmlProfilerApplication::QmlProfilerApplication(int &argc, char **argv) :
QCoreApplication(argc, argv),
m_runMode(LaunchMode),
m_process(nullptr),
m_hostName(QLatin1String("127.0.0.1")),
m_port(0),
m_pendingRequest(REQUEST_NONE),
m_verbose(false),
m_recording(true),
m_interactive(false),
m_connectionAttempts(0)
{
m_connection.reset(new QQmlDebugConnection);
m_profilerData.reset(new QmlProfilerData);
m_qmlProfilerClient.reset(new QmlProfilerClient(m_connection.data(), m_profilerData.data()));
m_connectTimer.setInterval(1000);
connect(&m_connectTimer, &QTimer::timeout, this, &QmlProfilerApplication::tryToConnect);
connect(m_connection.data(), &QQmlDebugConnection::connected,
this, &QmlProfilerApplication::connected);
connect(m_connection.data(), &QQmlDebugConnection::disconnected,
this, &QmlProfilerApplication::disconnected);
connect(m_qmlProfilerClient.data(), &QmlProfilerClient::enabledChanged,
this, &QmlProfilerApplication::traceClientEnabledChanged);
connect(m_qmlProfilerClient.data(), &QmlProfilerClient::traceStarted,
this, &QmlProfilerApplication::notifyTraceStarted);
connect(m_qmlProfilerClient.data(), &QmlProfilerClient::error,
this, &QmlProfilerApplication::logError);
connect(m_profilerData.data(), &QmlProfilerData::error,
this, &QmlProfilerApplication::logError);
connect(m_profilerData.data(), &QmlProfilerData::dataReady,
this, &QmlProfilerApplication::traceFinished);
}
QmlProfilerApplication::~QmlProfilerApplication()
{
if (!m_process)
return;
logStatus("Terminating process ...");
m_process->disconnect();
m_process->terminate();
if (!m_process->waitForFinished(1000)) {
logStatus("Killing process ...");
m_process->kill();
}
if (isInteractive())
std::cerr << std::endl;
delete m_process;
}
void QmlProfilerApplication::parseArguments()
{
setApplicationName(QLatin1String("qmlprofiler"));
setApplicationVersion(QLatin1String(qVersion()));
QCommandLineParser parser;
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
parser.setOptionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsPositionalArguments);
parser.setApplicationDescription(QChar::LineFeed + tr(
"The QML Profiler retrieves QML tracing data from an application. The data\n"
"collected can then be visualized in Qt Creator. The application to be profiled\n"
"has to enable QML debugging. See the Qt Creator documentation on how to do\n"
"this for different Qt versions."));
QCommandLineOption attach(QStringList() << QLatin1String("a") << QLatin1String("attach"),
tr("Attach to an application already running on <hostname>, "
"instead of starting it locally."),
QLatin1String("hostname"));
parser.addOption(attach);
QCommandLineOption port(QStringList() << QLatin1String("p") << QLatin1String("port"),
tr("Connect to the TCP port <port>. The default is 3768."),
QLatin1String("port"), QLatin1String("3768"));
parser.addOption(port);
QCommandLineOption output(QStringList() << QLatin1String("o") << QLatin1String("output"),
tr("Save tracing data in <file>. By default the data is sent to the "
"standard output."), QLatin1String("file"), QString());
parser.addOption(output);
QCommandLineOption record(QLatin1String("record"),
tr("If set to 'off', don't immediately start recording data when the "
"QML engine starts, but instead either start the recording "
"interactively or with the JavaScript console.profile() function. "
"By default the recording starts immediately."),
QLatin1String("on|off"), QLatin1String("on"));
parser.addOption(record);
QStringList featureList;
for (int i = 0; i < MaximumProfileFeature; ++i)
featureList << QLatin1String(features[i]);
QCommandLineOption include(QLatin1String("include"),
tr("Comma-separated list of features to record. By default all "
"features supported by the QML engine are recorded. If --include "
"is specified, only the given features will be recorded. "
"The following features are unserstood by qmlprofiler: %1").arg(
featureList.join(", ")),
QLatin1String("feature,..."));
parser.addOption(include);
QCommandLineOption exclude(QLatin1String("exclude"),
tr("Comma-separated list of features to exclude when recording. By "
"default all features supported by the QML engine are recorded. "
"See --include for the features understood by qmlprofiler."),
QLatin1String("feature,..."));
parser.addOption(exclude);
QCommandLineOption interactive(QLatin1String("interactive"),
tr("Manually control the recording from the command line. The "
"profiler will not terminate itself when the application "
"does so in this case.") + QChar::Space + tr(commandTextC));
parser.addOption(interactive);
QCommandLineOption verbose(QStringList() << QLatin1String("verbose"),
tr("Print debugging output."));
parser.addOption(verbose);
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument(QLatin1String("executable"),
tr("The executable to be started and profiled."),
QLatin1String("[executable]"));
parser.addPositionalArgument(QLatin1String("parameters"),
tr("Parameters for the executable to be started."),
QLatin1String("[parameters...]"));
parser.process(*this);
if (parser.isSet(attach)) {
m_hostName = parser.value(attach);
m_runMode = AttachMode;
m_port = 3768;
}
if (parser.isSet(port)) {
bool isNumber;
m_port = parser.value(port).toUShort(&isNumber);
if (!isNumber) {
logError(tr("'%1' is not a valid port.").arg(parser.value(port)));
parser.showHelp(1);
}
} else if (m_port == 0) {
QTemporaryFile file;
if (file.open())
m_socketFile = file.fileName();
}
m_outputFile = parser.value(output);
m_recording = (parser.value(record) == QLatin1String("on"));
m_interactive = parser.isSet(interactive);
quint64 features = std::numeric_limits<quint64>::max();
if (parser.isSet(include)) {
if (parser.isSet(exclude)) {
logError(tr("qmlprofiler can only process either --include or --exclude, not both."));
parser.showHelp(4);
}
features = parseFeatures(featureList, parser.value(include), false);
}
if (parser.isSet(exclude))
features = parseFeatures(featureList, parser.value(exclude), true);
if (features == 0)
parser.showHelp(4);
m_qmlProfilerClient->setRequestedFeatures(features);
if (parser.isSet(verbose))
m_verbose = true;
m_arguments = parser.positionalArguments();
if (!m_arguments.isEmpty())
m_executablePath = m_arguments.takeFirst();
if (m_runMode == LaunchMode && m_executablePath.isEmpty()) {
logError(tr("You have to specify either --attach or an executable to start."));
parser.showHelp(2);
}
if (m_runMode == AttachMode && !m_executablePath.isEmpty()) {
logError(tr("--attach cannot be used when starting an executable."));
parser.showHelp(3);
}
}
int QmlProfilerApplication::exec()
{
QTimer::singleShot(0, this, &QmlProfilerApplication::run);
return QCoreApplication::exec();
}
bool QmlProfilerApplication::isInteractive() const
{
return m_interactive;
}
quint64 QmlProfilerApplication::parseFeatures(const QStringList &featureList, const QString &values,
bool exclude)
{
quint64 features = exclude ? std::numeric_limits<quint64>::max() : 0;
const QStringList givenFeatures = values.split(QLatin1Char(','));
for (const QString &f : givenFeatures) {
int index = featureList.indexOf(f);
if (index < 0) {
logError(tr("Unknown feature '%1'").arg(f));
return 0;
}
quint64 flag = static_cast<quint64>(1) << index;
features = (exclude ? (features ^ flag) : (features | flag));
}
if (features == 0) {
logError(exclude ? tr("No features remaining to record after processing --exclude.") :
tr("No features specified for --include."));
}
return features;
}
void QmlProfilerApplication::flush()
{
if (m_recording) {
m_pendingRequest = REQUEST_FLUSH;
m_qmlProfilerClient->setRecording(false);
} else {
if (m_profilerData->save(m_interactiveOutputFile)) {
m_profilerData->clear();
if (!m_interactiveOutputFile.isEmpty())
prompt(tr("Data written to %1.").arg(m_interactiveOutputFile));
else
prompt();
} else {
prompt(tr("Saving failed."));
}
m_interactiveOutputFile.clear();
m_pendingRequest = REQUEST_NONE;
}
}
void QmlProfilerApplication::output()
{
if (m_profilerData->save(m_interactiveOutputFile)) {
if (!m_interactiveOutputFile.isEmpty())
prompt(tr("Data written to %1.").arg(m_interactiveOutputFile));
else
prompt();
} else {
prompt(tr("Saving failed"));
}
m_interactiveOutputFile.clear();
m_pendingRequest = REQUEST_NONE;
}
bool QmlProfilerApplication::checkOutputFile(PendingRequest pending)
{
if (m_interactiveOutputFile.isEmpty())
return true;
QFileInfo file(m_interactiveOutputFile);
if (file.exists()) {
if (!file.isFile()) {
prompt(tr("Cannot overwrite %1.").arg(m_interactiveOutputFile));
m_interactiveOutputFile.clear();
} else {
prompt(tr("%1 exists. Overwrite (y/n)?").arg(m_interactiveOutputFile));
m_pendingRequest = pending;
}
return false;
} else {
return true;
}
}
void QmlProfilerApplication::userCommand(const QString &command)
{
auto args = QStringView{command}.split(QChar::Space, Qt::SkipEmptyParts);
if (args.isEmpty()) {
prompt();
return;
}
QByteArray cmd = args.takeFirst().trimmed().toLatin1();
if (m_pendingRequest == REQUEST_QUIT) {
if (cmd == Constants::CMD_YES || cmd == Constants::CMD_YES2) {
quit();
} else if (cmd == Constants::CMD_NO || cmd == Constants::CMD_NO2) {
m_pendingRequest = REQUEST_NONE;
prompt();
} else {
prompt(tr("Really quit (y/n)?"));
}
return;
}
if (m_pendingRequest == REQUEST_OUTPUT_FILE || m_pendingRequest == REQUEST_FLUSH_FILE) {
if (cmd == Constants::CMD_YES || cmd == Constants::CMD_YES2) {
if (m_pendingRequest == REQUEST_OUTPUT_FILE)
output();
else
flush();
} else if (cmd == Constants::CMD_NO || cmd == Constants::CMD_NO2) {
m_pendingRequest = REQUEST_NONE;
m_interactiveOutputFile.clear();
prompt();
} else {
prompt(tr("%1 exists. Overwrite (y/n)?"));
}
return;
}
if (cmd == Constants::CMD_RECORD || cmd == Constants::CMD_RECORD2) {
m_pendingRequest = REQUEST_TOGGLE_RECORDING;
m_qmlProfilerClient->setRecording(!m_recording);
} else if (cmd == Constants::CMD_QUIT || cmd == Constants::CMD_QUIT2) {
m_pendingRequest = REQUEST_QUIT;
if (m_recording) {
prompt(tr("The application is still generating data. Really quit (y/n)?"));
} else if (!m_profilerData->isEmpty()) {
prompt(tr("There is still trace data in memory. Really quit (y/n)?"));
} else {
quit();
}
} else if (cmd == Constants::CMD_OUTPUT || cmd == Constants::CMD_OUTPUT2) {
if (m_recording) {
prompt(tr("Cannot output while recording data."));
} else if (m_profilerData->isEmpty()) {
prompt(tr("No data was recorded so far."));
} else {
m_interactiveOutputFile = args.size() > 0 ? args.at(0).toString() : m_outputFile;
if (checkOutputFile(REQUEST_OUTPUT_FILE))
output();
}
} else if (cmd == Constants::CMD_CLEAR || cmd == Constants::CMD_CLEAR2) {
if (m_recording) {
prompt(tr("Cannot clear data while recording."));
} else if (m_profilerData->isEmpty()) {
prompt(tr("No data was recorded so far."));
} else {
m_profilerData->clear();
prompt(tr("Trace data cleared."));
}
} else if (cmd == Constants::CMD_FLUSH || cmd == Constants::CMD_FLUSH2) {
if (!m_recording && m_profilerData->isEmpty()) {
prompt(tr("No data was recorded so far."));
} else {
m_interactiveOutputFile = args.size() > 0 ? args.at(0).toString() : m_outputFile;
if (checkOutputFile(REQUEST_FLUSH_FILE))
flush();
}
} else {
prompt(tr(commandTextC));
}
}
void QmlProfilerApplication::notifyTraceStarted()
{
// Synchronize to server state. It doesn't hurt to do this multiple times in a row for
// different traces. There is no symmetric event to "Complete" after all.
m_recording = true;
if (m_pendingRequest == REQUEST_TOGGLE_RECORDING) {
m_pendingRequest = REQUEST_NONE;
prompt(tr("Recording started"));
} else {
prompt(tr("Application started recording"), false);
}
}
void QmlProfilerApplication::outputData()
{
if (!m_profilerData->isEmpty()) {
m_profilerData->save(m_outputFile);
m_profilerData->clear();
}
}
void QmlProfilerApplication::run()
{
if (m_runMode == LaunchMode) {
if (!m_socketFile.isEmpty()) {
logStatus(QString::fromLatin1("Listening on %1 ...").arg(m_socketFile));
m_connection->startLocalServer(m_socketFile);
}
m_process = new QProcess(this);
QStringList arguments;
arguments << QString::fromLatin1("-qmljsdebugger=%1:%2,block,services:CanvasFrameRate")
.arg(QLatin1String(m_socketFile.isEmpty() ? "port" : "file"))
.arg(m_socketFile.isEmpty() ? QString::number(m_port) : m_socketFile);
arguments << m_arguments;
m_process->setProcessChannelMode(QProcess::MergedChannels);
connect(m_process, &QIODevice::readyRead, this, &QmlProfilerApplication::processHasOutput);
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this](int){ processFinished(); });
logStatus(QString("Starting '%1 %2' ...").arg(m_executablePath,
arguments.join(QLatin1Char(' '))));
m_process->start(m_executablePath, arguments);
if (!m_process->waitForStarted()) {
logError(QString("Could not run '%1': %2").arg(m_executablePath,
m_process->errorString()));
exit(1);
}
}
m_connectTimer.start();
}
void QmlProfilerApplication::tryToConnect()
{
Q_ASSERT(!m_connection->isConnected());
++ m_connectionAttempts;
if (!(m_connectionAttempts % 5)) {// print every 5 seconds
if (m_socketFile.isEmpty()) {
logWarning(QString::fromLatin1("Could not connect to %1:%2 for %3 seconds ...")
.arg(m_hostName).arg(m_port).arg(m_connectionAttempts));
} else {
logWarning(QString::fromLatin1("No connection received on %1 for %2 seconds ...")
.arg(m_socketFile).arg(m_connectionAttempts));
}
}
if (m_socketFile.isEmpty()) {
logStatus(QString::fromLatin1("Connecting to %1:%2 ...").arg(m_hostName).arg(m_port));
m_connection->connectToHost(m_hostName, m_port);
}
}
void QmlProfilerApplication::connected()
{
m_connectTimer.stop();
QString endpoint = m_socketFile.isEmpty() ?
QString::fromLatin1("%1:%2").arg(m_hostName).arg(m_port) :
m_socketFile;
prompt(tr("Connected to %1. Wait for profile data or type a command (type 'help' to show list "
"of commands).\nRecording Status: %2")
.arg(endpoint).arg(m_recording ? tr("on") : tr("off")));
}
void QmlProfilerApplication::disconnected()
{
if (m_runMode == AttachMode) {
int exitCode = 0;
if (m_recording) {
logError("Connection dropped while recording, last trace is damaged!");
exitCode = 2;
}
if (!m_interactive )
exit(exitCode);
else
m_qmlProfilerClient->clearAll();
}
}
void QmlProfilerApplication::processHasOutput()
{
Q_ASSERT(m_process);
while (m_process->bytesAvailable())
std::cerr << m_process->readAll().constData();
}
void QmlProfilerApplication::processFinished()
{
Q_ASSERT(m_process);
int exitCode = 0;
if (m_process->exitStatus() == QProcess::NormalExit) {
logStatus(QString("Process exited (%1).").arg(m_process->exitCode()));
if (m_recording) {
logError("Process exited while recording, last trace is damaged!");
exitCode = 2;
}
} else {
logError("Process crashed!");
exitCode = 3;
}
if (!m_interactive)
exit(exitCode);
else
m_qmlProfilerClient->clearAll();
}
void QmlProfilerApplication::traceClientEnabledChanged(bool enabled)
{
if (enabled) {
logStatus("Trace client is attached.");
// blocked server is waiting for recording message from both clients
// once the last one is connected, both messages should be sent
m_qmlProfilerClient->setRecording(m_recording);
}
}
void QmlProfilerApplication::traceFinished()
{
m_recording = false; // only on "Complete" we know that the trace is really finished.
if (m_pendingRequest == REQUEST_FLUSH) {
flush();
} else if (m_pendingRequest == REQUEST_TOGGLE_RECORDING) {
m_pendingRequest = REQUEST_NONE;
prompt(tr("Recording stopped."));
} else {
prompt(tr("Application stopped recording."), false);
}
m_qmlProfilerClient->clearEvents();
}
void QmlProfilerApplication::prompt(const QString &line, bool ready)
{
if (m_interactive) {
if (!line.isEmpty())
std::cerr << qPrintable(line) << std::endl;
std::cerr << "> ";
if (ready)
emit readyForCommand();
}
}
void QmlProfilerApplication::logError(const QString &error)
{
std::cerr << "Error: " << qPrintable(error) << std::endl;
}
void QmlProfilerApplication::logWarning(const QString &warning)
{
std::cerr << "Warning: " << qPrintable(warning) << std::endl;
}
void QmlProfilerApplication::logStatus(const QString &status)
{
if (!m_verbose)
return;
std::cerr << qPrintable(status) << std::endl;
}
|