Skip to content

Commit 906536f

Browse files
committed
Fix #2750, add in Task Affinity TA module to cFE
1 parent 9cf6c04 commit 906536f

57 files changed

Lines changed: 6054 additions & 2 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmake/sample_defs/cfe_perfids.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#define CFE_MISSION_TIME_LOCAL1HZTASK_PERF_ID 10 /**< \brief Performance ID for 1 Hz Local Task */
5555
#define CFE_MISSION_TIME_TONE1HZTASK_PERF_ID 11 /**< \brief Performance ID for 1 Hz Tone Task */
5656

57+
#define CFE_MISSION_TA_MAIN_PERF_ID 12 /**< \brief Performance ID for Task Affinity Task */
5758
/** \} */
5859

5960
#endif /* SAMPLE_PERFIDS_H */

‎cmake/sample_defs/eds/cfe-topicids.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
<Define name="SB_SUB_RPT_CTRL_TOPICID" value="${CFE_MISSION/TELECOMMAND_BASE_TOPICID} + 014"/>
8484
<Define name="TIME_TONE_CMD_TOPICID" value="${CFE_MISSION/TELECOMMAND_BASE_TOPICID} + 016"/>
8585
<Define name="TIME_ONEHZ_CMD_TOPICID" value="${CFE_MISSION/TELECOMMAND_BASE_TOPICID} + 017"/>
86+
<Define name="TA_CMD_TOPICID" value="${CFE_MISSION/TELECOMMAND_BASE_TOPICID} + 018"/>
87+
<Define name="TA_SEND_HK_TOPICID" value="${CFE_MISSION/TELECOMMAND_BASE_TOPICID} + 019"/>
8688
<Define name="TO_LAB_CMD_TOPICID" value="${CFE_MISSION/TELECOMMAND_BASE_TOPICID} + 128"/>
8789
<Define name="TO_LAB_SEND_HK_TOPICID" value="${CFE_MISSION/TELECOMMAND_BASE_TOPICID} + 129"/>
8890
<Define name="TO_SEND_HK_TOPICID" value="${CFE_MISSION/TELECOMMAND_BASE_TOPICID} + 129"/>
@@ -109,6 +111,7 @@
109111
<Define name="TBL_REG_TLM_TOPICID" value="${CFE_MISSION/TELEMETRY_BASE_TOPICID} + 012"/>
110112
<Define name="SB_ALLSUBS_TLM_TOPICID" value="${CFE_MISSION/TELEMETRY_BASE_TOPICID} + 013"/>
111113
<Define name="SB_ONESUB_TLM_TOPICID" value="${CFE_MISSION/TELEMETRY_BASE_TOPICID} + 014"/>
114+
<Define name="TA_HK_TLM_TOPICID" value="${CFE_MISSION/TELEMETRY_BASE_TOPICID} + 015"/>
112115
<Define name="ES_MEMSTATS_TLM_TOPICID" value="${CFE_MISSION/TELEMETRY_BASE_TOPICID} + 016"/>
113116
<Define name="TO_LAB_HK_TLM_TOPICID" value="${CFE_MISSION/TELEMETRY_BASE_TOPICID} + 128"/>
114117
<Define name="TO_LAB_DATA_TYPES_TOPICID" value="${CFE_MISSION/TELEMETRY_BASE_TOPICID} + 129"/>

‎cmake/sample_defs/example_platform_cfg.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,37 @@
15181518
#define CFE_PLATFORM_TBL_VALID_PRID_3 0
15191519
#define CFE_PLATFORM_TBL_VALID_PRID_4 0
15201520

1521+
/***************************************************************************/
1522+
/*
1523+
* CFE Task Affinity (CFE_TA) Application Private Config Definitions
1524+
*/
1525+
1526+
/**
1527+
** \cfeescfg Define TA Task Priority
1528+
**
1529+
** \par Description:
1530+
** Defines the cFE_TA Task priority.
1531+
**
1532+
** \par Limits
1533+
** Not Applicable
1534+
*/
1535+
#define CFE_PLATFORM_TA_START_TASK_PRIORITY 75
1536+
1537+
/**
1538+
** \cfeescfg Define TA Task Stack Size
1539+
**
1540+
** \par Description:
1541+
** Defines the cFE_TA Task Stack Size
1542+
**
1543+
** \par Limits
1544+
** There is a lower limit of 2048 on this configuration parameter. There
1545+
** are no restrictions on the upper limit however, the maximum stack size
1546+
** is system dependent and should be verified. Most operating systems provide
1547+
** tools for measuring the amount of stack used by a task during operation. It
1548+
** is always a good idea to verify that no more than 1/2 of the stack is used.
1549+
*/
1550+
#define CFE_PLATFORM_TA_START_TASK_STACK_SIZE CFE_PLATFORM_ES_DEFAULT_STACK_SIZE
1551+
15211552
/*************************************************************************/
15221553
/*
15231554
* CFE Time Service (CFE_TIME) Application Private Config Definitions

‎cmake/sample_defs/targets.cmake‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ SET(SPACECRAFT_ID 0x42)
8181
# or override parts of the CFE core on a mission-specific basis.
8282
#list(APPEND MISSION_CORE_MODULES mymodule)
8383

84+
# Task Affinity CFE module
85+
#list(APPEND MISSION_CORE_MODULES ta)
86+
87+
8488
# The "MISSION_GLOBAL_APPLIST" is a set of apps/libs that will be built
8589
# for every defined target. These are built as dynamic modules
8690
# and must be loaded explicitly via startup script or command.

‎cmake/target/CMakeLists.txt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cfe_module_version_table.c
141141
${MISSION_BINARY_DIR}/src/cfe_module_version_table.c
142142
)
143143

144+
# Only locate and compile the TA configuration if TA is appended to Core Modules
145+
if ("ta" IN_LIST MISSION_CORE_MODULES)
146+
cfe_locate_implementation_file(CFE_TA_AFFINITY_TABLE
147+
"cfe_ta_cpuaffinity.c"
148+
PREFIX "${BUILD_CONFIG}"
149+
FALLBACK_FILE "${CFE_SOURCE_DIR}/modules/ta/examples/sample_cfe_ta_cpuaffinity.c"
150+
)
151+
else()
152+
# If TA is disabled, set this to empty so the executable doesn't try to compile anything
153+
set(CFE_TA_AFFINITY_TABLE "")
154+
endif()
155+
144156
# Target for the final executable
145157
add_executable(core-${TGTNAME}
146158
${MISSION_BINARY_DIR}/src/cfe_mission_strings.c
@@ -150,6 +162,7 @@ add_executable(core-${TGTNAME}
150162
${CMAKE_CURRENT_BINARY_DIR}/cfe_static_symbol_list.c
151163
${CMAKE_CURRENT_BINARY_DIR}/cfe_static_module_list.c
152164
src/target_config.c
165+
${CFE_TA_AFFINITY_TABLE}
153166
)
154167

155168
target_compile_definitions(core-${TGTNAME} PRIVATE
@@ -160,6 +173,13 @@ target_compile_definitions(core-${TGTNAME} PRIVATE
160173
CFE_CPU_ID_VALUE=${${TGTNAME}_PROCESSORID}
161174
)
162175

176+
if (CFE_TA_AFFINITY_TABLE)
177+
# If the file was found, tell target_config.c the name of the extern array
178+
target_compile_definitions(core-${TGTNAME} PRIVATE
179+
CFE_TA_AFFINITY_TABLE_PTR=CFE_TA_TargetConfigList
180+
)
181+
endif()
182+
163183
target_include_directories(core-${TGTNAME} PRIVATE
164184
"${CMAKE_BINARY_DIR}/${CFE_CORE_TARGET}/inc"
165185
)

‎cmake/target/inc/target_config.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ typedef const struct
232232
*/
233233
const CFE_SbIntfDbObject_t *SbIntfDb;
234234

235+
const void *TaAffinityTable;
236+
235237
} Target_ConfigData;
236238

237239
/**

‎cmake/target/src/target_config.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@
5555
#define CFE_DEFAULT_CORE_FILENAME ""
5656
#endif
5757

58+
/*
59+
* If the CMake build didn't provide a TA affinity table, default to NULL.
60+
* Otherwise, declare the extern array that the mission config provided.
61+
*/
62+
#ifndef CFE_TA_AFFINITY_TABLE_PTR
63+
#define CFE_TA_AFFINITY_TABLE_PTR NULL
64+
#else
65+
extern const char CFE_TA_AFFINITY_TABLE_PTR[];
66+
#endif
67+
5868
/*
5969
* Many configdata items are instantiated by the
6070
* build system, where it generates a .c file containing
@@ -261,5 +271,6 @@ Target_ConfigData GLOBAL_CONFIGDATA = {
261271
.StaticAppList = CFE_STATIC_APP_LIST,
262272
.EdsDb = CFE_CONST_EDS_DB_PTR,
263273
.DynamicEdsDb = CFE_NONCONST_EDS_DB_PTR,
264-
.SbIntfDb = CFE_SB_INTF_DB_PTR
274+
.SbIntfDb = CFE_SB_INTF_DB_PTR,
275+
.TaAffinityTable = CFE_TA_AFFINITY_TABLE_PTR
265276
};

‎docs/src/cfe_ta.dox‎

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
/**
2+
\page cfetaovr cFE Task Affinity (TA) Overview
3+
4+
The cFE Task Affinity (TA) application provides a centralized mechanism for managing
5+
and configuring CPU core affinities for operating system tasks (threads) within the
6+
core Flight System (cFS).
7+
8+
In multi-core processing environments, it is often critical to ensure that certain
9+
tasks run on specific CPU cores to meet real-time deadlines, reduce cache thrashing,
10+
or isolate high-priority operations from background processing. TA bridges the gap
11+
between the cFS environment and the underlying OS (via the OSAL), allowing system
12+
operators to dynamically monitor and alter the core affinity of running tasks.
13+
14+
TA operates both autonomously during system startup (via OSAL event hooks) and
15+
interactively via ground commands. It is designed as an optional core module
16+
that can be conditionally compiled into the cFS objtab based on mission needs.
17+
18+
<h3>Mission Integration</h3>
19+
20+
Because TA is an optional core module, it must be explicitly enabled and configured
21+
for your specific mission deployment. To integrate TA into a cFS mission, follow
22+
these steps:
23+
24+
<B>1. Enable the Module</B> <BR>
25+
Add the `ta` module to the core modules list in your mission's `targets.cmake` file:
26+
\code
27+
list(APPEND MISSION_CORE_MODULES ta)
28+
\endcode
29+
30+
<B>2. Register Message IDs</B> <BR>
31+
The TA message IDs must be integrated into the mission's global message ID namespace.
32+
Include the TA message ID definitions in your mission's `cfe_msgids.h` file
33+
(typically located in the mission `sample_defs` directory):
34+
\code
35+
#include "cfe_ta_msgids.h"
36+
\endcode
37+
38+
<B>3. Configure Target-Specific Affinities</B> <BR>
39+
The default task affinity assignments are defined in `cfe_ta_cpuaffinity.c`.
40+
Because different deployment targets (e.g., `cpu1`, `cpu2`) often have different
41+
hardware architectures and core counts, you can provide target-specific affinity
42+
configurations. Simply place a uniquely prefixed file in your mission definitions
43+
directory (e.g., `sample_defs/`), and the cFS build system will automatically
44+
locate and compile it.
45+
<BR><BR>
46+
For example, to configure affinities specifically for `cpu1`, create the file:
47+
<tt>cpu1_cfe_ta_cpuaffinity.c</tt>
48+
49+
<hr>
50+
51+
For additional detail on Task Affinity and how to manage it, see the following sections:
52+
53+
<UL>
54+
<LI> \subpage cfetaugmanage <BR>
55+
<LI> \subpage cfetaugstartup <BR>
56+
<LI> \subpage cfetaugtelemetry <BR>
57+
<LI> \subpage cfetaugfaq <BR>
58+
<LI> \subpage cfetacmds <BR>
59+
<LI> \subpage cfetatlm <BR>
60+
<LI> \subpage cfetacfg <BR>
61+
</UL>
62+
**/
63+
64+
/**
65+
\page cfetaugmanage Managing Task Affinity
66+
67+
Operators can interact with the TA application to query the current state of task
68+
affinities across the system and make real-time adjustments.
69+
70+
When a multi-core OS schedules a task, it uses an "Affinity Mask" to determine which
71+
cores the task is permitted to run on. Each bit in the mask corresponds to a specific
72+
CPU core (e.g., Bit 0 = Core 0, Bit 1 = Core 1). A mask of `0x03` (binary `0011`)
73+
indicates the task can run on Core 0 or Core 1.
74+
75+
The TA application maintains an internal list of tasks (`CpuTaskAffinity`)
76+
and their associated affinity masks. Operators can manage these masks using the
77+
following methods:
78+
79+
<B>1. Setting Task Affinity</B> <BR>
80+
Operators can send the \ref CFE_TA_SET_TASK_AFFINITY_CC command, specifying a task
81+
name and a desired affinity mask. TA will look up the task via OSAL (`OS_TaskGetIdByName`),
82+
apply the new mask to the OS scheduling queue, and read the applied mask back
83+
from the OS to verify the change.
84+
85+
<B>2. Getting Task Affinity</B> <BR>
86+
Operators can send the \ref CFE_TA_GET_TASK_AFFINITY_CC command to request the current active
87+
affinity mask for a specific task. TA will query the OS and update its internal
88+
table. If the OS reports a different affinity than what TA has on record, TA will
89+
log a discrepancy event and resynchronize its internal state.
90+
91+
<B>3. Querying All Tasks</B> <BR>
92+
For a comprehensive view of the system, operators can send the \ref CFE_TA_QUERY_ALL_TASKS_CC
93+
command. TA will generate a diagnostic packet containing system-wide CPU
94+
capabilities (cores configured, cores online) and write the current affinity
95+
state of all tracked tasks into an onboard file. This file can then be downlinked
96+
and analyzed by the ground system.
97+
**/
98+
99+
/**
100+
\page cfetaugstartup Startup and Autonomous Operations
101+
102+
Unlike standard cFE applications that only respond to commands, TA actively listens
103+
to the Operating System through an OSAL Event Handler (`CFE_TA_OS_EventHandler`).
104+
105+
During the `CFE_TA_EarlyInit` phase, TA registers itself with OSAL to receive
106+
notifications whenever a new OS resource is created. Specifically, TA monitors for
107+
`OS_EVENT_TASK_STARTUP` events.
108+
109+
When a new task starts, the following sequence occurs autonomously:
110+
-# The OSAL triggers the TA event handler.
111+
-# TA extracts the name of the newly started task.
112+
-# TA looks up the task name in its default configuration table
113+
(`CpuTaskAffinity`).
114+
-# If the task is found and configured to use a specific mask (`CFE_TA_Mask`),
115+
TA immediately applies that mask to the new task.
116+
-# If the task is not in the list, TA automatically adds it to its internal tracking
117+
table and flags it as `CFE_TA_Added`, leaving its affinity to the OS default.
118+
119+
This allows mission developers to define a static layout of task affinities in the
120+
`cfe_ta_cpuaffinity.c` file before compilation. As the cFS system boots up and
121+
various apps start their child tasks, TA seamlessly intercepts them and pins them
122+
to their pre-assigned cores without requiring any manual ground intervention.
123+
**/
124+
125+
/**
126+
\page cfetaugtelemetry TA Telemetry and Diagnostics
127+
128+
The TA application produces a standard Housekeeping packet (\ref CFE_TA_HK_TLM_MID)
129+
that provides operators with insight into the health and status of the application.
130+
131+
The housekeeping packet tracks standard command processing statistics:
132+
<UL>
133+
<LI> `CommandCounter`: Increments on successfully executed commands.
134+
<LI> `CommandErrorCounter`: Increments when commands fail length checks, use
135+
invalid function codes, or encounter execution errors.
136+
</UL>
137+
138+
Additionally, it tracks autonomous startup statistics:
139+
<UL>
140+
<LI> `StartupCounter`: The number of tasks successfully processed and assigned
141+
an affinity mask during system boot/task creation.
142+
<LI> `StartupErrorCounter`: The number of tasks that failed to receive their
143+
startup affinity assignment due to OSAL errors.
144+
</UL>
145+
**/
146+
147+
/**
148+
\page cfetaugfaq Frequently Asked Questions about Task Affinity
149+
150+
<B> (Q)
151+
I sent a SET_TASK_AFFINITY command, but I received a CFE_TA_SET_AFFINITY_CMD_NOTFOUND_ERR_EID.
152+
What happened?
153+
</B> <BR> <BR> <I>
154+
The TA application maintains a finite internal list of tasks (`OS_MAX_TASKS`).
155+
If the task name provided in the command does not match any task currently
156+
tracked by TA, it cannot set the affinity. Ensure the task name is spelled
157+
correctly and matches the OSAL assigned name.
158+
</I>
159+
160+
<B> (Q)
161+
Why does the GET_TASK_AFFINITY command sometimes generate a "discrepancy" event?
162+
</B> <BR> <BR> <I>
163+
TA tracks what it *believes* the affinity mask should be. However, the
164+
underlying OS (or a lower-level library) might have altered the thread's
165+
affinity outside of TA's awareness. When TA queries the OS and notices the
166+
real mask differs from its internal record, it issues a
167+
`CFE_TA_GET_TASK_AFFINITY_DISCREPANCY_INF_EID` to alert operators that an
168+
uncommanded change occurred, and it resynchronizes its table.
169+
</I>
170+
171+
<B> (Q)
172+
Is TA dependent on the Platform Service Package (PSP)?
173+
</B> <BR> <BR> <I>
174+
No. In the modern cFS modular architecture, TA is implemented entirely at the
175+
CFE core module level. It utilizes OSAL APIs (`OS_TaskAffinitySetAffinity`, etc.) to
176+
abstract away the specific underlying operating system. If it is included in
177+
the `MISSION_CORE_MODULES` CMake list, the cFE Executive Services (ES) will
178+
automatically initialize it and generate its objtab entry.
179+
</I>
180+
181+
<B> (Q)
182+
What happens if I try to set an affinity mask for a core that doesn't exist?
183+
</B> <BR> <BR> <I>
184+
TA safely clamps the user-provided mask. During the mask translation process
185+
(`CFE_TA_Cpu_Set`), TA checks the requested bits against `OS_MAX_CPUS` and
186+
the actual hardware cores configured (`OS_TaskAffinityCoresConfigured()`). Bits mapped
187+
to non-existent cores are safely ignored, preventing OS-level exceptions.
188+
</I>
189+
**/
190+
191+
/**
192+
** \page cfetacmds cFE TA Commands
193+
**
194+
** Upon receipt of any command, the TA application will confirm that the
195+
** message length embedded within the header (from `CFE_MSG_GetSize()`) matches the expected
196+
** length of that message, based on the size of the C structure defining that command.
197+
** If there is any discrepancy between the expected and actual message size, TA will generate
198+
** the #CFE_TA_LEN_ERR_EID event, increment the command error counter, and the
199+
** command will _not_ be accepted for processing.
200+
**
201+
** The following is a list of commands that are processed by the cFE TA Task:
202+
**
203+
** \tableofcontents
204+
**
205+
** <table border="1" cellpadding="2" width="100%">
206+
** <tr><th>Command Name</th><th>Function Code</th><th>Payload Structure</th><th>Description</th></tr>
207+
** <tr><td>No-Op</td><td>\ref CFE_TA_NOOP_CC</td><td>\ref CFE_TA_NoopCmd_t</td><td>Generates an info event to verify app is alive.</td></tr>
208+
** <tr><td>Reset Counters</td><td>\ref CFE_TA_RESET_COUNTERS_CC</td><td>\ref CFE_TA_ResetCountersCmd_t</td><td>Resets the command and error counters to zero.</td></tr>
209+
** <tr><td>Query All Tasks</td><td>\ref CFE_TA_QUERY_ALL_TASKS_CC</td><td>\ref CFE_TA_QueryAllTasksCmd_t</td><td>Writes the current affinity state of all tracked tasks to an onboard file.</td></tr>
210+
** <tr><td>Set Task Affinity</td><td>\ref CFE_TA_SET_TASK_AFFINITY_CC</td><td>\ref CFE_TA_SetTaskAffinityCmd_t</td><td>Sets a new CPU affinity mask for a specific task.</td></tr>
211+
** <tr><td>Get Task Affinity</td><td>\ref CFE_TA_GET_TASK_AFFINITY_CC</td><td>\ref CFE_TA_GetTaskAffinityCmd_t</td><td>Queries the OS for a specific task's affinity and updates the internal tracking table.</td></tr>
212+
** </table>
213+
**/
214+
215+
/**
216+
** \page cfetatlm cFE TA Telemetry
217+
**
218+
** The following are telemetry packets generated by the cFE TA Task:
219+
**
220+
** \tableofcontents
221+
**
222+
** <table border="1" cellpadding="2" width="100%">
223+
** <tr><th>Telemetry Packet Name</th><th>Message ID</th><th>Payload Structure</th><th>Description</th></tr>
224+
** <tr><td>Housekeeping (HK)</td><td>\ref CFE_TA_HK_TLM_MID</td><td>\ref CFE_TA_HousekeepingTlm_t</td><td>Contains command execution counters, errors, and startup tracking metrics. Generated upon receipt of the SEND_HK command.</td></tr>
225+
** </table>
226+
**
227+
** Additionally, the TA application defines a diagnostic structure (\ref CFE_TA_DiagInfo_t)
228+
** which is used to dump the complete system affinity state to a file when the
229+
** \ref CFE_TA_QUERY_ALL_TASKS_CC command is received.
230+
**/
231+
232+
/**
233+
** \page cfetacfg cFE TA Configuration Parameters
234+
**
235+
** The following are configuration parameters used to configure the cFE TA Application
236+
** for a specific platform or mission. These are defined in the associated configuration
237+
** header files.
238+
**
239+
** <table border="1" cellpadding="2" width="100%">
240+
** <tr><th>Parameter Name</th><th>Description</th></tr>
241+
** <tr><td>\ref CFE_TA_PIPE_NAME</td><td>The string name used for the TA application's command pipe.</td></tr>
242+
** <tr><td>\ref CFE_TA_PIPE_DEPTH</td><td>The depth of the TA command pipe (number of messages it can hold).</td></tr>
243+
** <tr><td>\ref CFE_TA_MAX_EVENT_SEND_COUNT</td><td>The maximum number of times an identical event can be sent before being filtered.</td></tr>
244+
** <tr><td>\ref CFE_TA_TASK_LOG_DESC</td><td>The file description header placed in the file generated by the Query All Tasks command.</td></tr>
245+
** </table>
246+
**/

0 commit comments

Comments
 (0)