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 bool anyRunning =
false;
182 for (QQmlAbstractProfilerAdapter *engineProfiler : std::as_const(m_engineProfilers)) {
183 features |= engineProfiler->features();
184 anyRunning = anyRunning || engineProfiler->isRunning();
188 profiler->startProfiling(features);
193 QMutexLocker lock(&m_configMutex);
194 removeProfilerFromStartTimes(profiler);
195 m_globalProfilers.removeOne(profiler);
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++);
212
213
214
215
216
219 QMutexLocker lock(&m_configMutex);
221 if (features &
static_cast<quint64>(1) << ProfileDebugMessages) {
222 if (QDebugMessageService *messageService =
223 QQmlDebugConnector::instance()->service<QDebugMessageService>())
224 messageService->synchronizeTime(m_timer);
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);
241 d << idForObject(engine);
243 m_globalEnabled =
true;
244 m_globalFeatures = features;
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()) {
251 i.value()->startProfiling(features);
255 for (QJSEngine *profiledEngine : std::as_const(engines))
256 d << idForObject(profiledEngine);
260 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
261 if (!profiler->isRunning())
262 profiler->startProfiling(features);
265 emit startFlushTimer();
266 emit messageToClient(name(), d.data());
271
272
273
274
275
276
279 QMutexLocker lock(&m_configMutex);
280 QList<QQmlAbstractProfilerAdapter *> stopping;
281 QList<QQmlAbstractProfilerAdapter *> reporting;
283 if (engine ==
nullptr)
284 m_globalEnabled =
false;
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();
294 reporting << i.value();
300 if (stopping.isEmpty())
303 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
304 if (!profiler->isRunning())
306 m_startTimes.insert(-1, profiler);
308 reporting << profiler;
310 stopping << profiler;
314 emit stopFlushTimer();
315 m_waitingForStop =
true;
317 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(reporting))
318 profiler->reportData();
320 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(stopping))
321 profiler->stopProfiling();
325
326
329 QList<QByteArray> messages;
331 QQmlDebugPacket traceEnd;
332 if (m_waitingForStop) {
333 traceEnd << m_timer.nsecsElapsed() <<
static_cast<qint32>(Event)
334 <<
static_cast<qint32>(EndTrace);
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())) {
342 traceEnd << idForObject(i.key());
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);
355 m_startTimes.insert(next, first);
357 if (messages.size() >= QQmlAbstractProfilerAdapter::s_numMessagesPerBatch) {
358 emit messagesToClient(name(), messages);
363 bool stillRunning =
false;
364 for (
const QQmlAbstractProfilerAdapter *profiler : std::as_const(m_engineProfilers)) {
365 if (profiler->isRunning()) {
371 if (m_waitingForStop) {
373 messages << traceEnd.data();
378 ds <<
static_cast<qint64>(-1) <<
static_cast<qint32>(Complete);
379 messages << ds.data();
380 m_waitingForStop =
false;
384 emit messagesToClient(name(), messages);
388 emit startFlushTimer();
393 QMutexLocker lock(&m_configMutex);
395 if (state() == newState)
399 if (newState != Enabled) {
400 for (
auto it = m_engineProfilers.keyBegin(), end = m_engineProfilers.keyEnd();
409 QMutexLocker lock(&m_configMutex);
411 QQmlDebugPacket stream(message);
414 quint64 features = std::numeric_limits<quint64>::max();
416 quint32 flushInterval = 0;
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) {
430 connect(
this, &QQmlProfilerServiceImpl::startFlushTimer, &m_flushTimer, timerStart);
434 disconnect(
this, &QQmlProfilerServiceImpl::startFlushTimer, &m_flushTimer, timerStart);
436 &m_flushTimer, &QTimer::stop);
440 bool useMessageTypes =
false;
442 stream >> useMessageTypes;
445 if (enabled && useMessageTypes)
446 startProfiling(qobject_cast<QJSEngine *>(objectForId(engineId)), features);
448 stopProfiling(qobject_cast<QJSEngine *>(objectForId(engineId)));
455 QMutexLocker lock(&m_configMutex);
456 QList<QQmlAbstractProfilerAdapter *> reporting;
458 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_engineProfilers)) {
459 if (profiler->isRunning()) {
460 m_startTimes.insert(-1, profiler);
461 reporting.append(profiler);
465 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(m_globalProfilers)) {
466 if (profiler->isRunning()) {
467 m_startTimes.insert(-1, profiler);
468 reporting.append(profiler);
472 for (QQmlAbstractProfilerAdapter *profiler : std::as_const(reporting))
473 profiler->reportData();
478#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