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 : #ifndef __AE_SO_MANAGER_H__
12 : #define __AE_SO_MANAGER_H__
13 :
14 : #include <string>
15 : #include <unordered_map>
16 : #include <set>
17 : #include <pthread.h>
18 : #include <unordered_set>
19 : #include "aicpu_engine.h"
20 : #include "ae_def.hpp"
21 : #include "aicpu_event_struct.h"
22 : #include "aicpu_context.h"
23 :
24 : namespace cce {
25 :
26 : using tSoHandle = void*;
27 :
28 : class SingleSoManager {
29 : public:
30 30 : SingleSoManager() = default;
31 : ~SingleSoManager();
32 :
33 : /**
34 : * @brief Init single so manager.
35 : * @param [in] guardDirName: dir name
36 : * @param [in] soFile: full so file
37 : * @return AICPU_SCHEDULE_OK: success, other: error code
38 : */
39 : aeStatus_t Init(const std::string& guardDirName, const std::string& soFile);
40 :
41 : /**
42 : * @brief Get a function addr by function name from a so lib file.
43 : * @param [in] soName: so name
44 : * @param [in] funcName: kernel name
45 : * @param [out] funcAddrPtr: func addr
46 : * @return AICPU_SCHEDULE_OK: success, other: error code
47 : */
48 : aeStatus_t GetApi(const char_t* const soNamePtr, const char_t* const funcName, void** const funcAddrPtr);
49 :
50 : /**
51 : * @brief Open a so file.
52 : * @param [in] soFile: full so path
53 : * @param [out] retHandle: so handle
54 : * @return AICPU_SCHEDULE_OK: success, other: error code
55 : */
56 : static aeStatus_t OpenSo(const std::string& soFile, void** const retHandle);
57 :
58 : /**
59 : * @brief Get the func addr in so handle by func name.
60 : * @param [in] soHandle: so handle
61 : * @param [in] funcName: func name
62 : * @param [out] retFuncAddr: func addr
63 : * @return AICPU_SCHEDULE_OK: success, other: error code
64 : */
65 : static aeStatus_t GetFunc(void* const soHandle, const char_t* const funcName, void** const retFuncAddr);
66 :
67 : /**
68 : * @brief Check so file.
69 : * @param [in] guardDirName: dir name
70 : * @param [in] soFile: so full path
71 : * @return AICPU_SCHEDULE_OK: success, other: error code
72 : */
73 : static aeStatus_t CheckSoFile(const std::string& guardDirName, const std::string& soFile);
74 :
75 : /**
76 : * @brief Get a function addr and so handle by so file and function name.
77 : * @param [in] soFile: so full path
78 : * @param [in] funcName: kernel name
79 : * @param [out] funcAddrPtr: func addr
80 : * @param [out] retHandle: so handle
81 : * @return AICPU_SCHEDULE_OK: success, other: error code
82 : */
83 : static aeStatus_t GetApi(
84 : const char_t* const soFile, const char_t* const funcName, void** const funcAddrPtr, void** const retHandle);
85 :
86 : /**
87 : * @brief Close so.
88 : * @param [in] retHandle: so handle
89 : * @return AICPU_SCHEDULE_OK: success, other: error code
90 : */
91 : static aeStatus_t CloseSo(void* const retHandle);
92 :
93 : static void GetCustSoPathByUniqueVfId(std::string& soPath, const uint32_t uniqueVfId);
94 :
95 : private:
96 : static std::string GetRealCustSoPath();
97 :
98 : // so hande which opened by dlopen
99 : tSoHandle soHandle_ = nullptr;
100 : // so name
101 : std::string soName_;
102 : // api cacher
103 : std::unordered_map<std::string, void*> apiCacher_;
104 : // A Read Write lock to protect apiCacher_
105 : tAERwLock rwLock_ = PTHREAD_RWLOCK_INITIALIZER;
106 : };
107 :
108 : class MultiSoManager {
109 : public:
110 82 : MultiSoManager() = default;
111 : ~MultiSoManager();
112 :
113 : /**
114 : * @brief Init multi so manager.
115 : * @return AICPU_SCHEDULE_OK: success, other: error code
116 : */
117 : aeStatus_t Init();
118 :
119 : /**
120 : * @brief Get a function addr by function name from a so lib file.
121 : * @param [in] kernelType: kernel type
122 : * @param [in] soName: so name
123 : * @param [in] funcName: kernel name
124 : * @param [out] funcAddrPtr: func addr
125 : * @return AICPU_SCHEDULE_OK: success, other: error code
126 : */
127 : aeStatus_t GetApi(
128 : const aicpu::KernelType kernelType, const char_t* const soName, const char_t* const funcName,
129 : void** const funcAddrPtr);
130 :
131 : /**
132 : * @brief Load a so lib file.
133 : * @param [in] kernelType: kernel type
134 : * @param [in] soName: so name
135 : * @return AICPU_SCHEDULE_OK: success, other: error code
136 : */
137 : aeStatus_t LoadSo(const aicpu::KernelType kernelType, const std::string& soName);
138 :
139 : /**
140 : * @brief Close a so.
141 : * @param [in] soName: so name
142 : * @return AICPU_SCHEDULE_OK: success, other: error code
143 : */
144 : aeStatus_t CloseSo(const std::string& soName);
145 :
146 : private:
147 : /**
148 : * @brief Get so load path.
149 : * @param [in] kernelType: kernel type
150 : * @param [in] soName: so name
151 : * @param [in|out] soPath: so load path
152 : * @return AICPU_SCHEDULE_OK: success, other: error code
153 : */
154 : aeStatus_t GetSoPath(const aicpu::KernelType kernelType, const std::string& soName, std::string& soPath) const;
155 :
156 : /**
157 : * @brief Get so load path for cust aicpu so.
158 : * @param [in|out] soPath: so load path
159 : * @return AICPU_SCHEDULE_OK: success, other: error code
160 : */
161 : aeStatus_t GetCustSoPath(std::string& soPath) const;
162 :
163 : /**
164 : * @brief build so load path for cust aicpu so.
165 : * @param [in|out] soPath: so load path
166 : * @return AICPU_SCHEDULE_OK: success, other: error code
167 : */
168 : aeStatus_t BuildCustSoPath(std::string& soPath, const uint32_t uniqueVfId) const;
169 :
170 : /**
171 : * @brief Get so load path for inner aicpu so.
172 : * @param [in] soName: so name
173 : * @param [in|out] soPath: so load path
174 : * @return AICPU_SCHEDULE_OK: success, other: error code
175 : */
176 : aeStatus_t GetInnerSoPath(const std::string& soName, std::string& soPath) const;
177 :
178 : /**
179 : * @brief Load a so lib file.
180 : * @param [in] kernelType: kernel type
181 : * @param [in] soName: so name
182 : * @param [in|out] soMgr: single so manager
183 : * @return AICPU_SCHEDULE_OK: success, other: error code
184 : */
185 : aeStatus_t LoadSo(const aicpu::KernelType kernelType, const std::string& soName, SingleSoManager*& soMgr);
186 :
187 : /**
188 : * @brief Load a so lib file.
189 : * @param [in] kernelType: kernel type
190 : * @param [in] soName: so name
191 : * @param [in|out] soMgr: single so manager
192 : * @return AICPU_SCHEDULE_OK: success, other: error code
193 : */
194 : aeStatus_t CreateSingleSoMgr(
195 : const aicpu::KernelType kernelType, const std::string& soName, SingleSoManager*& soMgr);
196 :
197 : aeStatus_t GetThreadModeSoPath(std::string& soPath) const;
198 :
199 : aeStatus_t GetSoInHostPidPath(
200 : const std::string& soName, std::string& oriPath, SingleSoManager* const newSSoMngr, std::string& soFile) const;
201 :
202 : private:
203 : // so cacher
204 : std::unordered_map<std::string, SingleSoManager*> soCacher_;
205 : // a read write lock to protect soCacher_ and apiCacher_
206 : tAERwLock rwLock_ = PTHREAD_RWLOCK_INITIALIZER;
207 : // aipcu kernel so path for lhisi
208 : std::string innerKernelPath_;
209 : // cust aicpu kernel so path for lhisi
210 : std::string custKernelPath_;
211 : // run mode
212 : uint32_t runMode_ = aicpu::AicpuRunMode::INVALID_MODE;
213 : };
214 :
215 : } // namespace cce
216 : #endif
|