Skip to content

Profiling Linux Builds with Perf

Beherith edited this page Jun 5, 2023 · 6 revisions

For profiling running instances spring-dedicated and spring-headless

If you want to profile spring-dedicated, while it is running, there is one good way of doing so, using linux perf tools, which can be installed on most ubuntu/debian based systems easily.

Preparing for a profile capture

Find out the PID of the spring-dedicated process you wish to attach to. The process seems to have two threads, with approximately equal shares of CPU usage. Find it with

ps aux | grep spring

Remember the process ID of the main thread (generally the smaller one)

Get the debug symbols

  • Create a working directory for where you will store your captures.
  • Download the debug symbols from the releases page here on github (take care to select linux, and the correct version too).
  • Unpack the debug symbols, and make sure the symbols (e.g. spring-dedicated.dbg) are unzipped into your working directory.

Starting the profiler

Often you will need sudo permissions to perform this capture. perf will warn you otherwise.

Example command:

sudo perf record --pid=3140530 --freq=1000 -o spring-dedicated_6cg_lag1.perfoutput --stat -g --call-graph dwarf

Arguments:

  • --pid= the process id for spring-dedicated, can pass multiple pids comma separated list
  • --freq= the number of samples per second to take
  • -o the name of the output file, if you dont name it, it will overwrite previous captures
  • -g enable call graphs (this is implied by below)
  • --stat Record per-thread event counts. Use it with perf report -T to
  • --call-graph dwarf : the format of the call graphs. I am unsure if 'fp' or 'dwarf' is the correct argument here, but it seems like dwarf is better.

               When "dwarf" recording is used, perf also records (user) stack dump
               when sampled.  Default size of the stack dump is 8192 (bytes).
               User can change the size by passing the size after comma like
               "--call-graph dwarf,4096".
               When "fp" recording is used, perf tries to save stack enties
               up to the number specified in sysctl.kernel.perf_event_max_stack
               by default.  User can change the number by passing it after comma
               like "--call-graph fp,32".

Stop the profiler:

Press CTRL+C to stop the profiler, or send it a SIGTERM signal from code, if you are doing this automatically

Displaying the results in a browsable format:

sudo perf report -i spring-dedicated_6cg_lag1.perfoutput

Use the '+' key on your keyboard to expand call graphs within perf

Drawing a full-on call graph:

sudo perf report -T -i spring-dedicated_6cg_lag1.perfoutput