Btrfs/Compression
Compression[edit | edit source]
Btrfs supports three types of data compression methods. zlib, lzo and zstd.
Algorithm | Compression levels | Default | Description |
---|---|---|---|
zlib | 1-9 | 3 | slow, good compression ratios, default method |
LZO | N/A | N/A | very fast, low compression ratios |
zstd | 1-15 | 3 | slow to very fast, good compression ratios at all levels. |
Setting compression level is available since Linux Kernel 5.1. |
The compression speeds with zstd and lzo are generally real-time. That is that they are faster than the storage medium can read or write. This will increase the overall read/write throughput and increased the life span on SSD and flash storage.
Take a look at Btrfs/Zstd benchmarks to see examples on how the compression level affects performance.
Enable/Disable Compression[edit | edit source]
Compression can be enabled with mount options, with chattr +c
[1] or with btrfs property set <filename> compression <type>
[2]. Compression level can only be set with mount[3].
To see if a file is compressed you can use lsattr
[4]:
# lsattr --------c----------- ./myfile.db
zlib[edit | edit source]
Zlib is the default method used unless specified otherwise.
Enable zlib with one of these options:
chattr +c <filename> # Enables compression chattr -c <filename> # Disables compression btrfs property set <filename> compression zlib # Enables compression btrfs property set <filename> compression none # Disables compression mount -o compress # Enables compression mount -o compress=zlib:5 # Enables compression level 5
LZO[edit | edit source]
LZO is extremely very fast but offers worse compression ratios that zlib and zstd. It is available since the beginning of Btrfs.
Enable LZO with one of these options:
btrfs property set <filename> compression lzo # Enables compression btrfs property set <filename> compression none # Disables compression chattr -c <filename> # Disables compression mount -o compress=lzo # Enables compression
zstd[edit | edit source]
Zstd is the newest compression algorithm in Btrfs. It is available since Linux Kernel 4.14. It offers good compression ratios at very high speeds. By choosing compression level you can tailor performance to your workloads even better.
Enable Zstd with one these options:
btrfs property set <filename> compression zstd # Enables compression btrfs property set <filename> compression none # Disables compression chattr -c <filename> # Disables compression mount -o compress=zstd # Enables compression mount -o compress=zstd:5 # Enables compression level 5
Note about compress-force. Btrfs contains an internal heuristics that determines if some data is compressible so that it doesn't try to compress data that isn't compressible as this wastes CPU time. However, Zstd's internal algorithm already does this very efficiently. Therefore it can better to use compress-force mount option with Zstd. One downside with compress-force is with non-compressible files in that it can creates excess amounts of extents.
Benchmark[edit | edit source]
Best way to know what compression levels suit your needs is to simply try out the different compression levels and options. This only tests the zstd implementation, not how it interacts with the filesystem. There are many factors affecting read/write speeds and a quick benchmark does not give all answers.
A quick one-line benchmark for zstd would otherwise be:
# for i in {1..15}; do zstd -b$i -i3 digikam4.db; done
This will benchmark zstd and iterate through all levels between 1 and 15. Replace digikam4.db with the file you want to benchmark.