<p>I'm developing a multi-threaded app for a Linux embedded platform.</p>

<p>At the moment I'm setting the stack size for each thread (via pthread_set_attr) to a fairly large default value. I would like to fine tune that value for each thread to something smaller to reduce my application's memory usage. I could go through the trial and error route of setting each thread's stack size to progressively smaller values until the program crashed, but the application uses ~15 threads each with completely different functionality/attributes so that approach would be extremely time consuming.</p>

<p>I would much rather prefer being able to directly measure each thread's stack usage. Is there some utility people can recommend to do this? (For example, I come from a vxWorks background and using the 'ti' command from the vxWorks shell directly gives stats on the stack usage as well as other useful info on the task status.)</p>

<p>Thanks</p>

[{'answer_id': 58628, 'author': 'Tobi', 'author_id': 5422, 'author_profile': 'https://Stackoverflow.com/users/5422', 'pm_score': 2, 'selected': False, 'text': "<p>I do not know any good tools but as last resort you could include some code in your application to check it, similar to the following:</p>\n\n<pre><code>__thread void* stack_start;\n__thread long stack_max_size = 0L;\n\nvoid check_stack_size() {\n  // address of 'nowhere' approximates end of stack\n  char nowhere;\n  void* stack_end = (void*)&amp;nowhere;\n  // may want to double check stack grows downward on your platform\n  long stack_size = (long)stack_start - (long)stack_end;\n  // update max_stack_size for this thread\n  if (stack_size &gt; stack_max_size)\n    stack_max_size = stack_size;\n}\n</code></pre>\n\n<p>The check_stack_size() function would have to be called in some of the functions that are most deeply nested.</p>\n\n<p>Then as last statement in the thread you could output stack_max_size to somewhere.</p>\n\n<p>The stack_start variable would have to be initialized at start of your thread:</p>\n\n<pre><code>void thread_proc() {\n  char nowhere;\n  stack_start = (void*)&amp;nowhere;\n  // do stuff including calls to check_stack_size()\n  // in deeply nested functions\n  // output stack_max_size here\n}\n</code></pre>\n"}, {'answer_id': 58980, 'author': 'DGentry', 'author_id': 4761, 'author_profile': 'https://Stackoverflow.com/users/4761', 'pm_score': 2, 'selected': False, 'text': "<p>Referencing Tobi's answer: You can use <code>pthread_attr_getstackaddr</code> to get the base of the stack at any time, if setting a variable at thread initialization is difficult. You can then get the address of an automatic variable in your own function to determine how deep the stack is at that moment.</p>\n"}, {'answer_id': 28856039, 'author': 'd99kris', 'author_id': 485047, 'author_profile': 'https://Stackoverflow.com/users/485047', 'pm_score': 3, 'selected': False, 'text': '<p>Here are two tools that measure (native pthreads) stack usage in Linux applications:</p>\n\n<ul>\n<li><a href="http://valgrind.org" rel="nofollow noreferrer" title="Valgrind">Valgrind</a></li>\n<li><a href="https://github.com/d99kris/stackusage" rel="nofollow noreferrer" title="stackusage">Stackusage</a></li>\n</ul>\n\n<p><strong>Valgrind</strong></p>\n\n<p>Usage:</p>\n\n<pre><code>valgrind --tool=drd --show-stack-usage=yes PROG\n</code></pre>\n\n<p>Valgrind is a stable and powerful tool, useful not only for measuring stack usage. It may not support all embedded CPU models though.</p>\n\n<p><strong>Stackusage</strong></p>\n\n<p>Usage:</p>\n\n<pre><code>stackusage PROG\n</code></pre>\n\n<p>Stackusage is a light-weight tool specifically designed for measuring thread stack usage which should be portable for most embedded Linux platforms equipped with glibc. It is likely not as well-tested or mature as Valgrind/drd at this point.</p>\n\n<p>Full disclosure: I\'m the author of Stackusage.</p>\n'}]
['https://Stackoverflow.com/questions/58614', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6089/']