1 //=================================================================
5 // Testcase for C library clock()
7 //=================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //=================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): ctarpy, jlarmour
46 // Description: Contains testcode for C library clock() function
49 //####DESCRIPTIONEND####
53 #include <pkgconf/libc_time.h> // Configuration header
54 #include <pkgconf/system.h>
55 #include <pkgconf/isoinfra.h>
56 #include <pkgconf/infra.h>
58 #include <cyg/infra/testcase.h>
60 // This test is bound to fail often on the synthetic target -- we
61 // don't have exclusive access to the CPU.
63 #if defined(CYGPKG_HAL_SYNTH)
64 # define NA_MSG "Timing accuracy not guaranteed on synthetic target"
65 #elif !defined(CYGINT_ISO_MAIN_STARTUP)
66 # define NA_MSG "Requires main() startup"
67 #elif defined(CYGDBG_USE_TRACING)
68 # define NA_MSG "Cannot give an accurate test when tracing is enabled"
76 CYG_TEST_NA( NA_MSG );
85 #include <cyg/infra/diag.h>
86 #include <cyg/hal/hal_cache.h>
87 #include <cyg/hal/hal_intr.h>
92 // This defines how many loops before we decide that
93 // the clock doesnt work
94 #define MAX_TIMEOUT 100000
96 // Percentage error before we declare fail: range 0 - 100
99 // Permissible absolute deviation from mean value to take care of incorrect
100 // failure conclusions in case of small mean values (if absolute values of
101 // clock() are small, percentage variation can be large)
102 #define FUDGE_FACTOR 6
104 // Number of samples to take
107 // We ignore ctrs[0] because it's always 0
108 // We ignore ctrs[1] because it will always be odd since it was
109 // the first measurement taken at the start of the looping, and
110 // the initial clock measurement (in clocks[0]) was not treated as
111 // part of the loop and therefore can't be considered to take the same
113 // We ignore ctrs[2] because it always seems to be substantially faster
114 // that the other samples. Probably due to cache/timing effect after the
116 // Finally, ctrs[3] is skipped because it's also very fast on ARM targets.
118 #define SKIPPED_SAMPLES 6
126 return (i < 0) ? -i : i;
130 // Clock measurement is done in a separate function so that alignment
131 // constraints are deterministic - some processors may perform better
132 // in loops that are better aligned, so by making it always the same
133 // function, this is prevented.
134 // FIXME: how do we guarantee the compiler won't inline this on -O3?
136 clock_loop( const int timeout, clock_t prevclock, clock_t *newclock )
141 for (i=0; i<timeout; i++) {
143 if ( c != prevclock )
144 break; // Hit the next clock pulse
148 CYG_TEST_FAIL_FINISH("No change in clock state!");
150 // it should not overflow in the lifetime of this test
152 CYG_TEST_FAIL_FINISH("Clock decremented!");
159 // both of these get zeroed out
160 static unsigned long ctrs[SAMPLES];
161 static clock_t clocks[SAMPLES];
164 main(int argc, char *argv[])
166 unsigned long mean=0, sum=0, maxerr=0;
172 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
175 // First disable the caches - they may affect the timing loops
176 // below - causing the elapsed time during the clock() call to vary.
178 register CYG_INTERRUPT_STATE oldints;
180 HAL_DISABLE_INTERRUPTS(oldints);
182 HAL_ICACHE_DISABLE();
183 HAL_DCACHE_DISABLE();
185 HAL_ICACHE_INVALIDATE_ALL();
186 HAL_DCACHE_INVALIDATE_ALL();
187 HAL_RESTORE_INTERRUPTS(oldints);
190 // This waits for a clock tick, to ensure that we are at the
191 // start of a clock period. Then sit in a tight loop to get
192 // the clock period. Repeat this, and make sure that it the
193 // two timed periods are acceptably close.
197 if (clocks[0] == (clock_t)-1) // unimplemented is potentially valid.
199 #ifdef CYGSEM_LIBC_TIME_CLOCK_WORKING
200 CYG_TEST_FAIL_FINISH( "clock() returns -1, meaning unimplemented");
202 CYG_TEST_PASS_FINISH( "clock() returns -1, meaning unimplemented");
206 // record clocks in a tight consistent loop to avoid random variations
207 for (i=1; i<SAMPLES; i++) {
208 ctrs[i] = clock_loop( MAX_TIMEOUT, clocks[i-1], &clocks[i] );
211 for (i=0;i<SAMPLES;i++) {
212 // output what we got - useful for diagnostics of occasional
214 diag_printf("clocks[%d] = %d, ctrs[%d] = %d\n", i, clocks[i],
217 // Now we work out the error etc.
218 if (i>=SKIPPED_SAMPLES) {
223 // deduce out the average
224 mean = sum / (SAMPLES-SKIPPED_SAMPLES);
226 // now go through valid results and compare against average
227 for (i=SKIPPED_SAMPLES;i<SAMPLES;i++) {
230 absdev = my_abs(ctrs[i]-mean);
231 // use mean+1 as divisor to avoid div-by-zero
232 err = (100 * absdev) / (mean+1);
233 if (err > TOLERANCE && absdev > FUDGE_FACTOR) {
234 diag_printf("mean=%d, ctrs[%d]=%d, err=%d\n", mean, i, ctrs[i],
236 CYG_TEST_FAIL_FINISH("clock() within tolerance");
242 diag_printf("mean=%d, maxerr=%d\n", mean, maxerr);
243 CYG_TEST_PASS_FINISH("clock() stable");
247 #endif // ifndef NA_MSG