<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html xmlns:fn="http://www.w3.org/2005/02/xpath-functions"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="../../../../doc/otp_doc.css" type="text/css"> <title>Erlang -- fprof</title> </head> <body bgcolor="white" text="#000000" link="#0000ff" vlink="#ff00ff" alink="#ff0000"><div id="container"> <script id="js" type="text/javascript" language="JavaScript" src="../../../../doc/js/flipmenu/flipmenu.js"></script><script id="js2" type="text/javascript" src="../../../../doc/js/erlresolvelinks.js"></script><script language="JavaScript" type="text/javascript"> <!-- function getWinHeight() { var myHeight = 0; if( typeof( window.innerHeight ) == 'number' ) { //Non-IE myHeight = window.innerHeight; } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode' myHeight = document.documentElement.clientHeight; } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible myHeight = document.body.clientHeight; } return myHeight; } function setscrollpos() { var objf=document.getElementById('loadscrollpos'); document.getElementById("leftnav").scrollTop = objf.offsetTop - getWinHeight()/2; } function addEvent(obj, evType, fn){ if (obj.addEventListener){ obj.addEventListener(evType, fn, true); return true; } else if (obj.attachEvent){ var r = obj.attachEvent("on"+evType, fn); return r; } else { return false; } } addEvent(window, 'load', setscrollpos); //--></script><div id="leftnav"><div class="innertube"> <img alt="Erlang logo" src="../../../../doc/erlang-logo.png"><br><small><a href="users_guide.html">User's Guide</a><br><a href="index.html">Reference Manual</a><br><a href="release_notes.html">Release Notes</a><br><a href="../pdf/tools-2.6.5.1.pdf">PDF</a><br><a href="../../../../doc/index.html">Top</a></small><p><strong>Tools</strong><br><strong>Reference Manual</strong><br><small>Version 2.6.5.1</small></p> <br><a href="javascript:openAllFlips()">Expand All</a><br><a href="javascript:closeAllFlips()">Contract All</a><p><small><strong>Table of Contents</strong></small></p> <ul class="flipMenu"> <li id="no" title="cover " expanded="false">cover<ul> <li><a href="cover.html"> Top of manual page </a></li> <li title="start-0"><a href="cover.html#start-0">start/0</a></li> <li title="start-1"><a href="cover.html#start-1">start/1</a></li> <li title="compile-1"><a href="cover.html#compile-1">compile/1</a></li> <li title="compile-2"><a href="cover.html#compile-2">compile/2</a></li> <li title="compile_module-1"><a href="cover.html#compile_module-1">compile_module/1</a></li> <li title="compile_module-2"><a href="cover.html#compile_module-2">compile_module/2</a></li> <li title="compile_directory-0"><a href="cover.html#compile_directory-0">compile_directory/0</a></li> <li title="compile_directory-1"><a href="cover.html#compile_directory-1">compile_directory/1</a></li> <li title="compile_directory-2"><a href="cover.html#compile_directory-2">compile_directory/2</a></li> <li title="compile_beam-1"><a href="cover.html#compile_beam-1">compile_beam/1</a></li> <li title="compile_beam_directory-0"><a href="cover.html#compile_beam_directory-0">compile_beam_directory/0</a></li> <li title="compile_beam_directory-1"><a href="cover.html#compile_beam_directory-1">compile_beam_directory/1</a></li> <li title="analyse-1"><a href="cover.html#analyse-1">analyse/1</a></li> <li title="analyse-2"><a href="cover.html#analyse-2">analyse/2</a></li> <li title="analyse-2"><a href="cover.html#analyse-2">analyse/2</a></li> <li title="analyse-3"><a href="cover.html#analyse-3">analyse/3</a></li> <li title="analyse_to_file-1"><a href="cover.html#analyse_to_file-1">analyse_to_file/1</a></li> <li title="analyse_to_file-2"><a href="cover.html#analyse_to_file-2">analyse_to_file/2</a></li> <li title="analyse_to_file-2"><a href="cover.html#analyse_to_file-2">analyse_to_file/2</a></li> <li title="analyse_to_file-3"><a href="cover.html#analyse_to_file-3">analyse_to_file/3</a></li> <li title="modules-0"><a href="cover.html#modules-0">modules/0</a></li> <li title="imported_modules-0"><a href="cover.html#imported_modules-0">imported_modules/0</a></li> <li title="imported-0"><a href="cover.html#imported-0">imported/0</a></li> <li title="which_nodes-0"><a href="cover.html#which_nodes-0">which_nodes/0</a></li> <li title="is_compiled-1"><a href="cover.html#is_compiled-1">is_compiled/1</a></li> <li title="reset-1"><a href="cover.html#reset-1">reset/1</a></li> <li title="reset-0"><a href="cover.html#reset-0">reset/0</a></li> <li title="export-1"><a href="cover.html#export-1">export/1</a></li> <li title="export-2"><a href="cover.html#export-2">export/2</a></li> <li title="import-1"><a href="cover.html#import-1">import/1</a></li> <li title="stop-0"><a href="cover.html#stop-0">stop/0</a></li> <li title="stop-1"><a href="cover.html#stop-1">stop/1</a></li> </ul> </li> <li id="no" title="cprof " expanded="false">cprof<ul> <li><a href="cprof.html"> Top of manual page </a></li> <li title="analyse-0"><a href="cprof.html#analyse-0">analyse/0</a></li> <li title="analyse-1"><a href="cprof.html#analyse-1">analyse/1</a></li> <li title="analyse-1"><a href="cprof.html#analyse-1">analyse/1</a></li> <li title="analyse-2"><a href="cprof.html#analyse-2">analyse/2</a></li> <li title="pause-0"><a href="cprof.html#pause-0">pause/0</a></li> <li title="pause-1"><a href="cprof.html#pause-1">pause/1</a></li> <li title="pause-2"><a href="cprof.html#pause-2">pause/2</a></li> <li title="pause-3"><a href="cprof.html#pause-3">pause/3</a></li> <li title="restart-0"><a href="cprof.html#restart-0">restart/0</a></li> <li title="restart-1"><a href="cprof.html#restart-1">restart/1</a></li> <li title="restart-2"><a href="cprof.html#restart-2">restart/2</a></li> <li title="restart-3"><a href="cprof.html#restart-3">restart/3</a></li> <li title="start-0"><a href="cprof.html#start-0">start/0</a></li> <li title="start-1"><a href="cprof.html#start-1">start/1</a></li> <li title="start-2"><a href="cprof.html#start-2">start/2</a></li> <li title="start-3"><a href="cprof.html#start-3">start/3</a></li> <li title="stop-0"><a href="cprof.html#stop-0">stop/0</a></li> <li title="stop-1"><a href="cprof.html#stop-1">stop/1</a></li> <li title="stop-2"><a href="cprof.html#stop-2">stop/2</a></li> <li title="stop-3"><a href="cprof.html#stop-3">stop/3</a></li> </ul> </li> <li id="no" title="eprof " expanded="false">eprof<ul> <li><a href="eprof.html"> Top of manual page </a></li> <li title="start-0"><a href="eprof.html#start-0">start/0</a></li> <li title="start_profiling-1"><a href="eprof.html#start_profiling-1">start_profiling/1</a></li> <li title="profile-1"><a href="eprof.html#profile-1">profile/1</a></li> <li title="stop_profiling-0"><a href="eprof.html#stop_profiling-0">stop_profiling/0</a></li> <li title="profile-2"><a href="eprof.html#profile-2">profile/2</a></li> <li title="profile-4"><a href="eprof.html#profile-4">profile/4</a></li> <li title="analyse-0"><a href="eprof.html#analyse-0">analyse/0</a></li> <li title="total_analyse-0"><a href="eprof.html#total_analyse-0">total_analyse/0</a></li> <li title="log-1"><a href="eprof.html#log-1">log/1</a></li> <li title="stop-0"><a href="eprof.html#stop-0">stop/0</a></li> </ul> </li> <li title="erlang.el"><a href="erlang.el.html">erlang.el</a></li> <li id="loadscrollpos" title="fprof " expanded="true">fprof<ul> <li><a href="fprof.html"> Top of manual page </a></li> <li title="start-0"><a href="fprof.html#start-0">start/0</a></li> <li title="stop-0"><a href="fprof.html#stop-0">stop/0</a></li> <li title="stop-1"><a href="fprof.html#stop-1">stop/1</a></li> <li title="apply-2"><a href="fprof.html#apply-2">apply/2</a></li> <li title="apply-3"><a href="fprof.html#apply-3">apply/3</a></li> <li title="apply-3"><a href="fprof.html#apply-3">apply/3</a></li> <li title="apply-4"><a href="fprof.html#apply-4">apply/4</a></li> <li title="trace-2"><a href="fprof.html#trace-2">trace/2</a></li> <li title="trace-2"><a href="fprof.html#trace-2">trace/2</a></li> <li title="trace-2"><a href="fprof.html#trace-2">trace/2</a></li> <li title="trace-1"><a href="fprof.html#trace-1">trace/1</a></li> <li title="trace-1"><a href="fprof.html#trace-1">trace/1</a></li> <li title="trace-1"><a href="fprof.html#trace-1">trace/1</a></li> <li title="trace-1"><a href="fprof.html#trace-1">trace/1</a></li> <li title="profile-0"><a href="fprof.html#profile-0">profile/0</a></li> <li title="profile-2"><a href="fprof.html#profile-2">profile/2</a></li> <li title="profile-1"><a href="fprof.html#profile-1">profile/1</a></li> <li title="profile-1"><a href="fprof.html#profile-1">profile/1</a></li> <li title="profile-1"><a href="fprof.html#profile-1">profile/1</a></li> <li title="analyse-0"><a href="fprof.html#analyse-0">analyse/0</a></li> <li title="analyse-2"><a href="fprof.html#analyse-2">analyse/2</a></li> <li title="analyse-1"><a href="fprof.html#analyse-1">analyse/1</a></li> <li title="analyse-1"><a href="fprof.html#analyse-1">analyse/1</a></li> <li title="analyse-1"><a href="fprof.html#analyse-1">analyse/1</a></li> </ul> </li> <li id="no" title="instrument " expanded="false">instrument<ul> <li><a href="instrument.html"> Top of manual page </a></li> <li title="allocator_descr-2"><a href="instrument.html#allocator_descr-2">allocator_descr/2</a></li> <li title="block_header_size-1"><a href="instrument.html#block_header_size-1">block_header_size/1</a></li> <li title="class_descr-2"><a href="instrument.html#class_descr-2">class_descr/2</a></li> <li title="descr-1"><a href="instrument.html#descr-1">descr/1</a></li> <li title="holes-1"><a href="instrument.html#holes-1">holes/1</a></li> <li title="mem_limits-1"><a href="instrument.html#mem_limits-1">mem_limits/1</a></li> <li title="memory_data-0"><a href="instrument.html#memory_data-0">memory_data/0</a></li> <li title="memory_status-1"><a href="instrument.html#memory_status-1">memory_status/1</a></li> <li title="read_memory_data-1"><a href="instrument.html#read_memory_data-1">read_memory_data/1</a></li> <li title="read_memory_status-1"><a href="instrument.html#read_memory_status-1">read_memory_status/1</a></li> <li title="sort-1"><a href="instrument.html#sort-1">sort/1</a></li> <li title="store_memory_data-1"><a href="instrument.html#store_memory_data-1">store_memory_data/1</a></li> <li title="store_memory_status-1"><a href="instrument.html#store_memory_status-1">store_memory_status/1</a></li> <li title="sum_blocks-1"><a href="instrument.html#sum_blocks-1">sum_blocks/1</a></li> <li title="type_descr-2"><a href="instrument.html#type_descr-2">type_descr/2</a></li> <li title="type_no_range-1"><a href="instrument.html#type_no_range-1">type_no_range/1</a></li> </ul> </li> <li id="no" title="lcnt " expanded="false">lcnt<ul> <li><a href="lcnt.html"> Top of manual page </a></li> <li title="start-0"><a href="lcnt.html#start-0">start/0</a></li> <li title="stop-0"><a href="lcnt.html#stop-0">stop/0</a></li> <li title="collect-0"><a href="lcnt.html#collect-0">collect/0</a></li> <li title="collect-1"><a href="lcnt.html#collect-1">collect/1</a></li> <li title="clear-0"><a href="lcnt.html#clear-0">clear/0</a></li> <li title="clear-1"><a href="lcnt.html#clear-1">clear/1</a></li> <li title="conflicts-0"><a href="lcnt.html#conflicts-0">conflicts/0</a></li> <li title="conflicts-1"><a href="lcnt.html#conflicts-1">conflicts/1</a></li> <li title="locations-0"><a href="lcnt.html#locations-0">locations/0</a></li> <li title="locations-1"><a href="lcnt.html#locations-1">locations/1</a></li> <li title="inspect-1"><a href="lcnt.html#inspect-1">inspect/1</a></li> <li title="inspect-2"><a href="lcnt.html#inspect-2">inspect/2</a></li> <li title="information-0"><a href="lcnt.html#information-0">information/0</a></li> <li title="swap_pid_keys-0"><a href="lcnt.html#swap_pid_keys-0">swap_pid_keys/0</a></li> <li title="load-1"><a href="lcnt.html#load-1">load/1</a></li> <li title="save-1"><a href="lcnt.html#save-1">save/1</a></li> <li title="apply-1"><a href="lcnt.html#apply-1">apply/1</a></li> <li title="apply-2"><a href="lcnt.html#apply-2">apply/2</a></li> <li title="apply-3"><a href="lcnt.html#apply-3">apply/3</a></li> <li title="pid-2"><a href="lcnt.html#pid-2">pid/2</a></li> <li title="pid-3"><a href="lcnt.html#pid-3">pid/3</a></li> <li title="port-1"><a href="lcnt.html#port-1">port/1</a></li> <li title="port-2"><a href="lcnt.html#port-2">port/2</a></li> <li title="rt_collect-0"><a href="lcnt.html#rt_collect-0">rt_collect/0</a></li> <li title="rt_collect-1"><a href="lcnt.html#rt_collect-1">rt_collect/1</a></li> <li title="rt_clear-0"><a href="lcnt.html#rt_clear-0">rt_clear/0</a></li> <li title="rt_clear-1"><a href="lcnt.html#rt_clear-1">rt_clear/1</a></li> <li title="rt_opt-1"><a href="lcnt.html#rt_opt-1">rt_opt/1</a></li> <li title="rt_opt-2"><a href="lcnt.html#rt_opt-2">rt_opt/2</a></li> </ul> </li> <li id="no" title="make " expanded="false">make<ul> <li><a href="make.html"> Top of manual page </a></li> <li title="all-0"><a href="make.html#all-0">all/0</a></li> <li title="all-1"><a href="make.html#all-1">all/1</a></li> <li title="files-1"><a href="make.html#files-1">files/1</a></li> <li title="files-2"><a href="make.html#files-2">files/2</a></li> </ul> </li> <li id="no" title="tags " expanded="false">tags<ul> <li><a href="tags.html"> Top of manual page </a></li> <li title="file-1"><a href="tags.html#file-1">file/1</a></li> <li title="files-1"><a href="tags.html#files-1">files/1</a></li> <li title="dir-1"><a href="tags.html#dir-1">dir/1</a></li> <li title="dirs-1"><a href="tags.html#dirs-1">dirs/1</a></li> <li title="subdir-1"><a href="tags.html#subdir-1">subdir/1</a></li> <li title="subdirs-1"><a href="tags.html#subdirs-1">subdirs/1</a></li> <li title="root-1"><a href="tags.html#root-1">root/1</a></li> </ul> </li> <li id="no" title="xref " expanded="false">xref<ul> <li><a href="xref.html"> Top of manual page </a></li> <li title="add_application-2"><a href="xref.html#add_application-2">add_application/2</a></li> <li title="add_directory-2"><a href="xref.html#add_directory-2">add_directory/2</a></li> <li title="add_module-2"><a href="xref.html#add_module-2">add_module/2</a></li> <li title="add_release-2"><a href="xref.html#add_release-2">add_release/2</a></li> <li title="analyze-2"><a href="xref.html#analyze-2">analyze/2</a></li> <li title="d-1"><a href="xref.html#d-1">d/1</a></li> <li title="forget-1"><a href="xref.html#forget-1">forget/1</a></li> <li title="forget-2"><a href="xref.html#forget-2">forget/2</a></li> <li title="format_error-1"><a href="xref.html#format_error-1">format_error/1</a></li> <li title="get_default-1"><a href="xref.html#get_default-1">get_default/1</a></li> <li title="get_default-2"><a href="xref.html#get_default-2">get_default/2</a></li> <li title="get_library_path-1"><a href="xref.html#get_library_path-1">get_library_path/1</a></li> <li title="info-1"><a href="xref.html#info-1">info/1</a></li> <li title="info-2"><a href="xref.html#info-2">info/2</a></li> <li title="info-3"><a href="xref.html#info-3">info/3</a></li> <li title="m-1"><a href="xref.html#m-1">m/1</a></li> <li title="m-1"><a href="xref.html#m-1">m/1</a></li> <li title="q-2"><a href="xref.html#q-2">q/2</a></li> <li title="remove_application-2"><a href="xref.html#remove_application-2">remove_application/2</a></li> <li title="remove_module-2"><a href="xref.html#remove_module-2">remove_module/2</a></li> <li title="remove_release-2"><a href="xref.html#remove_release-2">remove_release/2</a></li> <li title="replace_application-3"><a href="xref.html#replace_application-3">replace_application/3</a></li> <li title="replace_module-3"><a href="xref.html#replace_module-3">replace_module/3</a></li> <li title="set_default-3"><a href="xref.html#set_default-3">set_default/3</a></li> <li title="set_default-2"><a href="xref.html#set_default-2">set_default/2</a></li> <li title="set_library_path-2"><a href="xref.html#set_library_path-2">set_library_path/2</a></li> <li title="start-1"><a href="xref.html#start-1">start/1</a></li> <li title="start-2"><a href="xref.html#start-2">start/2</a></li> <li title="stop-1"><a href="xref.html#stop-1">stop/1</a></li> <li title="update-1"><a href="xref.html#update-1">update/1</a></li> <li title="variables-1"><a href="xref.html#variables-1">variables/1</a></li> </ul> </li> </ul> </div></div> <div id="content"> <div class="innertube"> <!-- refpage --><center><h1>fprof</h1></center> <h3>MODULE</h3> <div class="REFBODY">fprof</div> <h3>MODULE SUMMARY</h3> <div class="REFBODY">A Time Profiling Tool using trace to file for minimal runtime performance impact.</div> <h3>DESCRIPTION</h3> <div class="REFBODY"><p> <p>This module is used to profile a program to find out how the execution time is used. Trace to file is used to minimize runtime performance impact. </p> <p>The <span class="code">fprof</span> module uses tracing to collect profiling data, hence there is no need for special compilation of any module to be profiled. When it starts tracing, <span class="code">fprof</span> will erase all previous tracing in the node and set the necessary trace flags on the profiling target processes as well as local call trace on all functions in all loaded modules and all modules to be loaded. <span class="code">fprof</span> erases all tracing in the node when it stops tracing. </p> <p><span class="code">fprof</span> presents both <strong>own time</strong> i.e how much time a function has used for its own execution, and <strong>accumulated time</strong> i.e including called functions. All presented times are collected using trace timestamps. <span class="code">fprof</span> tries to collect cpu time timestamps, if the host machine OS supports it. Therefore the times may be wallclock times and OS scheduling will randomly strike all called functions in a presumably fair way. </p> <p>If, however, the profiling time is short, and the host machine OS does not support high resolution cpu time measurements, some few OS schedulings may show up as ridiculously long execution times for functions doing practically nothing. An example of a function more or less just composing a tuple in about 100 times the normal execution time has been seen, and when the tracing was repeated, the execution time became normal. </p> <p>Profiling is essentially done in 3 steps:</p> <dl> <dt><strong><span class="code">1</span></strong></dt> <dd>Tracing; to file, as mentioned in the previous paragraph. The trace contains entries for function calls, returns to function, process scheduling, other process related (spawn, etc) events, and garbage collection. All trace entries are timestamped.</dd> <dt><strong><span class="code">2</span></strong></dt> <dd>Profiling; the trace file is read, the execution call stack is simulated, and raw profile data is calculated from the simulated call stack and the trace timestamps. The profile data is stored in the <span class="code">fprof</span> server state. During this step the trace data may be dumped in text format to file or console. </dd> <dt><strong><span class="code">3</span></strong></dt> <dd>Analysing; the raw profile data is sorted, filtered and dumped in text format either to file or console. The text format intended to be both readable for a human reader, as well as parsable with the standard erlang parsing tools.</dd> </dl> <p>Since <span class="code">fprof</span> uses trace to file, the runtime performance degradation is minimized, but still far from negligible, especially for programs that use the filesystem heavily by themselves. Where you place the trace file is also important, e.g on Solaris <span class="code">/tmp</span> is usually a good choice since it is essentially a RAM disk, while any NFS (network) mounted disk is a bad idea. </p> <p><span class="code">fprof</span> can also skip the file step and trace to a tracer process that does the profiling in runtime. <a name="start"></a> </p> </p></div> <h3>EXPORTS</h3> <p><a name="start-0"><span class="bold_code">start() -> {ok, Pid} | {error, {already_started, Pid}}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Pid = pid()</span><br> </div> </div> <div class="REFBODY"><p> <p>Starts the <span class="code">fprof</span> server. </p> <p>Note that it seldom needs to be started explicitly since it is automatically started by the functions that need a running server. <a name="stop"></a> </p> </p></div> <p><a name="stop-0"><span class="bold_code">stop() -> ok</span></a><br></p> <div class="REFBODY"><p> <p>Same as <span class="code">stop(normal)</span>.</p> </p></div> <p><a name="stop-1"><span class="bold_code">stop(Reason) -> ok</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Stops the <span class="code">fprof</span> server. </p> <p>The supplied <span class="code">Reason</span> becomes the exit reason for the server process. Default Any <span class="code">Reason</span> other than <span class="code">kill</span> sends a request to the server and waits for it to clean up, reply and exit. If <span class="code">Reason</span> is <span class="code">kill</span>, the server is bluntly killed. </p> <p>If the <span class="code">fprof</span> server is not running, this function returns immediately with the same return value. </p> <div class="note"> <div class="label">Note</div> <div class="content"><p> <p>When the <span class="code">fprof</span> server is stopped the collected raw profile data is lost.</p> </p></div> </div> <a name="apply"></a> </p></div> <p><a name="apply-2"><span class="bold_code">apply(Func, Args) -> term()</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Func = function() | {Module, Function}</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Args = [term()]</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Module = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Function = atom()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">apply(Func, Args, [])</span>.</p> </p></div> <p><a name="apply-3"><span class="bold_code">apply(Module, Function, Args) -> term()</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Args = [term()]</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Module = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Function = atom()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">apply({Module, Function}, Args, [])</span>.</p> </p></div> <p><a name="apply-3"><span class="bold_code">apply(Func, Args, OptionList) -> term()</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Func = function() | {Module, Function}</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Args = [term()]</span><br> </div> <div class="REFTYPES"> <span class="bold_code">OptionList = [Option]</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Module = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Function = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Option = continue | start | {procs, PidList} | TraceStartOption</span><br> </div> </div> <div class="REFBODY"><p> <p>Calls <span class="code">erlang:apply(Func, Args)</span> surrounded by <span class="code">trace([start, ...])</span> and <span class="code">trace(stop)</span>. </p> <p>Some effort is made to keep the trace clean from unnecessary trace messages; tracing is started and stopped from a spawned process while the <span class="code">erlang:apply/2</span> call is made in the current process, only surrounded by <span class="code">receive</span> and <span class="code">send</span> statements towards the trace starting process. The trace starting process exits when not needed any more. </p> <p>The <span class="code">TraceStartOption</span> is any option allowed for <span class="code">trace/1</span>. The options <span class="code">[start, {procs, [self() | PidList]} | OptList]</span> are given to <span class="code">trace/1</span>, where <span class="code">OptList</span> is <span class="code">OptionList</span> with <span class="code">continue</span>, <span class="code">start</span> and <span class="code">{procs, _}</span> options removed. </p> <p>The <span class="code">continue</span> option inhibits the call to <span class="code">trace(stop)</span> and leaves it up to the caller to stop tracing at a suitable time.</p> </p></div> <p><a name="apply-4"><span class="bold_code">apply(Module, Function, Args, OptionList) -> term()</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Module = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Function = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Args = [term()]</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">apply({Module, Function}, Args, OptionList)</span>. </p> <p><span class="code">OptionList</span> is an option list allowed for <span class="code">apply/3</span>. <a name="trace"></a> </p> </p></div> <p><a name="trace-2"><span class="bold_code">trace(start, Filename) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">trace([start, {file, Filename}])</span>.</p> </p></div> <p><a name="trace-2"><span class="bold_code">trace(verbose, Filename) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">trace([start, verbose, {file, Filename}])</span>.</p> </p></div> <p><a name="trace-2"><span class="bold_code">trace(OptionName, OptionValue) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">OptionName = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">OptionValue = term()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">trace([{OptionName, OptionValue}])</span>.</p> </p></div> <p><a name="trace-1"><span class="bold_code">trace(verbose) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">trace([start, verbose])</span>.</p> </p></div> <p><a name="trace-1"><span class="bold_code">trace(OptionName) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">OptionName = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">trace([OptionName])</span>.</p> </p></div> <p><a name="trace-1"><span class="bold_code">trace({OptionName, OptionValue}) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">OptionName = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">OptionValue = term()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">trace([{OptionName, OptionValue}])</span>.</p> </p></div> <p><a name="trace-1"><span class="bold_code">trace([Option]) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Option = start | stop | {procs, PidSpec} | {procs, [PidSpec]} | verbose | {verbose, bool()} | file | {file, Filename} | {tracer, Tracer}</span><br> </div> <div class="REFTYPES"> <span class="bold_code">PidSpec = pid() | atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Tracer = pid() | port()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Starts or stops tracing. </p> <p><span class="code">PidSpec</span> and <span class="code">Tracer</span> are used in calls to <span class="code">erlang:trace(PidSpec, true, [{tracer, Tracer} | Flags])</span>, and <span class="code">Filename</span> is used to call <span class="code">dbg:trace_port(file, Filename)</span>. Please see the appropriate documentation.</p> <p>Option description:</p> <dl> <dt><strong><span class="code">stop</span></strong></dt> <dd>Stops a running <span class="code">fprof</span> trace and clears all tracing from the node. Either option <span class="code">stop</span> or <span class="code">start</span> must be specified, but not both.</dd> <dt><strong><span class="code">start</span></strong></dt> <dd>Clears all tracing from the node and starts a new <span class="code">fprof</span> trace. Either option <span class="code">start</span> or <span class="code">stop</span> must be specified, but not both.</dd> <dt><strong><span class="code">verbose</span>| <span class="code">{verbose, bool()}</span></strong></dt> <dd>The options <span class="code">verbose</span> or <span class="code">{verbose, true}</span> adds some trace flags that <span class="code">fprof</span> does not need, but that may be interesting for general debugging purposes. This option is only allowed with the <span class="code">start</span> option.</dd> <dt><strong><span class="code">cpu_time</span>| <span class="code">{cpu_time, bool()}</span></strong></dt> <dd>The options <span class="code">cpu_time</span> or <span class="code">{cpu_time, true></span> makes the timestamps in the trace be in CPU time instead of wallclock time which is the default. This option is only allowed with the <span class="code">start</span> option.</dd> <dt><strong><span class="code">{procs, PidSpec}</span>| <span class="code">{procs, [PidSpec]}</span></strong></dt> <dd>Specifies which processes that shall be traced. If this option is not given, the calling process is traced. All processes spawned by the traced processes are also traced. This option is only allowed with the <span class="code">start</span> option.</dd> <dt><strong><span class="code">file</span>| <span class="code">{file, Filename}</span></strong></dt> <dd>Specifies the filename of the trace. If the option <span class="code">file</span> is given, or none of these options are given, the file <span class="code">"fprof.trace"</span> is used. This option is only allowed with the <span class="code">start</span> option, but not with the <span class="code">{tracer, Tracer}</span> option.</dd> <dt><strong><span class="code">{tracer, Tracer}</span></strong></dt> <dd>Specifies that trace to process or port shall be done instead of trace to file. This option is only allowed with the <span class="code">start</span> option, but not with the <span class="code">{file, Filename}</span> option.</dd> </dl> <a name="profile"></a> </p></div> <p><a name="profile-0"><span class="bold_code">profile() -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">profile([])</span>.</p> </p></div> <p><a name="profile-2"><span class="bold_code">profile(OptionName, OptionValue) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">OptionName = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">OptionValue = term()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">profile([{OptionName, OptionValue}])</span>.</p> </p></div> <p><a name="profile-1"><span class="bold_code">profile(OptionName) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">OptionName = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">profile([OptionName])</span>.</p> </p></div> <p><a name="profile-1"><span class="bold_code">profile({OptionName, OptionValue}) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">OptionName = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">OptionValue = term()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">profile([{OptionName, OptionValue}])</span>.</p> </p></div> <p><a name="profile-1"><span class="bold_code">profile([Option]) -> ok | {ok, Tracer} | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Option = file | {file, Filename} | dump | {dump, Dump} | append | start | stop</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Dump = pid() | Dumpfile | []</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Tracer = pid()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Compiles a trace into raw profile data held by the <span class="code">fprof</span> server. </p> <p><span class="code">Dumpfile</span> is used to call <span class="code">file:open/2</span>, and <span class="code">Filename</span> is used to call <span class="code">dbg:trace_port(file, Filename)</span>. Please see the appropriate documentation.</p> <p>Option description:</p> <dl> <dt><strong><span class="code">file</span>| <span class="code">{file, Filename}</span></strong></dt> <dd>Reads the file <span class="code">Filename</span> and creates raw profile data that is stored in RAM by the <span class="code">fprof</span> server. If the option <span class="code">file</span> is given, or none of these options are given, the file <span class="code">"fprof.trace"</span> is read. The call will return when the whole trace has been read with the return value <span class="code">ok</span> if successful. This option is not allowed with the <span class="code">start</span> or <span class="code">stop</span> options.</dd> <dt><strong><span class="code">dump</span>| <span class="code">{dump, Dump}</span></strong></dt> <dd>Specifies the destination for the trace text dump. If this option is not given, no dump is generated, if it is <span class="code">dump</span> the destination will be the caller's group leader, otherwise the destination <span class="code">Dump</span> is either the pid of an I/O device or a filename. And, finally, if the filename is <span class="code">[]</span> - <span class="code">"fprof.dump"</span> is used instead. This option is not allowed with the <span class="code">stop</span> option.</dd> <dt><strong><span class="code">append</span></strong></dt> <dd>Causes the trace text dump to be appended to the destination file. This option is only allowed with the <span class="code">{dump, Dumpfile}</span> option.</dd> <dt><strong><span class="code">start</span></strong></dt> <dd>Starts a tracer process that profiles trace data in runtime. The call will return immediately with the return value <span class="code">{ok, Tracer}</span> if successful. This option is not allowed with the <span class="code">stop</span>, <span class="code">file</span> or <span class="code">{file, Filename}</span> options.</dd> <dt><strong><span class="code">stop</span></strong></dt> <dd>Stops the tracer process that profiles trace data in runtime. The return value will be value <span class="code">ok</span> if successful. This option is not allowed with the <span class="code">start</span>, <span class="code">file</span> or <span class="code">{file, Filename}</span> options.</dd> </dl> <a name="analyse"></a> </p></div> <p><a name="analyse-0"><span class="bold_code">analyse() -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">analyse([])</span>.</p> </p></div> <p><a name="analyse-2"><span class="bold_code">analyse(OptionName, OptionValue) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">OptionName = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">OptionValue = term()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">analyse([{OptionName, OptionValue}])</span>.</p> </p></div> <p><a name="analyse-1"><span class="bold_code">analyse(OptionName) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">OptionName = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">analyse([OptionName])</span>.</p> </p></div> <p><a name="analyse-1"><span class="bold_code">analyse({OptionName, OptionValue}) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">OptionName = atom()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">OptionValue = term()</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Same as <span class="code">analyse([{OptionName, OptionValue}])</span>.</p> </p></div> <p><a name="analyse-1"><span class="bold_code">analyse([Option]) -> ok | {error, Reason} | {'EXIT', ServerPid, Reason}</span></a><br></p> <div class="REFBODY"> <p>Types:</p> <div class="REFTYPES"> <span class="bold_code">Option = dest | {dest, Dest} | append | {cols, Cols} | callers | {callers, bool()} | no_callers | {sort, SortSpec} | totals | {totals, bool()} | details | {details, bool()} | no_details</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Dest = pid() | Destfile</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Cols = integer() >= 80</span><br> </div> <div class="REFTYPES"> <span class="bold_code">SortSpec = acc | own</span><br> </div> <div class="REFTYPES"> <span class="bold_code">Reason = term()</span><br> </div> </div> <div class="REFBODY"><p> <p>Analyses raw profile data in the <span class="code">fprof</span> server. If called while there is no raw profile data available, <span class="code">{error, no_profile}</span> is returned. </p> <p><span class="code">Destfile</span> is used to call <span class="code">file:open/2</span>. Please see the appropriate documentation.</p> <p>Option description:</p> <dl> <dt><strong><span class="code">dest</span>| <span class="code">{dest, Dest}</span></strong></dt> <dd>Specifies the destination for the analysis. If this option is not given or it is <span class="code">dest</span>, the destination will be the caller's group leader, otherwise the destination <span class="code">Dest</span> is either the <span class="code">pid()</span> of an I/O device or a filename. And, finally, if the filename is <span class="code">[]</span> - <span class="code">"fprof.analysis"</span> is used instead.</dd> <dt><strong><span class="code">append</span></strong></dt> <dd>Causes the analysis to be appended to the destination file. This option is only allowed with the <span class="code">{dest, Destfile}</span> option.</dd> <dt><strong><span class="code">{cols, Cols}</span></strong></dt> <dd>Specifies the number of columns in the analysis text. If this option is not given the number of columns is set to 80.</dd> <dt><strong><span class="code">callers</span>| <span class="code">{callers, true}</span></strong></dt> <dd>Prints callers and called information in the analysis. This is the default.</dd> <dt><strong><span class="code">{callers, false}</span>| <span class="code">no_callers</span></strong></dt> <dd>Suppresses the printing of callers and called information in the analysis.</dd> <dt><strong><span class="code">{sort, SortSpec}</span></strong></dt> <dd>Specifies if the analysis should be sorted according to the ACC column, which is the default, or the OWN column. See <span class="bold_code"><a href="#analysis">Analysis Format</a></span> below.</dd> <dt><strong><span class="code">totals</span>| <span class="code">{totals, true}</span></strong></dt> <dd>Includes a section containing call statistics for all calls regardless of process, in the analysis.</dd> <dt><strong><span class="code">{totals, false}</span></strong></dt> <dd>Supresses the totals section in the analysis, which is the default.</dd> <dt><strong><span class="code">details</span>| <span class="code">{details, true}</span></strong></dt> <dd>Prints call statistics for each process in the analysis. This is the default.</dd> <dt><strong><span class="code">{details, false}</span>| <span class="code">no_details</span></strong></dt> <dd>Suppresses the call statistics for each process from the analysis.</dd> </dl> </p></div> <h3><a name="id2277599">Analysis format</a></h3> <div class="REFBODY"> <a name="analysis"></a> <p>This section describes the output format of the analyse command. See <span class="bold_code"><a href="#analyse">analyse/0</a></span>. </p> <p>The format is parsable with the standard Erlang parsing tools <span class="code">erl_scan</span> and <span class="code">erl_parse</span>, <span class="code">file:consult/1</span> or <span class="code">io:read/2</span>. The parse format is not explained here - it should be easy for the interested to try it out. Note that some flags to <span class="code">analyse/1</span> will affect the format. </p> <p>The following example was run on OTP/R8 on Solaris 8, all OTP internals in this example are very version dependent. </p> <p>As an example, we will use the following function, that you may recognise as a slightly modified benchmark function from the manpage file(3):</p> <div class="example"><pre> -module(foo). -export([create_file_slow/2]). create_file_slow(Name, N) when integer(N), N >= 0 -> {ok, FD} = file:open(Name, [raw, write, delayed_write, binary]), if N > 256 -> ok = file:write(FD, lists:map(fun (X) -> <<X:32/unsigned>> end, lists:seq(0, 255))), ok = create_file_slow(FD, 256, N); true -> ok = create_file_slow(FD, 0, N) end, ok = file:close(FD). create_file_slow(FD, M, M) -> ok; create_file_slow(FD, M, N) -> ok = file:write(FD, <<M:32/unsigned>>), create_file_slow(FD, M+1, N).</pre></div> <p>Let us have a look at the printout after running:</p> <div class="example"><pre> 1> <span class="bold_code">fprof:apply(foo, create_file_slow, [junk, 1024]).</span> 2> <span class="bold_code">fprof:profile().</span> 3> <span class="bold_code">fprof:analyse().</span></pre></div> <p>The printout starts with:</p> <div class="example"><pre> %% Analysis results: { analysis_options, [{callers, true}, {sort, acc}, {totals, false}, {details, true}]}. % CNT ACC OWN [{ totals, 9627, 1691.119, 1659.074}]. %%%</pre></div> <p>The CNT column shows the total number of function calls that was found in the trace. In the ACC column is the total time of the trace from first timestamp to last. And in the OWN column is the sum of the execution time in functions found in the trace, not including called functions. In this case it is very close to the ACC time since the emulator had practically nothing else to do than to execute our test program. </p> <p>All time values in the printout are in milliseconds. </p> <p>The printout continues:</p> <div class="example"><pre> % CNT ACC OWN [{ "<0.28.0>", 9627,undefined, 1659.074}]. %%</pre></div> <p>This is the printout header of one process. The printout contains only this one process since we did <span class="code">fprof:apply/3</span> which traces only the current process. Therefore the CNT and OWN columns perfectly matches the totals above. The ACC column is undefined since summing the ACC times of all calls in the process makes no sense - you would get something like the ACC value from totals above multiplied by the average depth of the call stack, or something. </p> <p>All paragraphs up to the next process header only concerns function calls within this process. </p> <p>Now we come to something more interesting:</p> <div class="example"><pre> {[{undefined, 0, 1691.076, 0.030}], { {fprof,apply_start_stop,4}, 0, 1691.076, 0.030}, % [{{foo,create_file_slow,2}, 1, 1691.046, 0.103}, {suspend, 1, 0.000, 0.000}]}. {[{{fprof,apply_start_stop,4}, 1, 1691.046, 0.103}], { {foo,create_file_slow,2}, 1, 1691.046, 0.103}, % [{{file,close,1}, 1, 1398.873, 0.019}, {{foo,create_file_slow,3}, 1, 249.678, 0.029}, {{file,open,2}, 1, 20.778, 0.055}, {{lists,map,2}, 1, 16.590, 0.043}, {{lists,seq,2}, 1, 4.708, 0.017}, {{file,write,2}, 1, 0.316, 0.021}]}. </pre></div> <p>The printout consists of one paragraph per called function. The function <strong>marked</strong> with '%' is the one the paragraph concerns - <span class="code">foo:create_file_slow/2</span>. Above the marked function are the <strong>calling</strong> functions - those that has called the marked, and below are those <strong>called</strong> by the marked function. </p> <p>The paragraphs are per default sorted in decreasing order of the ACC column for the marked function. The calling list and called list within one paragraph are also per default sorted in decreasing order of their ACC column. </p> <p>The columns are: CNT - the number of times the function has been called, ACC - the time spent in the function including called functions, and OWN - the time spent in the function not including called functions. </p> <p>The rows for the <strong>calling</strong> functions contain statistics for the <strong>marked</strong> function with the constraint that only the occasions when a call was made from the <strong>row's</strong> function to the <strong>marked</strong> function are accounted for. </p> <p>The row for the <strong>marked</strong> function simply contains the sum of all <strong>calling</strong> rows. </p> <p>The rows for the <strong>called</strong> functions contains statistics for the <strong>row's</strong> function with the constraint that only the occasions when a call was made from the <strong>marked</strong> to the <strong>row's</strong> function are accounted for. </p> <p>So, we see that <span class="code">foo:create_file_slow/2</span> used very little time for its own execution. It spent most of its time in <span class="code">file:close/1</span>. The function <span class="code">foo:create_file_slow/3</span> that writes 3/4 of the file contents is the second biggest time thief. </p> <p>We also see that the call to <span class="code">file:write/2</span> that writes 1/4 of the file contents takes very little time in itself. What takes time is to build the data (<span class="code">lists:seq/2</span> and <span class="code">lists:map/2</span>). </p> <p>The function 'undefined' that has called <span class="code">fprof:apply_start_stop/4</span> is an unknown function because that call was not recorded in the trace. It was only recorded that the execution returned from <span class="code">fprof:apply_start_stop/4</span> to some other function above in the call stack, or that the process exited from there. </p> <p>Let us continue down the printout to find:</p> <div class="example"><pre> {[{{foo,create_file_slow,2}, 1, 249.678, 0.029}, {{foo,create_file_slow,3}, 768, 0.000, 23.294}], { {foo,create_file_slow,3}, 769, 249.678, 23.323}, % [{{file,write,2}, 768, 220.314, 14.539}, {suspend, 57, 6.041, 0.000}, {{foo,create_file_slow,3}, 768, 0.000, 23.294}]}. </pre></div> <p>If you compare with the code you will see there also that <span class="code">foo:create_file_slow/3</span> was called only from <span class="code">foo:create_file_slow/2</span> and itself, and called only <span class="code">file:write/2</span>, note the number of calls to <span class="code">file:write/2</span>. But here we see that <span class="code">suspend</span> was called a few times. This is a pseudo function that indicates that the process was suspended while executing in <span class="code">foo:create_file_slow/3</span>, and since there is no <span class="code">receive</span> or <span class="code">erlang:yield/0</span> in the code, it must be Erlang scheduling suspensions, or the trace file driver compensating for large file write operations (these are regarded as a schedule out followed by a schedule in to the same process). </p> <p></p> <p>Let us find the <span class="code">suspend</span> entry:</p> <div class="example"><pre> {[{{file,write,2}, 53, 6.281, 0.000}, {{foo,create_file_slow,3}, 57, 6.041, 0.000}, {{prim_file,drv_command,4}, 50, 4.582, 0.000}, {{prim_file,drv_get_response,1}, 34, 2.986, 0.000}, {{lists,map,2}, 10, 2.104, 0.000}, {{prim_file,write,2}, 17, 1.852, 0.000}, {{erlang,port_command,2}, 15, 1.713, 0.000}, {{prim_file,drv_command,2}, 22, 1.482, 0.000}, {{prim_file,translate_response,2}, 11, 1.441, 0.000}, {{prim_file,'-drv_command/2-fun-0-',1}, 15, 1.340, 0.000}, {{lists,seq,4}, 3, 0.880, 0.000}, {{foo,'-create_file_slow/2-fun-0-',1}, 5, 0.523, 0.000}, {{erlang,bump_reductions,1}, 4, 0.503, 0.000}, {{prim_file,open_int_setopts,3}, 1, 0.165, 0.000}, {{prim_file,i32,4}, 1, 0.109, 0.000}, {{fprof,apply_start_stop,4}, 1, 0.000, 0.000}], { suspend, 299, 32.002, 0.000}, % [ ]}.</pre></div> <p>We find no particulary long suspend times, so no function seems to have waited in a receive statement. Actually, <span class="code">prim_file:drv_command/4</span> contains a receive statement, but in this test program, the message lies in the process receive buffer when the receive statement is entered. We also see that the total suspend time for the test run is small. </p> <p>The <span class="code">suspend</span> pseudo function has got an OWN time of zero. This is to prevent the process total OWN time from including time in suspension. Whether suspend time is really ACC or OWN time is more of a philosophical question. </p> <p>Now we look at another interesting pseudo function, <span class="code">garbage_collect</span>:</p> <div class="example"><pre> {[{{prim_file,drv_command,4}, 25, 0.873, 0.873}, {{prim_file,write,2}, 16, 0.692, 0.692}, {{lists,map,2}, 2, 0.195, 0.195}], { garbage_collect, 43, 1.760, 1.760}, % [ ]}.</pre></div> <p>Here we see that no function distinguishes itself considerably, which is very normal. </p> <p>The <span class="code">garbage_collect</span> pseudo function has not got an OWN time of zero like <span class="code">suspend</span>, instead it is equal to the ACC time. </p> <p>Garbage collect often occurs while a process is suspended, but <span class="code">fprof</span> hides this fact by pretending that the suspended function was first unsuspended and then garbage collected. Otherwise the printout would show <span class="code">garbage_collect</span> being called from <span class="code">suspend</span> but not not which function that might have caused the garbage collection. </p> <p>Let us now get back to the test code:</p> <div class="example"><pre> {[{{foo,create_file_slow,3}, 768, 220.314, 14.539}, {{foo,create_file_slow,2}, 1, 0.316, 0.021}], { {file,write,2}, 769, 220.630, 14.560}, % [{{prim_file,write,2}, 769, 199.789, 22.573}, {suspend, 53, 6.281, 0.000}]}. </pre></div> <p>Not unexpectedly, we see that <span class="code">file:write/2</span> was called from <span class="code">foo:create_file_slow/3</span> and <span class="code">foo:create_file_slow/2</span>. The number of calls in each case as well as the used time are also just confirms the previous results. </p> <p>We see that <span class="code">file:write/2</span> only calls <span class="code">prim_file:write/2</span>, but let us refrain from digging into the internals of the kernel application. </p> <p>But, if we nevertheless <strong>do</strong> dig down we find the call to the linked in driver that does the file operations towards the host operating system:</p> <div class="example"><pre> {[{{prim_file,drv_command,4}, 772, 1458.356, 1456.643}], { {erlang,port_command,2}, 772, 1458.356, 1456.643}, % [{suspend, 15, 1.713, 0.000}]}. </pre></div> <p>This is 86 % of the total run time, and as we saw before it is the close operation the absolutely biggest contributor. We find a comparison ratio a little bit up in the call stack:</p> <div class="example"><pre> {[{{prim_file,close,1}, 1, 1398.748, 0.024}, {{prim_file,write,2}, 769, 174.672, 12.810}, {{prim_file,open_int,4}, 1, 19.755, 0.017}, {{prim_file,open_int_setopts,3}, 1, 0.147, 0.016}], { {prim_file,drv_command,2}, 772, 1593.322, 12.867}, % [{{prim_file,drv_command,4}, 772, 1578.973, 27.265}, {suspend, 22, 1.482, 0.000}]}. </pre></div> <p>The time for file operations in the linked in driver distributes itself as 1 % for open, 11 % for write and 87 % for close. All data is probably buffered in the operating system until the close. </p> <p>The unsleeping reader may notice that the ACC times for <span class="code">prim_file:drv_command/2</span> and <span class="code">prim_file:drv_command/4</span> is not equal between the paragraphs above, even though it is easy to believe that <span class="code">prim_file:drv_command/2</span> is just a passthrough function. </p> <p>The missing time can be found in the paragraph for <span class="code">prim_file:drv_command/4</span> where it is evident that not only <span class="code">prim_file:drv_command/2</span> is called but also a fun: </p> <div class="example"><pre> {[{{prim_file,drv_command,2}, 772, 1578.973, 27.265}], { {prim_file,drv_command,4}, 772, 1578.973, 27.265}, % [{{erlang,port_command,2}, 772, 1458.356, 1456.643}, {{prim_file,'-drv_command/2-fun-0-',1}, 772, 87.897, 12.736}, {suspend, 50, 4.582, 0.000}, {garbage_collect, 25, 0.873, 0.873}]}. </pre></div> <p>And some more missing time can be explained by the fact that <span class="code">prim_file:open_int/4</span> both calls <span class="code">prim_file:drv_command/2</span> directly as well as through <span class="code">prim_file:open_int_setopts/3</span>, which complicates the picture. </p> <div class="example"><pre> {[{{prim_file,open,2}, 1, 20.309, 0.029}, {{prim_file,open_int,4}, 1, 0.000, 0.057}], { {prim_file,open_int,4}, 2, 20.309, 0.086}, % [{{prim_file,drv_command,2}, 1, 19.755, 0.017}, {{prim_file,open_int_setopts,3}, 1, 0.360, 0.032}, {{prim_file,drv_open,2}, 1, 0.071, 0.030}, {{erlang,list_to_binary,1}, 1, 0.020, 0.020}, {{prim_file,i32,1}, 1, 0.017, 0.017}, {{prim_file,open_int,4}, 1, 0.000, 0.057}]}. . . . {[{{prim_file,open_int,4}, 1, 0.360, 0.032}, {{prim_file,open_int_setopts,3}, 1, 0.000, 0.016}], { {prim_file,open_int_setopts,3}, 2, 0.360, 0.048}, % [{suspend, 1, 0.165, 0.000}, {{prim_file,drv_command,2}, 1, 0.147, 0.016}, {{prim_file,open_int_setopts,3}, 1, 0.000, 0.016}]}. </pre></div> </div> <h3><a name="id2278261">Notes</a></h3> <div class="REFBODY"> <p>The actual supervision of execution times is in itself a CPU intensive activity. A message is written on the trace file for every function call that is made by the profiled code. </p> <p>The ACC time calculation is sometimes difficult to make correct, since it is difficult to define. This happens especially when a function occurs in several instances in the call stack, for example by calling itself perhaps through other functions and perhaps even non-tail recursively. </p> <p>To produce sensible results, <span class="code">fprof</span> tries not to charge any function more than once for ACC time. The instance highest up (with longest duration) in the call stack is chosen. </p> <p>Sometimes a function may unexpectedly waste a lot (some 10 ms or more depending on host machine OS) of OWN (and ACC) time, even functions that does practically nothing at all. The problem may be that the OS has chosen to schedule out the Erlang runtime system process for a while, and if the OS does not support high resolution cpu time measurements <span class="code">fprof</span> will use wallclock time for its calculations, and it will appear as functions randomly burn virtual machine time.</p> </div> <h3><a name="id2278310">See Also</a></h3> <div class="REFBODY"> <p>dbg(3), <span class="bold_code"><a href="eprof.html">eprof</a></span>(3), erlang(3), io(3), <span class="bold_code"><a href="fprof_chapter.html">Tools User's Guide</a></span></p> </div> </div> <div class="footer"> <hr> <p>Copyright © 1997-2010 Ericsson AB. All Rights Reserved.</p> </div> </div> </div></body> </html>