Skip to content

Commit

Permalink
bug 1505205 - don't write telemetry for recursive mach command invoca…
Browse files Browse the repository at this point in the history
…tions. r=firefox-build-system-reviewers,chmanchester

This change tries to ensure that we don't write telemetry data for mach
commands invoked recursively as part of other mach commands. The intent of
build system telemetry is to only collect data about commands that users are
invoking directly.

There are two ways that we found mach commands can be recursively invoked:
* By running a python subprocess to recursively invoke mach (used in
  `mach bootstrap` to call `mach artifact toolchain`)
* By using `Registrar.dispatch` to delegate to a sub-command (used by many
  build system commands to invoke `mach build`).

The subprocess case is handled here by having mach set a `MACH_MAIN_PID`
environment variable whose value is the current process' pid on startup if it
does not already exist in the environment. Telemetry code then checks that the
value of that variable matches the current pid and skips writing telemetry data
if not.

The dispatch case is handled by making `MachRegistrar` store the current depth
of the command stack and pass it to the `post_dispatch_handler` which will skip
writing telemetry data if depth != 1.

Additionally the `should_skip_dispatch` function in mach_bootstrap is renamed
to `should_skip_telemetry_submission`, which was its original intent. The
combination of checks added in this change should be sufficient for deciding
when to write telemetry data, and we were not collecting telemetry for the set
of mach commands in that function (which included `mach bootstrap`).

In order to facilitate writing a test for the dispatch case this change adds a
`mach python --exec-file` option to execute Python code directly in the context
of the `mach python` command.

Differential Revision: https://phabricator.services.mozilla.com/D11207

--HG--
extra : moz-landing-system : lando
  • Loading branch information
luser committed Nov 10, 2018
1 parent e85df1c commit 54b3bbd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
21 changes: 15 additions & 6 deletions build/mach_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,25 +201,30 @@ def resolve_repository():
mozversioncontrol.MissingVCSTool):
return None

def should_skip_dispatch(context, handler):
def should_skip_telemetry_submission(handler):
# The user is performing a maintenance command.
if handler.name in ('bootstrap', 'doctor', 'mach-commands', 'vcs-setup',
# We call mach environment in client.mk which would cause the
# data submission to block the forward progress of make.
'environment'):
return True

# Never submit data when running in automation or when running tests.
if any(e in os.environ for e in ('MOZ_AUTOMATION', 'TASK_ID', 'MACH_TELEMETRY_NO_SUBMIT')):
return True

return False

def post_dispatch_handler(context, handler, instance, result,
start_time, end_time, args):
start_time, end_time, depth, args):
"""Perform global operations after command dispatch.
For now, we will use this to handle build system telemetry.
"""
# Don't do anything when...
if should_skip_dispatch(context, handler):
# Don't write telemetry data if this mach command was invoked as part of another
# mach command.
if depth != 1 or os.environ.get('MACH_MAIN_PID') != str(os.getpid()):
return

# We have not opted-in to telemetry
Expand Down Expand Up @@ -270,8 +275,7 @@ def post_dispatch_handler(context, handler, instance, result,
'w') as f:
json.dump(data, f, sort_keys=True)

# Never submit data when running in automation.
if 'MOZ_AUTOMATION' in os.environ or 'TASK_ID' in os.environ:
if should_skip_telemetry_submission(handler):
return True

# But only submit about every n-th operation
Expand Down Expand Up @@ -320,6 +324,11 @@ def populate_context(context, key=None):

raise AttributeError(key)

# Note which process is top-level so that recursive mach invocations can avoid writing
# telemetry data.
if 'MACH_MAIN_PID' not in os.environ:
os.environ[b'MACH_MAIN_PID'] = str(os.getpid()).encode('ascii')

driver = mach.main.Mach(os.getcwd())
driver.populate_context_handler = populate_context

Expand Down
5 changes: 4 additions & 1 deletion python/mach/mach/registrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self):
self.settings_providers = set()
self.categories = {}
self.require_conditions = False
self.command_depth = 0

def register_command_handler(self, handler):
name = handler.name
Expand Down Expand Up @@ -82,6 +83,7 @@ def _run_command_handler(self, handler, context=None, debug_command=False, **kwa
print(self._condition_failed_message(handler.name, fail_conditions))
return 1

self.command_depth += 1
fn = getattr(instance, handler.method)

start_time = time.time()
Expand All @@ -101,7 +103,8 @@ def _run_command_handler(self, handler, context=None, debug_command=False, **kwa
postrun = getattr(context, 'post_dispatch_handler', None)
if postrun:
postrun(context, handler, instance, result,
start_time, end_time, args=kwargs)
start_time, end_time, self.command_depth, args=kwargs)
self.command_depth -= 1

return result

Expand Down
4 changes: 4 additions & 0 deletions python/mach/mach/test/invoke_mach_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import subprocess
import sys

subprocess.check_call([sys.executable] + sys.argv[1:])
3 changes: 3 additions & 0 deletions python/mach/mach/test/registrar_dispatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This code is loaded via `mach python --exec-file`, so it runs in the scope of
# the `mach python` command.
self._mach_context.commands.dispatch('uuid', self._mach_context) # noqa: F821
25 changes: 24 additions & 1 deletion python/mach/mach/test/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def run_mach(tmpdir):
# a machrc there.
update_or_create_build_telemetry_config(unicode(tmpdir.join('machrc')))
env = dict(os.environ)
env['MOZBUILD_STATE_PATH'] = str(tmpdir)
env[b'MOZBUILD_STATE_PATH'] = str(tmpdir)
env[b'MACH_TELEMETRY_NO_SUBMIT'] = b'1'
# Let whatever mach command we invoke from tests believe it's the main command.
del env['MACH_MAIN_PID']
mach = os.path.join(buildconfig.topsrcdir, 'mach')

def run(*args, **kwargs):
Expand Down Expand Up @@ -120,5 +123,25 @@ def test_path_filtering_other_cwd(run_mach, tmpdir):
assert d['argv'] == expected


def test_mach_invoke_recursive(run_mach):
data = run_mach('python',
os.path.join(os.path.dirname(__file__), 'invoke_mach_command.py'),
os.path.join(buildconfig.topsrcdir, 'mach'),
'uuid')
assert len(data) == 1
d = data[0]
assert d['command'] == 'python'


def test_registrar_dispatch(run_mach):
# Use --exec-file so this script can use Registrar.dispatch to dispatch a mach command
# from within the same interpreter as the `mach python` command.
data = run_mach('python', '--exec-file',
os.path.join(os.path.dirname(__file__), 'registrar_dispatch.py'))
assert len(data) == 1
d = data[0]
assert d['command'] == 'python'


if __name__ == '__main__':
mozunit.main()
9 changes: 8 additions & 1 deletion python/mach_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ class MachCommands(MachCommandBase):
description='Run Python.')
@CommandArgument('--no-virtualenv', action='store_true',
help='Do not set up a virtualenv')
@CommandArgument('--exec-file',
default=None,
help='Execute this Python file using `execfile`')
@CommandArgument('args', nargs=argparse.REMAINDER)
def python(self, no_virtualenv, args):
def python(self, no_virtualenv, exec_file, args):
# Avoid logging the command
self.log_manager.terminal_handler.setLevel(logging.CRITICAL)

Expand All @@ -57,6 +60,10 @@ def python(self, no_virtualenv, args):
self._activate_virtualenv()
python_path = self.virtualenv_manager.python_path

if exec_file:
execfile(exec_file)
return 0

return self.run_process([python_path] + args,
pass_thru=True, # Allow user to run Python interactively.
ensure_exit_code=False, # Don't throw on non-zero exit code.
Expand Down

0 comments on commit 54b3bbd

Please sign in to comment.