common/bin/hgforest.sh

Fri, 11 Apr 2014 10:31:20 -0700

author
mduigou
date
Fri, 11 Apr 2014 10:31:20 -0700
changeset 1144
e16a393ee5e3
parent 1143
e3ae43560332
child 1145
3c596cad39e6
permissions
-rw-r--r--

8041151: More concurrent hgforest
Reviewed-by: chegar, erikj, sla

mduigou@1142 1 #!/bin/sh
ohrstrom@538 2
ohrstrom@538 3 #
mduigou@845 4 # Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
ohrstrom@538 5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@538 6 #
ohrstrom@538 7 # This code is free software; you can redistribute it and/or modify it
ohrstrom@538 8 # under the terms of the GNU General Public License version 2 only, as
ohrstrom@538 9 # published by the Free Software Foundation.
ohrstrom@538 10 #
ohrstrom@538 11 # This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@538 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@538 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@538 14 # version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@538 15 # accompanied this code).
ohrstrom@538 16 #
ohrstrom@538 17 # You should have received a copy of the GNU General Public License version
ohrstrom@538 18 # 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@538 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@538 20 #
ohrstrom@538 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@538 22 # or visit www.oracle.com if you need additional information or have any
ohrstrom@538 23 # questions.
ohrstrom@538 24 #
ohrstrom@538 25
ohrstrom@538 26 # Shell script for a fast parallel forest command
ohrstrom@538 27
mduigou@1141 28 global_opts=""
mduigou@1141 29 status_output="/dev/stdout"
mduigou@1141 30 qflag="false"
mduigou@1141 31 vflag="false"
chegar@1143 32 sflag="false"
mduigou@1141 33 while [ $# -gt 0 ]
mduigou@1141 34 do
mduigou@1141 35 case $1 in
mduigou@1141 36 -q | --quiet )
mduigou@1141 37 qflag="true"
mduigou@1141 38 global_opts="${global_opts} -q"
mduigou@1141 39 status_output="/dev/null"
mduigou@1141 40 ;;
mduigou@1141 41
mduigou@1141 42 -v | --verbose )
mduigou@1141 43 vflag="true"
mduigou@1141 44 global_opts="${global_opts} -v"
mduigou@1141 45 ;;
mduigou@1141 46
chegar@1143 47 -s | --sequential )
chegar@1143 48 sflag="true"
chegar@1143 49 ;;
chegar@1143 50
mduigou@1141 51 '--' ) # no more options
mduigou@1141 52 shift; break
mduigou@1141 53 ;;
mduigou@1141 54
mduigou@1141 55 -*) # bad option
mduigou@1141 56 usage
mduigou@1141 57 ;;
mduigou@1141 58
mduigou@1141 59 * ) # non option
mduigou@1141 60 break
mduigou@1141 61 ;;
mduigou@1141 62 esac
mduigou@1141 63 shift
mduigou@1141 64 done
mduigou@1141 65
mduigou@1141 66
mduigou@1141 67 command="$1"; shift
mduigou@1142 68 command_args="$@"
mduigou@1141 69
mduigou@1141 70 usage() {
chegar@1143 71 echo "usage: $0 [-q|--quiet] [-v|--verbose] [-s|--sequential] [--] <command> [commands...]" > ${status_output}
mduigou@1141 72 exit 1
mduigou@1141 73 }
mduigou@1141 74
mduigou@1141 75 if [ "x" = "x$command" ] ; then
mduigou@1141 76 echo "ERROR: No command to hg supplied!"
mduigou@1141 77 usage
ohrstrom@538 78 fi
ohrstrom@538 79
mduigou@1144 80 # Check if we can use fifos for monitoring sub-process completion.
mduigou@1144 81 on_windows=`uname -s | egrep -ic -e 'cygwin|msys'`
mduigou@1144 82 if [ ${on_windows} = "1" ]; then
mduigou@1144 83 # cygwin has (2014-04-18) broken (single writer only) FIFOs
mduigou@1144 84 # msys has (2014-04-18) no FIFOs.
mduigou@1144 85 have_fifos="false"
mduigou@1144 86 else
mduigou@1144 87 have_fifos="true"
mduigou@1144 88 fi
mduigou@1144 89
ohrstrom@538 90 # Clean out the temporary directory that stores the pid files.
ohrstrom@538 91 tmp=/tmp/forest.$$
ohrstrom@538 92 rm -f -r ${tmp}
ohrstrom@538 93 mkdir -p ${tmp}
ohrstrom@538 94
ohrstrom@538 95 safe_interrupt () {
chegar@615 96 if [ -d ${tmp} ]; then
chegar@615 97 if [ "`ls ${tmp}/*.pid`" != "" ]; then
mduigou@1141 98 echo "Waiting for processes ( `cat ${tmp}/*.pid | tr '\n' ' '`) to terminate nicely!" > ${status_output}
ohrstrom@538 99 sleep 1
ohrstrom@538 100 # Pipe stderr to dev/null to silence kill, that complains when trying to kill
ohrstrom@538 101 # a subprocess that has already exited.
chegar@615 102 kill -TERM `cat ${tmp}/*.pid | tr '\n' ' '` 2> /dev/null
chegar@615 103 wait
mduigou@1141 104 echo "Interrupt complete!" > ${status_output}
chegar@615 105 fi
mduigou@1141 106 rm -f -r ${tmp}
ohrstrom@538 107 fi
mduigou@1141 108 exit 130
ohrstrom@538 109 }
ohrstrom@538 110
ohrstrom@538 111 nice_exit () {
chegar@615 112 if [ -d ${tmp} ]; then
chegar@615 113 if [ "`ls ${tmp}`" != "" ]; then
chegar@615 114 wait
chegar@615 115 fi
mduigou@1141 116 rm -f -r ${tmp}
ohrstrom@538 117 fi
ohrstrom@538 118 }
ohrstrom@538 119
ohrstrom@538 120 trap 'safe_interrupt' INT QUIT
ohrstrom@538 121 trap 'nice_exit' EXIT
chegar@615 122
mduigou@1141 123 subrepos="corba jaxp jaxws langtools jdk hotspot nashorn"
mduigou@1141 124 subrepos_extra="jdk/src/closed jdk/make/closed jdk/test/closed hotspot/make/closed hotspot/src/closed hotspot/test/closed deploy install sponsors pubs"
mduigou@1141 125
ohrstrom@538 126 # Only look in specific locations for possible forests (avoids long searches)
ohrstrom@538 127 pull_default=""
ohrstrom@538 128 repos=""
ohrstrom@538 129 repos_extra=""
mduigou@1142 130 if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
mduigou@1142 131 if [ ! -f .hg/hgrc ] ; then
mduigou@1141 132 echo "ERROR: Need initial repository to use this script" > ${status_output}
ohrstrom@538 133 exit 1
ohrstrom@538 134 fi
mduigou@1142 135
mduigou@1142 136 pull_default=`hg paths default`
mduigou@1142 137 if [ "${pull_default}" = "" ] ; then
mduigou@1142 138 echo "ERROR: Need initial clone with 'hg paths default' defined" > ${status_output}
mduigou@1142 139 exit 1
mduigou@1142 140 fi
mduigou@1142 141
ohrstrom@538 142 for i in ${subrepos} ; do
ohrstrom@538 143 if [ ! -f ${i}/.hg/hgrc ] ; then
ohrstrom@538 144 repos="${repos} ${i}"
ohrstrom@538 145 fi
ohrstrom@538 146 done
mduigou@1142 147 if [ "${command_args}" != "" ] ; then
ohrstrom@538 148 pull_default_tail=`echo ${pull_default} | sed -e 's@^.*://[^/]*/\(.*\)@\1@'`
mduigou@1142 149 if [ "x${pull_default}" = "x${pull_default_tail}" ] ; then
mduigou@1142 150 echo "ERROR: Need initial clone from non-local source" > ${status_output}
mduigou@1142 151 exit 1
mduigou@1142 152 fi
mduigou@1142 153 pull_extra="${command_args}/${pull_default_tail}"
ohrstrom@538 154 for i in ${subrepos_extra} ; do
ohrstrom@538 155 if [ ! -f ${i}/.hg/hgrc ] ; then
ohrstrom@538 156 repos_extra="${repos_extra} ${i}"
ohrstrom@538 157 fi
ohrstrom@538 158 done
ohrstrom@538 159 fi
ohrstrom@538 160 at_a_time=2
ohrstrom@538 161 # Any repos to deal with?
ohrstrom@538 162 if [ "${repos}" = "" -a "${repos_extra}" = "" ] ; then
mduigou@1141 163 echo "No repositories to process." > ${status_output}
ohrstrom@538 164 exit
ohrstrom@538 165 fi
ohrstrom@538 166 else
mduigou@1141 167 for i in . ${subrepos} ${subrepos_extra} ; do
mduigou@1141 168 if [ -d ${i}/.hg ] ; then
mduigou@1141 169 repos="${repos} ${i}"
mduigou@1141 170 fi
ohrstrom@538 171 done
mduigou@1141 172
mduigou@1141 173 # Any repos to deal with?
mduigou@1141 174 if [ "${repos}" = "" ] ; then
mduigou@1141 175 echo "No repositories to process." > ${status_output}
mduigou@1141 176 exit
mduigou@1141 177 fi
mduigou@1141 178
mduigou@1141 179 # any of the repos locked?
ohrstrom@538 180 for i in ${repos} ; do
ohrstrom@538 181 if [ -h ${i}/.hg/store/lock -o -f ${i}/.hg/store/lock ] ; then
ohrstrom@538 182 locked="${i} ${locked}"
ohrstrom@538 183 fi
ohrstrom@538 184 done
mduigou@1141 185 if [ "${locked}" != "" ] ; then
mduigou@1141 186 echo "ERROR: These repositories are locked: ${locked}" > ${status_output}
mduigou@1141 187 exit 1
mduigou@1141 188 fi
ohrstrom@538 189 at_a_time=8
ohrstrom@538 190 fi
ohrstrom@538 191
ohrstrom@538 192 # Echo out what repositories we do a command on.
mduigou@1141 193 echo "# Repositories: ${repos} ${repos_extra}" > ${status_output}
ohrstrom@538 194
mduigou@1141 195 if [ "${command}" = "serve" ] ; then
mduigou@1141 196 # "serve" is run for all the repos.
mduigou@1141 197 (
mduigou@1141 198 (
mduigou@1141 199 (
mduigou@1141 200 echo "[web]"
mduigou@1141 201 echo "description = $(basename $(pwd))"
mduigou@1141 202 echo "allow_push = *"
mduigou@1141 203 echo "push_ssl = False"
mduigou@1141 204
mduigou@1141 205 echo "[paths]"
mduigou@1141 206 for i in ${repos} ${repos_extra} ; do
mduigou@1141 207 if [ "${i}" != "." ] ; then
mduigou@1141 208 echo "/$(basename $(pwd))/${i} = ${i}"
mduigou@1141 209 else
mduigou@1141 210 echo "/$(basename $(pwd)) = $(pwd)"
mduigou@1141 211 fi
mduigou@1141 212 done
mduigou@1141 213 ) > ${tmp}/serve.web-conf
mduigou@1141 214
mduigou@1141 215 echo "serving root repo $(basename $(pwd))"
mduigou@1141 216
mduigou@1141 217 (PYTHONUNBUFFERED=true hg${global_opts} serve -A ${status_output} -E ${status_output} --pid-file ${tmp}/serve.pid --web-conf ${tmp}/serve.web-conf; echo "$?" > ${tmp}/serve.pid.rc ) 2>&1 &
mduigou@1141 218 ) 2>&1 | sed -e "s@^@serve: @" > ${status_output}
mduigou@1141 219 ) &
mduigou@1141 220 else
mduigou@1141 221 # Run the supplied command on all repos in parallel.
mduigou@1144 222
mduigou@1144 223 # n is the number of subprocess started or which might still be running.
mduigou@1141 224 n=0
mduigou@1144 225 if [ $have_fifos = "true" ]; then
mduigou@1144 226 # if we have fifos use them to detect command completion.
mduigou@1144 227 mkfifo ${tmp}/fifo
mduigou@1144 228 exec 3<>${tmp}/fifo
mduigou@1144 229 if [ "${sflag}" = "true" ] ; then
mduigou@1144 230 # force sequential
mduigou@1144 231 at_a_time=1
mduigou@1144 232 fi
mduigou@1144 233 fi
mduigou@1144 234
mduigou@1141 235 for i in ${repos} ${repos_extra} ; do
mduigou@1141 236 n=`expr ${n} '+' 1`
mduigou@1141 237 repopidfile=`echo ${i} | sed -e 's@./@@' -e 's@/@_@g'`
mduigou@1141 238 reponame=`echo ${i} | sed -e :a -e 's/^.\{1,20\}$/ &/;ta'`
mduigou@1141 239 pull_base="${pull_default}"
mduigou@1141 240 for j in $repos_extra ; do
ohrstrom@538 241 if [ "$i" = "$j" ] ; then
ohrstrom@538 242 pull_base="${pull_extra}"
ohrstrom@538 243 fi
mduigou@1141 244 done
mduigou@1144 245 pull_base="`echo ${pull_base} | sed -e 's@[/]*$@@'`"
mduigou@1141 246 (
mduigou@1141 247 (
mduigou@1142 248 if [ "${command}" = "clone" -o "${command}" = "fclone" -o "${command}" = "tclone" ] ; then
mduigou@1144 249 pull_newrepo="${pull_base}/${i}"
mduigou@1141 250 path="`dirname ${i}`"
mduigou@1141 251 if [ "${path}" != "." ] ; then
mduigou@1141 252 times=0
mduigou@1141 253 while [ ! -d "${path}" ] ## nested repo, ensure containing dir exists
mduigou@1141 254 do
mduigou@1141 255 times=`expr ${times} '+' 1`
mduigou@1141 256 if [ `expr ${times} '%' 10` -eq 0 ] ; then
mduigou@1141 257 echo "${path} still not created, waiting..." > ${status_output}
mduigou@1141 258 fi
mduigou@1141 259 sleep 5
mduigou@1141 260 done
mduigou@1141 261 fi
mduigou@1144 262 echo "hg${global_opts} clone ${pull_newrepo} ${i}" > ${status_output}
mduigou@1141 263 (PYTHONUNBUFFERED=true hg${global_opts} clone ${pull_newrepo} ${i}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
mduigou@1141 264 else
mduigou@1142 265 echo "cd ${i} && hg${global_opts} ${command} ${command_args}" > ${status_output}
mduigou@1142 266 cd ${i} && (PYTHONUNBUFFERED=true hg${global_opts} ${command} ${command_args}; echo "$?" > ${tmp}/${repopidfile}.pid.rc ) 2>&1 &
mduigou@1141 267 fi
mduigou@1141 268
mduigou@1141 269 echo $! > ${tmp}/${repopidfile}.pid
mduigou@1141 270 ) 2>&1 | sed -e "s@^@${reponame}: @" > ${status_output}
mduigou@1144 271 if [ $have_fifos = "true" ]; then
mduigou@1144 272 echo "${reponame}" >&3
mduigou@1144 273 fi
mduigou@1141 274 ) &
mduigou@1141 275
mduigou@1144 276 if [ $have_fifos = "true" ]; then
mduigou@1144 277 # check on count of running subprocesses and possibly wait for completion
mduigou@1144 278 if [ ${at_a_time} -lt ${n} ] ; then
mduigou@1144 279 # read will block until there are completed subprocesses
mduigou@1144 280 while read repo_done; do
mduigou@1144 281 n=`expr ${n} '-' 1`
mduigou@1144 282 if [ ${n} -lt ${at_a_time} ] ; then
mduigou@1144 283 # we should start more subprocesses
mduigou@1144 284 break;
mduigou@1144 285 fi
mduigou@1144 286 done <&3
mduigou@1144 287 fi
mduigou@1144 288 else
mduigou@1144 289 if [ "${sflag}" = "false" ] ; then
mduigou@1144 290 # Compare completions to starts
mduigou@1144 291 completed="`(ls -1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
mduigou@1144 292 while [ ${at_a_time} -lt `expr ${n} '-' ${completed}` ] ; do
mduigou@1144 293 # sleep a short time to give time for something to complete
mduigou@1144 294 sleep 1
mduigou@1144 295 completed="`(ls -1 ${tmp}/*.pid.rc 2> /dev/null | wc -l) || echo 0`"
mduigou@1144 296 done
mduigou@1144 297 else
mduigou@1144 298 # complete this task before starting another.
chegar@1143 299 wait
mduigou@1144 300 fi
chegar@1143 301 fi
ohrstrom@538 302 done
mduigou@1141 303 fi
chegar@615 304
mduigou@1144 305 # Wait for all subprocesses to complete
ohrstrom@538 306 wait
ohrstrom@538 307
chegar@615 308 # Terminate with exit 0 only if all subprocesses were successful
chegar@615 309 ec=0
chegar@615 310 if [ -d ${tmp} ]; then
chegar@615 311 for rc in ${tmp}/*.pid.rc ; do
chegar@615 312 exit_code=`cat ${rc} | tr -d ' \n\r'`
chegar@615 313 if [ "${exit_code}" != "0" ] ; then
mduigou@1141 314 repo="`echo ${rc} | sed -e s@^${tmp}@@ -e 's@/*\([^/]*\)\.pid\.rc$@\1@' -e 's@_@/@g'`"
mduigou@1144 315 echo "WARNING: ${repo} exited abnormally ($exit_code)" > ${status_output}
chegar@615 316 ec=1
chegar@615 317 fi
chegar@615 318 done
chegar@615 319 fi
chegar@615 320 exit ${ec}

mercurial