]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/kernel/v2_0/tests/timeslice.c
6041da5d3b42022d32ce54409e7d07ea5191590a
[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 //
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):     nickg
44 // Contributors:  nickg
45 // Date:          2001-06-18
46 // Description:   A basic timeslicing test.
47 //
48 //####DESCRIPTIONEND####
49 //==========================================================================
50
51 #include <pkgconf/kernel.h>
52 #include <pkgconf/hal.h>
53
54 #include <cyg/hal/hal_arch.h>
55
56 #include <cyg/kernel/smp.hxx>
57
58 #include <cyg/kernel/kapi.h>
59
60 #include <cyg/infra/testcase.h>
61 #include <cyg/infra/diag.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*5
75
76 #define NTHREADS_MAX (CYGNUM_KERNEL_CPU_MAX*6)
77
78 static int ncpus = CYGNUM_KERNEL_CPU_MAX;
79
80 static char test_stack[STACK_SIZE];
81 static cyg_thread test_thread;
82 static cyg_handle_t main_thread;
83
84 static char stacks[NTHREADS_MAX][STACK_SIZE];
85 static cyg_thread test_threads[NTHREADS_MAX];
86 static cyg_handle_t threads[NTHREADS_MAX];
87
88 static volatile int failed = false;
89
90 static volatile cyg_uint32 slicerun[NTHREADS_MAX][CYGNUM_KERNEL_CPU_MAX];
91
92 //==========================================================================
93
94 void 
95 test_thread_timeslice(CYG_ADDRESS id)
96 {
97     for(;;)
98         slicerun[id][CYG_KERNEL_CPU_THIS()]++;
99 }
100
101 //==========================================================================
102
103 void run_test_timeslice(int nthread)
104 {
105     int i,j;
106     cyg_uint32 cpu_total[CYGNUM_KERNEL_CPU_MAX];
107     cyg_uint32 cpu_threads[CYGNUM_KERNEL_CPU_MAX];
108     cyg_uint32 thread_total[NTHREADS_MAX];
109
110     CYG_TEST_INFO( "Timeslice Test: Check timeslicing works");
111     
112     // Init flags.
113     for (i = 0;  i < nthread;  i++)
114         for( j = 0; j < ncpus; j++ )
115             slicerun[i][j] = 0;
116     
117     // Set my priority higher than any I plan to create
118     cyg_thread_set_priority(cyg_thread_self(), 2);
119
120     for (i = 0;  i < nthread;  i++) {
121         cyg_thread_create(10,              // Priority - just a number
122                           test_thread_timeslice, // entry
123                           i,               // index
124                           "test_thread",     // Name
125                           &stacks[i][0],   // Stack
126                           STACK_SIZE,      // Size
127                           &threads[i],     // Handle
128                           &test_threads[i] // Thread data structure
129             );
130         cyg_thread_resume( threads[i]);
131     }
132
133     // Just wait a while, until the threads have all run for a bit.
134     cyg_thread_delay( CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS*100 );
135
136     // Suspend all the threads
137     for (i = 0;  i < nthread;  i++) {
138         cyg_thread_suspend(threads[i]);
139     }
140
141     
142     // And check that a thread ran on each CPU, and that each thread
143     // ran.
144     
145     
146     diag_printf(" Thread ");
147     for( j = 0; j < ncpus; j++ )
148     {
149         cpu_total[j] = 0;
150         cpu_threads[j] = 0;
151         // "  %11d"  __123456789ab"
152         diag_printf("       CPU %2d",j);
153     }
154     // "  %11d"  __123456789ab"
155     diag_printf("        Total\n");
156     for (i = 0;  i < nthread;  i++)
157     {
158         thread_total[i] = 0;
159         diag_printf("     %2d ",i);
160         for( j = 0; j < ncpus; j++ )
161         {
162             thread_total[i] += slicerun[i][j];
163             cpu_total[j] += slicerun[i][j];
164             if( slicerun[i][j] > 0 )
165                 cpu_threads[j]++;
166             diag_printf("  %11d",slicerun[i][j]);
167         }
168         diag_printf("  %11d\n",thread_total[i]);
169         if( thread_total[i] == 0 )
170             failed++;
171     }
172     
173     diag_printf(" Total  ");
174     for( j = 0; j < ncpus; j++ )
175         diag_printf("  %11d",cpu_total[j]);
176     diag_printf("\n");
177     diag_printf("Threads ");
178     for( j = 0; j < ncpus; j++ )
179     {
180         diag_printf("  %11d",cpu_threads[j]);
181         if( cpu_threads[j] < 2 )
182             failed++;
183     }
184     diag_printf("\n");
185
186     // Delete all the threads
187     for (i = 0;  i < nthread;  i++) {
188         cyg_thread_delete(threads[i]);
189     }
190
191     CYG_TEST_INFO( "Timeslice Test: done");
192 }
193
194
195 //==========================================================================
196
197 void 
198 run_tests(CYG_ADDRESS id)
199 {
200     int step;
201     int nthread;
202     
203     // Try to run about 10 times in total, with varying numbers of threads
204     // from only one extra up to the full set:
205
206     step = (NTHREADS_MAX - (1 + CYG_KERNEL_CPU_COUNT()))/10;
207     if( step == 0 ) step = 1;
208     
209     for( nthread = 1 + CYG_KERNEL_CPU_COUNT() ;
210          nthread <= NTHREADS_MAX ;
211          nthread += step )
212             run_test_timeslice(nthread);
213
214     if( failed )
215         CYG_TEST_FAIL_FINISH("Timeslice test failed\n");
216     
217     CYG_TEST_PASS_FINISH("Timeslice test OK");    
218 }
219
220 //==========================================================================
221
222 void timeslice_main( void )
223 {
224     CYG_TEST_INIT();
225
226     // Work out how many CPUs we actually have.
227     ncpus = CYG_KERNEL_CPU_COUNT();
228
229     cyg_thread_create(0,              // Priority - just a number
230                       run_tests, // entry
231                       0,               // index
232                       "run_tests",     // Name
233                       test_stack,   // Stack
234                       STACK_SIZE,      // Size
235                       &main_thread,     // Handle
236                       &test_thread // Thread data structure
237         );
238     cyg_thread_resume( main_thread);
239     
240     cyg_scheduler_start();
241 }
242
243 //==========================================================================
244
245 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
246 externC void
247 cyg_hal_invoke_constructors();
248 #endif
249
250 externC void
251 cyg_start( void )
252 {
253 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
254     cyg_hal_invoke_constructors();
255 #endif
256     timeslice_main();
257 }   
258
259 //==========================================================================
260
261 #else // CYGSEM_KERNEL_SCHED_TIMESLICE etc
262
263 externC void
264 cyg_start( void )
265 {
266     CYG_TEST_INIT();
267     CYG_TEST_INFO("SMP test requires:\n"
268                 "CYGSEM_KERNEL_SCHED_TIMESLICE &&\n"
269                 "CYGPKG_KERNEL_SMP_SUPPORT &&\n"
270                 "CYGFUN_KERNEL_API_C && \n"
271                 "CYGSEM_KERNEL_SCHED_MLQUEUE &&\n"
272                 "CYGVAR_KERNEL_COUNTERS_CLOCK &&\n"
273                 "!CYGPKG_HAL_I386_LINUX &&\n"
274                 "!CYGDBG_INFRA_DIAG_USE_DEVICE &&\n"
275                 "(CYGNUM_KERNEL_SCHED_PRIORITIES > 12)\n");
276     CYG_TEST_NA("SMP test requirements");
277 }
278
279 #endif // CYGSEM_KERNEL_SCHED_TIMESLICE etc.
280
281 //==========================================================================
282 // EOF timeslice.c