From 8beda1a21c25bffe423adfaaba6a9f11043d8974 Mon Sep 17 00:00:00 2001 From: jiangwei Date: Wed, 30 Jul 2025 15:52:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ai=E6=A3=80=E8=A7=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jiangwei --- include/callstack.h | 1 + include/subcommand_record.h | 2 +- include/subcommand_stat.h | 2 +- src/callstack.cpp | 12 +++++++++--- src/perf_events.cpp | 7 ++++++- src/spe_decoder.cpp | 6 +++--- src/subcommand_record.cpp | 6 +++--- src/subcommand_stat.cpp | 4 ++-- 8 files changed, 26 insertions(+), 14 deletions(-) diff --git a/include/callstack.h b/include/callstack.h index c401fd4..0bcb6ba 100644 --- a/include/callstack.h +++ b/include/callstack.h @@ -53,6 +53,7 @@ public: size_t ExpandCallStack(pid_t tid, std::vector &callFrames, size_t expandLimit = 1u); private: + mutable std::mutex cacheMutex_; pid_t lastPid_ = -1; ADDR_TYPE lastAddr_ = 0; ADDR_TYPE lastData_ = 0; diff --git a/include/subcommand_record.h b/include/subcommand_record.h index 5f265f9..41a148b 100644 --- a/include/subcommand_record.h +++ b/include/subcommand_record.h @@ -316,7 +316,7 @@ private: int nullFd_ = -1; std::thread clientCommandHandle_; std::thread replyCommandHandle_; - bool clientRunning_ = true; + std::atomic_bool clientRunning_ = true; bool isHiperfClient_ = false; struct ControlCommandHandler { std::function preProcess = []() -> bool { diff --git a/include/subcommand_stat.h b/include/subcommand_stat.h index 466479a..6adfc88 100644 --- a/include/subcommand_stat.h +++ b/include/subcommand_stat.h @@ -185,7 +185,7 @@ private: int nullFd_ = -1; FILE* filePtr_ = nullptr; std::thread clientCommandHandle_; - bool clientRunning_ = true; + std::atomic_bool clientRunning_ = true; struct ControlCommandHandler { std::function preProcess = []() -> bool { return false; diff --git a/src/callstack.cpp b/src/callstack.cpp index 41dfa66..8f7653f 100644 --- a/src/callstack.cpp +++ b/src/callstack.cpp @@ -215,6 +215,8 @@ size_t CallStack::ExpandCallStack(pid_t tid, std::vector &callFrames, HLOGM("new callstack is too small, skip it"); return expand; } + + std::lock_guard lock(cacheMutex_); if (!cachedCallFramesMap_.count(tid)) { cachedCallFramesMap_[tid].reserve(MAX_CALL_FRAME_EXPAND_CACHE_SIZE); } @@ -273,14 +275,14 @@ bool CallStack::DoUnwind2(const VirtualThread &thread, std::vector &ca auto unwinder = pidUnwinder_[thread.pid_]; #ifdef target_cpu_arm - static std::shared_ptr regs = std::make_shared(); + thread_local std::shared_ptr regs = std::make_shared(); std::vector tempRegs; for (u64 i = 0; i < regsNum_; ++i) { tempRegs.push_back(static_cast(regs_[i])); } regs->SetRegsData(tempRegs); #else - static std::shared_ptr regs = std::make_shared(); + thread_local std::shared_ptr regs = std::make_shared(); regs->SetRegsData(reinterpret_cast(regs_), regsNum_); #endif CHECK_TRUE(unwinder != nullptr, false, 0, ""); @@ -396,7 +398,10 @@ int CallStack::AccessMem2(uintptr_t addr, uintptr_t *val, void *arg) } } else { size_t stackOffset = addr - unwindInfoPtr->callStack.stackPoint_; - *val = *(uintptr_t *)&unwindInfoPtr->callStack.stack_[stackOffset]; + if (memcpy_s(val, sizeof(uintptr_t), &unwindInfoPtr->callStack.stack_[stackOffset], sizeof(uintptr_t)) != 0) { + HLOGE("memcpy_s failed for stack offset %zu", stackOffset); + return -1; + } HLOGM("access_mem addr %p val %" UNW_WORD_PFLAG ", from stack offset %zu", reinterpret_cast(addr), *val, stackOffset); } @@ -432,6 +437,7 @@ CallStack::CallStack() void CallStack::ClearCache() { + std::lock_guard lock(cacheMutex_); cachedCallFramesMap_.clear(); lastPid_ = -1; lastAddr_ = 0; diff --git a/src/perf_events.cpp b/src/perf_events.cpp index 41a5f06..90fdb30 100644 --- a/src/perf_events.cpp +++ b/src/perf_events.cpp @@ -367,6 +367,10 @@ bool PerfEvents::AddEvent(const std::string &eventString, bool followGroup) } else { if (eventName == "arm_spe_0") { u32 speType = GetSpeType(); + if (speType == UINT_MAX) { + HLOGE("Failed to get ARM SPE type. Check if the device supports ARM SPE."); + return false; + } return AddSpeEvent(speType); } if (StringStartsWith(eventName, "0x") @@ -569,7 +573,8 @@ static bool CaptureSig() }; sig.sa_handler = [](int sig) { - printf("\n Ctrl + C detected.\n"); + const char* msg = "\n Ctrl + C detected.\n"; + write(STDOUT_FILENO, msg, sizeof(msg) - 1); g_trackRunning = false; }; diff --git a/src/spe_decoder.cpp b/src/spe_decoder.cpp index 992709a..a271d2e 100644 --- a/src/spe_decoder.cpp +++ b/src/spe_decoder.cpp @@ -546,14 +546,14 @@ int SpePktDesc(const struct SpePkt *packet, char *buf, break; case PERF_SPE_DATA_SOURCE: case PERF_SPE_TIMESTAMP: - SpePktOutString(&err, &buf, &blen, "%s %lld", name.c_str(), payload); + SpePktOutString(&err, &buf, &blen, "%s %llu", name.c_str(), payload); break; case PERF_SPE_ADDRESS: err = SpePktDescAddr(packet, buf, bufLen); break; case PERF_SPE_CONTEXT: - SpePktOutString(&err, &buf, &blen, "%s 0x%lx el%d", - name.c_str(), (unsigned long)payload, idx + 1); + SpePktOutString(&err, &buf, &blen, "%s 0x%llx el%d", + name.c_str(), payload, idx + 1); break; case PERF_SPE_COUNTER: err = SpePktDesCont(packet, buf, bufLen); diff --git a/src/subcommand_record.cpp b/src/subcommand_record.cpp index aa24d52..dcef269 100644 --- a/src/subcommand_record.cpp +++ b/src/subcommand_record.cpp @@ -1418,7 +1418,7 @@ void SubCommandRecord::ClientCommandHandle() InitControlCommandHandlerMap(); bool hasRead = true; - while (clientRunning_) { + while (clientRunning_.load()) { if (isFifoServer_ && hasRead) { if (clientPipeInput_ != -1) { // after read(), block is disabled, the poll will be waked neven if no data @@ -1738,7 +1738,7 @@ HiperfError SubCommandRecord::OnSubCommand(std::vector& args) void SubCommandRecord::CloseClientThread() { if (clientCommandHandle_.joinable()) { - clientRunning_ = false; + clientRunning_.store(false); HLOGI("CloseClientThread"); if (nullFd_ != -1) { close(nullFd_); @@ -1755,7 +1755,7 @@ void SubCommandRecord::CloseClientThread() void SubCommandRecord::CloseReplyThread() { if (replyCommandHandle_.joinable()) { - clientRunning_ = false; + clientRunning_.store(false); HLOGI("CloseReplyThread"); replyCommandHandle_.join(); } diff --git a/src/subcommand_stat.cpp b/src/subcommand_stat.cpp index ce00f4a..decdae8 100644 --- a/src/subcommand_stat.cpp +++ b/src/subcommand_stat.cpp @@ -839,7 +839,7 @@ void SubCommandStat::ClientCommandHandle() InitControlCommandHandlerMap(); bool hasRead = true; - while (clientRunning_) { + while (clientRunning_.load()) { if (isFifoServer_ && hasRead) { if (clientPipeInput_ != -1) { // after read(), block is disabled, the poll will be waked neven if no data @@ -996,7 +996,7 @@ HiperfError SubCommandStat::OnSubCommand(std::vector& args) void SubCommandStat::CloseClientThread() { if (clientCommandHandle_.joinable()) { - clientRunning_ = false; + clientRunning_.store(false); HLOGI("CloseClientThread"); if (nullFd_ != -1) { close(nullFd_); -- Gitee