{"by":"201984","descendants":148,"id":40932492,"kids":[40933207,40934590,40934720,40934005,40933298,40934256,40933388,40933908,40934434,40932959,40938244,40933010,40933344,40933847,40935609,40937824,40933888,40933758,40934004,40933300,40932968,40939729,40934822,40933265,40933331,40933933,40935482,40934092,40938633,40941313,40933755,40934778,40949800,40933937,40933037,40935560,40933136,40933889,40938596,40941273,40933770,40934064],"score":396,"text":"&quot;dut&quot; is a disk usage calculator that I wrote a couple months ago in C. It is multi-threaded, making it one of the fastest such programs. It beats normal &quot;du&quot; in all cases, and beats all other similar programs when Linux&#x27;s caches are warm (so, not on the first run). I wrote &quot;dut&quot; as a challenge to beat similar programs that I used a lot, namely pdu[1] and dust[2].<p>&quot;dut&quot; displays a tree of the biggest things under your current directory, and it also shows the size of hard-links under each directory as well. The hard-link tallying was inspired by ncdu[3], but I don&#x27;t like how unintuitive the readout is. Anyone have ideas for a better format?<p>There&#x27;s installation instructions in the README. dut is a single source file, so you only need to download it and copy-paste the compiler command, and then copy somewhere on your path like &#x2F;usr&#x2F;local&#x2F;bin.<p>I went through a few different approaches writing it, and you can see most of them in the git history. At the core of the program is a datastructure that holds the directories that still need to be traversed, and binary heaps to hold statted files and directories. I had started off using C++ std::queues with mutexes, but the performance was awful, so I took it as a learning opportunity and wrote all the datastructures from scratch. That was the hardest part of the program to get right.<p>These are the other techniques I used to improve performance:<p>* Using fstatat(2) with the parent directory&#x27;s fd instead of lstat(2) with an absolute path. (10-15% performance increase)<p>* Using statx(2) instead of fstatat. (perf showed fstatat running statx code in the kernel). (10% performance increase)<p>* Using getdents(2) to get directory contents instead of opendir&#x2F;readdir&#x2F;closedir. (also around 10%)<p>* Limiting inter-thread communication. I originally had fs-traversal results accumulated in a shared binary heap, but giving each thread a binary-heap and then merging them all at the end was faster.<p>I couldn&#x27;t find any information online about fstatat and statx being significantly faster than plain old stat, so maybe this info will help someone in the future.<p>[1]: <a href=\"https:&#x2F;&#x2F;github.com&#x2F;KSXGitHub&#x2F;parallel-disk-usage\">https:&#x2F;&#x2F;github.com&#x2F;KSXGitHub&#x2F;parallel-disk-usage</a><p>[2]: <a href=\"https:&#x2F;&#x2F;github.com&#x2F;bootandy&#x2F;dust\">https:&#x2F;&#x2F;github.com&#x2F;bootandy&#x2F;dust</a><p>[3]: <a href=\"https:&#x2F;&#x2F;dev.yorhel.nl&#x2F;doc&#x2F;ncdu2\" rel=\"nofollow\">https:&#x2F;&#x2F;dev.yorhel.nl&#x2F;doc&#x2F;ncdu2</a>, see &quot;Shared Links&quot;","time":1720654196,"title":"Show HN: Dut – a fast Linux disk usage calculator","type":"story","url":"https://codeberg.org/201984/dut"}