source: rtems/testsuites/smptests/smpschedaffinity02/init.c @ 98c6d50

5
Last change on this file since 98c6d50 was 98c6d50, checked in by Chris Johns <chrisj@…>, on 10/19/17 at 05:39:16

testsuite: Use printk for all test output where possible.

  • Remove the printf support leaving the direct printk support configured with TESTS_USE_PRINTK and all other output goes via a buffered vsniprintf call to printk.
  • Control the test's single init for functions and global data with TEST_INIT and not CONFIGURE_INIT. They are now separate.

Updates #3170.

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 *  COPYRIGHT (c) 2014.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10/*
11 * Test designed for 2 cores: Init task and TA1 task.
12 * of equal priorities. 
13 *
14 *  - Set TA1 affinity to core 0 verify
15 *  - Set TA1 affinity to core 1 verify it does not run because
16 *    the Init task never blocks
17 *  - Set Init affinity to core 0 verify both tasks are on the correct cores.
18 */
19
20#ifdef HAVE_CONFIG_H
21  #include "config.h"
22#endif
23
24#define TEST_INIT
25
26#include <rtems.h>
27
28#include "tmacros.h"
29
30const char rtems_test_name[] = "SMPSCHEDAFFINITY 2";
31
32#define NUM_CPUS   2
33
34struct task_data_t {
35  rtems_id   id;
36  int        expected_cpu;
37  cpu_set_t  cpuset;
38  bool       ran;
39  int        actual_cpu;
40};
41
42struct task_data_t task_data = {
43  0x0, 0, {{0x3}}, false, -1
44};
45
46rtems_id           task_sem;
47
48static void task(rtems_task_argument arg);
49static void task_verify( bool ran, bool change_affinity, int cpu );
50static void init_verify( int expect );
51
52static void test_delay(int ticks)
53{
54  rtems_interval start, stop;
55  start = rtems_clock_get_ticks_since_boot();
56  do {
57    stop = rtems_clock_get_ticks_since_boot();
58  } while ( (stop - start) < ticks );
59}
60
61static void task_verify( bool ran, bool change_affinity, int cpu )
62{
63  rtems_status_code   sc;
64  size_t              size = sizeof(cpu_set_t);
65
66  /* Obtain the semaphore without blocking */
67  while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
68
69  /* print the expected and actual values */
70  printf( "TA01: expected=%d actual=%d ran=%d\n",
71   task_data.expected_cpu,
72   task_data.actual_cpu,
73   task_data.ran
74   );
75
76  /* Verify expected results */
77  rtems_test_assert( task_data.ran == ran );
78  if (ran)
79    rtems_test_assert( task_data.expected_cpu == task_data.actual_cpu );
80
81  if (change_affinity) {
82    printf("Set TA01 to cpu %d\n", cpu);
83    CPU_ZERO(&task_data.cpuset);
84    CPU_SET(cpu, &task_data.cpuset);
85    sc = rtems_task_set_affinity( task_data.id, size, &task_data.cpuset );
86    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
87  }
88
89  /* Reset the states */
90  task_data.ran = false;
91  task_data.expected_cpu = cpu;
92
93  /* Release the semaphore */
94  rtems_semaphore_release(task_sem);
95}
96
97static void init_verify( int expect )
98{
99  int cpu;
100
101
102  test_delay(20);
103
104  cpu = rtems_get_current_processor();
105  printf( "Init: expected=%d actual=%d\n", expect, cpu);
106  rtems_test_assert( expect == cpu );
107}
108
109static void task(rtems_task_argument arg)
110{
111  rtems_status_code   sc;
112
113  /* Never block and continually get core id  */
114  while (true) {
115    sc = rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0);
116    if (sc == RTEMS_SUCCESSFUL) {
117      task_data.actual_cpu = rtems_get_current_processor();
118      task_data.ran = true;
119      test_delay(25);
120      rtems_semaphore_release(task_sem);
121    }
122  }
123}
124
125static void test(void)
126{
127  rtems_status_code   sc;
128  uint32_t            cpu_count;
129  rtems_id            id_self;
130  cpu_set_t           cpuset;
131 
132  /* Get the number of processors that we are using. */
133  cpu_count = rtems_get_processor_count();
134  if (cpu_count < NUM_CPUS) {
135    printf("Error: Test requires at least 2 cpus\n");
136    return;
137  }
138
139  id_self = rtems_task_self();
140 
141  printf("Create Semaphore\n");
142  sc = rtems_semaphore_create( 
143    rtems_build_name('S', 'E', 'M', '0'),
144    1,
145    RTEMS_BINARY_SEMAPHORE |
146    RTEMS_PRIORITY |
147    RTEMS_PRIORITY_CEILING,
148    0,
149    &task_sem
150  ); 
151  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
152
153  printf("Create TA1\n");
154  sc = rtems_task_create(
155    rtems_build_name('T', 'A', '0', '1'),
156    4,
157    RTEMS_MINIMUM_STACK_SIZE,
158    RTEMS_DEFAULT_MODES,
159    RTEMS_DEFAULT_ATTRIBUTES,
160    &task_data.id
161  );
162  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
163
164  /* TA1 should start on cpu  0, since init starts on core 1 */
165  sc = rtems_task_start( task_data.id, task, 1 );
166  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
167
168  /* Verify Init task is on cpu 1  */
169  init_verify(1);
170
171  /* Verify TA1 on cpu 0 and set the affinity to cpu 0 */
172  task_verify( true, true, 0 );
173   
174  /* Verify Init task is on cpu 1  */
175  init_verify(1);
176
177  /* Verify TA1 on cpu 0 and change the affinity to cpu 1 */
178  task_verify( true, true, 1 );
179
180  /* Verify Init task is on cpu 1  */
181  init_verify(1);
182
183  /* Verify TA1 did not run */
184  task_verify( false, false, 1 );
185
186  /* Set affinity of Init to cpu 0 */
187  printf("Set Affinity of init task to cpu 0\n");
188  CPU_ZERO(&cpuset);
189  CPU_SET(0, &cpuset);
190  sc = rtems_task_set_affinity( id_self, sizeof(cpuset), &cpuset );
191  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
192
193  /* Verify init task went to cpu 0 */
194  test_delay(50);
195  init_verify(0);
196
197  /* Verfiy TA1 is now running on cpu 1 */
198  task_verify(true, false, 1);
199}
200
201static void Init(rtems_task_argument arg)
202{
203  TEST_BEGIN();
204
205  test();
206
207  TEST_END();
208  rtems_test_exit(0);
209}
210
211#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
212#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
213
214#define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
215
216#define CONFIGURE_MAXIMUM_PROCESSORS NUM_CPUS
217
218#define CONFIGURE_MAXIMUM_TASKS          NUM_CPUS
219
220#define CONFIGURE_INIT_TASK_PRIORITY      4
221
222#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
223
224#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
225
226#define CONFIGURE_INIT
227
228#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.