Skip to content

Commit

Permalink
Add cpu load (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
onkwon authored Jan 14, 2024
1 parent ccc58d4 commit 67713c1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/memsize.baseline
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
text data bss dec hex filename
181721 51664 371173 604558 9398e build/template.elf
182109 51744 371125 604978 93b32 build/template.elf
2 changes: 1 addition & 1 deletion external/libmcu
8 changes: 5 additions & 3 deletions ports/esp-idf/build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ add_executable(${PROJECT_EXECUTABLE}
${LIBMCU_ROOT}/ports/posix/button.c
)

idf_build_set_property(COMPILE_DEFINITIONS -DtraceTASK_SWITCHED_IN=on_task_switch_in APPEND)
idf_build_set_property(COMPILE_DEFINITIONS -DxPortIsInsideInterrupt=xPortInIsrContext APPEND)
idf_build_set_property(C_COMPILE_OPTIONS "-Wno-implicit-function-declaration" APPEND)

target_compile_definitions(${PROJECT_EXECUTABLE}
PRIVATE
${APP_DEFS}

ESP_PLATFORM=1
xPortIsInsideInterrupt=xPortInIsrContext
ESP_PLATFORM
)
target_include_directories(${PROJECT_EXECUTABLE}
PRIVATE
Expand Down
69 changes: 69 additions & 0 deletions ports/esp-idf/cpuload.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* SPDX-FileCopyrightText: 2024 Kyunghwan Kwon <k@libmcu.org>
*
* SPDX-License-Identifier: MIT
*/

#include "libmcu/board.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#define CPULOAD_CALC_INTERVAL_MS 1000

static struct cpuload {
uint32_t idle_time_elapsed;
uint32_t running_time_elapsed;
TaskHandle_t prev_task;
uint8_t cpuload;
} cores[SOC_CPU_CORES_NUM];

void on_task_switch_in(void)
{
static uint32_t t0;
static uint32_t sum_elapsed;

uint32_t t1 = board_get_time_since_boot_ms();
uint32_t elapsed = t1 - t0;

/* NOTE: count at least 1 even if the task has run for much shorter time
* as millisecond unit timer used here. For fine granularity, introduce
* high-resolution timer. */
if (elapsed == 0) {
elapsed = 1;
}

TaskHandle_t current = xTaskGetCurrentTaskHandle();
struct cpuload *core = &cores[xPortGetCoreID()];

if (current == xTaskGetIdleTaskHandle()) {
if (current == core->prev_task) { /* idle to idle */
core->idle_time_elapsed += elapsed;
} else { /* active to idle */
core->running_time_elapsed += elapsed;
}
} else {
if (current == core->prev_task) { /* active to active */
core->running_time_elapsed += elapsed;
} else { /* idle to active */
core->idle_time_elapsed += elapsed;
}
}

sum_elapsed += elapsed;
core->cpuload = (uint8_t)(core->running_time_elapsed * 100 /
(core->running_time_elapsed + core->idle_time_elapsed));

if (sum_elapsed >= CPULOAD_CALC_INTERVAL_MS) {
core->running_time_elapsed = core->idle_time_elapsed = 0;
sum_elapsed = 0;
}

t0 = t1;
core->prev_task = current;
}

uint8_t board_cpuload(int core_id)
{
return cores[core_id].cpuload;
}

0 comments on commit 67713c1

Please sign in to comment.