21 #include "cpu_features/cpu_features_macros.h" 23 #if defined(CPU_FEATURES_ARCH_X86) 24 #include "cpu_features/cpuinfo_x86.h" 25 #elif defined(CPU_FEATURES_ARCH_ARM) 26 #include "cpu_features/cpuinfo_arm.h" 27 #elif defined(CPU_FEATURES_ARCH_AARCH64) 28 #include "cpu_features/cpuinfo_aarch64.h" 29 #elif defined(CPU_FEATURES_ARCH_PPC) 30 #include "cpu_features/cpuinfo_ppc.h" 31 #elif defined(CPU_FEATURES_ARCH_RISCV) 32 #include "cpu_features/cpuinfo_riscv.h" 43 #include <fmt/format.h> 51 using CPUInfoType = std::vector<std::pair<std::string, std::string>>;
57 ~CPU()
override =
default;
83 if (
p != std::string::npos) {
86 s =
s.erase(0,
s.find_first_not_of(
drop));
91 std::unique(
s.begin(),
s.end(), [](
const auto a,
const auto b) {
return std::isspace(
a) &&
a ==
b; });
97 bool getCpuSetSize(
unsigned &set_size) {
100 unsigned current_size = 128;
101 unsigned cpu_count = 0;
102 while (current_size * 2 > current_size) {
103 cpusetp = CPU_ALLOC(current_size);
104 CPU_ZERO_S(CPU_ALLOC_SIZE(current_size), cpusetp);
106 if (sched_getaffinity(0, CPU_ALLOC_SIZE(current_size), cpusetp)) {
108 if (errno == EINVAL) {
114 cpu_count = CPU_COUNT_S(CPU_ALLOC_SIZE(current_size), cpusetp);
118 set_size = cpu_count;
127 : reportCPUProperties_(iPS.getUntrackedParameter<
bool>(
"reportCPUProperties")),
128 disableJobReportOutput_(iPS.getUntrackedParameter<
bool>(
"disableJobReportOutput")) {
144 desc.addUntracked<
bool>(
"reportCPUProperties",
false);
145 desc.addUntracked<
bool>(
"disableJobReportOutput",
false);
162 unsigned totalNumberCPUs = 0;
163 std::map<std::string, std::string> currentCoreProperties;
167 if (
entry.first ==
"processor") {
169 if (currentCore.empty()) {
170 currentCore =
entry.second;
172 reportSvc->reportPerformanceForModule(
"SystemCPU",
"CPU-" + currentCore, currentCoreProperties);
173 currentCoreProperties.clear();
174 currentCore =
entry.second;
179 currentCoreProperties.insert(
entry);
183 reportSvc->reportPerformanceForModule(
"SystemCPU",
"CPU-" + currentCore, currentCoreProperties);
186 std::map<std::string, std::string> reportCPUProperties{
190 unsigned set_size = -1;
191 if (getCpuSetSize(set_size)) {
192 reportCPUProperties.insert(std::make_pair(
"cpusetCount",
std::to_string(set_size)));
194 reportSvc->reportPerformanceSummary(
"SystemCPU", reportCPUProperties);
199 std::ifstream fcpuinfo(
"/proc/cpuinfo");
200 if (!fcpuinfo.is_open()) {
203 while (!fcpuinfo.eof()) {
205 std::getline(fcpuinfo,
buf);
207 std::istringstream iss(
buf);
214 while (std::getline(iss,
token,
':')) {
230 if (property.empty()) {
234 if (property ==
"model name") {
235 compressWhitespace(
value);
243 using namespace cpu_features;
246 #if defined(CPU_FEATURES_ARCH_X86) 247 const auto info{GetX86Info()};
249 #elif defined(CPU_FEATURES_ARCH_ARM) 250 const auto info{GetArmInfo()};
252 #elif defined(CPU_FEATURES_ARCH_AARCH64) 253 const auto info{GetAarch64Info()};
255 #elif defined(CPU_FEATURES_ARCH_PPC) 256 const auto strings{GetPPCPlatformStrings()};
258 #elif defined(CPU_FEATURES_ARCH_RISCV) 259 const auto info{GetRiscvInfo()};
266 std::set<std::string> modelSet;
268 if (
entry.first ==
"model name") {
269 modelSet.insert(
entry.second);
272 std::vector<std::string> modelsVector(modelSet.begin(), modelSet.end());
275 if (modelsVector.empty()) {
282 std::stringstream
ss;
284 for (
const auto &modelname :
models) {
294 double averageCoreSpeed = 0.0;
295 unsigned coreCount = 0;
297 if (
entry.first ==
"cpu MHz") {
299 averageCoreSpeed += std::stod(
entry.second);
300 }
catch (
const std::logic_error &
e) {
301 LogWarning(
"CPU::getAverageSpeed") <<
"stod(" <<
entry.second <<
") conversion error: " <<
e.what();
309 return averageCoreSpeed /
static_cast<double>(coreCount);
std::vector< std::pair< std::string, std::string > > CPUInfoType
#define DEFINE_FWK_SERVICE_MAKER(concrete, maker)
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
bool isProcessWideService(TFileService const *)
static void trim(std::string &s)
std::string formatModels(const std::vector< std::string > &models) const
static std::string to_string(const XMLCh *ch)
std::vector< std::string > getModels(const CPUInfoType &info) const
def unique(seq, keepstr=True)
CPU(ParameterSet const &, ActivityRegistry &)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
const bool reportCPUProperties_
bool parseCPUInfo(CPUInfoType &info) const
Log< level::Warning, false > LogWarning
const bool disableJobReportOutput_
double getAverageSpeed(const CPUInfoType &info) const
std::string getModelFromCPUFeatures() const