OKVIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2  * OKVIS - Open Keyframe-based Visual-Inertial SLAM
3  * Copyright (c) 2015, Autonomous Systems Lab / ETH Zurich
4  *
5  * Copyright (c) 2011-2013, Paul Furgale and others.
6  * All rights reserved.
7  *
8  * Unlike otherwise stated in source files, the code in this repository is
9  * published under the Revised BSD (New BSD) license.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * * Neither the name of the <organization> nor the
19  * names of its contributors may be used to endorse or promote products
20  * derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *********************************************************************************/
33 
34  /*
35  * Modified: Stefan Leutenegger (s.leutenegger@imperial.ac.uk)
36  * Modified: Andreas Forster (an.forster@gmail.com)
37  */
38 
39 #ifndef INCLUDE_OKVIS_TIMING_TIMER_HPP_
40 #define INCLUDE_OKVIS_TIMING_TIMER_HPP_
41 
42 #ifndef BOOST_DATE_TIME_NO_LOCALE
43 #define BOOST_DATE_TIME_NO_LOCALE
44 #include <boost/date_time/posix_time/posix_time.hpp>
45 #undef BOOST_DATE_TIME_NO_LOCALE
46 #else
47 #include <boost/date_time/posix_time/posix_time.hpp>
48 #endif
49 
50 #include <mutex>
51 #include <boost/accumulators/accumulators.hpp>
52 #include <boost/accumulators/statistics.hpp>
53 #include <boost/accumulators/statistics/rolling_mean.hpp>
54 #include <unordered_map>
55 #include <vector>
56 
57 #include <okvis/assert_macros.hpp>
58 
59 #ifdef _WIN32
60 #define OKVIS_USE_HIGH_PERF_TIMER
61 #include <windows.h>
62 #endif
63 
64 
65 namespace okvis {
66 namespace timing {
67 
68  OKVIS_DEFINE_EXCEPTION(TimerException, std::runtime_error)
69 
70  struct TimerMapValue {
71  // Initialize the window size for the rolling mean.
72  TimerMapValue() : m_acc(boost::accumulators::tag::rolling_window::window_size = 50){}
73  boost::accumulators::accumulator_set<
74  double,
75  boost::accumulators::features<
76  boost::accumulators::tag::lazy_variance,
77  boost::accumulators::tag::sum,
78  boost::accumulators::tag::min,
79  boost::accumulators::tag::max,
80  boost::accumulators::tag::rolling_mean,
81  boost::accumulators::tag::mean
82  >
83  > m_acc;
84  };
85 
86  // A class that has the timer interface but does nothing.
87  // Swapping this in in place of the Timer class (say with a
88  // typedef) should allow one to disable timing. Because all
89  // of the functions are inline, they should just disappear.
90  class DummyTimer {
91  public:
92  DummyTimer(size_t /* handle */, bool /* constructStopped */ ){}
93  DummyTimer(size_t /* handle */){}
94  DummyTimer(std::string const & /* tag */){}
95  DummyTimer(std::string const & /* tag */, bool /* constructStopped */){}
97 
98  void start(){}
99  void stop(){}
100  void discardTiming(){}
101  bool isTiming(){ return false; }
102  };
103 
104  class Timer {
105  public:
106  Timer(size_t handle, bool constructStopped = false);
107  Timer(std::string const & tag, bool constructStopped = false);
108  ~Timer();
109 
110  void start();
111  void stop();
112  bool isTiming();
113  void discardTiming();
114  private:
115 #ifdef OKVIS_USE_HIGH_PERF_TIMER
116  LARGE_INTEGER m_time;
117 #else
118  boost::posix_time::ptime m_time;
119 #endif
120  bool m_timing;
121  size_t m_handle;
122  };
123 
124  class Timing{
125  public:
126  friend class Timer;
127  // Static funcitons to query the timers:
128  static size_t getHandle(std::string const & tag);
129  static std::string getTag(size_t handle);
130  static double getTotalSeconds(size_t handle);
131  static double getTotalSeconds(std::string const & tag);
132  static double getMeanSeconds(size_t handle);
133  static double getMeanSeconds(std::string const & tag);
134  static size_t getNumSamples(size_t handle);
135  static size_t getNumSamples(std::string const & tag);
136  static double getVarianceSeconds(size_t handle);
137  static double getVarianceSeconds(std::string const & tag);
138  static double getMinSeconds(size_t handle);
139  static double getMinSeconds(std::string const & tag);
140  static double getMaxSeconds(size_t handle);
141  static double getMaxSeconds(std::string const & tag);
142  static double getHz(size_t handle);
143  static double getHz(std::string const & tag);
144  static void print(std::ostream & out);
145  static void reset(size_t handle);
146  static void reset(std::string const & tag);
147  static std::string print();
148  static std::string secondsToTimeString(double seconds);
149 
150  private:
151  void addTime(size_t handle, double seconds);
152 
153  static Timing & instance();
154 
155  // Singleton design pattern
156  Timing();
157  ~Timing();
158 
159  typedef std::unordered_map<std::string,size_t> map_t;
160  typedef std::vector<TimerMapValue> list_t;
161 
162  std::mutex addNewHandleMutex_;
163 
164  // Static members
167 #ifdef OKVIS_USE_HIGH_PERF_TIMER
168  double m_clockPeriod;
169 #endif
171 
172  }; // end class timer
173 
174 #ifdef NDEBUG
175  typedef DummyTimer DebugTimer;
176 #else
177  typedef Timer DebugTimer;
178 #endif
179 
180 } // namespace timing
181 } // end namespace sm
182 
183 #endif // INCLUDE_OKVIS_TIMING_TIMER_HPP_
TimerMapValue()
Definition: Timer.hpp:72
void start()
Definition: Timer.hpp:98
static double getMeanSeconds(size_t handle)
Definition: Timer.cpp:169
DummyTimer(std::string const &)
Definition: Timer.hpp:94
static double getMaxSeconds(size_t handle)
Definition: Timer.cpp:197
void start()
Definition: Timer.cpp:120
~DummyTimer()
Definition: Timer.hpp:96
static std::string print()
Definition: Timer.cpp:274
static size_t getHandle(std::string const &tag)
Definition: Timer.cpp:62
void stop()
Definition: Timer.hpp:99
static std::string secondsToTimeString(double seconds)
Definition: Timer.cpp:226
DummyTimer(size_t, bool)
Definition: Timer.hpp:92
~Timer()
Definition: Timer.cpp:115
static double getVarianceSeconds(size_t handle)
Definition: Timer.cpp:183
~Timing()
Definition: Timer.cpp:57
static double getTotalSeconds(size_t handle)
Definition: Timer.cpp:162
static double getHz(size_t handle)
Definition: Timer.cpp:205
void discardTiming()
Definition: Timer.hpp:100
list_t m_timers
Definition: Timer.hpp:165
static void reset(size_t handle)
Definition: Timer.cpp:216
boost::accumulators::accumulator_set< double, boost::accumulators::features< boost::accumulators::tag::lazy_variance, boost::accumulators::tag::sum, boost::accumulators::tag::min, boost::accumulators::tag::max, boost::accumulators::tag::rolling_mean, boost::accumulators::tag::mean > > m_acc
Definition: Timer.hpp:83
Definition: Timer.hpp:104
Definition: Timer.hpp:90
Timer(size_t handle, bool constructStopped=false)
Definition: Timer.cpp:98
static size_t getNumSamples(size_t handle)
Definition: Timer.cpp:176
DummyTimer(size_t)
Definition: Timer.hpp:93
#define OKVIS_DEFINE_EXCEPTION(exceptionName, exceptionParent)
Macro for defining an exception with a given parent.
Definition: assert_macros.hpp:52
This file contains some useful assert macros.
boost::posix_time::ptime m_time
Definition: Timer.hpp:118
std::mutex addNewHandleMutex_
Definition: Timer.hpp:162
Timer DebugTimer
Definition: Timer.hpp:177
Definition: Timer.hpp:70
Timing()
Definition: Timer.cpp:46
void addTime(size_t handle, double seconds)
Definition: Timer.cpp:158
Definition: Timer.hpp:124
std::vector< TimerMapValue > list_t
Definition: Timer.hpp:160
static Timing & instance()
Definition: Timer.cpp:41
DummyTimer(std::string const &, bool)
Definition: Timer.hpp:95
map_t m_tagMap
Definition: Timer.hpp:166
void stop()
Definition: Timer.cpp:130
bool isTiming()
Definition: Timer.hpp:101
static std::string getTag(size_t handle)
Definition: Timer.cpp:79
size_t m_handle
Definition: Timer.hpp:121
void discardTiming()
Definition: Timer.cpp:152
bool isTiming()
Definition: Timer.cpp:147
static double getMinSeconds(size_t handle)
Definition: Timer.cpp:190
std::unordered_map< std::string, size_t > map_t
Definition: Timer.hpp:159
bool m_timing
Definition: Timer.hpp:120
size_t m_maxTagLength
Definition: Timer.hpp:170