CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/DQM/HLTEvF/src/PathTimerService.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Services
00004 // Class  :     PathTimerService
00005 // 
00006 // Implementation:
00007 //
00008 // Original Author:  Jim Kowalkowski
00009 // $Id: PathTimerService.cc,v 1.14 2008/07/30 09:35:08 wittich Exp $
00010 //
00011 
00012 #include "DQM/HLTEvF/interface/PathTimerService.h"
00013 #include "FWCore/ServiceRegistry/interface/Service.h"
00014 #include "DataFormats/Provenance/interface/ModuleDescription.h"
00015 #include "FWCore/Utilities/interface/Exception.h"
00016 #include "FWCore/Framework/interface/TriggerNamesService.h"
00017 #include <iostream>
00018 #include <string>
00019 #include <vector>
00020 #include <sys/time.h>
00021 #include <sstream>
00022 #include <math.h>
00023 
00024 using namespace std ;
00025 namespace edm {
00026     namespace service {
00027 
00028         static double getTime()  {
00029             struct timeval t;
00030             if(gettimeofday(&t,0)<0)
00031                 throw cms::Exception("SysCallFailed","Failed call to gettimeofday");
00032       
00033             return (double)t.tv_sec + (double(t.tv_usec) * 1E-6);
00034         }
00035 
00036         edm::CPUTimer* PathTimerService::_CPUtimer = 0;
00037 
00038         PathTimerService::PathTimerService(const ParameterSet& iPS, ActivityRegistry&iRegistry):
00039             total_event_count_(0),
00040             _perfInfo(new HLTPerformanceInfo)
00041         {
00042             iRegistry.watchPostBeginJob(this,&PathTimerService::postBeginJob);
00043             iRegistry.watchPostEndJob(this,&PathTimerService::postEndJob);
00044             
00045             iRegistry.watchPreProcessEvent(this,&PathTimerService::preEventProcessing);
00046             iRegistry.watchPostProcessEvent(this,&PathTimerService::postEventProcessing);
00047       
00048             iRegistry.watchPreModule(this,&PathTimerService::preModule);
00049             iRegistry.watchPostModule(this,&PathTimerService::postModule);
00050 
00051             iRegistry.watchPostProcessPath(this,&PathTimerService::postPathProcessing);
00052             
00053             _myPS=iPS;
00054 
00055             if (!_CPUtimer) _CPUtimer = new edm::CPUTimer();
00056         }
00057 
00058 
00059         PathTimerService::~PathTimerService() {
00060             if (_CPUtimer) {
00061                 delete _CPUtimer ;
00062                 _CPUtimer = 0 ;
00063             }
00064         }
00065 
00066 
00067         void PathTimerService::postBeginJob() {
00068 
00069             edm::Service<edm::service::TriggerNamesService> tns;
00070             std::vector<std::string> trigPaths= tns->getTrigPaths();
00071             for ( unsigned int i=0; i<trigPaths.size(); i++) {
00072                 _pathMapping[i]=trigPaths[i];
00073                 HLTPerformanceInfo::Path hltPath(trigPaths[i]);
00074                 std::vector<unsigned int> loc ; 
00075                 const std::vector<std::string> modules=tns->getTrigPathModules(trigPaths[i]);
00076                 unsigned int mIdx = 0 ; 
00077                 _perfInfo->addPath(hltPath);
00078                 for ( unsigned int j=0; j<modules.size(); j++) {
00079                     _moduleTime[modules[j]]=0. ;
00080                     _moduleCPUTime[modules[j]]=0. ;
00081                     HLTPerformanceInfo::Modules::const_iterator iMod =
00082                         _perfInfo->findModule(modules[j].c_str());
00083                     if ( iMod == _perfInfo->endModules() ) {
00084                         HLTPerformanceInfo::Module hltModule(modules[j].c_str(),0.,0.,hlt::Ready);
00085                         _perfInfo->addModule(hltModule);
00086                     }
00087 
00088                     //--- Check the module frequency in the path ---//
00089                     bool duplicateModule = false ; 
00090                     for (unsigned int k=0; k<j; k++) {
00091                         if (modules[k] == modules[j]) {
00092                             if (!duplicateModule) loc.push_back(k) ; 
00093                             duplicateModule = true ;
00094                         }
00095                     }
00096                     if (!duplicateModule) {
00097                       _perfInfo->addModuleToPath(modules[j].c_str(),trigPaths[i].c_str());
00098                         loc.push_back(mIdx++) ; 
00099                     }
00100                 }
00101                 _newPathIndex.push_back(loc) ;
00102             }
00103             curr_job_ = getTime();
00104         }
00105 
00106     void PathTimerService::postEndJob() {}
00107 
00108     void PathTimerService::preEventProcessing(const edm::EventID& iID,
00109                                     const edm::Timestamp& iTime) {
00110       curr_event_ = iID;
00111       curr_event_time_ = getTime();
00112 
00113       _perfInfo->clearModules();
00114       
00115       std::map<std::string, double>::iterator iter=_moduleTime.begin();
00116       std::map<std::string, double>::iterator iCPU=_moduleCPUTime.begin();
00117 
00118       while ( iter != _moduleTime.end()) {
00119         (*iter).second=0.;
00120         iter++;
00121       }
00122       while ( iCPU != _moduleCPUTime.end()) {
00123         (*iCPU).second=0.;
00124         iCPU++;
00125       }
00126 
00127     }
00128     void PathTimerService::postEventProcessing(const Event& e, const EventSetup&)  {
00129       total_event_count_ = total_event_count_ + 1;
00130     }
00131 
00132     void PathTimerService::preModule(const ModuleDescription&) {
00133         _CPUtimer->reset() ; 
00134         _CPUtimer->start() ; 
00135     }
00136 
00137       void PathTimerService::postModule(const ModuleDescription& desc) {
00138 
00139           _CPUtimer->stop() ;
00140           double tWall = _CPUtimer->realTime() ; 
00141           double tCPU  = _CPUtimer->cpuTime() ;
00142           _CPUtimer->reset() ; 
00143       
00144           _moduleTime[desc.moduleLabel()] = tWall ;
00145           _moduleCPUTime[desc.moduleLabel()] = tCPU ; 
00146         
00147           HLTPerformanceInfo::Modules::iterator iMod =
00148               _perfInfo->findModule(desc.moduleLabel().c_str());
00149           if ( iMod != _perfInfo->endModules() ) {
00150             iMod->setTime(tWall) ;
00151             iMod->setCPUTime(tCPU) ;
00152           }
00153 
00154       }
00155 
00156       void PathTimerService::postPathProcessing(const std::string &name, 
00157                                                 const HLTPathStatus &status) {
00158           HLTPerformanceInfo::PathList::iterator iPath=_perfInfo->beginPaths();
00159           int ctr = 0 ; 
00160           while ( iPath != _perfInfo->endPaths() ) {
00161             if ( iPath->name() == name) { 
00162               unsigned int pIndex = _newPathIndex.at(ctr).at(status.index()) ;
00163               iPath->setStatus(HLTPathStatus(status.state(),pIndex)) ; 
00164               _perfInfo->setStatusOfModulesFromPath(name.c_str());
00165             }
00166             iPath++;
00167             ctr++; 
00168 
00169           }
00170 
00171       }
00172   }
00173 }