This repository was archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathgcstats.cc
149 lines (116 loc) · 4.83 KB
/
gcstats.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <nan.h>
using namespace v8;
struct HeapInfo {
size_t totalHeapSize;
size_t totalHeapExecutableSize;
size_t totalPhysicalSize;
size_t usedHeapSize;
size_t heapSizeLimit;
};
struct HeapData {
HeapInfo* before;
HeapInfo* after;
uint64_t gcStartTime;
uint64_t gcEndTime;
};
static Nan::Callback* afterGCCallback;
static HeapStatistics beforeGCStats;
uint64_t gcStartTime;
GCType gctype;
static NAN_GC_CALLBACK(recordBeforeGC) {
//Docs say that new objects should not be created
gcStartTime = uv_hrtime();
Nan::GetHeapStatistics(&beforeGCStats);
}
static void copyHeapStats(HeapStatistics* stats, HeapInfo* info) {
info->totalHeapSize = stats->total_heap_size();
info->totalHeapExecutableSize = stats->total_heap_size_executable();
#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
info->totalPhysicalSize = stats->total_physical_size();
#endif
info->usedHeapSize = stats->used_heap_size();
info->heapSizeLimit = stats->heap_size_limit();
}
static void formatStats(Local<Object> obj, HeapInfo* info) {
Nan::Set(obj, Nan::New("totalHeapSize").ToLocalChecked(), Nan::New<Number>(info->totalHeapSize));
Nan::Set(obj, Nan::New("totalHeapExecutableSize").ToLocalChecked(), Nan::New<Number>(info->totalHeapExecutableSize));
Nan::Set(obj, Nan::New("usedHeapSize").ToLocalChecked(), Nan::New<Number>(info->usedHeapSize));
Nan::Set(obj, Nan::New("heapSizeLimit").ToLocalChecked(), Nan::New<Number>(info->heapSizeLimit));
#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
Nan::Set(obj, Nan::New("totalPhysicalSize").ToLocalChecked(), Nan::New<Number>(info->totalPhysicalSize));
#endif
}
static void formatStatDiff(Local<Object> obj, HeapInfo* before, HeapInfo* after) {
Nan::Set(obj, Nan::New("totalHeapSize").ToLocalChecked(), Nan::New<Number>(
static_cast<double>(after->totalHeapSize) - static_cast<double>(before->totalHeapSize)));
Nan::Set(obj, Nan::New("totalHeapExecutableSize").ToLocalChecked(), Nan::New<Number>(
static_cast<double>(after->totalHeapExecutableSize) - static_cast<double>(before->totalHeapExecutableSize)));
Nan::Set(obj, Nan::New("usedHeapSize").ToLocalChecked(), Nan::New<Number>(
static_cast<double>(after->usedHeapSize) - static_cast<double>(before->usedHeapSize)));
Nan::Set(obj, Nan::New("heapSizeLimit").ToLocalChecked(), Nan::New<Number>(
static_cast<double>(after->heapSizeLimit) - static_cast<double>(before->heapSizeLimit)));
#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
Nan::Set(obj, Nan::New("totalPhysicalSize").ToLocalChecked(), Nan::New<Number>(
static_cast<double>(after->totalPhysicalSize) - static_cast<double>(before->totalPhysicalSize)));
#endif
}
static void asyncAfter(uv_work_t* work, int status) {
Nan::HandleScope scope;
HeapData* data = static_cast<HeapData*>(work->data);
Local<Object> obj = Nan::New<Object>();
Local<Object> beforeGCStats = Nan::New<Object>();
Local<Object> afterGCStats = Nan::New<Object>();
formatStats(beforeGCStats, data->before);
formatStats(afterGCStats, data->after);
Local<Object> diffStats = Nan::New<Object>();
formatStatDiff(diffStats, data->before, data->after);
Nan::Set(obj, Nan::New("pause").ToLocalChecked(),
Nan::New<Number>(static_cast<double>(data->gcEndTime - data->gcStartTime)));
Nan::Set(obj, Nan::New("pauseMS").ToLocalChecked(),
Nan::New<Number>(static_cast<double>((data->gcEndTime - data->gcStartTime) / 1000000)));
Nan::Set(obj, Nan::New("gctype").ToLocalChecked(), Nan::New<Number>(gctype));
Nan::Set(obj, Nan::New("before").ToLocalChecked(), beforeGCStats);
Nan::Set(obj, Nan::New("after").ToLocalChecked(), afterGCStats);
Nan::Set(obj, Nan::New("diff").ToLocalChecked(), diffStats);
Local<Value> arguments[] = {obj};
afterGCCallback->Call(1, arguments);
delete data->before;
delete data->after;
delete data;
delete work;
}
static void asyncWork(uv_work_t* work) {
//can't create V8 objects here because this is different thread?
}
NAN_GC_CALLBACK(afterGC) {
uv_work_t* work = new uv_work_t;
HeapData* data = new HeapData;
data->before = new HeapInfo;
data->after = new HeapInfo;
gctype = type;
HeapStatistics stats;
Nan::GetHeapStatistics(&stats);
data->gcEndTime = uv_hrtime();
data->gcStartTime = gcStartTime;
copyHeapStats(&beforeGCStats, data->before);
copyHeapStats(&stats, data->after);
work->data = data;
uv_queue_work(uv_default_loop(), work, asyncWork, asyncAfter);
}
static NAN_METHOD(AfterGC) {
if(info.Length() != 1 || !info[0]->IsFunction()) {
return Nan::ThrowError("Callback is required");
}
Local<Function> callbackHandle = info[0].As<Function>();
afterGCCallback = new Nan::Callback(callbackHandle);
Nan::AddGCEpilogueCallback(afterGC);
}
NAN_MODULE_INIT(init) {
Nan::HandleScope scope;
Nan::AddGCPrologueCallback(recordBeforeGC);
Nan::Set(target,
Nan::New("afterGC").ToLocalChecked(),
Nan::GetFunction(
Nan::New<FunctionTemplate>(AfterGC)).ToLocalChecked());
}
NODE_MODULE(gcstats, init)