test
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 
13 
20 
21 #include <iostream>
22 #include <sys/time.h>
23 #include <sys/resource.h>
24 #include <stdio.h>
25 #include <string>
26 #include <fstream>
27 #include <sstream>
28 #include <set>
29 
30 namespace edm {
31 
32  namespace service {
33  class CPU {
34  public:
36  ~CPU();
37 
38  static void fillDescriptions(ConfigurationDescriptions& descriptions);
39 
40  private:
44 
45  void postEndJob();
46  };
47 
48  inline
49  bool isProcessWideService(CPU const*) {
50  return true;
51  }
52  }
53 }
54 
55 namespace edm {
56  namespace service {
57  namespace {
58 
59  std::string i2str(int i){
60  std::ostringstream t;
61  t << i;
62  return t.str();
63  }
64 
65  std::string d2str(double d){
66  std::ostringstream t;
67  t << d;
68  return t.str();
69  }
70 
71  double str2d(std::string s){
72  return atof(s.c_str());
73  }
74 
75  inline int str2i(std::string s){
76  return atoi(s.c_str());
77  }
78 
79  void trim(std::string& s, const std::string& drop = " \t") {
80  std::string::size_type p = s.find_last_not_of(drop);
81  if(p != std::string::npos) {
82  s = s.erase(p+1);
83  }
84  s = s.erase(0, s.find_first_not_of(drop));
85  }
86 
87  std::string eraseExtraSpaces(std::string s) {
88  bool founded = false;
90  for(std::string::const_iterator iter = s.begin(); iter != s.end(); iter++){
91  if(founded){
92  if(*iter == ' ') founded = true;
93  else{
94  aux += " "; aux += *iter;
95  founded = false;
96  }
97  }
98  else{
99  if(*iter == ' ') founded = true;
100  else aux += *iter;
101  }
102  }
103  return aux;
104  }
105  } // namespace {}
106 
107 
108  CPU::CPU(const ParameterSet& iPS, ActivityRegistry&iRegistry):
109  totalNumberCPUs_(0),
110  averageCoreSpeed_(0.0),
111  reportCPUProperties_(iPS.getUntrackedParameter<bool>("reportCPUProperties"))
112  {
113  iRegistry.watchPostEndJob(this,&CPU::postEndJob);
114  }
115 
116 
118  {
119  }
120 
123  desc.addUntracked<bool>("reportCPUProperties", false);
124  descriptions.add("CPU", desc);
125  }
126 
127 
129  {
130  Service<JobReport> reportSvc;
131 
132  std::map<std::string, std::string> reportCPUProperties; // Summary
133  std::map<std::string, std::string> currentCoreProperties; // Module(s)
134 
135  std::ifstream fcpuinfo ("/proc/cpuinfo");
136 
137  if(fcpuinfo.is_open()){
138 
139  std::string buf;
140  std::string currentCore;
141  std::string CPUModels;
142 
143  std::set<std::string> models;
144 
145  while(!fcpuinfo.eof()){
146 
147  std::getline(fcpuinfo, buf);
148 
149  std::istringstream iss(buf);
151  std::string property;
153 
154  int time = 1;
155 
156  while(std::getline(iss, token, ':')) {
157  switch(time){
158  case 1:
159  property = token;
160  break;
161  case 2:
162  value = token;
163  break;
164  default:
165  value += token;
166  break;
167  }
168  time++;
169  }
170  trim(property);
171  trim(value);
172 
173  if(!property.empty()){
174  if(property == "processor") {
176  if(currentCore.empty()) { // first core
177  currentCore = value;
178  }
179  else{
180  reportSvc->reportPerformanceForModule("SystemCPU", "CPU-"+currentCore, currentCoreProperties);
181  currentCoreProperties.clear();
182  currentCore = value;
183  }
184  }
186  }
187  else {
189  currentCoreProperties.insert(std::make_pair(property, value));
190  }
191  if(property == "cpu MHz"){
192  averageCoreSpeed_ += str2d(value);
193  }
194  if(property == "model name"){
195  models.insert(eraseExtraSpaces(value));
196  }
197  }
198  }
199  } //while
200 
201  fcpuinfo.close();
202 
203  if(!currentCore.empty() && reportCPUProperties_) {
204  reportSvc->reportPerformanceForModule("SystemCPU", "CPU-"+currentCore, currentCoreProperties);
205  }
206 
207  reportCPUProperties.insert(std::make_pair("totalCPUs", i2str(totalNumberCPUs_)));
208 
209  if(totalNumberCPUs_ == 0){
210  averageCoreSpeed_ = 0.0;
211  }
212  else{
214  }
215 
216  reportCPUProperties.insert(std::make_pair("averageCoreSpeed", d2str(averageCoreSpeed_)));
217 
218  int model = 0;
219  for(std::set<std::string>::const_iterator iter = models.begin(); iter != models.end(); iter++){
220  if(model == 0)
221  CPUModels += *iter;
222  else
223  CPUModels += ", " + *iter;
224  model++;
225  }
226  reportCPUProperties.insert(std::make_pair("CPUModels", CPUModels));
227 
228 
229  reportSvc->reportPerformanceSummary("SystemCPU", reportCPUProperties);
230 
231  } //if
232  } //postEndJob
233  } //service
234 } //edm
235 
236 
237 using edm::service::CPU;
239 
240 
static std::string i2str(int i)
int i
Definition: DBlmapReader.cc:9
double averageCoreSpeed_
Definition: CPU.cc:42
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void postEndJob()
Definition: CPU.cc:128
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
bool reportCPUProperties_
Definition: CPU.cc:43
bool isProcessWideService(TFileService const *)
Definition: TFileService.h:99
uint16_t size_type
tuple d
Definition: ztail.py:151
int totalNumberCPUs_
Definition: CPU.cc:41
const int drop
#define DEFINE_FWK_SERVICE(type)
Definition: ServiceMaker.h:113
CPU(ParameterSet const &, ActivityRegistry &)
Definition: CPU.cc:108
static std::string d2str(double d)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
Definition: CPU.cc:121