-
Notifications
You must be signed in to change notification settings - Fork 216
Support thermal policy management including policy file loading, information collecting, condition matching and action executing policy #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
314d29d
DeviceBase.get_name should raise NotImplementedError like other membe…
Junchao-Mellanox 265a8d9
Add thermal policy control
Junchao-Mellanox c3b9a73
add thermal information to thermal manager
Junchao-Mellanox 3314a12
Fix issues found in manual test
Junchao-Mellanox 1825ea9
1.move default thermal policy impl here due to dependency reason; 2.a…
Junchao-Mellanox 49ebc6a
adjust gitignore file to ignore pyc and pycache
Junchao-Mellanox 6fd9634
move default thermal policy to platform API and remove useless unit test
Junchao-Mellanox a9bfb29
fix issues found by unit test
Junchao-Mellanox 56e746f
rename some functions to make it more readable according to Kebo comm…
Junchao-Mellanox 5430513
1. add a common base class for info, action and condition; 2. raise E…
Junchao-Mellanox 2db8215
add get_high_critical_threshold and get_low_critical_threshold to the…
Junchao-Mellanox fc80aa1
fix typo when load thermal algorithm JSON config
Junchao-Mellanox b8e2ab7
Fix review comment by Joe
Junchao-Mellanox 52ee784
Merge branch 'master' into thermal-policy
Junchao-Mellanox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| build/ | ||
| *.pyc | ||
| */__pycache__/ | ||
| build/ | ||
| sonic_platform_common.egg-info/ | ||
| .cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
17 changes: 17 additions & 0 deletions
17
sonic_platform_base/sonic_thermal_control/thermal_action_base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 would be executed. | ||
jleveque marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| 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 | ||
|
|
||
14 changes: 14 additions & 0 deletions
14
sonic_platform_base/sonic_thermal_control/thermal_condition_base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
15
sonic_platform_base/sonic_thermal_control/thermal_info_base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
63
sonic_platform_base/sonic_thermal_control/thermal_json_object.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
202
sonic_platform_base/sonic_thermal_control/thermal_manager_base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 policy.json 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 policy.json. | ||
| :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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.