Skip to content

Commit 5189408

Browse files
committed
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
1 parent e35d4cc commit 5189408

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

ansible_mitogen/process.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ def getenv_int(key, default=0):
8787
return default
8888

8989

90+
def setup_gil():
91+
"""
92+
Set extremely long GIL release interval to let threads naturally progress
93+
through CPU-heavy sequences without forcing the wake of another thread that
94+
may contend trying to run the same CPU-heavy code. For the new-style work,
95+
this drops runtime ~33% and involuntary context switches by >80%,
96+
essentially making threads cooperatively scheduled.
97+
"""
98+
try:
99+
# Python 2.
100+
sys.setcheckinterval(100000)
101+
except AttributeError:
102+
pass
103+
104+
try:
105+
# Python 3.
106+
sys.setswitchinterval(10)
107+
except AttributeError:
108+
pass
109+
110+
90111
class MuxProcess(object):
91112
"""
92113
Implement a subprocess forked from the Ansible top-level, as a safe place
@@ -147,6 +168,7 @@ def start(cls):
147168
if faulthandler is not None:
148169
faulthandler.enable()
149170

171+
setup_gil()
150172
cls.unix_listener_path = mitogen.unix.make_socket_path()
151173
cls.worker_sock, cls.child_sock = socket.socketpair()
152174
atexit.register(lambda: clean_shutdown(cls.worker_sock))

0 commit comments

Comments
 (0)