PolarSSL v1.3.2
timing.c
Go to the documentation of this file.
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 
26 #include "polarssl/config.h"
27 
28 #if defined(POLARSSL_TIMING_C)
29 
30 #include "polarssl/timing.h"
31 
32 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
33 
34 #include <windows.h>
35 #include <winbase.h>
36 
37 struct _hr_time
38 {
39  LARGE_INTEGER start;
40 };
41 
42 #else
43 
44 #include <unistd.h>
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <signal.h>
48 #include <time.h>
49 
50 struct _hr_time
51 {
52  struct timeval start;
53 };
54 
55 #endif
56 
57 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
58  (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
59 
60 #define POLARSSL_HAVE_HARDCLOCK
61 
62 unsigned long hardclock( void )
63 {
64  unsigned long tsc;
65  __asm rdtsc
66  __asm mov [tsc], eax
67  return( tsc );
68 }
69 #endif
70 
71 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
72  defined(__GNUC__) && defined(__i386__)
73 
74 #define POLARSSL_HAVE_HARDCLOCK
75 
76 unsigned long hardclock( void )
77 {
78  unsigned long lo, hi;
79  asm( "rdtsc" : "=a" (lo), "=d" (hi) );
80  return( lo );
81 }
82 #endif
83 
84 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
85  defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
86 
87 #define POLARSSL_HAVE_HARDCLOCK
88 
89 unsigned long hardclock( void )
90 {
91  unsigned long lo, hi;
92  asm( "rdtsc" : "=a" (lo), "=d" (hi) );
93  return( lo | (hi << 32) );
94 }
95 #endif
96 
97 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
98  defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
99 
100 #define POLARSSL_HAVE_HARDCLOCK
101 
102 unsigned long hardclock( void )
103 {
104  unsigned long tbl, tbu0, tbu1;
105 
106  do
107  {
108  asm( "mftbu %0" : "=r" (tbu0) );
109  asm( "mftb %0" : "=r" (tbl ) );
110  asm( "mftbu %0" : "=r" (tbu1) );
111  }
112  while( tbu0 != tbu1 );
113 
114  return( tbl );
115 }
116 #endif
117 
118 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
119  defined(__GNUC__) && defined(__sparc64__)
120 
121 #if defined(__OpenBSD__)
122 #warning OpenBSD does not allow access to tick register using software version instead
123 #else
124 #define POLARSSL_HAVE_HARDCLOCK
125 
126 unsigned long hardclock( void )
127 {
128  unsigned long tick;
129  asm( "rdpr %%tick, %0;" : "=&r" (tick) );
130  return( tick );
131 }
132 #endif
133 #endif
134 
135 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
136  defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
137 
138 #define POLARSSL_HAVE_HARDCLOCK
139 
140 unsigned long hardclock( void )
141 {
142  unsigned long tick;
143  asm( ".byte 0x83, 0x41, 0x00, 0x00" );
144  asm( "mov %%g1, %0" : "=r" (tick) );
145  return( tick );
146 }
147 #endif
148 
149 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
150  defined(__GNUC__) && defined(__alpha__)
151 
152 #define POLARSSL_HAVE_HARDCLOCK
153 
154 unsigned long hardclock( void )
155 {
156  unsigned long cc;
157  asm( "rpcc %0" : "=r" (cc) );
158  return( cc & 0xFFFFFFFF );
159 }
160 #endif
161 
162 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
163  defined(__GNUC__) && defined(__ia64__)
164 
165 #define POLARSSL_HAVE_HARDCLOCK
166 
167 unsigned long hardclock( void )
168 {
169  unsigned long itc;
170  asm( "mov %0 = ar.itc" : "=r" (itc) );
171  return( itc );
172 }
173 #endif
174 
175 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
176  !defined(EFIX64) && !defined(EFI32)
177 
178 #define POLARSSL_HAVE_HARDCLOCK
179 
180 unsigned long hardclock( void )
181 {
182  LARGE_INTEGER offset;
183 
184  QueryPerformanceCounter( &offset );
185 
186  return (unsigned long)( offset.QuadPart );
187 }
188 #endif
189 
190 #if !defined(POLARSSL_HAVE_HARDCLOCK)
191 
192 #define POLARSSL_HAVE_HARDCLOCK
193 
194 static int hardclock_init = 0;
195 static struct timeval tv_init;
196 
197 unsigned long hardclock( void )
198 {
199  struct timeval tv_cur;
200 
201  if( hardclock_init == 0 )
202  {
203  gettimeofday( &tv_init, NULL );
204  hardclock_init = 1;
205  }
206 
207  gettimeofday( &tv_cur, NULL );
208  return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
209  + ( tv_cur.tv_usec - tv_init.tv_usec ) );
210 }
211 #endif
212 
213 volatile int alarmed = 0;
214 
215 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
216 
217 unsigned long get_timer( struct hr_time *val, int reset )
218 {
219  unsigned long delta;
220  LARGE_INTEGER offset, hfreq;
221  struct _hr_time *t = (struct _hr_time *) val;
222 
223  QueryPerformanceCounter( &offset );
224  QueryPerformanceFrequency( &hfreq );
225 
226  delta = (unsigned long)( ( 1000 *
227  ( offset.QuadPart - t->start.QuadPart ) ) /
228  hfreq.QuadPart );
229 
230  if( reset )
231  QueryPerformanceCounter( &t->start );
232 
233  return( delta );
234 }
235 
236 DWORD WINAPI TimerProc( LPVOID uElapse )
237 {
238  Sleep( (DWORD) uElapse );
239  alarmed = 1;
240  return( TRUE );
241 }
242 
243 void set_alarm( int seconds )
244 {
245  DWORD ThreadId;
246 
247  alarmed = 0;
248  CloseHandle( CreateThread( NULL, 0, TimerProc,
249  (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
250 }
251 
252 void m_sleep( int milliseconds )
253 {
254  Sleep( milliseconds );
255 }
256 
257 #else
258 
259 unsigned long get_timer( struct hr_time *val, int reset )
260 {
261  unsigned long delta;
262  struct timeval offset;
263  struct _hr_time *t = (struct _hr_time *) val;
264 
265  gettimeofday( &offset, NULL );
266 
267  delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
268  + ( offset.tv_usec - t->start.tv_usec ) / 1000;
269 
270  if( reset )
271  {
272  t->start.tv_sec = offset.tv_sec;
273  t->start.tv_usec = offset.tv_usec;
274  }
275 
276  return( delta );
277 }
278 
279 #if defined(INTEGRITY)
280 void m_sleep( int milliseconds )
281 {
282  usleep( milliseconds * 1000 );
283 }
284 
285 #else
286 
287 static void sighandler( int signum )
288 {
289  alarmed = 1;
290  signal( signum, sighandler );
291 }
292 
293 void set_alarm( int seconds )
294 {
295  alarmed = 0;
296  signal( SIGALRM, sighandler );
297  alarm( seconds );
298 }
299 
300 void m_sleep( int milliseconds )
301 {
302  struct timeval tv;
303 
304  tv.tv_sec = milliseconds / 1000;
305  tv.tv_usec = milliseconds * 1000;
306 
307  select( 0, NULL, NULL, NULL, &tv );
308 }
309 #endif /* INTEGRITY */
310 
311 #endif
312 
313 #endif
volatile int alarmed
unsigned long get_timer(struct hr_time *val, int reset)
Return the elapsed time in milliseconds.
void set_alarm(int seconds)
Setup an alarm clock.
Configuration options (set of defines)
unsigned long hardclock(void)
Return the CPU cycle counter value.
void m_sleep(int milliseconds)
Sleep for a certain amount of time.
timer structure
Definition: timing.h:37
Portable interface to the CPU cycle counter.