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 "aicpusd_msq_operator_manager.h"
12 :
13 : #include <dlfcn.h>
14 : #include <map>
15 : #include <string>
16 :
17 : #include "aicpusd_status.h"
18 :
19 : namespace AicpuSchedule {
20 : namespace {
21 : const std::string MSQ_OPERATOR_SO_NAME = "libaicpu_msq_operator.so";
22 :
23 : template <typename FuncType>
24 267 : bool LoadSymbol(void *handle, const char *name, FuncType &func)
25 : {
26 267 : void *symbol = dlsym(handle, name);
27 267 : if ((symbol == nullptr)) {
28 1 : aicpusd_err("Load symbol failed, so=%s, symbol=%s",
29 : MSQ_OPERATOR_SO_NAME.c_str(), name);
30 1 : func = nullptr;
31 1 : return false;
32 : }
33 :
34 266 : func = reinterpret_cast<FuncType>(symbol);
35 266 : return true;
36 : }
37 : } // namespace
38 :
39 : void *MsqOperatorManager::handle_ = nullptr;
40 : bool MsqOperatorManager::inited_ = false;
41 : std::mutex MsqOperatorManager::mutex_;
42 : MsqOperatorManager::MsqResetFunc MsqOperatorManager::v1ResetT0Status_ = nullptr;
43 : MsqOperatorManager::MsqResetFunc MsqOperatorManager::v1ResetT1Status_ = nullptr;
44 : MsqOperatorManager::MsqReadStatusFunc MsqOperatorManager::v1ReadT0Status_ = nullptr;
45 : MsqOperatorManager::MsqReadStatusFunc MsqOperatorManager::v1ReadT1Status_ = nullptr;
46 : MsqOperatorManager::MsqReadDataFunc MsqOperatorManager::v1ReadT0Data_ = nullptr;
47 : MsqOperatorManager::MsqReadDataFunc MsqOperatorManager::v1ReadT1Data_ = nullptr;
48 : MsqOperatorManager::MsqSendRspFunc MsqOperatorManager::v1SendT0Response_ = nullptr;
49 : MsqOperatorManager::MsqSendRspFunc MsqOperatorManager::v1SendT1Response_ = nullptr;
50 : MsqOperatorManager::MsqResetFunc MsqOperatorManager::v2ResetT0Status_ = nullptr;
51 : MsqOperatorManager::MsqResetFunc MsqOperatorManager::v2ResetT1Status_ = nullptr;
52 : MsqOperatorManager::MsqReadStatusFunc MsqOperatorManager::v2ReadT1Status_ = nullptr;
53 : MsqOperatorManager::MsqReadDataFunc MsqOperatorManager::v2ReadT1Data_ = nullptr;
54 : MsqOperatorManager::MsqSendRspFunc MsqOperatorManager::v2SendT1Response_ = nullptr;
55 : MsqOperatorManager::WaitFunc MsqOperatorManager::waitFunc_ = nullptr;
56 :
57 29 : int32_t MsqOperatorManager::Init()
58 : {
59 29 : std::lock_guard<std::mutex> lock(mutex_);
60 29 : if (inited_) {
61 8 : return AICPU_SCHEDULE_OK;
62 : }
63 :
64 21 : handle_ = dlopen(MSQ_OPERATOR_SO_NAME.c_str(), RTLD_NOW);
65 21 : if (handle_ == nullptr) {
66 1 : aicpusd_err("Open so failed, so=%s, err=%s", MSQ_OPERATOR_SO_NAME.c_str(), dlerror());
67 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
68 : }
69 :
70 20 : if (!LoadAllSymbols()) {
71 1 : (void)dlclose(handle_);
72 1 : handle_ = nullptr;
73 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
74 : }
75 :
76 19 : inited_ = true;
77 19 : aicpusd_info("Init msq operator manager success, so=%s", MSQ_OPERATOR_SO_NAME.c_str());
78 19 : return AICPU_SCHEDULE_OK;
79 29 : }
80 :
81 42 : void MsqOperatorManager::Finalize()
82 : {
83 42 : std::lock_guard<std::mutex> lock(mutex_);
84 42 : if (handle_ != nullptr) {
85 19 : (void)dlclose(handle_);
86 19 : handle_ = nullptr;
87 : }
88 :
89 42 : inited_ = false;
90 42 : v1ResetT0Status_ = nullptr;
91 42 : v1ResetT1Status_ = nullptr;
92 42 : v1ReadT0Status_ = nullptr;
93 42 : v1ReadT1Status_ = nullptr;
94 42 : v1ReadT0Data_ = nullptr;
95 42 : v1ReadT1Data_ = nullptr;
96 42 : v1SendT0Response_ = nullptr;
97 42 : v1SendT1Response_ = nullptr;
98 42 : v2ResetT0Status_ = nullptr;
99 42 : v2ResetT1Status_ = nullptr;
100 42 : v2ReadT1Status_ = nullptr;
101 42 : v2ReadT1Data_ = nullptr;
102 42 : v2SendT1Response_ = nullptr;
103 42 : waitFunc_ = nullptr;
104 42 : }
105 :
106 20 : bool MsqOperatorManager::LoadAllSymbols()
107 : {
108 : const std::map<std::string, RawFuncPtr *> symbolTable = {
109 0 : {"MsqV1ResetT0Status", reinterpret_cast<RawFuncPtr *>(&v1ResetT0Status_)},
110 0 : {"MsqV1ResetT1Status", reinterpret_cast<RawFuncPtr *>(&v1ResetT1Status_)},
111 0 : {"MsqV1ReadT0Status", reinterpret_cast<RawFuncPtr *>(&v1ReadT0Status_)},
112 0 : {"MsqV1ReadT1Status", reinterpret_cast<RawFuncPtr *>(&v1ReadT1Status_)},
113 0 : {"MsqV1ReadT0Data", reinterpret_cast<RawFuncPtr *>(&v1ReadT0Data_)},
114 0 : {"MsqV1ReadT1Data", reinterpret_cast<RawFuncPtr *>(&v1ReadT1Data_)},
115 0 : {"MsqV1SendT0Response", reinterpret_cast<RawFuncPtr *>(&v1SendT0Response_)},
116 0 : {"MsqV1SendT1Response", reinterpret_cast<RawFuncPtr *>(&v1SendT1Response_)},
117 0 : {"MsqV2ResetT0Status", reinterpret_cast<RawFuncPtr *>(&v2ResetT0Status_)},
118 0 : {"MsqV2ResetT1Status", reinterpret_cast<RawFuncPtr *>(&v2ResetT1Status_)},
119 0 : {"MsqV2ReadT1Status", reinterpret_cast<RawFuncPtr *>(&v2ReadT1Status_)},
120 0 : {"MsqV2ReadT1Data", reinterpret_cast<RawFuncPtr *>(&v2ReadT1Data_)},
121 0 : {"MsqV2SendT1Response", reinterpret_cast<RawFuncPtr *>(&v2SendT1Response_)},
122 0 : {"Wait", reinterpret_cast<RawFuncPtr *>(&waitFunc_)},
123 320 : };
124 :
125 286 : for (const auto &symbol : symbolTable) {
126 267 : if (!LoadSymbol(handle_, symbol.first.c_str(), *symbol.second)) {
127 1 : return false;
128 : }
129 : }
130 :
131 19 : return true;
132 40 : }
133 :
134 11 : void MsqOperatorManager::CallV1ResetT0Status()
135 : {
136 11 : v1ResetT0Status_();
137 11 : }
138 :
139 10 : void MsqOperatorManager::CallV1ResetT1Status()
140 : {
141 10 : v1ResetT1Status_();
142 10 : }
143 :
144 12 : MsqStatus MsqOperatorManager::CallV1ReadT0Status()
145 : {
146 12 : return v1ReadT0Status_();
147 : }
148 :
149 1 : MsqStatus MsqOperatorManager::CallV1ReadT1Status()
150 : {
151 1 : return v1ReadT1Status_();
152 : }
153 :
154 2 : void MsqOperatorManager::CallV1ReadT0Data(uint32_t msgSize, MsqDatas *datas)
155 : {
156 2 : v1ReadT0Data_(msgSize, datas);
157 2 : }
158 :
159 2 : void MsqOperatorManager::CallV1ReadT1Data(uint32_t msgSize, MsqDatas *datas)
160 : {
161 2 : v1ReadT1Data_(msgSize, datas);
162 2 : }
163 :
164 2 : void MsqOperatorManager::CallV1SendT0Response()
165 : {
166 2 : v1SendT0Response_();
167 2 : }
168 :
169 2 : void MsqOperatorManager::CallV1SendT1Response()
170 : {
171 2 : v1SendT1Response_();
172 2 : }
173 :
174 1 : void MsqOperatorManager::CallV2ResetT0Status()
175 : {
176 1 : v2ResetT0Status_();
177 1 : }
178 :
179 1 : void MsqOperatorManager::CallV2ResetT1Status()
180 : {
181 1 : v2ResetT1Status_();
182 1 : }
183 :
184 1 : MsqStatus MsqOperatorManager::CallV2ReadT1Status()
185 : {
186 1 : return v2ReadT1Status_();
187 : }
188 :
189 1 : void MsqOperatorManager::CallV2ReadT1Data(uint32_t msgSize, MsqDatas *datas)
190 : {
191 1 : v2ReadT1Data_(msgSize, datas);
192 1 : }
193 :
194 1 : void MsqOperatorManager::CallV2SendT1Response()
195 : {
196 1 : v2SendT1Response_();
197 1 : }
198 :
199 2 : void MsqOperatorManager::CallWait()
200 : {
201 2 : waitFunc_();
202 2 : return;
203 : }
204 : } // namespace AicpuSchedule
|