Show process I/O by /proc/<pid>/io.
procio.sh [OPTIONS ...] <pid>
This project is distributed under the MIT license.
| #!/usr/bin/env bash | |
| source hhs.bash 0.2.0 | |
| function optparse_procio () | |
| { | |
| case "$1" in | |
| -i|--interval) # <sec> | |
| # Set update interval | |
| nparams 1 | |
| optset INTERVAL "$2" | |
| ;; | |
| *) return 1;; | |
| esac | |
| } | |
| function procio () # [OPTIONS ...] <pid> | |
| # Show process I/O | |
| { | |
| (( $# != 1 )) && { invoke_usage; exit 1; } | |
| local line i k v t t0 t1 t2 dt0 dt1 r0 w0 r1 w1 r2=0 w2=0 | |
| local -A io | |
| printf -v t0 "%(%s)T" | |
| cat /proc/$1/cmdline; echo | |
| printf "T:%-16s %-13s %13s\n" Total Current Average | |
| while true; do | |
| readarray -t line < /proc/$1/io | |
| for i in "${line[@]}"; do | |
| t=( $i ) | |
| io[${t[0]}]=${t[1]} | |
| done | |
| r1="$r2" | |
| w1="$w2" | |
| t1="$t2" | |
| r2="${io[rchar:]}" | |
| w2="${io[wchar:]}" | |
| : ${r0:=$r2} | |
| : ${w0:=$w2} | |
| printf -v t2 "%(%s)T" | |
| dt0=$(( t2-t0 )); dt0=$(( dt0 <= 0 ? 1: dt0 )) | |
| dt1="${OPT_INTERVAL:-1}" | |
| printf "R:%'16d %'13d %'13d\n" "$r2" $(( (r2-r1)/dt1 )) $(( (r2-r0)/dt0 )) | |
| printf "W:%'16d %'13d %'13d\n" "$w2" $(( (w2-w1)/dt1 )) $(( (w2-w0)/dt0 )) | |
| printf "\e[2A" # Cursor Up 2 | |
| sleep "${OPT_INTERVAL:-1}" | |
| done | |
| } | |
| invoke_command "$@" |