当前位置:网站首页>Embedded practice -- CPU utilization statistics based on rt1170 FreeRTOS (24)
Embedded practice -- CPU utilization statistics based on rt1170 FreeRTOS (24)
2022-07-26 04:31:00 【Embedded practice】
This paper is mainly through the thinking of transfer , Record my first use NXP MCUXpresso SDK API Conduct BSP Development
This article mainly describes how to RT1170 Under the platform , be based on FreeRTOS Realization CPU Statistics of utilization rate
Reference resources :https://www.fatalerrors.org/a/statistical-analysis-of-cpu-utilization-in-freertos-system.html
1. CPU The principle of usage statistics
First of all 、 Want to achieve CPU Usage statistics , It needs an interrupt with smaller granularity than the system timing interrupt to realize CPU Statistics of utilization rate , This is a prerequisite .
second 、 You need to register in the corresponding operating system , Implement the corresponding interface .
2. CPU Utilization statistics timer interrupt implementation
Use... In this article PIT2 To play this role . stay PIT2_IRQHANDLER Pair of functions ulCpuTraceTick Carry out self addition . This variable will be used later .
#include "fsl_debug_console.h"
#include "board.h"
#include "fsl_pit.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "FreeRTOS.h"
#include "task.h"
#include "string.h"
/*----------------------------------------------* * external variables * *----------------------------------------------*/
/*----------------------------------------------* * external routine prototypes * *----------------------------------------------*/
/*----------------------------------------------* * internal routine prototypes * *----------------------------------------------*/
/*----------------------------------------------* * project-wide global variables * *----------------------------------------------*/
/*----------------------------------------------* * module-wide global variables * *----------------------------------------------*/
const pit_config_t PIT2_config = {
.enableRunInDebug = false
};
volatile unsigned int ulCpuTraceTick = 0;
/*----------------------------------------------* * constants * *----------------------------------------------*/
/*----------------------------------------------* * macros * *----------------------------------------------*/
/* Get source clock for PIT driver */
#define PIT_SOURCE_CLOCK CLOCK_GetRootClockFreq(kCLOCK_Root_Bus)
/* Definitions for BOARD_InitPeripheral_PIT2_Ch_0 functional group */
/* BOARD_InitPeripheral_PIT2_Ch_0 defines for PIT2 */
/* Definition of peripheral ID. */
#define PIT2_PERIPHERAL PIT2
/* Definition of clock source frequency. */
#define PIT2_CLK_FREQ 240000000UL
/* Definition of channel number for channel 0. */
#define PIT2_CHANNEL_0 kPIT_Chnl_0
/* Definition of ticks count for channel 0. */
//#define PIT2_CHANNEL_0_TICKS 2399999U
#define PIT2_CHANNEL_0_TICKS 1199999U
/* PIT2 interrupt vector ID (number). */
#define PIT2_IRQN PIT2_IRQn
/* PIT2 interrupt handler identifier. */
#define PIT2_IRQHANDLER PIT2_IRQHandler
/*----------------------------------------------* * routines' implementations * *----------------------------------------------*/
/* PIT2_IRQn interrupt handler */
void PIT2_IRQHANDLER(void) {
/* Place your code here */
/* Clear interrupt flag.*/
PIT_ClearStatusFlags(PIT2_PERIPHERAL, PIT2_CHANNEL_0, kPIT_TimerFlag);
ulCpuTraceTick++;
/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt. */
#if defined __CORTEX_M && (__CORTEX_M == 4U)
__DSB();
#endif
}
void PIT2_init(void) {
/* Initialize the PIT. */
PIT_Init(PIT2_PERIPHERAL, &PIT2_config);
/* Set channel 0 period to 1 s (240000000 ticks). */
PIT_SetTimerPeriod(PIT2_PERIPHERAL, PIT2_CHANNEL_0, PIT2_CHANNEL_0_TICKS);
/* Enable interrupts from channel 0. */
PIT_EnableInterrupts(PIT2_PERIPHERAL, PIT2_CHANNEL_0, kPIT_TimerInterruptEnable);
/* Enable interrupt PIT2_IRQN request in the NVIC */
EnableIRQ(PIT2_IRQN);
/* Start channel 0. */
PIT_StartTimer(PIT2_PERIPHERAL, PIT2_CHANNEL_0);
}
3. Operating system interface registration
stay FreeRTOSConfig.h Make the following configuration
/* Run time and task stats gathering related definitions. */
-#define configGENERATE_RUN_TIME_STATS 0
+#define configGENERATE_RUN_TIME_STATS 1
#define configUSE_TRACE_FACILITY 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 1
+
+extern volatile unsigned int ulCpuTraceTick;
+
+#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() (ulCpuTraceTick = 0ul)
+#define portGET_RUN_TIME_COUNTER_VALUE() ulCpuTraceTick
Compile function interface print CPU Usage rate :
uint8_t CPU_RunInfo[400]; // Save task runtime information
//ref https://www.jianshu.com/p/cc4b948c7741
void CPU_Task(void* parameter)
{
PRINTF("CPU_Task \r\n");
while (1)
{
memset(CPU_RunInfo,0,400); // The information buffer is cleared
vTaskList((char *)&CPU_RunInfo); // Get task runtime information
PRINTF("---------------------------------------------\r\n");
PRINTF(" Task name Task status priority Remaining stack Task number \r\n");
PRINTF("%s", CPU_RunInfo);
PRINTF("---------------------------------------------\r\n");
memset(CPU_RunInfo,0,400); // The information buffer is cleared
vTaskGetRunTimeStats((char *)&CPU_RunInfo);
PRINTF(" Task name Run count utilization \r\n");
PRINTF("%s", CPU_RunInfo);
PRINTF("---------------------------------------------\r\n\n");
vTaskDelay(1000); /* Time delay 500 individual tick */
// PRINTF("CPU_Task \r\n");
}
}
4. verification
By creating tasks CPU Statistics of utilization rate :
extern void CPU_Task(void* parameter);
stat = xTaskCreate(CPU_Task, "CpuTask", configMINIMAL_STACK_SIZE + 5000, NULL, tskIDLE_PRIORITY + 1, NULL);
if (pdPASS != stat)
{
PRINTF("Failed to create awtk task");
while (1)
;
}
The actual effect is as follows :
---------------------------------------------
Task name Task status priority Remaining stack Task number
CpuTask X 1 5016 2
TestTask R 1 11952 1
IDLE R 0 79 4
bsp_can_t B 2 12069 3
Tmr Svc B 17 153 5
---------------------------------------------
Task name Run count utilization
TestTask 1856 97%
CpuTask 40 2%
IDLE 0 <1%
bsp_can_t 0 <1%
Tmr Svc 0 <1%
---------------------------------------------
---------------------------------------------
Task name Task status priority Remaining stack Task number
CpuTask X 1 5008 2
TestTask R 1 11952 1
IDLE R 0 79 4
bsp_can_t B 2 12069 3
Tmr Svc B 17 153 5
---------------------------------------------
Task name Run count utilization
TestTask 2088 99%
CpuTask 48 2%
IDLE 0 <1%
bsp_can_t 0 <1%
Tmr Svc 0 <1%
---------------------------------------------
---------------------------------------------
Task name Task status priority Remaining stack Task number
CpuTask X 1 5008 2
TestTask R 1 11952 1
IDLE R 0 79 4
bsp_can_t B 2 12069 3
Tmr Svc B 17 153 5
---------------------------------------------
Task name Run count utilization
TestTask 2320 100%
CpuTask 53 2%
IDLE 0 <1%
bsp_can_t 0 <1%
Tmr Svc 0 <1%
---------------------------------------------
4. summary
Turn on CPU There are two problems in usage statistics :
First of all : Maximum statistical time .
second : Occupy CPU resources .
Welcome to subscribe to
“ Embedded practice ” A place to share practical development experience .
The article will also be posted to my CSDN Home page 、 Today's headlines On the platform .
边栏推荐
猜你喜欢

Acwing brush questions

Several methods of realizing high-low byte or high-low word exchange in TIA botu s7-1200

数组排序2

Postman imports curl, exports curl, and exports corresponding language codes

SEGGER Embedded Studio找不到xxx.c或者xxx.h文件

FFmpeg 视频添加水印

Scroll view pull-down refresh and pull-up load (bottom)

Getting started with mongodb Basics

1. If function of Excel

这种是我的vs没连上数据库吗
随机推荐
MySQL的优化分析及效率执行
性能和成本的综合架构:单元化架构
9、 File upload and download
Use of anonymous functions
数组排序1
UE4 通过按键控制物体的旋转
Comprehensive evaluation and decision-making method
第三篇如何使用SourceTree提交代码
YAPI安装
三、@RequestMapping注解
Why is mongodb fast
Integrated architecture of performance and cost: modular architecture
Compiled by egg serialize JS
Graph translation model
UE4 键盘控制开关灯
MySQL - multi table query - Cartesian product sum, correct multi table query, equivalent connection and unequal connection, inner connection and outer connection
Build a maker Education Laboratory for teenagers
九、文件上传和下载
FFmpeg 视频编码
五、域对象共享数据