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

Last change on this file since 98c6d50 was 98c6d50, checked in by Chris Johns <chrisj@…>, on Oct 19, 2017 at 5:39:16 AM

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