source: rtems/testsuites/smptests/smpschedaffinity02/init.c @ 03c9f24

5
Last change on this file since 03c9f24 was 03c9f24, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 5, 2019 at 6:03:12 AM

rtems: Add rtems_scheduler_get_processor()

Add rtems_scheduler_get_processor() as a replacement for
rtems_get_current_processor(). The rtems_get_current_processor() is a
bit orphaned. Adopt it by the Scheduler Manager. This is in line with
the glibc sched_getcpu() function.

Deprecate rtems_get_current_processor().

Update #3731.

  • 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#include <rtems.h>
25
26#include "tmacros.h"
27
28const char rtems_test_name[] = "SMPSCHEDAFFINITY 2";
29
30#define NUM_CPUS   2
31
32struct task_data_t {
33  rtems_id   id;
34  int        expected_cpu;
35  cpu_set_t  cpuset;
36  bool       ran;
37  int        actual_cpu;
38};
39
40struct task_data_t task_data = {
41  0x0, 0, {{0x3}}, false, -1
42};
43
44rtems_id           task_sem;
45
46static void task(rtems_task_argument arg);
47static void task_verify( bool ran, bool change_affinity, int cpu );
48static void init_verify( int expect ); 
49
50static void test_delay(int ticks)
51{ 
52  rtems_interval start, stop;
53  start = rtems_clock_get_ticks_since_boot();
54  do {
55    stop = rtems_clock_get_ticks_since_boot();
56  } while ( (stop - start) < ticks );
57}
58
59static void task_verify( bool ran, bool change_affinity, int cpu )
60{
61  rtems_status_code   sc;
62  size_t              size = sizeof(cpu_set_t);
63
64  /* Obtain the semaphore without blocking */
65  while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
66
67  /* print the expected and actual values */
68  printf( "TA01: expected=%d actual=%d ran=%d\n", 
69   task_data.expected_cpu,
70   task_data.actual_cpu,
71   task_data.ran
72   );
73
74  /* Verify expected results */
75  rtems_test_assert( task_data.ran == ran );
76  if (ran)
77    rtems_test_assert( task_data.expected_cpu == task_data.actual_cpu );
78
79  if (change_affinity) {
80    printf("Set TA01 to cpu %d\n", cpu);
81    CPU_ZERO(&task_data.cpuset);
82    CPU_SET(cpu, &task_data.cpuset);
83    sc = rtems_task_set_affinity( task_data.id, size, &task_data.cpuset );
84    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
85  }
86
87  /* Reset the states */
88  task_data.ran = false;
89  task_data.expected_cpu = cpu;
90
91  /* Release the semaphore */
92  rtems_semaphore_release(task_sem);
93} 
94
95static void init_verify( int expect ) 
96{
97  int cpu;
98
99
100  test_delay(20);
101
102  cpu = rtems_scheduler_get_processor();
103  printf( "Init: expected=%d actual=%d\n", expect, cpu);
104  rtems_test_assert( expect == cpu );
105}
106
107static void task(rtems_task_argument arg)
108{
109  rtems_status_code   sc;
110
111  /* Never block and continually get core id  */
112  while (true) {
113    sc = rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0);
114    if (sc == RTEMS_SUCCESSFUL) {
115      task_data.actual_cpu = rtems_scheduler_get_processor();
116      task_data.ran = true;
117      test_delay(25);
118      rtems_semaphore_release(task_sem);
119    }
120  }
121}
122
123static void test(void)
124{
125  rtems_status_code   sc;
126  uint32_t            cpu_count;
127  rtems_id            id_self;
128  cpu_set_t           cpuset;
129 
130  /* Get the number of processors that we are using. */
131  cpu_count = rtems_get_processor_count();
132  if (cpu_count < NUM_CPUS) {
133    printf("Error: Test requires at least 2 cpus\n");
134    return;
135  }
136
137  id_self = rtems_task_self();
138 
139  printf("Create Semaphore\n");
140  sc = rtems_semaphore_create( 
141    rtems_build_name('S', 'E', 'M', '0'),
142    1,
143    RTEMS_BINARY_SEMAPHORE |
144    RTEMS_PRIORITY | 
145    RTEMS_PRIORITY_CEILING,
146    0,
147    &task_sem
148  ); 
149  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
150
151  printf("Create TA1\n");
152  sc = rtems_task_create(
153    rtems_build_name('T', 'A', '0', '1'),
154    4,
155    RTEMS_MINIMUM_STACK_SIZE,
156    RTEMS_DEFAULT_MODES,
157    RTEMS_DEFAULT_ATTRIBUTES,
158    &task_data.id
159  );
160  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
161
162  /* TA1 should start on cpu  0, since init starts on core 1 */
163  sc = rtems_task_start( task_data.id, task, 1 );
164  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
165
166  /* Verify Init task is on cpu 1  */
167  init_verify(1);
168
169  /* Verify TA1 on cpu 0 and set the affinity to cpu 0 */
170  task_verify( true, true, 0 );
171   
172  /* Verify Init task is on cpu 1  */
173  init_verify(1);
174
175  /* Verify TA1 on cpu 0 and change the affinity to cpu 1 */
176  task_verify( true, true, 1 );
177
178  /* Verify Init task is on cpu 1  */
179  init_verify(1);
180
181  /* Verify TA1 did not run */
182  task_verify( false, false, 1 );
183
184  /* Set affinity of Init to cpu 0 */
185  printf("Set Affinity of init task to cpu 0\n");
186  CPU_ZERO(&cpuset);
187  CPU_SET(0, &cpuset);
188  sc = rtems_task_set_affinity( id_self, sizeof(cpuset), &cpuset );
189  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
190
191  /* Verify init task went to cpu 0 */
192  test_delay(50);
193  init_verify(0);
194
195  /* Verfiy TA1 is now running on cpu 1 */
196  task_verify(true, false, 1);
197}
198
199static void Init(rtems_task_argument arg)
200{
201  TEST_BEGIN();
202
203  test();
204
205  TEST_END();
206  rtems_test_exit(0);
207}
208
209#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
210#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
211
212#define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
213
214#define CONFIGURE_MAXIMUM_PROCESSORS NUM_CPUS
215
216#define CONFIGURE_MAXIMUM_TASKS          NUM_CPUS
217
218#define CONFIGURE_MAXIMUM_SEMAPHORES 1
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.