|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +toc_group: debugging-and-diagnostics |
| 4 | +link_title: Linux Perf Profiler Support |
| 5 | +permalink: /reference-manual/native-image/debugging-and-diagnostics/perf-profiler/ |
| 6 | +--- |
| 7 | + |
| 8 | +# Linux Perf Profiler Support in Native Image |
| 9 | + |
| 10 | +The [`perf` profiler](https://perf.wiki.kernel.org/){:target="_blank"} is a performance analysis tool in Linux that enables you to collect and analyze various performance-related data such as CPU utilization, memory usage, and more. |
| 11 | +It is particularly useful for profiling and understanding the behavior of applications. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +Perf is a profiler tool for Linux systems. |
| 16 | +Most distributions come with `perf` pre-installed, but you can install it using your package manager if it is not available. |
| 17 | + |
| 18 | +To install `perf` on Oracle Linux/Red Hat/CentOS, run this command: |
| 19 | + |
| 20 | +```bash |
| 21 | +sudo yum install perf |
| 22 | +``` |
| 23 | + |
| 24 | +To install `perf` on Debian/Ubuntu, run the following commands one by one: |
| 25 | + |
| 26 | +```bash |
| 27 | +sudo apt update |
| 28 | +sudo apt install linux-tools-common linux-tools-generic |
| 29 | +``` |
| 30 | + |
| 31 | +After installing `perf`, backup the default values of the following options: |
| 32 | + |
| 33 | +```bash |
| 34 | +cat /proc/sys/kernel/perf_event_paranoid > perf_event_paranoid.backup |
| 35 | +cat /proc/sys/kernel/kptr_restrict > kptr_restrict.backup |
| 36 | +``` |
| 37 | + |
| 38 | +Then set them to the new desired values: |
| 39 | + |
| 40 | +```bash |
| 41 | +echo -1 > /proc/sys/kernel/perf_event_paranoid |
| 42 | +echo 0 > /proc/sys/kernel/kptr_restrict |
| 43 | +``` |
| 44 | + |
| 45 | +In the example above, `-1` and `0` are used as values, which are the least restrictive, so it is not recommended to use them in production code. |
| 46 | +You can customize these values according to your needs. |
| 47 | + |
| 48 | +_perf_event_paranoid_ has four different levels (values): |
| 49 | +- **-1**: Allow use of (almost) all events by all users. |
| 50 | +- **>=0**: Disallow `ftrace` function tracepoint by users without `CAP_SYS_ADMIN`. |
| 51 | +- **>=1**: Disallow CPU event access by users without `CAP_SYS_ADMIN`. |
| 52 | +- **>=2**: Disallow kernel profiling by users without `CAP_SYS_ADMIN`. |
| 53 | + |
| 54 | +_kptr_restrict_ has three different levels (values): |
| 55 | +- **0**: Kernel pointers are readable by all users. |
| 56 | +- **1**: Kernel pointers are only accessible to privileged users (those with the `CAP_SYS_ADMIN` capability). |
| 57 | +- **2**: Kernel pointers are hidden from all users. |
| 58 | + |
| 59 | +Once finished using `perf`, restore the original values: |
| 60 | + |
| 61 | +```bash |
| 62 | +cat perf_event_paranoid.backup > /proc/sys/kernel/perf_event_paranoid |
| 63 | +cat kptr_restrict.backup > /proc/sys/kernel/kptr_restrict |
| 64 | +``` |
| 65 | + |
| 66 | +## Building Native Executables |
| 67 | + |
| 68 | +The following command assumes that `native-image` is on the system path and available. |
| 69 | +If it is not installed, refer to the [Getting Started](README.md). |
| 70 | + |
| 71 | +```bash |
| 72 | +native-image -g <entry_class> |
| 73 | +``` |
| 74 | + |
| 75 | +The `-g` option instructs Native Image to produce debug information for the generated binary. |
| 76 | +`perf` can use this debug information, for example, to provide proper names for types and methods in traces. |
| 77 | + |
| 78 | +## Basic Operations |
| 79 | + |
| 80 | +### CPU Profiling |
| 81 | + |
| 82 | +1. List all available events: |
| 83 | + |
| 84 | + ```bash |
| 85 | + perf list |
| 86 | + ``` |
| 87 | + This command displays a list of all available events that you can use for profiling. |
| 88 | + |
| 89 | +2. Record CPU events: |
| 90 | + |
| 91 | + ```bash |
| 92 | + perf record -e <event> -o perf.data <your_executable> |
| 93 | + ``` |
| 94 | + |
| 95 | + Replace `<event>` with the desired event from the list. |
| 96 | + This command profiles your executable and save the data to a file named _perf.data_. |
| 97 | + |
| 98 | +3. Generate a report: |
| 99 | + |
| 100 | + ```bash |
| 101 | + perf report |
| 102 | + ``` |
| 103 | + |
| 104 | + This command generates a report based on the collected data. |
| 105 | + You can use various options to customize the output. |
| 106 | + |
| 107 | +### Memory Profiling |
| 108 | + |
| 109 | +1. Record memory events: |
| 110 | + |
| 111 | + ```bash |
| 112 | + perf record -e memory:<event> -o perf.data <your_executable> |
| 113 | + ``` |
| 114 | + |
| 115 | + Replace `<event>` with a specific memory event. |
| 116 | + This command profiles memory-related events. |
| 117 | + |
| 118 | +2. Generate a memory report: |
| 119 | + |
| 120 | + ```bash |
| 121 | + perf report --sort=dso |
| 122 | + ``` |
| 123 | + |
| 124 | + This command generates a report focused on memory-related events, sorted by dynamic shared object (DSO). |
| 125 | + |
| 126 | +### Tracing |
| 127 | + |
| 128 | +1. Record system-wide traces: |
| 129 | + |
| 130 | + ```bash |
| 131 | + sudo perf record -a -g -o perf.data |
| 132 | + ``` |
| 133 | + |
| 134 | + This command records system-wide traces, including call-graph information, and saves the data to a file named _perf.data_. |
| 135 | + Use sudo for system-wide tracing. |
| 136 | + |
| 137 | +2. Generate a trace report: |
| 138 | + ```bash |
| 139 | + perf script |
| 140 | + ``` |
| 141 | + |
| 142 | + This command generates a script that can be used for analyzing the recorded trace data. |
| 143 | + |
| 144 | +### Related Documentation |
| 145 | + |
| 146 | +* [Debug Information](DebugInfo.md) |
| 147 | +* [JDK Flight Recorder (JFR)](JFR.md) |
0 commit comments