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 "ub_conn_lite_mgr.h"
11 :
12 : namespace Hccl {
13 :
14 2 : UbConnLiteMgr::UbConnLiteMgr() {}
15 :
16 2 : UbConnLiteMgr::~UbConnLiteMgr() { ubConnLiteMap.clear(); }
17 :
18 6 : std::string UbConnLiteMgr::GetKey(const UbConnLiteParam& liteParam) const
19 : {
20 : // dieId + funcId + jettyId + tp + eid 可唯一确定一个connection
21 6 : std::string result;
22 12 : result += to_string(liteParam.dieId) + to_string(liteParam.funcId) + to_string(liteParam.jettyId)
23 18 : + to_string(liteParam.tpn);
24 6 : result += Bytes2hex(liteParam.rmtEid.raw, sizeof(liteParam.rmtEid.raw));
25 6 : return result;
26 0 : }
27 :
28 6 : bool UbConnLiteMgr::IsExist(const std::string& key)
29 : {
30 6 : if (ubConnLiteMap.find(key) == ubConnLiteMap.end()) {
31 5 : return false;
32 : }
33 1 : return true;
34 : }
35 :
36 16 : UbConnLiteMgr& UbConnLiteMgr::GetInstance()
37 : {
38 16 : static UbConnLiteMgr ubConnLiteMgr;
39 16 : return ubConnLiteMgr;
40 : }
41 :
42 1 : RmaConnLite* UbConnLiteMgr::Get(std::vector<char>& uniqueId)
43 : {
44 1 : UbConnLiteParam liteParam(uniqueId);
45 1 : auto key = GetKey(liteParam);
46 1 : if (IsExist(key)) {
47 0 : return ubConnLiteMap[key].get();
48 : }
49 :
50 1 : ubConnLiteMap[key] = make_unique<UbConnLite>(liteParam);
51 1 : return ubConnLiteMap[key].get();
52 1 : }
53 :
54 5 : void UbConnLiteMgr::Clear(std::vector<char>& uniqueId)
55 : {
56 5 : UbConnLiteParam liteParam(uniqueId);
57 5 : auto key = GetKey(liteParam);
58 5 : if (!IsExist(key)) {
59 4 : return;
60 : }
61 :
62 1 : ubConnLiteMap.erase(key);
63 5 : }
64 :
65 : } // namespace Hccl
|