Skip to content
Browse files

Added heap.getHeapObjectById

  • Loading branch information...
1 parent 843e567 commit 2e731baebfe6ffe992421c05da747b3755a18ca5 @3y3 3y3 committed Jul 1, 2014
Showing with 85 additions and 4 deletions.
  1. +13 −0 src/heap_graph_node.cc
  2. +1 −0 src/heap_graph_node.h
  3. +49 −0 src/heap_profiler.cc
  4. +1 −0 src/heap_profiler.h
  5. +18 −4 src/heap_snapshot.cc
  6. +1 −0 src/heap_snapshot.h
  7. +2 −0 v8-profiler.js
View
13 src/heap_graph_node.cc
@@ -23,10 +23,23 @@ namespace nodex {
Local<ObjectTemplate> o = NanNew<ObjectTemplate>();
Local<Object> _cache = NanNew<Object>();
o->SetInternalFieldCount(1);
+#if (NODE_MODULE_VERSION <= 0x000B)
+ NODE_SET_METHOD(o, "getHeapValue", GraphNode::GetHeapValue);
+#endif
o->SetAccessor(NanNew<String>("children"), GraphNode::GetChildren);
NanAssignPersistent(graph_node_template_, o);
NanAssignPersistent(graph_node_cache, _cache);
}
+
+#if (NODE_MODULE_VERSION <= 0x000B)
+ NAN_METHOD(GraphNode::GetHeapValue) {
+ NanScope();
+
+ void* ptr = NanGetInternalFieldPointer(args.This(), 0);
+ HeapGraphNode* node = static_cast<HeapGraphNode*>(ptr);
+ NanReturnValue(node->GetHeapValue());
+ }
+#endif
NAN_GETTER(GraphNode::GetChildren) {
NanScope();
View
1 src/heap_graph_node.h
@@ -11,6 +11,7 @@ namespace nodex {
static v8::Handle<v8::Value> New(const v8::HeapGraphNode* node);
private:
static void Initialize();
+ static NAN_METHOD(GetHeapValue);
static NAN_GETTER(GetChildren);
static v8::Persistent<v8::ObjectTemplate> graph_node_template_;
static v8::Persistent<v8::Object> graph_node_cache;
View
49 src/heap_profiler.cc
@@ -58,6 +58,7 @@ namespace nodex {
NODE_SET_METHOD(heapProfiler, "startTrackingHeapObjects", HeapProfiler::StartTrackingHeapObjects);
NODE_SET_METHOD(heapProfiler, "stopTrackingHeapObjects", HeapProfiler::StopTrackingHeapObjects);
NODE_SET_METHOD(heapProfiler, "getHeapStats", HeapProfiler::GetHeapStats);
+ NODE_SET_METHOD(heapProfiler, "getObjectByHeapObjectId", HeapProfiler::GetObjectByHeapObjectId);
heapProfiler->Set(NanNew<String>("snapshots"), snapshots);
NanAssignPersistent(Snapshot::snapshots, snapshots);
@@ -113,6 +114,54 @@ namespace nodex {
NanReturnUndefined();
}
+ NAN_METHOD(HeapProfiler::GetObjectByHeapObjectId) {
+ NanScope();
+
+ if (args.Length() < 1) {
+ return NanThrowError("Invalid number of arguments");
+ } else if (!args[0]->IsNumber()) {
+ return NanThrowTypeError("Arguments must be a number");
+ }
+
+ SnapshotObjectId id = args[0]->Uint32Value();
+ Local<Value> object;
+#if (NODE_MODULE_VERSION > 0x000B)
+ object = v8::Isolate::GetCurrent()->GetHeapProfiler()->FindObjectById(id);
+#else
+ Local<Array> snapshots = Local<Array>::Cast(args.This()->Get(NanNew<String>("snapshots")));
+ Local<Object> snapshot;
+ uint32_t length = snapshots->Length();
+
+ if (length == 0) NanReturnUndefined();
+
+ for (uint32_t i = 0; i < length; ++i) {
+ snapshot = snapshots->Get(i)->ToObject();
+ Local<Value> argv[1] = { args[0] };
+ if (snapshot->Get(NanNew<String>("maxSnapshotJSObjectId"))->Uint32Value() >= id) {
+ Local<Object> graph_node = Function::Cast(*snapshot->Get(NanNew<String>("getNodeById")))
+ ->Call(snapshot, 1, argv)->ToObject();
+ object = Function::Cast(*graph_node->Get(NanNew<String>("getHeapValue")))
+ ->Call(graph_node, 0, NULL);
+ break;
+ }
+ }
+#endif
+
+ if (object.IsEmpty()) {
+ NanReturnUndefined();
+ } else if (object->IsObject()
+ || object->IsNumber()
+ || object->IsString()
+#if (NODE_MODULE_VERSION > 0x000B)
+ || object->IsSymbol()
+#endif
+ || object->IsBoolean()) {
+ NanReturnValue(object);
+ } else {
+ NanReturnValue(NanNew<String>("Preview is not available"));
+ }
+ }
+
NAN_METHOD(HeapProfiler::StopTrackingHeapObjects) {
NanScope();
View
1 src/heap_profiler.h
@@ -18,6 +18,7 @@ namespace nodex {
static NAN_METHOD(StartTrackingHeapObjects);
static NAN_METHOD(StopTrackingHeapObjects);
static NAN_METHOD(GetHeapStats);
+ static NAN_METHOD(GetObjectByHeapObjectId);
};
} //namespace nodex
View
22 src/heap_snapshot.cc
@@ -13,6 +13,7 @@ namespace nodex {
using v8::Object;
using v8::ObjectTemplate;
using v8::Persistent;
+ using v8::SnapshotObjectId;
using v8::String;
using v8::Function;
using v8::Value;
@@ -28,6 +29,7 @@ namespace nodex {
o->SetInternalFieldCount(1);
o->SetAccessor(NanNew<String>("root"), Snapshot::GetRoot);
NODE_SET_METHOD(o, "getNode", Snapshot::GetNode);
+ NODE_SET_METHOD(o, "getNodeById", Snapshot::GetNodeById);
NODE_SET_METHOD(o, "delete", Snapshot::Delete);
NODE_SET_METHOD(o, "serialize", Snapshot::Serialize);
NanAssignPersistent(snapshot_template_, o);
@@ -51,18 +53,30 @@ namespace nodex {
NanScope();
if (!args.Length()) {
- NanThrowError("No index specified");
- NanReturnUndefined();
+ return NanThrowError("No index specified");
} else if (!args[0]->IsInt32()) {
- NanThrowTypeError("Argument must be an integer");
- NanReturnUndefined();
+ return NanThrowTypeError("Argument must be an integer");
}
int32_t index = args[0]->Int32Value();
void* ptr = NanGetInternalFieldPointer(args.This(), 0);
NanReturnValue(GraphNode::New(static_cast<HeapSnapshot*>(ptr)->GetNode(index)));
}
+ NAN_METHOD(Snapshot::GetNodeById) {
+ NanScope();
+
+ if (!args.Length()) {
+ return NanThrowError("No id specified");
+ } else if (!args[0]->IsInt32()) {
+ return NanThrowTypeError("Argument must be an integer");
+ }
+
+ SnapshotObjectId id = args[0]->Int32Value();
+ void* ptr = NanGetInternalFieldPointer(args.This(), 0);
+ NanReturnValue(GraphNode::New(static_cast<HeapSnapshot*>(ptr)->GetNodeById(id)));
+ }
+
NAN_METHOD(Snapshot::Serialize) {
NanScope();
View
1 src/heap_snapshot.h
@@ -14,6 +14,7 @@ namespace nodex {
static void Initialize();
static NAN_GETTER(GetRoot);
static NAN_METHOD(GetNode);
+ static NAN_METHOD(GetNodeById);
static NAN_METHOD(Delete);
static NAN_METHOD(Serialize);
static v8::Persistent<v8::ObjectTemplate> snapshot_template_;
View
2 v8-profiler.js
@@ -105,6 +105,8 @@ var profiler = {
getHeapStats: binding.heap.getHeapStats,
+ getObjectByHeapObjectId: binding.heap.getObjectByHeapObjectId,
+
/*CPU PROFILER API*/
get profiles() { return binding.cpu.profiles; },

0 comments on commit 2e731ba

Please sign in to comment.
Something went wrong with that request. Please try again.