diff --git a/src/workerd/io/cdp.capnp b/src/workerd/io/cdp.capnp index f0bc874f..e6f1b706 100644 --- a/src/workerd/io/cdp.capnp +++ b/src/workerd/io/cdp.capnp @@ -287,54 +287,6 @@ struct Network { } } -struct Profiler { - - struct PositionTickInfo { - line @0 :Int32; - ticks @1 :Int32; - } - - struct ProfileNode { - id @0 :Int32; - callFrame @1 :Runtime.CallFrame; - hitCount @2 :Int32; - children @3 :List(Int32); - deoptReason @4 :Text; - positionTicks @5 :List(PositionTickInfo); - } - - struct Profile { - nodes @0 :List(ProfileNode); - startTime @1 :Float64; - endTime @2 :Float64; - samples @3 :List(Int32); - timeDeltas @4 :List(Int32); - } - - struct Command { - struct Enable { - struct Params {} - struct Result {} - } - struct SetSamplingInterval { - struct Params { - interval @0 :Int32; - } - struct Result {} - } - struct Start { - struct Params {} - struct Result {} - } - struct Stop { - struct Params {} - struct Result { - profile @0 :Profile; - } - } - } -} - struct HeapProfiler { struct Command { struct TakeHeapSnapshot { @@ -379,11 +331,11 @@ struct Command $Json.discriminator(name = "method") { networkEnable @2 :Method(Network.Command.Enable.Params, Network.Command.Enable.Result) $Json.name("Network.enable") $Json.flatten(); networkDisable @3 :Method(Network.Command.Disable.Params, Network.Command.Disable.Result) $Json.name("Network.disable") $Json.flatten(); networkGetResponseBody @4 :Method(Network.Command.GetResponseBody.Params, Network.Command.GetResponseBody.Result) $Json.name("Network.getResponseBody") $Json.flatten(); - profilerSetSamplingInterval @5 :Method(Profiler.Command.SetSamplingInterval.Params, Profiler.Command.SetSamplingInterval.Result) $Json.name("Profiler.setSamplingInterval") $Json.flatten(); - profilerEnable @6 :Method(Profiler.Command.Enable.Params, Profiler.Command.Enable.Result) $Json.name("Profiler.enable") $Json.flatten(); - profilerStart @7 :Method(Profiler.Command.Start.Params, Profiler.Command.Start.Result) $Json.name("Profiler.start") $Json.flatten(); - profilerStop @8 :Method(Profiler.Command.Stop.Params, Profiler.Command.Stop.Result) $Json.name("Profiler.stop") $Json.flatten(); - takeHeapSnapshot @9 : Method(HeapProfiler.Command.TakeHeapSnapshot.Params, HeapProfiler.Command.TakeHeapSnapshot.Result) $Json.name("HeapProfiler.takeHeapSnapshot") $Json.flatten(); + # profilerSetSamplingInterval @5 :Method(Profiler.Command.SetSamplingInterval.Params, Profiler.Command.SetSamplingInterval.Result) $Json.name("Profiler.setSamplingInterval") $Json.flatten(); + # profilerEnable @6 :Method(Profiler.Command.Enable.Params, Profiler.Command.Enable.Result) $Json.name("Profiler.enable") $Json.flatten(); + # profilerStart @7 :Method(Profiler.Command.Start.Params, Profiler.Command.Start.Result) $Json.name("Profiler.start") $Json.flatten(); + # profilerStop @8 :Method(Profiler.Command.Stop.Params, Profiler.Command.Stop.Result) $Json.name("Profiler.stop") $Json.flatten(); + # takeHeapSnapshot @5 : Method(HeapProfiler.Command.TakeHeapSnapshot.Params, HeapProfiler.Command.TakeHeapSnapshot.Result) $Json.name("HeapProfiler.takeHeapSnapshot") $Json.flatten(); } } diff --git a/src/workerd/io/worker.c++ b/src/workerd/io/worker.c++ index 15f8de8c..8c29500c 100644 --- a/src/workerd/io/worker.c++ +++ b/src/workerd/io/worker.c++ @@ -488,7 +488,6 @@ struct Worker::Isolate::Impl { InspectorClient inspectorClient; kj::Maybe> inspector; InspectorPolicy inspectorPolicy; - kj::Maybe> profiler; ActorCache::SharedLru actorCacheLru; // Notification messages to deliver to the next inspector client when it connects. @@ -697,107 +696,6 @@ struct Worker::Isolate::Impl { } }; -namespace { - -class CpuProfilerDisposer final: public kj::Disposer { -public: - virtual void disposeImpl(void* pointer) const override { - reinterpret_cast(pointer)->Dispose(); - } - - static const CpuProfilerDisposer instance; -}; - -const CpuProfilerDisposer CpuProfilerDisposer::instance {}; - -static constexpr kj::StringPtr PROFILE_NAME = "Default Profile"_kj; - -static void setSamplingInterval(v8::CpuProfiler& profiler, int interval) { - profiler.SetSamplingInterval(interval); -} - -static void startProfiling(jsg::Lock& js, v8::CpuProfiler& profiler) { - js.withinHandleScope([&] { - v8::CpuProfilingOptions options( - v8::kLeafNodeLineNumbers, - v8::CpuProfilingOptions::kNoSampleLimit - ); - profiler.StartProfiling(jsg::v8StrIntern(js.v8Isolate, PROFILE_NAME), kj::mv(options)); - }); -} - -static void stopProfiling(jsg::Lock& js, - v8::CpuProfiler& profiler, - cdp::Command::Builder& cmd) { - js.withinHandleScope([&] { - auto cpuProfile = profiler.StopProfiling(jsg::v8StrIntern(js.v8Isolate, PROFILE_NAME)); - if (cpuProfile == nullptr) return; // profiling never started - - kj::Vector allNodes; - kj::Vector unvisited; - - unvisited.add(cpuProfile->GetTopDownRoot()); - while (!unvisited.empty()) { - auto next = unvisited.back(); - allNodes.add(next); - unvisited.removeLast(); - for (int i=0; i < next->GetChildrenCount(); i++) { - unvisited.add(next->GetChild(i)); - } - } - - auto res = cmd.getProfilerStop().initResult(); - auto profile = res.initProfile(); - profile.setStartTime(cpuProfile->GetStartTime()); - profile.setEndTime(cpuProfile->GetEndTime()); - - auto nodes = profile.initNodes(allNodes.size()); - for (auto i : kj::indices(allNodes)) { - auto nodeBuilder = nodes[i]; - nodeBuilder.setId(allNodes[i]->GetNodeId()); - - auto callFrame = nodeBuilder.initCallFrame(); - callFrame.setFunctionName(allNodes[i]->GetFunctionNameStr()); - callFrame.setScriptId(kj::str(allNodes[i]->GetScriptId())); - callFrame.setUrl(allNodes[i]->GetScriptResourceNameStr()); - // V8 locations are 1-based, but CDP locations are 0-based... - callFrame.setLineNumber(allNodes[i]->GetLineNumber() - 1); - callFrame.setColumnNumber(allNodes[i]->GetColumnNumber() - 1); - - nodeBuilder.setHitCount(allNodes[i]->GetHitCount()); - - auto children = nodeBuilder.initChildren(allNodes[i]->GetChildrenCount()); - for (int j=0; j < allNodes[i]->GetChildrenCount(); j++) { - children.set(j, allNodes[i]->GetChild(j)->GetNodeId()); - } - - auto hitLineCount = allNodes[i]->GetHitLineCount(); - auto lineBuffer = kj::heapArray(hitLineCount); - allNodes[i]->GetLineTicks(lineBuffer.begin(), lineBuffer.size()); - - auto positionTicks = nodeBuilder.initPositionTicks(hitLineCount); - for (uint j=0; j < hitLineCount; j++) { - auto positionTick = positionTicks[j]; - positionTick.setLine(lineBuffer[j].line); - positionTick.setTicks(lineBuffer[j].hit_count); - } - } - - auto sampleCount = cpuProfile->GetSamplesCount(); - auto samples = profile.initSamples(sampleCount); - auto timeDeltas = profile.initTimeDeltas(sampleCount); - auto lastTimestamp = cpuProfile->GetStartTime(); - for (int i=0; i < sampleCount; i++) { - samples.set(i, cpuProfile->GetSample(i)->GetNodeId()); - auto sampleTime = cpuProfile->GetSampleTimestamp(i); - timeDeltas.set(i, sampleTime - lastTimestamp); - lastTimestamp = sampleTime; - } - }); -} - -} // anonymous namespace - struct Worker::Script::Impl { kj::OneOf unboundScriptOrMainModule; @@ -2333,42 +2231,6 @@ public: err.setMessage("Network.getResponseBody is not supported in this fork"); break; } - case cdp::Command::PROFILER_STOP: { - KJ_IF_SOME(p, isolate.impl->profiler) { - auto& lock = recordedLock.lock; - stopProfiling(*lock, *p, cmd); - } - break; - } - case cdp::Command::PROFILER_START: { - KJ_IF_SOME(p, isolate.impl->profiler) { - auto& lock = recordedLock.lock; - startProfiling(*lock, *p); - } - break; - } - case cdp::Command::PROFILER_SET_SAMPLING_INTERVAL: { - KJ_IF_SOME(p, isolate.impl->profiler) { - auto interval = cmd.getProfilerSetSamplingInterval().getParams().getInterval(); - setSamplingInterval(*p, interval); - } - break; - } - case cdp::Command::PROFILER_ENABLE: { - auto& lock = recordedLock.lock; - isolate.impl->profiler = kj::Own( - v8::CpuProfiler::New(lock->v8Isolate, v8::kDebugNaming, v8::kLazyLogging), - CpuProfilerDisposer::instance); - break; - } - case cdp::Command::TAKE_HEAP_SNAPSHOT: { - auto& lock = recordedLock.lock; - auto params = cmd.getTakeHeapSnapshot().getParams(); - takeHeapSnapshot(*lock, - params.getExposeInternals(), - params.getCaptureNumericValue()); - break; - } } if (!cmd.isUnknown()) {