From: Kai Koehne Date: Wed, 29 Feb 2012 13:59:49 +0000 (+0100) Subject: Profiler: Use RAII helper structs for ranges X-Git-Tag: qt-v5.0.0-alpha1~251 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=181545935323c74c2ec9c5a89ff51606008e30c0;p=profile%2Fivi%2Fqtdeclarative.git Profiler: Use RAII helper structs for ranges Exclusively use RAII helper structs for ranges. Change-Id: Ief9ab25a9e49e1b2c3c091e5d9de6479e36eaa50 Reviewed-by: Christiaan Janssen --- diff --git a/src/declarative/debugger/qdeclarativeprofilerservice.cpp b/src/declarative/debugger/qdeclarativeprofilerservice.cpp index 074355f..3ad8749 100644 --- a/src/declarative/debugger/qdeclarativeprofilerservice.cpp +++ b/src/declarative/debugger/qdeclarativeprofilerservice.cpp @@ -52,23 +52,10 @@ QT_BEGIN_NAMESPACE +// instance will be set, unset in constructor. Allows static methods to be inlined. +QDeclarativeProfilerService *QDeclarativeProfilerService::instance = 0; Q_GLOBAL_STATIC(QDeclarativeProfilerService, profilerInstance) -QDeclarativeBindingProfiler::QDeclarativeBindingProfiler(const QString &url, int line, int column) -{ - QDeclarativeProfilerService::startRange(QDeclarativeProfilerService::Binding); - QDeclarativeProfilerService::rangeLocation(QDeclarativeProfilerService::Binding, url, line, column); -} - -QDeclarativeBindingProfiler::~QDeclarativeBindingProfiler() -{ - QDeclarativeProfilerService::endRange(QDeclarativeProfilerService::Binding); -} - -void QDeclarativeBindingProfiler::addDetail(const QString &details) -{ - QDeclarativeProfilerService::rangeData(QDeclarativeProfilerService::Binding, details); -} // convert to a QByteArray that can be sent to the debug client // use of QDataStream can skew results @@ -106,12 +93,13 @@ QDeclarativeProfilerService::QDeclarativeProfilerService() QDeclarativeProfilerService::~QDeclarativeProfilerService() { + instance = 0; } void QDeclarativeProfilerService::initialize() { // just make sure that the service is properly registered - profilerInstance(); + instance = profilerInstance(); } bool QDeclarativeProfilerService::startProfiling() @@ -134,31 +122,6 @@ void QDeclarativeProfilerService::addEvent(EventType t) profilerInstance()->addEventImpl(t); } -void QDeclarativeProfilerService::startRange(RangeType t) -{ - profilerInstance()->startRangeImpl(t); -} - -void QDeclarativeProfilerService::rangeData(RangeType t, const QString &data) -{ - profilerInstance()->rangeDataImpl(t, data); -} - -void QDeclarativeProfilerService::rangeLocation(RangeType t, const QString &fileName, int line, int column) -{ - profilerInstance()->rangeLocationImpl(t, fileName, line, column); -} - -void QDeclarativeProfilerService::rangeLocation(RangeType t, const QUrl &fileName, int line, int column) -{ - profilerInstance()->rangeLocationImpl(t, fileName, line, column); -} - -void QDeclarativeProfilerService::endRange(RangeType t) -{ - profilerInstance()->endRangeImpl(t); -} - void QDeclarativeProfilerService::animationFrame(qint64 delta) { profilerInstance()->animationFrameImpl(delta); @@ -209,7 +172,7 @@ void QDeclarativeProfilerService::addEventImpl(EventType event) processMessage(ed); } -void QDeclarativeProfilerService::startRangeImpl(RangeType range) +void QDeclarativeProfilerService::startRange(RangeType range) { if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled) return; @@ -218,7 +181,7 @@ void QDeclarativeProfilerService::startRangeImpl(RangeType range) processMessage(rd); } -void QDeclarativeProfilerService::rangeDataImpl(RangeType range, const QString &rData) +void QDeclarativeProfilerService::rangeData(RangeType range, const QString &rData) { if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled) return; @@ -227,7 +190,7 @@ void QDeclarativeProfilerService::rangeDataImpl(RangeType range, const QString & processMessage(rd); } -void QDeclarativeProfilerService::rangeDataImpl(RangeType range, const QUrl &rData) +void QDeclarativeProfilerService::rangeData(RangeType range, const QUrl &rData) { if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled) return; @@ -236,7 +199,7 @@ void QDeclarativeProfilerService::rangeDataImpl(RangeType range, const QUrl &rDa processMessage(rd); } -void QDeclarativeProfilerService::rangeLocationImpl(RangeType range, const QString &fileName, int line, int column) +void QDeclarativeProfilerService::rangeLocation(RangeType range, const QString &fileName, int line, int column) { if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled) return; @@ -245,7 +208,7 @@ void QDeclarativeProfilerService::rangeLocationImpl(RangeType range, const QStri processMessage(rd); } -void QDeclarativeProfilerService::rangeLocationImpl(RangeType range, const QUrl &fileName, int line, int column) +void QDeclarativeProfilerService::rangeLocation(RangeType range, const QUrl &fileName, int line, int column) { if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled) return; @@ -254,7 +217,7 @@ void QDeclarativeProfilerService::rangeLocationImpl(RangeType range, const QUrl processMessage(rd); } -void QDeclarativeProfilerService::endRangeImpl(RangeType range) +void QDeclarativeProfilerService::endRange(RangeType range) { if (!QDeclarativeDebugService::isDebuggingEnabled() || !m_enabled) return; diff --git a/src/declarative/debugger/qdeclarativeprofilerservice_p.h b/src/declarative/debugger/qdeclarativeprofilerservice_p.h index d2f263c..ef92e68 100644 --- a/src/declarative/debugger/qdeclarativeprofilerservice_p.h +++ b/src/declarative/debugger/qdeclarativeprofilerservice_p.h @@ -57,6 +57,7 @@ #include #include #include +#include QT_BEGIN_HEADER @@ -83,13 +84,6 @@ Q_DECLARE_TYPEINFO(QDeclarativeProfilerData, Q_MOVABLE_TYPE); class QUrl; class QDeclarativeEngine; -// RAII -class Q_AUTOTEST_EXPORT QDeclarativeBindingProfiler { -public: - QDeclarativeBindingProfiler(const QString &url, int line, int column); - ~QDeclarativeBindingProfiler(); - void addDetail(const QString &details); -}; class Q_DECLARATIVE_EXPORT QDeclarativeProfilerService : public QDeclarativeDebugService { @@ -132,11 +126,6 @@ public: static bool stopProfiling(); static void sendStartedProfilingMessage(); static void addEvent(EventType); - static void startRange(RangeType); - static void rangeData(RangeType, const QString &); - static void rangeLocation(RangeType, const QString &, int, int); - static void rangeLocation(RangeType, const QUrl &, int, int); - static void endRange(RangeType); static void animationFrame(qint64); static void sendProfilingData(); @@ -153,14 +142,16 @@ private: bool stopProfilingImpl(); void sendStartedProfilingMessageImpl(); void addEventImpl(EventType); - void startRangeImpl(RangeType); - void rangeDataImpl(RangeType, const QString &); - void rangeDataImpl(RangeType, const QUrl &); - void rangeLocationImpl(RangeType, const QString &, int, int); - void rangeLocationImpl(RangeType, const QUrl &, int, int); - void endRangeImpl(RangeType); void animationFrameImpl(qint64); + void startRange(RangeType); + void rangeData(RangeType, const QString &); + void rangeData(RangeType, const QUrl &); + void rangeLocation(RangeType, const QString &, int, int); + void rangeLocation(RangeType, const QUrl &, int, int); + void endRange(RangeType); + + bool profilingEnabled(); void setProfilingEnabled(bool enable); void sendMessages(); @@ -172,6 +163,135 @@ private: bool m_messageReceived; QVector m_data; QMutex m_mutex; + + static QDeclarativeProfilerService *instance; + + friend struct QDeclarativeBindingProfiler; + friend struct QDeclarativeHandlingSignalProfiler; + friend struct QDeclarativeObjectCreatingProfiler; + friend struct QDeclarativeCompilingProfiler; +}; + +// +// RAII helper structs +// + +struct QDeclarativeBindingProfiler { + QDeclarativeBindingProfiler(const QString &url, int line, int column) + { + QDeclarativeProfilerService *instance = QDeclarativeProfilerService::instance; + enabled = instance ? instance->profilingEnabled() : false; + if (enabled) { + instance->startRange(QDeclarativeProfilerService::Binding); + instance->rangeLocation(QDeclarativeProfilerService::Binding, url, line, column); + } + } + + ~QDeclarativeBindingProfiler() + { + if (enabled) + QDeclarativeProfilerService::instance->endRange(QDeclarativeProfilerService::Binding); + } + + void addDetail(const QString &details) + { + if (enabled) + QDeclarativeProfilerService::instance->rangeData(QDeclarativeProfilerService::Binding, + details); + } +\ + bool enabled; +}; + +struct QDeclarativeHandlingSignalProfiler { + QDeclarativeHandlingSignalProfiler() + { + enabled = QDeclarativeProfilerService::instance + ? QDeclarativeProfilerService::instance->profilingEnabled() : false; + if (enabled) { + QDeclarativeProfilerService::instance->startRange( + QDeclarativeProfilerService::HandlingSignal); + } + } + + void setSignalInfo(const QString &name, const QString &expression) + { + if (enabled) + QDeclarativeProfilerService::instance->rangeData( + QDeclarativeProfilerService::HandlingSignal, + name % QLatin1String(": ") % expression); + } + + void setLocation(const QString &file, int line, int column) + { + if (enabled) + QDeclarativeProfilerService::instance->rangeLocation( + QDeclarativeProfilerService::HandlingSignal, file, line, column); + } + + ~QDeclarativeHandlingSignalProfiler() + { + if (enabled) + QDeclarativeProfilerService::instance->endRange( + QDeclarativeProfilerService::HandlingSignal); + } + + bool enabled; +}; + +struct QDeclarativeObjectCreatingProfiler { + QDeclarativeObjectCreatingProfiler() + { + QDeclarativeProfilerService *instance = QDeclarativeProfilerService::instance; + enabled = instance ? + instance->profilingEnabled() : false; + if (enabled) + instance->startRange(QDeclarativeProfilerService::Creating); + } + + void setTypeName(const QString &typeName) + { + if (enabled) + QDeclarativeProfilerService::instance->rangeData( + QDeclarativeProfilerService::Creating, typeName); + } + + void setLocation(const QUrl &url, int line, int column) + { + if (enabled) + QDeclarativeProfilerService::instance->rangeLocation( + QDeclarativeProfilerService::Creating, url, line, column); + } + + ~QDeclarativeObjectCreatingProfiler() + { + if (enabled) + QDeclarativeProfilerService::instance->endRange(QDeclarativeProfilerService::Creating); + } + + bool enabled; +}; + +struct QDeclarativeCompilingProfiler { + QDeclarativeCompilingProfiler(const QString &name) + { + QDeclarativeProfilerService *instance = QDeclarativeProfilerService::instance; + enabled = instance ? + instance->profilingEnabled() : false; + if (enabled) { + instance->startRange(QDeclarativeProfilerService::Compiling); + instance->rangeLocation(QDeclarativeProfilerService::Compiling, name, 1, 1); + instance->rangeData(QDeclarativeProfilerService::Compiling, name); + } + } + + ~QDeclarativeCompilingProfiler() + { + if (enabled) + QDeclarativeProfilerService::instance->endRange(QDeclarativeProfilerService::Compiling); + } + + bool enabled; }; QT_END_NAMESPACE diff --git a/src/declarative/qml/qdeclarativeboundsignal.cpp b/src/declarative/qml/qdeclarativeboundsignal.cpp index 5d1c28b..bf96f03 100644 --- a/src/declarative/qml/qdeclarativeboundsignal.cpp +++ b/src/declarative/qml/qdeclarativeboundsignal.cpp @@ -170,12 +170,18 @@ int QDeclarativeBoundSignal::qt_metacall(QMetaObject::Call c, int id, void **a) if (c == QMetaObject::InvokeMetaMethod && id == evaluateIdx) { if (!m_expression) return -1; - if (QDeclarativeDebugService::isDebuggingEnabled()) { - QDeclarativeProfilerService::startRange(QDeclarativeProfilerService::HandlingSignal); - QDeclarativeProfilerService::rangeData(QDeclarativeProfilerService::HandlingSignal, QLatin1String(m_signal.signature()) % QLatin1String(": ") % m_expression->expression()); - QDeclarativeProfilerService::rangeLocation(QDeclarativeProfilerService::HandlingSignal, m_expression->sourceFile(), m_expression->lineNumber(), m_expression->columnNumber()); + + if (QDeclarativeDebugService::isDebuggingEnabled()) QV8DebugService::instance()->signalEmitted(QString::fromAscii(m_signal.signature())); + + QDeclarativeHandlingSignalProfiler prof; + if (prof.enabled) { + prof.setSignalInfo(QString::fromLatin1(m_signal.signature()), + m_expression->expression()); + prof.setLocation(m_expression->sourceFile(), m_expression->lineNumber(), + m_expression->columnNumber()); } + m_isEvaluating = true; if (!m_paramsValid) { if (!m_signal.parameterTypes().isEmpty()) @@ -191,7 +197,6 @@ int QDeclarativeBoundSignal::qt_metacall(QMetaObject::Call c, int id, void **a) } if (m_params) m_params->clearValues(); m_isEvaluating = false; - QDeclarativeProfilerService::endRange(QDeclarativeProfilerService::HandlingSignal); return -1; } else { return QObject::qt_metacall(c, id, a); diff --git a/src/declarative/qml/qdeclarativecomponent.cpp b/src/declarative/qml/qdeclarativecomponent.cpp index c168c8f..57c3074 100644 --- a/src/declarative/qml/qdeclarativecomponent.cpp +++ b/src/declarative/qml/qdeclarativecomponent.cpp @@ -739,14 +739,15 @@ QDeclarativeComponentPrivate::beginCreate(QDeclarativeContextData *context) QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine); - bool isRoot = enginePriv->inProgressCreations == 0; + if (enginePriv->inProgressCreations == 0) { + // only track root, since further ones might not be properly nested + profiler = new QDeclarativeObjectCreatingProfiler(); + } + enginePriv->inProgressCreations++; state.errors.clear(); state.completePending = true; - if (isRoot) - QDeclarativeProfilerService::startRange(QDeclarativeProfilerService::Creating); - enginePriv->referenceScarceResources(); state.vme.init(context, cc, start, creationContext); QObject *rv = state.vme.execute(&state.errors); @@ -762,13 +763,12 @@ QDeclarativeComponentPrivate::beginCreate(QDeclarativeContextData *context) if (!context->isInternal) context->asQDeclarativeContextPrivate()->instances.append(rv); QDeclarativeEngineDebugService::instance()->objectCreated(engine, rv); - if (isRoot) { - QDeclarativeProfilerService::rangeData(QDeclarativeProfilerService::Creating, - buildTypeNameForDebug(rv->metaObject())); + + if (profiler && profiler->enabled) { + profiler->setTypeName(buildTypeNameForDebug(rv->metaObject())); QDeclarativeData *data = QDeclarativeData::get(rv); Q_ASSERT(data); - QDeclarativeProfilerService::rangeLocation(QDeclarativeProfilerService::Creating, - cc->url, data->lineNumber, data->columnNumber); + profiler->setLocation(cc->url, data->lineNumber, data->columnNumber); } } @@ -824,7 +824,8 @@ void QDeclarativeComponentPrivate::completeCreate() QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine); complete(ep, &state); - QDeclarativeProfilerService::endRange(QDeclarativeProfilerService::Creating); + delete profiler; + profiler = 0; } } diff --git a/src/declarative/qml/qdeclarativecomponent_p.h b/src/declarative/qml/qdeclarativecomponent_p.h index 2a23736..6824bd2 100644 --- a/src/declarative/qml/qdeclarativecomponent_p.h +++ b/src/declarative/qml/qdeclarativecomponent_p.h @@ -62,6 +62,7 @@ #include "qdeclarativevme_p.h" #include "qdeclarativeerror.h" #include "qdeclarative.h" +#include "../debugger/qdeclarativeprofilerservice_p.h" #include #include @@ -83,7 +84,7 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeComponentPrivate : public QObject Q_DECLARE_PUBLIC(QDeclarativeComponent) public: - QDeclarativeComponentPrivate() : typeData(0), progress(0.), start(-1), cc(0), engine(0), creationContext(0) {} + QDeclarativeComponentPrivate() : typeData(0), progress(0.), start(-1), cc(0), engine(0), creationContext(0), profiler(0) {} QObject *beginCreate(QDeclarativeContextData *); void completeCreate(); @@ -116,6 +117,7 @@ public: QDeclarativeEngine *engine; QDeclarativeGuardedContextData creationContext; + QDeclarativeObjectCreatingProfiler *profiler; void clear(); diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index dca0ef2..963bec5 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -966,13 +966,13 @@ Q_AUTOTEST_EXPORT void qmlExecuteDeferred(QObject *object) QDeclarativeData *data = QDeclarativeData::get(object); if (data && data->deferredComponent) { - if (QDeclarativeDebugService::isDebuggingEnabled()) { - QDeclarativeProfilerService::startRange(QDeclarativeProfilerService::Creating); + QDeclarativeObjectCreatingProfiler prof; + if (prof.enabled) { QDeclarativeType *type = QDeclarativeMetaType::qmlType(object->metaObject()); - QString typeName = type ? type->qmlTypeName() : QString::fromUtf8(object->metaObject()->className()); - QDeclarativeProfilerService::rangeData(QDeclarativeProfilerService::Creating, typeName); + prof.setTypeName(type ? type->qmlTypeName() + : QString::fromUtf8(object->metaObject()->className())); if (data->outerContext) - QDeclarativeProfilerService::rangeLocation(QDeclarativeProfilerService::Creating, data->outerContext->url, data->lineNumber, data->columnNumber); + prof.setLocation(data->outerContext->url, data->lineNumber, data->columnNumber); } QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(data->context->engine); @@ -983,7 +983,6 @@ Q_AUTOTEST_EXPORT void qmlExecuteDeferred(QObject *object) data->deferredComponent = 0; QDeclarativeComponentPrivate::complete(ep, &state); - QDeclarativeProfilerService::endRange(QDeclarativeProfilerService::Creating); } } diff --git a/src/declarative/qml/qdeclarativetypeloader.cpp b/src/declarative/qml/qdeclarativetypeloader.cpp index a07e4fb..92dac18 100644 --- a/src/declarative/qml/qdeclarativetypeloader.cpp +++ b/src/declarative/qml/qdeclarativetypeloader.cpp @@ -1578,13 +1578,12 @@ void QDeclarativeTypeData::downloadProgressChanged(qreal p) void QDeclarativeTypeData::compile() { Q_ASSERT(m_compiledData == 0); - QDeclarativeProfilerService::startRange(QDeclarativeProfilerService::Compiling); m_compiledData = new QDeclarativeCompiledData(typeLoader()->engine()); m_compiledData->url = finalUrl(); m_compiledData->name = finalUrlString(); - QDeclarativeProfilerService::rangeLocation(QDeclarativeProfilerService::Compiling, m_compiledData->name,1,1); - QDeclarativeProfilerService::rangeData(QDeclarativeProfilerService::Compiling, m_compiledData->name); + + QDeclarativeCompilingProfiler prof(m_compiledData->name); QDeclarativeCompiler compiler(&scriptParser._pool); if (!compiler.compile(typeLoader()->engine(), this, m_compiledData)) { @@ -1592,7 +1591,6 @@ void QDeclarativeTypeData::compile() m_compiledData->release(); m_compiledData = 0; } - QDeclarativeProfilerService::endRange(QDeclarativeProfilerService::Compiling); } void QDeclarativeTypeData::resolveTypes() diff --git a/tests/auto/declarative/debugger/qdeclarativeprofilerservice/qdeclarativeprofilerservice.pro b/tests/auto/declarative/debugger/qdeclarativeprofilerservice/qdeclarativeprofilerservice.pro index 564945f..52139bf 100644 --- a/tests/auto/declarative/debugger/qdeclarativeprofilerservice/qdeclarativeprofilerservice.pro +++ b/tests/auto/declarative/debugger/qdeclarativeprofilerservice/qdeclarativeprofilerservice.pro @@ -12,4 +12,4 @@ include (../../../shared/util.pri) CONFIG += parallel_test declarative_debug -QT += declarative-private testlib +QT += core-private v8-private declarative-private testlib