CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PerfTools/Callgrind/src/ProfilerService.cc

Go to the documentation of this file.
00001 #include "PerfTools/Callgrind/interface/ProfilerService.h"
00002 
00003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00004 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 
00007 #include <algorithm>
00008 #include <limits>
00009 
00010 #include "valgrind/callgrind.h"
00011 
00012 ProfilerService::ProfilerService(edm::ParameterSet const& pset, 
00013                                  edm::ActivityRegistry  & activity) :
00014   
00015   m_firstEvent(pset.getUntrackedParameter<int>("firstEvent",0 )),
00016   m_lastEvent(pset.getUntrackedParameter<int>("lastEvent",std::numeric_limits<int>::max())),
00017   m_dumpInterval(pset.getUntrackedParameter<int>("dumpInterval",100)),
00018   m_paths(pset.getUntrackedParameter<std::vector<std::string> >("paths",std::vector<std::string>() )),
00019   m_excludedPaths(pset.getUntrackedParameter<std::vector<std::string> >("excludePaths",std::vector<std::string>() )),
00020   m_allPaths(false),
00021   m_evtCount(0),
00022   m_counts(0),
00023   m_doEvent(false),
00024   m_active(0),
00025   m_paused(false) {
00026   static std::string const allPaths("ALL");
00027   m_allPaths = std::find(m_paths.begin(),m_paths.end(),allPaths) != m_paths.end();
00028   
00029   // either FullEvent or selected path
00030   static std::string const fullEvent("FullEvent");
00031   if (std::find(m_paths.begin(),m_paths.end(),fullEvent) != m_paths.end())
00032     activity.watchPostSource(this,&ProfilerService::preSourceI);
00033   else {
00034     activity.watchPreProcessEvent(this,&ProfilerService::beginEventI);
00035     activity.watchPostProcessEvent(this,&ProfilerService::endEventI);
00036     activity.watchPreProcessPath(this,&ProfilerService::beginPathI);
00037     activity.watchPostProcessPath(this,&ProfilerService::endPathI);
00038   }
00039 }
00040 
00041 ProfilerService::~ProfilerService(){
00042   dumpStat();
00043 }
00044 
00045 bool ProfilerService::startInstrumentation(){
00046   // FIXME here or in client?
00047   if (!doEvent()) return false;
00048 
00049 
00050   if (m_active==0) {
00051     CALLGRIND_START_INSTRUMENTATION;
00052     if (m_counts%m_dumpInterval==0) dumpStat();
00053     ++m_counts;
00054   }
00055   // support nested start/stop
00056   ++m_active;
00057   return m_active==1;
00058 }
00059 
00060 bool ProfilerService::stopInstrumentation() {
00061   if (m_active==0) return false;
00062   --m_active;
00063   if (m_active==0)
00064     CALLGRIND_STOP_INSTRUMENTATION;
00065   return m_active==0;
00066 }
00067 
00068 bool ProfilerService::forceStopInstrumentation() {
00069   if (m_active==0) return false;
00070   // FIXME report something if appens;
00071   CALLGRIND_STOP_INSTRUMENTATION;
00072   m_active=0;
00073   return true;
00074 }
00075 
00076 bool ProfilerService::pauseInstrumentation() {
00077    if (m_active==0) return false;
00078    CALLGRIND_STOP_INSTRUMENTATION;
00079    m_paused=true;
00080    return true;
00081 }   
00082 
00083 bool ProfilerService::resumeInstrumentation() {
00084   if (m_active==0 || (!m_paused)) return false;
00085   CALLGRIND_START_INSTRUMENTATION;
00086   if (m_counts%m_dumpInterval==0) dumpStat();
00087   ++m_counts;
00088   m_paused=false;
00089   return true;
00090 }
00091 
00092 void ProfilerService::dumpStat() const {
00093      CALLGRIND_DUMP_STATS;
00094 }
00095 
00096 
00097 void ProfilerService::newEvent() {
00098   ++m_evtCount;
00099   m_doEvent = m_evtCount >= m_firstEvent && m_evtCount <= m_lastEvent;
00100 }
00101 
00102 
00103 void ProfilerService::fullEvent() {
00104   newEvent();
00105   if(m_doEvent&&m_active==0)
00106     startInstrumentation();
00107   if ( (!m_doEvent) && m_active!=0) {
00108     stopInstrumentation();
00109     // force, a nested instrumentation may fail to close in presence of filters
00110     forceStopInstrumentation();
00111     dumpStat();
00112   }
00113 }
00114 
00115 void  ProfilerService::beginEvent() {
00116   newEvent();
00117   //  static std::string const fullEvent("FullEvent");
00118   //  if (std::find(m_paths.begin(),m_paths.end(),fullEvent) != m_paths.end())
00119   if (m_allPaths) 
00120     startInstrumentation();
00121 }
00122 
00123 void  ProfilerService::endEvent() {
00124   stopInstrumentation();
00125   // force, a nested instrumentation may fail to close in presence of filters
00126   forceStopInstrumentation();
00127 }
00128 
00129 void  ProfilerService::beginPath(std::string const & path) {
00130   if (!doEvent()) return;
00131   // assume less than 5-6 path to instrument or to exclude
00132   if (std::find(m_excludedPaths.begin(),m_excludedPaths.end(),path) != m_excludedPaths.end()) {
00133     pauseInstrumentation();
00134     return; 
00135   }
00136   if (std::find(m_paths.begin(),m_paths.end(),path) == m_paths.end()) return; 
00137   m_activePath=path;
00138   startInstrumentation();
00139 }
00140 
00141 void  ProfilerService::endPath(std::string const & path) {
00142   resumeInstrumentation();
00143   if (m_activePath==path) {
00144     stopInstrumentation();
00145     m_activePath.clear();
00146   }
00147   // do to force, a nested instrumentation may fail to close in presence of filters  
00148   // forceStopInstrumentation();
00149 }