From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <8a7d369bb212b77cb4ca5c8b1408a267@plan9.bell-labs.com> To: 9fans@9fans.net Cc: 9fans@9fans.net Date: Wed, 19 May 2010 11:20:22 +0200 From: Sape Mullender In-Reply-To: <35dbc9ae832cb8869392d87a68e02dcf@iram.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Subject: [9fans] thread STACK size Topicbox-Message-UUID: 26e7eef6-ead6-11e9-9d60-3106f5b1d025 > A while ago, while working on btfs, I stumbled upon some sort of > overflow (http://9fans.net/archive/2009/07/77) which was in fact due > to the thread STACK being too small (and hence if I understood > correctly things would get written out of it, in the heap). > To be on the safe side, I have it set to 16384 now, but as I think I'm > getting near something usable with btfs, I'd like to go back to a more > fitting value. I think it's pretty important to have it as low as > possible since the number of threads/coroutines will grow linearly > with the number of peers connected (to be honest, I don't even know if > that can even scale in terms of memory use). > > So the question is, how can I evualuate what's the minimal value I can > set that to without getting into trouble again? Is there anything > smarter than just trial and error? There's no good way, really. One thing you might do is change the thread library to initialize the stack to some pattern (zeroing it will probably do, but you can let your phantasy go wild here). You can then, when your code has been running for a while, use acid -lthread and a bit of scripting to scan your stacks for the higest point where the pattern is disturbed. As a general rule in threaded programs, avoid declaring local arrays or large structs. Instead, malloc them and free them when you're done. A file server, as an example, should never allocate an 8K message buffer on the stack. If you can manage to obey the rule of not having arrays on the stack (as local variables), you can usually comfortably make use of 4K or 8K stacks. Sape