brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: xz -T0 compresses on every CPU core, for a slightly larger file

Linux
~/tips/tip-xz-threads-t0

xz -T0 -k big.log

Splits the input into blocks and compresses them on all cores at once.

xz gives small files, and by default it uses one core to do it. On a quad-core machine that is three cores doing nothing while you wait. One option fixes that.

xz -T0 -k big.log

The help text says -T, --threads=NUM uses at most NUM threads, that the default is 1, and that 0 means as many threads as there are processor cores. The -k keeps the original file. In a pipe it works the same way:

tar -cf - folder | xz -T0 > folder.tar.xz

The price is size. With threads, xz cuts the input into blocks and compresses each block on its own, which costs some compression. For a 92 MiB text file of numbers on a four-core machine, xz -l reported this for a -T0 and a -T1 run:

Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename
    1       4   1349.4 KiB     92.4 MiB  0.014  CRC64   big.T0.xz
    1       1   1203.7 KiB     92.4 MiB  0.013  CRC64   big.T1.xz

Four blocks against one, and about 12 percent bigger for this very repetitive data. Real files will differ, so measure on your own.

Second caveat: more threads need more memory. With -T0 -9 on the same machine xz printed Reduced the number of threads from 4 to 3 to not exceed the memory usage limit of 4022 MiB and carried on. A 1.2 MiB file stayed in a single block even with -T0, so small files gain nothing.

Further reading: XZ Utils

More Linux tips notify-send -r "$id" "Backup" "Done" pgrep -af 'sleep 301' smbstatus -L msmtp --serverinfo --tls --tls-certcheck=off --host=smtp.example.com --port=587 smbclient //server/share -U user -Tc backup.tar "*" All Linux tips (283) All topics

Link