commit 4d5f6ac5e6ed6cea4217fa027caf54d2558ea85e
parent 1b5ae1c6d20acc549ccbb847879a45ac6ddf40f6
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 7 Oct 2026 09:12:32 -0500
Added details/summary expansion to source code in post
Co-authored-by: Vim
Diffstat:
2 files changed, 88 insertions(+), 3 deletions(-)
diff --git a/posts/site_wip/basic-software-profiling-cpu-usage.html b/posts/site_wip/basic-software-profiling-cpu-usage.html
@@ -109,6 +109,48 @@
<p>Let’s start with something easy, how do you read a flamegraph? Take a look at the following flamegraph and, without my help, try to understand what’s being shown:</p>
<object data="https://blog.laack.co/images/ex1-fg.svg" type="image/svg+xml" width="100%">
</object>
+<details>
+<summary>
+Source code that generated this flamegraph
+</summary>
+<div class="sourceCode" id="cb1"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="dt">uint32_t</span> square<span class="op">(</span><span class="dt">uint32_t</span> input<span class="op">)</span> <span class="op">{</span></span>
+<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> input <span class="op">*</span> input<span class="op">;</span></span>
+<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
+<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
+<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co">// to show stack depths</span></span>
+<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="dt">uint32_t</span> squareHelper<span class="op">(</span><span class="dt">uint32_t</span> input<span class="op">)</span> <span class="op">{</span></span>
+<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> <span class="dt">uint32_t</span> x <span class="op">=</span> <span class="dv">100</span> <span class="op">*</span> <span class="dv">100</span><span class="op">;</span></span>
+<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> square<span class="op">(</span>input<span class="op">);</span></span>
+<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
+<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a></span>
+<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="dt">uint32_t</span> factorial<span class="op">(</span><span class="dt">uint32_t</span> input<span class="op">)</span> <span class="op">{</span></span>
+<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a> <span class="dt">uint32_t</span> acc <span class="op">=</span> <span class="dv">1</span><span class="op">;</span></span>
+<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">while</span><span class="op">(</span>input <span class="op">></span> <span class="dv">1</span><span class="op">)</span> <span class="op">{</span></span>
+<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a> acc <span class="op">*=</span> input<span class="op">;</span></span>
+<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a> input <span class="op">-=</span> <span class="dv">1</span><span class="op">;</span></span>
+<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
+<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> acc<span class="op">;</span></span>
+<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
+<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a></span>
+<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">()</span> <span class="op">{</span></span>
+<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a></span>
+<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a> <span class="dt">uint32_t</span> ub <span class="op">=</span> <span class="bu">std::</span>sqrt<span class="op">(</span>UINT32_MAX<span class="op">);</span></span>
+<span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a></span>
+<span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span><span class="op">(</span><span class="dt">uint32_t</span> i <span class="op">=</span> <span class="dv">0</span><span class="op">;</span> i <span class="op"><</span> ub<span class="op">;</span> <span class="op">++</span>i<span class="op">)</span> <span class="op">{</span></span>
+<span id="cb1-25"><a href="#cb1-25" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span><span class="op">(</span><span class="dt">uint32_t</span> x <span class="op">=</span> <span class="dv">0</span><span class="op">;</span> x <span class="op"><</span> <span class="dv">100000</span><span class="op">;</span> <span class="op">++</span>x<span class="op">)</span> <span class="op">{</span></span>
+<span id="cb1-26"><a href="#cb1-26" aria-hidden="true" tabindex="-1"></a> square<span class="op">(</span>i<span class="op">);</span></span>
+<span id="cb1-27"><a href="#cb1-27" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
+<span id="cb1-28"><a href="#cb1-28" aria-hidden="true" tabindex="-1"></a></span>
+<span id="cb1-29"><a href="#cb1-29" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span><span class="op">(</span><span class="dt">uint32_t</span> x <span class="op">=</span> <span class="dv">0</span><span class="op">;</span> x <span class="op"><</span> <span class="dv">100000</span><span class="op">;</span> <span class="op">++</span>x<span class="op">)</span> <span class="op">{</span></span>
+<span id="cb1-30"><a href="#cb1-30" aria-hidden="true" tabindex="-1"></a> squareHelper<span class="op">(</span>i<span class="op">);</span></span>
+<span id="cb1-31"><a href="#cb1-31" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
+<span id="cb1-32"><a href="#cb1-32" aria-hidden="true" tabindex="-1"></a></span>
+<span id="cb1-33"><a href="#cb1-33" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span><span class="op">(</span><span class="dt">uint32_t</span> x <span class="op">=</span> <span class="dv">0</span><span class="op">;</span> x <span class="op"><</span> <span class="dv">20000</span><span class="op">;</span> <span class="op">++</span>x<span class="op">)</span> <span class="op">{</span></span>
+<span id="cb1-34"><a href="#cb1-34" aria-hidden="true" tabindex="-1"></a> factorial<span class="op">(</span>x <span class="op">%</span> <span class="dv">10</span><span class="op">);</span></span>
+<span id="cb1-35"><a href="#cb1-35" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
+<span id="cb1-36"><a href="#cb1-36" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
+<span id="cb1-37"><a href="#cb1-37" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
+</details>
<p>The amount of time spent with the <code>main</code> function on the stack is represented by the width of its bar.</p>
<p>Notice how there are multiple bars that say <code>square</code>. This is because we execute <code>square</code> at different stack depths, and the width of each bar is dictated by the amount of time we spent with <code>square</code> on the call stack at a given stack depth.</p>
<p>Given how the widths are calculated, we can deduce the top-down width of a bar is how much time the program spent executing a specific function. In the case of <code>squareHelper</code> and <code>square</code>, we see we spent more time executing <code>squareHelper</code> than <code>square</code>, evidenced by the fact that from a top-down view, <code>squareHelper</code> is wider.</p>
@@ -185,9 +227,9 @@
<p>(* They could’ve done coloring as a second pass perhaps to improve perceived performance, but I’d imagine that’d be more complex, and it would increase the time to having the finalized text displayed)</p>
<p>I decided the first benchmark I was going to perform was a comparison between their performances when opening large files on my filesystem. This was quickly stopped when I realized amfora has a max file size limit of 2097152 bytes or ~2MB. That’s curious, but alright then. Let’s just make a file that’s bigger than 1MB.</p>
<p>This is a first pass at a direct comparison so I’m not going to make it the max size, and I’m not going to use a representative text, just a bunch of numbers.</p>
-<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> <span class="bu">open</span>(<span class="st">"lf.txt"</span>, <span class="st">"w"</span>) <span class="im">as</span> f:</span>
-<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">0</span>,<span class="dv">200000</span>):</span>
-<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> f.write(<span class="bu">str</span>(i) <span class="op">+</span> <span class="st">"</span><span class="ch">\n</span><span class="st">"</span>)</span></code></pre></div>
+<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> <span class="bu">open</span>(<span class="st">"lf.txt"</span>, <span class="st">"w"</span>) <span class="im">as</span> f:</span>
+<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">0</span>,<span class="dv">200000</span>):</span>
+<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> f.write(<span class="bu">str</span>(i) <span class="op">+</span> <span class="st">"</span><span class="ch">\n</span><span class="st">"</span>)</span></code></pre></div>
<p>That should be good enough, the resulting file is ~1.3MB in size.</p>
<p>I then ran following command:</p>
<pre><code>perf stat amfora lf.txt
diff --git a/posts/wip/basic-software-profiling-cpu-usage.md b/posts/wip/basic-software-profiling-cpu-usage.md
@@ -16,6 +16,49 @@ Let's start with something easy, how do you read a flamegraph? Take a look at th
<object data="https://blog.laack.co/images/ex1-fg.svg" type="image/svg+xml" width="100%"></object>
+<details>
+<summary>Source code that generated this flamegraph</summary>
+```cpp
+uint32_t square(uint32_t input) {
+ return input * input;
+}
+
+// to show stack depths
+uint32_t squareHelper(uint32_t input) {
+ uint32_t x = 100 * 100;
+ return square(input);
+}
+
+uint32_t factorial(uint32_t input) {
+ uint32_t acc = 1;
+ while(input > 1) {
+ acc *= input;
+ input -= 1;
+ }
+ return acc;
+}
+
+int main() {
+
+ uint32_t ub = std::sqrt(UINT32_MAX);
+
+ for(uint32_t i = 0; i < ub; ++i) {
+ for(uint32_t x = 0; x < 100000; ++x) {
+ square(i);
+ }
+
+ for(uint32_t x = 0; x < 100000; ++x) {
+ squareHelper(i);
+ }
+
+ for(uint32_t x = 0; x < 20000; ++x) {
+ factorial(x % 10);
+ }
+ }
+}
+```
+</details>
+
The amount of time spent with the `main` function on the stack is represented by the width of its bar.
Notice how there are multiple bars that say `square`. This is because we execute `square` at different stack depths, and the width of each bar is dictated by the amount of time we spent with `square` on the call stack at a given stack depth.