<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.18.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>mi-malloc: Profiling</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="codefolding.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="mimalloc-doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="mimalloc-logo.svg"/></td>
<td id="projectalign">
<div id="projectname">mi-malloc<span id="projectnumber"> 3.6/2.5/1.15</span>
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<span id="MSearchSelect" class="search-icon"><span class="search-icon-dropdown"></span></span>
<input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><div id="MSearchCloseImg" class="close-icon"></div></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.18.0 -->
<script type="text/javascript">
let searchBox = new SearchBox("searchBox", "search/",'.html');
</script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', codefold.init);
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded',() => { initNavTree('profiling_guide.html','',''); });
</script>
<div id="container">
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div><div class="header">
<div class="headertitle"><div class="title">Profiling </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Profiling support is experimental and may still change. It is enabled by default, including in release builds, and adds overhead only when activated. The <a class="el" href="group__profiling.html" title="Heap Profiling">profiling API</a> in <span class="tt"><mimalloc-profile.h></span> supports both custom profiling hooks and the built-in pprof-compatible profiler.</p>
<h2>Programmatic Profiling</h2>
<p>Explicit profiler APIs remain available in secure builds and when environment activation is disabled. Build with profiling support (<span class="tt">-DMI_PROFILE=ON</span>), then create a profiler, attach it, and start sampling:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include <mimalloc.h></span></div>
<div class="line"><span class="preprocessor">#include <mimalloc-profile.h></span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">void</span>) {</div>
<div class="line"> <a class="code hl_typedef" href="group__profiling.html#ga9783ebac45ef297ce1358cfd1f8e4080">mi_profiler_t</a>* prof = <a class="code hl_function" href="group__profiling.html#ga22e300c5e486531efc88fd31ac08e9f9">mi_pprof_profiler_new</a>(</div>
<div class="line"> 512 * 1024, <span class="comment">// sample roughly every 512 KiB</span></div>
<div class="line"> <span class="stringliteral">"myprofile"</span>, <span class="comment">// output basename; protobuf format</span></div>
<div class="line"> 0, 0, 0); <span class="comment">// no automatic snapshots</span></div>
<div class="line"> <span class="keywordflow">if</span> (prof == NULL) <span class="keywordflow">return</span> 1;</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">if</span> (!<a class="code hl_function" href="group__profiling.html#ga36e950fb134eb95fd4669c3a3b85e212">mi_profile</a>(prof)) {</div>
<div class="line"> <a class="code hl_function" href="group__profiling.html#ga36e950fb134eb95fd4669c3a3b85e212">mi_profile</a>(NULL);</div>
<div class="line"> <a class="code hl_function" href="group__profiling.html#gaef24d75e08cedc0018925908b51d7fb1">mi_pprof_profiler_delete</a>(prof);</div>
<div class="line"> <span class="keywordflow">return</span> 1;</div>
<div class="line"> }</div>
<div class="line"> <a class="code hl_function" href="group__profiling.html#ga759a5cad55bd9014bf7bde946ca2fa38">mi_profiler_start</a>(prof);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Run your workload here; join worker threads before cleanup.</span></div>
<div class="line"> </div>
<div class="line"> <a class="code hl_function" href="group__profiling.html#gaa7e0a272b9157783b0b63bbe44070a5b">mi_profiler_stop</a>(prof);</div>
<div class="line"> <a class="code hl_function" href="group__profiling.html#gaa3be0ee40c382b0633b10284857f4b1c">mi_profiler_snapshot</a>(prof);</div>
<div class="line"> <a class="code hl_function" href="group__profiling.html#ga36e950fb134eb95fd4669c3a3b85e212">mi_profile</a>(NULL); <span class="comment">// detach before deleting</span></div>
<div class="line"> <a class="code hl_function" href="group__profiling.html#gaef24d75e08cedc0018925908b51d7fb1">mi_pprof_profiler_delete</a>(prof);</div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="ttc" id="agroup__profiling_html_ga22e300c5e486531efc88fd31ac08e9f9"><div class="ttname"><a href="group__profiling.html#ga22e300c5e486531efc88fd31ac08e9f9">mi_pprof_profiler_new</a></div><div class="ttdeci">mi_profiler_t * mi_pprof_profiler_new(size_t initial_threshold, const char *base_file_name, size_t alloc_interval_size, size_t inuse_interval_size, size_t time_interval_secs)</div><div class="ttdoc">experimental: Create the built-in pprof-compatible profiler.</div></div>
<div class="ttc" id="agroup__profiling_html_ga36e950fb134eb95fd4669c3a3b85e212"><div class="ttname"><a href="group__profiling.html#ga36e950fb134eb95fd4669c3a3b85e212">mi_profile</a></div><div class="ttdeci">bool mi_profile(mi_profiler_t *profiler)</div><div class="ttdoc">experimental: Attach a profiler to the current and future heaps of the main subprocess.</div></div>
<div class="ttc" id="agroup__profiling_html_ga759a5cad55bd9014bf7bde946ca2fa38"><div class="ttname"><a href="group__profiling.html#ga759a5cad55bd9014bf7bde946ca2fa38">mi_profiler_start</a></div><div class="ttdeci">bool mi_profiler_start(mi_profiler_t *profiler)</div><div class="ttdoc">experimental: Start sampling with an attached profiler.</div></div>
<div class="ttc" id="agroup__profiling_html_ga9783ebac45ef297ce1358cfd1f8e4080"><div class="ttname"><a href="group__profiling.html#ga9783ebac45ef297ce1358cfd1f8e4080">mi_profiler_t</a></div><div class="ttdeci">struct mi_profiler_s mi_profiler_t</div><div class="ttdoc">Profiler type. Custom profiler fields and callback types are defined in <mimalloc-profile....</div><div class="ttdef"><b>Definition</b> mimalloc-doc.h:1597</div></div>
<div class="ttc" id="agroup__profiling_html_gaa3be0ee40c382b0633b10284857f4b1c"><div class="ttname"><a href="group__profiling.html#gaa3be0ee40c382b0633b10284857f4b1c">mi_profiler_snapshot</a></div><div class="ttdeci">void mi_profiler_snapshot(mi_profiler_t *profiler)</div><div class="ttdoc">experimental: Take a snapshot of the current profile.</div></div>
<div class="ttc" id="agroup__profiling_html_gaa7e0a272b9157783b0b63bbe44070a5b"><div class="ttname"><a href="group__profiling.html#gaa7e0a272b9157783b0b63bbe44070a5b">mi_profiler_stop</a></div><div class="ttdeci">bool mi_profiler_stop(mi_profiler_t *profiler)</div><div class="ttdoc">experimental: Stop sampling.</div></div>
<div class="ttc" id="agroup__profiling_html_gaef24d75e08cedc0018925908b51d7fb1"><div class="ttname"><a href="group__profiling.html#gaef24d75e08cedc0018925908b51d7fb1">mi_pprof_profiler_delete</a></div><div class="ttdeci">void mi_pprof_profiler_delete(mi_profiler_t *profiler)</div><div class="ttdoc">experimental: Delete a built-in profiler created by mi_pprof_profiler_new().</div></div>
</div><!-- fragment --><p>Stopping sampling does not detach the profiler: its <span class="tt">on_free</span> callback can still run for sampled allocations. Detach it from all associated heaps and subprocesses, and ensure no other thread can still invoke its callbacks before deleting it. See <a class="el" href="group__profiling.html" title="Heap Profiling">Heap Profiling</a> for the complete API.</p>
<h2>Environment-based Profiling</h2>
<p>The built-in profiler works with Google's <a href="https://github.com/google/pprof">pprof</a> tools. When both <span class="tt">MI_SECURE=0</span> and <span class="tt">MI_SECURE_ENV=0</span>, and the process is not elevated, it can also be activated through the environment:</p>
<ul>
<li><span class="tt">MIMALLOC_PROFILE=<basename></span> activates profiling for the main subprocess and writes pprof-compatible <span class="tt"><basename>.<seq>.pb</span> files. A <span class="tt">.<a class="el" href="group__heap.html" title="First-class heaps.">Heaps</a></span> or <span class="tt">.text</span> extension selects the text heap-profile format instead. For a basename of <span class="tt">profile</span>, view the results in a browser with <span class="tt">pprof -http=:8080 ./myprogram profile.*</span>.</li>
<li><span class="tt">MIMALLOC_PROFILE_ALLOC_INTERVAL=N</span> sets the approximate number of allocated bytes between automatic snapshots (default: <span class="tt">1GiB</span>; <span class="tt">0</span> disables).</li>
<li><span class="tt">MIMALLOC_PROFILE_INUSE_INTERVAL=N</span> takes a snapshot when in-use bytes grow by that amount (default: <span class="tt">0</span>, disabled).</li>
<li><span class="tt">MIMALLOC_PROFILE_TIME_INTERVAL=N</span> sets seconds between automatic snapshots, checked on allocation events (default: <span class="tt">0</span>, disabled).</li>
<li><span class="tt">MIMALLOC_PROFILE_SAMPLE_RATE=N</span> sets the average number of allocated bytes between samples, using a Poisson distribution (default: <span class="tt">512KiB</span>). </li>
</ul>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
</div><!-- container -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.18.0 </li>
</ul>
</div>
</body>
</html>