From e0fe2f72a88f1e4f056e30387b17d0aedd3b0825 Mon Sep 17 00:00:00 2001 From: Wonbin Jin Date: Mon, 12 Aug 2024 00:37:33 +0900 Subject: [PATCH] Fix lint warnings --- tests/device/cpu/test_rapl.py | 34 ++++++++++++++++++++++++---------- zeus/device/cpu/rapl.py | 10 +++------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/tests/device/cpu/test_rapl.py b/tests/device/cpu/test_rapl.py index e867ac27..3d2e03b8 100644 --- a/tests/device/cpu/test_rapl.py +++ b/tests/device/cpu/test_rapl.py @@ -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) @@ -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 diff --git a/zeus/device/cpu/rapl.py b/zeus/device/cpu/rapl.py index 6a60254e..1bf8f343 100644 --- a/zeus/device/cpu/rapl.py +++ b/zeus/device/cpu/rapl.py @@ -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 @@ -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( @@ -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: @@ -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: