source: rtems/testsuites/smptests/smpschedaffinity04/init.c @ 63fab5aa

Last change on this file since 63fab5aa was 63fab5aa, checked in by Sebastian Huber <sebastian.huber@…>, on Feb 1, 2018 at 2:14:55 PM

smpschedaffinity04: Fix configuration

  • Property mode set to 100644
File size: 4.4 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 * Use the Init task to walk the higher priority TA1 across all the cores.
12 */
13
14#ifdef HAVE_CONFIG_H
15  #include "config.h"
16#endif
17
18#include <rtems.h>
19
20#include "tmacros.h"
21
22const char rtems_test_name[] = "SMPSCHEDAFFINITY 4";
23
24#define NUM_CPUS   4
25#define TASK_COUNT 2
26
27struct task_data_t {
28  rtems_id   id;
29  int        expected_cpu;
30  bool       ran;
31  int        actual_cpu;
32};
33
34struct task_data_t task_data = {
35  0x0, 2, false, 0xff
36};
37
38rtems_id           task_sem;
39
40static void task(rtems_task_argument arg);
41
42static void test_delay(int ticks)
43{ 
44  rtems_interval start, stop;
45  start = rtems_clock_get_ticks_since_boot();
46  do {
47    stop = rtems_clock_get_ticks_since_boot();
48  } while ( (stop - start) < ticks );
49}
50
51/*
52 * Task that continually sets the cpu and
53 * run indicators without blocking.
54 */
55static void task(rtems_task_argument arg)
56{
57  rtems_status_code   sc;
58
59  while (true) {
60    sc = rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0);
61    if (sc == RTEMS_SUCCESSFUL) {
62      task_data.ran = true;
63      task_data.actual_cpu = rtems_get_current_processor();
64      rtems_semaphore_release(task_sem);
65    }
66  }
67}
68
69static void test(void)
70{
71  rtems_status_code   sc;
72  uint32_t            cpu_count;
73  int                 cpu;
74  int                 i;
75  cpu_set_t           cpuset;
76
77  /* Get the number of processors that we are using. */
78  cpu_count = rtems_get_processor_count();
79  if (cpu_count < 2) {
80    printf("Error: Test requires at least 2 cpus\n");
81    return;
82  }
83
84  printf("Create Semaphore\n");
85  sc = rtems_semaphore_create( 
86    rtems_build_name('S', 'E', 'M', '0'),
87    1,                                               /* initial count = 1 */
88    RTEMS_BINARY_SEMAPHORE |
89    RTEMS_PRIORITY | 
90    RTEMS_PRIORITY_CEILING,
91    0,
92    &task_sem
93  ); 
94  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
95
96  /*
97   * Create and start TA1 at a higher priority
98   * than the init task.
99   */
100  sc = rtems_task_create(
101    rtems_build_name('T', 'A', '0', '1'),
102    4,
103    RTEMS_MINIMUM_STACK_SIZE,
104    RTEMS_DEFAULT_MODES,
105    RTEMS_DEFAULT_ATTRIBUTES,
106    &task_data.id
107  );
108  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
109
110  printf("Start TA1\n");
111  sc = rtems_task_start( task_data.id, task, 0 );
112  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
113
114  /*
115   * Verify the Init task is running on the max core.
116   */ 
117  printf("Verify Init task is on cpu %" PRIu32 "\n",cpu_count-1);
118  cpu = rtems_get_current_processor();
119  rtems_test_assert(cpu == (cpu_count-1));
120
121  /* Walk TA1 across all of the cores */
122  for(i=0; i < cpu_count; i++) {
123    /* Set the Affinity to core i */
124    CPU_ZERO(&cpuset);
125    CPU_SET(i, &cpuset);
126    printf("Set Affinity TA1 to cpu %d\n", i);
127    sc = rtems_task_set_affinity( task_data.id, sizeof(cpuset), &cpuset );
128    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
129
130    /* Wait a bit to be sure it has switched cores then clear the task data */
131    test_delay(50);
132    while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
133    task_data.ran = false;
134    task_data.expected_cpu = i;
135    rtems_semaphore_release(task_sem);
136    test_delay(50);
137
138    /* Verify the task ran on core i */
139    while( rtems_semaphore_obtain (task_sem, RTEMS_NO_WAIT, 0) != RTEMS_SUCCESSFUL );
140    if (task_data.ran != true)
141      printf("Error: TA01 never ran.\n");
142    else
143      printf(
144        "TA1 expected cpu: %d actual cpu %d\n", 
145        task_data.expected_cpu,
146        task_data.actual_cpu
147      );
148    rtems_test_assert(task_data.ran == true);
149    rtems_test_assert(task_data.expected_cpu == task_data.actual_cpu);
150    rtems_semaphore_release(task_sem);
151  }
152}
153
154static void Init(rtems_task_argument arg)
155{
156  TEST_BEGIN();
157
158  test();
159
160  TEST_END();
161  rtems_test_exit(0);
162}
163
164#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
165#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
166
167#define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP
168
169#define CONFIGURE_MAXIMUM_SEMAPHORES 1
170
171#define CONFIGURE_MAXIMUM_PROCESSORS NUM_CPUS
172
173#define CONFIGURE_MAXIMUM_TASKS          TASK_COUNT
174
175#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
176
177#define CONFIGURE_INIT_TASK_PRIORITY      8
178#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
179
180#define CONFIGURE_INIT
181
182#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.