Skip to content

Commit

Permalink
Fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
wbjin committed Aug 11, 2024
1 parent 17f8e3e commit e0fe2f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
34 changes: 24 additions & 10 deletions tests/device/cpu/test_rapl.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,51 @@
)
from zeus.device.cpu.common import CpuDramMeasurement


class MockRaplFileOutOfValues(Exception):
"""Exception raised when MockRaplFile runs out of values."""

def __init__(self, message="Out of values"):
self.message = message


class MockRaplFile:
def __init__(self, file_path, values):
self.file_path = file_path
self.values = iter(values)

def read(self, *args, **kwargs):
if (value := next(self.values, None)) is not None:
return value
raise MockRaplFileOutOfValues()

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
pass


@pytest.fixture
def mock_rapl_values():
rapl_values = ["1000", "900", "800", "700", "600", "500", "400", "500", "200", "100"]
mocked_rapl_file = MockRaplFile(RAPL_DIR+"/intel-rapl:0/energy_uj", rapl_values)
rapl_values = [
"1000",
"900",
"800",
"700",
"600",
"500",
"400",
"500",
"200",
"100",
]
mocked_rapl_file = MockRaplFile(RAPL_DIR + "/intel-rapl:0/energy_uj", rapl_values)

real_open = builtins.open

def mock_file_open(filepath, *args, **kwargs):
if filepath == (RAPL_DIR+"/intel-rapl:0/energy_uj"):
if filepath == (RAPL_DIR + "/intel-rapl:0/energy_uj"):
return mocked_rapl_file
else:
return real_open(filepath, *args, **kwargs)
Expand All @@ -77,9 +91,9 @@ def mock_file_open(filepath, *args, **kwargs):
patch_open.stop()
patch_sleep.stop()


def test_rapl_polling_process(mock_rapl_values):
wraparound_counter = mp.Value('i', 0)
wraparound_counter = mp.Value("i", 0)
with pytest.raises(MockRaplFileOutOfValues) as exception:
_polling_process(RAPL_DIR+"/intel-rapl:0/energy_uj", 1000, wraparound_counter)
assert(wraparound_counter.value == 8)

_polling_process(RAPL_DIR + "/intel-rapl:0/energy_uj", 1000, wraparound_counter)
assert wraparound_counter.value == 8
10 changes: 3 additions & 7 deletions zeus/device/cpu/rapl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
import os
import warnings
from glob import glob
import typing
from typing import Sequence
from functools import lru_cache
import tempfile
import multiprocessing as mp
import time

import pandas as pd

import zeus.device.cpu.common as cpu_common
from zeus.device.cpu.common import CpuDramMeasurement
from zeus.device.exception import ZeusBaseCPUError
Expand Down Expand Up @@ -76,7 +72,7 @@ def __init__(
self.logger.info("Monitoring wrap around of %s", rapl_file_path)

context = mp.get_context("spawn")
self.wraparound_counter = context.Value('i', 0)
self.wraparound_counter = context.Value("i", 0)
# Spawn the power polling process.
atexit.register(self._stop)
self.process = context.Process(
Expand All @@ -102,7 +98,7 @@ def get_num_wraparounds(self) -> int:
def _polling_process(
rapl_file_path: str,
max_energy_uj: float,
wraparound_counter: mp.Value,
wraparound_counter,
) -> None:
"""Check for wraparounds in the specified rapl file."""
try:
Expand All @@ -117,7 +113,7 @@ def _polling_process(
sleep_time = 0.1
if energy_uj < last_energy_uj:
with wraparound_counter.get_lock():
wraparound_counter.value+=1
wraparound_counter.value += 1
last_energy_uj = energy_uj
time.sleep(sleep_time)
except KeyboardInterrupt:
Expand Down

0 comments on commit e0fe2f7

Please sign in to comment.