linux – 通过单个进程计算总磁盘i / o

前端之家收集整理的这篇文章主要介绍了linux – 通过单个进程计算总磁盘i / o前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

> iotop =它实时显示每个进程的i / o但不给出
过程结束后的总数.
> iostat =显示实时I / O但是
不告诉过程

例如,我有一些进程在后台运行PID ####.在流程结束后,我需要在整个流程中编写和读取总字节数.任何人都可以告诉我如何在给定流程PID的情况下提取此信息.

最佳答案
随意玩这个涂鸦(myio.sh):

  1. #!/bin/bash
  2. TEMPFILE=$(tempfile) # create temp file for results
  3. trap "rm $TEMPFILE; exit 1" SIGINT # cleanup after Ctrl+C
  4. SECONDS=0 # reset timer
  5. $@ & # execute command in background
  6. IO=/proc/$!/io # io data of command
  7. while [ -e $IO ]; do
  8. cat $IO > "$TEMPFILE" # "copy" data
  9. sed 's/.*/& Bytes/' "$TEMPFILE" | column -t
  10. echo
  11. sleep 1
  12. done
  13. S=$SECONDS # save timer
  14. echo -e "\nPerformace after $S seconds:"
  15. while IFS=" " read string value; do
  16. echo $string $(($value/1024/1024/$S)) MByte/s
  17. done < "$TEMPFILE" | column -t
  18. rm "$TEMPFILE" # remove temp file

语法:./ myio.sh< your command>

例子:

> ./myio.sh dd if = / dev / zero of = / dev / null bs = 1G count = 4096
>以root身份:./ myio.sh dd if = / dev / sda1 of = / dev / null bs = 1M count = 4096

只有在您知道自己在做什么的情况下,才能在最后一个示例中更改=的dd.

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

语法:pio.sh PID

  1. #!/bin/bash
  2. [ "$1" == "" ] && echo "Error: Missing PID" && exit 1
  3. IO=/proc/$1/io # io data of PID
  4. [ ! -e "$IO" ] && echo "Error: PID does not exist" && exit 2
  5. I=3 # interval in seconds
  6. SECONDS=0 # reset timer
  7. echo "Watching command $(cat /proc/$1/comm) with PID $1"
  8. IFS=" " read rchar wchar syscr syscw rbytes wbytes cwbytes < <(cut -d " " -f2 $IO | tr "\n" " ")
  9. while [ -e $IO ]; do
  10. IFS=" " read rchart wchart syscrt syscwt rbytest wbytest cwbytest < <(cut -d " " -f2 $IO | tr "\n" " ")
  11. S=$SECONDS
  12. [ $S -eq 0 ] && continue
  13. cat << EOF
  14. rchar: $((($rchart-$rchar)/1024/1024/$S)) MByte/s
  15. wchar: $((($wchart-$wchar)/1024/1024/$S)) MByte/s
  16. syscr: $((($syscrt-$syscr)/1024/1024/$S)) MByte/s
  17. syscw: $((($syscwt-$syscw)/1024/1024/$S)) MByte/s
  18. read_bytes: $((($rbytest-$rbytes)/1024/1024/$S)) MByte/s
  19. write_bytes: $((($wbytest-$wbytest)/1024/1024/$S)) MByte/s
  20. cancelled_write_bytes: $((($cwbytest-$cwbytes)/1024/1024/$S)) MByte/s
  21. EOF
  22. echo
  23. sleep $I
  24. done

猜你在找的Linux相关文章