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 "aicpusd_op_executor.h"
12 : #include <cstring>
13 : #include <securec.h>
14 : #include "aicpusd_info.h"
15 : #include "aicpu_engine.h"
16 : #include "aicpu_context.h"
17 : #include "aicpusd_monitor.h"
18 : #include "aicpusd_status.h"
19 : namespace AicpuSchedule {
20 40 : CustomOpExecutor &CustomOpExecutor::GetInstance()
21 : {
22 : static CustomOpExecutor instance;
23 40 : return instance;
24 : }
25 :
26 21 : void CustomOpExecutor::InitOpExecutor(const pid_t pid, const std::string &custSoPath)
27 : {
28 21 : hostPid_ = pid;
29 21 : aicpusd_run_info("Cust so path is %s.", custSoPath.c_str());
30 21 : }
31 :
32 5 : int32_t CustomOpExecutor::OpenKernelSo(const event_info_priv &privEventInfo) const
33 : {
34 5 : const uint32_t soNameLen = privEventInfo.msg_len;
35 5 : if ((soNameLen == 0U) || (soNameLen > MAX_CUST_SO_NAME_LEN)) {
36 2 : aicpusd_err("Input param custom so name length is zero or max reached(%u), soNameBufLen=%u.",
37 : MAX_CUST_SO_NAME_LEN, soNameLen);
38 2 : return AICPU_SCHEDULE_ERROR_PARAMETER_NOT_VALID;
39 : }
40 :
41 : // custom so name
42 3 : const std::string customSoName(privEventInfo.msg, static_cast<size_t>(soNameLen));
43 : // load so
44 3 : const uint32_t loadSoCnt = 1U;
45 3 : const char_t *soNames[loadSoCnt] = {customSoName.c_str()};
46 3 : const aeStatus_t ret = aeBatchLoadKernelSo(static_cast<uint32_t>(aicpu::KERNEL_TYPE_AICPU_CUSTOM),
47 : loadSoCnt, &(soNames[0]));
48 3 : if (ret != AE_STATUS_SUCCESS) {
49 2 : aicpusd_err("Failed to OpenSingleSo, customSoName[%s].", customSoName.c_str());
50 2 : return static_cast<int32_t>(ret);
51 : }
52 1 : aicpusd_info("Open custom so handle success, customSoName=%s.", customSoName.c_str());
53 1 : return AICPU_SCHEDULE_OK;
54 3 : }
55 :
56 4 : int32_t CustomOpExecutor::OpenKernelSoByAicpuEvent(const struct TsdSubEventInfo * const msg)
57 : {
58 4 : if (msg == nullptr) {
59 1 : aicpusd_err("open custom so msg is null");
60 1 : return AicpuSchedule::AICPU_SCHEDULE_ERROR_INNER_ERROR;
61 : }
62 3 : event_info_priv privEventInfo = { };
63 3 : const errno_t errRet = strcpy_s(privEventInfo.msg, EVENT_MAX_MSG_LEN, msg->priMsg);
64 3 : if (errRet != EOK) {
65 1 : aicpusd_err("Copy %s failed, error[%d]", msg->priMsg, errRet);
66 1 : return AicpuSchedule::AICPU_SCHEDULE_ERROR_INNER_ERROR;
67 : }
68 2 : privEventInfo.msg_len = strnlen(privEventInfo.msg, MAX_CUST_SO_NAME_LEN);
69 2 : return CustomOpExecutor::GetInstance().OpenKernelSo(privEventInfo);
70 : }
71 :
72 9 : int32_t CustomOpExecutor::ExecuteKernel(aicpu::HwtsTsKernel &tsKernelInfo) const
73 : {
74 9 : const auto ret = aeCallInterface(&tsKernelInfo);
75 9 : if ((ret == AE_STATUS_SUCCESS) ||
76 1 : (ret == AE_STATUS_END_OF_SEQUENCE) ||
77 : (ret == AE_STATUS_TASK_WAIT)) {
78 8 : aicpusd_info("Aicpu cust engine process success.");
79 : } else {
80 1 : aicpusd_err("Aicpu cust engine process failed, result[%d].", static_cast<int32_t>(ret));
81 : }
82 9 : return static_cast<int32_t>(ret);
83 : }
84 : }
|