bug 1505205 - don't write telemetry for recursive mach command invocations. 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
This commit is contained in:
Ted Mielczarek 2018-11-10 19:04:30 +00:00
parent e85df1cdc8
commit 54b3bbd1ce
6 changed files with 58 additions and 9 deletions

View File

@ -201,7 +201,7 @@ def bootstrap(topsrcdir, mozilla_dir=None):
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
@ -209,17 +209,22 @@ def bootstrap(topsrcdir, mozilla_dir=None):
'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
@ -270,8 +275,7 @@ def bootstrap(topsrcdir, mozilla_dir=None):
'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
@ -320,6 +324,11 @@ def bootstrap(topsrcdir, mozilla_dir=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

View File

@ -24,6 +24,7 @@ class MachRegistrar(object):
self.settings_providers = set()
self.categories = {}
self.require_conditions = False
self.command_depth = 0
def register_command_handler(self, handler):
name = handler.name
@ -82,6 +83,7 @@ class MachRegistrar(object):
print(self._condition_failed_message(handler.name, fail_conditions))
return 1
self.command_depth += 1
fn = getattr(instance, handler.method)
start_time = time.time()
@ -101,7 +103,8 @@ class MachRegistrar(object):
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

View File

@ -0,0 +1,4 @@
import subprocess
import sys
subprocess.check_call([sys.executable] + sys.argv[1:])

View File

@ -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

View File

@ -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):
@ -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()

View File

@ -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)
@ -57,6 +60,10 @@ class MachCommands(MachCommandBase):
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.