Skip to content

Instantly share code, notes, and snippets.

@tomislacker
Last active August 29, 2015 14:10
Show Gist options
  • Save tomislacker/980de8cfe74801704f56 to your computer and use it in GitHub Desktop.
Save tomislacker/980de8cfe74801704f56 to your computer and use it in GitHub Desktop.
Emulate SIGUSR1 Signal Output from DD
#!/bin/bash
###
# dd emulator
###
BYTES_PER_RECORD=512
BYTES_TOTAL=1020837888
BYTES_RATE=$((10*1024*1024))
BYTES_RATE_FUDGE=$((1*1024*1024))
OUTPUT_STATUS_SLEEP=1
get_microtime ()
{
local precision=${1:-4}
echo "scale=${precision}; $(date +%s%${precision}N)/(10^${precision})" | bc -l
}
get_bytes_copied ()
{
local fudgeBytes=$(($(shuf -n 1 -i 0-$((${BYTES_RATE_FUDGE}*2)))-${BYTES_RATE_FUDGE}))
local addBytes=$((${BYTES_RATE}+${fudgeBytes}))
# Ensure returned value is multiple of BYTES_PER_RECORD
echo $((${addBytes}-(${addBytes}%${BYTES_PER_RECORD})))
}
get_MB ()
{
local bytes=$1
echo "scale=0; ${bytes}/1024/1024" | bc -l
}
bytes_to_sectors ()
{
local bytes=$1
echo $((${bytes}/${BYTES_PER_RECORD}))
}
get_MB_persec ()
{
local bytes=$1
local mtime=$2
echo "scale=1; ${bytes}/1024/1024/${mtime}" | bc -l
}
###
# dd SIGUSR1 output looks like this:
# 512 bytes per record
###
# 1038465+0 records in
# 1038465+0 records out
# 531694080 bytes (532 MB) copied, 11.6338 seconds, 45.7 MB/s
###
bytes_written=0
start_time=$(date +%s)
start_mtime=$(get_microtime)
while [[ ${bytes_written} -lt ${BYTES_TOTAL} ]]; do
sleep 1
let bytes_written+=$(get_bytes_copied)
running_mtime=$(echo "$(get_microtime)-${start_mtime}" | bc -l)
echo "$(bytes_to_sectors ${bytes_written})+0 records in" >&2
echo "$(bytes_to_sectors ${bytes_written})+0 records out" >&2
echo "${bytes_written} bytes ($(get_MB ${bytes_written}) MB) copied, ${running_mtime}, $(get_MB_persec ${bytes_written} ${running_mtime}) MB/s" >&2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment