Jump to content

Birds Eye View/Performance/Profiling: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 12: Line 12:
Install the library packages from GitHub repository
Install the library packages from GitHub repository


<source lang="bash">
<syntaxhighlight lang="bash">
git clone https://github.com/gperftools/gperftools
git clone https://github.com/gperftools/gperftools
cd gperftools
cd gperftools
Line 19: Line 19:
make
make
sudo make install
sudo make install
</source>
</syntaxhighlight>


On Debian-based systems, the complementary tools are packaged under the google-perftools package. For graphical output you also need Graphviz installed:
On Debian-based systems, the complementary tools are packaged under the google-perftools package. For graphical output you also need Graphviz installed:
<source lang="bash">
<syntaxhighlight lang="bash">
sudo apt-get install google-perftools graphviz
sudo apt-get install google-perftools graphviz
</source>
</syntaxhighlight>
Note that all the tools have the “google-” prefix under debian - the prefix may be missing on other systems (and is also missing in the official documentation).
Note that all the tools have the “google-” prefix under debian - the prefix may be missing on other systems (and is also missing in the official documentation).


Line 32: Line 32:
To enable profiling execute meson and compile the library with the following option:
To enable profiling execute meson and compile the library with the following option:


<source lang="bash">
<syntaxhighlight lang="bash">
meson -Dprofiling=enabled ..
meson -Dprofiling=enabled ..
ninja
ninja
</source>
</syntaxhighlight>


=== A) Profile the whole process runtime ===
=== A) Profile the whole process runtime ===
Line 45: Line 45:
'''CPUPROFILE:''' Name of the output log file
'''CPUPROFILE:''' Name of the output log file


<source lang="bash">
<syntaxhighlight lang="bash">
LD_PRELOAD=/usr/local/lib/libprofiler.so CPUPROFILE=test.prof ./path/to/bin
LD_PRELOAD=/usr/local/lib/libprofiler.so CPUPROFILE=test.prof ./path/to/bin
</source>
</syntaxhighlight>


'''2.''' Keep the application open or running until it finishes, in this mode if the execution is canceled
'''2.''' Keep the application open or running until it finishes, in this mode if the execution is canceled
Line 53: Line 53:


'''3.1''' Once the application ended the ''test.prof'' file contains the CPU profile information. To get a graphical output run:
'''3.1''' Once the application ended the ''test.prof'' file contains the CPU profile information. To get a graphical output run:
<source lang="bash">
<syntaxhighlight lang="bash">
pprof -gv ./path/to/bin test.prof
pprof -gv ./path/to/bin test.prof
</source>
</syntaxhighlight>


'''3.2''' Also as an alternative viewer you can display it to a web browser by running:
'''3.2''' Also as an alternative viewer you can display it to a web browser by running:
<source lang="bash">
<syntaxhighlight lang="bash">
pprof --web ./path/to/bin test.prof
pprof --web ./path/to/bin test.prof
</source>
</syntaxhighlight>


'''4.''' To generate a PDF report with the previous graphic output run:
'''4.''' To generate a PDF report with the previous graphic output run:
<source lang="bash">
<syntaxhighlight lang="bash">
pprof --pdf ./path/to/bin test.prof > output.pdf
pprof --pdf ./path/to/bin test.prof > output.pdf
</source>
</syntaxhighlight>


Example graphical output:
Example graphical output:
Line 76: Line 76:


'''1.''' To start profiling run:
'''1.''' To start profiling run:
<source lang="bash">
<syntaxhighlight lang="bash">
LD_PRELOAD=/usr/local/lib/libprofiler.so CPUPROFILE=test.prof CPUPROFILESIGNAL=12 ./path/to/bin
LD_PRELOAD=/usr/local/lib/libprofiler.so CPUPROFILE=test.prof CPUPROFILESIGNAL=12 ./path/to/bin
</source>
</syntaxhighlight>


'''2.''' Leave the program running until you want to start the profiling process. Then send the signal:
'''2.''' Leave the program running until you want to start the profiling process. Then send the signal:
Line 87: Line 87:
'''2.1''' Also you can use '''killall''' command to send the ''-12'' signal
'''2.1''' Also you can use '''killall''' command to send the ''-12'' signal


<source lang="bash">
<syntaxhighlight lang="bash">
killall -12 /path/to/bin
killall -12 /path/to/bin
</source>
</syntaxhighlight>


'''3.''' Leave the program until the point you want to profile it, then run again:
'''3.''' Leave the program until the point you want to profile it, then run again:
<source lang="bash">
<syntaxhighlight lang="bash">
killall -12 /path/to/bin
killall -12 /path/to/bin
</source>
</syntaxhighlight>


You will notice the following output when the output file was correctly generated:
You will notice the following output when the output file was correctly generated:
<source lang="bash">
<syntaxhighlight lang="bash">
Using signal 12 as cpu profiling switch
Using signal 12 as cpu profiling switch
PROFILE: interrupts/evictions/bytes = 4773/1826/231936
PROFILE: interrupts/evictions/bytes = 4773/1826/231936
</source>
</syntaxhighlight>


'''4.''' Once the application ended the ''test.prof.0'' file contains the CPU profile information. To get a graphical output run:
'''4.''' Once the application ended the ''test.prof.0'' file contains the CPU profile information. To get a graphical output run:
<source lang="bash">
<syntaxhighlight lang="bash">
pprof -gv ./path/to/bin test.prof
pprof -gv ./path/to/bin test.prof
</source>
</syntaxhighlight>


=== C) Profile specific section of source code ===
=== C) Profile specific section of source code ===


'''1.''' Add the header file in your code:
'''1.''' Add the header file in your code:
<source lang="C">
<syntaxhighlight lang="bash">
#include <gperftools/profiler.h>
#include <gperftools/profiler.h>
</source>
</syntaxhighlight>


'''2.''' Add the following functions calls around the code you want to profile:
'''2.''' Add the following functions calls around the code you want to profile:
<source lang="C">
<syntaxhighlight lang="C">


ProfilerStart("output_inside.prof"); //Start profiling section and save to file
ProfilerStart("output_inside.prof"); //Start profiling section and save to file
Line 124: Line 124:


ProfilerStop(); //End profiling section
ProfilerStop(); //End profiling section
</source>
</syntaxhighlight>


'''3.''' Run the test application, where you will an output similar to the following:
'''3.''' Run the test application, where you will an output similar to the following:


<source lang="bash">
<syntaxhighlight lang="bash">
fsolano@ridgerun-laptop:build$ ./test
fsolano@ridgerun-laptop:build$ ./test
PROFILE: interrupts/evictions/bytes = 9/0/280
PROFILE: interrupts/evictions/bytes = 9/0/280
Line 137: Line 137:
PROFILE: interrupts/evictions/bytes = 7/0/464
PROFILE: interrupts/evictions/bytes = 7/0/464
PROFILE: interrupts/evictions/bytes = 8/0/680
PROFILE: interrupts/evictions/bytes = 8/0/680
</source>
</syntaxhighlight>


'''4.''' With this method you can wait until the application execution ends, or end it with Ctrl+C.
'''4.''' With this method you can wait until the application execution ends, or end it with Ctrl+C.


'''5.''' Once the application ended the ''output_inside.prof'' file contains the CPU profile information. To get a graphical output run:
'''5.''' Once the application ended the ''output_inside.prof'' file contains the CPU profile information. To get a graphical output run:
<source lang="bash">
<syntaxhighlight lang="bash">
pprof -gv ./test output_inside.prof
pprof -gv ./test output_inside.prof
</source>
</syntaxhighlight>


<noinclude>
<noinclude>
{{Birds Eye View/Foot|Performance/PC |Contact_us}}
{{Birds Eye View/Foot|Performance/PC |Contact_us}}
</noinclude>
</noinclude>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.