Profiling Erlang Code with fprof

Rajat Gupta
#FML
Published in
3 min readFeb 25, 2019

This is the second article in the troubleshooting. In this article, we will look into the performance analysis for the applications. When you are developing the code, performance is very important.

To scale the application, to maintain the reliability of the application, you need to analyze the performance of your code and need to profile your code.

We will look into profiling the Erlang application using fprof and how to analyze its output. frpof is the tool provided with the Erlang, so you need not to install it separately.

What is fprof?

It provides the most detailed information about where the program time is spent, but it significantly slows down the program it profiles. It is a standard tool to profile the code.

To know more about it, you can look into the documentation.

Now, we will log in to Erlang Remote shell and start doing:

1> fprof:start().
2> fprof:trace([start, {procs, all}]).
3> fprof:trace([stop]).
4> fprof:profile().
5> fprof:analyse([totals, {dest, "/tmp/application.analysis"}]).
6> fprof:stop()

These steps will generate an analysis file in /tmp/application.analysis which contains the result of the analysis of your application.

How to read the output?

Now, we will look into how to read this analysis file.

fprof output

The file will look like this. The fields are:

  • CNT: Total number of count of functions
  • ACC: Accumulated Time, including called functions.
  • OWN: Own Time, how much time a function has used for its own execution

I can see, archiver_writer:io_loop/1 is taking a lot of OWN time which means it is not optimized. I saw and it was writing data on a file in a loop which is not optimized way for file IO. I will improve this function and increase the throughput of my application.

Visualizing the output

You can visualize the output of the file by erlgrind and can be used to visualize fprof data in kcachegrind.

Install kcachegrind on mac by:

brew install qcachegrind

Now, convert the output of the fprof to callgrind format by:

$ cd erlgrind/src
$ ./erlgrind_script application.analysis application.cgrind

Now you can open the file in the qcachegrind app in mac. The visualization will look like:

You can get this kind of detailed analysis of the application with graphs.

P.S. This is one way to profile the erlang app. There are other ways to profile the Erlang application, which we will cover later.

--

--