diff --git a/testing/mozbase/mozsystemmonitor/mozsystemmonitor/resourcemonitor.py b/testing/mozbase/mozsystemmonitor/mozsystemmonitor/resourcemonitor.py index 59db40c1e40a..dd2d8f448617 100644 --- a/testing/mozbase/mozsystemmonitor/mozsystemmonitor/resourcemonitor.py +++ b/testing/mozbase/mozsystemmonitor/mozsystemmonitor/resourcemonitor.py @@ -5,6 +5,7 @@ import multiprocessing import sys import time +import warnings # psutil will raise NotImplementedError if the platform is not supported. try: @@ -178,15 +179,26 @@ class SystemResourceMonitor(object): self._running = False self._stopped = False + self._process = None if psutil is None: return - cpu_percent = psutil.cpu_percent(0.0, True) - cpu_times = psutil.cpu_times(False) - io = get_disk_io_counters() - virt = psutil.virtual_memory() - swap = psutil.swap_memory() + # This try..except should not be needed! However, some tools (like + # |mach build|) attempt to load psutil before properly creating a + # virtualenv by building psutil. As a result, python/psutil may be in + # sys.path and its .py files may pick up the psutil C extension from + # the system install. If the versions don't match, we typically see + # failures invoking one of these functions. + try: + cpu_percent = psutil.cpu_percent(0.0, True) + cpu_times = psutil.cpu_times(False) + io = get_disk_io_counters() + virt = psutil.virtual_memory() + swap = psutil.swap_memory() + except Exception as e: + warnings.warn('psutil failed to run: %s' % e) + return self._cpu_cores = len(cpu_percent) self._cpu_times_type = type(cpu_times) @@ -215,7 +227,7 @@ class SystemResourceMonitor(object): You should only call this once per instance. """ - if psutil is None: + if not self._process: return self._running = True @@ -229,7 +241,7 @@ class SystemResourceMonitor(object): Currently, data is not available until you call stop(). """ - if psutil is None: + if not self._process: self._stopped = True return