tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 3af3263ff263064953e8a19f335a4254a78c8dfa
parent af0f713f785417023666ef557e856b3a9d132041
Author: serge-sans-paille <sguelton@mozilla.com>
Date:   Mon, 24 Nov 2025 22:26:33 +0000

Bug 2001600 - Harmonize and cache process listing for telemetry r=ahochheiden

We already rely a lot on psutil for telemetry, so let's use that once
and for all.

Also avoid listing long-running process several times.

This also saves around 40ms for every build process on my setup.

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

Diffstat:
Mpython/mozbuild/mozbuild/telemetry.py | 52+++++++++++++++++++++++-----------------------------
1 file changed, 23 insertions(+), 29 deletions(-)

diff --git a/python/mozbuild/mozbuild/telemetry.py b/python/mozbuild/mozbuild/telemetry.py @@ -6,6 +6,7 @@ This file contains functions used for telemetry. """ +import functools import os import platform import subprocess @@ -166,38 +167,31 @@ def get_shell_info(): def get_vscode_running(): """Return if the vscode is currently running.""" - try: - import psutil - - for proc in psutil.process_iter(): - try: - # On Windows we have "Code.exe" - # On MacOS we have "Code Helper (Renderer)" - # On Linux we have "" - if proc.name in ("Code.exe", "Code Helper (Renderer)", "code"): - return True - except Exception: - # may not be able to access process info for all processes - continue - except Exception: - # On some platforms, sometimes, the generator throws an - # exception preventing us to enumerate. - return False - - return False + return any( + _is_process_running(pname) + for pname in ("Code.exe", "Code Helper (Renderer)", "code") + ) def _is_process_running(process_name: str) -> bool: - """Check if a process is running using pgrep.""" - try: - result = subprocess.run( - ["pgrep", "-f", process_name], - capture_output=True, - check=False, - ) - return result.returncode == 0 - except (subprocess.CalledProcessError, FileNotFoundError): - return False + """Check if a process is running using psutil.""" + + @functools.cache + def list_running_processes(): + running_processes = set() + try: + import psutil + + for proc in psutil.process_iter(): + try: + running_processes.add(proc.name()) + except Exception: + continue + except Exception: + ... + return running_processes + + return process_name in list_running_processes() def get_fleet_running() -> bool: