GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: histogram.cc Lines: 66 221 29.9 %
Date: 2021-09-21 04:13:38 Branches: 2 92 2.2 %

Line Branch Exec Source
1
#include "histogram.h"  // NOLINT(build/include_inline)
2
#include "base_object-inl.h"
3
#include "histogram-inl.h"
4
#include "memory_tracker-inl.h"
5
#include "node_errors.h"
6
#include "node_external_reference.h"
7
8
namespace node {
9
10
using v8::BigInt;
11
using v8::FunctionCallbackInfo;
12
using v8::FunctionTemplate;
13
using v8::Local;
14
using v8::Map;
15
using v8::Number;
16
using v8::Object;
17
using v8::String;
18
using v8::Value;
19
20
Histogram::Histogram(int64_t lowest, int64_t highest, int figures) {
21
  hdr_histogram* histogram;
22
  CHECK_EQ(0, hdr_init(lowest, highest, figures, &histogram));
23
  histogram_.reset(histogram);
24
}
25
26
void Histogram::MemoryInfo(MemoryTracker* tracker) const {
27
  tracker->TrackFieldWithSize("histogram", GetMemorySize());
28
}
29
30
HistogramImpl::HistogramImpl(int64_t lowest, int64_t highest, int figures)
31
    : histogram_(new Histogram(lowest, highest, figures)) {}
32
33
HistogramImpl::HistogramImpl(std::shared_ptr<Histogram> histogram)
34
    : histogram_(std::move(histogram)) {}
35
36
HistogramBase::HistogramBase(
37
    Environment* env,
38
    Local<Object> wrap,
39
    int64_t lowest,
40
    int64_t highest,
41
    int figures)
42
    : BaseObject(env, wrap),
43
      HistogramImpl(lowest, highest, figures) {
44
  MakeWeak();
45
}
46
47
HistogramBase::HistogramBase(
48
    Environment* env,
49
    Local<Object> wrap,
50
    std::shared_ptr<Histogram> histogram)
51
    : BaseObject(env, wrap),
52
      HistogramImpl(std::move(histogram)) {
53
  MakeWeak();
54
}
55
56
void HistogramBase::MemoryInfo(MemoryTracker* tracker) const {
57
  tracker->TrackField("histogram", histogram());
58
}
59
60
void HistogramBase::GetMin(const FunctionCallbackInfo<Value>& args) {
61
  HistogramBase* histogram;
62
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
63
  double value = static_cast<double>((*histogram)->Min());
64
  args.GetReturnValue().Set(value);
65
}
66
67
void HistogramBase::GetMax(const FunctionCallbackInfo<Value>& args) {
68
  HistogramBase* histogram;
69
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
70
  double value = static_cast<double>((*histogram)->Max());
71
  args.GetReturnValue().Set(value);
72
}
73
74
void HistogramBase::GetMean(const FunctionCallbackInfo<Value>& args) {
75
  HistogramBase* histogram;
76
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
77
  args.GetReturnValue().Set((*histogram)->Mean());
78
}
79
80
void HistogramBase::GetExceeds(const FunctionCallbackInfo<Value>& args) {
81
  HistogramBase* histogram;
82
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
83
  double value = static_cast<double>((*histogram)->Exceeds());
84
  args.GetReturnValue().Set(value);
85
}
86
87
void HistogramBase::GetStddev(const FunctionCallbackInfo<Value>& args) {
88
  HistogramBase* histogram;
89
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
90
  args.GetReturnValue().Set((*histogram)->Stddev());
91
}
92
93
void HistogramBase::GetPercentile(const FunctionCallbackInfo<Value>& args) {
94
  HistogramBase* histogram;
95
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
96
  CHECK(args[0]->IsNumber());
97
  double percentile = args[0].As<Number>()->Value();
98
  args.GetReturnValue().Set((*histogram)->Percentile(percentile));
99
}
100
101
void HistogramBase::GetPercentiles(const FunctionCallbackInfo<Value>& args) {
102
  Environment* env = Environment::GetCurrent(args);
103
  HistogramBase* histogram;
104
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
105
  CHECK(args[0]->IsMap());
106
  Local<Map> map = args[0].As<Map>();
107
  (*histogram)->Percentiles([map, env](double key, double value) {
108
    map->Set(
109
        env->context(),
110
        Number::New(env->isolate(), key),
111
        Number::New(env->isolate(), value)).IsEmpty();
112
  });
113
}
114
115
void HistogramBase::DoReset(const FunctionCallbackInfo<Value>& args) {
116
  HistogramBase* histogram;
117
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
118
  (*histogram)->Reset();
119
}
120
121
void HistogramBase::RecordDelta(const FunctionCallbackInfo<Value>& args) {
122
  HistogramBase* histogram;
123
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
124
  (*histogram)->RecordDelta();
125
}
126
127
void HistogramBase::Record(const FunctionCallbackInfo<Value>& args) {
128
  Environment* env = Environment::GetCurrent(args);
129
  CHECK_IMPLIES(!args[0]->IsNumber(), args[0]->IsBigInt());
130
  bool lossless = true;
131
  int64_t value = args[0]->IsBigInt()
132
      ? args[0].As<BigInt>()->Int64Value(&lossless)
133
      : static_cast<int64_t>(args[0].As<Number>()->Value());
134
  if (!lossless || value < 1)
135
    return THROW_ERR_OUT_OF_RANGE(env, "value is out of range");
136
  HistogramBase* histogram;
137
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
138
  (*histogram)->Record(value);
139
}
140
141
BaseObjectPtr<HistogramBase> HistogramBase::Create(
142
    Environment* env,
143
    int64_t lowest,
144
    int64_t highest,
145
    int figures) {
146
  Local<Object> obj;
147
  if (!GetConstructorTemplate(env)
148
          ->InstanceTemplate()
149
          ->NewInstance(env->context()).ToLocal(&obj)) {
150
    return BaseObjectPtr<HistogramBase>();
151
  }
152
153
  return MakeBaseObject<HistogramBase>(
154
      env, obj, lowest, highest, figures);
155
}
156
157
BaseObjectPtr<HistogramBase> HistogramBase::Create(
158
    Environment* env,
159
    std::shared_ptr<Histogram> histogram) {
160
  Local<Object> obj;
161
  if (!GetConstructorTemplate(env)
162
          ->InstanceTemplate()
163
          ->NewInstance(env->context()).ToLocal(&obj)) {
164
    return BaseObjectPtr<HistogramBase>();
165
  }
166
  return MakeBaseObject<HistogramBase>(env, obj, std::move(histogram));
167
}
168
169
void HistogramBase::New(const FunctionCallbackInfo<Value>& args) {
170
  CHECK(args.IsConstructCall());
171
  Environment* env = Environment::GetCurrent(args);
172
  new HistogramBase(env, args.This());
173
}
174
175
98
Local<FunctionTemplate> HistogramBase::GetConstructorTemplate(
176
    Environment* env) {
177
98
  Local<FunctionTemplate> tmpl = env->histogram_ctor_template();
178
98
  if (tmpl.IsEmpty()) {
179
98
    tmpl = env->NewFunctionTemplate(New);
180
    Local<String> classname =
181
98
        FIXED_ONE_BYTE_STRING(env->isolate(), "Histogram");
182
98
    tmpl->SetClassName(classname);
183
98
    tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
184
185
196
    tmpl->InstanceTemplate()->SetInternalFieldCount(
186
        HistogramBase::kInternalFieldCount);
187
98
    env->SetProtoMethodNoSideEffect(tmpl, "exceeds", GetExceeds);
188
98
    env->SetProtoMethodNoSideEffect(tmpl, "min", GetMin);
189
98
    env->SetProtoMethodNoSideEffect(tmpl, "max", GetMax);
190
98
    env->SetProtoMethodNoSideEffect(tmpl, "mean", GetMean);
191
98
    env->SetProtoMethodNoSideEffect(tmpl, "stddev", GetStddev);
192
98
    env->SetProtoMethodNoSideEffect(tmpl, "percentile", GetPercentile);
193
98
    env->SetProtoMethodNoSideEffect(tmpl, "percentiles", GetPercentiles);
194
98
    env->SetProtoMethod(tmpl, "reset", DoReset);
195
98
    env->SetProtoMethod(tmpl, "record", Record);
196
98
    env->SetProtoMethod(tmpl, "recordDelta", RecordDelta);
197
98
    env->set_histogram_ctor_template(tmpl);
198
  }
199
98
  return tmpl;
200
}
201
202
2612
void HistogramBase::RegisterExternalReferences(
203
    ExternalReferenceRegistry* registry) {
204
2612
  registry->Register(New);
205
2612
  registry->Register(GetExceeds);
206
2612
  registry->Register(GetMin);
207
2612
  registry->Register(GetMax);
208
2612
  registry->Register(GetMean);
209
2612
  registry->Register(GetStddev);
210
2612
  registry->Register(GetPercentile);
211
2612
  registry->Register(GetPercentiles);
212
2612
  registry->Register(DoReset);
213
2612
  registry->Register(Record);
214
2612
  registry->Register(RecordDelta);
215
2612
}
216
217
98
void HistogramBase::Initialize(Environment* env, Local<Object> target) {
218
98
  env->SetConstructorFunction(target, "Histogram", GetConstructorTemplate(env));
219
98
}
220
221
BaseObjectPtr<BaseObject> HistogramBase::HistogramTransferData::Deserialize(
222
    Environment* env,
223
    v8::Local<v8::Context> context,
224
    std::unique_ptr<worker::TransferData> self) {
225
  return Create(env, std::move(histogram_));
226
}
227
228
std::unique_ptr<worker::TransferData> HistogramBase::CloneForMessaging() const {
229
  return std::make_unique<HistogramTransferData>(this);
230
}
231
232
void HistogramBase::HistogramTransferData::MemoryInfo(
233
    MemoryTracker* tracker) const {
234
  tracker->TrackField("histogram", histogram_);
235
}
236
237
98
Local<FunctionTemplate> IntervalHistogram::GetConstructorTemplate(
238
    Environment* env) {
239
98
  Local<FunctionTemplate> tmpl = env->intervalhistogram_constructor_template();
240
98
  if (tmpl.IsEmpty()) {
241
98
    tmpl = FunctionTemplate::New(env->isolate());
242
98
    tmpl->Inherit(HandleWrap::GetConstructorTemplate(env));
243
196
    tmpl->InstanceTemplate()->SetInternalFieldCount(
244
        HistogramBase::kInternalFieldCount);
245
98
    env->SetProtoMethodNoSideEffect(tmpl, "exceeds", GetExceeds);
246
98
    env->SetProtoMethodNoSideEffect(tmpl, "min", GetMin);
247
98
    env->SetProtoMethodNoSideEffect(tmpl, "max", GetMax);
248
98
    env->SetProtoMethodNoSideEffect(tmpl, "mean", GetMean);
249
98
    env->SetProtoMethodNoSideEffect(tmpl, "stddev", GetStddev);
250
98
    env->SetProtoMethodNoSideEffect(tmpl, "percentile", GetPercentile);
251
98
    env->SetProtoMethodNoSideEffect(tmpl, "percentiles", GetPercentiles);
252
98
    env->SetProtoMethod(tmpl, "reset", DoReset);
253
98
    env->SetProtoMethod(tmpl, "start", Start);
254
98
    env->SetProtoMethod(tmpl, "stop", Stop);
255
98
    env->set_intervalhistogram_constructor_template(tmpl);
256
  }
257
98
  return tmpl;
258
}
259
260
2612
void IntervalHistogram::RegisterExternalReferences(
261
    ExternalReferenceRegistry* registry) {
262
2612
  registry->Register(GetExceeds);
263
2612
  registry->Register(GetMin);
264
2612
  registry->Register(GetMax);
265
2612
  registry->Register(GetMean);
266
2612
  registry->Register(GetStddev);
267
2612
  registry->Register(GetPercentile);
268
2612
  registry->Register(GetPercentiles);
269
2612
  registry->Register(DoReset);
270
2612
  registry->Register(Start);
271
2612
  registry->Register(Stop);
272
2612
}
273
274
IntervalHistogram::IntervalHistogram(
275
    Environment* env,
276
    Local<Object> wrap,
277
    AsyncWrap::ProviderType type,
278
    int32_t interval,
279
    int64_t lowest,
280
    int64_t highest,
281
    int figures)
282
    : HandleWrap(
283
          env,
284
          wrap,
285
          reinterpret_cast<uv_handle_t*>(&timer_),
286
          type),
287
      HistogramImpl(lowest, highest, figures),
288
      interval_(interval) {
289
  MakeWeak();
290
  uv_timer_init(env->event_loop(), &timer_);
291
}
292
293
void IntervalHistogram::TimerCB(uv_timer_t* handle) {
294
  IntervalHistogram* histogram =
295
      ContainerOf(&IntervalHistogram::timer_, handle);
296
  histogram->OnInterval();
297
}
298
299
void IntervalHistogram::MemoryInfo(MemoryTracker* tracker) const {
300
  tracker->TrackField("histogram", histogram());
301
}
302
303
void IntervalHistogram::OnStart(StartFlags flags) {
304
  if (enabled_ || IsHandleClosing()) return;
305
  enabled_ = true;
306
  if (flags == StartFlags::RESET)
307
    histogram()->Reset();
308
  uv_timer_start(&timer_, TimerCB, interval_, interval_);
309
  uv_unref(reinterpret_cast<uv_handle_t*>(&timer_));
310
}
311
312
void IntervalHistogram::OnStop() {
313
  if (!enabled_ || IsHandleClosing()) return;
314
  enabled_ = false;
315
  uv_timer_stop(&timer_);
316
}
317
318
void IntervalHistogram::Start(const FunctionCallbackInfo<Value>& args) {
319
  IntervalHistogram* histogram;
320
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
321
  histogram->OnStart(args[0]->IsTrue() ? StartFlags::RESET : StartFlags::NONE);
322
}
323
324
void IntervalHistogram::Stop(const FunctionCallbackInfo<Value>& args) {
325
  IntervalHistogram* histogram;
326
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
327
  histogram->OnStop();
328
}
329
330
void IntervalHistogram::GetMin(const FunctionCallbackInfo<Value>& args) {
331
  IntervalHistogram* histogram;
332
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
333
  double value = static_cast<double>((*histogram)->Min());
334
  args.GetReturnValue().Set(value);
335
}
336
337
void IntervalHistogram::GetMax(const FunctionCallbackInfo<Value>& args) {
338
  IntervalHistogram* histogram;
339
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
340
  double value = static_cast<double>((*histogram)->Max());
341
  args.GetReturnValue().Set(value);
342
}
343
344
void IntervalHistogram::GetMean(const FunctionCallbackInfo<Value>& args) {
345
  IntervalHistogram* histogram;
346
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
347
  args.GetReturnValue().Set((*histogram)->Mean());
348
}
349
350
void IntervalHistogram::GetExceeds(const FunctionCallbackInfo<Value>& args) {
351
  IntervalHistogram* histogram;
352
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
353
  double value = static_cast<double>((*histogram)->Exceeds());
354
  args.GetReturnValue().Set(value);
355
}
356
357
void IntervalHistogram::GetStddev(const FunctionCallbackInfo<Value>& args) {
358
  IntervalHistogram* histogram;
359
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
360
  args.GetReturnValue().Set((*histogram)->Stddev());
361
}
362
363
void IntervalHistogram::GetPercentile(const FunctionCallbackInfo<Value>& args) {
364
  IntervalHistogram* histogram;
365
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
366
  CHECK(args[0]->IsNumber());
367
  double percentile = args[0].As<Number>()->Value();
368
  args.GetReturnValue().Set((*histogram)->Percentile(percentile));
369
}
370
371
void IntervalHistogram::GetPercentiles(
372
    const FunctionCallbackInfo<Value>& args) {
373
  Environment* env = Environment::GetCurrent(args);
374
  IntervalHistogram* histogram;
375
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
376
  CHECK(args[0]->IsMap());
377
  Local<Map> map = args[0].As<Map>();
378
  (*histogram)->Percentiles([map, env](double key, double value) {
379
    map->Set(
380
        env->context(),
381
        Number::New(env->isolate(), key),
382
        Number::New(env->isolate(), value)).IsEmpty();
383
  });
384
}
385
386
void IntervalHistogram::DoReset(const FunctionCallbackInfo<Value>& args) {
387
  IntervalHistogram* histogram;
388
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
389
  (*histogram)->Reset();
390
}
391
392
std::unique_ptr<worker::TransferData>
393
IntervalHistogram::CloneForMessaging() const {
394
  return std::make_unique<HistogramBase::HistogramTransferData>(histogram());
395
}
396
397
}  // namespace node