Profiling¶
In this section you can find some tips on how to profile your ScalaNative binary in Linux.
Scala Native binaries are regular executables. Tools that works with native executables will work with ScalaNative executables. That includes the Linuxperf performance analysis tool.
Measuring execution time and memory¶
With the
timecommand you can measure execution time:
time./target/scala-2.13/scala-native-outreal0m0,718suser0m0,419ssys0m0,299s
With the
/usr/bin/time--verbosecommand you can also see memoryconsumption:
/usr/bin/time--verbose./target/scala-2.13/scala-native-out
Commandbeingtimed:"./target/scala-2.13/scala-native-out"Usertime(seconds):0.49Systemtime(seconds):0.23PercentofCPUthisjobgot:99%Elapsed(wallclock)time(h:mm:ssorm:ss):0:00.72Averagesharedtextsize(kbytes):0Averageunshareddatasize(kbytes):0Averagestacksize(kbytes):0Averagetotalsize(kbytes):0Maximumresidentsetsize(kbytes):1317184Averageresidentsetsize(kbytes):0Major(requiringI/O)pagefaults:0Minor(reclaimingaframe)pagefaults:328341Voluntarycontextswitches:1Involuntarycontextswitches:70Swaps:0Filesysteminputs:0Filesystemoutputs:0Socketmessagessent:0Socketmessagesreceived:0Signalsdelivered:0Pagesize(bytes):4096Exitstatus:0
Creating Flamegraphs¶
Aflamegraph is avisualization of the most frequent code-paths of a program. You can useflamegraphs to see where your program spends most of its CPU time.
Use samply¶
samply is a command line CPU profiler which uses the Firefox profiler asits UI. Samply has support for de-mangling Scala Native symbols.
Use hotspot¶
hotspot is a GUI forperf which provides a flamegraph view. As of thiswriting, hotspot does not de-mangle Scala Native symbols.
Usingperf andFlameGraph¶
Follow these steps:
You need to install the
perfcommand if you haven’t got italready:
sudoaptupdate&&sudoaptinstalllinux-tools-genericThen clone the flamegraph repository into e.g.
~/git/hub/
cd~&&mkdir-pgit/hub&&cdgit/hub/gitclonegit@github.com:brendangregg/FlameGraph.git
Then navigate to your Scala Native project and, after building yourbinary, you can create a flamegraph like so:
sudoperfrecord-F1000-a-g./target/scala-2.13/scala-native-outsudoperfscript>out.perf~/git/hub/FlameGraph/stackcollapse-perf.plout.perf>out.folded~/git/hub/FlameGraph/flamegraph.plout.folded>kernel.svgOpen the file
kernel.svgin your browser and you can zoom in theinteractive SVG-file by clicking on the colored boxes as explainedhere.A box represents a stack frame. The broader a box is the more CPUcycles have been spent. The higher the box is, the deeper in thecall-chain it is.The perf option
-F1000means that the sampling frequency is setto 1000 Hz. You can experiment with changing this option to get theright accuracy; start with e.g.-F99and see what you get. Youcan then increase the sampling frequency to see if more details addsinteresting information.
Continue toruntime.
