LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to get current stack usage (https://www.linuxquestions.org/questions/programming-9/how-to-get-current-stack-usage-352122/)

phuna 08-11-2005 03:59 AM

How to get current stack usage
 
Hi all,
I'm programming in Linux, is there any way to get current stack usage of a thread (it's stack usage, not the stack size), means that how much stack space that all local variables, function return addresses, etc. use at a the moment?
Sorry for my bad English.
Any help would be appreciated.

--
phuna

fouldsy 08-11-2005 04:14 AM

What language are you scripting/programming this in? Might help.

phuna 08-11-2005 08:19 AM

Thanks for your reply, I'm using C

jim mcnamara 08-11-2005 09:19 AM

The only available system call is getrusage().
It may not give you what you need, but it will indicate if you are near to crashing into limits.

phuna 08-11-2005 09:25 PM

I've tried getrusage, but it returns zero for stack usage!

Code:

#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
  int a = 0xabcd;
  int b = 0x12345678;
  unsigned short c = 0xff;
  int ret;
  struct rusage usage;

  ret = getrusage(RUSAGE_SELF, &usage);
  if (ret == 0)
    {
      printf("Stack usage: %d\n", usage.ru_isrss);
    }
  else
    {
      printf("Fail to get resource usage!");
    }
}



All times are GMT -5. The time now is 11:26 AM.