Skip to content

Commit ae74335

Browse files
committed
Allow simulating idle components
1 parent e43b3e4 commit ae74335

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

carbontracker/cli.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,28 @@ def main():
2525
The **carbontracker** CLI allows the user to track the energy consumption and carbon intensity of any program.
2626
[Make sure that you have relevant permissions before running this.](/#permissions)
2727
28-
Args:
29-
--log_dir (path, optional): Log directory. Defaults to `./logs`.
30-
--api_keys (str, optional): API keys in a dictionary-like format, e.g. `\'{"electricitymaps": "YOUR_KEY"}\'`
31-
--parse (path, optional): Directory containing the log files to parse.
32-
--report (path, optional): Generate a PDF report from a log file.
33-
--output (path, optional): Output path for the generated report. Defaults to 'carbon_report.pdf'
34-
--sim-cpu (str, optional): Simulated CPU name (overrides detection)
35-
--sim-cpu-tdp (float, optional): Simulated CPU TDP in Watts
36-
--sim-cpu-util (float, optional): Simulated CPU utilization (0.0 to 1.0)
37-
--sim-gpu (str, optional): Simulated GPU name (overrides detection)
38-
--sim-gpu-watts (float, optional): Simulated GPU power consumption in Watts
39-
--sim-gpu-util (float, optional): Simulated GPU utilization (0.0 to 1.0)
28+
The following command-line arguments are supported:
29+
30+
* `--log_dir` (str, optional): Log directory. Defaults to `./logs`.
31+
* `--api_keys` (str, optional): API keys in a dictionary-like format, e.g. `{ \\"electricitymaps\\": \\"YOUR_KEY\\" }`
32+
* `--parse` (str, optional): Directory containing the log files to parse.
33+
* `--report` (str, optional): Generate a PDF report from a log file.
34+
* `--output` (str, optional): Output path for the generated report. Defaults to 'carbon_report.pdf'
35+
* `--sim-cpu` (str, optional): Simulated CPU name (overrides detection)
36+
* `--sim-cpu-tdp` (float, optional): Simulated CPU TDP in Watts
37+
* `--sim-cpu-util` (float, optional): Simulated CPU utilization (0.0 to 1.0)
38+
* `--sim-gpu` (str, optional): Simulated GPU name (overrides detection)
39+
* `--sim-gpu-watts` (float, optional): Simulated GPU power consumption in Watts
40+
* `--sim-gpu-util` (float, optional): Simulated GPU utilization (0.0 to 1.0)
4041
4142
Example:
42-
Tracking the carbon intensity of `script.py`.
43+
Tracking the carbon intensity of `script.py`:
4344
4445
$ carbontracker python script.py
4546
46-
With example options
47+
With example options:
4748
48-
$ carbontracker --log_dir='./logs' --api_keys='{"electricitymaps": "API_KEY_EXAMPLE"}' python script.py
49+
$ carbontracker --log_dir='./logs' --api_keys='{ \\"electricitymaps\\": \\"API_KEY_EXAMPLE\\" }' python script.py
4950
5051
Using simulated hardware:
5152

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

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ See [CLI](documentation/CLI.md) for CLI options.
1111
**carbontracker** supports the following components:
1212

1313
- Intel CPUs that support [Intel RAPL](http://web.eece.maine.edu/~vweaver/projects/rapl/rapl_support.html) on Linux. [Note on how to enable permissions](/#permissions)
14-
- NVIDIA GPUs that support [NVIDIA Management Library (NVML)](Intel CPUs that support Intel RAPL) on Linux
14+
- NVIDIA GPUs that support [NVIDIA Management Library (NVML)] on Linux
1515
- Apple Silicon on MacOS
1616

1717
## Permissions

0 commit comments

Comments
 (0)