Remove function from gprof profiling

I’m profiling a C code with a python automated algorithm using GNU gprof via subprocess.run calls. I’m in trouble to remove a function (let’s say a clean_cache function) from results. I’ve already tried to use -P, -Q and --no-time flags but these only remove the function to be printed in results output. I want to remove a function to be computed at all. I’ll leave my code in the end of this session. Any help will be greatly appreciated!

[some imports here!]

class Gprof:
    """
    Flags:
        -b : Print data without explanation.
        
    """
    FLAGS = "-b --no-time=clean_cache -Pclean_cache -Qclean_cache"

    COMMAND = f"gprof {FLAGS}"

    CALL_GRAPH_COLS = [
        'index',
        '% time',
        'self',
        'children',
        'called',
        'name'
    ]

    FLAT_PROFILE_COLS = [
        '% time',
        'cumulative seconds',
        'self seconds',
        'calls',
        'self ms/call',
        'total ms/call',
        'name'    
    ]

def main():
    [...]
    for i in tqdm.tqdm(range(runs)):
    # sys.argv[2] is the name of C algorithm file
    _ = subprocess.run(f"{sys.argv[2]}", capture_output = True, shell = True)
    result = subprocess.run(f"{Gprof.COMMAND} {sys.argv[2]} {gmon_filename}", 
capture_output = True, shell = True)
    [...]

Example of output:

Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name    
  0.00      0.00     0.00    12095     0.00     0.00  swap_scalars
  0.00      0.00     0.00     2049     0.00     0.00  copy_array
  0.00      0.00     0.00     1535     0.00     0.00  heapify
  0.00      0.00     0.00     1024     0.00     0.00  random_scalar
  0.00      0.00     0.00     1023     0.00     0.00  merge
  0.00      0.00     0.00      931     0.00     0.00  median_pivot
  0.00      0.00     0.00        3     0.00     0.00  is_sorted
  0.00      0.00     0.00        1     0.00     0.00  heap_sort
  0.00      0.00     0.00        1     0.00     0.00  merge_sort
  0.00      0.00     0.00        1     0.00     0.00  quick_sort
  0.00      0.00     0.00        1     0.00     0.00  randomize_array

            Call graph


granularity: each sample hit covers 2 byte(s) no time propagated

index % time    self  children    called     name
                0.00    0.00    1023/12095       heap_sort [9]
                0.00    0.00    2755/12095       quick_sort [11]
                0.00    0.00    8317/12095       heapify [3]
[1]      0.0    0.00    0.00   12095         swap_scalars [1]
-----------------------------------------------
                0.00    0.00       3/2049        main [18]
                0.00    0.00    2046/2049        merge_sort [10]
[2]      0.0    0.00    0.00    2049         copy_array [2]
-----------------------------------------------
                                8317             heapify [3]
                0.00    0.00    1535/1535        heap_sort [9]
[3]      0.0    0.00    0.00    1535+8317    heapify [3]
                0.00    0.00    8317/12095       swap_scalars [1]
                                8317             heapify [3]
-----------------------------------------------
                0.00    0.00    1024/1024        randomize_array [12]
[4]      0.0    0.00    0.00    1024         random_scalar [4]
-----------------------------------------------
                0.00    0.00    1023/1023        merge_sort [10]
[5]      0.0    0.00    0.00    1023         merge [5]
-----------------------------------------------
                0.00    0.00     931/931         quick_sort [11]
[6]      0.0    0.00    0.00     931         median_pivot [6]
-----------------------------------------------
                0.00    0.00       3/3           main [18]
[8]      0.0    0.00    0.00       3         is_sorted [8]
-----------------------------------------------
                0.00    0.00       1/1           main [18]
[9]      0.0    0.00    0.00       1         heap_sort [9]
                0.00    0.00    1535/1535        heapify [3]
                0.00    0.00    1023/12095       swap_scalars [1]
-----------------------------------------------
                                2046             merge_sort [10]
                0.00    0.00       1/1           main [18]
[10]     0.0    0.00    0.00       1+2046    merge_sort [10]
                0.00    0.00    2046/2049        copy_array [2]
                0.00    0.00    1023/1023        merge [5]
                                2046             merge_sort [10]
-----------------------------------------------
                                 930             quick_sort [11]
                0.00    0.00       1/1           main [18]
[11]     0.0    0.00    0.00       1+930     quick_sort [11]
                0.00    0.00    2755/12095       swap_scalars [1]
                0.00    0.00     931/931         median_pivot [6]
                                 930             quick_sort [11]
-----------------------------------------------
                0.00    0.00       1/1           main [18]
[12]     0.0    0.00    0.00       1         randomize_array [12]
                0.00    0.00    1024/1024        random_scalar [4]
-----------------------------------------------

Index by function name

   (7) clean_cache             [8] is_sorted              [11] quick_sort
   [2] copy_array              [6] median_pivot            [4] random_scalar
   [9] heap_sort               [5] merge                  [12] randomize_array
   [3] heapify                [10] merge_sort              [1] swap_scalars

As we can see, all functions have 0% of CPU time. What shows that clean_cache function is only being removed from output.

Leave a Comment