Last active
February 9, 2019 19:46
-
-
Save rcj4747/39d2da825c6d7bad87808f3a4e1ee374 to your computer and use it in GitHub Desktop.
Sum bytes read by a process (and children)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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