1 //==========================================================================
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.
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.
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
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.
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.
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.
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####
44 // Contributors: nickg
46 // Description: A basic timeslicing test.
48 //####DESCRIPTIONEND####
49 //==========================================================================
51 #include <pkgconf/kernel.h>
52 #include <pkgconf/hal.h>
54 #include <cyg/hal/hal_arch.h>
56 #include <cyg/kernel/smp.hxx>
58 #include <cyg/kernel/kapi.h>
60 #include <cyg/infra/testcase.h>
61 #include <cyg/infra/diag.h>
63 //==========================================================================
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)
72 //==========================================================================
74 #define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL*5
76 #define NTHREADS_MAX (CYGNUM_KERNEL_CPU_MAX*6)
78 static int ncpus = CYGNUM_KERNEL_CPU_MAX;
80 static char test_stack[STACK_SIZE];
81 static cyg_thread test_thread;
82 static cyg_handle_t main_thread;
84 static char stacks[NTHREADS_MAX][STACK_SIZE];
85 static cyg_thread test_threads[NTHREADS_MAX];
86 static cyg_handle_t threads[NTHREADS_MAX];
88 static volatile int failed = false;
90 static volatile cyg_uint32 slicerun[NTHREADS_MAX][CYGNUM_KERNEL_CPU_MAX];
92 //==========================================================================
95 test_thread_timeslice(CYG_ADDRESS id)
98 slicerun[id][CYG_KERNEL_CPU_THIS()]++;
101 //==========================================================================
103 void run_test_timeslice(int nthread)
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];
110 CYG_TEST_INFO( "Timeslice Test: Check timeslicing works");
113 for (i = 0; i < nthread; i++)
114 for( j = 0; j < ncpus; j++ )
117 // Set my priority higher than any I plan to create
118 cyg_thread_set_priority(cyg_thread_self(), 2);
120 for (i = 0; i < nthread; i++) {
121 cyg_thread_create(10, // Priority - just a number
122 test_thread_timeslice, // entry
124 "test_thread", // Name
125 &stacks[i][0], // Stack
127 &threads[i], // Handle
128 &test_threads[i] // Thread data structure
130 cyg_thread_resume( threads[i]);
133 // Just wait a while, until the threads have all run for a bit.
134 cyg_thread_delay( CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS*100 );
136 // Suspend all the threads
137 for (i = 0; i < nthread; i++) {
138 cyg_thread_suspend(threads[i]);
142 // And check that a thread ran on each CPU, and that each thread
146 diag_printf(" Thread ");
147 for( j = 0; j < ncpus; j++ )
151 // " %11d" __123456789ab"
152 diag_printf(" CPU %2d",j);
154 // " %11d" __123456789ab"
155 diag_printf(" Total\n");
156 for (i = 0; i < nthread; i++)
159 diag_printf(" %2d ",i);
160 for( j = 0; j < ncpus; j++ )
162 thread_total[i] += slicerun[i][j];
163 cpu_total[j] += slicerun[i][j];
164 if( slicerun[i][j] > 0 )
166 diag_printf(" %11d",slicerun[i][j]);
168 diag_printf(" %11d\n",thread_total[i]);
169 if( thread_total[i] == 0 )
173 diag_printf(" Total ");
174 for( j = 0; j < ncpus; j++ )
175 diag_printf(" %11d",cpu_total[j]);
177 diag_printf("Threads ");
178 for( j = 0; j < ncpus; j++ )
180 diag_printf(" %11d",cpu_threads[j]);
181 if( cpu_threads[j] < 2 )
186 // Delete all the threads
187 for (i = 0; i < nthread; i++) {
188 cyg_thread_delete(threads[i]);
191 CYG_TEST_INFO( "Timeslice Test: done");
195 //==========================================================================
198 run_tests(CYG_ADDRESS id)
203 // Try to run about 10 times in total, with varying numbers of threads
204 // from only one extra up to the full set:
206 step = (NTHREADS_MAX - (1 + CYG_KERNEL_CPU_COUNT()))/10;
207 if( step == 0 ) step = 1;
209 for( nthread = 1 + CYG_KERNEL_CPU_COUNT() ;
210 nthread <= NTHREADS_MAX ;
212 run_test_timeslice(nthread);
215 CYG_TEST_FAIL_FINISH("Timeslice test failed\n");
217 CYG_TEST_PASS_FINISH("Timeslice test OK");
220 //==========================================================================
222 void timeslice_main( void )
226 // Work out how many CPUs we actually have.
227 ncpus = CYG_KERNEL_CPU_COUNT();
229 cyg_thread_create(0, // Priority - just a number
235 &main_thread, // Handle
236 &test_thread // Thread data structure
238 cyg_thread_resume( main_thread);
240 cyg_scheduler_start();
243 //==========================================================================
245 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
247 cyg_hal_invoke_constructors();
253 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
254 cyg_hal_invoke_constructors();
259 //==========================================================================
261 #else // CYGSEM_KERNEL_SCHED_TIMESLICE etc
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");
279 #endif // CYGSEM_KERNEL_SCHED_TIMESLICE etc.
281 //==========================================================================