PolarSSL v1.3.9
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  if( reset )
287  {
288  t->start.tv_sec = offset.tv_sec;
289  t->start.tv_usec = offset.tv_usec;
290  return( 0 );
291  }
292 
293  delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
294  + ( offset.tv_usec - t->start.tv_usec ) / 1000;
295 
296  return( delta );
297 }
298 
299 #if defined(INTEGRITY)
300 void m_sleep( int milliseconds )
301 {
302  usleep( milliseconds * 1000 );
303 }
304 
305 #else /* INTEGRITY */
306 
307 static void sighandler( int signum )
308 {
309  alarmed = 1;
310  signal( signum, sighandler );
311 }
312 
313 void set_alarm( int seconds )
314 {
315  alarmed = 0;
316  signal( SIGALRM, sighandler );
317  alarm( seconds );
318 }
319 
320 void m_sleep( int milliseconds )
321 {
322  struct timeval tv;
323 
324  tv.tv_sec = milliseconds / 1000;
325  tv.tv_usec = ( milliseconds % 1000 ) * 1000;
326 
327  select( 0, NULL, NULL, NULL, &tv );
328 }
329 #endif /* INTEGRITY */
330 
331 #endif /* _WIN32 && !EFIX64 && !EFI32 */
332 
333 #if defined(POLARSSL_SELF_TEST)
334 
335 /* To test net_usleep against our functions */
336 #if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
337 #include "polarssl/net.h"
338 #endif
339 
340 /*
341  * Busy-waits for the given number of milliseconds.
342  * Used for testing hardclock.
343  */
344 static void busy_msleep( unsigned long msec )
345 {
346  struct hr_time hires;
347  unsigned long i = 0; /* for busy-waiting */
348  volatile unsigned long j; /* to prevent optimisation */
349 
350  (void) get_timer( &hires, 1 );
351 
352  while( get_timer( &hires, 0 ) < msec )
353  i++;
354 
355  j = i;
356  (void) j;
357 }
358 
359 /*
360  * Checkup routine
361  *
362  * Warning: this is work in progress, some tests may not be reliable enough
363  * yet! False positives may happen.
364  */
365 int timing_self_test( int verbose )
366 {
367  unsigned long cycles, ratio;
368  unsigned long millisecs, secs;
369  int hardfail;
370  struct hr_time hires;
371 
372  if( verbose != 0 )
373  polarssl_printf( " TIMING tests note: will take some time!\n" );
374 
375  if( verbose != 0 )
376  polarssl_printf( " TIMING test #1 (m_sleep / get_timer): " );
377 
378  for( secs = 1; secs <= 3; secs++ )
379  {
380  (void) get_timer( &hires, 1 );
381 
382  m_sleep( (int)( 500 * secs ) );
383 
384  millisecs = get_timer( &hires, 0 );
385 
386  if( millisecs < 450 * secs || millisecs > 550 * secs )
387  {
388  if( verbose != 0 )
389  polarssl_printf( "failed\n" );
390 
391  return( 1 );
392  }
393  }
394 
395  if( verbose != 0 )
396  polarssl_printf( "passed\n" );
397 
398  if( verbose != 0 )
399  polarssl_printf( " TIMING test #2 (set_alarm / get_timer): " );
400 
401  for( secs = 1; secs <= 3; secs++ )
402  {
403  (void) get_timer( &hires, 1 );
404 
405  set_alarm( (int) secs );
406  while( !alarmed )
407  ;
408 
409  millisecs = get_timer( &hires, 0 );
410 
411  if( millisecs < 900 * secs || millisecs > 1100 * secs )
412  {
413  if( verbose != 0 )
414  polarssl_printf( "failed\n" );
415 
416  return( 1 );
417  }
418  }
419 
420  if( verbose != 0 )
421  polarssl_printf( "passed\n" );
422 
423  if( verbose != 0 )
424  polarssl_printf( " TIMING test #3 (hardclock / get_timer): " );
425 
426  /*
427  * Allow one failure for possible counter wrapping.
428  * On a 4Ghz 32-bit machine the cycle counter wraps about once per second;
429  * since the whole test is about 10ms, it shouldn't happen twice in a row.
430  */
431  hardfail = 0;
432 
433 hard_test:
434  if( hardfail > 1 )
435  {
436  if( verbose != 0 )
437  polarssl_printf( "failed\n" );
438 
439  return( 1 );
440  }
441 
442  /* Get a reference ratio cycles/ms */
443  millisecs = 1;
444  cycles = hardclock();
445  busy_msleep( millisecs );
446  cycles = hardclock() - cycles;
447  ratio = cycles / millisecs;
448 
449  /* Check that the ratio is mostly constant */
450  for( millisecs = 2; millisecs <= 4; millisecs++ )
451  {
452  cycles = hardclock();
453  busy_msleep( millisecs );
454  cycles = hardclock() - cycles;
455 
456  /* Allow variation up to 20% */
457  if( cycles / millisecs < ratio - ratio / 5 ||
458  cycles / millisecs > ratio + ratio / 5 )
459  {
460  hardfail++;
461  goto hard_test;
462  }
463  }
464 
465  if( verbose != 0 )
466  polarssl_printf( "passed\n" );
467 
468 #if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
469  if( verbose != 0 )
470  polarssl_printf( " TIMING test #4 (net_usleep/ get_timer): " );
471 
472  for( secs = 1; secs <= 3; secs++ )
473  {
474  (void) get_timer( &hires, 1 );
475 
476  net_usleep( 500000 * secs );
477 
478  millisecs = get_timer( &hires, 0 );
479 
480  if( millisecs < 450 * secs || millisecs > 550 * secs )
481  {
482  if( verbose != 0 )
483  polarssl_printf( "failed\n" );
484 
485  return( 1 );
486  }
487  }
488 
489  if( verbose != 0 )
490  polarssl_printf( "passed\n" );
491 #endif /* POLARSSL_NET_C */
492 
493  if( verbose != 0 )
494  polarssl_printf( "\n" );
495 
496  return( 0 );
497 }
498 
499 #endif /* POLARSSL_SELF_TEST */
500 
501 #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.