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