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 : #include "tsd/tsd_client.h"
11 : #include "tsd_log.h"
12 : #include "inc/client_manager.h"
13 : #include "tsd_msg_parse_reg.h"
14 : #include "tsd_util_func.h"
15 :
16 11 : uint32_t TsdOpen(const uint32_t logicDeviceId, const uint32_t rankSize)
17 : {
18 11 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
19 : // CheckDestructFlag true cannot print log, resource(errDesc_) for tdt log may be destructed.
20 1 : return tsd::TSD_OK;
21 : }
22 10 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
23 10 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for open.");
24 10 : const tsd::TSD_StatusT ret = clientManager->Open(rankSize);
25 10 : if (ret != tsd::TSD_OK) {
26 0 : if (!clientManager->IsAdcEnv()) {
27 0 : TSD_ERROR("TsdOpen failed, deviceId[%u].", logicDeviceId);
28 : }
29 0 : clientManager->Destroy();
30 : }
31 10 : return ret;
32 10 : }
33 :
34 5 : uint32_t TsdOpenEx(const uint32_t logicDeviceId, const uint32_t rankSize, const uint32_t deviceMode)
35 : {
36 5 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
37 : // CheckDestructFlag true cannot print log, resource(errDesc_) for tdt log may be destructed.
38 1 : return tsd::TSD_OK;
39 : }
40 : const ::std::shared_ptr<tsd::ClientManager> clientManager =
41 4 : tsd::ClientManager::GetInstance(logicDeviceId, deviceMode);
42 4 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for open.");
43 4 : const tsd::TSD_StatusT ret = clientManager->Open(rankSize);
44 4 : if (ret != tsd::TSD_OK) {
45 0 : TSD_ERROR("TsdOpenEx failed, deviceId[%u].", logicDeviceId);
46 0 : clientManager->Destroy();
47 : }
48 4 : return ret;
49 4 : }
50 :
51 2 : uint32_t TsdOpenAicpuSd(const uint32_t logicDeviceId)
52 : {
53 2 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
54 : // CheckDestructFlag true cannot print log, resource(errDesc_) for tdt log may be destructed.
55 1 : return tsd::TSD_OK;
56 : }
57 1 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
58 1 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for openAicpuSd.");
59 1 : const tsd::TSD_StatusT ret = clientManager->OpenAicpuSd();
60 1 : if (ret != tsd::TSD_OK) {
61 0 : TSD_ERROR("TsdOpenAicpuSd failed, deviceId[%u].", logicDeviceId);
62 0 : clientManager->Destroy();
63 : }
64 1 : return ret;
65 1 : }
66 :
67 17 : uint32_t TsdClose(const uint32_t logicDeviceId)
68 : {
69 17 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
70 : // CheckDestructFlag true cannot print log, resource(errDesc_) for tdt log may be destructed.
71 1 : return tsd::TSD_OK;
72 : }
73 16 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
74 16 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for close.");
75 16 : const uint32_t flag = 0;
76 16 : const tsd::TSD_StatusT ret = clientManager->Close(flag);
77 16 : if (ret != tsd::TSD_OK) {
78 0 : TSD_ERROR("TsdClose failed, deviceId[%u].", logicDeviceId);
79 0 : clientManager->Destroy();
80 : }
81 16 : return ret;
82 16 : }
83 :
84 2 : uint32_t TsdCloseEx(const uint32_t logicDeviceId, const uint32_t flag)
85 : {
86 2 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
87 : // CheckDestructFlag true cannot print log, resource(errDesc_) for tdt log may be destructed.
88 1 : return tsd::TSD_OK;
89 : }
90 1 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
91 1 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_CLOSEEX_FAILED, "Get ClientManager failed for close.");
92 1 : const tsd::TSD_StatusT ret = clientManager->Close(flag);
93 1 : if (ret != tsd::TSD_OK) {
94 0 : TSD_ERROR("TsdCloseEx failed, deviceId[%u], flag[%u].", logicDeviceId, flag);
95 0 : clientManager->Destroy();
96 : }
97 1 : return ret;
98 1 : }
99 :
100 2 : uint32_t UpdateProfilingMode(const uint32_t logicDeviceId, const uint32_t flag)
101 : {
102 2 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
103 : // CheckDestructFlag true cannot print log, resource(errDesc_) for tdt log may be destructed.
104 1 : return tsd::TSD_OK;
105 : }
106 1 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
107 1 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for UpdateProfilingMode.");
108 1 : const tsd::TSD_StatusT ret = clientManager->UpdateProfilingConf(flag);
109 1 : if (ret != tsd::TSD_OK) {
110 0 : TSD_ERROR("UpdateProfilingMode failed, deviceId[%u].", logicDeviceId);
111 : }
112 1 : return ret;
113 1 : }
114 :
115 2 : uint32_t TsdSetMsprofReporterCallback(const MsprofReporterCallback callback)
116 : {
117 2 : tsd::ClientManager::SetProfilingCallback(callback);
118 2 : return tsd::TSD_OK;
119 : }
120 :
121 1 : uint32_t TsdInitQs(const uint32_t logicDeviceId, const char_t* const groupName)
122 : {
123 1 : const InitFlowGwInfo tmpFlowGwInfo = {groupName, 0U, 0U, {}};
124 2 : return TsdInitFlowGw(logicDeviceId, &tmpFlowGwInfo);
125 : }
126 :
127 5 : uint32_t TsdInitFlowGw(const uint32_t logicDeviceId, const InitFlowGwInfo* const initInfo)
128 : {
129 5 : TSD_RUN_INFO("TsdInitFlowGw Begin.");
130 5 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
131 : // CheckDestructFlag true cannot print log, resource(errDesc_) for tdt log may be destructed.
132 1 : return tsd::TSD_OK;
133 : }
134 4 : if (initInfo == nullptr) {
135 2 : TSD_ERROR("TsdInitQs failed, initInfo is nullptr.");
136 2 : return tsd::TSD_INTERNAL_ERROR;
137 : }
138 :
139 : // get client instance
140 2 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
141 2 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for TsdInitQs.");
142 2 : const tsd::TSD_StatusT ret = clientManager->InitQs(initInfo);
143 2 : if (ret != tsd::TSD_OK) {
144 0 : TSD_ERROR("TsdInitQs failed, deviceId[%u]", logicDeviceId);
145 0 : clientManager->Destroy();
146 : }
147 2 : return ret;
148 2 : }
149 :
150 4 : uint32_t GetHdcConctStatus(const uint32_t logicDeviceId, int32_t* hdcSessStat)
151 : {
152 4 : if (hdcSessStat == nullptr) {
153 2 : return tsd::TSD_OK;
154 : }
155 3 : if ((tsd::ClientManager::CheckDestructFlag(logicDeviceId)) ||
156 3 : (tsd::ClientManager::GetInstance(logicDeviceId) == nullptr)) {
157 : // CheckDestructFlag true cannot print log, resource(errDesc_) for tdt log may be destructed.
158 1 : *hdcSessStat = HDC_SESSION_STATUS_CONNECT;
159 1 : return tsd::TSD_OK;
160 : }
161 1 : return tsd::ClientManager::GetInstance(logicDeviceId)->GetHdcConctStatus(*hdcSessStat);
162 : }
163 :
164 8 : uint32_t TsdSetAttr(const char_t* const attrKey, const char_t* const attrValue)
165 : {
166 8 : tsd::TSD_StatusT ret = tsd::TSD_OK;
167 8 : if ((attrKey == nullptr) || (attrValue == nullptr)) {
168 4 : TSD_ERROR("[TsdClient] set attr failed, either key or value is null ptr!");
169 4 : return tsd::TSD_INTERNAL_ERROR;
170 : }
171 8 : const std::string keyStr(attrKey);
172 4 : const std::string valueStr(attrValue);
173 4 : if (keyStr == "RunMode") {
174 1 : ret = tsd::ClientManager::SetRunMode(valueStr);
175 : } else {
176 3 : TSD_RUN_INFO("[TsdClient] set attr is not supported, key is [%s].", keyStr.c_str());
177 : }
178 4 : return ret;
179 4 : }
180 :
181 1 : uint32_t SetAicpuSchedMode(const uint32_t schedMode)
182 : {
183 1 : return static_cast<uint32_t>(tsd::ClientManager::SetAicpuSchedMode(schedMode));
184 : }
185 :
186 7 : uint32_t TsdCapabilityGet(const uint32_t logicDeviceId, const int32_t type, const uint64_t ptr)
187 : {
188 7 : TSD_RUN_INFO("TsdCapabilityGet Begin.");
189 7 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
190 1 : return tsd::TSD_OK;
191 : }
192 6 : if (type >= TSD_CAPABILITY_BUT) {
193 2 : TSD_ERROR("capability type is error");
194 2 : return tsd::TSD_CLT_OPEN_FAILED;
195 : }
196 :
197 4 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
198 4 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for TsdCapabilityGet.");
199 4 : const tsd::TSD_StatusT ret = clientManager->CapabilityGet(type, ptr);
200 4 : if (ret != tsd::TSD_OK) {
201 0 : TSD_ERROR("TsdCapabilityGet failed, type[%d].", type);
202 0 : if (type != TSD_CAPABILITY_LEVEL) {
203 0 : clientManager->Destroy();
204 : }
205 : }
206 4 : return ret;
207 4 : }
208 :
209 1 : uint32_t TsdFileLoad(
210 : const uint32_t logicDeviceId, const char_t* filePath, const uint64_t pathLen, const char_t* fileName,
211 : const uint64_t fileNameLen)
212 : {
213 1 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
214 1 : return tsd::TSD_OK;
215 : }
216 :
217 0 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
218 0 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for TsdFileLoad.");
219 0 : const tsd::TSD_StatusT ret = clientManager->LoadFileToDevice(filePath, pathLen, fileName, fileNameLen);
220 0 : if (ret != tsd::TSD_OK) {
221 0 : TSD_ERROR("TsdFileLoad failed, pathLen[%llu], fileNameLen[%llu].", pathLen, fileNameLen);
222 : }
223 0 : return ret;
224 0 : }
225 :
226 11 : uint32_t TsdProcessOpen(const uint32_t logicDeviceId, ProcOpenArgs* openArgs)
227 : {
228 11 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
229 1 : return tsd::TSD_OK;
230 : }
231 :
232 10 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
233 10 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdProcessOpen.");
234 10 : const tsd::TSD_StatusT ret = clientManager->ProcessOpenSubProc(openArgs);
235 10 : if (ret != tsd::TSD_OK) {
236 1 : TSD_ERROR("TsdProcessOpen failed.");
237 : }
238 10 : return ret;
239 10 : }
240 :
241 4 : uint32_t TsdProcessClose(const uint32_t logicDeviceId, const pid_t closePid)
242 : {
243 4 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
244 1 : return tsd::TSD_OK;
245 : }
246 :
247 3 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
248 3 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdProcessClose.");
249 3 : const tsd::TSD_StatusT ret = clientManager->ProcessCloseSubProc(closePid);
250 3 : if (ret != tsd::TSD_OK) {
251 0 : TSD_ERROR("TsdProcessClose failed");
252 : }
253 3 : return ret;
254 3 : }
255 :
256 1 : uint32_t TsdFileUnLoad(const uint32_t logicDeviceId, const char_t* filePath, const uint64_t pathLen)
257 : {
258 1 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
259 1 : return tsd::TSD_OK;
260 : }
261 :
262 0 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
263 0 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdFileUnLoad.");
264 0 : const tsd::TSD_StatusT ret = clientManager->RemoveFileOnDevice(filePath, pathLen);
265 0 : if (ret != tsd::TSD_OK) {
266 0 : TSD_ERROR("TsdFileUnLoad failed");
267 : }
268 0 : return ret;
269 0 : }
270 :
271 3 : uint32_t TsdGetProcStatus(const uint32_t logicDeviceId, ProcStatusInfo* pidInfo, const uint32_t arrayLen)
272 : {
273 3 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
274 1 : return tsd::TSD_OK;
275 : }
276 :
277 2 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
278 2 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdGetProcStatus.");
279 2 : const tsd::TSD_StatusT ret = clientManager->GetSubProcStatus(pidInfo, arrayLen);
280 2 : if (ret != tsd::TSD_OK) {
281 0 : TSD_ERROR("TsdGetProcStatus failed");
282 : }
283 2 : return ret;
284 2 : }
285 :
286 1 : uint32_t NotifyPmToStartTsdaemon(const uint32_t logicDeviceId)
287 : {
288 : (void)logicDeviceId;
289 1 : return tsd::TSD_PARAMETER_INVALID;
290 : }
291 :
292 3 : uint32_t ProcessCloseSubProcList(
293 : const uint32_t logicDeviceId, const ProcStatusParam* closeList, const uint32_t listSize)
294 : {
295 3 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
296 0 : return tsd::TSD_OK;
297 : }
298 :
299 3 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
300 3 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for ProcessCloseSubProcList.");
301 3 : const tsd::TSD_StatusT ret = clientManager->ProcessCloseSubProcList(closeList, listSize);
302 3 : if ((ret != tsd::TSD_OK) && (ret != tsd::TSD_HDC_CLIENT_CLOSED_EXTERNAL)) {
303 0 : TSD_ERROR("ProcessCloseSubProcList failed");
304 : }
305 3 : return ret;
306 3 : }
307 :
308 2 : uint32_t TsdGetProcListStatus(const uint32_t logicDeviceId, ProcStatusParam* pidInfo, const uint32_t arrayLen)
309 : {
310 2 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
311 0 : return tsd::TSD_OK;
312 : }
313 :
314 2 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
315 2 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdGetProcListStatus.");
316 2 : const tsd::TSD_StatusT ret = clientManager->GetSubProcListStatus(pidInfo, arrayLen);
317 2 : if (ret != tsd::TSD_OK) {
318 0 : TSD_ERROR("TsdGetProcListStatus failed");
319 : }
320 2 : return ret;
321 2 : }
322 :
323 2 : uint32_t TsdOpenNetService(const uint32_t logicDeviceId, const NetServiceOpenArgs* args)
324 : {
325 2 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
326 0 : return tsd::TSD_OK;
327 : }
328 :
329 2 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
330 2 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdOpenNetService.");
331 2 : const tsd::TSD_StatusT ret = clientManager->OpenNetService(args);
332 2 : if (ret != tsd::TSD_OK) {
333 0 : if (ret == tsd::ErroCodeExt::TSD_OPEN_DEFAULT_NET_SERVICE_FAILED) {
334 0 : clientManager->Destroy();
335 : }
336 0 : TSD_ERROR("TsdOpenNetService failed");
337 : }
338 2 : return ret;
339 2 : }
340 :
341 1 : uint32_t TsdCloseNetService(const uint32_t logicDeviceId)
342 : {
343 1 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
344 0 : return tsd::TSD_OK;
345 : }
346 :
347 1 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
348 1 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdCloseNetService.");
349 1 : const tsd::TSD_StatusT ret = clientManager->CloseNetService();
350 1 : if (ret != tsd::TSD_OK) {
351 1 : TSD_ERROR("TsdCloseNetService failed");
352 : }
353 1 : return ret;
354 1 : }
|