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 "hccl_comm_conn_mgr.h"
12 :
13 : using namespace std;
14 :
15 : namespace hccl {
16 0 : HcclCommConnMgr &HcclCommConnMgr::GetInstance()
17 : {
18 0 : static HcclCommConnMgr connMgr;
19 0 : HCCL_INFO("HcclCommConnMgr::GetInstance connMgr[%p]", &connMgr);
20 0 : return connMgr;
21 : }
22 :
23 0 : HcclCommConnMgr::HcclCommConnMgr()
24 : {
25 0 : }
26 :
27 0 : HcclCommConnMgr::~HcclCommConnMgr()
28 : {
29 0 : (void)UninitRa();
30 0 : }
31 :
32 0 : HcclResult HcclCommConnMgr::InitRa()
33 : {
34 0 : if (raInited_) {
35 0 : HCCL_DEBUG("InitRa has been already inited");
36 0 : return HCCL_SUCCESS;
37 : }
38 :
39 0 : CHK_RET(DlRaFunction::GetInstance().DlRaFunctionInit());
40 :
41 0 : raConfig_.phyId = defaultDevId_; // 暂缺获取物理id的手段
42 0 : raConfig_.nicPosition = static_cast<u32>(NICDeployment::NIC_DEPLOYMENT_HOST);
43 0 : CHK_RET(HrtRaInit(&raConfig_));
44 :
45 0 : raInited_ = true;
46 0 : HCCL_INFO("Ra has been inited successfully");
47 :
48 0 : return HCCL_SUCCESS;
49 : }
50 :
51 0 : HcclResult HcclCommConnMgr::UninitRa()
52 : {
53 0 : if (!raInited_) {
54 0 : HCCL_DEBUG("InitRa has been already uninited");
55 0 : return HCCL_SUCCESS;
56 : }
57 :
58 0 : CHK_RET(HrtRaDeInit(&raConfig_));
59 0 : raInited_ = false;
60 0 : HCCL_INFO("Ra has been uninited successfully");
61 :
62 0 : return HCCL_SUCCESS;
63 : }
64 :
65 0 : HcclResult HcclCommConnMgr::AddAndGetCommConn(HcclCommConn *&commConn)
66 : {
67 0 : commConn = new(nothrow) HcclCommConn();
68 0 : CHK_PTR_NULL(commConn);
69 :
70 0 : lock_guard<mutex> lock(commConnMtx_);
71 0 : auto result = commConnSet_.insert(commConn);
72 0 : if (result.second && commConnSet_.size() == COMM_CONN_NUM_ONE) {
73 0 : CHK_RET(InitRa());
74 0 : CHK_RET(InitExternalInput());
75 : }
76 0 : HCCL_INFO("AddAndGetCommConn commConnSet_ size[%u]", commConnSet_.size());
77 :
78 0 : return HCCL_SUCCESS;
79 0 : }
80 :
81 0 : HcclResult HcclCommConnMgr::AddCommConn(HcclCommConn *&commConn)
82 : {
83 0 : CHK_PTR_NULL(commConn);
84 :
85 0 : lock_guard<mutex> lock(commConnMtx_);
86 0 : commConnSet_.insert(commConn);
87 :
88 0 : return HCCL_SUCCESS;
89 0 : }
90 :
91 0 : bool HcclCommConnMgr::IsExceedMaxLinkNum(u32 role)
92 : {
93 0 : lock_guard<mutex> lock(commConnMtx_);
94 0 : if (role == SERVER_ROLE_SOCKET) {
95 : // server侧有自身的一个comm, 因此判断最大通信连接数的时候要加1
96 0 : return commConnSet_.size() >= (MAX_CONN_LINK_NUM + 1);
97 : }
98 0 : return commConnSet_.size() > MAX_CONN_LINK_NUM;
99 0 : }
100 :
101 0 : bool HcclCommConnMgr::IsExistCommConn(HcclAddr &connectAddr)
102 : {
103 0 : std::unique_lock<std::mutex> lock(connectCommMapMtx_);
104 0 : return connectCommMap_.find(connectAddr.info.tcp.ipv4Addr) != connectCommMap_.end();
105 0 : }
106 :
107 0 : void HcclCommConnMgr::InsertConnectCommMap(HcclAddr &connectAddr, HcclConn &conn)
108 : {
109 0 : std::unique_lock<std::mutex> lock(connectCommMapMtx_);
110 0 : connectCommMap_.insert(std::make_pair(connectAddr.info.tcp.ipv4Addr, conn));
111 0 : }
112 :
113 0 : void HcclCommConnMgr::DeleteConnectCommMap(HcclAddr &connectAddr)
114 : {
115 0 : std::unique_lock<std::mutex> lock(connectCommMapMtx_);
116 0 : connectCommMap_.erase(connectAddr.info.tcp.ipv4Addr);
117 0 : }
118 :
119 0 : HcclResult HcclCommConnMgr::DelCommConn(HcclCommConn *commConn)
120 : {
121 : {
122 0 : lock_guard<mutex> lock(commConnMtx_);
123 0 : auto result = commConnSet_.erase(commConn);
124 0 : if (result == 0) {
125 0 : HCCL_ERROR("commConn is not found in commConnSet");
126 0 : return HCCL_E_NOT_FOUND;
127 : }
128 0 : HCCL_INFO("DelCommConn commConnSet_ size[%u]", commConnSet_.size());
129 0 : }
130 :
131 : // 对外接口已经统一校验过commConn
132 0 : delete commConn;
133 0 : commConn = nullptr;
134 :
135 : // commConnSet_.size() == 0的时候说明P侧进程业务完成要退出
136 0 : lock_guard<mutex> lock(commConnMtx_);
137 0 : if (commConnSet_.size() == 0) {
138 0 : (void)UninitRa();
139 : }
140 0 : return HCCL_SUCCESS;
141 0 : }
142 :
143 : }
|