|
| 1 | +# Copyright 2017, David Wilson |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions are met: |
| 5 | +# |
| 6 | +# 1. Redistributions of source code must retain the above copyright notice, |
| 7 | +# this list of conditions and the following disclaimer. |
| 8 | +# |
| 9 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +# this list of conditions and the following disclaimer in the documentation |
| 11 | +# and/or other materials provided with the distribution. |
| 12 | +# |
| 13 | +# 3. Neither the name of the copyright holder nor the names of its contributors |
| 14 | +# may be used to endorse or promote products derived from this software without |
| 15 | +# specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +# POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +import ctypes |
| 30 | +import mmap |
| 31 | +import multiprocessing |
| 32 | +import os |
| 33 | +import struct |
| 34 | + |
| 35 | +import mitogen.parent |
| 36 | + |
| 37 | + |
| 38 | +try: |
| 39 | + _libc = ctypes.CDLL(None, use_errno=True) |
| 40 | + _strerror = _libc.strerror |
| 41 | + _strerror.restype = ctypes.c_char_p |
| 42 | + _pthread_mutex_init = _libc.pthread_mutex_init |
| 43 | + _pthread_mutex_lock = _libc.pthread_mutex_lock |
| 44 | + _pthread_mutex_unlock = _libc.pthread_mutex_unlock |
| 45 | + _sched_setaffinity = _libc.sched_setaffinity |
| 46 | +except (OSError, AttributeError): |
| 47 | + _libc = None |
| 48 | + |
| 49 | + |
| 50 | +class pthread_mutex_t(ctypes.Structure): |
| 51 | + _fields_ = [ |
| 52 | + ('data', ctypes.c_uint8 * 512), |
| 53 | + ] |
| 54 | + |
| 55 | + def init(self): |
| 56 | + if _pthread_mutex_init(self.data, 0): |
| 57 | + raise Exception(_strerror(ctypes.get_errno())) |
| 58 | + |
| 59 | + def acquire(self): |
| 60 | + if _pthread_mutex_lock(self.data): |
| 61 | + raise Exception(_strerror(ctypes.get_errno())) |
| 62 | + |
| 63 | + def release(self): |
| 64 | + if _pthread_mutex_unlock(self.data): |
| 65 | + raise Exception(_strerror(ctypes.get_errno())) |
| 66 | + |
| 67 | + |
| 68 | +class State(ctypes.Structure): |
| 69 | + _fields_ = [ |
| 70 | + ('lock', pthread_mutex_t), |
| 71 | + ('counter', ctypes.c_uint8), |
| 72 | + ] |
| 73 | + |
| 74 | + |
| 75 | +class Manager(object): |
| 76 | + """ |
| 77 | + Bind this process to a randomly selected CPU. If done prior to starting |
| 78 | + threads, all threads will be bound to the same CPU. This call is a no-op on |
| 79 | + systems other than Linux. |
| 80 | +
|
| 81 | + A hook is installed that causes `reset_affinity(clear=True)` to run in the |
| 82 | + child of any process created with :func:`mitogen.parent.detach_popen`, |
| 83 | + ensuring CPU-intensive children like SSH are not forced to share the same |
| 84 | + core as the (otherwise potentially very busy) parent. |
| 85 | +
|
| 86 | + Threads bound to the same CPU share cache and experience the lowest |
| 87 | + possible inter-thread roundtrip latency, for example ensuring the minimum |
| 88 | + possible time required for :class:`mitogen.service.Pool` to interact with |
| 89 | + :class:`mitogen.core.Broker`, as required for every message transmitted or |
| 90 | + received. |
| 91 | +
|
| 92 | + Binding threads of a Python process to one CPU makes sense, as they are |
| 93 | + otherwise unable to operate in parallel, and all must acquire the same lock |
| 94 | + prior to executing. |
| 95 | + """ |
| 96 | + def __init__(self): |
| 97 | + self.mem = mmap.mmap(-1, 4096) |
| 98 | + self.state = State.from_buffer(self.mem) |
| 99 | + self.state.lock.init() |
| 100 | + |
| 101 | + def _set_affinity(self, mask): |
| 102 | + mitogen.parent._preexec_hook = self.clear |
| 103 | + s = struct.pack('L', mask) |
| 104 | + _sched_setaffinity(os.getpid(), len(s), s) |
| 105 | + |
| 106 | + def cpu_count(self): |
| 107 | + return multiprocessing.cpu_count() |
| 108 | + |
| 109 | + def clear(self): |
| 110 | + """ |
| 111 | + Clear any prior binding, except for reserved CPUs. |
| 112 | + """ |
| 113 | + self._set_affinity(0xffffffff & ~3) |
| 114 | + |
| 115 | + def set_cpu(self, cpu): |
| 116 | + """ |
| 117 | + Bind to 0-based `cpu`. |
| 118 | + """ |
| 119 | + self._set_affinity(1 << cpu) |
| 120 | + |
| 121 | + def assign(self): |
| 122 | + self.state.lock.acquire() |
| 123 | + try: |
| 124 | + n = self.state.counter |
| 125 | + self.state.counter += 1 |
| 126 | + finally: |
| 127 | + self.state.lock.release() |
| 128 | + |
| 129 | + self.set_cpu(2 + (n % (self.cpu_count() - 2))) |
| 130 | + |
| 131 | + |
| 132 | +manager = Manager() |
0 commit comments