1 |
|
|
#include "node_perf.h" |
2 |
|
|
#include "aliased_buffer.h" |
3 |
|
|
#include "env-inl.h" |
4 |
|
|
#include "histogram-inl.h" |
5 |
|
|
#include "memory_tracker-inl.h" |
6 |
|
|
#include "node_buffer.h" |
7 |
|
|
#include "node_external_reference.h" |
8 |
|
|
#include "node_internals.h" |
9 |
|
|
#include "node_process-inl.h" |
10 |
|
|
#include "util-inl.h" |
11 |
|
|
|
12 |
|
|
#include <cinttypes> |
13 |
|
|
|
14 |
|
|
namespace node { |
15 |
|
|
namespace performance { |
16 |
|
|
|
17 |
|
|
using v8::Context; |
18 |
|
|
using v8::DontDelete; |
19 |
|
|
using v8::Function; |
20 |
|
|
using v8::FunctionCallbackInfo; |
21 |
|
|
using v8::FunctionTemplate; |
22 |
|
|
using v8::GCCallbackFlags; |
23 |
|
|
using v8::GCType; |
24 |
|
|
using v8::Int32; |
25 |
|
|
using v8::Integer; |
26 |
|
|
using v8::Isolate; |
27 |
|
|
using v8::Local; |
28 |
|
|
using v8::MaybeLocal; |
29 |
|
|
using v8::Number; |
30 |
|
|
using v8::Object; |
31 |
|
|
using v8::PropertyAttribute; |
32 |
|
|
using v8::ReadOnly; |
33 |
|
|
using v8::String; |
34 |
|
|
using v8::Value; |
35 |
|
|
|
36 |
|
|
// Microseconds in a millisecond, as a float. |
37 |
|
|
#define MICROS_PER_MILLIS 1e3 |
38 |
|
|
|
39 |
|
|
// https://w3c.github.io/hr-time/#dfn-time-origin |
40 |
|
|
const uint64_t timeOrigin = PERFORMANCE_NOW(); |
41 |
|
|
// https://w3c.github.io/hr-time/#dfn-time-origin-timestamp |
42 |
|
|
const double timeOriginTimestamp = GetCurrentTimeInMicroseconds(); |
43 |
|
|
uint64_t performance_v8_start; |
44 |
|
|
|
45 |
|
6054 |
PerformanceState::PerformanceState(Isolate* isolate, |
46 |
|
6054 |
const PerformanceState::SerializeInfo* info) |
47 |
|
|
: root(isolate, |
48 |
|
|
sizeof(performance_state_internal), |
49 |
|
|
MAYBE_FIELD_PTR(info, root)), |
50 |
|
|
milestones(isolate, |
51 |
|
|
offsetof(performance_state_internal, milestones), |
52 |
|
|
NODE_PERFORMANCE_MILESTONE_INVALID, |
53 |
|
6054 |
root, |
54 |
|
|
MAYBE_FIELD_PTR(info, milestones)), |
55 |
|
|
observers(isolate, |
56 |
|
|
offsetof(performance_state_internal, observers), |
57 |
|
|
NODE_PERFORMANCE_ENTRY_TYPE_INVALID, |
58 |
|
6054 |
root, |
59 |
✓✓✓✓ ✓✓ |
6054 |
MAYBE_FIELD_PTR(info, observers)) { |
60 |
✓✓ |
6054 |
if (info == nullptr) { |
61 |
✓✓ |
5978 |
for (size_t i = 0; i < milestones.Length(); i++) milestones[i] = -1.; |
62 |
|
|
} |
63 |
|
6054 |
} |
64 |
|
|
|
65 |
|
6 |
PerformanceState::SerializeInfo PerformanceState::Serialize( |
66 |
|
|
v8::Local<v8::Context> context, v8::SnapshotCreator* creator) { |
67 |
|
6 |
SerializeInfo info{root.Serialize(context, creator), |
68 |
|
6 |
milestones.Serialize(context, creator), |
69 |
|
6 |
observers.Serialize(context, creator)}; |
70 |
|
6 |
return info; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
5200 |
void PerformanceState::Deserialize(v8::Local<v8::Context> context) { |
74 |
|
5200 |
root.Deserialize(context); |
75 |
|
|
// This is just done to set up the pointers, we will actually reset |
76 |
|
|
// all the milestones after deserialization. |
77 |
|
5200 |
milestones.Deserialize(context); |
78 |
|
5200 |
observers.Deserialize(context); |
79 |
|
5200 |
} |
80 |
|
|
|
81 |
|
6 |
std::ostream& operator<<(std::ostream& o, |
82 |
|
|
const PerformanceState::SerializeInfo& i) { |
83 |
|
|
o << "{\n" |
84 |
|
6 |
<< " " << i.root << ", // root\n" |
85 |
|
6 |
<< " " << i.milestones << ", // milestones\n" |
86 |
|
6 |
<< " " << i.observers << ", // observers\n" |
87 |
|
6 |
<< "}"; |
88 |
|
6 |
return o; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
35261 |
void PerformanceState::Mark(PerformanceMilestone milestone, uint64_t ts) { |
92 |
|
35261 |
this->milestones[milestone] = static_cast<double>(ts); |
93 |
✓✓✓✓
|
40477 |
TRACE_EVENT_INSTANT_WITH_TIMESTAMP0( |
94 |
|
|
TRACING_CATEGORY_NODE1(bootstrap), |
95 |
|
|
GetPerformanceMilestoneName(milestone), |
96 |
|
|
TRACE_EVENT_SCOPE_THREAD, ts / 1000); |
97 |
|
35261 |
} |
98 |
|
|
|
99 |
|
|
// Allows specific Node.js lifecycle milestones to be set from JavaScript |
100 |
|
|
void MarkMilestone(const FunctionCallbackInfo<Value>& args) { |
101 |
|
|
Environment* env = Environment::GetCurrent(args); |
102 |
|
|
PerformanceMilestone milestone = |
103 |
|
|
static_cast<PerformanceMilestone>(args[0].As<Int32>()->Value()); |
104 |
|
|
if (milestone != NODE_PERFORMANCE_MILESTONE_INVALID) |
105 |
|
|
env->performance_state()->Mark(milestone); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
854 |
void SetupPerformanceObservers(const FunctionCallbackInfo<Value>& args) { |
109 |
|
854 |
Environment* env = Environment::GetCurrent(args); |
110 |
✗✓ |
854 |
CHECK(args[0]->IsFunction()); |
111 |
|
1708 |
env->set_performance_entry_callback(args[0].As<Function>()); |
112 |
|
854 |
} |
113 |
|
|
|
114 |
|
|
// Marks the start of a GC cycle |
115 |
|
1 |
void MarkGarbageCollectionStart( |
116 |
|
|
Isolate* isolate, |
117 |
|
|
GCType type, |
118 |
|
|
GCCallbackFlags flags, |
119 |
|
|
void* data) { |
120 |
|
1 |
Environment* env = static_cast<Environment*>(data); |
121 |
|
1 |
env->performance_state()->performance_last_gc_start_mark = PERFORMANCE_NOW(); |
122 |
|
1 |
} |
123 |
|
|
|
124 |
|
1 |
MaybeLocal<Object> GCPerformanceEntryTraits::GetDetails( |
125 |
|
|
Environment* env, |
126 |
|
|
const GCPerformanceEntry& entry) { |
127 |
|
1 |
Local<Object> obj = Object::New(env->isolate()); |
128 |
|
|
|
129 |
|
2 |
if (!obj->Set( |
130 |
|
|
env->context(), |
131 |
|
|
env->kind_string(), |
132 |
|
|
Integer::NewFromUnsigned( |
133 |
|
|
env->isolate(), |
134 |
✗✓ |
4 |
entry.details.kind)).IsJust()) { |
135 |
|
|
return MaybeLocal<Object>(); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
2 |
if (!obj->Set( |
139 |
|
|
env->context(), |
140 |
|
|
env->flags_string(), |
141 |
|
|
Integer::NewFromUnsigned( |
142 |
|
|
env->isolate(), |
143 |
✗✓ |
4 |
entry.details.flags)).IsJust()) { |
144 |
|
|
return MaybeLocal<Object>(); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
1 |
return obj; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
// Marks the end of a GC cycle |
151 |
|
1 |
void MarkGarbageCollectionEnd( |
152 |
|
|
Isolate* isolate, |
153 |
|
|
GCType type, |
154 |
|
|
GCCallbackFlags flags, |
155 |
|
|
void* data) { |
156 |
|
1 |
Environment* env = static_cast<Environment*>(data); |
157 |
|
1 |
PerformanceState* state = env->performance_state(); |
158 |
|
|
// If no one is listening to gc performance entries, do not create them. |
159 |
✗✓ |
1 |
if (LIKELY(!state->observers[NODE_PERFORMANCE_ENTRY_TYPE_GC])) |
160 |
|
|
return; |
161 |
|
|
|
162 |
|
1 |
double start_time = state->performance_last_gc_start_mark / 1e6; |
163 |
|
1 |
double duration = (PERFORMANCE_NOW() / 1e6) - start_time; |
164 |
|
|
|
165 |
|
|
std::unique_ptr<GCPerformanceEntry> entry = |
166 |
|
|
std::make_unique<GCPerformanceEntry>( |
167 |
|
|
"gc", |
168 |
|
|
start_time, |
169 |
|
|
duration, |
170 |
|
1 |
GCPerformanceEntry::Details(static_cast<PerformanceGCKind>(type), |
171 |
|
1 |
static_cast<PerformanceGCFlags>(flags))); |
172 |
|
|
|
173 |
|
1 |
env->SetImmediate([entry = std::move(entry)](Environment* env) { |
174 |
|
1 |
entry->Notify(env); |
175 |
|
1 |
}, CallbackFlags::kUnrefed); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
4 |
void GarbageCollectionCleanupHook(void* data) { |
179 |
|
4 |
Environment* env = static_cast<Environment*>(data); |
180 |
|
4 |
env->isolate()->RemoveGCPrologueCallback(MarkGarbageCollectionStart, data); |
181 |
|
4 |
env->isolate()->RemoveGCEpilogueCallback(MarkGarbageCollectionEnd, data); |
182 |
|
4 |
} |
183 |
|
|
|
184 |
|
4 |
static void InstallGarbageCollectionTracking( |
185 |
|
|
const FunctionCallbackInfo<Value>& args) { |
186 |
|
4 |
Environment* env = Environment::GetCurrent(args); |
187 |
|
|
|
188 |
|
4 |
env->isolate()->AddGCPrologueCallback(MarkGarbageCollectionStart, |
189 |
|
|
static_cast<void*>(env)); |
190 |
|
4 |
env->isolate()->AddGCEpilogueCallback(MarkGarbageCollectionEnd, |
191 |
|
|
static_cast<void*>(env)); |
192 |
|
4 |
env->AddCleanupHook(GarbageCollectionCleanupHook, env); |
193 |
|
4 |
} |
194 |
|
|
|
195 |
|
3 |
static void RemoveGarbageCollectionTracking( |
196 |
|
|
const FunctionCallbackInfo<Value> &args) { |
197 |
|
3 |
Environment* env = Environment::GetCurrent(args); |
198 |
|
|
|
199 |
|
3 |
env->RemoveCleanupHook(GarbageCollectionCleanupHook, env); |
200 |
|
3 |
GarbageCollectionCleanupHook(env); |
201 |
|
3 |
} |
202 |
|
|
|
203 |
|
|
// Gets the name of a function |
204 |
|
|
inline Local<Value> GetName(Local<Function> fn) { |
205 |
|
|
Local<Value> val = fn->GetDebugName(); |
206 |
|
|
if (val.IsEmpty() || val->IsUndefined()) { |
207 |
|
|
Local<Value> boundFunction = fn->GetBoundFunction(); |
208 |
|
|
if (!boundFunction.IsEmpty() && !boundFunction->IsUndefined()) { |
209 |
|
|
val = GetName(boundFunction.As<Function>()); |
210 |
|
|
} |
211 |
|
|
} |
212 |
|
|
return val; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
// Notify a custom PerformanceEntry to observers |
216 |
|
|
void Notify(const FunctionCallbackInfo<Value>& args) { |
217 |
|
|
Environment* env = Environment::GetCurrent(args); |
218 |
|
|
Utf8Value type(env->isolate(), args[0]); |
219 |
|
|
Local<Value> entry = args[1]; |
220 |
|
|
PerformanceEntryType entry_type = ToPerformanceEntryTypeEnum(*type); |
221 |
|
|
AliasedUint32Array& observers = env->performance_state()->observers; |
222 |
|
|
if (entry_type != NODE_PERFORMANCE_ENTRY_TYPE_INVALID && |
223 |
|
|
observers[entry_type]) { |
224 |
|
|
USE(env->performance_entry_callback()-> |
225 |
|
|
Call(env->context(), Undefined(env->isolate()), 1, &entry)); |
226 |
|
|
} |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
// Return idle time of the event loop |
230 |
|
50 |
void LoopIdleTime(const FunctionCallbackInfo<Value>& args) { |
231 |
|
50 |
Environment* env = Environment::GetCurrent(args); |
232 |
|
50 |
uint64_t idle_time = uv_metrics_idle_time(env->event_loop()); |
233 |
|
50 |
args.GetReturnValue().Set(1.0 * idle_time / 1e6); |
234 |
|
50 |
} |
235 |
|
|
|
236 |
|
3 |
void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) { |
237 |
|
3 |
Environment* env = Environment::GetCurrent(args); |
238 |
|
6 |
int64_t interval = args[0].As<Integer>()->Value(); |
239 |
✗✓ |
3 |
CHECK_GT(interval, 0); |
240 |
|
|
BaseObjectPtr<IntervalHistogram> histogram = |
241 |
|
1758 |
IntervalHistogram::Create(env, interval, [](Histogram& histogram) { |
242 |
|
1758 |
uint64_t delta = histogram.RecordDelta(); |
243 |
✓✓✗✓
|
1760 |
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop), |
244 |
|
|
"delay", delta); |
245 |
✓✓✗✓
|
1760 |
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop), |
246 |
|
|
"min", histogram.Min()); |
247 |
✓✓✗✓
|
1760 |
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop), |
248 |
|
|
"max", histogram.Max()); |
249 |
✓✓✗✓
|
1760 |
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop), |
250 |
|
|
"mean", histogram.Mean()); |
251 |
✓✓✗✓
|
1760 |
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop), |
252 |
|
|
"stddev", histogram.Stddev()); |
253 |
|
6 |
}, Histogram::Options { 1000 }); |
254 |
|
6 |
args.GetReturnValue().Set(histogram->object()); |
255 |
|
3 |
} |
256 |
|
|
|
257 |
|
6875 |
void GetTimeOrigin(const FunctionCallbackInfo<Value>& args) { |
258 |
|
6875 |
args.GetReturnValue().Set(Number::New(args.GetIsolate(), timeOrigin / 1e6)); |
259 |
|
6875 |
} |
260 |
|
|
|
261 |
|
6875 |
void GetTimeOriginTimeStamp(const FunctionCallbackInfo<Value>& args) { |
262 |
|
6875 |
args.GetReturnValue().Set( |
263 |
|
|
Number::New(args.GetIsolate(), timeOriginTimestamp / MICROS_PER_MILLIS)); |
264 |
|
6875 |
} |
265 |
|
|
|
266 |
|
854 |
void Initialize(Local<Object> target, |
267 |
|
|
Local<Value> unused, |
268 |
|
|
Local<Context> context, |
269 |
|
|
void* priv) { |
270 |
|
854 |
Environment* env = Environment::GetCurrent(context); |
271 |
|
854 |
Isolate* isolate = env->isolate(); |
272 |
|
854 |
PerformanceState* state = env->performance_state(); |
273 |
|
|
|
274 |
|
854 |
target->Set(context, |
275 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "observerCounts"), |
276 |
|
3416 |
state->observers.GetJSArray()).Check(); |
277 |
|
854 |
target->Set(context, |
278 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "milestones"), |
279 |
|
2562 |
state->milestones.GetJSArray()).Check(); |
280 |
|
|
|
281 |
|
|
Local<String> performanceEntryString = |
282 |
|
854 |
FIXED_ONE_BYTE_STRING(isolate, "PerformanceEntry"); |
283 |
|
|
|
284 |
|
854 |
Local<FunctionTemplate> pe = FunctionTemplate::New(isolate); |
285 |
|
854 |
pe->SetClassName(performanceEntryString); |
286 |
|
1708 |
Local<Function> fn = pe->GetFunction(context).ToLocalChecked(); |
287 |
|
854 |
target->Set(context, performanceEntryString, fn).Check(); |
288 |
|
854 |
env->set_performance_entry_template(fn); |
289 |
|
|
|
290 |
|
854 |
env->SetMethod(target, "markMilestone", MarkMilestone); |
291 |
|
854 |
env->SetMethod(target, "setupObservers", SetupPerformanceObservers); |
292 |
|
854 |
env->SetMethod(target, |
293 |
|
|
"installGarbageCollectionTracking", |
294 |
|
|
InstallGarbageCollectionTracking); |
295 |
|
854 |
env->SetMethod(target, |
296 |
|
|
"removeGarbageCollectionTracking", |
297 |
|
|
RemoveGarbageCollectionTracking); |
298 |
|
854 |
env->SetMethod(target, "notify", Notify); |
299 |
|
854 |
env->SetMethod(target, "loopIdleTime", LoopIdleTime); |
300 |
|
854 |
env->SetMethod(target, "getTimeOrigin", GetTimeOrigin); |
301 |
|
854 |
env->SetMethod(target, "getTimeOriginTimestamp", GetTimeOriginTimeStamp); |
302 |
|
854 |
env->SetMethod(target, "createELDHistogram", CreateELDHistogram); |
303 |
|
|
|
304 |
|
854 |
Local<Object> constants = Object::New(isolate); |
305 |
|
|
|
306 |
|
2562 |
NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_MAJOR); |
307 |
|
2562 |
NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_MINOR); |
308 |
|
2562 |
NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_INCREMENTAL); |
309 |
|
2562 |
NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_WEAKCB); |
310 |
|
|
|
311 |
|
2562 |
NODE_DEFINE_CONSTANT( |
312 |
|
|
constants, NODE_PERFORMANCE_GC_FLAGS_NO); |
313 |
|
2562 |
NODE_DEFINE_CONSTANT( |
314 |
|
|
constants, NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED); |
315 |
|
2562 |
NODE_DEFINE_CONSTANT( |
316 |
|
|
constants, NODE_PERFORMANCE_GC_FLAGS_FORCED); |
317 |
|
2562 |
NODE_DEFINE_CONSTANT( |
318 |
|
|
constants, NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING); |
319 |
|
2562 |
NODE_DEFINE_CONSTANT( |
320 |
|
|
constants, NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE); |
321 |
|
2562 |
NODE_DEFINE_CONSTANT( |
322 |
|
|
constants, NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY); |
323 |
|
2562 |
NODE_DEFINE_CONSTANT( |
324 |
|
|
constants, NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE); |
325 |
|
|
|
326 |
|
|
#define V(name, _) \ |
327 |
|
|
NODE_DEFINE_HIDDEN_CONSTANT(constants, NODE_PERFORMANCE_ENTRY_TYPE_##name); |
328 |
|
9394 |
NODE_PERFORMANCE_ENTRY_TYPES(V) |
329 |
|
|
#undef V |
330 |
|
|
|
331 |
|
|
#define V(name, _) \ |
332 |
|
|
NODE_DEFINE_HIDDEN_CONSTANT(constants, NODE_PERFORMANCE_MILESTONE_##name); |
333 |
|
10248 |
NODE_PERFORMANCE_MILESTONES(V) |
334 |
|
|
#undef V |
335 |
|
|
|
336 |
|
854 |
PropertyAttribute attr = |
337 |
|
|
static_cast<PropertyAttribute>(ReadOnly | DontDelete); |
338 |
|
|
|
339 |
|
854 |
target->DefineOwnProperty(context, |
340 |
|
|
env->constants_string(), |
341 |
|
|
constants, |
342 |
|
1708 |
attr).ToChecked(); |
343 |
|
|
|
344 |
|
854 |
HistogramBase::Initialize(env, target); |
345 |
|
854 |
} |
346 |
|
|
|
347 |
|
5206 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
348 |
|
5206 |
registry->Register(MarkMilestone); |
349 |
|
5206 |
registry->Register(SetupPerformanceObservers); |
350 |
|
5206 |
registry->Register(InstallGarbageCollectionTracking); |
351 |
|
5206 |
registry->Register(RemoveGarbageCollectionTracking); |
352 |
|
5206 |
registry->Register(Notify); |
353 |
|
5206 |
registry->Register(LoopIdleTime); |
354 |
|
5206 |
registry->Register(GetTimeOrigin); |
355 |
|
5206 |
registry->Register(GetTimeOriginTimeStamp); |
356 |
|
5206 |
registry->Register(CreateELDHistogram); |
357 |
|
5206 |
HistogramBase::RegisterExternalReferences(registry); |
358 |
|
5206 |
IntervalHistogram::RegisterExternalReferences(registry); |
359 |
|
5206 |
} |
360 |
|
|
} // namespace performance |
361 |
|
|
} // namespace node |
362 |
|
|
|
363 |
|
5274 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(performance, node::performance::Initialize) |
364 |
|
5206 |
NODE_MODULE_EXTERNAL_REFERENCE(performance, |
365 |
|
|
node::performance::RegisterExternalReferences) |