source: rtems/testsuites/smptests/smpschedaffinity05/init.c @ 239dd35f

5
Last change on this file since 239dd35f was 239dd35f, checked in by Sebastian Huber <sebastian.huber@…>, on 02/03/17 at 10:24:30

smptests: Fix warnings

  • Property mode set to 100644
File size: 5.9 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_get_current_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_get_current_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%d at priority %" PRIu32 " on cpu %d\n",
180         i,
181         task_data[i].priority,
182         task_data[i].expected_cpu
183      );
184      sc = rtems_task_start( task_data[ i ].id, task, i );
185      rtems_test_assert(sc == RTEMS_SUCCESSFUL);
186  }
187
188  /* spin for 100 ticks */
189  test_delay(100);
190
191  verify_tasks();
192
193  i = TASK_COUNT - 1;
194  task_data[ i ].priority = 4;
195  printf("Set TA%d priority %" PRIu32 "\n", i,task_data[i].priority );
196  sc = rtems_task_set_priority(
197    task_data[ i ].id,
198    task_data[ i ].priority,
199    &priority
200  );
201  test_delay(25);
202
203  while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
204  for (i = 0; i < TASK_COUNT; i++) {
205    task_data[ i ].expected_cpu = task_data[ i ].migrate_cpu;
206    task_data[ i ].actual_cpu = -1;
207    task_data[ i ].ran = false;
208  }
209  rtems_semaphore_release(task_sem);
210  test_delay(25);
211  verify_tasks();
212}
213
214static void Init(rtems_task_argument arg)
215{
216  TEST_BEGIN();
217
218  test();
219
220  TEST_END();
221  rtems_test_exit(0);
222}
223
224#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
225#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
226
227#define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
228
229#define CONFIGURE_SMP_MAXIMUM_PROCESSORS NUM_CPUS
230
231#define CONFIGURE_MAXIMUM_TASKS          TASK_COUNT
232
233#define CONFIGURE_MAXIMUM_SEMAPHORES 1
234
235#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
236
237#define CONFIGURE_INIT_TASK_PRIORITY       7
238#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
239
240#define CONFIGURE_INIT
241
242#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.