/[gentoo-x86]/eclass/multiprocessing.eclass
Gentoo

Contents of /eclass/multiprocessing.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations) (download)
Sat Dec 7 09:14:15 2013 UTC (12 months, 1 week ago) by vapier
Branch: MAIN
Changes since 1.6: +2 -2 lines
use $CBUILD rather than uname for host detection #479656

1 vapier 1.3 # Copyright 1999-2013 Gentoo Foundation
2 vapier 1.1 # Distributed under the terms of the GNU General Public License v2
3 vapier 1.7 # $Header: /var/cvsroot/gentoo-x86/eclass/multiprocessing.eclass,v 1.6 2013/12/06 03:04:01 jcallen Exp $
4 vapier 1.1
5     # @ECLASS: multiprocessing.eclass
6     # @MAINTAINER:
7     # base-system@gentoo.org
8     # @AUTHOR:
9     # Brian Harring <ferringb@gentoo.org>
10     # Mike Frysinger <vapier@gentoo.org>
11     # @BLURB: parallelization with bash (wtf?)
12     # @DESCRIPTION:
13     # The multiprocessing eclass contains a suite of functions that allow ebuilds
14     # to quickly run things in parallel using shell code.
15     #
16     # It has two modes: pre-fork and post-fork. If you don't want to dive into any
17     # more nuts & bolts, just use the pre-fork mode. For main threads that mostly
18     # spawn children and then wait for them to finish, use the pre-fork mode. For
19     # main threads that do a bit of processing themselves, use the post-fork mode.
20     # You may mix & match them for longer computation loops.
21     # @EXAMPLE:
22     #
23     # @CODE
24     # # First initialize things:
25     # multijob_init
26     #
27     # # Then hash a bunch of files in parallel:
28     # for n in {0..20} ; do
29     # multijob_child_init md5sum data.${n} > data.${n}
30     # done
31     #
32     # # Then wait for all the children to finish:
33     # multijob_finish
34     # @CODE
35    
36     if [[ ${___ECLASS_ONCE_MULTIPROCESSING} != "recur -_+^+_- spank" ]] ; then
37     ___ECLASS_ONCE_MULTIPROCESSING="recur -_+^+_- spank"
38    
39     # @FUNCTION: makeopts_jobs
40     # @USAGE: [${MAKEOPTS}]
41     # @DESCRIPTION:
42     # Searches the arguments (defaults to ${MAKEOPTS}) and extracts the jobs number
43     # specified therein. Useful for running non-make tools in parallel too.
44     # i.e. if the user has MAKEOPTS=-j9, this will echo "9" -- we can't return the
45     # number as bash normalizes it to [0, 255]. If the flags haven't specified a
46     # -j flag, then "1" is shown as that is the default `make` uses. Since there's
47     # no way to represent infinity, we return 999 if the user has -j without a number.
48     makeopts_jobs() {
49     [[ $# -eq 0 ]] && set -- ${MAKEOPTS}
50     # This assumes the first .* will be more greedy than the second .*
51     # since POSIX doesn't specify a non-greedy match (i.e. ".*?").
52     local jobs=$(echo " $* " | sed -r -n \
53     -e 's:.*[[:space:]](-j|--jobs[=[:space:]])[[:space:]]*([0-9]+).*:\2:p' \
54     -e 's:.*[[:space:]](-j|--jobs)[[:space:]].*:999:p')
55     echo ${jobs:-1}
56     }
57    
58 vapier 1.4 # @FUNCTION: makeopts_loadavg
59     # @USAGE: [${MAKEOPTS}]
60     # @DESCRIPTION:
61     # Searches the arguments (defaults to ${MAKEOPTS}) and extracts the value set
62     # for load-average. For make and ninja based builds this will mean new jobs are
63     # not only limited by the jobs-value, but also by the current load - which might
64     # get excessive due to I/O and not just due to CPU load.
65     # Be aware that the returned number might be a floating-point number. Test
66     # whether your software supports that.
67     makeopts_loadavg() {
68     [[ $# -eq 0 ]] && set -- ${MAKEOPTS}
69     # This assumes the first .* will be more greedy than the second .*
70     # since POSIX doesn't specify a non-greedy match (i.e. ".*?").
71     local lavg=$(echo " $* " | sed -r -n \
72     -e 's:.*[[:space:]](-l|--load-average[=[:space:]])[[:space:]]*([0-9]+|[0-9]+\.[0-9]+)[^0-9.]*:\2:p' \
73     -e 's:.*[[:space:]](-l|--load-average)[[:space:]].*:999:p')
74     echo ${lavg:-1}
75     }
76    
77 vapier 1.1 # @FUNCTION: multijob_init
78     # @USAGE: [${MAKEOPTS}]
79     # @DESCRIPTION:
80     # Setup the environment for executing code in parallel.
81     # You must call this before any other multijob function.
82     multijob_init() {
83     # When something goes wrong, try to wait for all the children so we
84     # don't leave any zombies around.
85 vapier 1.2 has wait ${EBUILD_DEATH_HOOKS} || EBUILD_DEATH_HOOKS+=" wait "
86 vapier 1.1
87     # Setup a pipe for children to write their pids to when they finish.
88 vapier 1.3 # We have to allocate two fd's because POSIX has undefined behavior
89     # when you open a FIFO for simultaneous read/write. #487056
90 vapier 1.1 local pipe="${T}/multijob.pipe"
91 vapier 1.3 mkfifo -m 600 "${pipe}"
92     redirect_alloc_fd mj_write_fd "${pipe}"
93     redirect_alloc_fd mj_read_fd "${pipe}"
94 vapier 1.1 rm -f "${pipe}"
95    
96     # See how many children we can fork based on the user's settings.
97     mj_max_jobs=$(makeopts_jobs "$@")
98     mj_num_jobs=0
99     }
100    
101     # @FUNCTION: multijob_child_init
102     # @USAGE: [--pre|--post] [command to run in background]
103     # @DESCRIPTION:
104     # This function has two forms. You can use it to execute a simple command
105     # in the background (and it takes care of everything else), or you must
106     # call this first thing in your forked child process.
107     #
108     # The --pre/--post options allow you to select the child generation mode.
109     #
110     # @CODE
111     # # 1st form: pass the command line as arguments:
112     # multijob_child_init ls /dev
113     # # Or if you want to use pre/post fork modes:
114     # multijob_child_init --pre ls /dev
115     # multijob_child_init --post ls /dev
116     #
117     # # 2nd form: execute multiple stuff in the background (post fork):
118     # (
119     # multijob_child_init
120     # out=`ls`
121     # if echo "${out}" | grep foo ; then
122     # echo "YEAH"
123     # fi
124     # ) &
125     # multijob_post_fork
126     #
127     # # 2nd form: execute multiple stuff in the background (pre fork):
128     # multijob_pre_fork
129     # (
130     # multijob_child_init
131     # out=`ls`
132     # if echo "${out}" | grep foo ; then
133     # echo "YEAH"
134     # fi
135     # ) &
136     # @CODE
137     multijob_child_init() {
138     local mode="pre"
139     case $1 in
140     --pre) mode="pre" ; shift ;;
141     --post) mode="post"; shift ;;
142     esac
143    
144     if [[ $# -eq 0 ]] ; then
145 vapier 1.3 trap 'echo ${BASHPID} $? >&'${mj_write_fd} EXIT
146 vapier 1.1 trap 'exit 1' INT TERM
147     else
148     local ret
149     [[ ${mode} == "pre" ]] && { multijob_pre_fork; ret=$?; }
150     ( multijob_child_init ; "$@" ) &
151     [[ ${mode} == "post" ]] && { multijob_post_fork; ret=$?; }
152     return ${ret}
153     fi
154     }
155    
156     # @FUNCTION: _multijob_fork
157     # @INTERNAL
158     # @DESCRIPTION:
159     # Do the actual book keeping.
160     _multijob_fork() {
161     [[ $# -eq 1 ]] || die "incorrect number of arguments"
162    
163     local ret=0
164     [[ $1 == "post" ]] && : $(( ++mj_num_jobs ))
165     if [[ ${mj_num_jobs} -ge ${mj_max_jobs} ]] ; then
166     multijob_finish_one
167     ret=$?
168     fi
169     [[ $1 == "pre" ]] && : $(( ++mj_num_jobs ))
170     return ${ret}
171     }
172    
173     # @FUNCTION: multijob_pre_fork
174     # @DESCRIPTION:
175     # You must call this in the parent process before forking a child process.
176     # If the parallel limit has been hit, it will wait for one child to finish
177     # and return its exit status.
178     multijob_pre_fork() { _multijob_fork pre "$@" ; }
179    
180     # @FUNCTION: multijob_post_fork
181     # @DESCRIPTION:
182     # You must call this in the parent process after forking a child process.
183     # If the parallel limit has been hit, it will wait for one child to finish
184     # and return its exit status.
185     multijob_post_fork() { _multijob_fork post "$@" ; }
186    
187     # @FUNCTION: multijob_finish_one
188     # @DESCRIPTION:
189     # Wait for a single process to exit and return its exit code.
190     multijob_finish_one() {
191     [[ $# -eq 0 ]] || die "${FUNCNAME} takes no arguments"
192    
193     local pid ret
194 vapier 1.3 read -r -u ${mj_read_fd} pid ret || die
195 vapier 1.1 : $(( --mj_num_jobs ))
196     return ${ret}
197     }
198    
199     # @FUNCTION: multijob_finish
200     # @DESCRIPTION:
201     # Wait for all pending processes to exit and return the bitwise or
202     # of all their exit codes.
203     multijob_finish() {
204     local ret=0
205     while [[ ${mj_num_jobs} -gt 0 ]] ; do
206     multijob_finish_one
207     : $(( ret |= $? ))
208     done
209     # Let bash clean up its internal child tracking state.
210     wait
211    
212     # Do this after reaping all the children.
213     [[ $# -eq 0 ]] || die "${FUNCNAME} takes no arguments"
214    
215 vapier 1.2 # No need to hook anymore.
216     EBUILD_DEATH_HOOKS=${EBUILD_DEATH_HOOKS/ wait / }
217    
218 vapier 1.1 return ${ret}
219     }
220    
221     # @FUNCTION: redirect_alloc_fd
222     # @USAGE: <var> <file> [redirection]
223     # @DESCRIPTION:
224     # Find a free fd and redirect the specified file via it. Store the new
225     # fd in the specified variable. Useful for the cases where we don't care
226     # about the exact fd #.
227     redirect_alloc_fd() {
228     local var=$1 file=$2 redir=${3:-"<>"}
229    
230 jcallen 1.6 # Make sure /dev/fd is sane on Linux hosts. #479656
231 vapier 1.7 if [[ ! -L /dev/fd && ${CBUILD} == *linux* ]] ; then
232 vapier 1.5 eerror "You're missing a /dev/fd symlink to /proc/self/fd."
233     eerror "Please fix the symlink and check your boot scripts (udev/etc...)."
234     die "/dev/fd is broken"
235     fi
236    
237 vapier 1.1 if [[ $(( (BASH_VERSINFO[0] << 8) + BASH_VERSINFO[1] )) -ge $(( (4 << 8) + 1 )) ]] ; then
238     # Newer bash provides this functionality.
239     eval "exec {${var}}${redir}'${file}'"
240     else
241     # Need to provide the functionality ourselves.
242     local fd=10
243     while :; do
244     # Make sure the fd isn't open. It could be a char device,
245     # or a symlink (possibly broken) to something else.
246     if [[ ! -e /dev/fd/${fd} ]] && [[ ! -L /dev/fd/${fd} ]] ; then
247     eval "exec ${fd}${redir}'${file}'" && break
248     fi
249     [[ ${fd} -gt 1024 ]] && die 'could not locate a free temp fd !?'
250     : $(( ++fd ))
251     done
252     : $(( ${var} = fd ))
253     fi
254     }
255    
256     fi

  ViewVC Help
Powered by ViewVC 1.1.20