{"by":"jkool702","descendants":12,"id":45010461,"kids":[45020243,45020614,45041113,45028802],"score":53,"text":"Note: this is an update to [this](<a href=\"https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=44568529\">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=44568529</a>) &quot;Show HN&quot; post.<p>timep is a state-of-the-art [debug-]trap-based bash profiler that is efficient and extremely accurate. Unlike other profilers, timep records:<p>1. per-command wall-clock time<p>2. per-command CPU time, and<p>3. the hierarchy of parent function calls &#x2F;subshells for each command<p>the wall-clock + CPU time combination allows you to determine if a particular command is CPU-bound or IO-bound, and the hierarchical logging gives you a map of <i>how</i> the code actually executed.<p>The standout feature of timep is that it will take these records and automatically generate a bash-native flamegraph (that shows bash commands, not syscalls).<p>------------------------------------------------<p>USAGE<p>timep is extremely easy to use - just source the `timep.bash` file from the repo and add &quot;timep&quot; in front of whatever you want to profile. for example:<p><pre><code>    . &#x2F;path&#x2F;to&#x2F;timep.bash\n    timep .&#x2F;some_script\n    echo &quot;stdin&quot; | timep some_function\n</code></pre>\nZERO changes need to be made to the code being profiled!<p>------------------------------------------------<p>EXAMPLES<p>[test code that will be profiled](<a href=\"https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;timep.tests.bash\" rel=\"nofollow\">https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;timep.test...</a>)<p>[output profile for that test code](<a href=\"https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;OUTPUT&#x2F;out.profile\" rel=\"nofollow\">https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;OUTPUT&#x2F;out...</a>)<p>[flamegraph for that test code](<a href=\"https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;OUTPUT&#x2F;flamegraph.ALL.svg\" rel=\"nofollow\">https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;OUTPUT&#x2F;fla...</a>)<p>[flamegraph from a &quot;real world&quot; test of &quot;forkrun&quot;, a parallelization engine written in bash](<a href=\"https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;FORKRUN&#x2F;flamegraph.ALL.svg\" rel=\"nofollow\">https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;FORKRUN&#x2F;fl...</a>)<p>In the &quot;forkrun test&quot;, 13 different checksums were computed for ~670k small files on a ramdisk using 28 parallel workers. this was repeated twice. In total, this test ran around 67,000 individual bash commands. [This is its `perf stat` (without timep)](<a href=\"https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;FORKRUN&#x2F;perf.compare.txt\" rel=\"nofollow\">https:&#x2F;&#x2F;github.com&#x2F;jkool702&#x2F;timep&#x2F;blob&#x2F;main&#x2F;TESTS&#x2F;FORKRUN&#x2F;pe...</a>).<p>------------------------------------------------<p>EFFICIENCY AND ACCURACY<p>The forkrun test (see &quot;examples&quot; section above) was basically as demanding of a workload as one can have in bash.  it fully utilized 24.5 cores on a 14c&#x2F;28t i9-7940x CPU, racking up &gt;840 seconds of CPU time in ~34.5 seconds of wall-clock time. When profiling this group of 67,000 commands with timep:<p>1. the time it took for the code to run with the debug-trap instrumentation was ~38 seconds, an increase of just slightly over 10%. CPU time had a similiar increase.<p>2. the time profile was ready at +2 minutes (1 minute + 15 seconds after the profiling run finished)<p>3. the flamegraphs were ready at +5 minutes (4 minute + 15 seconds after the profiling run finished)<p>Note that timep records both &quot;start&quot; and &quot;stop&quot; timestamps for every command, and the debug trap instrumentation runs between one commands &quot;stop&quot; timestamp and the next commands &quot;start&quot; timestamp, meaning the error in the profiles timings is far less than the 10% overhead. Comparing the total (sys+user) CPU time that perf stat gave (without using timep) and the CPU time timep gives (from summing together the CPU time of all 67,000-ish commands), the difference is virtually always less than 0.5%, and often less than 0.2%. Ive seen as low as 0.04%, which is 1&#x2F;3 of a second on a run that took ~850 seconds of CPU time.<p>------------------------------------------------<p>MAJOR CHANGES SINCE THE LAST &quot;SHOW HN&quot; POST<p>1. CPU time is now recorded too (instead of just wall-clock time). This is done via a loadable builtin that calls `getrusage` and (if available) `clock_gettime` to efficiently and accurate determine the CPU time of the process and all its descendants.<p>2. the .so file required to use the loadable builtin mentioned in #1 is built directly into the script has an embedded compressed base64 sequence. I also developed the bash-native compression scheme that it uses. The .so files for x86_64, aarch64, ppc64le and i686 are all included. Im hoping to add arm7 soon as well. the flamegraph generator perl script is also embedded, making the script 100% fully self-contained. NOTE: these embedded base64 strings include both sha256 and md5 checksums of the resulting .so file that are verified on extraction.<p>3. the flamegraph generation has been completely overhauled. The flamegraphs now a) are colored based on runtime (hot colors = longer runtime), b) desaturate colors for commands where cpu time &lt;&lt; wall-clock time (e.g., blocking reads, sleep, wait, ...), and c) use a runtime-weighted CDF color mapping that ensures, regardless of the distribution of the underlying data, that the resulting flamegraph has a roughly equal amount of each color in the colorspace (where &quot;equal&quot; means &quot;the same number of pixels are showing each color&quot;). timep also combines multiple flamegraphs (that show wallclock time vs cpu time and that us the full vs folded set of traces) by vertically stacking them into a single SVG image, giving &quot;dual stack&quot; and &quot;quad stack&quot; flamegraphs.<p>4. the post-processing workflow has been basically completely re-written, making it more robust, easier to understand&#x2F;maintain, and much faster. The &quot;forkrun&quot; test linked above (that ran 67,000 commands) previously took ~20 minutes. With the new version, you can get a profile in 2 minutes or a profile + flamegraph in 5 minutes - a 4x to 10x speedup!","time":1756099064,"title":"Show HN: Timep – A next-gen profiler and flamegraph-generator for bash code","type":"story","url":"https://github.com/jkool702/timep"}