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 for (QQmlAbstractProfilerAdapter *engineProfiler : std::as_const(m_engineProfilers))
182 features |= engineProfiler->features();
183
184 if (features != 0)
185 profiler->startProfiling(features);
186}
187
188void QQmlProfilerServiceImpl::removeGlobalProfiler(QQmlAbstractProfilerAdapter *profiler)
189{
190 QMutexLocker lock(&m_configMutex);
191 removeProfilerFromStartTimes(profiler);
192 m_globalProfilers.removeOne(profiler);
193}
194
195void QQmlProfilerServiceImpl::removeProfilerFromStartTimes(const QQmlAbstractProfilerAdapter *profiler)
196{
197 for (QMultiMap<qint64, QQmlAbstractProfilerAdapter *>::iterator i(m_startTimes.begin());
198 i != m_startTimes.end();) {
199 if (i.value() == profiler) {
200 m_startTimes.erase(i++);
201 break;
202 } else {
203 ++i;
204 }
205 }
206}
207
208/*!
209 * Start profiling the given \a engine. If \a engine is 0, start all engine profilers that aren't
210 * currently running.
211 *
212 * If any engine profiler is started like that also start all global profilers.
213 */
214void QQmlProfilerServiceImpl::startProfiling(QJSEngine *engine, quint64 features)
215{
216 QMutexLocker lock(&m_configMutex);
217
218 if (features & static_cast<quint64>(1) << ProfileDebugMessages) {
219 if (QDebugMessageService *messageService =
220 QQmlDebugConnector::instance()->service<QDebugMessageService>())
221 messageService->synchronizeTime(m_timer);
222 }
223
224 QQmlDebugPacket d;
225
226 d << m_timer.nsecsElapsed() << static_cast<qint32>(Event) << static_cast<qint32>(StartTrace);
227 bool startedAny = false;
228 if (engine != nullptr) {
229 const auto range = std::as_const(m_engineProfilers).equal_range(engine);
230 for (auto it = range.first; it != range.second; ++it) {
231 QQmlAbstractProfilerAdapter *profiler = *it;
232 if (!profiler->isRunning()) {
233 profiler->startProfiling(features);
234 startedAny = true;
235 }
236 }
237 if (startedAny)
238 d << idForObject(engine);
239 } else {
240 m_globalEnabled = true;
241 m_globalFeatures = features;
242
243 QSet<QJSEngine *> engines;
244 for (QMultiHash<QJSEngine *, QQmlAbstractProfilerAdapter *>::iterator i(m_engineProfilers.begin());
245 i != m_engineProfilers.end(); ++i) {
246 if (!i.value()->isRunning()) {
247 engines << i.key();
248 i.value()->startProfiling(features);
249 startedAny = true;
250 }
251 }
252 for (QJSEngine *profiledEngine : std::as_const(engines))
253 d << idForObject(profiledEngine);
254 }
255
256 if (startedAny) {
257 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
258 if (!profiler->isRunning())
259 profiler->startProfiling(features);
260 }
261
262 emit startFlushTimer();
263 emit messageToClient(name(), d.data());
264 }
265}
266
267/*!
268 * Stop profiling the given \a engine. If \a engine is 0, stop all currently running engine
269 * profilers.
270 *
271 * If afterwards no more engine profilers are running, also stop all global profilers. Otherwise
272 * only make them report their data.
273 */
275{
276 QMutexLocker lock(&m_configMutex);
277 QList<QQmlAbstractProfilerAdapter *> stopping;
278 QList<QQmlAbstractProfilerAdapter *> reporting;
279
280 if (engine == nullptr)
281 m_globalEnabled = false;
282
283 bool stillRunning = false;
284 for (QMultiHash<QJSEngine *, QQmlAbstractProfilerAdapter *>::iterator i(m_engineProfilers.begin());
285 i != m_engineProfilers.end(); ++i) {
286 if (i.value()->isRunning()) {
287 m_startTimes.insert(-1, i.value());
288 if (engine == nullptr || i.key() == engine) {
289 stopping << i.value();
290 } else {
291 reporting << i.value();
292 stillRunning = true;
293 }
294 }
295 }
296
297 if (stopping.isEmpty())
298 return;
299
300 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
301 if (!profiler->isRunning())
302 continue;
303 m_startTimes.insert(-1, profiler);
304 if (stillRunning) {
305 reporting << profiler;
306 } else {
307 stopping << profiler;
308 }
309 }
310
311 emit stopFlushTimer();
312 m_waitingForStop = true;
313
314 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(reporting))
315 profiler->reportData();
316
317 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(stopping))
318 profiler->stopProfiling();
319}
320
321/*
322 Send the queued up messages.
323*/
324void QQmlProfilerServiceImpl::sendMessages()
325{
326 QList<QByteArray> messages;
327
328 QQmlDebugPacket traceEnd;
329 if (m_waitingForStop) {
330 traceEnd << m_timer.nsecsElapsed() << static_cast<qint32>(Event)
331 << static_cast<qint32>(EndTrace);
332
333 QSet<QJSEngine *> seen;
334 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_startTimes)) {
335 for (QMultiHash<QJSEngine *, QQmlAbstractProfilerAdapter *>::iterator i(m_engineProfilers.begin());
336 i != m_engineProfilers.end(); ++i) {
337 if (i.value() == profiler && !seen.contains(i.key())) {
338 seen << i.key();
339 traceEnd << idForObject(i.key());
340 }
341 }
342 }
343 }
344
345 while (!m_startTimes.empty()) {
346 QQmlAbstractProfilerAdapter *first = m_startTimes.begin().value();
347 m_startTimes.erase(m_startTimes.begin());
348 qint64 next = first->sendMessages(m_startTimes.isEmpty() ?
349 std::numeric_limits<qint64>::max() :
350 m_startTimes.begin().key(), messages);
351 if (next != -1)
352 m_startTimes.insert(next, first);
353
354 if (messages.size() >= QQmlAbstractProfilerAdapter::s_numMessagesPerBatch) {
355 emit messagesToClient(name(), messages);
356 messages.clear();
357 }
358 }
359
360 bool stillRunning = false;
361 for (const QQmlAbstractProfilerAdapter *profiler : std::as_const(m_engineProfilers)) {
362 if (profiler->isRunning()) {
363 stillRunning = true;
364 break;
365 }
366 }
367
368 if (m_waitingForStop) {
369 // EndTrace can be sent multiple times, as it's engine specific.
370 messages << traceEnd.data();
371
372 if (!stillRunning) {
373 // Complete is only sent once, when no engines are running anymore.
374 QQmlDebugPacket ds;
375 ds << static_cast<qint64>(-1) << static_cast<qint32>(Complete);
376 messages << ds.data();
377 m_waitingForStop = false;
378 }
379 }
380
381 emit messagesToClient(name(), messages);
382
383 // Restart flushing if any profilers are still running
384 if (stillRunning)
385 emit startFlushTimer();
386}
387
388void QQmlProfilerServiceImpl::stateAboutToBeChanged(QQmlDebugService::State newState)
389{
390 QMutexLocker lock(&m_configMutex);
391
392 if (state() == newState)
393 return;
394
395 // Stop all profiling and send the data before we get disabled.
396 if (newState != Enabled) {
397 for (auto it = m_engineProfilers.keyBegin(), end = m_engineProfilers.keyEnd();
398 it != end; ++it) {
399 stopProfiling(*it);
400 }
401 }
402}
403
404void QQmlProfilerServiceImpl::messageReceived(const QByteArray &message)
405{
406 QMutexLocker lock(&m_configMutex);
407
408 QQmlDebugPacket stream(message);
409
410 int engineId = -1;
411 quint64 features = std::numeric_limits<quint64>::max();
412 bool enabled;
413 quint32 flushInterval = 0;
414 stream >> enabled;
415 if (!stream.atEnd())
416 stream >> engineId;
417 if (!stream.atEnd())
418 stream >> features;
419 if (!stream.atEnd()) {
420 stream >> flushInterval;
421 m_flushTimer.setInterval(
422 static_cast<int>(qMin(flushInterval,
423 static_cast<quint32>(std::numeric_limits<int>::max()))));
424 auto timerStart = static_cast<void(QTimer::*)()>(&QTimer::start);
425 if (flushInterval > 0) {
426 connect(&m_flushTimer, &QTimer::timeout, this, &QQmlProfilerServiceImpl::flush);
427 connect(this, &QQmlProfilerServiceImpl::startFlushTimer, &m_flushTimer, timerStart);
428 connect(this, &QQmlProfilerServiceImpl::stopFlushTimer, &m_flushTimer, &QTimer::stop);
429 } else {
430 disconnect(&m_flushTimer, &QTimer::timeout, this, &QQmlProfilerServiceImpl::flush);
431 disconnect(this, &QQmlProfilerServiceImpl::startFlushTimer, &m_flushTimer, timerStart);
432 disconnect(this, &QQmlProfilerServiceImpl::stopFlushTimer,
433 &m_flushTimer, &QTimer::stop);
434 }
435 }
436
437 bool useMessageTypes = false;
438 if (!stream.atEnd())
439 stream >> useMessageTypes;
440
441 // If engineId == -1 objectForId() and then the cast will return 0.
442 if (enabled && useMessageTypes) // If the client doesn't support message types don't profile.
443 startProfiling(qobject_cast<QJSEngine *>(objectForId(engineId)), features);
444 else if (!enabled) // On stopProfiling the client doesn't repeat useMessageTypes.
445 stopProfiling(qobject_cast<QJSEngine *>(objectForId(engineId)));
446
447 stopWaiting();
448}
449
450void QQmlProfilerServiceImpl::flush()
451{
452 QMutexLocker lock(&m_configMutex);
453 QList<QQmlAbstractProfilerAdapter *> reporting;
454
455 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_engineProfilers)) {
456 if (profiler->isRunning()) {
457 m_startTimes.insert(-1, profiler);
458 reporting.append(profiler);
459 }
460 }
461
462 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
463 if (profiler->isRunning()) {
464 m_startTimes.insert(-1, profiler);
465 reporting.append(profiler);
466 }
467 }
468
469 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(reporting))
470 profiler->reportData();
471}
472
473QT_END_NAMESPACE
474
475#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.