timing.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Portable interface to the CPU cycle counter
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #include "config.h"
  26. #if defined(POLARSSL_TIMING_C)
  27. #include "timing.h"
  28. #if defined(_WIN32)
  29. #include <windows.h>
  30. #include <winbase.h>
  31. struct _hr_time
  32. {
  33. LARGE_INTEGER start;
  34. };
  35. #else
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39. #include <signal.h>
  40. #include <time.h>
  41. struct _hr_time
  42. {
  43. struct timeval start;
  44. };
  45. #endif
  46. #if defined(POLARSSL_HAVE_ASM) && \
  47. (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  48. unsigned long hardclock( void )
  49. {
  50. unsigned long tsc;
  51. __asm rdtsc
  52. __asm mov [tsc], eax
  53. return( tsc );
  54. }
  55. #else
  56. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__)
  57. unsigned long hardclock( void )
  58. {
  59. unsigned long tsc;
  60. asm( "rdtsc" : "=a" (tsc) );
  61. return( tsc );
  62. }
  63. #else
  64. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
  65. (defined(__amd64__) || defined(__x86_64__))
  66. unsigned long hardclock( void )
  67. {
  68. unsigned long lo, hi;
  69. asm( "rdtsc" : "=a" (lo), "=d" (hi) );
  70. return( lo | (hi << 32) );
  71. }
  72. #else
  73. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
  74. (defined(__powerpc__) || defined(__ppc__))
  75. unsigned long hardclock( void )
  76. {
  77. unsigned long tbl, tbu0, tbu1;
  78. do
  79. {
  80. asm( "mftbu %0" : "=r" (tbu0) );
  81. asm( "mftb %0" : "=r" (tbl ) );
  82. asm( "mftbu %0" : "=r" (tbu1) );
  83. }
  84. while( tbu0 != tbu1 );
  85. return( tbl );
  86. }
  87. #else
  88. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__sparc__)
  89. unsigned long hardclock( void )
  90. {
  91. unsigned long tick;
  92. asm( ".byte 0x83, 0x41, 0x00, 0x00" );
  93. asm( "mov %%g1, %0" : "=r" (tick) );
  94. return( tick );
  95. }
  96. #else
  97. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__alpha__)
  98. unsigned long hardclock( void )
  99. {
  100. unsigned long cc;
  101. asm( "rpcc %0" : "=r" (cc) );
  102. return( cc & 0xFFFFFFFF );
  103. }
  104. #else
  105. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__ia64__)
  106. unsigned long hardclock( void )
  107. {
  108. unsigned long itc;
  109. asm( "mov %0 = ar.itc" : "=r" (itc) );
  110. return( itc );
  111. }
  112. #else
  113. static int hardclock_init = 0;
  114. static struct timeval tv_init;
  115. unsigned long hardclock( void )
  116. {
  117. struct timeval tv_cur;
  118. if( hardclock_init == 0 )
  119. {
  120. gettimeofday( &tv_init, NULL );
  121. hardclock_init = 1;
  122. }
  123. gettimeofday( &tv_cur, NULL );
  124. return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
  125. + ( tv_cur.tv_usec - tv_init.tv_usec ) );
  126. }
  127. #endif /* generic */
  128. #endif /* IA-64 */
  129. #endif /* Alpha */
  130. #endif /* SPARC8 */
  131. #endif /* PowerPC */
  132. #endif /* AMD64 */
  133. #endif /* i586+ */
  134. int alarmed = 0;
  135. #if defined(_WIN32)
  136. unsigned long get_timer( struct hr_time *val, int reset )
  137. {
  138. unsigned long delta;
  139. LARGE_INTEGER offset, hfreq;
  140. struct _hr_time *t = (struct _hr_time *) val;
  141. QueryPerformanceCounter( &offset );
  142. QueryPerformanceFrequency( &hfreq );
  143. delta = (unsigned long)( ( 1000 *
  144. ( offset.QuadPart - t->start.QuadPart ) ) /
  145. hfreq.QuadPart );
  146. if( reset )
  147. QueryPerformanceCounter( &t->start );
  148. return( delta );
  149. }
  150. DWORD WINAPI TimerProc( LPVOID uElapse )
  151. {
  152. Sleep( (DWORD) uElapse );
  153. alarmed = 1;
  154. return( TRUE );
  155. }
  156. void set_alarm( int seconds )
  157. {
  158. DWORD ThreadId;
  159. alarmed = 0;
  160. CloseHandle( CreateThread( NULL, 0, TimerProc,
  161. (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
  162. }
  163. void m_sleep( int milliseconds )
  164. {
  165. Sleep( milliseconds );
  166. }
  167. #else
  168. unsigned long get_timer( struct hr_time *val, int reset )
  169. {
  170. unsigned long delta;
  171. struct timeval offset;
  172. struct _hr_time *t = (struct _hr_time *) val;
  173. gettimeofday( &offset, NULL );
  174. delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
  175. + ( offset.tv_usec - t->start.tv_usec ) / 1000;
  176. if( reset )
  177. {
  178. t->start.tv_sec = offset.tv_sec;
  179. t->start.tv_usec = offset.tv_usec;
  180. }
  181. return( delta );
  182. }
  183. static void sighandler( int signum )
  184. {
  185. alarmed = 1;
  186. signal( signum, sighandler );
  187. }
  188. void set_alarm( int seconds )
  189. {
  190. alarmed = 0;
  191. signal( SIGALRM, sighandler );
  192. alarm( seconds );
  193. }
  194. void m_sleep( int milliseconds )
  195. {
  196. struct timeval tv;
  197. tv.tv_sec = milliseconds / 1000;
  198. tv.tv_usec = milliseconds * 1000;
  199. select( 0, NULL, NULL, NULL, &tv );
  200. }
  201. #endif
  202. #endif