堆栈大小估计 [英] Stack Size Estimation

查看:174
本文介绍了堆栈大小估计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多线程嵌入式软件(用C或C ++),线程必须以允许它完成它的操作不会溢出给予足够的堆栈空间。堆栈的正确尺寸,在一些实时嵌入式环境至关重要,因为(至少在一些系统中我使用过),操作系统将不会检测这个给你。

In multi-threaded embedded software (written in C or C++), a thread must be given enough stack space in order to allow it to complete its operations without overflowing. Correct sizing of the stack is critical in some real-time embedded environments, because (at least in some systems I've worked with), the operating system will NOT detect this for you.

通常,对于一个新的线程的堆栈大小(比主线程其他)在该线程被创建时指定(即,在一个参数给pthread_create()等)。通常,这些堆栈大小是硬coded到已知要善于在code最初是书面或测试的时间值。

Usually, the stack size for a new thread (other than the main thread) is designated at the time that thread is created (i.e. in an argument to pthread_create() or the like). Often, these stack sizes are hard-coded to values that are known to be good at the time the code was originally written or tested.

不过,未来的code经常变化打破该硬codeD堆栈大小是基于的假设,一个灾难性的一天,你的线程进入其调用图和溢出的更深层次的分支之一堆栈 - 打倒整个系统或默默破坏内存

However, future changes to the code often break the assumptions on which the hard-coded stack sizes were based, and one fateful day, your thread enters one of the deeper branches of its call graph and overflows the stack -- bringing down the whole system or silently corrupting memory.

我亲眼见到在在线程中执行code声明堆栈结构实例的情况下这个问题。当结构被扩充以容纳更多的数据,堆栈大小相应地膨胀,可能允许出现堆栈溢出。我想这可能是建立codebases哪里添加字段不能立即知道结构的全面影响(过多线程/功能来找到在使用该结构的所有地方)一个巨大的问题。

I have personally seen this problem in the case where code executed in the thread declares struct instances on the stack. When the struct is augmented to hold additional data, the stack size inflates accordingly, potentially allowing stack overflows to occur. I imagine this could be a huge problem for established codebases where the full effects of adding fields to a structure cannot be known immediately (too many threads/functions to find all the places where that struct is used).

由于平时应对堆栈大小的问题是他们是不可移植的,让我们假设编译器,操作系统和处理器都是已知量针对此事展开调查。让我们假设不使用递归,所以我们不处理的可能性的无限循环的情景。

Since the usual response to "stack sizing" questions is "they're not portable", let's assume that the compiler, operating system, and processor are all known quantities for this investigation. Let's also assume recursion isn't used, so we're not dealing with the possibility an "infinite recursion" scenario.

什么是估计一个线程必要的堆栈大小一些可靠的方法?我想preFER方法是离线(静态分析)和自动的,但所有的想法都欢迎。

What are some reliable ways to estimate the necessary stack size for a thread? I'd prefer methods that are offline (static analysis) and automatic, but all ideas are welcome.

推荐答案

运行 - 评估

这是网上的方法是有一定价值的油漆完整堆栈,如加上0xAAAA(或和0xAA,无论你的宽度)。然后,你可以检查多大栈在过去最大增长了检查多少画的是保持不变。

An online method is to paint the complete stack with a certain value, like 0xAAAA (or 0xAA, whatever your width is). Then you can check how large the stack has maximally grown in the past by checking how much of the painting is left untouched.

看一看这个链接与插图的说明。

Have a look at this link for an explanation with illustration.

的优点是它的简单。缺点是,你不能确定你的堆栈大小不会最终你的测试过程中超过所堆的数量。

The advantage is that it's simple. A disadvantage is that you cannot be certain that your stack size won't eventually exceed the amount of used stack during your testing.

静态评估

有一些静态的检查,我认为甚至有存在,试图做到这一点被攻击的gcc版本。我可以告诉你的唯一的事情就是静态检查是很难在一般情况下做的。

There are some static checks and I think there even exists a hacked gcc version that tries to do this. The only thing I can tell you is that static checking is very difficult to do in the general case.

也看看<一个href=\"http://stackoverflow.com/questions/141498/what-open-source-c-static-analysis-tools-are-available\">this问题。

这篇关于堆栈大小估计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆