Skip to content

Instantly share code, notes, and snippets.

@fl64
Last active May 19, 2025 10:18
Show Gist options
  • Save fl64/87ad174725603c2d73db575e788ff7d5 to your computer and use it in GitHub Desktop.
Save fl64/87ad174725603c2d73db575e788ff7d5 to your computer and use it in GitHub Desktop.
memory-rate.sh
#!/bin/bash
# Configuration
INTERVAL=1 # Measurement interval in seconds
DURATION=60 # Number of measurements to take
# Automatically detect page size
PAGE_SIZE=$(getconf PAGESIZE)
echo "Detected page size: ${PAGE_SIZE} bytes"
# Function to read values from /proc/vmstat
read_vmstat() {
local file="/proc/vmstat"
pgmajfault=$(grep 'pgmajfault' $file | awk '{print $2}')
pgpgin=$(grep 'pgpgin' $file | awk '{print $2}')
pgpgout=$(grep 'pgpgout' $file | awk '{print $2}')
echo "$pgmajfault $pgpgin $pgpgout"
}
# Array to store results
declare -a speeds
echo "Starting measurements every $INTERVAL second(s). Total: $DURATION measurements."
for ((i = 1; i <= DURATION; i++)); do
read FIRST_FAULT FIRST_IN FIRST_OUT <<<$(read_vmstat)
sleep $INTERVAL
read LAST_FAULT LAST_IN LAST_OUT <<<$(read_vmstat)
DELTA_FAULT=$((LAST_FAULT - FIRST_FAULT))
DELTA_IN=$((LAST_IN - FIRST_IN))
DELTA_OUT=$((LAST_OUT - FIRST_OUT))
TOTAL_DELTA_PAGES=$((DELTA_IN + DELTA_OUT + DELTA_FAULT))
DATA_BYTES=$((TOTAL_DELTA_PAGES * PAGE_SIZE))
DATA_Mbps=$(echo "scale=2; ($DATA_BYTES * 8) / ($INTERVAL * 1000000)" | bc)
speeds+=("$DATA_Mbps")
echo "[$i] Dirty Page Bandwidth: ${DATA_Mbps} Mbps"
done
# Calculate statistics
sum=0
min=${speeds[0]}
max=0
for speed in "${speeds[@]}"; do
sum=$(echo "$sum + $speed" | bc)
(( $(echo "$speed > $max" | bc -l) )) && max=$speed
(( $(echo "$speed < $min" | bc -l) )) && min=$speed
done
average=$(echo "scale=2; $sum / $DURATION" | bc)
# Summary output
echo "---------------------------------------------"
echo "SUMMARY:"
echo " Minimum bandwidth: $min Mbps"
echo " Maximum bandwidth: $max Mbps"
echo " Average bandwidth: $average Mbps"
echo "---------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment