Skip to content

Instantly share code, notes, and snippets.

@eatnumber1
Created June 21, 2023 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eatnumber1/a95b68fd80d6450b3c87bde8fc2d6aa9 to your computer and use it in GitHub Desktop.
Save eatnumber1/a95b68fd80d6450b3c87bde8fc2d6aa9 to your computer and use it in GitHub Desktop.
Draw a sparkline of dirty pages over time on Linux.
#!/bin/bash
set -e
set -o pipefail
statefile="$(mktemp --tmpdir=/run spark-dirty-pages.XXXXX)"
trap 'rm -f $statefile' INT HUP QUIT
function watch_iter {
declare -a dirty
readarray -t dirty < "$statefile"
dirty+=( "$(grep Dirty /proc/meminfo | awk '{print $2 "K";}' | numfmt --from=iec)" )
declare -i trimstart
(( trimstart = ${#dirty[@]} - COLUMNS ))
if (( trimstart < 0 )); then
trimstart=0
fi
dirty=( "${dirty[@]:$trimstart:$COLUMNS}" )
# Flush the state file
printf "%s\n" "${dirty[@]}" > "$statefile"
echo "Dirty: $(numfmt --to=iec --suffix=B ${dirty[-1]})"
# https://github.com/holman/spark
spark <<< "${dirty[@]}"
}
export -f watch_iter
export statefile
watch -ex "$@" -- "$BASH" -c watch_iter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment