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