ST做的一个FreeRTOS的CPU利用率统计方法
仅需下面两个文件即可:使用方法:1- in the _OS_Config.h file (ex. FreeRTOSConfig.h) enable the following macros :
- #define configUSE_IDLE_HOOK 1
- #define configUSE_TICK_HOOK 1
2- in the _OS_Config.h define the following macros :
- #define traceTASK_SWITCHED_IN()extern void StartIdleMonitor(void); \
StartIdleMonitor()
- #define traceTASK_SWITCHED_OUT() extern void EndIdleMonitor(void); \
EndIdleMonitor()
代码:
cpu_utils.c
/**
******************************************************************************
* @file cpu_utils.c
* @authorMCD Application Team
* @version V1.1.0
* @date 20-November-2014
* @brief Utilities for CPU Load calculation
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/********************** NOTES **********************************************
To use this module, the following steps should be followed :
1- in the _OS_Config.h file (ex. FreeRTOSConfig.h) enable the following macros :
- #define configUSE_IDLE_HOOK 1
- #define configUSE_TICK_HOOK 1
2- in the _OS_Config.h define the following macros :
- #define traceTASK_SWITCHED_IN()extern void StartIdleMonitor(void); \
StartIdleMonitor()
- #define traceTASK_SWITCHED_OUT() extern void EndIdleMonitor(void); \
EndIdleMonitor()
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "cpu_utils.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
xTaskHandle xIdleHandle = NULL;
__IO uint32_tosCPU_Usage = 0;
uint32_t osCPU_IdleStartTime = 0;
uint32_t osCPU_IdleSpentTime = 0;
uint32_t osCPU_TotalIdleTime = 0;
/* Private functions ---------------------------------------------------------*/
/**
* @briefApplication Idle Hook
* @paramNone
* @retval None
*/
void vApplicationIdleHook(void)
{
if( xIdleHandle == NULL )
{
/* Store the handle to the idle task. */
xIdleHandle = xTaskGetCurrentTaskHandle();
}
}
/**
* @briefApplication Idle Hook
* @paramNone
* @retval None
*/
void vApplicationTickHook (void)
{
static int tick = 0;
if(tick ++ > CALCULATION_PERIOD)
{
tick = 0;
if(osCPU_TotalIdleTime > 1000)
{
osCPU_TotalIdleTime = 1000;
}
osCPU_Usage = (100 - (osCPU_TotalIdleTime * 100) / CALCULATION_PERIOD);
osCPU_TotalIdleTime = 0;
}
}
/**
* @briefStart Idle monitor
* @paramNone
* @retval None
*/
void StartIdleMonitor (void)
{
if( xTaskGetCurrentTaskHandle() == xIdleHandle )
{
osCPU_IdleStartTime = xTaskGetTickCountFromISR();
}
}
/**
* @briefStop Idle monitor
* @paramNone
* @retval None
*/
void EndIdleMonitor (void)
{
if( xTaskGetCurrentTaskHandle() == xIdleHandle )
{
/* Store the handle to the idle task. */
osCPU_IdleSpentTime = xTaskGetTickCountFromISR() - osCPU_IdleStartTime;
osCPU_TotalIdleTime += osCPU_IdleSpentTime;
}
}
/**
* @briefStop Idle monitor
* @paramNone
* @retval None
*/
uint16_t osGetCPUUsage (void)
{
return (uint16_t)osCPU_Usage;
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
实现起来还是很简单的。 不错,正是我需要的。 改成rtx能用的 请问一下,这函数osGetCPUUsage()是不是在每个任务上调用,打印出来显示CPU利用率 0520kuang 发表于 2018-1-26 14:25
请问一下,这函数osGetCPUUsage()是不是在每个任务上调用,打印出来显示CPU利用率
不用,任意一个任务都可以调用,获取的结果就是CPU利用率。 不受FreeRTOS管理的中断是不是测量不到? eemwin 发表于 2019-10-8 17:41
不受FreeRTOS管理的中断是不是测量不到?
他这个统计的是1000个滴答时钟周期内,空闲任务执行的滴答周期个数,所以都统计了。 eric2013 发表于 2019-10-9 00:25
他这个统计的是1000个滴答时钟周期内,空闲任务执行的滴答周期个数,所以都统计了。
如果空闲任务中发生中断,那这个中断时间会被统计 认为是空闲任务吧,所以不受FreeRTOS管理的中断应该是测不到 yuche 发表于 2020-11-26 16:56
如果空闲任务中发生中断,那这个中断时间会被统计 认为是空闲任务吧,所以不受FreeRTOS管理的中断应该是 ...
这么理解的话,是的。
这种情况下,受不受FreeRTOS管理,没有进行任务切换,都无法正常统计,都会被统计到空闲里。 eric2013 发表于 2020-11-27 01:59
这么理解的话,是的。
这种情况下,受不受FreeRTOS管理,没有进行任务切换,都无法正常统计,都会被统 ...
都是几年前的帖子了,现在统一了cmosis了,应该有统一的函数了吧? caicaptain2 发表于 2020-12-1 14:38
都是几年前的帖子了,现在统一了cmosis了,应该有统一的函数了吧?
CMSIS-RTOS封装了下,这个机制还是没变。
添加进工程CPU利用率显示波动很大
页:
[1]