Skip to content

Commit

Permalink
Merge branch 'main' of github.com:green-coding-solutions/green-metric…
Browse files Browse the repository at this point in the history
…s-tool

* 'main' of github.com:green-coding-solutions/green-metrics-tool:
  Codespaces merge into main (#993)
  Container energy idle (#992)
  • Loading branch information
ArneTR committed Nov 23, 2024
2 parents 98fb794 + 6172f32 commit ac5d708
Show file tree
Hide file tree
Showing 41 changed files with 346 additions and 219 deletions.
36 changes: 36 additions & 0 deletions .devcontainer/codespace-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
set -euxo pipefail

# we have to rename this makefile as it doesn't compile in Codespaces
if [ -f /workspaces/green-metrics-tool/metric_providers/lm_sensors/Makefile ]; then
mv /workspaces/green-metrics-tool/metric_providers/lm_sensors/Makefile /workspaces/green-metrics-tool/metric_providers/lm_sensors/Makefile.bak
git update-index --assume-unchanged /workspaces/green-metrics-tool/metric_providers/lm_sensors/Makefile
fi

/workspaces/green-metrics-tool/install_linux.sh -p testpw -a "https://${CODESPACE_NAME}-9142.app.github.dev" -m "https://${CODESPACE_NAME}-9143.app.github.dev" -t -i -s -l
source venv/bin/activate

# Also add XGBoost, as we need it
python3 -m pip install -r /workspaces/green-metrics-tool/metric_providers/psu/energy/ac/xgboost/machine/model/requirements.txt

# make edits to ports so we can use 9143 to access front end
sed -i 's/listen \[::\]:9142;/listen [::]:9143;/; s/listen 9142;/listen 9143;/' /workspaces/green-metrics-tool/docker/nginx/frontend.conf
sed -i 's/- 9142:9142/- 9142:9142\n - 9143:9143/' /workspaces/green-metrics-tool/docker/compose.yml
sed -i 's|- ./nginx/block.conf|#- ./nginx/block.conf|' /workspaces/green-metrics-tool/docker/compose.yml

# activate XGBoost provider with sane values for GitHub Codespaces
sed -i 's/common:/common:\n psu.energy.ac.xgboost.machine.provider.PsuEnergyAcXgboostMachineProvider:\n resolution: 99\n CPUChips: 1\n HW_CPUFreq: 2800\n CPUCores: 32\n CPUThreads: 64\n TDP: 270\n HW_MemAmountGB: 256\n VHost_Ratio: 0.03125\n/' /workspaces/green-metrics-tool/config.yml


git clone https://github.com/green-coding-solutions/example-applications.git --depth=1 --single-branch /workspaces/green-metrics-tool/example-applications || true

source venv/bin/activate

docker compose -f /workspaces/green-metrics-tool/docker/compose.yml down

docker compose -f /workspaces/green-metrics-tool/docker/compose.yml up -d


gh codespace ports visibility 9142:public -c $CODESPACE_NAME

gh codespace ports visibility 9143:public -c $CODESPACE_NAME
31 changes: 31 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"postStartCommand": "/workspaces/green-metrics-tool/.devcontainer/on-start.sh",
"forwardPorts": [9142, 9143],
"portsAttributes": {
"9143": {
"label": "metrics page"
},
"9142": {
"label": "api"
}
},
"customizations": {
"codespaces": {
"openFiles": [
".devcontainer/splash.md"
]
},
"vscode": {
"settings": {
"workbench.editorAssociations": {
"*.md": "vscode.markdown.preview.editor" // Open markdown files in preview mode by default
}
},
"extensions":
[
"ms-python.python",
"ms-azuretools.vscode-docker"
]
}
}
}
17 changes: 17 additions & 0 deletions .devcontainer/on-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -euo pipefail

etc_hosts_line_1="127.0.0.1 green-coding-postgres-container"

echo "Writing to /etc/hosts file..."

# Entry 1 is needed for the local resolution of the containers through the jobs.py and runner.py
if ! sudo grep -Fxq "$etc_hosts_line_1" /etc/hosts; then
echo "$etc_hosts_line_1" | sudo tee -a /etc/hosts
else
echo "Entry was already present..."
fi

# Ensure that after a restart of the Codespace the ports are set to public again
gh codespace ports visibility 9142:public -c $CODESPACE_NAME
gh codespace ports visibility 9143:public -c $CODESPACE_NAME
35 changes: 35 additions & 0 deletions .devcontainer/splash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Green Metrics Tool Codespaces Quickstart

Thank you for trying out the Green Metrics Tool :-)

Please run the following command in the terminal to set up everything! 🚀

```sh
bash .devcontainer/codespace-setup.sh
```

It will take about 3 minutes.

Afterwards, load the python environment:

```sh
source venv/bin/activate
```

Do your first measurement run like this:

```sh
python3 runner.py --name "Simple Test" --uri "/workspaces/green-metrics-tool/example-applications/" --filename "stress/usage_scenario.yml" --skip-system-checks --dev-no-optimizations --dev-cache-build
```

Then, if you want to see a more representative repository, try running our Bakery Demo repository we did together with the Wagtail Community:

```sh
python3 runner.py --uri https://github.com/green-coding-solutions/bakerydemo --branch gmt --skip-system-checks --dev-no-optimization --dev-cache-build --skip-unsafe --name "Bakery Demo Test"
```

To see the Metrics front end, go to your ports tab and follow the forwarding address for port 9143.

Make sure the `api` port (9142) is public. If it's private, the metrics frontend will not be able to access the API due to CORS issues.

If you are experiencing problems, see the file [.devcontainer/troubleshooting.md](./troubleshooting.md) for some common troubleshooting tips.
27 changes: 27 additions & 0 deletions .devcontainer/troubleshooting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Troubleshooting

## Frontend can't be open

Make sure the ports 9142 (`api`) und 9143 (`metrics page`) are public. If they are private, the metrics frontend will not be able to access the API due to CORS issues. After a restart of the codespace the ports are set to private, so you have to change the visibility manually.

You can use the following commands in the terminal to make the ports public:

```sh
gh codespace ports visibility 9142:public -c $CODESPACE_NAME
gh codespace ports visibility 9143:public -c $CODESPACE_NAME
```

## Connection to server failed

If you entcounter an error like

```log
error connecting in 'pool-1': connection failed: connection to server at "127.0.0.1", port 9573 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
```

then ensure that the Docker containers of GMT are running.

```sh
docker compose -f docker/compose.yml up -d
```
15 changes: 13 additions & 2 deletions .github/workflows/build-codespace-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
gmt-api-token: ${{ secrets.GMT_API_TOKEN }}
electricitymaps-api-token: ${{ secrets.ELECTRICITYMAPS_TOKEN }}

- name: Checkout repository
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

Expand All @@ -31,13 +34,21 @@ jobs:
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ inputs.github-token }}
password: ${{ secrets.GITHUB_TOKEN }}

# - name: Build and push gunicorn container
# uses: docker/build-push-action@v5
# with:
# context: ./docker
# file: ./docker/Dockerfile-gunicorn
# push: true
# tags: ghcr.io/green-coding-berlin/green-coding-gunicorn-container:latest

- name: Build and push Docker container
uses: docker/build-push-action@v6
with:
context: .
file: /.devcontainer/containerized/Dockerfile # Path to the Dockerfile
file: ./.devcontainer/containerized/Dockerfile
push: true
tags: ghcr.io/green-coding-solutions/codespace-container:latest

Expand Down
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ metric-provider-binary
.vscode
static-binary
.pytest_cache
/docker/test-compose.yml
/tests/structure.sql
/tools/sgx_enable
/venv/
/lib/hardware_info_root.py
/tools/cluster/cleanup.sh
/node_modules/
/lib/c/parse_int.o
/tools/backup
/manager-config.yml
/manager-config.yml
/venv/
Makefile.bak
/docker/test-compose.yml
/tests/structure.sql
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

[![Energy Used](https://api.green-coding.io/v1/ci/badge/get/?repo=green-coding-solutions/green-metrics-tool&branch=main&workflow=45267393)](https://metrics.green-coding.io/ci.html?repo=green-coding-solutions/green-metrics-tool&branch=main&workflow=45267393) (This is the energy cost of running our CI-Pipelines on Github. [Find out more about Eco-CI](https://www.green-coding.io/projects/eco-ci/))

[![Try in Github Codespaces!](https://github.com/codespaces/badge.svg)](https://codespaces.new/green-coding-berlin/green-metrics-tool)

# Introduction

The Green Metrics Tool is a developer tool indented for measuring the energy and CO2 consumption of software through a software life cycle analysis (SLCA).
Expand Down
2 changes: 1 addition & 1 deletion config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ machine:
measurement:
system_check_threshold: 3 # Can be 1=INFO, 2=WARN or 3=ERROR
pre-test-sleep: 5
idle-duration: 5
idle-duration: 10
baseline-duration: 5
post-test-sleep: 5
phase-transition-time: 1
Expand Down
3 changes: 2 additions & 1 deletion docker/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ events {


http {
server_names_hash_bucket_size 128;
include /etc/nginx/mime.types;
default_type application/octet-stream;

Expand All @@ -34,4 +35,4 @@ http {
gzip_disable "MSIE [1-6]\.";

include /etc/nginx/conf.d/*.conf;
}
}
12 changes: 12 additions & 0 deletions frontend/js/helpers/config.js.example
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ METRIC_MAPPINGS = {
'explanation': 'Container energy estimated via CPU-% share',
},

'psu_energy_cgroup_slice': {
'clean_name': 'Container Energy (+Idle)',
'source': 'estimation',
'explanation': 'Container energy estimated via CPU-% share (incl. idle)',
},

'psu_power_cgroup_container': {
'clean_name': 'Container Power',
'source': 'estimation',
'explanation': 'Container power estimated via CPU-% share',
},

'psu_power_cgroup_slice': {
'clean_name': 'Container Power (+Idle)',
'source': 'estimation',
'explanation': 'Container power estimated via CPU-% share incl. Idle',
},

'psu_carbon_dc_rapl_msr_machine': {
'clean_name': 'Machine CO₂',
'source': 'RAPL',
Expand Down
3 changes: 0 additions & 3 deletions install_linux.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/bash
set -euo pipefail


if [[ $(uname) != "Linux" ]]; then
echo "Error: This script can only be run on Linux."
exit 1
Expand Down Expand Up @@ -88,5 +87,3 @@ if ! mount | grep -E '\s/tmp\s' | grep -Eq '\stmpfs\s' && [[ $ask_tmpfs == true
fi

finalize


4 changes: 0 additions & 4 deletions install_mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,4 @@ echo "ALL ALL=(ALL) NOPASSWD:/usr/bin/powermetrics" | sudo tee /etc/sudoers.d/gr
echo "ALL ALL=(ALL) NOPASSWD:/usr/bin/killall powermetrics" | sudo tee /etc/sudoers.d/green_coding_kill_powermetrics
echo "ALL ALL=(ALL) NOPASSWD:/usr/bin/killall -9 powermetrics" | sudo tee /etc/sudoers.d/green_coding_kill_powermetrics_sigkill





finalize
5 changes: 5 additions & 0 deletions lib/c/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
CFLAGS = -O3 -Wall

all: parse_int.o detect_cgroup_path.o

parse_int.o: parse_int.c
gcc -c $< $(CFLAGS) -o $@

detect_cgroup_path.o: detect_cgroup_path.c
gcc -c $< $(CFLAGS) -o $@
64 changes: 64 additions & 0 deletions lib/c/detect_cgroup_path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "detect_cgroup_path.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>

char* detect_cgroup_path(const char* controller, int user_id, const char* id) {
char* path = malloc(PATH_MAX);
if (path == NULL) {
fprintf(stderr, "Could not allocate memory for detect_cgroup_path\n");
exit(1);
}

FILE* fd = NULL;

// Try cgroups v2 with systemd slices (typically in rootless mode)
snprintf(path, PATH_MAX,
"/sys/fs/cgroup/user.slice/user-%d.slice/user@%d.service/user.slice/docker-%s.scope/%s",
user_id, user_id, id, controller);
fd = fopen(path, "r");
if (fd != NULL) {
fclose(fd);
return path;
}

// Try cgroups v2 with systemd but non-slice mountpoints (typically in non-rootless mode)
snprintf(path, PATH_MAX,
"/sys/fs/cgroup/system.slice/docker-%s.scope/%s",
id, controller);
fd = fopen(path, "r");
if (fd != NULL) {
fclose(fd);
return path;
}

// Try cgroups v2 without slice mountpoints (used in Github codespaces)
snprintf(path, PATH_MAX,
"/sys/fs/cgroup/docker/%s/%s",
id, controller);
fd = fopen(path, "r");
if (fd != NULL) {
fclose(fd);
return path;
}

// Try cgroups v2 without slice mountpoints and in subdir (used in Github actions)
snprintf(path, PATH_MAX,
"/sys/fs/cgroup/actions_job/%s/%s",
id, controller);
fd = fopen(path, "r");
if (fd != NULL) {
fclose(fd);
return path;
}

// If no valid path is found, free the allocated memory and error
free(path);
fprintf(stderr, "Error - Could not open container for reading: %s. Maybe the container is not running anymore? Errno: %d\n", id, errno);
exit(1);

}

6 changes: 6 additions & 0 deletions lib/c/detect_cgroup_path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef DETECT_CGROUP_PATH_H
#define DETECT_CGROUP_PATH_H

char* detect_cgroup_path(const char* controller, int user_id, const char* id);

#endif // DETECT_CGROUP_PATH_H
4 changes: 3 additions & 1 deletion lib/install_shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ function build_binaries() {
if [[ $(uname) == "Linux" ]] && [[ "$make_path" == *"/mach/"* ]]; then
continue
fi
if [[ "$make_path" == *"/lmsensors/"* ]] && [[ "${install_sensors}" == false ]]; then
continue
fi
echo "Installing $subdir/metric-provider-binary ..."
rm -f $subdir/metric-provider-binary 2> /dev/null
make -C $subdir
Expand Down Expand Up @@ -290,7 +293,6 @@ while getopts "p:a:m:nhtbisyrlc:k:" o; do
;;
s)
install_sensors=false
# currently unused
;;
r)
install_msr_tools=false
Expand Down
Loading

0 comments on commit ac5d708

Please sign in to comment.