diff --git a/ansible_mitogen/logging.py b/ansible_mitogen/logging.py index cb472336..37e309e2 100644 --- a/ansible_mitogen/logging.py +++ b/ansible_mitogen/logging.py @@ -85,8 +85,16 @@ def setup(): mitogen.core.IOLOG.propagate = False if display.verbosity > 2: - logging.getLogger('ansible_mitogen').setLevel(logging.DEBUG) mitogen.core.LOG.setLevel(logging.DEBUG) + logging.getLogger('ansible_mitogen').setLevel(logging.DEBUG) + else: + # Mitogen copies the active log level into new children, allowing them + # to filter tiny messages before they hit the network, and therefore + # before they wake the IO loop. Explicitly setting INFO saves ~4% + # running against just the local machine. + mitogen.core.LOG.setLevel(logging.ERROR) + logging.getLogger('ansible_mitogen').setLevel(logging.ERROR) if display.verbosity > 3: mitogen.core.IOLOG.setLevel(logging.DEBUG) + logging.getLogger('ansible_mitogen').setLevel(logging.DEBUG) diff --git a/ansible_mitogen/services.py b/ansible_mitogen/services.py index ad28ce48..4f4779fe 100644 --- a/ansible_mitogen/services.py +++ b/ansible_mitogen/services.py @@ -297,7 +297,8 @@ class ContextService(mitogen.service.Service): lambda: self._on_stream_disconnect(stream)) self._send_module_forwards(context) - init_child_result = context.call(ansible_mitogen.target.init_child) + init_child_result = context.call(ansible_mitogen.target.init_child, + log_level=LOG.getEffectiveLevel()) if os.environ.get('MITOGEN_DUMP_THREAD_STACKS'): from mitogen import debug diff --git a/ansible_mitogen/target.py b/ansible_mitogen/target.py index 22c5cb02..e5365dd4 100644 --- a/ansible_mitogen/target.py +++ b/ansible_mitogen/target.py @@ -202,7 +202,7 @@ def reset_temp_dir(econtext): @mitogen.core.takes_econtext -def init_child(econtext): +def init_child(econtext, log_level): """ Called by ContextService immediately after connection; arranges for the (presently) spotless Python interpreter to be forked, where the newly @@ -213,6 +213,9 @@ def init_child(econtext): polluting the global interpreter state in a way that effects explicitly isolated modules. + :param int log_level: + Logging package level active in the master. + :returns: Dict like:: @@ -230,6 +233,12 @@ def init_child(econtext): _fork_parent = econtext.router.fork() reset_temp_dir(econtext) + # Copying the master's log level causes log messages to be filtered before + # they reach LogForwarder, thus reducing an influx of tiny messges waking + # the connection multiplexer process in the master. + LOG.setLevel(log_level) + logging.getLogger('ansible_mitogen').setLevel(log_level) + return { 'fork_context': _fork_parent, 'home_dir': mitogen.core.to_text(os.path.expanduser('~')),