搜索
bottom↓
回复: 10

freertos如何确定任务堆栈大小(已找到相关检测堆栈溢出方法,分享给大家)

[复制链接]

出0入0汤圆

发表于 2010-11-8 18:52:59 | 显示全部楼层 |阅读模式
各位有谁知道freertos如何确定任务堆栈大小?非常感谢!

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

发表于 2010-11-8 20:02:34 | 显示全部楼层
提示一点,FREERTOS没有管理中断,所以要额外的考虑中断用到的堆栈。

出0入0汤圆

 楼主| 发表于 2010-11-9 13:37:39 | 显示全部楼层
回复【1楼】zhonghua_li 蓝色天空
-----------------------------------------------------------------------

具体如何确定堆栈大小,有没有具体的方法么?

出0入0汤圆

 楼主| 发表于 2010-11-9 14:21:06 | 显示全部楼层
Stack Usage and Stack Overflow Checking
[Configuration]


--------------------------------------------------------------------------------

--------------------------------------------------------------------------------


Stack Usage
Each task maintains its own stack. The memory used by the task stack is allocated
automatically when the task is created, and dimensioned by a parameter passed to
the xTaskCreate() API function. Stack overflow is a very common cause of application
instability. FreeRTOS.org therefore provides two optional mechanisms that can be
used to assist in the detection and correction of just such an occurrence. The option
used is configured using the configCHECK_FOR_STACK_OVERFLOW configuration constant.

Note that these options are only available on architectures where the memory map is
not segmented and the stack grows down from high memory. Also, some processors could
generate a fault or exception in response to a stack corruption before the kernel
overflow check can occur. The application must provide a stack overflow hook function
if configCHECK_FOR_STACK_OVERFLOW is not set to 0. The hook function must be called
vApplicationStackOverflowHook(), and have the prototype below:



void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName );


The pxTask and pcTaskName parameters pass to the hook function the handle and name
of the offending task respectively. Note however, depending on the severity of the
overflow, these parameters could themselves be corrupted.

Stack overflow checking introduces a context switch overhead so its use is only
recommended during the development or testing phases.




Stack Overflow Detection - Method 1
It is likely that the stack will reach its greatest (deepest) value after the kernel
has swapped the task out of the Running state (when the stack contains the task context).
At this point the kernel can check that the processor stack pointer remains within
the valid stack space. The stack overflow hook function is called should the stack
pointer contain an invalid value.

This method is quick but not guaranteed to catch all stack overflows. To use this
method only set configCHECK_FOR_STACK_OVERFLOW to 1.




Stack Overflow Detection - Method 2
When a task is first created its stack is filled with a known value. When swapping a
task out of the Running state the kernel can check the last 16 bytes within the valid
stack range to ensure that these known values have not been overwritten by the task
or interrupt activity. The stack overflow hook function is called should any of
these 16 bytes not remain at their initial value.

This method is less efficient than method one, but still fairly fast. It is very
likely to catch stack overflows but is still not guaranteed to catch all overflows.

To use this method in combination with method 1 set configCHECK_FOR_STACK_OVERFLOW
to 2. It is not possible to use only this method.

出0入0汤圆

发表于 2010-11-15 15:43:36 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-11-15 17:03:06 | 显示全部楼层
如果是RT-Thread那么就简单得多了,在finsh shell下输入命令:
list_thread()

会显示如下结果:
thread  pri  status sp         stack size max used   left tick  error
------  ---- ------ ---------- ---------- ---------- ---------- -----
tidle   0xff ready  0x00000074 0x00000100 0x00000074 0x0000003b 000
tshell  0x14 ready  0x0000024c 0x00000800 0x00000418 0x00000064 000

其中:
thread字段表示线程的名称
pri字段表示线程的优先级
status字段表示线程当前的状态
sp字段表示线程当前的栈位置
stack size字段表示线程的栈大小
max used字段表示线程历史上使用的最大栈位置
left tick字段表示线程剩余的运行节拍数
error字段表示线程的错误号

所以,可以开始的时候设置一个比较大的栈,然后系统运行一段时候后,再按照max used指示的值来调整栈大小即可(至少应该是max userd + 32字节)。

出0入0汤圆

发表于 2011-11-22 22:49:43 | 显示全部楼层
mark

出0入0汤圆

发表于 2013-8-4 11:13:39 | 显示全部楼层
怎么还是没有结果啊?不是可以看编译文件。

出0入0汤圆

发表于 2014-2-13 09:18:23 | 显示全部楼层
mark,学习了

出0入0汤圆

发表于 2014-4-9 21:21:12 | 显示全部楼层
这个有需要,正在学习中。多谢分享。

出0入0汤圆

发表于 2014-4-9 21:21:55 | 显示全部楼层
这个有需要,正在学习中。多谢分享。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-25 19:23

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表