O arquivo em /proc/<pid>/io
representa o que você precisa. É um trabalho de script de bit para obter uma saída semelhante a iotop
. Veja a documentação do kernel do Linux para os valores do arquivo /proc/<pid>/io
:
rchar
I/O counter: chars read The number of bytes which this task has caused to be read from storage. This is simply the sum of bytes which this process passed to read() and pread(). It includes things like tty IO and it is unaffected by whether or not actual physical disk IO was required (the read might have been satisfied from pagecache)
wchar
I/O counter: chars written The number of bytes which this task has caused, or shall cause to be written to disk. Similar caveats apply here as with rchar.
[...]
read_bytes
I/O counter: bytes read Attempt to count the number of bytes which this process really did cause to be fetched from the storage layer. Done at the submit_bio() level, so it is accurate for block-backed filesystems.
write_bytes
I/O counter: bytes written Attempt to count the number of bytes which this process caused to be sent to the storage layer. This is done at page-dirtying time.
Agora, você pode usar esse pequeno script bash
:
#!/bin/bash
if [ "$(id -u)" -ne 0 ] ; then
echo "Must be root" 2>&1
exit 1
fi
delay=2
lista=$(for p in $(pgrep "."); do echo -n "$p "; grep ^rchar /proc/$p/io 2>/dev/null; done)
while :; do
echo "-----"
listb=$(for p in $(pgrep "."); do echo -n "$p "; grep ^rchar /proc/$p/io 2>/dev/null; done)
echo "$lista" | while read -r pida xa bytesa; do
[ -e "/proc/$pida" ] || continue
echo -en "$pida:\t"
bytesb=$(echo "$listb" | awk -v pid=$pida '$1==pid{print $3}')
echo "$((($bytesb - $bytesa) / $delay)) b/s"
done | sort -nk2 | tail
sleep $delay
listb=$lista
done
Ele cria duas listas com um atraso de 2 segundos ( $delay
: pode ser induzido ou diminuído) entre elas e compara as listas e calcula as diferenças. Os 10 processos com mais E / S são impressos com sua banda de E / S nos últimos 2 segundos. Se você quiser escrever E / S em vez de ler E / S, basta editar o comando grep
nas listas para wchar
em vez de rchar
:
lista=$(for p in $(pgrep "."); do echo -n "$p "; grep ^wchar /proc/$p/io 2>/dev/null; done)
listb=$(for p in $(pgrep "."); do echo -n "$p "; grep ^wchar /proc/$p/io 2>/dev/null; done)