LCOV - code coverage report
Current view: top level - legacy/ascend910/platform/typical - typical_window_mem.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 80.4 % 56 45
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 6 6

            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              : #include "typical_window_mem.h"
      12              : #include "adapter_rts_common.h"
      13              : 
      14              : namespace hccl {
      15           60 : TypicalWindowMem& TypicalWindowMem::GetInstance()
      16              : {
      17          126 :     static TypicalWindowMem typicalWindowMem[MAX_MODULE_DEVICE_NUM + 1];
      18           60 :     s32 deviceLogicId = INVALID_INT;
      19           60 :     HcclResult ret = hrtGetDevice(&deviceLogicId);
      20           60 :     if (ret == HCCL_SUCCESS && (static_cast<u32>(deviceLogicId) < MAX_MODULE_DEVICE_NUM)) {
      21           60 :         HCCL_INFO("[TypicalWindowMem::GetInstance]deviceLogicID[%d]", deviceLogicId);
      22           60 :         return typicalWindowMem[deviceLogicId];
      23              :     }
      24            0 :     HCCL_WARNING("[TypicalWindowMem::GetInstance]deviceLogicID[%d] is invalid, ret[%d]", deviceLogicId, ret);
      25            0 :     return typicalWindowMem[MAX_MODULE_DEVICE_NUM];
      26              : }
      27              : 
      28           66 : TypicalWindowMem::TypicalWindowMem() {}
      29              : 
      30           66 : TypicalWindowMem::~TypicalWindowMem() { FreeAllWinowMem(); }
      31              : 
      32           30 : HcclResult TypicalWindowMem::AllocWindowMem(void** ptr, uint64_t size)
      33              : {
      34           30 :     HCCL_DEBUG("[TypicalWindowMem][AllocWindowMem]start alloc window mem to [%p] of size[%llu].", ptr, size);
      35              :     // Check ptr and size validation.
      36           30 :     CHK_PTR_NULL(ptr);
      37           30 :     CHK_PRT_RET(
      38              :         size < 1, HCCL_ERROR("[TypicalWindowMem][AllocWindowMem]size[%lu], cannot alloc window mem less than 1.", size),
      39              :         HCCL_E_PARA);
      40              : 
      41              :     // Check whether the addr already exist in window mem map.
      42           30 :     std::unique_lock<std::mutex> lockWindowMemMap(windowMemMapMutex_);
      43           30 :     if (*ptr != nullptr) {
      44           30 :         uint64_t inputAddr = reinterpret_cast<uintptr_t>(*ptr);
      45           30 :         auto wmIter = windowMemMap_.find(inputAddr);
      46           30 :         if (wmIter != windowMemMap_.end()) {
      47            0 :             HCCL_ERROR("[TypicalWindowMem][AllocWindowMem]addr[%p] already allocated.", *ptr);
      48            0 :             return HCCL_E_PARA;
      49              :         }
      50           30 :         HCCL_WARNING(
      51              :             "[TypicalWindowMem][AllocWindowMem]addr[%p] is not nullptr, "
      52              :             "we will overwrite it with the new allocated address.",
      53              :             *ptr);
      54              :     }
      55              :     // Alloc window mem.
      56           30 :     CHK_RET(hrtMalloc(ptr, size));
      57           30 :     CHK_PRT_RET(
      58              :         (*ptr) == nullptr, HCCL_ERROR("[TypicalWindowMem][AllocWindowMem]In typical notify src buffer, malloc failed."),
      59              :         HCCL_E_MEMORY);
      60              : 
      61              :     // Add allocated mem into window mem map。
      62           30 :     uint64_t allocatedAddr = reinterpret_cast<uintptr_t>(*ptr);
      63           30 :     windowMemMap_[allocatedAddr] = size;
      64           30 :     HCCL_INFO(
      65              :         "[TypicalWindowMem][AllocWindowMem]alloc window memory success, addr[%p], size[%llu]. "
      66              :         "please register mr before use",
      67              :         *ptr, size);
      68           30 :     return HCCL_SUCCESS;
      69           30 : }
      70              : 
      71           30 : HcclResult TypicalWindowMem::FreeWindowMem(void* ptr)
      72              : {
      73           30 :     HCCL_DEBUG(
      74              :         "[TypicalWindowMem][FreeWindowMem]start free window mem on [%p], please deregister mr before free.", ptr);
      75           30 :     CHK_PTR_NULL(ptr);
      76              :     // Check whether the addr exist in window mem map. Remove allocated mem from window mem map if exists.
      77           30 :     uint64_t addr = reinterpret_cast<uintptr_t>(ptr);
      78           30 :     std::unique_lock<std::mutex> lockWindowMemMap(windowMemMapMutex_);
      79           30 :     auto wmIter = windowMemMap_.find(addr);
      80           30 :     if (wmIter == windowMemMap_.end()) {
      81            0 :         HCCL_ERROR("[TypicalWindowMem][AllocWindowMem]addr[%p] were not allocated or already freed.", ptr);
      82            0 :         return HCCL_E_PARA;
      83              :     }
      84              :     // Free window mem.
      85           30 :     CHK_RET(hrtFree(ptr));
      86           30 :     windowMemMap_.erase(wmIter);
      87           30 :     ptr = nullptr;
      88           30 :     HCCL_INFO("[TypicalWindowMem][AllocWindowMem]free window memory success, addr[%p].", ptr);
      89           30 :     return HCCL_SUCCESS;
      90           30 : }
      91              : 
      92           66 : HcclResult TypicalWindowMem::FreeAllWinowMem()
      93              : {
      94           66 :     std::unique_lock<std::mutex> lockWindowMemMap(windowMemMapMutex_);
      95           66 :     if (!windowMemMap_.empty()) {
      96            0 :         for (auto& wmIter : windowMemMap_) {
      97            0 :             auto ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(wmIter.first));
      98            0 :             if (ptr != nullptr) {
      99            0 :                 CHK_RET(hrtFree(ptr));
     100              :             }
     101              :         }
     102            0 :         windowMemMap_.clear();
     103              :     }
     104           66 :     HCCL_INFO("[TypicalWindowMem][FreeAllWinowMem]free all window memory success.");
     105           66 :     return HCCL_SUCCESS;
     106           66 : }
     107              : } // namespace hccl
        

Generated by: LCOV version 2.0-1