Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys.
I need to measure how much stack is used when I call a function in a thread. If I do this at the starting function of a thead, I will get some information about stack usage of thread. This helps me create threads with appropriate stack size.
mr.abzadeh

Edit: Why no respnce? where are guys?
Posted
Updated 9-Dec-19 3:54am
v2
Comments
H.Brydon 1-Feb-14 17:43pm    
Write an API that looks at the address of the stack, and takes the difference from a call to the same API at program start...

 
Share this answer
 
Comments
mr.abzadeh 2-Feb-14 21:03pm    
These link are very good for measuring purposes, but they are in .net I need to measure stack usage in native C/C++
Thanks for your help
mr.abzadeh
I don't know how many threads you are creating but if this were a persistent issue Windows would provide an api.
Do you have any reason to think that your threads are running out of stack?

There is a solution here: http://stackoverflow.com/questions/1740888/determining-stack-space-with-visual-studio/1747499#1747499[^]using http://en.wikipedia.org/wiki/Win32_Thread_Information_Block[^]
 
Share this answer
 
v2
Comments
mr.abzadeh 2-Feb-14 20:49pm    
My program does not run out of stack. I want to measure my threads stack user so that create threads with smaller stack size, using memory efficiently
mr.abzadeh
[no name] 2-Feb-14 20:59pm    
I ask again why? If this was an real issue it would be already addressed. How many threads do you have that you are concerned about use of memory? If you have so many that you are running out of memory because of thread stacks then maybe you really have other issues. However my answer shows a way of doing it.
mr.abzadeh 4-Feb-14 2:15am    
I provide a service. I create a thread for each connection. Currently my clients may be up to 200, so my service may have up to 200 threads. Sharing threads may increase performance but I don't know how to wake a thread when new packet arrives. Any suggestion appreciated
mr.abzadeh
[no name] 4-Feb-14 2:18am    
Are you using thread pooling?
mr.abzadeh 4-Feb-14 12:07pm    
Thanks. The links you showed me is great and very helpfull and now I can estimate my services stack usage. I create threads with 64 KB of stack size. I do not use thread pooling because any connection to the service I provide remains active for some hours. Now I think it's time to create a few number of threads, say 16, and The main thread only accept sockets and pass it to worker threads, Mentioning again, My problem is how the main thread will pass control to worker threads immediately.
mr.abzadeh
hai
try this link

Detecting Application Idleness[^]

but u need to code language
 
Share this answer
 
Comments
mr.abzadeh 29-Jan-14 13:27pm    
Thanks for your comment.
The link you pointed detects idleness of system resources such as cpu, gpu, and does not help measuring stack usage measurement.
It is not clear for me what you meant by 'code language'
A very simple method of measuring your current stack level is to take the address of a variable that is allocated on the stack, for example:
C++
char* GetCurrentStackLevel()
{
    char a;
    return &a;
}

Then call this function before and inside the function of which you want to measure the stack usage and subtract the two pointers. The stack usage of GetCurrentStackLevel itself will cancel out in that subtraction.

Note that you may not use the returned value for anything else! By the time GetCurrentStackLevel returns, the returned pointer will point to no man's land. It just can be used to measure your current stack level.
 
Share this answer
 
Comments
mr.abzadeh 2-Feb-14 20:53pm    
This measures stack usage from begining to current position, not overall stack usage. I can not guess which function will be called more deeply, and windows api system calls may use stack, which Is not measured in this way
mr.abzadeh
nv3 3-Feb-14 3:20am    
That is what I thought you are interested in, as you asked for "how much stack is used when I call a function in a thread".
{$IFDEF MSWINDOWS}
function currentStackUsage: NativeUInt;
//NB: Win32 uses FS, Win64 uses GS as base for Thread Information Block.
asm
  {$IFDEF WIN32}
  mov eax, fs:[4]  // TIB: base of the stack
  sub eax, esp     // compute difference in EAX (=Result)
  {$ENDIF}
  {$IFDEF WIN64}
  mov rax, gs:[8]  // TIB: base of the stack
  sub rax, rsp     // compute difference in RAX (=Result)
  {$ENDIF}
{$ENDIF}
end;
 
Share this answer
 
Comments
Dave Kreskowiak 9-Dec-19 10:35am    
AN unexplained code dump, and how to use it, isn't an answer.
Member 14683465 26-Sep-20 12:11pm    
Call this function before or inside the function of which you want to know the stack usage. For example in a recursive function to avoid stack overflow.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900