]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/kernel/v2_0/tests/timeslice2.c
Initial revision
[karo-tx-redboot.git] / packages / kernel / v2_0 / tests / timeslice2.c
1 //==========================================================================
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 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:   An additional 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 hipri_stack[STACK_SIZE];
86 static cyg_thread hipri_thread_obj;
87 static cyg_handle_t hipri_thread;
88
89 static char stacks[NTHREADS_MAX][STACK_SIZE];
90 static cyg_thread test_threads[NTHREADS_MAX];
91 static cyg_handle_t threads[NTHREADS_MAX];
92
93 static volatile int failed = false;
94
95 static volatile cyg_uint32 slicerun[NTHREADS_MAX][CYGNUM_KERNEL_CPU_MAX];
96
97 //==========================================================================
98
99 void 
100 test_thread_timeslice(CYG_ADDRESS id)
101 {
102     for(;;)
103         slicerun[id][CYG_KERNEL_CPU_THIS()]++;
104 }
105
106 //==========================================================================
107
108 void run_test_timeslice(int nthread)
109 {
110     int i,j;
111     cyg_uint32 cpu_total[CYGNUM_KERNEL_CPU_MAX];
112     cyg_uint32 cpu_threads[CYGNUM_KERNEL_CPU_MAX];
113     cyg_uint32 thread_total[NTHREADS_MAX];
114
115     CYG_TEST_INFO( "Timeslice2 Test: Check timeslicing works under preemption");
116     
117     // Init flags.
118     for (i = 0;  i < nthread;  i++)
119         for( j = 0; j < ncpus; j++ )
120             slicerun[i][j] = 0;
121     
122     // Set my priority higher than any I plan to create
123     cyg_thread_set_priority(cyg_thread_self(), 2);
124
125     for (i = 0;  i < nthread;  i++) {
126         cyg_thread_create(10,              // Priority - just a number
127                           test_thread_timeslice, // entry
128                           i,               // index
129                           "test_thread",     // Name
130                           &stacks[i][0],   // Stack
131                           STACK_SIZE,      // Size
132                           &threads[i],     // Handle
133                           &test_threads[i] // Thread data structure
134             );
135         cyg_thread_resume( threads[i]);
136     }
137
138     // Just wait a while, until the threads have all run for a bit.
139     cyg_thread_delay( CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS*100 );
140
141     // Suspend all the threads
142     for (i = 0;  i < nthread;  i++) {
143         cyg_thread_suspend(threads[i]);
144     }
145
146     
147     // And check that a thread ran on each CPU, and that each thread
148     // ran.
149     
150     
151     diag_printf(" Thread ");
152     for( j = 0; j < ncpus; j++ )
153     {
154         cpu_total[j] = 0;
155         cpu_threads[j] = 0;
156         // "  %11d"  __123456789ab"
157         diag_printf("       CPU %2d",j);
158     }
159     // "  %11d"  __123456789ab"
160     diag_printf("        Total\n");
161     for (i = 0;  i < nthread;  i++)
162     {
163         thread_total[i] = 0;
164         diag_printf("     %2d ",i);
165         for( j = 0; j < ncpus; j++ )
166         {
167             thread_total[i] += slicerun[i][j];
168             cpu_total[j] += slicerun[i][j];
169             if( slicerun[i][j] > 0 )
170                 cpu_threads[j]++;
171             diag_printf("  %11d",slicerun[i][j]);
172         }
173         diag_printf("  %11d\n",thread_total[i]);
174         if( thread_total[i] == 0 )
175             failed++;
176     }
177     
178     diag_printf(" Total  ");
179     for( j = 0; j < ncpus; j++ )
180         diag_printf("  %11d",cpu_total[j]);
181     diag_printf("\n");
182     diag_printf("Threads ");
183     for( j = 0; j < ncpus; j++ )
184     {
185         diag_printf("  %11d",cpu_threads[j]);
186         if( cpu_threads[j] < 2 )
187             failed++;
188     }
189     diag_printf("\n");
190
191     // Delete all the threads
192     for (i = 0;  i < nthread;  i++) {
193         cyg_thread_delete(threads[i]);
194     }
195
196     CYG_TEST_INFO( "Timeslice2 Test: done");
197 }
198
199
200 //==========================================================================
201
202 void 
203 hipri_test(CYG_ADDRESS id)
204 {
205     while( 1 )
206     {
207         cyg_thread_delay( CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS/2 );
208     }
209 }
210
211 //==========================================================================
212
213 void 
214 run_tests(CYG_ADDRESS id)
215 {
216     int step;
217     int nthread;
218     
219     // Try to run about 10 times in total, with varying numbers of threads
220     // from only one extra up to the full set:
221
222     step = (NTHREADS_MAX - (1 + CYG_KERNEL_CPU_COUNT()))/10;
223     if( step == 0 ) step = 1;
224     
225     for( nthread = 1 + CYG_KERNEL_CPU_COUNT() ;
226          nthread <= NTHREADS_MAX ;
227          nthread += step )
228             run_test_timeslice(nthread);
229
230     if( failed )
231         CYG_TEST_FAIL_FINISH("Timeslice2 test failed\n");
232     
233     CYG_TEST_PASS_FINISH("Timeslice2 test OK");    
234 }
235
236 //==========================================================================
237
238 void timeslice_main( void )
239 {
240     CYG_TEST_INIT();
241
242     // Work out how many CPUs we actually have.
243     ncpus = CYG_KERNEL_CPU_COUNT();
244
245     cyg_thread_create(0,              // Priority - just a number
246                       run_tests, // entry
247                       0,               // index
248                       "run_tests",     // Name
249                       test_stack,   // Stack
250                       STACK_SIZE,      // Size
251                       &main_thread,     // Handle
252                       &test_thread // Thread data structure
253         );
254     cyg_thread_resume( main_thread);
255
256     cyg_thread_create(5,               // Priority - just a number
257                       hipri_test,      // entry
258                       0,               // index
259                       "hipri_run",     // Name
260                       hipri_stack,   // Stack
261                       STACK_SIZE,      // Size
262                       &hipri_thread,     // Handle
263                       &hipri_thread_obj // Thread data structure
264         );
265     cyg_thread_resume( hipri_thread);
266     
267     cyg_scheduler_start();
268 }
269
270 //==========================================================================
271
272 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
273 externC void
274 cyg_hal_invoke_constructors();
275 #endif
276
277 externC void
278 cyg_start( void )
279 {
280 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
281     cyg_hal_invoke_constructors();
282 #endif
283     timeslice_main();
284 }   
285
286 //==========================================================================
287
288 #else // CYGSEM_KERNEL_SCHED_TIMESLICE etc
289
290 externC void
291 cyg_start( void )
292 {
293     CYG_TEST_INIT();
294     CYG_TEST_INFO("Timeslice test requires:\n"
295                 "CYGSEM_KERNEL_SCHED_TIMESLICE &&\n"
296                 "CYGFUN_KERNEL_API_C && \n"
297                 "CYGSEM_KERNEL_SCHED_MLQUEUE &&\n"
298                 "CYGVAR_KERNEL_COUNTERS_CLOCK &&\n"
299                 "!CYGDBG_INFRA_DIAG_USE_DEVICE &&\n"
300                 "(CYGNUM_KERNEL_SCHED_PRIORITIES > 12)\n");
301     CYG_TEST_NA("Timeslice test requirements");
302 }
303
304 #endif // CYGSEM_KERNEL_SCHED_TIMESLICE etc.
305
306 //==========================================================================
307 // EOF timeslice2.c