GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_perf.cc Lines: 361 394 91.6 %
Date: 2020-08-17 22:13:26 Branches: 133 202 65.8 %

Line Branch Exec Source
1
#include "aliased_buffer.h"
2
#include "memory_tracker-inl.h"
3
#include "node_internals.h"
4
#include "node_perf.h"
5
#include "node_buffer.h"
6
#include "node_process.h"
7
#include "util-inl.h"
8
9
#include <cinttypes>
10
11
namespace node {
12
namespace performance {
13
14
using v8::Context;
15
using v8::DontDelete;
16
using v8::Function;
17
using v8::FunctionCallbackInfo;
18
using v8::FunctionTemplate;
19
using v8::GCCallbackFlags;
20
using v8::GCType;
21
using v8::HandleScope;
22
using v8::Integer;
23
using v8::Isolate;
24
using v8::Local;
25
using v8::Map;
26
using v8::MaybeLocal;
27
using v8::Number;
28
using v8::Object;
29
using v8::PropertyAttribute;
30
using v8::ReadOnly;
31
using v8::String;
32
using v8::Value;
33
34
// Microseconds in a millisecond, as a float.
35
#define MICROS_PER_MILLIS 1e3
36
37
// https://w3c.github.io/hr-time/#dfn-time-origin
38
4460
const uint64_t timeOrigin = PERFORMANCE_NOW();
39
// https://w3c.github.io/hr-time/#dfn-time-origin-timestamp
40
4460
const double timeOriginTimestamp = GetCurrentTimeInMicroseconds();
41
uint64_t performance_v8_start;
42
43
4776
PerformanceState::PerformanceState(Isolate* isolate,
44
4776
                                   const PerformanceState::SerializeInfo* info)
45
    : root(isolate,
46
           sizeof(performance_state_internal),
47
           MAYBE_FIELD_PTR(info, root)),
48
      milestones(isolate,
49
                 offsetof(performance_state_internal, milestones),
50
                 NODE_PERFORMANCE_MILESTONE_INVALID,
51
                 root,
52
                 MAYBE_FIELD_PTR(info, milestones)),
53
      observers(isolate,
54
                offsetof(performance_state_internal, observers),
55
                NODE_PERFORMANCE_ENTRY_TYPE_INVALID,
56
                root,
57

4776
                MAYBE_FIELD_PTR(info, observers)) {
58
4776
  if (info == nullptr) {
59
387
    for (size_t i = 0; i < milestones.Length(); i++) milestones[i] = -1.;
60
  }
61
4776
}
62
63
1
PerformanceState::SerializeInfo PerformanceState::Serialize(
64
    v8::Local<v8::Context> context, v8::SnapshotCreator* creator) {
65
1
  SerializeInfo info{root.Serialize(context, creator),
66
1
                     milestones.Serialize(context, creator),
67
2
                     observers.Serialize(context, creator)};
68
1
  return info;
69
}
70
71
4389
void PerformanceState::Deserialize(v8::Local<v8::Context> context) {
72
4389
  root.Deserialize(context);
73
  // This is just done to set up the pointers, we will actually reset
74
  // all the milestones after deserialization.
75
4389
  milestones.Deserialize(context);
76
4389
  observers.Deserialize(context);
77
4389
}
78
79
1
std::ostream& operator<<(std::ostream& o,
80
                         const PerformanceState::SerializeInfo& i) {
81
  o << "{\n"
82
1
    << "  " << i.root << ",  // root\n"
83
2
    << "  " << i.milestones << ",  // milestones\n"
84
2
    << "  " << i.observers << ",  // observers\n"
85
1
    << "}";
86
1
  return o;
87
}
88
89
27773
void PerformanceState::Mark(enum PerformanceMilestone milestone,
90
                             uint64_t ts) {
91
27773
  this->milestones[milestone] = ts;
92

55546
  TRACE_EVENT_INSTANT_WITH_TIMESTAMP0(
93
      TRACING_CATEGORY_NODE1(bootstrap),
94
      GetPerformanceMilestoneName(milestone),
95
      TRACE_EVENT_SCOPE_THREAD, ts / 1000);
96
27773
}
97
98
// Initialize the performance entry object properties
99
53
inline void InitObject(const PerformanceEntry& entry, Local<Object> obj) {
100
53
  Environment* env = entry.env();
101
53
  Isolate* isolate = env->isolate();
102
53
  Local<Context> context = env->context();
103
  PropertyAttribute attr =
104
53
      static_cast<PropertyAttribute>(ReadOnly | DontDelete);
105
106
  obj->DefineOwnProperty(context,
106
                         env->name_string(),
107
106
                         String::NewFromUtf8(isolate,
108
53
                                             entry.name().c_str())
109
                             .ToLocalChecked(),
110
159
                         attr)
111
      .Check();
112
106
  obj->DefineOwnProperty(context,
113
                         env->entry_type_string(),
114
106
                         String::NewFromUtf8(isolate,
115
53
                                             entry.type().c_str())
116
                             .ToLocalChecked(),
117
159
                         attr)
118
      .Check();
119
106
  obj->DefineOwnProperty(context,
120
                         env->start_time_string(),
121
                         Number::New(isolate, entry.startTime()),
122
212
                         attr).Check();
123
106
  obj->DefineOwnProperty(context,
124
                         env->duration_string(),
125
                         Number::New(isolate, entry.duration()),
126
212
                         attr).Check();
127
53
}
128
129
// Create a new PerformanceEntry object
130
53
MaybeLocal<Object> PerformanceEntry::ToObject() const {
131
  Local<Object> obj;
132
159
  if (!env_->performance_entry_template()
133
159
           ->NewInstance(env_->context())
134
53
           .ToLocal(&obj)) {
135
    return MaybeLocal<Object>();
136
  }
137
53
  InitObject(*this, obj);
138
53
  return obj;
139
}
140
141
// Allow creating a PerformanceEntry object from JavaScript
142
void PerformanceEntry::New(const FunctionCallbackInfo<Value>& args) {
143
  Environment* env = Environment::GetCurrent(args);
144
  Isolate* isolate = env->isolate();
145
  Utf8Value name(isolate, args[0]);
146
  Utf8Value type(isolate, args[1]);
147
  uint64_t now = PERFORMANCE_NOW();
148
  PerformanceEntry entry(env, *name, *type, now, now);
149
  Local<Object> obj = args.This();
150
  InitObject(entry, obj);
151
  PerformanceEntry::Notify(env, entry.kind(), obj);
152
}
153
154
// Pass the PerformanceEntry object to the PerformanceObservers
155
53
void PerformanceEntry::Notify(Environment* env,
156
                              PerformanceEntryType type,
157
                              Local<Value> object) {
158
53
  Context::Scope scope(env->context());
159
53
  AliasedUint32Array& observers = env->performance_state()->observers;
160

265
  if (!env->performance_entry_callback().IsEmpty() &&
161

159
      type != NODE_PERFORMANCE_ENTRY_TYPE_INVALID && observers[type]) {
162
    node::MakeCallback(env->isolate(),
163
                       object.As<Object>(),
164
                       env->performance_entry_callback(),
165
                       1, &object,
166
54
                       node::async_context{0, 0});
167
  }
168
53
}
169
170
// Create a User Timing Mark
171
22
void Mark(const FunctionCallbackInfo<Value>& args) {
172
22
  Environment* env = Environment::GetCurrent(args);
173
44
  HandleScope scope(env->isolate());
174
44
  Utf8Value name(env->isolate(), args[0]);
175
22
  uint64_t now = PERFORMANCE_NOW();
176
22
  auto marks = env->performance_marks();
177
22
  (*marks)[*name] = now;
178
179

44
  TRACE_EVENT_COPY_MARK_WITH_TIMESTAMP(
180
      TRACING_CATEGORY_NODE2(perf, usertiming),
181
      *name, now / 1000);
182
183
44
  PerformanceEntry entry(env, *name, "mark", now, now);
184
  Local<Object> obj;
185
44
  if (!entry.ToObject().ToLocal(&obj)) return;
186
22
  PerformanceEntry::Notify(env, entry.kind(), obj);
187
44
  args.GetReturnValue().Set(obj);
188
}
189
190
3
void ClearMark(const FunctionCallbackInfo<Value>& args) {
191
3
  Environment* env = Environment::GetCurrent(args);
192
3
  auto marks = env->performance_marks();
193
194
3
  if (args.Length() == 0) {
195
2
    marks->clear();
196
  } else {
197
2
    Utf8Value name(env->isolate(), args[0]);
198
1
    marks->erase(*name);
199
  }
200
3
}
201
202
24
inline uint64_t GetPerformanceMark(Environment* env, const std::string& name) {
203
24
  auto marks = env->performance_marks();
204
24
  auto res = marks->find(name);
205
24
  return res != marks->end() ? res->second : 0;
206
}
207
208
// Create a User Timing Measure. A Measure is a PerformanceEntry that
209
// measures the duration between two distinct user timing marks
210
13
void Measure(const FunctionCallbackInfo<Value>& args) {
211
13
  Environment* env = Environment::GetCurrent(args);
212
26
  HandleScope scope(env->isolate());
213
26
  Utf8Value name(env->isolate(), args[0]);
214
26
  Utf8Value startMark(env->isolate(), args[1]);
215
216
13
  AliasedFloat64Array& milestones = env->performance_state()->milestones;
217
218
13
  uint64_t startTimestamp = timeOrigin;
219
13
  uint64_t start = GetPerformanceMark(env, *startMark);
220
13
  if (start != 0) {
221
7
    startTimestamp = start;
222
  } else {
223
6
    PerformanceMilestone milestone = ToPerformanceMilestoneEnum(*startMark);
224
6
    if (milestone != NODE_PERFORMANCE_MILESTONE_INVALID)
225
      startTimestamp = milestones[milestone];
226
  }
227
228
13
  uint64_t endTimestamp = 0;
229
39
  if (args[2]->IsUndefined()) {
230
2
    endTimestamp = PERFORMANCE_NOW();
231
  } else {
232
22
    Utf8Value endMark(env->isolate(), args[2]);
233
11
    endTimestamp = GetPerformanceMark(env, *endMark);
234
11
    if (endTimestamp == 0) {
235
      PerformanceMilestone milestone = ToPerformanceMilestoneEnum(*endMark);
236
      if (milestone != NODE_PERFORMANCE_MILESTONE_INVALID)
237
        endTimestamp = milestones[milestone];
238
    }
239
  }
240
241
13
  if (endTimestamp < startTimestamp)
242
    endTimestamp = startTimestamp;
243
244

26
  TRACE_EVENT_COPY_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
245
      TRACING_CATEGORY_NODE2(perf, usertiming),
246
      *name, *name, startTimestamp / 1000);
247

26
  TRACE_EVENT_COPY_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(
248
      TRACING_CATEGORY_NODE2(perf, usertiming),
249
      *name, *name, endTimestamp / 1000);
250
251
26
  PerformanceEntry entry(env, *name, "measure", startTimestamp, endTimestamp);
252
  Local<Object> obj;
253
26
  if (!entry.ToObject().ToLocal(&obj)) return;
254
13
  PerformanceEntry::Notify(env, entry.kind(), obj);
255
26
  args.GetReturnValue().Set(obj);
256
}
257
258
// Allows specific Node.js lifecycle milestones to be set from JavaScript
259
void MarkMilestone(const FunctionCallbackInfo<Value>& args) {
260
  Environment* env = Environment::GetCurrent(args);
261
  Local<Context> context = env->context();
262
  PerformanceMilestone milestone =
263
      static_cast<PerformanceMilestone>(
264
          args[0]->Int32Value(context).ToChecked());
265
  if (milestone != NODE_PERFORMANCE_MILESTONE_INVALID)
266
    env->performance_state()->Mark(milestone);
267
}
268
269
270
204
void SetupPerformanceObservers(const FunctionCallbackInfo<Value>& args) {
271
204
  Environment* env = Environment::GetCurrent(args);
272
408
  CHECK(args[0]->IsFunction());
273
408
  env->set_performance_entry_callback(args[0].As<Function>());
274
204
}
275
276
// Creates a GC Performance Entry and passes it to observers
277
1
void PerformanceGCCallback(Environment* env,
278
                           std::unique_ptr<GCPerformanceEntry> entry) {
279
2
  HandleScope scope(env->isolate());
280
1
  Local<Context> context = env->context();
281
282
1
  AliasedUint32Array& observers = env->performance_state()->observers;
283
1
  if (observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]) {
284
    Local<Object> obj;
285
2
    if (!entry->ToObject().ToLocal(&obj)) return;
286
    PropertyAttribute attr =
287
1
        static_cast<PropertyAttribute>(ReadOnly | DontDelete);
288
2
    obj->DefineOwnProperty(context,
289
                           env->kind_string(),
290
1
                           Integer::New(env->isolate(), entry->gckind()),
291
4
                           attr).Check();
292
2
    obj->DefineOwnProperty(context,
293
                           env->flags_string(),
294
1
                           Integer::New(env->isolate(), entry->gcflags()),
295
4
                           attr).Check();
296
1
    PerformanceEntry::Notify(env, entry->kind(), obj);
297
  }
298
}
299
300
// Marks the start of a GC cycle
301
1
void MarkGarbageCollectionStart(Isolate* isolate,
302
                                GCType type,
303
                                GCCallbackFlags flags,
304
                                void* data) {
305
1
  Environment* env = static_cast<Environment*>(data);
306
1
  env->performance_state()->performance_last_gc_start_mark = PERFORMANCE_NOW();
307
1
}
308
309
// Marks the end of a GC cycle
310
1
void MarkGarbageCollectionEnd(Isolate* isolate,
311
                              GCType type,
312
                              GCCallbackFlags flags,
313
                              void* data) {
314
1
  Environment* env = static_cast<Environment*>(data);
315
1
  PerformanceState* state = env->performance_state();
316
  // If no one is listening to gc performance entries, do not create them.
317
1
  if (!state->observers[NODE_PERFORMANCE_ENTRY_TYPE_GC])
318
    return;
319
  auto entry = std::make_unique<GCPerformanceEntry>(
320
      env,
321
2
      static_cast<PerformanceGCKind>(type),
322
2
      static_cast<PerformanceGCFlags>(flags),
323
      state->performance_last_gc_start_mark,
324
4
      PERFORMANCE_NOW());
325
6
  env->SetImmediate([entry = std::move(entry)](Environment* env) mutable {
326
1
    PerformanceGCCallback(env, std::move(entry));
327
2
  }, CallbackFlags::kUnrefed);
328
}
329
330
1
void GarbageCollectionCleanupHook(void* data) {
331
1
  Environment* env = static_cast<Environment*>(data);
332
1
  env->isolate()->RemoveGCPrologueCallback(MarkGarbageCollectionStart, data);
333
1
  env->isolate()->RemoveGCEpilogueCallback(MarkGarbageCollectionEnd, data);
334
1
}
335
336
1
static void InstallGarbageCollectionTracking(
337
    const FunctionCallbackInfo<Value>& args) {
338
1
  Environment* env = Environment::GetCurrent(args);
339
340
1
  env->isolate()->AddGCPrologueCallback(MarkGarbageCollectionStart,
341
1
                                        static_cast<void*>(env));
342
1
  env->isolate()->AddGCEpilogueCallback(MarkGarbageCollectionEnd,
343
1
                                        static_cast<void*>(env));
344
1
  env->AddCleanupHook(GarbageCollectionCleanupHook, env);
345
1
}
346
347
1
static void RemoveGarbageCollectionTracking(
348
  const FunctionCallbackInfo<Value> &args) {
349
1
  Environment* env = Environment::GetCurrent(args);
350
351
1
  env->RemoveCleanupHook(GarbageCollectionCleanupHook, env);
352
1
  GarbageCollectionCleanupHook(env);
353
1
}
354
355
// Gets the name of a function
356
7
inline Local<Value> GetName(Local<Function> fn) {
357
7
  Local<Value> val = fn->GetDebugName();
358

21
  if (val.IsEmpty() || val->IsUndefined()) {
359
    Local<Value> boundFunction = fn->GetBoundFunction();
360
    if (!boundFunction.IsEmpty() && !boundFunction->IsUndefined()) {
361
      val = GetName(boundFunction.As<Function>());
362
    }
363
  }
364
7
  return val;
365
}
366
367
// Executes a wrapped Function and captures timing information, causing a
368
// Function PerformanceEntry to be emitted to PerformanceObservers after
369
// execution.
370
7
void TimerFunctionCall(const FunctionCallbackInfo<Value>& args) {
371
7
  Isolate* isolate = args.GetIsolate();
372
7
  Local<Context> context = isolate->GetCurrentContext();
373
7
  Environment* env = Environment::GetCurrent(context);
374
7
  CHECK_NOT_NULL(env);
375
14
  Local<Function> fn = args.Data().As<Function>();
376
7
  size_t count = args.Length();
377
  size_t idx;
378
9
  SlicedArguments call_args(args);
379
9
  Utf8Value name(isolate, GetName(fn));
380
7
  bool is_construct_call = args.IsConstructCall();
381
382
7
  uint64_t start = PERFORMANCE_NOW();
383

14
  TRACE_EVENT_COPY_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(
384
      TRACING_CATEGORY_NODE2(perf, timerify),
385
      *name, *name, start / 1000);
386
  v8::MaybeLocal<Value> ret;
387
388
7
  if (is_construct_call) {
389
4
    ret = fn->NewInstance(context, call_args.length(), call_args.out())
390
2
        .FromMaybe(Local<Object>());
391
  } else {
392
12
    ret = fn->Call(context, args.This(), call_args.length(), call_args.out());
393
  }
394
395
7
  uint64_t end = PERFORMANCE_NOW();
396

14
  TRACE_EVENT_COPY_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(
397
      TRACING_CATEGORY_NODE2(perf, timerify),
398
      *name, *name, end / 1000);
399
400
7
  if (ret.IsEmpty())
401
1
    return;
402
12
  args.GetReturnValue().Set(ret.ToLocalChecked());
403
404
6
  AliasedUint32Array& observers = env->performance_state()->observers;
405
6
  if (!observers[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION])
406
4
    return;
407
408
4
  PerformanceEntry entry(env, *name, "function", start, end);
409
  Local<Object> obj;
410
4
  if (!entry.ToObject().ToLocal(&obj)) return;
411
4
  for (idx = 0; idx < count; idx++)
412
8
    obj->Set(context, idx, args[idx]).Check();
413
2
  PerformanceEntry::Notify(env, entry.kind(), obj);
414
}
415
416
// Wraps a Function in a TimerFunctionCall
417
8
void Timerify(const FunctionCallbackInfo<Value>& args) {
418
8
  Environment* env = Environment::GetCurrent(args);
419
8
  Local<Context> context = env->context();
420
16
  CHECK(args[0]->IsFunction());
421
16
  CHECK(args[1]->IsNumber());
422
16
  Local<Function> fn = args[0].As<Function>();
423
24
  int length = args[1]->IntegerValue(context).ToChecked();
424
  Local<Function> wrap =
425
16
      Function::New(context, TimerFunctionCall, fn, length).ToLocalChecked();
426
16
  args.GetReturnValue().Set(wrap);
427
8
}
428
429
// Notify a custom PerformanceEntry to observers
430
2
void Notify(const FunctionCallbackInfo<Value>& args) {
431
2
  Environment* env = Environment::GetCurrent(args);
432
4
  Utf8Value type(env->isolate(), args[0]);
433
2
  Local<Value> entry = args[1];
434
2
  PerformanceEntryType entry_type = ToPerformanceEntryTypeEnum(*type);
435
2
  AliasedUint32Array& observers = env->performance_state()->observers;
436

6
  if (entry_type != NODE_PERFORMANCE_ENTRY_TYPE_INVALID &&
437
4
      observers[entry_type]) {
438
4
    USE(env->performance_entry_callback()->
439
8
      Call(env->context(), Undefined(env->isolate()), 1, &entry));
440
  }
441
2
}
442
443
444
// Event Loop Timing Histogram
445
namespace {
446
2
static void ELDHistogramMin(const FunctionCallbackInfo<Value>& args) {
447
  ELDHistogram* histogram;
448
2
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
449
2
  double value = static_cast<double>(histogram->Min());
450
4
  args.GetReturnValue().Set(value);
451
}
452
453
2
static void ELDHistogramMax(const FunctionCallbackInfo<Value>& args) {
454
  ELDHistogram* histogram;
455
2
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
456
2
  double value = static_cast<double>(histogram->Max());
457
4
  args.GetReturnValue().Set(value);
458
}
459
460
2
static void ELDHistogramMean(const FunctionCallbackInfo<Value>& args) {
461
  ELDHistogram* histogram;
462
2
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
463
6
  args.GetReturnValue().Set(histogram->Mean());
464
}
465
466
static void ELDHistogramExceeds(const FunctionCallbackInfo<Value>& args) {
467
  ELDHistogram* histogram;
468
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
469
  double value = static_cast<double>(histogram->Exceeds());
470
  args.GetReturnValue().Set(value);
471
}
472
473
2
static void ELDHistogramStddev(const FunctionCallbackInfo<Value>& args) {
474
  ELDHistogram* histogram;
475
2
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
476
6
  args.GetReturnValue().Set(histogram->Stddev());
477
}
478
479
991
static void ELDHistogramPercentile(const FunctionCallbackInfo<Value>& args) {
480
  ELDHistogram* histogram;
481
991
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
482
1982
  CHECK(args[0]->IsNumber());
483
2973
  double percentile = args[0].As<Number>()->Value();
484
2973
  args.GetReturnValue().Set(histogram->Percentile(percentile));
485
}
486
487
2
static void ELDHistogramPercentiles(const FunctionCallbackInfo<Value>& args) {
488
2
  Environment* env = Environment::GetCurrent(args);
489
  ELDHistogram* histogram;
490
2
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
491
4
  CHECK(args[0]->IsMap());
492
4
  Local<Map> map = args[0].As<Map>();
493
18
  histogram->Percentiles([&](double key, double value) {
494
84
    map->Set(env->context(),
495
             Number::New(env->isolate(), key),
496
70
             Number::New(env->isolate(), value)).IsEmpty();
497
16
  });
498
}
499
500
3
static void ELDHistogramEnable(const FunctionCallbackInfo<Value>& args) {
501
  ELDHistogram* histogram;
502
3
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
503
9
  args.GetReturnValue().Set(histogram->Enable());
504
}
505
506
3
static void ELDHistogramDisable(const FunctionCallbackInfo<Value>& args) {
507
  ELDHistogram* histogram;
508
3
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
509
9
  args.GetReturnValue().Set(histogram->Disable());
510
}
511
512
2
static void ELDHistogramReset(const FunctionCallbackInfo<Value>& args) {
513
  ELDHistogram* histogram;
514
2
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
515
2
  histogram->ResetState();
516
}
517
518
2
static void ELDHistogramNew(const FunctionCallbackInfo<Value>& args) {
519
2
  Environment* env = Environment::GetCurrent(args);
520
2
  CHECK(args.IsConstructCall());
521
8
  int32_t resolution = args[0]->IntegerValue(env->context()).FromJust();
522
2
  CHECK_GT(resolution, 0);
523
2
  new ELDHistogram(env, args.This(), resolution);
524
2
}
525
}  // namespace
526
527
2
ELDHistogram::ELDHistogram(
528
    Environment* env,
529
    Local<Object> wrap,
530
2
    int32_t resolution) : HandleWrap(env,
531
                                     wrap,
532
2
                                     reinterpret_cast<uv_handle_t*>(&timer_),
533
                                     AsyncWrap::PROVIDER_ELDHISTOGRAM),
534
                          Histogram(1, 3.6e12),
535
2
                          resolution_(resolution) {
536
2
  MakeWeak();
537
2
  uv_timer_init(env->event_loop(), &timer_);
538
2
}
539
540
1836
void ELDHistogram::DelayIntervalCallback(uv_timer_t* req) {
541
1836
  ELDHistogram* histogram = ContainerOf(&ELDHistogram::timer_, req);
542
1836
  histogram->RecordDelta();
543

3672
  TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
544
                 "min", histogram->Min());
545

3672
  TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
546
                 "max", histogram->Max());
547

3672
  TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
548
                 "mean", histogram->Mean());
549

3672
  TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
550
                 "stddev", histogram->Stddev());
551
1836
}
552
553
1836
bool ELDHistogram::RecordDelta() {
554
1836
  uint64_t time = uv_hrtime();
555
1836
  bool ret = true;
556
1836
  if (prev_ > 0) {
557
1835
    int64_t delta = time - prev_;
558
1835
    if (delta > 0) {
559
1835
      ret = Record(delta);
560

3670
      TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
561
                     "delay", delta);
562
1835
      if (!ret) {
563
        if (exceeds_ < 0xFFFFFFFF)
564
          exceeds_++;
565
        ProcessEmitWarning(
566
            env(),
567
            "Event loop delay exceeded 1 hour: %" PRId64 " nanoseconds",
568
            delta);
569
      }
570
    }
571
  }
572
1836
  prev_ = time;
573
1836
  return ret;
574
}
575
576
3
bool ELDHistogram::Enable() {
577

3
  if (enabled_ || IsHandleClosing()) return false;
578
2
  enabled_ = true;
579
2
  prev_ = 0;
580
4
  uv_timer_start(&timer_,
581
                 DelayIntervalCallback,
582
2
                 resolution_,
583
4
                 resolution_);
584
2
  uv_unref(reinterpret_cast<uv_handle_t*>(&timer_));
585
2
  return true;
586
}
587
588
3
bool ELDHistogram::Disable() {
589

3
  if (!enabled_ || IsHandleClosing()) return false;
590
2
  enabled_ = false;
591
2
  uv_timer_stop(&timer_);
592
2
  return true;
593
}
594
595
891
void Initialize(Local<Object> target,
596
                Local<Value> unused,
597
                Local<Context> context,
598
                void* priv) {
599
891
  Environment* env = Environment::GetCurrent(context);
600
891
  Isolate* isolate = env->isolate();
601
891
  PerformanceState* state = env->performance_state();
602
603
1782
  target->Set(context,
604
              FIXED_ONE_BYTE_STRING(isolate, "observerCounts"),
605
3564
              state->observers.GetJSArray()).Check();
606
1782
  target->Set(context,
607
              FIXED_ONE_BYTE_STRING(isolate, "milestones"),
608
3564
              state->milestones.GetJSArray()).Check();
609
610
  Local<String> performanceEntryString =
611
891
      FIXED_ONE_BYTE_STRING(isolate, "PerformanceEntry");
612
613
891
  Local<FunctionTemplate> pe = FunctionTemplate::New(isolate);
614
891
  pe->SetClassName(performanceEntryString);
615
1782
  Local<Function> fn = pe->GetFunction(context).ToLocalChecked();
616
1782
  target->Set(context, performanceEntryString, fn).Check();
617
891
  env->set_performance_entry_template(fn);
618
619
891
  env->SetMethod(target, "clearMark", ClearMark);
620
891
  env->SetMethod(target, "mark", Mark);
621
891
  env->SetMethod(target, "measure", Measure);
622
891
  env->SetMethod(target, "markMilestone", MarkMilestone);
623
891
  env->SetMethod(target, "setupObservers", SetupPerformanceObservers);
624
891
  env->SetMethod(target, "timerify", Timerify);
625
  env->SetMethod(target,
626
                 "installGarbageCollectionTracking",
627
891
                 InstallGarbageCollectionTracking);
628
  env->SetMethod(target,
629
                 "removeGarbageCollectionTracking",
630
891
                 RemoveGarbageCollectionTracking);
631
891
  env->SetMethod(target, "notify", Notify);
632
633
891
  Local<Object> constants = Object::New(isolate);
634
635
3564
  NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_MAJOR);
636
3564
  NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_MINOR);
637
3564
  NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_INCREMENTAL);
638
3564
  NODE_DEFINE_CONSTANT(constants, NODE_PERFORMANCE_GC_WEAKCB);
639
640
3564
  NODE_DEFINE_CONSTANT(
641
    constants, NODE_PERFORMANCE_GC_FLAGS_NO);
642
3564
  NODE_DEFINE_CONSTANT(
643
    constants, NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED);
644
3564
  NODE_DEFINE_CONSTANT(
645
    constants, NODE_PERFORMANCE_GC_FLAGS_FORCED);
646
3564
  NODE_DEFINE_CONSTANT(
647
    constants, NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING);
648
3564
  NODE_DEFINE_CONSTANT(
649
    constants, NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE);
650
3564
  NODE_DEFINE_CONSTANT(
651
    constants, NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY);
652
3564
  NODE_DEFINE_CONSTANT(
653
    constants, NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE);
654
655
#define V(name, _)                                                            \
656
  NODE_DEFINE_HIDDEN_CONSTANT(constants, NODE_PERFORMANCE_ENTRY_TYPE_##name);
657
24948
  NODE_PERFORMANCE_ENTRY_TYPES(V)
658
#undef V
659
660
#define V(name, _)                                                            \
661
  NODE_DEFINE_HIDDEN_CONSTANT(constants, NODE_PERFORMANCE_MILESTONE_##name);
662
21384
  NODE_PERFORMANCE_MILESTONES(V)
663
#undef V
664
665
  PropertyAttribute attr =
666
891
      static_cast<PropertyAttribute>(ReadOnly | DontDelete);
667
668
1782
  target->DefineOwnProperty(context,
669
                            FIXED_ONE_BYTE_STRING(isolate, "timeOrigin"),
670
                            Number::New(isolate, timeOrigin / 1e6),
671
3564
                            attr).ToChecked();
672
673
1782
  target->DefineOwnProperty(
674
      context,
675
      FIXED_ONE_BYTE_STRING(isolate, "timeOriginTimestamp"),
676
      Number::New(isolate, timeOriginTimestamp / MICROS_PER_MILLIS),
677
3564
      attr).ToChecked();
678
679
1782
  target->DefineOwnProperty(context,
680
                            env->constants_string(),
681
                            constants,
682
2673
                            attr).ToChecked();
683
684
891
  Local<String> eldh_classname = FIXED_ONE_BYTE_STRING(isolate, "ELDHistogram");
685
  Local<FunctionTemplate> eldh =
686
891
      env->NewFunctionTemplate(ELDHistogramNew);
687
891
  eldh->SetClassName(eldh_classname);
688
2673
  eldh->InstanceTemplate()->SetInternalFieldCount(
689
891
      ELDHistogram::kInternalFieldCount);
690
1782
  eldh->Inherit(BaseObject::GetConstructorTemplate(env));
691
891
  env->SetProtoMethod(eldh, "exceeds", ELDHistogramExceeds);
692
891
  env->SetProtoMethod(eldh, "min", ELDHistogramMin);
693
891
  env->SetProtoMethod(eldh, "max", ELDHistogramMax);
694
891
  env->SetProtoMethod(eldh, "mean", ELDHistogramMean);
695
891
  env->SetProtoMethod(eldh, "stddev", ELDHistogramStddev);
696
891
  env->SetProtoMethod(eldh, "percentile", ELDHistogramPercentile);
697
891
  env->SetProtoMethod(eldh, "percentiles", ELDHistogramPercentiles);
698
891
  env->SetProtoMethod(eldh, "enable", ELDHistogramEnable);
699
891
  env->SetProtoMethod(eldh, "disable", ELDHistogramDisable);
700
891
  env->SetProtoMethod(eldh, "reset", ELDHistogramReset);
701
1782
  target->Set(context, eldh_classname,
702
3564
              eldh->GetFunction(env->context()).ToLocalChecked()).Check();
703
891
}
704
705
}  // namespace performance
706
}  // namespace node
707
708

17839
NODE_MODULE_CONTEXT_AWARE_INTERNAL(performance, node::performance::Initialize)