Skip to content

Commit

Permalink
Profiler: Use RAII helper structs for ranges
Browse files Browse the repository at this point in the history
Exclusively use RAII helper structs for ranges.

Change-Id: Ief9ab25a9e49e1b2c3c091e5d9de6479e36eaa50
Reviewed-by: Christiaan Janssen <christiaan.janssen@nokia.com>
  • Loading branch information
Kai Koehne authored and Qt by Nokia committed Mar 2, 2012
1 parent d290cb3 commit 1815459
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 92 deletions.
57 changes: 10 additions & 47 deletions src/declarative/debugger/qdeclarativeprofilerservice.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
156 changes: 138 additions & 18 deletions src/declarative/debugger/qdeclarativeprofilerservice_p.h
Expand Up @@ -57,6 +57,7 @@
#include <QtCore/qelapsedtimer.h>
#include <QtCore/qmutex.h>
#include <QtCore/qvector.h>
#include <QtCore/qstringbuilder.h>

QT_BEGIN_HEADER

Expand All @@ -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
{
Expand Down Expand Up @@ -132,11 +126,6 @@ class Q_DECLARATIVE_EXPORT QDeclarativeProfilerService : public QDeclarativeDebu
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();
Expand All @@ -153,14 +142,16 @@ class Q_DECLARATIVE_EXPORT QDeclarativeProfilerService : public QDeclarativeDebu
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();
Expand All @@ -172,6 +163,135 @@ class Q_DECLARATIVE_EXPORT QDeclarativeProfilerService : public QDeclarativeDebu
bool m_messageReceived;
QVector<QDeclarativeProfilerData> 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
Expand Down
15 changes: 10 additions & 5 deletions src/declarative/qml/qdeclarativeboundsignal.cpp
Expand Up @@ -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())
Expand All @@ -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);
Expand Down
21 changes: 11 additions & 10 deletions src/declarative/qml/qdeclarativecomponent.cpp
Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -824,7 +824,8 @@ void QDeclarativeComponentPrivate::completeCreate()
QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
complete(ep, &state);

QDeclarativeProfilerService::endRange(QDeclarativeProfilerService::Creating);
delete profiler;
profiler = 0;
}
}

Expand Down

0 comments on commit 1815459

Please sign in to comment.