PolarSSL v1.3.8
timing.c
Go to the documentation of this file.
1 /*
2  * Portable interface to the CPU cycle counter
3  *
4  * Copyright (C) 2006-2014, 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 #if !defined(POLARSSL_CONFIG_FILE)
27 #include "polarssl/config.h"
28 #else
29 #include POLARSSL_CONFIG_FILE
30 #endif
31 
32 #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_PLATFORM_C)
33 #include "polarssl/platform.h"
34 #else
35 #include <stdio.h>
36 #define polarssl_printf printf
37 #endif
38 
39 #if defined(POLARSSL_TIMING_C) && !defined(POLARSSL_TIMING_ALT)
40 
41 #include "polarssl/timing.h"
42 
43 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
44 
45 #include <windows.h>
46 #include <winbase.h>
47 
48 struct _hr_time
49 {
50  LARGE_INTEGER start;
51 };
52 
53 #else
54 
55 #include <unistd.h>
56 #include <sys/types.h>
57 #include <sys/time.h>
58 #include <signal.h>
59 #include <time.h>
60 
61 struct _hr_time
62 {
63  struct timeval start;
64 };
65 
66 #endif /* _WIN32 && !EFIX64 && !EFI32 */
67 
68 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
69  ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
70 
71 #define POLARSSL_HAVE_HARDCLOCK
72 
73 unsigned long hardclock( void )
74 {
75  unsigned long tsc;
76  __asm rdtsc
77  __asm mov [tsc], eax
78  return( tsc );
79 }
80 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
81  ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
82 
83 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
84  defined(__GNUC__) && defined(__i386__)
85 
86 #define POLARSSL_HAVE_HARDCLOCK
87 
88 unsigned long hardclock( void )
89 {
90  unsigned long lo, hi;
91  asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
92  return( lo );
93 }
94 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
95  __GNUC__ && __i386__ */
96 
97 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
98  defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
99 
100 #define POLARSSL_HAVE_HARDCLOCK
101 
102 unsigned long hardclock( void )
103 {
104  unsigned long lo, hi;
105  asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
106  return( lo | ( hi << 32 ) );
107 }
108 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
109  __GNUC__ && ( __amd64__ || __x86_64__ ) */
110 
111 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
112  defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
113 
114 #define POLARSSL_HAVE_HARDCLOCK
115 
116 unsigned long hardclock( void )
117 {
118  unsigned long tbl, tbu0, tbu1;
119 
120  do
121  {
122  asm volatile( "mftbu %0" : "=r" (tbu0) );
123  asm volatile( "mftb %0" : "=r" (tbl ) );
124  asm volatile( "mftbu %0" : "=r" (tbu1) );
125  }
126  while( tbu0 != tbu1 );
127 
128  return( tbl );
129 }
130 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
131  __GNUC__ && ( __powerpc__ || __ppc__ ) */
132 
133 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
134  defined(__GNUC__) && defined(__sparc64__)
135 
136 #if defined(__OpenBSD__)
137 #warning OpenBSD does not allow access to tick register using software version instead
138 #else
139 #define POLARSSL_HAVE_HARDCLOCK
140 
141 unsigned long hardclock( void )
142 {
143  unsigned long tick;
144  asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
145  return( tick );
146 }
147 #endif /* __OpenBSD__ */
148 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
149  __GNUC__ && __sparc64__ */
150 
151 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
152  defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
153 
154 #define POLARSSL_HAVE_HARDCLOCK
155 
156 unsigned long hardclock( void )
157 {
158  unsigned long tick;
159  asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
160  asm volatile( "mov %%g1, %0" : "=r" (tick) );
161  return( tick );
162 }
163 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
164  __GNUC__ && __sparc__ && !__sparc64__ */
165 
166 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
167  defined(__GNUC__) && defined(__alpha__)
168 
169 #define POLARSSL_HAVE_HARDCLOCK
170 
171 unsigned long hardclock( void )
172 {
173  unsigned long cc;
174  asm volatile( "rpcc %0" : "=r" (cc) );
175  return( cc & 0xFFFFFFFF );
176 }
177 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
178  __GNUC__ && __alpha__ */
179 
180 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
181  defined(__GNUC__) && defined(__ia64__)
182 
183 #define POLARSSL_HAVE_HARDCLOCK
184 
185 unsigned long hardclock( void )
186 {
187  unsigned long itc;
188  asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
189  return( itc );
190 }
191 #endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
192  __GNUC__ && __ia64__ */
193 
194 #if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
195  !defined(EFIX64) && !defined(EFI32)
196 
197 #define POLARSSL_HAVE_HARDCLOCK
198 
199 unsigned long hardclock( void )
200 {
201  LARGE_INTEGER offset;
202 
203  QueryPerformanceCounter( &offset );
204 
205  return( (unsigned long)( offset.QuadPart ) );
206 }
207 #endif /* !POLARSSL_HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
208 
209 #if !defined(POLARSSL_HAVE_HARDCLOCK)
210 
211 #define POLARSSL_HAVE_HARDCLOCK
212 
213 static int hardclock_init = 0;
214 static struct timeval tv_init;
215 
216 unsigned long hardclock( void )
217 {
218  struct timeval tv_cur;
219 
220  if( hardclock_init == 0 )
221  {
222  gettimeofday( &tv_init, NULL );
223  hardclock_init = 1;
224  }
225 
226  gettimeofday( &tv_cur, NULL );
227  return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
228  + ( tv_cur.tv_usec - tv_init.tv_usec ) );
229 }
230 #endif /* !POLARSSL_HAVE_HARDCLOCK */
231 
232 volatile int alarmed = 0;
233 
234 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
235 
236 unsigned long get_timer( struct hr_time *val, int reset )
237 {
238  unsigned long delta;
239  LARGE_INTEGER offset, hfreq;
240  struct _hr_time *t = (struct _hr_time *) val;
241 
242  QueryPerformanceCounter( &offset );
243  QueryPerformanceFrequency( &hfreq );
244 
245  delta = (unsigned long)( ( 1000 *
246  ( offset.QuadPart - t->start.QuadPart ) ) /
247  hfreq.QuadPart );
248 
249  if( reset )
250  QueryPerformanceCounter( &t->start );
251 
252  return( delta );
253 }
254 
255 DWORD WINAPI TimerProc( LPVOID uElapse )
256 {
257  Sleep( (DWORD) uElapse );
258  alarmed = 1;
259  return( TRUE );
260 }
261 
262 void set_alarm( int seconds )
263 {
264  DWORD ThreadId;
265 
266  alarmed = 0;
267  CloseHandle( CreateThread( NULL, 0, TimerProc,
268  (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
269 }
270 
271 void m_sleep( int milliseconds )
272 {
273  Sleep( milliseconds );
274 }
275 
276 #else /* _WIN32 && !EFIX64 && !EFI32 */
277 
278 unsigned long get_timer( struct hr_time *val, int reset )
279 {
280  unsigned long delta;
281  struct timeval offset;
282  struct _hr_time *t = (struct _hr_time *) val;
283 
284  gettimeofday( &offset, NULL );
285 
286  delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
287  + ( offset.tv_usec - t->start.tv_usec ) / 1000;
288 
289  if( reset )
290  {
291  t->start.tv_sec = offset.tv_sec;
292  t->start.tv_usec = offset.tv_usec;
293  }
294 
295  return( delta );
296 }
297 
298 #if defined(INTEGRITY)
299 void m_sleep( int milliseconds )
300 {
301  usleep( milliseconds * 1000 );
302 }
303 
304 #else /* INTEGRITY */
305 
306 static void sighandler( int signum )
307 {
308  alarmed = 1;
309  signal( signum, sighandler );
310 }
311 
312 void set_alarm( int seconds )
313 {
314  alarmed = 0;
315  signal( SIGALRM, sighandler );
316  alarm( seconds );
317 }
318 
319 void m_sleep( int milliseconds )
320 {
321  struct timeval tv;
322 
323  tv.tv_sec = milliseconds / 1000;
324  tv.tv_usec = ( milliseconds % 1000 ) * 1000;
325 
326  select( 0, NULL, NULL, NULL, &tv );
327 }
328 #endif /* INTEGRITY */
329 
330 #endif /* _WIN32 && !EFIX64 && !EFI32 */
331 
332 #if defined(POLARSSL_SELF_TEST)
333 
334 /* To test net_usleep against our functions */
335 #if defined(POLARSSL_NET_C)
336 #include "polarssl/net.h"
337 #endif
338 
339 /*
340  * Busy-waits for the given number of milliseconds.
341  * Used for testing hardclock.
342  */
343 static void busy_msleep( unsigned long msec )
344 {
345  struct hr_time hires;
346  unsigned long i = 0; /* for busy-waiting */
347  volatile unsigned long j; /* to prevent optimisation */
348 
349  (void) get_timer( &hires, 1 );
350 
351  while( get_timer( &hires, 0 ) < msec )
352  i++;
353 
354  j = i;
355  (void) j;
356 }
357 
358 /*
359  * Checkup routine
360  *
361  * Warning: this is work in progress, some tests may not be reliable enough
362  * yet! False positives may happen.
363  */
364 int timing_self_test( int verbose )
365 {
366  unsigned long cycles, ratio;
367  unsigned long millisecs, secs;
368  int hardfail;
369  struct hr_time hires;
370 
371  if( verbose != 0 )
372  polarssl_printf( " TIMING tests note: will take some time!\n" );
373 
374  if( verbose != 0 )
375  polarssl_printf( " TIMING test #1 (m_sleep / get_timer): " );
376 
377  for( secs = 1; secs <= 3; secs++ )
378  {
379  (void) get_timer( &hires, 1 );
380 
381  m_sleep( 500 * secs );
382 
383  millisecs = get_timer( &hires, 0 );
384 
385  if( millisecs < 450 * secs || millisecs > 550 * secs )
386  {
387  if( verbose != 0 )
388  polarssl_printf( "failed\n" );
389 
390  return( 1 );
391  }
392  }
393 
394  if( verbose != 0 )
395  polarssl_printf( "passed\n" );
396 
397  if( verbose != 0 )
398  polarssl_printf( " TIMING test #2 (set_alarm / get_timer): " );
399 
400  for( secs = 1; secs <= 3; secs++ )
401  {
402  (void) get_timer( &hires, 1 );
403 
404  set_alarm( secs );
405  while( !alarmed )
406  ;
407 
408  millisecs = get_timer( &hires, 0 );
409 
410  if( millisecs < 900 * secs || millisecs > 1100 * secs )
411  {
412  if( verbose != 0 )
413  polarssl_printf( "failed\n" );
414 
415  return( 1 );
416  }
417  }
418 
419  if( verbose != 0 )
420  polarssl_printf( "passed\n" );
421 
422  if( verbose != 0 )
423  polarssl_printf( " TIMING test #3 (hardclock / get_timer): " );
424 
425  /*
426  * Allow one failure for possible counter wrapping.
427  * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
428  * since the whole test is about 10ms, it shouldn't happen twice in a row.
429  */
430  hardfail = 0;
431 
432 hard_test:
433  if( hardfail > 1 )
434  {
435  if( verbose != 0 )
436  polarssl_printf( "failed\n" );
437 
438  return( 1 );
439  }
440 
441  /* Get a reference ratio cycles/ms */
442  millisecs = 1;
443  cycles = hardclock();
444  busy_msleep( millisecs );
445  cycles = hardclock() - cycles;
446  ratio = cycles / millisecs;
447 
448  /* Check that the ratio is mostly constant */
449  for( millisecs = 2; millisecs <= 4; millisecs++ )
450  {
451  cycles = hardclock();
452  busy_msleep( millisecs );
453  cycles = hardclock() - cycles;
454 
455  /* Allow variation up to 20% */
456  if( cycles / millisecs < ratio - ratio / 5 ||
457  cycles / millisecs > ratio + ratio / 5 )
458  {
459  hardfail++;
460  goto hard_test;
461  }
462  }
463 
464  if( verbose != 0 )
465  polarssl_printf( "passed\n" );
466 
467 #if defined(POLARSSL_NET_C)
468  if( verbose != 0 )
469  polarssl_printf( " TIMING test #4 (net_usleep/ get_timer): " );
470 
471  for( secs = 1; secs <= 3; secs++ )
472  {
473  (void) get_timer( &hires, 1 );
474 
475  net_usleep( 500000 * secs );
476 
477  millisecs = get_timer( &hires, 0 );
478 
479  if( millisecs < 450 * secs || millisecs > 550 * secs )
480  {
481  if( verbose != 0 )
482  polarssl_printf( "failed\n" );
483 
484  return( 1 );
485  }
486  }
487 
488  if( verbose != 0 )
489  polarssl_printf( "passed\n" );
490 #endif /* POLARSSL_NET_C */
491 
492  if( verbose != 0 )
493  polarssl_printf( "\n" );
494 
495  return( 0 );
496 }
497 
498 #endif /* POLARSSL_SELF_TEST */
499 
500 #endif /* POLARSSL_TIMING_C && !POLARSSL_TIMING_ALT */
void net_usleep(unsigned long usec)
Portable usleep helper.
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.
Network communication functions.
Configuration options (set of defines)
PolarSSL Platform abstraction layer.
unsigned long hardclock(void)
Return the CPU cycle counter value.
#define polarssl_printf
Definition: timing.c:36
void m_sleep(int milliseconds)
Sleep for a certain amount of time.
timer structure
Definition: timing.h:47
int timing_self_test(int verbose)
Checkup routine.
Portable interface to the CPU cycle counter.