You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timing.hpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // timing.hpp
  2. //
  3. // Copyright (C) 2004-2009 MicroNeil Research Corporation.
  4. // This program is part of the MicroNeil Research Open Library Project. For
  5. // more information go to http://www.microneil.com/OpenLibrary/index.html
  6. //
  7. // This program is free software; you can redistribute it and/or modify it
  8. // under the terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 2 of the License, or (at your
  10. // option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful, but WITHOUT
  13. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. // more details.
  16. //
  17. // You should have received a copy of the GNU General Public License along with
  18. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  19. // Place, Suite 330, Boston, MA 02111-1307 USA
  20. // The purpose of this module is to abstract timing functions for
  21. // cross platform C++ development usning GNU compilers in *nix and
  22. // win32 environments (minGW). Timing resolution is in milliseconds
  23. // throughout to provide consistency and reasonable expectations.
  24. // 20060404 _M Added Timer::start(msclock startt) for chaining.
  25. // 20060403 _M This "timing" module has been completed and tested on
  26. // win32 (compiled using CodeBlocks and minGW) and on RHES3 (g++).
  27. //
  28. // The bottom line is that this code is perfect for most applications that
  29. // don't need real-time interaction on the win32 platform. That is, for
  30. // any application that can accept 15ms or so of "wiggle" in their timing
  31. // functions. On linux I was able to observe very consistent results with
  32. // variations measured in 1-2ms.
  33. //
  34. // Aynone seeking real-time accuracy on the win32 platform will need to contend
  35. // with all of the landmines in place against that and will need to write more
  36. // ellaborate versions of Timer::getLocalRawClock() and Sleeper::doRawSleep()
  37. // aa appropriate for their application. The existing code should work fine for
  38. // almost all other applications.
  39. //
  40. // This code was written with that in mind to some extent. That is why all of
  41. // the timing functions are measured in milliseconds rather than microseconds
  42. // or something smaller. Milliseconds are convenient for polling delays,
  43. // communications timeouts, measuring database application performance, and
  44. // other similar tasks. For that purpose - this timing module is just fine :-)
  45. // 20060323 _M Rewrote this module from a combination of previous
  46. // bits and pieces. This module will provide classes that abstract
  47. // timing functions for use in GNU projects on *nix and win32 systems.
  48. #pragma once
  49. namespace codedweller {
  50. ///////////////////////////////////////////////////////////////////////////////
  51. // class Sleeper - An object that remembers how long it is supposed to sleep.
  52. // This allows an application to create "standard" sleep timers that can be
  53. // established at the top of the code (easy to find) and reused.
  54. ///////////////////////////////////////////////////////////////////////////////
  55. static const int MinimumSleeperTime = 1; // Minimum msec allowed.
  56. static const int MaximumSleeperTime = 2000000000; // Maximum msec allowed.
  57. class Sleeper {
  58. private:
  59. int MillisecondsToSleep; // How long to sleep.
  60. void doRawSleep(int x); // Abstracted local sleep function.
  61. public:
  62. class BadSleeperValue {}; // Exception for bad values.
  63. Sleeper(); // Constructed empty - set to zero.
  64. Sleeper(int x); // Constructed with a value.
  65. int setMillisecondsToSleep(int x); // Safe way to set the vlaue.
  66. int getMillisecondsToSleep(); // Safe way to get the value.
  67. void sleep(); // Here's where we snooze if we can.
  68. void sleep(int x); // Shortcut - set the time and then sleep.
  69. void operator()();
  70. };
  71. /* Sleeper Documentation...
  72. **
  73. ** Sleeper.Sleeper()
  74. ** Constructs a Sleeper with a zero value.
  75. **
  76. ** Sleeper.Sleeper(int x)
  77. ** Constructs a Sleeper to "snooze" for x milliseconds.
  78. **
  79. ** Sleeper.setMillisecondsToSleep(int x)
  80. ** Sets the sleep time for the Sleeper and returns the time set.
  81. ** If the value is out of range then the Sleeper::BadSleeperValue will be thrown.
  82. **
  83. ** Sleeper.getMillisecondsToSleep()
  84. ** Returns the current MillisecondsToSleep.
  85. **
  86. ** Sleeper.sleep()
  87. ** Goes to sleep for MillisecondsToSleep. If MillisecondsToSleep has not been set
  88. ** then the function throws Sleeper::BadSleeperVlaue.
  89. **
  90. ** Sleeper.sleep(int x)
  91. ** First sets MillisecondsToSleep, then goes to sleep. If x is too big or too small
  92. ** then the method throws Sleeper::BadSleeperValue.
  93. */
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // class PollTimer - An object to pause during polling processes where the
  96. // time between polls is expanded according to a Fibonacci sequence. This
  97. // allows self organizing automata to relax a bit when a particular process
  98. // is taking a long time so that the resources used in the polling process are
  99. // reduced if the system is under load - The idea is to prevent the polling
  100. // process from loading the system when there are many nodes poling, yet to
  101. // allow for a rapid response when there are few or when the answer we're
  102. // waiting for is ready quickly. We use a Fibonacci expansion because it is
  103. // a natural spiral.
  104. ///////////////////////////////////////////////////////////////////////////////
  105. class PollTimer {
  106. private:
  107. Sleeper mySleeper; // We need a sleeper to do this.
  108. int NominalPollTime; // Normal poll delay in msec.
  109. int MaximumPollTime; // Maximum poll delay in msec.
  110. bool LimitReached;
  111. // Why not use unsigned int everywhere? Because sometimes libraries use
  112. // int for their Sleep() functions... so we calculate with unsigned ints,
  113. // but we use ints for inputs to keep things sane. Wierd bugs show up if
  114. // signed ints overflow in clock_t values -- this learned by experience.
  115. unsigned int FibA; // One poll delay ago.
  116. unsigned int FibB; // Two poll delays ago.
  117. // FibA and FibB are used to generate the fibonacci expansion. The current
  118. // delay will always be the sum of the previous two delays assuming that
  119. // there was always a first delay of 1 x Nominal Poll time. This results
  120. // in an expansion like this: 1,2,3,5,8,13,21,34,...
  121. public:
  122. class BadPollTimerValue {}; // Exception for bad values.
  123. PollTimer(int Nom, int Max); // Construct with nominal and max delays.
  124. int setNominalPollTime(int Nom); // Set the Nominal Poll Time.
  125. int setMaximumPollTime(int Max); // Set the Maximum Poll Time.
  126. void reset(); // Reset the spiral.
  127. int pause(); // Pause between polls.
  128. };
  129. /* PollTimer Documentation...
  130. **
  131. ** PollTimer(nominal_delay, maximum_delay)
  132. ** Constructs a PollTimer and sets it's basic parameters. If the parameters are
  133. ** out of range then BadPollTimerValue will be thrown.
  134. **
  135. ** setNiminalPollTime(Nom)
  136. ** Sets the nominal (base unit) poll delay time. Throws BadPollTimerValue if
  137. ** the value is out of range.
  138. **
  139. ** setMaximumPollTime(Max)
  140. ** Sets the maximum (upper limit) poll delay. If the value is out of range then
  141. ** BadPollTimerValue is thrown.
  142. **
  143. ** reset()
  144. ** Resets the current poll delay to the nominal delay. The next call to pause()
  145. ** will sleep for the nominal delay. This method would normally be called when
  146. ** a poll cycle turns up some work to do so that subsequent poll delays will be
  147. ** short - leading to a responsive system.
  148. **
  149. ** pause()
  150. ** Calling this method will cause the current thread to sleep for the current
  151. ** poll delay time. Subsquent calls to pause will cause longer sleep times
  152. ** according to a natural spiral. An intervening call to reset() will shorten
  153. ** the delay times again. This method returns the number of milliseconds
  154. ** paused on this pass.
  155. */
  156. ///////////////////////////////////////////////////////////////////////////////
  157. // class Timer - This one acts much like a stop watch with millisecond
  158. // resolution. The time is based on wall-clock time using gettimeofday() or
  159. // GetSystemTimeAsFileTime depending on the OS.
  160. ///////////////////////////////////////////////////////////////////////////////
  161. typedef unsigned long long int msclock; // 64 bit int used for measuring time in ms.
  162. static msclock EPOCH_DELTA_IN_USEC = 11644473600000000ULL; // Microseconds difference between epochs.
  163. static msclock EPOCH_DELTA_IN_MSEC = EPOCH_DELTA_IN_USEC / 1000; // Milliseconds difference between epochs.
  164. class Timer {
  165. private:
  166. bool RunningFlag; // True if clock is running.
  167. msclock StartTime; // TimeOfDay at start.
  168. msclock StopTime; // TimeOfDay at stop or check.
  169. msclock getLocalRawClock() const; // Derives unix epoch ms from local clock.
  170. public:
  171. Timer(); // Construct and start the Timer.
  172. Timer(msclock startt); // Constructs and starts from a specific moment.
  173. void clear(); // Stop and set elapsed time to zero at now.
  174. msclock start(); // Re(set) the Start time to this moment.
  175. msclock start(msclock startt); // Re(set) the Start time to startt.
  176. msclock getStartClock(); // Return the unix epoch start clock.
  177. bool isRunning(); // Return true if the clock is running.
  178. msclock getElapsedTime() const; // get milliseconds since Timer start.
  179. msclock stop(); // Stop the Timer.
  180. msclock getStopClock(); // Return the unix epoch stop clock.
  181. double getElapsedSeconds()const; // Get floating point elapsed seconds.
  182. bool isUnixBased(); // True if base clock is unix/posix.
  183. msclock toWindowsEpoch(msclock unixt); // Converts unix t to windows t.
  184. msclock toUnixEpoch(msclock win32t); // Converts windows t to unix t.
  185. };
  186. /* Timer Documentation...
  187. **
  188. ** All raw clock values are returned as 64 bit unsigned integers using a special
  189. ** type - msclock. Conversions are done using microsecond accuracy.
  190. **
  191. ** Timer()
  192. ** Creates a new timer and starts the clock at this moment.
  193. **
  194. ** Timer(msclock startt)
  195. ** Creates a new timer and starts the clock at a specific moment. This can be
  196. ** used to start one clock precisely when another one ends as in:
  197. ** new Timer B(A.stop());
  198. **
  199. ** getLocalRawClock()
  200. ** This method uses slightly different code depending upon whether the system
  201. ** is a unix box or win32. In both cases the function determines the local time
  202. ** value as a 64bit integer with millisecond resolution using the unix epoch of
  203. ** Jan 1, 1970.
  204. **
  205. ** start()
  206. ** This method starts or restarts the Timer's clock at this moment.
  207. ** The msclock value for the start clock is returned.
  208. **
  209. ** start(msclock startt)
  210. ** This method starts or restarts the Timer's clock at the time specified
  211. ** int startt. This is used for chaining operations such as B.start(A.stop())
  212. **
  213. ** getStartClock()
  214. ** This method returns the start clock value.
  215. **
  216. ** isRunning()
  217. ** Returns true if the clock is running.
  218. **
  219. ** getElapsedTime()
  220. ** This method returns the elapsed time in milliseconds.
  221. ** If the clock is running this value will be different each time it is called.
  222. **
  223. ** stop()
  224. ** This method stops the clock and returns the stop clock value. If this method
  225. ** is called more than once then the stop clock is reset to the current time and
  226. ** that time is returned.
  227. **
  228. ** getStopClock()
  229. ** This method returns the stop clock value. If the Timer is still running
  230. ** then the result is the same as calling getElapsedTime(). If the clock is
  231. ** not running then the time the clock was last stopped is returned.
  232. **
  233. ** getElapsedSeconds()
  234. ** Returns the elapsed time as a floating point number with millisecond
  235. ** resolution.
  236. **
  237. ** isUnixBased()
  238. ** Returns true if the raw clock values are being derived from a unix/posix OS.
  239. **
  240. ** toWindowsEpoch(msclock unixt)
  241. ** Converts unixt to a windows value by adding the epoch delta.
  242. **
  243. ** toUnixEpoch(msclock win32t)
  244. ** Converts win32t to a unix value by subtracting the epoch delta.
  245. */
  246. ////////////////////////////////////////////////////////////////////////////////
  247. // class ScopeTimer - Runs a Timer while ScopeTimer is in scope.
  248. ////////////////////////////////////////////////////////////////////////////////
  249. class ScopeTimer { // Runs a timer when in scope.
  250. private:
  251. Timer& myTimer; // This is the timer to run.
  252. public:
  253. ScopeTimer(Timer& T) : myTimer(T) { myTimer.start(); } // The Timer starts at construction.
  254. ~ScopeTimer() { myTimer.stop(); } // The Timer stops at destruction.
  255. };
  256. ///////////////////////////////////////////////////////////////////////////////
  257. // class Timeout - This one uses a Timer to establish a timeout value.
  258. ///////////////////////////////////////////////////////////////////////////////
  259. class Timeout {
  260. private:
  261. Timer myTimer; // We need a timer to do this.
  262. msclock myDuration; // Milliseconds before timout expires.
  263. public:
  264. class BadTimeoutValue {}; // If the value is bad throw this.
  265. Timeout(msclock duration); // Create and set the duration.
  266. msclock setDuration(msclock duration); // Set/Change the duration in milliseconds.
  267. msclock getDuration(); // Return the current duration in milliseconds.
  268. msclock restart(); // Restart the timeout timer.
  269. msclock getElapsedTime(); // Get elapsed milliseconds.
  270. msclock getRemainingTime(); // Get remaining milliseconds.
  271. bool isExpired(); // Return true if time is up.
  272. };
  273. /* Timeout Documentation...
  274. **
  275. ** Timeout(int duration)
  276. ** Creates a Timout timer and sets the duration in milliseconds.
  277. **
  278. ** setDuration(int duration)
  279. ** Sets or changes the duration of the timeout timer.
  280. ** The Timout is NOT reset by this method. This allows you to change
  281. ** the timeout on the fly.
  282. **
  283. ** restart()
  284. ** Restarts the timeout timer.
  285. **
  286. ** getElapsedTime()
  287. ** Returns the number of milliseconds elapsed since the Timout was created
  288. ** or reset.
  289. **
  290. ** getRemainingTime()
  291. ** Returns the number of milliseconds remaining before time is up.
  292. **
  293. ** isExpired()
  294. ** Returns true if time is up.
  295. */
  296. } // End namespace codedweller