1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
#ifndef SRC_NODE_PERF_H_
#define SRC_NODE_PERF_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "base_object-inl.h"
#include "histogram.h"
#include "node.h"
#include "node_internals.h"
#include "node_perf_common.h"
#include "v8.h"
#include "uv.h"
#include <string>
namespace node {
class Environment;
class ExternalReferenceRegistry;
namespace performance {
extern const uint64_t timeOrigin;
inline const char* GetPerformanceMilestoneName(
PerformanceMilestone milestone) {
switch (milestone) {
#define V(name, label) case NODE_PERFORMANCE_MILESTONE_##name: return label;
NODE_PERFORMANCE_MILESTONES(V)
#undef V
default:
UNREACHABLE();
}
}
inline PerformanceMilestone ToPerformanceMilestoneEnum(const char* str) {
#define V(name, label) \
if (strcmp(str, label) == 0) return NODE_PERFORMANCE_MILESTONE_##name;
NODE_PERFORMANCE_MILESTONES(V)
#undef V
return NODE_PERFORMANCE_MILESTONE_INVALID;
}
inline const char* GetPerformanceEntryTypeName(
PerformanceEntryType type) {
switch (type) {
#define V(name, label) case NODE_PERFORMANCE_ENTRY_TYPE_##name: return label;
NODE_PERFORMANCE_ENTRY_TYPES(V)
#undef V
default:
UNREACHABLE();
}
}
inline PerformanceEntryType ToPerformanceEntryTypeEnum(
const char* type) {
#define V(name, label) \
if (strcmp(type, label) == 0) return NODE_PERFORMANCE_ENTRY_TYPE_##name;
NODE_PERFORMANCE_ENTRY_TYPES(V)
#undef V
return NODE_PERFORMANCE_ENTRY_TYPE_INVALID;
}
enum PerformanceGCKind {
NODE_PERFORMANCE_GC_MAJOR = v8::GCType::kGCTypeMarkSweepCompact,
NODE_PERFORMANCE_GC_MINOR = v8::GCType::kGCTypeScavenge,
NODE_PERFORMANCE_GC_INCREMENTAL = v8::GCType::kGCTypeIncrementalMarking,
NODE_PERFORMANCE_GC_WEAKCB = v8::GCType::kGCTypeProcessWeakCallbacks
};
enum PerformanceGCFlags {
NODE_PERFORMANCE_GC_FLAGS_NO =
v8::GCCallbackFlags::kNoGCCallbackFlags,
NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED =
v8::GCCallbackFlags::kGCCallbackFlagConstructRetainedObjectInfos,
NODE_PERFORMANCE_GC_FLAGS_FORCED =
v8::GCCallbackFlags::kGCCallbackFlagForced,
NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING =
v8::GCCallbackFlags::kGCCallbackFlagSynchronousPhantomCallbackProcessing,
NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE =
v8::GCCallbackFlags::kGCCallbackFlagCollectAllAvailableGarbage,
NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY =
v8::GCCallbackFlags::kGCCallbackFlagCollectAllExternalMemory,
NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE =
v8::GCCallbackFlags::kGCCallbackScheduleIdleGarbageCollection
};
template <typename Traits>
struct PerformanceEntry {
using Details = typename Traits::Details;
std::string name;
double start_time;
double duration;
Details details;
PerformanceEntry(
const std::string& name_,
double start_time_,
double duration_,
const Details& details_)
: name(name_),
start_time(start_time_),
duration(duration_),
details(details_) {}
static v8::MaybeLocal<v8::Object> GetDetails(
Environment* env,
const PerformanceEntry<Traits>& entry) {
return Traits::GetDetails(env, entry);
}
void Notify(Environment* env) {
v8::HandleScope handle_scope(env->isolate());
v8::Context::Scope scope(env->context());
AliasedUint32Array& observers = env->performance_state()->observers;
if (env->performance_entry_callback().IsEmpty() ||
!observers[Traits::kType]) {
return;
}
v8::Local<v8::Object> detail;
if (!Traits::GetDetails(env, *this).ToLocal(&detail)) {
// TODO(@jasnell): Handle the error here
return;
}
v8::Local<v8::Value> argv[] = {
OneByteString(env->isolate(), name.c_str()),
OneByteString(env->isolate(), GetPerformanceEntryTypeName(Traits::kType)),
v8::Number::New(env->isolate(), start_time),
v8::Number::New(env->isolate(), duration),
detail
};
node::MakeSyncCallback(
env->isolate(),
env->context()->Global(),
env->performance_entry_callback(),
arraysize(argv),
argv);
}
};
struct GCPerformanceEntryTraits {
static constexpr PerformanceEntryType kType =
NODE_PERFORMANCE_ENTRY_TYPE_GC;
struct Details {
PerformanceGCKind kind;
PerformanceGCFlags flags;
Details(PerformanceGCKind kind_, PerformanceGCFlags flags_)
: kind(kind_), flags(flags_) {}
};
static v8::MaybeLocal<v8::Object> GetDetails(
Environment* env,
const PerformanceEntry<GCPerformanceEntryTraits>& entry);
};
using GCPerformanceEntry = PerformanceEntry<GCPerformanceEntryTraits>;
} // namespace performance
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_NODE_PERF_H_
|