PolarSSL v1.2.9
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)
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 
177 #define POLARSSL_HAVE_HARDCLOCK
178 
179 unsigned long hardclock( void )
180 {
181  LARGE_INTEGER offset;
182 
183  QueryPerformanceCounter( &offset );
184 
185  return (unsigned long)( offset.QuadPart );
186 }
187 #endif
188 
189 #if !defined(POLARSSL_HAVE_HARDCLOCK)
190 
191 #define POLARSSL_HAVE_HARDCLOCK
192 
193 static int hardclock_init = 0;
194 static struct timeval tv_init;
195 
196 unsigned long hardclock( void )
197 {
198  struct timeval tv_cur;
199 
200  if( hardclock_init == 0 )
201  {
202  gettimeofday( &tv_init, NULL );
203  hardclock_init = 1;
204  }
205 
206  gettimeofday( &tv_cur, NULL );
207  return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
208  + ( tv_cur.tv_usec - tv_init.tv_usec ) );
209 }
210 #endif
211 
212 volatile int alarmed = 0;
213 
214 #if defined(_WIN32)
215 
216 unsigned long get_timer( struct hr_time *val, int reset )
217 {
218  unsigned long delta;
219  LARGE_INTEGER offset, hfreq;
220  struct _hr_time *t = (struct _hr_time *) val;
221 
222  QueryPerformanceCounter( &offset );
223  QueryPerformanceFrequency( &hfreq );
224 
225  delta = (unsigned long)( ( 1000 *
226  ( offset.QuadPart - t->start.QuadPart ) ) /
227  hfreq.QuadPart );
228 
229  if( reset )
230  QueryPerformanceCounter( &t->start );
231 
232  return( delta );
233 }
234 
235 DWORD WINAPI TimerProc( LPVOID uElapse )
236 {
237  Sleep( (DWORD) uElapse );
238  alarmed = 1;
239  return( TRUE );
240 }
241 
242 void set_alarm( int seconds )
243 {
244  DWORD ThreadId;
245 
246  alarmed = 0;
247  CloseHandle( CreateThread( NULL, 0, TimerProc,
248  (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
249 }
250 
251 void m_sleep( int milliseconds )
252 {
253  Sleep( milliseconds );
254 }
255 
256 #else
257 
258 unsigned long get_timer( struct hr_time *val, int reset )
259 {
260  unsigned long delta;
261  struct timeval offset;
262  struct _hr_time *t = (struct _hr_time *) val;
263 
264  gettimeofday( &offset, NULL );
265 
266  delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
267  + ( offset.tv_usec - t->start.tv_usec ) / 1000;
268 
269  if( reset )
270  {
271  t->start.tv_sec = offset.tv_sec;
272  t->start.tv_usec = offset.tv_usec;
273  }
274 
275  return( delta );
276 }
277 
278 #if defined(INTEGRITY)
279 void m_sleep( int milliseconds )
280 {
281  usleep( milliseconds * 1000 );
282 }
283 
284 #else
285 
286 static void sighandler( int signum )
287 {
288  alarmed = 1;
289  signal( signum, sighandler );
290 }
291 
292 void set_alarm( int seconds )
293 {
294  alarmed = 0;
295  signal( SIGALRM, sighandler );
296  alarm( seconds );
297 }
298 
299 void m_sleep( int milliseconds )
300 {
301  struct timeval tv;
302 
303  tv.tv_sec = milliseconds / 1000;
304  tv.tv_usec = milliseconds * 1000;
305 
306  select( 0, NULL, NULL, NULL, &tv );
307 }
308 #endif /* INTEGRITY */
309 
310 #endif
311 
312 #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:33
Portable interface to the CPU cycle counter.