Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/config_cmd/interface.py: 89%
47 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-14 17:42 +0800
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-14 17:42 +0800
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# ----------------------------------------------------------------------------
4# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# ----------------------------------------------------------------------------
19import ctypes
20import sys
21import re
23from common import log_info, log_error, FileOperate
24from common.const import ConfigOptionName, ALL_SUPPORTED_CHIP_TYPE, ConfigOperateType
25from view import generate_report
28def _check_supported_chips(device_obj, device_id, operate):
29 options = FileOperate().read_config()
30 if not options:
31 return False, False, None
32 aic_supported_chips = options[ConfigOptionName.AIC_VOLTAGE.value][operate]
33 bus_supported_chips = options[ConfigOptionName.BUS_VOLTAGE.value][operate]
34 chip_info = device_obj.get_chip_info(device_id)
35 aic_res = (
36 ALL_SUPPORTED_CHIP_TYPE in aic_supported_chips or
37 any(re.search(rf"{i}", chip_info) for i in aic_supported_chips)
38 )
39 bus_res = (
40 ALL_SUPPORTED_CHIP_TYPE in aic_supported_chips or
41 any(re.search(rf"{i}", chip_info) for i in bus_supported_chips)
42 )
43 return aic_res, bus_res, chip_info
46def get_stress_detect_config(device_id, device_obj):
47 """config get stress_detect, aic & bus volt"""
48 aic_res, bus_res, _ = _check_supported_chips(device_obj, device_id, ConfigOperateType.GET.value)
49 config_data = []
50 aic_info = device_obj.get_device_aic_info(device_id)
51 if aic_info[1] != device_obj.UNSUPPORTED_KEY_WORDS[-1] and aic_res:
52 config_data.append(["AI Core Voltage (MV)", aic_info[1]])
53 bus_info = device_obj.get_device_bus_info(device_id)
54 if bus_info[0] != device_obj.UNSUPPORTED_KEY_WORDS[-1] and bus_res:
55 config_data.append(["Bus Voltage (MV)", bus_info[0]])
56 # get ai core volt & bus volt, all failed
57 if not config_data:
58 log_error(f'Configuration unsuccessfully get, on device {device_id}.')
59 return False
61 table_header = [[f"Device ID: {device_id}", "CURRENT CONFIGURATION"]]
62 table_data = {"none": config_data}
63 ret_str = generate_report(table_header, table_data)
64 sys.stdout.write(ret_str) # print screen
65 return True
68def restore_stress_detect_config(device_id, device_obj):
69 """config restore stress_detect, aic & bus volt"""
70 aic_res, bus_res, chip_info = _check_supported_chips(device_obj, device_id, ConfigOperateType.RESTORE.value)
71 if not aic_res and not bus_res:
72 log_error(f"Restore aic_voltage and bus_voltage not supported at {chip_info}")
73 return False
74 try:
75 ret_code = device_obj.ascend_ml.AmlStressRestore(ctypes.c_int32(device_id))
76 except AttributeError:
77 ret_code = None
79 if ret_code != 0:
80 log_error(f"Configuration unsuccessfully restore, on device {device_id}.")
81 return False
83 log_info(f"Configuration successfully restore, on device {device_id}.")
84 return True