Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added cpu profiler

  • Loading branch information...
commit 8345ba7b85aff94818695fbc9a8a776d1bed6950 1 parent 400fb8a
Danny Coates authored
9  graph_node.cc
@@ -14,6 +14,7 @@ void GraphNode::Initialize() {
14 14
   node_template_->SetAccessor(String::New("type"), GraphNode::GetType);
15 15
   node_template_->SetAccessor(String::New("name"), GraphNode::GetName);
16 16
   node_template_->SetAccessor(String::New("id"), GraphNode::GetId);
  17
+  node_template_->SetAccessor(String::New("ptr"), GraphNode::GetPtr);
17 18
   node_template_->SetAccessor(String::New("instancesCount"), GraphNode::GetInstancesCount);
18 19
   node_template_->SetAccessor(String::New("childrenCount"), GraphNode::GetChildrenCount);
19 20
   node_template_->SetAccessor(String::New("retainersCount"), GraphNode::GetRetainersCount);
@@ -77,6 +78,14 @@ Handle<Value> GraphNode::GetId(Local<String> property, const AccessorInfo& info)
77 78
   return scope.Close(Integer::NewFromUnsigned(id));
78 79
 }
79 80
 
  81
+Handle<Value> GraphNode::GetPtr(Local<String> property, const AccessorInfo& info) {
  82
+  HandleScope scope;
  83
+  Local<Object> self = info.Holder();
  84
+  void* ptr = self->GetPointerFromInternalField(0);
  85
+  uint64_t id = reinterpret_cast<uint64_t>(ptr);
  86
+  return scope.Close(Integer::NewFromUnsigned(id));
  87
+}
  88
+
80 89
 Handle<Value> GraphNode::GetInstancesCount(Local<String> property, const AccessorInfo& info) {
81 90
   HandleScope scope;
82 91
   Local<Object> self = info.Holder();
1  graph_node.h
@@ -18,6 +18,7 @@ class GraphNode {
18 18
    static Handle<Value> GetType(Local<String> property, const AccessorInfo& info);
19 19
    static Handle<Value> GetName(Local<String> property, const AccessorInfo& info);
20 20
    static Handle<Value> GetId(Local<String> property, const AccessorInfo& info);
  21
+   static Handle<Value> GetPtr(Local<String> property, const AccessorInfo& info);
21 22
    static Handle<Value> GetInstancesCount(Local<String> property, const AccessorInfo& info);
22 23
    static Handle<Value> GetChildrenCount(Local<String> property, const AccessorInfo& info);
23 24
    static Handle<Value> GetRetainersCount(Local<String> property, const AccessorInfo& info);
54  heap.cc
... ...
@@ -1,54 +0,0 @@
1  
-#include <v8.h>
2  
-#include <v8-profiler.h>
3  
-#include <node.h>
4  
-#include "snapshot.h"
5  
-
6  
-using namespace v8;
7  
-using namespace node;
8  
-using namespace nodex;
9  
-
10  
-static Handle<Value> TakeSnapshot(const Arguments& args) {
11  
-  HandleScope scope;
12  
-  Local<String> title = String::New("");
13  
-  if (args.Length() > 0) {
14  
-    title = args[0]->ToString();
15  
-  }
16  
-  const HeapSnapshot* snapshot = HeapProfiler::TakeSnapshot(title);
17  
-  return scope.Close(Snapshot::New(snapshot));
18  
-}
19  
-
20  
-static Handle<Value> GetSnapshot(const Arguments& args) {
21  
-  HandleScope scope;
22  
-  if (args.Length() < 1) {
23  
-    return ThrowException(Exception::Error(String::New("No index specified")));
24  
-  } else if (!args[0]->IsInt32()) {
25  
-    return ThrowException(Exception::Error(String::New("Argument must be integer")));
26  
-  }
27  
-  int32_t index = args[0]->Int32Value();
28  
-  const HeapSnapshot* snapshot = HeapProfiler::GetSnapshot(index);
29  
-  return scope.Close(Snapshot::New(snapshot));
30  
-}
31  
-
32  
-static Handle<Value> FindSnapshot(const Arguments& args) {
33  
-  HandleScope scope;
34  
-  if (args.Length() < 1) {
35  
-    return ThrowException(Exception::Error(String::New("No uid specified")));
36  
-  }
37  
-  uint32_t uid = args[0]->Uint32Value();
38  
-  const HeapSnapshot* snapshot = HeapProfiler::FindSnapshot(uid);
39  
-  return scope.Close(Snapshot::New(snapshot));
40  
-}
41  
-
42  
-static Handle<Value> GetSnapshotsCount(const Arguments& args) {
43  
-  HandleScope scope;
44  
-  return scope.Close(Integer::New(HeapProfiler::GetSnapshotsCount()));
45  
-}
46  
-
47  
-extern "C" void init(Handle<Object> target) {
48  
-  HandleScope scope;
49  
-
50  
-  NODE_SET_METHOD(target, "takeSnapshot", TakeSnapshot);
51  
-  NODE_SET_METHOD(target, "getSnapshot", GetSnapshot);
52  
-  NODE_SET_METHOD(target, "findSnapshot", FindSnapshot);
53  
-  NODE_SET_METHOD(target, "snapshotCount", GetSnapshotsCount);
54  
-}
69  profile.cc
... ...
@@ -0,0 +1,69 @@
  1
+#include "profile.h"
  2
+#include "profile_node.h"
  3
+
  4
+using namespace v8;
  5
+
  6
+namespace nodex {
  7
+
  8
+Persistent<ObjectTemplate> Profile::profile_template_;
  9
+
  10
+void Profile::Initialize() {
  11
+  profile_template_ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
  12
+  profile_template_->SetInternalFieldCount(1);
  13
+  profile_template_->SetAccessor(String::New("title"), Profile::GetTitle);
  14
+  profile_template_->SetAccessor(String::New("uid"), Profile::GetUid);
  15
+  profile_template_->SetAccessor(String::New("topRoot"), Profile::GetTopRoot);
  16
+  profile_template_->SetAccessor(String::New("bottomRoot"), Profile::GetBottomRoot);
  17
+}
  18
+
  19
+Handle<Value> Profile::GetUid(Local<String> property, const AccessorInfo& info) {
  20
+  HandleScope scope;
  21
+  Local<Object> self = info.Holder();
  22
+  void* ptr = self->GetPointerFromInternalField(0);
  23
+  uint32_t uid = static_cast<CpuProfile*>(ptr)->GetUid();
  24
+  return scope.Close(Integer::NewFromUnsigned(uid));
  25
+}
  26
+
  27
+
  28
+Handle<Value> Profile::GetTitle(Local<String> property, const AccessorInfo& info) {
  29
+  HandleScope scope;
  30
+  Local<Object> self = info.Holder();
  31
+  void* ptr = self->GetPointerFromInternalField(0);
  32
+  Handle<String> title = static_cast<CpuProfile*>(ptr)->GetTitle();
  33
+  return scope.Close(title);
  34
+}
  35
+
  36
+Handle<Value> Profile::GetTopRoot(Local<String> property, const AccessorInfo& info) {
  37
+  HandleScope scope;
  38
+  Local<Object> self = info.Holder();
  39
+  void* ptr = self->GetPointerFromInternalField(0);
  40
+  const CpuProfileNode* node = static_cast<CpuProfile*>(ptr)->GetTopDownRoot();
  41
+  return scope.Close(ProfileNode::New(node));
  42
+}
  43
+
  44
+
  45
+Handle<Value> Profile::GetBottomRoot(Local<String> property, const AccessorInfo& info) {
  46
+  HandleScope scope;
  47
+  Local<Object> self = info.Holder();
  48
+  void* ptr = self->GetPointerFromInternalField(0);
  49
+  const CpuProfileNode* node = static_cast<CpuProfile*>(ptr)->GetBottomUpRoot();
  50
+  return scope.Close(ProfileNode::New(node));
  51
+}
  52
+
  53
+Handle<Value> Profile::New(const CpuProfile* profile) {
  54
+  HandleScope scope;
  55
+  
  56
+  if (profile_template_.IsEmpty()) {
  57
+    Profile::Initialize();
  58
+  }
  59
+  
  60
+  if(!profile) {
  61
+    return Undefined();
  62
+  }
  63
+  else {
  64
+    Local<Object> obj = profile_template_->NewInstance();
  65
+    obj->SetPointerInInternalField(0, const_cast<CpuProfile*>(profile));
  66
+    return scope.Close(obj);
  67
+  }
  68
+}
  69
+}
27  profile.h
... ...
@@ -0,0 +1,27 @@
  1
+
  2
+
  3
+#ifndef NODE_PROFILE_
  4
+#define NODE_PROFILE_
  5
+
  6
+#include <v8.h>
  7
+#include <v8-profiler.h>
  8
+
  9
+using namespace v8;
  10
+
  11
+namespace nodex {
  12
+
  13
+class Profile {
  14
+ public:
  15
+  static Handle<Value> New(const CpuProfile* profile);
  16
+ 
  17
+ private:
  18
+  static Handle<Value> GetUid(Local<String> property, const AccessorInfo& info);
  19
+  static Handle<Value> GetTitle(Local<String> property, const AccessorInfo& info);
  20
+  static Handle<Value> GetTopRoot(Local<String> property, const AccessorInfo& info);
  21
+  static Handle<Value> GetBottomRoot(Local<String> property, const AccessorInfo& info);
  22
+  static void Initialize();
  23
+  static Persistent<ObjectTemplate> profile_template_;
  24
+};
  25
+
  26
+}
  27
+#endif  // NODE_PROFILE_
126  profile_node.cc
... ...
@@ -0,0 +1,126 @@
  1
+#include "profile_node.h"
  2
+
  3
+using namespace v8;
  4
+
  5
+namespace nodex {
  6
+
  7
+Persistent<ObjectTemplate> ProfileNode::node_template_;
  8
+
  9
+void ProfileNode::Initialize() {
  10
+  node_template_ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
  11
+  node_template_->SetInternalFieldCount(1);
  12
+  node_template_->SetAccessor(String::New("functionName"), ProfileNode::GetFunctionName);
  13
+  node_template_->SetAccessor(String::New("scriptName"), ProfileNode::GetScriptName);
  14
+  node_template_->SetAccessor(String::New("lineNumber"), ProfileNode::GetLineNumber);
  15
+  node_template_->SetAccessor(String::New("totalTime"), ProfileNode::GetTotalTime);
  16
+  node_template_->SetAccessor(String::New("selfTime"), ProfileNode::GetSelfTime);
  17
+  node_template_->SetAccessor(String::New("totalSamplesCount"), ProfileNode::GetTotalSamplesCount);
  18
+  node_template_->SetAccessor(String::New("selfSamplesCount"), ProfileNode::GetSelfSamplesCount);
  19
+  node_template_->SetAccessor(String::New("callUid"), ProfileNode::GetCallUid);
  20
+  node_template_->SetAccessor(String::New("childrenCount"), ProfileNode::GetChildrenCount);
  21
+  node_template_->Set(String::New("getChild"), FunctionTemplate::New(ProfileNode::GetChild));
  22
+}
  23
+
  24
+Handle<Value> ProfileNode::GetFunctionName(Local<String> property, const AccessorInfo& info) {
  25
+  HandleScope scope;
  26
+  Local<Object> self = info.Holder();
  27
+  void* ptr = self->GetPointerFromInternalField(0);
  28
+  Handle<String> fname = static_cast<CpuProfileNode*>(ptr)->GetFunctionName();
  29
+  return scope.Close(fname);
  30
+}
  31
+
  32
+Handle<Value> ProfileNode::GetScriptName(Local<String> property, const AccessorInfo& info) {
  33
+  HandleScope scope;
  34
+  Local<Object> self = info.Holder();
  35
+  void* ptr = self->GetPointerFromInternalField(0);
  36
+  Handle<String> sname = static_cast<CpuProfileNode*>(ptr)->GetScriptResourceName();
  37
+  return scope.Close(sname);
  38
+}
  39
+
  40
+Handle<Value> ProfileNode::GetLineNumber(Local<String> property, const AccessorInfo& info) {
  41
+  HandleScope scope;
  42
+  Local<Object> self = info.Holder();
  43
+  void* ptr = self->GetPointerFromInternalField(0);
  44
+  int32_t ln = static_cast<CpuProfileNode*>(ptr)->GetLineNumber();
  45
+  return scope.Close(Integer::New(ln));
  46
+}
  47
+
  48
+Handle<Value> ProfileNode::GetTotalTime(Local<String> property, const AccessorInfo& info) {
  49
+  HandleScope scope;
  50
+  Local<Object> self = info.Holder();
  51
+  void* ptr = self->GetPointerFromInternalField(0);
  52
+  double ttime = static_cast<CpuProfileNode*>(ptr)->GetTotalTime();
  53
+  return scope.Close(Number::New(ttime));
  54
+}
  55
+
  56
+Handle<Value> ProfileNode::GetSelfTime(Local<String> property, const AccessorInfo& info) {
  57
+  HandleScope scope;
  58
+  Local<Object> self = info.Holder();
  59
+  void* ptr = self->GetPointerFromInternalField(0);
  60
+  double stime = static_cast<CpuProfileNode*>(ptr)->GetSelfTime();
  61
+  return scope.Close(Number::New(stime));
  62
+}
  63
+
  64
+Handle<Value> ProfileNode::GetTotalSamplesCount(Local<String> property, const AccessorInfo& info) {
  65
+  HandleScope scope;
  66
+  Local<Object> self = info.Holder();
  67
+  void* ptr = self->GetPointerFromInternalField(0);
  68
+  double samples = static_cast<CpuProfileNode*>(ptr)->GetTotalSamplesCount();
  69
+  return scope.Close(Number::New(samples));
  70
+}
  71
+
  72
+Handle<Value> ProfileNode::GetSelfSamplesCount(Local<String> property, const AccessorInfo& info) {
  73
+  HandleScope scope;
  74
+  Local<Object> self = info.Holder();
  75
+  void* ptr = self->GetPointerFromInternalField(0);
  76
+  double samples = static_cast<CpuProfileNode*>(ptr)->GetSelfSamplesCount();
  77
+  return scope.Close(Number::New(samples));
  78
+}
  79
+
  80
+Handle<Value> ProfileNode::GetCallUid(Local<String> property, const AccessorInfo& info) {
  81
+  HandleScope scope;
  82
+  Local<Object> self = info.Holder();
  83
+  void* ptr = self->GetPointerFromInternalField(0);
  84
+  uint32_t uid = static_cast<CpuProfileNode*>(ptr)->GetCallUid();
  85
+  return scope.Close(Integer::NewFromUnsigned(uid));
  86
+}
  87
+
  88
+Handle<Value> ProfileNode::GetChildrenCount(Local<String> property, const AccessorInfo& info) {
  89
+  HandleScope scope;
  90
+  Local<Object> self = info.Holder();
  91
+  void* ptr = self->GetPointerFromInternalField(0);
  92
+  int32_t count = static_cast<CpuProfileNode*>(ptr)->GetChildrenCount();
  93
+  return scope.Close(Integer::New(count));
  94
+}
  95
+
  96
+Handle<Value> ProfileNode::GetChild(const Arguments& args) {
  97
+  HandleScope scope;
  98
+  if (args.Length() < 1) {
  99
+    return ThrowException(Exception::Error(String::New("No index specified")));
  100
+  } else if (!args[0]->IsInt32()) {
  101
+    return ThrowException(Exception::Error(String::New("Argument must be integer")));
  102
+  }
  103
+  int32_t index = args[0]->Int32Value();
  104
+  Handle<Object> self = args.This();
  105
+  void* ptr = self->GetPointerFromInternalField(0);
  106
+  const CpuProfileNode* node = static_cast<CpuProfileNode*>(ptr)->GetChild(index);
  107
+  return scope.Close(ProfileNode::New(node));
  108
+}
  109
+
  110
+Handle<Value> ProfileNode::New(const CpuProfileNode* node) {
  111
+  HandleScope scope;
  112
+  
  113
+  if (node_template_.IsEmpty()) {
  114
+    ProfileNode::Initialize();
  115
+  }
  116
+  
  117
+  if(!node) {
  118
+    return Undefined();
  119
+  }
  120
+  else {
  121
+    Local<Object> obj = node_template_->NewInstance();
  122
+    obj->SetPointerInInternalField(0, const_cast<CpuProfileNode*>(node));
  123
+    return scope.Close(obj);
  124
+  }
  125
+}
  126
+}
33  profile_node.h
... ...
@@ -0,0 +1,33 @@
  1
+
  2
+
  3
+#ifndef NODE_PROFILE_NODE_
  4
+#define NODE_PROFILE_NODE_
  5
+
  6
+#include <v8.h>
  7
+#include <v8-profiler.h>
  8
+
  9
+using namespace v8;
  10
+
  11
+namespace nodex {
  12
+
  13
+class ProfileNode {
  14
+ public:
  15
+   static Handle<Value> New(const CpuProfileNode* node);
  16
+
  17
+ private:
  18
+   static Handle<Value> GetFunctionName(Local<String> property, const AccessorInfo& info);
  19
+   static Handle<Value> GetScriptName(Local<String> property, const AccessorInfo& info);
  20
+   static Handle<Value> GetLineNumber(Local<String> property, const AccessorInfo& info);
  21
+   static Handle<Value> GetTotalTime(Local<String> property, const AccessorInfo& info);
  22
+   static Handle<Value> GetSelfTime(Local<String> property, const AccessorInfo& info);
  23
+   static Handle<Value> GetTotalSamplesCount(Local<String> property, const AccessorInfo& info);
  24
+   static Handle<Value> GetSelfSamplesCount(Local<String> property, const AccessorInfo& info);
  25
+   static Handle<Value> GetCallUid(Local<String> property, const AccessorInfo& info);
  26
+   static Handle<Value> GetChildrenCount(Local<String> property, const AccessorInfo& info);
  27
+   static Handle<Value> GetChild(const Arguments& args);
  28
+   static void Initialize();
  29
+   static Persistent<ObjectTemplate> node_template_;
  30
+};
  31
+
  32
+}
  33
+#endif  // NODE_PROFILE_NODE_
109  profiler.cc
... ...
@@ -0,0 +1,109 @@
  1
+#include <v8.h>
  2
+#include <v8-profiler.h>
  3
+#include <node.h>
  4
+#include "snapshot.h"
  5
+#include "profile.h"
  6
+
  7
+using namespace v8;
  8
+using namespace node;
  9
+using namespace nodex;
  10
+
  11
+static Handle<Value> TakeSnapshot(const Arguments& args) {
  12
+  HandleScope scope;
  13
+  Local<String> title = String::New("");
  14
+  int32_t len = args.Length();
  15
+  HeapSnapshot::Type mode = HeapSnapshot::kAggregated;
  16
+  if (len > 0) {
  17
+    title = args[0]->ToString();
  18
+  }
  19
+  if (len > 1 && args[1]->IsInt32()) {
  20
+    mode = static_cast<HeapSnapshot::Type>(args[1]->Int32Value());
  21
+  }
  22
+  const HeapSnapshot* snapshot = HeapProfiler::TakeSnapshot(title, mode);
  23
+  return scope.Close(Snapshot::New(snapshot));
  24
+}
  25
+
  26
+static Handle<Value> GetSnapshot(const Arguments& args) {
  27
+  HandleScope scope;
  28
+  if (args.Length() < 1) {
  29
+    return ThrowException(Exception::Error(String::New("No index specified")));
  30
+  } else if (!args[0]->IsInt32()) {
  31
+    return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
  32
+  }
  33
+  int32_t index = args[0]->Int32Value();
  34
+  const HeapSnapshot* snapshot = HeapProfiler::GetSnapshot(index);
  35
+  return scope.Close(Snapshot::New(snapshot));
  36
+}
  37
+
  38
+static Handle<Value> FindSnapshot(const Arguments& args) {
  39
+  HandleScope scope;
  40
+  if (args.Length() < 1) {
  41
+    return ThrowException(Exception::Error(String::New("No uid specified")));
  42
+  }
  43
+  uint32_t uid = args[0]->Uint32Value();
  44
+  const HeapSnapshot* snapshot = HeapProfiler::FindSnapshot(uid);
  45
+  return scope.Close(Snapshot::New(snapshot));
  46
+}
  47
+
  48
+static Handle<Value> GetSnapshotsCount(const Arguments& args) {
  49
+  HandleScope scope;
  50
+  return scope.Close(Integer::New(HeapProfiler::GetSnapshotsCount()));
  51
+}
  52
+
  53
+Handle<Value> StartProfiling(const Arguments& args) {
  54
+  HandleScope scope;
  55
+  Local<String> title = args.Length() > 0 ? args[0]->ToString() : String::New("");
  56
+  v8::CpuProfiler::StartProfiling(title);
  57
+  return Undefined();
  58
+}
  59
+
  60
+Handle<Value> StopProfiling(const Arguments& args) {
  61
+  HandleScope scope;
  62
+  Local<String> title = args.Length() > 0 ? args[0]->ToString() : String::New("");
  63
+  const CpuProfile* profile = v8::CpuProfiler::StopProfiling(title);
  64
+  return scope.Close(Profile::New(profile));
  65
+}
  66
+
  67
+Handle<Value> GetProfile(const Arguments& args) {
  68
+  HandleScope scope;
  69
+  if (args.Length() < 1) {
  70
+    return ThrowException(Exception::Error(String::New("No index specified")));
  71
+  } else if (!args[0]->IsInt32()) {
  72
+    return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
  73
+  }
  74
+  int32_t index = args[0]->Int32Value();
  75
+  const CpuProfile* profile = v8::CpuProfiler::GetProfile(index);
  76
+  return scope.Close(Profile::New(profile));
  77
+}
  78
+
  79
+Handle<Value> FindProfile(const Arguments& args) {
  80
+  HandleScope scope;
  81
+  if (args.Length() < 1) {
  82
+    return ThrowException(Exception::Error(String::New("No index specified")));
  83
+  } else if (!args[0]->IsInt32()) {
  84
+    return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
  85
+  }
  86
+  uint32_t uid = args[0]->Uint32Value();
  87
+  const CpuProfile* profile = v8::CpuProfiler::FindProfile(uid);
  88
+  return scope.Close(Profile::New(profile));
  89
+}
  90
+
  91
+Handle<Value> GetProfilesCount(const Arguments& args) {
  92
+  HandleScope scope;
  93
+  return scope.Close(Integer::New(v8::CpuProfiler::GetProfilesCount()));
  94
+}
  95
+
  96
+extern "C" void init(Handle<Object> target) {
  97
+  HandleScope scope;
  98
+
  99
+  NODE_SET_METHOD(target, "takeSnapshot", TakeSnapshot);
  100
+  NODE_SET_METHOD(target, "getSnapshot", GetSnapshot);
  101
+  NODE_SET_METHOD(target, "findSnapshot", FindSnapshot);
  102
+  NODE_SET_METHOD(target, "snapshotCount", GetSnapshotsCount);
  103
+  
  104
+  NODE_SET_METHOD(target, "startProfiling", StartProfiling);
  105
+  NODE_SET_METHOD(target, "stopProfiling", StopProfiling);
  106
+  NODE_SET_METHOD(target, "getProfile", GetProfile);
  107
+  NODE_SET_METHOD(target, "findProfile", FindProfile);
  108
+  NODE_SET_METHOD(target, "getProfilesCount", GetProfilesCount);
  109
+}
22  test.js
... ...
@@ -1,22 +0,0 @@
1  
-var assert = require('assert'),
2  
-    heap = require('./build/default/heap-profiler');
3  
-    
4  
-var x = heap.takeSnapshot('hello world');
5  
-
6  
-assert.equal(heap.snapshotCount(), 1);
7  
-assert.equal(x.title, 'hello world');
8  
-var root = x.root;
9  
-assert.notEqual(root, null);
10  
-
11  
-assert.ok(x.root.childrenCount > 0);
12  
-
13  
-for (var i = 0; i < root.childrenCount; i++) {
14  
-  assert.notEqual(root.getChild(i), undefined);
15  
-}
16  
-
17  
-var y = heap.takeSnapshot();
18  
-
19  
-var delta = x.compareWith(y);
20  
-assert.notEqual(delta.additions, null);
21  
-
22  
-console.log('done');
120  v8-profiler.js
... ...
@@ -0,0 +1,120 @@
  1
+var binding = require("./build/default/profiler");
  2
+
  3
+function Snapshot() {}
  4
+
  5
+//adapted from WebCore/bindings/v8/ScriptHeapSnapshot.cpp
  6
+Snapshot.prototype.stringify = function() {
  7
+  var root = this.root, i, j, count_i, count_j, node,
  8
+      lowLevels = {}, entries = {}, entry,
  9
+      children = {}, child, edge, result = {};
  10
+  for (i = 0, count_i = root.childrenCount; i < count_i; i++) {
  11
+    node = root.getChild(i).to;
  12
+    if (node.type === 'Hidden') {
  13
+      lowLevels[node.name] = {
  14
+        count: node.instancesCount,
  15
+        size: node.size,
  16
+        type: node.name
  17
+      };
  18
+    }
  19
+    else if (node.instancesCount > 0) {
  20
+      entries[node.name] = {
  21
+        constructorName: node.name,
  22
+        count: node.instancesCount,
  23
+        size: node.size
  24
+      };
  25
+    }
  26
+    else {
  27
+      entry = {
  28
+        constructorName: node.name
  29
+      };
  30
+      for(j = 0, count_j = node.childrenCount; j < count_j; j++) {
  31
+        edge = node.getChild(j);
  32
+        child = edge.to;
  33
+        entry[child.ptr.toString()] = {
  34
+          constructorName: child.name,
  35
+          count: parseInt(edge.name, 10)
  36
+        }
  37
+      }
  38
+      children[node.ptr.toString()] = entry;
  39
+    }
  40
+  }
  41
+  result.lowlevels = lowLevels;
  42
+  result.entries = entries;
  43
+  result.children = children;
  44
+  return JSON.stringify(result);
  45
+}
  46
+
  47
+function CpuProfile() {}
  48
+
  49
+function inspectorObjectFor(node) {
  50
+  var i, count, child,
  51
+      result = {
  52
+        functionName: node.functionName,
  53
+        url: node.scriptName,
  54
+        lineNumber: node.lineNumber,
  55
+        totalTime: node.totalTime,
  56
+        selfTime: node.selfTime,
  57
+        numberOfCalls: 0,
  58
+        visible: true,
  59
+        callUID: node.callUid,
  60
+        children: []
  61
+      };
  62
+  for(i = 0, count = node.childrenCount; i < count; i++) {
  63
+    child = node.getChild(i);
  64
+    result.children.push(inspectorObjectFor(child));
  65
+  }
  66
+  return result;
  67
+}
  68
+
  69
+CpuProfile.prototype.stringify = function() {
  70
+  return JSON.stringify(inspectorObjectFor(this.topRoot));
  71
+}
  72
+
  73
+var heapCache = [];
  74
+
  75
+exports.takeSnapshot = function(name, mode) {
  76
+  var type = (mode === 'full') ? 0 : 1;
  77
+  var snapshot = binding.takeSnapshot(name, type);
  78
+  snapshot.__proto__ = Snapshot.prototype;
  79
+  heapCache.push(snapshot);
  80
+  return snapshot;
  81
+}
  82
+
  83
+exports.getSnapshot = function(index) {
  84
+  return heapCache[index];
  85
+}
  86
+
  87
+exports.findSnapshot = function(uid) {
  88
+  return heapCache.filter(function(s) {return s.uid === uid;})[0];
  89
+}
  90
+
  91
+exports.snapshotCount = function() {
  92
+  return heapCache.length;
  93
+}
  94
+
  95
+var cpuCache = [];
  96
+
  97
+exports.startProfiling = function(name) {
  98
+  binding.startProfiling(name);
  99
+}
  100
+
  101
+exports.stopProfiling = function(name) {
  102
+  var profile = binding.stopProfiling(name);
  103
+  profile.__proto__ = CpuProfile.prototype;
  104
+  cpuCache.push(profile);
  105
+  return profile;
  106
+}
  107
+
  108
+exports.getProfile = function(index) {
  109
+  return cpuCache[index];
  110
+}
  111
+
  112
+exports.findProfile = function(uid) {
  113
+  return cpuCache.filter(function(s) {return s.uid === uid;})[0];
  114
+}
  115
+
  116
+exports.profileCount = function() {
  117
+  return cpuCache.length;
  118
+}
  119
+
  120
+process.profiler = exports;
6  wscript
@@ -11,12 +11,14 @@ def configure(ctx):
11 11
 
12 12
 def build(ctx):
13 13
   t = ctx.new_task_gen('cxx', 'shlib', 'node_addon')
14  
-  t.target = 'heap-profiler'
  14
+  t.target = 'profiler'
15 15
   t.source = """
16 16
     snapshot.cc
17 17
     graph_edge.cc
18 18
     graph_node.cc
19 19
     graph_path.cc
20 20
     snapshot_diff.cc
21  
-    heap.cc
  21
+    profile.cc
  22
+    profile_node.cc
  23
+    profiler.cc
22 24
   """

0 notes on commit 8345ba7

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