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