source: rtems/testsuites/smptests/smpschedaffinity05/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: 6.0 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 * Start 4 tasks with affinity for each of the 4 cpus.
12 * Allow tasks to set their actual cpu value and delete themselves.
13 * Verify the actual cpu values match the expected cpu values.
14 *
15 * Init task is at a lower priority 8 and the threads
16 * with affinity are at priority 4, so the affinity task
17 * on the core init is running on will preempt it.
18 *
19 * Test tasks run and delete themselves.
20 * Init task never blocks.
21 */
22
23#ifdef HAVE_CONFIG_H
24  #include "config.h"
25#endif
26
27#include <rtems.h>
28#include <inttypes.h>
29
30#include "tmacros.h"
31
32const char rtems_test_name[] = "SMPSCHEDAFFINITY 5";
33
34#define NUM_CPUS   4
35#define TASK_COUNT 5
36
37struct task_data_t {
38  rtems_id            id;
39  cpu_set_t           cpuset;
40  rtems_task_priority priority;
41  bool                ran;
42  int                 expected_cpu;
43  int                 actual_cpu;
44  int                 migrate_cpu;
45};
46
47static struct task_data_t task_data[TASK_COUNT] = {
48  {0x0, {{0xc}}, 7, false,  3, -1,  2},
49  {0x0, {{0xf}}, 8, false,  2, -1, -1},
50  {0x0, {{0x3}}, 5, false,  1, -1,  0},
51  {0x0, {{0x9}}, 6, false,  0, -1,  3},
52  {0x0, {{0x2}}, 9, false, -1, -1,  1}
53};
54
55rtems_id           task_sem;
56
57static void verify_tasks(void);
58
59/*
60 * Spin loop to allow tasks to delay without yeilding the
61 * processor.
62 */
63static void test_delay(int ticks)
64{
65  rtems_interval start, stop;
66  start = rtems_clock_get_ticks_since_boot();
67  do {
68    stop = rtems_clock_get_ticks_since_boot();
69  } while ( (stop - start) < ticks );
70}
71
72static void task(rtems_task_argument arg)
73{
74  rtems_status_code   sc;
75
76  while (true) {
77    sc = rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0);
78    if (sc == RTEMS_SUCCESSFUL) {
79      task_data[arg].ran = true;
80      task_data[arg].actual_cpu = rtems_scheduler_get_processor();
81      rtems_semaphore_release(task_sem);
82    }
83  }
84}
85static void verify_tasks(void)
86{
87  int i;
88
89  printf("Verify Tasks Ran\n");
90
91  while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
92
93  /* Set Init task data */
94  task_data[0].ran = true;
95  task_data[0].actual_cpu = rtems_scheduler_get_processor();
96
97  /* Verify all tasks */
98  for (i = 0; i < NUM_CPUS; i++) {
99    if (i==0)
100      printf("Init(%" PRIu32 "): ran=%d expected=%d actual=%d\n",
101        task_data[i].priority,
102        task_data[i].ran,
103        task_data[i].expected_cpu,
104        task_data[i].actual_cpu
105      );
106    else
107      printf( "TA0%d(%" PRIu32 "): ran=%d expected=%d actual=%d\n",
108        i,
109        task_data[i].priority,
110        task_data[i].ran,
111        task_data[i].expected_cpu,
112        task_data[i].actual_cpu
113      );
114
115    /*  Abort test if values are not as expected */
116    if ( task_data[i].expected_cpu == -1 )
117      rtems_test_assert( task_data[i].ran == false );
118    else {
119      rtems_test_assert( task_data[i].ran == true );
120      rtems_test_assert( task_data[i].expected_cpu == task_data[i].actual_cpu );
121    }
122  }
123
124  rtems_semaphore_release(task_sem);
125}
126
127static void test(void)
128{
129  rtems_status_code   sc;
130  rtems_task_argument i;
131  size_t              size;
132  uint32_t            cpu_count;
133  rtems_task_priority priority;
134
135  /* Get the number of processors that we are using. */
136  cpu_count = rtems_get_processor_count();
137  if (cpu_count != 4) {
138    printf("Test requires a minimum of 4 cores\n");
139    return;
140  }
141
142  size = sizeof(cpu_set_t);
143  task_data[0].id = rtems_task_self();
144  printf("Create Semaphore\n");
145
146  sc = rtems_semaphore_create(
147    rtems_build_name('S', 'E', 'M', '0'),
148    1,                                               /* initial count = 1 */
149    RTEMS_BINARY_SEMAPHORE |
150    RTEMS_PRIORITY |
151    RTEMS_PRIORITY_CEILING,
152    0,
153    &task_sem
154  );
155  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
156
157
158  /* Create and start tasks on each cpu with the appropriate affinity. */
159  for (i = 1; i < TASK_COUNT; i++) {
160
161      sc = rtems_task_create(
162        rtems_build_name('T', 'A', '0', '0'+i),
163        task_data[ i ].priority,
164        RTEMS_MINIMUM_STACK_SIZE,
165        RTEMS_DEFAULT_MODES,
166        RTEMS_DEFAULT_ATTRIBUTES,
167        &task_data[ i ].id
168      );
169      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
170
171      sc = rtems_task_set_affinity(
172        task_data[ i ].id,
173        size,
174        &task_data[i].cpuset
175      );
176      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
177
178      printf(
179        "Start TA%" PRIdrtems_task_argument " at priority %"
180          PRIu32 " on cpu %d\n",
181         i,
182         task_data[i].priority,
183         task_data[i].expected_cpu
184      );
185      sc = rtems_task_start( task_data[ i ].id, task, i );
186      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
187  }
188
189  /* spin for 100 ticks */
190  test_delay(100);
191
192  verify_tasks();
193
194  i = TASK_COUNT - 1;
195  task_data[ i ].priority = 4;
196  printf(
197    "Set TA%" PRIdrtems_task_argument " priority %" PRIu32 "\n",
198    i,
199    task_data[i].priority
200  );
201  sc = rtems_task_set_priority(
202    task_data[ i ].id,
203    task_data[ i ].priority,
204    &priority
205  );
206  test_delay(25);
207
208  while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
209  for (i = 0; i < TASK_COUNT; i++) {
210    task_data[ i ].expected_cpu = task_data[ i ].migrate_cpu;
211    task_data[ i ].actual_cpu = -1;
212    task_data[ i ].ran = false;
213  }
214  rtems_semaphore_release(task_sem);
215  test_delay(25);
216  verify_tasks();
217}
218
219static void Init(rtems_task_argument arg)
220{
221  TEST_BEGIN();
222
223  test();
224
225  TEST_END();
226  rtems_test_exit(0);
227}
228
229#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
230#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
231
232#define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
233
234#define CONFIGURE_MAXIMUM_PROCESSORS NUM_CPUS
235
236#define CONFIGURE_MAXIMUM_TASKS          TASK_COUNT
237
238#define CONFIGURE_MAXIMUM_SEMAPHORES 1
239
240#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
241
242#define CONFIGURE_INIT_TASK_PRIORITY       7
243#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
244
245#define CONFIGURE_INIT
246
247#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.