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(const uint32_t * const sizeList, const uint32_t len, const uint32_t modelId,
69 : const bool isLinkMbuf, Mbuf ** const mbufPtrStore);
70 :
71 : /**
72 : * @brief UnGuard buf.
73 : * BufManager releases ownership of buf.
74 : * @param modelId model id
75 : * @param mbuf UnGuard buf
76 : * @return AICPU_SCHEDULE_OK:success, other failed.
77 : */
78 : int32_t UnGuardBuf(const uint32_t modelId, const Mbuf *const mbuf);
79 :
80 : /**
81 : * @brief free all buf in model.
82 : * @param modelId model id
83 : */
84 : void FreeBuf(const uint32_t modelId);
85 :
86 : /**
87 : * @brief free all buf.
88 : */
89 : void FreeAllBuf();
90 :
91 : // Init memzone info
92 : void InitBufManager();
93 :
94 : // not allow copy constructor and assignment operators
95 : BufManager(const BufManager &) = delete;
96 :
97 : BufManager &operator=(const BufManager &) = delete;
98 :
99 : BufManager(BufManager &&) = delete;
100 :
101 : BufManager &&operator=(BufManager &&) = delete;
102 :
103 : private:
104 3075 : BufManager() = default;
105 :
106 : /**
107 : * @brief Malloc buf
108 : * BufManager malloc and take ownership of buf.
109 : * @param allocSize buf size
110 : * @return AICPU_SCHEDULE_OK:success, other failed.
111 : */
112 : Mbuf *MallocBuf(const uint32_t allocSize);
113 :
114 : Mbuf *MallocBufU64(const uint64_t allocSize);
115 :
116 : /**
117 : * @brief BufManager malloc and append mbuf
118 : * @return AICPU_SCHEDULE_OK:success, other failed.
119 : */
120 : int32_t MallocAndAppend(const uint32_t * const sizeList, const uint32_t idx, const uint32_t modelId,
121 : Mbuf *&mbuf, Mbuf *&mbufListHead);
122 :
123 : // record mbufs belong to model, no mutex
124 : std::list<Mbuf *> modelBufs_[MAX_MODEL_COUNT];
125 :
126 : SpinLock lockForModels_[MAX_MODEL_COUNT];
127 : // record mbufs memzone info
128 : BuffCfg buffConfig_;
129 : };
130 :
131 : /**
132 : * @brief Event wait manager.
133 : */
134 : class EventWaitManager {
135 : public:
136 : static EventWaitManager &NotifyWaitManager(const uint32_t waitIdCount = MAX_NOTIFY_COUNT);
137 :
138 : static EventWaitManager &EndGraphWaitManager(const uint32_t waitIdCount = MAX_MODEL_COUNT);
139 :
140 : static EventWaitManager &QueueNotEmptyWaitManager(const uint32_t waitIdCount = DEFAULT_QUEUE_COUNT);
141 :
142 : static EventWaitManager &QueueNotFullWaitManager(const uint32_t waitIdCount = DEFAULT_QUEUE_COUNT);
143 :
144 : static EventWaitManager &PrepareMemWaitManager(const uint32_t waitIdCount = MAX_MODEL_COUNT);
145 :
146 : static EventWaitManager &AnyQueNotEmptyWaitManager(const uint32_t waitIdCount = MAX_MODEL_COUNT);
147 :
148 : static EventWaitManager &TableUnlockWaitManager(const uint32_t waitIdCount = MAX_MODEL_COUNT);
149 :
150 21 : ~EventWaitManager() = default;
151 :
152 : // not allow copy constructor and assignment operators
153 : EventWaitManager(const EventWaitManager &) = delete;
154 :
155 : EventWaitManager &operator=(const EventWaitManager &) = delete;
156 :
157 : EventWaitManager(EventWaitManager &&) = delete;
158 :
159 : EventWaitManager &&operator=(EventWaitManager &&) = delete;
160 :
161 : /**
162 : * @brief Get wait stream or save notify state
163 : * @param eventWaitId wait id.
164 : * @param hasWait some stream is waiting
165 : * @param waitStreamId wait stream id, valid only when hasWaitStream is true
166 : */
167 : void Event(const size_t eventWaitId, bool &hasWait, uint32_t &waitStreamId);
168 :
169 : /**
170 : * @brief when event is come, clear event state,
171 : * or else save wait stream info and return need wait
172 : * @param eventWaitId wait id
173 : * @param waitStreamId wait stream id
174 : * @param needWait if event state is true, set needWait to true;
175 : */
176 : void WaitEvent(const size_t eventWaitId, const uint32_t waitStreamId, bool &needWait);
177 :
178 : /**
179 : * @brief reset specified event state
180 : * @param eventWaitId wait id
181 : */
182 : void ResetEventState(const size_t eventWaitId);
183 :
184 : /**
185 : * @brief clear specified record
186 : * @param eventWaitId wait id
187 : * @return AICPU_SCHEDULE_OK: success, other: failed
188 : */
189 : __attribute__((visibility("hidden")))
190 : 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,
201 21 : const uint32_t waitIdCount) : 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 :
207 : // event type
208 : const std::string eventType_;
209 :
210 : // count
211 : const uint32_t count_;
212 :
213 : // true means event come
214 : std::vector<bool> eventState_;
215 :
216 : // record wait stream id
217 : std::vector<uint32_t> waitStream_;
218 :
219 : // protect eventState_, waitStream_
220 : std::mutex waitMutex_;
221 :
222 : int32_t waitCount_;
223 : };
224 :
225 : /**
226 : * @brief model stream manager.
227 : */
228 : class ModelStreamManager {
229 : public:
230 : static ModelStreamManager &GetInstance();
231 :
232 4 : ~ModelStreamManager() = default;
233 :
234 : // not allow copy constructor and assignment operators
235 : ModelStreamManager(const ModelStreamManager &) = delete;
236 :
237 : ModelStreamManager &operator=(const ModelStreamManager &) = delete;
238 :
239 : ModelStreamManager(ModelStreamManager &&) = delete;
240 :
241 : ModelStreamManager &&operator=(ModelStreamManager &&) = delete;
242 :
243 : void Reg(const uint32_t modelId, const std::vector<StreamInfo> &streams);
244 :
245 : void UnReg(const uint32_t modelId, const std::vector<StreamInfo> &streams);
246 :
247 : int32_t GetStreamFlag(const uint32_t streamId, uint32_t &streamFlag);
248 :
249 : int32_t GetStreamModelId(const uint32_t streamId, uint32_t &modelId);
250 :
251 : private:
252 4 : ModelStreamManager() = default;
253 :
254 : mutable std::mutex streamInfoMtx_;
255 : // streamId: {modelId, streamFlag}
256 : std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> streamInfos_;
257 : };
258 :
259 : class RwLock {
260 : public:
261 2 : RwLock() = default;
262 :
263 : ~RwLock() = default;
264 :
265 : void Init();
266 :
267 : bool RdLock();
268 :
269 : bool WrLock();
270 :
271 : void UnLock();
272 :
273 : private:
274 : std::mutex mu_;
275 : uint32_t readCount_;
276 : uint32_t writeCount_;
277 : };
278 :
279 : class TableLockManager {
280 : public:
281 : static TableLockManager &GetInstance();
282 :
283 1 : ~TableLockManager() = default;
284 :
285 : bool RdLockTable(const uint32_t tableId);
286 :
287 : bool WrLockTable(const uint32_t tableId);
288 :
289 : void UnLockTable(const uint32_t tableId);
290 :
291 : private:
292 1 : TableLockManager() = default;
293 :
294 : RwLock &GetTableLock(const uint32_t tableId);
295 :
296 : std::mutex mutexForLockMap_;
297 : std::unordered_map<uint32_t, RwLock> tableLocks_;
298 : };
299 : }
300 :
301 : #endif // CORE_AICPUSD_RESOURCE_MANAGER_H
|