Re: Profiling applications

From: Paul Floyd <paulf2718_at_gmail.com>
Date: Sat, 03 Feb 2024 07:20:55 UTC

On 03-02-24 01:16, Chuck Tuffli wrote:
> What is the recommended way of profiling applications on FreeBSD these 
> days? I’m looking for information like “28% of the execution time is 
> spent in function X”.
> 
> —chuck

First off, you will get best results with DWARF debuginfo. If you can, 
build your test exe with "-g -O3" or similar.

The first answer should be pmcstat. When I've used it, it didn't inspire 
a great amount of confidence. The wiki page 
https://wiki.freebsd.org/PmcTools hasn't been updated in 15 years, so 
I've no idea if it is still relevant. pmc and pmcstat are in base. No 
commits for the last 2 or 3 years, and low activity before that.

Using a non-debug kernel I get lots of

addr2line: dwarf_init: Debug info NULL [_dwarf_consumer_init(66)]
pmcstat: WARNING: addr2line function name read error
pmcstat: WARNING: addr2line pipe error

messages.

Still, I can get to see some measurements with it.

To see sampling data
0. pkg install kcachegrind
1. kldload hwpmc
2. profile: pmcstat -P inst_retired.any_p -O /tmp/pmctest.bin -d [your 
command and arguments]
3. convert to callgrind format: pmcstat -R/tmp/pmctest.bin -F/tmp/pmctest.cg
4. view results: kcachegrind /tmp/pmctest.cg

The next thing that I would suggest is Google perftools. You will need 
to install google-perftools. Roughly, you will need to link with their 
library (iirc LD_PRELOAD should also work), set some environment 
variables and then run your exe. You'll need to do some postprocessing 
similar to pmcstat. perftools are fairly well documented on the web.

Lastly, you could try cachegrind and callgrind. They are slow, do not 
accurately model the cache or branch prediction and not at all 
speculative execution. What they measure is accurate and as close to 
deterministic as you can get.


A+
Paul