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