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