forked from cjchanx/CubePlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
Profiling task created #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChristyGuirguis
wants to merge
6
commits into
master
Choose a base branch
from
Christy/freeRTOSProfiler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+331
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95d82da
test profiler function created
f652d8a
profiling messages made
d65d8c7
refactored display
1fae097
display in table, run as task
0808d1b
modified output to comma seperated values
b5e47f5
moved profiler to seperate folder, modified readme
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| /** | ||
| ******************************************************************************** | ||
| * @file Profiler.cpp | ||
| * @author Christy | ||
| * @date Jan 28, 2026 | ||
| * @brief | ||
| ******************************************************************************** | ||
| */ | ||
|
|
||
| /************************************ | ||
| * INCLUDES | ||
| ************************************/ | ||
| #include "ProfilerTask.hpp" | ||
| #include "SystemDefines.hpp" | ||
| #include <string> | ||
| #include <cstring> | ||
| #include <vector> | ||
|
|
||
| /************************************ | ||
| * PRIVATE MACROS AND DEFINES | ||
| ************************************/ | ||
|
|
||
| /************************************ | ||
| * VARIABLES | ||
| ************************************/ | ||
| bool profileSystem = false; | ||
|
|
||
| /************************************ | ||
| * FUNCTION DECLARATIONS | ||
| ************************************/ | ||
|
|
||
| /************************************ | ||
| * FUNCTION DEFINITIONS | ||
| ************************************/ | ||
| /** | ||
| * @brief Constructor, sets all member variables | ||
| */ | ||
| ProfilerTask::ProfilerTask() | ||
| : Task(TASK_PROFILER_QUEUE_DEPTH_OBJS) {} | ||
|
|
||
| /** | ||
| * @brief Init task for RTOS | ||
| */ | ||
| void ProfilerTask::InitTask() { | ||
| // Make sure the task is not already initialized | ||
| SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize Profiler task twice"); | ||
|
|
||
| // Start the task | ||
| BaseType_t rtValue = xTaskCreate( | ||
| (TaskFunction_t)ProfilerTask::RunTask, (const char*)"ProfilerTask", | ||
| (uint16_t)TASK_PROFILER_STACK_DEPTH_WORDS, (void*)this, | ||
| (UBaseType_t)TASK_PROFILER_PRIORITY, (TaskHandle_t*)&rtTaskHandle); | ||
|
|
||
| // Ensure creation succeded | ||
| SOAR_ASSERT(rtValue == pdPASS, "ProfilerTask::InitTask - xTaskCreate() failed"); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @brief Runcode for the ProfilerTask | ||
| */ | ||
| void ProfilerTask::Run(void* pvParams) { | ||
| while (1) { | ||
| // profile system while profileSystem is true | ||
| if (profileSystem) { | ||
| ProfileSystem(); | ||
| } | ||
| vTaskDelay(pdMS_TO_TICKS(2000)); // delay interval | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void ProfilerTask::CollectTaskList(std::vector<TaskProfile>& Profiles) { | ||
| // place vTaskList output into buffer | ||
| char buffer[512]; | ||
| vTaskList(buffer); | ||
|
|
||
| // parse tasks from line | ||
| char* line = strtok(buffer, "\n"); | ||
| while (line != NULL) { | ||
| char name[configMAX_TASK_NAME_LEN]; | ||
| char state; | ||
| int priority; | ||
| int stack; | ||
| int num; | ||
|
|
||
| // read 5 attributes succesfully | ||
| if (sscanf(line, "%15[^ ] %c %d %d %d", name, &state, &priority, &stack, &num) == 5) { | ||
| TaskProfile profile; | ||
|
|
||
| // if name, state, priority, stack, and num successfully read populate profile object | ||
| strncpy(profile.name, name, sizeof(profile.name)); | ||
|
|
||
| // convert state to human readable format | ||
| std::string readableState; | ||
| switch (state) { | ||
| case 'R': readableState = "Ready"; | ||
| break; | ||
| case 'B': readableState = "Blocked"; | ||
| break; | ||
| case 'S': readableState = "Suspended"; | ||
| break; | ||
| case 'X': readableState = "Executing"; | ||
| break; | ||
| default: readableState = "Unknown"; | ||
| break; | ||
| } | ||
| strncpy(profile.state, readableState.c_str(), sizeof(profile.state)); | ||
| profile.priority = priority; | ||
| profile.stackRemaining = stack; | ||
| strcpy(profile.cpuPercent, "?"); | ||
|
|
||
| // append task profile to global vector | ||
| Profiles.push_back(profile); | ||
| } | ||
|
|
||
| line = strtok(NULL, "\n"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void ProfilerTask::CollectCPUStats(std::vector<TaskProfile>& Profiles) { | ||
| // place vTaskGetRunTimeStats output into buffer | ||
| char buffer[512]; | ||
| vTaskGetRunTimeStats(buffer); | ||
|
|
||
| // parse runtime stats | ||
| char* line = strtok(buffer, "\n"); | ||
| while (line != NULL) { | ||
| char name[32]; | ||
| char percent[8]; | ||
|
|
||
| // read 2 attributes succesfully | ||
| if (sscanf(line, "%15s %*s %3s", name, percent) == 2) { // ignore abs time | ||
| // if name and time percentage sucessfully read, find task in global vector | ||
| for (auto& p : Profiles) { | ||
| if (strcmp(p.name, name) == 0) { | ||
| // initialize task percentage | ||
| strncpy(p.cpuPercent, percent, sizeof(p.cpuPercent)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| line = strtok(NULL, "\n"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void ProfilerTask::DisplayTable(std::vector<TaskProfile>& Profiles) { | ||
| // print profile message for tasks | ||
| SOAR_PRINT("Task Name, Task State, Task Priority, Stack High Water Mark (words), Task CPU Usage\r\n\n"); | ||
|
|
||
| for (auto& p : Profiles) { | ||
| SOAR_PRINT("%s, %s, %d, %d, %s\r\n", p.name, p.state, p.priority, p.stackRemaining, p.cpuPercent); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void ProfilerTask::ProfileSystem() { | ||
| // clear terminal, previous task profiles | ||
| SOAR_PRINT("\033[2J\033[H"); | ||
|
|
||
| // store task structs in vector | ||
| std::vector<TaskProfile> Profiles; | ||
|
|
||
| // collect profile stats, display profile stat table | ||
| CollectTaskList(Profiles); | ||
| CollectCPUStats(Profiles); | ||
| DisplayTable(Profiles); | ||
|
|
||
| // display heap stats | ||
| SOAR_PRINT("\r\nFree Heap (bytes), Min Ever Free Heap (bytes)"); | ||
| SOAR_PRINT("\r\n%lu, %lu", xPortGetFreeHeapSize(), xPortGetMinimumEverFreeHeapSize()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| ******************************************************************************** | ||
| * @file Profiler.hpp | ||
| * @author Christy | ||
| * @date Jan 28, 2026 | ||
| * @brief | ||
| ******************************************************************************** | ||
| */ | ||
|
|
||
| #ifndef PROFILER_TASK_HPP_ | ||
| #define PROFILER_TASK_HPP_ | ||
| extern bool profileSystem; | ||
| /************************************ | ||
| * INCLUDES | ||
| ************************************/ | ||
| #include "Task.hpp" | ||
| #include "SystemDefines.hpp" | ||
| #include <vector> | ||
|
|
||
| /************************************ | ||
| * MACROS AND DEFINES | ||
| ************************************/ | ||
| extern bool profileSystem; // profiling status bool | ||
|
|
||
| // struct to store task data | ||
| struct TaskProfile { | ||
| char name[configMAX_TASK_NAME_LEN]; | ||
| char state[10]; | ||
| int priority; | ||
| int stackRemaining; | ||
| char cpuPercent[8]; | ||
| }; | ||
| /************************************ | ||
| * TYPEDEFS | ||
| ************************************/ | ||
|
|
||
| /************************************ | ||
| * CLASS DEFINITIONS | ||
| ************************************/ | ||
| class ProfilerTask : public Task { | ||
| public: | ||
| static ProfilerTask& Inst() { | ||
| static ProfilerTask inst; | ||
| return inst; | ||
| } | ||
|
|
||
| void InitTask(); | ||
| void ProfileSystem(); // public handle to run profiling | ||
|
|
||
| protected: | ||
| // Main run code | ||
| void Run(void* pvParams); | ||
|
|
||
| // Static Task Interface, passes control to the instance Run(); | ||
| static void RunTask(void* pvParams) { | ||
| ProfilerTask::Inst().Run(pvParams); | ||
| } | ||
|
|
||
| // profiling methods | ||
| void CollectTaskList(std::vector<TaskProfile>& Profiles); | ||
| void CollectCPUStats(std::vector<TaskProfile>& Profiles); | ||
| void DisplayTable(std::vector<TaskProfile>& Profiles); | ||
|
|
||
| private: | ||
| ProfilerTask(); // Private constructor | ||
| ProfilerTask(const ProfilerTask&); // Prevent copy-construction | ||
| ProfilerTask& operator=(const ProfilerTask&); // Prevent assignment | ||
|
|
||
| }; | ||
| /************************************ | ||
| * FUNCTION DECLARATIONS | ||
| ************************************/ | ||
|
|
||
| #endif /* PROFILER_TASK_HPP_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is top for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"top" is the UART command to start profiling, "stoptop" is the UART command to stop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to ask what it stands for? Why not just profile? and stop profiling?