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 "hwts_kernel_cust_so.h"
12 :
13 : #include "aicpusd_cust_so_manager.h"
14 : #include "aicpusd_drv_manager.h"
15 : #include "hwts_kernel_common.h"
16 :
17 : namespace AicpuSchedule {
18 : namespace {
19 : const std::string LOADOP_FROM_BUFF = "loadOpFromBuf";
20 : const std::string BATCH_LOADSO_FROM_BUFF = "batchLoadsoFrombuf";
21 : const std::string DELETE_CUSTOP = "deleteCustOp";
22 :
23 : constexpr uint32_t MAX_CUSTOM_SO_NUM = 1024U;
24 : constexpr uint32_t CCPU_DEFAULT_GROUP_ID = 30U;
25 : constexpr GroupShareAttr GROUP_WITH_ALL_ATTR = {1U, 1U, 1U, 1U, 0U}; // admin + read + write + alloc
26 : } // namespace
27 :
28 :
29 : std::mutex LoadOpFromBuffTsKernel::mutexForStartCustProcess_;
30 : std::set<std::string> LoadOpFromBuffTsKernel::alreadyLoadSoName_;
31 :
32 5 : int32_t CustOperationCommon::SendCtrlCpuMsg(
33 : int32_t custAicpuPid, const uint32_t eventType, char_t *msg, const uint32_t msgLen) const
34 : {
35 5 : event_summary eventInfoSummary = {};
36 5 : eventInfoSummary.pid = custAicpuPid;
37 5 : eventInfoSummary.event_id = EVENT_CCPU_CTRL_MSG;
38 5 : eventInfoSummary.subevent_id = eventType;
39 5 : eventInfoSummary.msg = msg;
40 5 : eventInfoSummary.msg_len = msgLen;
41 5 : eventInfoSummary.grp_id = CCPU_DEFAULT_GROUP_ID;
42 5 : eventInfoSummary.dst_engine = CCPU_DEVICE;
43 5 : DeployContext deployCtx = DeployContext::DEVICE;
44 5 : (void)GetAicpuDeployContext(deployCtx);
45 5 : if (deployCtx == DeployContext::HOST) {
46 0 : eventInfoSummary.dst_engine = static_cast<uint32_t>(CCPU_HOST);
47 0 : aicpusd_info("SendCtrlCpuMsg to dst engine: %u", eventInfoSummary.dst_engine);
48 : }
49 5 : const drvError_t ret = halEschedSubmitEvent(AicpuDrvManager::GetInstance().GetDeviceId(),
50 : &eventInfoSummary);
51 5 : if (ret != DRV_ERROR_NONE) {
52 0 : aicpusd_err("Failed to submit aicpu event. ret is %d.", ret);
53 0 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
54 : }
55 5 : aicpusd_info("SendCtrlCpuMsg success type:%u", eventType);
56 5 : return AICPU_SCHEDULE_OK;
57 : }
58 :
59 10 : int32_t CustOperationCommon::GetGroupNameInfo(std::vector<std::string> &groupNameList, std::string &groupNameStr) const
60 : {
61 10 : const pid_t curPid = drvDeviceGetBareTgid();
62 10 : std::map<std::string, GroupShareAttr> buffGrpInfo = {};
63 10 : const int32_t ret = AicpuDrvManager::GetInstance().QueryProcBuffInfo(static_cast<uint32_t>(curPid), buffGrpInfo);
64 10 : if (ret != AICPU_SCHEDULE_OK) {
65 1 : aicpusd_err("Fail to get group info of acipusd[%d]", curPid);
66 1 : return ret;
67 : }
68 :
69 25 : for (const auto &groupInfo : buffGrpInfo) {
70 16 : if (groupInfo.second.admin == 1U) {
71 9 : groupNameList.emplace_back(groupInfo.first);
72 9 : groupNameStr = groupNameStr + groupInfo.first + ",";
73 9 : aicpusd_info("Get aicpusd buff group[%s] succ.", groupInfo.first.c_str());
74 : }
75 : }
76 :
77 9 : return AICPU_SCHEDULE_OK;
78 10 : }
79 :
80 10 : int32_t CustOperationCommon::StartCustProcess(
81 : const uint32_t loadLibNum, const char_t *const loadLibName[]) const
82 : {
83 : // 01-Get group name of current process
84 10 : std::vector<std::string> groupNameList = {};
85 10 : std::string groupNameStr = "";
86 10 : const int32_t ret = GetGroupNameInfo(groupNameList, groupNameStr);
87 10 : if (ret != AICPU_SCHEDULE_OK) {
88 1 : return ret;
89 : }
90 :
91 9 : const uint32_t validGroupNum = static_cast<uint32_t>(groupNameList.size());
92 : // Remove comma at the end of the string
93 9 : groupNameStr = ((validGroupNum != 0U) ? groupNameStr.substr(0UL, groupNameStr.length() - 1UL) : groupNameStr);
94 : // 02-startup cust aicpusd and get cust aicpusd pid
95 9 : int32_t custAicpuPid = -1;
96 9 : bool firstStart = true;
97 9 : int32_t tdtStatus = 0;
98 9 : if (&CreateOrFindCustPidEx != nullptr) {
99 0 : aicpusd_info("Create or find custom pid by CreateOrFindCustPidEx.");
100 0 : CustAicpuPara para = {};
101 0 : para.paraVersion = static_cast<uint32_t>(CustAicpuParaVersion::CUST_AICPU_PARA_WITHOUT_SO_INFO);
102 0 : para.infoPara.paraWithoutSoInfo.deviceId = AicpuDrvManager::GetInstance().GetDeviceId();
103 0 : para.infoPara.paraWithoutSoInfo.hostPid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
104 0 : para.infoPara.paraWithoutSoInfo.vfId = AicpuDrvManager::GetInstance().GetVfId();
105 0 : para.infoPara.paraWithoutSoInfo.groupNameList = groupNameStr.c_str();
106 0 : para.infoPara.paraWithoutSoInfo.groupNameNum = validGroupNum;
107 0 : para.infoPara.paraWithoutSoInfo.custProcPid = &custAicpuPid;
108 0 : para.infoPara.paraWithoutSoInfo.firstStart = &firstStart;
109 0 : tdtStatus = CreateOrFindCustPidEx(¶);
110 : } else {
111 9 : aicpusd_info("Create or find custom pid by CreateOrFindCustPid, load so event will be send to tsd.");
112 18 : tdtStatus = CreateOrFindCustPid(AicpuDrvManager::GetInstance().GetDeviceId(), loadLibNum,
113 9 : loadLibName, static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid()),
114 9 : AicpuDrvManager::GetInstance().GetVfId(), groupNameStr.c_str(), validGroupNum, &custAicpuPid, &firstStart);
115 : }
116 9 : if ((tdtStatus != 0) || (custAicpuPid < 0)) {
117 4 : aicpusd_err("Call tdt interface to create or find custom process pid failed, error[%d] pid[%d].",
118 : tdtStatus, custAicpuPid);
119 4 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
120 : }
121 :
122 5 : AicpuScheduleInterface::GetInstance().SetAicpuCustSdProcId(custAicpuPid);
123 5 : if (!firstStart) {
124 1 : aicpusd_run_info("Cust aicpu have already existed");
125 1 : return AICPU_SCHEDULE_OK;
126 : }
127 :
128 : // 03-add group for cust aicpu
129 8 : for (const auto &grpName : groupNameList) {
130 4 : const auto drvRet = halGrpAddProc(grpName.c_str(), custAicpuPid, GROUP_WITH_ALL_ATTR);
131 4 : if (drvRet != DRV_ERROR_NONE) {
132 0 : aicpusd_err("Add group[%s] for cust aicpusd failed, ret[%d]", grpName.c_str(), drvRet);
133 0 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
134 : }
135 : }
136 4 : aicpusd_run_info("halGrpAddProc success, custAicpuPid[%d], validGroupNum[%u]", custAicpuPid, validGroupNum);
137 :
138 4 : if (validGroupNum > 0U) {
139 4 : if (SendCtrlCpuMsg(
140 4 : custAicpuPid, static_cast<uint32_t>(TsdSubEventType::TSD_EVENT_STOP_SUB_PROCESS_WAIT), nullptr, 0U) !=
141 : AICPU_SCHEDULE_OK) {
142 0 : aicpusd_run_info("SendCtrlCpuMsg send to custaicpu no success");
143 : }
144 4 : aicpusd_run_info("SendCtrlCpuMsg Send to custaicpu success");
145 4 : (void)StopWaitForCustAicpu();
146 4 : aicpusd_run_info("wait custaicpu attach group success");
147 : }
148 4 : if (&CreateOrFindCustPidEx != nullptr) {
149 0 : aicpusd_run_info("Begin to notify open custom so event to cust ctrl cpu, custAicpuPid[%d]", custAicpuPid);
150 0 : int32_t notifyRet = AicpuNotifyLoadSoEventToCustCtrlCpu(AicpuDrvManager::GetInstance().GetDeviceId(),
151 0 : static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid()),
152 0 : AicpuDrvManager::GetInstance().GetVfId(), custAicpuPid, loadLibNum, loadLibName);
153 0 : if (notifyRet != 0) {
154 0 : aicpusd_warn("Aicpu notify open custom so event to cust ctrl aicpu failed, error[%d] pid[%d].",
155 : notifyRet, custAicpuPid);
156 : }
157 : }
158 :
159 4 : aicpusd_run_info("StartCustProcess end");
160 4 : return AICPU_SCHEDULE_OK;
161 10 : }
162 :
163 4 : int32_t CustOperationCommon::AicpuNotifyLoadSoEventToCustCtrlCpu(const uint32_t deviceId, const uint32_t hostPid,
164 : const uint32_t vfId, const int32_t custAicpuPid,
165 : const uint32_t loadLibNum,
166 : const char_t * const loadLibName[]) const
167 : {
168 4 : if ((loadLibNum > 0U) && (loadLibName == nullptr)) {
169 1 : aicpusd_err("Load lib name is nullptr, loadLibNum=%u, hostPid=%u", loadLibNum, hostPid);
170 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
171 : }
172 5 : for (uint32_t i = 0U; i < loadLibNum; i++) {
173 3 : TsdSubEventInfo info = { };
174 3 : info.deviceId = deviceId;
175 3 : info.srcPid = static_cast<uint32_t>(getpid());
176 3 : info.dstPid = custAicpuPid;
177 3 : info.hostPid = hostPid;
178 3 : info.vfId = vfId;
179 3 : info.procType = 1U;
180 3 : info.eventType = static_cast<uint32_t>(AICPU_SUB_EVENT_OPEN_CUSTOM_SO);
181 3 : const errno_t errRet = strcpy_s(info.priMsg, MAX_EVENT_PRI_MSG_LENGTH, loadLibName[i]);
182 3 : if (errRet != EOK) {
183 1 : aicpusd_err("Copy %s failed, error[%d]", loadLibName[i], errRet);
184 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
185 : }
186 2 : event_summary event = { };
187 2 : event.dst_engine = CCPU_DEVICE;
188 2 : event.policy = ONLY;
189 2 : event.pid = custAicpuPid;
190 2 : event.grp_id = 30U;
191 2 : event.event_id = EVENT_CCPU_CTRL_MSG;
192 2 : event.subevent_id = AICPU_SUB_EVENT_OPEN_CUSTOM_SO;
193 2 : event.msg = PtrToPtr<TsdSubEventInfo, char_t>(&info);
194 2 : event.msg_len = static_cast<uint32_t>(sizeof(TsdSubEventInfo));
195 2 : aicpusd_info("msg[%s], msglen[%u]", event.msg, event.msg_len);
196 2 : const drvError_t ret = halEschedSubmitEvent(deviceId, &event);
197 2 : if (ret != DRV_ERROR_NONE) {
198 1 : aicpusd_run_warn("Sending open custom so event to custom ctrl cpu was not successful, deviceId[%u], "
199 : "errcode[%d], please check so name length", deviceId, ret);
200 : } else {
201 1 : aicpusd_info("Aicpu send open custom so event to custom ctrl cpu success.");
202 : }
203 2 : aicpusd_run_info("Notify custom soName info on deviceId[%u] hostpid[%u] vfId[%u] index[%u] soName[%s] success.",
204 : deviceId, hostPid, vfId, i, loadLibName[i]);
205 : }
206 2 : aicpusd_run_info("End AicpuNotifyLoadSoEventToCustCtrlCpu on deviceId[%u] hostPid[%u] vfId[%u] soNum[%u].",
207 : deviceId, hostPid, vfId, loadLibNum);
208 2 : return AICPU_SCHEDULE_OK;
209 : }
210 :
211 8 : int32_t LoadOpFromBuffTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
212 : {
213 8 : aicpusd_run_info("Begin to process ts kernel loadOpFromBuf");
214 8 : if (!AicpuCustSoManager::GetInstance().IsSupportCustAicpu()) {
215 1 : aicpusd_err("Cust aicpu scheduler is not supported in message queue mode.");
216 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
217 : }
218 :
219 : const auto paramKernelName =
220 7 : PtrToPtr<void, char_t>(ValueToPtr(tsKernelInfo.kernelBase.cceKernel.kernelName));
221 7 : if (paramKernelName == nullptr) {
222 0 : aicpusd_err("Process LoadOpFromBuf failed as the name of kernel is null.");
223 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
224 : }
225 :
226 7 : std::string realSoName;
227 : const auto loadOpFromBufArgs =
228 7 : PtrToPtr<void, LoadOpFromBufArgs>(ValueToPtr(tsKernelInfo.kernelBase.cceKernel.paramBase));
229 7 : const int32_t ret = AicpuCustSoManager::GetInstance().CheckAndCreateSoFile(loadOpFromBufArgs, realSoName);
230 7 : if (ret != AICPU_SCHEDULE_OK) {
231 4 : return ret;
232 : }
233 :
234 : // if have cust pid, create or find cust pid
235 : uint32_t runMode;
236 3 : const aicpu::status_t status = aicpu::GetAicpuRunMode(runMode);
237 3 : if (status != aicpu::AICPU_ERROR_NONE) {
238 0 : aicpusd_err("Get current aicpu ctx failed.");
239 0 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
240 : }
241 :
242 3 : if (runMode != aicpu::AicpuRunMode::THREAD_MODE) {
243 3 : const std::lock_guard<std::mutex> lk(mutexForStartCustProcess_);
244 3 : constexpr uint32_t soNum = 1U;
245 3 : const char_t *soNames[soNum] = {realSoName.data()};
246 3 : auto it = alreadyLoadSoName_.insert(realSoName);
247 3 : if (it.second == true) {
248 2 : const int32_t startRet = StartCustProcess(soNum, &soNames[0UL]);
249 2 : if (startRet != AICPU_SCHEDULE_OK) {
250 1 : aicpusd_err("Get aicpu group, start up and add group to cust aicpu failed.");
251 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
252 : }
253 : } else {
254 1 : aicpusd_run_info("[%s] is already loaded.", realSoName.c_str());
255 : }
256 3 : }
257 :
258 2 : aicpusd_run_info("End to process ts LoadOpFromBuf event.");
259 2 : return AICPU_SCHEDULE_OK;
260 7 : }
261 :
262 8 : int32_t BatchLoadSoFromBuffTsKernel::TryToStartCustProcess(
263 : const std::unique_ptr<const char_t *[]> &soNames, const uint32_t soNum) const
264 : {
265 : // if have cust pid, create or find cust pid
266 8 : uint32_t runMode = 0U;
267 8 : const aicpu::status_t status = aicpu::GetAicpuRunMode(runMode);
268 8 : if (status != aicpu::AICPU_ERROR_NONE) {
269 0 : aicpusd_err("Get current aicpu ctx failed.");
270 0 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
271 : }
272 :
273 8 : if (runMode != aicpu::AicpuRunMode::THREAD_MODE) {
274 8 : const int32_t ret = StartCustProcess(soNum, soNames.get());
275 8 : if (ret != AICPU_SCHEDULE_OK) {
276 5 : aicpusd_err("Get aicpu group, start up and add group to cust aicpu failed.");
277 5 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
278 : }
279 : }
280 :
281 3 : return AICPU_SCHEDULE_OK;
282 : }
283 :
284 13 : int32_t BatchLoadSoFromBuffTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
285 : {
286 13 : aicpusd_info("Begin to process ts kernel BatchLoadOpFromBuf event.");
287 13 : if (!AicpuCustSoManager::GetInstance().IsSupportCustAicpu()) {
288 1 : aicpusd_err("Cust aicpu scheduler is not supported in message queue mode.");
289 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
290 : }
291 :
292 : const auto paramKernelName =
293 12 : PtrToPtr<void, char_t>(ValueToPtr(tsKernelInfo.kernelBase.cceKernel.kernelName));
294 12 : if (paramKernelName == nullptr) {
295 0 : aicpusd_err("Process BatchLoadOpFromBuf failed as the name of kernel is null.");
296 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
297 : }
298 :
299 : const auto batchLoadOpFromBufArgs =
300 12 : PtrToPtr<void, BatchLoadOpFromBufArgs>(ValueToPtr(tsKernelInfo.kernelBase.cceKernel.paramBase));
301 12 : if (batchLoadOpFromBufArgs == nullptr) {
302 0 : aicpusd_err("param base for batch load op from buffer is null.");
303 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
304 : }
305 :
306 12 : const uint64_t custSoInfo = static_cast<uint64_t>(batchLoadOpFromBufArgs->opInfoArgs);
307 12 : const uint32_t soNum = static_cast<uint32_t>(batchLoadOpFromBufArgs->soNum);
308 : // check so num
309 12 : if ((soNum == 0U) || (soNum > MAX_CUSTOM_SO_NUM)) {
310 1 : aicpusd_err("BatchLoadOpFromBuf kernel input param soNum is invalid. soNum value is %u.", soNum);
311 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
312 : }
313 :
314 11 : aicpusd_info("BatchLoadOpFromBuf soNum is %u.", soNum);
315 11 : const std::unique_ptr<const char_t *[]> soNames(new (std::nothrow) const char_t *[soNum]);
316 11 : if (soNames == nullptr) {
317 0 : aicpusd_err("BatchLoadOpFromBuf kernel malloc for so names array failed, so number[%u]", soNum);
318 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
319 : }
320 :
321 11 : std::vector<std::string> soNameVec;
322 27 : for (uint32_t i = 0U; i < soNum; i++) {
323 19 : std::string realSoName;
324 : LoadOpFromBufArgs * const custSo =
325 19 : PtrToPtr<void, LoadOpFromBufArgs>(ValueToPtr(custSoInfo)) + i;
326 19 : const int32_t ret = AicpuCustSoManager::GetInstance().CheckAndCreateSoFile(custSo, realSoName);
327 19 : if (ret != AICPU_SCHEDULE_OK) {
328 3 : return ret;
329 : }
330 16 : soNameVec.emplace_back(realSoName);
331 16 : const size_t index = static_cast<size_t>(i);
332 16 : soNames[index] = soNameVec[index].data();
333 19 : }
334 :
335 8 : if (TryToStartCustProcess(soNames, soNum) != AICPU_SCHEDULE_OK) {
336 5 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
337 : }
338 :
339 3 : aicpusd_info("End to process ts BatchLoadOpFromBuf event.");
340 3 : return AICPU_SCHEDULE_OK;
341 11 : }
342 :
343 10 : int32_t DeleteCustOpTsKernel::Compute(const aicpu::HwtsTsKernel &tsKernelInfo)
344 : {
345 10 : aicpusd_info("Begin to process ts kernel deleteCustOp event.");
346 10 : if (!AicpuCustSoManager::GetInstance().IsSupportCustAicpu()) {
347 1 : aicpusd_err("Cust aicpu scheduler is not supported in message queue mode.");
348 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
349 : }
350 :
351 : const auto batchLoadOpFromBufArgs =
352 9 : PtrToPtr<void, BatchLoadOpFromBufArgs>(ValueToPtr(tsKernelInfo.kernelBase.cceKernel.paramBase));
353 9 : if (batchLoadOpFromBufArgs == nullptr) {
354 0 : aicpusd_err("param base for delete cust op is null.");
355 0 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
356 : }
357 :
358 9 : const uint64_t custSoInfo = static_cast<uint64_t>(batchLoadOpFromBufArgs->opInfoArgs);
359 9 : const uint32_t soNum = static_cast<uint32_t>(batchLoadOpFromBufArgs->soNum);
360 : // check so num
361 9 : if ((soNum == 0U) || (soNum > MAX_CUSTOM_SO_NUM)) {
362 1 : aicpusd_err("DeleteCustOp kernel input param soNum is invalid. soNum value is %u.", soNum);
363 1 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
364 : }
365 19 : for (uint32_t i = 0U; i < soNum; i++) {
366 : LoadOpFromBufArgs * const custSo =
367 14 : PtrToPtr<void, LoadOpFromBufArgs>(ValueToPtr(custSoInfo)) + i;
368 14 : const int32_t ret = AicpuCustSoManager::GetInstance().CheckAndDeleteSoFile(custSo);
369 14 : if (ret != AICPU_SCHEDULE_OK) {
370 3 : return ret;
371 : }
372 : }
373 :
374 : // After delete so, check whether dir is empty
375 5 : (void)AicpuCustSoManager::GetInstance().CheckAndDeleteCustSoDir();
376 5 : aicpusd_info("End to process ts deleteCustOp event.");
377 5 : return AICPU_SCHEDULE_OK;
378 : }
379 :
380 : REGISTER_HWTS_KERNEL(LOADOP_FROM_BUFF, LoadOpFromBuffTsKernel);
381 : REGISTER_HWTS_KERNEL(BATCH_LOADSO_FROM_BUFF, BatchLoadSoFromBuffTsKernel);
382 : REGISTER_HWTS_KERNEL(DELETE_CUSTOP, DeleteCustOpTsKernel);
383 : } // namespace AicpuSchedule
|