From e647adc62e7219735b89982c3dfa12e4e5641dae Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 7 Sep 2018 22:32:00 +0100 Subject: [PATCH] ansible: copy GIL change from linear2 branch. Reduces runtime by 25% given 100 25ms SSH targets: ANSIBLE_STRATEGY=mitogen \ MITOGEN_POOL_SIZE=100 \ /usr/bin/time -l ansible k3-x100 -m shell -a hostname Before: 39.56 real 35.29 user 17.24 sys 59600896 maximum resident set size 1784252 page reclaims 9016 messages sent 10382 messages received 18774 voluntary context switches 770070 involuntary context switches After: 29.79 real 22.10 user 11.77 sys 59281408 maximum resident set size 1725268 page reclaims 8582 messages sent 9959 messages received 14582 voluntary context switches 75280 involuntary context switches --- ansible_mitogen/process.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ansible_mitogen/process.py b/ansible_mitogen/process.py index daffe7cf..6e18a863 100644 --- a/ansible_mitogen/process.py +++ b/ansible_mitogen/process.py @@ -87,6 +87,27 @@ def getenv_int(key, default=0): return default +def setup_gil(): + """ + Set extremely long GIL release interval to let threads naturally progress + through CPU-heavy sequences without forcing the wake of another thread that + may contend trying to run the same CPU-heavy code. For the new-style work, + this drops runtime ~33% and involuntary context switches by >80%, + essentially making threads cooperatively scheduled. + """ + try: + # Python 2. + sys.setcheckinterval(100000) + except AttributeError: + pass + + try: + # Python 3. + sys.setswitchinterval(10) + except AttributeError: + pass + + class MuxProcess(object): """ Implement a subprocess forked from the Ansible top-level, as a safe place @@ -147,6 +168,7 @@ class MuxProcess(object): if faulthandler is not None: faulthandler.enable() + setup_gil() cls.unix_listener_path = mitogen.unix.make_socket_path() cls.worker_sock, cls.child_sock = socket.socketpair() atexit.register(lambda: clean_shutdown(cls.worker_sock))