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 10 : uint32_t TsdOpen(const uint32_t logicDeviceId, const uint32_t rankSize)
17 : {
18 10 : 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 9 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
23 9 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for open.");
24 9 : const tsd::TSD_StatusT ret = clientManager->Open(rankSize);
25 9 : 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 9 : return ret;
32 9 : }
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 16 : uint32_t TsdClose(const uint32_t logicDeviceId)
68 : {
69 16 : 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 15 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
74 15 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for close.");
75 15 : const uint32_t flag = 0;
76 15 : const tsd::TSD_StatusT ret = clientManager->Close(flag);
77 15 : if (ret != tsd::TSD_OK) {
78 0 : TSD_ERROR("TsdClose failed, deviceId[%u].", logicDeviceId);
79 0 : clientManager->Destroy();
80 : }
81 15 : return ret;
82 15 : }
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 4 : uint32_t TsdInitFlowGw(const uint32_t logicDeviceId, const InitFlowGwInfo* const initInfo)
128 : {
129 4 : TSD_RUN_INFO("TsdInitFlowGw Begin.");
130 4 : 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 3 : if (initInfo == nullptr) {
135 1 : TSD_ERROR("TsdInitQs failed, initInfo is nullptr.");
136 1 : 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 3 : uint32_t GetHdcConctStatus(const uint32_t logicDeviceId, int32_t* hdcSessStat)
151 : {
152 3 : if (hdcSessStat == nullptr) {
153 1 : 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 6 : uint32_t TsdSetAttr(const char_t* const attrKey, const char_t* const attrValue)
165 : {
166 6 : tsd::TSD_StatusT ret = tsd::TSD_OK;
167 6 : if ((attrKey == nullptr) || (attrValue == nullptr)) {
168 2 : TSD_ERROR("[TsdClient] set attr failed, either key or value is null ptr!");
169 2 : 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 6 : uint32_t TsdCapabilityGet(const uint32_t logicDeviceId, const int32_t type, const uint64_t ptr)
187 : {
188 6 : TSD_RUN_INFO("TsdCapabilityGet Begin.");
189 6 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
190 1 : return tsd::TSD_OK;
191 : }
192 5 : if (type >= TSD_CAPABILITY_BUT) {
193 1 : TSD_ERROR("capability type is error");
194 1 : 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 3 : 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 3 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
214 1 : return tsd::TSD_OK;
215 : }
216 :
217 2 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
218 2 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_CLT_OPEN_FAILED, "Get ClientManager failed for TsdFileLoad.");
219 2 : const tsd::TSD_StatusT ret = clientManager->LoadFileToDevice(filePath, pathLen, fileName, fileNameLen);
220 2 : if (ret != tsd::TSD_OK) {
221 0 : TSD_ERROR("TsdFileLoad failed, pathLen[%llu], fileNameLen[%llu].", pathLen, fileNameLen);
222 : }
223 2 : return ret;
224 2 : }
225 :
226 8 : uint32_t TsdProcessOpen(const uint32_t logicDeviceId, ProcOpenArgs* openArgs)
227 : {
228 8 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
229 1 : return tsd::TSD_OK;
230 : }
231 :
232 7 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
233 7 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdProcessOpen.");
234 7 : const tsd::TSD_StatusT ret = clientManager->ProcessOpenSubProc(openArgs);
235 7 : if (ret != tsd::TSD_OK) {
236 1 : TSD_ERROR("TsdProcessOpen failed.");
237 : }
238 7 : return ret;
239 7 : }
240 :
241 3 : uint32_t TsdProcessClose(const uint32_t logicDeviceId, const pid_t closePid)
242 : {
243 3 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
244 1 : return tsd::TSD_OK;
245 : }
246 :
247 2 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
248 2 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdProcessClose.");
249 2 : const tsd::TSD_StatusT ret = clientManager->ProcessCloseSubProc(closePid);
250 2 : if (ret != tsd::TSD_OK) {
251 0 : TSD_ERROR("TsdProcessClose failed");
252 : }
253 2 : return ret;
254 2 : }
255 :
256 3 : uint32_t TsdFileUnLoad(const uint32_t logicDeviceId, const char_t* filePath, const uint64_t pathLen)
257 : {
258 3 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
259 1 : return tsd::TSD_OK;
260 : }
261 :
262 2 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
263 2 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdFileUnLoad.");
264 2 : const tsd::TSD_StatusT ret = clientManager->RemoveFileOnDevice(filePath, pathLen);
265 2 : if (ret != tsd::TSD_OK) {
266 0 : TSD_ERROR("TsdFileUnLoad failed");
267 : }
268 2 : return ret;
269 2 : }
270 :
271 2 : uint32_t TsdGetProcStatus(const uint32_t logicDeviceId, ProcStatusInfo* pidInfo, const uint32_t arrayLen)
272 : {
273 2 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
274 1 : return tsd::TSD_OK;
275 : }
276 :
277 1 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
278 1 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdGetProcStatus.");
279 1 : const tsd::TSD_StatusT ret = clientManager->GetSubProcStatus(pidInfo, arrayLen);
280 1 : if (ret != tsd::TSD_OK) {
281 0 : TSD_ERROR("TsdGetProcStatus failed");
282 : }
283 1 : return ret;
284 1 : }
285 :
286 1 : uint32_t NotifyPmToStartTsdaemon(const uint32_t logicDeviceId)
287 : {
288 : (void)logicDeviceId;
289 1 : return tsd::TSD_PARAMETER_INVALID;
290 : }
291 :
292 2 : uint32_t ProcessCloseSubProcList(
293 : const uint32_t logicDeviceId, const ProcStatusParam* closeList, const uint32_t listSize)
294 : {
295 2 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
296 0 : return tsd::TSD_OK;
297 : }
298 :
299 2 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
300 2 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for ProcessCloseSubProcList.");
301 2 : const tsd::TSD_StatusT ret = clientManager->ProcessCloseSubProcList(closeList, listSize);
302 2 : if ((ret != tsd::TSD_OK) && (ret != tsd::TSD_HDC_CLIENT_CLOSED_EXTERNAL)) {
303 0 : TSD_ERROR("ProcessCloseSubProcList failed");
304 : }
305 2 : return ret;
306 2 : }
307 :
308 1 : uint32_t TsdGetProcListStatus(const uint32_t logicDeviceId, ProcStatusParam* pidInfo, const uint32_t arrayLen)
309 : {
310 1 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
311 0 : return tsd::TSD_OK;
312 : }
313 :
314 1 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
315 1 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdGetProcListStatus.");
316 1 : const tsd::TSD_StatusT ret = clientManager->GetSubProcListStatus(pidInfo, arrayLen);
317 1 : if (ret != tsd::TSD_OK) {
318 0 : TSD_ERROR("TsdGetProcListStatus failed");
319 : }
320 1 : return ret;
321 1 : }
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 2 : uint32_t TsdCloseNetService(const uint32_t logicDeviceId)
342 : {
343 2 : if (tsd::ClientManager::CheckDestructFlag(logicDeviceId)) {
344 0 : return tsd::TSD_OK;
345 : }
346 :
347 2 : const std::shared_ptr<tsd::ClientManager> clientManager = tsd::ClientManager::GetInstance(logicDeviceId);
348 2 : TSD_CHECK_NULLPTR(clientManager, tsd::TSD_INTERNAL_ERROR, "Get ClientManager failed for TsdCloseNetService.");
349 2 : const tsd::TSD_StatusT ret = clientManager->CloseNetService();
350 2 : if (ret != tsd::TSD_OK) {
351 2 : TSD_ERROR("TsdCloseNetService failed");
352 : }
353 2 : return ret;
354 2 : }
|