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 CORE_AICPUSD_RESOURCE_MANAGER_H
12 : #define CORE_AICPUSD_RESOURCE_MANAGER_H
13 :
14 : #include <memory>
15 : #include <mutex>
16 : #include <list>
17 : #include <vector>
18 : #include <unordered_set>
19 : #include <unordered_map>
20 : #include "ascend_hal.h"
21 : #include "aicpusd_common.h"
22 : #include "aicpusd_util.h"
23 :
24 : namespace AicpuSchedule {
25 : /**
26 : * @brief Guard for MBuf.
27 : */
28 : class BufManager {
29 : public:
30 : /**
31 : * @brief get BufManager instance.
32 : * @return instance
33 : */
34 : static BufManager& GetInstance();
35 :
36 3075 : ~BufManager() = default;
37 :
38 : /**
39 : * @brief Guard buf.
40 : * BufManager take ownership of buf.
41 : * @param mbuf mbuf for guard.
42 : * @param modelId buf for model
43 : * @return AICPU_SCHEDULE_OK:success, other failed.
44 : */
45 : int32_t GuardBuf(Mbuf* const mbuf, const uint32_t modelId);
46 :
47 : /**
48 : * @brief Malloc and guard buf
49 : * BufManager malloc and take ownership of buf.
50 : * @param allocSize buf size
51 : * @param modelId buf for model
52 : * @return AICPU_SCHEDULE_OK:success, other failed.
53 : */
54 : Mbuf* MallocAndGuardBuf(const uint32_t allocSize, const uint32_t modelId);
55 :
56 : Mbuf* MallocAndGuardBufU64(const uint64_t allocSize, const uint32_t modelId);
57 :
58 : /**
59 : * @brief Malloc and guard buf list
60 : * BufManager malloc and take ownership of buf list.
61 : * @param sizeList buf size list
62 : * @param len sizeList length
63 : * @param modelId buf for model
64 : * @param isLinkMbuf is link mbuf
65 : * @param mbufPtrStore output all mbuf here, if isLinkMbuf is true, the first is mbuflist head
66 : * @return AICPU_SCHEDULE_OK:success, other failed.
67 : */
68 : int32_t MallocAndGuardBufList(
69 : const uint32_t* const sizeList, const uint32_t len, const uint32_t modelId, const bool isLinkMbuf,
70 : Mbuf** const mbufPtrStore);
71 :
72 : /**
73 : * @brief UnGuard buf.
74 : * BufManager releases ownership of buf.
75 : * @param modelId model id
76 : * @param mbuf UnGuard buf
77 : * @return AICPU_SCHEDULE_OK:success, other failed.
78 : */
79 : int32_t UnGuardBuf(const uint32_t modelId, const Mbuf* const mbuf);
80 :
81 : /**
82 : * @brief free all buf in model.
83 : * @param modelId model id
84 : */
85 : void FreeBuf(const uint32_t modelId);
86 :
87 : /**
88 : * @brief free all buf.
89 : */
90 : void FreeAllBuf();
91 :
92 : // Init memzone info
93 : void InitBufManager();
94 :
95 : // not allow copy constructor and assignment operators
96 : BufManager(const BufManager&) = delete;
97 :
98 : BufManager& operator=(const BufManager&) = delete;
99 :
100 : BufManager(BufManager&&) = delete;
101 :
102 : BufManager&& operator=(BufManager&&) = delete;
103 :
104 : private:
105 3075 : BufManager() = default;
106 :
107 : /**
108 : * @brief Malloc buf
109 : * BufManager malloc and take ownership of buf.
110 : * @param allocSize buf size
111 : * @return AICPU_SCHEDULE_OK:success, other failed.
112 : */
113 : Mbuf* MallocBuf(const uint32_t allocSize);
114 :
115 : Mbuf* MallocBufU64(const uint64_t allocSize);
116 :
117 : /**
118 : * @brief BufManager malloc and append mbuf
119 : * @return AICPU_SCHEDULE_OK:success, other failed.
120 : */
121 : int32_t MallocAndAppend(
122 : const uint32_t* const sizeList, const uint32_t idx, const uint32_t modelId, Mbuf*& mbuf, Mbuf*& mbufListHead);
123 :
124 : // record mbufs belong to model, no mutex
125 : std::list<Mbuf*> modelBufs_[MAX_MODEL_COUNT];
126 :
127 : SpinLock lockForModels_[MAX_MODEL_COUNT];
128 : // record mbufs memzone info
129 : BuffCfg buffConfig_;
130 : };
131 :
132 : /**
133 : * @brief Event wait manager.
134 : */
135 : class EventWaitManager {
136 : public:
137 : static EventWaitManager& NotifyWaitManager(const uint32_t waitIdCount = MAX_NOTIFY_COUNT);
138 :
139 : static EventWaitManager& EndGraphWaitManager(const uint32_t waitIdCount = MAX_MODEL_COUNT);
140 :
141 : static EventWaitManager& QueueNotEmptyWaitManager(const uint32_t waitIdCount = DEFAULT_QUEUE_COUNT);
142 :
143 : static EventWaitManager& QueueNotFullWaitManager(const uint32_t waitIdCount = DEFAULT_QUEUE_COUNT);
144 :
145 : static EventWaitManager& PrepareMemWaitManager(const uint32_t waitIdCount = MAX_MODEL_COUNT);
146 :
147 : static EventWaitManager& AnyQueNotEmptyWaitManager(const uint32_t waitIdCount = MAX_MODEL_COUNT);
148 :
149 : static EventWaitManager& TableUnlockWaitManager(const uint32_t waitIdCount = MAX_MODEL_COUNT);
150 :
151 21 : ~EventWaitManager() = default;
152 :
153 : // not allow copy constructor and assignment operators
154 : EventWaitManager(const EventWaitManager&) = delete;
155 :
156 : EventWaitManager& operator=(const EventWaitManager&) = delete;
157 :
158 : EventWaitManager(EventWaitManager&&) = delete;
159 :
160 : EventWaitManager&& operator=(EventWaitManager&&) = delete;
161 :
162 : /**
163 : * @brief Get wait stream or save notify state
164 : * @param eventWaitId wait id.
165 : * @param hasWait some stream is waiting
166 : * @param waitStreamId wait stream id, valid only when hasWaitStream is true
167 : */
168 : void Event(const size_t eventWaitId, bool& hasWait, uint32_t& waitStreamId);
169 :
170 : /**
171 : * @brief when event is come, clear event state,
172 : * or else save wait stream info and return need wait
173 : * @param eventWaitId wait id
174 : * @param waitStreamId wait stream id
175 : * @param needWait if event state is true, set needWait to true;
176 : */
177 : void WaitEvent(const size_t eventWaitId, const uint32_t waitStreamId, bool& needWait);
178 :
179 : /**
180 : * @brief reset specified event state
181 : * @param eventWaitId wait id
182 : */
183 : void ResetEventState(const size_t eventWaitId);
184 :
185 : /**
186 : * @brief clear specified record
187 : * @param eventWaitId wait id
188 : * @return AICPU_SCHEDULE_OK: success, other: failed
189 : */
190 : __attribute__((visibility("hidden"))) int32_t ClearBatch(const std::unordered_set<size_t>& waitIds);
191 :
192 : /**
193 : * @brief check eventState_ and waitStream_ length
194 : */
195 : bool CheckEvent(const bool eventStateNeedCheck, const bool waitStreamNeedCheck, const size_t length);
196 :
197 : void GetWaitingEvent(std::vector<size_t>& eventWaitIds);
198 :
199 : private:
200 21 : EventWaitManager(const std::string& eventType, const uint32_t waitIdCount)
201 21 : : eventType_(eventType),
202 21 : count_(waitIdCount),
203 42 : eventState_(static_cast<uint64_t>(waitIdCount), false),
204 42 : waitStream_(static_cast<uint64_t>(waitIdCount), UINT32_MAX),
205 21 : waitCount_(0)
206 21 : {}
207 :
208 : // event type
209 : const std::string eventType_;
210 :
211 : // count
212 : const uint32_t count_;
213 :
214 : // true means event come
215 : std::vector<bool> eventState_;
216 :
217 : // record wait stream id
218 : std::vector<uint32_t> waitStream_;
219 :
220 : // protect eventState_, waitStream_
221 : std::mutex waitMutex_;
222 :
223 : int32_t waitCount_;
224 : };
225 :
226 : /**
227 : * @brief model stream manager.
228 : */
229 : class ModelStreamManager {
230 : public:
231 : static ModelStreamManager& GetInstance();
232 :
233 4 : ~ModelStreamManager() = default;
234 :
235 : // not allow copy constructor and assignment operators
236 : ModelStreamManager(const ModelStreamManager&) = delete;
237 :
238 : ModelStreamManager& operator=(const ModelStreamManager&) = delete;
239 :
240 : ModelStreamManager(ModelStreamManager&&) = delete;
241 :
242 : ModelStreamManager&& operator=(ModelStreamManager&&) = delete;
243 :
244 : void Reg(const uint32_t modelId, const std::vector<StreamInfo>& streams);
245 :
246 : void UnReg(const uint32_t modelId, const std::vector<StreamInfo>& streams);
247 :
248 : int32_t GetStreamFlag(const uint32_t streamId, uint32_t& streamFlag);
249 :
250 : int32_t GetStreamModelId(const uint32_t streamId, uint32_t& modelId);
251 :
252 : private:
253 4 : ModelStreamManager() = default;
254 :
255 : mutable std::mutex streamInfoMtx_;
256 : // streamId: {modelId, streamFlag}
257 : std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> streamInfos_;
258 : };
259 :
260 : class RwLock {
261 : public:
262 2 : RwLock() = default;
263 :
264 : ~RwLock() = default;
265 :
266 : void Init();
267 :
268 : bool RdLock();
269 :
270 : bool WrLock();
271 :
272 : void UnLock();
273 :
274 : private:
275 : std::mutex mu_;
276 : uint32_t readCount_;
277 : uint32_t writeCount_;
278 : };
279 :
280 : class TableLockManager {
281 : public:
282 : static TableLockManager& GetInstance();
283 :
284 1 : ~TableLockManager() = default;
285 :
286 : bool RdLockTable(const uint32_t tableId);
287 :
288 : bool WrLockTable(const uint32_t tableId);
289 :
290 : void UnLockTable(const uint32_t tableId);
291 :
292 : private:
293 1 : TableLockManager() = default;
294 :
295 : RwLock& GetTableLock(const uint32_t tableId);
296 :
297 : std::mutex mutexForLockMap_;
298 : std::unordered_map<uint32_t, RwLock> tableLocks_;
299 : };
300 : } // namespace AicpuSchedule
301 :
302 : #endif // CORE_AICPUSD_RESOURCE_MANAGER_H
|