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

Generated by: LCOV version 2.0-1