Programowanie w systemie UNIX/gprof


wstęp

edytuj
  • Opis [1]
  • Krytyczna analiza [2]
  • skompiluj program w celu profilowania, tzn. z opcjami -pg -g [3]
  • uruchom program (w celu utworzenia danych dla profilera)[4]
  • uruchom profiler
  • analizuj profil (wynik profilera)[5]
    • analiza tekstu
    • analiza graficzna : gprof2dot[6]

Analiza

edytuj

Utworzenie pliku do analizy graficznej:[7]

 chmod 0700 gprof2dot.py


 gprof ./a.out| gprof2dot.py | dot -T pdf > profile.pdf
 gprof ./a.out| gprof2dot.py | dot -T png > profile.png

Przykład

edytuj

Przykład [8]

gcc -pg -Wall myfile.c
# run myfile
./a.out
# generate the profile
gprof ./a.out

lub do pliku tekstowego prof.txt (nazwa przykładowa):

gprof ./a.out > prof.txt

Przykładowy wynik (dla programu z commons[9]) zawiera 2 części:

  • płaski profil
  • graf wywołań
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  us/call  us/call  name    
 37.89      7.07     7.07  4004001     1.77     1.77  ComputeColor
 34.89     13.59     6.51 200000162     0.03     0.03  iDrawLine
 16.03     16.58     2.99 200000241     0.01     0.05  dDrawLine.constprop.0
 10.72     18.58     2.00                             ComputeRays
  0.32     18.64     0.06                             ColourNewTrap
  0.16     18.67     0.03                             ComputeBoundariesIn
  0.05     18.68     0.01                             CopyRaysTo
  0.00     18.68     0.00        2     0.00     0.00  SaveFiles4ManualDebug
  0.00     18.68     0.00        1     0.00     0.00  FillGapsInRaysArray
  0.00     18.68     0.00        1     0.00     0.00  GiveC

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.
 
 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
	   else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this 
	   function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
	   the function in the gprof listing. If the index is
	   in parenthesis it shows where it would appear in
	   the gprof listing if it were to be printed.
Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 0.05% of 18.68 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]     61.6    2.00    9.51                 ComputeRays [1]
                2.99    6.51 200000241/200000241     dDrawLine.constprop.0 [2]
                0.00    0.00       2/2           SaveFiles4ManualDebug [9]
                0.00    0.00       1/1           FillGapsInRaysArray [10]
-----------------------------------------------
                2.99    6.51 200000241/200000241     ComputeRays [1]
[2]     50.9    2.99    6.51 200000241         dDrawLine.constprop.0 [2]
                6.51    0.00 200000162/200000162     iDrawLine [5]
-----------------------------------------------
                7.07    0.00 4004001/4004001     ComputeFatouComponents [4]
[3]     37.9    7.07    0.00 4004001         ComputeColor [3]
-----------------------------------------------
                                                 <spontaneous>
[4]     37.9    0.00    7.07                 ComputeFatouComponents [4]
                7.07    0.00 4004001/4004001     ComputeColor [3]
-----------------------------------------------
                6.51    0.00 200000162/200000162     dDrawLine.constprop.0 [2]
[5]     34.9    6.51    0.00 200000162         iDrawLine [5]
-----------------------------------------------
                                                 <spontaneous>
[6]      0.3    0.06    0.00                 ColourNewTrap [6]
-----------------------------------------------
                                                 <spontaneous>
[7]      0.2    0.03    0.00                 ComputeBoundariesIn [7]
-----------------------------------------------
                                                 <spontaneous>
[8]      0.1    0.01    0.00                 CopyRaysTo [8]
-----------------------------------------------
                0.00    0.00       2/2           ComputeRays [1]
[9]      0.0    0.00    0.00       2         SaveFiles4ManualDebug [9]
-----------------------------------------------
                0.00    0.00       1/1           ComputeRays [1]
[10]     0.0    0.00    0.00       1         FillGapsInRaysArray [10]
-----------------------------------------------
                0.00    0.00       1/1           setup [38]
[11]     0.0    0.00    0.00       1         GiveC [11]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

Odnośniki

edytuj
  1. Profilowanie pod Linuxem. gcc + gprof by mateusz midor
  2. Alternatywy dla gprof
  3. Debugging and profiling your C/C++ programs using Free Software by Xenofon Papadopoulos
  4. GNU gprof
  5. GPROF Tutorial – How to use Linux GNU GCC Profiling Tool by Himanshu Arora on August 10, 2012
  6. gprof2dot by José Fonseca
  7. scicomp stackexchange question: profiling-optimized-c-code-using-gprof
  8. profile-c-program-linux
  9. Program z commons