From fe142aed240b6223881f9443f0bb425f4125dabe Mon Sep 17 00:00:00 2001 From: Junyi Ye <294572668@qq.com> Date: Mon, 20 May 2024 17:39:34 +0800 Subject: [PATCH] Pfm support uncore raw type. --- pmu/pfm/pfm.cpp | 5 ++++ pmu/pfm/pfm_event.h | 1 + pmu/pfm/uncore.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++-- pmu/pfm/uncore.h | 2 ++ 4 files changed, 78 insertions(+), 3 deletions(-) diff --git a/pmu/pfm/pfm.cpp b/pmu/pfm/pfm.cpp index 15c3636..7b617ce 100644 --- a/pmu/pfm/pfm.cpp +++ b/pmu/pfm/pfm.cpp @@ -87,6 +87,7 @@ static const std::unordered_map EvtMap{ {KUNPENG_PMU::RAW_TYPE, GetRawEvent}, {KUNPENG_PMU::CORE_TYPE, GetCoreEvent}, {KUNPENG_PMU::UNCORE_TYPE, GetUncoreEvent}, + {KUNPENG_PMU::UNCORE_RAW_TYPE, GetUncoreRawEvent}, {KUNPENG_PMU::TRACE_TYPE, GetKernelTraceEvent}, }; @@ -134,6 +135,10 @@ static int GetEventType(const char *pmuName) if (CheckEventInList(UNCORE_EVENT, pmuName)) { return UNCORE_TYPE; } + // Parse uncore event raw name like 'hisi_sccl3_ddrc0/config=0x0/' + if (CheckUncoreRawEvent(pmuName)) { + return UNCORE_RAW_TYPE; + } return -1; } diff --git a/pmu/pfm/pfm_event.h b/pmu/pfm/pfm_event.h index 99b5514..4987d65 100644 --- a/pmu/pfm/pfm_event.h +++ b/pmu/pfm/pfm_event.h @@ -25,6 +25,7 @@ namespace KUNPENG_PMU { RAW_TYPE, CORE_TYPE, UNCORE_TYPE, + UNCORE_RAW_TYPE, TRACE_TYPE, }; diff --git a/pmu/pfm/uncore.cpp b/pmu/pfm/uncore.cpp index f5f67c3..674e14f 100644 --- a/pmu/pfm/uncore.cpp +++ b/pmu/pfm/uncore.cpp @@ -22,6 +22,28 @@ using namespace std; using namespace KUNPENG_PMU; +static const string CONFIG_EQUAL = "config="; + +static int ParseConfig(const char* pmuName) { + string strName = pmuName; + auto findSlash = strName.find('/'); + string devName = strName.substr(0, findSlash); + string evtName = strName.substr(devName.size() + 1, strName.size() - 1 - (devName.size() + 1)); + try { + size_t pos = evtName.find('='); + string config = evtName.substr(pos + 1); + if (config.find("0x") != string::npos) { + // hexadecimal config + return stoi(config, nullptr, 16); + } else { + // decimal config + return stoi(config); + } + } catch (const invalid_argument& e) { + return -1; + } +} + static int GetDeviceType(const string &devName) { string typePath = "/sys/devices/" + devName + "/type"; @@ -102,15 +124,40 @@ int FillUncoreFields(const char* pmuName, PmuEvt *evt) if (cpuMask == -1) { return UNKNOWN_ERROR; } - if (GetUncoreEventConfig(pmuName) == -1) { - return LIBPERF_ERR_INVALID_EVENT; - } evt->cpumask = cpuMask; evt->name = pmuName; return SUCCESS; } +bool CheckUncoreRawEvent(const char *pmuName) +{ + string strName = pmuName; + unsigned numEvt; + auto eventList = PmuEventList(UNCORE_EVENT, &numEvt); + if (eventList == nullptr) { + return false; + } + auto findSlash = strName.find('/'); + string devName = strName.substr(0, findSlash); + string evtName = strName.substr(devName.size() + 1, strName.size() - 1 - (devName.size() + 1)); + // check if "config=" at back part of pmuName + if (strstr(evtName.c_str(), CONFIG_EQUAL.c_str()) == nullptr) { + return false; + } + // check if config invalid + if (ParseConfig(pmuName) == -1) { + return false; + } + // check if front part of pmuName in Uncore Pmu Event List + for (int j = 0; j < numEvt; ++j) { + if (strstr(eventList[j], devName.c_str()) == nullptr) { + return true; + } + } + return false; +} + struct PmuEvt* GetUncoreEvent(const char* pmuName, int collectType) { int64_t config = GetUncoreEventConfig(pmuName); @@ -123,6 +170,26 @@ struct PmuEvt* GetUncoreEvent(const char* pmuName, int collectType) pmuEvtPtr->pmuType = UNCORE_TYPE; pmuEvtPtr->collectType = collectType; + // Fill fields for uncore devices. + auto err = FillUncoreFields(pmuName, pmuEvtPtr); + if (err != SUCCESS) { + return nullptr; + } + return pmuEvtPtr; +} + +struct PmuEvt* GetUncoreRawEvent(const char* pmuName, int collectType) +{ + int64_t config = ParseConfig(pmuName); + if (config == -1) { + return nullptr; + } + auto* pmuEvtPtr = new PmuEvt {0}; + pmuEvtPtr->config = config; + pmuEvtPtr->name = pmuName; + pmuEvtPtr->pmuType = UNCORE_RAW_TYPE; + pmuEvtPtr->collectType = collectType; + // Fill fields for uncore devices. auto err = FillUncoreFields(pmuName, pmuEvtPtr); if (err != SUCCESS) { diff --git a/pmu/pfm/uncore.h b/pmu/pfm/uncore.h index 69ed562..0412e9a 100644 --- a/pmu/pfm/uncore.h +++ b/pmu/pfm/uncore.h @@ -16,6 +16,8 @@ #define UNCORE_H #include "pmu_event.h" +bool CheckUncoreRawEvent(const char *pmuName); struct PmuEvt* GetUncoreEvent(const char* pmuName, int collectType); +struct PmuEvt* GetUncoreRawEvent(const char* pmuName, int collectType); #endif \ No newline at end of file -- Gitee