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 DGW_ENTITY_MANAGER_H
12 : #define DGW_ENTITY_MANAGER_H
13 :
14 : #include <functional>
15 : #include <map>
16 : #include <pthread.h>
17 : #include <vector>
18 :
19 : #include "entity.h"
20 : #include "channel_entity.h"
21 :
22 : namespace dgw {
23 : // buf pool register/unregister msg
24 : constexpr uint32_t EVENT_BUF_POOL_MSG = static_cast<uint32_t>(EVENT_ID::EVENT_DRV_MSG);
25 : // request recv msg
26 : constexpr uint32_t EVENT_RECV_REQUEST_MSG = static_cast<uint32_t>(EVENT_ID::EVENT_USR_START);
27 : // request send completion msg
28 : constexpr uint32_t EVENT_SEND_COMPLETION_MSG = static_cast<uint32_t>(EVENT_ID::EVENT_USR_START) + 1U;
29 : // request recv completion msg
30 : constexpr uint32_t EVENT_RECV_COMPLETION_MSG = static_cast<uint32_t>(EVENT_ID::EVENT_USR_START) + 2U;
31 : // hccl full2NotFull msg
32 : constexpr uint32_t EVENT_CONGESTION_RELIEF_MSG = static_cast<uint32_t>(EVENT_ID::EVENT_USR_START) + 3U;
33 :
34 : struct CommChannels {
35 : std::vector<ChannelEntityPtr> entities; // comm channel vector
36 : std::vector<HcclRequest> requests; // requests
37 : std::vector<int32_t> compIndices; // comp indices: when testsome, completed request index in requests
38 : std::vector<HcclStatus> compStatus; // comp status: when testsome, status of completed request
39 : pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER; // lock for comm channel vector
40 : };
41 :
42 : using MapIdToEntityVector = std::map<uint32_t, std::vector<EntityPtr>>;
43 : using IdToEntityMap = std::map<uint32_t, std::map<uint32_t, std::map<EntityType, MapIdToEntityVector>>>;
44 : using GroupEntityMap = std::map<uint32_t, std::vector<EntityPtr>>;
45 :
46 : class EntityManager {
47 : public:
48 7 : explicit EntityManager(const uint32_t resIndex) { resIndex_ = resIndex; }
49 : ~EntityManager();
50 :
51 : EntityManager(const EntityManager&) = delete;
52 : EntityManager(const EntityManager&&) = delete;
53 : EntityManager& operator=(const EntityManager&) = delete;
54 : EntityManager& operator=(EntityManager&&) = delete;
55 :
56 : static EntityManager& Instance(const uint32_t resIndex = 0U);
57 :
58 : /**
59 : * get entity by id
60 : * @param eType: entity type
61 : * @param id: entity id
62 : * @return entity pointer
63 : */
64 : EntityPtr GetEntityById(
65 : const uint32_t queueType, const uint32_t deviceId, const EntityType eType, const uint32_t id,
66 : const EntityDirection direction);
67 :
68 : EntityPtr DoGetEntity(
69 : const uint32_t queueType, const uint32_t deviceId, const EntityType eType, const uint32_t id,
70 : const EntityDirection direction);
71 :
72 : EntityPtr GetSrcEntityByGlobalId(const uint32_t key, const uint32_t globalId) const;
73 :
74 : EntityPtr GetDstEntityByGlobalId(const uint32_t key, const uint32_t globalId) const;
75 : /**
76 : * get entities from group
77 : * @param groupId: group id
78 : * @return const std::vector<EntityPtr> &
79 : */
80 : const std::vector<EntityPtr>& GetEntitiesInGroup(const uint32_t groupId);
81 :
82 : /**
83 : * create group
84 : * @param groupId group id
85 : * @param entities entities in group
86 : * @return FSM_SUCCESS: success, other: failed
87 : */
88 : FsmStatus CreateGroup(const uint32_t groupId, std::vector<EntityPtr>& entities);
89 :
90 : /**
91 : * delete group
92 : * @param groupId group id
93 : * @return FSM_SUCCESS: success, other: failed
94 : */
95 : FsmStatus DeleteGroup(const uint32_t groupId);
96 :
97 : /**
98 : * create entity
99 : * @param etype entity for queue:EntityType::ENTITY_QUEUE, entity for tag:EntityType::ENTITY_TAG
100 : * @param state send entity:FsmState::FSM_IDLE_STATE, recv entity:FsmState::FSM_WAIT_PUSH_STATE
101 : * @param id queue id or group id or channel id (not tag id)
102 : * @param channel comm channel info, only for comm channel
103 : * @param hostGroupId host group id
104 : * @param groupPolicy group policy, only for group
105 : * @return return EntityPtr
106 : */
107 : EntityPtr CreateEntity(const EntityMaterial& material);
108 :
109 : /**
110 : * delete entity
111 : * @param type entity for queue:EntityType::ENTITY_QUEUE, entity for tag:EntityType::ENTITY_TAG
112 : * @param id queue id or tag id or group id
113 : * @return FsmStatus FSM_SUCCESS: success, other: failed
114 : */
115 : FsmStatus DeleteEntity(
116 : const uint32_t queueType, const uint32_t deviceId, const EntityType eType, const uint32_t id,
117 : const EntityDirection direction);
118 :
119 : /**
120 : * probe source comm channel
121 : * @param procFunc process function
122 : * @return FsmStatus FSM_SUCCESS: success, other: failed
123 : */
124 : FsmStatus ProbeSrcCommChannel(const std::function<FsmStatus(const ChannelEntityPtr&, uint32_t&)> procFunc);
125 :
126 : /**
127 : * check and supply recv request event
128 : * @return FsmStatus FSM_SUCCESS: success, other: failed
129 : */
130 : FsmStatus SupplyRecvRequestEvent();
131 :
132 : FsmStatus SupplyOneTrackEvent();
133 :
134 : /**
135 : * test some comm channel
136 : * @param procFunc process function
137 : * @param isSrc is src
138 : * @return FsmStatus FSM_SUCCESS: success, other: failed
139 : */
140 : FsmStatus TestSomeCommChannels(
141 : const std::function<FsmStatus(CommChannels&, uint32_t&, uint32_t&)> procFunc, const bool isSrc);
142 :
143 : /**
144 : * erase comm channel from src/dst comm channels
145 : * @param isSrc is source entity
146 : */
147 : FsmStatus EraseCommChannel(const EntityPtr& entity, const bool isSrc);
148 :
149 : /**
150 : * insert comm channel to src/dst comm channels
151 : * @param entity entity
152 : * @param isSrc is source entity
153 : * @return FsmStatus FSM_SUCCESS: success, other: failed
154 : */
155 : FsmStatus InsertCommChannel(const ChannelEntityPtr& entity, const bool isSrc);
156 :
157 : /**
158 : * @brief Get comm channels
159 : * @param isSrc is source entity
160 : * @return const CommChannels& src or dst comm channels
161 : */
162 : CommChannels& GetCommChannels(const bool isSrc);
163 :
164 : /**
165 : * @brief supply event
166 : * @param eventId event id
167 : * @return FsmStatus FSM_SUCCESS: success, other: failed
168 : */
169 : FsmStatus SupplyEvent(const uint32_t eventId) const;
170 :
171 : FsmStatus SupplyEvent(const uint32_t eventId, const uint32_t deviceId, const uint32_t groupId) const;
172 :
173 : /**
174 : * @brief check link status
175 : * @return FsmStatus FSM_SUCCESS: success, other: failed
176 : */
177 : FsmStatus CheckLinkStatus();
178 :
179 : /**
180 : * @brief show whether some entity has been full
181 : * @return true: yes, false: no
182 : */
183 28 : inline bool IsExistFullEntity() const { return existFull_; }
184 :
185 : /**
186 : * @brief mark some entity has been full
187 : */
188 2 : inline void SetExistFullEntity() { existFull_ = true; }
189 :
190 : /**
191 : * @brief show whether some entity has been Async Mem Buff
192 : * @return true: yes, false: no
193 : */
194 4 : inline bool IsExistAsyncMemEntity() const { return existDstAsyncMem_; }
195 :
196 : /**
197 : * @brief mark some entity has been Async Mem Buff
198 : */
199 5 : inline void SetExistAsyncMemEntity() { existDstAsyncMem_ = true; }
200 :
201 : /**
202 : * @brief set subscription_pause_policy for full queue
203 : */
204 17 : inline void SetSubscriptionPausePolicy(const bool pauseSubscriptionWhenFull)
205 : {
206 17 : pauseSubscriptionWhenFull_ = pauseSubscriptionWhenFull;
207 17 : }
208 :
209 : /**
210 : * @brief whether pause subscription
211 : * @return true: yes, false: no
212 : */
213 3 : inline bool ShouldPauseSubscirpiton() const { return pauseSubscriptionWhenFull_; }
214 :
215 : void CleanEntityMap(const uint32_t queueType, const uint32_t deviceId, const EntityType eType, const uint32_t id);
216 :
217 : private:
218 : /**
219 : * @brief create new entity by type
220 : * @param etype entity type
221 : * @param id entity id
222 : * @param channel channel info, only for channel
223 : * @param hostGroupId host group id, for queue or channel in group
224 : * @param groupPolicy group policy, only for group
225 : * @return EntityPtr
226 : */
227 : EntityPtr AllocEntity(const EntityMaterial& material) const;
228 :
229 : void DoAllocEntity(const EntityMaterial& material, EntityPtr& entity) const;
230 :
231 : FsmStatus SupplyEventForRecvRequest(uint32_t msgType);
232 :
233 : // entity map
234 : IdToEntityMap idToEntity_;
235 : // group entity map
236 : GroupEntityMap groupEntityMap_;
237 : // src comm channels info
238 : CommChannels srcCommChannels_;
239 : // dst comm channels info
240 : CommChannels dstCommChannels_;
241 : bool existFull_{false};
242 : bool pauseSubscriptionWhenFull_{true};
243 : uint32_t resIndex_{0U};
244 : std::map<uint64_t, EntityPtr> globalIdToSrcEntity_;
245 : std::map<uint64_t, EntityPtr> globalIdToDstEntity_;
246 : bool existDstAsyncMem_{false};
247 : };
248 : } // namespace dgw
249 : #endif
|