diff --git a/jupyter_resource_usage/api.py b/jupyter_resource_usage/api.py index c0587ed..2670bd8 100644 --- a/jupyter_resource_usage/api.py +++ b/jupyter_resource_usage/api.py @@ -47,18 +47,19 @@ async def get(self): metrics = {"rss": rss, "limits": limits} - # Always get cpu usage information - cpu_count = psutil.cpu_count() - cpu_percent = await self._get_cpu_percent(all_processes) - - if config.cpu_limit != 0: - limits["cpu"] = {"cpu": config.cpu_limit} - if config.cpu_warning_threshold != 0: - limits["cpu"]["warn"] = (config.cpu_limit - cpu_percent) < ( - config.cpu_limit * config.cpu_warning_threshold - ) - - metrics.update(cpu_percent=cpu_percent, cpu_count=cpu_count) + # Optionally get CPU information + if config.track_cpu_percent: + cpu_count = psutil.cpu_count() + cpu_percent = await self._get_cpu_percent(all_processes) + + if config.cpu_limit != 0: + limits["cpu"] = {"cpu": config.cpu_limit} + if config.cpu_warning_threshold != 0: + limits["cpu"]["warn"] = (config.cpu_limit - cpu_percent) < ( + config.cpu_limit * config.cpu_warning_threshold + ) + + metrics.update(cpu_percent=cpu_percent, cpu_count=cpu_count) self.write(json.dumps(metrics)) diff --git a/jupyter_resource_usage/config.py b/jupyter_resource_usage/config.py index e8622bf..a047ebd 100644 --- a/jupyter_resource_usage/config.py +++ b/jupyter_resource_usage/config.py @@ -87,9 +87,9 @@ def _mem_limit_default(self): return int(os.environ.get("MEM_LIMIT", 0)) track_cpu_percent = Bool( - default_value=False, + default_value=True, help=""" - Set to True in order to enable reporting of CPU usage statistics. + Set to False in order to disable reporting of CPU usage statistics. """, ).tag(config=True)