os_timestamp_win32.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/os.h>
  20. #include <pj/assert.h>
  21. #include <pj/errno.h>
  22. #include <pj/log.h>
  23. #include <windows.h>
  24. #define THIS_FILE "os_timestamp_win32.c"
  25. #if 1
  26. # define TRACE_(x) PJ_LOG(3,x)
  27. #else
  28. # define TRACE_(x) ;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. #if defined(PJ_TIMESTAMP_USE_RDTSC) && PJ_TIMESTAMP_USE_RDTSC!=0 && \
  32. defined(PJ_M_I386) && PJ_M_I386 != 0 && \
  33. defined(PJ_HAS_PENTIUM) && PJ_HAS_PENTIUM!=0 && \
  34. defined(_MSC_VER)
  35. /*
  36. * Use rdtsc to get the OS timestamp.
  37. */
  38. static LONG CpuMhz;
  39. static pj_int64_t CpuHz;
  40. static pj_status_t GetCpuHz(void)
  41. {
  42. HKEY key;
  43. LONG rc;
  44. DWORD size;
  45. #if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0
  46. rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  47. L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
  48. 0, 0, &key);
  49. #else
  50. rc = RegOpenKey( HKEY_LOCAL_MACHINE,
  51. "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
  52. &key);
  53. #endif
  54. if (rc != ERROR_SUCCESS)
  55. return PJ_RETURN_OS_ERROR(rc);
  56. size = sizeof(CpuMhz);
  57. rc = RegQueryValueEx(key, "~MHz", NULL, NULL, (BYTE*)&CpuMhz, &size);
  58. RegCloseKey(key);
  59. if (rc != ERROR_SUCCESS) {
  60. return PJ_RETURN_OS_ERROR(rc);
  61. }
  62. CpuHz = CpuMhz;
  63. CpuHz = CpuHz * 1000000;
  64. return PJ_SUCCESS;
  65. }
  66. /* __int64 is nicely returned in EDX:EAX */
  67. __declspec(naked) __int64 rdtsc()
  68. {
  69. __asm
  70. {
  71. RDTSC
  72. RET
  73. }
  74. }
  75. PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts)
  76. {
  77. ts->u64 = rdtsc();
  78. return PJ_SUCCESS;
  79. }
  80. PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq)
  81. {
  82. pj_status_t status;
  83. if (CpuHz == 0) {
  84. status = GetCpuHz();
  85. if (status != PJ_SUCCESS)
  86. return status;
  87. }
  88. freq->u64 = CpuHz;
  89. return PJ_SUCCESS;
  90. }
  91. /////////////////////////////////////////////////////////////////////////////
  92. #elif defined(PJ_TIMESTAMP_WIN32_USE_SAFE_QPC) && \
  93. PJ_TIMESTAMP_WIN32_USE_SAFE_QPC!=0
  94. /* Use safe QueryPerformanceCounter.
  95. * This implementation has some protection against bug in KB Q274323:
  96. * Performance counter value may unexpectedly leap forward
  97. * http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q274323
  98. *
  99. * THIS SHOULD NOT BE USED YET AS IT DOESN'T HANDLE SYSTEM TIME
  100. * CHANGE.
  101. */
  102. static pj_timestamp g_ts_freq;
  103. static pj_timestamp g_ts_base;
  104. static pj_int64_t g_time_base;
  105. PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts)
  106. {
  107. enum { MAX_RETRY = 10 };
  108. unsigned i;
  109. /* pj_get_timestamp_freq() must have been called before.
  110. * This is done when application called pj_init().
  111. */
  112. pj_assert(g_ts_freq.u64 != 0);
  113. /* Retry QueryPerformanceCounter() until we're sure that the
  114. * value returned makes sense.
  115. */
  116. i = 0;
  117. do {
  118. LARGE_INTEGER val;
  119. pj_int64_t counter64, time64, diff;
  120. pj_time_val time_now;
  121. /* Retrieve the counter */
  122. if (!QueryPerformanceCounter(&val))
  123. return PJ_RETURN_OS_ERROR(GetLastError());
  124. /* Regardless of the goodness of the value, we should put
  125. * the counter here, because normally application wouldn't
  126. * check the error result of this function.
  127. */
  128. ts->u64 = val.QuadPart;
  129. /* Retrieve time */
  130. pj_gettimeofday(&time_now);
  131. /* Get the counter elapsed time in miliseconds */
  132. counter64 = (val.QuadPart - g_ts_base.u64) * 1000 / g_ts_freq.u64;
  133. /* Get the time elapsed in miliseconds.
  134. * We don't want to use PJ_TIME_VAL_MSEC() since it's using
  135. * 32bit calculation, which limits the maximum elapsed time
  136. * to around 49 days only.
  137. */
  138. time64 = time_now.sec;
  139. time64 = time64 * 1000 + time_now.msec;
  140. //time64 = GetTickCount();
  141. /* It's good if the difference between two clocks are within
  142. * some compile time constant (default: 20ms, which to allow
  143. * context switch happen between QueryPerformanceCounter and
  144. * pj_gettimeofday()).
  145. */
  146. diff = (time64 - g_time_base) - counter64;
  147. if (diff >= -20 && diff <= 20) {
  148. /* It's good */
  149. return PJ_SUCCESS;
  150. }
  151. ++i;
  152. } while (i < MAX_RETRY);
  153. TRACE_((THIS_FILE, "QueryPerformanceCounter returned bad value"));
  154. return PJ_ETIMEDOUT;
  155. }
  156. static pj_status_t init_performance_counter(void)
  157. {
  158. LARGE_INTEGER val;
  159. pj_time_val time_base;
  160. pj_status_t status;
  161. /* Get the frequency */
  162. if (!QueryPerformanceFrequency(&val))
  163. return PJ_RETURN_OS_ERROR(GetLastError());
  164. g_ts_freq.u64 = val.QuadPart;
  165. /* Get the base timestamp */
  166. if (!QueryPerformanceCounter(&val))
  167. return PJ_RETURN_OS_ERROR(GetLastError());
  168. g_ts_base.u64 = val.QuadPart;
  169. /* Get the base time */
  170. status = pj_gettimeofday(&time_base);
  171. if (status != PJ_SUCCESS)
  172. return status;
  173. /* Convert time base to 64bit value in msec */
  174. g_time_base = time_base.sec;
  175. g_time_base = g_time_base * 1000 + time_base.msec;
  176. //g_time_base = GetTickCount();
  177. return PJ_SUCCESS;
  178. }
  179. PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq)
  180. {
  181. if (g_ts_freq.u64 == 0) {
  182. enum { MAX_REPEAT = 10 };
  183. unsigned i;
  184. pj_status_t status;
  185. /* Make unellegant compiler happy */
  186. status = 0;
  187. /* Repeat initializing performance counter until we're sure
  188. * the base timing is correct. It is possible that the system
  189. * returns bad counter during this initialization!
  190. */
  191. for (i=0; i<MAX_REPEAT; ++i) {
  192. pj_timestamp dummy;
  193. /* Init base time */
  194. status = init_performance_counter();
  195. if (status != PJ_SUCCESS)
  196. return status;
  197. /* Try the base time */
  198. status = pj_get_timestamp(&dummy);
  199. if (status == PJ_SUCCESS)
  200. break;
  201. }
  202. if (status != PJ_SUCCESS)
  203. return status;
  204. }
  205. freq->u64 = g_ts_freq.u64;
  206. return PJ_SUCCESS;
  207. }
  208. /////////////////////////////////////////////////////////////////////////////
  209. #else
  210. /*
  211. * Use QueryPerformanceCounter and QueryPerformanceFrequency.
  212. * This should be the default implementation to be used on Windows.
  213. */
  214. PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts)
  215. {
  216. LARGE_INTEGER val;
  217. if (!QueryPerformanceCounter(&val))
  218. return PJ_RETURN_OS_ERROR(GetLastError());
  219. ts->u64 = val.QuadPart;
  220. return PJ_SUCCESS;
  221. }
  222. PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq)
  223. {
  224. LARGE_INTEGER val;
  225. if (!QueryPerformanceFrequency(&val))
  226. return PJ_RETURN_OS_ERROR(GetLastError());
  227. freq->u64 = val.QuadPart;
  228. return PJ_SUCCESS;
  229. }
  230. #endif /* PJ_TIMESTAMP_USE_RDTSC */