Warning, /acts/docs/contribution/profiling.md is written in an unsupported language. File is not indexed.
0001 # Profiling
0002
0003 Software profiling allows you to inspect the performance of a piece of software, seeing where the bottlenecks are and how they can be improved.
0004 gperftools is a software profiling package. It contains a CPU profiler, thread-caching malloc library, memory leak detection tool, memory allocation profiler and pprof (discussed later). More information about gperftools and its components can be found on the project's [GitHub](https://github.com/gperftools/gperftools) and [documentation page](https://gperftools.github.io/gperftools/).
0005
0006 ## Install gperftools
0007
0008 It is strongly recommended to install [libunwind](https://github.com/libunwind/libunwind) before trying to configure or install gperftools.
0009
0010 ### Ubuntu
0011
0012 If you're using Ubuntu you can use the following command to install gperftools:
0013
0014 ```
0015 apt install google-perftools libgoogle-perftools-dev
0016 ```
0017
0018 ### Other Systems
0019
0020 Alternatively, you can use the following commands to install it:
0021
0022 ```console
0023 $ git clone https://github.com/gperftools/gperftools
0024 $ cd gperftools
0025 $ git tag -l # checkout the latest release version
0026 $ git checkout <gperftools-X.x>
0027 $ ./autogen.sh
0028 $ ./configure --prefix=<your/desired/install/dir>
0029 $ make
0030 $ make install
0031 ```
0032
0033 This will install gperftools in `your/desired/install/dir/lib` which is the path you should use when specifying where gperftools is, if necessary.
0034
0035 If you wish to install gperftools to a directory that is not one of the standard directories for libraries and therefore not findable by the `-l` compiler flag, you will need to specify the path to it with the `GPERF_INSTALL_DIR` option at build time.
0036 Further information about installing gperftools is [here](https://github.com/gperftools/gperftools/blob/master/INSTALL).
0037
0038 ## pprof
0039
0040 pprof is a tool for visualising and analysing profiling data.
0041 An older version of pprof comes bundled with gperftools but using the newer Go version comes with several benefits: nicer looking graphs and additional options that make looking through specific sections of a program easier being among them.
0042
0043 ### Install Go pprof (Optional)
0044
0045 First, you must install Go. Instructions to do so are available [here](https://go.dev/doc/install).
0046 Optionally, you can install [Graphviz](https://www.graphviz.org/download/) to produce visualisations of profiles.
0047
0048 Then, run the following command to install pprof itself:
0049
0050 ```console
0051 $ go install github.com/google/pprof@latest
0052 ```
0053
0054 ## Link gperftools Libraries When Compiling
0055
0056 The library needed to run the CPU profiler should be linked into the ACTS project using the following build option:
0057
0058 ```
0059 -DACTS_ENABLE_CPU_PROFILING=ON
0060 ```
0061
0062 Similarly, to enable the memory profiler the following build option should be used:
0063
0064 ```
0065 -DACTS_ENABLE_MEMORY_PROFILING=ON
0066 ```
0067
0068 ## Alternative to Recompiling
0069
0070 Alternatively, you can avoid rebuilding the project by pointing the `LD_PRELOAD` environment variable to the profiler library for CPU profiling:
0071
0072 ```
0073 LD_PRELOAD="<path/to/libprofiler.so>" <other_options> <path/to/binary> <binary_flags>
0074 ```
0075
0076 You can do the same thing with the tcmalloc library for memory profiling:
0077
0078 ```
0079 LD_PRELOAD="<path/to/libtcmalloc.so>" <other_options> <path/to/binary> <binary_flags>
0080 ```
0081
0082 Using the `LD_PRELOAD` method is not recommended by the developers of gperftools so using the build options is preferable. Both CPU and memory profiling can be enabled at the same time but note that turning on memory profiling (or the heap checker) will affect performance.
0083 Specify multiple libraries to load with `LD_PRELOAD` using a space-separated list e.g.
0084
0085 ```
0086 LD_PRELOAD="<path/to/first/library> <path/to/second/library>"
0087 ```
0088
0089 Note that these steps don't turn on profiling, they only enable it to work. The following section details how to turn it on.
0090
0091 ## Produce a CPU Profile
0092
0093 To turn on CPU profiling when running an executable define the `CPUPROFILE` environment variable when executing the program:
0094
0095 ```
0096 CPUPROFILE=<path/to/profile> <path/to/binary> [binary args]
0097 ```
0098
0099 This variable specifies where the profile will be written to.
0100 There are additional environment variables that modify the behaviour of the profiler.
0101 [Would you like to know more](https://github.com/gperftools/gperftools)?
0102
0103 ## Produce a Memory Profile
0104
0105 To turn on memory profiling use the following command:
0106
0107 ```
0108 HEAPPROFILE=<path/to/profile> <path/to/binary> [binary args]
0109 ```
0110
0111 ## Run the Heap Checker
0112
0113 To run the heap checker for checking for memory leaks run the following command:
0114
0115 ```
0116 PPROF_PATH=<path/to/pprof> HEAPCHECK=normal <path/to/binary> [binary args]
0117 ```
0118
0119 The CPU profiler, memory profiler and heap checker can be used in tandem.
0120
0121 ## Using pprof
0122
0123 ### View Profile as a Graph
0124
0125 A graphical representation of a profile can be produced using:
0126
0127 ```
0128 pprof -pdf <path/to/binary> <path/to/profile> > <path/to/pdf>
0129 ```
0130
0131 Where `path/to/binary` is the binary is used to produce the profile in the first place.
0132 Other output formats are available.
0133
0134 The following opens the graph in your web browser:
0135
0136 ```
0137 pprof -web <path/to/binary> <path/to/profile>
0138 ```
0139
0140 ### Interactive Mode
0141
0142 To launch pprof in interactive mode use the following command:
0143
0144 ```
0145 pprof <path/to/binary> <path/to/profile>
0146 ```
0147
0148 The following command will display the top x entries by the current sorting criteria:
0149
0150 ```
0151 top <number>
0152 ```
0153
0154 To view the statistics of a function line by line use:
0155
0156 ```
0157 list <nameOfFunction>
0158 ```
0159
0160 Various options can be specified to filter, sort and set the granularity of entries.
0161 There are also a number of other commands available.
0162 Read more about pprof [here](https://github.com/google/pprof).