From 6c16afe71bd469bdeca0ca43795514da9ce0b2f7 Mon Sep 17 00:00:00 2001 From: glx Date: Mon, 21 Jul 2025 14:33:21 +0800 Subject: [PATCH] Fix bugs in PmuWriteData MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复生成perf.data的bug: - 修复文档错误。 - 修复同时指定-p和-- 的问题。 - 修复freq指定为0时采集错误的问题。 - 补充错误码描述。 --- docs/Details_Usage.md | 7 ++++--- example/pmu_perfdata.cpp | 5 +++++ pmu/dump_perf_data.cpp | 2 +- pmu/pmu.cpp | 4 ++++ pmu/pmu_event.cpp | 2 +- util/pcerr.cpp | 4 ++++ 6 files changed, 19 insertions(+), 5 deletions(-) diff --git a/docs/Details_Usage.md b/docs/Details_Usage.md index 5eba4b3..a4d3c07 100644 --- a/docs/Details_Usage.md +++ b/docs/Details_Usage.md @@ -1546,7 +1546,7 @@ LD_LIBRARY_PATH=/path/to/install/lib/ ./pmu_perfdata -d 3 -- /tmp/test ``` ```python -// python代码示例 +# python代码示例 evtList = ["cycles"] pidlist = [273282] pmu_attr = kperf.PmuAttr( @@ -1557,7 +1557,8 @@ pmu_attr = kperf.PmuAttr( ) pd = kperf.open(kperf.PmuTaskType.SAMPLING, pmu_attr) if pd == -1: - print(f"kperf pmuopen sample failed, expect err is nil, but is {kperf.errorno()}\n") + print(f"kperf pmuopen sample failed, expect err is nil, but is {kperf.error()}") + exit(0) kperf.enable(pd) time.sleep(1) kperf.disable(pd) @@ -1579,7 +1580,7 @@ func main() { attr := kperf.PmuAttr{EvtList:[]string{"task-clock"}, SymbolMode:kperf.ELF, SampleRate: 1000, PidList: pidlist} pd, err := kperf.PmuOpen(kperf.SAMPLE, attr) if err != nil { - fmt.Printf("kperf pmuopen counting failed, expect err is nil, but is %v", err) + fmt.Printf("kperf pmuopen sampling failed, expect err is nil, but is %v", err) return } diff --git a/example/pmu_perfdata.cpp b/example/pmu_perfdata.cpp index 052bb2e..5ecd766 100644 --- a/example/pmu_perfdata.cpp +++ b/example/pmu_perfdata.cpp @@ -148,6 +148,11 @@ static Param ParseArgs(int argc, char** argv) if (param.events.empty()) { param.events.push_back("cycles"); } + + if (!param.pidList.empty() && !param.command.empty()) { + cerr << "cannot use -p and command line in the same time\n"; + exit(0); + } return param; } diff --git a/pmu/dump_perf_data.cpp b/pmu/dump_perf_data.cpp index 0b8be0e..5fdb04c 100644 --- a/pmu/dump_perf_data.cpp +++ b/pmu/dump_perf_data.cpp @@ -245,7 +245,7 @@ private: return LIBPERF_ERR_NOT_SUPPORT_PMU_FILE; } if (pattr->period == 0) { - New(LIBPERF_ERR_NOT_SUPPORT_PMU_FILE, "Period ofr freq is zero, maybe it is not sampling PmuAttr"); + New(LIBPERF_ERR_NOT_SUPPORT_PMU_FILE, "Period or freq is zero, maybe it is not sampling PmuAttr"); return LIBPERF_ERR_NOT_SUPPORT_PMU_FILE; } return SUCCESS; diff --git a/pmu/pmu.cpp b/pmu/pmu.cpp index 6a65845..b230e22 100644 --- a/pmu/pmu.cpp +++ b/pmu/pmu.cpp @@ -216,6 +216,10 @@ static int CheckCollectTypeConfig(enum PmuTaskType collectType, struct PmuAttr * return LIBPERF_ERR_INVALID_BLOCKED_SAMPLE; } if (collectType == SAMPLING) { + if (attr->freq == 0) { + New(LIBPERF_ERR_INVALID_EVTLIST, "Invalid frequency or period in PmuAttr."); + return LIBPERF_ERR_INVALID_EVTLIST; + } if (attr->blockedSample == 0 && attr->evtList == nullptr) { New(LIBPERF_ERR_INVALID_EVTLIST, "In sampling mode without blocked sample, the event list cannot be null."); return LIBPERF_ERR_INVALID_EVTLIST; diff --git a/pmu/pmu_event.cpp b/pmu/pmu_event.cpp index 71a3616..21324a6 100644 --- a/pmu/pmu_event.cpp +++ b/pmu/pmu_event.cpp @@ -36,4 +36,4 @@ namespace KUNPENG_PMU { return UNKNOWN_ERROR; } } -} // namespace KUNPENG_PMU \ No newline at end of file +} // namespace KUNPENG_PMU diff --git a/util/pcerr.cpp b/util/pcerr.cpp index 22b4470..2e3ad91 100644 --- a/util/pcerr.cpp +++ b/util/pcerr.cpp @@ -22,6 +22,7 @@ namespace pcerr { static std::unordered_map defaultMsg = { {SUCCESS, "success"}, {COMMON_ERR_NOMEM, "not enough memory"}, + {COMMON_ERR_WRITE, "failed to write file"}, {LIBPERF_ERR_NO_AVAIL_PD, "no available pd for libperf"}, {LIBPERF_ERR_CHIP_TYPE_INVALID, "invalid cpu arch"}, {LIBPERF_ERR_FAIL_LISTEN_PROC, "failed to listen process"}, @@ -56,6 +57,9 @@ namespace pcerr { {LIBPERF_ERR_SET_FD_RDONLY_NONBLOCK, "failed to set fd readonly and nonbolock"}, {LIBPERF_ERR_INTERFACE_NOT_SUPPORT_X86, "the current interface does not support x86"}, {LIBPERF_ERR_INVALID_CGROUP_LIST, "invalid cgroup name list"}, + {LIBPERF_ERR_NOT_SUPPORT_PMU_FILE, "writing perf.data is not supported"}, + {LIBPERF_ERR_INVALID_PMU_FILE, "invalid pmu file handler"}, + {LIBPERF_ERR_OPEN_INVALID_FILE, "failed to open file"}, }; static std::unordered_map warnMsgs = { {LIBPERF_WARN_CTXID_LOST, "Some SPE context packets are not found in the traces."}, -- Gitee