forked from node-inspector/v8-profiler
-
Notifications
You must be signed in to change notification settings - Fork 5
/
heap_profiler.cc
129 lines (98 loc) · 4.37 KB
/
heap_profiler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "heap_profiler.h"
#include "snapshot.h"
namespace nodex {
Persistent<ObjectTemplate> HeapProfiler::heap_profiler_template_;
class ActivityControlAdapter : public ActivityControl {
public:
ActivityControlAdapter(Handle<Value> progress)
: reportProgress(Handle<Function>::Cast(progress)),
abort(Local<Value>::New(Boolean::New(false))) {}
ControlOption ReportProgressValue(int done, int total) {
HandleScope scope;
Local<Value> argv[2] = {
Integer::New(done),
Integer::New(total)
};
TryCatch try_catch;
abort = reportProgress->Call(Context::GetCurrent()->Global(), 2, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
return kAbort;
}
fprintf(stderr, "here!\n");
if (abort.IsEmpty() || !abort->IsBoolean()) {
return kContinue;
}
return abort->IsTrue() ? kAbort : kContinue;
}
private:
Handle<Function> reportProgress;
Local<Value> abort;
};
void HeapProfiler::Initialize(Handle<Object> target) {
HandleScope scope;
heap_profiler_template_ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
heap_profiler_template_->SetInternalFieldCount(1);
Local<Object> heapProfilerObj = heap_profiler_template_->NewInstance();
NODE_SET_METHOD(heapProfilerObj, "takeSnapshot", HeapProfiler::TakeSnapshot);
NODE_SET_METHOD(heapProfilerObj, "getSnapshot", HeapProfiler::GetSnapshot);
NODE_SET_METHOD(heapProfilerObj, "findSnapshot", HeapProfiler::FindSnapshot);
NODE_SET_METHOD(heapProfilerObj, "getSnapshotsCount", HeapProfiler::GetSnapshotsCount);
NODE_SET_METHOD(heapProfilerObj, "deleteAllSnapshots", HeapProfiler::DeleteAllSnapshots);
target->Set(String::NewSymbol("heapProfiler"), heapProfilerObj);
}
HeapProfiler::HeapProfiler() {}
HeapProfiler::~HeapProfiler() {}
Handle<Value> HeapProfiler::GetSnapshotsCount(const Arguments& args) {
HandleScope scope;
return scope.Close(Integer::New(v8::HeapProfiler::GetSnapshotsCount()));
}
Handle<Value> HeapProfiler::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::TypeError(String::New("Argument must be an integer")));
}
int32_t index = args[0]->Int32Value();
const v8::HeapSnapshot* snapshot = v8::HeapProfiler::GetSnapshot(index);
return scope.Close(Snapshot::New(snapshot));
}
Handle<Value> HeapProfiler::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 v8::HeapSnapshot* snapshot = v8::HeapProfiler::FindSnapshot(uid);
return scope.Close(Snapshot::New(snapshot));
}
Handle<Value> HeapProfiler::TakeSnapshot(const Arguments& args) {
HandleScope scope;
Local<String> title = String::New("");
uint32_t len = args.Length();
ActivityControlAdapter *control = NULL;
if (len == 1) {
if (args[0]->IsString()) {
title = args[0]->ToString();
} else if (args[0]->IsFunction()) {
//control = new ActivityControlAdapter(args[0]);
}
}
if (len == 2) {
if (args[0]->IsString()) {
title = args[0]->ToString();
}
if (args[1]->IsFunction()) {
//control = new ActivityControlAdapter(args[1]);
}
}
const v8::HeapSnapshot* snapshot = v8::HeapProfiler::TakeSnapshot(title, HeapSnapshot::kFull, control);
return scope.Close(Snapshot::New(snapshot));
}
Handle<Value> HeapProfiler::DeleteAllSnapshots(const Arguments& args) {
HandleScope scope;
v8::HeapProfiler::DeleteAllSnapshots();
return Undefined();
}
} //namespace nodex