Bird
Raised Fist0
FreeRTOSprogramming~20 mins

vTaskGetRunTimeStats() for CPU usage in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS CPU Usage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of vTaskGetRunTimeStats() after two tasks run?

Consider two FreeRTOS tasks running with different CPU times. The vTaskGetRunTimeStats() function fills a buffer with CPU usage stats. What will the output string contain?

FreeRTOS
char buffer[100];
vTaskGetRunTimeStats(buffer);
printf("%s", buffer);
ATask1 100%\r\nTask2 0%\r\n
BTask1 30%\r\nTask2 70%\r\n
CTask1 70%\r\nTask2 30%\r\n
DTask1 50%\r\nTask2 50%\r\n
Attempts:
2 left
💡 Hint

Remember CPU usage is proportional to the run time of each task relative to total run time.

🧠 Conceptual
intermediate
1:30remaining
What does vTaskGetRunTimeStats() measure?

Which of the following best describes what vTaskGetRunTimeStats() reports?

AThe total time each task has spent running since system start, as a percentage of total CPU time.
BThe current stack usage of each task in bytes.
CThe number of times each task has been switched in and out by the scheduler.
DThe priority level of each task.
Attempts:
2 left
💡 Hint

Think about what CPU usage means in a multitasking system.

🔧 Debug
advanced
2:00remaining
Why does vTaskGetRunTimeStats() show zero CPU usage for all tasks?

You call vTaskGetRunTimeStats() but the output shows zero CPU usage for every task. What is the most likely cause?

AThe buffer passed to <code>vTaskGetRunTimeStats()</code> is too small.
BThe configGENERATE_RUN_TIME_STATS macro is not enabled or the timer for run time stats is not configured.
CThe tasks have not been created yet.
DThe scheduler is not running.
Attempts:
2 left
💡 Hint

Check your FreeRTOS configuration related to run time stats.

📝 Syntax
advanced
1:30remaining
Which code snippet correctly uses vTaskGetRunTimeStats()?

Choose the code snippet that correctly calls vTaskGetRunTimeStats() and prints the CPU usage stats.

Achar stats[200];\nvTaskGetRunTimeStats(&stats);\nprintf("CPU Usage:\n%s", stats);
Bchar stats[200];\nvTaskGetRunTimeStats();\nprintf("CPU Usage:\n%s", stats);
Cchar *stats;\nvTaskGetRunTimeStats(stats);\nprintf("CPU Usage:\n%s", stats);
Dchar stats[200];\nvTaskGetRunTimeStats(stats);\nprintf("CPU Usage:\n%s", stats);
Attempts:
2 left
💡 Hint

Check the function parameter type and how the buffer is passed.

🚀 Application
expert
3:00remaining
How to use vTaskGetRunTimeStats() to monitor CPU usage in a running system?

You want to monitor CPU usage of tasks periodically in a FreeRTOS system. Which approach correctly uses vTaskGetRunTimeStats() to achieve this?

ACreate a high priority monitoring task that calls <code>vTaskGetRunTimeStats()</code> every second, stores the output, and prints it via UART.
BCall <code>vTaskGetRunTimeStats()</code> once at system start and store the output for later use.
CModify each task to call <code>vTaskGetRunTimeStats()</code> inside their loop to get their own CPU usage.
DUse <code>vTaskGetRunTimeStats()</code> inside an interrupt service routine to get real-time CPU usage.
Attempts:
2 left
💡 Hint

Think about how to safely and periodically collect stats without blocking or causing errors.

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

  1. Step 1: Understand the function's role

    vTaskGetRunTimeStats() is designed to collect and display CPU usage statistics for each task in FreeRTOS.
  2. 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().
  3. Final Answer:

    To display the CPU usage time of each task -> Option D
  4. 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

  1. Step 1: Check function parameter type

    vTaskGetRunTimeStats() expects a pointer to a char array (string buffer), so passing the buffer name directly is correct.
  2. 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.
  3. Final Answer:

    vTaskGetRunTimeStats(buffer); -> Option A
  4. 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?
char statsBuffer[100];
vTaskGetRunTimeStats(statsBuffer);
printf("%s", 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

  1. 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).
  2. 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.
  3. Final Answer:

    TaskA 7000 TaskB 3000 -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Run time stats collection is not enabled in FreeRTOSConfig.h -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Enable run time stats, provide a large buffer, call periodically, and reset counters between calls -> Option A
  4. Quick Check:

    Enable + buffer + periodic calls + reset = accurate stats [OK]
Hint: Enable stats, use big buffer, call regularly, reset counters [OK]
Common Mistakes:
  • Calling once only at startup
  • Disabling run time stats
  • Calling inside each task expecting per-task stats
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard