diff options
author | James M Snell <jasnell@gmail.com> | 2020-02-28 00:14:38 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2020-03-02 22:01:00 +0300 |
commit | eb2fe5ff90d68520b148fdc86362c07561c1c635 (patch) | |
tree | 6ad5259a9423f6de5d414abe938576627b9ea432 /src/histogram.h | |
parent | 0fac393d263fc7e2f4f054c9d4aab0c1c3cf00c8 (diff) |
perf,src: add HistogramBase and internal/histogram.js
Separating this out from the QUIC PR to allow it to be separately
reviewed. The QUIC implementation makes use of the hdr_histogram
for dynamic performance monitoring. This introduces a BaseObject
class that allows the internal histograms to be accessed on the
JavaScript side and adds a generic Histogram class that will be
used by both QUIC and perf_hooks (for the event loop delay
monitoring).
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/31988
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'src/histogram.h')
-rw-r--r-- | src/histogram.h | 70 |
1 files changed, 65 insertions, 5 deletions
diff --git a/src/histogram.h b/src/histogram.h index eb94af5da2a..e92c31c4724 100644 --- a/src/histogram.h +++ b/src/histogram.h @@ -4,15 +4,24 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #include "hdr_histogram.h" +#include "base_object.h" +#include "util.h" + #include <functional> +#include <limits> #include <map> namespace node { +constexpr int kDefaultHistogramFigures = 3; + class Histogram { public: - inline Histogram(int64_t lowest, int64_t highest, int figures = 3); - inline virtual ~Histogram(); + Histogram( + int64_t lowest = std::numeric_limits<int64_t>::min(), + int64_t highest = std::numeric_limits<int64_t>::max(), + int figures = kDefaultHistogramFigures); + virtual ~Histogram() = default; inline bool Record(int64_t value); inline void Reset(); @@ -21,14 +30,65 @@ class Histogram { inline double Mean(); inline double Stddev(); inline double Percentile(double percentile); - inline void Percentiles(std::function<void(double, double)> fn); + + // Iterator is a function type that takes two doubles as argument, one for + // percentile and one for the value at that percentile. + template <typename Iterator> + inline void Percentiles(Iterator&& fn); size_t GetMemorySize() const { - return hdr_get_memory_size(histogram_); + return hdr_get_memory_size(histogram_.get()); } private: - hdr_histogram* histogram_; + using HistogramPointer = DeleteFnPtr<hdr_histogram, hdr_close>; + HistogramPointer histogram_; +}; + +class HistogramBase : public BaseObject, public Histogram { + public: + virtual ~HistogramBase() = default; + + virtual void TraceDelta(int64_t delta) {} + virtual void TraceExceeds(int64_t delta) {} + + inline bool RecordDelta(); + inline void ResetState(); + + int64_t Exceeds() const { return exceeds_; } + + void MemoryInfo(MemoryTracker* tracker) const override; + SET_MEMORY_INFO_NAME(HistogramBase) + SET_SELF_SIZE(HistogramBase) + + static void GetMin(const v8::FunctionCallbackInfo<v8::Value>& args); + static void GetMax(const v8::FunctionCallbackInfo<v8::Value>& args); + static void GetMean(const v8::FunctionCallbackInfo<v8::Value>& args); + static void GetExceeds(const v8::FunctionCallbackInfo<v8::Value>& args); + static void GetStddev(const v8::FunctionCallbackInfo<v8::Value>& args); + static void GetPercentile( + const v8::FunctionCallbackInfo<v8::Value>& args); + static void GetPercentiles( + const v8::FunctionCallbackInfo<v8::Value>& args); + static void DoReset(const v8::FunctionCallbackInfo<v8::Value>& args); + static void Initialize(Environment* env); + + static BaseObjectPtr<HistogramBase> New( + Environment* env, + int64_t lowest = std::numeric_limits<int64_t>::min(), + int64_t highest = std::numeric_limits<int64_t>::max(), + int figures = kDefaultHistogramFigures); + + HistogramBase( + Environment* env, + v8::Local<v8::Object> wrap, + int64_t lowest = std::numeric_limits<int64_t>::min(), + int64_t highest = std::numeric_limits<int64_t>::max(), + int figures = kDefaultHistogramFigures); + + private: + int64_t exceeds_ = 0; + uint64_t prev_ = 0; }; } // namespace node |