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