#!/bin/bash
# look for glitches in scheduling
#   glitch1.sh 1.9 2007-05-21 17:18:41-04 davidsen Stable

# see if this is a rerun (1st arg)
if [ "$1" = "rerun" ]; then
  # don't initialize, discard the arg
  shift
  # add a delimiter to the files
  echo "===" >>gl_vmstat
else
  # clear out the output files
  rm -f gl_*
fi

Jobs=""

# xterm scroll option - SCROLL
#   -j - do jump scroll
#   +j - do one line scroll
# if SCROLL is specified that's used instead
scroll=${SCROLL:-"+j"}
#
# number of xwindows scrolling - MAXWIN
MaxWin=${MAXWIN:-4}
#
# number of glxgears samples for stddev
#   run time for those plus a startup sample
glxSamples=10
glxrun=$[5*(glxSamples+1)+3]

rm -f gl_loop*
for n in $(seq 1 ${MaxWin}); do
  vpos=$[(n-1)*80+50]
  xterm -geom 100x2-0+${vpos} ${scroll} \
  -e bash -c "exec 3>gl_loop$n; lc=0; while true; do echo \$RANDOM; let lc=lc+1; echo \$lc >&3; done" &
  Jobs="$Jobs $!"
done

# save the uname info for identification
uname -a >>gl_uname

# track the resources in use
vmstat 1 >>gl_vmstat &
Jobs="$Jobs $!"

echo -e "\nObserve the gear rotation for obvious glitches\n" \
  "Close the glxgears window to end this test early\n" \
  "Test will end automatically after about ${glxrun}sec" >&2

# save the fps information
script -a -c "glxgears -printfps & sleep ${glxrun}; kill \$!" gl_gears

# close the rest
kill ${Jobs}

# summarize the loop files
for n in $(seq 1 ${MaxWin})
do
  echo -n "$n "
  tail -n1 gl_loop$n
  rm gl_loop$n
done >>gl_fairloops
echo "===" >>gl_fairloops

