From 317aa5758770cce4c504f0708c22b02415436a31 Mon Sep 17 00:00:00 2001 From: Junyi Ye <294572668@qq.com> Date: Mon, 29 Apr 2024 11:38:32 +0800 Subject: [PATCH 1/3] Optimize the query method for uncore sub events. --- pmu/pmu.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pmu/pmu.cpp b/pmu/pmu.cpp index f89b03c..8ad2469 100644 --- a/pmu/pmu.cpp +++ b/pmu/pmu.cpp @@ -144,10 +144,16 @@ void AppendChildEvents(char* evt, unordered_map& eventSplitMap) return; } for (int i = 0; i < numEvt; ++i) { - auto uncoreEvent = uncoreEventList[i]; - if (strncmp(uncoreEvent, devName.c_str(), devName.length()) == 0 && - strstr(uncoreEvent, evtName.c_str()) != nullptr) { - eventSplitMap.emplace(uncoreEvent, evt); + auto uncoreEventChar = uncoreEventList[i]; + string uncoreEventString = uncoreEventChar; + auto findUncoreSlash = uncoreEventString.find('/'); + string uncoreDevName = uncoreEventString.substr(0, findUncoreSlash); + string uncoreEvtName = uncoreEventString.substr( + uncoreDevName.size() + 1, uncoreEventString.size() - 1 - (uncoreDevName.size() + 1)); + // Determine whether "hisi_sccl1_ddrc" is front part and "act_cmd" is the back part of + // "hisi_sccl1_ddrc0/act_cmd/" + if (strncmp(uncoreEventChar, devName.c_str(), devName.length()) == 0 && evtName == uncoreEvtName) { + eventSplitMap.emplace(uncoreEventString, evt); } } } -- Gitee From d0d7d9bfe8b2ca9936c37abd0fcee63b2e526293 Mon Sep 17 00:00:00 2001 From: Junyi Ye <294572668@qq.com> Date: Mon, 29 Apr 2024 11:55:20 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=AA=A2=E8=A6=96=E6=84=8F=E8=A6=8B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pmu/pmu.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pmu/pmu.cpp b/pmu/pmu.cpp index 8ad2469..81df7e4 100644 --- a/pmu/pmu.cpp +++ b/pmu/pmu.cpp @@ -143,19 +143,24 @@ void AppendChildEvents(char* evt, unordered_map& eventSplitMap) New(LIBPERF_ERR_INVALID_EVENT, "Invalid uncore event list"); return; } + bool invalidFlag = true; for (int i = 0; i < numEvt; ++i) { - auto uncoreEventChar = uncoreEventList[i]; - string uncoreEventString = uncoreEventChar; - auto findUncoreSlash = uncoreEventString.find('/'); - string uncoreDevName = uncoreEventString.substr(0, findUncoreSlash); - string uncoreEvtName = uncoreEventString.substr( - uncoreDevName.size() + 1, uncoreEventString.size() - 1 - (uncoreDevName.size() + 1)); + string uncoreEvent = uncoreEventList[i]; + auto findUncoreSlash = uncoreEvent.find('/'); + string uncoreDevName = uncoreEvent.substr(0, findUncoreSlash); + string uncoreEvtName = uncoreEvent.substr( + uncoreDevName.size() + 1, uncoreEvent.size() - 1 - (uncoreDevName.size() + 1)); // Determine whether "hisi_sccl1_ddrc" is front part and "act_cmd" is the back part of // "hisi_sccl1_ddrc0/act_cmd/" - if (strncmp(uncoreEventChar, devName.c_str(), devName.length()) == 0 && evtName == uncoreEvtName) { - eventSplitMap.emplace(uncoreEventString, evt); + if (strncmp(uncoreEvent.c_str(), devName.c_str(), devName.length()) == 0 && evtName == uncoreEvtName) { + invalidFlag = false; + eventSplitMap.emplace(uncoreEvent, evt); } } + if (invalidFlag) { + string err = "Invalid uncore event " + string(evt); + New(LIBPERF_ERR_INVALID_EVENT, err); + } } static void SplitUncoreEvent(struct PmuAttr *attr, unordered_map &eventSplitMap) -- Gitee From 7976d86352923fd6a9fbdda6555399202e00c01d Mon Sep 17 00:00:00 2001 From: Junyi Ye <294572668@qq.com> Date: Mon, 29 Apr 2024 14:31:58 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=AA=A2=E8=A6=96=E6=84=8F=E8=A6=8B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pmu/pfm/pfm.cpp | 14 +++++++++++++- pmu/pmu.cpp | 17 ++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pmu/pfm/pfm.cpp b/pmu/pfm/pfm.cpp index fbbb93b..598c769 100644 --- a/pmu/pfm/pfm.cpp +++ b/pmu/pfm/pfm.cpp @@ -105,9 +105,21 @@ static bool CheckEventInList(enum PmuEventType eventType, const char *pmuName) return false; } +static bool CheckRawEvent(const char *pmuName) +{ + // check raw event name like 'r11' or 'r60ea' is valid or not + const char *numStr = pmuName + 1; + char *endPtr; + __u64 _ = strtol(numStr, &endPtr, 16); + if (*endPtr != '\0') { + return false; + } + return true; +} + static int GetEventType(const char *pmuName) { - if (pmuName[0] == 'r') { + if (pmuName[0] == 'r' && CheckRawEvent(pmuName)) { return RAW_TYPE; } if (CheckEventInList(CORE_EVENT, pmuName)) { diff --git a/pmu/pmu.cpp b/pmu/pmu.cpp index 81df7e4..44d85d7 100644 --- a/pmu/pmu.cpp +++ b/pmu/pmu.cpp @@ -131,7 +131,7 @@ static int CheckAttr(enum PmuTaskType collectType, struct PmuAttr *attr) return SUCCESS; } -void AppendChildEvents(char* evt, unordered_map& eventSplitMap) +static bool AppendChildEvents(char* evt, unordered_map& eventSplitMap) { string strName(evt); auto findSlash = strName.find('/'); @@ -141,7 +141,7 @@ void AppendChildEvents(char* evt, unordered_map& eventSplitMap) auto uncoreEventList = uncoreEventPair.second; if (uncoreEventList == nullptr) { New(LIBPERF_ERR_INVALID_EVENT, "Invalid uncore event list"); - return; + return false; } bool invalidFlag = true; for (int i = 0; i < numEvt; ++i) { @@ -160,10 +160,12 @@ void AppendChildEvents(char* evt, unordered_map& eventSplitMap) if (invalidFlag) { string err = "Invalid uncore event " + string(evt); New(LIBPERF_ERR_INVALID_EVENT, err); + return false; } + return true; } -static void SplitUncoreEvent(struct PmuAttr *attr, unordered_map &eventSplitMap) +static bool SplitUncoreEvent(struct PmuAttr *attr, unordered_map &eventSplitMap) { char** evtList = attr->evtList; unsigned size = attr->numEvt; @@ -178,13 +180,16 @@ static void SplitUncoreEvent(struct PmuAttr *attr, unordered_map char* prevChar = slashPos - 1; if (!std::isdigit(*prevChar)) { // 添加子事件 - AppendChildEvents(evt, eventSplitMap); + if (!AppendChildEvents(evt, eventSplitMap)){ + return false; + } continue; } } eventSplitMap.emplace(evt, evt); newSize++; } + return true; } static unsigned GenerateSplitList(unordered_map& eventSplitMap, vector &newEvtlist) @@ -222,7 +227,9 @@ int PmuOpen(enum PmuTaskType collectType, struct PmuAttr *attr) return -1; } unordered_map eventSplitMap; - SplitUncoreEvent(attr, eventSplitMap); + if (!SplitUncoreEvent(attr, eventSplitMap)) { + return -1; + } auto previousEventList = make_pair(attr->numEvt, attr->evtList); vector newEvtlist; attr->numEvt = GenerateSplitList(eventSplitMap, newEvtlist); -- Gitee