Line data Source code
1 : /**
2 : * Copyright (c) 2025 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 "inc/process_mode_manager.h"
12 : #include <string>
13 : #include "tsd_log.h"
14 : #include "tsd/status.h"
15 : #include "env_internal_api.h"
16 : #include "tsd_msg_parse_reg.h"
17 :
18 : namespace {
19 : constexpr int32_t ERROR_LOG = 3;
20 : constexpr int32_t DEBUG_LOG = 0;
21 : constexpr uint32_t DEFAULT_NET_SERVICE = 2U;
22 : } // namespace
23 :
24 : namespace tsd {
25 :
26 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
27 : ServerToClientMsgProc1, HDCMessage::TSD_START_PROC_RSP, &ProcessModeManager::ServerToClientMsgProc);
28 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
29 : ServerToClientMsgProc2, HDCMessage::TSD_CLOSE_PROC_RSP, &ProcessModeManager::ServerToClientMsgProc);
30 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
31 : ServerToClientMsgProc3, HDCMessage::TSD_UPDATE_PROIFILING_RSP, &ProcessModeManager::ServerToClientMsgProc);
32 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
33 : ServerToClientMsgProc4, HDCMessage::TSD_CHECK_PACKAGE_RSP, &ProcessModeManager::PackageInfoMsgProc);
34 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
35 : ServerToClientMsgProc5, HDCMessage::TSD_GET_PID_QOS_RSP, &ProcessModeManager::CapabilityResMsgProc);
36 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
37 : ServerToClientMsgProc6, HDCMessage::TSD_DRV_BIND_HOST_PID_RSP, &ProcessModeManager::ServerToClientMsgProc);
38 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
39 : ServerToClientMsgProc7, HDCMessage::TSD_GET_DEVICE_RUNTIME_CHECKCODE_RSP, &ProcessModeManager::PackageInfoMsgProc);
40 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
41 : ServerToClientMsgProc8, HDCMessage::TSD_REMOVE_FILE_RSP, &ProcessModeManager::ServerToClientMsgProc);
42 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
43 : ServerToClientMsgProc9, HDCMessage::TSD_OPEN_SUB_PROC_RSP, &ProcessModeManager::ServerToClientMsgProc);
44 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
45 : ServerToClientMsgProc10, HDCMessage::TSD_GET_SUB_PROC_STATUS_RSP, &ProcessModeManager::ServerToClientMsgProc);
46 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
47 : ServerToClientMsgProc11, HDCMessage::TSD_OM_PKG_DECOMPRESS_STATUS_RSP, &ProcessModeManager::ServerToClientMsgProc);
48 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
49 : ServerToClientMsgProc12, HDCMessage::TSD_CLOSE_SUB_PROC_RSP, &ProcessModeManager::ServerToClientMsgProc);
50 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
51 : ServerToClientMsgProc13, HDCMessage::TSD_GET_SUPPORT_CAPABILITY_LEVEL_RSP,
52 : &ProcessModeManager::ServerToClientMsgProc);
53 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
54 : ServerToClientMsgProc14, HDCMessage::TSD_GET_DEVICE_DSHAPE_CHECKCODE_RSP, &ProcessModeManager::PackageInfoMsgProc);
55 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
56 : ServerToClientMsgProc15, HDCMessage::TSD_SUPPORT_OM_INNER_DEC_RSP, &ProcessModeManager::ServerToClientMsgProc);
57 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
58 : ServerToClientMsgProc16, HDCMessage::TSD_CLOSE_SUB_PROC_LIST_RSP, &ProcessModeManager::ServerToClientMsgProc);
59 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
60 : ServerToClientMsgProc17, HDCMessage::TSD_GET_SUPPORT_ADPROF_RSP, &ProcessModeManager::ServerToClientMsgProc);
61 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
62 : ServerToClientMsgProc18, HDCMessage::TSD_CHECK_PACKAGE_RETRY_RSP, &ProcessModeManager::PackageInfoMsgProc);
63 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
64 : ServerToClientMsgProc19, HDCMessage::TSD_GET_DEVICE_PACKAGE_CHECKCODE_NORMAL_RSP,
65 : &ProcessModeManager::PackageInfoMsgProc);
66 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
67 : ServerToClientMsgProc20, HDCMessage::TSD_UPDATE_PACKAGE_PROCESS_CONFIG_RSP,
68 : &ProcessModeManager::ServerToClientMsgProc);
69 : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
70 : ServerToClientMsgProc21, HDCMessage::TSD_GET_DEVICE_CANN_HS_CHECKCODE_RSP, &ProcessModeManager::PackageInfoMsgProc);
71 :
72 365 : ProcessModeManager::ProcessModeManager(const uint32_t deviceId, const uint32_t deviceMode)
73 : : ClientManager(deviceId),
74 365 : logLevel_("003"),
75 365 : commAgent_(deviceId),
76 365 : sharedCtx_(),
77 365 : capabilityMgr_(deviceId, commAgent_, IsAdcEnv()),
78 365 : packageMgr_(deviceId, commAgent_, capabilityMgr_, GetPlatInfoMode(), IsAdcEnv(), GetPlatInfoChipType()),
79 365 : rspDispatcher_(sharedCtx_, packageMgr_, capabilityMgr_),
80 365 : tsdCtrl_(commAgent_, capabilityMgr_, packageMgr_, sharedCtx_, deviceMode),
81 365 : subProcCtrl_(tsdCtrl_, commAgent_, capabilityMgr_, packageMgr_, sharedCtx_),
82 730 : ccecpuLogLevel_(""),
83 1095 : aicpuLogLevel_("")
84 : {
85 365 : sharedCtx_.logicDeviceId = deviceId;
86 365 : sharedCtx_.isAdcEnv = IsAdcEnv();
87 365 : sharedCtx_.platInfoMode = GetPlatInfoMode();
88 365 : sharedCtx_.aicpuSchedMode = aicpuSchedMode_;
89 365 : GetLogLevel();
90 365 : SyncSharedCtxLogLevels();
91 365 : tsdCtrl_.SetTsdStartInfo(false, false, false);
92 365 : }
93 :
94 2 : void ProcessModeManager::Destroy()
95 : {
96 2 : tsdCtrl_.SetTsdStartInfo(false, false, false);
97 2 : commAgent_.ReleaseDeviceConnection();
98 2 : packageMgr_.ResetOnClose();
99 2 : }
100 :
101 5 : void ProcessModeManager::ParseModuleLogLevelByKey(const std::string& keyStr, const std::string& valStr)
102 : {
103 5 : int32_t val = 0;
104 5 : if (TransStrToInt(valStr, val)) {
105 4 : if (val >= DEBUG_LOG && val <= ERROR_LOG) {
106 4 : if (keyStr == "CCECPU") {
107 3 : ccecpuLogLevel_ = valStr;
108 1 : } else if (keyStr == "AICPU") {
109 1 : aicpuLogLevel_ = valStr;
110 : } else {
111 0 : TSD_INFO("[TsdClient] invalid key[%s]", keyStr.c_str());
112 : }
113 : }
114 : }
115 5 : }
116 :
117 372 : void ProcessModeManager::ParseModuleLogLevel(const std::string& envModuleLogLevel)
118 : {
119 372 : std::size_t offsetColon = 0;
120 372 : std::size_t offsetEqual = 0;
121 744 : std::string keyStr = "";
122 372 : std::string valStr = "";
123 372 : std::string moduleLogLevel = envModuleLogLevel;
124 : while (true) {
125 374 : offsetColon = moduleLogLevel.find(":");
126 374 : if (offsetColon == std::string::npos) {
127 372 : offsetEqual = moduleLogLevel.find("=");
128 372 : if (offsetEqual != std::string::npos) {
129 3 : keyStr = moduleLogLevel.substr(0U, offsetEqual);
130 3 : valStr = moduleLogLevel.substr(offsetEqual + 1UL);
131 3 : ParseModuleLogLevelByKey(keyStr, valStr);
132 : }
133 372 : return;
134 : } else {
135 2 : std::string moduleLogLevelPart1 = moduleLogLevel.substr(0U, offsetColon);
136 2 : offsetEqual = moduleLogLevelPart1.find("=");
137 2 : if (offsetEqual != std::string::npos) {
138 2 : keyStr = moduleLogLevelPart1.substr(0U, offsetEqual);
139 2 : valStr = moduleLogLevelPart1.substr(offsetEqual + 1UL);
140 2 : ParseModuleLogLevelByKey(keyStr, valStr);
141 : }
142 2 : moduleLogLevel = moduleLogLevel.substr(offsetColon + 1UL);
143 2 : }
144 2 : }
145 372 : }
146 :
147 368 : void ProcessModeManager::GetLogLevel()
148 : {
149 368 : int32_t defaultEventLevel = 1;
150 368 : int32_t defaultLogLevel = 3;
151 368 : if (&dlog_getlevel != nullptr) {
152 368 : defaultLogLevel = dlog_getlevel(AICPU, &defaultEventLevel);
153 : }
154 368 : std::string eventLevelString = std::to_string(defaultEventLevel);
155 368 : std::string logLevelString = std::to_string(defaultLogLevel);
156 368 : if (!IsAdcEnv()) {
157 734 : const std::string logPattern = "^[0-4]{1}$";
158 367 : const std::string eventPattern = "^[0-1]{1}$";
159 367 : std::string envLogLevel;
160 367 : std::string envModuleLogLevel;
161 367 : std::string envEventLevel;
162 367 : GetEnvFromMmSys(MM_ENV_ASCEND_GLOBAL_LOG_LEVEL, "ASCEND_GLOBAL_LOG_LEVEL", envLogLevel);
163 367 : GetEnvFromMmSys(MM_ENV_ASCEND_GLOBAL_EVENT_ENABLE, "ASCEND_GLOBAL_EVENT_ENABLE", envEventLevel);
164 367 : GetEnvFromMmSys(MM_ENV_ASCEND_MODULE_LOG_LEVEL, "ASCEND_MODULE_LOG_LEVEL", envModuleLogLevel);
165 367 : TSD_RUN_INFO(
166 : "[TsdClient] get ASCEND_GLOBAL_LOG_LEVEL [%s] ASCEND_GLOBAL_EVENT_ENABLE [%s] "
167 : "ASCEND_MODULE_LOG_LEVEL [%s]",
168 : envLogLevel.c_str(), envEventLevel.c_str(), envModuleLogLevel.c_str());
169 1468 : if (ValidateStr(envLogLevel.c_str(), logPattern.c_str())) {
170 2 : logLevelString = envLogLevel;
171 : }
172 1468 : if (ValidateStr(envEventLevel.c_str(), eventPattern.c_str())) {
173 2 : eventLevelString = envEventLevel;
174 : }
175 1468 : if (ValidateStr(envModuleLogLevel.c_str(), envModuleLogLevel.c_str())) {
176 367 : ParseModuleLogLevel(envModuleLogLevel);
177 : }
178 367 : }
179 368 : logLevel_ = eventLevelString + "0" + logLevelString;
180 368 : TSD_INFO("[TsdClient] Get Log Level[%s]", logLevel_.c_str());
181 368 : SyncSharedCtxLogLevels();
182 368 : }
183 :
184 735 : void ProcessModeManager::SyncSharedCtxLogLevels()
185 : {
186 735 : sharedCtx_.logLevel = logLevel_;
187 735 : sharedCtx_.ccecpuLogLevel = ccecpuLogLevel_;
188 735 : sharedCtx_.aicpuLogLevel = aicpuLogLevel_;
189 735 : sharedCtx_.profilingMode = static_cast<uint32_t>(profilingMode_);
190 735 : sharedCtx_.aicpuSchedMode = aicpuSchedMode_;
191 735 : }
192 :
193 18 : TSD_StatusT ProcessModeManager::Open(const uint32_t rankSize) { return tsdCtrl_.Open(rankSize); }
194 :
195 1 : TSD_StatusT ProcessModeManager::OpenAicpuSd() { return tsdCtrl_.OpenAicpuSd(); }
196 :
197 18 : TSD_StatusT ProcessModeManager::Close(const uint32_t flag) { return tsdCtrl_.Close(flag); }
198 :
199 4 : TSD_StatusT ProcessModeManager::GetHdcConctStatus(int32_t& hdcSessStat)
200 : {
201 4 : return tsdCtrl_.GetHdcConctStatus(hdcSessStat);
202 : }
203 :
204 1 : TSD_StatusT ProcessModeManager::UpdateProfilingConf(const uint32_t flag) { return tsdCtrl_.UpdateProfilingConf(flag); }
205 :
206 2 : TSD_StatusT ProcessModeManager::InitQs(const InitFlowGwInfo* const initInfo) { return tsdCtrl_.InitQs(initInfo); }
207 :
208 8 : TSD_StatusT ProcessModeManager::CapabilityGet(const int32_t type, const uint64_t ptr)
209 : {
210 8 : return capabilityMgr_.CapabilityGet(type, ptr);
211 : }
212 :
213 3 : TSD_StatusT ProcessModeManager::LoadFileToDevice(
214 : const char_t* const filePath, const uint64_t pathLen, const char_t* const fileName, const uint64_t fileNameLen)
215 : {
216 3 : return packageMgr_.LoadFileToDevice(filePath, pathLen, fileName, fileNameLen, tsdCtrl_.BuildBaseMessageContext());
217 : }
218 :
219 8 : TSD_StatusT ProcessModeManager::ProcessOpenSubProc(ProcOpenArgs* openArgs)
220 : {
221 8 : return subProcCtrl_.OpenSubProc(openArgs);
222 : }
223 :
224 3 : TSD_StatusT ProcessModeManager::ProcessCloseSubProc(const pid_t closePid)
225 : {
226 3 : return subProcCtrl_.CloseSubProc(closePid);
227 : }
228 :
229 2 : TSD_StatusT ProcessModeManager::GetSubProcStatus(ProcStatusInfo* pidInfo, const uint32_t arrayLen)
230 : {
231 2 : return subProcCtrl_.GetSubProcStatus(pidInfo, arrayLen);
232 : }
233 :
234 3 : TSD_StatusT ProcessModeManager::RemoveFileOnDevice(const char_t* const filePath, const uint64_t pathLen)
235 : {
236 3 : return subProcCtrl_.RemoveFileOnDevice(filePath, pathLen);
237 : }
238 :
239 2 : TSD_StatusT ProcessModeManager::ProcessCloseSubProcList(const ProcStatusParam* closeList, const uint32_t listSize)
240 : {
241 2 : return subProcCtrl_.CloseSubProcList(closeList, listSize);
242 : }
243 :
244 1 : TSD_StatusT ProcessModeManager::GetSubProcListStatus(ProcStatusParam* pidInfo, const uint32_t arrayLen)
245 : {
246 1 : return subProcCtrl_.GetSubProcListStatus(pidInfo, arrayLen);
247 : }
248 :
249 5 : TSD_StatusT ProcessModeManager::OpenNetService(const NetServiceOpenArgs* args)
250 : {
251 5 : if (!capabilityMgr_.IsSupportCommonInterface(TSD_SUPPORT_MUL_HCCP)) {
252 1 : TSD_RUN_INFO("current package does not support opening net service with args");
253 1 : if (tsdCtrl_.Open(DEFAULT_NET_SERVICE) != TSD_OK) {
254 1 : TSD_ERROR("open default net service failed");
255 1 : return TSD_OPEN_DEFAULT_NET_SERVICE_FAILED;
256 : } else {
257 0 : return TSD_OK;
258 : }
259 : } else {
260 4 : if (args == nullptr) {
261 1 : TSD_ERROR("openArgs is null");
262 1 : return TSD_INTERNAL_ERROR;
263 : }
264 : ProcOpenArgs openArgs;
265 3 : pid_t subPid = 0;
266 3 : openArgs.procType = SubProcType::TSD_SUB_PROC_HCCP;
267 3 : openArgs.envParaList = nullptr;
268 3 : openArgs.envCnt = 0UL;
269 3 : openArgs.filePath = nullptr;
270 3 : openArgs.pathLen = 0UL;
271 3 : openArgs.extParamCnt = args->extParamCnt;
272 3 : openArgs.extParamList = args->extParamList;
273 3 : openArgs.subPid = &subPid;
274 3 : return subProcCtrl_.OpenSubProc(&openArgs);
275 : }
276 : }
277 :
278 3 : TSD_StatusT ProcessModeManager::CloseNetService()
279 : {
280 3 : if (!capabilityMgr_.IsSupportCommonInterface(TSD_SUPPORT_MUL_HCCP)) {
281 0 : TSD_RUN_INFO("current package does not support closing net service with args");
282 0 : return TsdClose(0U);
283 : } else {
284 : ProcStatusParam closeList;
285 3 : closeList.pid = static_cast<pid_t>(tsdCtrl_.GetHccpPid());
286 3 : closeList.curStat = SubProcessStatus::SUB_PROCESS_STATUS_NORMAL;
287 3 : closeList.procType = SubProcType::TSD_SUB_PROC_HCCP;
288 3 : return subProcCtrl_.CloseSubProcList(&closeList, 1U);
289 : }
290 : }
291 :
292 91 : void ProcessModeManager::ServerToClientMsgProc(const uint32_t sessionID, const HDCMessage& msg)
293 : {
294 91 : TSD_INFO("[ServerToClientMsgProc] [sessionID=%u]", sessionID);
295 91 : const uint32_t realDeviceId = msg.real_device_id();
296 : const std::shared_ptr<ProcessModeManager> client =
297 91 : std::dynamic_pointer_cast<ProcessModeManager>(ClientManager::GetInstance(realDeviceId, DIE_MODE, false));
298 91 : TSD_CHECK_NULLPTR_VOID(client);
299 91 : client->GetDispatcher().DeviceMsgProcess(msg);
300 91 : }
301 :
302 5 : void ProcessModeManager::PackageInfoMsgProc(const uint32_t sessionID, const HDCMessage& msg)
303 : {
304 5 : TSD_INFO("[PackageInfoMsgProc] [sessionID=%u]", sessionID);
305 5 : const uint32_t realDeviceId = msg.real_device_id();
306 5 : TSD_INFO(
307 : "[PackageInfoMsgProc] [sessionID=%u] deviceId[%u], checkCode[%u]", sessionID, realDeviceId, msg.check_code());
308 : const std::shared_ptr<ProcessModeManager> client =
309 5 : std::dynamic_pointer_cast<ProcessModeManager>(ClientManager::GetInstance(realDeviceId, DIE_MODE, false));
310 5 : TSD_CHECK_NULLPTR_VOID(client);
311 5 : client->GetDispatcher().SaveDeviceCheckCode(msg);
312 5 : }
313 :
314 3 : void ProcessModeManager::CapabilityResMsgProc(const uint32_t sessionID, const HDCMessage& msg)
315 : {
316 3 : TSD_INFO("[CapabilityResMsgProc] [sessionID=%u]", sessionID);
317 3 : const uint32_t realDeviceId = msg.real_device_id();
318 : const std::shared_ptr<ProcessModeManager> client =
319 3 : std::dynamic_pointer_cast<ProcessModeManager>(ClientManager::GetInstance(realDeviceId, DIE_MODE, false));
320 3 : TSD_CHECK_NULLPTR_VOID(client);
321 3 : client->GetDispatcher().PidQosMsgProc(msg);
322 3 : }
323 :
324 : } // namespace tsd
|