forked from heycalmdown/v8-profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_profiler.cc
75 lines (62 loc) · 3.08 KB
/
cpu_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
#include "cpu_profiler.h"
#include "profile.h"
namespace nodex {
Persistent<ObjectTemplate> CpuProfiler::cpu_profiler_template_;
void CpuProfiler::Initialize(Handle<Object> target) {
HandleScope scope;
cpu_profiler_template_ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
cpu_profiler_template_->SetInternalFieldCount(1);
Local<Object> cpuProfilerObj = cpu_profiler_template_->NewInstance();
NODE_SET_METHOD(cpuProfilerObj, "getProfilesCount", CpuProfiler::GetProfilesCount);
NODE_SET_METHOD(cpuProfilerObj, "getProfile", CpuProfiler::GetProfile);
NODE_SET_METHOD(cpuProfilerObj, "findProfile", CpuProfiler::FindProfile);
NODE_SET_METHOD(cpuProfilerObj, "startProfiling", CpuProfiler::StartProfiling);
NODE_SET_METHOD(cpuProfilerObj, "stopProfiling", CpuProfiler::StopProfiling);
NODE_SET_METHOD(cpuProfilerObj, "deleteAllProfiles", CpuProfiler::DeleteAllProfiles);
target->Set(String::NewSymbol("cpuProfiler"), cpuProfilerObj);
}
CpuProfiler::CpuProfiler() {}
CpuProfiler::~CpuProfiler() {}
Handle<Value> CpuProfiler::GetProfilesCount(const Arguments& args) {
HandleScope scope;
return scope.Close(Integer::New(v8::CpuProfiler::GetProfilesCount()));
}
Handle<Value> CpuProfiler::GetProfile(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 CpuProfile* profile = v8::CpuProfiler::GetProfile(index);
return scope.Close(Profile::New(profile));
}
Handle<Value> CpuProfiler::FindProfile(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")));
}
uint32_t uid = args[0]->Uint32Value();
const CpuProfile* profile = v8::CpuProfiler::FindProfile(uid);
return scope.Close(Profile::New(profile));
}
Handle<Value> CpuProfiler::StartProfiling(const Arguments& args) {
HandleScope scope;
Local<String> title = args.Length() > 0 ? args[0]->ToString() : String::New("");
v8::CpuProfiler::StartProfiling(title);
return Undefined();
}
Handle<Value> CpuProfiler::StopProfiling(const Arguments& args) {
HandleScope scope;
Local<String> title = args.Length() > 0 ? args[0]->ToString() : String::New("");
const CpuProfile* profile = v8::CpuProfiler::StopProfiling(title);
return scope.Close(Profile::New(profile));
}
Handle<Value> CpuProfiler::DeleteAllProfiles(const Arguments& args) {
v8::CpuProfiler::DeleteAllProfiles();
return Undefined();
}
} //namespace nodex