11 namespace performance {
14 using v8::ArrayBuffer;
17 using v8::FunctionCallbackInfo;
18 using v8::FunctionTemplate;
19 using v8::HandleScope;
25 using v8::ObjectTemplate;
26 using v8::PropertyCallbackInfo;
38 Environment* env = Environment::GetCurrent(args);
39 Isolate* isolate = env->isolate();
40 Utf8Value name(isolate, args[0]);
41 Utf8Value type(isolate, args[1]);
43 new PerformanceEntry(env, args.This(), *name, *type, now, now);
46 void PerformanceEntry::NotifyObservers(Environment* env,
47 PerformanceEntry* entry) {
48 uint32_t* observers = env->performance_state()->observers;
50 if (observers ==
nullptr ||
55 Local<Context> context = env->context();
56 Isolate* isolate = env->isolate();
57 Local<Value> argv = entry->object();
58 env->performance_entry_callback()->Call(context,
59 v8::Undefined(isolate),
60 1, &argv).ToLocalChecked();
63 void Mark(
const FunctionCallbackInfo<Value>& args) {
64 Environment* env = Environment::GetCurrent(args);
65 Local<Context> context = env->context();
66 Isolate* isolate = env->isolate();
67 Utf8Value name(isolate, args[0]);
69 auto marks = env->performance_marks();
70 (*marks)[*name] = now;
75 Local<Function> fn = env->performance_entry_template();
76 Local<Object> obj = fn->NewInstance(context).ToLocalChecked();
77 new PerformanceEntry(env, obj, *name,
"mark", now, now);
78 args.GetReturnValue().Set(obj);
82 auto marks = env->performance_marks();
83 auto res = marks->find(name);
84 return res != marks->end() ? res->second : 0;
87 void Measure(
const FunctionCallbackInfo<Value>& args) {
88 Environment* env = Environment::GetCurrent(args);
89 Local<Context> context = env->context();
90 Isolate* isolate = env->isolate();
91 Utf8Value name(isolate, args[0]);
92 Utf8Value startMark(isolate, args[1]);
93 Utf8Value endMark(isolate, args[2]);
95 double* milestones = env->performance_state()->milestones;
100 startTimestamp = start;
104 startTimestamp = milestones[milestone];
108 if (endTimestamp == 0) {
111 endTimestamp = milestones[milestone];
114 if (endTimestamp < startTimestamp)
115 endTimestamp = startTimestamp;
120 Local<Function> fn = env->performance_entry_template();
121 Local<Object> obj = fn->NewInstance(context).ToLocalChecked();
122 new PerformanceEntry(env, obj, *name,
"measure",
123 startTimestamp, endTimestamp);
124 args.GetReturnValue().Set(obj);
128 const PropertyCallbackInfo<Value>& info) {
129 Isolate* isolate = info.GetIsolate();
130 PerformanceEntry* entry;
131 ASSIGN_OR_RETURN_UNWRAP(&entry, info.Holder());
132 info.GetReturnValue().Set(
133 String::NewFromUtf8(isolate, entry->name().c_str(), String::kNormalString));
137 const PropertyCallbackInfo<Value>& info) {
138 Isolate* isolate = info.GetIsolate();
139 PerformanceEntry* entry;
140 ASSIGN_OR_RETURN_UNWRAP(&entry, info.Holder());
141 info.GetReturnValue().Set(
142 String::NewFromUtf8(isolate, entry->type().c_str(), String::kNormalString));
146 const PropertyCallbackInfo<Value>& info) {
147 PerformanceEntry* entry;
148 ASSIGN_OR_RETURN_UNWRAP(&entry, info.Holder());
149 info.GetReturnValue().Set(entry->startTime());
153 const PropertyCallbackInfo<Value>& info) {
154 PerformanceEntry* entry;
155 ASSIGN_OR_RETURN_UNWRAP(&entry, info.Holder());
156 info.GetReturnValue().Set(entry->duration());
160 Environment* env = Environment::GetCurrent(args);
161 Local<Context> context = env->context();
162 double* milestones = env->performance_state()->milestones;
165 args[0]->Int32Value(context).ToChecked());
172 Environment* env = Environment::GetCurrent(args);
173 CHECK(args[0]->IsFunction());
174 env->set_performance_entry_callback(args[0].As<Function>());
180 Isolate* isolate = Isolate::GetCurrent();
181 HandleScope scope(isolate);
182 Environment* env = Environment::GetCurrent(isolate);
183 Local<Context> context = env->context();
186 PerformanceGCKind kind =
static_cast<PerformanceGCKind
>(data->data());
188 uint32_t* observers = env->performance_state()->observers;
189 if (!observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]) {
193 fn = env->performance_entry_template();
194 obj = fn->NewInstance(context).ToLocalChecked();
196 FIXED_ONE_BYTE_STRING(isolate,
"kind"),
198 new PerformanceEntry(env, obj, data);
202 auto closeCB = [](uv_handle_t* handle) {
delete handle; };
203 uv_close(reinterpret_cast<uv_handle_t*>(handle), closeCB);
208 v8::GCCallbackFlags flags) {
210 performance_last_gc_type_ = type;
215 v8::GCCallbackFlags flags) {
216 uv_async_t *async =
new uv_async_t;
219 performance_last_gc_start_mark_,
222 uv_async_send(async);
230 inline Local<Value>
GetName(Local<Function> fn) {
231 Local<Value> val = fn->GetDebugName();
232 if (val.IsEmpty() || val->IsUndefined()) {
233 Local<Value> boundFunction = fn->GetBoundFunction();
234 if (!boundFunction.IsEmpty() && !boundFunction->IsUndefined()) {
235 val =
GetName(boundFunction.As<Function>());
242 Isolate* isolate = args.GetIsolate();
243 HandleScope scope(isolate);
244 Environment* env = Environment::GetCurrent(isolate);
245 Local<Context> context = env->context();
246 Local<Function> fn = args.Data().As<Function>();
247 size_t count = args.Length();
249 std::vector<Local<Value>> call_args;
250 for (
size_t i = 0; i < count; ++i) {
251 call_args.push_back(args[i]);
254 Utf8Value name(isolate,
GetName(fn));
258 v8::TryCatch try_catch(isolate);
259 if (args.IsConstructCall()) {
261 v8::MaybeLocal<Object> ret = fn->NewInstance(context,
269 args.GetReturnValue().Set(ret.ToLocalChecked());
272 v8::MaybeLocal<Value> ret = fn->Call(context,
281 args.GetReturnValue().Set(ret.ToLocalChecked());
285 uint32_t* observers = env->performance_state()->observers;
286 if (!observers[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION])
289 Local<Function> ctor = env->performance_entry_template();
290 v8::MaybeLocal<Object> instance = ctor->NewInstance(context);
291 Local<Object> obj = instance.ToLocalChecked();
292 for (idx = 0; idx < count; idx++) {
293 obj->Set(context, idx, args[idx]).ToChecked();
295 new PerformanceEntry(env, obj, *name,
"function", start, end);
298 void Timerify(
const FunctionCallbackInfo<Value>& args) {
299 Environment* env = Environment::GetCurrent(args);
300 Local<Context> context = env->context();
301 CHECK(args[0]->IsFunction());
302 CHECK(args[1]->IsNumber());
303 Local<Function> fn = args[0].As<Function>();
304 int length = args[1]->IntegerValue(context).ToChecked();
305 Local<Function>
wrap =
307 args.GetReturnValue().Set(wrap);
310 void Init(Local<Object> target,
312 Local<Context> context) {
313 Environment* env = Environment::GetCurrent(context);
314 Isolate* isolate = env->isolate();
318 #define SET_STATE_TYPEDARRAY(name, type, field) \ 319 target->Set(context, \ 320 FIXED_ONE_BYTE_STRING(isolate, (name)), \ 321 type::New(state_ab, \ 322 offsetof(performance_state, field), \ 323 arraysize(state->field))) \ 327 #undef SET_STATE_TYPEDARRAY 329 Local<String> performanceEntryString =
330 FIXED_ONE_BYTE_STRING(isolate,
"PerformanceEntry");
333 pe->InstanceTemplate()->SetInternalFieldCount(1);
334 pe->SetClassName(performanceEntryString);
335 Local<ObjectTemplate> ot = pe->InstanceTemplate();
337 ot->SetAccessor(FIXED_ONE_BYTE_STRING(isolate,
"entryType"),
339 ot->SetAccessor(FIXED_ONE_BYTE_STRING(isolate,
"startTime"),
341 ot->SetAccessor(FIXED_ONE_BYTE_STRING(isolate,
"duration"),
343 Local<Function> fn = pe->GetFunction();
344 target->Set(performanceEntryString, fn);
345 env->set_performance_entry_template(fn);
347 env->SetMethod(target,
"mark",
Mark);
348 env->SetMethod(target,
"measure",
Measure);
351 env->SetMethod(target,
"timerify",
Timerify);
361 NODE_DEFINE_HIDDEN_CONSTANT(constants, NODE_PERFORMANCE_ENTRY_TYPE_##name); 366 NODE_DEFINE_HIDDEN_CONSTANT(constants, NODE_PERFORMANCE_MILESTONE_##name); 370 v8::PropertyAttribute attr =
371 static_cast<v8::PropertyAttribute
>(v8::ReadOnly | v8::DontDelete);
373 target->DefineOwnProperty(context,
374 FIXED_ONE_BYTE_STRING(isolate,
"timeOrigin"),
378 target->DefineOwnProperty(context,
379 env->constants_string(),
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
#define SET_STATE_TYPEDARRAY(name, type, field)
#define PERFORMANCE_NOW()
union node::cares_wrap::@8::CaresAsyncData::@0 data
char * Data(Local< Value > val)
#define NODE_PERFORMANCE_ENTRY_TYPES(V)
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
#define NODE_PERFORMANCE_MILESTONES(V)
#define NODE_DEFINE_CONSTANT(target, constant)