xref: /petsc/config/petsc_harness.sh (revision e7f46db8d62cb2e4e59111bf21061e64ea60daab)
1
2
3scriptname=`basename $0`
4rundir=${scriptname%.sh}
5TIMEOUT=60
6
7if test "$PWD"!=`dirname $0`; then
8  cd `dirname $0`
9fi
10if test -d "${rundir}" && test -n "${rundir}"; then
11  rm -f ${rundir}/*.tmp ${rundir}/*.err ${rundir}/*.out
12fi
13mkdir -p ${rundir}
14if test -d "${runfiles}"; then
15  cp -r ${runfiles} ${rundir}
16elif test -n "${runfiles}"; then
17  cp ${runfiles} ${rundir}
18fi
19cd ${rundir}
20
21#
22# Method to print out general and script specific options
23#
24print_usage() {
25
26cat >&2 <<EOF
27Usage: $0 [options]
28
29OPTIONS
30  -a <args> ......... Override default arguments
31  -c <cleanup> ...... Cleanup (remove generated files)
32  -d ................ Launch in debugger
33  -e <args> ......... Add extra arguments to default
34  -f ................ force attempt to run test that would otherwise be skipped
35  -h ................ help: print this message
36  -n <integer> ...... Override the number of processors to use
37  -j ................ Pass -j to petscdiff (just use diff)
38  -J <arg> .......... Pass -J to petscdiff (just use diff with arg)
39  -m ................ Update results using petscdiff
40  -M ................ Update alt files using petscdiff
41  -t ................ Override the default timeout (default=$TIMEOUT sec)
42  -V ................ run Valgrind
43  -v ................ Verbose: Print commands
44EOF
45
46  if declare -f extrausage > /dev/null; then extrausage; fi
47  exit $1
48}
49###
50##  Arguments for overriding things
51#
52verbose=false
53cleanup=false
54debugger=false
55force=false
56diff_flags=""
57while getopts "a:cde:fhjJ:mMn:t:vV" arg
58do
59  case $arg in
60    a ) args="$OPTARG"       ;;
61    c ) cleanup=true         ;;
62    d ) debugger=true        ;;
63    e ) extra_args="$OPTARG" ;;
64    f ) force=true           ;;
65    h ) print_usage; exit    ;;
66    n ) nsize="$OPTARG"      ;;
67    j ) diff_flags="-j"      ;;
68    J ) diff_flags="-J $OPTARG" ;;
69    m ) diff_flags="-m"      ;;
70    M ) diff_flags="-M"      ;;
71    t ) TIMEOUT=$OPTARG      ;;
72    V ) mpiexec="petsc_mpiexec_valgrind $mpiexec" ;;
73    v ) verbose=true         ;;
74    *)  # To take care of any extra args
75      if test -n "$OPTARG"; then
76        eval $arg=\"$OPTARG\"
77      else
78        eval $arg=found
79      fi
80      ;;
81  esac
82done
83shift $(( $OPTIND - 1 ))
84
85# Individual tests can extend the default
86TIMEOUT=$((TIMEOUT*timeoutfactor))
87STARTTIME=`date +%s`
88
89if test -n "$extra_args"; then
90  args="$args $extra_args"
91fi
92if $debugger; then
93  args="-start_in_debugger $args"
94fi
95
96
97# Init
98success=0; failed=0; failures=""; rmfiles=""
99total=0
100todo=-1; skip=-1
101job_level=0
102
103function petsc_testrun() {
104  # First arg = Basic command
105  # Second arg = stdout file
106  # Third arg = stderr file
107  # Fourth arg = label for reporting
108  # Fifth arg = Filter
109  rmfiles="${rmfiles} $2 $3"
110  tlabel=$4
111  filter=$5
112  job_control=true
113  cmd="$1 > $2 2> $3"
114  if test -n "$filter"; then
115    if test "${filter:0:6}"=="Error:"; then
116      job_control=false      # redirection error method causes job control probs
117      filter=${filter##Error:}
118      cmd="$1 2>&1 | cat > $2"
119    fi
120  fi
121  echo "$cmd" > ${tlabel}.sh; chmod 755 ${tlabel}.sh
122
123  if $job_control; then
124    # The action:
125    eval "($cmd) &"
126    pid=$!
127    # Put a watcher process in that will kill a job that exceeds limit
128    $config_dir/watchtime.sh $pid $TIMEOUT &
129    watcher=$!
130
131    # See if the job we want finishes
132    wait $pid 2> /dev/null
133    cmd_res=$?
134    if ps -p $watcher > /dev/null; then
135      # Keep processes tidy by killing watcher
136      pkill -13 -P $watcher
137      wait $watcher 2>/dev/null  # Wait used here to capture the kill message
138    else
139      # Timeout
140      cmd_res=1
141      echo "Exceeded timeout limit of $TIMEOUT s" > $3
142    fi
143  else
144    # The action -- assume no timeout needed
145    eval "$cmd"
146    # We are testing error codes so just make it pass
147    cmd_res=0
148  fi
149
150  # Handle filters separately and assume no timeout check needed
151  if test -n "$filter"; then
152    cmd="cat $2 | $filter > $2.tmp 2>> $3 && mv $2.tmp $2"
153    echo "$cmd" >> ${tlabel}.sh
154    eval "$cmd"
155  fi
156
157  # Report errors
158  if test $cmd_res == 0; then
159    if "${verbose}"; then
160     printf "ok $tlabel $cmd\n" | tee -a ${testlogfile}
161    else
162     printf "ok $tlabel\n" | tee -a ${testlogfile}
163    fi
164    let success=$success+1
165  else
166    if "${verbose}"; then
167      printf "not ok $tlabel $cmd\n" | tee -a ${testlogfile}
168    else
169      printf "not ok $tlabel\n" | tee -a ${testlogfile}
170    fi
171    # We've had tests fail but stderr->stdout. Fix with this test.
172    if test -s $3; then
173       awk '{print "#\t" $0}' < $3 | tee -a ${testlogfile}
174    else
175       awk '{print "#\t" $0}' < $2 | tee -a ${testlogfile}
176    fi
177    let failed=$failed+1
178    failures="$failures $tlabel"
179  fi
180  let total=$success+$failed
181  return $cmd_res
182}
183
184function petsc_testend() {
185  logfile=$1/counts/${label}.counts
186  logdir=`dirname $logfile`
187  if ! test -d "$logdir"; then
188    mkdir -p $logdir
189  fi
190  if ! test -e "$logfile"; then
191    touch $logfile
192  fi
193  printf "total $total\n" > $logfile
194  printf "success $success\n" >> $logfile
195  printf "failed $failed\n" >> $logfile
196  printf "failures $failures\n" >> $logfile
197  if test ${todo} -gt 0; then
198    printf "todo $todo\n" >> $logfile
199  fi
200  if test ${skip} -gt 0; then
201    printf "skip $skip\n" >> $logfile
202  fi
203  ENDTIME=`date +%s`
204  printf "time $(($ENDTIME - $STARTTIME))\n" >> $logfile
205  if $cleanup; then
206    echo "Cleaning up"
207    /bin/rm -f $rmfiles
208  fi
209}
210
211function petsc_mpiexec_valgrind() {
212  mpiexec=$1;shift
213  npopt=$1;shift
214  np=$1;shift
215
216  valgrind="valgrind -q --tool=memcheck --leak-check=yes --num-callers=20 --track-origins=yes --suppressions=$petsc_bindir/maint/petsc-val.supp"
217
218  $mpiexec $npopt $np $valgrind $*
219}
220export LC_ALL=C
221