I'd like to monitor disk space requirements of a running process. Ideally, I want to be able to point to a process and find out the net change in used disk space attributable to it. Is there an easy way of doing this in Linux? (I'm pretty sure it would be feasible, though maybe not very easy, to do this in Solaris with DTrace)
3 Answers
Probably you'll have to ptrace it (or get strace to do it for you and parse the output), and then try to work out what disc is being used.
This is nontrivial, as your tracing process will need to understand which file operations use disc space - and be free of race conditions. However, you might be able to do an approximation.
Quite a lot of things can use up disc space, because most Linux filesystems support "holes". I suppose you could count holes as well for accounting purposes.
Another problem is knowing what filesystem operations free up disc space - for example, opening a file for writing may, in some cases, truncate it. This clearly frees up space. Likewise, renaming a file can free up space if it's renamed over an existing file.
Another issue is processes which invoke helper processes to do stuff - for example if myprog does a system("rm -rf somedir").
Also it's somewhat difficult to know when a file has been completely deleted, as it might be deleted from the filesystem but still open by another process.
Happy hacking :)

- 62,604
- 14
- 116
- 151
If you know the PID of the process to monitor, you'll find plenty of information about it in /proc/<PID>
.
The file /proc/<PID>/io
contains statistics about bytes read and written by the process, it should be what you are seeking for.
Moreover, in /proc/<PID>/fd/
you'll find links to all the files opened by your process, so you could monitor them.

- 12,378
- 5
- 40
- 54
-
Looking around in /proc/
/, I found /proc/ – Andrea Spadaccini Feb 04 '11 at 17:20/io and I believe this is what you are searching for, so I edited my answer to mention it. -
`proc/
/io` has bytes written/read information but I don't see how that can be used to infer used disk space; you could be overwriting a file without changing its size, you could be deleting a file. `/proc/ – Cagatay Feb 04 '11 at 19:29/fd` sounds promising, I'll check it out. -
It'll need some intelligence possiably to detect the case where you delete/overwrite, etc but it should work because the raw number/data are there. But anyway using that along with /proc/
/fd and some work/scripting you should be able to roughly determine if something was deleted/replaced. – Pharaun Feb 04 '11 at 22:29