1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qv8profilerservice_p.h"
43 #include "qdeclarativedebugservice_p_p.h"
44 #include <private/qdeclarativeengine_p.h>
45 #include <private/qv8profiler_p.h>
47 #include <QtCore/QHash>
51 Q_GLOBAL_STATIC(QV8ProfilerService, v8ProfilerInstance)
53 class ByteArrayOutputStream : public v8::OutputStream
57 ByteArrayOutputStream(QByteArray *buffer)
61 WriteResult WriteAsciiChunk(char *data, int size)
63 QByteArray b(data, size);
69 // convert to a QByteArray that can be sent to the debug client
70 QByteArray QV8ProfilerData::toByteArray() const
73 //### using QDataStream is relatively expensive
74 QDataStream ds(&data, QIODevice::WriteOnly);
75 ds << messageType << filename << functionname << lineNumber << totalTime << selfTime << treeLevel;
80 class QV8ProfilerServicePrivate : public QDeclarativeDebugServicePrivate
82 Q_DECLARE_PUBLIC(QV8ProfilerService)
85 QV8ProfilerServicePrivate()
91 void takeSnapshot(v8::HeapSnapshot::Type);
93 void printProfileTree(const v8::CpuProfileNode *node, int level = 0);
96 QList<QV8ProfilerData> m_data;
99 QList<QDeclarativeEngine *> engines;
100 v8::Isolate *isolate;
103 QV8ProfilerService::QV8ProfilerService(QObject *parent)
104 : QDeclarativeDebugService(*(new QV8ProfilerServicePrivate()), QLatin1String("V8Profiler"), parent)
106 Q_D(QV8ProfilerService);
108 if (registerService() == Enabled) {
109 // ,block mode, client attached
110 while (!d->initialized)
115 QV8ProfilerService::~QV8ProfilerService()
119 QV8ProfilerService *QV8ProfilerService::instance()
121 return v8ProfilerInstance();
124 void QV8ProfilerService::addEngine(QDeclarativeEngine *engine)
126 Q_D(QV8ProfilerService);
128 Q_ASSERT(!d->engines.contains(engine));
130 d->engines.append(engine);
133 void QV8ProfilerService::removeEngine(QDeclarativeEngine *engine)
135 Q_D(QV8ProfilerService);
137 Q_ASSERT(d->engines.contains(engine));
139 d->engines.removeOne(engine);
142 void QV8ProfilerService::messageReceived(const QByteArray &message)
144 Q_D(QV8ProfilerService);
146 QDataStream ds(message);
150 ds >> command >> option;
153 d->isolate = v8::Isolate::New();
154 v8::Isolate::Scope scope(d->isolate);
155 v8::V8::Initialize();
158 v8::Isolate::Scope scope(d->isolate);
160 if (command == "V8PROFILER") {
162 if (option == "start") {
163 startProfiling(QString::fromUtf8(title));
164 } else if (option == "stop") {
165 stopProfiling(QString::fromUtf8(title));
168 d->initialized = true;
171 if (command == "V8SNAPSHOT") {
172 if (option == "full")
173 d->takeSnapshot(v8::HeapSnapshot::kFull);
174 else if (option == "delete") {
175 v8::HeapProfiler::DeleteAllSnapshots();
179 QDeclarativeDebugService::messageReceived(message);
182 void QV8ProfilerService::startProfiling(const QString &title)
185 v8::HandleScope handle_scope;
186 v8::Handle<v8::String> v8title = v8::String::New(reinterpret_cast<const uint16_t*>(title.data()), title.size());
187 v8::CpuProfiler::StartProfiling(v8title);
190 void QV8ProfilerService::stopProfiling(const QString &title)
192 Q_D(QV8ProfilerService);
194 v8::HandleScope handle_scope;
195 v8::Handle<v8::String> v8title = v8::String::New(reinterpret_cast<const uint16_t*>(title.data()), title.size());
196 const v8::CpuProfile *cpuProfile = v8::CpuProfiler::StopProfiling(v8title);
198 // can happen at start
199 const v8::CpuProfileNode *rootNode = cpuProfile->GetTopDownRoot();
200 d->printProfileTree(rootNode);
204 void QV8ProfilerService::sendProfilingData()
206 Q_D(QV8ProfilerService);
207 // Send messages to client
211 void QV8ProfilerServicePrivate::printProfileTree(const v8::CpuProfileNode *node, int level)
213 for (int index = 0 ; index < node->GetChildrenCount() ; index++) {
214 const v8::CpuProfileNode* childNode = node->GetChild(index);
215 if (QV8Engine::toStringStatic(childNode->GetScriptResourceName()).length() > 0) {
217 QV8ProfilerData rd = {(int)QV8ProfilerService::V8Entry, QV8Engine::toStringStatic(childNode->GetScriptResourceName()),
218 QV8Engine::toStringStatic(childNode->GetFunctionName()),
219 childNode->GetLineNumber(), childNode->GetTotalTime(), childNode->GetSelfTime(), level};
222 // different nodes might have common children: fix at client side
223 if (childNode->GetChildrenCount() > 0) {
224 printProfileTree(childNode, level+1);
230 void QV8ProfilerServicePrivate::takeSnapshot(v8::HeapSnapshot::Type snapshotType)
232 Q_Q(QV8ProfilerService);
234 v8::HandleScope scope;
235 v8::Local<v8::String> title = v8::String::New("");
237 QByteArray jsonSnapshot;
238 ByteArrayOutputStream bos(&jsonSnapshot);
239 const v8::HeapSnapshot *snapshot = v8::HeapProfiler::TakeSnapshot(title, snapshotType);
240 snapshot->Serialize(&bos, v8::HeapSnapshot::kJSON);
243 QDataStream ds(&data, QIODevice::WriteOnly);
244 ds << (int)QV8ProfilerService::V8Snapshot << jsonSnapshot;
246 q->sendMessage(data);
249 void QV8ProfilerServicePrivate::sendMessages()
251 Q_Q(QV8ProfilerService);
253 for (int i = 0; i < m_data.count(); ++i)
254 q->sendMessage(m_data.at(i).toByteArray());
257 //indicate completion
259 QDataStream ds(&data, QIODevice::WriteOnly);
260 ds << (int)QV8ProfilerService::V8Complete;
262 q->sendMessage(data);