source: rtems/testsuites/smptests/smpschedaffinity04/init.c @ 0f1fa19

Last change on this file since 0f1fa19 was 0f1fa19, checked in by Sebastian Huber <sebastian.huber@…>, on Oct 26, 2017 at 11:19:57 AM

smptests: Fix format warnings

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