1 | .. comment SPDX-License-Identifier: CC-BY-SA-4.0 |
---|
2 | |
---|
3 | .. COMMENT: COPYRIGHT (c) 2014. |
---|
4 | .. COMMENT: On-Line Applications Research Corporation (OAR). |
---|
5 | .. COMMENT: All rights reserved. |
---|
6 | |
---|
7 | Symmetric Multiprocessing Services |
---|
8 | ********************************** |
---|
9 | |
---|
10 | Introduction |
---|
11 | ============ |
---|
12 | |
---|
13 | The Symmetric Multiprocessing (SMP) support of the RTEMS 4.11.0 and later is available |
---|
14 | on |
---|
15 | |
---|
16 | - ARM, |
---|
17 | |
---|
18 | - PowerPC, and |
---|
19 | |
---|
20 | - SPARC. |
---|
21 | |
---|
22 | It must be explicitly enabled via the ``--enable-smp`` configure command line |
---|
23 | option. To enable SMP in the application configuration see :ref:`Enable SMP |
---|
24 | Support for Applications`. The default scheduler for SMP applications supports |
---|
25 | up to 32 processors and is a global fixed priority scheduler, see also |
---|
26 | :ref:`Configuring Clustered Schedulers`. For example applications |
---|
27 | see:file:`testsuites/smptests`. |
---|
28 | |
---|
29 | .. warning:: |
---|
30 | |
---|
31 | The SMP support in the release of RTEMS is a work in progress. Before you |
---|
32 | start using this RTEMS version for SMP ask on the RTEMS mailing list. |
---|
33 | |
---|
34 | This chapter describes the services related to Symmetric Multiprocessing |
---|
35 | provided by RTEMS. |
---|
36 | |
---|
37 | The application level services currently provided are: |
---|
38 | |
---|
39 | - rtems_get_processor_count_ - Get processor count |
---|
40 | |
---|
41 | - rtems_get_current_processor_ - Get current processor index |
---|
42 | |
---|
43 | - rtems_scheduler_ident_ - Get ID of a scheduler |
---|
44 | |
---|
45 | - rtems_scheduler_get_processor_set_ - Get processor set of a scheduler |
---|
46 | |
---|
47 | - rtems_scheduler_add_processor_ - Add processor to a scheduler |
---|
48 | |
---|
49 | - rtems_scheduler_remove_processor_ - Remove processor from a scheduler |
---|
50 | |
---|
51 | Background |
---|
52 | ========== |
---|
53 | |
---|
54 | Uniprocessor versus SMP Parallelism |
---|
55 | ----------------------------------- |
---|
56 | |
---|
57 | Uniprocessor systems have long been used in embedded systems. In this hardware |
---|
58 | model, there are some system execution characteristics which have long been |
---|
59 | taken for granted: |
---|
60 | |
---|
61 | - one task executes at a time |
---|
62 | |
---|
63 | - hardware events result in interrupts |
---|
64 | |
---|
65 | There is no true parallelism. Even when interrupts appear to occur at the same |
---|
66 | time, they are processed in largely a serial fashion. This is true even when |
---|
67 | the interupt service routines are allowed to nest. From a tasking viewpoint, |
---|
68 | it is the responsibility of the real-time operatimg system to simulate |
---|
69 | parallelism by switching between tasks. These task switches occur in response |
---|
70 | to hardware interrupt events and explicit application events such as blocking |
---|
71 | for a resource or delaying. |
---|
72 | |
---|
73 | With symmetric multiprocessing, the presence of multiple processors allows for |
---|
74 | true concurrency and provides for cost-effective performance |
---|
75 | improvements. Uniprocessors tend to increase performance by increasing clock |
---|
76 | speed and complexity. This tends to lead to hot, power hungry microprocessors |
---|
77 | which are poorly suited for many embedded applications. |
---|
78 | |
---|
79 | The true concurrency is in sharp contrast to the single task and interrupt |
---|
80 | model of uniprocessor systems. This results in a fundamental change to |
---|
81 | uniprocessor system characteristics listed above. Developers are faced with a |
---|
82 | different set of characteristics which, in turn, break some existing |
---|
83 | assumptions and result in new challenges. In an SMP system with N processors, |
---|
84 | these are the new execution characteristics. |
---|
85 | |
---|
86 | - N tasks execute in parallel |
---|
87 | |
---|
88 | - hardware events result in interrupts |
---|
89 | |
---|
90 | There is true parallelism with a task executing on each processor and the |
---|
91 | possibility of interrupts occurring on each processor. Thus in contrast to |
---|
92 | their being one task and one interrupt to consider on a uniprocessor, there are |
---|
93 | N tasks and potentially N simultaneous interrupts to consider on an SMP system. |
---|
94 | |
---|
95 | This increase in hardware complexity and presence of true parallelism results |
---|
96 | in the application developer needing to be even more cautious about mutual |
---|
97 | exclusion and shared data access than in a uniprocessor embedded system. Race |
---|
98 | conditions that never or rarely happened when an application executed on a |
---|
99 | uniprocessor system, become much more likely due to multiple threads executing |
---|
100 | in parallel. On a uniprocessor system, these race conditions would only happen |
---|
101 | when a task switch occurred at just the wrong moment. Now there are N-1 tasks |
---|
102 | executing in parallel all the time and this results in many more opportunities |
---|
103 | for small windows in critical sections to be hit. |
---|
104 | |
---|
105 | Task Affinity |
---|
106 | ------------- |
---|
107 | .. index:: task affinity |
---|
108 | .. index:: thread affinity |
---|
109 | |
---|
110 | RTEMS provides services to manipulate the affinity of a task. Affinity is used |
---|
111 | to specify the subset of processors in an SMP system on which a particular task |
---|
112 | can execute. |
---|
113 | |
---|
114 | By default, tasks have an affinity which allows them to execute on any |
---|
115 | available processor. |
---|
116 | |
---|
117 | Task affinity is a possible feature to be supported by SMP-aware |
---|
118 | schedulers. However, only a subset of the available schedulers support |
---|
119 | affinity. Although the behavior is scheduler specific, if the scheduler does |
---|
120 | not support affinity, it is likely to ignore all attempts to set affinity. |
---|
121 | |
---|
122 | The scheduler with support for arbitary processor affinities uses a proof of |
---|
123 | concept implementation. See https://devel.rtems.org/ticket/2510. |
---|
124 | |
---|
125 | Task Migration |
---|
126 | -------------- |
---|
127 | .. index:: task migration |
---|
128 | .. index:: thread migration |
---|
129 | |
---|
130 | With more than one processor in the system tasks can migrate from one processor |
---|
131 | to another. There are three reasons why tasks migrate in RTEMS. |
---|
132 | |
---|
133 | - The scheduler changes explicitly via ``rtems_task_set_scheduler()`` or |
---|
134 | similar directives. |
---|
135 | |
---|
136 | - The task resumes execution after a blocking operation. On a priority based |
---|
137 | scheduler it will evict the lowest priority task currently assigned to a |
---|
138 | processor in the processor set managed by the scheduler instance. |
---|
139 | |
---|
140 | - The task moves temporarily to another scheduler instance due to locking |
---|
141 | protocols like *Migratory Priority Inheritance* or the *Multiprocessor |
---|
142 | Resource Sharing Protocol*. |
---|
143 | |
---|
144 | Task migration should be avoided so that the working set of a task can stay on |
---|
145 | the most local cache level. |
---|
146 | |
---|
147 | The current implementation of task migration in RTEMS has some implications |
---|
148 | with respect to the interrupt latency. It is crucial to preserve the system |
---|
149 | invariant that a task can execute on at most one processor in the system at a |
---|
150 | time. This is accomplished with a boolean indicator in the task context. The |
---|
151 | processor architecture specific low-level task context switch code will mark |
---|
152 | that a task context is no longer executing and waits that the heir context |
---|
153 | stopped execution before it restores the heir context and resumes execution of |
---|
154 | the heir task. So there is one point in time in which a processor is without a |
---|
155 | task. This is essential to avoid cyclic dependencies in case multiple tasks |
---|
156 | migrate at once. Otherwise some supervising entity is necessary to prevent |
---|
157 | life-locks. Such a global supervisor would lead to scalability problems so |
---|
158 | this approach is not used. Currently the thread dispatch is performed with |
---|
159 | interrupts disabled. So in case the heir task is currently executing on |
---|
160 | another processor then this prolongs the time of disabled interrupts since one |
---|
161 | processor has to wait for another processor to make progress. |
---|
162 | |
---|
163 | It is difficult to avoid this issue with the interrupt latency since interrupts |
---|
164 | normally store the context of the interrupted task on its stack. In case a |
---|
165 | task is marked as not executing we must not use its task stack to store such an |
---|
166 | interrupt context. We cannot use the heir stack before it stopped execution on |
---|
167 | another processor. So if we enable interrupts during this transition we have |
---|
168 | to provide an alternative task independent stack for this time frame. This |
---|
169 | issue needs further investigation. |
---|
170 | |
---|
171 | Clustered Scheduling |
---|
172 | -------------------- |
---|
173 | |
---|
174 | We have clustered scheduling in case the set of processors of a system is |
---|
175 | partitioned into non-empty pairwise-disjoint subsets. These subsets are called |
---|
176 | clusters. Clusters with a cardinality of one are partitions. Each cluster is |
---|
177 | owned by exactly one scheduler instance. |
---|
178 | |
---|
179 | Clustered scheduling helps to control the worst-case latencies in |
---|
180 | multi-processor systems, see :cite:`Brandenburg:2011:SL`. The goal is to reduce |
---|
181 | the amount of shared state in the system and thus prevention of lock |
---|
182 | contention. Modern multi-processor systems tend to have several layers of data |
---|
183 | and instruction caches. With clustered scheduling it is possible to honour the |
---|
184 | cache topology of a system and thus avoid expensive cache synchronization |
---|
185 | traffic. It is easy to implement. The problem is to provide synchronization |
---|
186 | primitives for inter-cluster synchronization (more than one cluster is involved |
---|
187 | in the synchronization process). In RTEMS there are currently four means |
---|
188 | available |
---|
189 | |
---|
190 | - events, |
---|
191 | |
---|
192 | - message queues, |
---|
193 | |
---|
194 | - semaphores using the :ref:`Priority Inheritance` protocol (priority |
---|
195 | boosting), and |
---|
196 | |
---|
197 | - semaphores using the Multiprocessor Resource Sharing Protocol :cite:`Burns:2013:MrsP`. |
---|
198 | |
---|
199 | The clustered scheduling approach enables separation of functions with |
---|
200 | real-time requirements and functions that profit from fairness and high |
---|
201 | throughput provided the scheduler instances are fully decoupled and adequate |
---|
202 | inter-cluster synchronization primitives are used. This is work in progress. |
---|
203 | |
---|
204 | For the configuration of clustered schedulers see :ref:`Configuring Clustered |
---|
205 | Schedulers`. |
---|
206 | |
---|
207 | To set the scheduler of a task see :ref:`SCHEDULER_IDENT - Get ID of a |
---|
208 | scheduler` and :ref:`TASK_SET_SCHEDULER - Set scheduler of a task`. |
---|
209 | |
---|
210 | Task Priority Queues |
---|
211 | -------------------- |
---|
212 | |
---|
213 | Due to the support for clustered scheduling the task priority queues need |
---|
214 | special attention. It makes no sense to compare the priority values of two |
---|
215 | different scheduler instances. Thus, it is not possible to simply use one |
---|
216 | plain priority queue for tasks of different scheduler instances. |
---|
217 | |
---|
218 | One solution to this problem is to use two levels of queues. The top level |
---|
219 | queue provides FIFO ordering and contains priority queues. Each priority queue |
---|
220 | is associated with a scheduler instance and contains only tasks of this |
---|
221 | scheduler instance. Tasks are enqueued in the priority queue corresponding to |
---|
222 | their scheduler instance. In case this priority queue was empty, then it is |
---|
223 | appended to the FIFO. To dequeue a task the highest priority task of the first |
---|
224 | priority queue in the FIFO is selected. Then the first priority queue is |
---|
225 | removed from the FIFO. In case the previously first priority queue is not |
---|
226 | empty, then it is appended to the FIFO. So there is FIFO fairness with respect |
---|
227 | to the highest priority task of each scheduler instances. See also |
---|
228 | :cite:`Brandenburg:2013:OMIP`. |
---|
229 | |
---|
230 | Such a two level queue may need a considerable amount of memory if fast enqueue |
---|
231 | and dequeue operations are desired (depends on the scheduler instance count). |
---|
232 | To mitigate this problem an approch of the FreeBSD kernel was implemented in |
---|
233 | RTEMS. We have the invariant that a task can be enqueued on at most one task |
---|
234 | queue. Thus, we need only as many queues as we have tasks. Each task is |
---|
235 | equipped with spare task queue which it can give to an object on demand. The |
---|
236 | task queue uses a dedicated memory space independent of the other memory used |
---|
237 | for the task itself. In case a task needs to block, then there are two options |
---|
238 | |
---|
239 | - the object already has task queue, then the task enqueues itself to this |
---|
240 | already present queue and the spare task queue of the task is added to a list |
---|
241 | of free queues for this object, or |
---|
242 | |
---|
243 | - otherwise, then the queue of the task is given to the object and the task |
---|
244 | enqueues itself to this queue. |
---|
245 | |
---|
246 | In case the task is dequeued, then there are two options |
---|
247 | |
---|
248 | - the task is the last task on the queue, then it removes this queue from the |
---|
249 | object and reclaims it for its own purpose, or |
---|
250 | |
---|
251 | - otherwise, then the task removes one queue from the free list of the object |
---|
252 | and reclaims it for its own purpose. |
---|
253 | |
---|
254 | Since there are usually more objects than tasks, this actually reduces the |
---|
255 | memory demands. In addition the objects contain only a pointer to the task |
---|
256 | queue structure. This helps to hide implementation details and makes it |
---|
257 | possible to use self-contained synchronization objects in Newlib and GCC (C++ |
---|
258 | and OpenMP run-time support). |
---|
259 | |
---|
260 | Scheduler Helping Protocol |
---|
261 | -------------------------- |
---|
262 | |
---|
263 | The scheduler provides a helping protocol to support locking protocols like |
---|
264 | *Migratory Priority Inheritance* or the *Multiprocessor Resource Sharing |
---|
265 | Protocol*. Each ready task can use at least one scheduler node at a time to |
---|
266 | gain access to a processor. Each scheduler node has an owner, a user and an |
---|
267 | optional idle task. The owner of a scheduler node is determined a task |
---|
268 | creation and never changes during the life time of a scheduler node. The user |
---|
269 | of a scheduler node may change due to the scheduler helping protocol. A |
---|
270 | scheduler node is in one of the four scheduler help states: |
---|
271 | |
---|
272 | :dfn:`help yourself` |
---|
273 | This scheduler node is solely used by the owner task. This task owns no |
---|
274 | resources using a helping protocol and thus does not take part in the |
---|
275 | scheduler helping protocol. No help will be provided for other tasks. |
---|
276 | |
---|
277 | :dfn:`help active owner` |
---|
278 | This scheduler node is owned by a task actively owning a resource and can |
---|
279 | be used to help out tasks. In case this scheduler node changes its state |
---|
280 | from ready to scheduled and the task executes using another node, then an |
---|
281 | idle task will be provided as a user of this node to temporarily execute on |
---|
282 | behalf of the owner task. Thus lower priority tasks are denied access to |
---|
283 | the processors of this scheduler instance. In case a task actively owning |
---|
284 | a resource performs a blocking operation, then an idle task will be used |
---|
285 | also in case this node is in the scheduled state. |
---|
286 | |
---|
287 | :dfn:`help active rival` |
---|
288 | This scheduler node is owned by a task actively obtaining a resource |
---|
289 | currently owned by another task and can be used to help out tasks. The |
---|
290 | task owning this node is ready and will give away its processor in case the |
---|
291 | task owning the resource asks for help. |
---|
292 | |
---|
293 | :dfn:`help passive` |
---|
294 | This scheduler node is owned by a task obtaining a resource currently owned |
---|
295 | by another task and can be used to help out tasks. The task owning this |
---|
296 | node is blocked. |
---|
297 | |
---|
298 | The following scheduler operations return a task in need for help |
---|
299 | |
---|
300 | - unblock, |
---|
301 | |
---|
302 | - change priority, |
---|
303 | |
---|
304 | - yield, and |
---|
305 | |
---|
306 | - ask for help. |
---|
307 | |
---|
308 | A task in need for help is a task that encounters a scheduler state change from |
---|
309 | scheduled to ready (this is a pre-emption by a higher priority task) or a task |
---|
310 | that cannot be scheduled in an unblock operation. Such a task can ask tasks |
---|
311 | which depend on resources owned by this task for help. |
---|
312 | |
---|
313 | In case it is not possible to schedule a task in need for help, then the |
---|
314 | scheduler nodes available for the task will be placed into the set of ready |
---|
315 | scheduler nodes of the corresponding scheduler instances. Once a state change |
---|
316 | from ready to scheduled happens for one of scheduler nodes it will be used to |
---|
317 | schedule the task in need for help. |
---|
318 | |
---|
319 | The ask for help scheduler operation is used to help tasks in need for help |
---|
320 | returned by the operations mentioned above. This operation is also used in |
---|
321 | case the root of a resource sub-tree owned by a task changes. |
---|
322 | |
---|
323 | The run-time of the ask for help procedures depend on the size of the resource |
---|
324 | tree of the task needing help and other resource trees in case tasks in need |
---|
325 | for help are produced during this operation. Thus the worst-case latency in |
---|
326 | the system depends on the maximum resource tree size of the application. |
---|
327 | |
---|
328 | Critical Section Techniques and SMP |
---|
329 | ----------------------------------- |
---|
330 | |
---|
331 | As discussed earlier, SMP systems have opportunities for true parallelism which |
---|
332 | was not possible on uniprocessor systems. Consequently, multiple techniques |
---|
333 | that provided adequate critical sections on uniprocessor systems are unsafe on |
---|
334 | SMP systems. In this section, some of these unsafe techniques will be |
---|
335 | discussed. |
---|
336 | |
---|
337 | In general, applications must use proper operating system provided mutual |
---|
338 | exclusion mechanisms to ensure correct behavior. This primarily means the use |
---|
339 | of binary semaphores or mutexes to implement critical sections. |
---|
340 | |
---|
341 | Disable Interrupts and Interrupt Locks |
---|
342 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
343 | |
---|
344 | A low overhead means to ensure mutual exclusion in uni-processor configurations |
---|
345 | is to disable interrupts around a critical section. This is commonly used in |
---|
346 | device driver code and throughout the operating system core. On SMP |
---|
347 | configurations, however, disabling the interrupts on one processor has no |
---|
348 | effect on other processors. So, this is insufficient to ensure system wide |
---|
349 | mutual exclusion. The macros |
---|
350 | |
---|
351 | - ``rtems_interrupt_disable()``, |
---|
352 | |
---|
353 | - ``rtems_interrupt_enable()``, and |
---|
354 | |
---|
355 | - ``rtems_interrupt_flush()`` |
---|
356 | |
---|
357 | are disabled on SMP configurations and its use will lead to compiler warnings |
---|
358 | and linker errors. In the unlikely case that interrupts must be disabled on |
---|
359 | the current processor, then the |
---|
360 | |
---|
361 | - ``rtems_interrupt_local_disable()``, and |
---|
362 | |
---|
363 | - ``rtems_interrupt_local_enable()`` |
---|
364 | |
---|
365 | macros are now available in all configurations. |
---|
366 | |
---|
367 | Since disabling of interrupts is not enough to ensure system wide mutual |
---|
368 | exclusion on SMP, a new low-level synchronization primitive was added - the |
---|
369 | interrupt locks. They are a simple API layer on top of the SMP locks used for |
---|
370 | low-level synchronization in the operating system core. Currently they are |
---|
371 | implemented as a ticket lock. On uni-processor configurations they degenerate |
---|
372 | to simple interrupt disable/enable sequences. It is disallowed to acquire a |
---|
373 | single interrupt lock in a nested way. This will result in an infinite loop |
---|
374 | with interrupts disabled. While converting legacy code to interrupt locks care |
---|
375 | must be taken to avoid this situation. |
---|
376 | |
---|
377 | .. code-block:: c |
---|
378 | :linenos: |
---|
379 | |
---|
380 | void legacy_code_with_interrupt_disable_enable( void ) |
---|
381 | { |
---|
382 | rtems_interrupt_level level; |
---|
383 | rtems_interrupt_disable( level ); |
---|
384 | /* Some critical stuff */ |
---|
385 | rtems_interrupt_enable( level ); |
---|
386 | } |
---|
387 | |
---|
388 | RTEMS_INTERRUPT_LOCK_DEFINE( static, lock, "Name" ); |
---|
389 | |
---|
390 | void smp_ready_code_with_interrupt_lock( void ) |
---|
391 | { |
---|
392 | rtems_interrupt_lock_context lock_context; |
---|
393 | rtems_interrupt_lock_acquire( &lock, &lock_context ); |
---|
394 | /* Some critical stuff */ |
---|
395 | rtems_interrupt_lock_release( &lock, &lock_context ); |
---|
396 | } |
---|
397 | |
---|
398 | The ``rtems_interrupt_lock`` structure is empty on uni-processor |
---|
399 | configurations. Empty structures have a different size in C |
---|
400 | (implementation-defined, zero in case of GCC) and C++ (implementation-defined |
---|
401 | non-zero value, one in case of GCC). Thus the |
---|
402 | ``RTEMS_INTERRUPT_LOCK_DECLARE()``, ``RTEMS_INTERRUPT_LOCK_DEFINE()``, |
---|
403 | ``RTEMS_INTERRUPT_LOCK_MEMBER()``, and ``RTEMS_INTERRUPT_LOCK_REFERENCE()`` |
---|
404 | macros are provided to ensure ABI compatibility. |
---|
405 | |
---|
406 | Highest Priority Task Assumption |
---|
407 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
408 | |
---|
409 | On a uniprocessor system, it is safe to assume that when the highest priority |
---|
410 | task in an application executes, it will execute without being preempted until |
---|
411 | it voluntarily blocks. Interrupts may occur while it is executing, but there |
---|
412 | will be no context switch to another task unless the highest priority task |
---|
413 | voluntarily initiates it. |
---|
414 | |
---|
415 | Given the assumption that no other tasks will have their execution interleaved |
---|
416 | with the highest priority task, it is possible for this task to be constructed |
---|
417 | such that it does not need to acquire a binary semaphore or mutex for protected |
---|
418 | access to shared data. |
---|
419 | |
---|
420 | In an SMP system, it cannot be assumed there will never be a single task |
---|
421 | executing. It should be assumed that every processor is executing another |
---|
422 | application task. Further, those tasks will be ones which would not have been |
---|
423 | executed in a uniprocessor configuration and should be assumed to have data |
---|
424 | synchronization conflicts with what was formerly the highest priority task |
---|
425 | which executed without conflict. |
---|
426 | |
---|
427 | Disable Preemption |
---|
428 | ~~~~~~~~~~~~~~~~~~ |
---|
429 | |
---|
430 | On a uniprocessor system, disabling preemption in a task is very similar to |
---|
431 | making the highest priority task assumption. While preemption is disabled, no |
---|
432 | task context switches will occur unless the task initiates them |
---|
433 | voluntarily. And, just as with the highest priority task assumption, there are |
---|
434 | N-1 processors also running tasks. Thus the assumption that no other tasks will |
---|
435 | run while the task has preemption disabled is violated. |
---|
436 | |
---|
437 | Task Unique Data and SMP |
---|
438 | ------------------------ |
---|
439 | |
---|
440 | Per task variables are a service commonly provided by real-time operating |
---|
441 | systems for application use. They work by allowing the application to specify a |
---|
442 | location in memory (typically a ``void *``) which is logically added to the |
---|
443 | context of a task. On each task switch, the location in memory is stored and |
---|
444 | each task can have a unique value in the same memory location. This memory |
---|
445 | location is directly accessed as a variable in a program. |
---|
446 | |
---|
447 | This works well in a uniprocessor environment because there is one task |
---|
448 | executing and one memory location containing a task-specific value. But it is |
---|
449 | fundamentally broken on an SMP system because there are always N tasks |
---|
450 | executing. With only one location in memory, N-1 tasks will not have the |
---|
451 | correct value. |
---|
452 | |
---|
453 | This paradigm for providing task unique data values is fundamentally broken on |
---|
454 | SMP systems. |
---|
455 | |
---|
456 | Classic API Per Task Variables |
---|
457 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
---|
458 | |
---|
459 | The Classic API provides three directives to support per task variables. These are: |
---|
460 | |
---|
461 | - ``rtems_task_variable_add`` - Associate per task variable |
---|
462 | |
---|
463 | - ``rtems_task_variable_get`` - Obtain value of a a per task variable |
---|
464 | |
---|
465 | - ``rtems_task_variable_delete`` - Remove per task variable |
---|
466 | |
---|
467 | As task variables are unsafe for use on SMP systems, the use of these services |
---|
468 | must be eliminated in all software that is to be used in an SMP environment. |
---|
469 | The task variables API is disabled on SMP. Its use will lead to compile-time |
---|
470 | and link-time errors. It is recommended that the application developer consider |
---|
471 | the use of POSIX Keys or Thread Local Storage (TLS). POSIX Keys are available |
---|
472 | in all RTEMS configurations. For the availablity of TLS on a particular |
---|
473 | architecture please consult the *RTEMS CPU Architecture Supplement*. |
---|
474 | |
---|
475 | The only remaining user of task variables in the RTEMS code base is the Ada |
---|
476 | support. So basically Ada is not available on RTEMS SMP. |
---|
477 | |
---|
478 | OpenMP |
---|
479 | ------ |
---|
480 | |
---|
481 | OpenMP support for RTEMS is available via the GCC provided libgomp. There is |
---|
482 | libgomp support for RTEMS in the POSIX configuration of libgomp since GCC 4.9 |
---|
483 | (requires a Newlib snapshot after 2015-03-12). In GCC 6.1 or later (requires a |
---|
484 | Newlib snapshot after 2015-07-30 for <sys/lock.h> provided self-contained |
---|
485 | synchronization objects) there is a specialized libgomp configuration for RTEMS |
---|
486 | which offers a significantly better performance compared to the POSIX |
---|
487 | configuration of libgomp. In addition application configurable thread pools |
---|
488 | for each scheduler instance are available in GCC 6.1 or later. |
---|
489 | |
---|
490 | The run-time configuration of libgomp is done via environment variables |
---|
491 | documented in the `libgomp manual <https://gcc.gnu.org/onlinedocs/libgomp/>`_. |
---|
492 | The environment variables are evaluated in a constructor function which |
---|
493 | executes in the context of the first initialization task before the actual |
---|
494 | initialization task function is called (just like a global C++ constructor). |
---|
495 | To set application specific values, a higher priority constructor function must |
---|
496 | be used to set up the environment variables. |
---|
497 | |
---|
498 | .. code-block:: c |
---|
499 | |
---|
500 | #include <stdlib.h> |
---|
501 | void __attribute__((constructor(1000))) config_libgomp( void ) |
---|
502 | { |
---|
503 | setenv( "OMP_DISPLAY_ENV", "VERBOSE", 1 ); |
---|
504 | setenv( "GOMP_SPINCOUNT", "30000", 1 ); |
---|
505 | setenv( "GOMP_RTEMS_THREAD_POOLS", "1$2@SCHD", 1 ); |
---|
506 | } |
---|
507 | |
---|
508 | The environment variable ``GOMP_RTEMS_THREAD_POOLS`` is RTEMS-specific. It |
---|
509 | determines the thread pools for each scheduler instance. The format for |
---|
510 | ``GOMP_RTEMS_THREAD_POOLS`` is a list of optional |
---|
511 | ``<thread-pool-count>[$<priority>]@<scheduler-name>`` configurations separated |
---|
512 | by ``:`` where: |
---|
513 | |
---|
514 | - ``<thread-pool-count>`` is the thread pool count for this scheduler instance. |
---|
515 | |
---|
516 | - ``$<priority>`` is an optional priority for the worker threads of a thread |
---|
517 | pool according to ``pthread_setschedparam``. In case a priority value is |
---|
518 | omitted, then a worker thread will inherit the priority of the OpenMP master |
---|
519 | thread that created it. The priority of the worker thread is not changed by |
---|
520 | libgomp after creation, even if a new OpenMP master thread using the worker |
---|
521 | has a different priority. |
---|
522 | |
---|
523 | - ``@<scheduler-name>`` is the scheduler instance name according to the RTEMS |
---|
524 | application configuration. |
---|
525 | |
---|
526 | In case no thread pool configuration is specified for a scheduler instance, |
---|
527 | then each OpenMP master thread of this scheduler instance will use its own |
---|
528 | dynamically allocated thread pool. To limit the worker thread count of the |
---|
529 | thread pools, each OpenMP master thread must call ``omp_set_num_threads``. |
---|
530 | |
---|
531 | Lets suppose we have three scheduler instances ``IO``, ``WRK0``, and ``WRK1`` |
---|
532 | with ``GOMP_RTEMS_THREAD_POOLS`` set to ``"1@WRK0:3$4@WRK1"``. Then there are |
---|
533 | no thread pool restrictions for scheduler instance ``IO``. In the scheduler |
---|
534 | instance ``WRK0`` there is one thread pool available. Since no priority is |
---|
535 | specified for this scheduler instance, the worker thread inherits the priority |
---|
536 | of the OpenMP master thread that created it. In the scheduler instance |
---|
537 | ``WRK1`` there are three thread pools available and their worker threads run at |
---|
538 | priority four. |
---|
539 | |
---|
540 | Thread Dispatch Details |
---|
541 | ----------------------- |
---|
542 | |
---|
543 | This section gives background information to developers interested in the |
---|
544 | interrupt latencies introduced by thread dispatching. A thread dispatch |
---|
545 | consists of all work which must be done to stop the currently executing thread |
---|
546 | on a processor and hand over this processor to an heir thread. |
---|
547 | |
---|
548 | In SMP systems, scheduling decisions on one processor must be propagated |
---|
549 | to other processors through inter-processor interrupts. A thread dispatch |
---|
550 | which must be carried out on another processor does not happen instantaneously. |
---|
551 | Thus, several thread dispatch requests might be in the air and it is possible |
---|
552 | that some of them may be out of date before the corresponding processor has |
---|
553 | time to deal with them. The thread dispatch mechanism uses three per-processor |
---|
554 | variables, |
---|
555 | |
---|
556 | - the executing thread, |
---|
557 | |
---|
558 | - the heir thread, and |
---|
559 | |
---|
560 | - a boolean flag indicating if a thread dispatch is necessary or not. |
---|
561 | |
---|
562 | Updates of the heir thread are done via a normal store operation. The thread |
---|
563 | dispatch necessary indicator of another processor is set as a side-effect of an |
---|
564 | inter-processor interrupt. So, this change notification works without the use |
---|
565 | of locks. The thread context is protected by a TTAS lock embedded in the |
---|
566 | context to ensure that it is used on at most one processor at a time. |
---|
567 | Normally, only thread-specific or per-processor locks are used during a thread |
---|
568 | dispatch. This implementation turned out to be quite efficient and no lock |
---|
569 | contention was observed in the testsuite. The heavy-weight thread dispatch |
---|
570 | sequence is only entered in case the thread dispatch indicator is set. |
---|
571 | |
---|
572 | The context-switch is performed with interrupts enabled. During the transition |
---|
573 | from the executing to the heir thread neither the stack of the executing nor |
---|
574 | the heir thread must be used during interrupt processing. For this purpose a |
---|
575 | temporary per-processor stack is set up which may be used by the interrupt |
---|
576 | prologue before the stack is switched to the interrupt stack. |
---|
577 | |
---|
578 | Directives |
---|
579 | ========== |
---|
580 | |
---|
581 | This section details the symmetric multiprocessing services. A subsection is |
---|
582 | dedicated to each of these services and describes the calling sequence, related |
---|
583 | constants, usage, and status codes. |
---|
584 | |
---|
585 | .. raw:: latex |
---|
586 | |
---|
587 | \clearpage |
---|
588 | |
---|
589 | .. _rtems_get_processor_count: |
---|
590 | |
---|
591 | GET_PROCESSOR_COUNT - Get processor count |
---|
592 | ----------------------------------------- |
---|
593 | |
---|
594 | CALLING SEQUENCE: |
---|
595 | .. code-block:: c |
---|
596 | |
---|
597 | uint32_t rtems_get_processor_count(void); |
---|
598 | |
---|
599 | DIRECTIVE STATUS CODES: |
---|
600 | The count of processors in the system. |
---|
601 | |
---|
602 | DESCRIPTION: |
---|
603 | On uni-processor configurations a value of one will be returned. |
---|
604 | |
---|
605 | On SMP configurations this returns the value of a global variable set |
---|
606 | during system initialization to indicate the count of utilized processors. |
---|
607 | The processor count depends on the physically or virtually available |
---|
608 | processors and application configuration. The value will always be less |
---|
609 | than or equal to the maximum count of application configured processors. |
---|
610 | |
---|
611 | NOTES: |
---|
612 | None. |
---|
613 | |
---|
614 | .. raw:: latex |
---|
615 | |
---|
616 | \clearpage |
---|
617 | |
---|
618 | .. _rtems_get_current_processor: |
---|
619 | |
---|
620 | GET_CURRENT_PROCESSOR - Get current processor index |
---|
621 | --------------------------------------------------- |
---|
622 | |
---|
623 | CALLING SEQUENCE: |
---|
624 | .. code-block:: c |
---|
625 | |
---|
626 | uint32_t rtems_get_current_processor(void); |
---|
627 | |
---|
628 | DIRECTIVE STATUS CODES: |
---|
629 | The index of the current processor. |
---|
630 | |
---|
631 | DESCRIPTION: |
---|
632 | On uni-processor configurations a value of zero will be returned. |
---|
633 | |
---|
634 | On SMP configurations an architecture specific method is used to obtain the |
---|
635 | index of the current processor in the system. The set of processor indices |
---|
636 | is the range of integers starting with zero up to the processor count minus |
---|
637 | one. |
---|
638 | |
---|
639 | Outside of sections with disabled thread dispatching the current processor |
---|
640 | index may change after every instruction since the thread may migrate from |
---|
641 | one processor to another. Sections with disabled interrupts are sections |
---|
642 | with thread dispatching disabled. |
---|
643 | |
---|
644 | NOTES: |
---|
645 | None. |
---|
646 | |
---|
647 | .. raw:: latex |
---|
648 | |
---|
649 | \clearpage |
---|
650 | |
---|
651 | .. _rtems_scheduler_ident: |
---|
652 | |
---|
653 | SCHEDULER_IDENT - Get ID of a scheduler |
---|
654 | --------------------------------------- |
---|
655 | |
---|
656 | CALLING SEQUENCE: |
---|
657 | .. code-block:: c |
---|
658 | |
---|
659 | rtems_status_code rtems_scheduler_ident( |
---|
660 | rtems_name name, |
---|
661 | rtems_id *id |
---|
662 | ); |
---|
663 | |
---|
664 | DIRECTIVE STATUS CODES: |
---|
665 | .. list-table:: |
---|
666 | :class: rtems-table |
---|
667 | |
---|
668 | * - ``RTEMS_SUCCESSFUL`` |
---|
669 | - Successful operation. |
---|
670 | * - ``RTEMS_INVALID_ADDRESS`` |
---|
671 | - The ``id`` parameter is ``NULL``. |
---|
672 | * - ``RTEMS_INVALID_NAME`` |
---|
673 | - Invalid scheduler name. |
---|
674 | |
---|
675 | DESCRIPTION: |
---|
676 | Identifies a scheduler by its name. The scheduler name is determined by |
---|
677 | the scheduler configuration. See :ref:`Configuring Clustered Schedulers` |
---|
678 | and :ref:`Configuring a Scheduler Name`. |
---|
679 | |
---|
680 | NOTES: |
---|
681 | None. |
---|
682 | |
---|
683 | .. raw:: latex |
---|
684 | |
---|
685 | \clearpage |
---|
686 | |
---|
687 | .. _rtems_scheduler_get_processor_set: |
---|
688 | |
---|
689 | SCHEDULER_GET_PROCESSOR_SET - Get processor set of a scheduler |
---|
690 | -------------------------------------------------------------- |
---|
691 | |
---|
692 | CALLING SEQUENCE: |
---|
693 | .. code-block:: c |
---|
694 | |
---|
695 | rtems_status_code rtems_scheduler_get_processor_set( |
---|
696 | rtems_id scheduler_id, |
---|
697 | size_t cpusetsize, |
---|
698 | cpu_set_t *cpuset |
---|
699 | ); |
---|
700 | |
---|
701 | DIRECTIVE STATUS CODES: |
---|
702 | .. list-table:: |
---|
703 | :class: rtems-table |
---|
704 | |
---|
705 | * - ``RTEMS_SUCCESSFUL`` |
---|
706 | - Successful operation. |
---|
707 | * - ``RTEMS_INVALID_ID`` |
---|
708 | - Invalid scheduler instance identifier. |
---|
709 | * - ``RTEMS_INVALID_ADDRESS`` |
---|
710 | - The ``cpuset`` parameter is ``NULL``. |
---|
711 | * - ``RTEMS_INVALID_NUMBER`` |
---|
712 | - The processor set buffer is too small for the set of processors owned |
---|
713 | by the scheduler instance. |
---|
714 | |
---|
715 | DESCRIPTION: |
---|
716 | Returns the processor set owned by the scheduler instance in ``cpuset``. A |
---|
717 | set bit in the processor set means that this processor is owned by the |
---|
718 | scheduler instance and a cleared bit means the opposite. |
---|
719 | |
---|
720 | NOTES: |
---|
721 | None. |
---|
722 | |
---|
723 | .. raw:: latex |
---|
724 | |
---|
725 | \clearpage |
---|
726 | |
---|
727 | .. _rtems_scheduler_add_processor: |
---|
728 | |
---|
729 | SCHEDULER_ADD_PROCESSOR - Add processor to a scheduler |
---|
730 | ------------------------------------------------------ |
---|
731 | |
---|
732 | CALLING SEQUENCE: |
---|
733 | .. code-block:: c |
---|
734 | |
---|
735 | rtems_status_code rtems_scheduler_add_processor( |
---|
736 | rtems_id scheduler_id, |
---|
737 | uint32_t cpu_index |
---|
738 | ); |
---|
739 | |
---|
740 | DIRECTIVE STATUS CODES: |
---|
741 | .. list-table:: |
---|
742 | :class: rtems-table |
---|
743 | |
---|
744 | * - ``RTEMS_SUCCESSFUL`` |
---|
745 | - Successful operation. |
---|
746 | * - ``RTEMS_INVALID_ID`` |
---|
747 | - Invalid scheduler instance identifier. |
---|
748 | * - ``RTEMS_NOT_CONFIGURED`` |
---|
749 | - The processor is not configured to be used by the application. |
---|
750 | * - ``RTEMS_INCORRECT_STATE`` |
---|
751 | - The processor is configured to be used by the application, however, it |
---|
752 | is not online. |
---|
753 | * - ``RTEMS_RESOURCE_IN_USE`` |
---|
754 | - The processor is already assigned to a scheduler instance. |
---|
755 | |
---|
756 | DESCRIPTION: |
---|
757 | Adds a processor to the set of processors owned by the specified scheduler |
---|
758 | instance. |
---|
759 | |
---|
760 | NOTES: |
---|
761 | Must be called from task context. This operation obtains and releases the |
---|
762 | objects allocator lock. |
---|
763 | |
---|
764 | .. raw:: latex |
---|
765 | |
---|
766 | \clearpage |
---|
767 | |
---|
768 | .. _rtems_scheduler_remove_processor: |
---|
769 | |
---|
770 | SCHEDULER_REMOVE_PROCESSOR - Remove processor from a scheduler |
---|
771 | -------------------------------------------------------------- |
---|
772 | |
---|
773 | CALLING SEQUENCE: |
---|
774 | .. code-block:: c |
---|
775 | |
---|
776 | rtems_status_code rtems_scheduler_remove_processor( |
---|
777 | rtems_id scheduler_id, |
---|
778 | uint32_t cpu_index |
---|
779 | ); |
---|
780 | |
---|
781 | DIRECTIVE STATUS CODES: |
---|
782 | .. list-table:: |
---|
783 | :class: rtems-table |
---|
784 | |
---|
785 | * - ``RTEMS_SUCCESSFUL`` |
---|
786 | - Successful operation. |
---|
787 | * - ``RTEMS_INVALID_ID`` |
---|
788 | - Invalid scheduler instance identifier. |
---|
789 | * - ``RTEMS_INVALID_NUMBER`` |
---|
790 | - The processor is not owned by the specified scheduler instance. |
---|
791 | * - ``RTEMS_RESOURCE_IN_USE`` |
---|
792 | - The set of processors owned by the specified scheduler instance would |
---|
793 | be empty after the processor removal and there exists a non-idle task |
---|
794 | that uses this scheduler instance as its home scheduler instance. |
---|
795 | |
---|
796 | DESCRIPTION: |
---|
797 | Removes a processor from set of processors owned by the specified scheduler |
---|
798 | instance. |
---|
799 | |
---|
800 | NOTES: |
---|
801 | Must be called from task context. This operation obtains and releases the |
---|
802 | objects allocator lock. Removing a processor from a scheduler is a complex |
---|
803 | operation that involves all tasks of the system. |
---|