Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/drv/env_type.py: 99%

103 statements  

« prev     ^ index     » next       coverage.py v7.15.3, created at 2026-08-11 18:39 +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# ---------------------------------------------------------------------------- 

18 

19import os 

20import ctypes 

21 

22from common import log_error 

23from common import RetCode 

24from common import Singleton 

25 

26AICORE_STL_SO_SUBPATH = "tools/aml/lib64/aicore_stl" 

27AICORE_STL_SO_NAME = "libaml_aicore_stl.so" 

28 

29 

30class LoadSoType(metaclass=Singleton): 

31 def __init__(self): 

32 self.drvdsmi = None 

33 self.drvhal = None 

34 self.asendml = None 

35 self.aml_aicore_stl = None 

36 self.ascend_trace = None 

37 self.ascendcl = None 

38 self.env_type = "" 

39 

40 @staticmethod 

41 def load_dll(so_name): 

42 try: 

43 dll = ctypes.cdll.LoadLibrary(so_name) 

44 except OSError as err: 

45 log_error(f"OSError: {err}") 

46 return RetCode.FAILED 

47 return dll 

48 

49 @staticmethod 

50 def ctypes_close_library(lib): 

51 if lib and lib != RetCode.FAILED: 

52 dlclose_func = ctypes.CDLL(None).dlclose 

53 dlclose_func.argtypes = [ctypes.c_void_p] 

54 dlclose_func.restype = ctypes.c_int 

55 dlclose_func(lib._handle) 

56 

57 @staticmethod 

58 def get_aicore_stl_so_path(): 

59 # Resolve libaml_aicore_stl.so under ASCEND_HOME_PATH; return None if absent. 

60 home_path = os.getenv("ASCEND_HOME_PATH") 

61 if not home_path: 

62 log_error("ASCEND_HOME_PATH is not set.") 

63 return None 

64 so_path = os.path.realpath(os.path.join(home_path, AICORE_STL_SO_SUBPATH, 

65 AICORE_STL_SO_NAME)) 

66 if not os.path.isfile(so_path): 

67 return None 

68 return so_path 

69 

70 def get_drvdsmi_env_type(self): 

71 if self.drvdsmi is None: 

72 if self.get_env_type() == "EP": 

73 so_name = "libdrvdsmi_host.so" 

74 else: 

75 so_name = "libdrvdsmi.so" 

76 self.drvdsmi = self.load_dll(so_name) 

77 return self.drvdsmi 

78 

79 def get_drvhal_env_type(self): 

80 if self.drvhal is None: 

81 so_name = "libascend_hal.so" 

82 self.drvhal = self.load_dll(so_name) 

83 return self.drvhal 

84 

85 def get_ascend_ml(self): 

86 # libascend_ml.so is only in the toolkit run pkg. 

87 if self.asendml is None and self.get_env_type() == "EP": 

88 so_name = "libascend_ml.so" 

89 self.asendml = self.load_dll(so_name) 

90 return self.asendml 

91 

92 def get_aml_aicore_stl(self): 

93 # libaml_aicore_stl.so exports AmlAicoreStlDetect (AICore STL self-diagnose). 

94 # Only in the toolkit run pkg, EP side. 

95 if self.aml_aicore_stl is None and self.get_env_type() == "EP": 

96 so_path = self.get_aicore_stl_so_path() 

97 if so_path is None: 

98 log_error(f"{AICORE_STL_SO_NAME} not found under " 

99 f"$ASCEND_HOME_PATH/{AICORE_STL_SO_SUBPATH}, " 

100 "AICore STL detect unavailable.") 

101 return RetCode.FAILED 

102 self.aml_aicore_stl = self.load_dll(so_path) 

103 return self.aml_aicore_stl 

104 

105 def get_ascend_trace(self): 

106 if self.ascend_trace is None: 

107 so_name = "libascend_trace.so" 

108 self.ascend_trace = self.load_dll(so_name) 

109 return self.ascend_trace 

110 

111 def get_ascend_cl(self): 

112 if self.ascendcl is None: 

113 so_name = "libascendcl.so" 

114 self.ascendcl = self.load_dll(so_name) 

115 return self.ascendcl 

116 

117 def get_env_type(self): 

118 if self.env_type: 

119 return self.env_type 

120 dev = self.get_drvhal_env_type() 

121 if dev != RetCode.FAILED: 

122 dev.drvGetPlatformInfo.argtypes = [ctypes.POINTER(ctypes.c_int)] 

123 num = ctypes.c_int(-1) 

124 ret = dev.drvGetPlatformInfo(ctypes.pointer(num)) 

125 if ret == 0: 

126 if num.value == 0: 

127 self.env_type = "RC" 

128 elif num.value == 1: 

129 self.env_type = "EP" 

130 return self.env_type 

131 

132 def dll_close(self): 

133 self.ctypes_close_library(self.drvdsmi) 

134 self.ctypes_close_library(self.drvhal) 

135 self.ctypes_close_library(self.asendml) 

136 self.ctypes_close_library(self.aml_aicore_stl) 

137 self.ctypes_close_library(self.ascend_trace) 

138 self.ctypes_close_library(self.ascendcl) 

139 self.drvdsmi = None 

140 self.drvhal = None 

141 self.asendml = None 

142 self.aml_aicore_stl = None 

143 self.ascend_trace = None 

144 self.ascendcl = None