Timer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/CoreEvents.h"
  24. #include "../Core/Profiler.h"
  25. #include <ctime>
  26. #ifdef WIN32
  27. #include <windows.h>
  28. #include <mmsystem.h>
  29. #else
  30. #include <sys/time.h>
  31. #include <unistd.h>
  32. #endif
  33. #include "../DebugNew.h"
  34. namespace Atomic
  35. {
  36. bool HiresTimer::supported(false);
  37. long long HiresTimer::frequency(1000);
  38. Time::Time(Context* context) :
  39. Object(context),
  40. frameNumber_(0),
  41. timeStep_(0.0f),
  42. timerPeriod_(0)
  43. {
  44. #ifdef WIN32
  45. LARGE_INTEGER frequency;
  46. if (QueryPerformanceFrequency(&frequency))
  47. {
  48. HiresTimer::frequency = frequency.QuadPart;
  49. HiresTimer::supported = true;
  50. }
  51. #else
  52. HiresTimer::frequency = 1000000;
  53. HiresTimer::supported = true;
  54. #endif
  55. }
  56. Time::~Time()
  57. {
  58. SetTimerPeriod(0);
  59. }
  60. void Time::BeginFrame(float timeStep)
  61. {
  62. ++frameNumber_;
  63. if (!frameNumber_)
  64. ++frameNumber_;
  65. timeStep_ = timeStep;
  66. Profiler* profiler = GetSubsystem<Profiler>();
  67. if (profiler)
  68. profiler->BeginFrame();
  69. {
  70. PROFILE(BeginFrame);
  71. // Frame begin event
  72. using namespace BeginFrame;
  73. VariantMap& eventData = GetEventDataMap();
  74. eventData[P_FRAMENUMBER] = frameNumber_;
  75. eventData[P_TIMESTEP] = timeStep_;
  76. SendEvent(E_BEGINFRAME, eventData);
  77. }
  78. }
  79. void Time::EndFrame()
  80. {
  81. {
  82. PROFILE(EndFrame);
  83. // Frame end event
  84. SendEvent(E_ENDFRAME);
  85. }
  86. Profiler* profiler = GetSubsystem<Profiler>();
  87. if (profiler)
  88. profiler->EndFrame();
  89. }
  90. void Time::SetTimerPeriod(unsigned mSec)
  91. {
  92. #ifdef WIN32
  93. if (timerPeriod_ > 0)
  94. timeEndPeriod(timerPeriod_);
  95. timerPeriod_ = mSec;
  96. if (timerPeriod_ > 0)
  97. timeBeginPeriod(timerPeriod_);
  98. #endif
  99. }
  100. float Time::GetElapsedTime()
  101. {
  102. return elapsedTime_.GetMSec(false) / 1000.0f;
  103. }
  104. unsigned Time::GetSystemTime()
  105. {
  106. #ifdef WIN32
  107. unsigned currentTime = (unsigned)timeGetTime();
  108. #else
  109. struct timeval time;
  110. gettimeofday(&time, NULL);
  111. unsigned currentTime = (unsigned)(time.tv_sec * 1000 + time.tv_usec / 1000);
  112. #endif
  113. return currentTime;
  114. }
  115. unsigned Time::GetTimeSinceEpoch()
  116. {
  117. return (unsigned)time(NULL);
  118. }
  119. String Time::GetTimeStamp()
  120. {
  121. time_t sysTime;
  122. time(&sysTime);
  123. const char* dateTime = ctime(&sysTime);
  124. return String(dateTime).Replaced("\n", "");
  125. }
  126. void Time::Sleep(unsigned mSec)
  127. {
  128. #ifdef WIN32
  129. ::Sleep(mSec);
  130. #else
  131. usleep(mSec * 1000);
  132. #endif
  133. }
  134. Timer::Timer()
  135. {
  136. Reset();
  137. }
  138. unsigned Timer::GetMSec(bool reset)
  139. {
  140. #ifdef WIN32
  141. unsigned currentTime = (unsigned)timeGetTime();
  142. #else
  143. struct timeval time;
  144. gettimeofday(&time, NULL);
  145. unsigned currentTime = (unsigned)(time.tv_sec * 1000 + time.tv_usec / 1000);
  146. #endif
  147. unsigned elapsedTime = currentTime - startTime_;
  148. if (reset)
  149. startTime_ = currentTime;
  150. return elapsedTime;
  151. }
  152. void Timer::Reset()
  153. {
  154. #ifdef WIN32
  155. startTime_ = (unsigned)timeGetTime();
  156. #else
  157. struct timeval time;
  158. gettimeofday(&time, NULL);
  159. startTime_ = (unsigned)(time.tv_sec * 1000 + time.tv_usec / 1000);
  160. #endif
  161. }
  162. HiresTimer::HiresTimer()
  163. {
  164. Reset();
  165. }
  166. long long HiresTimer::GetUSec(bool reset)
  167. {
  168. long long currentTime;
  169. #ifdef WIN32
  170. if (supported)
  171. {
  172. LARGE_INTEGER counter;
  173. QueryPerformanceCounter(&counter);
  174. currentTime = counter.QuadPart;
  175. }
  176. else
  177. currentTime = timeGetTime();
  178. #else
  179. struct timeval time;
  180. gettimeofday(&time, NULL);
  181. currentTime = time.tv_sec * 1000000LL + time.tv_usec;
  182. #endif
  183. long long elapsedTime = currentTime - startTime_;
  184. // Correct for possible weirdness with changing internal frequency
  185. if (elapsedTime < 0)
  186. elapsedTime = 0;
  187. if (reset)
  188. startTime_ = currentTime;
  189. return (elapsedTime * 1000000LL) / frequency;
  190. }
  191. void HiresTimer::Reset()
  192. {
  193. #ifdef WIN32
  194. if (supported)
  195. {
  196. LARGE_INTEGER counter;
  197. QueryPerformanceCounter(&counter);
  198. startTime_ = counter.QuadPart;
  199. }
  200. else
  201. startTime_ = timeGetTime();
  202. #else
  203. struct timeval time;
  204. gettimeofday(&time, NULL);
  205. startTime_ = time.tv_sec * 1000000LL + time.tv_usec;
  206. #endif
  207. }
  208. }