Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .context/gpu-benchmark-results.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"timestamp": "2026-06-22T13:04:21.864329+00:00", "mode": "all", "hostname": "Mac.lan", "gpu_backend": "simulated (8 GPUs)", "gpu_count_real": 0, "simulated": true, "call_breakdown": [{"call_name": "nvmlDeviceGetMemoryInfo", "used_by_monitor": false, "simulated_latency_us": 50}, {"call_name": "nvmlDeviceGetTemperature", "used_by_monitor": false, "simulated_latency_us": 40}, {"call_name": "nvmlDeviceGetPowerUsage", "used_by_monitor": false, "simulated_latency_us": 45}, {"call_name": "nvmlDeviceGetTotalEnergyConsumption", "used_by_monitor": false, "simulated_latency_us": 40}, {"call_name": "nvmlDeviceGetUtilizationRates", "used_by_monitor": true, "simulated_latency_us": 50}, {"call_name": "nvmlDeviceGetComputeMode", "used_by_monitor": false, "simulated_latency_us": 35}, {"call_name": "nvmlDeviceGetComputeRunningProcesses", "used_by_monitor": false, "simulated_latency_us": 500}, {"call_name": "nvmlDeviceGetGraphicsRunningProcesses", "used_by_monitor": false, "simulated_latency_us": 500}], "method_benchmarks": [{"method": "get_gpu_details", "gpu_count": 8, "nvml_calls_per_second": 64, "nvml_calls_unused_per_second": 56, "latency_per_call_ms": {"count": 200, "min_ms": 12.55650000530295, "max_ms": 67.93629100138787, "mean_ms": 13.547916884053848, "p50_ms": 12.920312496135011, "p95_ms": 15.044183352438257}, "latency_per_second_ms": 12.920312496135011}, {"method": "get_gpu_utilization_list", "gpu_count": 8, "nvml_calls_per_second": 8, "nvml_calls_unused_per_second": 0, "latency_per_call_ms": {"count": 200, "min_ms": 0.5256250005913898, "max_ms": 0.6670419970760122, "mean_ms": 0.5427240101562347, "p50_ms": 0.5367710036807694, "p95_ms": 0.5937666595855262}, "latency_per_second_ms": 0.5367710036807694}], "projections": [{"metric": "Per-second monitoring overhead", "heavy_path_ms": 12.920312496135011, "lightweight_path_ms": 0.5367710036807694, "savings_ms": 12.383541492454242, "savings_pct": 95.8, "unit": "ms/s"}, {"metric": "Per-minute monitoring overhead", "heavy_path_ms": 775.2187497681007, "lightweight_path_ms": 32.20626022084616, "savings_ms": 743.0124895472545, "savings_pct": 95.8, "unit": "ms/min"}, {"metric": "Per-hour monitoring overhead", "heavy_path_ms": 46513.12498608604, "lightweight_path_ms": 1932.3756132507697, "savings_ms": 44580.74937283527, "savings_pct": 95.8, "unit": "ms/hr"}, {"metric": "Per-day monitoring overhead (24h)", "heavy_path_ms": 1116314.999666065, "lightweight_path_ms": 46377.01471801847, "savings_ms": 1069937.9849480465, "savings_pct": 95.8, "unit": "ms/day"}, {"metric": "Unnecessary NVML calls per second", "heavy_path_value": 56, "lightweight_path_value": 0, "savings_value": 56, "unit": "calls/s"}, {"metric": "Unnecessary NVML calls per hour (on 8 GPUs)", "heavy_path_value": 201600, "lightweight_path_value": 0, "savings_value": 201600, "unit": "calls/hr"}], "result": ""}
{"timestamp": "2026-06-22T13:04:31.008513+00:00", "mode": "all", "hostname": "Mac.lan", "gpu_backend": "simulated (1 GPU)", "gpu_count_real": 0, "simulated": true, "call_breakdown": [{"call_name": "nvmlDeviceGetMemoryInfo", "used_by_monitor": false, "simulated_latency_us": 50}, {"call_name": "nvmlDeviceGetTemperature", "used_by_monitor": false, "simulated_latency_us": 40}, {"call_name": "nvmlDeviceGetPowerUsage", "used_by_monitor": false, "simulated_latency_us": 45}, {"call_name": "nvmlDeviceGetTotalEnergyConsumption", "used_by_monitor": false, "simulated_latency_us": 40}, {"call_name": "nvmlDeviceGetUtilizationRates", "used_by_monitor": true, "simulated_latency_us": 50}, {"call_name": "nvmlDeviceGetComputeMode", "used_by_monitor": false, "simulated_latency_us": 35}, {"call_name": "nvmlDeviceGetComputeRunningProcesses", "used_by_monitor": false, "simulated_latency_us": 500}, {"call_name": "nvmlDeviceGetGraphicsRunningProcesses", "used_by_monitor": false, "simulated_latency_us": 500}], "method_benchmarks": [{"method": "get_gpu_details", "gpu_count": 1, "nvml_calls_per_second": 8, "nvml_calls_unused_per_second": 7, "latency_per_call_ms": {"count": 200, "min_ms": 1.5633330040145665, "max_ms": 1.9456660083960742, "mean_ms": 1.6176056357653579, "p50_ms": 1.605146004294511, "p95_ms": 1.665493459586287}, "latency_per_second_ms": 1.605146004294511}, {"method": "get_gpu_utilization_list", "gpu_count": 1, "nvml_calls_per_second": 1, "nvml_calls_unused_per_second": 0, "latency_per_call_ms": {"count": 200, "min_ms": 0.06458298594225198, "max_ms": 0.14504100545309484, "mean_ms": 0.06767040984414052, "p50_ms": 0.06535449210787192, "p95_ms": 0.0750916529796086}, "latency_per_second_ms": 0.06535449210787192}], "projections": [{"metric": "Per-second monitoring overhead", "heavy_path_ms": 1.605146004294511, "lightweight_path_ms": 0.06535449210787192, "savings_ms": 1.539791512186639, "savings_pct": 95.9, "unit": "ms/s"}, {"metric": "Per-minute monitoring overhead", "heavy_path_ms": 96.30876025767066, "lightweight_path_ms": 3.921269526472315, "savings_ms": 92.38749073119834, "savings_pct": 95.9, "unit": "ms/min"}, {"metric": "Per-hour monitoring overhead", "heavy_path_ms": 5778.525615460239, "lightweight_path_ms": 235.2761715883389, "savings_ms": 5543.2494438719, "savings_pct": 95.9, "unit": "ms/hr"}, {"metric": "Per-day monitoring overhead (24h)", "heavy_path_ms": 138684.61477104574, "lightweight_path_ms": 5646.628118120134, "savings_ms": 133037.9866529256, "savings_pct": 95.9, "unit": "ms/day"}, {"metric": "Unnecessary NVML calls per second", "heavy_path_value": 7, "lightweight_path_value": 0, "savings_value": 7, "unit": "calls/s"}, {"metric": "Unnecessary NVML calls per hour (on 1 GPU)", "heavy_path_value": 25200, "lightweight_path_value": 0, "savings_value": 25200, "unit": "calls/hr"}], "result": ""}
{"timestamp": "2026-07-02T03:54:32.856813+00:00", "mode": "all", "hostname": "Mac.lan", "gpu_backend": "simulated (1 GPU)", "gpu_count_real": 0, "simulated": true, "call_breakdown": [{"call_name": "nvmlDeviceGetMemoryInfo", "used_by_monitor": false, "simulated_latency_us": 50}, {"call_name": "nvmlDeviceGetTemperature", "used_by_monitor": false, "simulated_latency_us": 40}, {"call_name": "nvmlDeviceGetPowerUsage", "used_by_monitor": false, "simulated_latency_us": 45}, {"call_name": "nvmlDeviceGetTotalEnergyConsumption", "used_by_monitor": false, "simulated_latency_us": 40}, {"call_name": "nvmlDeviceGetUtilizationRates", "used_by_monitor": true, "simulated_latency_us": 50}, {"call_name": "nvmlDeviceGetComputeMode", "used_by_monitor": false, "simulated_latency_us": 35}, {"call_name": "nvmlDeviceGetComputeRunningProcesses", "used_by_monitor": false, "simulated_latency_us": 500}, {"call_name": "nvmlDeviceGetGraphicsRunningProcesses", "used_by_monitor": false, "simulated_latency_us": 500}], "method_benchmarks": [{"method": "get_gpu_details", "gpu_count": 1, "nvml_calls_per_second": 8, "nvml_calls_unused_per_second": 7, "latency_per_call_ms": {"count": 200, "min_ms": 1.4970839984016493, "max_ms": 1.756750003551133, "mean_ms": 1.6207081100947107, "p50_ms": 1.6159379993041512, "p95_ms": 1.666629193641711}, "latency_per_second_ms": 1.6159379993041512}, {"method": "get_gpu_utilization_list", "gpu_count": 1, "nvml_calls_per_second": 1, "nvml_calls_unused_per_second": 0, "latency_per_call_ms": {"count": 200, "min_ms": 0.062167004216462374, "max_ms": 0.13416699948720634, "mean_ms": 0.06766192989744013, "p50_ms": 0.0667294989398215, "p95_ms": 0.07051039865473285}, "latency_per_second_ms": 0.0667294989398215}], "projections": [{"metric": "Per-second monitoring overhead", "heavy_path_ms": 1.6159379993041512, "lightweight_path_ms": 0.0667294989398215, "savings_ms": 1.5492085003643297, "savings_pct": 95.9, "unit": "ms/s"}, {"metric": "Per-minute monitoring overhead", "heavy_path_ms": 96.95627995824907, "lightweight_path_ms": 4.00376993638929, "savings_ms": 92.95251002185978, "savings_pct": 95.9, "unit": "ms/min"}, {"metric": "Per-hour monitoring overhead", "heavy_path_ms": 5817.376797494944, "lightweight_path_ms": 240.2261961833574, "savings_ms": 5577.150601311587, "savings_pct": 95.9, "unit": "ms/hr"}, {"metric": "Per-day monitoring overhead (24h)", "heavy_path_ms": 139617.04313987866, "lightweight_path_ms": 5765.428708400577, "savings_ms": 133851.61443147808, "savings_pct": 95.9, "unit": "ms/day"}, {"metric": "Unnecessary NVML calls per second", "heavy_path_value": 7, "lightweight_path_value": 0, "savings_value": 7, "unit": "calls/s"}, {"metric": "Unnecessary NVML calls per hour (on 1 GPU)", "heavy_path_value": 25200, "lightweight_path_value": 0, "savings_value": 25200, "unit": "calls/hr"}], "result": ""}
17 changes: 17 additions & 0 deletions codecarbon/core/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ def get_gpu_details(self) -> List:
logger.warning("Failed to retrieve gpu information", exc_info=True)
return []

def get_gpu_utilization_list(self) -> List:
"""Lightweight alternative to :meth:`get_gpu_details` for the 1s
monitoring hot path. Returns only ``gpu_index`` and
``gpu_utilization`` per device, skipping heavyweight queries
(memory, temperature, compute mode, process lists).

>>> get_gpu_utilization_list()
[
{"gpu_index": 0, "gpu_utilization": 0},
]
"""
try:
return [d.get_gpu_utilization_lightweight() for d in self.devices]
except Exception:
logger.warning("Failed to retrieve gpu utilization", exc_info=True)
return []

def get_delta(self, last_duration: Time) -> List:
"""Get difference since last time this function was called
>>> get_delta()
Expand Down
14 changes: 14 additions & 0 deletions codecarbon/core/gpu_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ def get_gpu_details(self) -> Dict[str, Any]:
}
return device_details

def get_gpu_utilization_lightweight(self) -> Dict[str, Any]:
"""
Lightweight alternative to :meth:`get_gpu_details` for the hot path
(``_monitor_power`` which runs every 1s).

Only queries the GPU utilization — avoids heavyweight calls like
memory info, temperature, compute mode, and process lists which are
not consumed by the tracker's monitoring loop.
"""
return {
"gpu_index": self.gpu_index,
"gpu_utilization": self._get_gpu_utilization(),
}

def _to_utf8(self, str_or_bytes) -> Any:
if hasattr(str_or_bytes, "decode"):
return str_or_bytes.decode("utf-8", errors="replace")
Expand Down
11 changes: 6 additions & 5 deletions codecarbon/emissions_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,15 +1147,16 @@ def _monitor_power(self) -> None:
self._ram_utilization_history.append(psutil.virtual_memory().percent)
self._ram_used_history.append(psutil.virtual_memory().used / (1024**3))

# Collect GPU utilization metrics
# Collect GPU utilization metrics (lightweight path — skips
# heavyweight calls like process lists, memory, temperature).
for hardware in self._hardware:
if isinstance(hardware, GPU):
gpu_ids_to_monitor = hardware.gpu_ids
gpu_details = hardware.devices.get_gpu_details()
for gpu_index, gpu_detail in enumerate(gpu_details):
resolved_gpu_index = gpu_detail.get("gpu_index", gpu_index)
for gpu_detail in hardware.devices.get_gpu_utilization_list():
resolved_gpu_index = gpu_detail.get("gpu_index")
if (
resolved_gpu_index in gpu_ids_to_monitor
resolved_gpu_index is not None
and resolved_gpu_index in gpu_ids_to_monitor
and "gpu_utilization" in gpu_detail
):
self._gpu_utilization_history.append(
Expand Down
Loading