Skip to content

Commit d3ac941

Browse files
authored
Merge pull request #754 from mlco2/doc_service
Document how to run CodeCarbon as a service
2 parents 3a5856c + d8c5087 commit d3ac941

File tree

4 files changed

+126
-9
lines changed

4 files changed

+126
-9
lines changed

.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ __pycache__/
88
*intel_power_gadget_log.csv
99
*powermetrics_log.txt
1010
!tests/test_data/mock*
11-
.codecarbon.config
1211

1312
# C extensions
1413
*.so
15-
.codecarbon.config
1614

1715
# Distribution / packaging
1816
.Python
@@ -132,4 +130,5 @@ tests/test_data/rapl/*
132130
*.cast
133131

134132
# credentials
135-
credentials.json
133+
credentials*
134+
.codecarbon.config*

codecarbon/cli/main.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
import time
34
from pathlib import Path
45
from typing import Optional
@@ -99,12 +100,14 @@ def show_config(path: Path = Path("./.codecarbon.config")) -> None:
99100
)
100101

101102

102-
fief = Fief(AUTH_SERVER_URL, AUTH_CLIENT_ID)
103-
fief_auth = FiefAuth(fief, "./credentials.json")
103+
def get_fief_auth():
104+
fief = Fief(AUTH_SERVER_URL, AUTH_CLIENT_ID)
105+
fief_auth = FiefAuth(fief, "./credentials.json")
106+
return fief_auth
104107

105108

106109
def _get_access_token():
107-
access_token_info = fief_auth.access_token_info()
110+
access_token_info = get_fief_auth().access_token_info()
108111
access_token = access_token_info["access_token"]
109112
return access_token
110113

@@ -124,7 +127,7 @@ def api_get():
124127

125128
@codecarbon.command("login", short_help="Login to CodeCarbon")
126129
def login():
127-
fief_auth.authorize()
130+
get_fief_auth().authorize()
128131

129132

130133
def get_api_key(project_id: str):
@@ -327,7 +330,10 @@ def monitor(
327330
experiment_id = get_existing_local_exp_id()
328331
if api:
329332
if experiment_id is None:
330-
print("ERROR: No experiment id, call 'codecarbon config' first.", err=True)
333+
print(
334+
"ERROR: No experiment id, call 'codecarbon config' first.",
335+
file=sys.stderr,
336+
)
331337
print("CodeCarbon is going in an infinite loop to monitor this machine.")
332338
with EmissionsTracker(
333339
measure_power_secs=measure_power_secs,

docs/edit/installation.rst

+112
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,115 @@ The following packages are used by the CodeCarbon package, and will be installed
6262
6363
6464
Please refer to `pyproject.toml <https://github.com/mlco2/codecarbon/blob/master/pyproject.toml>`_ for the latest list of the packages used.
65+
66+
Install CodeCarbon as a Linux service
67+
-------------------------------------
68+
69+
To install CodeCarbon as a Linux service, follow the instructions below. It works on Ubuntu or other Debian-based systems using systemd.
70+
71+
Create a dedicated user:
72+
73+
.. code-block:: bash
74+
75+
sudo useradd -r -s /bin/false codecarbon
76+
77+
Create a directory for the CodeCarbon service:
78+
79+
.. code-block:: bash
80+
81+
sudo mkdir /opt/codecarbon
82+
83+
Change the ownership of the directory to the user created above:
84+
85+
.. code-block:: bash
86+
87+
sudo chown codecarbon:codecarbon /opt/codecarbon
88+
89+
Create a virtual environment for CodeCarbon :
90+
91+
.. code-block:: bash
92+
93+
sudo apt install python3-venv
94+
sudo -u codecarbon python3 -m venv /opt/codecarbon/.venv
95+
96+
Install CodeCarbon in the virtual environment:
97+
98+
.. code-block:: bash
99+
100+
sudo -u codecarbon /opt/codecarbon/.venv/bin/pip install codecarbon
101+
102+
Go to https://dashboard.codecarbon.io/ and create an account to get your API key.
103+
104+
Configure CodeCarbon:
105+
106+
.. code-block:: bash
107+
108+
sudo -u codecarbon /opt/codecarbon/.venv/bin/codecarbon login
109+
110+
Create a systemd service file:
111+
112+
.. code-block:: bash
113+
114+
sudo tee /etc/systemd/system/codecarbon.service <<EOF
115+
[Unit]
116+
Description=CodeCarbon service
117+
After=network.target
118+
119+
[Service]
120+
User=codecarbon
121+
Group=codecarbon
122+
WorkingDirectory=/opt/codecarbon
123+
ExecStart=/opt/codecarbon/.venv/bin/codecarbon monitor
124+
Restart=always
125+
126+
[Install]
127+
WantedBy=multi-user.target
128+
EOF
129+
130+
Give permissions to the ``codecarbon`` group to read the RAPL (Running Average Power Limit) information:
131+
132+
.. code-block:: bash
133+
134+
sudo chown -R root:codecarbon /sys/class/powercap/intel-rapl/*
135+
sudo chmod g+r -R /sys/class/powercap/intel-rapl/*
136+
137+
sudo apt install sysfsutils
138+
echo "mode class/powercap/intel-rapl:0/energy_uj = 0440" >> /etc/sysfs.conf
139+
echo "owner class/powercap/intel-rapl:0/energy_uj = root:codecarbon" >> /etc/sysfs.conf
140+
141+
Create the configuration file for CodeCarbon:
142+
143+
.. code-block:: bash
144+
145+
sudo tee /opt/codecarbon/.codecarbon.config <<EOF
146+
[codecarbon]
147+
api_endpoint = https://api.codecarbon.io
148+
organization_id = <organization_id>
149+
project_id = <project_id>
150+
experiment_id = <experiment_id>
151+
api_key = <api_key>
152+
# Verbose logging
153+
log_level=DEBUG
154+
# Measure power every 30 seconds
155+
measure_power_secs=30
156+
# Send measure to API every 5 minutes (10*30 seconds)
157+
api_call_interval=10
158+
EOF
159+
160+
Enable and start the service:
161+
162+
.. code-block:: bash
163+
164+
sudo systemctl enable codecarbon
165+
sudo systemctl start codecarbon
166+
167+
Check the traces of the service:
168+
169+
.. code-block:: bash
170+
171+
journalctl -u codecarbon
172+
173+
174+
You are done, CodeCarbon is now running as a service on your machine.
175+
176+
Wait 5 minutes for the first measure to be send to the dashboard at https://dashboard.codecarbon.io/.

docs/edit/methodology.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ If you do not want to give sudo rights to your user, then CodeCarbon will fall b
120120

121121
- **On Linux**
122122

123-
Tracks Intel and AMD processor energy consumption from Intel RAPL files at ``\sys\class\powercap\intel-rapl`` ( `reference <https://web.eece.maine.edu/~vweaver/projects/rapl/>`_ ).
123+
Tracks Intel and AMD processor energy consumption from Intel RAPL files at ``/sys/class/powercap/intel-rapl`` ( `reference <https://web.eece.maine.edu/~vweaver/projects/rapl/>`_ ).
124124
All CPUs listed in this directory will be tracked. `Help us improve this and make it configurable <https://github.com/mlco2/codecarbon/issues/156>`_.
125125

126126
*Note*: The Power Consumption will be tracked only if the RAPL files exist at the above-mentioned path

0 commit comments

Comments
 (0)