LCOV - code coverage report
Current view: top level - legacy/ascend910/platform/inc - mem_name_repository_pub.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 33.3 % 12 4
Test Date: 2026-08-18 17:47:01 Functions: 50.0 % 2 1

            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 MEM_NAME_REPOSITORY_PUB_H
      12              : #define MEM_NAME_REPOSITORY_PUB_H
      13              : 
      14              : #include <map>
      15              : #include <memory>
      16              : #include <string>
      17              : #include <mutex>
      18              : #include <securec.h>
      19              : #include "log.h"
      20              : #include "hccl_common.h"
      21              : 
      22              : namespace hccl {
      23              : /* IPC 相关 */
      24              : constexpr u32 HCCL_IPC_MEM_NAME_LEN = 65;
      25              : 
      26              : using SecIpcName_t = struct tagSecIpcName {
      27              :     u8 ipcName[HCCL_IPC_MEM_NAME_LEN] = {0};
      28              :     // 析构时,自动将内存数据清零
      29          115 :     ~tagSecIpcName()
      30              :     {
      31          115 :         s32 ret = memset_s(ipcName, HCCL_IPC_MEM_NAME_LEN, 0, HCCL_IPC_MEM_NAME_LEN);
      32          115 :         if (ret != EOK) {
      33            0 :             HCCL_ERROR(
      34              :                 "[Destroy][TagSecIpcName]errNo[0x%016llx] In SecIpcName, memset_s failed. "
      35              :                 "errorno[%d]",
      36              :                 HCCL_ERROR_CODE(HCCL_E_SYSCALL), ret);
      37              :         }
      38          115 :     }
      39              : };
      40              : 
      41              : struct IpcMemInfo {
      42            0 :     inline bool operator<(const IpcMemInfo& that) const
      43              :     {
      44            0 :         std::string strThat = std::to_string(reinterpret_cast<uintptr_t>(that.ptr)) + std::to_string(that.size)
      45            0 :                               + std::to_string(that.isSioToHccs);
      46            0 :         std::string strThis = std::to_string(reinterpret_cast<uintptr_t>(this->ptr)) + std::to_string(this->size)
      47            0 :                               + std::to_string(this->isSioToHccs);
      48            0 :         return (strThis < strThat);
      49            0 :     }
      50              :     void* ptr;
      51              :     u64 size;
      52              :     bool isSioToHccs; // sio和hccs并发时,同一块内存需要多次set ipc,通过标记区分
      53              : };
      54              : 
      55              : class MemNameRepository {
      56              : public:
      57              :     ~MemNameRepository();
      58              :     static MemNameRepository* GetInstance(s32 deviceLogicID);
      59              :     // 设置一个ipc mem, 并且返回ipc 名字
      60              :     HcclResult SetIpcMem(void* ptr, u64 size, u8* name, u32 nameLen, u64& offset, bool isSioToHccs = false);
      61              : 
      62              :     HcclResult SetIpcMem(void* ptr, u64 size, u8* name, u32 nameLen);
      63              : 
      64              :     HcclResult SetIpcMem(
      65              :         void* ptr, u64 size, u8* name, u32 nameLen, u64& offset, s32 pid, s32 sdid = INVALID_INT,
      66              :         bool isSioToHccs = false);
      67              : 
      68              :     // 抽离SetIpcMem的公共逻辑,在SetNameMap中查找MemName,若未找到则插入
      69              :     HcclResult FindIpcMem(IpcMemInfo& ipcMemInfo, u8* name, u32 nameLen);
      70              :     // 打开ipc nem
      71              :     HcclResult
      72              :     OpenIpcMem(void** ptr, u64 size, const u8* name, u32 nameLen, u64 offset, bool& isOpened, bool isSioToHccs = false);
      73              : 
      74              :     // 关闭ipc mem
      75              :     void CloseIpcMem(const u8* name);
      76              : 
      77              :     // 销毁ipc mem
      78              :     void DestroyIpcMem(void* ptr, u64 size, bool isSioToHccs = false);
      79              : 
      80              :     // 清空map
      81              :     void ClearMemNameRepository();
      82              : 
      83              :     HcclResult SetDeviceUnavailable(bool unavailable);
      84              : 
      85              : private:
      86              :     std::map<IpcMemInfo, SecIpcName_t> setNameMap_; // 记录用于input/output和mem name对应关系,由link模块填充
      87              :     std::map<IpcMemInfo, SecIpcName_t> openedNameMap_; // 记录已打开的mem name 与ptr对应关系
      88              :     std::map<IpcMemInfo, Referenced> setNameMapRef_;   // 记录用于input/output和mem name对应关系的引用计数
      89              :     std::map<IpcMemInfo, Referenced> openedNameMapRef_; // 记录已打开的mem name 与ptr对应关系的引用计数
      90              :     std::map<IpcMemInfo, IpcMemInfo> alignPtrMap_;      // 记录根据页表对齐前后的ipc mem对应关系
      91              :     std::mutex memMutex_;
      92              :     bool unavailable_{false};
      93              : 
      94              :     // 清空map不加锁
      95              :     void ClearMemNameRepositoryImpl();
      96              : };
      97              : } // namespace hccl
      98              : 
      99              : #endif /* __MEM_NAME_REPOSITRY_PUB_H__ */
        

Generated by: LCOV version 2.0-1