CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/HLTrigger/Timer/interface/FastTimerService.h

Go to the documentation of this file.
00001 #ifndef FastTimerService_h
00002 #define FastTimerService_h
00003 
00004 //system headers
00005 #ifdef __linux
00006 #include <time.h>
00007 #else
00008 typedef int clockid_t;
00009 #endif
00010 
00011 // C++ headers
00012 #include <cmath>
00013 #include <string>
00014 #include <map>
00015 #include <tr1/unordered_map>
00016 
00017 // CMSSW headers
00018 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00019 #include "FWCore/ServiceRegistry/interface/Service.h"
00020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00021 #include "FWCore/Framework/interface/TriggerNamesService.h"
00022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00023 #include "DataFormats/Common/interface/HLTPathStatus.h"
00024 #include "DataFormats/Provenance/interface/EventID.h"
00025 #include "DataFormats/Provenance/interface/Timestamp.h"
00026 #include "DataFormats/Provenance/interface/ModuleDescription.h"
00027 #include "DQMServices/Core/interface/DQMStore.h"
00028 #include "DQMServices/Core/interface/MonitorElement.h"
00029 
00030 
00031 /*
00032 procesing time is diveded into
00033  - source
00034  - pre-event processing overhead
00035  - event processing
00036  - post-event processing overhead
00037 
00038 until lumi-processing and run-processing are taken into account, they will count as overhead
00039 
00040 event processing time is diveded into
00041  - trigger processing (from the begin of the first path to the end of the last path)
00042  - trigger overhead
00043  - endpath processing (from the begin of the first endpath to the end of the last endpath)
00044  - endpath overhead
00045 */
00046 
00047 /*
00048 Timer per-call overhead (on lxplus, SLC 5.7):
00049 
00050 Test results for "clock_gettime(CLOCK_THREAD_CPUTIME_ID, & value)"
00051  - average time per call: 340 ns
00052  - resolution:              ? ns
00053 
00054 Test results for "clock_gettime(CLOCK_PROCESS_CPUTIME_ID, & value)"
00055  - average time per call: 360 ns
00056  - resolution:              ? ns
00057 
00058 Test results for "clock_gettime(CLOCK_REALTIME, & value)"
00059  - average time per call: 320 ns
00060  - resolution:              1 us
00061 
00062 Test results for "getrusage(RUSAGE_SELF, & value)"
00063  - average time per call: 380 ns
00064  - resolution:              1 ms
00065 
00066 Test results for "gettimeofday(& value, NULL)"
00067  - average time per call:  65 ns
00068  - resolution:              1 us
00069 
00070 Assuming an HLT process with ~2500 modules and ~500 paths, tracking each step
00071 with clock_gettime(CLOCK_THREAD_CPUTIME_ID) gives a per-event overhead of 2 ms
00072 
00073 Detailed informations on different timers can be extracted running $CMSSW_RELEASE_BASE/test/$SCRAM_ARCH/testTimer .
00074 */
00075 
00076 
00077 class FastTimerService {
00078 public:
00079   FastTimerService(const edm::ParameterSet &, edm::ActivityRegistry & );
00080   ~FastTimerService();
00081 
00082   // query the current module/path/event
00083   // Note: these functions incur in a "per-call timer overhead" (see above), currently of the order of 340ns
00084   double currentModuleTime() const;         // return the time spent since the last preModule() event
00085   double currentPathTime() const;           // return the time spent since the last preProcessPath() event
00086   double currentEventTime() const;          // return the time spent since the last preProcessEvent() event
00087 
00088   // query the time spent in a module/path the last time it has run
00089   double queryModuleTime(const edm::ModuleDescription &) const;
00090   double queryPathTime(const std::string &) const;
00091 
00092   // query the time spent in the current (or last) event's
00093   //  - source        (available during event processing)
00094   //  - all paths     (available during endpaths)
00095   //  - all endpaths  (available after all endpaths have run, usually returns the last event's value)
00096   //  - processing    (available after the event has been processed, usually returns the last event's value)
00097   double querySourceTime() const;
00098   double queryPathsTime() const;
00099   double queryEndPathsTime() const;
00100   double queryEventTime() const;
00101 
00102   // try to assess the overhead which may not be included in the source, paths and event timers
00103   double queryPreSourceOverhead() const;    // time spent after the previous event's postProcessEvent and this event's preSource
00104   double queryPreEventOverhead() const;     // time spent after this event's postSource and preProcessEvent
00105   double queryPreEndPathsOverhead() const;  // time spent after the last path's postProcessPath and the first endpath's preProcessPath
00106 
00107 
00108 private:
00109   void postBeginJob();
00110   void postEndJob();
00111   void preModuleBeginJob( edm::ModuleDescription const & );
00112   void preProcessEvent( edm::EventID const &, edm::Timestamp const & );
00113   void postProcessEvent( edm::Event const &, edm::EventSetup const & );
00114   void preSource();
00115   void postSource();
00116   void prePathBeginRun( std::string const & );
00117   void preProcessPath(  std::string const & );
00118   void postProcessPath( std::string const &, edm::HLTPathStatus const & );
00119   void preModule(  edm::ModuleDescription const & );
00120   void postModule( edm::ModuleDescription const & );
00121 
00122 private:
00123 
00124   struct ModuleInfo {
00125     double                      time_active;        // per-event timer: time actually spent in this module
00126     double                      summary_active;
00127     TH1F *                      dqm_active;
00128     bool                        has_just_run;       // flag sed to check if a module was active inside a particular path, or not
00129 
00130   public:
00131     ModuleInfo() :
00132       time_active(0.),
00133       summary_active(0.),
00134       dqm_active(0),
00135       has_just_run(false)
00136     { }
00137   };
00138 
00139   struct PathInfo {
00140     std::vector<ModuleInfo *>   modules;            // list of all modules contributing to the path (duplicate modules stored as null pointers)
00141     double                      time_active;        // per-event timer: time actually spent in this path
00142 #ifdef FASTTIMERSERVICE_DETAILED_OVERHEAD_ACCOUNTING
00143     double                      time_premodules;    // per-event timer: time spent between "begin path" and the first "begin module"
00144     double                      time_intermodules;  // per-event timer: time spent between modules
00145     double                      time_postmodules;   // per-event timer: time spent between the last "end module" and "end path"
00146 #else
00147     double                      time_overhead;      // per-event timer: sum of time_premodules, time_intermodules, time_postmodules
00148 #endif
00149     double                      time_total;         // per-event timer: sum of the time spent in all modules which would have run in this path (plus overhead)
00150     double                      summary_active;
00151 #ifdef FASTTIMERSERVICE_DETAILED_OVERHEAD_ACCOUNTING
00152     double                      summary_premodules;
00153     double                      summary_intermodules;
00154     double                      summary_postmodules;
00155 #else
00156     double                      summary_overhead;
00157 #endif
00158     double                      summary_total;
00159     TH1F *                      dqm_active;
00160 #ifdef FASTTIMERSERVICE_DETAILED_OVERHEAD_ACCOUNTING
00161     TH1F *                      dqm_premodules;
00162     TH1F *                      dqm_intermodules;
00163     TH1F *                      dqm_postmodules;
00164 #else
00165     TH1F *                      dqm_overhead;
00166 #endif
00167     TH1F *                      dqm_total;
00168     TH1F *                      dqm_module_counter;     // for each module in the path, track how many times it ran
00169     TH1F *                      dqm_module_active;      // for each module in the path, track the active time spent 
00170     TH1F *                      dqm_module_total;       // for each module in the path, track the total time spent 
00171 
00172   public:
00173     PathInfo() :
00174       modules(),
00175       time_active(0.),
00176 #ifdef FASTTIMERSERVICE_DETAILED_OVERHEAD_ACCOUNTING
00177       time_premodules(0.),
00178       time_intermodules(0.),
00179       time_postmodules(0.),
00180 #else
00181       time_overhead(0.),
00182 #endif
00183       time_total(0.),
00184       summary_active(0.),
00185 #ifdef FASTTIMERSERVICE_DETAILED_OVERHEAD_ACCOUNTING
00186       summary_premodules(0.),
00187       summary_intermodules(0.),
00188       summary_postmodules(0.),
00189 #else
00190       summary_overhead(0.),
00191 #endif
00192       summary_total(0.),
00193       dqm_active(0),
00194 #ifdef FASTTIMERSERVICE_DETAILED_OVERHEAD_ACCOUNTING
00195       dqm_premodules(0),
00196       dqm_intermodules(0),
00197       dqm_postmodules(0),
00198 #else
00199       dqm_overhead(0),
00200 #endif
00201       dqm_total(0),
00202       dqm_module_counter(0),
00203       dqm_module_active(0),
00204       dqm_module_total(0)
00205     { }
00206   };
00207 
00208   template <typename T> class PathMap   : public std::tr1::unordered_map<std::string, T> {};
00209   template <typename T> class ModuleMap : public std::tr1::unordered_map<edm::ModuleDescription const *, T> {};
00210 
00211   // configuration
00212   const clockid_t                               m_timer_id;             // the default is to use CLOCK_THREAD_CPUTIME_ID, unless useRealTimeClock is set, which will use CLOCK_REALTIME
00213   bool                                          m_is_cpu_bound;         // if the process is not bound to a single CPU, per-thread or per-process measuerements may be unreliable
00214   const bool                                    m_enable_timing_modules;
00215   const bool                                    m_enable_timing_paths;
00216   const bool                                    m_enable_timing_summary;
00217   const bool                                    m_enable_dqm;
00218   const bool                                    m_enable_dqm_bylumi;
00219 
00220   // dqm configuration
00221   const double                                  m_dqm_time_range;
00222   const double                                  m_dqm_time_resolution;
00223   std::string                                   m_dqm_path;
00224 
00225   // job configuration and caching
00226   std::string const *                           m_first_path;           // the framework does not provide a pre-paths or pre-endpaths signal,
00227   std::string const *                           m_last_path;            // so we emulate them keeping track of the first and last path and endpath
00228   std::string const *                           m_first_endpath;
00229   std::string const *                           m_last_endpath;
00230   bool                                          m_is_first_module;      // helper to measure the time spent between the beginning of the path and the execution of the first module
00231 
00232   // per-event accounting
00233   double                                        m_event;
00234   double                                        m_source;
00235   double                                        m_all_paths;
00236   double                                        m_all_endpaths;
00237 
00238   // per-job summary
00239   unsigned int                                  m_summary_events;       // number of events
00240   double                                        m_summary_event;
00241   double                                        m_summary_source;
00242   double                                        m_summary_all_paths;
00243   double                                        m_summary_all_endpaths;
00244 
00245   // DQM
00246   DQMStore *                                    m_dqms;
00247   TH1F *                                        m_dqm_event;
00248   TH1F *                                        m_dqm_source;
00249   TH1F *                                        m_dqm_all_paths;
00250   TH1F *                                        m_dqm_all_endpaths;
00251 
00252   // per-path and per-module accounting
00253   PathInfo *                                    m_current_path;
00254   PathMap<PathInfo>                             m_paths;
00255   ModuleMap<ModuleInfo>                         m_modules;              // this assumes that ModuleDescription are stored in the same object through the whole job,
00256                                                                         // which is true only *after* the edm::Worker constructors have run
00257   std::vector<PathInfo *>                       m_cache_paths;
00258   std::vector<ModuleInfo *>                     m_cache_modules;
00259 
00260 
00261   // timers
00262   std::pair<struct timespec, struct timespec>   m_timer_event;          // track time spent in each event
00263   std::pair<struct timespec, struct timespec>   m_timer_source;         // track time spent in the source
00264   std::pair<struct timespec, struct timespec>   m_timer_paths;          // track time spent in all paths
00265   std::pair<struct timespec, struct timespec>   m_timer_endpaths;       // track time spent in all endpaths
00266   std::pair<struct timespec, struct timespec>   m_timer_path;           // track time spent in each path
00267   std::pair<struct timespec, struct timespec>   m_timer_module;         // track time spent in each module
00268   struct timespec                               m_timer_first_module;   // record the start of the first active module in a path, if any
00269 
00270   void gettime(struct timespec & stamp)
00271   {
00272 #if defined __linux
00273     clock_gettime(m_timer_id, & stamp);
00274 #elif defined __APPLE__ && defined __MACH__
00275     // implement here an OS X version
00276 #else
00277     // unsupported on any other platform
00278 #endif
00279   }
00280 
00281   void start(std::pair<struct timespec, struct timespec> & times)
00282   {
00283     gettime(times.first);
00284   }
00285 
00286   void stop(std::pair<struct timespec, struct timespec> & times)
00287   {
00288     gettime(times.second);
00289   }
00290 
00291   static
00292   double delta(const struct timespec & first, const struct timespec & second)
00293   {
00294     if (second.tv_nsec > first.tv_nsec)
00295       return (double) (second.tv_sec - first.tv_sec) + (double) (second.tv_nsec - first.tv_nsec) / (double) 1e9;
00296     else
00297       return (double) (second.tv_sec - first.tv_sec) - (double) (first.tv_nsec - second.tv_nsec) / (double) 1e9;
00298   }
00299 
00300   static
00301   double delta(const std::pair<struct timespec, struct timespec> & times)
00302   {
00303     return delta(times.first, times.second);
00304   }
00305 
00306   // find the module description associated to a module, by name
00307   edm::ModuleDescription const * findModuleDescription(const std::string & module) const;
00308 
00309   // associate to a path all the modules it contains
00310   void fillPathMap(std::string const & name, std::vector<std::string> const & modules);
00311 
00312 };
00313 
00314 #endif // ! FastTimerService_h