mlco2 / mlco2/codecarbon

Allow MacOS sudo to be bypassed

Open
#736 0 comments 1 reaction 1 assignee View on GitHub

@LuisBlanche is already working on this.

Since Dec 14, 2024.

enhancement
Dominant language
Python
Stars
1.9k
Forks
323
Avg merge
1d 12h
Merged PRs (30d)
12

Description

  • CodeCarbon version: 2.8.1
  • Python version: 3.12.8
  • Operating System: MacOS 15.1
Description

Currently, when sudo is available on MacOS, I believe it is unavoidable that powermetrics.is_powermetrics_available() will attempt to use it, even if a default power is supplied.

Although ways to avoid the need for sudo entirely would be significantly more useful (e.g. discussion in #731), a simple short term solution could be to swap the current check:

powermetrics.is_powermetrics_available() and self._default_cpu_power is None

If instead the check against _default_cpu_power is performed first, this would enable avoiding the need to call powermetrics.is_powermetrics_available().

It may also be more efficient to swap this check for other branches of if statements, although I haven't encountered specific issues. (There's also cpu.is_rapl_available(), which doesn't check whether self._default_cpu_power is None. This seems inconsistent, but it may be intentional?)

What I Did
from codecarbon import OfflineEmissionsTracker
from codecarbon.external.hardware import POWER_CONSTANT

OfflineEmissionsTracker(
    country_iso_code="GBR",
    default_cpu_power=POWER_CONSTANT,
    log_level="DEBUG",
)

This currently prompts for a password, even when the supplied default will be used.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.