2828#include  " node_external_reference.h" 
2929#include  " util-inl.h" 
3030#include  " v8.h" 
31+ #include  " v8-profiler.h" 
3132
3233namespace  node  {
3334namespace  v8_utils  {
3435using  v8::Array;
3536using  v8::BigInt;
3637using  v8::CFunction;
3738using  v8::Context;
39+ using  v8::CpuProfile;
40+ using  v8::CpuProfiler;
41+ using  v8::CpuProfilingResult;
42+ using  v8::CpuProfilingStatus;
3843using  v8::FunctionCallbackInfo;
3944using  v8::FunctionTemplate;
4045using  v8::HandleScope;
@@ -47,6 +52,8 @@ using v8::Local;
4752using  v8::LocalVector;
4853using  v8::MaybeLocal;
4954using  v8::Name;
55+ using  v8::NewStringType;
56+ using  v8::Number;
5057using  v8::Object;
5158using  v8::ScriptCompiler;
5259using  v8::String;
@@ -243,6 +250,36 @@ void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
243250  V8::SetFlagsFromString (*flags, static_cast <size_t >(flags.length ()));
244251}
245252
253+ void  StartCpuProfile (const  FunctionCallbackInfo<Value>& args) {
254+   Environment* env = Environment::GetCurrent (args);
255+   Isolate* isolate = env->isolate ();
256+   CpuProfilingResult result = env->StartCpuProfile ();
257+   if  (result.status  == CpuProfilingStatus::kErrorTooManyProfilers ) {
258+     return  THROW_ERR_CPU_PROFILE_TOO_MANY (isolate, " 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, " 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).ToLocal (&ret)) {
279+     args.GetReturnValue ().Set (ret);
280+   }
281+ }
282+ 
246283static  void  IsStringOneByteRepresentation (
247284    const  FunctionCallbackInfo<Value>& args) {
248285  CHECK_EQ (args.Length (), 1 );
@@ -682,6 +719,9 @@ void Initialize(Local<Object> target,
682719  //  Export symbols used by v8.setFlagsFromString()
683720  SetMethod (context, target, " setFlagsFromString" 
684721
722+   SetMethod (context, target, " startCpuProfile" 
723+   SetMethod (context, target, " stopCpuProfile" 
724+ 
685725  //  Export symbols used by v8.isStringOneByteRepresentation()
686726  SetFastMethodNoSideEffect (context,
687727                            target,
@@ -726,6 +766,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
726766  registry->Register (GetCppHeapStatistics);
727767  registry->Register (IsStringOneByteRepresentation);
728768  registry->Register (fast_is_string_one_byte_representation_);
769+   registry->Register (StartCpuProfile);
770+   registry->Register (StopCpuProfile);
729771}
730772
731773}  //  namespace v8_utils
0 commit comments