@ -22,33 +22,49 @@ static uint32_t enter_ts;
@@ -22,33 +22,49 @@ static uint32_t enter_ts;
static uint32_t cyc_start ;
static uint32_t ticks_idle ;
static struct k_work_delayable cpu_load_log ;
static cpu_load_cb_t load_cb ;
static uint8_t cpu_load_threshold_percent ;
static void cpu_load_log_fn ( struct k_timer * dummy )
{
int load = cpu_load_get ( true ) ;
uint32_t percent = load / 10 ;
uint32_t fraction = load % 10 ;
LOG_INF ( " Load:%d.%03d%% " , percent , fraction ) ;
if ( load_cb ! = NULL & & percent > = cpu_load_threshold_percent ) {
load_cb ( percent ) ;
}
}
K_TIMER_DEFINE ( cpu_load_timer , cpu_load_log_fn , NULL ) ;
void cpu_load_log_control ( bool enable )
{
if ( CONFIG_CPU_LOAD_LOG_PERIODICALLY = = 0 ) {
return ;
}
if ( enable ) {
( void ) cpu_load_get ( true ) ;
k_work_schedule ( & cpu_load_log , K_MSEC ( CONFIG_CPU_LOAD_LOG_PERIODICALLY ) ) ;
k_timer_start ( & cpu_load_timer , K_MSEC ( CONFIG_CPU_LOAD_LOG_PERIODICALLY ) ,
K_MSEC ( CONFIG_CPU_LOAD_LOG_PERIODICALLY ) ) ;
} else {
k_work_cancel_delayable ( & cpu_load_log ) ;
k_timer_stop ( & cpu_load_timer ) ;
}
}
# if CONFIG_CPU_LOAD_USE_COUNTER || CONFIG_CPU_LOAD_LOG_PERIODICALLY
static void cpu_load_log_fn ( struct k_work * work )
int cpu_load_cb_reg ( cpu_load_cb_t cb , uint8_t threshold_percent )
{
int load = cpu_load_get ( true ) ;
uint32_t percent = load / 10 ;
uint32_t fraction = load % 10 ;
if ( threshold_percent > 100 ) {
return - EINVAL ;
}
LOG_INF ( " Load:%d.%03d%% " , percent , fraction ) ;
cpu_load_log_control ( true ) ;
cpu_load_threshold_percent = threshold_percent ;
load_cb = cb ;
return 0 ;
}
# if CONFIG_CPU_LOAD_USE_COUNTER || CONFIG_CPU_LOAD_LOG_PERIODICALLY
static int cpu_load_init ( void )
{
if ( IS_ENABLED ( CONFIG_CPU_LOAD_USE_COUNTER ) ) {
@ -59,8 +75,8 @@ static int cpu_load_init(void)
@@ -59,8 +75,8 @@ static int cpu_load_init(void)
}
if ( CONFIG_CPU_LOAD_LOG_PERIODICALLY > 0 ) {
k_work_init_delayable ( & cpu_load_log , cpu_load_log_fn ) ;
return k_work_schedule ( & cpu_load_log , K_MSEC ( CONFIG_CPU_LOAD_LOG_PERIODICALLY ) ) ;
k_timer_start ( & cpu_load_timer , K_MSEC ( CONFIG_CPU_LOAD_LOG_PERIODICALLY ) ,
K_MSEC ( CONFIG_CPU_LOAD_LOG_PERIODICALLY ) ) ;
}
return 0 ;