Permalink
Browse files

added cpu profiler

  • Loading branch information...
1 parent 400fb8a commit 8345ba7b85aff94818695fbc9a8a776d1bed6950 @dannycoates dannycoates committed Dec 1, 2010
Showing with 498 additions and 78 deletions.
  1. +9 −0 graph_node.cc
  2. +1 −0 graph_node.h
  3. +0 −54 heap.cc
  4. +69 −0 profile.cc
  5. +27 −0 profile.h
  6. +126 −0 profile_node.cc
  7. +33 −0 profile_node.h
  8. +109 −0 profiler.cc
  9. +0 −22 test.js
  10. +120 −0 v8-profiler.js
  11. +4 −2 wscript
View
@@ -14,6 +14,7 @@ void GraphNode::Initialize() {
node_template_->SetAccessor(String::New("type"), GraphNode::GetType);
node_template_->SetAccessor(String::New("name"), GraphNode::GetName);
node_template_->SetAccessor(String::New("id"), GraphNode::GetId);
+ node_template_->SetAccessor(String::New("ptr"), GraphNode::GetPtr);
node_template_->SetAccessor(String::New("instancesCount"), GraphNode::GetInstancesCount);
node_template_->SetAccessor(String::New("childrenCount"), GraphNode::GetChildrenCount);
node_template_->SetAccessor(String::New("retainersCount"), GraphNode::GetRetainersCount);
@@ -77,6 +78,14 @@ Handle<Value> GraphNode::GetId(Local<String> property, const AccessorInfo& info)
return scope.Close(Integer::NewFromUnsigned(id));
}
+Handle<Value> GraphNode::GetPtr(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ uint64_t id = reinterpret_cast<uint64_t>(ptr);
+ return scope.Close(Integer::NewFromUnsigned(id));
+}
+
Handle<Value> GraphNode::GetInstancesCount(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
View
@@ -18,6 +18,7 @@ class GraphNode {
static Handle<Value> GetType(Local<String> property, const AccessorInfo& info);
static Handle<Value> GetName(Local<String> property, const AccessorInfo& info);
static Handle<Value> GetId(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetPtr(Local<String> property, const AccessorInfo& info);
static Handle<Value> GetInstancesCount(Local<String> property, const AccessorInfo& info);
static Handle<Value> GetChildrenCount(Local<String> property, const AccessorInfo& info);
static Handle<Value> GetRetainersCount(Local<String> property, const AccessorInfo& info);
View
@@ -1,54 +0,0 @@
-#include <v8.h>
-#include <v8-profiler.h>
-#include <node.h>
-#include "snapshot.h"
-
-using namespace v8;
-using namespace node;
-using namespace nodex;
-
-static Handle<Value> TakeSnapshot(const Arguments& args) {
- HandleScope scope;
- Local<String> title = String::New("");
- if (args.Length() > 0) {
- title = args[0]->ToString();
- }
- const HeapSnapshot* snapshot = HeapProfiler::TakeSnapshot(title);
- return scope.Close(Snapshot::New(snapshot));
-}
-
-static Handle<Value> GetSnapshot(const Arguments& args) {
- HandleScope scope;
- if (args.Length() < 1) {
- return ThrowException(Exception::Error(String::New("No index specified")));
- } else if (!args[0]->IsInt32()) {
- return ThrowException(Exception::Error(String::New("Argument must be integer")));
- }
- int32_t index = args[0]->Int32Value();
- const HeapSnapshot* snapshot = HeapProfiler::GetSnapshot(index);
- return scope.Close(Snapshot::New(snapshot));
-}
-
-static Handle<Value> FindSnapshot(const Arguments& args) {
- HandleScope scope;
- if (args.Length() < 1) {
- return ThrowException(Exception::Error(String::New("No uid specified")));
- }
- uint32_t uid = args[0]->Uint32Value();
- const HeapSnapshot* snapshot = HeapProfiler::FindSnapshot(uid);
- return scope.Close(Snapshot::New(snapshot));
-}
-
-static Handle<Value> GetSnapshotsCount(const Arguments& args) {
- HandleScope scope;
- return scope.Close(Integer::New(HeapProfiler::GetSnapshotsCount()));
-}
-
-extern "C" void init(Handle<Object> target) {
- HandleScope scope;
-
- NODE_SET_METHOD(target, "takeSnapshot", TakeSnapshot);
- NODE_SET_METHOD(target, "getSnapshot", GetSnapshot);
- NODE_SET_METHOD(target, "findSnapshot", FindSnapshot);
- NODE_SET_METHOD(target, "snapshotCount", GetSnapshotsCount);
-}
View
@@ -0,0 +1,69 @@
+#include "profile.h"
+#include "profile_node.h"
+
+using namespace v8;
+
+namespace nodex {
+
+Persistent<ObjectTemplate> Profile::profile_template_;
+
+void Profile::Initialize() {
+ profile_template_ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
+ profile_template_->SetInternalFieldCount(1);
+ profile_template_->SetAccessor(String::New("title"), Profile::GetTitle);
+ profile_template_->SetAccessor(String::New("uid"), Profile::GetUid);
+ profile_template_->SetAccessor(String::New("topRoot"), Profile::GetTopRoot);
+ profile_template_->SetAccessor(String::New("bottomRoot"), Profile::GetBottomRoot);
+}
+
+Handle<Value> Profile::GetUid(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ uint32_t uid = static_cast<CpuProfile*>(ptr)->GetUid();
+ return scope.Close(Integer::NewFromUnsigned(uid));
+}
+
+
+Handle<Value> Profile::GetTitle(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ Handle<String> title = static_cast<CpuProfile*>(ptr)->GetTitle();
+ return scope.Close(title);
+}
+
+Handle<Value> Profile::GetTopRoot(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ const CpuProfileNode* node = static_cast<CpuProfile*>(ptr)->GetTopDownRoot();
+ return scope.Close(ProfileNode::New(node));
+}
+
+
+Handle<Value> Profile::GetBottomRoot(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ const CpuProfileNode* node = static_cast<CpuProfile*>(ptr)->GetBottomUpRoot();
+ return scope.Close(ProfileNode::New(node));
+}
+
+Handle<Value> Profile::New(const CpuProfile* profile) {
+ HandleScope scope;
+
+ if (profile_template_.IsEmpty()) {
+ Profile::Initialize();
+ }
+
+ if(!profile) {
+ return Undefined();
+ }
+ else {
+ Local<Object> obj = profile_template_->NewInstance();
+ obj->SetPointerInInternalField(0, const_cast<CpuProfile*>(profile));
+ return scope.Close(obj);
+ }
+}
+}
View
@@ -0,0 +1,27 @@
+
+
+#ifndef NODE_PROFILE_
+#define NODE_PROFILE_
+
+#include <v8.h>
+#include <v8-profiler.h>
+
+using namespace v8;
+
+namespace nodex {
+
+class Profile {
+ public:
+ static Handle<Value> New(const CpuProfile* profile);
+
+ private:
+ static Handle<Value> GetUid(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetTitle(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetTopRoot(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetBottomRoot(Local<String> property, const AccessorInfo& info);
+ static void Initialize();
+ static Persistent<ObjectTemplate> profile_template_;
+};
+
+}
+#endif // NODE_PROFILE_
View
@@ -0,0 +1,126 @@
+#include "profile_node.h"
+
+using namespace v8;
+
+namespace nodex {
+
+Persistent<ObjectTemplate> ProfileNode::node_template_;
+
+void ProfileNode::Initialize() {
+ node_template_ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
+ node_template_->SetInternalFieldCount(1);
+ node_template_->SetAccessor(String::New("functionName"), ProfileNode::GetFunctionName);
+ node_template_->SetAccessor(String::New("scriptName"), ProfileNode::GetScriptName);
+ node_template_->SetAccessor(String::New("lineNumber"), ProfileNode::GetLineNumber);
+ node_template_->SetAccessor(String::New("totalTime"), ProfileNode::GetTotalTime);
+ node_template_->SetAccessor(String::New("selfTime"), ProfileNode::GetSelfTime);
+ node_template_->SetAccessor(String::New("totalSamplesCount"), ProfileNode::GetTotalSamplesCount);
+ node_template_->SetAccessor(String::New("selfSamplesCount"), ProfileNode::GetSelfSamplesCount);
+ node_template_->SetAccessor(String::New("callUid"), ProfileNode::GetCallUid);
+ node_template_->SetAccessor(String::New("childrenCount"), ProfileNode::GetChildrenCount);
+ node_template_->Set(String::New("getChild"), FunctionTemplate::New(ProfileNode::GetChild));
+}
+
+Handle<Value> ProfileNode::GetFunctionName(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ Handle<String> fname = static_cast<CpuProfileNode*>(ptr)->GetFunctionName();
+ return scope.Close(fname);
+}
+
+Handle<Value> ProfileNode::GetScriptName(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ Handle<String> sname = static_cast<CpuProfileNode*>(ptr)->GetScriptResourceName();
+ return scope.Close(sname);
+}
+
+Handle<Value> ProfileNode::GetLineNumber(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ int32_t ln = static_cast<CpuProfileNode*>(ptr)->GetLineNumber();
+ return scope.Close(Integer::New(ln));
+}
+
+Handle<Value> ProfileNode::GetTotalTime(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ double ttime = static_cast<CpuProfileNode*>(ptr)->GetTotalTime();
+ return scope.Close(Number::New(ttime));
+}
+
+Handle<Value> ProfileNode::GetSelfTime(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ double stime = static_cast<CpuProfileNode*>(ptr)->GetSelfTime();
+ return scope.Close(Number::New(stime));
+}
+
+Handle<Value> ProfileNode::GetTotalSamplesCount(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ double samples = static_cast<CpuProfileNode*>(ptr)->GetTotalSamplesCount();
+ return scope.Close(Number::New(samples));
+}
+
+Handle<Value> ProfileNode::GetSelfSamplesCount(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ double samples = static_cast<CpuProfileNode*>(ptr)->GetSelfSamplesCount();
+ return scope.Close(Number::New(samples));
+}
+
+Handle<Value> ProfileNode::GetCallUid(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ uint32_t uid = static_cast<CpuProfileNode*>(ptr)->GetCallUid();
+ return scope.Close(Integer::NewFromUnsigned(uid));
+}
+
+Handle<Value> ProfileNode::GetChildrenCount(Local<String> property, const AccessorInfo& info) {
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ int32_t count = static_cast<CpuProfileNode*>(ptr)->GetChildrenCount();
+ return scope.Close(Integer::New(count));
+}
+
+Handle<Value> ProfileNode::GetChild(const Arguments& args) {
+ HandleScope scope;
+ if (args.Length() < 1) {
+ return ThrowException(Exception::Error(String::New("No index specified")));
+ } else if (!args[0]->IsInt32()) {
+ return ThrowException(Exception::Error(String::New("Argument must be integer")));
+ }
+ int32_t index = args[0]->Int32Value();
+ Handle<Object> self = args.This();
+ void* ptr = self->GetPointerFromInternalField(0);
+ const CpuProfileNode* node = static_cast<CpuProfileNode*>(ptr)->GetChild(index);
+ return scope.Close(ProfileNode::New(node));
+}
+
+Handle<Value> ProfileNode::New(const CpuProfileNode* node) {
+ HandleScope scope;
+
+ if (node_template_.IsEmpty()) {
+ ProfileNode::Initialize();
+ }
+
+ if(!node) {
+ return Undefined();
+ }
+ else {
+ Local<Object> obj = node_template_->NewInstance();
+ obj->SetPointerInInternalField(0, const_cast<CpuProfileNode*>(node));
+ return scope.Close(obj);
+ }
+}
+}
View
@@ -0,0 +1,33 @@
+
+
+#ifndef NODE_PROFILE_NODE_
+#define NODE_PROFILE_NODE_
+
+#include <v8.h>
+#include <v8-profiler.h>
+
+using namespace v8;
+
+namespace nodex {
+
+class ProfileNode {
+ public:
+ static Handle<Value> New(const CpuProfileNode* node);
+
+ private:
+ static Handle<Value> GetFunctionName(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetScriptName(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetLineNumber(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetTotalTime(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetSelfTime(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetTotalSamplesCount(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetSelfSamplesCount(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetCallUid(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetChildrenCount(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetChild(const Arguments& args);
+ static void Initialize();
+ static Persistent<ObjectTemplate> node_template_;
+};
+
+}
+#endif // NODE_PROFILE_NODE_
Oops, something went wrong.

0 comments on commit 8345ba7

Please sign in to comment.