blog

Personal blog
git clone git://git.laack.co/blog.git
Log | Files | Refs

commit 2e3c82cb7919077128b44907271f60facfe44f19
parent b457c36f5c02b7a96f5268de35cb02bf716ef24b
Author: Andrew Laack <andrew@laack.co>
Date:   Wed,  7 Oct 2026 10:14:11 -0500

Styling and working to understand svg rendering

Co-authored-by: Vim

Diffstat:
Mposts/gemini/index.gmi | 1-
Mposts/site/feed.xml | 130+------------------------------------------------------------------------------
Mposts/site/index.html | 4----
Mposts/site/style.css | 15+++++++++------
Dposts/site_wip/basic-software-profiling-part-1-cpu-usage.html | 315-------------------------------------------------------------------------------
Aposts/site_wip/style.css | 145+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mposts/wip/basic-software-profiling-cpu-usage.md | 1+
7 files changed, 156 insertions(+), 455 deletions(-)

diff --git a/posts/gemini/index.gmi b/posts/gemini/index.gmi @@ -8,4 +8,3 @@ => you-dont-need-anything.gmi you-dont-need-anything 2025/10/17 => stop-collecting-user-data.gmi stop-collecting-user-data 2025/10/12 => adnauseum-track-me-not-and-privacy-through-obscurity.gmi adnauseum-track-me-not-and-privacy-through-obscurity 2025/10/4 -=> the-sustainability-of-youtube.gmi the-sustainability-of-youtube 2025/9/28 diff --git a/posts/site/feed.xml b/posts/site/feed.xml @@ -7,7 +7,7 @@ <language>en-us</language> <managingEditor>andrew@laack.co</managingEditor> <webMaster>andrew@laack.co</webMaster> -<lastBuildDate>Wed, 07 Oct 2026 09:50:43 -0500</lastBuildDate> +<lastBuildDate>Wed, 07 Oct 2026 10:10:43 -0500</lastBuildDate> <atom:link href="https://blog.laack.co/feed.xml" rel="self" type="application/rss+xml"/> <item> <title><![CDATA[PyGame vs. Raylib]]></title> @@ -474,131 +474,3 @@ pygame.draw.line(display, WHITE, (self.v1.x,self.v1.y), (self.v2.x,self.v2.y), L <pubDate>Sun, 12 Oct 2025 00:00:00 -0500</pubDate> <guid>https://blog.laack.co/stop-collecting-user-data.html</guid> </item> -<item> -<title><![CDATA[AdNauseum, Track Me Not, and Privacy Through Obscurity]]></title> -<link>https://blog.laack.co/adnauseum-track-me-not-and-privacy-through-obscurity.html</link> -<description><![CDATA[<h2 id="context">Context</h2> -<p>AdNauseum [1] is a fork of uBlock Origin [2] that hides ads, clicks them in the background, and aggregates the clicked ads in an easy to view interface. The key difference between UBlock Origin and AdNauseum is AdNauseum both hides and clicks ads.</p> -<p>Track Me Not [3] is a browser extension that mimics human search queries to obscure real queries in the noise.</p> -<h2 id="usability">Usability</h2> -<p>AdNauseum is as effective as uBlock Origin at hiding advertisements. For a user, there is no trade off in the usability of the web when compared with uBlock Origin. This is nice because more restrictive approaches to privacy, like GNU IceCat [4] and Tor [5], hinder modern web usage.</p> -<p>Similarly, Track Me Not has almost no impact on the usability of the web. I say almost no because the traffic it generates likely increases the probability of being shown CAPTCHAs, given that the traffic it generates is likely distributionally different than normal traffic.</p> -<h2 id="effectiveness-at-improving-privacy">Effectiveness at Improving Privacy</h2> -<p>AdNauseum is likely worse at protecting your privacy than UBlock Origin. By clicking ads in the background, there is an inherent trail of where you have been. In some ways this trail exists the moment the ad network sells the ad you are shown, but by using a more obscure technology than UBlock Origin, you are more likely to be fingerprinted. Despite this, AdNauseum makes the modern web a better experience to use than not having an ad blocker.</p> -<p>Track Me Not may also hinder privacy, depending on your privacy goals. The inherent problem is it is phoning home to search engines over time, giving them information about where you are. Search providers are also likely to fingerprint you on the basis of these strange searches. The possible saving grace is that by having so much noise in your search history, it could difficult to figure out what you are interested in. I am uncertain about the value of this though as there are likely going to be two search distributions; Track Me Not’s, and yours. This allows sophisticated search providers to ignore the synthetic requests and track your real requests while also gaining real time IP information.</p> -<p>In short, I find the idea that these tools improve privacy to be dubious.</p> -<h2 id="why-you-still-might-want-to-use-them">Why You Still Might Want to Use Them</h2> -<p>The value of these tools is they are a form of active resistance against ads and tracking. While your privacy is likely hindered by them, you are sending a message. That message costs ad networks and search providers money. In the case of AdNauseum, clicks are expensive for advertisers because most people ignore ads, and Track Me Not imposes computational costs on search providers. Even so, by clicking on so many ads ad networks may catch on and stop charging as much per click. Similarly, search providers may block you or start giving CAPTCHAs which likely impose less computational costs on them than running a query.</p> -<h2 id="my-thoughts">My Thoughts</h2> -<p>You probably shouldn’t be using them. While I enjoy active resistance, this is unlikely to be the right way to do it. It may mess with their knowledge about you if they are not sophisticated, but any sophisticated search provider or ad network, which I think most of them are, will easily sus out inauthentic traffic and gain more information about you as a result.</p> -<p>Instead of using these tools, I recommend doing the following to improve privacy:</p> -<ul> -<li>Use Tor when possible</li> -<li>Avoid sites that require signing in</li> -<li>Don’t use social media</li> -<li>Use a privacy respecting browser</li> -<li>Use UBlock Origin</li> -<li>Use a Pi-hole [6] and privacy respecting DNS servers</li> -<li>Minimize your usage of search engines</li> -<li>Use a variety of privacy respecting search engines</li> -<li>Use local AI tools (if you must use any at all)</li> -<li>Use E2EE messaging when possible</li> -<li>Minimize the trackability of financial transactions</li> -<li>Don’t carry a phone and minimize your usage of it</li> -<li>Avoid proprietary software and software that collects data</li> -</ul> -<p>This results in me doing the following:</p> -<ul> -<li>Using Tor for most of my traffic</li> -<li>Avoiding sites that require signing in</li> -<li>Not using social media sites</li> -<li>Using LibreWolf [7] as my default non-Tor browser</li> -<li>Routing all non-Tor DNS requests through a self-hosted Pi-hole with additional domain filtering</li> -<li>Using DuckDuckGo and a variety of public SearX instances for search</li> -<li>Running Ollama [8] models locally</li> -<li>Preferring communication with PGP encrypted emails, Matrix, or Signal</li> -<li>Using cash or Monero when possible for transactions</li> -<li>Not carrying my phone with me and only using it when it is the only means of achieving a specific goal (ie. SMS 2FA, communication with certain individuals, etc.)</li> -<li>Only using Libre software that doesn’t collect data</li> -</ul> -<p>Unfortunately there are sometimes exceptions to the above for the purpose of completing my work in an efficient manner, but in my personal life, I am unwilling to compromise on these things.</p> -<h2 id="citations">Citations</h2> -<p>[1] - <a href="https://github.com/dhowe/AdNauseam">https://github.com/dhowe/AdNauseam</a></p> -<p>[2] - <a href="https://github.com/gorhill/uBlock">https://github.com/gorhill/uBlock</a></p> -<p>[3] - <a href="https://github.com/vtoubiana/TrackMeNot">https://github.com/vtoubiana/TrackMeNot</a></p> -<p>[4] - <a href="https://www.gnu.org/software/gnuzilla/">https://www.gnu.org/software/gnuzilla/</a></p> -<p>[5] - <a href="https://www.torproject.org/about/history/">https://www.torproject.org/about/history/</a></p> -<p>[6] - <a href="https://pi-hole.net/">https://pi-hole.net/</a></p> -<p>[7] - <a href="https://librewolf.net/">https://librewolf.net/</a></p> -<p>[8] - <a href="https://ollama.com/">https://ollama.com/</a></p>]]></description> -<pubDate>Sat, 04 Oct 2025 00:00:00 -0500</pubDate> -<guid>https://blog.laack.co/adnauseum-track-me-not-and-privacy-through-obscurity.html</guid> -</item> -<item> -<title><![CDATA[The Sustainability of YouTube]]></title> -<link>https://blog.laack.co/the-sustainability-of-youtube.html</link> -<description><![CDATA[<h2 id="context">Context</h2> -<p>I dislike using cloud services because they may discontinue my service [1] or they may do something stupid [2] that negatively impacts me. These concerns, along with concerns about privacy [3], have led me to keep information and content I care about away from cloud services. This does make me wonder, how many people would be distraught about the loss of their content if YouTube terminated their accounts? This is not the topic today, nor is it something I can easily answer, but it is something I wonder about and would like others to consider.</p> -<p>Similarly, I am skeptical of “free” services. It’s incorrect to say “if something is free, you are the product” because charity does exist, but when it comes to Google, they aren’t a charity. Their current model with YouTube is to have people upload videos to their site and show ads to some users when they watch said videos. There are also paid subscriptions, but their primary monetization comes from ads. An important point is they don’t purge content on a regular basis, except in cases of ToS violations. As such, there is a (nearly) monotonically increasing function that describes the storage requirements of YouTube. This motivates my question below.</p> -<h2 id="question">Question</h2> -<p>When will YouTube’s storage costs exceed their revenue if they don’t start purging old content, assuming their revenue does not increase over time?</p> -<h2 id="how-to-answer-this-question">How to Answer This Question</h2> -<p>We need the following information to answer this question:</p> -<ul> -<li>What is YouTube’s annual net profit?</li> -<li>How much data does YouTube store?</li> -<li>How much does data storage cost?</li> -</ul> -<h2 id="youtubes-profit">YouTube’s Profit</h2> -<p>According to Alphabet’s 2025 Q2 earnings release [4], YouTube ads made a revenue of $9.769 billion. Annualized, this is $39.076 billion, but this is only revenue, not net profit. If we assume the operating margin across Alphabet matches the operating margin of YouTube (32%), we find an approximate net profit of $12.50432 billion / year. Actual net profit could differ from this, but since we are concerned with how much data storage this could support, we don’t need to factor in how this would be taxed.</p> -<h2 id="storage-needs">Storage Needs</h2> -<h3 id="total-videos">Total Videos</h3> -<p>YouTube states on their official blog there are over 20 million videos uploaded per day [5]. While I don’t trust YouTube very much, and they don’t have many incentives to be honest on this topic, they seem more trustworthy in this context than the slop factory sites as they are, in fact, the ones who are hosting the content. As such, I will accept this metric.</p> -<h3 id="average-video-size">Average Video Size</h3> -<p>I wrote a python script that uses a curated list of popular Google Trends searches over the past few decades [6] to search YouTube for recently uploaded videos. I ran this script and compiled a list of ~7.65 million YouTube videos.</p> -<p>Before continuing, I will list a few limitations of this approach:</p> -<ul> -<li>YouTube likely imposes some amount of algorithmic filtering when sorting by ‘recently uploaded’</li> -<li>The videos in question are all public (not inclusive of private/unlisted videos)</li> -<li>Less popular search terms may have a different distribution of video sizes</li> -</ul> -<p>These are the main flaws in my methodology, but any approach will be imperfect without being able to get the data directly from YouTube.</p> -<p>Of these 7.65 million videos, I sampled 615,222 of them and queried YouTube using <code>yt-dlp</code> [7] to find all video resolutions and formats YouTube will serve. It seems unlikely to me that YouTube stores each of these resolutions on their servers, but I think it is very likely that YouTube is storing the highest resolution version they are willing to serve to users.</p> -<p>Based on my findings, I propose a lower bound of ~396.17 MB / video, which assumes they are only storing the highest resolution version and all other versions are generated in real time via transcoding (I am confident this isn’t the case, but it provides a nice lower bound). I also propose an upper bound of ~1.44 GB / video, which assumes they are storing every resolution and format for each video they are serving.</p> -<p>All of the code used for this is available on my git server [8].</p> -<h3 id="annual-storage-increase">Annual Storage Increase</h3> -<p>Using my findings above about video size and YouTube’s stated video upload rate, we find:</p> -<p>Lower bound:</p> -<ul> -<li>7.923 PB / Day</li> -<li>2.89 EB / Year</li> -</ul> -<p>Upper bound:</p> -<ul> -<li>28.895 PB / Day</li> -<li>10.547 EB / Year</li> -</ul> -<p>Note: These values may vary depending on rounding, but they should be similar to what anyone else would find.</p> -<h2 id="storage-cost-by-volume">Storage Cost by Volume</h2> -<p>GCP currently charges $26 / month for 1 TB of standard multi-region, US based, cloud storage [9]. If we assume the same 32% profit margin as before, this would cost ~$17.68 / TB / month or $212.16 / TB / year. I don’t know if this is high or low relative to what they actually pay. YouTube requires quick access to many of their videos, but many of their videos are likely retrieved infrequently. Additionally, it seems likely Alphabet’s cloud storage margins are higher than the average margins across the organization. Additionally, these are only US storage prices so this could vary depending on the regions this data is being hosted in. In any case, I think this is a fair estimate.</p> -<h2 id="answer-to-the-question">Answer to the Question</h2> -<p>Given YouTube’s approximated net profit of $12.50432 billion / year and an estimated cost of $212.16 / TB / year for cloud storage, we find their profits can support an additional ~58.94 EB of data.</p> -<p>At the lower bound of 2.89 EB / year we find YouTube’s storage costs will surpass their current profits in ~20.39 years.</p> -<p>If we assume our upper bound of 10.547 EB / year we find YouTube’s storage costs will surpass their current profits in ~5.59 years.</p> -<h2 id="conclusion">Conclusion</h2> -<p>These are very rough bounds, especially given how difficult it is to estimate the cost per TB / year for storage of this data given their retrieval needs, but we find that in ~5.59 - ~20.39 years, YouTube will be forced to start purging old content to remain profitable at their current profit rate.</p> -<h2 id="citations">Citations</h2> -<p>[1] - <a href="https://killedbygoogle.com/">https://killedbygoogle.com/</a></p> -<p>[2] - <a href="https://arstechnica.com/gadgets/2024/05/google-cloud-accidentally-nukes-customer-account-causes-two-weeks-of-downtime/">https://arstechnica.com/gadgets/2024/05/google-cloud-accidentally-nukes-customer-account-causes-two-weeks-of-downtime/</a></p> -<p>[3] - <a href="https://www.gnu.org/proprietary/proprietary-surveillance.html">https://www.gnu.org/proprietary/proprietary-surveillance.html</a></p> -<p>[4] - <a href="https://www.sec.gov/Archives/edgar/data/1652044/000165204425000056/googexhibit991q22025.htm.md">https://www.sec.gov/Archives/edgar/data/1652044/000165204425000056/googexhibit991q22025.htm</a></p> -<p>[5] - <a href="https://web.archive.org/web/20250911091711/https://blog.youtube/press/">https://web.archive.org/web/20250911091711/https://blog.youtube/press/</a></p> -<p>[6] - <a href="https://www.kaggle.com/datasets/dhruvildave/google-trends-dataset">https://www.kaggle.com/datasets/dhruvildave/google-trends-dataset</a></p> -<p>[7] - <a href="https://github.com/yt-dlp/yt-dlp">https://github.com/yt-dlp/yt-dlp</a></p> -<p>[8] - <a href="http://git.laack.co/blog/log.html">http://git.laack.co/blog/log.html</a></p> -<p>[9] - <a href="https://cloud.google.com/storage/pricing#multi-regions">https://cloud.google.com/storage/pricing#multi-regions</a></p>]]></description> -<pubDate>Sun, 28 Sep 2025 00:00:00 -0500</pubDate> -<guid>https://blog.laack.co/the-sustainability-of-youtube.html</guid> -</item> -</channel> -</rss> diff --git a/posts/site/index.html b/posts/site/index.html @@ -17,7 +17,3 @@ <li><a href="you-dont-need-anything.html">you-dont-need-anything</a> - <em>2025/10/17</em></li> <li><a href="stop-collecting-user-data.html">stop-collecting-user-data</a> - <em>2025/10/12</em></li> <li><a href="adnauseum-track-me-not-and-privacy-through-obscurity.html">adnauseum-track-me-not-and-privacy-through-obscurity</a> - <em>2025/10/4</em></li> -<li><a href="the-sustainability-of-youtube.html">the-sustainability-of-youtube</a> - <em>2025/9/28</em></li> -</ol> -</body> -</html> diff --git a/posts/site/style.css b/posts/site/style.css @@ -20,12 +20,15 @@ background-color: transparent; } - pre { - font-family: 'LMRoman10', monospace; - font-size: 1rem; - padding: 0.75rem; - background-color: #eeeeee; - } +pre { + font-family: 'LMMono10', 'Latin Modern Mono', monospace; + font-size: 1rem; + padding: 0.75rem; + background-color: #eeeeee; + max-width: 100%; + overflow-x: auto; + box-sizing: border-box; +} .pgp-container { text-align: center; padding: 20px; diff --git a/posts/site_wip/basic-software-profiling-part-1-cpu-usage.html b/posts/site_wip/basic-software-profiling-part-1-cpu-usage.html @@ -1,315 +0,0 @@ -<!DOCTYPE html> -<html xmlns="http://www.w3.org/1999/xhtml"> -<head> - <meta charset="utf-8" /> - <meta name="generator" content="pandoc 3.10.2" /> - <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> - <title>basic-software-profiling-part-1-cpu-usage</title> - <style> - /* Default styles provided by pandoc. - ** See https://pandoc.org/MANUAL.html#variables-for-html for config info. - */ - span.smallcaps{font-variant: small-caps;} - div.columns{display: flex; gap: 1.5em;} - div.column{flex: auto;} - @media screen { - div.columns{gap: min(4vw, 1.5em);} - div.column{overflow-x: auto;} - } - div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} - /* The extra [class] is a hack that increases specificity enough to - override a similar rule in reveal.js */ - ul.task-list[class]{list-style: none;} - ul.task-list li input[type="checkbox"] { - font-size: inherit; - width: 0.8em; - margin: 0 0.8em 0.2em -1.6em; - vertical-align: middle; - } - .display.math{display: block; text-align: center; margin: 0.5rem auto;} - /* CSS for syntax highlighting */ - html { -webkit-text-size-adjust: 100%; } - pre > code.sourceCode { white-space: pre; position: relative; } - pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } - pre > code.sourceCode > span:empty { height: 1.2em; } - .sourceCode { overflow: visible; } - code.sourceCode > span { color: inherit; text-decoration: inherit; } - div.sourceCode { margin: 1em 0; } - pre.sourceCode { margin: 0; } - @media screen { - div.sourceCode { overflow: auto; } - } - @media print { - pre > code.sourceCode { white-space: pre-wrap; } - pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } - } - pre.numberSource code - { counter-reset: source-line 0; } - pre.numberSource code > span - { position: relative; left: -4em; counter-increment: source-line; } - pre.numberSource code > span > a:first-child::before - { content: counter(source-line); - position: relative; left: -1em; text-align: right; vertical-align: baseline; - border: none; display: inline-block; - -webkit-touch-callout: none; -webkit-user-select: none; - -khtml-user-select: none; -moz-user-select: none; - -ms-user-select: none; user-select: none; - padding: 0 4px; width: 4em; - background-color: #ffffff; - color: #a0a0a0; - } - pre.numberSource { margin-left: 3em; border-left: 1px solid #a0a0a0; padding-left: 4px; } - div.sourceCode - { color: #1f1c1b; background-color: #ffffff; } - @media screen { - pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } - } - code span { color: #1f1c1b; } /* Normal */ - code span.al { color: #bf0303; background-color: #f7e6e6; font-weight: bold; } /* Alert */ - code span.an { color: #ca60ca; } /* Annotation */ - code span.at { color: #0057ae; } /* Attribute */ - code span.bn { color: #b08000; } /* BaseN */ - code span.bu { color: #644a9b; font-weight: bold; } /* BuiltIn */ - code span.cf { color: #1f1c1b; font-weight: bold; } /* ControlFlow */ - code span.ch { color: #924c9d; } /* Char */ - code span.cn { color: #aa5500; } /* Constant */ - code span.co { color: #898887; } /* Comment */ - code span.cv { color: #0095ff; } /* CommentVar */ - code span.do { color: #607880; } /* Documentation */ - code span.dt { color: #0057ae; } /* DataType */ - code span.dv { color: #b08000; } /* DecVal */ - code span.er { color: #bf0303; text-decoration: underline; } /* Error */ - code span.ex { color: #0095ff; font-weight: bold; } /* Extension */ - code span.fl { color: #b08000; } /* Float */ - code span.fu { color: #644a9b; } /* Function */ - code span.im { color: #ff5500; } /* Import */ - code span.in { color: #b08000; } /* Information */ - code span.kw { color: #1f1c1b; font-weight: bold; } /* Keyword */ - code span.op { color: #1f1c1b; } /* Operator */ - code span.ot { color: #006e28; } /* Other */ - code span.pp { color: #006e28; } /* Preprocessor */ - code span.re { color: #0057ae; background-color: #e0e9f8; } /* RegionMarker */ - code span.sc { color: #3daee9; } /* SpecialChar */ - code span.ss { color: #ff5500; } /* SpecialString */ - code span.st { color: #bf0303; } /* String */ - code span.va { color: #0057ae; } /* Variable */ - code span.vs { color: #bf0303; } /* VerbatimString */ - code span.wa { color: #bf0303; } /* Warning */ - </style> - <link rel="stylesheet" href="style.css" /> -</head> -<body> -<h1 id="basic-software-profiling-part-1-cpu-usage">Basic Software Profiling, Part 1: CPU Usage</h1> -<p>There aren’t many good resources that explain software profiling in an approachable and applicable way. I hope these posts can help readers understand basic software profiling techniques without getting too in the weeds, as I believe getting too into the nuances of performance tends to cause people to dismiss it as something too specialized to try reasoning about.</p> -<h2 id="tools">Tools</h2> -<p><a href="https://en.wikipedia.org/wiki/Perf_%28Linux%29">Perf</a> is your friend. Unfortunately, only one of the three most popular operating systems (Linux, OpenBSD, FreeBSD) supports perf, but there are sufficient analogs on FreeBSD, and if you’re on OpenBSD good luck.</p> -<p>You might also want a visualization tool. I’ll be showing <a href="https://github.com/brendangregg/FlameGraph">flamegraph</a>. Normally I just use perf because <a href="https://www.brendangregg.com/perf.html">it is very powerful</a>, but having pleasing visuals is useful for demonstrations (newer versions of <code>perf</code> can also generate flamegraphs directly, but they aren’t as aesthetic).</p> -<h2 id="reading-a-flamegraph">Reading a flamegraph</h2> -<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> -<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> -<p>The astute among you might’ve noticed that <code>square</code> and <code>squareHelper</code> appear to sometimes be executed outside of the main loop. This is due to how stack depth is calculated, and that’s a bit more technical than I’ll be able to discuss here.</p> -<h2 id="perf">Perf</h2> -<p>I’ve been a little loose with my words thus far in describing flamegraphs. The command I’ve been using with perf to generate the data for flamegraphs isn’t tracking the amount of time spent with a function on the stack at a given depth, we are instead tracking the number of on-CPU samples taken with the function on the top of the call stack. This distinction is important because things like blocking I/O will not be reflected in our timings. Additionally, since perf is interrupt based, it may miss some data, but it gives a good enough approximation of CPU utilization.</p> -<pre><code>perf record -g ./ex1</code></pre> -<p>This is the command I ran to generate the data for the flamegraph. The <code>record</code> command is used to record events that will be written to the <code>perf.data</code> file. By default, <code>perf record</code> will sample at 1000Hz. The final option, <code>-g</code>, is used to specify we are interested in having the call stack included in our data.</p> -<p>If you want to generate a flamegraph right away, you can run the following command:</p> -<pre><code>perf script report flamegraph</code></pre> -<p>This isn’t how the flamegraph above was generated because I prefer Brendan’s FlameGraph program, but both are fairly similar, and in a pinch, <code>perf script report flamegraph</code> will work. A command I use more often to view performance is:</p> -<pre><code>perf report -g</code></pre> -<pre><code>+ 100.00% 0.00% ex1 ex1 [.] _start -+ 100.00% 0.00% ex1 libc.so.6 [.] __libc_start_main -+ 100.00% 0.00% ex1 libc.so.6 [.] call_init (inlined) -+ 100.00% 0.00% ex1 libc.so.6 [.] 0x00007f6302a27781 -+ 99.79% 39.46% ex1 ex1 [.] main -+ 34.97% 19.94% ex1 ex1 [.] squareHelper(unsigned int) -+ 29.99% 19.90% ex1 ex1 [.] square(unsigned int) -+ 21.61% 20.69% ex1 ex1 [.] factorial(unsigned int) -...</code></pre> -<p>or if I’m curious to see function calling information:</p> -<pre><code>perf report -g --stdio</code></pre> -<pre><code>... -|--88.87%--main -| | -| |--19.84%--squareHelper(unsigned int) -| | | -| | --4.99%--square(unsigned int) -| | -| |--19.55%--factorial(unsigned int) -| | -| --10.07%--square(unsigned int) -| -|--5.02%--squareHelper(unsigned int) -| -|--4.98%--square(unsigned int) -| ---1.13%--factorial(unsigned int) -...</code></pre> -<p>In many cases, I find the <code>--stdio</code> command a bit difficult to read because the report can get exceptionally wide for any non-trivial program (there are additional CLI options that can help with this), and such navigation in the terminal is difficult.</p> -<p>The final thing I wanted to mention is if you just want high level information about your program’s performance, this is a useful command:</p> -<pre><code>perf stat ./ex1</code></pre> -<pre><code> Performance counter stats for &#39;./ex1&#39;: - - 0 context-switches:u # 0.0 cs/sec cs_per_second - 0 cpu-migrations:u # 0.0 migrations/sec migrations_per_second - 132 page-faults:u # 4.1 faults/sec page_faults_per_second - 32,436.48 msec task-clock:u # nan CPUs CPUs_utilized - 356,775 branch-misses:u # 0.0 % branch_miss_rate (88.89%) - 63,706,083,988 branches:u # 1964.0 M/sec branch_frequency (88.89%) - 134,126,037,336 cpu-cycles:u # 4.1 GHz cycles_frequency (88.89%) - 300,951,617,557 instructions:u # 2.2 instructions insn_per_cycle (88.89%) - TopdownL1 # 4.4 % tma_backend_bound - # 61.4 % tma_bad_speculation (88.89%) - # 0.0 % tma_frontend_bound (77.78%) - # 34.2 % tma_retiring (88.89%) - - 32.440411262 seconds time elapsed - - 32.234190000 seconds user - 0.002939000 seconds sys</code></pre> -<p>This is the command I run most frequently when validating optimizations where the optimization is targeting a portion of the codebase with an outsized footprint. If the program is more uniformly expensive, <code>perf stat</code> is likely less useful than a full report / flamegraph.</p> -<h2 id="what-to-optimize">What to optimize</h2> -<p>Start with the functions that are costing the most CPU time. Often a very small number of functions take up the majority of the computation, and these are the ones worth optimizing. As it relates to optimization in this context, I don’t expect most people to be hand coding assembly, and if you are, you already know all of this anyways. This is for the people just building normal software, consider what’s costly and ask yourself two questions:</p> -<ol type="1"> -<li>Would it be possible to call this function less often</li> -<li>Would it be possible to make each call of this function perform less work</li> -</ol> -<p>Work is key for most optimization; cache friendliness is important, but what I really want from software I use is for it to not leave tons of performance on the table by using bad algorithms, performing unexpected / unnecessary copies, or calling functions (like draw functions) when they don’t have to.</p> -<h2 id="amfora">Amfora</h2> -<p>Something useful to understand when profiling is the ideal case. Without a conception of how long (roughly) reading a file should take, transferring data, or drawing text, it’s hard to understand if performance is to be expected or abnormal. This is very contextual, so I can’t answer every question that’s relevant for this, but something I’ve found useful is comparing what I have with something that’s similar in ways I’d anticipate that result in similar performance to see where each implementation is spending a disproportionate amount of cycles.</p> -<p>To this end, when I was working on my Gemini protocol browser I wanted to compare it against a well known browser. Amfora states in the readme that it “aims to be the best looking Gemini client with the most features”, and while I absotlutely detest the idea that more features means slower in all cases, they do have some features that make it slower*. Specifically, they have syntax highlighting and per character coloring, neither of which my browser has, instead opting for a simpler line based coloring approach which I believe still results in nice looking gemtext because it’s all line based at the end of the day.</p> -<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">&quot;lf.txt&quot;</span>, <span class="st">&quot;w&quot;</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">&quot;</span><span class="ch">\n</span><span class="st">&quot;</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 - -Performance counter stats for &#39;amfora lf.txt&#39;: - - 0 context-switches:u # 0.0 cs/sec cs_per_second - 0 cpu-migrations:u # 0.0 migrations/sec migrations_per_second - 2,742 page-faults:u # 9306.7 faults/sec page_faults_per_second - 294.63 msec task-clock:u # nan CPUs CPUs_utilized - 1,052,499 branch-misses:u # 0.2 % branch_miss_rate (90.02%) - 575,712,764 branches:u # 1954.1 M/sec branch_frequency (89.14%) - 955,922,330 cpu-cycles:u # 3.2 GHz cycles_frequency (87.42%) - 2,669,651,845 instructions:u # 2.8 instructions insn_per_cycle (88.36%) - TopdownL1 # 6.7 % tma_backend_bound - # 24.9 % tma_bad_speculation (90.11%) - # 16.4 % tma_frontend_bound (79.92%) - # 52.0 % tma_retiring (90.37%) - - 0.473990805 seconds time elapsed - - 0.272938000 seconds user - 0.021266000 seconds sys -</code></pre> -<p>This is quite unscientific because I simply Ctrl-C’d out once the first characters had rendered. Here’s the same for foreseen:</p> -<pre><code>perf stat foreseen lf.txt - - Performance counter stats for &#39;foreseen lf.txt&#39;: - - 0 context-switches:u # 0.0 cs/sec cs_per_second - 0 cpu-migrations:u # 0.0 migrations/sec migrations_per_second - 8,936 page-faults:u # 104842.2 faults/sec page_faults_per_second - 85.23 msec task-clock:u # nan CPUs CPUs_utilized - 209,148 branch-misses:u # 0.1 % branch_miss_rate (88.30%) - 176,970,330 branches:u # 2076.3 M/sec branch_frequency (88.29%) - 248,919,966 cpu-cycles:u # 2.9 GHz cycles_frequency (88.29%) - 736,194,375 instructions:u # 3.0 instructions insn_per_cycle (88.99%) - TopdownL1 # 3.9 % tma_backend_bound - # 29.7 % tma_bad_speculation (89.50%) - # 21.2 % tma_frontend_bound (77.84%) - # 45.2 % tma_retiring (88.33%) - - 0.354029324 seconds time elapsed - - 0.066861000 seconds user - 0.021186000 seconds sys</code></pre> -<p>Of these metrics, the most comparable one is CPU cycles because they aren’t spinning during the time it took me to react to the text having been rendered, but even so, this isn’t a very good benchmark, and it’s not something I’d post about.</p> -<p>Since Amfora can highlight code, let’s see how the performance comparse if we just add our python code block to the top of that file, and rename it to lf.gmi:</p> -<pre><code>perf stat amfora lf.gmi - - Performance counter stats for &#39;amfora lf.gmi&#39;: - - 0 context-switches:u # 0.0 cs/sec cs_per_second - 0 cpu-migrations:u # 0.0 migrations/sec migrations_per_second - 900,174 page-faults:u # 12090.4 faults/sec page_faults_per_second - 74,453.35 msec task-clock:u # nan CPUs CPUs_utilized - 229,042,926 branch-misses:u # 0.4 % branch_miss_rate (88.94%) - 62,713,218,442 branches:u # 842.3 M/sec branch_frequency (88.85%) - 153,193,058,619 cpu-cycles:u # 2.1 GHz cycles_frequency (88.97%) - 306,159,421,078 instructions:u # 2.0 instructions insn_per_cycle (88.90%) - TopdownL1 # 0.6 % tma_backend_bound - # 97.8 % tma_bad_speculation (88.83%) - # -0.1 % tma_frontend_bound (77.86%) - # 1.7 % tma_retiring (88.91%) - - 28.983517177 seconds time elapsed - - 63.894262000 seconds user - 8.207151000 seconds sys</code></pre> -<p>Woah, that’s a lot slower. With foreseen there shouldn’t be any difference because each line is treated ~ the same, so something’s up here. Let’s look at a flamegraph for this.</p> -<object data="https://blog.laack.co/images/amfora-before.svg" type="image/svg+xml" width="100%"> -</object> -<p>So 68.34% samples occurred while executing the GC, that’s a bit surprising, even given how awful garbage collectors are. We also see that 25.47% of the time, most of the remaining time, is spent in the RenderGemini function. Of this, most of it is spent on concatstring3, a function under the hood that’s used to concatenate strings.</p> -<p>Here’s where that’d be used in the function:</p> -<pre><code>rendered += fmt.Sprintf(&quot;[%s]&quot;, config.GetColorString(&quot;preformatted_text&quot;)) + - buf + fmt.Sprintf(&quot;[%s:%s:-]\r\n&quot;, config.GetColorString(&quot;regular_text&quot;), config.GetColorString(&quot;bg&quot;)) - -... - -rendered += buf + &quot;\r\n&quot; - -... - -rendered += ren - -... - -buf += strings.TrimSuffix(lines[i], &quot;\r&quot;) + &quot;\r\n&quot;</code></pre> -<p>Well, there’s our problem; each of these seemingly innocuous appends is actually very costly. Unlike sensible OO / procedural programming languages, Go strings are immutable so each of these operations will create an entirely new string each time, and since we accumulate into <code>buf</code> until reaching a preformatted text switch line (“```” lines), the file we were trying to open has to re-create the full string 200,000 times, since we only have the tail 200,000 lines in the file don’t have a format switcher.</p> -<p>This is relatively trivial to solve by using a strings.Builder which doesn’t have this issue, and that’s <a href="https://github.com/makew0rld/amfora/pull/370/changes">what I did</a>*. After this change, we can perform our profiling again to find:</p> -<p>(NOTE: The profiling I did to find this differed slightly from what’s shown here, hence the difference in numbers between what we see now, and what’s seen in the PR, but they’re similar enough)</p> -<pre><code>perf stat amfora lf.gmi - - Performance counter stats for &#39;amfora lf.gmi&#39;: - - 0 context-switches:u # 0.0 cs/sec cs_per_second - 0 cpu-migrations:u # 0.0 migrations/sec migrations_per_second - 6,039 page-faults:u # 4871.9 faults/sec page_faults_per_second - 1,239.55 msec task-clock:u # nan CPUs CPUs_utilized - 3,631,996 branch-misses:u # 0.1 % branch_miss_rate (89.02%) - 3,017,891,055 branches:u # 2434.7 M/sec branch_frequency (88.96%) - 4,654,531,574 cpu-cycles:u # 3.8 GHz cycles_frequency (88.93%) - 14,168,541,807 instructions:u # 3.1 instructions insn_per_cycle (88.81%) - TopdownL1 # 2.2 % tma_backend_bound - # 36.9 % tma_bad_speculation (88.80%) - # 18.5 % tma_frontend_bound (77.83%) - # 42.5 % tma_retiring (88.90%) - - 1.185772969 seconds time elapsed - - 1.193871000 seconds user - 0.039062000 seconds sys</code></pre> -<p>Much better, that’s now using 32x less cycles, and is far faster than before.</p> -<object data="https://blog.laack.co/images/amfora-after.svg" type="image/svg+xml" width="100%"> -</object> -<p>Looking again at the flamegraph, we see the RenderGemini function is now mostly performing word wrapping, and the GC is only utilizing the CPU during 16.48% of our samples, much better.</p> -<p>We can go deeper into this if we’d like, but there are now 3 ~equally costly groupings, and as performance becomes more diffuse, optimizations will generally have a smaller amount of impact on perf, so we can leave this where it is.</p> -<h2 id="refutations">Refutations</h2> -<p>I think everything above paints a picture that you don’t have to put in that much effort to, in many cases, make things a lot better, and you don’t have to be that well versed in optimization either.</p> -<p>I often hear arguments about how we should just let other people make software and we’ll just use it and be happy because they’re smarter / better than us at it, this is often the case with libraries, and I so vehemently disagree with this idea. Amfora is a piece of software with 1.4K Github stars, that’s existed for &gt; 6 years with 54 contributors; it’s not tiny, and yet a glaring issue took this long to find and solve.</p> -<p>While I do think there is blame in this situation, I don’t think there should be any shame in writing bad software, but it’s all of our jobs to make our software a little less bad over time, hopefully you can do the same, and help others with that too.</p> -<h2 id="whats-next">What’s Next</h2> -<p>Consider learning memory profiling next. I didn’t directly discuss the topic because 1) it’s more complex and 2) memory related performance issues often manifest, like we saw in the go example, as CPU performance issues too. I also didn’t discuss anything related to instrumented profiling; everything I’ve shown with perf can be done without changing any code. When you can change the code, you have the ability to get more granular than just getting metrics on the basis of functions. Despite the upsides of instrumented profiling, I rarely do it because it often takes more time. The main context where I do it is to get shareable information with others, in a working context, and for intra-function profiling.</p> -<p>Memory stuff is also more complex across languages, where they do weird stuff like go not using malloc.</p> -</body> -</html> diff --git a/posts/site_wip/style.css b/posts/site_wip/style.css @@ -0,0 +1,145 @@ + video { + width: 100%; + } + .spacer{ + height: 50px; + } + + th { + font-weight: bold; + border: 1px solid #dddddd; + padding: 5px 20px; + border: 1px solid #dddddd; + + } + + div.sourceCode { + background-color: #eeeeee; + } + div.sourceCode > pre.sourceCode{ + background-color: transparent; + } + +pre { + font-family: 'LMMono10', 'Latin Modern Mono', monospace; + font-size: 1rem; + padding: 0.75rem; + background-color: #eeeeee; + max-width: 100%; + overflow-x: auto; + box-sizing: border-box; +} + .pgp-container { + text-align: center; + padding: 20px; + margin-top: 20px; + background-color: #ffffff; + width: 100%; + max-width: 900px; + margin-left: auto; + margin-right: auto; + } + .pgp-key { + white-space: pre-wrap; + word-wrap: break-word; + } + body { + font-family: 'LMRoman10', sans-serif; + font-size: 18px; + color: #111; + background-color: white; + max-width: 1280px; + margin: 0 auto; + padding: 25px 50px; + box-sizing: border-box; + } + .landing { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + min-height:100vh; min-height:100dvh; + text-align: center; + } + svg { width: 100%; height: auto; } + .svg-container { width: 100%; max-width: 400px; display: inline-block; } + .separator { letter-spacing: -9px; } + hr { border: none; height: 2px; background-color: lightgrey; width: 100%; margin: 30px auto; } + table { + border-collapse: collapse; + text-align: left; + display: block; + max-width: 100%; + overflow-x: auto; + } + td { + padding: 5px 20px; + border: 1px solid #dddddd; + } + .quiet, .links-container a { color: inherit; } + .links-container { font-size: 24px; margin: 20px; } + .links-container a { text-decoration: none; margin: 0 10px; } + .product-photo { display: block; max-height: 640px; width: 100%; margin: 0 auto; } + .faqtable dt { margin-bottom: 0.75em; font-style: italic; } + .faqtable dd { margin-left: 0; margin-bottom: 2em; } + +h1 { + font-family:'LMRoman10',sans-serif; + font-size: 2.00rem; + margin-top: 0px; + margin-bottom: 10px; + text-align: left; +} + +h3 { + font-family:'LMRoman10',sans-serif; + font-size: 1.5rem; +} + + + +h2 { + font-family:'LMRoman10',sans-serif; + margin-bottom: 10px; + font-size: 1.75rem; + margin-top: 0px; + text-align: left; +} +/* Styling for the navigation container */ +.navigation { + display: flex; + justify-content: left; + align-items: center; + gap: 15px; + font-size: 1.25rem; + font-family:'LMRoman10', sans-serif; +} + +/* Styling for the links */ +.navigation a { + text-decoration: none; + color: #333; /* Dark gray for a clean, modern look */ + transition: color 0.3s; +} + +.navigation a:hover { + color: #007bff; +} + +a { + color: #007bff; + text-decoration: none; +} + +a:hover { + color: #0056b3; + text-decoration: underline; +} + + +/* Styling for the separator */ +.separator { + color: #ccc; /* Lighter gray for separator */ + font-weight: normal; + margin: 0 5px; +} diff --git a/posts/wip/basic-software-profiling-cpu-usage.md b/posts/wip/basic-software-profiling-cpu-usage.md @@ -330,6 +330,7 @@ Much better, that's now using 32x less cycles, and is far faster than before. <object data="https://blog.laack.co/images/amfora-after.svg" type="image/svg+xml" width="100%"></object> + Looking again at the flamegraph, we see the RenderGemini function is now mostly performing word wrapping, and the GC is only utilizing the CPU during 16.48% of our samples, much better. We can go deeper into this if we'd like, but there are now 3 ~equally costly groupings, and as performance becomes more diffuse, optimizations will generally have a smaller amount of impact on perf, so we can leave this where it is.