Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2021-01-30 20:26:15 +0300
committerJames M Snell <jasnell@gmail.com>2021-02-15 00:03:49 +0300
commit4a19cc8947b1bba2b2d27816ec3d0edf9b28e503 (patch)
tree8c75fbee068d6eda2682bd660198952ffb30bcf4 /src/histogram.h
parentce4798b55515827f2a68d269d0f433e3ea9828bb (diff)
perf_hooks: introduce createHistogram
Adds a new `perf_hooks.createHistogram()` API for creating histogram instances that allow user recording. Makes Histogram instances cloneable via MessagePort. This allows, for instance, an event loop delay monitor to be running on the main thread while the histogram data can be monitored actively from a worker thread. Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/37155 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'src/histogram.h')
-rw-r--r--src/histogram.h149
1 files changed, 131 insertions, 18 deletions
diff --git a/src/histogram.h b/src/histogram.h
index e92c31c4724..8c164f54cfd 100644
--- a/src/histogram.h
+++ b/src/histogram.h
@@ -5,20 +5,25 @@
#include "hdr_histogram.h"
#include "base_object.h"
+#include "memory_tracker.h"
+#include "node_messaging.h"
#include "util.h"
+#include "v8.h"
+#include "uv.h"
#include <functional>
#include <limits>
#include <map>
+#include <string>
namespace node {
constexpr int kDefaultHistogramFigures = 3;
-class Histogram {
+class Histogram : public MemoryRetainer {
public:
Histogram(
- int64_t lowest = std::numeric_limits<int64_t>::min(),
+ int64_t lowest = 1,
int64_t highest = std::numeric_limits<int64_t>::max(),
int figures = kDefaultHistogramFigures);
virtual ~Histogram() = default;
@@ -30,32 +35,61 @@ class Histogram {
inline double Mean();
inline double Stddev();
inline double Percentile(double percentile);
+ inline int64_t Exceeds() const { return exceeds_; }
+
+ inline uint64_t RecordDelta();
// 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_.get());
- }
+ inline size_t GetMemorySize() const;
+
+ void MemoryInfo(MemoryTracker* tracker) const override;
+ SET_MEMORY_INFO_NAME(Histogram)
+ SET_SELF_SIZE(Histogram)
private:
using HistogramPointer = DeleteFnPtr<hdr_histogram, hdr_close>;
HistogramPointer histogram_;
+ int64_t exceeds_ = 0;
+ uint64_t prev_ = 0;
+
+ Mutex mutex_;
};
-class HistogramBase : public BaseObject, public Histogram {
+class HistogramImpl {
public:
- virtual ~HistogramBase() = default;
+ HistogramImpl(int64_t lowest, int64_t highest, int figures);
+ explicit HistogramImpl(std::shared_ptr<Histogram> histogram);
- virtual void TraceDelta(int64_t delta) {}
- virtual void TraceExceeds(int64_t delta) {}
+ Histogram* operator->() { return histogram_.get(); }
- inline bool RecordDelta();
- inline void ResetState();
+ protected:
+ const std::shared_ptr<Histogram>& histogram() const { return histogram_; }
- int64_t Exceeds() const { return exceeds_; }
+ private:
+ std::shared_ptr<Histogram> histogram_;
+};
+
+class HistogramBase : public BaseObject, public HistogramImpl {
+ public:
+ static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
+ Environment* env);
+ static void Initialize(Environment* env, v8::Local<v8::Object> target);
+
+ static BaseObjectPtr<HistogramBase> Create(
+ Environment* env,
+ int64_t lowest = 1,
+ int64_t highest = std::numeric_limits<int64_t>::max(),
+ int figures = kDefaultHistogramFigures);
+
+ static BaseObjectPtr<HistogramBase> Create(
+ Environment* env,
+ std::shared_ptr<Histogram> histogram);
+
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(HistogramBase)
@@ -71,24 +105,103 @@ class HistogramBase : public BaseObject, public Histogram {
static void GetPercentiles(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void DoReset(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void Initialize(Environment* env);
+ static void Record(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void RecordDelta(const v8::FunctionCallbackInfo<v8::Value>& args);
- static BaseObjectPtr<HistogramBase> New(
+ HistogramBase(
Environment* env,
- int64_t lowest = std::numeric_limits<int64_t>::min(),
+ v8::Local<v8::Object> wrap,
+ int64_t lowest = 1,
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(),
+ std::shared_ptr<Histogram> histogram);
+
+ TransferMode GetTransferMode() const override {
+ return TransferMode::kCloneable;
+ }
+ std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
+
+ class HistogramTransferData : public worker::TransferData {
+ public:
+ explicit HistogramTransferData(const HistogramBase* histogram)
+ : histogram_(histogram->histogram()) {}
+
+ explicit HistogramTransferData(std::shared_ptr<Histogram> histogram)
+ : histogram_(std::move(histogram)) {}
+
+ BaseObjectPtr<BaseObject> Deserialize(
+ Environment* env,
+ v8::Local<v8::Context> context,
+ std::unique_ptr<worker::TransferData> self) override;
+
+ void MemoryInfo(MemoryTracker* tracker) const override;
+ SET_MEMORY_INFO_NAME(HistogramTransferData)
+ SET_SELF_SIZE(HistogramTransferData)
+
+ private:
+ std::shared_ptr<Histogram> histogram_;
+ };
+};
+
+class IntervalHistogram : public HandleWrap, public HistogramImpl {
+ public:
+ enum class StartFlags {
+ NONE,
+ RESET
+ };
+
+ static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
+ Environment* env);
+
+ static BaseObjectPtr<IntervalHistogram> Create(
+ Environment* env,
+ int64_t lowest = 1,
+ int64_t highest = std::numeric_limits<int64_t>::max(),
+ int figures = kDefaultHistogramFigures);
+
+ virtual void OnInterval() = 0;
+
+ void MemoryInfo(MemoryTracker* tracker) const override;
+
+ IntervalHistogram(
+ Environment* env,
+ v8::Local<v8::Object> wrap,
+ AsyncWrap::ProviderType type,
+ int32_t interval,
+ int64_t lowest = 1,
int64_t highest = std::numeric_limits<int64_t>::max(),
int figures = kDefaultHistogramFigures);
+ 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 Start(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void Stop(const v8::FunctionCallbackInfo<v8::Value>& args);
+
+ TransferMode GetTransferMode() const override {
+ return TransferMode::kCloneable;
+ }
+ std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
+
private:
- int64_t exceeds_ = 0;
- uint64_t prev_ = 0;
+ static void TimerCB(uv_timer_t* handle);
+ void OnStart(StartFlags flags = StartFlags::RESET);
+ void OnStop();
+
+ bool enabled_ = false;
+ int32_t interval_ = 0;
+ uv_timer_t timer_;
};
} // namespace node