Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does gperftools CPU profiler start?

gperftools documentation says that libprofiler should be linked into a target program:

$ gcc myprogram.c -lprofiler

(without changing a code of the program).

And then program should be run with a specific environment variable:

CPUPROFILE=/tmp/profiler_output ./a.out

The question is: how does libprofile have a chance to start and finish a profiler when it is merely loaded, but its functions are not called?

There is no constructor function in that library (proof). All occasions of "CPUPROFILE" in library code do not refer to any place where profiler is started.

I am out of ideas, where to look next?

like image 426
Daniel Vartanov Avatar asked Nov 29 '12 06:11

Daniel Vartanov


People also ask

What is gperftools?

gperftools is a set of tools for performance profiling and memory checking. One of the main advantages of the CPU profiler is a very nice graphical output, low overhead and very simple use (the profiled application doesn't require any recompilation, the profiling is enabled by simply preloading the profiler's library).

Where is Libprofiler so?

Where is your libprofiler. so. 0 ? It should be installed into /usr/local/lib or /usr/lib .

What is Google gperftools?

Gperftools from Google provides a set of tools aimed for analyzing and improving performance of multi-threaded applications. They offer a CPU profiler, a fast thread aware malloc implementation, a memory leak detector and a heap profiler. We focus on their sampling based CPU profiler.

How does gperftools work with signal handlers?

Fortunately, gperftools makes it very simple thanks to the possibility to install a signal handler for which the profiler listens. When the signal is received for the first time, profiling starts and when it's received for the second time, profiling ends. where CPUPROFILESIGNAL tells the number of the signal the profiler listens for.

How do I use the CPU profiler that Google uses?

This is the CPU profiler we use at Google. There are three parts to using it: linking the library into an application, running the code, and analyzing the output. On the off-chance that you should need to understand it, the CPU profiler data file format is documented separately, here .

What is the best CPU profiler for Linux?

The gperftools CPU profiler has a very little runtime overhead, provides some nice features like selectively profiling certain areas of interest and has no problem with multi-threaded applications. KCachegrind can be used to analyze the profiling data.


1 Answers

As per the documentation the linked webpage, under Linking the library, it describes that the -lprofiler step is the same as linking against the shared object file with LD_PRELOAD option.

The shared object file isn't the same as just the header file. The header file contains function declarations which are looked up by when compiling a program , so the names of the functions resolve, but the names are just names, not implementations. The shared object file (.so) contains the implementations of the functions. For more information see the following StackOverflow answer.

Source file of /trunk/src/profiler.cc on Line 182, has a CPUProfiler constructor, that checks for whether profiling should be enabled or not based on the CPUPROFILE environment variable (Line 187 and Line 230).

It then calls the Start function on Line 237. As per the comments in this file, the destructor calls the Stop function on Line 273.

To answer your question I believe Line 132 CpuProfiler CpuProfiler::instance_; is the line where the CpuProfiler is instantiated.

This lack of clarity in the gperftools documentation is known issue see here.

like image 186
Appleman1234 Avatar answered Oct 03 '22 10:10

Appleman1234