LCOV - code coverage report
Current view: top level - server/entity_manager - entity_manager.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 19 19
Test Date: 2026-07-28 10:54:05 Functions: 100.0 % 7 7

            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)
      49            7 :     {
      50            7 :         resIndex_ = resIndex;
      51            7 :     }
      52              :     ~EntityManager();
      53              : 
      54              :     EntityManager(const EntityManager &) = delete;
      55              :     EntityManager(const EntityManager &&) = delete;
      56              :     EntityManager &operator = (const EntityManager &) = delete;
      57              :     EntityManager &operator = (EntityManager &&) = delete;
      58              : 
      59              :     static EntityManager &Instance(const uint32_t resIndex = 0U);
      60              : 
      61              :     /**
      62              :      * get entity by id
      63              :      * @param eType: entity type
      64              :      * @param id: entity id
      65              :      * @return entity pointer
      66              :      */
      67              :     EntityPtr GetEntityById(const uint32_t queueType, const uint32_t deviceId, const EntityType eType,
      68              :                             const uint32_t id, const EntityDirection direction);
      69              : 
      70              :     EntityPtr DoGetEntity(const uint32_t queueType, const uint32_t deviceId, const EntityType eType,
      71              :                           const uint32_t id, const EntityDirection direction);
      72              : 
      73              :     EntityPtr GetSrcEntityByGlobalId(const uint32_t key, const uint32_t globalId) const;
      74              : 
      75              :     EntityPtr GetDstEntityByGlobalId(const uint32_t key, const uint32_t globalId) const;
      76              :     /**
      77              :      * get entities from group
      78              :      * @param groupId: group id
      79              :      * @return const std::vector<EntityPtr> &
      80              :      */
      81              :     const std::vector<EntityPtr> &GetEntitiesInGroup(const uint32_t groupId);
      82              : 
      83              :     /**
      84              :      * create group
      85              :      * @param groupId group id
      86              :      * @param entities entities in group
      87              :      * @return FSM_SUCCESS: success, other: failed
      88              :      */
      89              :     FsmStatus CreateGroup(const uint32_t groupId, std::vector<EntityPtr>& entities);
      90              : 
      91              :     /**
      92              :      * delete group
      93              :      * @param groupId group id
      94              :      * @return FSM_SUCCESS: success, other: failed
      95              :      */
      96              :     FsmStatus DeleteGroup(const uint32_t groupId);
      97              : 
      98              :     /**
      99              :      * create entity
     100              :      * @param etype entity for queue:EntityType::ENTITY_QUEUE, entity for tag:EntityType::ENTITY_TAG
     101              :      * @param state send entity:FsmState::FSM_IDLE_STATE, recv entity:FsmState::FSM_WAIT_PUSH_STATE
     102              :      * @param id queue id or group id or channel id (not tag id)
     103              :      * @param channel comm channel info, only for comm channel
     104              :      * @param hostGroupId host group id
     105              :      * @param groupPolicy group policy, only for group
     106              :      * @return return EntityPtr
     107              :      */
     108              :     EntityPtr CreateEntity(const EntityMaterial &material);
     109              : 
     110              :     /**
     111              :      * delete entity
     112              :      * @param type entity for queue:EntityType::ENTITY_QUEUE, entity for tag:EntityType::ENTITY_TAG
     113              :      * @param id queue id or tag id or group id
     114              :      * @return FsmStatus FSM_SUCCESS: success, other: failed
     115              :      */
     116              :     FsmStatus DeleteEntity(const uint32_t queueType, const uint32_t deviceId,
     117              :                            const EntityType eType, const uint32_t id,  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(const std::function<FsmStatus(CommChannels &, uint32_t &, uint32_t &)> procFunc,
     141              :                                    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
     184              :     {
     185           28 :         return existFull_;
     186              :     }
     187              : 
     188              :     /**
     189              :      * @brief mark some entity has been full
     190              :      */
     191            2 :     inline void SetExistFullEntity()
     192              :     {
     193            2 :         existFull_ = true;
     194            2 :     }
     195              : 
     196              :     /**
     197              :      * @brief show whether some entity has been Async Mem Buff
     198              :      * @return true: yes, false: no
     199              :      */
     200            4 :     inline bool IsExistAsyncMemEntity() const
     201              :     {
     202            4 :         return existDstAsyncMem_;
     203              :     }
     204              : 
     205              :     /**
     206              :      * @brief mark some entity has been Async Mem Buff
     207              :      */
     208            5 :     inline void SetExistAsyncMemEntity()
     209              :     {
     210            5 :         existDstAsyncMem_ = true;
     211            5 :     }
     212              : 
     213              :     /**
     214              :      * @brief set subscription_pause_policy for full queue
     215              :      */
     216           17 :     inline void SetSubscriptionPausePolicy(const bool pauseSubscriptionWhenFull)
     217              :     {
     218           17 :         pauseSubscriptionWhenFull_ = pauseSubscriptionWhenFull;
     219           17 :     }
     220              : 
     221              :     /**
     222              :      * @brief whether pause subscription
     223              :      * @return true: yes, false: no
     224              :      */
     225            3 :     inline bool ShouldPauseSubscirpiton() const
     226              :     {
     227            3 :         return pauseSubscriptionWhenFull_;
     228              :     }
     229              : 
     230              :     void CleanEntityMap(const uint32_t queueType, const uint32_t deviceId, const EntityType eType,
     231              :         const uint32_t id);
     232              : 
     233              : private:
     234              :     /**
     235              :      * @brief create new entity by type
     236              :      * @param etype entity type
     237              :      * @param id entity id
     238              :      * @param channel channel info, only for channel
     239              :      * @param hostGroupId host group id, for queue or channel in group
     240              :      * @param groupPolicy group policy, only for group
     241              :      * @return EntityPtr
     242              :      */
     243              :     EntityPtr AllocEntity(const EntityMaterial &material) const;
     244              : 
     245              :     void DoAllocEntity(const EntityMaterial &material, EntityPtr &entity) const;
     246              : 
     247              :     FsmStatus SupplyEventForRecvRequest(uint32_t msgType);
     248              : 
     249              :     // entity map
     250              :     IdToEntityMap idToEntity_;
     251              :     // group entity map
     252              :     GroupEntityMap groupEntityMap_;
     253              :     // src comm channels info
     254              :     CommChannels srcCommChannels_;
     255              :     // dst comm channels info
     256              :     CommChannels dstCommChannels_;
     257              :     bool existFull_{false};
     258              :     bool pauseSubscriptionWhenFull_{true};
     259              :     uint32_t resIndex_{0U};
     260              :     std::map<uint64_t, EntityPtr> globalIdToSrcEntity_;
     261              :     std::map<uint64_t, EntityPtr> globalIdToDstEntity_;
     262              :     bool existDstAsyncMem_{false};
     263              : };
     264              : }
     265              : #endif
        

Generated by: LCOV version 2.0-1