//================================================================= // // clock.c // // Testcase for C library clock() // //================================================================= // ####ECOSGPLCOPYRIGHTBEGIN#### // ------------------------------------------- // This file is part of eCos, the Embedded Configurable Operating System. // Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. // // eCos is free software; you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free // Software Foundation; either version 2 or (at your option) any later // version. // // eCos is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // for more details. // // You should have received a copy of the GNU General Public License // along with eCos; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // As a special exception, if other files instantiate templates or use // macros or inline functions from this file, or you compile this file // and link it with other works to produce a work based on this file, // this file does not by itself cause the resulting work to be covered by // the GNU General Public License. However the source code for this file // must still be made available in accordance with section (3) of the GNU // General Public License v2. // // This exception does not invalidate any other reasons why a work based // on this file might be covered by the GNU General Public License. // ------------------------------------------- // ####ECOSGPLCOPYRIGHTEND#### //================================================================= //#####DESCRIPTIONBEGIN#### // // Author(s): ctarpy, jlarmour // Contributors: // Date: 1999-03-05 // Description: Contains testcode for C library clock() function // // //####DESCRIPTIONEND#### // CONFIGURATION #include // Configuration header #include #include #include #include // This test is bound to fail often on the synthetic target -- we // don't have exclusive access to the CPU. #if defined(CYGPKG_HAL_SYNTH) # define NA_MSG "Timing accuracy not guaranteed on synthetic target" #elif !defined(CYGINT_ISO_MAIN_STARTUP) # define NA_MSG "Requires main() startup" #elif defined(CYGDBG_USE_TRACING) # define NA_MSG "Cannot give an accurate test when tracing is enabled" #endif #ifdef NA_MSG void cyg_start(void) { CYG_TEST_INIT(); CYG_TEST_NA( NA_MSG ); } #else // INCLUDES #include #include #include #include // CONSTANTS // This defines how many loops before we decide that // the clock doesnt work #define MAX_TIMEOUT 100000 // Percentage error before we declare fail: range 0 - 100 #define TOLERANCE 40 // Permissible absolute deviation from mean value to take care of incorrect // failure conclusions in case of small mean values (if absolute values of // clock() are small, percentage variation can be large) #define FUDGE_FACTOR 6 // Number of samples to take #define SAMPLES 30 // We ignore ctrs[0] because it's always 0 // We ignore ctrs[1] because it will always be odd since it was // the first measurement taken at the start of the looping, and // the initial clock measurement (in clocks[0]) was not treated as // part of the loop and therefore can't be considered to take the same // time. // We ignore ctrs[2] because it always seems to be substantially faster // that the other samples. Probably due to cache/timing effect after the // previous loop. // Finally, ctrs[3] is skipped because it's also very fast on ARM targets. #define SKIPPED_SAMPLES 6 // FUNCTIONS static int my_abs(int i) { return (i < 0) ? -i : i; } // my_abs() // Clock measurement is done in a separate function so that alignment // constraints are deterministic - some processors may perform better // in loops that are better aligned, so by making it always the same // function, this is prevented. // FIXME: how do we guarantee the compiler won't inline this on -O3? static unsigned long clock_loop( const int timeout, clock_t prevclock, clock_t *newclock ) { clock_t c=0; long i; for (i=0; i=SKIPPED_SAMPLES) { sum += ctrs[i]; } } // deduce out the average mean = sum / (SAMPLES-SKIPPED_SAMPLES); // now go through valid results and compare against average for (i=SKIPPED_SAMPLES;i TOLERANCE && absdev > FUDGE_FACTOR) { diag_printf("mean=%d, ctrs[%d]=%d, err=%d\n", mean, i, ctrs[i], err); CYG_TEST_FAIL_FINISH("clock() within tolerance"); } if (err > maxerr) maxerr=err; } diag_printf("mean=%d, maxerr=%d\n", mean, maxerr); CYG_TEST_PASS_FINISH("clock() stable"); } // main() #endif // ifndef NA_MSG // EOF clock.c