当前位置:网站首页>FreeRTOS -- a method to detect the usage of task stack
FreeRTOS -- a method to detect the usage of task stack
2022-07-22 01:20:00 【Embedded pit entry notes】
Use freeRTOS when , Every task created needs to allocate the stack space of the task , These stack spaces are generally roughly allocated by the user according to the situation of the task . Users often cannot directly know whether the allocated task stack space is enough for the task to run , Sometimes it may not be enough , Stack overflow occurs during operation , Cause all kinds of strange problems in the program , After the problem occurs, it may be difficult to find it for a while .
In this case, if there is a detection method to know the stack usage or remaining situation of the task in the running process , Then you can have a good grasp of the task stack , You can also avoid stack overflow at the beginning , In the future, the launch of the project products will also avoid such risks !
It's worth having fun ,freeRTOS It really has the function of detecting the remaining amount of stack . Here's how to use freeRTOS Method of remaining amount of task stack .
1、 Allow stack detection API Use
freeRTOS The detection of the remaining amount in the stack uses uxTaskGetStackHighWaterMark , To use this function, you need to define a macro as 1 To use the stack detection function .
open FreeRTOSConfig.h file , Find macro INCLUDE_uxTaskGetStackHighWaterMark And set its value as 1, as follows :
#define INCLUDE_uxTaskGetStackHighWaterMark 1
This macro is defined as 1 File later task.c You can see the function in uxTaskGetStackHighWaterMark It works , It shows that this function is already in a usable state .
2、uxTaskGetStackHighWaterMark Function description
freeRTOS The function prototype of the detection function of the task stack in is as follows :
UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
{
TCB_t * pxTCB;
uint8_t * pucEndOfStack;
UBaseType_t uxReturn;
pxTCB = prvGetTCBFromHandle( xTask );
#if portSTACK_GROWTH < 0
{
pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
}
#else
{
pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
}
#endif
uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
return uxReturn;
}
Function description :
Function parameter :
xTask: The task handle of the stack condition that needs to be checked . This handle can be known when creating the task .
Be careful : take xTask Set to NULL If so, it is the task stack that calls this function .
Function return value :
Self created xTask Since this task , The minimum amount of available stack space that already exists ( In words ). That is, the returned value is the stack remainder in words ( for example , stay 32 On the bit machine , Return value 1 Indicates that the unused stack in the task stack is still left 4 Bytes ). If the return value is zero , Then the task may have overflowed . If the return value is close to zero , Then the task approaches stack overflow .
Particular attention : freeRTOS One more uxTaskGetStackHighWaterMark2() , This is uxTaskGetStackHighWaterMark() Another version of , It returns a user definable type , Mainly used in some 8 There are restrictions on the width of data types in the bit architecture MCU in .
3、 How to use stack residue detection
3.1、 The task checks its own stack
If UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask ) Parameters of xTask Set to NULL, Which task calls this function to detect the stack condition of which task , as follows :
void vTask1( void * pvParameters )
{
UBaseType_t uxHighWaterMark;
// Check the stack condition before using the task
uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
for( ;; )
{
... // Other code
...
...
vTaskDelay( 1000 );
// Detect the remaining stack after the task runs
uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
}
}
3.2、 Use the task to detect the stack of the task separately
You can use a task to detect the task stack in the project alone , as follows :
void TaskStackDetect_Task1( void * pvParameters )
{
UBaseType_t uxHighWaterMark;
for( ;; )
{
uxHighWaterMark = uxTaskGetStackHighWaterMark( Task to handle 1 );
print(uxHighWaterMark);
uxHighWaterMark = uxTaskGetStackHighWaterMark( Task to handle 2 );
print(uxHighWaterMark);
uxHighWaterMark = uxTaskGetStackHighWaterMark( Task to handle 3 );
print(uxHighWaterMark);
......
uxHighWaterMark = uxTaskGetStackHighWaterMark( Task to handle n );
print(uxHighWaterMark);
vTaskDelay( 1000 );
}
}
3.3、 Use the software timer to detect the condition of the task stack
Detecting the rest of the stack can also be used freeRTOS Software timer service provided in , Create a software timer , Then it can be detected regularly in the callback function of the software timer , The principle is the same as using a task detection . as follows :
void TaskStackDetect_Callback(TimerHandle_t pxTimer)
{
UBaseType_t uxHighWaterMark;
uxHighWaterMark = uxTaskGetStackHighWaterMark( Task to handle 1 );
print(uxHighWaterMark);
uxHighWaterMark = uxTaskGetStackHighWaterMark( Task to handle 2 );
print(uxHighWaterMark);
uxHighWaterMark = uxTaskGetStackHighWaterMark( Task to handle 3 );
print(uxHighWaterMark);
......
uxHighWaterMark = uxTaskGetStackHighWaterMark( Task to handle n );
print(uxHighWaterMark);
}
Interested in Embedded Technology , Welcome to WeChat official account. “ Embedded pit notes ”, Study and discuss together !
边栏推荐
- (pytorch advanced road VII) attention based seq2seq
- 多智能体系统集群协同控制实验平台详解与典型案例
- 软件资源大全
- Mysql04(行专列)
- Leetcode skimming: related topics of binary tree sequence traversal
- 分析少儿编程思维的启蒙问题
- Share 50 free cloud disk and online disk Services - with unlimited storage space
- Difference between two lists
- Appium自动化框架升级到最新的2.0
- Malloc et configurateur d'espace
猜你喜欢
list解析<stl初级> (跑路人笔记)
Do traditional enterprises need data center?
【论文笔记】3D点云分割-PointNet
Websocket transfer file
Business innovation driven by metadata to build new competitive advantages of enterprises
44:第四章:开发文件服务:5:在【files】文件服务中,整合FastDFS,实现【上传头像】的逻辑;(包括:项目中整合FastDFS;跨域问题;创建资源文件,并在代码中获取属性;异常处理;)
(pytorch advanced road VI) implementation of swing transformer
传输层协议解析——UDP和TCP
多智能体系统集群协同控制实验平台详解与典型案例
人工神经网络的分类包括,人工神经网络的分类有
随机推荐
Share 50 free cloud disk and online disk Services - with unlimited storage space
Appium脚本启动参数如何设置
websocket 传输文件
Appium元素定位——App自动化测试
PO模式在Selenium自动化测试中怎么应用
Centos7 online MySQL installation
Malloc et configurateur d'espace
如何测试webservice接口
Pycharm使用教程:5个非常有用的技巧
Word 2016 strange problem: file save error copy crash
软件资源大全
Mysql04(行专列)
Oracle笔记 之 空值null的处理
Selenium被检测为爬虫,怎么屏蔽和绕过
ITK median filter
Mysql06(序列)
The classification of artificial neural network includes: the classification of artificial neural network includes
奇葩!面试题目竟设置如厕习惯、吃饭时长、入睡时间
Design and implementation of tcp/ip protocol stack LwIP: Part Five
Leetcode [剑指 Offer II 068. 查找插入位置