Skip to content

Instantly share code, notes, and snippets.

@rcj4747
Last active February 9, 2019 19:46
Show Gist options
  • Save rcj4747/39d2da825c6d7bad87808f3a4e1ee374 to your computer and use it in GitHub Desktop.
Save rcj4747/39d2da825c6d7bad87808f3a4e1ee374 to your computer and use it in GitHub Desktop.
Sum bytes read by a process (and children)
#!/bin/bash -e
# Measure the bytes read by a command
# count_command_io <cmd> <args>*
# Credit to shodanex from https://stackoverflow.com/a/8853287
if [ -z $@ ] ; then
echo "Usage: $0 <cmd> <args>*"
exit 1
fi
# Create a fifo in a temp directory
logdir=$(mktemp -d)
logpipe="$logdir/logpipe"
# The fifo saves us from consuming a lot of disk space for the strace log
mkfifo "$logpipe"
function cleanup() {
rm -rf "$logdir"
}
trap cleanup EXIT
# strace only read/write syscalls for the wrapped command (run in background)
# -f traces child processes too
sudo strace -fe trace=read,write -o "$logpipe" "$@" &
# This could be expanded to look at bytes written as well
grep read "$logpipe" | awk 'BEGIN {FS=") = "}{ sum += $2} END {print sum}'
echo "bytes read"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment