xref: /petsc/config/petsc_harness.sh (revision 542ff2eaf6fc08080c8f9ab00b53e1ef5e67cb5c)
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))
87
88if test -n "$extra_args"; then
89  args="$args $extra_args"
90fi
91if $debugger; then
92  args="-start_in_debugger $args"
93fi
94
95
96# Init
97success=0; failed=0; failures=""; rmfiles=""
98total=0
99todo=-1; skip=-1
100job_level=0
101
102function petsc_testrun() {
103  # First arg = Basic command
104  # Second arg = stdout file
105  # Third arg = stderr file
106  # Fourth arg = label for reporting
107  # Fifth arg = Filter
108  rmfiles="${rmfiles} $2 $3"
109  tlabel=$4
110  filter=$5
111  job_control=true
112  cmd="$1 > $2 2> $3"
113  if test -n "$filter"; then
114    if test "${filter:0:6}"=="Error:"; then
115      job_control=false      # redirection error method causes job control probs
116      filter=${filter##Error:}
117      cmd="$1 2>&1 | cat > $2"
118    fi
119  fi
120  echo "$cmd" > ${tlabel}.sh; chmod 755 ${tlabel}.sh
121
122  if $job_control; then
123    # The action:
124    eval "($cmd) &"
125    pid=$!
126    # Put a watcher process in that will kill a job that exceeds limit
127    $config_dir/watchtime.sh $pid $TIMEOUT &
128    watcher=$!
129
130    # See if the job we want finishes
131    wait $pid 2> /dev/null
132    cmd_res=$?
133    if ps -p $watcher > /dev/null; then
134      # Keep processes tidy by killing watcher
135      pkill -13 -P $watcher
136      wait $watcher 2>/dev/null  # Wait used here to capture the kill message
137    else
138      # Timeout
139      cmd_res=1
140      echo "Exceeded timeout limit of $TIMEOUT s" > $3
141    fi
142  else
143    # The action -- assume no timeout needed
144    eval "$cmd"
145    # We are testing error codes so just make it pass
146    cmd_res=0
147  fi
148
149  # Handle filters separately and assume no timeout check needed
150  if test -n "$filter"; then
151    cmd="cat $2 | $filter > $2.tmp 2>> $3 && mv $2.tmp $2"
152    echo "$cmd" >> ${tlabel}.sh
153    eval "$cmd"
154  fi
155
156  # Report errors
157  if test $cmd_res == 0; then
158    if "${verbose}"; then
159     printf "ok $tlabel $cmd\n" | tee -a ${testlogfile}
160    else
161     printf "ok $tlabel\n" | tee -a ${testlogfile}
162    fi
163    let success=$success+1
164  else
165    if "${verbose}"; then
166      printf "not ok $tlabel $cmd\n" | tee -a ${testlogfile}
167    else
168      printf "not ok $tlabel\n" | tee -a ${testlogfile}
169    fi
170    # We've had tests fail but stderr->stdout. Fix with this test.
171    if test -s $3; then
172       awk '{print "#\t" $0}' < $3 | tee -a ${testlogfile}
173    else
174       awk '{print "#\t" $0}' < $2 | tee -a ${testlogfile}
175    fi
176    let failed=$failed+1
177    failures="$failures $tlabel"
178  fi
179  let total=$success+$failed
180  return $cmd_res
181}
182
183function petsc_testend() {
184  logfile=$1/counts/${label}.counts
185  logdir=`dirname $logfile`
186  if ! test -d "$logdir"; then
187    mkdir -p $logdir
188  fi
189  if ! test -e "$logfile"; then
190    touch $logfile
191  fi
192  printf "total $total\n" > $logfile
193  printf "success $success\n" >> $logfile
194  printf "failed $failed\n" >> $logfile
195  printf "failures $failures\n" >> $logfile
196  if test ${todo} -gt 0; then
197    printf "todo $todo\n" >> $logfile
198  fi
199  if test ${skip} -gt 0; then
200    printf "skip $skip\n" >> $logfile
201  fi
202  if $cleanup; then
203    echo "Cleaning up"
204    /bin/rm -f $rmfiles
205  fi
206}
207
208function petsc_mpiexec_valgrind() {
209  mpiexec=$1;shift
210  npopt=$1;shift
211  np=$1;shift
212
213  valgrind="valgrind -q --tool=memcheck --leak-check=yes --num-callers=20 --track-origins=yes --suppressions=$petsc_bindir/maint/petsc-val.supp"
214
215  $mpiexec $npopt $np $valgrind $*
216}
217export LC_ALL=C
218