如何测量 Linux 中给定进程的活动导致的净使用磁盘空间变化?
我想监视正在运行的进程的磁盘空间需求。理想情况下,我希望能够指向一个进程并找出由它引起的已用磁盘空间的净变化。在 Linux 中是否有一种简单的方法可以做到这一点? (我很确定在 Solaris 中使用 DTrace 执行此操作是可行的,尽管可能不是很容易)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您必须对其进行 ptrace(或让 strace 为您执行此操作并解析输出),然后尝试找出正在使用的光盘。
这很重要,因为您的跟踪过程需要了解哪些文件操作使用磁盘空间 - 并且不受竞争条件的影响。但是,您也许可以进行近似。
很多东西都会占用磁盘空间,因为大多数 Linux 文件系统都支持“holes”。我想您也可以出于会计目的数孔。
另一个问题是了解哪些文件系统操作可以释放磁盘空间 - 例如,在某些情况下,打开文件进行写入可能会截断它。这显然释放了空间。同样,如果在现有文件上重命名文件,则重命名文件可以释放空间。
另一个问题是调用辅助进程来执行操作的进程 - 例如,如果 myprog 执行 system("rm -rf somedir")。
此外,知道文件何时被完全删除也有些困难,因为它可能已从文件系统中删除,但仍被另一个进程打开。
快乐的黑客:)
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 :)
如果您知道要监视的进程的 PID,您将在
/proc/
中找到大量有关它的信息。文件
/proc//io
包含有关进程读取和写入的字节的统计信息,它应该是您正在寻找的内容。此外,在
/proc//fd/
中,您将找到指向进程打开的所有文件的链接,以便您可以监视它们。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.有适用于 linux 的 Dtrace
http://librenix.com/?inode=13584
Ashitosh
there is Dtrace for linux is available
http://librenix.com/?inode=13584
Ashitosh