CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/FWCore/Services/src/CPU.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Services
00004 // Class  :     CPU
00005 // 
00006 // Implementation:
00007 //
00008 // Original Author:  Natalia Garcia
00009 // CPU.cc: v 1.0 2009/01/08 11:31:07
00010 
00011 
00012 #include "FWCore/Services/src/CPU.h"
00013 #include "FWCore/MessageLogger/interface/JobReport.h"
00014 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00015 #include "FWCore/ServiceRegistry/interface/Service.h"
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
00018 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00019 
00020 #include <iostream>
00021 #include <sys/time.h>
00022 #include <sys/resource.h>
00023 #include <stdio.h>
00024 #include <string>
00025 #include <fstream>
00026 #include <sstream>
00027 #include <set>
00028 
00029 namespace edm {
00030   namespace service {
00031     namespace {
00032 
00033       std::string i2str(int i){
00034         std::ostringstream t;
00035         t << i;
00036         return t.str();
00037       }
00038 
00039       std::string d2str(double d){
00040         std::ostringstream t;
00041         t << d;
00042         return t.str();
00043       }
00044 
00045       double str2d(std::string s){
00046         return atof(s.c_str());
00047       }
00048 
00049       int str2i(std::string s){
00050         return atoi(s.c_str());
00051       }
00052 
00053       void trim(std::string& s, const std::string& drop = " \t") {
00054         std::string::size_type p = s.find_last_not_of(drop);
00055         if(p != std::string::npos) {
00056           s = s.erase(p+1);
00057         }
00058         s = s.erase(0, s.find_first_not_of(drop));
00059       }
00060 
00061       std::string eraseExtraSpaces(std::string s) {
00062         bool founded = false; 
00063         std::string aux;
00064         for(std::string::const_iterator iter = s.begin(); iter != s.end(); iter++){
00065                 if(founded){
00066                         if(*iter == ' ') founded = true;
00067                         else{
00068                                 aux += " "; aux += *iter;
00069                                 founded = false;
00070                         }
00071                 }
00072                 else{
00073                         if(*iter == ' ') founded = true;
00074                         else aux += *iter;
00075                 }
00076         }
00077         return aux;
00078       }
00079     } // namespace {}
00080 
00081 
00082     CPU::CPU(const ParameterSet& iPS, ActivityRegistry&iRegistry):
00083         totalNumberCPUs_(0),
00084         averageCoreSpeed_(0.0),
00085         reportCPUProperties_(iPS.getUntrackedParameter<bool>("reportCPUProperties"))
00086     {
00087         iRegistry.watchPostEndJob(this,&CPU::postEndJob);
00088     }
00089 
00090 
00091     CPU::~CPU()
00092     {
00093     }
00094 
00095     void CPU::fillDescriptions(edm::ConfigurationDescriptions & descriptions) {
00096       edm::ParameterSetDescription desc;
00097       desc.addUntracked<bool>("reportCPUProperties", false);
00098       descriptions.add("CPU", desc);
00099     }
00100 
00101 
00102     void CPU::postEndJob()
00103     {
00104       Service<JobReport> reportSvc;
00105 
00106       std::map<std::string, std::string> reportCPUProperties; // Summary
00107       std::map<std::string, std::string> currentCoreProperties; // Module(s)
00108 
00109       std::ifstream fcpuinfo ("/proc/cpuinfo");
00110 
00111       if(fcpuinfo.is_open()){
00112 
00113         std::string buf;
00114         std::string currentCore;
00115         std::string CPUModels;
00116 
00117         std::set<std::string> models;
00118 
00119         while(!fcpuinfo.eof()){
00120 
00121                 std::getline(fcpuinfo, buf);
00122 
00123                 std::istringstream iss(buf);
00124                 std::string token;
00125                 std::string property;
00126                 std::string value;
00127 
00128                 int time = 1;
00129 
00130                 while(std::getline(iss, token, ':')) {
00131                         switch(time){
00132                         case 1:
00133                                 property = token;
00134                                 break;
00135                         case 2:
00136                                 value = token;
00137                                 break;
00138                         default:
00139                                 value += token;
00140                                 break;
00141                         }
00142                         time++;
00143                 }
00144                 trim(property);
00145                 trim(value);
00146 
00147                 if(!property.empty()){
00148                         if(property == "processor") {
00149                             if(reportCPUProperties_){
00150                                 if(currentCore.empty()) { // first core
00151                                         currentCore = value;
00152                                 }
00153                                 else{
00154                                         reportSvc->reportPerformanceForModule("SystemCPU", "CPU-"+currentCore, currentCoreProperties);
00155                                         currentCoreProperties.clear();
00156                                         currentCore = value;
00157                                 }
00158                             }
00159                             totalNumberCPUs_++;
00160                         }
00161                         else {
00162                                 if(reportCPUProperties_){
00163                                         currentCoreProperties.insert(std::make_pair(property, value));
00164                                 }
00165                                 if(property == "cpu MHz"){
00166                                         averageCoreSpeed_ += str2d(value);
00167                                 }
00168                                 if(property == "model name"){
00169                                         models.insert(eraseExtraSpaces(value));
00170                                 }
00171                         }
00172                 }
00173         } //while
00174 
00175         fcpuinfo.close();
00176 
00177         if(!currentCore.empty() && reportCPUProperties_) {
00178                 reportSvc->reportPerformanceForModule("SystemCPU", "CPU-"+currentCore, currentCoreProperties);
00179         }
00180 
00181         reportCPUProperties.insert(std::make_pair("totalCPUs", i2str(totalNumberCPUs_)));
00182         
00183         if(totalNumberCPUs_ == 0){
00184                 averageCoreSpeed_ = 0.0;
00185         }
00186         else{
00187                 averageCoreSpeed_ = averageCoreSpeed_/totalNumberCPUs_;
00188         }
00189         
00190         reportCPUProperties.insert(std::make_pair("averageCoreSpeed", d2str(averageCoreSpeed_)));
00191 
00192         int model = 0;
00193         for(std::set<std::string>::const_iterator iter = models.begin(); iter != models.end(); iter++){
00194                 if(model == 0)
00195                         CPUModels += *iter;
00196                 else
00197                         CPUModels += ", " + *iter;
00198                 model++;
00199         }
00200         reportCPUProperties.insert(std::make_pair("CPUModels", CPUModels));
00201 
00202 
00203         reportSvc->reportPerformanceSummary("SystemCPU", reportCPUProperties);
00204 
00205       } //if
00206     } //postEndJob
00207   } //service
00208 }  //edm
00209 
00210