Skip to content

Commit

Permalink
Support thermal policy management including policy file loading, info…
Browse files Browse the repository at this point in the history
…rmation collecting, condition matching and action executing policy (sonic-net#73)
  • Loading branch information
Junchao-Mellanox authored Feb 27, 2020
1 parent b11ab3d commit ed50e72
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build/
*.pyc
*/__pycache__/
build/
sonic_platform_common.egg-info/
.cache
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
packages=[
'sonic_eeprom',
'sonic_led',
'sonic_fan',
'sonic_platform_base',
'sonic_platform_base.sonic_eeprom',
'sonic_platform_base.sonic_sfp',
'sonic_platform_base.sonic_ssd',
'sonic_platform_base.sonic_thermal_control',
'sonic_psu',
'sonic_fan',
'sonic_sfp',
'sonic_thermal',
],
Expand All @@ -35,5 +36,5 @@
'Programming Language :: Python :: 3.6',
'Topic :: Utilities',
],
keywords='sonic SONiC platform hardware interface api API',
keywords='sonic SONiC platform hardware interface api API'
)
8 changes: 8 additions & 0 deletions sonic_platform_base/chassis_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ def get_thermal(self, index):

return thermal

def get_thermal_manager(self):
"""
Retrieves thermal manager class on this chassis
:return: A class derived from ThermalManagerBase representing the
specified thermal manager. ThermalManagerBase is returned as default
"""
raise NotImplementedError

##############################################
# SFP methods
##############################################
Expand Down
Empty file.
17 changes: 17 additions & 0 deletions sonic_platform_base/sonic_thermal_control/thermal_action_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .thermal_json_object import ThermalJsonObject


class ThermalPolicyActionBase(ThermalJsonObject):
"""
Base class for thermal action. Once all thermal conditions in a thermal policy are matched,
all predefined thermal action will be executed.
"""
def execute(self, thermal_info_dict):
"""
Take action when thermal condition matches. For example, adjust speed of fan or shut
down the switch.
:param thermal_info_dict: A dictionary stores all thermal information.
:return:
"""
raise NotImplementedError

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .thermal_json_object import ThermalJsonObject


class ThermalPolicyConditionBase(ThermalJsonObject):
"""
Base class for thermal condition
"""
def is_match(self, thermal_info_dict):
"""
Indicate if this condition is matched.
:param thermal_info_dict: A dictionary stores all thermal information.
:return: True if condition matched else False.
"""
raise NotImplementedError
15 changes: 15 additions & 0 deletions sonic_platform_base/sonic_thermal_control/thermal_info_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from .thermal_json_object import ThermalJsonObject


class ThermalPolicyInfoBase(object):
"""
Base class for thermal information
"""
def collect(self, chassis):
"""
Collect thermal information for thermal policy.
:param chassis: The chassis object.
:return:
"""
raise NotImplementedError

63 changes: 63 additions & 0 deletions sonic_platform_base/sonic_thermal_control/thermal_json_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class ThermalJsonObject(object):
"""
Base class for thermal json object.
"""
# JSON field definition
JSON_FIELD_TYPE = 'type'

# Dictionary of ThermalJsonObject-derived class representing all thermal json types
_object_type_dict = {}

def load_from_json(self, json_obj):
"""
Initialize this object by a json object. The json object is read from policy json file.
Derived class can define any field in policy json file and interpret them in this function.
:param json_obj: A json object representing an object.
:return:
"""
pass

@classmethod
def register_concrete_type(cls, type_name, object_type):
"""
Register a concrete class by type name. The concrete class must derive from
ThermalJsonObject.
:param type_name: Name of the class.
:param object_type: A concrete class.
:return:
"""
if type_name not in cls._object_type_dict:
cls._object_type_dict[type_name] = object_type
else:
raise Exception('ThermalJsonObject type {} already exists'.format(type_name))

@classmethod
def get_type(cls, json_obj):
"""
Get a concrete class by json object. The json object represents an object and must
have a 'type' field. This function returns a pre-registered concrete class if the specific
'type' is found.
:param json_obj: A json object representing an action.
:return: A concrete class if requested type exists; Otherwise None.
"""
if ThermalJsonObject.JSON_FIELD_TYPE in json_obj:
type_str = json_obj[ThermalJsonObject.JSON_FIELD_TYPE]
if type_str in cls._object_type_dict:
return cls._object_type_dict[type_str]
else:
raise Exception('ThermalJsonObject type {} not found'.format(type_str) )

raise Exception('Invalid policy file, {} field must be presented'.format(ThermalJsonObject.JSON_FIELD_TYPE))


def thermal_json_object(type_name):
"""
Decorator to auto register a ThermalJsonObject-derived class
:param type_name: Type name of the concrete class which corresponding to the 'type' field of
a condition, action or info.
:return: Wrapper function
"""
def wrapper(object_type):
ThermalJsonObject.register_concrete_type(type_name, object_type)
return object_type
return wrapper
202 changes: 202 additions & 0 deletions sonic_platform_base/sonic_thermal_control/thermal_manager_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import json
from .thermal_policy import ThermalPolicy
from .thermal_json_object import ThermalJsonObject


class ThermalManagerBase(object):
"""
Base class of ThermalManager representing a manager to control all thermal policies.
"""
# JSON field definition.
JSON_FIELD_POLICIES = 'policies'
JSON_FIELD_INFO_TYPES = 'info_types'
JSON_FIELD_POLICY_NAME = 'name'
JSON_FIELD_THERMAL_ALGORITHM = "thermal_control_algorithm"
JSON_FIELD_FAN_SPEED_WHEN_SUSPEND = "fan_speed_when_suspend"
JSON_FIELD_RUN_AT_BOOT_UP = "run_at_boot_up"

# Dictionary of ThermalPolicy objects.
_policy_dict = {}

# Dictionary of thermal information objects. A thermal information object is used by Thermal Policy
_thermal_info_dict = {}

_fan_speed_when_suspend = None

_run_thermal_algorithm_at_boot_up = None

@classmethod
def initialize(cls):
"""
Initialize thermal manager, including register thermal condition types and thermal action types
and any other vendor specific initialization.
:return:
"""
pass

@classmethod
def deinitialize(cls):
"""
Destroy thermal manager, including any vendor specific cleanup. The default behavior of this function
is a no-op.
:return:
"""
pass

@classmethod
def start_thermal_control_algorithm(cls):
"""
Start vendor specific thermal control algorithm. The default behavior of this function is a no-op.
:return:
"""
pass

@classmethod
def stop_thermal_control_algorithm(cls):
"""
Stop vendor specific thermal control algorithm. The default behavior of this function is a no-op.
:return:
"""
pass

@classmethod
def load(cls, policy_file_name):
"""
Load all thermal policies from JSON policy file. An example looks like:
{
"thermal_control_algorithm": {
"run_at_boot_up": "false",
"fan_speed_when_suspend": "60"
},
"info_types": [
{
"type": "fan_info" # collect fan information for each iteration
},
{
"type": "psu_info" # collect psu information for each iteration
}
],
"policies": [
{
"name": "any fan absence", # if any fan absence, set all fan speed to 100% and disable thermal control algorithm
"conditions": [
{
"type": "fan.any.absence" # see sonic-platform-daemons.sonic-thermalctld.thermal_policy.thermal_conditions
}
],
"actions": [
{
"type": "fan.all.set_speed", # see sonic-platform-daemons.sonic-thermalctld.thermal_policy.thermal_actions
"speed": "100"
},
{
"type": "thermal_control.control",
"status": "false"
}
]
},
{
"name": "all fan absence", # if all fan absence, shutdown the switch
"conditions": [
{
"type": "fan.all.absence"
}
],
"actions": [
{
"type": "switch.shutdown"
}
]
}
]
}
:param policy_file_name: Path of JSON policy file.
:return:
"""
with open(policy_file_name, 'r') as policy_file:
json_obj = json.load(policy_file)
if cls.JSON_FIELD_POLICIES in json_obj:
json_policies = json_obj[cls.JSON_FIELD_POLICIES]
for json_policy in json_policies:
cls._load_policy(json_policy)

if cls.JSON_FIELD_INFO_TYPES in json_obj:
for json_info in json_obj[cls.JSON_FIELD_INFO_TYPES]:
info_type = ThermalJsonObject.get_type(json_info)
info_obj = info_type()
cls._thermal_info_dict[json_info[ThermalJsonObject.JSON_FIELD_TYPE]] = info_obj

if cls.JSON_FIELD_THERMAL_ALGORITHM in json_obj:
json_thermal_algorithm_config = json_obj[cls.JSON_FIELD_THERMAL_ALGORITHM]
if cls.JSON_FIELD_RUN_AT_BOOT_UP in json_thermal_algorithm_config:
cls._run_thermal_algorithm_at_boot_up = \
True if json_thermal_algorithm_config[cls.JSON_FIELD_RUN_AT_BOOT_UP].lower() == 'true' else False

if cls.JSON_FIELD_FAN_SPEED_WHEN_SUSPEND in json_thermal_algorithm_config:
# if the string is not a valid int, let it raise
cls._fan_speed_when_suspend = \
int(json_thermal_algorithm_config[cls.JSON_FIELD_FAN_SPEED_WHEN_SUSPEND])

@classmethod
def _load_policy(cls, json_policy):
"""
Load a policy object from a JSON object.
:param json_policy: A JSON object representing a thermal policy.
:return:
"""
if cls.JSON_FIELD_POLICY_NAME in json_policy:
name = json_policy[cls.JSON_FIELD_POLICY_NAME]
if name in cls._policy_dict:
raise Exception('Policy {} already exists'.format(name))

policy = ThermalPolicy()
policy.load_from_json(json_policy)
cls._policy_dict[name] = policy
else:
raise Exception('{} not found in policy'.format(cls.JSON_FIELD_POLICY_NAME))

@classmethod
def run_policy(cls, chassis):
"""
Collect thermal information, run each policy, if one policy matches, execute the policy's action.
:param chassis: The chassis object.
:return:
"""
if not cls._policy_dict:
return

cls._collect_thermal_information(chassis)

for policy in cls._policy_dict.values():
if policy.is_match(cls._thermal_info_dict):
policy.do_action(cls._thermal_info_dict)

@classmethod
def _collect_thermal_information(cls, chassis):
"""
Collect thermal information. This function will be called before run_policy.
:param chassis: The chassis object.
:return:
"""
for thermal_info in cls._thermal_info_dict.values():
thermal_info.collect(chassis)

@classmethod
def init_thermal_algorithm(cls, chassis):
"""
Initialize thermal algorithm according to policy file.
:param chassis: The chassis object.
:return:
"""
if cls._run_thermal_algorithm_at_boot_up is not None:
if cls._run_thermal_algorithm_at_boot_up:
cls.start_thermal_control_algorithm()
else:
cls.stop_thermal_control_algorithm()
if cls._fan_speed_when_suspend is not None:
for fan in chassis.get_all_fans():
fan.set_speed(cls._fan_speed_when_suspend)

for psu in chassis.get_all_psus():
for fan in psu.get_all_fans():
fan.set_speed(cls._fan_speed_when_suspend)
Loading

0 comments on commit ed50e72

Please sign in to comment.