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 "hcclCommDfxLite.h"
12 : #include "hccl_common.h"
13 : #include "dfx_profiling_handler_lite.h"
14 : #include "res_pub.h"
15 : #include "dfx_circular_queue.h"
16 :
17 : namespace hccl {
18 : // HcclCommDfxLite构造函数实现
19 97 : HcclCommDfxLite::HcclCommDfxLite() {}
20 :
21 97 : HcclCommDfxLite::~HcclCommDfxLite()
22 : {
23 97 : delete profilingImpl_;
24 97 : profilingImpl_ = nullptr;
25 97 : if (queueInitialized_ && opInfoQueue_ != nullptr) {
26 27 : delete static_cast<Hccl::DfxOpInfoCircularQueue*>(opInfoQueue_);
27 27 : opInfoQueue_ = nullptr;
28 : }
29 97 : }
30 :
31 : // HcclCommDfxLite初始化流程 - 修改为返回HcclResult类型
32 27 : HcclResult HcclCommDfxLite::Init(u32 deviceId, const std::string& commTag, u32 rankSize, u32 localRank)
33 : {
34 27 : if (initializedFlag_) {
35 0 : return HCCL_SUCCESS;
36 : }
37 27 : HCCL_INFO("[HcclCommDfxLite][Init] Init begin deviceId[%u], commTag[%s]", deviceId, commTag.c_str());
38 27 : deviceId_ = deviceId;
39 27 : commTag_ = commTag;
40 27 : rankSize_ = rankSize;
41 27 : localRank_ = localRank;
42 :
43 27 : EXCEPTION_CATCH(profilingImpl_ = new HcclCommProfilingLite(deviceId_), return HCCL_E_PTR);
44 27 : CHK_RET(profilingImpl_->Init());
45 27 : opInfoQueue_ = new Hccl::DfxOpInfoCircularQueue();
46 27 : queueInitialized_ = true;
47 :
48 27 : Hccl::DfxProfilingHandlerLite::GetInstance().SetCachedCommInfo(
49 27 : Hccl::DfxProfilingHandlerLite::GetInstance().GetProfHashId(commTag_.c_str(), commTag_.length()), localRank_,
50 : rankSize_);
51 27 : Hccl::DfxProfilingHandlerLite::GetInstance().SetCachedChannelRemoteRankIdMap(&channelRemoteRankIdLite_);
52 27 : initializedFlag_ = true;
53 27 : return HCCL_SUCCESS;
54 : }
55 :
56 : // HcclCommDfxLite接口实现 - 修改为返回HcclResult类型
57 3 : HcclResult HcclCommDfxLite::SetCurrDfxOpInfo(const Hccl::DfxDfxOpInfo* newDfxOpInfo)
58 : {
59 3 : auto* queue = static_cast<Hccl::DfxOpInfoCircularQueue*>(opInfoQueue_);
60 3 : auto* slot = static_cast<Hccl::DfxDfxOpInfo*>(queue->NextSlot());
61 3 : if (slot != nullptr) {
62 3 : *slot = *newDfxOpInfo;
63 3 : auto it = Hccl::CMD_OP_TYPE_INFO_MAP.find(static_cast<HcclCMDType>(slot->opType));
64 3 : if (it == Hccl::CMD_OP_TYPE_INFO_MAP.end()) {
65 3 : HCCL_WARNING("[%s] opType[%u] not supported.", __func__, slot->opType);
66 : } else {
67 0 : slot->opType = static_cast<u8>(it->second.first);
68 : }
69 3 : slot->dataType = static_cast<u8>(Hccl::HcclDataTypeToDataType(static_cast<HcclDataType>(slot->dataType)));
70 3 : slot->hcclCommDfxLite = this;
71 3 : Hccl::DfxProfilingHandlerLite::GetInstance().SetCurrDfxOpInfo(slot);
72 : }
73 3 : return HCCL_SUCCESS;
74 : }
75 :
76 1 : void HcclCommDfxLite::ReportAllTasks(const std::vector<hccl::Thread*>& threads)
77 : {
78 1 : profilingImpl_->ReportAllTasks(threads);
79 1 : }
80 :
81 0 : HcclResult HcclCommDfxLite::ReportStreamTask(Hccl::TaskInfoCircularQueue* taskQueue)
82 : {
83 0 : profilingImpl_->ReportStreamTask(taskQueue);
84 0 : return HCCL_SUCCESS;
85 : }
86 :
87 0 : HcclResult HcclCommDfxLite::UpdateProfStat()
88 : {
89 0 : profilingImpl_->UpdateProfStat();
90 0 : return HCCL_SUCCESS;
91 : }
92 :
93 1 : void HcclCommDfxLite::AddChannelRemoteRankId(u64 handle, u32 remoteRankId)
94 : {
95 1 : HCCL_INFO("[%s] commTag[%s], handle[%llu], remoteRankId[%u]", __func__, commTag_.c_str(), handle, remoteRankId);
96 1 : channelRemoteRankIdLite_[handle] = remoteRankId;
97 1 : }
98 :
99 2 : u32 HcclCommDfxLite::GetChannelRemoteRankId(u64 handle)
100 : {
101 2 : if (handle == DFX_INVALID_U64) {
102 0 : return INVALID_UINT;
103 : }
104 2 : auto it = channelRemoteRankIdLite_.find(handle);
105 2 : if (UNLIKELY(it == channelRemoteRankIdLite_.end())) {
106 1 : HCCL_ERROR("[%s]handle[%llu] not found, commTag[%s]", __func__, handle, commTag_.c_str());
107 1 : return INVALID_UINT;
108 : }
109 1 : return it->second;
110 : }
111 :
112 6 : const void* HcclCommDfxLite::GetLatestDfxOpInfo() const
113 : {
114 6 : if (opInfoQueue_ == nullptr) {
115 2 : return nullptr;
116 : }
117 4 : auto* queue = static_cast<Hccl::DfxOpInfoCircularQueue*>(opInfoQueue_);
118 4 : if (queue->IsEmpty()) {
119 2 : return nullptr;
120 : }
121 2 : u16 end = queue->GetEnd();
122 2 : u16 latest = (end == 0) ? static_cast<u16>(queue->GetCapacity() - 1) : static_cast<u16>(end - 1);
123 2 : return queue->GetSlot(latest);
124 : }
125 : } // namespace hccl
|