concept fprof in category erlang

This is an excerpt from Manning's book Erlang and OTP in Action.
Profiling your code using cprof and fprof
The fprof profiler is perhaps the most valuable of the standard profiling tools. It gives you a wealth of useful information in a digestible format, and it’s easy to use. It’s one of the first tools to bring in when you need to diagnose performance issues. It supersedes the older eprof (which is still included in the distribution but is much less efficient). Both fprof and eprof are built on top of Erlang’s tracing functionality, so neither requires any special compilation of the code or additional debugging information. They have much higher overhead than cprof and can make the code run up to 10 times slower, so they should be used with some care if you need to profile a system in production.
fprof depends on dbg, which is part of the runtime_tools application (described in the Tools section of the Erlang/OTP documentation). This must be present on your system in order to use fprof. runtime_tools is included in the standard Erlang/OTP distribution, but if you’ve installed Erlang via a package manager, you may need to check that you also have this package. The fprof module, together with cprof, cover, instrument, and a few others, belong to the unimaginatively named tools application.
Running fprof is similar to running cprof. The following dialogue demonstrates how you can use fprof to analyze the profile_ex module from the previous section:
![]()
Instead of generating a term that you can inspect directly in the shell, this creates a file called profile.txt in your current directory. First, fprof is told to start tracing
. The tracing information is by default written to a file named fprof.trace, which is in a binary format, not fit for human consumption. You call profile_ex:run() as before, and after a moment or two, you stop the tracing again. Next, you must call fprof:profile()
to take the trace accumulated in the file and turn it into raw profiling data. This compiled data is kept in RAM by the fprof server. You can then analyze the data by calling fprof:analyse/1
, in this example telling it to output the results (in human readable format) to a file named profile.txt.
Several variations on how to start a trace and produce the final output exist, and we leave it as an exercise for you to study the documentation for fprof. It’s also important to be aware of the amount of code you’re actively profiling. Unlike cprof, fprof can accumulate data quickly, and if you profile large chunks of code over an extended period of time, fprof can generate gigabytes of profiling information.
. The tracing information is by default written to a file named fprof.trace, which is in a binary format, not fit for human consumption. You call profile_ex:run() as before, and after a moment or two, you stop the tracing again. Next, you must call fprof:profile()
to take the trace accumulated in the file and turn it into raw profiling data. This compiled data is kept in RAM by the fprof server. You can then analyze the data by calling fprof:analyse/1
, in this example telling it to output the results (in human readable format) to a file named profile.txt.