My root and home partitions are LUKS-encrypted. I wish to have discard/TRIM enabled on them (I'm aware of security implications). If my understanding is correct, this should result in discarded areas being zeros on a raw disk device and gibberish on cryptsetup-mapped device.
I've made some configuration changes, ran fstrim
and now I want to verify if discard is working. To do that, I want to count how many bytes on my partition are zeros. On a trimmed disk I'd expect that:
count of 0 bytes
÷total partition size
≈percentage of free space
On a non-trimmed, encrypted partition: (assuming all bytes were written at least once)
count of 0 bytes
≈total partition size
÷ 256
Inspired by How to gather byte occurrence statistics in binary file?, I've tried this approach:
sudo pv /dev/disk/by-label/ESP | od -vtu1 -An -w1 | grep -Fx ' 0' | wc -l
but results were disappointing: processing a 500 MB partition took almost 3 minutes. That's 3 MB/s, while my SSD can reach sequential reads of 360 MB/s. I've also noticed that one of my CPU cores was running at 100%, while others were idle, so I guess this could benefit from parallelization. (Impact of pv
is negligible, almost no difference compared to cat
)
What's the fastest way to count 0
bytes on a partition?