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.
12 // Copyright (C) 2006 eCosCentric Limited
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.
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
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.
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.
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.
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####
45 // Contributors: nickg
47 // Description: A basic timeslicing test.
49 //####DESCRIPTIONEND####
50 //==========================================================================
52 #include <pkgconf/kernel.h>
53 #include <pkgconf/hal.h>
55 #include <cyg/hal/hal_arch.h>
57 #include <cyg/kernel/smp.hxx>
59 #include <cyg/kernel/kapi.h>
61 #include <cyg/infra/testcase.h>
62 #include <cyg/infra/diag.h>
64 //==========================================================================
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)
73 //==========================================================================
75 #define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
77 #define NTHREADS_MAX (CYGNUM_KERNEL_CPU_MAX*6)
79 static int ncpus = CYGNUM_KERNEL_CPU_MAX;
81 static char test_stack[STACK_SIZE];
82 static cyg_thread test_thread;
83 static cyg_handle_t main_thread;
85 static char stacks[NTHREADS_MAX][STACK_SIZE];
86 static cyg_thread test_threads[NTHREADS_MAX];
87 static cyg_handle_t threads[NTHREADS_MAX];
89 static volatile int failed = false;
91 static volatile cyg_uint32 slicerun[NTHREADS_MAX][CYGNUM_KERNEL_CPU_MAX];
93 //==========================================================================
96 test_thread_timeslice(CYG_ADDRESS id)
99 slicerun[id][CYG_KERNEL_CPU_THIS()]++;
102 //==========================================================================
104 void run_test_timeslice(int nthread)
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];
111 CYG_TEST_INFO( "Timeslice Test: Check timeslicing works");
114 for (i = 0; i < nthread; i++)
115 for( j = 0; j < ncpus; j++ )
118 // Set my priority higher than any I plan to create
119 cyg_thread_set_priority(cyg_thread_self(), 2);
121 for (i = 0; i < nthread; i++) {
122 cyg_thread_create(10, // Priority - just a number
123 test_thread_timeslice, // entry
125 "test_thread", // Name
126 &stacks[i][0], // Stack
128 &threads[i], // Handle
129 &test_threads[i] // Thread data structure
131 cyg_thread_resume( threads[i]);
134 // Just wait a while, until the threads have all run for a bit.
135 cyg_thread_delay( CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS*100 );
137 // Suspend all the threads
138 for (i = 0; i < nthread; i++) {
139 cyg_thread_suspend(threads[i]);
143 // And check that a thread ran on each CPU, and that each thread
147 diag_printf(" Thread ");
148 for( j = 0; j < ncpus; j++ )
152 // " %11d" __123456789ab"
153 diag_printf(" CPU %2d",j);
155 // " %11d" __123456789ab"
156 diag_printf(" Total\n");
157 for (i = 0; i < nthread; i++)
160 diag_printf(" %2d ",i);
161 for( j = 0; j < ncpus; j++ )
163 thread_total[i] += slicerun[i][j];
164 cpu_total[j] += slicerun[i][j];
165 if( slicerun[i][j] > 0 )
167 diag_printf(" %11d",slicerun[i][j]);
169 diag_printf(" %11d\n",thread_total[i]);
170 if( thread_total[i] == 0 )
174 diag_printf(" Total ");
175 for( j = 0; j < ncpus; j++ )
176 diag_printf(" %11d",cpu_total[j]);
178 diag_printf("Threads ");
179 for( j = 0; j < ncpus; j++ )
181 diag_printf(" %11d",cpu_threads[j]);
182 if( cpu_threads[j] < 2 )
187 // Delete all the threads
188 for (i = 0; i < nthread; i++) {
189 cyg_thread_delete(threads[i]);
192 CYG_TEST_INFO( "Timeslice Test: done");
196 //==========================================================================
199 run_tests(CYG_ADDRESS id)
204 // Try to run about 10 times in total, with varying numbers of threads
205 // from only one extra up to the full set:
207 step = (NTHREADS_MAX - (1 + CYG_KERNEL_CPU_COUNT()))/10;
208 if( step == 0 ) step = 1;
210 for( nthread = 1 + CYG_KERNEL_CPU_COUNT() ;
211 nthread <= NTHREADS_MAX ;
213 run_test_timeslice(nthread);
216 CYG_TEST_FAIL_FINISH("Timeslice test failed\n");
218 CYG_TEST_PASS_FINISH("Timeslice test OK");
221 //==========================================================================
223 void timeslice_main( void )
227 // Work out how many CPUs we actually have.
228 ncpus = CYG_KERNEL_CPU_COUNT();
230 cyg_thread_create(0, // Priority - just a number
236 &main_thread, // Handle
237 &test_thread // Thread data structure
239 cyg_thread_resume( main_thread);
241 cyg_scheduler_start();
244 //==========================================================================
246 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
248 cyg_hal_invoke_constructors();
254 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
255 cyg_hal_invoke_constructors();
260 //==========================================================================
262 #else // CYGSEM_KERNEL_SCHED_TIMESLICE etc
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");
278 #endif // CYGSEM_KERNEL_SCHED_TIMESLICE etc.
280 //==========================================================================