11cb0ef41Sopenharmony_ci#ifndef SRC_HISTOGRAM_H_ 21cb0ef41Sopenharmony_ci#define SRC_HISTOGRAM_H_ 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci#include <hdr/hdr_histogram.h> 71cb0ef41Sopenharmony_ci#include "base_object.h" 81cb0ef41Sopenharmony_ci#include "memory_tracker.h" 91cb0ef41Sopenharmony_ci#include "node_messaging.h" 101cb0ef41Sopenharmony_ci#include "util.h" 111cb0ef41Sopenharmony_ci#include "uv.h" 121cb0ef41Sopenharmony_ci#include "v8.h" 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci#include <functional> 151cb0ef41Sopenharmony_ci#include <limits> 161cb0ef41Sopenharmony_ci#include <map> 171cb0ef41Sopenharmony_ci#include <string> 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cinamespace node { 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciclass ExternalReferenceRegistry; 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciconstexpr int kDefaultHistogramFigures = 3; 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciclass Histogram : public MemoryRetainer { 261cb0ef41Sopenharmony_ci public: 271cb0ef41Sopenharmony_ci struct Options { 281cb0ef41Sopenharmony_ci int64_t lowest = 1; 291cb0ef41Sopenharmony_ci int64_t highest = std::numeric_limits<int64_t>::max(); 301cb0ef41Sopenharmony_ci int figures = kDefaultHistogramFigures; 311cb0ef41Sopenharmony_ci }; 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci explicit Histogram(const Options& options); 341cb0ef41Sopenharmony_ci virtual ~Histogram() = default; 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci inline bool Record(int64_t value); 371cb0ef41Sopenharmony_ci inline void Reset(); 381cb0ef41Sopenharmony_ci inline int64_t Min() const; 391cb0ef41Sopenharmony_ci inline int64_t Max() const; 401cb0ef41Sopenharmony_ci inline double Mean() const; 411cb0ef41Sopenharmony_ci inline double Stddev() const; 421cb0ef41Sopenharmony_ci inline int64_t Percentile(double percentile) const; 431cb0ef41Sopenharmony_ci inline size_t Exceeds() const { return exceeds_; } 441cb0ef41Sopenharmony_ci inline size_t Count() const; 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci inline uint64_t RecordDelta(); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci inline double Add(const Histogram& other); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci // Iterator is a function type that takes two doubles as argument, one for 511cb0ef41Sopenharmony_ci // percentile and one for the value at that percentile. 521cb0ef41Sopenharmony_ci template <typename Iterator> 531cb0ef41Sopenharmony_ci inline void Percentiles(Iterator&& fn); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci inline size_t GetMemorySize() const; 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci void MemoryInfo(MemoryTracker* tracker) const override; 581cb0ef41Sopenharmony_ci SET_MEMORY_INFO_NAME(Histogram) 591cb0ef41Sopenharmony_ci SET_SELF_SIZE(Histogram) 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci private: 621cb0ef41Sopenharmony_ci using HistogramPointer = DeleteFnPtr<hdr_histogram, hdr_close>; 631cb0ef41Sopenharmony_ci HistogramPointer histogram_; 641cb0ef41Sopenharmony_ci uint64_t prev_ = 0; 651cb0ef41Sopenharmony_ci size_t exceeds_ = 0; 661cb0ef41Sopenharmony_ci size_t count_ = 0; 671cb0ef41Sopenharmony_ci Mutex mutex_; 681cb0ef41Sopenharmony_ci}; 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ciclass HistogramImpl { 711cb0ef41Sopenharmony_ci public: 721cb0ef41Sopenharmony_ci explicit HistogramImpl( 731cb0ef41Sopenharmony_ci const Histogram::Options& options = Histogram::Options {}); 741cb0ef41Sopenharmony_ci explicit HistogramImpl(std::shared_ptr<Histogram> histogram); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci Histogram* operator->() { return histogram_.get(); } 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci const std::shared_ptr<Histogram>& histogram() const { return histogram_; } 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci private: 811cb0ef41Sopenharmony_ci std::shared_ptr<Histogram> histogram_; 821cb0ef41Sopenharmony_ci}; 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ciclass HistogramBase : public BaseObject, public HistogramImpl { 851cb0ef41Sopenharmony_ci public: 861cb0ef41Sopenharmony_ci static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( 871cb0ef41Sopenharmony_ci Environment* env); 881cb0ef41Sopenharmony_ci static void Initialize(Environment* env, v8::Local<v8::Object> target); 891cb0ef41Sopenharmony_ci static void RegisterExternalReferences(ExternalReferenceRegistry* registry); 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci static BaseObjectPtr<HistogramBase> Create( 921cb0ef41Sopenharmony_ci Environment* env, 931cb0ef41Sopenharmony_ci const Histogram::Options& options = Histogram::Options {}); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci static BaseObjectPtr<HistogramBase> Create( 961cb0ef41Sopenharmony_ci Environment* env, 971cb0ef41Sopenharmony_ci std::shared_ptr<Histogram> histogram); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci void MemoryInfo(MemoryTracker* tracker) const override; 1021cb0ef41Sopenharmony_ci SET_MEMORY_INFO_NAME(HistogramBase) 1031cb0ef41Sopenharmony_ci SET_SELF_SIZE(HistogramBase) 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci static void GetCountBigInt(const v8::FunctionCallbackInfo<v8::Value>& args); 1061cb0ef41Sopenharmony_ci static void GetMinBigInt(const v8::FunctionCallbackInfo<v8::Value>& args); 1071cb0ef41Sopenharmony_ci static void GetMaxBigInt(const v8::FunctionCallbackInfo<v8::Value>& args); 1081cb0ef41Sopenharmony_ci static void GetExceedsBigInt(const v8::FunctionCallbackInfo<v8::Value>& args); 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci static void GetCount(const v8::FunctionCallbackInfo<v8::Value>& args); 1111cb0ef41Sopenharmony_ci static void GetMin(const v8::FunctionCallbackInfo<v8::Value>& args); 1121cb0ef41Sopenharmony_ci static void GetMax(const v8::FunctionCallbackInfo<v8::Value>& args); 1131cb0ef41Sopenharmony_ci static void GetMean(const v8::FunctionCallbackInfo<v8::Value>& args); 1141cb0ef41Sopenharmony_ci static void GetExceeds(const v8::FunctionCallbackInfo<v8::Value>& args); 1151cb0ef41Sopenharmony_ci static void GetStddev(const v8::FunctionCallbackInfo<v8::Value>& args); 1161cb0ef41Sopenharmony_ci static void GetPercentile( 1171cb0ef41Sopenharmony_ci const v8::FunctionCallbackInfo<v8::Value>& args); 1181cb0ef41Sopenharmony_ci static void GetPercentileBigInt( 1191cb0ef41Sopenharmony_ci const v8::FunctionCallbackInfo<v8::Value>& args); 1201cb0ef41Sopenharmony_ci static void GetPercentiles( 1211cb0ef41Sopenharmony_ci const v8::FunctionCallbackInfo<v8::Value>& args); 1221cb0ef41Sopenharmony_ci static void GetPercentilesBigInt( 1231cb0ef41Sopenharmony_ci const v8::FunctionCallbackInfo<v8::Value>& args); 1241cb0ef41Sopenharmony_ci static void DoReset(const v8::FunctionCallbackInfo<v8::Value>& args); 1251cb0ef41Sopenharmony_ci static void Record(const v8::FunctionCallbackInfo<v8::Value>& args); 1261cb0ef41Sopenharmony_ci static void RecordDelta(const v8::FunctionCallbackInfo<v8::Value>& args); 1271cb0ef41Sopenharmony_ci static void Add(const v8::FunctionCallbackInfo<v8::Value>& args); 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci HistogramBase( 1301cb0ef41Sopenharmony_ci Environment* env, 1311cb0ef41Sopenharmony_ci v8::Local<v8::Object> wrap, 1321cb0ef41Sopenharmony_ci const Histogram::Options& options = Histogram::Options {}); 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci HistogramBase( 1351cb0ef41Sopenharmony_ci Environment* env, 1361cb0ef41Sopenharmony_ci v8::Local<v8::Object> wrap, 1371cb0ef41Sopenharmony_ci std::shared_ptr<Histogram> histogram); 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci TransferMode GetTransferMode() const override { 1401cb0ef41Sopenharmony_ci return TransferMode::kCloneable; 1411cb0ef41Sopenharmony_ci } 1421cb0ef41Sopenharmony_ci std::unique_ptr<worker::TransferData> CloneForMessaging() const override; 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci class HistogramTransferData : public worker::TransferData { 1451cb0ef41Sopenharmony_ci public: 1461cb0ef41Sopenharmony_ci explicit HistogramTransferData(const HistogramBase* histogram) 1471cb0ef41Sopenharmony_ci : histogram_(histogram->histogram()) {} 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci explicit HistogramTransferData(std::shared_ptr<Histogram> histogram) 1501cb0ef41Sopenharmony_ci : histogram_(std::move(histogram)) {} 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci BaseObjectPtr<BaseObject> Deserialize( 1531cb0ef41Sopenharmony_ci Environment* env, 1541cb0ef41Sopenharmony_ci v8::Local<v8::Context> context, 1551cb0ef41Sopenharmony_ci std::unique_ptr<worker::TransferData> self) override; 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci void MemoryInfo(MemoryTracker* tracker) const override; 1581cb0ef41Sopenharmony_ci SET_MEMORY_INFO_NAME(HistogramTransferData) 1591cb0ef41Sopenharmony_ci SET_SELF_SIZE(HistogramTransferData) 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci private: 1621cb0ef41Sopenharmony_ci std::shared_ptr<Histogram> histogram_; 1631cb0ef41Sopenharmony_ci }; 1641cb0ef41Sopenharmony_ci}; 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ciclass IntervalHistogram : public HandleWrap, public HistogramImpl { 1671cb0ef41Sopenharmony_ci public: 1681cb0ef41Sopenharmony_ci enum class StartFlags { 1691cb0ef41Sopenharmony_ci NONE, 1701cb0ef41Sopenharmony_ci RESET 1711cb0ef41Sopenharmony_ci }; 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci static void RegisterExternalReferences(ExternalReferenceRegistry* registry); 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( 1761cb0ef41Sopenharmony_ci Environment* env); 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci static BaseObjectPtr<IntervalHistogram> Create( 1791cb0ef41Sopenharmony_ci Environment* env, 1801cb0ef41Sopenharmony_ci int32_t interval, 1811cb0ef41Sopenharmony_ci std::function<void(Histogram&)> on_interval, 1821cb0ef41Sopenharmony_ci const Histogram::Options& options); 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci IntervalHistogram( 1851cb0ef41Sopenharmony_ci Environment* env, 1861cb0ef41Sopenharmony_ci v8::Local<v8::Object> wrap, 1871cb0ef41Sopenharmony_ci AsyncWrap::ProviderType type, 1881cb0ef41Sopenharmony_ci int32_t interval, 1891cb0ef41Sopenharmony_ci std::function<void(Histogram&)> on_interval, 1901cb0ef41Sopenharmony_ci const Histogram::Options& options = Histogram::Options {}); 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ci static void GetCountBigInt(const v8::FunctionCallbackInfo<v8::Value>& args); 1931cb0ef41Sopenharmony_ci static void GetMinBigInt(const v8::FunctionCallbackInfo<v8::Value>& args); 1941cb0ef41Sopenharmony_ci static void GetMaxBigInt(const v8::FunctionCallbackInfo<v8::Value>& args); 1951cb0ef41Sopenharmony_ci static void GetExceedsBigInt(const v8::FunctionCallbackInfo<v8::Value>& args); 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci static void GetCount(const v8::FunctionCallbackInfo<v8::Value>& args); 1981cb0ef41Sopenharmony_ci static void GetMin(const v8::FunctionCallbackInfo<v8::Value>& args); 1991cb0ef41Sopenharmony_ci static void GetMax(const v8::FunctionCallbackInfo<v8::Value>& args); 2001cb0ef41Sopenharmony_ci static void GetMean(const v8::FunctionCallbackInfo<v8::Value>& args); 2011cb0ef41Sopenharmony_ci static void GetExceeds(const v8::FunctionCallbackInfo<v8::Value>& args); 2021cb0ef41Sopenharmony_ci static void GetStddev(const v8::FunctionCallbackInfo<v8::Value>& args); 2031cb0ef41Sopenharmony_ci static void GetPercentile( 2041cb0ef41Sopenharmony_ci const v8::FunctionCallbackInfo<v8::Value>& args); 2051cb0ef41Sopenharmony_ci static void GetPercentileBigInt( 2061cb0ef41Sopenharmony_ci const v8::FunctionCallbackInfo<v8::Value>& args); 2071cb0ef41Sopenharmony_ci static void GetPercentiles( 2081cb0ef41Sopenharmony_ci const v8::FunctionCallbackInfo<v8::Value>& args); 2091cb0ef41Sopenharmony_ci static void GetPercentilesBigInt( 2101cb0ef41Sopenharmony_ci const v8::FunctionCallbackInfo<v8::Value>& args); 2111cb0ef41Sopenharmony_ci static void DoReset(const v8::FunctionCallbackInfo<v8::Value>& args); 2121cb0ef41Sopenharmony_ci static void Start(const v8::FunctionCallbackInfo<v8::Value>& args); 2131cb0ef41Sopenharmony_ci static void Stop(const v8::FunctionCallbackInfo<v8::Value>& args); 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci TransferMode GetTransferMode() const override { 2161cb0ef41Sopenharmony_ci return TransferMode::kCloneable; 2171cb0ef41Sopenharmony_ci } 2181cb0ef41Sopenharmony_ci std::unique_ptr<worker::TransferData> CloneForMessaging() const override; 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ci void MemoryInfo(MemoryTracker* tracker) const override; 2211cb0ef41Sopenharmony_ci SET_MEMORY_INFO_NAME(IntervalHistogram) 2221cb0ef41Sopenharmony_ci SET_SELF_SIZE(IntervalHistogram) 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ci private: 2251cb0ef41Sopenharmony_ci static void TimerCB(uv_timer_t* handle); 2261cb0ef41Sopenharmony_ci void OnStart(StartFlags flags = StartFlags::RESET); 2271cb0ef41Sopenharmony_ci void OnStop(); 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci bool enabled_ = false; 2301cb0ef41Sopenharmony_ci int32_t interval_ = 0; 2311cb0ef41Sopenharmony_ci std::function<void(Histogram&)> on_interval_; 2321cb0ef41Sopenharmony_ci uv_timer_t timer_; 2331cb0ef41Sopenharmony_ci}; 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ci} // namespace node 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci#endif // SRC_HISTOGRAM_H_ 240