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: An additional 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 hipri_stack[STACK_SIZE];
86 static cyg_thread hipri_thread_obj;
87 static cyg_handle_t hipri_thread;
89 static char stacks[NTHREADS_MAX][STACK_SIZE];
90 static cyg_thread test_threads[NTHREADS_MAX];
91 static cyg_handle_t threads[NTHREADS_MAX];
93 static volatile int failed = false;
95 static volatile cyg_uint32 slicerun[NTHREADS_MAX][CYGNUM_KERNEL_CPU_MAX];
97 //==========================================================================
100 test_thread_timeslice(CYG_ADDRESS id)
103 slicerun[id][CYG_KERNEL_CPU_THIS()]++;
106 //==========================================================================
108 void run_test_timeslice(int nthread)
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];
115 CYG_TEST_INFO( "Timeslice2 Test: Check timeslicing works under preemption");
118 for (i = 0; i < nthread; i++)
119 for( j = 0; j < ncpus; j++ )
122 // Set my priority higher than any I plan to create
123 cyg_thread_set_priority(cyg_thread_self(), 2);
125 for (i = 0; i < nthread; i++) {
126 cyg_thread_create(10, // Priority - just a number
127 test_thread_timeslice, // entry
129 "test_thread", // Name
130 &stacks[i][0], // Stack
132 &threads[i], // Handle
133 &test_threads[i] // Thread data structure
135 cyg_thread_resume( threads[i]);
138 // Just wait a while, until the threads have all run for a bit.
139 cyg_thread_delay( CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS*100 );
141 // Suspend all the threads
142 for (i = 0; i < nthread; i++) {
143 cyg_thread_suspend(threads[i]);
147 // And check that a thread ran on each CPU, and that each thread
151 diag_printf(" Thread ");
152 for( j = 0; j < ncpus; j++ )
156 // " %11d" __123456789ab"
157 diag_printf(" CPU %2d",j);
159 // " %11d" __123456789ab"
160 diag_printf(" Total\n");
161 for (i = 0; i < nthread; i++)
164 diag_printf(" %2d ",i);
165 for( j = 0; j < ncpus; j++ )
167 thread_total[i] += slicerun[i][j];
168 cpu_total[j] += slicerun[i][j];
169 if( slicerun[i][j] > 0 )
171 diag_printf(" %11d",slicerun[i][j]);
173 diag_printf(" %11d\n",thread_total[i]);
174 if( thread_total[i] == 0 )
178 diag_printf(" Total ");
179 for( j = 0; j < ncpus; j++ )
180 diag_printf(" %11d",cpu_total[j]);
182 diag_printf("Threads ");
183 for( j = 0; j < ncpus; j++ )
185 diag_printf(" %11d",cpu_threads[j]);
186 if( cpu_threads[j] < 2 )
191 // Delete all the threads
192 for (i = 0; i < nthread; i++) {
193 cyg_thread_delete(threads[i]);
196 CYG_TEST_INFO( "Timeslice2 Test: done");
200 //==========================================================================
203 hipri_test(CYG_ADDRESS id)
207 cyg_thread_delay( CYGNUM_KERNEL_SCHED_TIMESLICE_TICKS/2 );
211 //==========================================================================
214 run_tests(CYG_ADDRESS id)
219 // Try to run about 10 times in total, with varying numbers of threads
220 // from only one extra up to the full set:
222 step = (NTHREADS_MAX - (1 + CYG_KERNEL_CPU_COUNT()))/10;
223 if( step == 0 ) step = 1;
225 for( nthread = 1 + CYG_KERNEL_CPU_COUNT() ;
226 nthread <= NTHREADS_MAX ;
228 run_test_timeslice(nthread);
231 CYG_TEST_FAIL_FINISH("Timeslice2 test failed\n");
233 CYG_TEST_PASS_FINISH("Timeslice2 test OK");
236 //==========================================================================
238 void timeslice_main( void )
242 // Work out how many CPUs we actually have.
243 ncpus = CYG_KERNEL_CPU_COUNT();
245 cyg_thread_create(0, // Priority - just a number
251 &main_thread, // Handle
252 &test_thread // Thread data structure
254 cyg_thread_resume( main_thread);
256 cyg_thread_create(5, // Priority - just a number
260 hipri_stack, // Stack
262 &hipri_thread, // Handle
263 &hipri_thread_obj // Thread data structure
265 cyg_thread_resume( hipri_thread);
267 cyg_scheduler_start();
270 //==========================================================================
272 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
274 cyg_hal_invoke_constructors();
280 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
281 cyg_hal_invoke_constructors();
286 //==========================================================================
288 #else // CYGSEM_KERNEL_SCHED_TIMESLICE etc
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");
304 #endif // CYGSEM_KERNEL_SCHED_TIMESLICE etc.
306 //==========================================================================