From 53c26e804601ecc56afccdbd606ec28ac10bd04d Mon Sep 17 00:00:00 2001 From: yp9522 Date: Thu, 15 May 2025 17:51:22 +0800 Subject: [PATCH] =?UTF-8?q?HeapProfiler=E4=BB=A3=E7=A0=81=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yp9522 Change-Id: I55569c04b1e7fc8cdf22341be07005487b8469e1 --- tooling/agent/heapprofiler_impl.cpp | 86 ++++++++++++++--------------- tooling/protocol_handler.cpp | 10 ++-- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/tooling/agent/heapprofiler_impl.cpp b/tooling/agent/heapprofiler_impl.cpp index 55736eaa..8951e976 100644 --- a/tooling/agent/heapprofiler_impl.cpp +++ b/tooling/agent/heapprofiler_impl.cpp @@ -32,43 +32,43 @@ void HeapProfilerImpl::DispatcherImpl::Dispatch(const DispatchRequest &request) LOG_DEBUGGER(DEBUG) << "dispatch [" << request.GetMethod() << "] to HeapProfilerImpl"; switch (method) { case Method::ADD_INSPECTED_HEAP_OBJECT: - AddInspectedHeapObject(request); + AddInspectedHeapObject(request); //添加已检查的堆对象 没实现 break; case Method::COLLECT_GARBAGE: - CollectGarbage(request); + CollectGarbage(request); //收集垃圾 break; case Method::ENABLE: - Enable(request); + Enable(request);//启用 break; case Method::DISABLE: - Disable(request); + Disable(request);//禁用 break; case Method::GET_HEAP_OBJECT_ID: - GetHeapObjectId(request); + GetHeapObjectId(request); //获取堆对象ID 没实现 break; case Method::GET_OBJECT_BY_HEAP_OBJECT_ID: - GetObjectByHeapObjectId(request); + GetObjectByHeapObjectId(request); //通过堆对象ID获取对象 没实现 break; case Method::GET_SAMPLING_PROFILE: - GetSamplingProfile(request); + GetSamplingProfile(request); //获取采样配置文件 break; case Method::START_SAMPLING: - StartSampling(request); + StartSampling(request); //开始采样 break; case Method::START_TRACKING_HEAP_OBJECTS: - StartTrackingHeapObjects(request); + StartTrackingHeapObjects(request); //开始跟踪堆对象 break; case Method::STOP_SAMPLING: - StopSampling(request); + StopSampling(request); //停止采样 break; case Method::STOP_TRACKING_HEAP_OBJECTS: - StopTrackingHeapObjects(request); + StopTrackingHeapObjects(request); //停止跟踪堆对象 break; case Method::TAKE_HEAP_SNAPSHOT: - TakeHeapSnapshot(request); + TakeHeapSnapshot(request); //拍摄堆快照 break; default: - SendResponse(request, DispatchResponse::Fail("Unknown method: " + request.GetMethod())); + SendResponse(request, DispatchResponse::Fail("Unknown method: " + request.GetMethod())); //发送响应 break; } } @@ -231,7 +231,7 @@ void HeapProfilerImpl::DispatcherImpl::StopTrackingHeapObjects(const DispatchReq void HeapProfilerImpl::DispatcherImpl::TakeHeapSnapshot(const DispatchRequest &request) { - std::unique_ptr params = StopTrackingHeapObjectsParams::Create(request.GetParams()); + std::unique_ptr params = StopTrackingHeapObjectsParams::Create(request.GetParams());// if (params == nullptr) { SendResponse(request, DispatchResponse::Fail("wrong params")); return; @@ -252,7 +252,7 @@ void HeapProfilerImpl::Frontend::AddHeapSnapshotChunk(char *data, int32_t size) } tooling::AddHeapSnapshotChunk addHeapSnapshotChunk; - addHeapSnapshotChunk.GetChunk().resize(size); + addHeapSnapshotChunk.GetChunk().resize(size); // 分配内存空间 for (int32_t i = 0; i < size; ++i) { addHeapSnapshotChunk.GetChunk()[i] = data[i]; } @@ -281,9 +281,9 @@ void HeapProfilerImpl::Frontend::HeapStatsUpdate(HeapStat* updateData, int32_t c } std::vector statsDiff; for (int32_t i = 0; i < count; ++i) { - statsDiff.emplace_back(updateData[i].index_); - statsDiff.emplace_back(updateData[i].count_); - statsDiff.emplace_back(updateData[i].size_); + statsDiff.emplace_back(updateData[i].index_);// 索引 + statsDiff.emplace_back(updateData[i].count_);// 计数 + statsDiff.emplace_back(updateData[i].size_);// 大小 } tooling::HeapStatsUpdate heapStatsUpdate; heapStatsUpdate.SetStatsUpdate(std::move(statsDiff)); @@ -329,8 +329,8 @@ DispatchResponse HeapProfilerImpl::AddInspectedHeapObject([[maybe_unused]] const DispatchResponse HeapProfilerImpl::CollectGarbage() { - panda::JSNApi::TriggerGC(vm_, panda::JSNApi::TRIGGER_GC_TYPE::FULL_GC); - panda::JSNApi::TriggerGC(vm_, panda::JSNApi::TRIGGER_GC_TYPE::SHARED_FULL_GC); + panda::JSNApi::TriggerGC(vm_, panda::JSNApi::TRIGGER_GC_TYPE::FULL_GC); // 触发垃圾回收 + panda::JSNApi::TriggerGC(vm_, panda::JSNApi::TRIGGER_GC_TYPE::SHARED_FULL_GC); // 触发共享垃圾回收 return DispatchResponse::Ok(); } @@ -341,7 +341,7 @@ DispatchResponse HeapProfilerImpl::Enable() DispatchResponse HeapProfilerImpl::Disable() { - panda::DFXJSNApi::DestroyHeapProfiler(vm_); + panda::DFXJSNApi::DestroyHeapProfiler(vm_); //销毁堆内存分析器 return DispatchResponse::Ok(); } @@ -361,7 +361,7 @@ DispatchResponse HeapProfilerImpl::GetObjectByHeapObjectId([[maybe_unused]] cons DispatchResponse HeapProfilerImpl::GetSamplingProfile([[maybe_unused]] std::unique_ptr *profile) { - auto samplingInfo = panda::DFXJSNApi::GetAllocationProfile(vm_); + auto samplingInfo = panda::DFXJSNApi::GetAllocationProfile(vm_); // 获取堆内存分配信息 if (samplingInfo == nullptr) { return DispatchResponse::Fail("GetSamplingProfile fail"); } @@ -393,34 +393,34 @@ DispatchResponse HeapProfilerImpl::StopSampling([[maybe_unused]] std::unique_ptr DispatchResponse HeapProfilerImpl::StartTrackingHeapObjects(const StartTrackingHeapObjectsParams ¶ms) { - panda::JSNApi::SetProfilerState(vm_, true); - if (handle_ != nullptr && uv_is_active(reinterpret_cast(handle_))) { + panda::JSNApi::SetProfilerState(vm_, true); // 开启堆内存分析器 + if (handle_ != nullptr && uv_is_active(reinterpret_cast(handle_))) { // 如果定时器已经启动,则直接返回 return DispatchResponse::Ok(); } - bool traceAllocation = params.GetTrackAllocations(); - bool result = panda::DFXJSNApi::StartHeapTracking(vm_, INTERVAL, true, &stream_, traceAllocation, false); + bool traceAllocation = params.GetTrackAllocations(); // 是否追踪分配 + bool result = panda::DFXJSNApi::StartHeapTracking(vm_, INTERVAL, true, &stream_, traceAllocation, false);// 启动堆内存追踪 - uv_loop_t *loop = reinterpret_cast(vm_->GetLoop()); + uv_loop_t *loop = reinterpret_cast(vm_->GetLoop());// 获取事件循环 if (loop == nullptr) { return DispatchResponse::Fail("Loop is nullptr"); } if (handle_ == nullptr) { - handle_ = new uv_timer_t; + handle_ = new uv_timer_t;// 创建定时器 } - uv_timer_init(loop, handle_); - handle_->data = this; - uv_timer_start(handle_, HeapTrackingCallback, 0, INTERVAL * MILLI_TO_MICRO); - if (DebuggerApi::IsMainThread()) { + uv_timer_init(loop, handle_);// 初始化定时器 + handle_->data = this;// 绑定回调函数 + uv_timer_start(handle_, HeapTrackingCallback, 0, INTERVAL * MILLI_TO_MICRO);// 启动定时器,每隔200毫秒执行一次回调函数 + if (DebuggerApi::IsMainThread()) {// 如果在主线程中,则直接发送异步事件 uv_async_send(&loop->wq_async); } else { uv_work_t *work = new uv_work_t; - uv_queue_work(loop, work, [](uv_work_t *) { }, [](uv_work_t *work, int32_t) { delete work; }); + uv_queue_work(loop, work, [](uv_work_t *) { }, [](uv_work_t *work, int32_t) { delete work; });// 异步执行回调函数 } if (result) { - return DispatchResponse::Ok(); + return DispatchResponse::Ok();// 启动成功,返回Ok } else { - return DispatchResponse::Fail("StartHeapTracking fail"); + return DispatchResponse::Fail("StartHeapTracking fail");// 启动失败,返回Fail } } @@ -436,17 +436,17 @@ void HeapProfilerImpl::HeapTrackingCallback(uv_timer_t* handle) DispatchResponse HeapProfilerImpl::StopTrackingHeapObjects(const StopTrackingHeapObjectsParams ¶ms) { if (handle_ != nullptr) { - uv_timer_stop(handle_); + uv_timer_stop(handle_); // 停止定时器 } bool result = false; - if (params.GetReportProgress()) { + if (params.GetReportProgress()) { // 如果需要报告进度,则使用HeapProfilerProgress类来记录进度 HeapProfilerProgress progress(&frontend_); result = panda::DFXJSNApi::StopHeapTracking(vm_, &stream_, &progress, false); } else { result = panda::DFXJSNApi::StopHeapTracking(vm_, &stream_, nullptr, false); } if (result) { - panda::JSNApi::SetProfilerState(vm_, false); + panda::JSNApi::SetProfilerState(vm_, false); // 关闭堆内存分析器 return DispatchResponse::Ok(); } else { return DispatchResponse::Fail("StopHeapTracking fail"); @@ -457,11 +457,11 @@ DispatchResponse HeapProfilerImpl::TakeHeapSnapshot(const StopTrackingHeapObject { bool captureNumericValue = params.GetCaptureNumericValue(); DumpSnapShotOption dumpOption; - dumpOption.dumpFormat = DumpFormat::JSON; - dumpOption.isVmMode = true; - dumpOption.isPrivate = false; - dumpOption.captureNumericValue = captureNumericValue; - if (params.GetReportProgress()) { + dumpOption.dumpFormat = DumpFormat::JSON;// 指定输出格式为JSON + dumpOption.isVmMode = true;// 是否为虚拟机模式 + dumpOption.isPrivate = false;// 是否为私有模式 + dumpOption.captureNumericValue = captureNumericValue;// 是否捕获数值 + if (params.GetReportProgress()) { // 如果需要报告进度,则使用HeapProfilerProgress类来记录进度 HeapProfilerProgress progress(&frontend_); panda::DFXJSNApi::DumpHeapSnapshot(vm_, &stream_, dumpOption, &progress); } else { diff --git a/tooling/protocol_handler.cpp b/tooling/protocol_handler.cpp index dd031867..f4ba0447 100644 --- a/tooling/protocol_handler.cpp +++ b/tooling/protocol_handler.cpp @@ -61,20 +61,20 @@ void ProtocolHandler::ProcessCommand() } requestQueueCond_.wait(queueLock); } - requestQueue_.swap(dispatchingQueue); + requestQueue_.swap(dispatchingQueue);//交换队列,防止在等待过程中有新的消息到来导致死锁 } isDispatchingMessage_ = true; { DebuggerApi::DebuggerManagedScope managedScope(vm_); while (!dispatchingQueue.empty()) { - std::string msg = std::move(dispatchingQueue.front()); + std::string msg = std::move(dispatchingQueue.front());//取出队列中的消息 dispatchingQueue.pop(); [[maybe_unused]] LocalScope scope(vm_); - auto exception = DebuggerApi::GetAndClearException(vm_); - dispatcher_.Dispatch(DispatchRequest(msg)); - DebuggerApi::SetException(vm_, exception); + auto exception = DebuggerApi::GetAndClearException(vm_);//保存之前的异常状态,防止调试器中断执行时,后续的调试命令无法正常执行 + dispatcher_.Dispatch(DispatchRequest(msg));//分发消息 + DebuggerApi::SetException(vm_, exception);//恢复之前的异常状态,防止调试器中断执行时,后续的调试命令无法正常执行 } } isDispatchingMessage_ = false; -- Gitee