/****************************************************************************
**
-** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Contact: http://www.qt-project.org/
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
Q_GLOBAL_STATIC(QV8ProfilerService, v8ProfilerInstance)
-class ByteArrayOutputStream : public v8::OutputStream
+class DebugServiceOutputStream : public v8::OutputStream
{
- QByteArray *_buffer;
+ QDeclarativeDebugService &_service;
public:
- ByteArrayOutputStream(QByteArray *buffer)
+ DebugServiceOutputStream(QDeclarativeDebugService &service)
: v8::OutputStream(),
- _buffer(buffer) {}
+ _service(service) {}
void EndOfStream() {}
- WriteResult WriteAsciiChunk(char *data, int size)
+ WriteResult WriteAsciiChunk(char *rawData, int size)
{
- QByteArray b(data, size);
- _buffer->append(b);
+ QByteArray data;
+ QDataStream ds(&data, QIODevice::WriteOnly);
+ ds << QV8ProfilerService::V8SnapshotChunk << QByteArray(rawData, size);
+ _service.sendMessage(data);
return kContinue;
}
};
public:
QV8ProfilerServicePrivate()
:initialized(false)
- , isolate(0)
{
}
QList<QV8ProfilerData> m_data;
bool initialized;
- v8::Isolate *isolate;
};
QV8ProfilerService::QV8ProfilerService(QObject *parent)
- : QDeclarativeDebugService(*(new QV8ProfilerServicePrivate()), QLatin1String("V8Profiler"), parent)
+ : QDeclarativeDebugService(*(new QV8ProfilerServicePrivate()), QLatin1String("V8Profiler"), 1, parent)
{
Q_D(QV8ProfilerService);
QByteArray title;
ds >> command >> option;
- if (!d->isolate) {
- d->isolate = v8::Isolate::New();
- v8::Isolate::Scope scope(d->isolate);
- v8::V8::Initialize();
- }
-
- v8::Isolate::Scope scope(d->isolate);
-
if (command == "V8PROFILER") {
ds >> title;
if (option == "start") {
- startProfiling(QString::fromUtf8(title));
+ QMetaObject::invokeMethod(this, "startProfiling", Qt::QueuedConnection, Q_ARG(QString, QString::fromUtf8(title)));
} else if (option == "stop") {
- stopProfiling(QString::fromUtf8(title));
- sendProfilingData();
+ QMetaObject::invokeMethod(this, "stopProfiling", Qt::QueuedConnection, Q_ARG(QString, QString::fromUtf8(title)));
+ QMetaObject::invokeMethod(this, "sendProfilingData", Qt::QueuedConnection);
}
d->initialized = true;
}
if (command == "V8SNAPSHOT") {
if (option == "full")
- d->takeSnapshot(v8::HeapSnapshot::kFull);
+ QMetaObject::invokeMethod(this, "takeSnapshot", Qt::QueuedConnection);
else if (option == "delete") {
- v8::HeapProfiler::DeleteAllSnapshots();
+ QMetaObject::invokeMethod(this, "deleteSnapshots", Qt::QueuedConnection);
}
}
}
}
+void QV8ProfilerService::takeSnapshot()
+{
+ Q_D(QV8ProfilerService);
+ d->takeSnapshot(v8::HeapSnapshot::kFull);
+}
+
+void QV8ProfilerService::deleteSnapshots()
+{
+ v8::HeapProfiler::DeleteAllSnapshots();
+}
+
void QV8ProfilerService::sendProfilingData()
{
Q_D(QV8ProfilerService);
v8::HandleScope scope;
v8::Local<v8::String> title = v8::String::New("");
- QByteArray jsonSnapshot;
- ByteArrayOutputStream bos(&jsonSnapshot);
+ DebugServiceOutputStream outputStream(*q);
const v8::HeapSnapshot *snapshot = v8::HeapProfiler::TakeSnapshot(title, snapshotType);
- snapshot->Serialize(&bos, v8::HeapSnapshot::kJSON);
+ snapshot->Serialize(&outputStream, v8::HeapSnapshot::kJSON);
+ //indicate completion
QByteArray data;
QDataStream ds(&data, QIODevice::WriteOnly);
- ds << (int)QV8ProfilerService::V8Snapshot << jsonSnapshot;
+ ds << (int)QV8ProfilerService::V8SnapshotComplete;
q->sendMessage(data);
}
{
Q_Q(QV8ProfilerService);
+ QList<QByteArray> messages;
for (int i = 0; i < m_data.count(); ++i)
- q->sendMessage(m_data.at(i).toByteArray());
+ messages << m_data.at(i).toByteArray();
+ q->sendMessages(messages);
m_data.clear();
//indicate completion