From 4a89159a4c953be44dc81a88bb5cd818d50eb0a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E9=BE=99?= Date: Mon, 31 Mar 2025 22:00:17 +0800 Subject: [PATCH 1/4] config metric evtList and add error code! --- include/pcerrc.h | 6 + include/pmu.h | 26 +-- pmu/pfm/uncore.cpp | 25 +-- pmu/pmu_event_list.cpp | 2 +- pmu/pmu_metric.cpp | 354 ++++++++++++++++++++++++++++++++++ python/modules/CMakeLists.txt | 2 +- util/common.cpp | 65 ++++++- util/common.h | 6 + 8 files changed, 448 insertions(+), 38 deletions(-) create mode 100644 pmu/pmu_metric.cpp diff --git a/include/pcerrc.h b/include/pcerrc.h index 465c25b..bbe22a8 100644 --- a/include/pcerrc.h +++ b/include/pcerrc.h @@ -90,6 +90,12 @@ extern "C" { #define LIBPERF_ERR_NOT_SUPPORT_CONFIG_EVENT 1046 #define LIBPERF_ERR_INVALID_BLOCKED_SAMPLE 1047 #define LIBPERF_ERR_NOT_SUPPORT_GROUP_EVENT 1048 +#define LIBPERF_ERR_INVALID_PMU_DEVICES_METRIC 1049 +#define LIBPERF_ERR_INVALID_PMU_DEVICES_BDF 1050 +#define LIBPERF_ERR_OPEN_INVALID_FILE 1051 +#define LIBPERF_ERR_INVALID_BDF_VALUE 1052 +#define LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF 1053 +#define LIBPERF_ERR_NOT_SOUUPUT_SMMU_BDF 1054 #define UNKNOWN_ERROR 9999 diff --git a/include/pmu.h b/include/pmu.h index b96baf4..c702a7f 100644 --- a/include/pmu.h +++ b/include/pmu.h @@ -409,20 +409,22 @@ enum PmuDeviceMetric { PMU_DDR_WRITE_BW, // Collect L3 access bandwidth. PMU_L3_BW, - // Collect L3 hit count. - PMU_L3_HIT, - // Collect L3 total reference count, including miss and hit count. - PMU_L3_REF, + // Collect L3 miss count. + PMU_L3_MISS, // Collect L3 average latency. PMU_L3_LAT, - // Collect pcie rx bandwidth. - PMU_PCIE_RX_BW, - // Collect pcie tx bandwidth. - PMU_PCIE_TX_BW, - // Collect pcie rx latency. - PMU_PCIE_RX_LAT, - // Collect pcie tx latency. - PMU_PCIE_TX_LAT, + // Collect Ring2PA_ALL bandwidth. + PMU_RING2PA_ALL_BW, + // Collect PA2Ring_ALL bandwidth. + PMU_PA2RING_ALL_BW, + // Collect pcie rx read memory bandwidth. + PMU_PCIE_RX_MRD_BW, + // Collect pcie rx write memory bandwidth. + PMU_PCIE_RX_MWR_BW, + // Collect pcie tx read memory bandwidth. + PMU_PCIE_TX_MRD_BW, + // Collect pcie tx write memory bandwidth. + PMU_PCIE_TX_MWR_BW, // Collect smmu address transaction. PMU_SMMU_TRAN }; diff --git a/pmu/pfm/uncore.cpp b/pmu/pfm/uncore.cpp index 75b4200..b4312b2 100644 --- a/pmu/pfm/uncore.cpp +++ b/pmu/pfm/uncore.cpp @@ -104,9 +104,10 @@ int FillUncoreFields(const char* pmuName, PmuEvt *evt) } evt->type = devType; int cpuMask = GetCpuMask(devName); - if (cpuMask == -1) { - return UNKNOWN_ERROR; - } + // if is armv8 的uncore事件是没有这个cpumask文件 此时默认为-1 暂时不知道这个参数的作用是啥 + // if (cpuMask == -1) { + // return UNKNOWN_ERROR; + // } evt->cpumask = cpuMask; evt->name = pmuName; @@ -360,21 +361,9 @@ struct PmuEvt* GetUncoreRawEvent(const char* pmuName, int collectType) } auto fieldsValues = unCoreRawFieldsValues[(std::string)pmuName]; auto* pmuEvtPtr = new PmuEvt {0}; - if (fieldsValues.find("config") == fieldsValues.end()) { - pmuEvtPtr->config = 0; - } else { - pmuEvtPtr->config = fieldsValues.at("config"); - } - if (fieldsValues.find("config1") == fieldsValues.end()) { - pmuEvtPtr->config1 = 0; - } else { - pmuEvtPtr->config1 = fieldsValues.at("config1"); - } - if (fieldsValues.find("config2") == fieldsValues.end()) { - pmuEvtPtr->config2 = 0; - } else { - pmuEvtPtr->config2 = fieldsValues.at("config2"); - } + pmuEvtPtr->config = fieldsValues.find("config") == fieldsValues.end() ? 0 : fieldsValues.at("config"); + pmuEvtPtr->config1 = fieldsValues.find("config1") == fieldsValues.end() ? 0 : fieldsValues.at("config1"); + pmuEvtPtr->config2 = fieldsValues.find("config2") == fieldsValues.end() ? 0 : fieldsValues.at("config2"); pmuEvtPtr->name = pmuName; pmuEvtPtr->pmuType = UNCORE_RAW_TYPE; diff --git a/pmu/pmu_event_list.cpp b/pmu/pmu_event_list.cpp index b96e500..c85bc52 100644 --- a/pmu/pmu_event_list.cpp +++ b/pmu/pmu_event_list.cpp @@ -37,7 +37,7 @@ static const string EVENT_DIR = "/events/"; static std::mutex pmuEventListMtx; -static vector supportDevPrefixs = {"hisi", "smmuv3", "hns3"}; +static vector supportDevPrefixs = {"hisi", "smmuv3", "hns3", "armv8"}; static vector uncoreEventList; static vector traceEventList; diff --git a/pmu/pmu_metric.cpp b/pmu/pmu_metric.cpp new file mode 100644 index 0000000..f707555 --- /dev/null +++ b/pmu/pmu_metric.cpp @@ -0,0 +1,354 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "common.h" +#include "symbol.h" +#include "pmu.h" +#include "pcerrc.h" +#include "pcerr.h" + +using namespace std; +using namespace pcerr; + +static const string SYS_DEVICES = "/sys/devices/"; +static const string SYS_BUS_PCI_DEVICES = "/sys/bus/pci/devices"; +static const string SYS_IOMMU_DEVICES = "/sys/class/iommu"; +static vector supportedDevicePrefixes = {"hisi", "smmuv3", "hns3", "armv8"}; +static vector uncoreRawDeviceList; +static vector> pciePmuBdfRang; +static unordered_map bdfToSmmuMap; +static unordered_map> classifiedDevices; + +struct UncoreDeviceConfig { + string devicePrefix; // 设备前缀,用于匹配设备名称 + string subDeviceName; // 子设备名称,用于进一步过滤设备 + vector events; // 事件码列表 + string extraConfig; // 额外配置参数(如 filter_enable=1) + string bdfParameter; // BDF 参数键(如 "bdf=" 或 "filter_stream_id=") + unsigned splitPosition; // 分割位置,用于分类设备 +}; + +static const unordered_map DEVICE_CONFIGS = { + {PmuDeviceMetric::PMU_DDR_READ_BW, {"hisi_sccl", "ddrc", {"0x84"}, "", "", 1}}, + {PmuDeviceMetric::PMU_DDR_WRITE_BW, {"hisi_sccl", "ddrc", {"0x83"}, "", "", 1}}, + {PmuDeviceMetric::PMU_L3_BW, {"armv8_pmu", "", {"0x0032"}, "", "", 0}}, + {PmuDeviceMetric::PMU_L3_MISS, {"armv8_pmu", "", {"0x0033"}, "", "", 0}}, + {PmuDeviceMetric::PMU_L3_LAT, {"hisi_sccl", "l3c", {"0x80"}, "", "", 1}}, + {PmuDeviceMetric::PMU_PA2RING_ALL_BW, {"hisi_sicl", "_pa", {"0x60", "0x61", "0x62", "0x63"}, "", "", 1}}, + {PmuDeviceMetric::PMU_RING2PA_ALL_BW, {"hisi_sicl", "_pa", {"0x40", "0x41", "0x42", "0x43"}, "", "", 1}}, + {PmuDeviceMetric::PMU_PCIE_RX_MRD_BW, {"hisi_pcie", "core", {"0x0804", "0x10804"}, "", "bdf=", 1}}, + {PmuDeviceMetric::PMU_PCIE_RX_MWR_BW, {"hisi_pcie", "core", {"0x0104", "0x10104"}, "", "bdf=", 1}}, + {PmuDeviceMetric::PMU_PCIE_TX_MRD_BW, {"hisi_pcie", "core", {"0x0405", "0x10405"}, "", "bdf=", 1}}, + {PmuDeviceMetric::PMU_PCIE_TX_MWR_BW, {"hisi_pcie", "core", {"0x0105", "0x10105"}, "", "bdf=", 1}}, + {PmuDeviceMetric::PMU_SMMU_TRAN, {"smmuv3_pmcg", "", {"0x1"}, "filter_enable=1", "filter_stream_id=", 2}} +}; + +void QueryUncoreRawDevices() +{ + if (!uncoreRawDeviceList.empty()) return; + if (!ExistPath(SYS_DEVICES) || !IsDirectory(SYS_DEVICES)) { + New(LIBPERF_ERR_QUERY_EVENT_LIST_FAILED, "Query uncore evtlist falied!"); + return; + } + vector entries = ListDirectoryEntries(SYS_DEVICES); + for (const auto& entry : entries) { + for (auto devPrefix : supportedDevicePrefixes) { + if (entry.find(devPrefix) == 0) { + uncoreRawDeviceList.emplace_back(entry); + break; + } + } + } +} + +void QueryBdfToSmmuMapping() +{ + if (!bdfToSmmuMap.empty()) { + return; + } + if (!ExistPath(SYS_IOMMU_DEVICES) || !IsDirectory(SYS_IOMMU_DEVICES)) { + cerr << "Directory does not exist or is not a directory: " << SYS_IOMMU_DEVICES << endl; + return; + } + vector entries = ListDirectoryEntries(SYS_IOMMU_DEVICES); + for (const auto& entry : entries) { + string devicesPath = SYS_IOMMU_DEVICES + "/" + entry + "/devices"; + if (ExistPath(devicesPath) && IsDirectory(devicesPath)) { + vector bdfEntries = ListDirectoryEntries(devicesPath); + for (const auto& bdfEntry : bdfEntries) { + if (IsDirectory(devicesPath + "/" + bdfEntry)) { + string bdfStr = bdfEntry; + if (bdfStr.find("0000:") != string::npos) { + string bdfValue = bdfStr.substr(bdfStr.find("0000:") + strlen("0000:")); + bdfToSmmuMap[bdfValue] = entry; + } + } + } + } + } +} + +int ConvertBdfStringToValue(const string& bdfStr, uint16_t& bdfValue) +{ + vector busDeviceFunction = SplitStringByDelimiter(bdfStr, ':'); + if (busDeviceFunction.size() != 2) { + New(LIBPERF_ERR_INVALID_BDF_VALUE, "bdf value is invalid, shoubld be like 00:00.0"); + return LIBPERF_ERR_INVALID_BDF_VALUE; + } + vector deviceFunction = SplitStringByDelimiter(busDeviceFunction[1], '.'); + if (deviceFunction.size() != 2) { + return LIBPERF_ERR_INVALID_BDF_VALUE; + } + uint16_t bus = 0; + uint16_t device = 0; + uint16_t function = 0; + try { + bus = static_cast(stoul(busDeviceFunction[0], nullptr, 16)); + device = static_cast(stoul(deviceFunction[0], nullptr, 16)); + function = static_cast(stoul(deviceFunction[1], nullptr, 16)); + } catch (const std::invalid_argument& e) { + return LIBPERF_ERR_INVALID_BDF_VALUE; + } + if (bus > 0xFF || device > 0x1F || function > 0x7) { + return LIBPERF_ERR_INVALID_BDF_VALUE; + } + bdfValue = (static_cast(bus) << 8) | (static_cast(device) << 3) | function; + return SUCCESS; +} + +string ConvertSmmuToDeviceAddress(const string& smmuDeviceName) +{ + const string prefix = "smmu3.0x"; + const uint64_t PMU_OFFSET = 0x20000; + size_t pos = smmuDeviceName.find(prefix); + string hexAddressStr = smmuDeviceName.substr(prefix.length()); + uint64_t physicalBaseAddress = stoul(hexAddressStr, nullptr, 16); + uint64_t pmuPhysicalAddress = physicalBaseAddress + PMU_OFFSET; + uint64_t pmuSuffix = pmuPhysicalAddress >> 12; + stringstream result; + result << hex << uppercase << pmuSuffix; + return result.str(); +} + +int FindSmmuDeviceByBdf(const string& bdf, string& smmuPmuName) +{ + auto it = bdfToSmmuMap.find(bdf); + if (it == bdfToSmmuMap.end()) { + cerr << "BDF Value " << bdf << " not found in any SMMU Directory." << endl; + SetCustomErr(LIBPERF_ERR_NOT_SOUUPUT_SMMU_BDF, "BDF Value " + bdf + " not found in any SMMU Directory."); + return LIBPERF_ERR_NOT_SOUUPUT_SMMU_BDF; + } + string smmuPmuKey = ConvertSmmuToDeviceAddress(it->second); + const auto smmu = classifiedDevices.find(smmuPmuKey); + if (smmu == classifiedDevices.end()) { + SetCustomErr(LIBPERF_ERR_NOT_SOUUPUT_SMMU_BDF, "BDF Value " + bdf + " not manage in any SMMU Directory."); + return LIBPERF_ERR_NOT_SOUUPUT_SMMU_BDF; + } + smmuPmuName = smmu->second.front(); + return SUCCESS; +} + +void QueryPcieBdfRanges() +{ + if (!pciePmuBdfRang.empty()) return; + if (!ExistPath(SYS_DEVICES) || !IsDirectory(SYS_DEVICES)) { + cerr << "PCI devices directory not found: " << SYS_DEVICES << endl; + return; + } + vector entries = ListDirectoryEntries(SYS_DEVICES); + for (const auto& entry : entries) { + if (entry.find("pcie") != string::npos) { + string bdfMinPath = SYS_DEVICES + "/" + entry + "/bdf_min"; + string bdfMaxPath = SYS_DEVICES + "/" + entry + "/bdf_max"; + if (ExistPath(bdfMinPath) && ExistPath(bdfMaxPath)) { + string bdfMinStr = ReadFileContent(bdfMinPath); + string bdfMaxStr = ReadFileContent(bdfMaxPath); + if (bdfMinStr.empty() || bdfMaxStr.empty()) continue; + uint16_t bdfMin = stoul(bdfMinStr, nullptr, 16); + uint16_t bdfMax = stoul(bdfMaxStr, nullptr, 16); + pciePmuBdfRang.emplace_back(entry, bdfMin, bdfMax); + } + } + } +} + +int FindPcieDeviceByBdf(const string& bdf, string& pciePmuName) +{ + uint16_t userBdf = 0; + int err = ConvertBdfStringToValue(bdf, userBdf); + if (err != SUCCESS) { + return err; + } + for (const auto& [deviceName, bdfMin, bdfMax] : pciePmuBdfRang) { + if (userBdf >= bdfMin && userBdf <= bdfMax) { + cout << "BDF Value " << bdf << " is managed by PCIe Device: " << deviceName << endl; + pciePmuName = deviceName; + return SUCCESS; + } + } + cout << "BDF Value " << bdf << " is not managed by any PCIe Device." << endl; + SetCustomErr(LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF, "bdf value " + bdf + " is not managed by any PCIe Device."); + return LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF; +} + +bool ValidateBdfValue(const string& bdf) +{ + if (!ExistPath(SYS_BUS_PCI_DEVICES) || !IsDirectory(SYS_BUS_PCI_DEVICES)) { + cerr << "PCI devices directory not found: " << SYS_BUS_PCI_DEVICES << endl; + return false; + } + unordered_set validBdfSet; + vector entries = ListDirectoryEntries(SYS_BUS_PCI_DEVICES); + for (const auto& entry : entries) { + if (entry.size() > 5 && entry.substr(0, 5) == "0000:") { + validBdfSet.insert(entry.substr(5)); + } + } + if (validBdfSet.find(bdf) == validBdfSet.end()) { + cerr << "Invalid BDF value: " << bdf << ". Not found in /sys/bus/pci/devices/" << endl; + return false; + } + return true; +} + +void ClassifyDevicesByPrefix(const string& devicePrefix, const string& subDeviceName, unsigned splitPos) +{ + for (const auto& device : uncoreRawDeviceList) { + if (device.find(devicePrefix) != string::npos && device.find(subDeviceName) != string::npos) { + if (splitPos == 0) { + classifiedDevices[device].push_back(device); + continue; + } + vector splitParts = SplitStringByDelimiter(device, '_'); + if (splitParts.size() > splitPos) { + classifiedDevices[splitParts[splitPos]].push_back(device); + } + } + } +} + +vector GenerateEventStrings(const vector& events, const string& extraConfig, + const string& bdfParameter, const string& bdf) +{ + vector eventList; + if (!bdf.empty()) { + for (const auto& evt : events) { + string device = ""; + if (bdfParameter == "bdf=") { + int err = FindPcieDeviceByBdf(bdf, device); + if (err != SUCCESS) { + return {}; + } + } else { + int err = FindSmmuDeviceByBdf(bdf, device); + if (err != SUCCESS) { + return {}; + } + } + string eventString = device + "/config=" + evt; + if (!extraConfig.empty()) eventString += "," + extraConfig; + if (!bdfParameter.empty() && !bdf.empty()) { + stringstream bdfValue; + uint16_t userBdf = 0; + ConvertBdfStringToValue(bdf, userBdf); + bdfValue << "0x" << hex << userBdf; + eventString += "," + bdfParameter + bdfValue.str(); + } + eventString += "/"; + eventList.push_back(eventString); + } + return eventList; + } + for (const auto& [key, devices] : classifiedDevices) { + for (const auto& evt : events) { + for (const auto& device : devices) { + string eventString = device + "/config=" + evt + "/"; + eventList.push_back(eventString); + } + } + } + return eventList; +} + +vector GenerateEventList(PmuDeviceMetric metric, const string& bdf = "") +{ + const auto& config = DEVICE_CONFIGS.at(metric); + QueryUncoreRawDevices(); + ClassifyDevicesByPrefix(config.devicePrefix, config.subDeviceName, config.splitPosition); + if (!bdf.empty()) { + QueryPcieBdfRanges(); + QueryBdfToSmmuMapping(); + } + return GenerateEventStrings(config.events, config.extraConfig, config.bdfParameter, bdf); +} + +int CheckDeviceMetricEnum(struct PmuDeviceAttr *attr, unsigned len) +{ + for (int i = 0; i < len; ++i) { + if (attr[i].metric > PMU_SMMU_TRAN) { + New(LIBPERF_ERR_INVALID_PMU_DEVICES_METRIC, "invalid value for PmuDeviceMetric!"); + return LIBPERF_ERR_INVALID_PMU_DEVICES_METRIC; + } + } + return SUCCESS; +} + +int CheckBdf(struct PmuDeviceAttr *attr, unsigned len) +{ + for (int i = 0; i < len; ++i) { + if (attr[i].metric >= PMU_PCIE_RX_MRD_BW && attr[i].bdf == nullptr) { + New(LIBPERF_ERR_INVALID_PMU_DEVICES_BDF, "When collecting pcie or smmu metric, bdf value can not is nullptr!"); + return LIBPERF_ERR_INVALID_PMU_DEVICES_BDF; + } + } + return SUCCESS; +} + +int CheckPmuDeviceAttr(struct PmuDeviceAttr *attr, unsigned len) +{ + int err = CheckDeviceMetricEnum(attr, len); + if (!err) { + return err; + } + + err = CheckBdf(attr, len); + if (!err) { + return err; + } + + return SUCCESS; +} + +int PmuDeviceOpen(struct PmuDeviceAttr *attr, unsigned len) +{ + if (CheckPmuDeviceAttr(attr, len) != SUCCESS) { + return -1; + } + vector configEvtList; + for (int i = 0; i < len; ++i) { + vector temp = GenerateEventList(attr[i].metric, (string)attr[i].bdf); + if (temp.empty()) { + return -1; + } + configEvtList.insert(configEvtList.end(), temp.begin(), temp.end()); + } + + vector evts; + for (auto& evt : configEvtList) { + evts.push_back(const_cast(evt.c_str())); + } + + PmuAttr attrConfig = {0}; + attrConfig.evtList = evts.data(); + attrConfig.numEvt = evts.size(); + int pd = PmuOpen(COUNTING, &attrConfig); + return pd; +} \ No newline at end of file diff --git a/python/modules/CMakeLists.txt b/python/modules/CMakeLists.txt index 40b53a1..8f898fc 100644 --- a/python/modules/CMakeLists.txt +++ b/python/modules/CMakeLists.txt @@ -22,7 +22,7 @@ configure_file( ) add_custom_target(${PROJECT_NAME} ALL - COMMAND ${PYTHON_EXECUTABLE} setup.py install + COMMAND ${PYTHON_EXECUTABLE} setup.py install --user WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/util/common.cpp b/util/common.cpp index ddf3613..21cacd1 100644 --- a/util/common.cpp +++ b/util/common.cpp @@ -14,12 +14,18 @@ ******************************************************************************/ #include +#include +#include +#include +#include #include #include +#include #include #include #include #include "pcerrc.h" +#include "pcerr.h" #include "common.h" bool IsValidIp(unsigned long ip) { @@ -48,7 +54,43 @@ bool IsValidPath(const std::string& filePath) return true; } -int RaiseNumFd(unsigned long numFd) +bool IsDirectory(const std::string& path) +{ + struct stat statbuf; + return stat(path.c_str(), &statbuf) == 0 && S_ISDIR(statbuf.st_mode); +} + +std::vector ListDirectoryEntries(const std::string& dirPath) +{ + std::vector entries; + DIR* dir = opendir(dirPath.c_str()); + if (!dir) { + pcerr::SetCustomErr(LIBPERF_ERR_OPEN_INVALID_FILE, "Failed to open directory: " + dirPath); + return entries; + } + struct dirent* entry; + while ((entry = readdir(dir)) != nullptr) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; + entries.push_back(entry->d_name); + } + closedir(dir); + return entries; +} + +std::string ReadFileContent(const std::string& filePath) +{ + std::ifstream file(filePath); + if (!file.is_open()) { + pcerr::SetCustomErr(LIBPERF_ERR_OPEN_INVALID_FILE, "Failed to open File: " + filePath); + return ""; + } + std::string content; + std::getline(file, content); + file.close(); + return content; +} + +int RaiseNumFd(uint64_t numFd) { unsigned long extra = 50; unsigned long setNumFd = extra + numFd; @@ -76,14 +118,25 @@ int RaiseNumFd(unsigned long numFd) } bool ExistPath(const std::string &filePath) { - struct stat st{}; - if(stat(filePath.c_str(), &st) != 0) { - return false; + struct stat statbuf{}; + return stat(filePath.c_str(), &statbuf) == 0; +} + +std::vector SplitStringByDelimiter(const std::string& str, char delimiter) +{ + std::vector parts; + std::stringstream ss(str); + std::string part; + while (std::getline(ss, part, delimiter)) { + if (!part.empty()) { + parts.push_back(part); + } } - return true; + return parts; } -std::string GetTraceEventDir() { +std::string GetTraceEventDir() +{ if (ExistPath(TRACE_EVENT_PATH)) { return TRACE_EVENT_PATH; } diff --git a/util/common.h b/util/common.h index 5e5d0aa..4ffd0c8 100644 --- a/util/common.h +++ b/util/common.h @@ -17,6 +17,7 @@ #define LIBKPROF_COMMON_H #include #include +#include const std::string TRACE_EVENT_PATH = "/sys/kernel/tracing/events/"; const std::string TRACE_DEBUG_EVENT_PATH = "/sys/kernel/debug/tracing/events/"; @@ -24,6 +25,11 @@ const std::string TRACE_DEBUG_EVENT_PATH = "/sys/kernel/debug/tracing/events/"; bool IsValidIp(unsigned long ip); std::string GetRealPath(const std::string filePath); bool IsValidPath(const std::string& filePath); +bool IsDirectory(const std::string& path); +bool FileExists(const std::string& path); +std::vector ListDirectoryEntries(const std::string& dirPath); +std::string ReadFileContent(const std::string& filePath); +std::vector SplitStringByDelimiter(const std::string& str, char delimiter); int RaiseNumFd(uint64_t numFd); bool ExistPath(const std::string& filePath); std::string GetTraceEventDir(); -- Gitee From 4860506bc7b72b784d6ab9950066ecaf304995bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E9=BE=99?= Date: Tue, 1 Apr 2025 17:24:45 +0800 Subject: [PATCH 2/4] fix adapt 920 and 920B --- pmu/pmu_metric.cpp | 254 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 227 insertions(+), 27 deletions(-) diff --git a/pmu/pmu_metric.cpp b/pmu/pmu_metric.cpp index f707555..e3c59ad 100644 --- a/pmu/pmu_metric.cpp +++ b/pmu/pmu_metric.cpp @@ -10,6 +10,7 @@ #include #include #include "common.h" +#include "cpu_map.h" #include "symbol.h" #include "pmu.h" #include "pcerrc.h" @@ -25,7 +26,6 @@ static vector supportedDevicePrefixes = {"hisi", "smmuv3", "hns3", static vector uncoreRawDeviceList; static vector> pciePmuBdfRang; static unordered_map bdfToSmmuMap; -static unordered_map> classifiedDevices; struct UncoreDeviceConfig { string devicePrefix; // 设备前缀,用于匹配设备名称 @@ -36,21 +36,215 @@ struct UncoreDeviceConfig { unsigned splitPosition; // 分割位置,用于分类设备 }; -static const unordered_map DEVICE_CONFIGS = { - {PmuDeviceMetric::PMU_DDR_READ_BW, {"hisi_sccl", "ddrc", {"0x84"}, "", "", 1}}, - {PmuDeviceMetric::PMU_DDR_WRITE_BW, {"hisi_sccl", "ddrc", {"0x83"}, "", "", 1}}, - {PmuDeviceMetric::PMU_L3_BW, {"armv8_pmu", "", {"0x0032"}, "", "", 0}}, - {PmuDeviceMetric::PMU_L3_MISS, {"armv8_pmu", "", {"0x0033"}, "", "", 0}}, - {PmuDeviceMetric::PMU_L3_LAT, {"hisi_sccl", "l3c", {"0x80"}, "", "", 1}}, - {PmuDeviceMetric::PMU_PA2RING_ALL_BW, {"hisi_sicl", "_pa", {"0x60", "0x61", "0x62", "0x63"}, "", "", 1}}, - {PmuDeviceMetric::PMU_RING2PA_ALL_BW, {"hisi_sicl", "_pa", {"0x40", "0x41", "0x42", "0x43"}, "", "", 1}}, - {PmuDeviceMetric::PMU_PCIE_RX_MRD_BW, {"hisi_pcie", "core", {"0x0804", "0x10804"}, "", "bdf=", 1}}, - {PmuDeviceMetric::PMU_PCIE_RX_MWR_BW, {"hisi_pcie", "core", {"0x0104", "0x10104"}, "", "bdf=", 1}}, - {PmuDeviceMetric::PMU_PCIE_TX_MRD_BW, {"hisi_pcie", "core", {"0x0405", "0x10405"}, "", "bdf=", 1}}, - {PmuDeviceMetric::PMU_PCIE_TX_MWR_BW, {"hisi_pcie", "core", {"0x0105", "0x10105"}, "", "bdf=", 1}}, - {PmuDeviceMetric::PMU_SMMU_TRAN, {"smmuv3_pmcg", "", {"0x1"}, "filter_enable=1", "filter_stream_id=", 2}} +using PMU_METRIC_PAIR = std::pair; + +namespace METRIC_CONFIG { + PMU_METRIC_PAIR DDR_READ_BW_A = { + PmuDeviceMetric::PMU_DDR_READ_BW, + { + "hisi_sccl", + "ddrc", + {"0x1"}, + "", + "", + 1 + } + }; + + PMU_METRIC_PAIR DDR_WRITE_BW_A = { + PmuDeviceMetric::PMU_DDR_WRITE_BW, + { + "hisi_sccl", + "ddrc", + {"0x0"}, + "", + "", + 1 + } + }; + + PMU_METRIC_PAIR DDR_READ_BW_B = { + PmuDeviceMetric::PMU_DDR_READ_BW, + { + "hisi_sccl", + "ddrc", + {"0x84"}, + "", + "", + 1 + } + }; + + PMU_METRIC_PAIR DDR_WRITE_BW_B = { + PmuDeviceMetric::PMU_DDR_WRITE_BW, + { + "hisi_sccl", + "ddrc", + {"0x83"}, + "", + "", + 1 + } + }; + + PMU_METRIC_PAIR L3_BW = { + PmuDeviceMetric::PMU_L3_BW, + { + "armv8_pmu", + "", + {"0x0032"}, + "", + "", + 0 + } + }; + + PMU_METRIC_PAIR L3_MISS = { + PmuDeviceMetric::PMU_L3_MISS, + { + "armv8_pmu", + "", + {"0x0033"}, + "", + "", + 0 + } + }; + + PMU_METRIC_PAIR L3_LAT = { + PmuDeviceMetric::PMU_L3_LAT, + { + "armv8_pmu", + "", + {"0x80"}, + "", + "", + 0 + } + }; + + PMU_METRIC_PAIR PA2RING_ALL_BW = { + PmuDeviceMetric::PMU_PA2RING_ALL_BW, + { + "hisi_sicl", + "_pa", + {"0x60", "0x61", "0x62", "0x63"}, + "", + "", + 1 + } + }; + + PMU_METRIC_PAIR RING2PA_ALL_BW = { + PmuDeviceMetric::PMU_RING2PA_ALL_BW, + { + "hisi_sicl", + "_pa", + {"0x40", "0x41", "0x42", "0x43"}, + "", + "", + 1 + } + + }; + + PMU_METRIC_PAIR PCIE_RX_MRD_BW = { + PmuDeviceMetric::PMU_PCIE_RX_MRD_BW, + { + "hisi_pcie", + "core", + {"0x0804", "0x10804"}, + "", + "bdf=", + 1 + } + }; + + PMU_METRIC_PAIR PCIE_RX_MWR_BW = { + PmuDeviceMetric::PMU_PCIE_RX_MWR_BW, + { + "hisi_pcie", + "core", + {"0x0104", "0x10104"}, + "", + "bdf=", + 1 + } + }; + + PMU_METRIC_PAIR PCIE_TX_MRD_BW = { + PmuDeviceMetric::PMU_PCIE_TX_MRD_BW, + { + "hisi_pcie", + "core", + {"0x0405", "0x10405"}, + "", + "bdf=", + 1 + } + }; + + PMU_METRIC_PAIR PCIE_TX_MWR_BW = { + PmuDeviceMetric::PMU_PCIE_TX_MWR_BW, + { + "hisi_pcie", + "core", + {"0x0105", "0x10105"}, + "", + "bdf=", + 1 + } + }; + + PMU_METRIC_PAIR SMMU_TRAN = { + PmuDeviceMetric::PMU_SMMU_TRAN, + { + "smmuv3_pmcg", + "", + {"0x1"}, + "filter_enable=1", + "filter_stream_id=", + 2 + } + }; +} + +static const unordered_map HIP_A_UNCORE_METRIC_MAP { + METRIC_CONFIG::DDR_READ_BW_A, + METRIC_CONFIG::DDR_WRITE_BW_A, + METRIC_CONFIG::L3_BW, + METRIC_CONFIG::L3_MISS, + METRIC_CONFIG::SMMU_TRAN, }; +static const unordered_map HIP_B_UNCORE_METRIC_MAP { + METRIC_CONFIG::DDR_READ_BW_B, + METRIC_CONFIG::DDR_WRITE_BW_B, + METRIC_CONFIG::L3_BW, + METRIC_CONFIG::L3_MISS, + METRIC_CONFIG::L3_LAT, + METRIC_CONFIG::PA2RING_ALL_BW, + METRIC_CONFIG::RING2PA_ALL_BW, + METRIC_CONFIG::PCIE_RX_MRD_BW, + METRIC_CONFIG::PCIE_RX_MWR_BW, + METRIC_CONFIG::PCIE_TX_MRD_BW, + METRIC_CONFIG::PCIE_TX_MWR_BW, + METRIC_CONFIG::SMMU_TRAN, +}; + +using UNCORE_METRIC_MAP = + std::unordered_map&>; + +const UNCORE_METRIC_MAP UNCORE_METRIC_CONFIG_MAP = { + {CHIP_TYPE::HIPA, HIP_A_UNCORE_METRIC_MAP}, + {CHIP_TYPE::HIPB, HIP_B_UNCORE_METRIC_MAP}, +}; + +unordered_map GetDeviceMtricConfig() +{ + return UNCORE_METRIC_CONFIG_MAP.at(GetCpuType()); +} + void QueryUncoreRawDevices() { if (!uncoreRawDeviceList.empty()) return; @@ -133,12 +327,12 @@ string ConvertSmmuToDeviceAddress(const string& smmuDeviceName) uint64_t physicalBaseAddress = stoul(hexAddressStr, nullptr, 16); uint64_t pmuPhysicalAddress = physicalBaseAddress + PMU_OFFSET; uint64_t pmuSuffix = pmuPhysicalAddress >> 12; - stringstream result; + std::stringstream result; result << hex << uppercase << pmuSuffix; return result.str(); } -int FindSmmuDeviceByBdf(const string& bdf, string& smmuPmuName) +int FindSmmuDeviceByBdf(std::unordered_map>& classifiedDevices, const string& bdf, string& smmuPmuName) { auto it = bdfToSmmuMap.find(bdf); if (it == bdfToSmmuMap.end()) { @@ -219,8 +413,10 @@ bool ValidateBdfValue(const string& bdf) return true; } -void ClassifyDevicesByPrefix(const string& devicePrefix, const string& subDeviceName, unsigned splitPos) +std::unordered_map> ClassifyDevicesByPrefix(const string& devicePrefix, + const string& subDeviceName, unsigned splitPos) { + std::unordered_map> classifiedDevices; for (const auto& device : uncoreRawDeviceList) { if (device.find(devicePrefix) != string::npos && device.find(subDeviceName) != string::npos) { if (splitPos == 0) { @@ -233,10 +429,11 @@ void ClassifyDevicesByPrefix(const string& devicePrefix, const string& subDevice } } } + return classifiedDevices; } -vector GenerateEventStrings(const vector& events, const string& extraConfig, - const string& bdfParameter, const string& bdf) +vector GenerateEventStrings(std::unordered_map> classifiedDevices, + const vector& events, const string& extraConfig, const string& bdfParameter, const string& bdf) { vector eventList; if (!bdf.empty()) { @@ -248,7 +445,7 @@ vector GenerateEventStrings(const vector& events, const string& return {}; } } else { - int err = FindSmmuDeviceByBdf(bdf, device); + int err = FindSmmuDeviceByBdf(classifiedDevices, bdf, device); if (err != SUCCESS) { return {}; } @@ -278,16 +475,19 @@ vector GenerateEventStrings(const vector& events, const string& return eventList; } -vector GenerateEventList(PmuDeviceMetric metric, const string& bdf = "") +vector GenerateEventList(struct PmuDeviceAttr& deviceAttr) { - const auto& config = DEVICE_CONFIGS.at(metric); - QueryUncoreRawDevices(); - ClassifyDevicesByPrefix(config.devicePrefix, config.subDeviceName, config.splitPosition); - if (!bdf.empty()) { + const auto& deviceConfig = GetDeviceMtricConfig(); + const auto& config = deviceConfig.at(deviceAttr.metric); + string bdf = ""; + if (deviceAttr.bdf != nullptr) { QueryPcieBdfRanges(); QueryBdfToSmmuMapping(); + bdf = deviceAttr.bdf; } - return GenerateEventStrings(config.events, config.extraConfig, config.bdfParameter, bdf); + QueryUncoreRawDevices(); + auto classifiedDevices = ClassifyDevicesByPrefix(config.devicePrefix, config.subDeviceName, config.splitPosition); + return GenerateEventStrings(classifiedDevices, config.events, config.extraConfig, config.bdfParameter, bdf); } int CheckDeviceMetricEnum(struct PmuDeviceAttr *attr, unsigned len) @@ -334,7 +534,7 @@ int PmuDeviceOpen(struct PmuDeviceAttr *attr, unsigned len) } vector configEvtList; for (int i = 0; i < len; ++i) { - vector temp = GenerateEventList(attr[i].metric, (string)attr[i].bdf); + vector temp = GenerateEventList(attr[i]); if (temp.empty()) { return -1; } -- Gitee From ccc269c4bc1f6031778bd448f757bfb7b0fc3838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E9=BE=99?= Date: Tue, 1 Apr 2025 18:39:49 +0800 Subject: [PATCH 3/4] add error code --- include/pcerrc.h | 1 + include/pmu.h | 6 ++- pmu/pmu_metric.cpp | 120 +++++++++++++++++++++++++++++++-------------- 3 files changed, 88 insertions(+), 39 deletions(-) diff --git a/include/pcerrc.h b/include/pcerrc.h index bbe22a8..8e34d4c 100644 --- a/include/pcerrc.h +++ b/include/pcerrc.h @@ -96,6 +96,7 @@ extern "C" { #define LIBPERF_ERR_INVALID_BDF_VALUE 1052 #define LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF 1053 #define LIBPERF_ERR_NOT_SOUUPUT_SMMU_BDF 1054 +#define LIBPERF_ERR_INVALID_IOSMMU_DIR 1055 #define UNKNOWN_ERROR 9999 diff --git a/include/pmu.h b/include/pmu.h index c702a7f..816c1e4 100644 --- a/include/pmu.h +++ b/include/pmu.h @@ -407,10 +407,12 @@ enum PmuDeviceMetric { PMU_DDR_READ_BW, // Collect ddr write bandwidth. PMU_DDR_WRITE_BW, - // Collect L3 access bandwidth. - PMU_L3_BW, + // Collect L3 access bytes. + PMU_L3_TRAFFIC, // Collect L3 miss count. PMU_L3_MISS, + // Collect L3 total reference count, including miss and hit count. + PMU_L3_REF, // Collect L3 average latency. PMU_L3_LAT, // Collect Ring2PA_ALL bandwidth. diff --git a/pmu/pmu_metric.cpp b/pmu/pmu_metric.cpp index e3c59ad..6917ecf 100644 --- a/pmu/pmu_metric.cpp +++ b/pmu/pmu_metric.cpp @@ -37,7 +37,7 @@ struct UncoreDeviceConfig { }; using PMU_METRIC_PAIR = std::pair; - +using UNCORE_METRIC_MAP = std::unordered_map&>; namespace METRIC_CONFIG { PMU_METRIC_PAIR DDR_READ_BW_A = { PmuDeviceMetric::PMU_DDR_READ_BW, @@ -87,8 +87,8 @@ namespace METRIC_CONFIG { } }; - PMU_METRIC_PAIR L3_BW = { - PmuDeviceMetric::PMU_L3_BW, + PMU_METRIC_PAIR L3_TRAFFIC = { + PmuDeviceMetric::PMU_L3_TRAFFIC, { "armv8_pmu", "", @@ -111,6 +111,18 @@ namespace METRIC_CONFIG { } }; + PMU_METRIC_PAIR L3_REF = { + PmuDeviceMetric::PMU_L3_REF, + { + "armv8_pmu", + "", + {"0x0032"}, + "", + "", + 0 + } + }; + PMU_METRIC_PAIR L3_LAT = { PmuDeviceMetric::PMU_L3_LAT, { @@ -212,16 +224,18 @@ namespace METRIC_CONFIG { static const unordered_map HIP_A_UNCORE_METRIC_MAP { METRIC_CONFIG::DDR_READ_BW_A, METRIC_CONFIG::DDR_WRITE_BW_A, - METRIC_CONFIG::L3_BW, + METRIC_CONFIG::L3_TRAFFIC, METRIC_CONFIG::L3_MISS, + METRIC_CONFIG::L3_REF, METRIC_CONFIG::SMMU_TRAN, }; static const unordered_map HIP_B_UNCORE_METRIC_MAP { METRIC_CONFIG::DDR_READ_BW_B, METRIC_CONFIG::DDR_WRITE_BW_B, - METRIC_CONFIG::L3_BW, + METRIC_CONFIG::L3_TRAFFIC, METRIC_CONFIG::L3_MISS, + METRIC_CONFIG::L3_REF, METRIC_CONFIG::L3_LAT, METRIC_CONFIG::PA2RING_ALL_BW, METRIC_CONFIG::RING2PA_ALL_BW, @@ -232,25 +246,24 @@ static const unordered_map HIP_B_UNCORE_MET METRIC_CONFIG::SMMU_TRAN, }; -using UNCORE_METRIC_MAP = - std::unordered_map&>; - const UNCORE_METRIC_MAP UNCORE_METRIC_CONFIG_MAP = { {CHIP_TYPE::HIPA, HIP_A_UNCORE_METRIC_MAP}, {CHIP_TYPE::HIPB, HIP_B_UNCORE_METRIC_MAP}, }; -unordered_map GetDeviceMtricConfig() +static unordered_map GetDeviceMtricConfig() { return UNCORE_METRIC_CONFIG_MAP.at(GetCpuType()); } -void QueryUncoreRawDevices() +static int QueryUncoreRawDevices() { - if (!uncoreRawDeviceList.empty()) return; + if (!uncoreRawDeviceList.empty()) { + return SUCCESS; + } if (!ExistPath(SYS_DEVICES) || !IsDirectory(SYS_DEVICES)) { New(LIBPERF_ERR_QUERY_EVENT_LIST_FAILED, "Query uncore evtlist falied!"); - return; + return LIBPERF_ERR_QUERY_EVENT_LIST_FAILED; } vector entries = ListDirectoryEntries(SYS_DEVICES); for (const auto& entry : entries) { @@ -261,16 +274,18 @@ void QueryUncoreRawDevices() } } } + return SUCCESS; } -void QueryBdfToSmmuMapping() +static int QueryBdfToSmmuMapping() { if (!bdfToSmmuMap.empty()) { - return; + return SUCCESS; } if (!ExistPath(SYS_IOMMU_DEVICES) || !IsDirectory(SYS_IOMMU_DEVICES)) { cerr << "Directory does not exist or is not a directory: " << SYS_IOMMU_DEVICES << endl; - return; + SetCustomErr(LIBPERF_ERR_INVALID_IOSMMU_DIR, "Directory does not exist or is not a directory: " + SYS_IOMMU_DEVICES); + return LIBPERF_ERR_INVALID_IOSMMU_DIR; } vector entries = ListDirectoryEntries(SYS_IOMMU_DEVICES); for (const auto& entry : entries) { @@ -286,11 +301,15 @@ void QueryBdfToSmmuMapping() } } } + } else { + SetCustomErr(LIBPERF_ERR_INVALID_IOSMMU_DIR, "Directory does not exist or is not a directory: " + devicesPath); + return LIBPERF_ERR_INVALID_IOSMMU_DIR; } } + return SUCCESS; } -int ConvertBdfStringToValue(const string& bdfStr, uint16_t& bdfValue) +static int ConvertBdfStringToValue(const string& bdfStr, uint16_t& bdfValue) { vector busDeviceFunction = SplitStringByDelimiter(bdfStr, ':'); if (busDeviceFunction.size() != 2) { @@ -318,7 +337,7 @@ int ConvertBdfStringToValue(const string& bdfStr, uint16_t& bdfValue) return SUCCESS; } -string ConvertSmmuToDeviceAddress(const string& smmuDeviceName) +static string ConvertSmmuToDeviceAddress(const string& smmuDeviceName) { const string prefix = "smmu3.0x"; const uint64_t PMU_OFFSET = 0x20000; @@ -332,7 +351,7 @@ string ConvertSmmuToDeviceAddress(const string& smmuDeviceName) return result.str(); } -int FindSmmuDeviceByBdf(std::unordered_map>& classifiedDevices, const string& bdf, string& smmuPmuName) +static int FindSmmuDeviceByBdf(std::unordered_map>& classifiedDevices, const string& bdf, string& smmuPmuName) { auto it = bdfToSmmuMap.find(bdf); if (it == bdfToSmmuMap.end()) { @@ -350,12 +369,15 @@ int FindSmmuDeviceByBdf(std::unordered_map>& classifiedDe return SUCCESS; } -void QueryPcieBdfRanges() +static int QueryPcieBdfRanges() { - if (!pciePmuBdfRang.empty()) return; + if (!pciePmuBdfRang.empty()) { + return SUCCESS; + } if (!ExistPath(SYS_DEVICES) || !IsDirectory(SYS_DEVICES)) { cerr << "PCI devices directory not found: " << SYS_DEVICES << endl; - return; + SetCustomErr(LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF, "PCI devices directory not found: " + SYS_DEVICES); + return LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF; } vector entries = ListDirectoryEntries(SYS_DEVICES); for (const auto& entry : entries) { @@ -365,16 +387,30 @@ void QueryPcieBdfRanges() if (ExistPath(bdfMinPath) && ExistPath(bdfMaxPath)) { string bdfMinStr = ReadFileContent(bdfMinPath); string bdfMaxStr = ReadFileContent(bdfMaxPath); - if (bdfMinStr.empty() || bdfMaxStr.empty()) continue; - uint16_t bdfMin = stoul(bdfMinStr, nullptr, 16); - uint16_t bdfMax = stoul(bdfMaxStr, nullptr, 16); + if (bdfMinStr.empty() || bdfMaxStr.empty()) { + SetCustomErr(LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF, "pcie pmu bdfMin or bdfMax file is empty"); + return LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF; + } + uint16_t bdfMin = 0; + uint16_t bdfMax = 0; + try { + bdfMin = stoul(bdfMinStr, nullptr, 16); + bdfMax = stoul(bdfMaxStr, nullptr, 16); + } catch (const std::exception& e) { + SetCustomErr(LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF, "pcie pmu bdfMin or bdfMax file is invalid"); + return LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF; + } pciePmuBdfRang.emplace_back(entry, bdfMin, bdfMax); + } else { + SetCustomErr(LIBPERF_ERR_INVALID_PMU_DEVICES_BDF, "under pcie pmu dir not exist bdf config file."); + return LIBPERF_ERR_INVALID_PMU_DEVICES_BDF; } } } + return SUCCESS; } -int FindPcieDeviceByBdf(const string& bdf, string& pciePmuName) +static int FindPcieDeviceByBdf(const string& bdf, string& pciePmuName) { uint16_t userBdf = 0; int err = ConvertBdfStringToValue(bdf, userBdf); @@ -393,7 +429,7 @@ int FindPcieDeviceByBdf(const string& bdf, string& pciePmuName) return LIBPERF_ERR_NOT_SOUUPUT_PCIE_BDF; } -bool ValidateBdfValue(const string& bdf) +static bool ValidateBdfValue(const string& bdf) { if (!ExistPath(SYS_BUS_PCI_DEVICES) || !IsDirectory(SYS_BUS_PCI_DEVICES)) { cerr << "PCI devices directory not found: " << SYS_BUS_PCI_DEVICES << endl; @@ -413,7 +449,7 @@ bool ValidateBdfValue(const string& bdf) return true; } -std::unordered_map> ClassifyDevicesByPrefix(const string& devicePrefix, +static std::unordered_map> ClassifyDevicesByPrefix(const string& devicePrefix, const string& subDeviceName, unsigned splitPos) { std::unordered_map> classifiedDevices; @@ -432,7 +468,7 @@ std::unordered_map> ClassifyDevicesByPrefix(const string& return classifiedDevices; } -vector GenerateEventStrings(std::unordered_map> classifiedDevices, +static vector GenerateEventStrings(std::unordered_map> classifiedDevices, const vector& events, const string& extraConfig, const string& bdfParameter, const string& bdf) { vector eventList; @@ -475,33 +511,43 @@ vector GenerateEventStrings(std::unordered_map> c return eventList; } -vector GenerateEventList(struct PmuDeviceAttr& deviceAttr) +static vector GenerateEventList(struct PmuDeviceAttr& deviceAttr) { const auto& deviceConfig = GetDeviceMtricConfig(); const auto& config = deviceConfig.at(deviceAttr.metric); string bdf = ""; if (deviceAttr.bdf != nullptr) { - QueryPcieBdfRanges(); - QueryBdfToSmmuMapping(); + int err = QueryPcieBdfRanges(); + if (err != SUCCESS) { + return vector(); + } + err = QueryBdfToSmmuMapping(); + if (err != SUCCESS) { + return vector(); + } bdf = deviceAttr.bdf; } - QueryUncoreRawDevices(); + int err = QueryUncoreRawDevices(); + if (err != SUCCESS) { + return vector(); + } auto classifiedDevices = ClassifyDevicesByPrefix(config.devicePrefix, config.subDeviceName, config.splitPosition); return GenerateEventStrings(classifiedDevices, config.events, config.extraConfig, config.bdfParameter, bdf); } -int CheckDeviceMetricEnum(struct PmuDeviceAttr *attr, unsigned len) +static int CheckDeviceMetricEnum(struct PmuDeviceAttr *attr, unsigned len) { for (int i = 0; i < len; ++i) { - if (attr[i].metric > PMU_SMMU_TRAN) { - New(LIBPERF_ERR_INVALID_PMU_DEVICES_METRIC, "invalid value for PmuDeviceMetric!"); + const auto & metricConfig = GetDeviceMtricConfig(); + if (metricConfig.find(attr[i].metric) == metricConfig.end()) { + New(LIBPERF_ERR_INVALID_PMU_DEVICES_METRIC, "for this platform this metric is invalid value for PmuDeviceMetric!"); return LIBPERF_ERR_INVALID_PMU_DEVICES_METRIC; } } return SUCCESS; } -int CheckBdf(struct PmuDeviceAttr *attr, unsigned len) +static int CheckBdf(struct PmuDeviceAttr *attr, unsigned len) { for (int i = 0; i < len; ++i) { if (attr[i].metric >= PMU_PCIE_RX_MRD_BW && attr[i].bdf == nullptr) { @@ -512,7 +558,7 @@ int CheckBdf(struct PmuDeviceAttr *attr, unsigned len) return SUCCESS; } -int CheckPmuDeviceAttr(struct PmuDeviceAttr *attr, unsigned len) +static int CheckPmuDeviceAttr(struct PmuDeviceAttr *attr, unsigned len) { int err = CheckDeviceMetricEnum(attr, len); if (!err) { -- Gitee From 56ab74e943e3a0f091a2ccb78937529d63423b2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E9=BE=99?= Date: Tue, 1 Apr 2025 20:38:03 +0800 Subject: [PATCH 4/4] fix some bugs --- pmu/pmu_metric.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pmu/pmu_metric.cpp b/pmu/pmu_metric.cpp index 4f2dd40..f1caa71 100644 --- a/pmu/pmu_metric.cpp +++ b/pmu/pmu_metric.cpp @@ -463,7 +463,7 @@ namespace KUNPENG_PMU { static bool ValidateBdfValue(const string& bdf) { if (!ExistPath(SYS_BUS_PCI_DEVICES) || !IsDirectory(SYS_BUS_PCI_DEVICES)) { - New(LIBPERF_ERR_OPEN_PCI_FILE_FAILD, SYS_BUS_PCI_DEVICES " is not exist."); + New(LIBPERF_ERR_OPEN_PCI_FILE_FAILD, SYS_BUS_PCI_DEVICES + " is not exist."); return false; } unordered_set validBdfSet; @@ -474,7 +474,7 @@ namespace KUNPENG_PMU { } } if (validBdfSet.find(bdf) == validBdfSet.end()) { - New(LIBPERF_ERR_OPEN_PCI_FILE_FAILD, SYS_BUS_PCI_DEVICES " is not exist this bdf number."); + New(LIBPERF_ERR_OPEN_PCI_FILE_FAILD, SYS_BUS_PCI_DEVICES + " is not exist this bdf number."); return false; } return true; @@ -697,7 +697,7 @@ namespace KUNPENG_PMU { int PcieBWAggregate(const PmuDeviceMetric metric, const vector &rawData, vector &devData) { const auto& deviceConfig = GetDeviceMtricConfig(); - const auto& findConfig = deviceConfig.find(deviceAttr.metric); + const auto& findConfig = deviceConfig.find(metric); if (findConfig == deviceConfig.end()) { return SUCCESS; } @@ -788,8 +788,9 @@ namespace KUNPENG_PMU { static bool IsMetricEvent(const string &devName, const string &evtName, const PmuDeviceAttr &devAttr) { - auto findDevConfig = DEVICE_CONFIGS.find(devAttr.metric); - if (findDevConfig == DEVICE_CONFIGS.end()) { + const auto& deviceConfig = GetDeviceMtricConfig(); + auto findDevConfig = deviceConfig.find(devAttr.metric); + if (findDevConfig == deviceConfig.end()) { return false; } -- Gitee