-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathcpu_profile.cc
104 lines (86 loc) · 3.52 KB
/
cpu_profile.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
#include "cpu_profile.h"
#include "cpu_profile_node.h"
namespace nodex {
using v8::Array;
using v8::CpuProfile;
using v8::CpuProfileNode;
using v8::Handle;
using v8::Number;
using v8::Integer;
using v8::Local;
using v8::Object;
using v8::ObjectTemplate;
using v8::FunctionTemplate;
using v8::String;
using v8::Function;
using v8::Value;
Nan::Persistent<ObjectTemplate> Profile::profile_template_;
Nan::Persistent<Object> Profile::profiles;
uint32_t Profile::uid_counter = 0;
NAN_METHOD(Profile_EmptyMethod) {
}
void Profile::Initialize () {
Nan::HandleScope scope;
Local<FunctionTemplate> f = Nan::New<FunctionTemplate>(Profile_EmptyMethod);
Local<ObjectTemplate> o = f->InstanceTemplate();
o->SetInternalFieldCount(1);
Nan::SetMethod(o, "delete", Profile::Delete);
profile_template_.Reset(o);
}
NAN_METHOD(Profile::Delete) {
Local<Object> self = info.This();
void* ptr = Nan::GetInternalFieldPointer(self, 0);
Local<Object> profiles = Nan::New<Object>(Profile::profiles);
Local<Value> _uid = info.This()->Get(Nan::New<String>("uid").ToLocalChecked());
Local<String> uid = Nan::To<String>(_uid).ToLocalChecked();
profiles->Delete(uid);
static_cast<CpuProfile*>(ptr)->Delete();
}
Local<Value> Profile::New (const CpuProfile* node) {
Nan::EscapableHandleScope scope;
if (profile_template_.IsEmpty()) {
Profile::Initialize();
}
uid_counter++;
Local<Object> profile = Nan::New(profile_template_)->NewInstance();
Nan::SetInternalFieldPointer(profile, 0, const_cast<CpuProfile*>(node));
const uint32_t uid_length = (((sizeof uid_counter) * 8) + 2)/3 + 2;
char _uid[uid_length];
sprintf(_uid, "%d", uid_counter);
Local<Value> CPU = Nan::New<String>("CPU").ToLocalChecked();
Local<Value> uid = Nan::New<String>(_uid).ToLocalChecked();
#if (NODE_MODULE_VERSION >= 45)
Local<String> title = node->GetTitle();
#else
Local<String> title = Nan::New(node->GetTitle());
#endif
if (!title->Length()) {
char _title[8 + uid_length];
sprintf(_title, "Profile %i", uid_counter);
title = Nan::New<String>(_title).ToLocalChecked();
}
Local<Value> head = ProfileNode::New(node->GetTopDownRoot());
profile->Set(Nan::New<String>("typeId").ToLocalChecked(), CPU);
profile->Set(Nan::New<String>("uid").ToLocalChecked(), uid);
profile->Set(Nan::New<String>("title").ToLocalChecked(), title);
profile->Set(Nan::New<String>("head").ToLocalChecked(), head);
#if (NODE_MODULE_VERSION > 0x000B)
Local<Value> start_time = Nan::New<Number>(node->GetStartTime()/1000000);
Local<Value> end_time = Nan::New<Number>(node->GetEndTime()/1000000);
Local<Array> samples = Nan::New<Array>();
Local<Array> timestamps = Nan::New<Array>();
uint32_t count = node->GetSamplesCount();
for (uint32_t index = 0; index < count; ++index) {
samples->Set(index, Nan::New<Integer>(node->GetSample(index)->GetNodeId()));
timestamps->Set(index, Nan::New<Number>(static_cast<double>(node->GetSampleTimestamp(index))));
}
profile->Set(Nan::New<String>("startTime").ToLocalChecked(), start_time);
profile->Set(Nan::New<String>("endTime").ToLocalChecked(), end_time);
profile->Set(Nan::New<String>("samples").ToLocalChecked(), samples);
profile->Set(Nan::New<String>("timestamps").ToLocalChecked(), timestamps);
#endif
Local<Object> profiles = Nan::New<Object>(Profile::profiles);
profiles->Set(uid, profile);
return scope.Escape(profile);
}
}