Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/drv/env_type.py: 97%
79 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-08-21 15:37 +0800
« prev ^ index » next coverage.py v7.14.1, created at 2026-08-21 15:37 +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
21from common import log_error
22from common import RetCode
23from common import Singleton
26class LoadSoType(metaclass=Singleton):
27 def __init__(self):
28 self.drvdsmi = None
29 self.drvhal = None
30 self.asendml = None
31 self.ascend_trace = None
32 self.ascendcl = None
33 self.env_type = ""
35 @staticmethod
36 def load_dll(so_name):
37 try:
38 dll = ctypes.cdll.LoadLibrary(so_name)
39 except OSError as err:
40 log_error(f"OSError: {err}")
41 return RetCode.FAILED
42 return dll
44 @ staticmethod
45 def ctypes_close_library(lib):
46 if lib and lib != RetCode.FAILED:
47 dlclose_func = ctypes.CDLL(None).dlclose
48 dlclose_func.argtypes = [ctypes.c_void_p]
49 dlclose_func.restype = ctypes.c_int
50 dlclose_func(lib._handle)
52 def get_drvdsmi_env_type(self):
53 if self.drvdsmi is None:
54 if self.get_env_type() == "EP":
55 so_name = "libdrvdsmi_host.so"
56 else:
57 so_name = "libdrvdsmi.so"
58 self.drvdsmi = self.load_dll(so_name)
59 return self.drvdsmi
61 def get_drvhal_env_type(self):
62 if self.drvhal is None:
63 so_name = "libascend_hal.so"
64 self.drvhal = self.load_dll(so_name)
65 return self.drvhal
67 def get_ascend_ml(self):
68 # libascend_ml.so is only in the toolkit run pkg.
69 if self.asendml is None and self.get_env_type() == "EP":
70 so_name = "libascend_ml.so"
71 self.asendml = self.load_dll(so_name)
72 return self.asendml
74 def get_ascend_trace(self):
75 if self.ascend_trace is None:
76 so_name = "libascend_trace.so"
77 self.ascend_trace = self.load_dll(so_name)
78 return self.ascend_trace
80 def get_ascend_cl(self):
81 if self.ascendcl is None:
82 so_name = "libascendcl.so"
83 self.ascendcl = self.load_dll(so_name)
84 return self.ascendcl
86 def get_env_type(self):
87 if self.env_type:
88 return self.env_type
89 dev = self.get_drvhal_env_type()
90 if dev != RetCode.FAILED:
91 dev.drvGetPlatformInfo.argtypes = [ctypes.POINTER(ctypes.c_int)]
92 num = ctypes.c_int(-1)
93 ret = dev.drvGetPlatformInfo(ctypes.pointer(num))
94 if ret == 0:
95 if num.value == 0:
96 self.env_type = "RC"
97 elif num.value == 1:
98 self.env_type = "EP"
99 return self.env_type
101 def dll_close(self):
102 self.ctypes_close_library(self.drvdsmi)
103 self.ctypes_close_library(self.drvhal)
104 self.ctypes_close_library(self.asendml)
105 self.ctypes_close_library(self.ascend_trace)
106 self.ctypes_close_library(self.ascendcl)
107 self.drvdsmi = None
108 self.drvhal = None
109 self.asendml = None
110 self.ascend_trace = None
111 self.ascendcl = None