当前位置:网站首页>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 .
边栏推荐
猜你喜欢

The auxiliary role of rational cognitive educational robot in teaching and entertainment

自动化测试框架该如何搭建?

Getting started with mongodb Basics

7、 Restful

Wu Enda's machine learning after class exercises - logical regression

机器学习之信用卡欺诈检测

Ffmpeg video coding

Creative design principle of youth maker Education

Authentication Encyclopedia (cookies, sessions, tokens, JWT, single sign on), in-depth understanding and understanding of authentication

Yapi installation
随机推荐
Life related -- the heartfelt words of a graduate tutor of Huake (mainly applicable to science and Engineering)
[uoj 429] runs (inclusive) + a little record about Lyndon tree and its application
Day26 job
7、 Restful
力扣每日一题-第42天-661. 图片平滑器
Li Kou daily question - day 42 -661. Picture smoother
[C language foundation] 13 preprocessor
Yuansaka Lin wallpaper
数组排序3
How does win11 change the power mode? Win11 method of changing power mode
图互译模型
Cnosdb Nirvana Rebirth: abandon go and fully embrace rust
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(八)
数组排序2
1. Mx6u system migration-6-uboot graphical configuration
Wu Enda's machine learning after class exercises - logical regression
egg-ts-sequelize-CLI
Matlab drawing
远坂凛壁纸
Acwing brush questions