|
28 | 28 | #include "node_external_reference.h" |
29 | 29 | #include "util-inl.h" |
30 | 30 | #include "v8.h" |
| 31 | +#include "v8-profiler.h" |
31 | 32 |
|
32 | 33 | namespace node { |
33 | 34 | namespace v8_utils { |
34 | 35 | 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::CpuProfiler; |
| 41 | +using v8::CpuProfilingStatus; |
38 | 42 | using v8::FunctionCallbackInfo; |
39 | 43 | using v8::FunctionTemplate; |
40 | 44 | using v8::HandleScope; |
@@ -243,6 +247,62 @@ void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) { |
243 | 247 | V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length())); |
244 | 248 | } |
245 | 249 |
|
| 250 | +class JSONOutputStream : public v8::OutputStream { |
| 251 | + public: |
| 252 | + JSONOutputStream() {} |
| 253 | + |
| 254 | + int GetChunkSize() override { return 65536; } |
| 255 | + |
| 256 | + void EndOfStream() override {} |
| 257 | + |
| 258 | + WriteResult WriteAsciiChunk(char* data, const int size) override { |
| 259 | + out_stream_.write(data, size); |
| 260 | + return kContinue; |
| 261 | + } |
| 262 | + |
| 263 | + std::ostringstream& out_stream() { return out_stream_; } |
| 264 | + |
| 265 | + private: |
| 266 | + std::ostringstream out_stream_; |
| 267 | +}; |
| 268 | + |
| 269 | +void StartCpuProfile(const FunctionCallbackInfo<Value>& args) { |
| 270 | + Environment* env = Environment::GetCurrent(args); |
| 271 | + CHECK(args[0]->IsString()); |
| 272 | + String::Utf8Value name(env->isolate(), args[0]); |
| 273 | + if (!env->cpu_profiler()) { |
| 274 | + env->set_cpu_profiler(CpuProfiler::New(env->isolate())); |
| 275 | + } |
| 276 | + Local<String> title = String::NewFromUtf8(isolate, |
| 277 | + *name, |
| 278 | + NewStringType::kNormal, |
| 279 | + name.length()) |
| 280 | + .ToLocalChecked(); |
| 281 | + CpuProfilingStatus status = env->cpu_profiler()->StartProfiling(title, true); |
| 282 | + args.GetReturnValue().Set(Number::New(env->isolate(), static_cast<double>(status))); |
| 283 | +} |
| 284 | + |
| 285 | +void StopCpuProfile(const FunctionCallbackInfo<Value>& args) { |
| 286 | + Environment* env = Environment::GetCurrent(args); |
| 287 | + CHECK(args[0]->IsString()); |
| 288 | + String::Utf8Value name(env->isolate(), args[0]); |
| 289 | + Local<String> title = String::NewFromUtf8(env->isolate(), |
| 290 | + *name, |
| 291 | + NewStringType::kNormal, |
| 292 | + name.length()) |
| 293 | + .ToLocalChecked(); |
| 294 | + auto json_out_stream = std::make_unique<JSONOutputStream>(); |
| 295 | + if (env->cpu_profiler()) { |
| 296 | + CpuProfile* profile = env->cpu_profiler()->StopProfiling(title); |
| 297 | + if (profile) { |
| 298 | + profile->Serialize(json_out_stream.get(), |
| 299 | + CpuProfile::SerializationFormat::kJSON); |
| 300 | + profile->Delete(); |
| 301 | + } |
| 302 | + } |
| 303 | + args.GetReturnValue().Set(json_out_stream->out_stream().str()); |
| 304 | +} |
| 305 | + |
246 | 306 | static void IsStringOneByteRepresentation( |
247 | 307 | const FunctionCallbackInfo<Value>& args) { |
248 | 308 | CHECK_EQ(args.Length(), 1); |
@@ -682,6 +742,9 @@ void Initialize(Local<Object> target, |
682 | 742 | // Export symbols used by v8.setFlagsFromString() |
683 | 743 | SetMethod(context, target, "setFlagsFromString", SetFlagsFromString); |
684 | 744 |
|
| 745 | + SetMethod(context, target, "startCpuProfile", StartCpuProfile); |
| 746 | + SetMethod(context, target, "stopCpuProfile", StopCpuProfile); |
| 747 | + |
685 | 748 | // Export symbols used by v8.isStringOneByteRepresentation() |
686 | 749 | SetFastMethodNoSideEffect(context, |
687 | 750 | target, |
@@ -726,6 +789,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
726 | 789 | registry->Register(GetCppHeapStatistics); |
727 | 790 | registry->Register(IsStringOneByteRepresentation); |
728 | 791 | registry->Register(fast_is_string_one_byte_representation_); |
| 792 | + registry->Register(StartCpuProfile); |
| 793 | + registry->Register(StopCpuProfile); |
729 | 794 | } |
730 | 795 |
|
731 | 796 | } // namespace v8_utils |
|
0 commit comments