Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:20

0001 #include "PerfTools/Callgrind/interface/ProfilerService.h"
0002 
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 
0007 #include <algorithm>
0008 #include <limits>
0009 
0010 #include "valgrind/callgrind.h"
0011 
0012 ProfilerService::ProfilerService(edm::ParameterSet const& pset, edm::ActivityRegistry& activity)
0013     :
0014 
0015       m_firstEvent(pset.getUntrackedParameter<int>("firstEvent", 0)),
0016       m_lastEvent(pset.getUntrackedParameter<int>("lastEvent", std::numeric_limits<int>::max())),
0017       m_dumpInterval(pset.getUntrackedParameter<int>("dumpInterval", 100)),
0018       m_paths(pset.getUntrackedParameter<std::vector<std::string> >("paths", std::vector<std::string>())),
0019       m_excludedPaths(
0020           pset.getUntrackedParameter<std::vector<std::string> >("excludePaths", std::vector<std::string>())),
0021       m_allPaths(false),
0022       m_evtCount(0),
0023       m_counts(0),
0024       m_doEvent(false),
0025       m_active(0),
0026       m_paused(false) {
0027   static std::string const allPaths("ALL");
0028   m_allPaths = std::find(m_paths.begin(), m_paths.end(), allPaths) != m_paths.end();
0029 
0030   // either FullEvent or selected path
0031   static std::string const fullEvent("FullEvent");
0032   if (std::find(m_paths.begin(), m_paths.end(), fullEvent) != m_paths.end())
0033     activity.watchPostSourceEvent(this, &ProfilerService::preSourceI);
0034   else {
0035     activity.watchPreEvent(this, &ProfilerService::beginEventI);
0036     activity.watchPostEvent(this, &ProfilerService::endEventI);
0037     activity.watchPrePathEvent(this, &ProfilerService::beginPathI);
0038     activity.watchPostPathEvent(this, &ProfilerService::endPathI);
0039   }
0040 }
0041 
0042 ProfilerService::~ProfilerService() { dumpStat(); }
0043 
0044 #pragma GCC diagnostic push
0045 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
0046 bool ProfilerService::startInstrumentation() {
0047   // FIXME here or in client?
0048   if (!doEvent())
0049     return false;
0050 
0051   if (m_active == 0) {
0052     CALLGRIND_START_INSTRUMENTATION;
0053     if (m_counts % m_dumpInterval == 0)
0054       dumpStat();
0055     ++m_counts;
0056   }
0057   // support nested start/stop
0058   ++m_active;
0059   return m_active == 1;
0060 }
0061 
0062 bool ProfilerService::stopInstrumentation() {
0063   if (m_active == 0)
0064     return false;
0065   --m_active;
0066   if (m_active == 0)
0067     CALLGRIND_STOP_INSTRUMENTATION;
0068   return m_active == 0;
0069 }
0070 
0071 bool ProfilerService::forceStopInstrumentation() {
0072   if (m_active == 0)
0073     return false;
0074   // FIXME report something if appens;
0075   CALLGRIND_STOP_INSTRUMENTATION;
0076   m_active = 0;
0077   return true;
0078 }
0079 
0080 bool ProfilerService::pauseInstrumentation() {
0081   if (m_active == 0)
0082     return false;
0083   CALLGRIND_STOP_INSTRUMENTATION;
0084   m_paused = true;
0085   return true;
0086 }
0087 
0088 bool ProfilerService::resumeInstrumentation() {
0089   if (m_active == 0 || (!m_paused))
0090     return false;
0091   CALLGRIND_START_INSTRUMENTATION;
0092   if (m_counts % m_dumpInterval == 0)
0093     dumpStat();
0094   ++m_counts;
0095   m_paused = false;
0096   return true;
0097 }
0098 
0099 void ProfilerService::dumpStat() const { CALLGRIND_DUMP_STATS; }
0100 #pragma GCC diagnostic pop
0101 
0102 void ProfilerService::newEvent() {
0103   ++m_evtCount;
0104   m_doEvent = m_evtCount >= m_firstEvent && m_evtCount <= m_lastEvent;
0105 }
0106 
0107 void ProfilerService::fullEvent() {
0108   newEvent();
0109   if (m_doEvent && m_active == 0)
0110     startInstrumentation();
0111   if ((!m_doEvent) && m_active != 0) {
0112     stopInstrumentation();
0113     // force, a nested instrumentation may fail to close in presence of filters
0114     forceStopInstrumentation();
0115     dumpStat();
0116   }
0117 }
0118 
0119 void ProfilerService::beginEvent() {
0120   newEvent();
0121   //  static std::string const fullEvent("FullEvent");
0122   //  if (std::find(m_paths.begin(),m_paths.end(),fullEvent) != m_paths.end())
0123   if (m_allPaths)
0124     startInstrumentation();
0125 }
0126 
0127 void ProfilerService::endEvent() {
0128   stopInstrumentation();
0129   // force, a nested instrumentation may fail to close in presence of filters
0130   forceStopInstrumentation();
0131 }
0132 
0133 void ProfilerService::beginPath(std::string const& path) {
0134   if (!doEvent())
0135     return;
0136   // assume less than 5-6 path to instrument or to exclude
0137   if (std::find(m_excludedPaths.begin(), m_excludedPaths.end(), path) != m_excludedPaths.end()) {
0138     pauseInstrumentation();
0139     return;
0140   }
0141   if (std::find(m_paths.begin(), m_paths.end(), path) == m_paths.end())
0142     return;
0143   m_activePath = path;
0144   startInstrumentation();
0145 }
0146 
0147 void ProfilerService::endPath(std::string const& path) {
0148   resumeInstrumentation();
0149   if (m_activePath == path) {
0150     stopInstrumentation();
0151     m_activePath.clear();
0152   }
0153   // do to force, a nested instrumentation may fail to close in presence of filters
0154   // forceStopInstrumentation();
0155 }