def __init__(self, output_dir: str, mode: str): self._output_dir = output_dir self._mode = mode if self._mode == "intel_power_gadget": self._intel_interface = IntelPowerGadget(self._output_dir) elif self._mode == "intel_rapl": self._intel_interface = IntelRAPL()
class CPU(BaseHardware): def __init__(self, output_dir: str, mode: str): self._output_dir = output_dir self._mode = mode if self._mode == "intel_power_gadget": self._intel_interface = IntelPowerGadget(self._output_dir) elif self._mode == "intel_rapl": self._intel_interface = IntelRAPL() def _get_power_from_cpus(self) -> Power: """ Get CPU power from Intel Power Gadget :return: power in kW """ all_cpu_details: Dict = self._intel_interface.get_cpu_details() power = 0 for metric, value in all_cpu_details.items(): if re.match("^Processor Power_\d+\(Watt\)$", metric): power += value return Power.from_watts(power) def total_power(self) -> Power: cpu_power = self._get_power_from_cpus() logger.info(f"CODECARBON CPU Power Consumption : {cpu_power}") return cpu_power @classmethod def from_utils(cls, output_dir: str, mode: str) -> "CPU": return cls(output_dir=output_dir, mode=mode)
def __init__( self, output_dir: str, mode: str, model: str, tdp: int, rapl_dir: str = "/sys/class/powercap/intel-rapl", ): self._output_dir = output_dir self._mode = mode self._model = model self._tdp = tdp self._is_generic_tdp = False if self._mode == "intel_power_gadget": self._intel_interface = IntelPowerGadget(self._output_dir) elif self._mode == "intel_rapl": self._intel_interface = IntelRAPL(rapl_dir=rapl_dir)
def test_intel_rapl(self): expected_cpu_details = {"Processor Power_0(Watt)": 0.0, "psys": 0.0} rapl = IntelRAPL(rapl_dir=os.path.join(os.path.dirname(__file__), "test_data", "rapl")) self.assertDictEqual(expected_cpu_details, rapl.get_cpu_details())
class CPU(BaseHardware): def __init__( self, output_dir: str, mode: str, model: str, tdp: int, rapl_dir: str = "/sys/class/powercap/intel-rapl", ): self._output_dir = output_dir self._mode = mode self._model = model self._tdp = tdp self._is_generic_tdp = False if self._mode == "intel_power_gadget": self._intel_interface = IntelPowerGadget(self._output_dir) elif self._mode == "intel_rapl": self._intel_interface = IntelRAPL(rapl_dir=rapl_dir) def __repr__(self) -> str: if self._mode != "constant": return "CPU({})".format(" ".join( map(str.capitalize, self._mode.split("_")))) s = "CPU({} > {}W".format(self._model, self._tdp) if self._is_generic_tdp: s += " [generic]" return s + ")" def _get_power_from_cpus(self) -> Power: """ Get CPU power :return: power in kW """ if self._mode == "constant": power = self._tdp * CONSUMPTION_PERCENTAGE_CONSTANT return Power.from_watts(power) elif self._mode == "intel_rapl": # Don't call get_cpu_details to avoid computing energy twice and loosing data. all_cpu_details: Dict = self._intel_interface.get_static_cpu_details( ) else: all_cpu_details: Dict = self._intel_interface.get_cpu_details() power = 0 for metric, value in all_cpu_details.items(): # "^Processor Power_\d+\(Watt\)$" for Inter Power Gadget if re.match(r"^Processor Power", metric): power += value logger.debug( f"_get_power_from_cpus - MATCH {metric} : {value}") else: logger.debug( f"_get_power_from_cpus - DONT MATCH {metric} : {value}") return Power.from_watts(power) def _get_energy_from_cpus(self, delay: Time) -> Energy: """ Get CPU energy deltas from RAPL files :return: energy in kWh """ all_cpu_details: Dict = self._intel_interface.get_cpu_details(delay) energy = 0 for metric, value in all_cpu_details.items(): if re.match(r"^Processor Energy Delta_\d", metric): energy += value # logger.debug(f"_get_energy_from_cpus - MATCH {metric} : {value}") return Energy.from_energy(energy) def total_power(self) -> Power: cpu_power = self._get_power_from_cpus() return cpu_power def measure_power_and_energy(self, last_duration: float) -> Tuple[Power, Energy]: if self._mode == "intel_rapl": energy = self._get_energy_from_cpus(delay=Time( seconds=last_duration)) power = self.total_power() return power, energy # If not intel_rapl return super().measure_power_and_energy(last_duration=last_duration) def start(self): if self._mode in ["intel_power_gadget", "intel_rapl"]: self._intel_interface.start() pass def get_model(self): return self._model @classmethod def from_utils( cls, output_dir: str, mode: str, model: Optional[str] = None, tdp: Optional[int] = None, ) -> "CPU": if model is None: model = detect_cpu_model() if model is None: logger.warning("Could not read CPU model.") if tdp is None: tdp = POWER_CONSTANT cpu = cls(output_dir=output_dir, mode=mode, model=model, tdp=tdp) cpu._is_generic_tdp = True return cpu return cls(output_dir=output_dir, mode=mode, model=model, tdp=tdp)