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" 41 #include <fmt/format.h> 49 using CPUInfoType = std::vector<std::pair<std::string, std::string>>;
55 ~CPU()
override =
default;
81 if (
p != std::string::npos) {
84 s =
s.erase(0,
s.find_first_not_of(
drop));
89 std::unique(
s.begin(),
s.end(), [](
const auto a,
const auto b) {
return std::isspace(
a) &&
a ==
b; });
95 bool getCpuSetSize(
unsigned &set_size) {
98 unsigned current_size = 128;
99 unsigned cpu_count = 0;
100 while (current_size * 2 > current_size) {
101 cpusetp = CPU_ALLOC(current_size);
102 CPU_ZERO_S(CPU_ALLOC_SIZE(current_size), cpusetp);
104 if (sched_getaffinity(0, CPU_ALLOC_SIZE(current_size), cpusetp)) {
106 if (errno == EINVAL) {
112 cpu_count = CPU_COUNT_S(CPU_ALLOC_SIZE(current_size), cpusetp);
116 set_size = cpu_count;
125 : reportCPUProperties_(iPS.getUntrackedParameter<
bool>(
"reportCPUProperties")),
126 disableJobReportOutput_(iPS.getUntrackedParameter<
bool>(
"disableJobReportOutput")) {
142 desc.addUntracked<
bool>(
"reportCPUProperties",
false);
143 desc.addUntracked<
bool>(
"disableJobReportOutput",
false);
160 unsigned totalNumberCPUs = 0;
161 std::map<std::string, std::string> currentCoreProperties;
165 if (
entry.first ==
"processor") {
167 if (currentCore.empty()) {
168 currentCore =
entry.second;
170 reportSvc->reportPerformanceForModule(
"SystemCPU",
"CPU-" + currentCore, currentCoreProperties);
171 currentCoreProperties.clear();
172 currentCore =
entry.second;
177 currentCoreProperties.insert(
entry);
181 reportSvc->reportPerformanceForModule(
"SystemCPU",
"CPU-" + currentCore, currentCoreProperties);
184 std::map<std::string, std::string> reportCPUProperties{
188 unsigned set_size = -1;
189 if (getCpuSetSize(set_size)) {
190 reportCPUProperties.insert(std::make_pair(
"cpusetCount",
std::to_string(set_size)));
192 reportSvc->reportPerformanceSummary(
"SystemCPU", reportCPUProperties);
197 std::ifstream fcpuinfo(
"/proc/cpuinfo");
198 if (!fcpuinfo.is_open()) {
201 while (!fcpuinfo.eof()) {
203 std::getline(fcpuinfo,
buf);
205 std::istringstream iss(
buf);
212 while (std::getline(iss,
token,
':')) {
228 if (property.empty()) {
232 if (property ==
"model name") {
233 compressWhitespace(
value);
241 using namespace cpu_features;
244 #if defined(CPU_FEATURES_ARCH_X86) 245 const auto info{GetX86Info()};
247 #elif defined(CPU_FEATURES_ARCH_ARM) 248 const auto info{GetArmInfo()};
250 #elif defined(CPU_FEATURES_ARCH_AARCH64) 251 const auto info{GetAarch64Info()};
253 #elif defined(CPU_FEATURES_ARCH_PPC) 254 const auto strings{GetPPCPlatformStrings()};
261 std::set<std::string> modelSet;
263 if (
entry.first ==
"model name") {
264 modelSet.insert(
entry.second);
267 std::vector<std::string> modelsVector(modelSet.begin(), modelSet.end());
270 if (modelsVector.empty()) {
277 std::stringstream
ss;
279 for (
const auto &modelname :
models) {
289 double averageCoreSpeed = 0.0;
290 unsigned coreCount = 0;
292 if (
entry.first ==
"cpu MHz") {
294 averageCoreSpeed += std::stod(
entry.second);
295 }
catch (
const std::logic_error &
e) {
296 LogWarning(
"CPU::getAverageSpeed") <<
"stod(" <<
entry.second <<
") conversion error: " <<
e.what();
304 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