GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: histogram.h Lines: 12 18 66.7 %
Date: 2021-10-04 04:12:38 Branches: 0 0 - %

Line Branch Exec Source
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 "memory_tracker.h"
9
#include "node_messaging.h"
10
#include "util.h"
11
#include "v8.h"
12
#include "uv.h"
13
14
#include <functional>
15
#include <limits>
16
#include <map>
17
#include <string>
18
19
namespace node {
20
21
class ExternalReferenceRegistry;
22
23
constexpr int kDefaultHistogramFigures = 3;
24
25
class Histogram : public MemoryRetainer {
26
 public:
27
  Histogram(
28
      int64_t lowest = 1,
29
      int64_t highest = std::numeric_limits<int64_t>::max(),
30
      int figures = kDefaultHistogramFigures);
31
32
  virtual ~Histogram() = default;
32
33
  inline bool Record(int64_t value);
34
  inline void Reset();
35
  inline int64_t Min();
36
  inline int64_t Max();
37
  inline double Mean();
38
  inline double Stddev();
39
  inline double Percentile(double percentile);
40
4
  inline int64_t Exceeds() const { return exceeds_; }
41
42
  inline uint64_t RecordDelta();
43
44
  // Iterator is a function type that takes two doubles as argument, one for
45
  // percentile and one for the value at that percentile.
46
  template <typename Iterator>
47
  inline void Percentiles(Iterator&& fn);
48
49
  inline size_t GetMemorySize() const;
50
51
  void MemoryInfo(MemoryTracker* tracker) const override;
52
  SET_MEMORY_INFO_NAME(Histogram)
53
  SET_SELF_SIZE(Histogram)
54
55
 private:
56
  using HistogramPointer = DeleteFnPtr<hdr_histogram, hdr_close>;
57
  HistogramPointer histogram_;
58
  int64_t exceeds_ = 0;
59
  uint64_t prev_ = 0;
60
61
  Mutex mutex_;
62
};
63
64
class HistogramImpl {
65
 public:
66
  HistogramImpl(int64_t lowest, int64_t highest, int figures);
67
  explicit HistogramImpl(std::shared_ptr<Histogram> histogram);
68
69
1056
  Histogram* operator->() { return histogram_.get(); }
70
71
 protected:
72
1762
  const std::shared_ptr<Histogram>& histogram() const { return histogram_; }
73
74
 private:
75
  std::shared_ptr<Histogram> histogram_;
76
};
77
78
class HistogramBase : public BaseObject, public HistogramImpl {
79
 public:
80
  static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
81
    Environment* env);
82
  static void Initialize(Environment* env, v8::Local<v8::Object> target);
83
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
84
85
  static BaseObjectPtr<HistogramBase> Create(
86
      Environment* env,
87
      int64_t lowest = 1,
88
      int64_t highest = std::numeric_limits<int64_t>::max(),
89
      int figures = kDefaultHistogramFigures);
90
91
  static BaseObjectPtr<HistogramBase> Create(
92
      Environment* env,
93
      std::shared_ptr<Histogram> histogram);
94
95
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
96
97
  void MemoryInfo(MemoryTracker* tracker) const override;
98
  SET_MEMORY_INFO_NAME(HistogramBase)
99
  SET_SELF_SIZE(HistogramBase)
100
101
  static void GetMin(const v8::FunctionCallbackInfo<v8::Value>& args);
102
  static void GetMax(const v8::FunctionCallbackInfo<v8::Value>& args);
103
  static void GetMean(const v8::FunctionCallbackInfo<v8::Value>& args);
104
  static void GetExceeds(const v8::FunctionCallbackInfo<v8::Value>& args);
105
  static void GetStddev(const v8::FunctionCallbackInfo<v8::Value>& args);
106
  static void GetPercentile(
107
      const v8::FunctionCallbackInfo<v8::Value>& args);
108
  static void GetPercentiles(
109
      const v8::FunctionCallbackInfo<v8::Value>& args);
110
  static void DoReset(const v8::FunctionCallbackInfo<v8::Value>& args);
111
  static void Record(const v8::FunctionCallbackInfo<v8::Value>& args);
112
  static void RecordDelta(const v8::FunctionCallbackInfo<v8::Value>& args);
113
114
  HistogramBase(
115
      Environment* env,
116
      v8::Local<v8::Object> wrap,
117
      int64_t lowest = 1,
118
      int64_t highest = std::numeric_limits<int64_t>::max(),
119
      int figures = kDefaultHistogramFigures);
120
121
  HistogramBase(
122
      Environment* env,
123
      v8::Local<v8::Object> wrap,
124
      std::shared_ptr<Histogram> histogram);
125
126
1
  TransferMode GetTransferMode() const override {
127
1
    return TransferMode::kCloneable;
128
  }
129
  std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
130
131
  class HistogramTransferData : public worker::TransferData {
132
   public:
133
1
    explicit HistogramTransferData(const HistogramBase* histogram)
134
1
        : histogram_(histogram->histogram()) {}
135
136
1
    explicit HistogramTransferData(std::shared_ptr<Histogram> histogram)
137
1
        : histogram_(std::move(histogram)) {}
138
139
    BaseObjectPtr<BaseObject> Deserialize(
140
        Environment* env,
141
        v8::Local<v8::Context> context,
142
        std::unique_ptr<worker::TransferData> self) override;
143
144
    void MemoryInfo(MemoryTracker* tracker) const override;
145
    SET_MEMORY_INFO_NAME(HistogramTransferData)
146
    SET_SELF_SIZE(HistogramTransferData)
147
148
   private:
149
    std::shared_ptr<Histogram> histogram_;
150
  };
151
};
152
153
class IntervalHistogram : public HandleWrap, public HistogramImpl {
154
 public:
155
  enum class StartFlags {
156
    NONE,
157
    RESET
158
  };
159
160
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
161
162
  static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
163
      Environment* env);
164
165
  static BaseObjectPtr<IntervalHistogram> Create(
166
      Environment* env,
167
      int64_t lowest = 1,
168
      int64_t highest = std::numeric_limits<int64_t>::max(),
169
      int figures = kDefaultHistogramFigures);
170
171
  virtual void OnInterval() = 0;
172
173
  void MemoryInfo(MemoryTracker* tracker) const override;
174
175
  IntervalHistogram(
176
      Environment* env,
177
      v8::Local<v8::Object> wrap,
178
      AsyncWrap::ProviderType type,
179
      int32_t interval,
180
      int64_t lowest = 1,
181
      int64_t highest = std::numeric_limits<int64_t>::max(),
182
      int figures = kDefaultHistogramFigures);
183
184
  static void GetMin(const v8::FunctionCallbackInfo<v8::Value>& args);
185
  static void GetMax(const v8::FunctionCallbackInfo<v8::Value>& args);
186
  static void GetMean(const v8::FunctionCallbackInfo<v8::Value>& args);
187
  static void GetExceeds(const v8::FunctionCallbackInfo<v8::Value>& args);
188
  static void GetStddev(const v8::FunctionCallbackInfo<v8::Value>& args);
189
  static void GetPercentile(
190
      const v8::FunctionCallbackInfo<v8::Value>& args);
191
  static void GetPercentiles(
192
      const v8::FunctionCallbackInfo<v8::Value>& args);
193
  static void DoReset(const v8::FunctionCallbackInfo<v8::Value>& args);
194
  static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
195
  static void Stop(const v8::FunctionCallbackInfo<v8::Value>& args);
196
197
1
  TransferMode GetTransferMode() const override {
198
1
    return TransferMode::kCloneable;
199
  }
200
  std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
201
202
 private:
203
  static void TimerCB(uv_timer_t* handle);
204
  void OnStart(StartFlags flags = StartFlags::RESET);
205
  void OnStop();
206
207
  bool enabled_ = false;
208
  int32_t interval_ = 0;
209
  uv_timer_t timer_;
210
};
211
212
}  // namespace node
213
214
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
215
216
#endif  // SRC_HISTOGRAM_H_