• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_NODE_PERF_H_
2 #define SRC_NODE_PERF_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "node.h"
7 #include "node_perf_common.h"
8 #include "base_object-inl.h"
9 #include "histogram.h"
10 
11 #include "v8.h"
12 #include "uv.h"
13 
14 #include <string>
15 
16 namespace node {
17 
18 class Environment;
19 
20 namespace performance {
21 
22 extern const uint64_t timeOrigin;
23 
GetPerformanceMilestoneName(enum PerformanceMilestone milestone)24 static inline const char* GetPerformanceMilestoneName(
25     enum PerformanceMilestone milestone) {
26   switch (milestone) {
27 #define V(name, label) case NODE_PERFORMANCE_MILESTONE_##name: return label;
28   NODE_PERFORMANCE_MILESTONES(V)
29 #undef V
30     default:
31       UNREACHABLE();
32   }
33 }
34 
ToPerformanceMilestoneEnum(const char * str)35 static inline PerformanceMilestone ToPerformanceMilestoneEnum(const char* str) {
36 #define V(name, label)                                                        \
37   if (strcmp(str, label) == 0) return NODE_PERFORMANCE_MILESTONE_##name;
38   NODE_PERFORMANCE_MILESTONES(V)
39 #undef V
40   return NODE_PERFORMANCE_MILESTONE_INVALID;
41 }
42 
ToPerformanceEntryTypeEnum(const char * type)43 static inline PerformanceEntryType ToPerformanceEntryTypeEnum(
44     const char* type) {
45 #define V(name, label)                                                        \
46   if (strcmp(type, label) == 0) return NODE_PERFORMANCE_ENTRY_TYPE_##name;
47   NODE_PERFORMANCE_ENTRY_TYPES(V)
48 #undef V
49   return NODE_PERFORMANCE_ENTRY_TYPE_INVALID;
50 }
51 
52 class PerformanceEntry {
53  public:
54   static void Notify(Environment* env,
55                      PerformanceEntryType type,
56                      v8::Local<v8::Value> object);
57 
58   static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
59 
PerformanceEntry(Environment * env,const char * name,const char * type,uint64_t startTime,uint64_t endTime)60   PerformanceEntry(Environment* env,
61                    const char* name,
62                    const char* type,
63                    uint64_t startTime,
64                    uint64_t endTime) : env_(env),
65                                        name_(name),
66                                        type_(type),
67                                        startTime_(startTime),
68                                        endTime_(endTime) { }
69 
70   virtual ~PerformanceEntry() = default;
71 
72   virtual v8::MaybeLocal<v8::Object> ToObject() const;
73 
env()74   Environment* env() const { return env_; }
75 
name()76   const std::string& name() const { return name_; }
77 
type()78   const std::string& type() const { return type_; }
79 
kind()80   PerformanceEntryType kind() {
81     return ToPerformanceEntryTypeEnum(type().c_str());
82   }
83 
startTime()84   double startTime() const { return startTimeNano() / 1e6; }
85 
duration()86   double duration() const { return durationNano() / 1e6; }
87 
startTimeNano()88   uint64_t startTimeNano() const { return startTime_ - timeOrigin; }
89 
durationNano()90   uint64_t durationNano() const { return endTime_ - startTime_; }
91 
92  private:
93   Environment* env_;
94   const std::string name_;
95   const std::string type_;
96   const uint64_t startTime_;
97   const uint64_t endTime_;
98 };
99 
100 enum PerformanceGCKind {
101   NODE_PERFORMANCE_GC_MAJOR = v8::GCType::kGCTypeMarkSweepCompact,
102   NODE_PERFORMANCE_GC_MINOR = v8::GCType::kGCTypeScavenge,
103   NODE_PERFORMANCE_GC_INCREMENTAL = v8::GCType::kGCTypeIncrementalMarking,
104   NODE_PERFORMANCE_GC_WEAKCB = v8::GCType::kGCTypeProcessWeakCallbacks
105 };
106 
107 enum PerformanceGCFlags {
108   NODE_PERFORMANCE_GC_FLAGS_NO =
109     v8::GCCallbackFlags::kNoGCCallbackFlags,
110   NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED =
111     v8::GCCallbackFlags::kGCCallbackFlagConstructRetainedObjectInfos,
112   NODE_PERFORMANCE_GC_FLAGS_FORCED =
113     v8::GCCallbackFlags::kGCCallbackFlagForced,
114   NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING =
115     v8::GCCallbackFlags::kGCCallbackFlagSynchronousPhantomCallbackProcessing,
116   NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE =
117     v8::GCCallbackFlags::kGCCallbackFlagCollectAllAvailableGarbage,
118   NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY =
119     v8::GCCallbackFlags::kGCCallbackFlagCollectAllExternalMemory,
120   NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE =
121     v8::GCCallbackFlags::kGCCallbackScheduleIdleGarbageCollection
122 };
123 
124 class GCPerformanceEntry : public PerformanceEntry {
125  public:
GCPerformanceEntry(Environment * env,PerformanceGCKind gckind,PerformanceGCFlags gcflags,uint64_t startTime,uint64_t endTime)126   GCPerformanceEntry(Environment* env,
127                      PerformanceGCKind gckind,
128                      PerformanceGCFlags gcflags,
129                      uint64_t startTime,
130                      uint64_t endTime) :
131                          PerformanceEntry(env, "gc", "gc", startTime, endTime),
132                          gckind_(gckind),
133                          gcflags_(gcflags) { }
134 
gckind()135   PerformanceGCKind gckind() const { return gckind_; }
gcflags()136   PerformanceGCFlags gcflags() const { return gcflags_; }
137 
138  private:
139   PerformanceGCKind gckind_;
140   PerformanceGCFlags gcflags_;
141 };
142 
143 class ELDHistogram : public IntervalHistogram {
144  public:
145   static void Initialize(Environment* env, v8::Local<v8::Object> target);
146   static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
147 
148   ELDHistogram(
149       Environment* env,
150       v8::Local<v8::Object> wrap,
151       int32_t interval);
152 
153   void OnInterval() override;
154 
155   SET_MEMORY_INFO_NAME(ELDHistogram)
156   SET_SELF_SIZE(ELDHistogram)
157 };
158 
159 }  // namespace performance
160 }  // namespace node
161 
162 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
163 
164 #endif  // SRC_NODE_PERF_H_
165