Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlprofilerservice.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
9
10#include <private/qjsengine_p.h>
11#include <private/qqmldebugpluginmanager_p.h>
12
13#include <QtCore/qurl.h>
14#include <QtCore/qtimer.h>
15#include <QtCore/qthread.h>
16#include <QtCore/qcoreapplication.h>
17
18#include <algorithm>
19
20QT_BEGIN_NAMESPACE
21
22Q_QML_DEBUG_PLUGIN_LOADER(QQmlAbstractProfilerAdapter)
23
24/*!
25 \class QQmlProfilerServiceImpl
26 \inmodule QtQml
27 \internal
28*/
30 QQmlConfigurableDebugService<QQmlProfilerService>(1, parent),
31 m_waitingForStop(false), m_globalEnabled(false), m_globalFeatures(0)
32{
33 m_timer.start();
34 QQmlAbstractProfilerAdapter *quickAdapter =
35 loadQQmlAbstractProfilerAdapter(QLatin1String("QQuickProfilerAdapter"));
36 if (quickAdapter) {
37 addGlobalProfiler(quickAdapter);
38 quickAdapter->setService(this);
39 }
40
41 // try to load QQuick3D profiler adapter if it exists
42 QQmlAbstractProfilerAdapter *quick3DAdapter =
43 loadQQmlAbstractProfilerAdapter(QLatin1String("QQuick3DProfilerAdapter"));
44 if (quick3DAdapter) {
45 addGlobalProfiler(quick3DAdapter);
46 quick3DAdapter->setService(this);
47 }
48
49
50}
51
53{
54 // No need to lock here. If any engine or global profiler is still trying to register at this
55 // point we have a nasty bug anyway.
56 qDeleteAll(m_engineProfilers);
57 qDeleteAll(m_globalProfilers);
58}
59
60void QQmlProfilerServiceImpl::dataReady(QQmlAbstractProfilerAdapter *profiler)
61{
62 QMutexLocker lock(&m_configMutex);
63 bool dataComplete = true;
64 for (QMultiMap<qint64, QQmlAbstractProfilerAdapter *>::iterator i(m_startTimes.begin()); i != m_startTimes.end();) {
65 if (i.value() == profiler) {
66 m_startTimes.erase(i++);
67 } else {
68 if (i.key() == -1)
69 dataComplete = false;
70 ++i;
71 }
72 }
73 m_startTimes.insert(0, profiler);
74 if (dataComplete) {
75 QList<QJSEngine *> enginesToRelease;
76 for (QJSEngine *engine : std::as_const(m_stoppingEngines)) {
77 const auto range = std::as_const(m_engineProfilers).equal_range(engine);
78 const auto startTimesEnd = m_startTimes.cend();
79 for (auto it = range.first; it != range.second; ++it) {
80 if (std::find(m_startTimes.cbegin(), startTimesEnd, *it) != startTimesEnd) {
81 enginesToRelease.append(engine);
82 break;
83 }
84 }
85 }
86 sendMessages();
87 for (QJSEngine *engine : std::as_const(enginesToRelease)) {
88 m_stoppingEngines.removeOne(engine);
89 emit detachedFromEngine(engine);
90 }
91 }
92}
93
94void QQmlProfilerServiceImpl::engineAboutToBeAdded(QJSEngine *engine)
95{
96 Q_ASSERT_X(QThread::currentThread() == engine->thread(), Q_FUNC_INFO,
97 "QML profilers have to be added from the engine thread");
98
99 QMutexLocker lock(&m_configMutex);
100 if (QQmlEngine *qmlEngine = qobject_cast<QQmlEngine *>(engine)) {
101 QQmlProfilerAdapter *qmlAdapter
102 = new QQmlProfilerAdapter(this, QQmlEnginePrivate::get(qmlEngine));
103 addEngineProfiler(qmlAdapter, engine);
104 QQmlProfilerAdapter *compileAdapter
105 = new QQmlProfilerAdapter(this, QQmlTypeLoader::get(engine));
106 addEngineProfiler(compileAdapter, engine);
107 }
108 QV4ProfilerAdapter *v4Adapter = new QV4ProfilerAdapter(this, engine->handle());
109 addEngineProfiler(v4Adapter, engine);
110 QQmlConfigurableDebugService<QQmlProfilerService>::engineAboutToBeAdded(engine);
111}
112
113void QQmlProfilerServiceImpl::engineAdded(QJSEngine *engine)
114{
115 Q_ASSERT_X(QThread::currentThread() == engine->thread(), Q_FUNC_INFO,
116 "QML profilers have to be added from the engine thread");
117
118 QMutexLocker lock(&m_configMutex);
119
120 if (m_globalEnabled)
121 startProfiling(engine, m_globalFeatures);
122
123 const auto range = std::as_const(m_engineProfilers).equal_range(engine);
124 for (auto it = range.first; it != range.second; ++it)
125 (*it)->stopWaiting();
126}
127
129{
130 Q_ASSERT_X(QThread::currentThread() == engine->thread(), Q_FUNC_INFO,
131 "QML profilers have to be removed from the engine thread");
132
133 QMutexLocker lock(&m_configMutex);
134 bool isRunning = false;
135 const auto range = std::as_const(m_engineProfilers).equal_range(engine);
136 for (auto it = range.first; it != range.second; ++it) {
137 QQmlAbstractProfilerAdapter *profiler = *it;
138 if (profiler->isRunning())
139 isRunning = true;
140 profiler->startWaiting();
141 }
142 if (isRunning) {
143 m_stoppingEngines.append(engine);
144 stopProfiling(engine);
145 } else {
146 emit detachedFromEngine(engine);
147 }
148}
149
151{
152 Q_ASSERT_X(QThread::currentThread() == engine->thread(), Q_FUNC_INFO,
153 "QML profilers have to be removed from the engine thread");
154
155 QMutexLocker lock(&m_configMutex);
156 const auto range = std::as_const(m_engineProfilers).equal_range(engine);
157 for (auto it = range.first; it != range.second; ++it) {
158 QQmlAbstractProfilerAdapter *profiler = *it;
159 removeProfilerFromStartTimes(profiler);
160 delete profiler;
161 }
162 m_engineProfilers.remove(engine);
163}
164
165void QQmlProfilerServiceImpl::addEngineProfiler(QQmlAbstractProfilerAdapter *profiler, QJSEngine *engine)
166{
167 profiler->moveToThread(thread());
168 profiler->synchronize(m_timer);
169 m_engineProfilers.insert(engine, profiler);
170}
171
172void QQmlProfilerServiceImpl::addGlobalProfiler(QQmlAbstractProfilerAdapter *profiler)
173{
174 QMutexLocker lock(&m_configMutex);
175 profiler->synchronize(m_timer);
176 m_globalProfilers.append(profiler);
177 // Global profiler, not connected to a specific engine.
178 // Global profilers are started whenever any engine profiler is started and stopped when
179 // all engine profilers are stopped.
180 quint64 features = 0;
181 bool anyRunning = false;
182 for (QQmlAbstractProfilerAdapter *engineProfiler : std::as_const(m_engineProfilers)) {
183 features |= engineProfiler->features();
184 anyRunning = anyRunning || engineProfiler->isRunning();
185 }
186
187 if (anyRunning)
188 profiler->startProfiling(features);
189}
190
191void QQmlProfilerServiceImpl::removeGlobalProfiler(QQmlAbstractProfilerAdapter *profiler)
192{
193 QMutexLocker lock(&m_configMutex);
194 removeProfilerFromStartTimes(profiler);
195 m_globalProfilers.removeOne(profiler);
196}
197
198void QQmlProfilerServiceImpl::removeProfilerFromStartTimes(const QQmlAbstractProfilerAdapter *profiler)
199{
200 for (QMultiMap<qint64, QQmlAbstractProfilerAdapter *>::iterator i(m_startTimes.begin());
201 i != m_startTimes.end();) {
202 if (i.value() == profiler) {
203 m_startTimes.erase(i++);
204 break;
205 } else {
206 ++i;
207 }
208 }
209}
210
211/*!
212 * Start profiling the given \a engine. If \a engine is 0, start all engine profilers that aren't
213 * currently running.
214 *
215 * If any engine profiler is started like that also start all global profilers.
216 */
217void QQmlProfilerServiceImpl::startProfiling(QJSEngine *engine, quint64 features)
218{
219 QMutexLocker lock(&m_configMutex);
220
221 if (features & static_cast<quint64>(1) << ProfileDebugMessages) {
222 if (QDebugMessageService *messageService =
223 QQmlDebugConnector::instance()->service<QDebugMessageService>())
224 messageService->synchronizeTime(m_timer);
225 }
226
227 QQmlDebugPacket d;
228
229 d << m_timer.nsecsElapsed() << static_cast<qint32>(Event) << static_cast<qint32>(StartTrace);
230 bool startedAny = false;
231 if (engine != nullptr) {
232 const auto range = std::as_const(m_engineProfilers).equal_range(engine);
233 for (auto it = range.first; it != range.second; ++it) {
234 QQmlAbstractProfilerAdapter *profiler = *it;
235 if (!profiler->isRunning()) {
236 profiler->startProfiling(features);
237 startedAny = true;
238 }
239 }
240 if (startedAny)
241 d << idForObject(engine);
242 } else {
243 m_globalEnabled = true;
244 m_globalFeatures = features;
245
246 QSet<QJSEngine *> engines;
247 for (QMultiHash<QJSEngine *, QQmlAbstractProfilerAdapter *>::iterator i(m_engineProfilers.begin());
248 i != m_engineProfilers.end(); ++i) {
249 if (!i.value()->isRunning()) {
250 engines << i.key();
251 i.value()->startProfiling(features);
252 startedAny = true;
253 }
254 }
255 for (QJSEngine *profiledEngine : std::as_const(engines))
256 d << idForObject(profiledEngine);
257 }
258
259 if (startedAny) {
260 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
261 if (!profiler->isRunning())
262 profiler->startProfiling(features);
263 }
264
265 emit startFlushTimer();
266 emit messageToClient(name(), d.data());
267 }
268}
269
270/*!
271 * Stop profiling the given \a engine. If \a engine is 0, stop all currently running engine
272 * profilers.
273 *
274 * If afterwards no more engine profilers are running, also stop all global profilers. Otherwise
275 * only make them report their data.
276 */
278{
279 QMutexLocker lock(&m_configMutex);
280 QList<QQmlAbstractProfilerAdapter *> stopping;
281 QList<QQmlAbstractProfilerAdapter *> reporting;
282
283 if (engine == nullptr)
284 m_globalEnabled = false;
285
286 bool stillRunning = false;
287 for (QMultiHash<QJSEngine *, QQmlAbstractProfilerAdapter *>::iterator i(m_engineProfilers.begin());
288 i != m_engineProfilers.end(); ++i) {
289 if (i.value()->isRunning()) {
290 m_startTimes.insert(-1, i.value());
291 if (engine == nullptr || i.key() == engine) {
292 stopping << i.value();
293 } else {
294 reporting << i.value();
295 stillRunning = true;
296 }
297 }
298 }
299
300 if (stopping.isEmpty())
301 return;
302
303 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
304 if (!profiler->isRunning())
305 continue;
306 m_startTimes.insert(-1, profiler);
307 if (stillRunning) {
308 reporting << profiler;
309 } else {
310 stopping << profiler;
311 }
312 }
313
314 emit stopFlushTimer();
315 m_waitingForStop = true;
316
317 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(reporting))
318 profiler->reportData();
319
320 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(stopping))
321 profiler->stopProfiling();
322}
323
324/*
325 Send the queued up messages.
326*/
327void QQmlProfilerServiceImpl::sendMessages()
328{
329 QList<QByteArray> messages;
330
331 QQmlDebugPacket traceEnd;
332 if (m_waitingForStop) {
333 traceEnd << m_timer.nsecsElapsed() << static_cast<qint32>(Event)
334 << static_cast<qint32>(EndTrace);
335
336 QSet<QJSEngine *> seen;
337 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_startTimes)) {
338 for (QMultiHash<QJSEngine *, QQmlAbstractProfilerAdapter *>::iterator i(m_engineProfilers.begin());
339 i != m_engineProfilers.end(); ++i) {
340 if (i.value() == profiler && !seen.contains(i.key())) {
341 seen << i.key();
342 traceEnd << idForObject(i.key());
343 }
344 }
345 }
346 }
347
348 while (!m_startTimes.empty()) {
349 QQmlAbstractProfilerAdapter *first = m_startTimes.begin().value();
350 m_startTimes.erase(m_startTimes.begin());
351 qint64 next = first->sendMessages(m_startTimes.isEmpty() ?
352 std::numeric_limits<qint64>::max() :
353 m_startTimes.begin().key(), messages);
354 if (next != -1)
355 m_startTimes.insert(next, first);
356
357 if (messages.size() >= QQmlAbstractProfilerAdapter::s_numMessagesPerBatch) {
358 emit messagesToClient(name(), messages);
359 messages.clear();
360 }
361 }
362
363 bool stillRunning = false;
364 for (const QQmlAbstractProfilerAdapter *profiler : std::as_const(m_engineProfilers)) {
365 if (profiler->isRunning()) {
366 stillRunning = true;
367 break;
368 }
369 }
370
371 if (m_waitingForStop) {
372 // EndTrace can be sent multiple times, as it's engine specific.
373 messages << traceEnd.data();
374
375 if (!stillRunning) {
376 // Complete is only sent once, when no engines are running anymore.
377 QQmlDebugPacket ds;
378 ds << static_cast<qint64>(-1) << static_cast<qint32>(Complete);
379 messages << ds.data();
380 m_waitingForStop = false;
381 }
382 }
383
384 emit messagesToClient(name(), messages);
385
386 // Restart flushing if any profilers are still running
387 if (stillRunning)
388 emit startFlushTimer();
389}
390
391void QQmlProfilerServiceImpl::stateAboutToBeChanged(QQmlDebugService::State newState)
392{
393 QMutexLocker lock(&m_configMutex);
394
395 if (state() == newState)
396 return;
397
398 // Stop all profiling and send the data before we get disabled.
399 if (newState != Enabled) {
400 for (auto it = m_engineProfilers.keyBegin(), end = m_engineProfilers.keyEnd();
401 it != end; ++it) {
402 stopProfiling(*it);
403 }
404 }
405}
406
407void QQmlProfilerServiceImpl::messageReceived(const QByteArray &message)
408{
409 QMutexLocker lock(&m_configMutex);
410
411 QQmlDebugPacket stream(message);
412
413 int engineId = -1;
414 quint64 features = std::numeric_limits<quint64>::max();
415 bool enabled;
416 quint32 flushInterval = 0;
417 stream >> enabled;
418 if (!stream.atEnd())
419 stream >> engineId;
420 if (!stream.atEnd())
421 stream >> features;
422 if (!stream.atEnd()) {
423 stream >> flushInterval;
424 m_flushTimer.setInterval(
425 static_cast<int>(qMin(flushInterval,
426 static_cast<quint32>(std::numeric_limits<int>::max()))));
427 auto timerStart = static_cast<void(QTimer::*)()>(&QTimer::start);
428 if (flushInterval > 0) {
429 connect(&m_flushTimer, &QTimer::timeout, this, &QQmlProfilerServiceImpl::flush);
430 connect(this, &QQmlProfilerServiceImpl::startFlushTimer, &m_flushTimer, timerStart);
431 connect(this, &QQmlProfilerServiceImpl::stopFlushTimer, &m_flushTimer, &QTimer::stop);
432 } else {
433 disconnect(&m_flushTimer, &QTimer::timeout, this, &QQmlProfilerServiceImpl::flush);
434 disconnect(this, &QQmlProfilerServiceImpl::startFlushTimer, &m_flushTimer, timerStart);
436 &m_flushTimer, &QTimer::stop);
437 }
438 }
439
440 bool useMessageTypes = false;
441 if (!stream.atEnd())
442 stream >> useMessageTypes;
443
444 // If engineId == -1 objectForId() and then the cast will return 0.
445 if (enabled && useMessageTypes) // If the client doesn't support message types don't profile.
446 startProfiling(qobject_cast<QJSEngine *>(objectForId(engineId)), features);
447 else if (!enabled) // On stopProfiling the client doesn't repeat useMessageTypes.
448 stopProfiling(qobject_cast<QJSEngine *>(objectForId(engineId)));
449
450 stopWaiting();
451}
452
453void QQmlProfilerServiceImpl::flush()
454{
455 QMutexLocker lock(&m_configMutex);
456 QList<QQmlAbstractProfilerAdapter *> reporting;
457
458 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_engineProfilers)) {
459 if (profiler->isRunning()) {
460 m_startTimes.insert(-1, profiler);
461 reporting.append(profiler);
462 }
463 }
464
465 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
466 if (profiler->isRunning()) {
467 m_startTimes.insert(-1, profiler);
468 reporting.append(profiler);
469 }
470 }
471
472 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(reporting))
473 profiler->reportData();
474}
475
476QT_END_NAMESPACE
477
478#include "moc_qqmlprofilerservice.cpp"
\inmodule QtCore
Definition qobject.h:105
void engineAboutToBeRemoved(QJSEngine *engine) override
void engineAdded(QJSEngine *engine) override
void messageReceived(const QByteArray &) override
void stateAboutToBeChanged(State state) override
void addGlobalProfiler(QQmlAbstractProfilerAdapter *profiler) override
void dataReady(QQmlAbstractProfilerAdapter *profiler) override
void engineRemoved(QJSEngine *engine) override
void stopProfiling(QJSEngine *engine) override
Stop profiling the given engine.
void removeGlobalProfiler(QQmlAbstractProfilerAdapter *profiler) override
void startProfiling(QJSEngine *engine, quint64 features=std::numeric_limits< quint64 >::max()) override
Start profiling the given engine.