• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_HISTOGRAM_INL_H_
2 #define SRC_HISTOGRAM_INL_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "histogram.h"
7 #include "base_object-inl.h"
8 #include "node_internals.h"
9 
10 namespace node {
11 
Reset()12 void Histogram::Reset() {
13   Mutex::ScopedLock lock(mutex_);
14   hdr_reset(histogram_.get());
15   exceeds_ = 0;
16   count_ = 0;
17   prev_ = 0;
18 }
19 
Add(const Histogram & other)20 double Histogram::Add(const Histogram& other) {
21   Mutex::ScopedLock lock(mutex_);
22   count_ += other.count_;
23   exceeds_ += other.exceeds_;
24   if (other.prev_ > prev_)
25     prev_ = other.prev_;
26   return static_cast<double>(hdr_add(histogram_.get(), other.histogram_.get()));
27 }
28 
Count()29 size_t Histogram::Count() const {
30   Mutex::ScopedLock lock(mutex_);
31   return count_;
32 }
33 
Min()34 int64_t Histogram::Min() const {
35   Mutex::ScopedLock lock(mutex_);
36   return hdr_min(histogram_.get());
37 }
38 
Max()39 int64_t Histogram::Max() const {
40   Mutex::ScopedLock lock(mutex_);
41   return hdr_max(histogram_.get());
42 }
43 
Mean()44 double Histogram::Mean() const {
45   Mutex::ScopedLock lock(mutex_);
46   return hdr_mean(histogram_.get());
47 }
48 
Stddev()49 double Histogram::Stddev() const {
50   Mutex::ScopedLock lock(mutex_);
51   return hdr_stddev(histogram_.get());
52 }
53 
Percentile(double percentile)54 int64_t Histogram::Percentile(double percentile) const {
55   Mutex::ScopedLock lock(mutex_);
56   CHECK_GT(percentile, 0);
57   CHECK_LE(percentile, 100);
58   return hdr_value_at_percentile(histogram_.get(), percentile);
59 }
60 
61 template <typename Iterator>
Percentiles(Iterator && fn)62 void Histogram::Percentiles(Iterator&& fn) {
63   Mutex::ScopedLock lock(mutex_);
64   hdr_iter iter;
65   hdr_iter_percentile_init(&iter, histogram_.get(), 1);
66   while (hdr_iter_next(&iter)) {
67     double key = iter.specifics.percentiles.percentile;
68     fn(key, iter.value);
69   }
70 }
71 
Record(int64_t value)72 bool Histogram::Record(int64_t value) {
73   Mutex::ScopedLock lock(mutex_);
74   bool recorded = hdr_record_value(histogram_.get(), value);
75   if (!recorded)
76     exceeds_++;
77   else
78     count_++;
79   return recorded;
80 }
81 
RecordDelta()82 uint64_t Histogram::RecordDelta() {
83   Mutex::ScopedLock lock(mutex_);
84   uint64_t time = uv_hrtime();
85   int64_t delta = 0;
86   if (prev_ > 0) {
87     CHECK_GE(time, prev_);
88     delta = time - prev_;
89     if (hdr_record_value(histogram_.get(), delta))
90       count_++;
91     else
92       exceeds_++;
93   }
94   prev_ = time;
95   return delta;
96 }
97 
GetMemorySize()98 size_t Histogram::GetMemorySize() const {
99   Mutex::ScopedLock lock(mutex_);
100   return hdr_get_memory_size(histogram_.get());
101 }
102 
103 }  // namespace node
104 
105 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
106 
107 #endif  // SRC_HISTOGRAM_INL_H_
108