CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/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 /* Darwin system headers */
00012 #if defined(__APPLE__) || defined(__MACH__)
00013 #include <mach/clock.h>
00014 #include <mach/mach.h>
00015 #endif // defined(__APPLE__) || defined(__MACH__)
00016 
00017 // C++ headers
00018 #include <cmath>
00019 #include <string>
00020 #include <map>
00021 #include <tr1/unordered_map>
00022 #include <unistd.h>
00023 
00024 // CMSSW headers
00025 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00026 #include "FWCore/ServiceRegistry/interface/Service.h"
00027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00028 #include "FWCore/Framework/interface/TriggerNamesService.h"
00029 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00030 #include "DataFormats/Common/interface/HLTPathStatus.h"
00031 #include "DataFormats/Provenance/interface/EventID.h"
00032 #include "DataFormats/Provenance/interface/Timestamp.h"
00033 #include "DataFormats/Provenance/interface/ModuleDescription.h"
00034 #include "DQMServices/Core/interface/DQMStore.h"
00035 #include "DQMServices/Core/interface/MonitorElement.h"
00036 
00037 
00038 /*
00039 procesing time is diveded into
00040  - source
00041  - pre-event processing overhead
00042  - event processing
00043  - post-event processing overhead
00044 
00045 until lumi-processing and run-processing are taken into account, they will count as inter-event overhead
00046 
00047 event processing time is diveded into
00048  - trigger processing (from the begin of the first path to the end of the last path)
00049  - trigger overhead
00050  - endpath processing (from the begin of the first endpath to the end of the last endpath)
00051  - endpath overhead
00052 */
00053 
00054 /*
00055 Timer per-call overhead (on lxplus, SLC 5.7):
00056 
00057 Test results for "clock_gettime(CLOCK_THREAD_CPUTIME_ID, & value)"
00058  - average time per call: 340 ns
00059  - resolution:              ? ns
00060 
00061 Test results for "clock_gettime(CLOCK_PROCESS_CPUTIME_ID, & value)"
00062  - average time per call: 360 ns
00063  - resolution:              ? ns
00064 
00065 Test results for "clock_gettime(CLOCK_REALTIME, & value)"
00066  - average time per call: 320 ns
00067  - resolution:              1 us
00068 
00069 Test results for "getrusage(RUSAGE_SELF, & value)"
00070  - average time per call: 380 ns
00071  - resolution:              1 ms
00072 
00073 Test results for "gettimeofday(& value, NULL)"
00074  - average time per call:  65 ns
00075  - resolution:              1 us
00076 
00077 Assuming an HLT process with ~2500 modules and ~500 paths, tracking each step
00078 with clock_gettime(CLOCK_THREAD_CPUTIME_ID) gives a per-event overhead of 2 ms
00079 
00080 Detailed informations on different timers can be extracted running $CMSSW_RELEASE_BASE/test/$SCRAM_ARCH/testTimer .
00081 */
00082 
00083 
00084 class FastTimerService {
00085 public:
00086   FastTimerService(const edm::ParameterSet &, edm::ActivityRegistry & );
00087   ~FastTimerService();
00088 
00089   // query the current module/path/event
00090   // Note: these functions incur in a "per-call timer overhead" (see above), currently of the order of 340ns
00091   double currentModuleTime() const;         // return the time spent since the last preModule() event
00092   double currentPathTime() const;           // return the time spent since the last preProcessPath() event
00093   double currentEventTime() const;          // return the time spent since the last preProcessEvent() event
00094 
00095   // query the time spent in a module/path (available after it has run)
00096   double queryModuleTime(const edm::ModuleDescription &) const;
00097   double queryPathActiveTime(const std::string &) const;
00098   double queryPathTotalTime(const std::string &) const;
00099 
00100   // query the time spent in the current event's
00101   //  - source        (available during event processing)
00102   //  - all paths     (available during endpaths)
00103   //  - all endpaths  (available after all endpaths have run)
00104   //  - processing    (available after the event has been processed)
00105   double querySourceTime() const;
00106   double queryPathsTime() const;
00107   double queryEndPathsTime() const;
00108   double queryEventTime() const;
00109 
00110   /* FIXME not yet implemented
00111   // try to assess the overhead which may not be included in the source, paths and event timers
00112   double queryPreSourceOverhead() const;    // time spent after the previous event's postProcessEvent and this event's preSource
00113   double queryPreEventOverhead() const;     // time spent after this event's postSource and preProcessEvent
00114   double queryPreEndPathsOverhead() const;  // time spent after the last path's postProcessPath and the first endpath's preProcessPath
00115   */
00116 
00117   // optionally called to specify the number of concurrent event processors
00118   void setNumberOfProcesses(unsigned int);
00119 
00120 private:
00121   void postBeginJob();
00122   void postEndJob();
00123   void preModuleBeginJob( edm::ModuleDescription const & );
00124   void preProcessEvent( edm::EventID const &, edm::Timestamp const & );
00125   void postProcessEvent( edm::Event const &, edm::EventSetup const & );
00126   void preSource();
00127   void postSource();
00128   void prePathBeginRun( std::string const & );
00129   void preProcessPath(  std::string const & );
00130   void postProcessPath( std::string const &, edm::HLTPathStatus const & );
00131   void preModule(  edm::ModuleDescription const & );
00132   void postModule( edm::ModuleDescription const & );
00133 
00134   // needed for the DAQ when reconfiguring between runs
00135   void reset();
00136 
00137 public:
00138   static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
00139 
00140 private:
00141 
00142   struct ModuleInfo {
00143     double                      time_active;        // per-event timer: time actually spent in this module
00144     double                      summary_active;
00145     TH1F *                      dqm_active;
00146     bool                        has_just_run;       // flag set to check if a module was active inside a particular path, or not
00147     bool                        is_exclusive;       // flag set to check if a module has been run only once
00148 
00149   public:
00150     ModuleInfo() :
00151       time_active(0.),
00152       summary_active(0.),
00153       dqm_active(0),
00154       has_just_run(false),
00155       is_exclusive(false)
00156     { }
00157 
00158     ~ModuleInfo() {
00159       reset();
00160     }
00161 
00162     // reset the timers and DQM plots
00163     void reset() {
00164       time_active = 0.;
00165       summary_active = 0.;
00166       // the DAQ destroys and re-creates the DQM and DQMStore services at each reconfigure, so we don't need to clean them up
00167       dqm_active = 0;
00168       has_just_run = false;
00169       is_exclusive = false;
00170     }
00171   };
00172 
00173   struct PathInfo {
00174     std::vector<ModuleInfo *>   modules;            // list of all modules contributing to the path (duplicate modules stored as null pointers)
00175     double                      time_active;        // per-event timer: time actually spent in this path
00176     double                      time_premodules;    // per-event timer: time spent between "begin path" and the first "begin module"
00177     double                      time_intermodules;  // per-event timer: time spent between modules
00178     double                      time_postmodules;   // per-event timer: time spent between the last "end module" and "end path"
00179     double                      time_overhead;      // per-event timer: sum of time_premodules, time_intermodules, time_postmodules
00180     double                      time_total;         // per-event timer: sum of the time spent in all modules which would have run in this path (plus overhead)
00181     double                      summary_active;
00182     double                      summary_premodules;
00183     double                      summary_intermodules;
00184     double                      summary_postmodules;
00185     double                      summary_overhead;
00186     double                      summary_total;
00187     uint32_t                    last_run;
00188     uint32_t                    index;              // index of the Path or EndPath in the "schedule"
00189     TH1F *                      dqm_active;
00190     TH1F *                      dqm_exclusive;
00191     TH1F *                      dqm_premodules;
00192     TH1F *                      dqm_intermodules;
00193     TH1F *                      dqm_postmodules;
00194     TH1F *                      dqm_overhead;
00195     TH1F *                      dqm_total;
00196     TH1F *                      dqm_module_counter; // for each module in the path, track how many times it ran
00197     TH1F *                      dqm_module_active;  // for each module in the path, track the active time spent 
00198     TH1F *                      dqm_module_total;   // for each module in the path, track the total time spent 
00199 
00200   public:
00201     PathInfo() :
00202       modules(),
00203       time_active(0.),
00204       time_premodules(0.),
00205       time_intermodules(0.),
00206       time_postmodules(0.),
00207       time_overhead(0.),
00208       time_total(0.),
00209       summary_active(0.),
00210       summary_premodules(0.),
00211       summary_intermodules(0.),
00212       summary_postmodules(0.),
00213       summary_overhead(0.),
00214       summary_total(0.),
00215       last_run(0),
00216       index(0),
00217       dqm_active(0),
00218       dqm_exclusive(0),
00219       dqm_premodules(0),
00220       dqm_intermodules(0),
00221       dqm_postmodules(0),
00222       dqm_overhead(0),
00223       dqm_total(0),
00224       dqm_module_counter(0),
00225       dqm_module_active(0),
00226       dqm_module_total(0)
00227     { }
00228 
00229     ~PathInfo() {
00230       reset();
00231     }
00232 
00233     // reset the timers and DQM plots
00234     void reset() {
00235       modules.clear();
00236       time_active = 0.;
00237       time_premodules = 0.;
00238       time_intermodules = 0.;
00239       time_postmodules = 0.;
00240       time_overhead = 0.;
00241       time_total = 0.;
00242       summary_active = 0.;
00243       summary_premodules = 0.;
00244       summary_intermodules = 0.;
00245       summary_postmodules = 0.;
00246       summary_overhead = 0.;
00247       summary_total = 0.;
00248       last_run = 0;
00249       index = 0;
00250 
00251       // the DAQ destroys and re-creates the DQM and DQMStore services at each reconfigure, so we don't need to clean them up
00252       dqm_active = 0; 
00253       dqm_premodules = 0;
00254       dqm_intermodules = 0;
00255       dqm_postmodules = 0;
00256       dqm_overhead = 0;
00257       dqm_total = 0;
00258       dqm_module_counter = 0;
00259       dqm_module_active = 0;
00260       dqm_module_total = 0;
00261     }
00262   };
00263 
00264   template <typename T> class PathMap   : public std::tr1::unordered_map<std::string, T> {};
00265   template <typename T> class ModuleMap : public std::tr1::unordered_map<edm::ModuleDescription const *, T> {};
00266 
00267   // timer configuration
00268   const clockid_t                               m_timer_id;             // the default is to use CLOCK_THREAD_CPUTIME_ID, unless useRealTimeClock is set, which will use CLOCK_REALTIME
00269   bool                                          m_is_cpu_bound;         // if the process is not bound to a single CPU, per-thread or per-process measuerements may be unreliable
00270   bool                                          m_enable_timing_paths;
00271   bool                                          m_enable_timing_modules;
00272   bool                                          m_enable_timing_exclusive;
00273   const bool                                    m_enable_timing_summary;
00274   const bool                                    m_skip_first_path;
00275 
00276   // dqm configuration
00277   const bool                                    m_enable_dqm;
00278   const bool                                    m_enable_dqm_bypath_active;     // require per-path timers
00279   const bool                                    m_enable_dqm_bypath_total;      // require per-path and per-module timers
00280   const bool                                    m_enable_dqm_bypath_overhead;   // require per-path and per-module timers
00281   const bool                                    m_enable_dqm_bypath_details;    // require per-path and per-module timers
00282   const bool                                    m_enable_dqm_bypath_counters;
00283   const bool                                    m_enable_dqm_bypath_exclusive;
00284   const bool                                    m_enable_dqm_bymodule;          // require per-module timers
00285   const bool                                    m_enable_dqm_summary;
00286   const bool                                    m_enable_dqm_byluminosity;
00287   const bool                                    m_enable_dqm_byls;
00288   const bool                                    m_enable_dqm_bynproc;
00289   bool                                          m_nproc_enabled;                // check if the plots by number of processes have been correctly enabled
00290   const double                                  m_dqm_eventtime_range;
00291   const double                                  m_dqm_eventtime_resolution;
00292   const double                                  m_dqm_pathtime_range;
00293   const double                                  m_dqm_pathtime_resolution;
00294   const double                                  m_dqm_moduletime_range;
00295   const double                                  m_dqm_moduletime_resolution;
00296   const double                                  m_dqm_luminosity_range;
00297   const double                                  m_dqm_luminosity_resolution;
00298   const uint32_t                                m_dqm_ls_range;
00299   const std::string                             m_dqm_path;
00300   const edm::InputTag                           m_luminosity_label;     // label of the per-Event luminosity EDProduct
00301   const std::vector<unsigned int>               m_supported_processes;  // possible number of concurrent processes
00302 
00303   // job configuration and caching
00304   std::string const *                           m_first_path;           // the framework does not provide a pre-paths or pre-endpaths signal,
00305   std::string const *                           m_last_path;            // so we emulate them keeping track of the first and last path and endpath
00306   std::string const *                           m_first_endpath;
00307   std::string const *                           m_last_endpath;
00308   bool                                          m_is_first_module;      // helper to measure the time spent between the beginning of the path and the execution of the first module
00309 
00310   // per-event accounting
00311   double                                        m_event;
00312   double                                        m_source;
00313   double                                        m_all_paths;
00314   double                                        m_all_endpaths;
00315   double                                        m_interpaths;
00316 
00317   // per-job summary
00318   unsigned int                                  m_summary_events;       // number of events
00319   double                                        m_summary_event;
00320   double                                        m_summary_source;
00321   double                                        m_summary_all_paths;
00322   double                                        m_summary_all_endpaths;
00323   double                                        m_summary_interpaths;
00324 
00325   // DQM
00326   DQMStore *                                    m_dqms;
00327 
00328   // event summary plots
00329   TH1F *                                        m_dqm_event;
00330   TH1F *                                        m_dqm_source;
00331   TH1F *                                        m_dqm_all_paths;
00332   TH1F *                                        m_dqm_all_endpaths;
00333   TH1F *                                        m_dqm_interpaths;
00334 
00335   // event summary plots - summed over nodes with the same number of processes
00336   TH1F *                                        m_dqm_nproc_event;
00337   TH1F *                                        m_dqm_nproc_source;
00338   TH1F *                                        m_dqm_nproc_all_paths;
00339   TH1F *                                        m_dqm_nproc_all_endpaths;
00340   TH1F *                                        m_dqm_nproc_interpaths;
00341 
00342   // plots by path
00343   TProfile *                                    m_dqm_paths_active_time;
00344   TProfile *                                    m_dqm_paths_total_time;
00345   TProfile *                                    m_dqm_paths_exclusive_time;
00346   TProfile *                                    m_dqm_paths_interpaths;
00347 
00348   // plots per lumisection
00349   TProfile *                                    m_dqm_byls_event;
00350   TProfile *                                    m_dqm_byls_source;
00351   TProfile *                                    m_dqm_byls_all_paths;
00352   TProfile *                                    m_dqm_byls_all_endpaths;
00353   TProfile *                                    m_dqm_byls_interpaths;
00354 
00355   // plots per lumisection - summed over nodes with the same number of processes
00356   TProfile *                                    m_dqm_nproc_byls_event;
00357   TProfile *                                    m_dqm_nproc_byls_source;
00358   TProfile *                                    m_dqm_nproc_byls_all_paths;
00359   TProfile *                                    m_dqm_nproc_byls_all_endpaths;
00360   TProfile *                                    m_dqm_nproc_byls_interpaths;
00361 
00362   // plots vs. instantaneous luminosity
00363   TProfile *                                    m_dqm_byluminosity_event;
00364   TProfile *                                    m_dqm_byluminosity_source;
00365   TProfile *                                    m_dqm_byluminosity_all_paths;
00366   TProfile *                                    m_dqm_byluminosity_all_endpaths;
00367   TProfile *                                    m_dqm_byluminosity_interpaths;
00368 
00369   // plots vs. instantaneous luminosity - summed over nodes with the same number of processes
00370   TProfile *                                    m_dqm_nproc_byluminosity_event;
00371   TProfile *                                    m_dqm_nproc_byluminosity_source;
00372   TProfile *                                    m_dqm_nproc_byluminosity_all_paths;
00373   TProfile *                                    m_dqm_nproc_byluminosity_all_endpaths;
00374   TProfile *                                    m_dqm_nproc_byluminosity_interpaths;
00375 
00376   // per-path and per-module accounting
00377   PathInfo *                                    m_current_path;
00378   PathMap<PathInfo>                             m_paths;
00379   ModuleMap<ModuleInfo>                         m_modules;              // this assumes that ModuleDescription are stored in the same object through the whole job,
00380                                                                         // which is true only *after* the edm::Worker constructors have run
00381   std::vector<PathInfo *>                       m_cache_paths;
00382   std::vector<ModuleInfo *>                     m_cache_modules;
00383 
00384 
00385   // timers
00386   std::pair<struct timespec, struct timespec>   m_timer_event;          // track time spent in each event
00387   std::pair<struct timespec, struct timespec>   m_timer_source;         // track time spent in the source
00388   std::pair<struct timespec, struct timespec>   m_timer_paths;          // track time spent in all paths
00389   std::pair<struct timespec, struct timespec>   m_timer_endpaths;       // track time spent in all endpaths
00390   std::pair<struct timespec, struct timespec>   m_timer_path;           // track time spent in each path
00391   std::pair<struct timespec, struct timespec>   m_timer_module;         // track time spent in each module
00392   struct timespec                               m_timer_first_module;   // record the start of the first active module in a path, if any
00393 
00394 #if defined(__APPLE__) || defined (__MACH__)
00395   clock_serv_t m_clock_port;
00396 #endif // defined(__APPLE__) || defined(__MACH__)
00397 
00398   void gettime(struct timespec & stamp) const
00399   {
00400 #if defined(_POSIX_TIMERS) && _POSIX_TIMERS >= 0
00401     clock_gettime(m_timer_id, & stamp);
00402 #else
00403 // special cases which do not support _POSIX_TIMERS
00404 #if defined(__APPLE__) || defined (__MACH__)
00405     mach_timespec_t timespec;
00406     clock_get_time(m_clock_port, &timespec);
00407     stamp.tv_sec  = timespec.tv_sec;
00408     stamp.tv_nsec = timespec.tv_nsec;
00409 #endif // defined(__APPLE__) || defined(__MACH__)
00410 #endif // defined(_POSIX_TIMERS) && _POSIX_TIMERS >= 0
00411   }
00412 
00413   void start(std::pair<struct timespec, struct timespec> & times) const
00414   {
00415     gettime(times.first);
00416   }
00417 
00418   void stop(std::pair<struct timespec, struct timespec> & times) const
00419   {
00420     gettime(times.second);
00421   }
00422 
00423   static
00424   double delta(const struct timespec & first, const struct timespec & second)
00425   {
00426     if (second.tv_nsec > first.tv_nsec)
00427       return (double) (second.tv_sec - first.tv_sec) + (double) (second.tv_nsec - first.tv_nsec) / (double) 1e9;
00428     else
00429       return (double) (second.tv_sec - first.tv_sec) - (double) (first.tv_nsec - second.tv_nsec) / (double) 1e9;
00430   }
00431 
00432   static
00433   double delta(const std::pair<struct timespec, struct timespec> & times)
00434   {
00435     return delta(times.first, times.second);
00436   }
00437 
00438   // find the module description associated to a module, by name
00439   edm::ModuleDescription const * findModuleDescription(const std::string & module) const;
00440 
00441   // associate to a path all the modules it contains
00442   void fillPathMap(std::string const & name, std::vector<std::string> const & modules);
00443 
00444 };
00445 
00446 #endif // ! FastTimerService_h