Using vTaskGetRunTimeStats() to Monitor CPU Usage in FreeRTOS
📖 Scenario: You are working on a FreeRTOS embedded system project. You want to monitor how much CPU time each task is using to optimize your system performance.
🎯 Goal: Build a simple FreeRTOS program that creates two tasks and uses vTaskGetRunTimeStats() to display CPU usage statistics of these tasks.
📋 What You'll Learn
Create two FreeRTOS tasks named Task1 and Task2
Create a buffer string called runtimeStatsBuffer with size 256
Use vTaskGetRunTimeStats(runtimeStatsBuffer) to collect CPU usage stats
Print the CPU usage stats using printf
💡 Why This Matters
🌍 Real World
Monitoring CPU usage helps embedded developers optimize task scheduling and system responsiveness.
💼 Career
Understanding FreeRTOS task monitoring is essential for embedded software engineers working on real-time systems.
Progress0 / 4 steps
1
Create two FreeRTOS tasks
Create two tasks named Task1 and Task2 using xTaskCreate(). Each task should run an empty infinite loop with a small delay using vTaskDelay(pdMS_TO_TICKS(100)).
FreeRTOS
Hint
Use xTaskCreate() to create tasks. Each task runs an infinite loop with vTaskDelay(pdMS_TO_TICKS(100)) to yield CPU.
2
Create a buffer for runtime stats
Create a character array called runtimeStatsBuffer with size 256 to hold the CPU usage statistics string.
FreeRTOS
Hint
Declare char runtimeStatsBuffer[256]; outside main() or inside before usage.
3
Collect CPU usage stats with vTaskGetRunTimeStats()
Inside main(), after starting the scheduler, call vTaskGetRunTimeStats(runtimeStatsBuffer) to fill the buffer with CPU usage statistics.
FreeRTOS
Hint
Call vTaskGetRunTimeStats(runtimeStatsBuffer); after vTaskStartScheduler();.
4
Print the CPU usage statistics
Add a printf statement to print the contents of runtimeStatsBuffer so you can see the CPU usage of each task.
FreeRTOS
Hint
Use printf("%s", runtimeStatsBuffer); to display the CPU usage stats.
Practice
(1/5)
1. What is the primary purpose of the vTaskGetRunTimeStats() function in FreeRTOS?
easy
A. To suspend all running tasks temporarily
B. To create a new task with specific priority
C. To delete a task from the scheduler
D. To display the CPU usage time of each task
Solution
Step 1: Understand the function's role
vTaskGetRunTimeStats() is designed to collect and display CPU usage statistics for each task in FreeRTOS.
Step 2: Compare options with function purpose
Options A, B, and C relate to task management but not CPU usage reporting, which is the key role of vTaskGetRunTimeStats().
Final Answer:
To display the CPU usage time of each task -> Option D
Quick Check:
Function purpose = CPU usage display [OK]
Hint: Remember: vTaskGetRunTimeStats shows CPU time per task [OK]
Common Mistakes:
Confusing it with task creation or deletion functions
Thinking it suspends tasks instead of reporting stats
Assuming it modifies task priorities
2. Which of the following is the correct way to call vTaskGetRunTimeStats() to store the stats in a buffer named buffer?
easy
A. vTaskGetRunTimeStats(buffer);
B. vTaskGetRunTimeStats(&buffer);
C. vTaskGetRunTimeStats(buffer[]);
D. vTaskGetRunTimeStats(*buffer);
Solution
Step 1: Check function parameter type
vTaskGetRunTimeStats() expects a pointer to a char array (string buffer), so passing the buffer name directly is correct.
Step 2: Analyze each option's syntax
vTaskGetRunTimeStats(buffer); passes the buffer correctly. vTaskGetRunTimeStats(&buffer); passes address of buffer pointer (wrong). vTaskGetRunTimeStats(buffer[]); uses invalid syntax. vTaskGetRunTimeStats(*buffer); dereferences buffer incorrectly.
Final Answer:
vTaskGetRunTimeStats(buffer); -> Option A
Quick Check:
Pass buffer name directly [OK]
Hint: Pass buffer name directly, no & or * needed [OK]
Common Mistakes:
Using &buffer which is wrong for arrays
Adding [] inside function call
Dereferencing buffer pointer incorrectly
3. Given the following code snippet, what will be the output stored in statsBuffer?
Assuming two tasks "TaskA" and "TaskB" run and TaskA used 70% CPU, TaskB used 30%.
medium
A. TaskA 7
TaskB 3
B. TaskA 70%
TaskB 30%
C. TaskA 7000
TaskB 3000
D. TaskA 700
TaskB 300
Solution
Step 1: Understand output format of vTaskGetRunTimeStats()
The function outputs CPU usage as run time counter ticks, not percentages. If TaskA used 70% CPU, it might show 7000 ticks and TaskB 3000 ticks (assuming 10000 total ticks).
Step 2: Match options with expected output
TaskA 7000
TaskB 3000
shows usage in ticks (7000 and 3000), matching expected output. Options B, C, and D show percentages or smaller numbers, which are not the direct output format.
Final Answer:
TaskA 7000
TaskB 3000
-> Option C
Quick Check:
Output shows ticks, not percentages [OK]
Hint: Output shows ticks, not percent values [OK]
Common Mistakes:
Assuming output shows percentages directly
Confusing ticks with smaller numbers
Expecting formatted percentages in output
4. You called vTaskGetRunTimeStats(statsBuffer); but the output is empty. What is the most likely cause?
medium
A. The function requires a NULL pointer as argument
B. Run time stats collection is not enabled in FreeRTOSConfig.h
C. The buffer size is too large
D. The tasks have zero priority
Solution
Step 1: Identify prerequisites for run time stats
Run time stats must be enabled in FreeRTOSConfig.h (configGENERATE_RUN_TIME_STATS set to 1) for vTaskGetRunTimeStats() to work.
Step 2: Analyze other options
Buffer size being too large does not cause empty output. Passing NULL is invalid. Task priority does not affect stats collection directly.
Final Answer:
Run time stats collection is not enabled in FreeRTOSConfig.h -> Option B
Quick Check:
Enable run time stats in config [OK]
Hint: Check configGENERATE_RUN_TIME_STATS is enabled [OK]
Common Mistakes:
Assuming buffer size too large causes empty output
Passing NULL pointer mistakenly
Thinking task priority disables stats
5. You want to monitor CPU usage of tasks over time using vTaskGetRunTimeStats(). Which approach correctly ensures accurate and meaningful stats?
hard
A. Enable run time stats, provide a large buffer, call periodically, and reset counters between calls
B. Call vTaskGetRunTimeStats() once at startup with a small buffer
C. Disable run time stats and manually count task switches
D. Call vTaskGetRunTimeStats() inside each task to get its own CPU usage
Solution
Step 1: Understand requirements for accurate CPU stats
Run time stats must be enabled, buffer must be large enough to hold all task info, and stats should be collected periodically to track changes over time.
Step 2: Evaluate each option
Enable run time stats, provide a large buffer, call periodically, and reset counters between calls describes the correct approach including resetting counters to avoid cumulative overflow. Call vTaskGetRunTimeStats() once at startup with a small buffer is insufficient (only once, small buffer). Disable run time stats and manually count task switches disables stats, losing automation. Call vTaskGetRunTimeStats() inside each task to get its own CPU usage is incorrect because stats are global, not per task.
Final Answer:
Enable run time stats, provide a large buffer, call periodically, and reset counters between calls -> Option A