-
Notifications
You must be signed in to change notification settings - Fork 30.5k
/
Copy pathnode_perf.cc
426 lines (375 loc) Β· 15.2 KB
/
node_perf.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "node_perf.h"
#include "aliased_buffer-inl.h"
#include "env-inl.h"
#include "histogram-inl.h"
#include "memory_tracker-inl.h"
#include "node_buffer.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "node_process-inl.h"
#include "util-inl.h"
#include <cinttypes>
namespace node {
namespace performance {
using v8::Array;
using v8::Context;
using v8::DontDelete;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::GCCallbackFlags;
using v8::GCType;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::ObjectTemplate;
using v8::PropertyAttribute;
using v8::ReadOnly;
using v8::Value;
// Microseconds in a millisecond, as a float.
#define MICROS_PER_MILLIS 1e3
// Nanoseconds in a millisecond, as a float.
#define NANOS_PER_MILLIS 1e6
const uint64_t performance_process_start = PERFORMANCE_NOW();
const double performance_process_start_timestamp =
GetCurrentTimeInMicroseconds();
uint64_t performance_v8_start;
PerformanceState::PerformanceState(Isolate* isolate,
uint64_t time_origin,
double time_origin_timestamp,
const PerformanceState::SerializeInfo* info)
: root(isolate,
sizeof(performance_state_internal),
MAYBE_FIELD_PTR(info, root)),
milestones(isolate,
offsetof(performance_state_internal, milestones),
NODE_PERFORMANCE_MILESTONE_INVALID,
root,
MAYBE_FIELD_PTR(info, milestones)),
observers(isolate,
offsetof(performance_state_internal, observers),
NODE_PERFORMANCE_ENTRY_TYPE_INVALID,
root,
MAYBE_FIELD_PTR(info, observers)) {
if (info == nullptr) {
// For performance states initialized from scratch, reset
// all the milestones and initialize the time origin.
// For deserialized performance states, we will do the
// initialization in the deserialize callback.
ResetMilestones();
Initialize(time_origin, time_origin_timestamp);
}
}
void PerformanceState::ResetMilestones() {
size_t milestones_length = milestones.Length();
for (size_t i = 0; i < milestones_length; ++i) {
milestones[i] = -1;
}
}
PerformanceState::SerializeInfo PerformanceState::Serialize(
v8::Local<v8::Context> context, v8::SnapshotCreator* creator) {
// Reset all the milestones to improve determinism in the snapshot.
// We'll re-initialize them after deserialization.
ResetMilestones();
SerializeInfo info{root.Serialize(context, creator),
milestones.Serialize(context, creator),
observers.Serialize(context, creator)};
return info;
}
void PerformanceState::Initialize(uint64_t time_origin,
double time_origin_timestamp) {
// We are only reusing the milestone array to store the time origin
// and time origin timestamp, so do not use the Mark() method.
// The time origin milestone is not exposed to user land.
this->milestones[NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN] =
static_cast<double>(time_origin);
this->milestones[NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN_TIMESTAMP] =
time_origin_timestamp;
}
void PerformanceState::Deserialize(v8::Local<v8::Context> context,
uint64_t time_origin,
double time_origin_timestamp) {
// Resets the pointers.
root.Deserialize(context);
milestones.Deserialize(context);
observers.Deserialize(context);
// Re-initialize the time origin and timestamp i.e. the process start time.
Initialize(time_origin, time_origin_timestamp);
}
std::ostream& operator<<(std::ostream& o,
const PerformanceState::SerializeInfo& i) {
o << "{\n"
<< " " << i.root << ", // root\n"
<< " " << i.milestones << ", // milestones\n"
<< " " << i.observers << ", // observers\n"
<< "}";
return o;
}
void PerformanceState::Mark(PerformanceMilestone milestone, uint64_t ts) {
this->milestones[milestone] = static_cast<double>(ts);
TRACE_EVENT_INSTANT_WITH_TIMESTAMP0(
TRACING_CATEGORY_NODE1(bootstrap),
GetPerformanceMilestoneName(milestone),
TRACE_EVENT_SCOPE_THREAD, ts / 1000);
}
void SetupPerformanceObservers(const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);
// TODO(legendecas): Remove this check once the sub-realms are supported.
CHECK_EQ(realm->kind(), Realm::Kind::kPrincipal);
CHECK(args[0]->IsFunction());
realm->set_performance_entry_callback(args[0].As<Function>());
}
// Marks the start of a GC cycle
void MarkGarbageCollectionStart(
Isolate* isolate,
GCType type,
GCCallbackFlags flags,
void* data) {
Environment* env = static_cast<Environment*>(data);
// Prevent gc callback from reentering with different type
// See https://github.com/nodejs/node/issues/44046
if (env->performance_state()->current_gc_type != 0) {
return;
}
env->performance_state()->performance_last_gc_start_mark = PERFORMANCE_NOW();
env->performance_state()->current_gc_type = type;
}
MaybeLocal<Object> GCPerformanceEntryTraits::GetDetails(
Environment* env,
const GCPerformanceEntry& entry) {
Local<Object> obj = Object::New(env->isolate());
if (!obj->Set(
env->context(),
env->kind_string(),
Integer::NewFromUnsigned(
env->isolate(),
entry.details.kind)).IsJust()) {
return MaybeLocal<Object>();
}
if (!obj->Set(
env->context(),
env->flags_string(),
Integer::NewFromUnsigned(
env->isolate(),
entry.details.flags)).IsJust()) {
return MaybeLocal<Object>();
}
return obj;
}
// Marks the end of a GC cycle
void MarkGarbageCollectionEnd(
Isolate* isolate,
GCType type,
GCCallbackFlags flags,
void* data) {
Environment* env = static_cast<Environment*>(data);
PerformanceState* state = env->performance_state();
if (type != state->current_gc_type) {
return;
}
env->performance_state()->current_gc_type = 0;
// If no one is listening to gc performance entries, do not create them.
if (!state->observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]) [[likely]] {
return;
}
double start_time =
(state->performance_last_gc_start_mark - env->time_origin()) /
NANOS_PER_MILLIS;
double duration = (PERFORMANCE_NOW() / NANOS_PER_MILLIS) -
(state->performance_last_gc_start_mark / NANOS_PER_MILLIS);
std::unique_ptr<GCPerformanceEntry> entry =
std::make_unique<GCPerformanceEntry>(
"gc",
start_time,
duration,
GCPerformanceEntry::Details(static_cast<PerformanceGCKind>(type),
static_cast<PerformanceGCFlags>(flags)));
env->SetImmediate([entry = std::move(entry)](Environment* env) {
entry->Notify(env);
}, CallbackFlags::kUnrefed);
}
void GarbageCollectionCleanupHook(void* data) {
Environment* env = static_cast<Environment*>(data);
// Reset current_gc_type to 0
env->performance_state()->current_gc_type = 0;
env->isolate()->RemoveGCPrologueCallback(MarkGarbageCollectionStart, data);
env->isolate()->RemoveGCEpilogueCallback(MarkGarbageCollectionEnd, data);
}
static void InstallGarbageCollectionTracking(
const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
// Reset current_gc_type to 0
env->performance_state()->current_gc_type = 0;
env->isolate()->AddGCPrologueCallback(MarkGarbageCollectionStart,
static_cast<void*>(env));
env->isolate()->AddGCEpilogueCallback(MarkGarbageCollectionEnd,
static_cast<void*>(env));
env->AddCleanupHook(GarbageCollectionCleanupHook, env);
}
static void RemoveGarbageCollectionTracking(
const FunctionCallbackInfo<Value> &args) {
Environment* env = Environment::GetCurrent(args);
env->RemoveCleanupHook(GarbageCollectionCleanupHook, env);
GarbageCollectionCleanupHook(env);
}
// Notify a custom PerformanceEntry to observers
void Notify(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Utf8Value type(env->isolate(), args[0]);
Local<Value> entry = args[1];
PerformanceEntryType entry_type = ToPerformanceEntryTypeEnum(*type);
AliasedUint32Array& observers = env->performance_state()->observers;
if (entry_type != NODE_PERFORMANCE_ENTRY_TYPE_INVALID &&
observers[entry_type]) {
USE(env->performance_entry_callback()->
Call(env->context(), Undefined(env->isolate()), 1, &entry));
}
}
// Return idle time of the event loop
void LoopIdleTime(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
uint64_t idle_time = uv_metrics_idle_time(env->event_loop());
args.GetReturnValue().Set(1.0 * idle_time / NANOS_PER_MILLIS);
}
void UvMetricsInfo(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
uv_metrics_t metrics;
// uv_metrics_info always return 0
CHECK_EQ(uv_metrics_info(env->event_loop(), &metrics), 0);
Local<Value> data[] = {
Integer::New(isolate, metrics.loop_count),
Integer::New(isolate, metrics.events),
Integer::New(isolate, metrics.events_waiting),
};
Local<Array> arr = Array::New(env->isolate(), data, arraysize(data));
args.GetReturnValue().Set(arr);
}
void CreateELDHistogram(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
int64_t interval = args[0].As<Integer>()->Value();
CHECK_GT(interval, 0);
BaseObjectPtr<IntervalHistogram> histogram =
IntervalHistogram::Create(env, interval, [](Histogram& histogram) {
uint64_t delta = histogram.RecordDelta();
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
"delay", delta);
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
"min", histogram.Min());
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
"max", histogram.Max());
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
"mean", histogram.Mean());
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
"stddev", histogram.Stddev());
}, Histogram::Options { 1000 });
args.GetReturnValue().Set(histogram->object());
}
void MarkBootstrapComplete(const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);
CHECK_EQ(realm->kind(), Realm::Kind::kPrincipal);
realm->env()->performance_state()->Mark(
performance::NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
}
static double PerformanceNowImpl() {
return static_cast<double>(uv_hrtime() - performance_process_start) /
NANOS_PER_MILLIS;
}
static double FastPerformanceNow(v8::Local<v8::Value> receiver) {
return PerformanceNowImpl();
}
static void SlowPerformanceNow(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(PerformanceNowImpl());
}
static v8::CFunction fast_performance_now(
v8::CFunction::Make(FastPerformanceNow));
static void CreatePerIsolateProperties(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->isolate();
HistogramBase::Initialize(isolate_data, target);
SetMethod(isolate, target, "setupObservers", SetupPerformanceObservers);
SetMethod(isolate,
target,
"installGarbageCollectionTracking",
InstallGarbageCollectionTracking);
SetMethod(isolate,
target,
"removeGarbageCollectionTracking",
RemoveGarbageCollectionTracking);
SetMethod(isolate, target, "notify", Notify);
SetMethod(isolate, target, "loopIdleTime", LoopIdleTime);
SetMethod(isolate, target, "createELDHistogram", CreateELDHistogram);
SetMethod(isolate, target, "markBootstrapComplete", MarkBootstrapComplete);
SetMethod(isolate, target, "uvMetricsInfo", UvMetricsInfo);
SetFastMethodNoSideEffect(
isolate, target, "now", SlowPerformanceNow, &fast_performance_now);
}
void CreatePerContextProperties(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
PerformanceState* state = env->performance_state();
target->Set(context,
FIXED_ONE_BYTE_STRING(isolate, "observerCounts"),
state->observers.GetJSArray()).Check();
target->Set(context,
FIXED_ONE_BYTE_STRING(isolate, "milestones"),
state->milestones.GetJSArray()).Check();
Local<Object> constants = Object::New(isolate);
NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_MAJOR);
NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_MINOR);
NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_INCREMENTAL);
NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_WEAKCB);
NODE_DEFINE_CONSTANT(
constants, NODE_PERFORMANCE_GC_FLAGS_NO);
NODE_DEFINE_CONSTANT(
constants, NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED);
NODE_DEFINE_CONSTANT(
constants, NODE_PERFORMANCE_GC_FLAGS_FORCED);
NODE_DEFINE_CONSTANT(
constants, NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING);
NODE_DEFINE_CONSTANT(
constants, NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE);
NODE_DEFINE_CONSTANT(
constants, NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY);
NODE_DEFINE_CONSTANT(
constants, NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE);
#define V(name, _) \
NODE_DEFINE_HIDDEN_CONSTANT(constants, NODE_PERFORMANCE_ENTRY_TYPE_##name);
NODE_PERFORMANCE_ENTRY_TYPES(V)
#undef V
#define V(name, _) \
NODE_DEFINE_HIDDEN_CONSTANT(constants, NODE_PERFORMANCE_MILESTONE_##name);
NODE_PERFORMANCE_MILESTONES(V)
#undef V
PropertyAttribute attr =
static_cast<PropertyAttribute>(ReadOnly | DontDelete);
target->DefineOwnProperty(context, env->constants_string(), constants, attr)
.ToChecked();
}
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(SetupPerformanceObservers);
registry->Register(InstallGarbageCollectionTracking);
registry->Register(RemoveGarbageCollectionTracking);
registry->Register(Notify);
registry->Register(LoopIdleTime);
registry->Register(CreateELDHistogram);
registry->Register(MarkBootstrapComplete);
registry->Register(UvMetricsInfo);
registry->Register(SlowPerformanceNow);
registry->Register(FastPerformanceNow);
registry->Register(fast_performance_now.GetTypeInfo());
HistogramBase::RegisterExternalReferences(registry);
IntervalHistogram::RegisterExternalReferences(registry);
}
} // namespace performance
} // namespace node
NODE_BINDING_CONTEXT_AWARE_INTERNAL(
performance, node::performance::CreatePerContextProperties)
NODE_BINDING_PER_ISOLATE_INIT(performance,
node::performance::CreatePerIsolateProperties)
NODE_BINDING_EXTERNAL_REFERENCE(performance,
node::performance::RegisterExternalReferences)