LCOV - code coverage report
Current view: top level - base_comm/resources/ccu/ccu_instance - ccu_instance_mgr.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 94.2 % 103 97
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 11 11

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2026 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              : #include "ccu_instance_mgr.h"
      12              : 
      13              : #include "ccu_log.h"
      14              : #include "hccl_common.h"
      15              : 
      16              : namespace hcomm {
      17              : 
      18          132 : CcuInstanceMgr::~CcuInstanceMgr()
      19              : {
      20          132 :     if (!initializedFlag_) {
      21          132 :         return;
      22              :     }
      23              : 
      24            0 :     (void)Deinit();
      25          264 : }
      26              : 
      27         1975 : CcuInstanceMgr& CcuInstanceMgr::GetInstance(const int32_t deviceLogicId)
      28              : {
      29         2107 :     static CcuInstanceMgr instanceMgrs[MAX_MODULE_DEVICE_NUM + 1];
      30              : 
      31         1975 :     int32_t devLogicId = deviceLogicId;
      32         1975 :     if (devLogicId < 0 || static_cast<uint32_t>(devLogicId) >= MAX_MODULE_DEVICE_NUM) {
      33            0 :         HCCL_WARNING(
      34              :             "[CcuInstanceMgr][%s] use the backup device, devLogicId[%d] should be "
      35              :             "less than %u.",
      36              :             __func__, devLogicId, MAX_MODULE_DEVICE_NUM);
      37            0 :         devLogicId = MAX_MODULE_DEVICE_NUM; // 使用备份设备
      38              :     }
      39              : 
      40         1975 :     instanceMgrs[devLogicId].devLogicId_ = devLogicId;
      41         1975 :     return instanceMgrs[devLogicId];
      42              : }
      43              : 
      44            6 : CcuResult CcuInstanceMgr::Init()
      45              : {
      46            6 :     std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
      47            6 :     if (initializedFlag_) {
      48            0 :         return CcuResult::CCU_SUCCESS;
      49              :     }
      50              : 
      51            6 :     initializedFlag_ = true;
      52            6 :     insMap_.clear();
      53            6 :     return CcuResult::CCU_SUCCESS;
      54            6 : }
      55              : 
      56          117 : CcuResult CcuInstanceMgr::Deinit()
      57              : {
      58          117 :     std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
      59          117 :     insMap_.clear();
      60          117 :     (void)resDescMgr_.Deinit();
      61          117 :     initializedFlag_ = false;
      62          117 :     return CcuResult::CCU_SUCCESS;
      63          117 : }
      64              : 
      65            6 : CcuResult CcuInstanceMgr::CreateByInsType(const CcuInstanceType insType, CcuInsHandle& insHandle)
      66              : {
      67            6 :     std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
      68              : 
      69            6 :     std::unique_ptr<CcuInstance> instance{nullptr};
      70            6 :     EXCEPTION_CATCH(instance = std::make_unique<CcuInstance>(), return CcuResult::CCU_E_INTERNAL);
      71              : 
      72            6 :     CCU_CHK_RET(instance->InitByInsType(insType));
      73              : 
      74            6 :     instanceId_ += 1;
      75            6 :     instance->SetHandle(instanceId_);
      76            6 :     EXCEPTION_CATCH(insMap_.emplace(instanceId_, std::move(instance)), return CcuResult::CCU_E_INTERNAL);
      77            6 :     insHandle = instanceId_;
      78            6 :     return CcuResult::CCU_SUCCESS;
      79            6 : }
      80              : 
      81          181 : CcuInstance* CcuInstanceMgr::Get(CcuInsHandle insHandle) const
      82              : {
      83          181 :     std::shared_lock<std::shared_timed_mutex> lock(insMapMutex_);
      84          181 :     auto it = insMap_.find(insHandle);
      85          181 :     if (it == insMap_.end()) {
      86            4 :         HCCL_ERROR("[CcuInstanceMgr][%s] handle[%llx] is not existed.", __func__, insHandle);
      87            4 :         return nullptr;
      88              :     }
      89              : 
      90          177 :     return it->second.get();
      91          181 : }
      92              : 
      93           70 : CcuResult CcuInstanceMgr::Destroy(CcuInsHandle insHandle)
      94              : {
      95           70 :     std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
      96           70 :     auto it = insMap_.find(insHandle);
      97           70 :     if (it == insMap_.end()) {
      98            4 :         HCCL_ERROR("[CcuInstanceMgr][%s] handle[%llx] is not existed.", __func__, insHandle);
      99            4 :         return CcuResult::CCU_E_NOT_FOUND;
     100              :     }
     101              : 
     102           66 :     insMap_.erase(it);
     103           66 :     return CcuResult::CCU_SUCCESS;
     104           70 : }
     105              : 
     106           62 : CcuResult CcuInstanceMgr::CreateByResDescs(const CcuResDesc* descs[], uint32_t descNum, CcuInsHandle& insHandle)
     107              : {
     108           62 :     std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
     109              : 
     110           62 :     std::unique_ptr<CcuInstance> instance{nullptr};
     111           62 :     EXCEPTION_CATCH(instance = std::make_unique<CcuInstance>(), return CcuResult::CCU_E_INTERNAL);
     112              : 
     113           62 :     CCU_CHK_RET(instance->InitByResDescs(descs, descNum));
     114              : 
     115           60 :     instanceId_ += 1;
     116           60 :     instance->SetHandle(instanceId_);
     117           60 :     EXCEPTION_CATCH(insMap_.emplace(instanceId_, std::move(instance)), return CcuResult::CCU_E_INTERNAL);
     118           60 :     insHandle = instanceId_;
     119           60 :     return CcuResult::CCU_SUCCESS;
     120           62 : }
     121              : 
     122            2 : CcuResult CcuInstanceMgr::CreateByAllRes(CcuInsHandle& insHandle)
     123              : {
     124            2 :     std::unique_lock<std::shared_timed_mutex> lock(insMapMutex_);
     125              : 
     126            2 :     std::unique_ptr<CcuInstance> instance{nullptr};
     127            2 :     EXCEPTION_CATCH(instance = std::make_unique<CcuInstance>(), return CcuResult::CCU_E_INTERNAL);
     128              : 
     129            2 :     CCU_CHK_RET(instance->InitByAllRes());
     130              : 
     131            1 :     instanceId_ += 1;
     132            1 :     instance->SetHandle(instanceId_);
     133            1 :     EXCEPTION_CATCH(insMap_.emplace(instanceId_, std::move(instance)), return CcuResult::CCU_E_INTERNAL);
     134            1 :     insHandle = instanceId_;
     135            1 :     return CcuResult::CCU_SUCCESS;
     136            2 : }
     137              : 
     138            2 : CcuResult CcuInstanceMgr::QueryInsResDesc(CcuInsHandle& ccuInsHandle, uint8_t dieId, HcommCcuResDescHandle& resDesc)
     139              : {
     140            2 :     std::shared_lock<std::shared_timed_mutex> lock(insMapMutex_);
     141              : 
     142            2 :     auto it = insMap_.find(ccuInsHandle);
     143            2 :     if (it == insMap_.end()) {
     144            0 :         HCCL_ERROR("[CcuInstanceMgr][%s] handle[%llx] is not existed.", __func__, ccuInsHandle);
     145            0 :         return CcuResult::CCU_E_NOT_FOUND;
     146              :     }
     147              : 
     148              :     // 从 ccuIns 持有的 totalResDescs_ 取该 die 的资源描述符,逐项写入入参 resDesc
     149            2 :     const auto& totalDesc = it->second->GetTotalResDescs(dieId);
     150            2 :     uint32_t num = 0;
     151            2 :     CCU_CHK_RET(totalDesc.QueryResNum(ResType::LOOP, num));
     152            2 :     CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::LOOP, num));
     153            2 :     CCU_CHK_RET(totalDesc.QueryResNum(ResType::MS, num));
     154            2 :     CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::MS, num));
     155            2 :     CCU_CHK_RET(totalDesc.QueryResNum(ResType::CKE, num));
     156            2 :     CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::CKE, num));
     157            2 :     CCU_CHK_RET(totalDesc.QueryResNum(ResType::XN, num));
     158            2 :     CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::XN, num));
     159            2 :     CCU_CHK_RET(totalDesc.QueryResNum(ResType::GSA, num));
     160            2 :     CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::GSA, num));
     161            2 :     CCU_CHK_RET(totalDesc.QueryResNum(ResType::MISSION, num));
     162            2 :     CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::MISSION, num));
     163            2 :     CCU_CHK_RET(totalDesc.QueryResNum(ResType::INS, num));
     164            2 :     CCU_CHK_RET(resDescMgr_.SetResNum(resDesc, ResType::INS, num));
     165              : 
     166            2 :     return CcuResult::CCU_SUCCESS;
     167            2 : }
     168              : 
     169         1648 : CcuResDescMgr& CcuInstanceMgr::GetResDescMgr() { return resDescMgr_; }
     170              : 
     171              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1