set test "postgres"

# Test sdt support in postgres.

global env

if {! [info exists env(SYSTEMTAP_TESTAPPS)] || (
    ! [string match "postgres" $env(SYSTEMTAP_TESTAPPS)] &&
    ! [string match "all" $env(SYSTEMTAP_TESTAPPS)])} {
    untested "$test sdt app"
    return
}

########## Create stap-postgres.stp ########## 
set pgdata "[pwd]/stap-postgres"
set postgresrelease "9.4.5"
set postgresdir "[pwd]/postgresql-$postgresrelease/install/"

set fp [open "$pgdata.stp" "w"]
puts $fp "
probe process(\"$postgresdir/bin/postgres\").mark(\"transaction__start\")
{
  printf(\"%s %#x\\n\", \$\$name, \$arg1);
}
probe process(\"$postgresdir/bin/postgres\").mark(\"transaction__commit\")
{
  printf(\"%s\\n\", \$\$name);
}
probe process(\"$postgresdir/bin/postgres\").mark(\"transaction__abort\")
{
  printf(\"%s\\n\", \$\$name);
}
probe process(\"$postgresdir/bin/postgres\").mark(\"lock__wait__start\")
{
  printf(\"%s %#x %#x\\n\", \$\$name, \$arg1, \$arg2);
}
probe process(\"$postgresdir/bin/postgres\").mark(\"lock__wait__done\")
{
  printf(\"%s %#x %#x\\n\", \$\$name, \$arg1, \$arg2);
}
probe process(\"$postgresdir/bin/postgres\").mark(\"lwlock__wait__done\")
{
  printf(\"%s %#x %#x\\n\", \$\$name, \$arg1, \$arg2);
}
probe process(\"$postgresdir/bin/postgres\").mark(\"lwlock__acquire\")
{
  printf(\"%s %#x %#x\\n\", \$\$name, \$arg1, \$arg2);
}
probe process(\"$postgresdir/bin/postgres\").mark(\"lwlock__condacquire__fail\")
{
  printf(\"%s %#x %#x\\n\", \$\$name, 
  	 \$arg1, \$arg2);
}
probe process(\"$postgresdir/bin/postgres\").mark(\"lwlock__condacquire\")
{
  printf(\"%s %#x %#x\\n\", \$\$name, \$arg1, \$arg2);
}
probe process(\"$postgresdir/bin/postgres\").mark(\"lwlock__release\")
{
  printf(\"%s %#x\\n\", \$\$name, \$arg1);
}
"
close $fp

########## Begin stap-postgres.sh ########## 
set fp [open "$pgdata.sh" "w"]
puts $fp "
function run_tests \{
/bin/rm -rf $pgdata
$postgresdir/bin/initdb $pgdata

echo '$env(SYSTEMTAP_PATH)/stap -m \$(date +stapsdt_%j%k%M%N | sed 's/ //') -c \"$postgresdir/bin/postgres -D $pgdata\" $pgdata.stp >$pgdata-markers.log'
$env(SYSTEMTAP_PATH)/stap -m \$(date +stapsdt_%j%k%M%N | sed 's/ //') -c \"$postgresdir/bin/postgres -D $pgdata\" $pgdata.stp >$pgdata-markers.log 2>&1 &
STAPPID=\$!

# wait until postgres is running
for i in \$(seq 0 10) ; do
    if $postgresdir/bin/pg_ctl status -D $pgdata
    then break;
    fi
    sleep 5
done

(cd $postgresdir/../src/test/regress/
 make installcheck > $pgdata.log 2>&1)

ACQUIRE=\$(grep 'lwlock__acquire 0x\[0-9\]* 0x\[0-9\]*' $pgdata-markers.log | wc -l)
RELEASE=\$(grep 'lwlock__release 0x\[0-9\]*' $pgdata-markers.log | wc -l)
START=\$(grep 'transaction__start 0x\[0-9\]*' $pgdata-markers.log | wc -l)
COMMIT=\$(grep 'transaction__commit' $pgdata-markers.log | wc -l)
OKAY=\$(grep 'test .*ok' $pgdata.log | wc -l)

echo lwlock__acquire=\$ACQUIRE lwlock__release=\$RELEASE transaction__start=\$START transaction__commit=\$COMMIT test-ok=\$OKAY
: 44873 75325 591 489 0

if \[ \$ACQUIRE -ge 0 -a \$RELEASE -gt 7300000 -a \$START -gt 17000 -a \$COMMIT -gt 14000 \] ; then
   echo PASS: postgres tests \$ACQUIRE/\$RELEASE/\$START/\$COMMIT
else
   echo FAIL: postgres tests \$ACQUIRE/\$RELEASE/\$START/\$COMMIT
fi

if \[ \$OKAY -gt 100 \] ; then
   echo PASS: postgres markers \$OKAY
else
   echo FAIL: postgres markers \$OKAY
fi

$postgresdir/bin/pg_ctl stop -D $pgdata
kill \$STAPPID
\}

if \[ ! -r postgresql-$postgresrelease.tar.bz2 \] ; then
wget -q http://ftp.postgresql.org/pub/source/v${postgresrelease}/postgresql-$postgresrelease.tar.bz2
fi
if \[ ! -r postgresql-$postgresrelease.tar.bz2 \] ; then
   echo FAIL: wget $postgresrelease.tar.gz
   exit
fi

if \[ ! -d $postgresdir/../src/backend \] ; then
tar -x -f postgresql-${postgresrelease}.tar.bz2
fi

cd postgresql-${postgresrelease}/
# postgres 9.0.4 now seems to want build done from source dir
# mkdir bld;cd bld
./configure --without-readline --enable-dtrace --prefix=$postgresdir
sed -i -e 's|^CFLAGS = -O2.*\$|& -g -I $env(SYSTEMTAP_INCLUDES)|' -e 's|-O2|-O0|' src/Makefile.global
if \[ \$(uname -m) = ppc64 \] ; then 
   sed -i -e 's|CFLAGS = |& -DSTAP_SDT_ARG_CONSTRAINT=nr |' src/Makefile.global
fi

J=\$(getconf _NPROCESSORS_CONF)
make -j\$J
make install
run_tests
"

########## End stap-postgres.sh ########## 
close $fp

########## stap-postgres.sh does most of the work ########## 
verbose -log Running postgres testsuite
spawn sh $pgdata.sh 2>&1
expect {
    -timeout 1000
    -re {FAIL: [a-z_ ]+} { regexp " .*$" $expect_out(0,string) s; 
	fail "$s"; exp_continue }
    -re {PASS: [a-z_ ]+} { regexp " .*$" $expect_out(0,string) s; 
	pass "$s"; exp_continue }
    -re {UNSUPPORTED: [a-zA-Z_/: ]+} { regexp " .*$" $expect_out(0,string) s; 
	verbose -log "$s"
	unsupported "$s"; exp_continue }
    timeout { fail "$test (timeout)" }
    eof { }
}
catch {close}; catch {wait}

if { $verbose == 0 } {
catch {exec rm -rf $pgdata}
catch {exec rm -rf $pgdata.stp $pgdata.log \
	   $pgdata-markers.log $pgdata.sh postgresql-${postgresrelease}.tar.bz2}
catch {exec rm -rf postgresql-${postgresrelease}}
}
