<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2002</year>
      <year>2020</year>
      <holder>Ericsson AB, All Rights Reserved</holder>
    </copyright>
    <legalnotice>
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  The Initial Developer of the Original Code is Ericsson AB.
    </legalnotice>

    <title>cprof</title>
    <prepared>Raimo Niskanen</prepared>
    <responsible>nobody</responsible>
    <docno></docno>
    <approved>nobody</approved>
    <checked></checked>
    <date>2002-09-12</date>
    <rev>PA1</rev>
    <file>cprof.sgml</file>
  </header>
  <module since="">cprof</module>
  <modulesummary>A simple Call Count Profiling Tool using breakpoints for minimal runtime performance impact.</modulesummary>
  <description>
    <p>The <c>cprof</c> module is used to profile a program
      to find out how many times different functions are called.
      Breakpoints similar to local call trace, but containing a
      counter, are used to minimise runtime performance impact. 
      </p>
    <p>Since breakpoints are used there is no need for special
      compilation of any module to be profiled. For now these
      breakpoints can only be set on BEAM code so BIFs
      cannot be call count traced.
      </p>
    <p>The size of the call counters is the host machine word
      size. One bit is used when pausing the counter, so the maximum
      counter value for a 32-bit host is 2147483647.
      </p>
    <p>The profiling result is delivered as a term containing a
      sorted list of entries, one per module. Each module entry
      contains a sorted list of functions. The sorting order in both
      cases is of decreasing call count.
      </p>
    <p>Call count tracing is very lightweight compared to other forms
      of tracing since no trace message has to be generated. Some 
      measurements indicates performance degradation in the vicinity
      of 10 percent.
      </p>
  </description>
  <funcs>
    <func>
      <name name="analyse" arity="0" since=""></name>
      <name name="analyse" arity="1" clause_i="1" since=""></name>
      <name name="analyse" arity="1" clause_i="2" since=""></name>
      <name name="analyse" arity="2" since=""></name>
      <fsummary>Collect and analyse call counters.</fsummary>
      <type name="mod_analysis_list"/>
      <type name="mod_analysis"/>
      <type name="func_analysis_list"/>
      <desc>
        <p>Collects and analyses the call counters presently in the
          node for either module <c><anno>Mod</anno></c>, or for all modules
          (except <c>cprof</c> itself), and returns: </p>
        <taglist>
          <tag><c><anno>FuncAnalysisList</anno></c></tag>
          <item>A list of tuples, one for each function in a module, in
           decreasing <c><anno>FuncCallCount</anno></c> order.</item>
          <tag><c><anno>ModCallCount</anno></c></tag>
          <item>The sum of <c><anno>FuncCallCount</anno></c> values for all
           functions in module <c><anno>Mod</anno></c>.</item>
          <tag><c><anno>AllCallCount</anno></c></tag>
          <item>The sum of <c><anno>ModCallCount</anno></c> values for all modules
           concerned in <c><anno>ModAnalysisList</anno></c>.</item>
          <tag><c><anno>ModAnalysisList</anno></c></tag>
          <item>A list of tuples, one for each module except
          <c>cprof</c>, in decreasing <c><anno>ModCallCount</anno></c> order.</item>
        </taglist>
        <p>If call counters are still running while
          <c>analyse/0..2</c> is executing, you might get an
          inconsistent result. This happens if the process executing
          <c>analyse/0..2</c> gets scheduled out so some other process
          can increment the counters that are being analysed, Calling
          <seemfa marker="#pause/0"><c>pause()</c></seemfa>
          before analysing takes care of the problem.
          </p>
        <p>If the <c><anno>Mod</anno></c> argument is given, the
          result contains a <c><anno>ModAnalysis</anno></c> tuple for
          module <c><anno>Mod</anno></c> only, otherwise the result
          contains one <c><anno>ModAnalysis</anno></c> tuple for all
          modules returned from <seemfa
          marker="kernel:code#all_loaded/0">
          <c>code:all_loaded()</c></seemfa> except <c>cprof</c> itself.
          </p>
        <p>All functions with a <c><anno>FuncCallCount</anno></c>
          lower than <c><anno>Limit</anno></c> are excluded from
          <c><anno>FuncAnalysisList</anno></c>. They are still included
          in <c><anno>ModCallCount</anno></c>, though. The default value
          for <c><anno>Limit</anno></c> is <c>1</c>.</p>
      </desc>
    </func>
    <func>
      <name name="pause" arity="0" since=""></name>
      <fsummary>Pause running call count trace for all functions.</fsummary>
      <desc>
        <p>Pause call count tracing for all functions in all modules
          and stop it for all functions in modules to be
          loaded. This is the same as 
          <c>(pause({'_','_','_'})+stop({on_load}))</c>.
          </p>
        <p>See also 
          <seemfa marker="#pause/1"><c>pause/1..3</c></seemfa> below.
          </p>
      </desc>
    </func>
    <func>
      <name name="pause" arity="1" since=""></name>
      <name name="pause" arity="2" since=""></name>
      <name name="pause" arity="3" since=""></name>
      <fsummary>Pause running call count trace for matching functions.</fsummary>
      <desc>

        <p>Pause call counters for matching functions in matching
          modules. The <c><anno>FS</anno></c> argument can be used to
          specify the first argument to <seemfa
          marker="erts:erlang#trace_pattern/3"><c>erlang:trace_pattern/3</c></seemfa>.
          </p>
        <p>The call counters for all matching functions that
          has got call count breakpoints are paused at their current
          count. 
          </p>
        <p>Return the number of matching functions that can have
          call count breakpoints, the same as
          <seemfa marker="#start/0"><c>start/0..3</c></seemfa>
          with the same arguments would have returned.
          </p>
      </desc>
    </func>
    <func>
      <name name="restart" arity="0" since=""></name>
      <name name="restart" arity="1" since=""></name>
      <name name="restart" arity="2" since=""></name>
      <name name="restart" arity="3" since=""></name>
      <fsummary>Restart existing call counters for matching functions.</fsummary>
      <desc>
        <p>Restart call counters for the matching functions in
          matching modules that are call count traced. The <c><anno>FS</anno></c>
          argument can be used to specify the first argument to <seemfa
          marker="erts:erlang#trace_pattern/3"><c>erlang:trace_pattern/3</c></seemfa>.
          </p>
        <p>The call counters for all matching functions that has got
          call count breakpoints are set to zero and running.
          </p>
        <p>Return the number of matching functions that can have
          call count breakpoints, the same as
          <seemfa marker="#start/0"><c>start/0..3</c></seemfa>
          with the same arguments would have returned. 
          </p>
      </desc>
    </func>
    <func>
      <name name="start" arity="0" since=""></name>
      <fsummary>Start call count tracing for all functions.</fsummary>
      <desc>
        <p>Start call count tracing for all functions in all modules, 
          and also for all functions in modules to be
          loaded. This is the same as 
          <c>(start({'_','_','_'})+start({on_load}))</c>.
          </p>
        <p>See also 
          <seemfa marker="#start/1"><c>start/1..3</c></seemfa> below.
          </p>
      </desc>
    </func>
    <func>
      <name name="start" arity="1" since=""></name>
      <name name="start" arity="2" since=""></name>
      <name name="start" arity="3" since=""></name>
      <fsummary>Start call count tracing for matching functions.</fsummary>
      <desc>
        <p>Start call count tracing for matching functions in matching 
          modules. The <c><anno>FS</anno></c> argument can be used to specify the
          first argument to <seemfa
          marker="erts:erlang#trace_pattern/3"><c>erlang:trace_pattern/3</c></seemfa>,
          for example <c>on_load</c>.
          </p>
        <p>Set call count breakpoints on the matching functions that
          has no call count breakpoints. Call counters 
          are set to zero and running for all matching functions.
          </p>
        <p>Return the number of matching functions that has got
          call count breakpoints.
          </p>
      </desc>
    </func>
    <func>
      <name name="stop" arity="0" since=""></name>
      <fsummary>Stop call count tracing for all functions.</fsummary>
      <desc>
        <p>Stop call count tracing for all functions in all modules, 
          and also for all functions in modules to be
          loaded. This is the same as 
          <c>(stop({'_','_','_'})+stop({on_load}))</c>.
          </p>
        <p>See also 
          <seemfa marker="#stop/1"><c>stop/1..3</c></seemfa> below.
          </p>
      </desc>
    </func>
    <func>
      <name name="stop" arity="1" since=""></name>
      <name name="stop" arity="2" since=""></name>
      <name name="stop" arity="3" since=""></name>
      <fsummary>Stop call count tracing for matching functions.</fsummary>
      <desc>
        <p>Stop call count tracing for matching functions in matching 
          modules. The <c><anno>FS</anno></c> argument can be used to specify the
          first argument to <seemfa
          marker="erts:erlang#trace_pattern/3"><c>erlang:trace_pattern/3</c></seemfa>,
          for example <c>on_load</c>.
          </p>
        <p>Remove call count breakpoints from the matching functions that
          has call count breakpoints.
          </p>
        <p>Return the number of matching functions that can have
          call count breakpoints, the same as
          <seemfa marker="#start/0"><c>start/0..3</c></seemfa>
          with the same arguments would have returned. 
          </p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>See Also</title>
    <p><seeerl marker="eprof">eprof</seeerl>(3), 
      <seeerl marker="fprof">fprof</seeerl>(3), 
      erlang(3), 
      <seeguide marker="cprof_chapter">User's Guide</seeguide></p>
  </section>
</erlref>

