OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [language/] [c/] [libc/] [time/] [v2_0/] [tests/] [clock.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//=================================================================
2
//
3
//        clock.c
4
//
5
//        Testcase for C library clock()
6
//
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.
12
//
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.
16
//
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
20
// for more details.
21
//
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.
25
//
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.
32
//
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.
35
//
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####
42
//
43
// Author(s):     ctarpy, jlarmour
44
// Contributors:  
45
// Date:          1999-03-05
46
// Description:   Contains testcode for C library clock() function
47
//
48
//
49
//####DESCRIPTIONEND####
50
 
51
// CONFIGURATION
52
 
53
#include <pkgconf/libc_time.h>   // Configuration header
54
#include <pkgconf/system.h>
55
#include <pkgconf/isoinfra.h>
56
#include <pkgconf/infra.h>
57
 
58
#include <cyg/infra/testcase.h>
59
 
60
// This test is bound to fail often on the synthetic target -- we
61
// don't have exclusive access to the CPU.
62
 
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"
69
#endif
70
 
71
#ifdef NA_MSG
72
void
73
cyg_start(void)
74
{
75
    CYG_TEST_INIT();
76
    CYG_TEST_NA( NA_MSG );
77
}
78
 
79
#else
80
 
81
 
82
// INCLUDES
83
 
84
#include <time.h>
85
#include <cyg/infra/diag.h>
86
#include <cyg/hal/hal_cache.h>
87
#include <cyg/hal/hal_intr.h>
88
 
89
 
90
// CONSTANTS
91
 
92
// This defines how many loops before we decide that
93
// the clock doesnt work
94
#define MAX_TIMEOUT 100000
95
 
96
// Percentage error before we declare fail: range 0 - 100
97
#define TOLERANCE 40
98
 
99
// Number of samples to take
100
#define SAMPLES 30
101
 
102
// We ignore ctrs[0] because it's always 0
103
// We ignore ctrs[1] because it will always be odd since it was
104
// the first measurement taken at the start of the looping, and
105
// the initial clock measurement (in clocks[0]) was not treated as
106
// part of the loop and therefore can't be considered to take the same
107
// time.
108
// We ignore ctrs[2] because it always seems to be substantially faster
109
// that the other samples. Probably due to cache/timing effect after the
110
// previous loop.
111
// Finally, ctrs[3] is skipped because it's also very fast on ARM targets.
112
 
113
#define SKIPPED_SAMPLES 6
114
 
115
 
116
// FUNCTIONS
117
 
118
static int
119
my_abs(int i)
120
{
121
    return (i < 0) ? -i : i;
122
} // my_abs()
123
 
124
 
125
// Clock measurement is done in a separate function so that alignment
126
// constraints are deterministic - some processors may perform better
127
// in loops that are better aligned, so by making it always the same
128
// function, this is prevented.
129
// FIXME: how do we guarantee the compiler won't inline this on -O3?
130
static unsigned long
131
clock_loop( const int timeout, clock_t prevclock, clock_t *newclock )
132
{
133
    clock_t c=0;
134
    long i;
135
 
136
    for (i=0; i<timeout; i++) {
137
        c = clock();
138
        if ( c != prevclock )
139
            break; // Hit the next clock pulse
140
    }
141
 
142
    if (i==timeout)
143
        CYG_TEST_FAIL_FINISH("No change in clock state!");
144
 
145
    // it should not overflow in the lifetime of this test
146
    if (c < prevclock)
147
        CYG_TEST_FAIL_FINISH("Clock decremented!");
148
 
149
    *newclock = c;
150
 
151
    return i;
152
} // clock_loop()
153
 
154
// both of these get zeroed out
155
static unsigned long ctrs[SAMPLES];
156
static clock_t clocks[SAMPLES];
157
 
158
int
159
main(int argc, char *argv[])
160
{
161
    unsigned long mean=0, sum=0, maxerr=0;
162
    int i;
163
 
164
    CYG_TEST_INIT();
165
 
166
    CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
167
                  "clock() function");
168
 
169
    // First disable the caches - they may affect the timing loops
170
    // below - causing the elapsed time during the clock() call to vary.
171
    {
172
        register CYG_INTERRUPT_STATE oldints;
173
 
174
        HAL_DISABLE_INTERRUPTS(oldints);
175
        HAL_DCACHE_SYNC();
176
        HAL_ICACHE_DISABLE();
177
        HAL_DCACHE_DISABLE();
178
        HAL_DCACHE_SYNC();
179
        HAL_ICACHE_INVALIDATE_ALL();
180
        HAL_DCACHE_INVALIDATE_ALL();
181
        HAL_RESTORE_INTERRUPTS(oldints);
182
    }
183
 
184
    // This waits for a clock tick, to ensure that we are at the
185
    // start of a clock period. Then sit in a tight loop to get
186
    // the clock period. Repeat this, and make sure that it the
187
    // two timed periods are acceptably close.
188
 
189
    clocks[0] = clock();
190
 
191
    if (clocks[0] == (clock_t)-1)  // unimplemented is potentially valid.
192
    {
193
#ifdef CYGSEM_LIBC_TIME_CLOCK_WORKING
194
        CYG_TEST_FAIL_FINISH( "clock() returns -1, meaning unimplemented");
195
#else
196
        CYG_TEST_PASS_FINISH( "clock() returns -1, meaning unimplemented");
197
#endif
198
    } // if
199
 
200
    // record clocks in a tight consistent loop to avoid random variations
201
    for (i=1; i<SAMPLES; i++) {
202
        ctrs[i] = clock_loop( MAX_TIMEOUT, clocks[i-1], &clocks[i] );
203
    }
204
 
205
    for (i=0;i<SAMPLES;i++) {
206
        // output what we got - useful for diagnostics of occasional
207
        // test failures
208
        diag_printf("clocks[%d] = %d, ctrs[%d] = %d\n", i, clocks[i],
209
                    i, ctrs[i]);
210
 
211
        // Now we work out the error etc.
212
        if (i>=SKIPPED_SAMPLES) {
213
            sum += ctrs[i];
214
        }
215
    }
216
 
217
    // deduce out the average
218
    mean = sum / (SAMPLES-SKIPPED_SAMPLES);
219
 
220
    // now go through valid results and compare against average
221
    for (i=SKIPPED_SAMPLES;i<SAMPLES;i++) {
222
        unsigned long err;
223
 
224
        err = (100 * my_abs(ctrs[i]-mean)) / mean;
225
        if (err > TOLERANCE) {
226
            diag_printf("mean=%d, ctrs[%d]=%d, err=%d\n", mean, i, ctrs[i],
227
                        err);
228
            CYG_TEST_FAIL_FINISH("clock() within tolerance");
229
        }
230
        if (err > maxerr)
231
            maxerr=err;
232
    }
233
 
234
    diag_printf("mean=%d, maxerr=%d\n", mean, maxerr);
235
    CYG_TEST_PASS_FINISH("clock() stable");
236
 
237
} // main()
238
 
239
#endif // ifndef NA_MSG
240
 
241
// EOF clock.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.