• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_HISTOGRAM_H_
2 #define SRC_HISTOGRAM_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "hdr_histogram.h"
7 #include "base_object.h"
8 #include "util.h"
9 
10 #include <functional>
11 #include <limits>
12 #include <map>
13 
14 namespace node {
15 
16 constexpr int kDefaultHistogramFigures = 3;
17 
18 class Histogram {
19  public:
20   Histogram(
21       int64_t lowest = std::numeric_limits<int64_t>::min(),
22       int64_t highest = std::numeric_limits<int64_t>::max(),
23       int figures = kDefaultHistogramFigures);
24   virtual ~Histogram() = default;
25 
26   inline bool Record(int64_t value);
27   inline void Reset();
28   inline int64_t Min();
29   inline int64_t Max();
30   inline double Mean();
31   inline double Stddev();
32   inline double Percentile(double percentile);
33 
34   // Iterator is a function type that takes two doubles as argument, one for
35   // percentile and one for the value at that percentile.
36   template <typename Iterator>
37   inline void Percentiles(Iterator&& fn);
38 
GetMemorySize()39   size_t GetMemorySize() const {
40     return hdr_get_memory_size(histogram_.get());
41   }
42 
43  private:
44   using HistogramPointer = DeleteFnPtr<hdr_histogram, hdr_close>;
45   HistogramPointer histogram_;
46 };
47 
48 class HistogramBase : public BaseObject, public Histogram {
49  public:
50   virtual ~HistogramBase() = default;
51 
TraceDelta(int64_t delta)52   virtual void TraceDelta(int64_t delta) {}
TraceExceeds(int64_t delta)53   virtual void TraceExceeds(int64_t delta) {}
54 
55   inline bool RecordDelta();
56   inline void ResetState();
57 
Exceeds()58   int64_t Exceeds() const { return exceeds_; }
59 
60   void MemoryInfo(MemoryTracker* tracker) const override;
61   SET_MEMORY_INFO_NAME(HistogramBase)
62   SET_SELF_SIZE(HistogramBase)
63 
64   static void GetMin(const v8::FunctionCallbackInfo<v8::Value>& args);
65   static void GetMax(const v8::FunctionCallbackInfo<v8::Value>& args);
66   static void GetMean(const v8::FunctionCallbackInfo<v8::Value>& args);
67   static void GetExceeds(const v8::FunctionCallbackInfo<v8::Value>& args);
68   static void GetStddev(const v8::FunctionCallbackInfo<v8::Value>& args);
69   static void GetPercentile(
70       const v8::FunctionCallbackInfo<v8::Value>& args);
71   static void GetPercentiles(
72       const v8::FunctionCallbackInfo<v8::Value>& args);
73   static void DoReset(const v8::FunctionCallbackInfo<v8::Value>& args);
74   static void Initialize(Environment* env);
75 
76   static BaseObjectPtr<HistogramBase> New(
77       Environment* env,
78       int64_t lowest = std::numeric_limits<int64_t>::min(),
79       int64_t highest = std::numeric_limits<int64_t>::max(),
80       int figures = kDefaultHistogramFigures);
81 
82   HistogramBase(
83       Environment* env,
84       v8::Local<v8::Object> wrap,
85       int64_t lowest = std::numeric_limits<int64_t>::min(),
86       int64_t highest = std::numeric_limits<int64_t>::max(),
87       int figures = kDefaultHistogramFigures);
88 
89  private:
90   int64_t exceeds_ = 0;
91   uint64_t prev_ = 0;
92 };
93 
94 }  // namespace node
95 
96 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
97 
98 #endif  // SRC_HISTOGRAM_H_
99