0
0
FreeRTOSprogramming~10 mins

vTaskGetRunTimeStats() for CPU usage in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - vTaskGetRunTimeStats() for CPU usage
Start Scheduler
Tasks run and consume CPU
Call vTaskGetRunTimeStats()
Collect run time counters for each task
Calculate CPU usage % per task
Return formatted string with stats
Display or log CPU usage
This flow shows how FreeRTOS collects CPU usage data by tracking task run times and then formats it into a readable string.
Execution Sample
FreeRTOS
char buffer[512];
vTaskGetRunTimeStats(buffer);
printf("%s", buffer);
This code collects CPU usage stats of all tasks and prints them as a formatted string.
Execution Table
StepActionInternal OperationResult
1Start SchedulerTasks begin running, run time counters start incrementingTasks run, counters increment
2Call vTaskGetRunTimeStats(buffer)Function reads run time counters for each taskCounters collected
3Calculate CPU usage %For each task: (task run time / total run time) * 100CPU usage % computed
4Format stats stringCreate human-readable table with task names and CPU %Formatted string stored in buffer
5Print bufferOutput the CPU usage stats to console or logCPU usage stats displayed
6EndFunction returns after filling bufferExecution complete
💡 Function ends after filling the buffer with CPU usage stats for all tasks
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
buffer"" (empty)"Counters collected string""CPU % calculated string""Formatted stats string""Ready to print string"
taskRunTimeCounter0Incremented by task run timeUsed for CPU % calculationUsed for formattingUsed for display
totalRunTime0Sum of all task run timesUsed for CPU % calculationUsed for formattingUsed for display
Key Moments - 3 Insights
Why does the CPU usage percentage not add up exactly to 100%?
Because of rounding during percentage calculation and possible time spent in the idle task or interrupts, the sum may be slightly less or more than 100%. See execution_table step 3.
What happens if the buffer passed to vTaskGetRunTimeStats() is too small?
The function may truncate the output string, so some task stats might be missing. Always provide a sufficiently large buffer as shown in execution_table step 4.
Does vTaskGetRunTimeStats() affect task scheduling or performance?
No, it only reads counters and formats a string. It does not block or change task priorities. This is shown in execution_table steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is calculated for each task?
AThe CPU usage percentage based on run time counters
BThe task priority level
CThe stack size used by the task
DThe number of times the task was switched
💡 Hint
Refer to execution_table row with Step 3 describing CPU usage % calculation
According to variable_tracker, what does the 'buffer' variable contain after step 4?
AEmpty string
BFormatted stats string ready to print
CRaw run time counters
DTask handles
💡 Hint
Check variable_tracker row for 'buffer' after Step 4
If the buffer size is too small, what is the likely effect on the output?
AFunction will crash
BAll task stats will be shown correctly
COutput string may be truncated, missing some tasks
DCPU usage percentages will be incorrect
💡 Hint
See key_moments explanation about buffer size and truncation
Concept Snapshot
vTaskGetRunTimeStats(buffer) collects CPU run time counters for all tasks.
It calculates CPU usage % = (task run time / total run time) * 100.
Results are formatted into a string stored in buffer.
Call after scheduler start to get current CPU usage.
Ensure buffer is large enough to hold all stats.
Useful for monitoring task CPU consumption in FreeRTOS.
Full Transcript
This visual execution trace shows how FreeRTOS's vTaskGetRunTimeStats() function works to measure CPU usage per task. First, the scheduler starts and tasks run, incrementing their run time counters. When vTaskGetRunTimeStats() is called, it reads these counters, sums total run time, and calculates each task's CPU usage percentage. Then it formats this data into a readable string stored in the provided buffer. Finally, the string can be printed or logged to show CPU usage stats. Variables like buffer, taskRunTimeCounter, and totalRunTime change step-by-step as the function executes. Common beginner questions include why percentages may not add to 100%, the importance of buffer size, and that this function does not affect scheduling. The quizzes test understanding of CPU usage calculation, buffer content, and effects of buffer size. This helps learners see exactly how CPU usage stats are gathered and displayed in FreeRTOS.