Show More
@@ -1,123 +1,139 | |||
|
1 | 1 | # worker.py - master-slave parallelism support |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2013 Facebook, Inc. |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from i18n import _ |
|
9 | 9 | import os, signal, sys, util |
|
10 | 10 | |
|
11 | 11 | def countcpus(): |
|
12 | 12 | '''try to count the number of CPUs on the system''' |
|
13 | 13 | |
|
14 | 14 | # posix |
|
15 | 15 | try: |
|
16 | 16 | n = int(os.sysconf('SC_NPROCESSORS_ONLN')) |
|
17 | 17 | if n > 0: |
|
18 | 18 | return n |
|
19 | 19 | except (AttributeError, ValueError): |
|
20 | 20 | pass |
|
21 | 21 | |
|
22 | 22 | # windows |
|
23 | 23 | try: |
|
24 | 24 | n = int(os.environ['NUMBER_OF_PROCESSORS']) |
|
25 | 25 | if n > 0: |
|
26 | 26 | return n |
|
27 | 27 | except (KeyError, ValueError): |
|
28 | 28 | pass |
|
29 | 29 | |
|
30 | 30 | return 1 |
|
31 | 31 | |
|
32 | 32 | def _numworkers(ui): |
|
33 | 33 | s = ui.config('worker', 'numcpus') |
|
34 | 34 | if s: |
|
35 | 35 | try: |
|
36 | 36 | n = int(s) |
|
37 | 37 | if n >= 1: |
|
38 | 38 | return n |
|
39 | 39 | except ValueError: |
|
40 | 40 | raise util.Abort(_('number of cpus must be an integer')) |
|
41 | 41 | return min(max(countcpus(), 4), 32) |
|
42 | 42 | |
|
43 | 43 | if os.name == 'posix': |
|
44 | 44 | _startupcost = 0.01 |
|
45 | 45 | else: |
|
46 | 46 | _startupcost = 1e30 |
|
47 | 47 | |
|
48 | 48 | def worthwhile(ui, costperop, nops): |
|
49 | 49 | '''try to determine whether the benefit of multiple processes can |
|
50 | 50 | outweigh the cost of starting them''' |
|
51 | 51 | linear = costperop * nops |
|
52 | 52 | workers = _numworkers(ui) |
|
53 | 53 | benefit = linear - (_startupcost * workers + linear / workers) |
|
54 | 54 | return benefit >= 0.15 |
|
55 | 55 | |
|
56 | 56 | def worker(ui, costperarg, func, staticargs, args): |
|
57 | 57 | '''run a function, possibly in parallel in multiple worker |
|
58 | 58 | processes. |
|
59 | 59 | |
|
60 | 60 | returns a progress iterator |
|
61 | 61 | |
|
62 | 62 | costperarg - cost of a single task |
|
63 | 63 | |
|
64 | 64 | func - function to run |
|
65 | 65 | |
|
66 | 66 | staticargs - arguments to pass to every invocation of the function |
|
67 | 67 | |
|
68 | 68 | args - arguments to split into chunks, to pass to individual |
|
69 | 69 | workers |
|
70 | 70 | ''' |
|
71 | 71 | if worthwhile(ui, costperarg, len(args)): |
|
72 | 72 | return _platformworker(ui, func, staticargs, args) |
|
73 | 73 | return func(*staticargs + (args,)) |
|
74 | 74 | |
|
75 | 75 | def _posixworker(ui, func, staticargs, args): |
|
76 | 76 | rfd, wfd = os.pipe() |
|
77 | 77 | workers = _numworkers(ui) |
|
78 | 78 | for pargs in partition(args, workers): |
|
79 | 79 | pid = os.fork() |
|
80 | 80 | if pid == 0: |
|
81 | 81 | try: |
|
82 | 82 | os.close(rfd) |
|
83 | 83 | for i, item in func(*(staticargs + (pargs,))): |
|
84 | 84 | os.write(wfd, '%d %s\n' % (i, item)) |
|
85 | 85 | os._exit(0) |
|
86 | 86 | except KeyboardInterrupt: |
|
87 | 87 | os._exit(255) |
|
88 | 88 | os.close(wfd) |
|
89 | 89 | fp = os.fdopen(rfd, 'rb', 0) |
|
90 | 90 | oldhandler = signal.getsignal(signal.SIGINT) |
|
91 | 91 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
|
92 | 92 | def cleanup(): |
|
93 | 93 | # python 2.4 is too dumb for try/yield/finally |
|
94 | 94 | signal.signal(signal.SIGINT, oldhandler) |
|
95 |
problem |
|
|
95 | problem = None | |
|
96 | 96 | for i in xrange(workers): |
|
97 |
p |
|
|
98 | if problems: | |
|
99 | sys.exit(1) | |
|
97 | pid, st = os.wait() | |
|
98 | st = _exitstatus(st) | |
|
99 | if st and not problem: | |
|
100 | problem = st | |
|
101 | if problem: | |
|
102 | if problem < 0: | |
|
103 | os.kill(os.getpid(), -problem) | |
|
104 | sys.exit(problem) | |
|
100 | 105 | try: |
|
101 | 106 | for line in fp: |
|
102 | 107 | l = line.split(' ', 1) |
|
103 | 108 | yield int(l[0]), l[1][:-1] |
|
104 | 109 | except: # re-raises |
|
105 | 110 | cleanup() |
|
106 | 111 | raise |
|
107 | 112 | cleanup() |
|
108 | 113 | |
|
114 | def _posixexitstatus(code): | |
|
115 | '''convert a posix exit status into the same form returned by | |
|
116 | os.spawnv | |
|
117 | ||
|
118 | returns None if the process was stopped instead of exiting''' | |
|
119 | if os.WIFEXITED(code): | |
|
120 | return os.WEXITSTATUS(code) | |
|
121 | elif os.WIFSIGNALED(code): | |
|
122 | return -os.WTERMSIG(code) | |
|
123 | ||
|
109 | 124 | if os.name != 'nt': |
|
110 | 125 | _platformworker = _posixworker |
|
126 | _exitstatus = _posixexitstatus | |
|
111 | 127 | |
|
112 | 128 | def partition(lst, nslices): |
|
113 | 129 | '''partition a list into N slices of equal size''' |
|
114 | 130 | n = len(lst) |
|
115 | 131 | chunk, slop = n / nslices, n % nslices |
|
116 | 132 | end = 0 |
|
117 | 133 | for i in xrange(nslices): |
|
118 | 134 | start = end |
|
119 | 135 | end = start + chunk |
|
120 | 136 | if slop: |
|
121 | 137 | end += 1 |
|
122 | 138 | slop -= 1 |
|
123 | 139 | yield lst[start:end] |
General Comments 0
You need to be logged in to leave comments.
Login now