From 87aa7478e8be5c54083c1953aea096f8d7258c7f Mon Sep 17 00:00:00 2001 From: benoit-cty <4-benoit-cty@users.noreply.git.leximpact.dev> Date: Mon, 23 Dec 2024 20:13:56 +0100 Subject: [PATCH 1/5] Fix CLI error --- codecarbon/cli/main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/codecarbon/cli/main.py b/codecarbon/cli/main.py index 9f1614837..8e53d19a4 100644 --- a/codecarbon/cli/main.py +++ b/codecarbon/cli/main.py @@ -1,4 +1,5 @@ import os +import sys import time from pathlib import Path from typing import Optional @@ -327,7 +328,10 @@ def monitor( experiment_id = get_existing_local_exp_id() if api: if experiment_id is None: - print("ERROR: No experiment id, call 'codecarbon config' first.", err=True) + print( + "ERROR: No experiment id, call 'codecarbon config' first.", + file=sys.stderr, + ) print("CodeCarbon is going in an infinite loop to monitor this machine.") with EmissionsTracker( measure_power_secs=measure_power_secs, From 44a8a6010c7b62e427c991c26c616a011e1bb660 Mon Sep 17 00:00:00 2001 From: benoit-cty <4-benoit-cty@users.noreply.git.leximpact.dev> Date: Mon, 23 Dec 2024 22:51:03 +0100 Subject: [PATCH 2/5] wip : service doc --- docs/edit/installation.rst | 100 +++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/docs/edit/installation.rst b/docs/edit/installation.rst index 0e5cf619f..0a8b8c941 100755 --- a/docs/edit/installation.rst +++ b/docs/edit/installation.rst @@ -62,3 +62,103 @@ The following packages are used by the CodeCarbon package, and will be installed Please refer to `pyproject.toml `_ for the latest list of the packages used. + +Install CodeCarbon as a Linux service +------------------------------------- + +To install CodeCarbon as a Linux service, follow the instructions below. It works on Ubuntu or other Debian-based systems using systemd. + +Create a dedicated user: + +.. code-block:: bash + + sudo useradd -r -s /bin/false codecarbon + +Create a directory for the CodeCarbon service: + +.. code-block:: bash + + sudo mkdir /opt/codecarbon + +Change the ownership of the directory to the user created above: + +.. code-block:: bash + + sudo chown codecarbon:codecarbon /opt/codecarbon + +Create a virtual environment for CodeCarbon : + +.. code-block:: bash + + sudo apt install python3-venv + sudo -u codecarbon python3 -m venv /opt/codecarbon/.venv + +Install CodeCarbon in the virtual environment: + +.. code-block:: bash + + sudo -u codecarbon /opt/codecarbon/.venv/bin/pip install codecarbon + +Go to https://dashboard.codecarbon.io/ and create an account to get your API key. + +Configure CodeCarbon: + +.. code-block:: bash + + sudo -u codecarbon /opt/codecarbon/.venv/bin/codecarbon login + +Create a systemd service file: + +.. code-block:: bash + + sudo tee /etc/systemd/system/codecarbon.service <> /etc/sysfs.conf + echo "owner class/powercap/intel-rapl:0/energy_uj = root:codecarbon" >> /etc/sysfs.conf + From 15f56f9232f0562a5c2cdc8850e1eab99aad2b60 Mon Sep 17 00:00:00 2001 From: benoit-cty <4-benoit-cty@users.noreply.git.leximpact.dev> Date: Tue, 24 Dec 2024 10:45:18 +0100 Subject: [PATCH 3/5] Prevent reading "./credentials.json" if we don't need it to avoid Permission denied error. --- codecarbon/cli/main.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/codecarbon/cli/main.py b/codecarbon/cli/main.py index 8e53d19a4..d3c807b3d 100644 --- a/codecarbon/cli/main.py +++ b/codecarbon/cli/main.py @@ -100,12 +100,14 @@ def show_config(path: Path = Path("./.codecarbon.config")) -> None: ) -fief = Fief(AUTH_SERVER_URL, AUTH_CLIENT_ID) -fief_auth = FiefAuth(fief, "./credentials.json") +def get_fief_auth(): + fief = Fief(AUTH_SERVER_URL, AUTH_CLIENT_ID) + fief_auth = FiefAuth(fief, "./credentials.json") + return fief_auth def _get_access_token(): - access_token_info = fief_auth.access_token_info() + access_token_info = get_fief_auth().access_token_info() access_token = access_token_info["access_token"] return access_token @@ -125,7 +127,7 @@ def api_get(): @codecarbon.command("login", short_help="Login to CodeCarbon") def login(): - fief_auth.authorize() + get_fief_auth().authorize() def get_api_key(project_id: str): From c479e8997975c5ea60fed54ceb79294cc2f7fc9c Mon Sep 17 00:00:00 2001 From: benoit-cty <4-benoit-cty@users.noreply.git.leximpact.dev> Date: Tue, 24 Dec 2024 11:06:39 +0100 Subject: [PATCH 4/5] Doc --- docs/edit/installation.rst | 48 ++++++++++++++++++++++++-------------- docs/edit/methodology.rst | 2 +- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/docs/edit/installation.rst b/docs/edit/installation.rst index 0a8b8c941..37269f75f 100755 --- a/docs/edit/installation.rst +++ b/docs/edit/installation.rst @@ -120,7 +120,6 @@ Create a systemd service file: User=codecarbon Group=codecarbon WorkingDirectory=/opt/codecarbon - Environment="CODECARBON_API_KEY=YOUR_API_KEY" ExecStart=/opt/codecarbon/.venv/bin/codecarbon monitor Restart=always @@ -128,37 +127,50 @@ Create a systemd service file: WantedBy=multi-user.target EOF -Replace YOUR_API_KEY with the API key you obtained from the CodeCarbon dashboard. - -Enable and start the service: +Give permissions to the ``codecarbon`` group to read the RAPL (Running Average Power Limit) information: .. code-block:: bash - sudo systemctl enable codecarbon - sudo systemctl start codecarbon + sudo chown -R root:codecarbon /sys/class/powercap/intel-rapl/* + sudo chmod g+r -R /sys/class/powercap/intel-rapl/* -Check the status of the service: + sudo apt install sysfsutils + echo "mode class/powercap/intel-rapl:0/energy_uj = 0440" >> /etc/sysfs.conf + echo "owner class/powercap/intel-rapl:0/energy_uj = root:codecarbon" >> /etc/sysfs.conf -.. code-block:: bash +Create the configuration file for CodeCarbon: - sudo systemctl status codecarbon +.. code-block:: bash -You should see the service running. + sudo tee /opt/codecarbon/.codecarbon.config < + project_id = + experiment_id = + api_key = + # Verbose logging + log_level=DEBUG + # Measure power every 30 seconds + measure_power_secs=30 + # Send measure to API every 5 minutes (10*30 seconds) + api_call_interval=10 + EOF -To stop the service: +Enable and start the service: .. code-block:: bash - sudo systemctl stop codecarbon - + sudo systemctl enable codecarbon + sudo systemctl start codecarbon -Optionaly, you can also give permissions to the user to read the RAPL information: +Check the traces of the service: .. code-block:: bash - sudo chown -R root:codecarbon /sys/class/powercap/intel-rapl + journalctl -u codecarbon - sudo apt install sysfsutils - echo "mode class/powercap/intel-rapl:0/energy_uj = 0440" >> /etc/sysfs.conf - echo "owner class/powercap/intel-rapl:0/energy_uj = root:codecarbon" >> /etc/sysfs.conf +You are done, CodeCarbon is now running as a service on your machine. + +Wait 5 minutes for the first measure to be send to the dashboard at https://dashboard.codecarbon.io/. diff --git a/docs/edit/methodology.rst b/docs/edit/methodology.rst index 061b0d696..a58ee1e61 100644 --- a/docs/edit/methodology.rst +++ b/docs/edit/methodology.rst @@ -120,7 +120,7 @@ If you do not want to give sudo rights to your user, then CodeCarbon will fall b - **On Linux** -Tracks Intel and AMD processor energy consumption from Intel RAPL files at ``\sys\class\powercap\intel-rapl`` ( `reference `_ ). +Tracks Intel and AMD processor energy consumption from Intel RAPL files at ``/sys/class/powercap/intel-rapl`` ( `reference `_ ). All CPUs listed in this directory will be tracked. `Help us improve this and make it configurable `_. *Note*: The Power Consumption will be tracked only if the RAPL files exist at the above-mentioned path From d8c50879cd00e9d0e49f361724363ff38705be9a Mon Sep 17 00:00:00 2001 From: benoit-cty <4-benoit-cty@users.noreply.git.leximpact.dev> Date: Tue, 24 Dec 2024 11:06:53 +0100 Subject: [PATCH 5/5] Prevent token leaks --- .gitignore | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index eee478f7a..3b1e627a9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,11 +8,9 @@ __pycache__/ *intel_power_gadget_log.csv *powermetrics_log.txt !tests/test_data/mock* -.codecarbon.config # C extensions *.so -.codecarbon.config # Distribution / packaging .Python @@ -132,4 +130,5 @@ tests/test_data/rapl/* *.cast # credentials -credentials.json +credentials* +.codecarbon.config*