Skip to content

Commit de9b405

Browse files
committed
feature(tinyusb): Scaled calculation for CPU Load percentage
1 parent 8cf8bf8 commit de9b405

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

device/esp_tinyusb/test_apps/runtime_config/main/test_cpu_load.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <stdio.h>
1212
#include <string.h>
13+
#include <inttypes.h>
1314
#include "freertos/FreeRTOS.h"
1415
#include "freertos/task.h"
1516
#include "esp_log.h"
@@ -20,7 +21,9 @@
2021
#include "device_handling.h"
2122

2223
// Enable this, if you need to see the stat for all tasks
23-
#define STAT_CONFIG_ALL_TASKS 0
24+
#define STAT_CONFIG_ALL_TASKS 1
25+
// Enable this, if you need precise percentage calculation
26+
#define STAT_CONFIG_PRECISE_PERCENTAGE 1
2427
// Increase this if print_real_time_stats returns ESP_ERR_INVALID_SIZE
2528
#define STAT_CONFIG_ARRAY_SIZE_OFFSET 5
2629

@@ -37,6 +40,29 @@ static UBaseType_t end_array_size;
3740
static configRUN_TIME_COUNTER_TYPE start_run_time;
3841
static configRUN_TIME_COUNTER_TYPE end_run_time;
3942

43+
#if (STAT_CONFIG_PRECISE_PERCENTAGE)
44+
static inline void print_task_precise_percentage(const char *name, uint32_t counter_delta, uint32_t elapsed_delta)
45+
{
46+
const uint32_t cores = CONFIG_FREERTOS_NUMBER_OF_CORES;
47+
uint64_t d = (uint64_t)elapsed_delta * (uint64_t)cores;
48+
uint64_t s_bp = (uint64_t)counter_delta * 10000ULL; // Scaled basis points
49+
uint32_t bp = 0; // Basis points
50+
51+
if (d != 0) {
52+
bp = (uint32_t)(s_bp / d);
53+
if (bp > 10000U) {
54+
bp = 10000U; // Round noise
55+
}
56+
}
57+
58+
printf("%-20.20s %10" PRIu32 " %3" PRIu32 ".%02" PRIu32 "%%\n",
59+
name, // left-aligned, max 20 chars
60+
counter_delta, // right-aligned, width 10
61+
bp / 100, // integer part of percentage
62+
bp % 100); // fractional part of percentage
63+
}
64+
#endif // STAT_CONFIG_PRECISE_PERCENTAGE
65+
4066
/**
4167
* @brief Initialize CPU load measurement
4268
*/
@@ -93,11 +119,18 @@ static void test_cpu_load_measure(void)
93119
if (k >= 0) {
94120
uint32_t task_elapsed_time = end_array[k].ulRunTimeCounter - start_array[i].ulRunTimeCounter;
95121
uint32_t percentage_time = (task_elapsed_time * 100UL) / (total_elapsed_time * CONFIG_FREERTOS_NUMBER_OF_CORES);
122+
96123
#if (STAT_CONFIG_ALL_TASKS)
124+
#if (STAT_CONFIG_PRECISE_PERCENTAGE)
125+
uint32_t task_delta = (uint32_t)((uint64_t)end_array[k].ulRunTimeCounter - (uint64_t)start_array[i].ulRunTimeCounter);
126+
uint32_t total_delta = (uint32_t)((uint64_t)end_run_time - (uint64_t)start_run_time);
127+
print_task_precise_percentage(start_array[i].pcTaskName, task_delta, total_delta);
128+
#else
97129
printf("%-20.20s %10" PRIu32 " %7" PRIu32 "%%\n",
98130
start_array[i].pcTaskName, // left-aligned, max 20 chars
99131
task_elapsed_time, // right-aligned, width 10
100-
percentage_time); // right-aligned, width 7 + '%' char
132+
percentage_time); // right-aligned, width 7
133+
#endif // STAT_CONFIG_PRECISE_PERCENTAGE
101134
#endif // STAT_CONFIG_ALL_TASKS
102135
// Save the TinyUSB task stats for test validation
103136
if (strcmp(start_array[i].pcTaskName, TINYUSB_TASK_NAME) == 0) {

0 commit comments

Comments
 (0)