|
| 1 | +--- |
| 2 | +linkTitle: Profiling builds |
| 3 | +title: Profiling builds with pprof |
| 4 | +description: | |
| 5 | + Learn how you can use `pprof` to analyze and debug your builds. |
| 6 | +--- |
| 7 | + |
| 8 | +{{< introduced buildx 0.18.0 >}} |
| 9 | + |
| 10 | +You can configure Buildx to generate [`pprof`](https://github.com/google/pprof) |
| 11 | +memory and CPU profiles to analyze and optimize your builds. These profiles |
| 12 | +help you identify performance bottlenecks, detect memory inefficiencies, and |
| 13 | +ensure your builds run efficiently. |
| 14 | + |
| 15 | +`pprof` is a profiling tool that captures and visualizes detailed data on CPU |
| 16 | +and memory usage in Go programs. It helps solve common performance problems by |
| 17 | +allowing developers to understand which parts of their application are |
| 18 | +consuming the most resources, whether CPU or memory. |
| 19 | + |
| 20 | +In the context of Buildx and BuildKit, CPU profiles show where processing time |
| 21 | +is spent during the build, helping to optimize slow builds. Memory profiles |
| 22 | +track where memory is allocated, allowing you to spot inefficiencies, memory |
| 23 | +leaks, or areas that require optimization. By generating these profiles, you |
| 24 | +can focus on making your builds faster and more resource-efficient. |
| 25 | + |
| 26 | +## Generate profiling data |
| 27 | + |
| 28 | +The following environment variables control whether Buildx generates profiling |
| 29 | +data for builds: |
| 30 | + |
| 31 | +- [`BUILDX_CPU_PROFILE`](/manuals/build/building/variables.md#buildxcpuprofile) |
| 32 | +- [`BUILDX_MEM_PROFILE`](/manuals/build/building/variables.md#buildxmemprofile) |
| 33 | + |
| 34 | +When set, Buildx emits profiling samples for the builds to the location |
| 35 | +specified by the environment variable. |
| 36 | + |
| 37 | +## How to analyze profiling samples |
| 38 | + |
| 39 | +To analyze and visualize profiling samples, you need `pprof` from the Go |
| 40 | +toolchain. The following example shows how to run `pprof` in a container. If |
| 41 | +you prefer to run `pprof` directly on your system, you need to install the Go |
| 42 | +toolchain and (optionally) GraphViz for visualization. |
| 43 | + |
| 44 | +1. Start a `golang` container named `pprof` in the background which publishes |
| 45 | + port 8081 (or any other available port) to the host. |
| 46 | + |
| 47 | + ```console |
| 48 | + $ docker run --rm --name pprof -w /profiles -p 8081:8081 -dt golang:alpine |
| 49 | + |
| 50 | +2. Install GraphViz in the container. |
| 51 | + |
| 52 | + ```console |
| 53 | + $ docker exec pprof apk add --no-cache graphviz |
| 54 | + ``` |
| 55 | + |
| 56 | +3. Execute your build as usual with the desired [environment |
| 57 | + variables](#generate-profiling-data) set. |
| 58 | + |
| 59 | + ```console |
| 60 | + $ BUILDX_CPU_PROFILE=cpu.prof docker build . |
| 61 | + ``` |
| 62 | + |
| 63 | +4. Copy the profiling sample into the `pprof` container. |
| 64 | + |
| 65 | + ```console |
| 66 | + $ docker cp cpu.prof pprof:/profiles |
| 67 | + ``` |
| 68 | + |
| 69 | +5. Run `pprof` with the sample. |
| 70 | + |
| 71 | + ```console |
| 72 | + $ docker exec -it pprof go tool pprof cpu.prof |
| 73 | + ``` |
| 74 | + |
| 75 | + This opens the `pprof` interactive console. From here, you can inspect the |
| 76 | + profiling sample using various commands. For example, use `top 10` command |
| 77 | + to view the top 10 most time-consuming entries. |
| 78 | + |
| 79 | + ```plaintext |
| 80 | + (pprof) top 10 |
| 81 | + Showing nodes accounting for 3.04s, 91.02% of 3.34s total |
| 82 | + Dropped 123 nodes (cum <= 0.02s) |
| 83 | + Showing top 10 nodes out of 159 |
| 84 | + flat flat% sum% cum cum% |
| 85 | + 1.14s 34.13% 34.13% 1.14s 34.13% syscall.syscall |
| 86 | + 0.91s 27.25% 61.38% 0.91s 27.25% runtime.kevent |
| 87 | + 0.35s 10.48% 71.86% 0.35s 10.48% runtime.pthread_cond_wait |
| 88 | + 0.22s 6.59% 78.44% 0.22s 6.59% runtime.pthread_cond_signal |
| 89 | + 0.15s 4.49% 82.93% 0.15s 4.49% runtime.usleep |
| 90 | + 0.10s 2.99% 85.93% 0.10s 2.99% runtime.memclrNoHeapPointers |
| 91 | + 0.10s 2.99% 88.92% 0.10s 2.99% runtime.memmove |
| 92 | + 0.03s 0.9% 89.82% 0.03s 0.9% runtime.madvise |
| 93 | + 0.02s 0.6% 90.42% 0.02s 0.6% runtime.(*mspan).typePointersOfUnchecked |
| 94 | + 0.02s 0.6% 91.02% 0.02s 0.6% runtime.pcvalue |
| 95 | + ``` |
| 96 | + |
| 97 | +6. To view the call graph in a graphical UI, run `go tool pprof |
| 98 | + -http=0.0.0.0:8081 <sample.prof>` in the container. |
| 99 | + |
| 100 | + ```console |
| 101 | + $ docker exec -it pprof |
| 102 | + /profiles # go tool pprof -http=0.0.0.0:8081 cpu.prof |
| 103 | + Serving web UI on http://0.0.0.0:8081 |
| 104 | + http://0.0.0.0:8081 |
| 105 | + ``` |
| 106 | + |
| 107 | +For more information about using `pprof` and how to interpret the call graph, |
| 108 | +refer to the [`pprof` README](https://github.com/google/pprof/blob/main/doc/README.md). |
0 commit comments