Greg
Greg

Reputation: 1

Valgrind stack measurement

I would like to clarify some points about using Valgrind soft in order to measure stack usage size.

I'm using Linux OS. My main program is written on C++ and using 3-d party shared libraries(some external so files that were compiled separately, and provided to me). It's absolutely multithreaded.

My main code start new tasks while execution, and those tasks start new threads etc. Shared libraries also start new tasks on their own.

Now, I want to measure a total stack usage of a program. It includes: my main code stack + all tasks stacks + sub tasks stacks + 3-d party code stacks + etc.

I used Valgrind tool massif with option --stacks=yes. It created an output file with some data. For example:

.
.
.
   snapshot=33
   #----------
   time=29470
   mem_heap_B=0
   mem_heap_extra_B=0 
   mem_stacks_B=4024
   heap_tree=empty
   #----------
.
.
.

My question is: what is this value 4024 comes for? Which stack size does it show: process? main thread? Sum of stacks?

Valgrind manual doesn't help at all. It's only describing syntax of command.

Thanks.

Tried to search internet, Valgrind manual. No help.

Upvotes: 0

Views: 179

Answers (1)

Paul Floyd
Paul Floyd

Reputation: 6906

I'll update the manual to make this clearer.

The stack memory accounting is triggered by changes to the stack pointer. It doesn't matter where the stack pointer is (main thread or secondary threads). It all gets counted.

Running with -v -v -v -v then you will get (a lot) more information on what Massif is doing. You should see stuff like

--18486-- Massif: <<< new_mem_stack (72)
--18486-- Massif: >>>
--18486-- Massif: <<< die_mem_stack (-80)
--18486-- Massif: >>>

as stack memory is increased and decreased.

Here's a small example

#include <thread>
#include <vector>

void r(int c)
{
   char b[1000];
   --c;
   if (c)
   {
      r(c);
   }
}

int main()
{
   std::vector<std::thread> threads;
   threads.reserve(100);
   for (int i = 0; i < 100; ++i)
   {
      threads.emplace_back(std::thread(r, 1000));
   }

   for (auto& t : threads)
   {
      t.join();
   }
}

which gives

    MB
1.128^                                                                  #     
     |                                               :  @@   @     @  : #     
     |                                          :    :  @    @     @ :: #     
     |                                          :    :  @  : @:    @::: #     
     |                                          :    :: @  : @:    @::: #     
     |                                          :    :: @  : @:    @::: # :   
     |                                          :    :: @ :: @:    @::: # :  :
     |                                          :    :: @ :: @:    @::: # :  :
     |                                          :    :: @ :: @:    @::: # :  :
     |                                          :  : :: @ :: @:  : @::: # :  :
     |                                          :  : :: @ :: @:  : @::: # :  :
     |                                          :: : :: @ :: @:  : @::: # :  :
     |                                          :: :::: @ :: @:  ::@::::# :  :
     |                                         ::: :::: @ :: @:::::@::::# :  :
     |                                         ::: :::: @ :: @::::@@::::#::  :
     |                                         ::: :::: @ :: @::::@@::::#::  :
     |                                         :::::::: @ :::@::::@@::::#::  :
     |                                         :::::::: @ :::@::::@@::::#::  :
     |                                         ::::::::@@ :::@::::@@::::#:::::
     |                                         ::::::::@@ :::@::::@@::::#::::@
   0 +----------------------------------------------------------------------->Mi
     0                                                                   3.582

Upvotes: 0

Related Questions