Line data Source code
1 : /**
2 : * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "package_env_info.h"
12 : #include <string>
13 : #include <vector>
14 : #include "driver/ascend_hal.h"
15 : #include "capability_manager.h"
16 : #include "env_internal_api.h"
17 : #include "error_manager.h"
18 : #include "platform_info.h"
19 : #include "tsd_log.h"
20 : #include "tsd/status.h"
21 : #include "tsd_util_func.h"
22 : #include "package_process_config.h"
23 : #include "weak_ascend_hal.h"
24 :
25 : namespace {
26 : const std::string AICPU_PACKAGE_PATTERN = "^Ascend([0-9]{3}(rc)?(P)?)?-aicpu_syskernels\\.tar\\.gz$";
27 : const std::string EXTEND_PACKAGE_PATTERN = "^Ascend([0-9]{3}(rc)?(P)?)?-aicpu_extend_syskernels\\.tar\\.gz$";
28 : const std::string ASCENDCPP_PACKAGE_PATTERN = "^transformer_tile_fwk_aicpu_kernel\\.tar\\.gz$";
29 : constexpr uint32_t SOC_VERSION_LEN = 50U;
30 : const std::string QUEUE_SCHEDULE_SO = "libqueue_schedule.so";
31 : const int64_t SUPPORT_MAX_DEVICE_PER_HOST = 8;
32 : } // namespace
33 :
34 : namespace tsd {
35 :
36 533 : PackageEnvInfo::PackageEnvInfo(uint32_t logicDeviceId, uint32_t platInfoMode, bool isAdcEnv, uint32_t chipType)
37 51701 : : platInfoMode_(platInfoMode), isAdcEnv_(isAdcEnv), chipType_(chipType), logicDeviceId_(logicDeviceId)
38 : {
39 533 : packagePattern_[static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_KERNEL)] = AICPU_PACKAGE_PATTERN;
40 : packagePattern_[static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_EXTEND_KERNEL)] =
41 533 : EXTEND_PACKAGE_PATTERN;
42 533 : packagePattern_[static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_ASCENDCPP)] = ASCENDCPP_PACKAGE_PATTERN;
43 533 : hostSoPath_ = tsd::GetHostSoPath();
44 533 : }
45 :
46 20 : std::string PackageEnvInfo::GetTrustedBasePath(bool useV2) const
47 : {
48 20 : constexpr int32_t peerNode = 0;
49 20 : constexpr uint32_t hdcNameMax = 256U;
50 20 : char_t path[hdcNameMax] = {};
51 : int32_t drvRet;
52 20 : if (useV2) {
53 16 : drvRet = drvHdcGetTrustedBasePathV2(peerNode, static_cast<int32_t>(logicDeviceId_), &path[0], hdcNameMax);
54 : } else {
55 4 : drvRet = drvHdcGetTrustedBasePath(peerNode, static_cast<int32_t>(logicDeviceId_), &path[0], hdcNameMax);
56 : }
57 20 : if (drvRet != DRV_ERROR_NONE) {
58 2 : TSD_ERROR("[PackageManager][deviceId_=%u] get trusted base path failed, ret[%d]", logicDeviceId_, drvRet);
59 : }
60 40 : return std::string(path);
61 : }
62 :
63 20 : void PackageEnvInfo::GetAscendLatestIntallPath(std::string& pkgBasePath) const
64 : {
65 20 : GetEnvFromMmSys(MM_ENV_ASCEND_LATEST_INSTALL_PATH, "ASCEND_LATEST_INSTALL_PATH", pkgBasePath);
66 20 : if (pkgBasePath.empty()) {
67 19 : GetEnvFromMmSys(MM_ENV_ASCEND_HOME_PATH, "ASCEND_HOME_PATH", pkgBasePath);
68 19 : if (pkgBasePath.empty()) {
69 19 : pkgBasePath = "/usr/local/Ascend/latest/";
70 19 : TSD_INFO(
71 : "[TsdClient][logicDeviceId_=%u] ASCEND_LATEST_INSTALL_PATH is not set, use default value[%s]",
72 : logicDeviceId_, pkgBasePath.c_str());
73 : }
74 : }
75 20 : TSD_RUN_INFO("latest install path:%s", pkgBasePath.c_str());
76 20 : }
77 :
78 8 : TSD_StatusT PackageEnvInfo::GetTrustedBasePathFromDevice(int32_t& peerNode, std::string& dstDirPreFix) const
79 : {
80 8 : peerNode = 0;
81 8 : dstDirPreFix = GetTrustedBasePath(true);
82 8 : return TSD_OK;
83 : }
84 :
85 21 : std::string PackageEnvInfo::GetCurHostMutexFile(bool useCannPath) const
86 : {
87 21 : if (!useCannPath) {
88 16 : return QUEUE_SCHEDULE_SO;
89 : }
90 5 : int64_t masterId = 0;
91 5 : uint32_t phyId = 0U;
92 5 : auto drvRet = drvDeviceGetPhyIdByIndex(logicDeviceId_, &phyId);
93 5 : if (drvRet != DRV_ERROR_NONE) {
94 1 : TSD_RUN_WARN("Getting the physical ID was not successful, retCode[%d] deviceId[%u]", drvRet, logicDeviceId_);
95 1 : return QUEUE_SCHEDULE_SO;
96 : }
97 4 : TSD_INFO("deviceId[%u] physical ID[%u]", logicDeviceId_, phyId);
98 4 : drvRet = halGetDeviceInfo(phyId, MODULE_TYPE_SYSTEM, INFO_TYPE_MASTERID, &masterId);
99 4 : if (drvRet != DRV_ERROR_NONE) {
100 1 : TSD_RUN_WARN(
101 : "Calling halGetDeviceInfo was not successful, retCode[%d] deviceId[%u] physical ID[%u]", drvRet,
102 : logicDeviceId_, phyId);
103 1 : return QUEUE_SCHEDULE_SO;
104 : }
105 3 : const int64_t devOsId = masterId % SUPPORT_MAX_DEVICE_PER_HOST;
106 3 : std::string mutexFile = MUTEX_FILE_PREFIX + std::to_string(devOsId) + ".cfg";
107 3 : TSD_RUN_INFO(
108 : "get masterId:%lld, logicDeviceId:%u, physicalDeviceId[%u],devOsId:%lld, mutexFile:%s, maxcount:%lld", masterId,
109 : logicDeviceId_, phyId, devOsId, mutexFile.c_str(), SUPPORT_MAX_DEVICE_PER_HOST);
110 3 : return mutexFile;
111 3 : }
112 :
113 8 : bool PackageEnvInfo::GetShortSocVersion(std::string& shortSocVersion) const
114 : {
115 8 : char_t socVersion[SOC_VERSION_LEN] = {};
116 8 : const auto drvRet = halGetSocVersion(logicDeviceId_, &socVersion[0U], SOC_VERSION_LEN);
117 8 : if (drvRet != DRV_ERROR_NONE) {
118 1 : TSD_RUN_WARN("get soc_version by halGetSocVersion failed");
119 1 : return false;
120 : }
121 7 : TSD_INFO("get soc_version:%s", socVersion);
122 7 : (void)fe::PlatformInfoManager::Instance().InitializePlatformInfo();
123 7 : fe::OptionalInfos optionalInfos;
124 7 : fe::PlatFormInfos platformInfos;
125 21 : (void)fe::PlatformInfoManager::Instance().GetPlatformInfos(socVersion, platformInfos, optionalInfos);
126 21 : const bool ret = platformInfos.GetPlatformRes("version", "Short_SoC_version", shortSocVersion);
127 7 : if (!ret) {
128 1 : TSD_RUN_WARN("get short_soc_version by fe::PlatFormInfos::GetPlatformRes failed.");
129 1 : return false;
130 : }
131 6 : TSD_RUN_INFO("get short_soc_version:%s", shortSocVersion.c_str());
132 6 : return true;
133 7 : }
134 :
135 94 : bool PackageEnvInfo::ResolvePackageTitle(uint32_t chipType, uint32_t platInfoMode, std::string& packageTitle)
136 : {
137 94 : switch (static_cast<ChipType_t>(chipType)) {
138 67 : case CHIP_MINI:
139 67 : if (platInfoMode == static_cast<uint32_t>(ModeType::OFFLINE)) {
140 9 : packageTitle = "Ascend310RC";
141 : } else {
142 58 : packageTitle = "Ascend310";
143 : }
144 67 : break;
145 22 : case CHIP_ASCEND_910A:
146 22 : packageTitle = "Ascend910";
147 22 : break;
148 3 : case CHIP_DC:
149 3 : packageTitle = "Ascend310P";
150 3 : break;
151 1 : case CHIP_ASCEND_950:
152 : case CHIP_ASCEND_910B:
153 : case CHIP_CLOUD_V5:
154 : case CHIP_MINI_V3:
155 : case CHIP_ASCEND_350:
156 1 : packageTitle = "Ascend";
157 1 : break;
158 1 : default:
159 1 : packageTitle = "";
160 1 : break;
161 : }
162 94 : return !packageTitle.empty();
163 : }
164 :
165 90 : bool PackageEnvInfo::GetPackageTitle(std::string& packageTitle) const
166 : {
167 90 : return ResolvePackageTitle(chipType_, platInfoMode_, packageTitle);
168 : }
169 :
170 86 : bool PackageEnvInfo::GetPackagePath(std::string& packagePath, const uint32_t packageType) const
171 : {
172 86 : std::string ascendAicpuPath;
173 86 : GetEnvFromMmSys(MM_ENV_ASCEND_AICPU_PATH, "ASCEND_AICPU_PATH", ascendAicpuPath);
174 86 : if (ascendAicpuPath.empty()) {
175 85 : ascendAicpuPath = "/usr/local/Ascend/";
176 85 : TSD_INFO(
177 : "[TsdClient][deviceId=%u] environment variable ASCEND_AICPU_PATH is not set, use default "
178 : "value[%s]",
179 : logicDeviceId_, ascendAicpuPath.c_str());
180 : }
181 86 : std::string title = "";
182 86 : if (!GetPackageTitle(title)) {
183 1 : TSD_RUN_INFO("[TsdClient][deviceId=%u] skip load aicpu opkernel packages", logicDeviceId_);
184 1 : return false;
185 : }
186 :
187 85 : if (packageType == static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_KERNEL) ||
188 : packageType == static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_EXTEND_KERNEL)) {
189 57 : packagePath = ascendAicpuPath + "/opp/" + title + "/aicpu/";
190 28 : } else if (packageType == static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_ASCENDCPP)) {
191 28 : packagePath = ascendAicpuPath + "/opp/built-in/";
192 : } else {
193 0 : return false;
194 : }
195 :
196 85 : TSD_INFO("[TsdClient][deviceId=%u] get packagePath[%s]", logicDeviceId_, packagePath.c_str());
197 85 : return true;
198 86 : }
199 :
200 87 : bool PackageEnvInfo::CheckPackageExistsOnce(const uint32_t packageType)
201 : {
202 87 : std::string pkgPath;
203 87 : if (!GetPackagePath(pkgPath, packageType)) {
204 3 : TSD_INFO(
205 : "[TsdClient][deviceId=%u] Package path is invalid, skip load package file, packageType[%u]", logicDeviceId_,
206 : packageType);
207 3 : return false;
208 : }
209 84 : if ((mmAccess(pkgPath.c_str()) != EN_OK) || (mmIsDir(pkgPath.c_str()) != EN_OK)) {
210 60 : TSD_WARN(
211 : "[TsdClient][deviceId=%u] path[%s] does not exist, packageType[%u]", logicDeviceId_, pkgPath.c_str(),
212 : packageType);
213 60 : return false;
214 : }
215 :
216 24 : std::vector<std::string> packages = ScanAndMatchPackages(pkgPath, packageType);
217 24 : packageName_[packageType] = "";
218 24 : if (packages.size() != 1U) {
219 18 : TSD_RUN_INFO(
220 : "[TsdClient][deviceId=%u] pkg size[%zu] in path[%s] must be only one, skip send package, packageType[%u]",
221 : logicDeviceId_, packages.size(), pkgPath.c_str(), packageType);
222 18 : return false;
223 : }
224 6 : packageName_[packageType] = packages[0U];
225 6 : packagePath_[packageType] = pkgPath;
226 6 : TSD_RUN_INFO(
227 : "[TsdClient][deviceId=%u] set package path[%s], package name[%s], packageType[%u]", logicDeviceId_,
228 : packagePath_[static_cast<uint32_t>(packageType)].c_str(), packageName_[packageType].c_str(), packageType);
229 6 : return true;
230 87 : }
231 :
232 24 : std::vector<std::string> PackageEnvInfo::ScanAndMatchPackages(
233 : const std::string& pkgPath, const uint32_t packageType) const
234 : {
235 24 : mmDirent2** fileList = nullptr;
236 24 : const int32_t fileCount = mmScandir2(pkgPath.c_str(), &fileList, nullptr, nullptr);
237 24 : if (fileCount < 0) {
238 6 : TSD_WARN(
239 : "[TsdClient][deviceId=%u] scandir path[%s] failed, packageType[%u]", logicDeviceId_, pkgPath.c_str(),
240 : packageType);
241 6 : return {};
242 : }
243 18 : std::vector<std::string> files;
244 39 : for (int32_t i = 0; i < fileCount; i++) {
245 21 : files.emplace_back(fileList[i]->d_name);
246 : }
247 18 : mmScandirFree2(fileList, fileCount);
248 :
249 18 : std::vector<std::string> packages;
250 39 : for (const std::string& fileName : files) {
251 21 : TSD_INFO(
252 : "[TsdClient][deviceId=%u] check file[%s], packageType[%u]", logicDeviceId_, fileName.c_str(), packageType);
253 84 : if (ValidateStr(fileName.c_str(), packagePattern_[packageType].c_str())) {
254 6 : TSD_INFO(
255 : "[TsdClient][deviceId=%u] find package[%s] in path[%s], packageType[%u]", logicDeviceId_,
256 : fileName.c_str(), pkgPath.c_str(), packageType);
257 6 : packages.emplace_back(fileName);
258 : }
259 : }
260 18 : return packages;
261 18 : }
262 :
263 29 : bool PackageEnvInfo::CheckPackageExists(const bool loadAicpuKernelFlag)
264 : {
265 29 : bool hasPackage = false;
266 29 : std::vector<uint32_t> packageTypes;
267 29 : if (loadAicpuKernelFlag) {
268 29 : packageTypes.push_back(static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_KERNEL));
269 29 : packageTypes.push_back(static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_EXTEND_KERNEL));
270 : }
271 29 : packageTypes.push_back(static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_ASCENDCPP));
272 116 : for (const auto packageType : packageTypes) {
273 87 : if (CheckPackageExistsOnce(packageType)) {
274 6 : TSD_INFO("[TsdClient][deviceId=%u] get package successfully, packageType[%u]", logicDeviceId_, packageType);
275 6 : hasPackage = true;
276 : }
277 : }
278 :
279 29 : return hasPackage;
280 29 : }
281 :
282 : } // namespace tsd
|