diff options
author | Ulf Hermann <[email protected]> | 2014-01-22 17:46:20 +0100 |
---|---|---|
committer | The Qt Project <[email protected]> | 2014-02-12 21:29:54 +0100 |
commit | ebef5a5a68d2432425109bd3e4945988c638786d (patch) | |
tree | f91b2d5e4003a0c5afb63ee703d4475e919bcd71 /src | |
parent | 146741561373693d24e64891b8865d00146cf1dc (diff) |
Satellite profiler for V4
Self-contained profiler for V4. By itself it doesn't have any
connections to qqmlprofilerservice.
Change-Id: I471a6119e07eab9c5f4712a16835be49c8886d1a
Reviewed-by: Kai Koehne <[email protected]>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/jsruntime/jsruntime.pri | 6 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4engine.cpp | 9 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4engine_p.h | 5 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4functionobject.cpp | 9 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4profiling.cpp | 101 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4profiling_p.h | 169 |
6 files changed, 293 insertions, 6 deletions
diff --git a/src/qml/jsruntime/jsruntime.pri b/src/qml/jsruntime/jsruntime.pri index 43eb61ac9a..ac26794506 100644 --- a/src/qml/jsruntime/jsruntime.pri +++ b/src/qml/jsruntime/jsruntime.pri @@ -42,7 +42,8 @@ SOURCES += \ $$PWD/qv4include.cpp \ $$PWD/qv4qobjectwrapper.cpp \ $$PWD/qv4qmlextensions.cpp \ - $$PWD/qv4vme_moth.cpp + $$PWD/qv4vme_moth.cpp \ + $$PWD/qv4profiling.cpp HEADERS += \ $$PWD/qv4global_p.h \ @@ -91,7 +92,8 @@ HEADERS += \ $$PWD/qv4include_p.h \ $$PWD/qv4qobjectwrapper_p.h \ $$PWD/qv4qmlextensions_p.h \ - $$PWD/qv4vme_moth_p.h + $$PWD/qv4vme_moth_p.h \ + $$PWD/qv4profiling_p.h # Use SSE2 floating point math on 32 bit instead of the default # 387 to make test results pass on 32 and on 64 bit builds. diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index 5a25a9f3b6..3cf705c584 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -63,6 +63,7 @@ #include <qv4stringobject_p.h> #include <qv4identifiertable_p.h> #include "qv4debugging_p.h" +#include "qv4profiling_p.h" #include "qv4executableallocator_p.h" #include "qv4sequenceobject_p.h" #include "qv4qobjectwrapper_p.h" @@ -166,6 +167,7 @@ ExecutionEngine::ExecutionEngine(QQmlJS::EvalISelFactory *factory) , bumperPointerAllocator(new WTF::BumpPointerAllocator) , jsStack(new WTF::PageAllocation) , debugger(0) + , profiler(0) , globalObject(0) , globalCode(0) , v8Engine(0) @@ -401,6 +403,7 @@ ExecutionEngine::ExecutionEngine(QQmlJS::EvalISelFactory *factory) ExecutionEngine::~ExecutionEngine() { delete debugger; + delete profiler; delete m_multiplyWrappedQObjects; m_multiplyWrappedQObjects = 0; delete identifierTable; @@ -428,6 +431,12 @@ void ExecutionEngine::enableDebugger() iselFactory.reset(new QQmlJS::Moth::ISelFactory); } +void ExecutionEngine::enableProfiler() +{ + Q_ASSERT(!profiler); + profiler = new QV4::Profiling::Profiler(); +} + void ExecutionEngine::initRootContext() { rootContext = static_cast<GlobalContext *>(memoryManager->allocManaged(sizeof(GlobalContext) + sizeof(CallData))); diff --git a/src/qml/jsruntime/qv4engine_p.h b/src/qml/jsruntime/qv4engine_p.h index 63c57b8478..7598bfb65a 100644 --- a/src/qml/jsruntime/qv4engine_p.h +++ b/src/qml/jsruntime/qv4engine_p.h @@ -61,6 +61,9 @@ namespace QV4 { namespace Debugging { class Debugger; } // namespace Debugging +namespace Profiling { +class Profiler; +} // namespace Profiling namespace CompiledData { struct CompilationUnit; } @@ -169,6 +172,7 @@ public: IdentifierTable *identifierTable; QV4::Debugging::Debugger *debugger; + QV4::Profiling::Profiler *profiler; Object *globalObject; @@ -291,6 +295,7 @@ public: ~ExecutionEngine(); void enableDebugger(); + void enableProfiler(); ExecutionContext *pushGlobalContext(); void pushContext(CallContext *context); diff --git a/src/qml/jsruntime/qv4functionobject.cpp b/src/qml/jsruntime/qv4functionobject.cpp index a3e47c42ca..58e0277395 100644 --- a/src/qml/jsruntime/qv4functionobject.cpp +++ b/src/qml/jsruntime/qv4functionobject.cpp @@ -67,6 +67,7 @@ #include <iostream> #include <algorithm> #include "qv4alloca_p.h" +#include "qv4profiling_p.h" using namespace QV4; @@ -450,7 +451,7 @@ ReturnedValue ScriptFunction::construct(Managed *that, CallData *callData) QmlContextWrapper::registerQmlDependencies(v4, f->function->compiledFunction); ExecutionContextSaver ctxSaver(context); - ScopedValue result(scope, f->function->code(ctx, f->function->codeData)); + ScopedValue result(scope, Q_V4_PROFILE(v4, ctx, f->function)); if (result->isObject()) return result.asReturnedValue(); return obj.asReturnedValue(); @@ -473,7 +474,7 @@ ReturnedValue ScriptFunction::call(Managed *that, CallData *callData) QmlContextWrapper::registerQmlDependencies(ctx->engine, f->function->compiledFunction); ExecutionContextSaver ctxSaver(context); - return f->function->code(ctx, f->function->codeData); + return Q_V4_PROFILE(v4, ctx, f->function); } DEFINE_OBJECT_VTABLE(SimpleScriptFunction); @@ -544,7 +545,7 @@ ReturnedValue SimpleScriptFunction::construct(Managed *that, CallData *callData) if (f->function->compiledFunction->hasQmlDependencies()) QmlContextWrapper::registerQmlDependencies(v4, f->function->compiledFunction); - Scoped<Object> result(scope, f->function->code(&ctx, f->function->codeData)); + Scoped<Object> result(scope, Q_V4_PROFILE(v4, &ctx, f->function)); if (!result) return callData->thisObject.asReturnedValue(); @@ -581,7 +582,7 @@ ReturnedValue SimpleScriptFunction::call(Managed *that, CallData *callData) if (f->function->compiledFunction->hasQmlDependencies()) QmlContextWrapper::registerQmlDependencies(v4, f->function->compiledFunction); - return f->function->code(&ctx, f->function->codeData); + return Q_V4_PROFILE(v4, &ctx, f->function); } diff --git a/src/qml/jsruntime/qv4profiling.cpp b/src/qml/jsruntime/qv4profiling.cpp new file mode 100644 index 0000000000..69efbfdee3 --- /dev/null +++ b/src/qml/jsruntime/qv4profiling.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qv4profiling_p.h" + +QT_BEGIN_NAMESPACE + +using namespace QV4; +using namespace QV4::Profiling; + +FunctionCallProperties FunctionCall::resolve() const +{ + FunctionCallProperties props = { + m_start, + m_end, + m_function->name.toQString(), + m_function->compilationUnit->fileName(), + m_function->compiledFunction->location.line, + m_function->compiledFunction->location.column + }; + return props; +} + + +Profiler::Profiler() : enabled(false) +{ + static int metatype = qRegisterMetaType<QList<QV4::Profiling::FunctionCallProperties> >(); + Q_UNUSED(metatype); + m_timer.start(); +} + +struct FunctionCallComparator { + bool operator()(const FunctionCallProperties &p1, const FunctionCallProperties &p2) + { return p1.start < p2.start; } +}; + +void Profiler::stopProfiling() +{ + enabled = false; + reportData(); +} + +void Profiler::reportData() +{ + QList<FunctionCallProperties> resolved; + resolved.reserve(m_data.size()); + FunctionCallComparator comp; + foreach (const FunctionCall &call, m_data) { + FunctionCallProperties props = call.resolve(); + resolved.insert(std::upper_bound(resolved.begin(), resolved.end(), props, comp), props); + } + emit dataReady(resolved); +} + +void Profiler::startProfiling() +{ + if (!enabled) { + m_data.clear(); + enabled = true; + } +} + +QT_END_NAMESPACE diff --git a/src/qml/jsruntime/qv4profiling_p.h b/src/qml/jsruntime/qv4profiling_p.h new file mode 100644 index 0000000000..6869b3134d --- /dev/null +++ b/src/qml/jsruntime/qv4profiling_p.h @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QV4PROFILING_H +#define QV4PROFILING_H + +#include "qv4global_p.h" +#include "qv4engine_p.h" +#include "qv4function_p.h" + +#include <QElapsedTimer> + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +namespace Profiling { + +struct FunctionCallProperties { + qint64 start; + qint64 end; + QString name; + QString file; + int line; + int column; +}; + +class FunctionCall { +public: + + FunctionCall() : m_function(0), m_start(0), m_end(0) + { Q_ASSERT_X(false, Q_FUNC_INFO, "Cannot construct a function call without function"); } + + FunctionCall(Function *function, qint64 start, qint64 end) : + m_function(function), m_start(start), m_end(end) + { m_function->compilationUnit->ref(); } + + FunctionCall(const FunctionCall &other) : + m_function(other.m_function), m_start(other.m_start), m_end(other.m_end) + { m_function->compilationUnit->ref(); } + + ~FunctionCall() + { m_function->compilationUnit->deref(); } + + FunctionCall &operator=(const FunctionCall &other) { + if (&other != this) { + if (m_function) + m_function->compilationUnit->deref(); + m_function = other.m_function; + m_start = other.m_start; + m_end = other.m_end; + m_function->compilationUnit->ref(); + } + return *this; + } + + FunctionCallProperties resolve() const; + +private: + + Function *m_function; + qint64 m_start; + qint64 m_end; +}; + +#define Q_V4_PROFILE(engine, ctx, function)\ + ((engine->profiler && engine->profiler->enabled) ?\ + Profiling::FunctionCallProfiler::profileCall(engine->profiler, ctx, function) :\ + function->code(ctx, function->codeData)) + +class Q_QML_EXPORT Profiler : public QObject { + Q_OBJECT + Q_DISABLE_COPY(Profiler) +public: + Profiler(); + + bool enabled; + +public slots: + void stopProfiling(); + void startProfiling(); + void reportData(); + void setTimer(const QElapsedTimer &timer) { m_timer = timer; } + +signals: + void dataReady(const QList<QV4::Profiling::FunctionCallProperties> &); + +private: + QElapsedTimer m_timer; + QVector<FunctionCall> m_data; + + friend class FunctionCallProfiler; +}; + +class FunctionCallProfiler { + Q_DISABLE_COPY(FunctionCallProfiler) +public: + + // It's enough to ref() the function in the destructor as it will probably not disappear while + // it's executing ... + FunctionCallProfiler(Profiler *profiler, Function *function) : + profiler(profiler), function(function), startTime(profiler->m_timer.nsecsElapsed()) + {} + + ~FunctionCallProfiler() + { + profiler->m_data.append(FunctionCall(function, startTime, profiler->m_timer.nsecsElapsed())); + } + + static ReturnedValue profileCall(Profiler *profiler, ExecutionContext *ctx, Function *function) + { + FunctionCallProfiler callProfiler(profiler, function); + return function->code(ctx, function->codeData); + } + + Profiler *profiler; + Function *function; + qint64 startTime; +}; + + +} // namespace Profiling +} // namespace QV4 + +Q_DECLARE_TYPEINFO(QV4::Profiling::FunctionCallProperties, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QV4::Profiling::FunctionCall, Q_MOVABLE_TYPE); + +QT_END_NAMESPACE +Q_DECLARE_METATYPE(QList<QV4::Profiling::FunctionCallProperties>) + +#endif // QV4PROFILING_H |