Skip to content
Advertisement

Managing Log File Sizes

I am implementing a simple log file handler for an embedded device. I cannot use syslog because it is already reserved for other uses. The device’s SSD size is limited, so there is a real risk of the log file using all of the disk space, which will crash the device.

What is the cheapest way I can guarantee I will have at least X remaining disk space after a write?

Advertisement

Answer

On Linux, the only way to find out the amount of remaining disk space is the statfs(2) syscall. If that’s too slow for you, I think you’ll just have to call it less frequently and assume that you aren’t logging so much in between calls that you’re filling up too much.

On many modern filesystems, it can generally be difficult to try and map how much less free space will remain after a particular write. Not only do you have block-granularity in allocation (or not, in case your filesystem supports tail-packing), but on some filesystems you may also be affected by sudden copy-on-write allocation after data de-duplication, or lazy allocation of zeroed blocks and whatnot. Trying to be too smart about this is bound to get you in trouble when switching between filesystems, so I’d recommend just setting some reasonable low-water mark on available space and stop writing more data after it has been reached.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement