10#include <private/qjsengine_p.h>
11#include <private/qqmldebugpluginmanager_p.h>
13#include <QtCore/qurl.h>
14#include <QtCore/qtimer.h>
15#include <QtCore/qthread.h>
16#include <QtCore/qcoreapplication.h>
22Q_QML_DEBUG_PLUGIN_LOADER(QQmlAbstractProfilerAdapter)
25
26
27
28
30 QQmlConfigurableDebugService<QQmlProfilerService>(1, parent),
31 m_waitingForStop(
false), m_globalEnabled(
false), m_globalFeatures(0)
34 QQmlAbstractProfilerAdapter *quickAdapter =
35 loadQQmlAbstractProfilerAdapter(QLatin1String(
"QQuickProfilerAdapter"));
37 addGlobalProfiler(quickAdapter);
38 quickAdapter->setService(
this);
42 QQmlAbstractProfilerAdapter *quick3DAdapter =
43 loadQQmlAbstractProfilerAdapter(QLatin1String(
"QQuick3DProfilerAdapter"));
45 addGlobalProfiler(quick3DAdapter);
46 quick3DAdapter->setService(
this);
56 qDeleteAll(m_engineProfilers);
57 qDeleteAll(m_globalProfilers);
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++);
73 m_startTimes.insert(0, profiler);
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);
87 for (QJSEngine *engine : std::as_const(enginesToRelease)) {
88 m_stoppingEngines.removeOne(engine);
89 emit detachedFromEngine(engine);
96 Q_ASSERT_X(QThread::currentThread() == engine->thread(), Q_FUNC_INFO,
97 "QML profilers have to be added from the engine thread");
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);
109 addEngineProfiler(v4Adapter, engine);
110 QQmlConfigurableDebugService<QQmlProfilerService>::engineAboutToBeAdded(engine);
115 Q_ASSERT_X(QThread::currentThread() == engine->thread(), Q_FUNC_INFO,
116 "QML profilers have to be added from the engine thread");
118 QMutexLocker lock(&m_configMutex);
121 startProfiling(engine, m_globalFeatures);
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();
130 Q_ASSERT_X(QThread::currentThread() == engine->thread(), Q_FUNC_INFO,
131 "QML profilers have to be removed from the engine thread");
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())
140 profiler->startWaiting();
143 m_stoppingEngines.append(engine);
144 stopProfiling(engine);
146 emit detachedFromEngine(engine);
152 Q_ASSERT_X(QThread::currentThread() == engine->thread(), Q_FUNC_INFO,
153 "QML profilers have to be removed from the engine thread");
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);
162 m_engineProfilers.remove(engine);
167 profiler->moveToThread(thread());
168 profiler->synchronize(m_timer);
169 m_engineProfilers.insert(engine, profiler);
174 QMutexLocker lock(&m_configMutex);
175 profiler->synchronize(m_timer);
176 m_globalProfilers.append(profiler);
180 quint64 features = 0;
181 for (QQmlAbstractProfilerAdapter *engineProfiler : std::as_const(m_engineProfilers))
182 features |= engineProfiler->features();
185 profiler->startProfiling(features);
190 QMutexLocker lock(&m_configMutex);
191 removeProfilerFromStartTimes(profiler);
192 m_globalProfilers.removeOne(profiler);
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++);
209
210
211
212
213
216 QMutexLocker lock(&m_configMutex);
218 if (features &
static_cast<quint64>(1) << ProfileDebugMessages) {
219 if (QDebugMessageService *messageService =
220 QQmlDebugConnector::instance()->service<QDebugMessageService>())
221 messageService->synchronizeTime(m_timer);
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);
238 d << idForObject(engine);
240 m_globalEnabled =
true;
241 m_globalFeatures = features;
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()) {
248 i.value()->startProfiling(features);
252 for (QJSEngine *profiledEngine : std::as_const(engines))
253 d << idForObject(profiledEngine);
257 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
258 if (!profiler->isRunning())
259 profiler->startProfiling(features);
262 emit startFlushTimer();
263 emit messageToClient(name(), d.data());
268
269
270
271
272
273
276 QMutexLocker lock(&m_configMutex);
277 QList<QQmlAbstractProfilerAdapter *> stopping;
278 QList<QQmlAbstractProfilerAdapter *> reporting;
280 if (engine ==
nullptr)
281 m_globalEnabled =
false;
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();
291 reporting << i.value();
297 if (stopping.isEmpty())
300 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
301 if (!profiler->isRunning())
303 m_startTimes.insert(-1, profiler);
305 reporting << profiler;
307 stopping << profiler;
311 emit stopFlushTimer();
312 m_waitingForStop =
true;
314 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(reporting))
315 profiler->reportData();
317 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(stopping))
318 profiler->stopProfiling();
322
323
326 QList<QByteArray> messages;
328 QQmlDebugPacket traceEnd;
329 if (m_waitingForStop) {
330 traceEnd << m_timer.nsecsElapsed() <<
static_cast<qint32>(Event)
331 <<
static_cast<qint32>(EndTrace);
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())) {
339 traceEnd << idForObject(i.key());
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);
352 m_startTimes.insert(next, first);
354 if (messages.size() >= QQmlAbstractProfilerAdapter::s_numMessagesPerBatch) {
355 emit messagesToClient(name(), messages);
360 bool stillRunning =
false;
361 for (
const QQmlAbstractProfilerAdapter *profiler : std::as_const(m_engineProfilers)) {
362 if (profiler->isRunning()) {
368 if (m_waitingForStop) {
370 messages << traceEnd.data();
375 ds <<
static_cast<qint64>(-1) <<
static_cast<qint32>(Complete);
376 messages << ds.data();
377 m_waitingForStop =
false;
381 emit messagesToClient(name(), messages);
385 emit startFlushTimer();
390 QMutexLocker lock(&m_configMutex);
392 if (state() == newState)
396 if (newState != Enabled) {
397 for (
auto it = m_engineProfilers.keyBegin(), end = m_engineProfilers.keyEnd();
406 QMutexLocker lock(&m_configMutex);
408 QQmlDebugPacket stream(message);
411 quint64 features = std::numeric_limits<quint64>::max();
413 quint32 flushInterval = 0;
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);
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);
437 bool useMessageTypes =
false;
439 stream >> useMessageTypes;
442 if (enabled && useMessageTypes)
443 startProfiling(qobject_cast<QJSEngine *>(objectForId(engineId)), features);
445 stopProfiling(qobject_cast<QJSEngine *>(objectForId(engineId)));
452 QMutexLocker lock(&m_configMutex);
453 QList<QQmlAbstractProfilerAdapter *> reporting;
455 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_engineProfilers)) {
456 if (profiler->isRunning()) {
457 m_startTimes.insert(-1, profiler);
458 reporting.append(profiler);
462 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
463 if (profiler->isRunning()) {
464 m_startTimes.insert(-1, profiler);
465 reporting.append(profiler);
469 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(reporting))
470 profiler->reportData();
475#include "moc_qqmlprofilerservice.cpp"
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.
~QQmlProfilerServiceImpl() override