From f0edd02608929ad56dcac85d1e6d062eec30f92b Mon Sep 17 00:00:00 2001 From: "lijindong (C)" <2220386943@qq.com> Date: Thu, 20 Nov 2025 16:21:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9numGroup->numEvtAttr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/C_C++_API.md | 4 +- docs/Details_Usage.md | 6 +-- go/src/libkperf/kperf/kperf.go | 2 +- include/pmu.h | 10 ++--- pmu/evt_list_default.cpp | 12 ++++-- pmu/pmu.cpp | 20 +++++----- pmu/pmu_metric.cpp | 2 +- python/modules/_libkperf/Pmu.py | 26 ++++++------- python/modules/kperf/pmu.py | 10 ++--- test/test_perf/test_api.cpp | 6 +-- test/test_perf/test_group.cpp | 66 ++++++++++++++++----------------- tools/cache_collect/collect.cpp | 4 +- 12 files changed, 85 insertions(+), 83 deletions(-) diff --git a/docs/C_C++_API.md b/docs/C_C++_API.md index 12b57ec..892ae8d 100644 --- a/docs/C_C++_API.md +++ b/docs/C_C++_API.md @@ -25,8 +25,8 @@ * period 如果PmuAttr中useFreq=True则为采样频率,否则为采样间隔 * excludeUser 排除对用户态数据的采集 * excludeKernel 排除对内核态数据的采集 - * unsigned numGroup - 参与分组的事件个数 + * unsigned numEvtAttr + 单独定义的事件数 * union unsigned period 采样周期,仅支持SAMPLING和SPE_SAMPLING模式 unsigned freq 采样频率,仅支持SAMPLING模式 diff --git a/docs/Details_Usage.md b/docs/Details_Usage.md index 61b0f0d..0baafac 100644 --- a/docs/Details_Usage.md +++ b/docs/Details_Usage.md @@ -977,14 +977,14 @@ perf stat -e "{inst_retired,inst_spec,cycles}","{inst_retired,cycles}" // 指定5个事件,因为inst_retired和cycles会重复出现在多个指标中,所以需要重复指定事件。 char *evtList[5] = {"inst_retired", "inst_spec", "cycles", "inst_retired", "cycles"}; -// 指定事件分组编号,前三个事件为一组,后两个事件为一组。设置groupId=-1表示对应事件不参与分组。 -// 当事件数量numEvt超过指定事件分组数量numGroup时,超过分组数量的事件的groupId默认为-1,即不参与分组。 +// 指定事件分组编号,前三个事件为一组,后两个事件为一组。设置EvtAttr属性groupId=-1表示对应事件不参与分组。 +// 当事件数量numEvt超过事件单独属性数量numEvtAttr时,超过数量的事件的groupId默认为-1,即不参与分组。 EvtAttr attrList[5] = {{1},{1},{1},{2},{2}}; PmuAttr attr = {0}; attr.evtList = evtList; attr.numEvt = 5; attr.evtAttr = attrList; -attr.numGroup = 5; +attr.numEvtAttr = 5; int pd = PmuOpen(COUNTING, &attr); PmuEnable(pd); sleep(1); diff --git a/go/src/libkperf/kperf/kperf.go b/go/src/libkperf/kperf/kperf.go index e9484c9..e1d29ab 100644 --- a/go/src/libkperf/kperf/kperf.go +++ b/go/src/libkperf/kperf/kperf.go @@ -621,7 +621,7 @@ func ToCPmuAttr(attr PmuAttr) (*C.struct_PmuAttr, int) { } } cAttr.evtAttr = &evtAttrList[0] - cAttr.numGroup = C.uint32_t(evtAttrLen) + cAttr.numEvtAttr = C.uint32_t(evtAttrLen) } if attr.UseFreq { diff --git a/include/pmu.h b/include/pmu.h index d6662d8..2856a3f 100644 --- a/include/pmu.h +++ b/include/pmu.h @@ -126,15 +126,11 @@ struct PmuAttr { // Length of core id list unsigned numCpu; - // event group id - // if not use event group function, this field will be nullptr. - // if use event group function. please confirm the event group id with eveList is one by one. - // the same group id is the a event group. - // Note: if the group id value is -1, it indicates that the event is not grouped. + // List of individual attributes of events. struct EvtAttr *evtAttr; // Length of evtAttr list. - // when numEvt > numGroup, the other events will not set to any group. - unsigned numGroup; + // when numEvt > numEvtAttr, the other events will not set individual attribute. + unsigned numEvtAttr; union { // Sample period, only available for SAMPLING and SPE_SAMPLING. diff --git a/pmu/evt_list_default.cpp b/pmu/evt_list_default.cpp index 5b050a4..f19f234 100644 --- a/pmu/evt_list_default.cpp +++ b/pmu/evt_list_default.cpp @@ -55,9 +55,15 @@ void KUNPENG_PMU::EvtListDefault::AdaptErrInfo(int err, PerfEvtPtr perfEvt) } break; case LIBPERF_ERR_NO_PERMISSION: - pcerr::SetCustomErr(LIBPERF_ERR_NO_PERMISSION, - "Current user does not have the permission to collect the event." - "Switch to the root user and run the 'echo -1 > /proc/sys/kernel/perf_event_paranoid'"); + if (GetParanoidVal() == -1) { + pcerr::SetCustomErr(LIBPERF_ERR_NO_PERMISSION, + "Current user does not have the permission to collect the event." + "Please Switch to the root user and try again"); + } else { + pcerr::SetCustomErr(LIBPERF_ERR_NO_PERMISSION, + "Current user does not have the permission to collect the event." + "Switch to the root user and run the 'echo -1 > /proc/sys/kernel/perf_event_paranoid'"); + } break; case LIBPERF_ERR_FAIL_MMAP: if (errno == ENOMEM) { diff --git a/pmu/pmu.cpp b/pmu/pmu.cpp index 259a3b9..34d97bf 100644 --- a/pmu/pmu.cpp +++ b/pmu/pmu.cpp @@ -117,10 +117,10 @@ static int CheckEvtList(unsigned numEvt, char** evtList) return SUCCESS; } -static int CheckGroupList(unsigned numGroup, struct EvtAttr *evtAttr) +static int CheckGroupList(unsigned numEvtAttr, struct EvtAttr *evtAttr) { - if (numGroup > 0 && evtAttr == nullptr) { - New(LIBPERF_ERR_INVALID_EVTATTR, "Invalid evtAttr list: numGroup is greater than 0, but evtAttr is null."); + if (numEvtAttr > 0 && evtAttr == nullptr) { + New(LIBPERF_ERR_INVALID_EVTATTR, "Invalid evtAttr list: numEvtAttr is greater than 0, but evtAttr is null."); return LIBPERF_ERR_INVALID_EVTATTR; } return SUCCESS; @@ -346,7 +346,7 @@ static int CheckAttr(enum PmuTaskType collectType, struct PmuAttr *attr) if (err != SUCCESS) { return err; } - err = CheckGroupList(attr->numGroup, attr->evtAttr); + err = CheckGroupList(attr->numEvtAttr, attr->evtAttr); if (err != SUCCESS) { return err; } @@ -493,7 +493,7 @@ static unsigned GenerateSplitList(unordered_map& eventSplitMap, v for (int i = 0; i < attr->numEvt; ++i) { auto evt = attr->evtList[i]; EvtAttr evtAttr = {-1, 0, false, false}; - if (attr->evtAttr != nullptr && i < attr->numGroup) { + if (attr->evtAttr != nullptr && i < attr->numEvtAttr) { auto inputAttr = attr->evtAttr[i]; evtAttr = {inputAttr.groupId, inputAttr.period, inputAttr.excludeUser, inputAttr.excludeKernel}; } @@ -959,16 +959,16 @@ int GetCgroupFd(std::string& cgroupName) { } static EvtAttr ResolveEvtAttrParams(struct PmuAttr *attr, int index) { - // numEvt must be greater than numGroup.The excludeUser, excluderKernel,and period attributes in PmuAttr have higher priority than those in EvtAttr. - int groupId = index >= attr->numGroup? -1 : attr->evtAttr[index].groupId; + // numEvt must be greater than numEvtAttr.The excludeUser, excluderKernel,and period attributes in PmuAttr have higher priority than those in EvtAttr. + int groupId = index >= attr->numEvtAttr? -1 : attr->evtAttr[index].groupId; - bool excludeKernel = index >= attr->numGroup ? false : attr->evtAttr[index].excludeKernel; + bool excludeKernel = index >= attr->numEvtAttr ? false : attr->evtAttr[index].excludeKernel; excludeKernel = attr->excludeKernel ? true : excludeKernel; - bool excludeUser = index >= attr->numGroup ? false : attr->evtAttr[index].excludeUser; + bool excludeUser = index >= attr->numEvtAttr ? false : attr->evtAttr[index].excludeUser; excludeUser = attr->excludeUser ? true : excludeUser; - unsigned period = index >= attr->numGroup ? SAMPLING_RECORD_PERIOD : attr->evtAttr[index].period; + unsigned period = index >= attr->numEvtAttr ? SAMPLING_RECORD_PERIOD : attr->evtAttr[index].period; period = attr->period ? attr->period : period; if (!period) { diff --git a/pmu/pmu_metric.cpp b/pmu/pmu_metric.cpp index ab31970..d9c9702 100644 --- a/pmu/pmu_metric.cpp +++ b/pmu/pmu_metric.cpp @@ -1929,7 +1929,7 @@ int PmuOpenWithHWMetric(struct PmuHwMetricAttr *hwMetricAttr) { evtAttr[2 * i + 1] = {groupId, dstPeriod, false, false}; } attr.numEvt = metricList.size() * 2; - attr.numGroup = metricList.size() * 2; + attr.numEvtAttr = metricList.size() * 2; int pidList[1]; if (hwMetricAttr->pid > 0) { diff --git a/python/modules/_libkperf/Pmu.py b/python/modules/_libkperf/Pmu.py index 6864c30..4b0d04d 100644 --- a/python/modules/_libkperf/Pmu.py +++ b/python/modules/_libkperf/Pmu.py @@ -104,7 +104,7 @@ class CtypesPmuAttr(ctypes.Structure): unsigned numCpu; // length of cpu id list struct EvtAttr *evtAttr; // events group id info - unsigned numGroup // length of evtAttr + unsigned numEvtAttr // length of evtAttr union { unsigned period; // sample period @@ -138,7 +138,7 @@ class CtypesPmuAttr(ctypes.Structure): ('cpuList', ctypes.POINTER(ctypes.c_int)), ('numCpu', ctypes.c_uint), ('evtAttr', ctypes.POINTER(CtypesEvtAttr)), - ('numGroup', ctypes.c_uint), + ('numEvtAttr', ctypes.c_uint), ('sampleRate', SampleRateUnion), ('useFreq', ctypes.c_uint, 1), ('excludeUser', ctypes.c_uint, 1), @@ -213,12 +213,12 @@ class CtypesPmuAttr(ctypes.Structure): self.sampleRate.freq = ctypes.c_uint(sampleRate) if evtAttr: - numGroup = len(evtAttr) - self.evtAttr = (CtypesEvtAttr * numGroup)(*[evt.c_evt_attr for evt in evtAttr]) - self.numGroup = ctypes.c_uint(numGroup) + numEvtAttr = len(evtAttr) + self.evtAttr = (CtypesEvtAttr * numEvtAttr)(*[evt.c_evt_attr for evt in evtAttr]) + self.numEvtAttr = ctypes.c_uint(numEvtAttr) else: self.evtAttr = None - self.numGroup = ctypes.c_uint(0) + self.numEvtAttr = ctypes.c_uint(0) if cgroupNameList: numCgroup = len(cgroupNameList) @@ -376,22 +376,22 @@ class PmuAttr(object): self.c_pmu_attr.numPid = ctypes.c_uint(0) @property - def numGroup(self): - return self.c_pmu_attr.numGroup + def numEvtAttr(self): + return self.c_pmu_attr.numEvtAttr @property def evtAttr(self): - return [self.c_pmu_attr.evtAttr[i] for i in range(self.numGroup)] + return [self.c_pmu_attr.evtAttr[i] for i in range(self.numEvtAttr)] @evtAttr.setter def evtAttr(self, evtAttr): if evtAttr: - numGroup = len(evtAttr) - self.c_pmu_attr.evtAttr = (CtypesEvtAttr * numGroup)(*[CtypesEvtAttr(evt) for evt in evtAttr]) - self.c_pmu_attr.numGroup = ctypes.c_uint(numGroup) + numEvtAttr = len(evtAttr) + self.c_pmu_attr.evtAttr = (CtypesEvtAttr * numEvtAttr)(*[CtypesEvtAttr(evt) for evt in evtAttr]) + self.c_pmu_attr.numEvtAttr = ctypes.c_uint(numEvtAttr) else: self.c_pmu_attr.evtAttr = None - self.c_pmu_attr.numGroup = ctypes.c_uint(0) + self.c_pmu_attr.numEvtAttr = ctypes.c_uint(0) @property def numCpu(self): diff --git a/python/modules/kperf/pmu.py b/python/modules/kperf/pmu.py index 82cbe8e..bfdcc3f 100644 --- a/python/modules/kperf/pmu.py +++ b/python/modules/kperf/pmu.py @@ -298,11 +298,11 @@ class PmuAttr(_libkperf.PmuAttr): If both and are NULL, all processes on all cores will be monitored. If is NULL and is not NULL, specified processes on all cores will be monitored. if both and are not NULL, specified processes on specified cores will be monitored. - evtAttr: event group id attributes. - if not use event group function, this field will be NULL. - if use event group function. please confirm the event group id with eveList is one by one. - the same group id is the a event group. - Note: if the group id value is -1, it indicates that the event is not grouped. + evtAttr: list of individual attributes of the event + groupId: the same group id is the a event group. + excludeUser: this event doesn't count user. + excludeKernel: this event doesn't count kernel. + period: sample period of the event sampleRate: sample time enum. period enum: Sample period, only available for SAMPLING and SPE_SAMPLING. freq enum: Sample frequency, only available for SAMPLING. diff --git a/test/test_perf/test_api.cpp b/test/test_perf/test_api.cpp index aec0db5..8d5215b 100644 --- a/test/test_perf/test_api.cpp +++ b/test/test_perf/test_api.cpp @@ -784,9 +784,9 @@ TEST_F(TestAPI, InvalidBpfAttr) ASSERT_NE(pd, -1); pd = PmuOpen(SAMPLING, &attr); ASSERT_EQ(pd, -1); - EvtAttr groupId[1] = {1}; - attr.evtAttr = groupId; - attr.numGroup = 1; + EvtAttr evtAttr[1] = {1}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = 1; pd = PmuOpen(COUNTING, &attr); ASSERT_EQ(pd, -1); #else diff --git a/test/test_perf/test_group.cpp b/test/test_perf/test_group.cpp index c93a79b..dd071b2 100644 --- a/test/test_perf/test_group.cpp +++ b/test/test_perf/test_group.cpp @@ -129,9 +129,9 @@ TEST_F(TestGroup, TestCountingEventGroup) "r26", "r2d", "r17", "r11", "r8", "r22", "r24", "r10"}; attr.evtList = evtList; - struct EvtAttr groupId[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}, {13}, {13}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}, {13}, {13}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(COUNTING, &attr); ASSERT_TRUE(pd != -1); @@ -157,9 +157,9 @@ TEST_F(TestGroup, TestEventGroupLessGroupId) "r26", "r2d", "r17", "r11", "r8", "r22", "r24", "r10"}; attr.evtList = evtList; - struct EvtAttr groupId[13] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {11}, {11}}; - attr.evtAttr = groupId; - attr.numGroup = 13; + struct EvtAttr evtAttr[13] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {11}, {11}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = 13; int pd = PmuOpen(COUNTING, &attr); ASSERT_TRUE(pd != -1); @@ -189,9 +189,9 @@ TEST_F(TestGroup, TestCountingEventGroupAllUncore) "r26", "r2d", "r17", "r11", "hisi_sccl1_ddrc2/flux_rd/", "hisi_sccl1_ddrc0/flux_wr/", "hisi_sccl1_hha2/rx_wbi/", "hisi_sccl1_hha3/bi_num/"}; attr.evtList = evtList; - struct EvtAttr groupId[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}, {13}, {13}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}, {13}, {13}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(COUNTING, &attr); ASSERT_TRUE(pd == -1); } @@ -206,9 +206,9 @@ TEST_F(TestGroup, TestCountingEventGroupHasAggregateUncore) "r22", "hisi_sccl1_ddrc/flux_rd/"}; attr.evtList = evtList; - struct EvtAttr groupId[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(COUNTING, &attr); ASSERT_TRUE(pd != -1); int ret = PmuCollect(pd, 100, collectInterval); @@ -230,9 +230,9 @@ TEST_F(TestGroup, TestCountingEventGroupHasAggregateUncoreEnd) "hisi_sccl1_ddrc/flux_rd/"}; attr.evtList = evtList; - struct EvtAttr groupId[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(COUNTING, &attr); ASSERT_TRUE(pd != -1); @@ -253,9 +253,9 @@ TEST_F(TestGroup, TestCountingEventGroupAllAggregateUncore) char *evtList[numEvt] = {"hisi_sccl1_ddrc/flux_wr/", "hisi_sccl1_ddrc/flux_rd/"}; attr.evtList = evtList; - struct EvtAttr groupId[numEvt] = {{1}, {1}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{1}, {1}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(COUNTING, &attr); ASSERT_TRUE(pd == -1); } @@ -270,9 +270,9 @@ TEST_F(TestGroup, TestCountingEventGroupHasUncore) "r22", "r24", "hisi_sccl1_ddrc/flux_rd/", "hisi_sccl1_ddrc/flux_wr/"}; attr.evtList = evtList; - struct EvtAttr groupId[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}, {13}, {13}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {13}, {13}, {13}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(COUNTING, &attr); ASSERT_TRUE(pd != -1); @@ -293,9 +293,9 @@ TEST_F(TestGroup, TestSamplingNoEventGroup) char *evtList[numEvt] = {"r11", "r3"}; attr.evtList = evtList; - struct EvtAttr groupId[numEvt] = {{1}, {2}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{1}, {2}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(SAMPLING, &attr); ASSERT_TRUE(pd!= -1); @@ -314,9 +314,9 @@ TEST_F(TestGroup, TestSamplingEventGroup) char *evtList[numEvt] = {"r11", "r3"}; attr.evtList = evtList; - struct EvtAttr groupId[numEvt] = {{2}, {2}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{2}, {2}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(SAMPLING, &attr); ASSERT_TRUE(pd != -1); @@ -335,9 +335,9 @@ TEST_F(TestGroup, TestSamplingEventGroupHasUncore) char *evtList[numEvt] = {"hisi_sccl1_ddrc/flux_rd/", "r3"}; attr.evtList = evtList; - struct EvtAttr groupId[numEvt] = {{2}, {2}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{2}, {2}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(SAMPLING, &attr); ASSERT_TRUE(pd == -1); @@ -355,9 +355,9 @@ TEST_F(TestGroup, TestEvtGroupForkNewThread) attr.pidList[0] = pid; attr.numPid = 1; attr.includeNewFork = 1; - struct EvtAttr groupId[numEvt] = {{2}, {2}}; - attr.evtAttr = groupId; - attr.numGroup = numEvt; + struct EvtAttr evtAttr[numEvt] = {{2}, {2}}; + attr.evtAttr = evtAttr; + attr.numEvtAttr = numEvt; int pd = PmuOpen(COUNTING, &attr); ASSERT_TRUE(pd != -1); diff --git a/tools/cache_collect/collect.cpp b/tools/cache_collect/collect.cpp index b7ffc89..0fdd330 100644 --- a/tools/cache_collect/collect.cpp +++ b/tools/cache_collect/collect.cpp @@ -356,7 +356,7 @@ void collectMiss(CollectArgs& args) attr.evtAttr = cfg.groupId.data(); attr.evtList = cfg.evtList.data(); attr.numEvt = cfg.baseEvents.size(); - attr.numGroup = cfg.baseEvents.size(); + attr.numEvtAttr = cfg.baseEvents.size(); attr.callStack = 1; attr.excludeKernel = true; attr.symbolMode = NO_SYMBOL_RESOLVE; @@ -471,7 +471,7 @@ void collectSummaryData(CollectArgs& args) attr.evtAttr = cfg.groupId.data(); attr.evtList = cfg.evtList.data(); attr.numEvt = cfg.baseEvents.size(); - attr.numGroup = cfg.baseEvents.size(); + attr.numEvtAttr = cfg.baseEvents.size(); attr.pidList = args.pids.data(); attr.numPid = args.pids.size(); -- Gitee