diff --git a/pmu/pmu.cpp b/pmu/pmu.cpp index a1acc91a3b4321e6d22d67656cf2f39cd3f07919..dc956a099e8a97ecd67f8255a84327ad3b88f17b 100644 --- a/pmu/pmu.cpp +++ b/pmu/pmu.cpp @@ -33,11 +33,10 @@ using namespace pcerr; using namespace KUNPENG_PMU; using namespace std; -#define MAX_CPU_NUM sysconf(_SC_NPROCESSORS_ONLN) - static unordered_map runningStatus; static SafeHandler pdMutex; static pair uncoreEventPair; +static std::set onLineCpuIds; struct PmuTaskAttr* AssignPmuTaskParam(PmuTaskType collectType, struct PmuAttr *attr); @@ -63,6 +62,7 @@ static int PmuCollectPause(const int pd) static int CheckCpuList(unsigned numCpu, int* cpuList) { + const set& onLineCpus = GetOnLineCpuIds(); if (numCpu > MAX_CPU_NUM) { string errMsg = "Invalid numCpu: " + to_string(numCpu); New(LIBPERF_ERR_INVALID_CPULIST, errMsg); @@ -78,6 +78,11 @@ static int CheckCpuList(unsigned numCpu, int* cpuList) New(LIBPERF_ERR_INVALID_CPULIST, errMsg); return LIBPERF_ERR_INVALID_CPULIST; } + if (!onLineCpus.count(cpuList[i])) { + string errMsg = "OffLine cpu id: " + to_string(cpuList[i]); + New(LIBPERF_ERR_INVALID_CPULIST, errMsg); + return LIBPERF_ERR_INVALID_CPULIST; + } } return SUCCESS; } @@ -746,10 +751,14 @@ static void PrepareCpuList(PmuAttr *attr, PmuTaskAttr *taskParam, PmuEvt* pmuEvt taskParam->cpuList[0] = -1; } else if (attr->cpuList == nullptr) { // For null cpulist, open fd with cpu 0,1,2...max_cpu - taskParam->numCpu = MAX_CPU_NUM; - taskParam->cpuList = new int[taskParam->numCpu]; - for (int i = 0; i < taskParam->numCpu; i++) { - taskParam->cpuList[i] = i; + const set &onLineCpus = GetOnLineCpuIds(); + int cpuNum = onLineCpus.size(); + taskParam->numCpu = cpuNum; + taskParam->cpuList = new int[cpuNum]; + int i = 0; + for (const auto &cpuId : onLineCpus) { + taskParam->cpuList[i] = cpuId; + i++; } } else { taskParam->numCpu = attr->numCpu; diff --git a/util/common.h b/util/common.h index a3da352e23fb5ecc761b6aa234c13b30616f4e4d..5e5d0aaa39be1f5d71915492f2132a9f0149604f 100644 --- a/util/common.h +++ b/util/common.h @@ -28,5 +28,4 @@ int RaiseNumFd(uint64_t numFd); bool ExistPath(const std::string& filePath); std::string GetTraceEventDir(); - #endif // LIBKPROF_COMMON_H diff --git a/util/cpu_map.cpp b/util/cpu_map.cpp index ec8f077d2c42cfb1fd1d39e3f0ebb2aa15323dd9..c8e0c9ea394b02a3bfd12e5e5899d471573acd47 100644 --- a/util/cpu_map.cpp +++ b/util/cpu_map.cpp @@ -27,7 +27,10 @@ using namespace std; static const std::string CPU_TOPOLOGY_PACKAGE_ID = "/sys/bus/cpu/devices/cpu%d/topology/physical_package_id"; static const std::string MIDR_EL1 = "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1"; +static const std::string CPU_ONLINE_PATH = "/sys/devices/system/cpu/online"; + static constexpr int PATH_LEN = 256; +static constexpr int LINE_LEN = 1024; static CHIP_TYPE g_chipType = CHIP_TYPE::UNDEFINED_TYPE; static map chipMap = {{"0x00000000481fd010", HIPA}, @@ -36,6 +39,8 @@ static map chipMap = {{"0x00000000481fd010", HIPA}, {"0x00000000480fd220", HIPF}, {"0x00000000480fd450", HIPE},}; +static std::set onLineCpuIds; + static inline bool ReadCpuPackageId(int coreId, CpuTopology* cpuTopo) { char filename[PATH_LEN]; @@ -99,3 +104,40 @@ CHIP_TYPE GetCpuType() } return g_chipType; } + +set GetOnLineCpuIds() +{ + if (!onLineCpuIds.empty()) { + return onLineCpuIds; + } + ifstream onLineFile(CPU_ONLINE_PATH); + if (!onLineFile.is_open()) { + for (int i = 0; i < MAX_CPU_NUM; i++) + { + onLineCpuIds.emplace(i); + } + return onLineCpuIds; + } + char line[LINE_LEN]; + onLineFile >> line; + onLineFile.close(); + char *tokStr = strtok(line, ","); + while (tokStr != nullptr) { + if (strstr(tokStr, "-") != nullptr) { + int minCpu, maxCpu; + if (sscanf(tokStr, "%d-%d", &minCpu, &maxCpu) != 2) { + continue; + } + for (int i = minCpu; i <= maxCpu; i++) { + onLineCpuIds.emplace(i); + } + } else { + int aloneNumber; + if (sscanf(tokStr, "%d", &aloneNumber) == 1) { + onLineCpuIds.emplace(aloneNumber); + } + } + tokStr = strtok(nullptr, ","); + } + return onLineCpuIds; +} \ No newline at end of file diff --git a/util/cpu_map.h b/util/cpu_map.h index 58c61b858bb1374e1929fe5481d48c9f55f8c2f2..6279954ab53b6490920f76d241855152d9fdf490 100644 --- a/util/cpu_map.h +++ b/util/cpu_map.h @@ -15,10 +15,10 @@ #ifndef CPU_MAP_H #define CPU_MAP_H #include +#include #include "pmu.h" -#ifdef __cplusplus -extern "C" { -#endif + +#define MAX_CPU_NUM sysconf(_SC_NPROCESSORS_CONF) enum CHIP_TYPE { UNDEFINED_TYPE = 0, @@ -31,7 +31,5 @@ enum CHIP_TYPE { struct CpuTopology* GetCpuTopology(int coreId); CHIP_TYPE GetCpuType(); -#ifdef __cplusplus -} -#endif -#endif +std::set GetOnLineCpuIds(); +#endif