CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/HLTrigger/Timer/interface/TimerService.h

Go to the documentation of this file.
00001 #ifndef Timer_Service_
00002 #define Timer_Service_
00003 
00013 #include "sigc++/signal.h"
00014 
00015 #include "FWCore/Utilities/interface/CPUTimer.h"
00016 
00017 #include "FWCore/Framework/interface/Frameworkfwd.h"
00018 #include "FWCore/Framework/interface/Event.h"
00019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00020 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00021 
00022 #include <string>
00023 
00024 #ifdef __linux
00025 #include <time.h>
00026 #else
00027 typedef int clockid_t;
00028 #define CLOCK_REALTIME               0                                                                                                                                                                                        
00029 #define CLOCK_MONOTONIC              1                                                                                                                                                                                        
00030 #define CLOCK_PROCESS_CPUTIME_ID     2                                                                                                                                                                                        
00031 #define CLOCK_THREAD_CPUTIME_ID      3                                                                                                                                                                                        
00032 #endif
00033 
00034 namespace hlt {
00035 
00036   class CPUTimer {
00037   public:
00038     explicit CPUTimer(bool cpu = true) :
00039       timer_( cpu ? CLOCK_THREAD_CPUTIME_ID : CLOCK_REALTIME )
00040     {
00041       reset();
00042     }
00043 
00044     void reset() {
00045       start_.tv_sec  = 0;
00046       start_.tv_nsec = 0;
00047       stop_.tv_sec   = 0;
00048       stop_.tv_nsec  = 0;
00049     }
00050 
00051     void start() {
00052 #ifdef __linux
00053       clock_gettime(timer_, & start_);
00054 #endif
00055     }
00056 
00057     void stop() {
00058 #ifdef __linux
00059       clock_gettime(timer_, & stop_);
00060 #endif
00061     }
00062 
00063   // return the delta between start and stop in seconds
00064   double delta() const {
00065     if (stop_.tv_nsec > start_.tv_nsec)
00066       return (double) (stop_.tv_sec - start_.tv_sec) + (double) (stop_.tv_nsec - start_.tv_nsec) / (double) 1e9;
00067     else
00068       return (double) (stop_.tv_sec - start_.tv_sec) - (double) (start_.tv_nsec - stop_.tv_nsec) / (double) 1e9;
00069   }
00070 
00071   private:
00072     const clockid_t timer_;
00073     timespec        start_; 
00074     timespec        stop_;
00075   };
00076 
00077 } // namespace hlt
00078 
00079 
00080 class TimerService {
00081  public:
00082   TimerService(const edm::ParameterSet&, edm::ActivityRegistry& iAR);
00083   ~TimerService();
00084 
00085   // signal with module-description and processing time (in secs)
00086   sigc::signal<void, const edm::ModuleDescription&, double> newMeasurementSignal;
00087 
00088   // fwk calls this method before a module is processed
00089   void preModule(const edm::ModuleDescription& iMod);
00090   // fwk calls this method after a module has been processed
00091   void postModule(const edm::ModuleDescription& iMod);
00092 
00093  private:
00094   // whether to use CPU-time (default) or wall-clock time
00095   bool useCPUtime;
00096 
00097   // cpu-timer
00098   hlt::CPUTimer cpu_timer;
00099 
00100   // true is the process is bound to a single CPU
00101   bool is_bound_;
00102 };
00103 
00104 #endif // #define Timer_Service_