-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[Mellanox] Add CPU thermal control for Nvidia platforms #10202
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
70 changes: 70 additions & 0 deletions
70
platform/mellanox/mlnx-platform-api/sonic_platform/cpu_thermal_control.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,70 @@ | ||
| # | ||
| # Copyright (c) 2019-2022 NVIDIA CORPORATION & AFFILIATES. | ||
| # Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| ############################################################################# | ||
| # Mellanox | ||
| # | ||
| # Module contains an implementation of SONiC Platform Base API and | ||
| # provides the Chassis information which are available in the platform | ||
| # | ||
| ############################################################################# | ||
|
|
||
| from sonic_py_common.task_base import ThreadTaskBase | ||
|
|
||
| from . import utils | ||
| from .device_data import DeviceDataManager | ||
|
|
||
|
|
||
| class CPUThermalControl(ThreadTaskBase): | ||
| CPU_COOLING_STATE = '/var/run/hw-management/thermal/cooling2_cur_state' | ||
| CPU_TEMP_FILE = '/var/run/hw-management/thermal/cpu_pack' | ||
| MAX_COOLING_STATE = 10 | ||
| MIN_COOLING_STATE = 2 | ||
| INTERVAL = 3 | ||
|
|
||
| def __init__(self): | ||
| super(CPUThermalControl, self).__init__() | ||
| self.temp_low, self.temp_high = DeviceDataManager.get_cpu_thermal_threshold() | ||
|
|
||
| def task_worker(self): | ||
| last_temp = 0 | ||
| while not self.task_stopping_event.wait(self.INTERVAL): | ||
| last_temp = self.run(last_temp) | ||
|
|
||
| def run(self, last_temp): | ||
| current_temp = self.read_cpu_temp() | ||
| if current_temp < self.temp_low: | ||
| self.set_cooling_state(self.MIN_COOLING_STATE) | ||
| elif current_temp > self.temp_high: | ||
| self.set_cooling_state(self.MAX_COOLING_STATE) | ||
| else: | ||
| cooling_state = self.get_cooling_state() | ||
| if current_temp > last_temp: | ||
| self.set_cooling_state(min(cooling_state + 1, self.MAX_COOLING_STATE)) | ||
| elif current_temp < last_temp: | ||
| self.set_cooling_state(max(cooling_state - 1, self.MIN_COOLING_STATE)) | ||
| return current_temp | ||
|
|
||
| def set_cooling_state(self, state): | ||
| utils.write_file(self.CPU_COOLING_STATE, state, log_func=None) | ||
|
|
||
| def get_cooling_state(self): | ||
| return utils.read_int_from_file(self.CPU_COOLING_STATE, default=self.MAX_COOLING_STATE, log_func=None) | ||
|
|
||
| def read_cpu_temp(self): | ||
| cpu_temp = utils.read_int_from_file(self.CPU_TEMP_FILE, default=self.temp_high, log_func=None) | ||
| return cpu_temp if cpu_temp <= 1000 else int(cpu_temp / 1000) | ||
|
|
||
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
83 changes: 83 additions & 0 deletions
83
platform/mellanox/mlnx-platform-api/tests/test_cpu_thermal_control.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,83 @@ | ||
| # | ||
| # Copyright (c) 2019-2022 NVIDIA CORPORATION & AFFILIATES. | ||
| # Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| ############################################################################# | ||
| # Mellanox | ||
| # | ||
| # Module contains an implementation of SONiC Platform Base API and | ||
| # provides the Chassis information which are available in the platform | ||
| # | ||
| ############################################################################# | ||
|
|
||
| import glob | ||
sujinmkang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import os | ||
| import pytest | ||
| import sys | ||
| if sys.version_info.major == 3: | ||
| from unittest import mock | ||
| else: | ||
| import mock | ||
|
|
||
| test_path = os.path.dirname(os.path.abspath(__file__)) | ||
| modules_path = os.path.dirname(test_path) | ||
| sys.path.insert(0, modules_path) | ||
|
|
||
| from sonic_platform.cpu_thermal_control import CPUThermalControl | ||
|
|
||
|
|
||
| class TestCPUThermalControl: | ||
| @mock.patch('sonic_platform.device_data.DeviceDataManager.get_cpu_thermal_threshold', mock.MagicMock(return_value=(85, 95))) | ||
| @mock.patch('sonic_platform.utils.read_int_from_file') | ||
| @mock.patch('sonic_platform.utils.write_file') | ||
| def test_run(self, mock_write_file, mock_read_file): | ||
| instance = CPUThermalControl() | ||
| file_content = { | ||
| CPUThermalControl.CPU_COOLING_STATE: 5, | ||
| CPUThermalControl.CPU_TEMP_FILE: instance.temp_high + 1 | ||
| } | ||
|
|
||
| def read_file(file_path, **kwargs): | ||
| return file_content[file_path] | ||
|
|
||
| mock_read_file.side_effect = read_file | ||
| # Test current temp is higher than high threshold | ||
| instance.run(0) | ||
| mock_write_file.assert_called_with(CPUThermalControl.CPU_COOLING_STATE, CPUThermalControl.MAX_COOLING_STATE, log_func=None) | ||
|
|
||
| # Test current temp is lower than low threshold | ||
| file_content[CPUThermalControl.CPU_TEMP_FILE] = instance.temp_low - 1 | ||
| instance.run(0) | ||
| mock_write_file.assert_called_with(CPUThermalControl.CPU_COOLING_STATE, CPUThermalControl.MIN_COOLING_STATE, log_func=None) | ||
|
|
||
| # Test current temp increasing | ||
| file_content[CPUThermalControl.CPU_TEMP_FILE] = instance.temp_low | ||
| instance.run(0) | ||
| mock_write_file.assert_called_with(CPUThermalControl.CPU_COOLING_STATE, 6, log_func=None) | ||
|
|
||
| # Test current temp decreasing | ||
| instance.run(instance.temp_low + 1) | ||
| mock_write_file.assert_called_with(CPUThermalControl.CPU_COOLING_STATE, 4, log_func=None) | ||
|
|
||
| # Test current temp increasing and current cooling state is already the max | ||
| file_content[CPUThermalControl.CPU_TEMP_FILE] = 85 | ||
| file_content[CPUThermalControl.CPU_COOLING_STATE] = CPUThermalControl.MAX_COOLING_STATE | ||
| instance.run(84) | ||
| mock_write_file.assert_called_with(CPUThermalControl.CPU_COOLING_STATE, CPUThermalControl.MAX_COOLING_STATE, log_func=None) | ||
|
|
||
| # Test current temp decreasing and current cooling state is already the max | ||
| file_content[CPUThermalControl.CPU_COOLING_STATE] = CPUThermalControl.MIN_COOLING_STATE | ||
| instance.run(86) | ||
| mock_write_file.assert_called_with(CPUThermalControl.CPU_COOLING_STATE, CPUThermalControl.MIN_COOLING_STATE, log_func=None) | ||
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.