mlco2 / codecarbon

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

Intel Power Gadget Alternative for Apple Silicon Chips #464

Closed LuisBlanche closed 5 months ago

LuisBlanche commented 8 months ago

Description

Test intel power gadget is not passing on Apple M1/M2 (Silicon) Processors. It is not possible to install Intel Power Gagdet there (moreover it will be deprecated soon cf #457)

What I Did

I found an alternative to check power consumption on Apple Silicon here : MX Power Gadget. I tested the app on my mac and it works well.

Suggestion

I can easily detect wether the processor is from Apple :

print(subprocess.check_output(["sysctl", '-n', 'machdep.cpu.brand_string']).decode('utf-8'))

>>> 'Apple M1\n'

Now all I need to find out is how to retrieve power consumption information

Two options :

  1. Use powermetrics, a native macos utility
  2. Use asitop built on top of powermetrics that is implemented in python

I will start exploring option 2

LuisBlanche commented 8 months ago

I have spend quite some time, it seems that we can get all the info we need using powermetrics but the problem is that powermetric needs sudo rights, and asitop is built on top of powermetrics, so same problem.

There is no command line associated to MX Power Gadget so no way to use it the same way as Intel Power Gadget.

I have read here that there is actually no known alternatives : https://www.green-coding.berlin/blog/power-measurement-on-macos/

LuisBlanche commented 8 months ago

Only solution I have for now is : sudo visudo and add

username ALL = (root) NOPASSWD: /usr/bin/powermetrics

which allows username to use powermetrics without prompting password

Which seems to be raising security issues

inimaz commented 8 months ago

Hey @LuisBlanche thanks for this! Indeed there is no way right now to measure Apple M1/M2 performances. Feel free to give it a try! About the need for sudo, we have a similar issue with Linux and RAPL (https://github.com/mlco2/codecarbon/issues/244).

For the case of M1/M2 and powermetrics, maybe there is a way to know specifically which classes/files need sudo permissions and give only to those?

LuisBlanche commented 8 months ago

@inimaz powermetrics is not open source so there is no way to access a lower level of permissions. The only way I found for now is giving it sudo permission. I've spent some time looking for other solutions but it seems that there is no other solution that does not necessitate administrative rights

benoit-cty commented 8 months ago

It may be the same security reason but it's a shame that in 2023 we need to be root to get hardware consumption... Low level API could send aggregated energy data to avoid security risk.

SudhanshuBlaze commented 2 months ago

Hi

I am on MacBook m2 pro 16'', my goal is to store the codecarbon logs in emission.csv and then use it with Dash App to have a dashboard regarding carbon emissions. But in my case codecarbon is outputting the logs in a file called powermetrics_log.txt, how can I visualize it on a dashboard (private)?

I'm following this portion of the documentation: codecarbon/visualize.html

Specs of my MacBook:

  Model Name:   MacBook Pro
  Model Identifier: Mac14,10
  Chip: Apple M2 Pro
  Total Number of Cores:    12 (8 performance and 4 efficiency)
  Memory:   16 GB