27
27
#include " node.h"
28
28
#include " node_external_reference.h"
29
29
#include " util-inl.h"
30
+ #include " v8-profiler.h"
30
31
#include " v8.h"
31
32
32
33
namespace node {
@@ -35,6 +36,9 @@ using v8::Array;
35
36
using v8::BigInt;
36
37
using v8::CFunction;
37
38
using v8::Context;
39
+ using v8::CpuProfile;
40
+ using v8::CpuProfilingResult;
41
+ using v8::CpuProfilingStatus;
38
42
using v8::DictionaryTemplate;
39
43
using v8::FunctionCallbackInfo;
40
44
using v8::FunctionTemplate;
@@ -47,6 +51,8 @@ using v8::Isolate;
47
51
using v8::Local;
48
52
using v8::LocalVector;
49
53
using v8::MaybeLocal;
54
+ using v8::Name;
55
+ using v8::Number;
50
56
using v8::Object;
51
57
using v8::ScriptCompiler;
52
58
using v8::String;
@@ -243,6 +249,39 @@ void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
243
249
V8::SetFlagsFromString (*flags, static_cast <size_t >(flags.length ()));
244
250
}
245
251
252
+ void StartCpuProfile (const FunctionCallbackInfo<Value>& args) {
253
+ Environment* env = Environment::GetCurrent (args);
254
+ Isolate* isolate = env->isolate ();
255
+ CpuProfilingResult result = env->StartCpuProfile ();
256
+ if (result.status == CpuProfilingStatus::kErrorTooManyProfilers ) {
257
+ return THROW_ERR_CPU_PROFILE_TOO_MANY (isolate,
258
+ " There are too many CPU profiles" );
259
+ } else if (result.status == CpuProfilingStatus::kStarted ) {
260
+ args.GetReturnValue ().Set (Number::New (isolate, result.id ));
261
+ }
262
+ }
263
+
264
+ void StopCpuProfile (const FunctionCallbackInfo<Value>& args) {
265
+ Environment* env = Environment::GetCurrent (args);
266
+ Isolate* isolate = env->isolate ();
267
+ CHECK (args[0 ]->IsUint32 ());
268
+ uint32_t profile_id = args[0 ]->Uint32Value (env->context ()).FromJust ();
269
+ CpuProfile* profile = env->StopCpuProfile (profile_id);
270
+ if (!profile) {
271
+ return THROW_ERR_CPU_PROFILE_NOT_STARTED (isolate,
272
+ " CPU profile not started" );
273
+ }
274
+ auto json_out_stream = std::make_unique<node::JSONOutputStream>();
275
+ profile->Serialize (json_out_stream.get (),
276
+ CpuProfile::SerializationFormat::kJSON );
277
+ profile->Delete ();
278
+ Local<Value> ret;
279
+ if (ToV8Value (env->context (), json_out_stream->out_stream ().str (), isolate)
280
+ .ToLocal (&ret)) {
281
+ args.GetReturnValue ().Set (ret);
282
+ }
283
+ }
284
+
246
285
static void IsStringOneByteRepresentation (
247
286
const FunctionCallbackInfo<Value>& args) {
248
287
CHECK_EQ (args.Length (), 1 );
@@ -701,6 +740,9 @@ void Initialize(Local<Object> target,
701
740
// Export symbols used by v8.setFlagsFromString()
702
741
SetMethod (context, target, " setFlagsFromString" , SetFlagsFromString);
703
742
743
+ SetMethod (context, target, " startCpuProfile" , StartCpuProfile);
744
+ SetMethod (context, target, " stopCpuProfile" , StopCpuProfile);
745
+
704
746
// Export symbols used by v8.isStringOneByteRepresentation()
705
747
SetFastMethodNoSideEffect (context,
706
748
target,
@@ -745,6 +787,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
745
787
registry->Register (GetCppHeapStatistics);
746
788
registry->Register (IsStringOneByteRepresentation);
747
789
registry->Register (fast_is_string_one_byte_representation_);
790
+ registry->Register (StartCpuProfile);
791
+ registry->Register (StopCpuProfile);
748
792
}
749
793
750
794
} // namespace v8_utils
0 commit comments