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(const char_t * const soFile, const char_t * const funcName,
84 : 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 : private:
95 : static std::string GetRealCustSoPath();
96 :
97 : // so hande which opened by dlopen
98 : tSoHandle soHandle_ = nullptr;
99 : // so name
100 : std::string soName_;
101 : // api cacher
102 : std::unordered_map<std::string, void *> apiCacher_;
103 : // A Read Write lock to protect apiCacher_
104 : tAERwLock rwLock_ = PTHREAD_RWLOCK_INITIALIZER;
105 : };
106 :
107 : class MultiSoManager {
108 : public:
109 82 : MultiSoManager() = default;
110 : ~MultiSoManager();
111 :
112 : /**
113 : * @brief Init multi so manager.
114 : * @return AICPU_SCHEDULE_OK: success, other: error code
115 : */
116 : aeStatus_t Init();
117 :
118 : /**
119 : * @brief Get a function addr by function name from a so lib file.
120 : * @param [in] kernelType: kernel type
121 : * @param [in] soName: so name
122 : * @param [in] funcName: kernel name
123 : * @param [out] funcAddrPtr: func addr
124 : * @return AICPU_SCHEDULE_OK: success, other: error code
125 : */
126 : aeStatus_t GetApi(const aicpu::KernelType kernelType, const char_t * const soName,
127 : const char_t * const funcName, void ** const funcAddrPtr);
128 :
129 : /**
130 : * @brief Load a so lib file.
131 : * @param [in] kernelType: kernel type
132 : * @param [in] soName: so name
133 : * @return AICPU_SCHEDULE_OK: success, other: error code
134 : */
135 : aeStatus_t LoadSo(const aicpu::KernelType kernelType, const std::string &soName);
136 :
137 : /**
138 : * @brief Close a so.
139 : * @param [in] soName: so name
140 : * @return AICPU_SCHEDULE_OK: success, other: error code
141 : */
142 : aeStatus_t CloseSo(const std::string &soName);
143 :
144 : private:
145 : /**
146 : * @brief Get so load path.
147 : * @param [in] kernelType: kernel type
148 : * @param [in] soName: so name
149 : * @param [in|out] soPath: so load path
150 : * @return AICPU_SCHEDULE_OK: success, other: error code
151 : */
152 : aeStatus_t GetSoPath(const aicpu::KernelType kernelType, const std::string &soName, std::string &soPath) const;
153 :
154 : /**
155 : * @brief Get so load path for cust aicpu so.
156 : * @param [in|out] soPath: so load path
157 : * @return AICPU_SCHEDULE_OK: success, other: error code
158 : */
159 : aeStatus_t GetCustSoPath(std::string &soPath) const;
160 :
161 : /**
162 : * @brief build so load path for cust aicpu so.
163 : * @param [in|out] soPath: so load path
164 : * @return AICPU_SCHEDULE_OK: success, other: error code
165 : */
166 : aeStatus_t BuildCustSoPath(std::string &soPath, const uint32_t uniqueVfId) const;
167 :
168 : /**
169 : * @brief Get so load path for inner aicpu so.
170 : * @param [in] soName: so name
171 : * @param [in|out] soPath: so load path
172 : * @return AICPU_SCHEDULE_OK: success, other: error code
173 : */
174 : aeStatus_t GetInnerSoPath(const std::string &soName, std::string &soPath) const;
175 :
176 : /**
177 : * @brief Load a so lib file.
178 : * @param [in] kernelType: kernel type
179 : * @param [in] soName: so name
180 : * @param [in|out] soMgr: single so manager
181 : * @return AICPU_SCHEDULE_OK: success, other: error code
182 : */
183 : aeStatus_t LoadSo(const aicpu::KernelType kernelType, const std::string &soName, SingleSoManager *&soMgr);
184 :
185 : /**
186 : * @brief Load a so lib file.
187 : * @param [in] kernelType: kernel type
188 : * @param [in] soName: so name
189 : * @param [in|out] soMgr: single so manager
190 : * @return AICPU_SCHEDULE_OK: success, other: error code
191 : */
192 : aeStatus_t CreateSingleSoMgr(const aicpu::KernelType kernelType,
193 : const std::string &soName, SingleSoManager *&soMgr);
194 :
195 : aeStatus_t GetThreadModeSoPath(std::string &soPath) const;
196 :
197 : aeStatus_t GetSoInHostPidPath(const std::string &soName, std::string &oriPath,
198 : SingleSoManager * const newSSoMngr, std::string &soFile) const;
199 : private:
200 : // so cacher
201 : std::unordered_map<std::string, SingleSoManager *> soCacher_;
202 : // a read write lock to protect soCacher_ and apiCacher_
203 : tAERwLock rwLock_ = PTHREAD_RWLOCK_INITIALIZER;
204 : // aipcu kernel so path for lhisi
205 : std::string innerKernelPath_;
206 : // cust aicpu kernel so path for lhisi
207 : std::string custKernelPath_;
208 : // run mode
209 : uint32_t runMode_ = aicpu::AicpuRunMode::INVALID_MODE;
210 : };
211 :
212 : }
213 : #endif
|