Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update emissions_tracker.py to fix issue #549 #570

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions codecarbon/emissions_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def __init__(
set_logger_format(self._logger_preamble)

self._start_time: Optional[float] = None
self._last_measured_time: float = time.time()
self._last_measured_time: float = time.perf_counter()
self._total_energy: Energy = Energy.from_energy(kWh=0)
self._total_cpu_energy: Energy = Energy.from_energy(kWh=0)
self._total_gpu_energy: Energy = Energy.from_energy(kWh=0)
Expand Down Expand Up @@ -453,7 +453,7 @@ def start(self) -> None:
logger.warning("Already started tracking")
return

self._last_measured_time = self._start_time = time.time()
self._last_measured_time = self._start_time = time.perf_counter()
# Read initial energy for hardware
for hardware in self._hardware:
hardware.start()
Expand All @@ -477,7 +477,9 @@ def start_task(self, task_name=None) -> None:
task_name = uuid.uuid4().__str__()
if task_name in self._tasks.keys():
task_name += "_" + uuid.uuid4().__str__()
self._last_measured_time = self._start_time = time.time()

self._last_measured_time = self._start_time = time.perf_counter()

# Read initial energy for hardware
for hardware in self._hardware:
hardware.start()
Expand Down Expand Up @@ -506,7 +508,7 @@ def stop_task(self, task_name: str = None) -> float:
emissions_data_delta = self._compute_emissions_delta(emissions_data)

task_duration = Time.from_seconds(
time.time() - self._tasks[task_name].start_time
time.perf_counter() - self._tasks[task_name].start_time
)

task_emission_data = emissions_data_delta
Expand Down Expand Up @@ -596,7 +598,7 @@ def _prepare_emissions_data(self) -> EmissionsData:
:delta: If 'True', return only the delta comsumption since the last call.
"""
cloud: CloudMetadata = self._get_cloud_metadata()
duration: Time = Time.from_seconds(time.time() - self._start_time)
duration: Time = Time.from_seconds(time.perf_counter() - self._start_time)

if cloud.is_on_private_infra:
emissions = self._emissions.get_private_infra_emissions(
Expand Down Expand Up @@ -682,9 +684,9 @@ def _get_cloud_metadata(self) -> CloudMetadata:

def _do_measurements(self) -> None:
for hardware in self._hardware:
h_time = time.time()
h_time = time.perf_counter()
# Compute last_duration again for more accuracy
last_duration = time.time() - self._last_measured_time
last_duration = time.perf_counter() - self._last_measured_time
(
power,
energy,
Expand Down Expand Up @@ -730,7 +732,7 @@ def _do_measurements(self) -> None:
)
else:
logger.error(f"Unknown hardware type: {hardware} ({type(hardware)})")
h_time = time.time() - h_time
h_time = time.perf_counter() - h_time
logger.debug(
f"{hardware.__class__.__name__} : {hardware.total_power().W:,.2f} "
+ f"W during {last_duration:,.2f} s [measurement time: {h_time:,.4f}]"
Expand All @@ -745,7 +747,7 @@ def _measure_power_and_energy(self) -> None:
every `self._measure_power_secs` seconds.
:return: None
"""
last_duration = time.time() - self._last_measured_time
last_duration = time.perf_counter() - self._last_measured_time

warning_duration = self._measure_power_secs * 3
if last_duration > warning_duration:
Expand All @@ -756,7 +758,7 @@ def _measure_power_and_energy(self) -> None:
logger.warning(warn_msg, last_duration)

self._do_measurements()
self._last_measured_time = time.time()
self._last_measured_time = time.perf_counter()
self._measure_occurrence += 1
# Special case: metrics and api calls are sent every `api_call_interval` measures
if (
Expand Down
Loading