Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active January 11, 2022 11:51
Show Gist options
  • Save kou1okada/d688059641ce69c0ad9146be8b81fd88 to your computer and use it in GitHub Desktop.
Save kou1okada/d688059641ce69c0ad9146be8b81fd88 to your computer and use it in GitHub Desktop.
procio.sh: Show process I/O

procio.sh: Show process I/O

Show process I/O by /proc/<pid>/io.

Usage

procio.sh [OPTIONS ...] <pid>

License

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 "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment