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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [tests/] [timeslice2.c] - Blame information for rev 838

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//        timeslice2.c
4
//
5
//        Timeslice 2 test
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, 2006 Free Software Foundation, 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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     nickg
43
// Contributors:  nickg
44
// Date:          2001-06-18
45
// Description:   An additional timeslicing test.
46
//
47
//####DESCRIPTIONEND####
48
//==========================================================================
49
 
50
#include <pkgconf/kernel.h>
51
#include <pkgconf/hal.h>
52
 
53
#include <cyg/hal/hal_arch.h>
54
 
55
#include <cyg/kernel/smp.hxx>
56
 
57
#include <cyg/kernel/kapi.h>
58
 
59
#include <cyg/infra/testcase.h>
60
#include <cyg/infra/diag.h>
61
#include CYGHWR_MEMORY_LAYOUT_H
62
 
63
//==========================================================================
64
 
65
#if defined(CYGSEM_KERNEL_SCHED_TIMESLICE) &&   \
66
    defined(CYGFUN_KERNEL_API_C) &&             \
67
    defined(CYGSEM_KERNEL_SCHED_MLQUEUE) &&     \
68
    defined(CYGVAR_KERNEL_COUNTERS_CLOCK) &&    \
69
    !defined(CYGDBG_INFRA_DIAG_USE_DEVICE) &&   \
70
    (CYGNUM_KERNEL_SCHED_PRIORITIES > 12)
71
 
72
//==========================================================================
73
 
74
#define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
75
 
76
#if (CYGMEM_REGION_ram_SIZE <= 32768)
77
#define NTHREADS_MAX (CYGNUM_KERNEL_CPU_MAX*3)
78
#else
79
#define NTHREADS_MAX (CYGNUM_KERNEL_CPU_MAX*6)
80
#endif
81
 
82
static int ncpus = CYGNUM_KERNEL_CPU_MAX;
83
 
84
static char test_stack[STACK_SIZE];
85
static cyg_thread test_thread;
86
static cyg_handle_t main_thread;
87
 
88
static char hipri_stack[STACK_SIZE];
89
static cyg_thread hipri_thread_obj;
90
static cyg_handle_t hipri_thread;
91
 
92
static char stacks[NTHREADS_MAX][STACK_SIZE];
93
static cyg_thread test_threads[NTHREADS_MAX];
94
static cyg_handle_t threads[NTHREADS_MAX];
95
 
96
static volatile int failed = false;
97
 
98
static volatile cyg_uint32 slicerun[NTHREADS_MAX][CYGNUM_KERNEL_CPU_MAX];
99
 
100
//==========================================================================
101
 
102
void
103
test_thread_timeslice(CYG_ADDRESS id)
104
{
105
    for(;;)
106
        slicerun[id][CYG_KERNEL_CPU_THIS()]++;
107
}
108
 
109
//==========================================================================
110
 
111
void run_test_timeslice(int nthread)
112
{
113
    int i,j;
114
    cyg_uint32 cpu_total[CYGNUM_KERNEL_CPU_MAX];
115
    cyg_uint32 cpu_threads[CYGNUM_KERNEL_CPU_MAX];
116
    cyg_uint32 thread_total[NTHREADS_MAX];
117
 
118
    CYG_TEST_INFO( "Timeslice2 Test: Check timeslicing works under preemption");
119
 
120
    // Init flags.
121
    for (i = 0;  i < nthread;  i++)
122
        for( j = 0; j < ncpus; j++ )
123
            slicerun[i][j] = 0;
124
 
125
    // Set my priority higher than any I plan to create
126
    cyg_thread_set_priority(cyg_thread_self(), 2);
127
 
128
    for (i = 0;  i < nthread;  i++) {
129
        cyg_thread_create(10,              // Priority - just a number
130
                          test_thread_timeslice, // entry
131
                          i,               // index
132
                          "test_thread",     // Name
133
                          &stacks[i][0],   // Stack
134
                          STACK_SIZE,      // Size
135
                          &threads[i],     // Handle
136
                          &test_threads[i] // Thread data structure
137
            );
138
        cyg_thread_resume( threads[i]);
139
    }
140
 
141
    // Just wait a while, until the threads have all run for a bit.
142
    cyg_thread_delay( CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS*100 );
143
 
144
    // Suspend all the threads
145
    for (i = 0;  i < nthread;  i++) {
146
        cyg_thread_suspend(threads[i]);
147
    }
148
 
149
 
150
    // And check that a thread ran on each CPU, and that each thread
151
    // ran.
152
 
153
 
154
    diag_printf(" Thread ");
155
    for( j = 0; j < ncpus; j++ )
156
    {
157
        cpu_total[j] = 0;
158
        cpu_threads[j] = 0;
159
        // "  %11d"  __123456789ab"
160
        diag_printf("       CPU %2d",j);
161
    }
162
    // "  %11d"  __123456789ab"
163
    diag_printf("        Total\n");
164
    for (i = 0;  i < nthread;  i++)
165
    {
166
        thread_total[i] = 0;
167
        diag_printf("     %2d ",i);
168
        for( j = 0; j < ncpus; j++ )
169
        {
170
            thread_total[i] += slicerun[i][j];
171
            cpu_total[j] += slicerun[i][j];
172
            if( slicerun[i][j] > 0 )
173
                cpu_threads[j]++;
174
            diag_printf("  %11d",slicerun[i][j]);
175
        }
176
        diag_printf("  %11d\n",thread_total[i]);
177
        if( thread_total[i] == 0 )
178
            failed++;
179
    }
180
 
181
    diag_printf(" Total  ");
182
    for( j = 0; j < ncpus; j++ )
183
        diag_printf("  %11d",cpu_total[j]);
184
    diag_printf("\n");
185
    diag_printf("Threads ");
186
    for( j = 0; j < ncpus; j++ )
187
    {
188
        diag_printf("  %11d",cpu_threads[j]);
189
        if( cpu_threads[j] < 2 )
190
            failed++;
191
    }
192
    diag_printf("\n");
193
 
194
    // Delete all the threads
195
    for (i = 0;  i < nthread;  i++) {
196
        cyg_thread_delete(threads[i]);
197
    }
198
 
199
    CYG_TEST_INFO( "Timeslice2 Test: done");
200
}
201
 
202
 
203
//==========================================================================
204
 
205
void
206
hipri_test(CYG_ADDRESS id)
207
{
208
    while( 1 )
209
    {
210
        cyg_thread_delay( CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS/2 );
211
    }
212
}
213
 
214
//==========================================================================
215
 
216
void
217
run_tests(CYG_ADDRESS id)
218
{
219
    int step;
220
    int nthread;
221
 
222
    // Try to run about 10 times in total, with varying numbers of threads
223
    // from only one extra up to the full set:
224
 
225
    step = (NTHREADS_MAX - (1 + CYG_KERNEL_CPU_COUNT()))/10;
226
    if( step == 0 ) step = 1;
227
 
228
    for( nthread = 1 + CYG_KERNEL_CPU_COUNT() ;
229
         nthread <= NTHREADS_MAX ;
230
         nthread += step )
231
            run_test_timeslice(nthread);
232
 
233
    if( failed )
234
        CYG_TEST_FAIL_FINISH("Timeslice2 test failed\n");
235
 
236
    CYG_TEST_PASS_FINISH("Timeslice2 test OK");
237
}
238
 
239
//==========================================================================
240
 
241
void timeslice_main( void )
242
{
243
    CYG_TEST_INIT();
244
 
245
    // Work out how many CPUs we actually have.
246
    ncpus = CYG_KERNEL_CPU_COUNT();
247
 
248
    cyg_thread_create(0,              // Priority - just a number
249
                      run_tests, // entry
250
                      0,               // index
251
                      "run_tests",     // Name
252
                      test_stack,   // Stack
253
                      STACK_SIZE,      // Size
254
                      &main_thread,     // Handle
255
                      &test_thread // Thread data structure
256
        );
257
    cyg_thread_resume( main_thread);
258
 
259
    cyg_thread_create(5,               // Priority - just a number
260
                      hipri_test,      // entry
261
                      0,               // index
262
                      "hipri_run",     // Name
263
                      hipri_stack,   // Stack
264
                      STACK_SIZE,      // Size
265
                      &hipri_thread,     // Handle
266
                      &hipri_thread_obj // Thread data structure
267
        );
268
    cyg_thread_resume( hipri_thread);
269
 
270
    cyg_scheduler_start();
271
}
272
 
273
//==========================================================================
274
 
275
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
276
externC void
277
cyg_hal_invoke_constructors();
278
#endif
279
 
280
externC void
281
cyg_start( void )
282
{
283
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
284
    cyg_hal_invoke_constructors();
285
#endif
286
    timeslice_main();
287
}
288
 
289
//==========================================================================
290
 
291
#else // CYGSEM_KERNEL_SCHED_TIMESLICE etc
292
 
293
externC void
294
cyg_start( void )
295
{
296
    CYG_TEST_INIT();
297
    CYG_TEST_INFO("Timeslice test requires:\n"
298
                "CYGSEM_KERNEL_SCHED_TIMESLICE &&\n"
299
                "CYGFUN_KERNEL_API_C && \n"
300
                "CYGSEM_KERNEL_SCHED_MLQUEUE &&\n"
301
                "CYGVAR_KERNEL_COUNTERS_CLOCK &&\n"
302
                "!CYGDBG_INFRA_DIAG_USE_DEVICE &&\n"
303
                "(CYGNUM_KERNEL_SCHED_PRIORITIES > 12)\n");
304
    CYG_TEST_NA("Timeslice test requirements");
305
}
306
 
307
#endif // CYGSEM_KERNEL_SCHED_TIMESLICE etc.
308
 
309
//==========================================================================
310
// EOF timeslice2.c

powered by: WebSVN 2.1.0

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