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 "inc/tsd_process_controller.h"
12 : #include <sstream>
13 : #include <string>
14 : #include <vector>
15 : #include <chrono>
16 : #include <memory>
17 : #include "error_manager.h"
18 : #include "tsd_log.h"
19 : #include "tsd/status.h"
20 : #include "tsd_util_func.h"
21 : #include "env_internal_api.h"
22 : #include "hdc_message_builder.h"
23 : #include "weak_ascend_hal.h"
24 : #include "driver/ascend_hal.h"
25 : #include "common/type_def.h"
26 :
27 : namespace {
28 : constexpr uint32_t DEFAULT_QS_RANKSIZE = 0U;
29 : constexpr uint32_t ASAN_OPEN_TIMEOUT = 3600000U;
30 : constexpr uint32_t MAX_QUEUE_ID_NUM = 8192U;
31 : } // namespace
32 :
33 : namespace tsd {
34 :
35 263 : TsdProcessController::TsdProcessController(
36 : DeviceCommAgent& commAgent, CapabilityManager& capabilityMgr, PackageManager& packageMgr,
37 263 : ProcessSharedContext& sharedCtx, uint32_t aicpuDeviceMode)
38 263 : : commAgent_(commAgent),
39 263 : capabilityMgr_(capabilityMgr),
40 263 : packageMgr_(packageMgr),
41 263 : sharedCtx_(sharedCtx),
42 263 : aicpuDeviceMode_(aicpuDeviceMode)
43 263 : {}
44 :
45 20 : TSD_StatusT TsdProcessController::Open(const uint32_t rankSize)
46 : {
47 20 : if (sharedCtx_.isAdcEnv) {
48 1 : TSD_INFO("skip to start aicpu-sd process.");
49 1 : return TSD_OPEN_NOT_SUPPORT_FOR_ADC;
50 : }
51 19 : return OpenProcess(rankSize);
52 : }
53 :
54 1 : TSD_StatusT TsdProcessController::OpenAicpuSd() { return OpenProcess(0U); }
55 :
56 21 : TSD_StatusT TsdProcessController::OpenProcess(const uint32_t rankSize)
57 : {
58 21 : TSD_RUN_INFO(
59 : "[ProcessModeManager] enter into open process deviceId[%u] rankSize[%u]", sharedCtx_.logicDeviceId, rankSize);
60 21 : TsdStartStatusInfo startInfo = {};
61 21 : const TimePoint beginOpen = std::chrono::high_resolution_clock::now();
62 21 : if (!CheckNeedToOpen(rankSize, startInfo)) {
63 1 : TSD_INFO("Open has already been done before.");
64 1 : return TSD_OK;
65 : }
66 :
67 20 : TSD_StatusT ret = LoadPackagesToDevice();
68 20 : TSD_CHECK(ret == TSD_OK, ret, "Load packages to device failed.");
69 :
70 20 : ret = InitTsdClient();
71 20 : TSD_CHECK(ret == TSD_OK, ret, "Init hdc client failed.");
72 19 : startInfo.startQs_ = false;
73 19 : ret = SendOpenMsg(rankSize, startInfo);
74 19 : TSD_CHECK(ret == TSD_OK, ret, "Send open message to device failed.");
75 19 : const TimePoint finSendOpenMsg = std::chrono::high_resolution_clock::now();
76 :
77 19 : ret = WaitOpenRsp(rankSize);
78 19 : if (ret != TSD_OK) {
79 2 : return ret;
80 : }
81 17 : const TimePoint finRecvRsp = std::chrono::high_resolution_clock::now();
82 17 : SetTsdStartInfo(startInfo.startCp_, startInfo.startHccp_, false);
83 :
84 17 : ret = ProcessQueueForAdc();
85 17 : if (ret != TSD_OK) {
86 1 : TSD_ERROR("ProcessQueueForAdc error");
87 1 : return ret;
88 : }
89 16 : const TimePoint finOpen = std::chrono::high_resolution_clock::now();
90 16 : LogOpenProcessDuration(beginOpen, finSendOpenMsg, finRecvRsp, finOpen);
91 16 : return TSD_OK;
92 : }
93 :
94 20 : TSD_StatusT TsdProcessController::LoadPackagesToDevice()
95 : {
96 20 : TSD_StatusT ret = packageMgr_.LoadPackageConfigInfoToDevice(true);
97 20 : TSD_CHECK(ret == TSD_OK, ret, "Load package config info to device failed.");
98 :
99 20 : ret = packageMgr_.LoadSysOpKernel();
100 20 : TSD_CHECK(ret == TSD_OK, ret, "Send aicpu package to device failed.");
101 :
102 20 : ret = packageMgr_.LoadPackageToDeviceByConfig();
103 20 : TSD_CHECK(ret == TSD_OK, ret, "Load package to device by config failed");
104 20 : return TSD_OK;
105 : }
106 :
107 19 : TSD_StatusT TsdProcessController::WaitOpenRsp(const uint32_t rankSize)
108 : {
109 19 : TSD_RUN_INFO(
110 : "[ProcessModeManager] deviceId[%u] sessionId[%u] rankSize[%u], wait subprocess start response",
111 : sharedCtx_.logicDeviceId, commAgent_.GetSessionId(), rankSize);
112 19 : const TSD_StatusT ret = IsAsanMmSysEnv() ? WaitRsp(ASAN_OPEN_TIMEOUT) : WaitRsp(0U);
113 19 : if (ret != TSD_OK) {
114 : REPORT_INPUT_ERROR("E39007", std::vector<std::string>(), std::vector<std::string>());
115 2 : TSD_ERROR("Wait open response from device failed.");
116 : }
117 19 : return ret;
118 : }
119 :
120 16 : void TsdProcessController::LogOpenProcessDuration(
121 : const TimePoint& beginOpen, const TimePoint& finSendOpenMsg, const TimePoint& finRecvRsp,
122 : const TimePoint& finOpen) const
123 : {
124 16 : TSD_RUN_INFO(
125 : "[TsdClient][deviceId=%u] [sessionId=%u] start hccp and computer process success,"
126 : "phase4:[%zu]ms send open message to device,"
127 : "phase5:[%zu]ms receive open message to device,"
128 : "phase6:[%zu]ms process for adc on host,"
129 : "whole duration:[%zu]ms",
130 : sharedCtx_.logicDeviceId, commAgent_.GetSessionId(),
131 : std::chrono::duration_cast<std::chrono::milliseconds>(finSendOpenMsg - beginOpen).count(),
132 : std::chrono::duration_cast<std::chrono::milliseconds>(finRecvRsp - finSendOpenMsg).count(),
133 : std::chrono::duration_cast<std::chrono::milliseconds>(finOpen - finRecvRsp).count(),
134 : std::chrono::duration_cast<std::chrono::milliseconds>(finOpen - beginOpen).count());
135 16 : }
136 :
137 21 : TSD_StatusT TsdProcessController::ConstructCloseMsg(HDCMessage& msg)
138 : {
139 21 : return HdcMessageBuilder::BuildClose(msg, BuildBaseMessageContext());
140 : }
141 :
142 21 : TSD_StatusT TsdProcessController::Close(const uint32_t flag)
143 : {
144 21 : if (!commAgent_.IsInit()) {
145 2 : TSD_RUN_INFO("[TsdClient] tsd client no need to close");
146 2 : return TSD_OK;
147 : }
148 19 : TSD_CHECK_NULLPTR(
149 : commAgent_.GetDeviceComm(), TSD_INSTANCE_NOT_INITIALED, "[TsdClient] devCommClient_ is null in Close function");
150 19 : TsdCloseFlag tsdCloseFlag = {};
151 19 : ParseTsdCloseFlag(flag, tsdCloseFlag);
152 19 : if (tsdCloseFlag.quickCloseFlag != QUICK_CLOSE_MODE) {
153 19 : TSD_RUN_INFO(
154 : "[TsdClient] Close [deviceId=%u][sessionId=%u] hccp and computer enter", sharedCtx_.logicDeviceId,
155 : commAgent_.GetSessionId());
156 19 : TSD_StatusT ret = SendCloseMsg();
157 19 : TSD_CHECK(ret == TSD_OK, ret, "Send close message to device failed.");
158 19 : TSD_RUN_INFO(
159 : "[TsdClient][deviceId=%u] [sessionId=%u] wait hccp and computer process close response",
160 : sharedCtx_.logicDeviceId, commAgent_.GetSessionId());
161 19 : ret = WaitRsp(0U, false, true);
162 19 : TSD_CHECK(ret == TSD_OK, ret, "Wait open response from device failed.");
163 19 : TSD_RUN_INFO(
164 : "[TsdClient][logicDeviceId_=%u]has recv close hccp and computer process response",
165 : sharedCtx_.logicDeviceId);
166 : } else {
167 0 : TSD_RUN_INFO("Enable quick tsd close, close will only in host.");
168 : }
169 :
170 19 : commAgent_.ReleaseDeviceConnection();
171 19 : sharedCtx_.rspCode = ResponseCode::FAIL;
172 19 : isStartedHccp_ = false;
173 19 : hccpPid_ = 0;
174 19 : SetTsdStartInfo(false, false, false);
175 19 : packageMgr_.ResetOnClose();
176 19 : TSD_RUN_INFO(
177 : "[TsdClient][deviceId=%u] [sessionId=%u] close hccp and "
178 : "computer process success",
179 : sharedCtx_.logicDeviceId, commAgent_.GetSessionId());
180 19 : return TSD_OK;
181 : }
182 :
183 71 : TSD_StatusT TsdProcessController::WaitRsp(const uint32_t timeout, const bool ignoreRecvErr, const bool isClose)
184 : {
185 71 : const TSD_StatusT ret = commAgent_.RecvData(ignoreRecvErr, timeout);
186 71 : if (!sharedCtx_.isAdcEnv) {
187 71 : if (isClose && (ret == TSD_HDC_SERVER_CLIENT_SOCKET_CLOSED)) {
188 1 : TSD_RUN_INFO("close rsp not receive, server close the session");
189 1 : return TSD_OK;
190 : }
191 : }
192 70 : if ((ret != TSD_OK) || (static_cast<uint32_t>(sharedCtx_.rspCode) != 0U)) {
193 13 : const std::string reportMsg = BuildWaitRspErrReport(ret);
194 13 : if (!ignoreRecvErr) {
195 12 : TSD_ERROR("%s", reportMsg.c_str());
196 : TSD_REPORT_INNER_ERROR("%s", reportMsg.c_str());
197 : }
198 13 : if ((ret == TSD_OK) && (!sharedCtx_.startOrStopFailCode.empty())) {
199 : REPORT_INPUT_ERROR(
200 : sharedCtx_.startOrStopFailCode.c_str(), std::vector<std::string>(), std::vector<std::string>());
201 : }
202 13 : return MapFailCodeToStatus();
203 13 : }
204 57 : return TSD_OK;
205 : }
206 :
207 19 : TSD_StatusT TsdProcessController::MapFailCodeToStatus() const
208 : {
209 19 : if (sharedCtx_.startOrStopFailCode == "E30003") {
210 3 : return TSD_SUBPROCESS_NUM_EXCEED_THE_LIMIT;
211 16 : } else if (sharedCtx_.startOrStopFailCode == "E30004") {
212 2 : return TSD_SUBPROCESS_BINARY_FILE_DAMAGED;
213 14 : } else if (sharedCtx_.startOrStopFailCode == "E30006") {
214 3 : return TSD_VERIFY_OPP_FAIL;
215 11 : } else if (sharedCtx_.startOrStopFailCode == "E30007") {
216 2 : return TSD_ADD_AICPUSD_TO_CGROUP_FAILED;
217 : } else {
218 9 : return TSD_CLT_OPEN_FAILED;
219 : }
220 : }
221 :
222 16 : std::string TsdProcessController::BuildWaitRspErrReport(const TSD_StatusT recvRet) const
223 : {
224 16 : std::ostringstream reportMsg;
225 16 : reportMsg << "tsd client wait response fail, hostpid:" << static_cast<uint32_t>(commAgent_.GetProcSign().tgid);
226 16 : if (recvRet != TSD_OK) {
227 9 : reportMsg << ", receive device response data failed, check hdc service.";
228 : } else {
229 7 : reportMsg << ", device response code[" << static_cast<uint32_t>(sharedCtx_.rspCode) << "]";
230 7 : if (!sharedCtx_.errMsg.empty()) {
231 3 : reportMsg << ". " << sharedCtx_.errMsg;
232 3 : reportMsg << ", error stack:\n" << sharedCtx_.errorLog;
233 : } else {
234 4 : reportMsg << ". unknown device error.";
235 : }
236 : }
237 32 : return reportMsg.str();
238 16 : }
239 :
240 29 : TSD_StatusT TsdProcessController::InitTsdClient()
241 : {
242 29 : if (commAgent_.IsInit()) {
243 22 : TSD_INFO("[TsdClient] tsd client has already been initialized");
244 22 : return TSD_OK;
245 : }
246 7 : return commAgent_.InitTsdClient(sharedCtx_.isAdcEnv);
247 : }
248 :
249 86 : MessageContext TsdProcessController::BuildBaseMessageContext() const
250 : {
251 86 : MessageContext ctx{};
252 86 : ctx.logicDeviceId = sharedCtx_.logicDeviceId;
253 86 : ctx.rankSize = rankSize_;
254 86 : ctx.procSign = commAgent_.GetProcSign();
255 86 : ctx.profilingMode = static_cast<uint32_t>(sharedCtx_.profilingMode);
256 86 : ctx.logLevel = sharedCtx_.logLevel;
257 86 : ctx.ccecpuLogLevel = sharedCtx_.ccecpuLogLevel;
258 86 : ctx.aicpuLogLevel = sharedCtx_.aicpuLogLevel;
259 86 : ctx.aicpuKernelCheckCode = packageMgr_.GetHostCheckCode(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_KERNEL);
260 86 : ctx.aicpuExtendKernelCheckCode = packageMgr_.GetHostCheckCode(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_EXTEND_KERNEL);
261 86 : ctx.ascendcppCheckCode = packageMgr_.GetHostCheckCode(TsdLoadPackageType::TSD_PKG_TYPE_ASCENDCPP);
262 86 : ctx.aicpuDeviceMode = aicpuDeviceMode_;
263 86 : ctx.aicpuSchedMode = static_cast<SchedMode>(sharedCtx_.aicpuSchedMode);
264 86 : ctx.qsInitGroupName = qsInitGrpName_;
265 86 : ctx.schedPolicy = schedPolicy_;
266 86 : return ctx;
267 0 : }
268 :
269 19 : TSD_StatusT TsdProcessController::ConstructOpenMsg(HDCMessage& hdcMsg, const TsdStartStatusInfo& startInfo)
270 : {
271 19 : MessageContext ctx = BuildBaseMessageContext();
272 19 : ctx.startHccp = startInfo.startHccp_;
273 19 : ctx.startCp = startInfo.startCp_;
274 38 : return HdcMessageBuilder::BuildOpen(hdcMsg, ctx);
275 19 : }
276 :
277 17 : TSD_StatusT TsdProcessController::SendOpenMsg(const uint32_t rankSize, const TsdStartStatusInfo startInfo)
278 : {
279 17 : rankSize_ = rankSize;
280 17 : HDCMessage hdcMsg;
281 17 : if (ConstructOpenMsg(hdcMsg, startInfo) != TSD_OK) {
282 0 : TSD_ERROR("ConstructOpenMsg open msg error");
283 0 : return TSD_CLT_OPEN_FAILED;
284 : }
285 17 : if (startInfo.startQs_) {
286 2 : hdcMsg.set_type(HDCMessage::TSD_START_QS_MSG);
287 2 : qsInitGroupName* const groupName = hdcMsg.mutable_qs_init_group_name();
288 2 : if (groupName == nullptr) {
289 0 : return TSD_INTERNAL_ERROR;
290 : }
291 2 : groupName->set_qs_init_group_name(qsInitGrpName_);
292 2 : hdcMsg.set_sched_policy(schedPolicy_);
293 2 : std::string installPath;
294 2 : packageMgr_.GetAscendLatestIntallPath(installPath);
295 2 : if (!installPath.empty()) {
296 : hdcMsg.set_ascend_install_path(installPath);
297 : }
298 2 : } else {
299 15 : hdcMsg.set_type(HDCMessage::TSD_START_PROC_MSG);
300 15 : if (isStartedHccp_) {
301 1 : hdcMsg.set_start_hccp(true);
302 : }
303 : }
304 17 : const TSD_StatusT ret = commAgent_.SendMsg(hdcMsg);
305 17 : if (ret != TSD_OK) {
306 0 : TSD_ERROR(
307 : "[TsdClient][deviceId=%u][rankSize_=%u][procpid =%u] "
308 : "tsdclient start hccp and computer process failed",
309 : sharedCtx_.logicDeviceId, rankSize_, static_cast<uint32_t>(commAgent_.GetProcSign().tgid));
310 0 : return TSD_CLT_OPEN_FAILED;
311 : }
312 17 : return TSD_OK;
313 17 : }
314 :
315 20 : TSD_StatusT TsdProcessController::SendCloseMsg()
316 : {
317 20 : HDCMessage msg;
318 20 : if (ConstructCloseMsg(msg) != TSD_OK) {
319 1 : TSD_ERROR("ConstructCloseMsg failed");
320 1 : return TSD_INTERNAL_ERROR;
321 : }
322 19 : const TSD_StatusT ret = commAgent_.SendMsg(msg);
323 19 : if (ret != TSD_OK) {
324 1 : TSD_ERROR(
325 : "[TsdClient][deviceId=%u] tsdclient close hccp and "
326 : "computer process failed",
327 : sharedCtx_.logicDeviceId);
328 1 : return TSD_CLT_CLOSE_FAILED;
329 : }
330 :
331 18 : return TSD_OK;
332 20 : }
333 :
334 5 : TSD_StatusT TsdProcessController::UpdateProfilingConf(const uint32_t& flag)
335 : {
336 5 : TSD_RUN_INFO(
337 : "[TsdClient] Update profiling mode [deviceId=%u][sessionId=%u][flag=%u]", sharedCtx_.logicDeviceId,
338 : commAgent_.GetSessionId(), flag);
339 :
340 5 : if (!commAgent_.IsInit()) {
341 1 : TSD_WARN("[TsdClient] tsd client need open first");
342 1 : return TSD_OK;
343 : }
344 4 : TSD_CHECK_NULLPTR(
345 : commAgent_.GetDeviceComm(), TSD_INSTANCE_NOT_INITIALED,
346 : "[TsdClient] devCommClient_ is null in update profiling function");
347 4 : TSD_StatusT ret = SendUpdateProfilingMsg(flag);
348 4 : TSD_CHECK(ret == TSD_OK, ret, "Send profiling message to tsdaemon failed.");
349 :
350 3 : TSD_RUN_INFO(
351 : "[TsdClient][deviceId=%u] [sessionId=%u] wait update profiling msg response", sharedCtx_.logicDeviceId,
352 : commAgent_.GetSessionId());
353 :
354 3 : ret = WaitRsp(0U);
355 3 : TSD_CHECK(ret == TSD_OK, ret, "Wait UpdateProfiling response from device failed.");
356 2 : return TSD_OK;
357 : }
358 :
359 3 : TSD_StatusT TsdProcessController::SendUpdateProfilingMsg(const uint32_t flag)
360 : {
361 3 : HDCMessage msg;
362 3 : MessageContext ctx = BuildBaseMessageContext();
363 3 : ctx.profilingMode = flag;
364 3 : if (HdcMessageBuilder::BuildUpdateProfiling(msg, ctx) != TSD_OK) {
365 0 : return TSD_INTERNAL_ERROR;
366 : }
367 3 : const TSD_StatusT ret = commAgent_.SendMsg(msg);
368 3 : if (ret != TSD_OK) {
369 1 : TSD_ERROR("[TsdClient][deviceId=%u] tsdclient update profiling mode failed", sharedCtx_.logicDeviceId);
370 1 : return TSD_CLT_UPDATE_PROFILING_FAILED;
371 : }
372 :
373 2 : return TSD_OK;
374 3 : }
375 :
376 4 : TSD_StatusT TsdProcessController::GetHdcConctStatus(int32_t& hdcSessStat)
377 : {
378 4 : return commAgent_.GetHdcConctStatus(hdcSessStat, sharedCtx_.isAdcEnv);
379 : }
380 :
381 3 : TSD_StatusT TsdProcessController::InitQs(const InitFlowGwInfo* const initInfo)
382 : {
383 3 : TSD_RUN_INFO(
384 : "[ProcessModeManager][logicDeviceId_=%u] begin to prepare send open message", sharedCtx_.logicDeviceId);
385 3 : if (initInfo == nullptr) {
386 0 : TSD_ERROR("TsdInitQs failed, initInfo is nullptr.");
387 0 : return tsd::TSD_INTERNAL_ERROR;
388 : }
389 :
390 3 : const char_t* const groupName = initInfo->groupName;
391 3 : if (tsdStartStatus_.startQs_) {
392 1 : TSD_INFO("[ProcessModeManager] QS has opened already, no need open again");
393 1 : return TSD_OK;
394 : }
395 2 : TSD_StatusT ret = InitTsdClient();
396 2 : TSD_CHECK(ret == TSD_OK, ret, "Init hdc client failed.");
397 :
398 2 : if (groupName != nullptr) {
399 2 : TSD_INFO("[TsdClient]QS open with group[%s]", groupName);
400 2 : qsInitGrpName_ = groupName;
401 : } else {
402 0 : TSD_INFO("[TsdClient]QS open with empty groupName");
403 0 : qsInitGrpName_.clear();
404 : }
405 2 : schedPolicy_ = initInfo->schedPolicy;
406 :
407 2 : constexpr TsdStartStatusInfo startInfo = {false, false, true};
408 2 : ret = SendOpenMsg(DEFAULT_QS_RANKSIZE, startInfo);
409 2 : TSD_CHECK(ret == TSD_OK, ret, "Send InitQs message to device failed.");
410 :
411 2 : ret = WaitRsp(0U);
412 2 : TSD_CHECK(ret == TSD_OK, ret, "Wait InitQs response from device failed.");
413 :
414 2 : tsdStartStatus_.startQs_ = true;
415 2 : TSD_RUN_INFO(
416 : "[TsdClient][logicDeviceId_=%u] [sessionId=%u] start QS process success", sharedCtx_.logicDeviceId,
417 : commAgent_.GetSessionId());
418 2 : return TSD_OK;
419 : }
420 :
421 301 : void TsdProcessController::SetTsdStartInfo(const bool cpStatus, const bool hccpStatus, const bool qsStatus)
422 : {
423 301 : tsdStartStatus_.startCp_ = cpStatus;
424 301 : tsdStartStatus_.startHccp_ = hccpStatus;
425 301 : tsdStartStatus_.startQs_ = qsStatus;
426 301 : capabilityMgr_.SetStartCpStatus(cpStatus);
427 301 : }
428 :
429 23 : bool TsdProcessController::CheckNeedToOpen(const uint32_t rankSize, TsdStartStatusInfo& startInfo)
430 : {
431 23 : constexpr uint32_t HCCP_START_RANK_SIZE = 1U;
432 23 : if (rankSize <= HCCP_START_RANK_SIZE) {
433 15 : if (tsdStartStatus_.startCp_) {
434 2 : TSD_INFO("[TsdClient] cp has already opened, no need open again");
435 2 : return false;
436 : } else {
437 13 : startInfo.startHccp_ = false;
438 13 : startInfo.startCp_ = true;
439 : }
440 : } else {
441 8 : if ((tsdStartStatus_.startCp_) && (tsdStartStatus_.startHccp_)) {
442 1 : TSD_INFO("[TsdClient] hccp and cp has already opened, no need open again");
443 1 : return false;
444 : }
445 7 : startInfo.startCp_ = true;
446 7 : startInfo.startHccp_ = true;
447 : }
448 20 : TSD_INFO(
449 : "[TsdClient] get open para startCp[%u] startHccp[%u] (0:false 1:true)",
450 : static_cast<uint32_t>(startInfo.startCp_), static_cast<uint32_t>(startInfo.startHccp_));
451 20 : return true;
452 : }
453 :
454 19 : void TsdProcessController::ParseTsdCloseFlag(const uint32_t flag, TsdCloseFlag& tsdCloseFlag) const
455 : {
456 19 : TSD_RUN_INFO("Parse tsd close flag [%u]", flag);
457 19 : tsdCloseFlag.quickCloseFlag = TSD_BITMAP_GET(flag, 0U);
458 19 : return;
459 : }
460 :
461 17 : TSD_StatusT TsdProcessController::ProcessQueueForAdc()
462 : {
463 17 : if ((sharedCtx_.platInfoMode != static_cast<uint32_t>(ModeType::OFFLINE)) || !sharedCtx_.isAdcEnv) {
464 16 : TSD_RUN_INFO(
465 : "[TsdClient] it is unnecessary for current mode[%u] to grant queue auth to aicpusd",
466 : static_cast<uint32_t>(ModeType::OFFLINE));
467 16 : return TSD_OK;
468 : }
469 1 : const auto ret = SyncQueueAuthority();
470 1 : if (ret != TSD_OK) {
471 1 : constexpr uint32_t flag = 0;
472 1 : const auto closeRet = Close(flag);
473 1 : if (closeRet != TSD_OK) {
474 0 : TSD_RUN_WARN("[TsdClient]Sync queue authority failed, call close failed!");
475 0 : return closeRet;
476 : }
477 1 : TSD_RUN_WARN("[TsdClient]Sync queue authority failed, call close success!");
478 1 : return ret;
479 : }
480 0 : return TSD_OK;
481 : }
482 :
483 7 : TSD_StatusT TsdProcessController::ProcessQueueGrant(
484 : const QueueQueryOutputPara& queueInfoOutBuff, const QueueQueryOutput* const queueInfoList,
485 : const pid_t aicpuPid) const
486 : {
487 7 : const uint32_t outLen = queueInfoOutBuff.outLen;
488 7 : if (outLen == 0U) {
489 0 : TSD_RUN_INFO("[TsdClient] Current process does not create queue yet. need not to sync");
490 0 : return TSD_OK;
491 : }
492 7 : if ((outLen % sizeof(queueInfoList->queQueryQuesOfProcInfo[0U])) != 0U) {
493 2 : TSD_ERROR("[TsdClient] QueueInfo outbuff size[%d] is invalid", outLen);
494 2 : return TSD_INTERNAL_ERROR;
495 : }
496 5 : const uint32_t queueNum = static_cast<uint32_t>(outLen / sizeof(queueInfoList->queQueryQuesOfProcInfo[0U]));
497 5 : TSD_RUN_INFO("[TsdClient] ProcessQueueGrant queueNum[%u]", queueNum);
498 8199 : for (uint32_t i = 0U; i < queueNum; ++i) {
499 8197 : const uint32_t queueId = static_cast<uint32_t>(queueInfoList->queQueryQuesOfProcInfo[i].qid);
500 8197 : if (queueId >= MAX_QUEUE_ID_NUM) {
501 2 : TSD_ERROR("Get invalid queueid[%d]", queueId);
502 2 : return TSD_INTERNAL_ERROR;
503 : }
504 8195 : if (!static_cast<bool>(queueInfoList->queQueryQuesOfProcInfo[i].attr.manage)) {
505 8193 : continue;
506 : }
507 4 : const auto drvRet = halQueueGrant(
508 2 : sharedCtx_.logicDeviceId, static_cast<int32_t>(queueId), aicpuPid,
509 : queueInfoList->queQueryQuesOfProcInfo[i].attr);
510 2 : if ((drvRet != DRV_ERROR_NONE) && (drvRet != DRV_ERROR_NOT_SUPPORT)) {
511 1 : TSD_ERROR("[TsdClient]Grant qid[%u] failed, ret[%d]", queueId, drvRet);
512 1 : return TSD_INTERNAL_ERROR;
513 : }
514 : }
515 2 : TSD_RUN_INFO("[TsdClient] ProcessQueueGrant process success");
516 2 : return TSD_OK;
517 : }
518 :
519 10 : TSD_StatusT TsdProcessController::SyncQueueAuthority() const
520 : {
521 10 : TSD_RUN_INFO("[TsdClient] Current type is ADC. Sync queue auth start.");
522 10 : auto drvRet = halQueueInit(sharedCtx_.logicDeviceId);
523 10 : if ((drvRet != DRV_ERROR_NONE) && (drvRet != DRV_ERROR_REPEATED_INIT) && (drvRet != DRV_ERROR_NOT_SUPPORT)) {
524 0 : TSD_ERROR("[TsdClient] halQueueInit error, drvRet[%d]", drvRet);
525 0 : return TSD_INTERNAL_ERROR;
526 : }
527 10 : TSD_RUN_INFO("[TsdClient]Interface halQueueInit success.");
528 10 : pid_t aicpuPid = -1;
529 10 : const auto ret = GetAicpusdPid(aicpuPid);
530 10 : if ((aicpuPid == -1) || (ret != TSD_OK)) {
531 2 : return ret;
532 : }
533 8 : pid_t srcPid = drvDeviceGetBareTgid();
534 8 : std::unique_ptr<QueueQueryOutput> queueInfoListPtr(new (std::nothrow) QueueQueryOutput());
535 8 : if (queueInfoListPtr == nullptr) {
536 0 : TSD_ERROR("[TsdClient] fail to alloc queueInfoListPtr");
537 0 : return TSD_INTERNAL_ERROR;
538 : }
539 8 : QueueQueryOutput* queueInfoList = queueInfoListPtr.get();
540 8 : if (queueInfoList == nullptr) {
541 0 : TSD_ERROR("[TsdClient] queueInfoList is null");
542 0 : return TSD_INTERNAL_ERROR;
543 : }
544 8 : QueueQueryOutputPara queueInfoOutBuff = {
545 8 : PtrToPtr<QueueQueryOutput, void>(queueInfoList), static_cast<uint32_t>(sizeof(QueueQueryOutput))};
546 8 : QueueQueryInputPara queueInput = {PtrToPtr<pid_t, void>(&srcPid), static_cast<uint32_t>(sizeof(srcPid))};
547 8 : drvRet = halQueueQuery(sharedCtx_.logicDeviceId, QUEUE_QUERY_QUES_OF_CUR_PROC, &queueInput, &queueInfoOutBuff);
548 8 : if (drvRet == DRV_ERROR_NOT_SUPPORT) {
549 0 : TSD_RUN_INFO("[TsdClient] halQueueQuery is not supported in current chip type.");
550 0 : return TSD_OK;
551 : }
552 8 : if (drvRet != DRV_ERROR_NONE) {
553 1 : TSD_ERROR("[TsdClient] halQueueQuery execute failed, ret[%d]", drvRet);
554 1 : return TSD_INTERNAL_ERROR;
555 : }
556 :
557 7 : return ProcessQueueGrant(queueInfoOutBuff, queueInfoList, aicpuPid);
558 8 : }
559 :
560 10 : TSD_StatusT TsdProcessController::GetAicpusdPid(pid_t& aicpusdPid) const
561 : {
562 10 : aicpusdPid = -1;
563 10 : const pid_t srcPid = drvDeviceGetBareTgid();
564 10 : if (&halQueryDevpid == nullptr) {
565 0 : TSD_RUN_INFO("[TsdClient] halQueryDevpid is nullptr, interface is not supported in current version.");
566 0 : return TSD_OK;
567 : }
568 10 : halQueryDevpidInfo para = {};
569 10 : para.hostpid = srcPid;
570 10 : para.proc_type = DEVDRV_PROCESS_CP1;
571 10 : para.vfid = 0U;
572 10 : para.devid = sharedCtx_.logicDeviceId;
573 10 : const auto drvRet = halQueryDevpid(para, &aicpusdPid);
574 10 : if (drvRet == DRV_ERROR_NOT_SUPPORT) {
575 1 : TSD_RUN_INFO("[TsdClient] halQueryDevpid is not supported in current chip type.");
576 1 : return TSD_OK;
577 : }
578 9 : if ((drvRet != DRV_ERROR_NONE) || (aicpusdPid == -1)) {
579 1 : TSD_ERROR("[TsdClient]Get aicpusd pid failed, result[%d]", drvRet);
580 1 : return TSD_INTERNAL_ERROR;
581 : }
582 8 : TSD_RUN_INFO(
583 : "Get aicpusd[%d] from host[%d] success.", static_cast<int32_t>(srcPid), static_cast<int32_t>(aicpusdPid));
584 8 : return TSD_OK;
585 : }
586 :
587 : } // namespace tsd
|