blob: b387bd87fe3190de458f34a070882267a7813428 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Portable interface to the CPU cycle counter
3 *
Paul Bakkerf2561b32014-02-06 15:11:55 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010032#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
Paul Bakkerf2561b32014-02-06 15:11:55 +010039#if defined(POLARSSL_TIMING_C) && !defined(POLARSSL_TIMING_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Paul Bakkerfa6a6202013-10-28 18:48:30 +010043#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000044
45#include <windows.h>
46#include <winbase.h>
47
48struct _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
61struct _hr_time
62{
63 struct timeval start;
64};
65
Paul Bakker9af723c2014-05-01 13:03:14 +020066#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakkerbb0139c2012-10-31 09:53:08 +000068#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020069 ( defined(_MSC_VER) && defined(_M_IX86) ) || defined(__WATCOMC__)
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakkerbb0139c2012-10-31 09:53:08 +000071#define POLARSSL_HAVE_HARDCLOCK
72
Paul Bakker5121ce52009-01-03 21:22:43 +000073unsigned long hardclock( void )
74{
75 unsigned long tsc;
76 __asm rdtsc
77 __asm mov [tsc], eax
78 return( tsc );
79}
Paul Bakker9af723c2014-05-01 13:03:14 +020080#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
81 ( _MSC_VER && _M_IX86 ) || __WATCOMC__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Paul Bakkerbb0139c2012-10-31 09:53:08 +000083#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
84 defined(__GNUC__) && defined(__i386__)
85
86#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88unsigned long hardclock( void )
89{
Paul Bakkerca410102011-10-19 14:27:36 +000090 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +010091 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakkerca410102011-10-19 14:27:36 +000092 return( lo );
Paul Bakker5121ce52009-01-03 21:22:43 +000093}
Paul Bakker9af723c2014-05-01 13:03:14 +020094#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
95 __GNUC__ && __i386__ */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Paul Bakkerbb0139c2012-10-31 09:53:08 +000097#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +020098 defined(__GNUC__) && ( defined(__amd64__) || defined(__x86_64__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +000099
100#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102unsigned long hardclock( void )
103{
104 unsigned long lo, hi;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100105 asm volatile( "rdtsc" : "=a" (lo), "=d" (hi) );
Paul Bakker66d5d072014-06-17 16:39:18 +0200106 return( lo | ( hi << 32 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107}
Paul Bakker9af723c2014-05-01 13:03:14 +0200108#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
109 __GNUC__ && ( __amd64__ || __x86_64__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000111#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
Paul Bakker66d5d072014-06-17 16:39:18 +0200112 defined(__GNUC__) && ( defined(__powerpc__) || defined(__ppc__) )
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000113
114#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116unsigned long hardclock( void )
117{
118 unsigned long tbl, tbu0, tbu1;
119
120 do
121 {
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100122 asm volatile( "mftbu %0" : "=r" (tbu0) );
123 asm volatile( "mftb %0" : "=r" (tbl ) );
124 asm volatile( "mftbu %0" : "=r" (tbu1) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 }
126 while( tbu0 != tbu1 );
127
128 return( tbl );
129}
Paul Bakker9af723c2014-05-01 13:03:14 +0200130#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
131 __GNUC__ && ( __powerpc__ || __ppc__ ) */
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000133#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
Paul Bakker5121ce52009-01-03 21:22:43 +0000138#else
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000139#define POLARSSL_HAVE_HARDCLOCK
140
141unsigned long hardclock( void )
142{
143 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100144 asm volatile( "rdpr %%tick, %0;" : "=&r" (tick) );
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000145 return( tick );
146}
Paul Bakker9af723c2014-05-01 13:03:14 +0200147#endif /* __OpenBSD__ */
148#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
149 __GNUC__ && __sparc64__ */
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000150
151#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
152 defined(__GNUC__) && defined(__sparc__) && !defined(__sparc64__)
153
154#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156unsigned long hardclock( void )
157{
158 unsigned long tick;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100159 asm volatile( ".byte 0x83, 0x41, 0x00, 0x00" );
160 asm volatile( "mov %%g1, %0" : "=r" (tick) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 return( tick );
162}
Paul Bakker9af723c2014-05-01 13:03:14 +0200163#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
164 __GNUC__ && __sparc__ && !__sparc64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000166#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
167 defined(__GNUC__) && defined(__alpha__)
168
169#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171unsigned long hardclock( void )
172{
173 unsigned long cc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100174 asm volatile( "rpcc %0" : "=r" (cc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 return( cc & 0xFFFFFFFF );
176}
Paul Bakker9af723c2014-05-01 13:03:14 +0200177#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
178 __GNUC__ && __alpha__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000180#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(POLARSSL_HAVE_ASM) && \
181 defined(__GNUC__) && defined(__ia64__)
182
183#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185unsigned long hardclock( void )
186{
187 unsigned long itc;
Manuel Pégourié-Gonnardd6aebe12014-03-27 21:15:40 +0100188 asm volatile( "mov %0 = ar.itc" : "=r" (itc) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 return( itc );
190}
Paul Bakker9af723c2014-05-01 13:03:14 +0200191#endif /* !POLARSSL_HAVE_HARDCLOCK && POLARSSL_HAVE_ASM &&
192 __GNUC__ && __ia64__ */
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100194#if !defined(POLARSSL_HAVE_HARDCLOCK) && defined(_MSC_VER) && \
195 !defined(EFIX64) && !defined(EFI32)
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000196
197#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker2eee9022011-04-24 15:28:55 +0000198
199unsigned long hardclock( void )
200{
201 LARGE_INTEGER offset;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100202
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100203 QueryPerformanceCounter( &offset );
Paul Bakker2eee9022011-04-24 15:28:55 +0000204
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200205 return( (unsigned long)( offset.QuadPart ) );
Paul Bakker2eee9022011-04-24 15:28:55 +0000206}
Paul Bakker9af723c2014-05-01 13:03:14 +0200207#endif /* !POLARSSL_HAVE_HARDCLOCK && _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker2eee9022011-04-24 15:28:55 +0000208
Paul Bakkerbb0139c2012-10-31 09:53:08 +0000209#if !defined(POLARSSL_HAVE_HARDCLOCK)
210
211#define POLARSSL_HAVE_HARDCLOCK
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213static int hardclock_init = 0;
214static struct timeval tv_init;
215
216unsigned 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}
Paul Bakker9af723c2014-05-01 13:03:14 +0200230#endif /* !POLARSSL_HAVE_HARDCLOCK */
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakker2eee9022011-04-24 15:28:55 +0000232volatile int alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100234#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236unsigned 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
255DWORD WINAPI TimerProc( LPVOID uElapse )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100256{
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 Sleep( (DWORD) uElapse );
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100258 alarmed = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 return( TRUE );
260}
261
262void set_alarm( int seconds )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100263{
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 DWORD ThreadId;
265
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100266 alarmed = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 CloseHandle( CreateThread( NULL, 0, TimerProc,
268 (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
269}
270
271void m_sleep( int milliseconds )
272{
273 Sleep( milliseconds );
274}
275
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100276#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278unsigned 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
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 if( reset )
287 {
288 t->start.tv_sec = offset.tv_sec;
289 t->start.tv_usec = offset.tv_usec;
Alfred Klompb308dd72014-07-14 22:32:21 +0200290 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 }
292
Alfred Klompb308dd72014-07-14 22:32:21 +0200293 delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
294 + ( offset.tv_usec - t->start.tv_usec ) / 1000;
295
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 return( delta );
297}
298
Paul Bakker49d75672012-09-26 15:22:07 +0000299#if defined(INTEGRITY)
300void m_sleep( int milliseconds )
301{
302 usleep( milliseconds * 1000 );
303}
304
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100305#else /* INTEGRITY */
Paul Bakker49d75672012-09-26 15:22:07 +0000306
Paul Bakker5121ce52009-01-03 21:22:43 +0000307static void sighandler( int signum )
Manuel Pégourié-Gonnard487588d2014-03-27 19:02:07 +0100308{
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 alarmed = 1;
310 signal( signum, sighandler );
311}
312
313void set_alarm( int seconds )
314{
315 alarmed = 0;
316 signal( SIGALRM, sighandler );
317 alarm( seconds );
318}
319
320void m_sleep( int milliseconds )
321{
322 struct timeval tv;
323
324 tv.tv_sec = milliseconds / 1000;
Manuel Pégourié-Gonnarddfbf9c72014-02-20 22:16:43 +0100325 tv.tv_usec = ( milliseconds % 1000 ) * 1000;
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327 select( 0, NULL, NULL, NULL, &tv );
328}
Paul Bakker49d75672012-09-26 15:22:07 +0000329#endif /* INTEGRITY */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100331#endif /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100333#if defined(POLARSSL_SELF_TEST)
334
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200335/* To test net_usleep against our functions */
Manuel Pégourié-Gonnard462906f2014-07-21 17:37:01 +0200336#if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200337#include "polarssl/net.h"
338#endif
339
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100340/*
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200341 * Busy-waits for the given number of milliseconds.
342 * Used for testing hardclock.
343 */
344static 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/*
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100360 * Checkup routine
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200361 *
362 * Warning: this is work in progress, some tests may not be reliable enough
363 * yet! False positives may happen.
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100364 */
365int 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
Paul Bakker66d5d072014-06-17 16:39:18 +0200372 if( verbose != 0 )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200373 polarssl_printf( " TIMING tests note: will take some time!\n" );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100374
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
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200382 m_sleep( (int)( 500 * secs ) );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100383
384 millisecs = get_timer( &hires, 0 );
385
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200386 if( millisecs < 450 * secs || millisecs > 550 * secs )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100387 {
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
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200405 set_alarm( (int) secs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100406 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 )
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200424 polarssl_printf( " TIMING test #3 (hardclock / get_timer): " );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100425
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
433hard_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 */
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200443 millisecs = 1;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100444 cycles = hardclock();
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200445 busy_msleep( millisecs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100446 cycles = hardclock() - cycles;
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200447 ratio = cycles / millisecs;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100448
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200449 /* Check that the ratio is mostly constant */
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100450 for( millisecs = 2; millisecs <= 4; millisecs++ )
451 {
452 cycles = hardclock();
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200453 busy_msleep( millisecs );
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100454 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
Manuel Pégourié-Gonnard462906f2014-07-21 17:37:01 +0200468#if defined(POLARSSL_NET_C) && defined(POLARSSL_HAVE_TIME)
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200469 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" );
Paul Bakker9af723c2014-05-01 13:03:14 +0200491#endif /* POLARSSL_NET_C */
Manuel Pégourié-Gonnard79e58422014-04-02 18:42:01 +0200492
Manuel Pégourié-Gonnarde1ac0f82014-05-28 11:44:20 +0200493 if( verbose != 0 )
494 polarssl_printf( "\n" );
495
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100496 return( 0 );
497}
498
499#endif /* POLARSSL_SELF_TEST */
500
501#endif /* POLARSSL_TIMING_C && !POLARSSL_TIMING_ALT */