Timer.cpp 5.5 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 "../Core/Timer.h"
  26. #include <ctime>
  27. #ifdef WIN32
  28. #include <windows.h>
  29. #include <mmsystem.h>
  30. #else
  31. #include <sys/time.h>
  32. #include <unistd.h>
  33. #endif
  34. #include "../DebugNew.h"
  35. namespace Atomic
  36. {
  37. bool HiresTimer::supported(false);
  38. long long HiresTimer::frequency(1000);
  39. Time::Time(Context* context) :
  40. Object(context),
  41. frameNumber_(0),
  42. timeStep_(0.0f),
  43. timerPeriod_(0)
  44. {
  45. #ifdef WIN32
  46. LARGE_INTEGER frequency;
  47. if (QueryPerformanceFrequency(&frequency))
  48. {
  49. HiresTimer::frequency = frequency.QuadPart;
  50. HiresTimer::supported = true;
  51. }
  52. #else
  53. HiresTimer::frequency = 1000000;
  54. HiresTimer::supported = true;
  55. #endif
  56. }
  57. Time::~Time()
  58. {
  59. SetTimerPeriod(0);
  60. }
  61. void Time::BeginFrame(float timeStep)
  62. {
  63. ++frameNumber_;
  64. if (!frameNumber_)
  65. ++frameNumber_;
  66. timeStep_ = timeStep;
  67. Profiler* profiler = GetSubsystem<Profiler>();
  68. if (profiler)
  69. profiler->BeginFrame();
  70. {
  71. PROFILE(BeginFrame);
  72. // Frame begin event
  73. using namespace BeginFrame;
  74. VariantMap& eventData = GetEventDataMap();
  75. eventData[P_FRAMENUMBER] = frameNumber_;
  76. eventData[P_TIMESTEP] = timeStep_;
  77. SendEvent(E_BEGINFRAME, eventData);
  78. }
  79. }
  80. void Time::EndFrame()
  81. {
  82. {
  83. PROFILE(EndFrame);
  84. // Frame end event
  85. SendEvent(E_ENDFRAME);
  86. }
  87. Profiler* profiler = GetSubsystem<Profiler>();
  88. if (profiler)
  89. profiler->EndFrame();
  90. }
  91. void Time::SetTimerPeriod(unsigned mSec)
  92. {
  93. #ifdef WIN32
  94. if (timerPeriod_ > 0)
  95. timeEndPeriod(timerPeriod_);
  96. timerPeriod_ = mSec;
  97. if (timerPeriod_ > 0)
  98. timeBeginPeriod(timerPeriod_);
  99. #endif
  100. }
  101. float Time::GetElapsedTime()
  102. {
  103. return elapsedTime_.GetMSec(false) / 1000.0f;
  104. }
  105. unsigned Time::GetSystemTime()
  106. {
  107. #ifdef WIN32
  108. unsigned currentTime = (unsigned)timeGetTime();
  109. #else
  110. struct timeval time;
  111. gettimeofday(&time, NULL);
  112. unsigned currentTime = (unsigned)(time.tv_sec * 1000 + time.tv_usec / 1000);
  113. #endif
  114. return currentTime;
  115. }
  116. unsigned Time::GetTimeSinceEpoch()
  117. {
  118. return (unsigned)time(NULL);
  119. }
  120. String Time::GetTimeStamp()
  121. {
  122. time_t sysTime;
  123. time(&sysTime);
  124. const char* dateTime = ctime(&sysTime);
  125. return String(dateTime).Replaced("\n", "");
  126. }
  127. void Time::Sleep(unsigned mSec)
  128. {
  129. #ifdef WIN32
  130. ::Sleep(mSec);
  131. #else
  132. usleep(mSec * 1000);
  133. #endif
  134. }
  135. Timer::Timer()
  136. {
  137. Reset();
  138. }
  139. unsigned Timer::GetMSec(bool reset)
  140. {
  141. #ifdef WIN32
  142. unsigned currentTime = (unsigned)timeGetTime();
  143. #else
  144. struct timeval time;
  145. gettimeofday(&time, NULL);
  146. unsigned currentTime = (unsigned)(time.tv_sec * 1000 + time.tv_usec / 1000);
  147. #endif
  148. unsigned elapsedTime = currentTime - startTime_;
  149. if (reset)
  150. startTime_ = currentTime;
  151. return elapsedTime;
  152. }
  153. void Timer::Reset()
  154. {
  155. #ifdef WIN32
  156. startTime_ = (unsigned)timeGetTime();
  157. #else
  158. struct timeval time;
  159. gettimeofday(&time, NULL);
  160. startTime_ = (unsigned)(time.tv_sec * 1000 + time.tv_usec / 1000);
  161. #endif
  162. }
  163. HiresTimer::HiresTimer()
  164. {
  165. Reset();
  166. }
  167. long long HiresTimer::GetUSec(bool reset)
  168. {
  169. long long currentTime;
  170. #ifdef WIN32
  171. if (supported)
  172. {
  173. LARGE_INTEGER counter;
  174. QueryPerformanceCounter(&counter);
  175. currentTime = counter.QuadPart;
  176. }
  177. else
  178. currentTime = timeGetTime();
  179. #else
  180. struct timeval time;
  181. gettimeofday(&time, NULL);
  182. currentTime = time.tv_sec * 1000000LL + time.tv_usec;
  183. #endif
  184. long long elapsedTime = currentTime - startTime_;
  185. // Correct for possible weirdness with changing internal frequency
  186. if (elapsedTime < 0)
  187. elapsedTime = 0;
  188. if (reset)
  189. startTime_ = currentTime;
  190. return (elapsedTime * 1000000LL) / frequency;
  191. }
  192. void HiresTimer::Reset()
  193. {
  194. #ifdef WIN32
  195. if (supported)
  196. {
  197. LARGE_INTEGER counter;
  198. QueryPerformanceCounter(&counter);
  199. startTime_ = counter.QuadPart;
  200. }
  201. else
  202. startTime_ = timeGetTime();
  203. #else
  204. struct timeval time;
  205. gettimeofday(&time, NULL);
  206. startTime_ = time.tv_sec * 1000000LL + time.tv_usec;
  207. #endif
  208. }
  209. }