2727#include  " node.h" 
2828#include  " node_external_reference.h" 
2929#include  " util-inl.h" 
30+ #include  " v8-profiler.h" 
3031#include  " v8.h" 
3132
3233namespace  node  {
@@ -35,6 +36,9 @@ using v8::Array;
3536using  v8::BigInt;
3637using  v8::CFunction;
3738using  v8::Context;
39+ using  v8::CpuProfile;
40+ using  v8::CpuProfilingResult;
41+ using  v8::CpuProfilingStatus;
3842using  v8::FunctionCallbackInfo;
3943using  v8::FunctionTemplate;
4044using  v8::HandleScope;
@@ -47,6 +51,7 @@ using v8::Local;
4751using  v8::LocalVector;
4852using  v8::MaybeLocal;
4953using  v8::Name;
54+ using  v8::Number;
5055using  v8::Object;
5156using  v8::ScriptCompiler;
5257using  v8::String;
@@ -243,6 +248,39 @@ void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
243248  V8::SetFlagsFromString (*flags, static_cast <size_t >(flags.length ()));
244249}
245250
251+ void  StartCpuProfile (const  FunctionCallbackInfo<Value>& args) {
252+   Environment* env = Environment::GetCurrent (args);
253+   Isolate* isolate = env->isolate ();
254+   CpuProfilingResult result = env->StartCpuProfile ();
255+   if  (result.status  == CpuProfilingStatus::kErrorTooManyProfilers ) {
256+     return  THROW_ERR_CPU_PROFILE_TOO_MANY (isolate,
257+                                           " There are too many CPU profiles" 
258+   } else  if  (result.status  == CpuProfilingStatus::kStarted ) {
259+     args.GetReturnValue ().Set (Number::New (isolate, result.id ));
260+   }
261+ }
262+ 
263+ void  StopCpuProfile (const  FunctionCallbackInfo<Value>& args) {
264+   Environment* env = Environment::GetCurrent (args);
265+   Isolate* isolate = env->isolate ();
266+   CHECK (args[0 ]->IsUint32 ());
267+   uint32_t  profile_id = args[0 ]->Uint32Value (env->context ()).FromJust ();
268+   CpuProfile* profile = env->StopCpuProfile (profile_id);
269+   if  (!profile) {
270+     return  THROW_ERR_CPU_PROFILE_NOT_STARTED (isolate,
271+                                              " CPU profile not started" 
272+   }
273+   auto  json_out_stream = std::make_unique<node::JSONOutputStream>();
274+   profile->Serialize (json_out_stream.get (),
275+                      CpuProfile::SerializationFormat::kJSON );
276+   profile->Delete ();
277+   Local<Value> ret;
278+   if  (ToV8Value (env->context (), json_out_stream->out_stream ().str (), isolate)
279+           .ToLocal (&ret)) {
280+     args.GetReturnValue ().Set (ret);
281+   }
282+ }
283+ 
246284static  void  IsStringOneByteRepresentation (
247285    const  FunctionCallbackInfo<Value>& args) {
248286  CHECK_EQ (args.Length (), 1 );
@@ -682,6 +720,9 @@ void Initialize(Local<Object> target,
682720  //  Export symbols used by v8.setFlagsFromString()
683721  SetMethod (context, target, " setFlagsFromString" 
684722
723+   SetMethod (context, target, " startCpuProfile" 
724+   SetMethod (context, target, " stopCpuProfile" 
725+ 
685726  //  Export symbols used by v8.isStringOneByteRepresentation()
686727  SetFastMethodNoSideEffect (context,
687728                            target,
@@ -726,6 +767,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
726767  registry->Register (GetCppHeapStatistics);
727768  registry->Register (IsStringOneByteRepresentation);
728769  registry->Register (fast_is_string_one_byte_representation_);
770+   registry->Register (StartCpuProfile);
771+   registry->Register (StopCpuProfile);
729772}
730773
731774}  //  namespace v8_utils
0 commit comments