xref: /petsc/config/petsc_harness.sh (revision 1e3d9a73dfd4cc3256b8567f08e8302e3ba2b695)
1
2
3scriptname=`basename $0`
4rundir=${scriptname%.sh}
5TIMEOUT=60
6
7if test "$PWD"!=`dirname $0`; then
8  cd `dirname $0`
9  abspath_scriptdir=$PWD
10fi
11if test -d "${rundir}" && test -n "${rundir}"; then
12  rm -f ${rundir}/*.tmp ${rundir}/*.err ${rundir}/*.out
13fi
14mkdir -p ${rundir}
15if test -n "${runfiles}"; then
16  for runfile in ${runfiles}; do
17      subdir=`dirname ${runfile}`
18      mkdir -p ${rundir}/${subdir}
19      cp -r ${runfile} ${rundir}/${subdir}
20  done
21fi
22cd ${rundir}
23
24#
25# Method to print out general and script specific options
26#
27print_usage() {
28
29cat >&2 <<EOF
30Usage: $0 [options]
31
32OPTIONS
33  -a <args> ......... Override default arguments
34  -c ................ Cleanup (remove generated files)
35  -C ................ Compile
36  -d ................ Launch in debugger
37  -e <args> ......... Add extra arguments to default
38  -f ................ force attempt to run test that would otherwise be skipped
39  -h ................ help: print this message
40  -n <integer> ...... Override the number of processors to use
41  -j ................ Pass -j to petscdiff (just use diff)
42  -J <arg> .......... Pass -J to petscdiff (just use diff with arg)
43  -m ................ Update results using petscdiff
44  -M ................ Update alt files using petscdiff
45  -o <arg> .......... Output format: 'interactive', 'err_only'
46  -p ................ Print command:  Print first command and exit
47  -t ................ Override the default timeout (default=$TIMEOUT sec)
48  -V ................ run Valgrind
49  -v ................ Verbose: Print commands
50EOF
51
52  if declare -f extrausage > /dev/null; then extrausage; fi
53  exit $1
54}
55###
56##  Arguments for overriding things
57#
58output_fmt="interactive"
59verbose=false
60cleanup=false
61compile=false
62debugger=false
63printcmd=false
64force=false
65diff_flags=""
66while getopts "a:cCde:fhjJ:mMn:o:pt:vV" arg
67do
68  case $arg in
69    a ) args="$OPTARG"       ;;
70    c ) cleanup=true         ;;
71    C ) compile=true         ;;
72    d ) debugger=true        ;;
73    e ) extra_args="$OPTARG" ;;
74    f ) force=true           ;;
75    h ) print_usage; exit    ;;
76    n ) nsize="$OPTARG"      ;;
77    j ) diff_flags=$diff_flags" -j"      ;;
78    J ) diff_flags=$diff_flags" -J $OPTARG" ;;
79    m ) diff_flags=$diff_flags" -m"      ;;
80    M ) diff_flags=$diff_flags" -M"      ;;
81    o ) output_fmt=$OPTARG   ;;
82    p ) printcmd=true        ;;
83    t ) TIMEOUT=$OPTARG      ;;
84    V ) mpiexec="petsc_mpiexec_valgrind $mpiexec" ;;
85    v ) verbose=true         ;;
86    *)  # To take care of any extra args
87      if test -n "$OPTARG"; then
88        eval $arg=\"$OPTARG\"
89      else
90        eval $arg=found
91      fi
92      ;;
93  esac
94done
95shift $(( $OPTIND - 1 ))
96
97# Individual tests can extend the default
98export MPIEXEC_TIMEOUT=$((TIMEOUT*timeoutfactor))
99STARTTIME=`date +%s`
100
101if test -n "$extra_args"; then
102  args="$args $extra_args"
103fi
104if $debugger; then
105  args="-start_in_debugger $args"
106fi
107if test -n "$filter"; then
108  diff_flags=$diff_flags" -F \$'$filter'"
109fi
110if test -n "$filter_output"; then
111  diff_flags=$diff_flags" -f \$'$filter_output'"
112fi
113
114
115# Init
116success=0; failed=0; failures=""; rmfiles=""
117total=0
118todo=-1; skip=-1
119job_level=0
120
121function petsc_report_tapoutput() {
122  notornot=$1
123  test_label=$2
124  comment=$3
125  if test -n "$comment"; then
126    comment=" # ${comment}"
127  fi
128
129  tap_message="${notornot} ok ${test_label}${comment}"
130
131  # Log messages
132  printf "${tap_message}\n" >> ${testlogtapfile}
133
134  if test ${output_fmt} == "err_only"; then
135     if test -n "${notornot}"; then
136        printf "${tap_message}\n" | tee -a ${testlogerrfile}
137     fi
138  else
139     printf "${tap_message}\n"
140  fi
141}
142
143function printcmd() {
144  # Print command that can be run from PETSC_DIR
145  cmd="$1"
146  basedir=`dirname ${PWD} | sed "s#${petsc_dir}/##"`
147  modcmd=`echo ${cmd} | sed -e "s#\.\.#${basedir}#" | sed s#\>.*##`
148  printf "${modcmd}\n"
149  exit
150}
151
152function petsc_testrun() {
153  # First arg = Basic command
154  # Second arg = stdout file
155  # Third arg = stderr file
156  # Fourth arg = label for reporting
157  rmfiles="${rmfiles} $2 $3"
158  tlabel=$4
159  error=$5
160  cmd="$1 > $2 2> $3"
161  if test -n "$error"; then
162    cmd="$1 2>&1 | cat > $2"
163  fi
164  echo "$cmd" > ${tlabel}.sh; chmod 755 ${tlabel}.sh
165  if $printcmd; then
166     printcmd "$cmd"
167  fi
168
169  eval "{ time -p $cmd ; } 2>> timing.out"
170  cmd_res=$?
171  #  If it is a lack of GPU resources, then try once more
172  #  See: src/sys/error/err.c
173  if [ $cmd_res -eq 96 ]; then
174    eval "{ time -p $cmd ; } 2>> timing.out"
175    cmd_res=$?
176  fi
177  touch "$2" "$3"
178  # ETIMEDOUT=110 on most systems (used by Open MPI 3.0).  MPICH uses
179  # 255.  Earlier Open MPI returns 1 but outputs about MPIEXEC_TIMEOUT.
180  if [ $cmd_res -eq 110 -o $cmd_res -eq 255 ] || \
181        fgrep -q -s 'APPLICATION TIMED OUT' "$2" "$3" || \
182        fgrep -q -s MPIEXEC_TIMEOUT "$2" "$3" || \
183        fgrep -q -s 'APPLICATION TERMINATED WITH THE EXIT STRING: job ending due to timeout' "$2" "$3" || \
184        grep -q -s "Timeout after [0-9]* seconds. Terminating job" "$2" "$3"; then
185    timed_out=1
186    # If timed out, then ensure non-zero error code
187    if [ $cmd_res -eq 0 ]; then
188      cmd_res=1
189    fi
190  fi
191
192  # Report errors
193  comment=""
194  if test $cmd_res == 0; then
195     if "${verbose}"; then
196        comment="${cmd}"
197     fi
198    petsc_report_tapoutput "" "$tlabel" "$comment"
199    let success=$success+1
200  else
201    if [ -n "$timed_out" ]; then
202      comment="Exceeded timeout limit of $MPIEXEC_TIMEOUT s"
203    else
204      comment="Error code: ${cmd_res}"
205    fi
206    petsc_report_tapoutput "not" "$tlabel" "$comment"
207
208    # Report errors in detail
209    if [ -z "$timed_out" ]; then
210      # We've had tests fail but stderr->stdout, as well as having
211      # mpi_abort go to stderr which throws this test off.  Show both
212      # with stdout first
213      awk '{print "#\t" $0}' < $2 | tee -a ${testlogerrfile}
214      # if statement is for diff tests
215      if test "$2" != "$3"; then
216        awk '{print "#\t" $0}' < $3 | tee -a ${testlogerrfile}
217      fi
218    fi
219    let failed=$failed+1
220    failures="$failures $tlabel"
221  fi
222  let total=$success+$failed
223  return $cmd_res
224}
225
226function petsc_testend() {
227  logfile=$1/counts/${label}.counts
228  logdir=`dirname $logfile`
229  if ! test -d "$logdir"; then
230    mkdir -p $logdir
231  fi
232  if ! test -e "$logfile"; then
233    touch $logfile
234  fi
235  printf "total $total\n" > $logfile
236  printf "success $success\n" >> $logfile
237  printf "failed $failed\n" >> $logfile
238  printf "failures $failures\n" >> $logfile
239  if test ${todo} -gt 0; then
240    printf "todo $todo\n" >> $logfile
241  fi
242  if test ${skip} -gt 0; then
243    printf "skip $skip\n" >> $logfile
244  fi
245  ENDTIME=`date +%s`
246  timing=`touch timing.out && egrep '(user|sys)' timing.out | awk '{if( sum1 == "" || $2 > sum1 ) { sum1=sprintf("%.2f",$2) } ; sum2 += sprintf("%.2f",$2)} END {printf "%.2f %.2f\n",sum1,sum2}'`
247  printf "time $timing\n" >> $logfile
248  if $cleanup; then
249    echo "Cleaning up"
250    /bin/rm -f $rmfiles
251  fi
252}
253
254function petsc_mpiexec_valgrind() {
255  _mpiexec=$1;shift
256  npopt=$1;shift
257  np=$1;shift
258
259  valgrind="valgrind -q --tool=memcheck --leak-check=yes --num-callers=20 --track-origins=yes --suppressions=$petsc_bindir/maint/petsc-val.supp --error-exitcode=10"
260
261  $_mpiexec $npopt $np $valgrind "$@"
262}
263export LC_ALL=C
264
265if $compile; then
266    curexec=`basename ${exec}`
267    (cd $petsc_dir && make -f gmakefile.test ${abspath_scriptdir}/${curexec})
268fi
269
270