#!/usr/bin/env bash
#
# Reproduce the following scenario:
# * Init a database
# * Allocate some huge pages
# * Prepare a limiting cgroup and move current shell into it
# * Run PostgreSQL and receive SIGBUS on start

set -ex

SHELL_PID=$$
CGROUP_VERSION=$1 # just a number, 1 or 2

rm -rf madvise_test
./bin/initdb -D madvise_test

sudo sysctl -w vm.nr_hugepages=100

case "${CGROUP_VERSION}" in
    v1) sudo ./cgroup-v1.sh "${SHELL_PID}";;
    v2) sudo ./cgroup-v2.sh "${SHELL_PID}";;
esac;

./bin/postgres -D madvise_test
