11cb0ef41Sopenharmony_ci#ifndef SRC_HISTOGRAM_INL_H_
21cb0ef41Sopenharmony_ci#define SRC_HISTOGRAM_INL_H_
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#include "histogram.h"
71cb0ef41Sopenharmony_ci#include "base_object-inl.h"
81cb0ef41Sopenharmony_ci#include "node_internals.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace node {
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_civoid Histogram::Reset() {
131cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
141cb0ef41Sopenharmony_ci  hdr_reset(histogram_.get());
151cb0ef41Sopenharmony_ci  exceeds_ = 0;
161cb0ef41Sopenharmony_ci  count_ = 0;
171cb0ef41Sopenharmony_ci  prev_ = 0;
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cidouble Histogram::Add(const Histogram& other) {
211cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
221cb0ef41Sopenharmony_ci  count_ += other.count_;
231cb0ef41Sopenharmony_ci  exceeds_ += other.exceeds_;
241cb0ef41Sopenharmony_ci  if (other.prev_ > prev_)
251cb0ef41Sopenharmony_ci    prev_ = other.prev_;
261cb0ef41Sopenharmony_ci  return static_cast<double>(hdr_add(histogram_.get(), other.histogram_.get()));
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cisize_t Histogram::Count() const {
301cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
311cb0ef41Sopenharmony_ci  return count_;
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciint64_t Histogram::Min() const {
351cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
361cb0ef41Sopenharmony_ci  return hdr_min(histogram_.get());
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciint64_t Histogram::Max() const {
401cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
411cb0ef41Sopenharmony_ci  return hdr_max(histogram_.get());
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cidouble Histogram::Mean() const {
451cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
461cb0ef41Sopenharmony_ci  return hdr_mean(histogram_.get());
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cidouble Histogram::Stddev() const {
501cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
511cb0ef41Sopenharmony_ci  return hdr_stddev(histogram_.get());
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciint64_t Histogram::Percentile(double percentile) const {
551cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
561cb0ef41Sopenharmony_ci  CHECK_GT(percentile, 0);
571cb0ef41Sopenharmony_ci  CHECK_LE(percentile, 100);
581cb0ef41Sopenharmony_ci  return hdr_value_at_percentile(histogram_.get(), percentile);
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_citemplate <typename Iterator>
621cb0ef41Sopenharmony_civoid Histogram::Percentiles(Iterator&& fn) {
631cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
641cb0ef41Sopenharmony_ci  hdr_iter iter;
651cb0ef41Sopenharmony_ci  hdr_iter_percentile_init(&iter, histogram_.get(), 1);
661cb0ef41Sopenharmony_ci  while (hdr_iter_next(&iter)) {
671cb0ef41Sopenharmony_ci    double key = iter.specifics.percentiles.percentile;
681cb0ef41Sopenharmony_ci    fn(key, iter.value);
691cb0ef41Sopenharmony_ci  }
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_cibool Histogram::Record(int64_t value) {
731cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
741cb0ef41Sopenharmony_ci  bool recorded = hdr_record_value(histogram_.get(), value);
751cb0ef41Sopenharmony_ci  if (!recorded)
761cb0ef41Sopenharmony_ci    exceeds_++;
771cb0ef41Sopenharmony_ci  else
781cb0ef41Sopenharmony_ci    count_++;
791cb0ef41Sopenharmony_ci  return recorded;
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ciuint64_t Histogram::RecordDelta() {
831cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
841cb0ef41Sopenharmony_ci  uint64_t time = uv_hrtime();
851cb0ef41Sopenharmony_ci  int64_t delta = 0;
861cb0ef41Sopenharmony_ci  if (prev_ > 0) {
871cb0ef41Sopenharmony_ci    CHECK_GE(time, prev_);
881cb0ef41Sopenharmony_ci    delta = time - prev_;
891cb0ef41Sopenharmony_ci    if (hdr_record_value(histogram_.get(), delta))
901cb0ef41Sopenharmony_ci      count_++;
911cb0ef41Sopenharmony_ci    else
921cb0ef41Sopenharmony_ci      exceeds_++;
931cb0ef41Sopenharmony_ci  }
941cb0ef41Sopenharmony_ci  prev_ = time;
951cb0ef41Sopenharmony_ci  return delta;
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_cisize_t Histogram::GetMemorySize() const {
991cb0ef41Sopenharmony_ci  Mutex::ScopedLock lock(mutex_);
1001cb0ef41Sopenharmony_ci  return hdr_get_memory_size(histogram_.get());
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci}  // namespace node
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci#endif  // SRC_HISTOGRAM_INL_H_
108