samedi 20 octobre 2018

gperftools heap profiler not dumping information in multithreads

I am trying to understand heap profiling and std threads, but I get a runtime error that I cannot understand. My code:

 #include <memory>
 #include <gperftools/heap-profiler.h>
 #include <thread>
 using namespace std;

 void fcn1() // no leaks
 {
    HeapProfilerStart("temp1");
    for(auto i=0; i<10000; i++)
    {
       unique_ptr<long long> t(new long long);
       *t = 10;
    }
    HeapProfilerDump("partial1");
    HeapProfilerStop();
 }

 int fcn2() // leaks
 {
    HeapProfilerStart("temp2");
    for(auto i=0; i<10000; i++)
    {
            long* t = new long;
            *t = 10;
    }
    HeapProfilerDump("partial2");
    HeapProfilerStop();
 }

 int main(void)
 {
    thread t1(fcn1);
    thread t2(fcn2);
    t1.join();
    t2.join();
    return 0;
 }

I have 2 issues that I am having trouble with.

  1. As is, only the first file is dumped (temp1.0001.heap). Why is this the case? Shouldn't it wait for both threads to finish before terminating? If I don't treat it in a multi threaded environment (i.e. call fcn1, then fcn2) then both appear. Is this an issue with gperftools or a race condition or what?

The output is:

 Starting tracking the heap
 Dumping heap profile to temp1.0001.heap (partial2)

  1. If I comment out the HeapProfilerDump calls, there is no file output i.e. nothing in dumped. Should it not dump the profile file when it stops, or is it something that must be done manually?

Aucun commentaire:

Enregistrer un commentaire