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
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 #pragma GCC diagnostic push
00046 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
00047 bool ProfilerService::startInstrumentation(){
00048
00049 if (!doEvent()) return false;
00050
00051
00052 if (m_active==0) {
00053 CALLGRIND_START_INSTRUMENTATION;
00054 if (m_counts%m_dumpInterval==0) dumpStat();
00055 ++m_counts;
00056 }
00057
00058 ++m_active;
00059 return m_active==1;
00060 }
00061
00062 bool ProfilerService::stopInstrumentation() {
00063 if (m_active==0) return false;
00064 --m_active;
00065 if (m_active==0)
00066 CALLGRIND_STOP_INSTRUMENTATION;
00067 return m_active==0;
00068 }
00069
00070 bool ProfilerService::forceStopInstrumentation() {
00071 if (m_active==0) return false;
00072
00073 CALLGRIND_STOP_INSTRUMENTATION;
00074 m_active=0;
00075 return true;
00076 }
00077
00078 bool ProfilerService::pauseInstrumentation() {
00079 if (m_active==0) return false;
00080 CALLGRIND_STOP_INSTRUMENTATION;
00081 m_paused=true;
00082 return true;
00083 }
00084
00085 bool ProfilerService::resumeInstrumentation() {
00086 if (m_active==0 || (!m_paused)) return false;
00087 CALLGRIND_START_INSTRUMENTATION;
00088 if (m_counts%m_dumpInterval==0) dumpStat();
00089 ++m_counts;
00090 m_paused=false;
00091 return true;
00092 }
00093
00094 void ProfilerService::dumpStat() const {
00095 CALLGRIND_DUMP_STATS;
00096 }
00097 #pragma GCC diagnostic pop
00098
00099 void ProfilerService::newEvent() {
00100 ++m_evtCount;
00101 m_doEvent = m_evtCount >= m_firstEvent && m_evtCount <= m_lastEvent;
00102 }
00103
00104
00105 void ProfilerService::fullEvent() {
00106 newEvent();
00107 if(m_doEvent&&m_active==0)
00108 startInstrumentation();
00109 if ( (!m_doEvent) && m_active!=0) {
00110 stopInstrumentation();
00111
00112 forceStopInstrumentation();
00113 dumpStat();
00114 }
00115 }
00116
00117 void ProfilerService::beginEvent() {
00118 newEvent();
00119
00120
00121 if (m_allPaths)
00122 startInstrumentation();
00123 }
00124
00125 void ProfilerService::endEvent() {
00126 stopInstrumentation();
00127
00128 forceStopInstrumentation();
00129 }
00130
00131 void ProfilerService::beginPath(std::string const & path) {
00132 if (!doEvent()) return;
00133
00134 if (std::find(m_excludedPaths.begin(),m_excludedPaths.end(),path) != m_excludedPaths.end()) {
00135 pauseInstrumentation();
00136 return;
00137 }
00138 if (std::find(m_paths.begin(),m_paths.end(),path) == m_paths.end()) return;
00139 m_activePath=path;
00140 startInstrumentation();
00141 }
00142
00143 void ProfilerService::endPath(std::string const & path) {
00144 resumeInstrumentation();
00145 if (m_activePath==path) {
00146 stopInstrumentation();
00147 m_activePath.clear();
00148 }
00149
00150
00151 }