Linux中的磁盘空间使用情况分析

时间:2016-05-23 09:09:06

标签: c++ linux

我在Linux [Red Hat]上运行C ++程序,该程序在硬盘上创建临时文件以计算其结果。我需要知道该程序在运行时从磁盘中使用了多少空间。我无法更改源代码以保留文件,因此我可以在生成结果之前和之后减去Program文件夹的大小。在这种情况下,我可以使用任何分析工具或命令行来帮助我。

2 个答案:

答案 0 :(得分:0)

我想,你可以在终端运行程序之前和之后使用du -h /path/todir

  

du估计文件空间使用情况

     

-h, - 人类可读                 人类可读格式的打印尺寸(例如,1K 234M 2G)

如果您需要更多选项,请查看man du

答案 1 :(得分:0)

您可以使用/ proc / [pid] / io中维护的统计数据。

来自/ proc手册页:

此文件包含进程的I / O统计信息               例如:

              # cat /proc/3828/io
              rchar: 323934931
              wchar: 323929600
              syscr: 632687
              syscw: 632675
              read_bytes: 0
              write_bytes: 323932160
              cancelled_write_bytes: 0

write_bytes: bytes written
                     Attempt to count the number of bytes which this process
                     caused to be sent to the storage layer.

您可以编写类似的脚本,

#!/bin/bash
c=0
echo $1
rm /tmp/writtenBytes.txt
psout=`ps aux | grep "$1" | grep -v $0 | grep -v grep `
while [[ "$psout" != "" ]]
do
    pid=`echo $psout | awk '{print $2}'`
    cat /proc/$pid/io  | grep write  | grep -v cancelled >> /tmp/writtenBytes.txt   
    psout=`ps aux | grep "$1" | grep -v $0 | grep -v grep`
    echo $psout
    sleep 1
done

运行此脚本
bash -x getIO.sh "postgres: stats collector"

此脚本将创建文件/tmp/writtenBytes.txt,其中包含由名为“postgres:stats collector”的进程写入的字节数

相关问题