From 73d0b93829de0432902d264cd907ddf6f0d70cb1 Mon Sep 17 00:00:00 2001 From: Alessio Morale Date: Sat, 22 Feb 2014 00:29:02 +0100 Subject: [PATCH] OP-1026 Add a task monitor api to calculate idle percentage --- flight/pios/common/pios_task_monitor.c | 32 ++++++++++++++++++++++++-- flight/pios/inc/pios_task_monitor.h | 6 +++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/flight/pios/common/pios_task_monitor.c b/flight/pios/common/pios_task_monitor.c index 7a3c13234..e4a1aceb5 100644 --- a/flight/pios/common/pios_task_monitor.c +++ b/flight/pios/common/pios_task_monitor.c @@ -33,6 +33,7 @@ static xSemaphoreHandle mLock; static xTaskHandle *mTaskHandles; static uint32_t mLastMonitorTime; +static uint32_t mLastIdleMonitorTime; static uint16_t mMaxTasks; /** @@ -53,9 +54,11 @@ int32_t PIOS_TASK_MONITOR_Initialize(uint16_t max_tasks) mMaxTasks = max_tasks; #if (configGENERATE_RUN_TIME_STATS == 1) - mLastMonitorTime = portGET_RUN_TIME_COUNTER_VALUE(); + mLastMonitorTime = portGET_RUN_TIME_COUNTER_VALUE(); + mLastIdleMonitorTime = portGET_RUN_TIME_COUNTER_VALUE(); #else - mLastMonitorTime = 0; + mLastMonitorTime = 0; + mLastIdleMonitorTime = 0; #endif return 0; } @@ -146,4 +149,29 @@ void PIOS_TASK_MONITOR_ForEachTask(TaskMonitorTaskInfoCallback callback, void *c xSemaphoreGiveRecursive(mLock); } +uint8_t PIOS_TASK_MONITOR_GetIdlePercentage() +{ +#if defined(ARCH_POSIX) || defined(ARCH_WIN32) + return 50; + +#elif (configGENERATE_RUN_TIME_STATS == 1) + xSemaphoreTakeRecursive(mLock, portMAX_DELAY); + + uint32_t currentTime = portGET_RUN_TIME_COUNTER_VALUE(); + + /* avoid divide-by-zero if the interval is too small */ + uint32_t deltaTime = ((currentTime - mLastIdleMonitorTime) / 100) ? : 1; + mLastIdleMonitorTime = currentTime; + uint8_t running_time_percentage = 0; + + /* Generate idle time percentage stats */ + running_time_percentage = uxTaskGetRunTime(xTaskGetIdleTaskHandle()) / deltaTime; + xSemaphoreGiveRecursive(mLock); + return running_time_percentage; + +#endif + return 0; +} + + #endif // PIOS_INCLUDE_TASK_MONITOR diff --git a/flight/pios/inc/pios_task_monitor.h b/flight/pios/inc/pios_task_monitor.h index ed3d1d519..b888cf637 100644 --- a/flight/pios/inc/pios_task_monitor.h +++ b/flight/pios/inc/pios_task_monitor.h @@ -104,4 +104,10 @@ typedef void (*TaskMonitorTaskInfoCallback)(uint16_t task_id, const struct pios_ */ extern void PIOS_TASK_MONITOR_ForEachTask(TaskMonitorTaskInfoCallback callback, void *context); +/** + * Return the idle task running time percentage. + */ + +extern uint8_t PIOS_TASK_MONITOR_GetIdlePercentage(); + #endif // PIOS_TASK_MONITOR_H