【问题标题】:Calculate Total disk i/o by a single process通过单个进程计算 Total disk i/o
【发布时间】:2014-09-03 01:38:01
【问题描述】:

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

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

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

【问题讨论】:

标签: linux bash process io disk


【解决方案1】:

随意玩这个涂鸦(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 &lt;your command&gt;

例子:

  • ./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

【讨论】:

  • 每个值的含义是什么?就像 mysql 一样,我应该记下哪个值来计算我的 mysql 进程使用的确切磁盘 I/O?
  • @Vineeth:这应该对你有帮助:Understanding the counters in /proc/pid/io
  • 兄弟,这有帮助。我的任务是将我的本地 mysql 生产安装迁移到 RDS。所以需要计算mysql使用的确切磁盘I/O。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2011-10-19
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多