/indra/llcommon/lltimer.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 168 lines · 92 code · 30 blank · 46 comment · 9 complexity · ef65eba1aeed8e53c3cc721a93e8db24 MD5 · raw file

  1. /**
  2. * @file lltimer.h
  3. * @brief Cross-platform objects for doing timing
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_TIMER_H
  27. #define LL_TIMER_H
  28. #if LL_LINUX || LL_DARWIN || LL_SOLARIS
  29. #include <sys/time.h>
  30. #endif
  31. #include <limits.h>
  32. #include "stdtypes.h"
  33. #include <string>
  34. #include <list>
  35. // units conversions
  36. #ifndef USEC_PER_SEC
  37. const U32 USEC_PER_SEC = 1000000;
  38. #endif
  39. const U32 SEC_PER_MIN = 60;
  40. const U32 MIN_PER_HOUR = 60;
  41. const U32 USEC_PER_MIN = USEC_PER_SEC * SEC_PER_MIN;
  42. const U32 USEC_PER_HOUR = USEC_PER_MIN * MIN_PER_HOUR;
  43. const U32 SEC_PER_HOUR = SEC_PER_MIN * MIN_PER_HOUR;
  44. const F64 SEC_PER_USEC = 1.0 / (F64) USEC_PER_SEC;
  45. class LL_COMMON_API LLTimer
  46. {
  47. public:
  48. static LLTimer *sTimer; // global timer
  49. protected:
  50. U64 mLastClockCount;
  51. U64 mExpirationTicks;
  52. BOOL mStarted;
  53. public:
  54. LLTimer();
  55. ~LLTimer();
  56. static void initClass() { if (!sTimer) sTimer = new LLTimer; }
  57. static void cleanupClass() { delete sTimer; sTimer = NULL; }
  58. // Return a high precision number of seconds since the start of
  59. // this application instance.
  60. static F64 getElapsedSeconds()
  61. {
  62. return sTimer->getElapsedTimeF64();
  63. }
  64. // Return a high precision usec since epoch
  65. static U64 getTotalTime();
  66. // Return a high precision seconds since epoch
  67. static F64 getTotalSeconds();
  68. // MANIPULATORS
  69. void start() { reset(); mStarted = TRUE; }
  70. void stop() { mStarted = FALSE; }
  71. void reset(); // Resets the timer
  72. void setLastClockCount(U64 current_count); // Sets the timer so that the next elapsed call will be relative to this time
  73. void setTimerExpirySec(F32 expiration);
  74. BOOL checkExpirationAndReset(F32 expiration);
  75. BOOL hasExpired() const;
  76. F32 getElapsedTimeAndResetF32(); // Returns elapsed time in seconds with reset
  77. F64 getElapsedTimeAndResetF64();
  78. F32 getRemainingTimeF32() const;
  79. static BOOL knownBadTimer();
  80. // ACCESSORS
  81. F32 getElapsedTimeF32() const; // Returns elapsed time in seconds
  82. F64 getElapsedTimeF64() const; // Returns elapsed time in seconds
  83. BOOL getStarted() const { return mStarted; }
  84. static U64 getCurrentClockCount(); // Returns the raw clockticks
  85. };
  86. //
  87. // Various functions for initializing/accessing clock and timing stuff. Don't use these without REALLY knowing how they work.
  88. //
  89. LL_COMMON_API U64 get_clock_count();
  90. LL_COMMON_API F64 calc_clock_frequency(U32 msecs);
  91. LL_COMMON_API void update_clock_frequencies();
  92. // Sleep for milliseconds
  93. LL_COMMON_API void ms_sleep(U32 ms);
  94. LL_COMMON_API U32 micro_sleep(U64 us, U32 max_yields = 0xFFFFFFFF);
  95. // Returns the correct UTC time in seconds, like time(NULL).
  96. // Useful on the viewer, which may have its local clock set wrong.
  97. LL_COMMON_API time_t time_corrected();
  98. static inline time_t time_min()
  99. {
  100. if (sizeof(time_t) == 4)
  101. {
  102. return (time_t) INT_MIN;
  103. } else {
  104. #ifdef LLONG_MIN
  105. return (time_t) LLONG_MIN;
  106. #else
  107. return (time_t) LONG_MIN;
  108. #endif
  109. }
  110. }
  111. static inline time_t time_max()
  112. {
  113. if (sizeof(time_t) == 4)
  114. {
  115. return (time_t) INT_MAX;
  116. } else {
  117. #ifdef LLONG_MAX
  118. return (time_t) LLONG_MAX;
  119. #else
  120. return (time_t) LONG_MAX;
  121. #endif
  122. }
  123. }
  124. // Correction factor used by time_corrected() above.
  125. extern LL_COMMON_API S32 gUTCOffset;
  126. // Is the current computer (in its current time zone)
  127. // observing daylight savings time?
  128. LL_COMMON_API BOOL is_daylight_savings();
  129. // Converts internal "struct tm" time buffer to Pacific Standard/Daylight Time
  130. // Usage:
  131. // S32 utc_time;
  132. // utc_time = time_corrected();
  133. // struct tm* internal_time = utc_to_pacific_time(utc_time, gDaylight);
  134. LL_COMMON_API struct tm* utc_to_pacific_time(time_t utc_time, BOOL pacific_daylight_time);
  135. LL_COMMON_API void microsecondsToTimecodeString(U64 current_time, std::string& tcstring);
  136. LL_COMMON_API void secondsToTimecodeString(F32 current_time, std::string& tcstring);
  137. U64 LL_COMMON_API totalTime(); // Returns current system time in microseconds
  138. #endif