code_junkie

Measuring stack usage for Linux multi-threaded app

Nov 10th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. __thread void* stack_start;
  2. __thread long stack_max_size = 0L;
  3.  
  4. void check_stack_size() {
  5. // address of 'nowhere' approximates end of stack
  6. char nowhere;
  7. void* stack_end = (void*)&nowhere;
  8. // may want to double check stack grows downward on your platform
  9. long stack_size = (long)stack_start - (long)stack_end;
  10. // update max_stack_size for this thread
  11. if (stack_size > stack_max_size)
  12. stack_max_size = stack_size;
  13. }
  14.  
  15. void thread_proc() {
  16. char nowhere;
  17. stack_start = (void*)&nowhere;
  18. // do stuff including calls to check_stack_size()
  19. // in deeply nested functions
  20. // output stack_max_size here
  21. }
Add Comment
Please, Sign In to add comment