#!/usr/bin/env bash # Verification for: buffer-pool-and-memory-tuning-guide # Self-contained. Boots throwaway mysqld instances on scratch ports/datadirs (never touches an # already-running server's data), prints PASS / FAIL / SKIP per claim, cleans up via EXIT trap. # Needs: mysqld + mysql client on PATH (Homebrew MySQL 8.4 layout assumed but auto-detected). # Missing mysqld, insufficient permissions, or no network for the doc-quote checks -> SKIP, not FAIL. # # Override with: MYSQLD_BIN=/path/to/mysqld MYSQL_BASEDIR=/path/to/basedir ./verify-...sh set -u PASS=0; FAIL=0; SKIP=0; CONTENT=0 ok() { echo "PASS $*"; PASS=$((PASS+1)); } no() { echo "FAIL $*"; FAIL=$((FAIL+1)); } skip() { echo "SKIP $*"; SKIP=$((SKIP+1)); } content() { echo "CONTENT $*"; CONTENT=$((CONTENT+1)); } echo "=== innodb_buffer_pool_size — claim verification ===" echo "date: $(date -u '+%Y-%m-%dT%H:%M:%SZ') host: $(uname -srm)" echo SCRATCH="$(mktemp -d "${TMPDIR:-/tmp}/hw_rw_bp.XXXXXX")" PIDS=() cleanup() { for p in "${PIDS[@]:-}"; do [ -n "$p" ] && kill -0 "$p" 2>/dev/null && kill "$p" 2>/dev/null done sleep 1 for p in "${PIDS[@]:-}"; do [ -n "$p" ] && kill -0 "$p" 2>/dev/null && kill -9 "$p" 2>/dev/null done rm -rf "$SCRATCH" } trap cleanup EXIT MYSQLD_BIN="${MYSQLD_BIN:-$(command -v mysqld 2>/dev/null || echo /opt/homebrew/bin/mysqld)}" MYSQL_BIN="${MYSQL_BIN:-$(command -v mysql 2>/dev/null || echo mysql)}" if [ ! -x "$MYSQLD_BIN" ]; then skip "mysqld binary not found (looked for \$MYSQLD_BIN, PATH, /opt/homebrew/bin/mysqld) — cannot run the scratch-instance checks" MYSQLD_OK=0 else MYSQLD_OK=1 fi MYSQL_BASEDIR="${MYSQL_BASEDIR:-$(dirname "$(dirname "$MYSQLD_BIN")")}" boot_mysqld() { # boot_mysqld -> sets SOCK_ and PID_, prints nothing local name="$1"; shift local dd="$SCRATCH/data_$name" local sock="/tmp/hw_rw_bp_${name}_$$.sock" local log="$SCRATCH/${name}.log" local pidfile="$SCRATCH/${name}.pid" mkdir -p "$dd" "$MYSQLD_BIN" --no-defaults --initialize-insecure --datadir="$dd" --basedir="$MYSQL_BASEDIR" >"$log" 2>&1 "$MYSQLD_BIN" --no-defaults --datadir="$dd" --basedir="$MYSQL_BASEDIR" \ --socket="$sock" --port=0 --skip-networking=0 --pid-file="$pidfile" \ "$@" >>"$log" 2>&1 & local pid=$! PIDS+=("$pid") local i=0 while [ $i -lt 30 ]; do if "$MYSQL_BIN" -u root -S "$sock" -e 'SELECT 1' >/dev/null 2>&1; then eval "SOCK_${name}=\"$sock\"" eval "PID_${name}=\"$pid\"" return 0 fi if ! kill -0 "$pid" 2>/dev/null; then return 1 fi sleep 1 i=$((i+1)) done return 1 } stop_mysqld() { local name="$1" local sockvar="SOCK_${name}" local sock="${!sockvar:-}" [ -n "$sock" ] && "$MYSQL_BIN" -u root -S "$sock" -e 'SHUTDOWN' >/dev/null 2>&1 sleep 1 } # --------------------------------------------------------------------------- # CLAIM 1: on a fresh instance with no explicit sizing and no dedicated-server # flag, the defaults are 128 MiB / 1 chunk / 1 instance. # --------------------------------------------------------------------------- if [ "$MYSQLD_OK" -eq 1 ] && boot_mysqld default; then OUT="$("$MYSQL_BIN" -u root -S "$SOCK_default" -N -B -e \ "SELECT @@innodb_buffer_pool_size, @@innodb_buffer_pool_chunk_size, @@innodb_buffer_pool_instances")" read -r SZ CHUNK INST <<<"$OUT" echo " fresh instance, no flags: buffer_pool_size=$SZ chunk_size=$CHUNK instances=$INST" if [ "$SZ" = "134217728" ]; then ok "C1 default innodb_buffer_pool_size is 134217728 bytes (128 MiB)" else no "C1 expected default 134217728, got $SZ" fi stop_mysqld default else skip "C1 default buffer pool size (mysqld unavailable or failed to start)" fi # --------------------------------------------------------------------------- # CLAIM 2: requesting a size that's not a multiple of chunk_size gets rounded # up, and instances gets forced to 1 when the result is < 1 GiB — # even if you asked for more instances explicitly. # --------------------------------------------------------------------------- if [ "$MYSQLD_OK" -eq 1 ] && boot_mysqld round \ --innodb_buffer_pool_size=300M --innodb_buffer_pool_chunk_size=134217728 \ --innodb_buffer_pool_instances=2; then OUT="$("$MYSQL_BIN" -u root -S "$SOCK_round" -N -B -e \ "SELECT @@innodb_buffer_pool_size, @@innodb_buffer_pool_instances")" read -r SZ INST <<<"$OUT" echo " requested 300M / instances=2 / chunk=128M -> got buffer_pool_size=$SZ instances=$INST" if [ "$SZ" = "402653184" ]; then ok "C2 300M with a 128M chunk size is rounded UP to 402653184 bytes (384 MiB), not truncated" else no "C2 expected rounded size 402653184, got $SZ" fi if [ "$INST" = "1" ]; then ok "C3 innodb_buffer_pool_instances is forced to 1 (requested 2) because the resulting pool is under 1 GiB" else no "C3 expected innodb_buffer_pool_instances forced to 1, got $INST" fi stop_mysqld round else skip "C2/C3 buffer pool rounding + instance forcing (mysqld unavailable or failed to start)" fi # --------------------------------------------------------------------------- # CLAIM 4: innodb_dedicated_server derives buffer pool size and redo log # capacity from detected memory/CPUs using the documented formula. # --------------------------------------------------------------------------- MEM_BYTES="" if command -v sysctl >/dev/null 2>&1; then MEM_BYTES="$(sysctl -n hw.memsize 2>/dev/null)" elif [ -r /proc/meminfo ]; then MEM_KB="$(awk '/MemTotal/{print $2}' /proc/meminfo)" [ -n "$MEM_KB" ] && MEM_BYTES=$((MEM_KB * 1024)) fi NCPU="$(command -v nproc >/dev/null 2>&1 && nproc || sysctl -n hw.ncpu 2>/dev/null)" if [ "$MYSQLD_OK" -eq 1 ] && [ -n "${MEM_BYTES:-}" ] && [ -n "${NCPU:-}" ] && \ boot_mysqld dedicated --innodb-dedicated-server=ON; then OUT="$("$MYSQL_BIN" -u root -S "$SOCK_dedicated" -N -B -e \ "SELECT @@innodb_buffer_pool_size, @@innodb_redo_log_capacity")" read -r BP REDO <<<"$OUT" EXPECT_BP="$(python3 -c " m = $MEM_BYTES gb = 1024**3 if m < gb: e = 128*1024*1024 elif m <= 4*gb: e = int(m*0.5) else: e = int(m*0.75) print(e) ")" EXPECT_REDO="$(python3 -c " c = $NCPU gb = 1024**3 e = min(int((c/2.0)*gb), 16*gb) print(e) ")" echo " detected memory=$MEM_BYTES bytes, cpus=$NCPU" echo " innodb-dedicated-server=ON -> buffer_pool_size=$BP (formula expects ~$EXPECT_BP) redo_log_capacity=$REDO (formula expects ~$EXPECT_REDO)" # Allow a tolerance of one chunk (128 MiB) for buffer pool rounding, and 5% for redo log. BP_OK="$(python3 -c "print(1 if abs($BP-$EXPECT_BP) <= 134217728 else 0)")" REDO_OK="$(python3 -c "print(1 if abs($REDO-$EXPECT_REDO) <= max(1, $EXPECT_REDO*0.05) else 0)")" if [ "$BP_OK" = "1" ]; then ok "C4 innodb_dedicated_server buffer pool size matches the documented memory-tier formula (within one chunk)" else no "C4 buffer pool size $BP does not match documented formula (~$EXPECT_BP)" fi if [ "$REDO_OK" = "1" ]; then ok "C5 innodb_dedicated_server redo log capacity matches (cores/2) GB capped at 16 GB (within 5%)" else no "C5 redo log capacity $REDO does not match documented formula (~$EXPECT_REDO)" fi stop_mysqld dedicated else skip "C4/C5 innodb_dedicated_server auto-sizing (mysqld/sysctl/nproc unavailable, or failed to start)" fi # --------------------------------------------------------------------------- # CLAIM 6/7: the manual's own wording — rounding rule and the 80%-of-RAM # dedicated-server guidance — quoted correctly (needs network). # --------------------------------------------------------------------------- detag() { python3 -c 'import sys,re,html; t=html.unescape(re.sub(r"<[^>]+>"," ",sys.stdin.read())); print(re.sub(r"\s+"," ",t))'; } RESIZE_PAGE="$(curl -s --max-time 25 https://dev.mysql.com/doc/refman/8.4/en/innodb-buffer-pool-resize.html | detag || true)" if [ -z "$RESIZE_PAGE" ]; then skip "C6 buffer-pool rounding wording (needs network)" elif printf '%s' "$RESIZE_PAGE" | grep -qi 'buffer pool size is automatically adjusted'; then ok "C6 manual: buffer pool size 'is automatically adjusted' when not a multiple of chunk_size * instances" else no "C6 could not find the documented rounding wording" fi PARAMS_PAGE="$(curl -s --max-time 25 https://dev.mysql.com/doc/refman/8.4/en/innodb-parameters.html | detag || true)" if [ -z "$PARAMS_PAGE" ]; then skip "C7 80%-of-RAM dedicated-server wording (needs network)" elif printf '%s' "$PARAMS_PAGE" | grep -qi 'set the buffer pool size to 80% of the machine'; then ok "C7 manual: you might set the buffer pool size to 80% of the machine's physical memory size" else no "C7 could not find the documented 80%-of-RAM wording" fi # --------------------------------------------------------------------------- # CONTENT regression guards (not proof — just checks the article text matches # the numbers this script produces). # --------------------------------------------------------------------------- SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ARTICLE="$SELF_DIR/buffer-pool-and-memory-tuning-guide.html" if [ -f "$ARTICLE" ]; then grep -q '134217728' "$ARTICLE" && content "article cites 134217728 (128 MiB default)" || no "article missing 134217728 default figure" grep -q '402653184' "$ARTICLE" && content "article cites 402653184 (rounded 384 MiB) figure" || no "article missing 402653184 rounding figure" else skip "article HTML not found next to this script — content regression checks skipped (this is expected when the script is downloaded standalone from /verify/)" fi echo echo "=== summary: PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT ===" [ "$FAIL" -eq 0 ]