Lurker News
"dut" 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 "du" in all cases, and beats all other similar programs when Linux's caches are warm (so, not on the first run). I wrote "dut" as a challenge to beat similar programs that I used a lot, namely pdu[1] and dust[2].

"dut" 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't like how unintuitive the readout is. Anyone have ideas for a better format?
There'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 /usr/local/bin.
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.
These are the other techniques I used to improve performance:
* Using fstatat(2) with the parent directory's fd instead of lstat(2) with an absolute path. (10-15% performance increase)
* Using statx(2) instead of fstatat. (perf showed fstatat running statx code in the kernel). (10% performance increase)
* Using getdents(2) to get directory contents instead of opendir/readdir/closedir. (also around 10%)
* 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.
I couldn'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.
[1]: https://github.com/KSXGitHub/parallel-disk-usage
[2]: https://github.com/bootandy/dust
[3]: https://dev.yorhel.nl/doc/ncdu2, see "Shared Links"

Comments (148) 42 threads

Show all comments

Showing thread 15 of 42

imiric1 reply#
I've been using my own function with `du` for ages now, similar to others here, but I appreciate new tools in this space.

I gave `dut` a try, but I'm confused by its output. For example:

  3.2G    0B |- .pyenv
  3.4G    0B | /- toolchains
  3.4G    0B |- .rustup
  4.0G    0B | |-
  4.4G    0B | /-
  9.2G    0B |- Work
  3.7G    0B |   /- flash
  3.8G    0B | /-
   16G  4.0K |- Downloads
  5.1G    0B | |-
  5.2G    0B | /-
   16G    0B |- Projects
  3.2G   42M | /-
   17G  183M |- src
   17G    0B | /-
   17G    0B |- Videos
  3.7G    0B | /- Videos
   28G    0B |- Music
  6.9G    0B | |- tmp
  3.4G    0B | | /- tmp
  8.8G    0B | |- go
  3.6G    0B | |   /- .versions
  3.9G    0B | | |- go
  8.5G    0B | | |     /- dir
  8.5G    0B | | |   /- vfs
  8.5G    0B | | | /- storage
  8.5G    0B | | /- containers
   15G  140M | /- share
   34G  183M /- .local
  161G    0B .
- I expected the output to be sorted by the first column, yet some items are clearly out of order. I don't use hard links much, so I wouldn't expect this to be because of shared data.

- The tree rendering is very confusing. Some directories are several levels deep, but in this output they're all jumbled, so it's not clear where they exist on disk. Showing the full path with the `-p` option, and removing indentation with `-i 0` somewhat helps, but I would almost remove tree rendering entirely.

2019841 reply#
It is being sorted by the first column, but it also keeps subdirectories with each other. Look at the order of your top-level directories.

  3.2G    0B |- .pyenv
  3.4G    0B |- .rustup
  9.2G    0B |- Work
   16G  4.0K |- Downloads
   17G  183M |- src
   28G    0B |- Music
   34G  183M /- .local
If you don't want the tree output and only want the top directories, you can use `-d 1` to limit to depth=1.
imiric1 reply#
Ah, I see, that makes sense.

But still, the second `Videos` directory of 3.7G is a subdirectory of `Music`, so it should appear below it, no? Same for the two `tmp` directories, they're subdirectories of `.local`, so I would expect them to be listed under it. Right now there doesn't seem to be a clear order in either case.

db48x1 reply#
Subdirectories cannot be larger than the directory that they are within, so they cannot be sorted _below_ their parent. Thus, the tree branches upwards, not downwards. The root is at the bottom, where a tree’s root should be!

Incidentally, dust sorts things the same way but presents it with a nicer tree:

     db48x  ~  1  dust -Db
    2.6G     ┌── saves
    2.6G   ┌─┴ .factorio
    3.4G   │   ┌── Steam
    3.4G   │ ┌─┴ share
    3.4G   ├─┴ .local
    1.8G   │ ┌── EgoSoft
    3.5G   ├─┴ .config
    7.2G   │ ┌── Amadeus (1984) DC (1080p BluRay x265 HEVC 10bit AAC 5.1 Tigole)
     16G   │ ├── Amadeus.1984.DC.INTERNAL.REPACK.1080p.BluRay.x264-CLASSiC[rarbg]
     23G   ├─┴ video
    1.9G   │   ┌── build
    2.0G   │ ┌─┴ notcurses
    2.2G   │ ├── wezterm
    2.1G   │ │ ┌── master
    2.6G   │ │ │   ┌── tiles
    2.6G   │ │ │ ┌─┴ obj
    4.1G   │ │ ├─┴ missions
    2.6G   │ │ │   ┌── tiles
    2.6G   │ │ │ ┌─┴ obj
    4.1G   │ │ ├─┴ iteminfo
    5.6G   │ │ ├── follower_rules
    8.5G   │ │ │     ┌── pack
    8.6G   │ │ │   ┌─┴ objects
    8.6G   │ │ │ ┌─┴ .git
     10G   │ │ ├─┴ uilist
     26G   │ ├─┴ cataclysm
     33G   ├─┴ src
     72G ┌─┴ .
imiric#
Gotcha, thanks for explaining.

Yeah, I guess my confusion was with how the tree is rendered in dut. The pipe rendering of dust makes this clearer.