Skip to content

Commit

Permalink
refactor: Use built-in collection types in type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
gfieni committed Sep 20, 2024
1 parent 53d4633 commit cc9fb3d
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/smartwatts/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import signal
import sys
from collections import OrderedDict
from typing import Dict

from powerapi import __version__ as powerapi_version
from powerapi.backend_supervisor import BackendSupervisor
Expand Down Expand Up @@ -90,7 +89,7 @@ def generate_smartwatts_parser() -> CommonCLIParsingManager:
return pm


def generate_formula_configuration(config: Dict, cpu_topology: CPUTopology, scope: SmartWattsFormulaScope) -> SmartWattsFormulaConfig:
def generate_formula_configuration(config: dict, cpu_topology: CPUTopology, scope: SmartWattsFormulaScope) -> SmartWattsFormulaConfig:
"""
Generate a SmartWatts actor configuration.
"""
Expand Down
3 changes: 1 addition & 2 deletions src/smartwatts/actor/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import logging
import re
from typing import Dict

from powerapi.formula import FormulaActor, FormulaState
from powerapi.handler import StartHandler, PoisonPillMessageHandler
Expand Down Expand Up @@ -69,7 +68,7 @@ class SmartWattsFormulaActor(FormulaActor):
This actor handle the reports for the SmartWatts formula.
"""

def __init__(self, name, pushers: Dict[str, PusherActor], config: SmartWattsFormulaConfig, level_logger=logging.WARNING, timeout=None):
def __init__(self, name, pushers: dict[str, PusherActor], config: SmartWattsFormulaConfig, level_logger=logging.WARNING, timeout=None):
super().__init__(name, pushers, level_logger, timeout)
self.state = SmartWattsFormulaState(self, pushers, self.formula_metadata, config)

Expand Down
4 changes: 1 addition & 3 deletions src/smartwatts/actor/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from typing import Dict

from powerapi.pusher import PusherActor

from .actor import SmartWattsFormulaActor
Expand All @@ -48,7 +46,7 @@ def __init__(self, actor_config: SmartWattsFormulaConfig):
"""
self.actor_config = actor_config

def __call__(self, name: str, pushers: Dict[str, PusherActor]) -> SmartWattsFormulaActor:
def __call__(self, name: str, pushers: dict[str, PusherActor]) -> SmartWattsFormulaActor:
"""
Create a new SmartWatts formula actor.
:param name: Name of the actor
Expand Down
20 changes: 10 additions & 10 deletions src/smartwatts/handler/hwpc_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import logging
from collections import OrderedDict, defaultdict
from math import ldexp, fabs
from typing import Dict, List, Any, Tuple
from typing import Any

from powerapi.handler import Handler
from powerapi.report import PowerReport, HWPCReport, FormulaReport
Expand All @@ -50,7 +50,7 @@ class HwPCReportHandler(Handler):
def __init__(self, state):
Handler.__init__(self, state)
self.layers = self._generate_frequency_layers()
self.ticks: OrderedDict[datetime.datetime, Dict[str, HWPCReport]] = OrderedDict()
self.ticks: OrderedDict[datetime.datetime, dict[str, HWPCReport]] = OrderedDict()

def _generate_frequency_layers(self) -> OrderedDict[int, FrequencyLayer]:
"""
Expand All @@ -70,7 +70,7 @@ def _get_nearest_frequency_layer(self, frequency: int) -> FrequencyLayer:
"""
return self.layers.get(max(freq for freq in self.layers.keys() if freq <= frequency))

def _compute_avg_pkg_frequency(self, system_msr: Dict[str, float]) -> int:
def _compute_avg_pkg_frequency(self, system_msr: dict[str, float]) -> int:
"""
Compute the average package frequency.
:param system_msr: MSR events group of System target
Expand All @@ -96,7 +96,7 @@ def handle(self, msg: HWPCReport) -> None:
pusher.send_data(report)
logging.debug('sent report: %s to %s', report, name)

def _process_oldest_tick(self) -> Tuple[List[PowerReport], List[FormulaReport]]:
def _process_oldest_tick(self) -> tuple[list[PowerReport], list[FormulaReport]]:
"""
Process the oldest tick stored in the stack and generate power reports for the running target(s).
:return: Power reports of the running target(s)
Expand Down Expand Up @@ -182,7 +182,7 @@ def _gen_formula_report(self, timestamp: datetime, pkg_frequency: int, layer: Fr
}
return FormulaReport(timestamp, self.state.sensor, layer.model.hash, metadata)

def _gen_power_report(self, timestamp: datetime, target: str, formula: str, power: float, ratio: float, metadata: Dict[str, Any]) -> PowerReport:
def _gen_power_report(self, timestamp: datetime, target: str, formula: str, power: float, ratio: float, metadata: dict[str, Any]) -> PowerReport:
"""
Generate a power report using the given parameters.
:param timestamp: Timestamp of the measurements
Expand All @@ -199,7 +199,7 @@ def _gen_power_report(self, timestamp: datetime, target: str, formula: str, powe
}
return PowerReport(timestamp, self.state.sensor, target, power, report_metadata)

def _gen_rapl_events_group(self, system_report) -> Dict[str, float]:
def _gen_rapl_events_group(self, system_report) -> dict[str, float]:
"""
Generate an events group with the RAPL reference event converted in Watts for the current socket.
:param system_report: The HWPC report of the System target
Expand All @@ -209,7 +209,7 @@ def _gen_rapl_events_group(self, system_report) -> Dict[str, float]:
energy = ldexp(cpu_events[self.state.config.rapl_event], -32) / (self.state.config.reports_frequency / 1000)
return {self.state.config.rapl_event: energy}

def _gen_msr_events_group(self, system_report) -> Dict[str, float]:
def _gen_msr_events_group(self, system_report) -> dict[str, float]:
"""
Generate an events group with the average of the MSR counters for the current socket.
:param system_report: The HWPC report of the System target
Expand All @@ -223,7 +223,7 @@ def _gen_msr_events_group(self, system_report) -> Dict[str, float]:
msr_events_count[event_name] += 1
return {k: (v / msr_events_count[k]) for k, v in msr_events_group.items()}

def _gen_core_events_group(self, report) -> Dict[str, float]:
def _gen_core_events_group(self, report) -> dict[str, float]:
"""
Generate an events group with Core events for the current socket.
The events value are the sum of the value for each CPU.
Expand All @@ -237,7 +237,7 @@ def _gen_core_events_group(self, report) -> Dict[str, float]:

return core_events_group

def _gen_agg_core_report_from_running_targets(self, targets_report) -> Dict[str, float]:
def _gen_agg_core_report_from_running_targets(self, targets_report) -> dict[str, float]:
"""
Generate an aggregate Core events group of the running targets for the current socket.
:param targets_report: List of Core events group of the running targets
Expand All @@ -251,7 +251,7 @@ def _gen_agg_core_report_from_running_targets(self, targets_report) -> Dict[str,
return agg_core_events_group

@staticmethod
def _extract_events_value(events: Dict[str, float]) -> List[float]:
def _extract_events_value(events: dict[str, float]) -> list[float]:
"""
Creates and return a list of events value from the events group.
:param events: Events group
Expand Down
4 changes: 1 addition & 3 deletions src/smartwatts/model/cpu_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from typing import List


class CPUTopology:
"""
Expand Down Expand Up @@ -73,7 +71,7 @@ def get_max_frequency(self) -> int:
"""
return self.freq_bclk * self.ratio_max

def get_supported_frequencies(self) -> List[int]:
def get_supported_frequencies(self) -> list[int]:
"""
Compute the supported frequencies for this CPU.
:return: A list of supported frequencies in MHz
Expand Down
4 changes: 1 addition & 3 deletions src/smartwatts/model/frequency_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from typing import List

from .sample_history import ReportHistory, ErrorHistory
from .power_model import PowerModel

Expand Down Expand Up @@ -60,7 +58,7 @@ def update_power_model(self, min_intercept: float, max_intercept: float) -> None
self.model.learn_power_model(self.samples_history, min_intercept, max_intercept)
self.error_history.clear()

def store_sample_in_history(self, power_reference: float, events_value: List[float]) -> None:
def store_sample_in_history(self, power_reference: float, events_value: list[float]) -> None:
"""
Append a sample to the history.
:param power_reference: Power reference (RAPL) of the machine
Expand Down
3 changes: 1 addition & 2 deletions src/smartwatts/model/power_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import warnings
from hashlib import sha1
from pickle import dumps
from typing import List, Optional

from sklearn.linear_model import ElasticNet

Expand Down Expand Up @@ -79,7 +78,7 @@ def learn_power_model(self, samples_history: ReportHistory, min_intercept: float
self.hash = sha1(dumps(self.clf)).hexdigest()
self.id += 1

def predict_power_consumption(self, events: List[float]) -> Optional[float]:
def predict_power_consumption(self, events: list[float]) -> float | None:
"""
Compute a power estimation from the events value using the power model.
:param events: Events value
Expand Down
3 changes: 1 addition & 2 deletions src/smartwatts/model/sample_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

from collections import deque
from statistics import median, mean
from typing import List


class ReportHistory:
Expand All @@ -55,7 +54,7 @@ def __len__(self) -> int:
"""
return len(self.events_values)

def store_report(self, power_reference: float, events_value: List[float]) -> None:
def store_report(self, power_reference: float, events_value: list[float]) -> None:
"""
Append a report to the report's history.
:param events_value: List of raw events value
Expand Down

0 comments on commit cc9fb3d

Please sign in to comment.