Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
try:
import subprocess

from sonic_platform.bfn_extensions.platform_sensors import platform_sensors_get
from collections import namedtuple
from bfn_extensions.platform_sensors import platform_sensors_get
from sonic_platform_base.thermal_base import ThermalBase
except ImportError as e:
raise ImportError (str(e) + "- required module not found")
Expand All @@ -18,6 +18,9 @@
temp2_input: 37.000
...
'''

Threshold = namedtuple('Threshold', ['crit', 'max', 'min', 'alarm'], defaults=[0.1]*4)

def _sensors_chip_parsed(data: str):
def kv(line):
k, v, *_ = [t.strip(': ') for t in line.split(':') if t] + ['']
Expand Down Expand Up @@ -68,27 +71,68 @@ def _value_get(d: dict, key_prefix, key_suffix=''):

# Thermal -> ThermalBase -> DeviceBase
class Thermal(ThermalBase):
def __init__(self, chip, label):
_thresholds = {
"com_e_driver-i2c-4-33:memory-temp": Threshold(85.0, 80.75, 0.2, 0.1),
"com_e_driver-i2c-4-33:cpu-temp": Threshold(99.9, 99.75, 0.2, 0.1),
"psu_driver-i2c-7-5a:psu1-temp1": Threshold(50.0, 47.5, 0.2, 0.1),
"psu_driver-i2c-7-5a:psu1-temp2": Threshold(90.0, 85.5, 0.2, 0.1),
"psu_driver-i2c-7-5a:psu1-temp3": Threshold(50.0, 47.5, 0.2, 0.1),
"tmp75-i2c-3-48:chip-temp": Threshold(90.0, 85.5, 0.2, 0.1),
"tmp75-i2c-3-49:exhaust2-temp": Threshold(80.0, 76.0, 0.2, 0.1),
"tmp75-i2c-3-4a:exhaust-temp": Threshold(60.0, 57.0, 0.2, 0.1),
"tmp75-i2c-3-4b:intake-temp": Threshold(60.0, 57.0, 0.2, 0.1),
"tmp75-i2c-3-4c:tofino-temp": Threshold(99.9, 99.75, 0.2, 0.1),
"tmp75-i2c-3-4d:intake2-temp": Threshold(60.0, 57.0, 0.2, 0.1),
"coretemp-isa-0000:package-id-0": Threshold(80.0, 76.0, 0.2, 0.1),
"coretemp-isa-0000:core-0": Threshold(99.9, 82.0, 0.2, 0.1),
"coretemp-isa-0000:core-1": Threshold(99.9, 82.0, 0.2, 0.1),
"coretemp-isa-0000:core-2": Threshold(99.9, 82.0, 0.2, 0.1),
"coretemp-isa-0000:core-3": Threshold(99.9, 82.0, 0.2, 0.1),
}

def __init__(self, chip, label, index = 0):
self.__chip = chip
self.__label = label
self.__name = f"{chip}:{label}".lower().replace(' ', '-')
self.__collect_temp = []
self.__index = index

def __get(self, attr_prefix, attr_suffix):
sensor_data = _sensors_get().get(self.__chip, {}).get(self.__label, {})
value = _value_get(sensor_data, attr_prefix, attr_suffix)
if value is not None: return value
raise NotImplementedError
if value is not None:
return value
elif attr_prefix == 'temp':
if attr_suffix == 'crit':
return self._thresholds[self.__name].crit
elif attr_suffix == 'max':
return self._thresholds[self.__name].max
elif attr_suffix == 'min':
return self._thresholds[self.__name].min
elif attr_suffix == 'alarm':
return self._thresholds[self.__name].alarm
else:
return 1.0

# ThermalBase interface methods:
def get_temperature(self) -> float:
return float(self.__get('temp', 'input'))
temp = self.__get('temp', 'input')
self.__collect_temp.append(float(temp))
self.__collect_temp.sort()
return float(temp)

def get_high_threshold(self) -> float:
return float(self.__get('temp', 'max'))

def get_high_critical_threshold(self) -> float:
return float(self.__get('temp', 'crit'))

def get_low_critical_threshold(self) -> float:
return float(self.__get('temp', 'alarm'))

def get_model(self):
return f"{self.__label}".lower()

# DeviceBase interface methods:
def get_name(self):
return self.__name
Expand All @@ -99,11 +143,41 @@ def get_presence(self):
def get_status(self):
return True

def is_replaceable(self):
return False

def get_low_threshold(self) -> float:
return float(self.__get('temp', 'min'))

def get_serial(self):
return 'N/A'

def get_minimum_recorded(self) -> float:
temp = self.__collect_temp[0] if len(self.__collect_temp) > 0 else 0.1
temp = temp if temp > 0.0 else 0.1
return float(temp)

def get_maximum_recorded(self) -> float:
temp = self.__collect_temp[-1] if len(self.__collect_temp) > 0 else 100.0
temp = temp if temp <= 100.0 else 100.0
return float(temp)

def get_position_in_parent(self):
return self.__index

def set_high_threshold(self, temperature):
return False

def set_low_threshold(self, temperature):
return False

def thermal_list_get():
l = []
index = 0
for chip, chip_data in _sensors_get().items():
for sensor, sensor_data in chip_data.items():
# add only temperature sensors
if _value_get(sensor_data, "temp") is not None:
l.append(Thermal(chip, sensor))
l.append(Thermal(chip, sensor, index))
index += 1
return l
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
try:
from threading import Timer
except ImportError as e:
raise ImportError (str(e) + "- required module not found")

class ThermalManager():
def __init__(self, polling_time = 30.0):
self.__polling_thermal_time = polling_time
self.__thermals = None
self.__timer = None
self.__chassis = None
self.__running = False

def start(self):
if self.__running == True:
self.work()
self.__timer = Timer(self.__polling_thermal_time, self.start)
self.__timer.start()

def work(self):
if self.__chassis is not None:
self.__thermals = self.__chassis._thermal_list
for term in self.__thermals:
self.check(term)

def check(self, sensor):
temperature = sensor.get_temperature()
if temperature is not None:
temp_high = sensor.get_high_threshold()
temp_low = sensor.get_low_threshold()
if temperature > temp_high:
print('Sensor ', sensor.get_name(), ' temperature more then', temp_high, '!!!')
if temperature < temp_low:
print('Sensor ', sensor.get_name(), ' temperature less then', temp_low, '!!!')

def stop(self):
if self.__timer is not None:
self.__running = False
self.__timer.cancel()

def __del__(self):
self.stop()

# for compatibility with old version
def run_policy(self, chassis_def):
self.__chassis = chassis_def

def get_interval(self):
return self.__polling_thermal_time

def initialize(self):
pass

def load(self, json_file):
pass

def init_thermal_algorithm(self, chassis_def):
self.__chassis = chassis_def
self.__running = True
self.start()

def deinitialize(self):
self.stop()