Skip to content

Commit 953691b

Browse files
committed
Allow simulating idle components
1 parent e43b3e4 commit 953691b

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

carbontracker/components/component.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ def handlers_by_name(
5050
):
5151
for comp in COMPONENTS:
5252
if comp["name"] == name:
53-
if name == "cpu" and sim_cpu and sim_cpu_tdp:
53+
if name == "cpu" and sim_cpu is not None and sim_cpu_tdp is not None:
5454
return [lambda pids, devices_by_pid: SimulatedCPUHandler(
5555
sim_cpu,
5656
float(sim_cpu_tdp),
5757
float(sim_cpu_util) if sim_cpu_util is not None else 0.5
5858
)]
59-
elif name == "gpu" and sim_gpu and sim_gpu_watts:
59+
elif name == "gpu" and sim_gpu is not None and sim_gpu_watts is not None:
6060
return [lambda pids, devices_by_pid: SimulatedGPUHandler(
6161
sim_gpu,
6262
float(sim_gpu_watts),

carbontracker/components/cpu/sim_cpu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ def __init__(self, name: str, tdp: float, utilization: float = 0.5):
66
super().__init__(pids=[], devices_by_pid=False)
77
if not isinstance(name, str) or not name.strip():
88
raise ValueError("CPU name must be a non-empty string.")
9-
if tdp is None or not isinstance(tdp, (int, float)) or tdp <= 0:
10-
raise ValueError("CPU TDP must be a positive number.")
9+
if tdp is None or not isinstance(tdp, (int, float)) or tdp < 0:
10+
raise ValueError("CPU TDP must be a non-negative number.")
1111
if not isinstance(utilization, (int, float)) or not (0.0 <= utilization <= 1.0):
1212
raise ValueError("CPU utilization must be between 0.0 and 1.0.")
1313
self.cpu_brand = name

carbontracker/components/gpu/sim_gpu.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ def __init__(self, name: str, watts: float, utilization: float = 0.5):
66
super().__init__(pids=[], devices_by_pid=False)
77
if not isinstance(name, str) or not name.strip():
88
raise ValueError("GPU name must be a non-empty string.")
9-
if watts is None or not isinstance(watts, (int, float)) or watts <= 0:
10-
raise ValueError("GPU watts must be a positive number.")
9+
if watts is None or not isinstance(watts, (int, float)) or watts < 0:
10+
raise ValueError("GPU watts must be a non-negative number.")
1111
if not isinstance(utilization, (int, float)) or not (0.0 <= utilization <= 1.0):
1212
raise ValueError("GPU utilization must be between 0.0 and 1.0.")
1313
self.gpu_brand = name
@@ -26,5 +26,20 @@ def power_usage(self) -> List[float]:
2626
def init(self):
2727
print(f"Using simulated GPU: {self.gpu_brand} with power consumption: {self.watts:.2f}W (at {self.utilization*100:.0f}% utilization)")
2828

29+
def shutdown(self):
30+
pass
31+
32+
def devices(self) -> List[str]:
33+
return [self.gpu_brand]
34+
35+
def available(self) -> bool:
36+
return True
37+
38+
def power_usage(self) -> List[float]:
39+
return [self.watts]
40+
41+
def init(self):
42+
print(f"Using simulated GPU: {self.gpu_brand} with power consumption: {self.watts:.2f}W (at {self.utilization*100:.0f}% utilization)")
43+
2944
def shutdown(self):
3045
pass

0 commit comments

Comments
 (0)