improved stability of cpu usage calculation

This commit is contained in:
Bernd Herzog 2023-04-24 13:00:34 +02:00
parent 29b7a5ee56
commit 948d8d947e
6 changed files with 102 additions and 26 deletions

View File

@ -155,6 +155,7 @@ set(CPPSRC
${COMMON}/ui_widget.cpp ${COMMON}/ui_widget.cpp
${COMMON}/utility.cpp ${COMMON}/utility.cpp
${COMMON}/wm8731.cpp ${COMMON}/wm8731.cpp
${COMMON}/performance_counter.cpp
app_settings.cpp app_settings.cpp
audio.cpp audio.cpp
baseband_api.cpp baseband_api.cpp

View File

@ -21,6 +21,7 @@
#include "ui_dfu_menu.hpp" #include "ui_dfu_menu.hpp"
#include "portapack_shared_memory.hpp" #include "portapack_shared_memory.hpp"
#include "performance_counter.hpp"
namespace ui { namespace ui {
@ -39,21 +40,11 @@ DfuMenu::DfuMenu(NavigationView& nav) : nav_ (nav) {
} }
void DfuMenu::paint(Painter& painter) { void DfuMenu::paint(Painter& painter) {
auto now = chTimeNow(); auto utilisation = get_cpu_utilisation_in_percent();
auto idle_ticks = chThdGetTicks(chSysGetIdleThread());
static systime_t last_time;
static systime_t last_last_time;
auto time_elapsed = now - last_time;
auto idle_elapsed = idle_ticks - last_last_time;
last_time = now;
last_last_time = idle_ticks;
text_info_line_1.set(to_string_dec_uint(chCoreStatus(), 6)); text_info_line_1.set(to_string_dec_uint(chCoreStatus(), 6));
text_info_line_2.set(to_string_dec_uint((uint32_t)get_free_stack_space(), 6)); text_info_line_2.set(to_string_dec_uint((uint32_t)get_free_stack_space(), 6));
text_info_line_3.set(to_string_dec_uint((time_elapsed - idle_elapsed) / 10, 6)); text_info_line_3.set(to_string_dec_uint(utilisation, 6));
text_info_line_4.set(to_string_dec_uint(shared_memory.m4_heap_usage, 6)); text_info_line_4.set(to_string_dec_uint(shared_memory.m4_heap_usage, 6));
text_info_line_5.set(to_string_dec_uint(shared_memory.m4_stack_usage, 6)); text_info_line_5.set(to_string_dec_uint(shared_memory.m4_stack_usage, 6));
text_info_line_6.set(to_string_dec_uint(shared_memory.m4_cpu_usage, 6)); text_info_line_6.set(to_string_dec_uint(shared_memory.m4_cpu_usage, 6));

View File

@ -139,6 +139,7 @@ set(CPPSRC
${COMMON}/chibios_cpp.cpp ${COMMON}/chibios_cpp.cpp
debug.cpp debug.cpp
${COMMON}/gcc.cpp ${COMMON}/gcc.cpp
${COMMON}/performance_counter.cpp
tone_gen.cpp tone_gen.cpp
) )

View File

@ -26,6 +26,7 @@
#include <hal.h> #include <hal.h>
#include "portapack_shared_memory.hpp" #include "portapack_shared_memory.hpp"
#include "performance_counter.hpp"
void write_m4_panic_msg(const char *panic_message, struct extctx *ctxp) { void write_m4_panic_msg(const char *panic_message, struct extctx *ctxp) {
if (ctxp == nullptr) { if (ctxp == nullptr) {
@ -120,29 +121,25 @@ void update_performance_counters() {
auto performance_counter_active = shared_memory.request_m4_performance_counter; auto performance_counter_active = shared_memory.request_m4_performance_counter;
if (performance_counter_active == 0x00) if (performance_counter_active == 0x00)
return; return;
static bool last_paint_state = false; static bool last_paint_state = false;
if ((((chTimeNow()>>10) & 0x01) == 0x01) == last_paint_state) if ((((chTimeNow()>>10) & 0x01) == 0x01) == last_paint_state)
return; return;
volatile auto idle_thread = chSysGetIdleThread();
if (chThdGetTicks(idle_thread) > 0x10000000) {
return;
}
idle_thread = nullptr;
last_paint_state = !last_paint_state; last_paint_state = !last_paint_state;
auto now = chTimeNow(); auto utilisation = get_cpu_utilisation_in_percent();
auto idle_ticks = chThdGetTicks(chSysGetIdleThread());
static systime_t last_time;
static systime_t last_last_time;
auto time_elapsed = now - last_time;
auto idle_elapsed = idle_ticks - last_last_time;
last_time = now;
last_last_time = idle_ticks;
auto cpu_usage = (time_elapsed - idle_elapsed) / 10;
auto free_stack = (uint32_t)get_free_stack_space(); auto free_stack = (uint32_t)get_free_stack_space();
auto free_heap = chCoreStatus(); auto free_heap = chCoreStatus();
shared_memory.m4_cpu_usage = cpu_usage; shared_memory.m4_cpu_usage = utilisation;
shared_memory.m4_stack_usage = free_stack; shared_memory.m4_stack_usage = free_stack;
shared_memory.m4_heap_usage = free_heap; shared_memory.m4_heap_usage = free_heap;
} }

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2023 Bernd Herzog
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "performance_counter.hpp"
#include "ch.h"
uint8_t get_cpu_utilisation_in_percent() {
static systime_t last_time = 0;
static systime_t last_idle_ticks = 0;
auto now = chTimeNow();
auto idle_ticks = chThdGetTicks(chSysGetIdleThread());
if (last_time == 0) {
last_time = now;
last_idle_ticks = idle_ticks;
return 0;
}
int32_t time_elapsed = now - last_time;
int32_t idle_elapsed = idle_ticks - last_idle_ticks;
int32_t working_ticks = time_elapsed - idle_elapsed;
if (working_ticks < 0)
working_ticks = 0;
auto utilisation = working_ticks * 100 / time_elapsed;
last_time = now;
last_idle_ticks = idle_ticks;
if (utilisation > 100) {
return 100;
}
return (uint8_t) utilisation;
}

View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2023 Bernd Herzog
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef __PERFORMANCE_COUNTER_H__
#define __PERFORMANCE_COUNTER_H__
#include <stdint.h>
uint8_t get_cpu_utilisation_in_percent();
#endif /* __PERFORMANCE_COUNTER_H__ */