lltimer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * @file lltimer.h
  3. * @brief Cross-platform objects for doing timing
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_TIMER_H
  33. #define LL_TIMER_H
  34. #if LL_LINUX || LL_DARWIN
  35. #include <sys/time.h>
  36. #endif
  37. #include <limits.h>
  38. #include <list>
  39. #include <string>
  40. #include "llcommonmath.h"
  41. #include "llerror.h"
  42. // Time units conversions
  43. #ifndef USEC_PER_SEC
  44. constexpr U32 USEC_PER_SEC = 1000000;
  45. #endif
  46. constexpr U32 SEC_PER_MIN = 60;
  47. constexpr U32 MIN_PER_HOUR = 60;
  48. constexpr U32 USEC_PER_MIN = USEC_PER_SEC * SEC_PER_MIN;
  49. constexpr U32 SEC_PER_DAY = 86400;
  50. constexpr U32 USEC_PER_HOUR = USEC_PER_MIN * MIN_PER_HOUR;
  51. constexpr U32 SEC_PER_HOUR = SEC_PER_MIN * MIN_PER_HOUR;
  52. constexpr F64 SEC_PER_USEC = 1.0 / (F64)USEC_PER_SEC;
  53. constexpr F32 SEC_TO_MICROSEC = 1000000.f;
  54. constexpr U64 SEC_TO_MICROSEC_U64 = 1000000;
  55. class LLTimer
  56. {
  57. protected:
  58. LOG_CLASS(LLTimer);
  59. public:
  60. LLTimer();
  61. static void initClass();
  62. static void cleanupClass();
  63. // Returns the number of seconds elapsed since UNIX epoch with a milli-
  64. // second resolution.
  65. static F64 getEpochSeconds();
  66. static U64 getCurrentClockCount(); // Returns the raw clockticks
  67. // Returns a high precision micro-seconds time (usually since computer boot
  68. // up time).
  69. static U64 totalTime();
  70. // Returns a high precision seconds time (usually since computer boot up
  71. // time).
  72. LL_INLINE static F64 getTotalSeconds()
  73. {
  74. constexpr F64 USEC_TO_SEC_F64 = 0.000001;
  75. return U64_to_F64(totalTime()) * USEC_TO_SEC_F64;
  76. }
  77. // Returns a high precision number of seconds since the start of this
  78. // application instance.
  79. LL_INLINE static F64 getElapsedSeconds()
  80. {
  81. return sTimer->getElapsedTimeF64();
  82. }
  83. LL_INLINE void start() { reset(); mStarted = true; }
  84. LL_INLINE void stop() { mStarted = false; }
  85. LL_INLINE void reset()
  86. {
  87. mLastClockCount = getCurrentClockCount();
  88. mExpirationTicks = 0;
  89. }
  90. // Sets the timer so that the next elapsed call will be relative to this
  91. // time:
  92. LL_INLINE void setLastClockCount(U64 current_count)
  93. {
  94. mLastClockCount = current_count;
  95. }
  96. void setTimerExpirySec(F32 expiration);
  97. bool checkExpirationAndReset(F32 expiration);
  98. LL_INLINE bool hasExpiration() const { return mExpirationTicks > 0; }
  99. LL_INLINE bool hasExpired() const { return getCurrentClockCount() >= mExpirationTicks; }
  100. LL_INLINE bool getStarted() const { return mStarted; }
  101. // These methods return the elapsed time in seconds
  102. F64 getElapsedTimeF64() const;
  103. LL_INLINE F32 getElapsedTimeF32() const { return (F32)getElapsedTimeF64(); }
  104. // These methods return the remaining time in seconds
  105. F64 getRemainingTimeF64() const;
  106. LL_INLINE F32 getRemainingTimeF32() const { return (F32)getRemainingTimeF64(); }
  107. // These methods return the elapsed time in seconds and reset the timer
  108. F64 getElapsedTimeAndResetF64();
  109. LL_INLINE F32 getElapsedTimeAndResetF32() { return (F32)getElapsedTimeAndResetF64(); }
  110. private:
  111. static U64 getElapsedTimeAndUpdate(U64& last_clock_count);
  112. public:
  113. static LLTimer* sTimer; // Global timer
  114. protected:
  115. U64 mLastClockCount;
  116. U64 mExpirationTicks;
  117. bool mStarted;
  118. };
  119. //
  120. // Various functions for initializing/accessing clock and timing stuff. Do not
  121. // use these without REALLY knowing how they work.
  122. //
  123. void update_clock_frequencies();
  124. // Sleep for milliseconds
  125. void ms_sleep(U32 ms);
  126. // Returns the correct UTC time in seconds, like time(NULL).
  127. // Useful on the viewer, which may have its local clock set wrong.
  128. time_t time_corrected();
  129. // Returns the computer (local) time in seconds, like time(NULL).
  130. time_t computer_time();
  131. static LL_INLINE time_t time_min()
  132. {
  133. if (sizeof(time_t) == 4)
  134. {
  135. return (time_t) INT_MIN;
  136. }
  137. else
  138. {
  139. #ifdef LLONG_MIN
  140. return (time_t) LLONG_MIN;
  141. #else
  142. return (time_t) LONG_MIN;
  143. #endif
  144. }
  145. }
  146. static LL_INLINE time_t time_max()
  147. {
  148. if (sizeof(time_t) == 4)
  149. {
  150. return (time_t) INT_MAX;
  151. }
  152. else
  153. {
  154. #ifdef LLONG_MAX
  155. return (time_t) LLONG_MAX;
  156. #else
  157. return (time_t) LONG_MAX;
  158. #endif
  159. }
  160. }
  161. // Correction factor used by time_corrected() above.
  162. extern S32 gUTCOffset;
  163. // Converts internal "struct tm" time buffer to UTC
  164. // Usage:
  165. // S32 utc_time;
  166. // utc_time = time_corrected();
  167. // struct tm* internal_time = utc_time_to_tm(utc_time);
  168. struct tm* utc_time_to_tm(time_t utc_time);
  169. // Converts internal "struct tm" time buffer to local time
  170. // Usage:
  171. // S32 local_time;
  172. // local_time = computer_time();
  173. // struct tm* internal_time = local_time_to_tm(local_time);
  174. struct tm* local_time_to_tm(time_t local_time);
  175. // Converts internal "struct tm" time buffer to Pacific Standard/Daylight Time
  176. // Usage:
  177. // S32 utc_time;
  178. // utc_time = time_corrected();
  179. // struct tm* internal_time = utc_to_pacific_time(utc_time, gDaylight);
  180. struct tm* utc_to_pacific_time(time_t utc_time, bool pacific_daylight_time);
  181. void microsecondsToTimecodeString(U64 current_time, std::string& tcstring);
  182. void secondsToTimecodeString(F32 current_time, std::string& tcstring);
  183. void timeToFormattedString(time_t time, const char* format,
  184. std::string& timestr);
  185. void timeStructToFormattedString(struct tm* time, const std::string& format,
  186. std::string& timestr);
  187. #endif