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