mlco2 / codecarbon

Track emissions from Compute and recommend ways to reduce their impact on the environment.
https://mlco2.github.io/codecarbon
MIT License
1.18k stars 177 forks source link

Get CPU infos on docker container #533

Closed TECO-Octo closed 4 months ago

TECO-Octo commented 7 months ago

Description

I wanted to track the emissions of a scope of code inside a docker container. Though it is not able to find and track the CPU, thus it is falling back on constant mode.

What I Did

I used codecarbon task manager in a small portion of code (in my case, during the inference of a model).

Here are the logs from my docker container

2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32] [setup] RAM Tracking...
2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32] [setup] GPU Tracking...
2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32] No GPU found.
2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32] [setup] CPU Tracking...
2024-04-22 16:57:32 [codecarbon WARNING @ 14:57:32] No CPU tracking mode found. Falling back on CPU constant mode.
2024-04-22 16:57:32 [codecarbon WARNING @ 14:57:32] We were unable to detect your CPU using the `cpuinfo` package. Resorting to a default power consumption of 85W.
2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32] CPU Model on constant consumption mode: Unknown
2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32] >>> Tracker's metadata:
2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32]   Platform system: Linux-6.6.16-linuxkit-aarch64-with-glibc2.17
2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32]   Python version: 3.8.16
2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32]   CodeCarbon version: 2.3.4
2024-04-22 16:57:32 [codecarbon INFO @ 14:57:32]   Available RAM : 7.657 GB

I wonder if there is a way to allow CPU infos access on a docker container to avoid the falling back mode in codecarbon.

benoit-cty commented 7 months ago

Hello, Yes, you could run the container in privileged mode to allow it to read /sys to use the RAPL files of the hosts.

But do this only on container you trust !

TECO-Octo commented 7 months ago

Hello and thank you for your suggestion!

This is what I did inside the container I trust (edge_orchestrator):

Capture d’écran 2024-04-26 à 07 46 44

Sadly, it is still unable to find the CPU infos

benoit-cty commented 7 months ago

Can you try with:

    volumes:
      - type: bind
        source: /proc
        target: /proc
      - type: bind
        source: /sys/class/powercap
        target: /sys/class/powercap

I see this on https://github.com/hubblo-org/scaphandre/blob/e09dfcc7abd28291e783643e366393325fad3f6d/docker-compose/docker-compose.yaml#L26 from https://hubblo-org.github.io/scaphandre-documentation/tutorials/getting_started.html

TECO-Octo commented 7 months ago

Hello!

/sys/class/powercap does not exist for me as I run the docker from a MacOS device (Apple Silicon M1 chip). Hence I followed the methodology from this link for Apple Silicon Chips

I modified the docker-compose as such

    privileged: true
    volumes:
      - type: bind
        source: /proc
        target: /proc
      - type: bind
        source: /usr/bin/powermetrics
        target: /usr/bin/powermetrics

But still encounter the same issue (switching to falling back mode)

benoit-cty commented 7 months ago

Ok, so I don't think it's possible because for Powermetrics we don't just read a file but executing a Mac application so it probably can't work in Docker.

@LuisBlanche any idea ?

LuisBlanche commented 7 months ago

Hi, yes I think it might be complicated indeed. I'll have a better look at it when I can, but it's been a common issue with people trying to use codecarbon in Docker. One suggestion I could give is to run codecarbon monitor outside docker right before running it, but I don't know if it fits your use case

LuisBlanche commented 7 months ago

Maybe using this doc about docker runtime metrics we could try to do something but i will only give us CPU usage % and not task level consumption

TECO-Octo commented 7 months ago

Thanks for your answers! Yes, sadly it seems complicate as for now. I will keep in touch with you guys if you come up with something else!

benoit-cty commented 4 months ago

I'm closing this PR, don't hesitate to re-open if you have more information.