CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CPU.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Services
4 // Class : CPU
5 //
6 // Implementation:
7 //
8 // Original Author: Natalia Garcia
9 // CPU.cc: v 1.0 2009/01/08 11:31:07
10 
11 
19 
20 #include <iostream>
21 #include <sys/time.h>
22 #include <sys/resource.h>
23 #include <stdio.h>
24 #include <string>
25 #include <fstream>
26 #include <sstream>
27 #include <set>
28 
29 namespace edm {
30  namespace service {
31  namespace {
32 
33  std::string i2str(int i){
34  std::ostringstream t;
35  t << i;
36  return t.str();
37  }
38 
39  std::string d2str(double d){
40  std::ostringstream t;
41  t << d;
42  return t.str();
43  }
44 
45  double str2d(std::string s){
46  return atof(s.c_str());
47  }
48 
49  inline int str2i(std::string s){
50  return atoi(s.c_str());
51  }
52 
53  void trim(std::string& s, const std::string& drop = " \t") {
54  std::string::size_type p = s.find_last_not_of(drop);
55  if(p != std::string::npos) {
56  s = s.erase(p+1);
57  }
58  s = s.erase(0, s.find_first_not_of(drop));
59  }
60 
61  std::string eraseExtraSpaces(std::string s) {
62  bool founded = false;
64  for(std::string::const_iterator iter = s.begin(); iter != s.end(); iter++){
65  if(founded){
66  if(*iter == ' ') founded = true;
67  else{
68  aux += " "; aux += *iter;
69  founded = false;
70  }
71  }
72  else{
73  if(*iter == ' ') founded = true;
74  else aux += *iter;
75  }
76  }
77  return aux;
78  }
79  } // namespace {}
80 
81 
82  CPU::CPU(const ParameterSet& iPS, ActivityRegistry&iRegistry):
83  totalNumberCPUs_(0),
84  averageCoreSpeed_(0.0),
85  reportCPUProperties_(iPS.getUntrackedParameter<bool>("reportCPUProperties"))
86  {
87  iRegistry.watchPostEndJob(this,&CPU::postEndJob);
88  }
89 
90 
92  {
93  }
94 
97  desc.addUntracked<bool>("reportCPUProperties", false);
98  descriptions.add("CPU", desc);
99  }
100 
101 
103  {
104  Service<JobReport> reportSvc;
105 
106  std::map<std::string, std::string> reportCPUProperties; // Summary
107  std::map<std::string, std::string> currentCoreProperties; // Module(s)
108 
109  std::ifstream fcpuinfo ("/proc/cpuinfo");
110 
111  if(fcpuinfo.is_open()){
112 
113  std::string buf;
114  std::string currentCore;
115  std::string CPUModels;
116 
117  std::set<std::string> models;
118 
119  while(!fcpuinfo.eof()){
120 
121  std::getline(fcpuinfo, buf);
122 
123  std::istringstream iss(buf);
124  std::string token;
125  std::string property;
127 
128  int time = 1;
129 
130  while(std::getline(iss, token, ':')) {
131  switch(time){
132  case 1:
133  property = token;
134  break;
135  case 2:
136  value = token;
137  break;
138  default:
139  value += token;
140  break;
141  }
142  time++;
143  }
144  trim(property);
145  trim(value);
146 
147  if(!property.empty()){
148  if(property == "processor") {
150  if(currentCore.empty()) { // first core
151  currentCore = value;
152  }
153  else{
154  reportSvc->reportPerformanceForModule("SystemCPU", "CPU-"+currentCore, currentCoreProperties);
155  currentCoreProperties.clear();
156  currentCore = value;
157  }
158  }
160  }
161  else {
163  currentCoreProperties.insert(std::make_pair(property, value));
164  }
165  if(property == "cpu MHz"){
166  averageCoreSpeed_ += str2d(value);
167  }
168  if(property == "model name"){
169  models.insert(eraseExtraSpaces(value));
170  }
171  }
172  }
173  } //while
174 
175  fcpuinfo.close();
176 
177  if(!currentCore.empty() && reportCPUProperties_) {
178  reportSvc->reportPerformanceForModule("SystemCPU", "CPU-"+currentCore, currentCoreProperties);
179  }
180 
181  reportCPUProperties.insert(std::make_pair("totalCPUs", i2str(totalNumberCPUs_)));
182 
183  if(totalNumberCPUs_ == 0){
184  averageCoreSpeed_ = 0.0;
185  }
186  else{
188  }
189 
190  reportCPUProperties.insert(std::make_pair("averageCoreSpeed", d2str(averageCoreSpeed_)));
191 
192  int model = 0;
193  for(std::set<std::string>::const_iterator iter = models.begin(); iter != models.end(); iter++){
194  if(model == 0)
195  CPUModels += *iter;
196  else
197  CPUModels += ", " + *iter;
198  model++;
199  }
200  reportCPUProperties.insert(std::make_pair("CPUModels", CPUModels));
201 
202 
203  reportSvc->reportPerformanceSummary("SystemCPU", reportCPUProperties);
204 
205  } //if
206  } //postEndJob
207  } //service
208 } //edm
209 
210 
static std::string i2str(int i)
Definition: Memory.cc:68
int i
Definition: DBlmapReader.cc:9
double averageCoreSpeed_
Definition: CPU.h:30
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void postEndJob()
Definition: CPU.cc:102
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
bool reportCPUProperties_
Definition: CPU.h:31
uint16_t size_type
tuple d
Definition: ztail.py:151
int totalNumberCPUs_
Definition: CPU.h:29
const int drop
CPU(ParameterSet const &, ActivityRegistry &)
Definition: CPU.cc:82
static std::string d2str(double d)
Definition: Memory.cc:62
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
Definition: CPU.cc:95