Hi,

> Thanks. Could you share the exact script (with the top-up patches if
> any) and the test environment (instance type, any concurrent activity,
> etc.)? I will verify it from my side.

To clarify, I benchmarked exact v11 patch set with the attached script. Now
instead of seeing the iostat, it tried to refer the statistics of Linux block
device.
(Use .txt file to bypass the local mail filtering rule)

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#!/bin/bash
#
# 0003: Use WALReadFromBuffers() for local WAL reads.
#
# Emits WAL with pg_logical_emit_message(), then scans the generated range
# with pg_walinspect through the local WAL reader.
#
# Run once per build, same knobs both times:
#   BUILD=HEAD    PGBIN=~/pg/head/bin   RUNS=3 ./nocfbot-local_wal_read_perf.sh
#   BUILD=PATCHED PGBIN=~/pg/patched/bin RUNS=3 ./nocfbot-local_wal_read_perf.sh
#
# Needs pg_walinspect (make -C contrib/pg_walinspect install).

set -eo pipefail

PGBIN=${PGBIN:?set PGBIN to the bin directory of the build under test}
BUILD=${BUILD:-HEAD}
RUNS=${RUNS:-3}
TXNS_PER_CLIENT=${TXNS_PER_CLIENT:-40000}
CLIENTS=${CLIENTS:-16}
DIRECT_IO=${DIRECT_IO:-"on off"}
DATA_ROOT=${DATA_ROOT:-./}
PGDATA=${PGDATA:-$DATA_ROOT/$BUILD/localread-data}
WALDIR=${WALDIR:-}
PORT=${PORT:-5432}
SHARED_BUFFERS=${SHARED_BUFFERS:-8GB}
WAL_BUFFERS=${WAL_BUFFERS:-16MB}
MAX_WAL_SIZE=${MAX_WAL_SIZE:-64GB}
OUTDIR=${OUTDIR:-./wal-read-perf/localread-$BUILD-$(date -u +%Y%m%d-%H%M%S)}

export PGHOST=/tmp PGPORT=$PORT PGDATABASE=postgres
PATH=$PGBIN:$PATH
mkdir -p "$OUTDIR"
CSV=$OUTDIR/runs.csv
SUMMARY=$OUTDIR/SUMMARY.txt
FMT='%-8s %-14s %-9s %-11s %-18s %-20s %-12s %-13s %-15s %s\n'

psql_q() { psql -X -v ON_ERROR_STOP=1 -Atq -F' ' -c "$1"; }
pg_start() {
  pg_ctl -D "$PGDATA" -l "$OUTDIR/server-$1.log" -w start >/dev/null ||
    { echo "server did not start, see $OUTDIR/server-$1.log"; exit 1; }
}
pg_stop() { pg_ctl -D "$PGDATA" -m fast -w stop >/dev/null 2>&1 || true; }
trap pg_stop EXIT

# median of the per-run values: integers for counts and large values, more
# digits only where a small value would otherwise round away to nothing
median() { printf '%s\n' "$@" | sort -n |
  awk '{a[NR]=$1} END {m = (NR%2) ? a[(NR+1)/2] : (a[NR/2]+a[NR/2+1])/2
                       fmt = (m == int(m)) ? "%.0f\n" : (m >= 100) ? "%.0f\n" : 
(m >= 1) ? "%.1f\n" : "%.2f\n"
                       printf fmt, m}'; }

# Linux block statistics report sectors in 512-byte units.
disk_sectors() {
  awk 'NF >= 7 {print $3, $7; found = 1} END {if (!found) exit 1}' 
"$WAL_DEV_STAT"
}

# assert builds and a build that overrides wal_buffers make the numbers useless
check_server() {
  [ "$(psql_q "SELECT current_setting('debug_assertions')")" = off ] || [ 
"${ALLOW_CASSERT:-0}" = 1 ] ||
    { echo "$PGBIN is an assert-enabled build, use a release build or set 
ALLOW_CASSERT=1"; exit 1; }
  [ "$(psql_q "SELECT pg_size_bytes(current_setting('wal_buffers')) = 
pg_size_bytes('$WAL_BUFFERS')")" = t ] ||
    { echo "wal_buffers is $(psql_q "SELECT current_setting('wal_buffers')"), 
not the requested $WAL_BUFFERS: this build forces it"; exit 1; }
}

setup_cluster() {
  if [ ! -s "$PGDATA/PG_VERSION" ]; then
    mkdir -p "$(dirname "$PGDATA")" "${WALDIR:-$PGDATA}"
    initdb -D "$PGDATA" --no-sync ${WALDIR:+-X "$WALDIR"} >"$OUTDIR/initdb.log" 
2>&1
  fi
  grep -q "include 'bench.conf'" "$PGDATA/postgresql.conf" ||
    echo "include 'bench.conf'" >>"$PGDATA/postgresql.conf"
  [ -f "$(pg_config --pkglibdir)/pg_walinspect.so" ] ||
    { echo "pg_walinspect not installed: make -C contrib/pg_walinspect 
install"; exit 1; }
}

write_conf() { # $1 = debug_io_direct value ('wal' or '')
  cat >"$PGDATA/bench.conf" <<EOF
port = $PORT
unix_socket_directories = '/tmp'
shared_buffers = $SHARED_BUFFERS
wal_buffers = $WAL_BUFFERS
max_wal_size = $MAX_WAL_SIZE
wal_keep_size = $MAX_WAL_SIZE
synchronous_commit = on
wal_level = logical
track_io_timing = on
debug_io_direct = '$1'
EOF
}

detect_wal_dev() {
  local src
  src=$(df --output=source "$PGDATA/pg_wal" | tail -1)
  lsblk -no PKNAME "$src" 2>/dev/null | head -1 | grep . || basename "$src"
}

run_one() { # $1 = direct io label (on|off), $2 = run number
  local dio=$1 run=$2 tag="$BUILD-dio$1-run$2" start_lsn end_lsn processed

  psql_q "CHECKPOINT" >/dev/null
  start_lsn=$(psql_q "SELECT pg_current_wal_flush_lsn()")

  LC_ALL=C pgbench -n -f "$OUTDIR/emit.sql" -c "$CLIENTS" -j "$CLIENTS" -t 
"$TXNS_PER_CLIENT" -P 10 \
    >"$OUTDIR/pgbench-$tag.txt" 2>&1

  processed=$(awk -F ': *|/' '/^number of transactions actually processed:/ 
{print $2}' "$OUTDIR/pgbench-$tag.txt")
  if [ "$processed" != "$((CLIENTS * TXNS_PER_CLIENT))" ]; then
    echo "Unexpected transaction count. See $OUTDIR/pgbench-$tag.txt" >&2
    return 1
  fi

  end_lsn=$(psql_q "SELECT pg_current_wal_flush_lsn()")
  psql_q "SELECT pg_stat_reset_shared('io')" >/dev/null

  local before after read_before write_before read_after write_after 
disk_read_bytes=NA disk_write_bytes=NA
  if [ "$WAL_DEV" != none ]; then
    before=$(disk_sectors)
  fi

  LC_ALL=C psql -X -v ON_ERROR_STOP=1 -v start_lsn="$start_lsn" -v 
end_lsn="$end_lsn" \
    >"$OUTDIR/scan-$tag.txt" <<'SQL'
\timing on
SELECT sum(count) AS wal_records FROM pg_get_wal_stats(:'start_lsn', 
:'end_lsn');
SQL

  if [ "$WAL_DEV" != none ]; then
    after=$(disk_sectors)
    read -r read_before write_before <<<"$before"
    read -r read_after write_after <<<"$after"
    if ((read_after < read_before || write_after < write_before)); then
      echo "Block device counters decreased during the scan: $WAL_DEV" >&2
      return 1
    fi
    disk_read_bytes=$(((read_after - read_before) * 512))
    disk_write_bytes=$(((write_after - write_before) * 512))
  fi

  local tps scan_ms wal_mb read_mb hits reads
  tps=$(awk '/^tps =/ {print $3}' "$OUTDIR/pgbench-$tag.txt" | tail -1)
  scan_ms=$(awk '$1 == "Time:" && $3 == "ms" {print $2}' 
"$OUTDIR/scan-$tag.txt")
  wal_mb=$(psql_q "SELECT round(pg_wal_lsn_diff('$end_lsn', 
'$start_lsn')/1048576.0, 1)")
  read -r read_mb hits reads <<<"$(psql_q "SELECT 
round(coalesce(sum(read_bytes), 0)/1048576.0, 1), coalesce(sum(hits), 0), 
coalesce(sum(reads), 0) FROM pg_stat_io WHERE backend_type = 'client backend' 
AND object = 'wal'")"

  echo 
"$BUILD,$dio,$run,$tps,$scan_ms,$wal_mb,$read_mb,$reads,$hits,$disk_read_bytes,$disk_write_bytes"
 | tee -a "$CSV"

  R_TPS+=("$tps"); R_SCAN_MS+=("$scan_ms"); R_WAL+=("$wal_mb"); 
R_READ+=("$read_mb")
  R_READS+=("$reads"); R_HITS+=("$hits")
  if [ "$WAL_DEV" != none ]; then
    R_DR+=("$disk_read_bytes"); R_DW+=("$disk_write_bytes")
  fi
}

echo "SELECT pg_logical_emit_message(true, 'bench', repeat('x', 350));" 
>"$OUTDIR/emit.sql"

setup_cluster
WAL_DEV=${WAL_DEV:-$(detect_wal_dev)}
[ -n "$WAL_DEV" ] || { echo "could not detect the pg_wal device, set WAL_DEV"; 
exit 1; }
if [ "$WAL_DEV" != none ]; then
  WAL_DEV_STAT="/sys/class/block/${WAL_DEV##*/}/stat"
  disk_sectors >/dev/null ||
    { echo "Cannot read block device statistics: $WAL_DEV_STAT (set 
WAL_DEV=none to skip)" >&2; exit 1; }
fi
echo "build=$BUILD pgdata=$PGDATA wal_dev=$WAL_DEV runs=$RUNS 
txns_per_client=$TXNS_PER_CLIENT clients=$CLIENTS out=$OUTDIR"

echo 
"build,direct_io,run,emit_tps,scan_ms,wal_generated_mb,reader_read_mb,reads,hits,scan_device_read_bytes,scan_device_write_bytes"
 >"$CSV"
{
  echo "local WAL reads via pg_walinspect forward scan, build=$BUILD"
  echo "shared_buffers=$SHARED_BUFFERS wal_buffers=$WAL_BUFFERS 
max_wal_size=$MAX_WAL_SIZE wal_keep_size=$MAX_WAL_SIZE synchronous_commit=on"
  echo "workload: pgbench -n -f emit.sql -c $CLIENTS -j $CLIENTS -t 
$TXNS_PER_CLIENT ($((CLIENTS * TXNS_PER_CLIENT)) transactions), reader: one 
pg_get_wal_stats() scan after WAL generation"
  echo "device I/O: total device bytes during the scan (NA when WAL_DEV=none)"
  echo "runs=$RUNS (median reported), wal device=$WAL_DEV"
  echo "binary: $PGBIN, $(pg_config --version)"
  echo
  printf "$FMT" build "WAL direct IO" TPS "scan ms" "WAL generated MB" "reader 
read MB" \
    "file reads" "buffer hits" "device read B" "device write B"
} >"$SUMMARY"

for dio in $DIRECT_IO; do
  if [ "$dio" = on ]; then write_conf wal; else write_conf ''; fi
  pg_stop
  pg_start "$BUILD-dio$dio"
  check_server
  psql_q "CREATE EXTENSION IF NOT EXISTS pg_walinspect" >/dev/null
  # report what the server actually used, a build may override these
  psql_q "SELECT name || ' = ' || current_setting(name) FROM pg_settings WHERE 
name IN ('shared_buffers', 'wal_buffers', 'max_wal_size', 'wal_keep_size', 
'synchronous_commit', 'wal_level', 'debug_io_direct', 'summarize_wal', 
'wal_writer_delay')" >"$OUTDIR/settings-$dio.txt"
  echo "direct IO $dio: effective wal_buffers=$(psql_q "SELECT 
current_setting('wal_buffers')") debug_io_direct='$(psql_q "SELECT 
current_setting('debug_io_direct')")'"

  R_TPS=(); R_SCAN_MS=(); R_WAL=(); R_READ=(); R_READS=(); R_HITS=(); R_DR=(); 
R_DW=();
  for run in $(seq 1 "$RUNS"); do run_one "$dio" "$run"; done

  device_read=NA
  device_write=NA
  if [ "$WAL_DEV" != none ]; then
    device_read=$(median "${R_DR[@]}")
    device_write=$(median "${R_DW[@]}")
  fi

  printf "$FMT" "$BUILD" "$dio" "$(median "${R_TPS[@]}")" "$(median 
"${R_SCAN_MS[@]}")" \
    "$(median "${R_WAL[@]}")" "$(median "${R_READ[@]}")" "$(median 
"${R_READS[@]}")" \
    "$(median "${R_HITS[@]}")" "$device_read" "$device_write" \
    >>"$SUMMARY"
  pg_stop
done

cat "$SUMMARY"
echo "per-run rows and effective settings in $OUTDIR"

Reply via email to