CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
edm::service::CPU Class Reference
Inheritance diagram for edm::service::CPU:
edm::CPUServiceBase

Public Member Functions

 CPU (ParameterSet const &, ActivityRegistry &)
 
 ~CPU () override=default
 
- Public Member Functions inherited from edm::CPUServiceBase
 CPUServiceBase ()
 
 CPUServiceBase (const CPUServiceBase &)=delete
 
const CPUServiceBaseoperator= (const CPUServiceBase &)=delete
 
virtual ~CPUServiceBase ()
 

Static Public Member Functions

static void fillDescriptions (ConfigurationDescriptions &descriptions)
 

Private Member Functions

std::string formatModels (const std::vector< std::string > &models) const
 
double getAverageSpeed (const CPUInfoType &info) const
 
std::string getModelFromCPUFeatures () const
 
std::vector< std::string > getModels (const CPUInfoType &info) const
 
bool parseCPUInfo (CPUInfoType &info) const
 
void postEndJob ()
 

Private Attributes

const bool disableJobReportOutput_
 
const bool reportCPUProperties_
 

Detailed Description

Definition at line 54 of file CPU.cc.

Constructor & Destructor Documentation

◆ CPU()

edm::service::CPU::CPU ( ParameterSet const &  iPS,
ActivityRegistry iRegistry 
)

Definition at line 126 of file CPU.cc.

References formatModels(), getAverageSpeed(), getModels(), info(), edm::Service< T >::isAvailable(), parseCPUInfo(), postEndJob(), edm::ResourceInformation::setCpuAverageSpeed(), edm::ResourceInformation::setCPUModels(), edm::ResourceInformation::setCpuModelsFormatted(), and edm::ActivityRegistry::watchPostEndJob().

127  : reportCPUProperties_(iPS.getUntrackedParameter<bool>("reportCPUProperties")),
128  disableJobReportOutput_(iPS.getUntrackedParameter<bool>("disableJobReportOutput")) {
129  edm::Service<edm::ResourceInformation> resourceInformationService;
130  if (resourceInformationService.isAvailable()) {
132  if (parseCPUInfo(info)) {
133  const auto models{getModels(info)};
134  resourceInformationService->setCPUModels(models);
135  resourceInformationService->setCpuModelsFormatted(formatModels(models));
136  resourceInformationService->setCpuAverageSpeed(getAverageSpeed(info));
137  }
138  }
139  iRegistry.watchPostEndJob(this, &CPU::postEndJob);
140  }
std::vector< std::pair< std::string, std::string > > CPUInfoType
Definition: CPU.cc:51
static const TGPicture * info(bool iBackgroundIsBlack)
void postEndJob()
Definition: CPU.cc:149
Definition: models.py:1
std::string formatModels(const std::vector< std::string > &models) const
Definition: CPU.cc:281
virtual void setCPUModels(std::vector< std::string > const &)=0
std::vector< std::string > getModels(const CPUInfoType &info) const
Definition: CPU.cc:265
virtual void setCpuModelsFormatted(std::string const &)=0
virtual void setCpuAverageSpeed(double)=0
const bool reportCPUProperties_
Definition: CPU.cc:62
bool isAvailable() const
Definition: Service.h:40
bool parseCPUInfo(CPUInfoType &info) const
Definition: CPU.cc:197
const bool disableJobReportOutput_
Definition: CPU.cc:63
double getAverageSpeed(const CPUInfoType &info) const
Definition: CPU.cc:293

◆ ~CPU()

edm::service::CPU::~CPU ( )
overridedefault

Member Function Documentation

◆ fillDescriptions()

void edm::service::CPU::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 142 of file CPU.cc.

References edm::ConfigurationDescriptions::add(), and submitPVResolutionJobs::desc.

142  {
144  desc.addUntracked<bool>("reportCPUProperties", false);
145  desc.addUntracked<bool>("disableJobReportOutput", false);
146  descriptions.add("CPU", desc);
147  }
void add(std::string const &label, ParameterSetDescription const &psetDescription)

◆ formatModels()

std::string edm::service::CPU::formatModels ( const std::vector< std::string > &  models) const
private

Definition at line 281 of file CPU.cc.

References ReggeGribovPartonMC_EposLHC_2760GeV_PbPb_cfi::model, and contentValuesCheck::ss.

Referenced by CPU(), and postEndJob().

281  {
282  std::stringstream ss;
283  int model = 0;
284  for (const auto &modelname : models) {
285  if (model++ != 0) {
286  ss << ", ";
287  }
288  ss << modelname;
289  }
290  return ss.str();
291  }
Definition: models.py:1

◆ getAverageSpeed()

double edm::service::CPU::getAverageSpeed ( const CPUInfoType info) const
private

Definition at line 293 of file CPU.cc.

References MillePedeFileConverter_cfg::e, mps_splice::entry, and info().

Referenced by CPU(), and postEndJob().

293  {
294  double averageCoreSpeed = 0.0;
295  unsigned coreCount = 0;
296  for (const auto &entry : info) {
297  if (entry.first == "cpu MHz") {
298  try {
299  averageCoreSpeed += std::stod(entry.second);
300  } catch (const std::logic_error &e) {
301  LogWarning("CPU::getAverageSpeed") << "stod(" << entry.second << ") conversion error: " << e.what();
302  }
303  coreCount++;
304  }
305  }
306  if (!coreCount) {
307  return 0;
308  }
309  return averageCoreSpeed / static_cast<double>(coreCount);
310  }
static const TGPicture * info(bool iBackgroundIsBlack)
Log< level::Warning, false > LogWarning

◆ getModelFromCPUFeatures()

std::string edm::service::CPU::getModelFromCPUFeatures ( ) const
private

Definition at line 242 of file CPU.cc.

References dqm-mbProfile::format, info(), ReggeGribovPartonMC_EposLHC_2760GeV_PbPb_cfi::model, AlCaHLTBitMon_QueryRunRegistry::string, and makeListRunsInFiles::strings.

Referenced by getModels().

242  {
243  using namespace cpu_features;
244 
246 #if defined(CPU_FEATURES_ARCH_X86)
247  const auto info{GetX86Info()};
248  model = info.brand_string;
249 #elif defined(CPU_FEATURES_ARCH_ARM)
250  const auto info{GetArmInfo()};
251  model = fmt::format("ARM {} {} {}", info.implementer, info.architecture, info.variant);
252 #elif defined(CPU_FEATURES_ARCH_AARCH64)
253  const auto info{GetAarch64Info()};
254  model = fmt::format("aarch64 {} {}", info.implementer, info.variant);
255 #elif defined(CPU_FEATURES_ARCH_PPC)
256  const auto strings{GetPPCPlatformStrings()};
257  model = strings.machine;
258 #elif defined(CPU_FEATURES_ARCH_RISCV)
259  const auto info{GetRiscvInfo()};
260  model = fmt::format("riscv64 {} {}", info.vendor, info.uarch);
261 #endif
262  return model;
263  }
static const TGPicture * info(bool iBackgroundIsBlack)

◆ getModels()

std::vector< std::string > edm::service::CPU::getModels ( const CPUInfoType info) const
private

Definition at line 265 of file CPU.cc.

References mps_splice::entry, getModelFromCPUFeatures(), and info().

Referenced by CPU(), and postEndJob().

265  {
266  std::set<std::string> modelSet;
267  for (const auto &entry : info) {
268  if (entry.first == "model name") {
269  modelSet.insert(entry.second);
270  }
271  }
272  std::vector<std::string> modelsVector(modelSet.begin(), modelSet.end());
273  // If "model name" isn't present in /proc/cpuinfo, see what we can get
274  // from cpu_features
275  if (modelsVector.empty()) {
276  modelsVector.emplace_back(getModelFromCPUFeatures());
277  }
278  return modelsVector;
279  }
static const TGPicture * info(bool iBackgroundIsBlack)
std::string getModelFromCPUFeatures() const
Definition: CPU.cc:242

◆ parseCPUInfo()

bool edm::service::CPU::parseCPUInfo ( CPUInfoType info) const
private

Definition at line 197 of file CPU.cc.

References visDQMUpload::buf, info(), AlCaHLTBitMon_QueryRunRegistry::string, hcalRecHitTable_cff::time, unpackBuffers-CaloStage2::token, trim(), and relativeConstraints::value.

Referenced by CPU(), and postEndJob().

197  {
198  info.clear();
199  std::ifstream fcpuinfo("/proc/cpuinfo");
200  if (!fcpuinfo.is_open()) {
201  return false;
202  }
203  while (!fcpuinfo.eof()) {
205  std::getline(fcpuinfo, buf);
206 
207  std::istringstream iss(buf);
209  std::string property;
211 
212  int time = 1;
213 
214  while (std::getline(iss, token, ':')) {
215  switch (time) {
216  case 1:
217  property = token;
218  break;
219  case 2:
220  value = token;
221  break;
222  default:
223  value += token;
224  break;
225  }
226  time++;
227  }
228  trim(property);
229  trim(value);
230  if (property.empty()) {
231  continue;
232  }
233 
234  if (property == "model name") {
235  compressWhitespace(value);
236  }
237  info.emplace_back(property, value);
238  }
239  return true;
240  }
static const TGPicture * info(bool iBackgroundIsBlack)
static void trim(std::string &s)
Definition: value.py:1

◆ postEndJob()

void edm::service::CPU::postEndJob ( )
private

Definition at line 149 of file CPU.cc.

References disableJobReportOutput_, mps_splice::entry, formatModels(), getAverageSpeed(), getModels(), info(), parseCPUInfo(), reportCPUProperties_, AlCaHLTBitMon_QueryRunRegistry::string, and to_string().

Referenced by CPU().

149  {
151  return;
152  }
153 
154  Service<JobReport> reportSvc;
155 
157  if (!parseCPUInfo(info)) {
158  return;
159  }
160 
161  const auto models{formatModels(getModels(info))};
162  unsigned totalNumberCPUs = 0;
163  std::map<std::string, std::string> currentCoreProperties;
164  std::string currentCore;
165 
166  for (const auto &entry : info) {
167  if (entry.first == "processor") {
168  if (reportCPUProperties_) {
169  if (currentCore.empty()) { // first core
170  currentCore = entry.second;
171  } else {
172  reportSvc->reportPerformanceForModule("SystemCPU", "CPU-" + currentCore, currentCoreProperties);
173  currentCoreProperties.clear();
174  currentCore = entry.second;
175  }
176  }
177  totalNumberCPUs++;
178  } else if (reportCPUProperties_) {
179  currentCoreProperties.insert(entry);
180  }
181  }
182  if (!currentCore.empty() && reportCPUProperties_) {
183  reportSvc->reportPerformanceForModule("SystemCPU", "CPU-" + currentCore, currentCoreProperties);
184  }
185 
186  std::map<std::string, std::string> reportCPUProperties{
187  {"totalCPUs", std::to_string(totalNumberCPUs)},
188  {"averageCoreSpeed", std::to_string(getAverageSpeed(info))},
189  {"CPUModels", models}};
190  unsigned set_size = -1;
191  if (getCpuSetSize(set_size)) {
192  reportCPUProperties.insert(std::make_pair("cpusetCount", std::to_string(set_size)));
193  }
194  reportSvc->reportPerformanceSummary("SystemCPU", reportCPUProperties);
195  }
std::vector< std::pair< std::string, std::string > > CPUInfoType
Definition: CPU.cc:51
static const TGPicture * info(bool iBackgroundIsBlack)
Definition: models.py:1
std::string formatModels(const std::vector< std::string > &models) const
Definition: CPU.cc:281
static std::string to_string(const XMLCh *ch)
std::vector< std::string > getModels(const CPUInfoType &info) const
Definition: CPU.cc:265
const bool reportCPUProperties_
Definition: CPU.cc:62
bool parseCPUInfo(CPUInfoType &info) const
Definition: CPU.cc:197
const bool disableJobReportOutput_
Definition: CPU.cc:63
double getAverageSpeed(const CPUInfoType &info) const
Definition: CPU.cc:293

Member Data Documentation

◆ disableJobReportOutput_

const bool edm::service::CPU::disableJobReportOutput_
private

Definition at line 63 of file CPU.cc.

Referenced by postEndJob().

◆ reportCPUProperties_

const bool edm::service::CPU::reportCPUProperties_
private

Definition at line 62 of file CPU.cc.

Referenced by postEndJob().