Method to calculate user space thread size

Tobias Boege tobias at gambas-buch.de
Wed Apr 3 09:38:43 EDT 2013


On Tue, 02 Apr 2013, naveen yadav wrote:
> Dear All,
> 
> I have very complex user space application contain more then 400 threads. I
> want to limit the stack size in user space, for this I want to know how
> much stack size each thread use in worst case.
> 
> To calculate this is I need to modify in kernel ? or current kernel have
> any support ?
> or is there a method I can get from smaps?

I wouldn't go over the kernel in this case - mainly because I don't know
where to look for the information.

The easiest way I can think of, is to add some assembly to sensible points
in your program. "Sensible points" would be end points of possibly long call
chains - because you want to get the stack pointer of the highest possible
stack frame of each thread. Then simply subtract the maximum of these values
from the stack starting address (you may want to disable ASLR).

On x86_64 I'd do it like:

__thread void *highest = (void *) -1;
register void *rsp asm("rsp");

/*
 * Note that the highest stack frame has the lowest address.
 */
#define CHECK_HIGHEST()	(highest = rsp < highest ? rsp : highest)

You have, nevertheless, the difficulty to ensure that the threads really
never exceed the maximum you obtained which is IMHO the really difficult
part here ;-)

Regards,
Tobi




More information about the Kernelnewbies mailing list