计算单个进程的总磁盘 i/o

问题描述 投票:0回答:2

我正在寻找一些工具,可以在单个进程结束后转储总磁盘 I/O。 到目前为止我的发现是:-

  • iotop= 它实时显示每个进程的 i/o,但不给出 流程结束后总计。
  • iostat= 它显示实时 I/O,但是 不告诉过程

例如,我有一些进程在后台运行,PID 为####。在进程结束后,我需要该进程的总字节数WrittenRead。任何人都可以告诉我如何在给定进程PID的情况下提取此信息。

linux bash process io disk
2个回答
6
投票

随意玩这个涂鸦(myio.sh):

#!/bin/bash 

TEMPFILE=$(tempfile)    # create temp file for results

trap "rm $TEMPFILE; exit 1" SIGINT  # cleanup after Ctrl+C

SECONDS=0               # reset timer

$@ &                    # execute command in background

IO=/proc/$!/io          # io data of command
while [ -e $IO ]; do
    cat $IO > "$TEMPFILE"   # "copy" data
    sed 's/.*/& Bytes/' "$TEMPFILE" | column -t
    echo
    sleep 1
done

S=$SECONDS              # save timer

echo -e "\nPerformace after $S seconds:"
while IFS=" " read string value; do
    echo $string $(($value/1024/1024/$S)) MByte/s
done < "$TEMPFILE" | column -t

rm "$TEMPFILE"          # remove temp file

语法:

./myio.sh <your command>

示例:

  • ./myio.sh dd if=/dev/zero of=/dev/null bs=1G count=4096
  • 作为根:
    ./myio.sh dd if=/dev/sda1 of=/dev/null bs=1M count=4096

仅当您知道自己在做什么时,请更改上一个示例中 dd 的

of=


通过我提供的这个简单脚本,您可以观察已经运行的进程及其 IO。

语法:

pio.sh PID

#!/bin/bash

[ "$1" == "" ] && echo "Error: Missing PID" && exit 1
IO=/proc/$1/io          # io data of PID
[ ! -e "$IO" ] && echo "Error: PID does not exist" && exit 2
I=3                     # interval in seconds
SECONDS=0               # reset timer

echo "Watching command $(cat /proc/$1/comm) with PID $1"

IFS=" " read rchar wchar syscr syscw rbytes wbytes cwbytes < <(cut -d " " -f2 $IO | tr "\n" " ")

while [ -e $IO ]; do
    IFS=" " read rchart wchart syscrt syscwt rbytest wbytest cwbytest < <(cut -d " " -f2 $IO | tr "\n" " ")

    S=$SECONDS
    [ $S -eq 0 ] && continue

cat << EOF
rchar:                 $((($rchart-$rchar)/1024/1024/$S)) MByte/s
wchar:                 $((($wchart-$wchar)/1024/1024/$S)) MByte/s
syscr:                 $((($syscrt-$syscr)/1024/1024/$S)) MByte/s
syscw:                 $((($syscwt-$syscw)/1024/1024/$S)) MByte/s
read_bytes:            $((($rbytest-$rbytes)/1024/1024/$S)) MByte/s
write_bytes:           $((($wbytest-$wbytest)/1024/1024/$S)) MByte/s
cancelled_write_bytes: $((($cwbytest-$cwbytes)/1024/1024/$S)) MByte/s
EOF
    echo
    sleep $I
done

0
投票

此方法的工作原理是在运行进程之前将

/proc/[pid]/io/
中的 shell 进程 io 计数器存储在环境变量中,然后在进程结束后减去它们。

有关

/proc/[pid]/io
计数器定义,请参阅:

在此示例中,没有来自/至存储的

read_bytes
write_bytes

> export proc_self_io="";while read line; do proc_self_io+="$line "; done < /proc/self/io

> head -c 100M < /dev/urandom > /dev/null

> perl -ane 'BEGIN{%prev=split / /,$ENV{proc_self_io}};printf( qq{%22s %i\n}, $F[0], $F[1]-$prev{$F[0]} ) ' /proc/$$/io
                rchar: 104863647
                wchar: 104858043
                syscr: 12941
                syscw: 25608
           read_bytes: 0
          write_bytes: 0
cancelled_write_bytes: 0

在此示例中,有

write_bytes
存储:

> export proc_self_io="";while read line; do proc_self_io+="$line "; done < /proc/self/io

> head -c 100M < /dev/urandom > /tmp/test.tmp

> perl -ane 'BEGIN{%prev=split / /,$ENV{proc_self_io}};printf( qq{%22s %i\n}, $F[0], $F[1]-$prev{$F[0]} ) ' /proc/$$/io
                rchar: 104863684
                wchar: 104857948
                syscr: 12978
                syscw: 25621
           read_bytes: 0
          write_bytes: 104857600
cancelled_write_bytes: 0

在此示例中,没有来自/至存储的

read_bytes
write_bytes

> export proc_self_io="";while read line; do proc_self_io+="$line "; done < /proc/self/io

> cat /tmp/test.tmp > /dev/null

> perl -ane 'BEGIN{%prev=split / /,$ENV{proc_self_io}};printf( qq{%22s %i\n}, $F[0], $F[1]-$prev{$F[0]} ) ' /proc/$$/io
                rchar: 104863564
                wchar: 104858183
                syscr: 859
                syscw: 836
           read_bytes: 0
          write_bytes: 0
cancelled_write_bytes: 0

有 0 个

read_bytes
,因为
/tmp/test.tmp
全部在内存中(在核心中)。

> fincore /tmp/test.tmp
  RES PAGES  SIZE FILE
 100M 25600  100M /tmp/test.tmp

dd
可用于从输入文件执行直接IO,所以现在有来自存储的
read_bytes

> export proc_self_io="";while read line; do proc_self_io+="$line "; done < /proc/self/io

> dd if=/tmp/test.tmp of=/dev/null iflag=direct
204800+0 records in
204800+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 5.77554 s, 18.2 MB/s

> perl -ane 'BEGIN{%prev=split / /,$ENV{proc_self_io}};printf( qq{%22s %i\n}, $F[0], $F[1]-$prev{$F[0]} ) ' /proc/$$/io
                rchar: 104863573
                wchar: 104858200
                syscr: 204868
                syscw: 204852
           read_bytes: 104857600
          write_bytes: 0
cancelled_write_bytes: 0

dd
可用于从输入文件到输出文件执行直接 IO:

> export proc_self_io="";while read line; do proc_self_io+="$line "; done < /proc/self/io

> dd if=/tmp/test.tmp of=/tmp/test2.tmp iflag=direct oflag=direct
204800+0 records in
204800+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 11.9083 s, 8.8 MB/s

> perl -ane 'BEGIN{%prev=split / /,$ENV{proc_self_io}};printf( qq{%22s %i\n}, $F[0], $F[1]-$prev{$F[0]} ) ' /proc/$$/io
                rchar: 104863754
                wchar: 104858310
                syscr: 205049
                syscw: 204863
           read_bytes: 104857600
          write_bytes: 104857600
cancelled_write_bytes: 0
> rm /tmp/test.tmp /tmp/test2.tmp
© www.soinside.com 2019 - 2024. All rights reserved.