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 "local_cnt_notify.h"
12 :
13 : #include "exchange_ub_buffer_dto.h"
14 : #include "rdma_handle_manager.h"
15 : #include "local_ub_rma_buffer.h"
16 :
17 : namespace Hccl {
18 :
19 55 : LocalCntNotify::LocalCntNotify(RdmaHandle rdmaHandle, RtsCntNotify* notify)
20 55 : : rdmaHandle(rdmaHandle),
21 55 : notify(notify),
22 55 : tokenValue(GetUbToken()),
23 55 : addr(notify->GetAddr()),
24 55 : size(notify->GetSize())
25 : {
26 55 : std::pair<u64, u64> alignBuf = BufAlign(addr, size);
27 55 : bufKey_ = BufferKey<uintptr_t, u64>{alignBuf.first, alignBuf.second};
28 55 : auto tokenIdInfoPair = RdmaHandleManager::GetInstance().GetTokenIdInfo(rdmaHandle, bufKey_);
29 55 : tokenIdHandle_ = tokenIdInfoPair.first;
30 55 : tokenId = tokenIdInfoPair.second;
31 165 : HCCL_INFO("[LocalCntNotify] tokenIdHandle=0x[%llx].", tokenIdHandle_);
32 55 : HrtRaUbLocMemRegParam memRegInput(alignBuf.first, alignBuf.second, tokenValue, tokenIdHandle_);
33 55 : reqReg = HrtRaUbLocalMemReg(rdmaHandle, memRegInput);
34 55 : keySize = reqReg.keySize;
35 55 : memHandle = reqReg.handle;
36 55 : (void)memcpy_s(key, HRT_UB_MEM_KEY_MAX_LEN, reqReg.key, HRT_UB_MEM_KEY_MAX_LEN);
37 55 : }
38 :
39 2 : std::unique_ptr<Serializable> LocalCntNotify::GetExchangeDto()
40 : {
41 : std::unique_ptr<ExchangeUbBufferDto> dto
42 2 : = make_unique<ExchangeUbBufferDto>(addr, size, tokenValue, tokenId, keySize, notify->GetId());
43 2 : (void)memcpy_s(dto->key, HRT_UB_MEM_KEY_MAX_LEN, key, HRT_UB_MEM_KEY_MAX_LEN);
44 4 : return std::unique_ptr<Serializable>(dto.release());
45 2 : }
46 :
47 0 : std::string LocalCntNotify::Describe() const
48 : {
49 : return StringFormat(
50 0 : "UbLocalNotify:notify=%s, addr=0x%llx, keySize=%u, memHandle=0x%llx", notify->Describe().c_str(), addr, keySize,
51 0 : memHandle);
52 : }
53 :
54 55 : LocalCntNotify::~LocalCntNotify()
55 : {
56 55 : const bool ctxValid = rdmaHandle != nullptr && RdmaHandleManager::GetInstance().IsHandleValid(rdmaHandle);
57 :
58 55 : if (rdmaHandle && memHandle != 0) {
59 4 : if (!ctxValid) {
60 9 : HCCL_WARNING(
61 : "[LocalCntNotify][%s] skip HrtRaUbLocalMemUnreg, "
62 : "rdmaHandle=%p invalid (DeInit/DestroyAll done), memHandle=0x%llx",
63 : __func__, rdmaHandle, static_cast<unsigned long long>(memHandle));
64 : } else {
65 1 : DECTOR_TRY_CATCH("LocalCntNotify", HrtRaUbLocalMemUnreg(rdmaHandle, memHandle));
66 : }
67 4 : memHandle = 0;
68 : }
69 :
70 55 : if (ctxValid) {
71 5 : RdmaHandleManager::GetInstance().PutTokenIdInfo(rdmaHandle, bufKey_, tokenIdHandle_);
72 50 : } else if (rdmaHandle != nullptr) {
73 150 : HCCL_WARNING("[LocalCntNotify][%s] skip PutTokenIdInfo, rdmaHandle=%p invalid", __func__, rdmaHandle);
74 : }
75 55 : tokenIdHandle_ = 0; // 防御性置零
76 55 : }
77 : } // namespace Hccl
|