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 | |
---|
29 | #include "tmacros.h" |
---|
30 | |
---|
31 | const char rtems_test_name[] = "SMPSCHEDAFFINITY 1"; |
---|
32 | |
---|
33 | #define NUM_CPUS 4 |
---|
34 | #define TASK_COUNT 5 |
---|
35 | |
---|
36 | struct task_data_t { |
---|
37 | rtems_id id; |
---|
38 | int expected_cpu; |
---|
39 | cpu_set_t cpuset; |
---|
40 | bool ran; |
---|
41 | int actual_cpu; |
---|
42 | }; |
---|
43 | |
---|
44 | static struct task_data_t task_data[NUM_CPUS] = { |
---|
45 | {0x0, 2, {{0x4}}, false, -1}, |
---|
46 | {0x0, 0, {{0x1}}, false, -1}, |
---|
47 | {0x0, 3, {{0x8}}, false, -1}, |
---|
48 | {0x0, 1, {{0x2}}, false, -1} |
---|
49 | }; |
---|
50 | |
---|
51 | /* |
---|
52 | * Spin loop to allow tasks to delay without yeilding the |
---|
53 | * processor. |
---|
54 | */ |
---|
55 | static void test_delay(int ticks) |
---|
56 | { |
---|
57 | rtems_interval start, stop; |
---|
58 | start = rtems_clock_get_ticks_since_boot(); |
---|
59 | do { |
---|
60 | stop = rtems_clock_get_ticks_since_boot(); |
---|
61 | } while ( (stop - start) < ticks ); |
---|
62 | } |
---|
63 | |
---|
64 | static void task(rtems_task_argument arg) |
---|
65 | { |
---|
66 | uint32_t cpu; |
---|
67 | cpu_set_t cpuset; |
---|
68 | |
---|
69 | cpu = rtems_get_current_processor(); |
---|
70 | |
---|
71 | rtems_task_get_affinity( rtems_task_self(), sizeof(cpuset), &cpuset ); |
---|
72 | |
---|
73 | task_data[arg].ran = true; |
---|
74 | task_data[arg].actual_cpu = cpu; |
---|
75 | |
---|
76 | rtems_task_delete( RTEMS_SELF ); |
---|
77 | } |
---|
78 | |
---|
79 | static void test(void) |
---|
80 | { |
---|
81 | rtems_status_code sc; |
---|
82 | rtems_task_argument i; |
---|
83 | size_t size; |
---|
84 | uint32_t cpu_count; |
---|
85 | |
---|
86 | /* Get the number of processors that we are using. */ |
---|
87 | cpu_count = rtems_get_processor_count(); |
---|
88 | |
---|
89 | size = sizeof(cpu_set_t); |
---|
90 | |
---|
91 | /* Create and start tasks on each cpu with the appropriate affinity. */ |
---|
92 | for (i = 0; i < NUM_CPUS; i++) { |
---|
93 | |
---|
94 | /* Skip if this cpu doesn't exist, don't create task */ |
---|
95 | if ( task_data[i].expected_cpu >= cpu_count ) { |
---|
96 | printf( "Skipping TA0%d because on a core we do not have\n", i ); |
---|
97 | continue; |
---|
98 | } |
---|
99 | |
---|
100 | sc = rtems_task_create( |
---|
101 | rtems_build_name('T', 'A', '0', '0'+i), |
---|
102 | 4, |
---|
103 | RTEMS_MINIMUM_STACK_SIZE, |
---|
104 | RTEMS_DEFAULT_MODES, |
---|
105 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
106 | &task_data[ i ].id |
---|
107 | ); |
---|
108 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
109 | |
---|
110 | printf("Set TA%d affinity to cpu %d\n", i, task_data[i].expected_cpu ); |
---|
111 | sc = rtems_task_set_affinity( task_data[ i ].id, size, &task_data[i].cpuset ); |
---|
112 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
113 | |
---|
114 | printf("Start TA%d on cpu %d\n", i, task_data[i].expected_cpu); |
---|
115 | sc = rtems_task_start( task_data[ i ].id, task, i ); |
---|
116 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
117 | } |
---|
118 | |
---|
119 | /* spin for 100 ticks */ |
---|
120 | test_delay(100); |
---|
121 | |
---|
122 | printf("Verify Tasks Ran\n"); |
---|
123 | for (i = 0; i < NUM_CPUS; i++) { |
---|
124 | |
---|
125 | /* Skip if this cpu doesn't exist, task doesn't exist */ |
---|
126 | if ( task_data[i].expected_cpu >= cpu_count ) { |
---|
127 | printf( "Skipping TA0%d because on a core we do not have\n", i ); |
---|
128 | continue; |
---|
129 | } |
---|
130 | |
---|
131 | /* print the expected and actual values */ |
---|
132 | printf( "TA0%d: ran=%d expected=%d actual=%d\n", |
---|
133 | i, |
---|
134 | task_data[i].ran, |
---|
135 | task_data[i].expected_cpu, |
---|
136 | task_data[i].actual_cpu |
---|
137 | ); |
---|
138 | |
---|
139 | /* Abort test if values are not as expected */ |
---|
140 | rtems_test_assert( task_data[i].ran == true ); |
---|
141 | rtems_test_assert( task_data[i].expected_cpu == task_data[i].actual_cpu ); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | static void Init(rtems_task_argument arg) |
---|
146 | { |
---|
147 | TEST_BEGIN(); |
---|
148 | |
---|
149 | test(); |
---|
150 | |
---|
151 | TEST_END(); |
---|
152 | rtems_test_exit(0); |
---|
153 | } |
---|
154 | |
---|
155 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
156 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
157 | |
---|
158 | #define CONFIGURE_SCHEDULER_PRIORITY_AFFINITY_SMP |
---|
159 | |
---|
160 | #define CONFIGURE_MAXIMUM_PROCESSORS NUM_CPUS |
---|
161 | |
---|
162 | #define CONFIGURE_MAXIMUM_TASKS TASK_COUNT |
---|
163 | |
---|
164 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
165 | |
---|
166 | #define CONFIGURE_INIT_TASK_PRIORITY 8 |
---|
167 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
168 | |
---|
169 | #define CONFIGURE_INIT |
---|
170 | |
---|
171 | #include <rtems/confdefs.h> |
---|