FreeRTOS / FreeRTOS-Kernel

FreeRTOS kernel files only, submoduled into https://github.com/FreeRTOS/FreeRTOS and various other repos.
https://www.FreeRTOS.org
MIT License
2.71k stars 1.11k forks source link

Add traceSTARTING_SCHEDULER tracing hook. #1082

Closed schilkp closed 3 months ago

schilkp commented 3 months ago

Description

Discussed here: https://forums.freertos.org/t/tracing-improvements/20097

This hook enables tracers to run code on startup after all RTOS resources are created and to detect that the scheduler is starting without relying on traceENTER/traceEXIT macros.

It also provides tracers access to the task handle of all IDLE tasks, allowing them to be identified unambiguously and without relying on INCLUDE_xTaskGetIdleTaskHandle.

Notes: I had another think about the above discussion regarding a traceTASK_IS_IDLE_TASK and traceTASK_IS_TIMER_TASK hook. I agree that they are probably very specific and maybe not the best solution. Instead I propose a traceSTARTING_SCHEDULER hook that is called just before xPortStartScheduler and exposes the idle task TCBs.

This is probably a much more generic solution. It allows tracers to cleanly detect that the scheduler has started, and gives them an opportunity to inject code after the idle and timer tasks have been created. This allows a tracer to, if they wish, identify those tasks unambiguously.

I opted to expose xIdleTaskHandles as a macro parameter so the tracer does not have rely on "unhygienically" accessing this static variable or on INCLUDE_xTaskGetIdleTaskHandle being enabled.

I am happy for feedback here if you think this is appropriate.

Test Steps

The CMOCK test suite in https://github.com/FreeRTOS/FreeRTOS continues to pass with no regressions, after the fallback tracing hook has also been added to the duplicate FreeRTOS.h header file in that repo used during mock code generation:

diff --git a/FreeRTOS/Test/CMock/tasks/tasks_freertos/FreeRTOS.h b/FreeRTOS/Test/CMock/tasks/tasks_freertos/FreeRTOS.h
index bd7b65c10..1e6b6a72f 100644
--- a/FreeRTOS/Test/CMock/tasks/tasks_freertos/FreeRTOS.h
+++ b/FreeRTOS/Test/CMock/tasks/tasks_freertos/FreeRTOS.h
@@ -565,6 +565,13 @@
     #define traceTASK_SWITCHED_IN()
 #endif

+#ifndef traceSTARTING_SCHEDULER
+
+/* Called after all idle tasks and timer task (if enabled) have been created
+ * sucesfully, just before the scheduler is started. */
+    #define traceSTARTING_SCHEDULER( xIdleTaskHandles )
+#endif
+
 #ifndef traceINCREASE_TICK_COUNT

 /* Called before stepping the tick count after waking from tickless idle

Please let me know how, if you are looking at merging, how I should go about updating this?

Checklist:

Related Issue

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

schilkp commented 3 months ago

Fixed spelling error. CMock tests continue to fail due to missing fallback macro in FreeRTOS.h in "FreeRTOS" repo as described above.

shubnil commented 3 months ago

Hi @schilkp , Thanks for creating the PR. Please fix the spell check errors. After that we can merge the PR.

schilkp commented 3 months ago

Hi @shubnil!

Thanks for the review. Sure - It seems I still can't get that word correct after 2 tries :)

In general, what is the preferred workflow here? Do you want to me to force-push a fixed version (including possibly a re-base) or stack a commit?

shubnil commented 3 months ago

You can abandon this PR and create a new one with the fix.

sonarcloud[bot] commented 3 months ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud