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 "aicpu_kfc_utils.h"
12 :
13 : #include "common/aicpu_hccl_common.h"
14 : #include "utils/hccl_aicpu_utils.h"
15 : #include "dfx/aicpu_profiling_manager.h"
16 :
17 : using namespace HcclApi;
18 : namespace {
19 : #define HCCL_DLOG_DEFAULT 0x10
20 : #define HCCL_LOG_BY_LEVEL(level, format, ...) \
21 : do { \
22 : switch (level) { \
23 : case DLOG_INFO: \
24 : HCCL_INFO(format, ##__VA_ARGS__); \
25 : break; \
26 : case DLOG_ERROR: \
27 : HCCL_ERROR(format, ##__VA_ARGS__); \
28 : break; \
29 : default: \
30 : HCCL_RUN_INFO(format, ##__VA_ARGS__); \
31 : break; \
32 : } \
33 : } while (0)
34 : } // namespace
35 :
36 3 : void AicpuKfcUtils::PrintKFCTask(const KFCTask& task)
37 : {
38 3 : HCCL_INFO("KFCTask.inputA Addr %lu", task.inputA);
39 3 : HCCL_INFO("KFCTask.outputC Addr %lu", task.outputC);
40 3 : HCCL_INFO("KFCTask.commOut Addr %lu", task.commOut);
41 3 : HCCL_INFO("KFCTask.context Addr %lu", task.context);
42 3 : HCCL_INFO("KFCTask.workSpace Addr %lu", task.workSpace);
43 3 : }
44 :
45 80 : void AicpuKfcUtils::PrintTilingData(const HcclKFCTilingData& tilingData, bool errorFlag)
46 : {
47 80 : const s32 logLevel = (errorFlag ? DLOG_ERROR : DLOG_INFO);
48 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.sendOff %lu.", tilingData.sendOff);
49 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.recvOff %lu.", tilingData.recvOff);
50 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.tailSendOff %lu.", tilingData.tailSendOff);
51 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.tailRecvOff %lu.", tilingData.tailRecvOff);
52 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.sendCnt %lu.", tilingData.sendCnt);
53 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.recvCnt %lu.", tilingData.recvCnt);
54 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.tailSendCnt %lu.", tilingData.tailSendCnt);
55 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.tailRecvCnt %lu.", tilingData.tailRecvCnt);
56 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.totalCnt %lu.", tilingData.totalCnt);
57 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.turnNum %u.", tilingData.turnNum);
58 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.tailNum %u.", tilingData.tailNum);
59 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.stride %u.", tilingData.stride);
60 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.workspaceOff %u.", tilingData.workspaceOff);
61 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.notifyOff %u.", tilingData.notifyOff);
62 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.notifyBeginCnt %u.", tilingData.notifyBeginCnt);
63 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.notifyEndCnt %u.", tilingData.notifyEndCnt);
64 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.useBufferType %u.", tilingData.useBufferType);
65 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.funID %u.", tilingData.funID);
66 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.dataType %u.", tilingData.dataType);
67 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.groupNum %u.", tilingData.groupNum);
68 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.reuseMode %u.", tilingData.reuseMode);
69 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.commType %u.", tilingData.commType);
70 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.reduceOp %u.", tilingData.reduceOp);
71 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.commOrder %u.", tilingData.commOrder);
72 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.waitPolicy %u.", tilingData.waitPolicy);
73 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.rspPolicy %u.", tilingData.rspPolicy);
74 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.exitPolicy %u.", tilingData.exitPolicy);
75 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.commAlg %u.", tilingData.commAlg);
76 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.taskType %u.", tilingData.taskType);
77 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.debugMode %u.", tilingData.debugMode);
78 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.stepSize %u.", tilingData.stepSize);
79 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.sendArgIndex %u.", tilingData.sendArgIndex);
80 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.recvArgIndex %u.", tilingData.recvArgIndex);
81 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.commOutArgIndex %u.", tilingData.commOutArgIndex);
82 80 : HCCL_LOG_BY_LEVEL(logLevel, "HcclKFCTilingData.hasCommOut %u.", tilingData.hasCommOut);
83 80 : }
84 :
85 4 : void AicpuKfcUtils::PrintTilingData(const Mc2InitTilingInner& tilingData, bool errorFlag)
86 : {
87 4 : const s32 logLevel = (errorFlag ? DLOG_ERROR : DLOG_INFO);
88 4 : HCCL_LOG_BY_LEVEL(logLevel, "version %lu.", static_cast<u64>(tilingData.version));
89 4 : HCCL_LOG_BY_LEVEL(logLevel, "mc2HcommCnt %lu.", static_cast<u64>(tilingData.mc2HcommCnt));
90 4 : HCCL_LOG_BY_LEVEL(logLevel, "debugMode %lu.", static_cast<u64>(tilingData.debugMode));
91 4 : HCCL_LOG_BY_LEVEL(logLevel, "preparePosition %lu.", static_cast<u64>(tilingData.preparePosition));
92 4 : HCCL_LOG_BY_LEVEL(logLevel, "queueNum %lu.", static_cast<u64>(tilingData.queueNum));
93 4 : HCCL_LOG_BY_LEVEL(logLevel, "commBlockNum %lu.", static_cast<u64>(tilingData.commBlockNum));
94 4 : }
95 :
96 1 : void AicpuKfcUtils::PrintTilingData(const std::string& desc, const Mc2CcTilingInner& tilingData, bool runFlag)
97 : {
98 1 : const s32 logLevel = (runFlag ? HCCL_DLOG_DEFAULT : DLOG_INFO);
99 5 : HCCL_LOG_BY_LEVEL(
100 : logLevel,
101 : "%s: Mc2CcTilingInner[skipLocalRankCopy %u, skipBufferWindowCopy %u, stepSize %u, "
102 : "version %u, groupName %s, algConfig %s, opType %d, reduceType %d]",
103 : desc.c_str(), tilingData.skipLocalRankCopy, tilingData.skipBufferWindowCopy, tilingData.stepSize,
104 : tilingData.version, std::string(tilingData.groupName).c_str(), std::string(tilingData.algConfig).c_str(),
105 : tilingData.opType, tilingData.reduceType);
106 1 : }
107 :
108 89 : void AicpuKfcUtils::PrintMsg(const std::string& desc, const HcclMsg& msg, bool runFlag)
109 : {
110 89 : const s32 logLevel = (runFlag ? HCCL_DLOG_DEFAULT : DLOG_INFO);
111 89 : const HcclTilingVersion ver = msg.addMsg.v0Msg.version;
112 89 : if (ver != HcclTilingVersion::DEPRECATED_TILING_VERSION) {
113 29 : HCCL_LOG_BY_LEVEL(
114 : logLevel,
115 : "%s: Msg[version %u, commType %u, opType %u, sendBuffer %p, recvBuffer %p, "
116 : "dataCnt %lu, strideCount %lu, ccOpTilingData %#llx, valid %u, hcclDataType %u, "
117 : "repeatCnt %u, selfHandleID %d, seqNum %u, xorCheck %u]",
118 : desc.c_str(), static_cast<u32>(msg.addMsg.v0Msg.version), static_cast<u32>(msg.commType.msgType),
119 : static_cast<u32>(msg.opType), msg.sendBuffer, msg.recvBuffer, msg.dataCnt, msg.strideCount,
120 : msg.addMsg.v1Msg.ccOpTilingData, msg.addMsg.v1Msg.valid, static_cast<u32>(msg.addMsg.v1Msg.hcclDataType),
121 : msg.addMsg.v1Msg.repeatCnt, static_cast<s32>(msg.addMsg.v1Msg.selfHandleID), msg.addMsg.v1Msg.seqNum,
122 : msg.addMsg.v1Msg.xorCheck);
123 29 : if (ver == HcclTilingVersion::NEW_TILING_VERSION && msg.addMsg.v1Msg.ccOpTilingData != 0UL) {
124 1 : PrintTilingData(desc, *(reinterpret_cast<Mc2CcTilingInner*>(msg.addMsg.v1Msg.ccOpTilingData)), runFlag);
125 : }
126 : } else {
127 60 : HCCL_LOG_BY_LEVEL(
128 : logLevel,
129 : "%s: Msg[version %u, commType %u, opType %u, sendBuffer %p, recvBuffer %p, "
130 : "dataCnt %lu, strideCount %lu, hcclDataType %u, p2pSrcDestRankId %u, valid %u, "
131 : "repeatCnt %u, everyTurnRsp %u, everyTurnWait %u, commDepGroupID %d, "
132 : "commDepHandleID %d, selfHandleID %d, seqNum %u, xorCheck %u]",
133 : desc.c_str(), static_cast<u32>(msg.addMsg.v0Msg.version), static_cast<u32>(msg.commType.msgType),
134 : static_cast<u32>(msg.opType), msg.sendBuffer, msg.recvBuffer, msg.dataCnt, msg.strideCount,
135 : static_cast<u32>(msg.addMsg.v0Msg.hcclDataType), msg.addMsg.v0Msg.p2pSrcDestRankId, msg.addMsg.v0Msg.valid,
136 : msg.addMsg.v0Msg.repeatCnt, msg.addMsg.v0Msg.everyTurnRsp, msg.addMsg.v0Msg.everyTurnWait,
137 : msg.addMsg.v0Msg.commDepGroupID, msg.addMsg.v0Msg.commDepHandleID, msg.addMsg.v0Msg.selfHandleID,
138 : msg.addMsg.v0Msg.seqNum, msg.addMsg.v0Msg.xorCheck);
139 : }
140 89 : }
141 :
142 90 : std::string AicpuKfcUtils::GetMsgSimpleStr(const HcclMsg& msg)
143 : {
144 90 : const HcclTilingVersion ver = msg.addMsg.v0Msg.version;
145 90 : std::stringstream ss;
146 90 : ss << std::to_string(static_cast<u32>(msg.commType.msgType)) << ",";
147 90 : ss << std::to_string(static_cast<u32>(msg.opType)) << ",";
148 90 : ss << "0x" << std::hex << msg.sendBuffer << ",";
149 90 : ss << "0x" << std::hex << msg.recvBuffer << ",";
150 90 : ss << std::to_string(msg.dataCnt) << ",";
151 90 : ss << std::to_string(msg.strideCount) << ",";
152 90 : if (ver != HcclTilingVersion::DEPRECATED_TILING_VERSION) {
153 66 : ss << "0x" << std::hex << msg.addMsg.v1Msg.ccOpTilingData << ",";
154 66 : ss << "0x" << std::hex << msg.addMsg.v1Msg.valid << ",";
155 66 : ss << std::to_string(static_cast<u32>(msg.addMsg.v1Msg.hcclDataType)) << ",";
156 66 : ss << std::to_string(msg.addMsg.v1Msg.repeatCnt) << ",";
157 66 : ss << std::to_string(static_cast<s32>(msg.addMsg.v1Msg.selfHandleID)) << ",";
158 66 : ss << std::to_string(static_cast<u32>(msg.addMsg.v1Msg.seqNum)) << ",";
159 66 : ss << std::to_string(static_cast<u32>(msg.addMsg.v1Msg.version)) << ",";
160 66 : ss << std::to_string(msg.addMsg.v1Msg.xorCheck) << ".";
161 : } else {
162 24 : ss << std::to_string(static_cast<u32>(msg.addMsg.v0Msg.hcclDataType)) << ",";
163 24 : ss << std::to_string(msg.addMsg.v0Msg.p2pSrcDestRankId) << ",";
164 24 : ss << "0x" << std::hex << msg.addMsg.v0Msg.valid << ",";
165 24 : ss << std::to_string(static_cast<u32>(msg.addMsg.v0Msg.repeatCnt)) << ",";
166 24 : ss << std::to_string(static_cast<u32>(msg.addMsg.v0Msg.everyTurnRsp)) << ",";
167 24 : ss << std::to_string(static_cast<u32>(msg.addMsg.v0Msg.everyTurnWait)) << ",";
168 24 : ss << std::to_string(static_cast<s32>(msg.addMsg.v0Msg.commDepGroupID)) << ",";
169 24 : ss << std::to_string(static_cast<s32>(msg.addMsg.v0Msg.commDepHandleID)) << ",";
170 24 : ss << std::to_string(static_cast<s32>(msg.addMsg.v0Msg.selfHandleID)) << ",";
171 24 : ss << std::to_string(static_cast<u32>(msg.addMsg.v0Msg.seqNum)) << ",";
172 24 : ss << std::to_string(static_cast<u32>(msg.addMsg.v0Msg.version)) << ",";
173 24 : ss << std::to_string(msg.addMsg.v0Msg.xorCheck) << ",";
174 : }
175 180 : return ss.str();
176 90 : }
177 :
178 5 : std::string AicpuKfcUtils::GetMsgSimpleStr(u32 rankSize, const HcclMsgExt& msg)
179 : {
180 5 : std::stringstream ss;
181 5 : ss << "sendCounts:";
182 2013 : for (u32 i = 0; i < rankSize; ++i) {
183 2008 : ss << msg.sendCounts[i] << ",";
184 : }
185 5 : ss << " sendOffset:";
186 2013 : for (u32 i = 0; i < rankSize; ++i) {
187 2008 : ss << msg.sendOffset[i] << ",";
188 : }
189 5 : ss << " recvCounts:";
190 2013 : for (u32 i = 0; i < rankSize; ++i) {
191 2008 : ss << msg.recvCounts[i] << ",";
192 : }
193 5 : ss << " recvOffset:";
194 2013 : for (u32 i = 0; i < rankSize; ++i) {
195 2008 : ss << msg.recvOffset[i] << ",";
196 : }
197 10 : return ss.str();
198 5 : }
199 :
200 83 : void AicpuKfcUtils::PrintMC2AicpuContext(const AicpuComContext& ctx, bool errorFlag)
201 : {
202 83 : const s32 logLevel = (errorFlag ? DLOG_ERROR : DLOG_INFO);
203 83 : HCCL_LOG_BY_LEVEL(logLevel, "AicpuComContext.devId %u", ctx.devId);
204 83 : HCCL_LOG_BY_LEVEL(logLevel, "AicpuComContext.ssid %u", ctx.ssid);
205 83 : HCCL_LOG_BY_LEVEL(logLevel, "AicpuComContext.rankId %u", ctx.rankId);
206 83 : HCCL_LOG_BY_LEVEL(logLevel, "AicpuComContext.rankNum %u", ctx.rankNum);
207 83 : HCCL_LOG_BY_LEVEL(logLevel, "AicpuComContext.windowSize %lu", ctx.windowSize);
208 83 : HCCL_LOG_BY_LEVEL(logLevel, "AicpuComContext.workSpaceAddr %p", ctx.workSpaceAddr);
209 2739 : for (uint32_t i = 0; i < AC_MAX_RANK_NUM; i++) {
210 2656 : HCCL_LOG_BY_LEVEL(logLevel, "AicpuComContext.eventIds[%u] %lu", i, ctx.eventIds[i]);
211 :
212 2656 : const auto& si = ctx.streamInfo[i];
213 2656 : HCCL_LOG_BY_LEVEL(
214 : logLevel, "AicpuComContext.streamInfo[%u] streamId %d sqId %d depth %u addr %p", i, si.actualStreamId,
215 : si.sqId, si.sqDepth, si.sqBaseAddr);
216 :
217 2656 : const auto& noIpcPre = ctx.noIpcPreNotify[i];
218 2656 : HCCL_LOG_BY_LEVEL(
219 : logLevel, "AicpuComContext.noIpcPreNotify[%u] addr %p notifyId %d", i, noIpcPre.address,
220 : noIpcPre.actualNotifyId);
221 :
222 2656 : const auto& noIpcPost = ctx.noIpcPostNotify[i];
223 2656 : HCCL_LOG_BY_LEVEL(
224 : logLevel, "AicpuComContext.noIpcPostNotify[%u] addr %p notifyId %d", i, noIpcPost.address,
225 : noIpcPost.actualNotifyId);
226 :
227 2656 : const auto& ipcPreRec = ctx.ipcPreRecordNotify[i];
228 2656 : HCCL_LOG_BY_LEVEL(
229 : logLevel, "AicpuComContext.ipcPreRecordNotify[%u] addr %p notifyId %d", i, ipcPreRec.address,
230 : ipcPreRec.actualNotifyId);
231 :
232 2656 : const auto& ipcPreWait = ctx.ipcPreWaitNotify[i];
233 2656 : HCCL_LOG_BY_LEVEL(
234 : logLevel, "AicpuComContext.ipcPreWaitNotify[%u] addr %p notifyId %d", i, ipcPreWait.address,
235 : ipcPreWait.actualNotifyId);
236 :
237 2656 : const auto& ipcPostRec = ctx.ipcPostRecordNotify[i];
238 2656 : HCCL_LOG_BY_LEVEL(
239 : logLevel, "AicpuComContext.ipcPostRecordNotify[%u] addr %p notifyId %d", i, ipcPostRec.address,
240 : ipcPostRec.actualNotifyId);
241 :
242 2656 : const auto& ipcPostWait = ctx.ipcPostWaitNotify[i];
243 2656 : HCCL_LOG_BY_LEVEL(
244 : logLevel, "AicpuComContext.ipcPostWaitNotify[%u] addr %p notifyId %d", i, ipcPostWait.address,
245 : ipcPostWait.actualNotifyId);
246 : }
247 83 : HCCL_LOG_BY_LEVEL(logLevel, "AicpuComContext.determinism %u", ctx.determinism);
248 83 : }
249 :
250 1182 : void AicpuKfcUtils::PrintApiBuffer(const void* const buffer, uint64_t totalSize, const std::string& desc)
251 : {
252 1182 : if (buffer == nullptr) {
253 1162 : return;
254 : }
255 20 : HCCL_RUN_INFO("%s, buffer: %p totalSize: %lu", desc.c_str(), buffer, totalSize);
256 20 : constexpr uint32_t maxPrintNum = 192U;
257 20 : constexpr uint32_t partNum = 64U;
258 20 : constexpr uint32_t everyNum = 8U;
259 20 : uint32_t cnt = totalSize / sizeof(uint32_t);
260 20 : const uint32_t* const cmd = reinterpret_cast<const uint32_t*>(buffer);
261 20 : if (cnt <= maxPrintNum) {
262 20 : if (cnt < everyNum) {
263 0 : for (size_t i = 0UL; i < cnt; i++) {
264 0 : HCCL_RUN_INFO("%zu: %08x", i, cmd[i]);
265 : }
266 : } else {
267 : // cnt向下取整到最近的8的倍数
268 20 : cnt = cnt / everyNum * everyNum;
269 40 : for (size_t i = 0UL; i < cnt; i += everyNum) {
270 20 : HCCL_RUN_INFO(
271 : "%zu: %08x %08x %08x %08x %08x %08x %08x %08x", i, cmd[i], cmd[i + 1U], cmd[i + 2U], cmd[i + 3U],
272 : cmd[i + 4U], cmd[i + 5U], cmd[i + 6U], cmd[i + 7U]);
273 : }
274 : }
275 : } else {
276 : // 打印前64个uint32_t数据
277 0 : for (size_t i = 0UL; i < partNum; i += everyNum) {
278 0 : HCCL_RUN_INFO(
279 : "%zu: %08x %08x %08x %08x %08x %08x %08x %08x", i, cmd[i], cmd[i + 1U], cmd[i + 2U], cmd[i + 3U],
280 : cmd[i + 4U], cmd[i + 5U], cmd[i + 6U], cmd[i + 7U]);
281 : }
282 : // 打印中间64个uint32_t数据
283 0 : size_t start = (cnt / 2) - (partNum / 2);
284 0 : for (size_t i = start; i < start + partNum; i += everyNum) {
285 0 : HCCL_RUN_INFO(
286 : "%zu: %08x %08x %08x %08x %08x %08x %08x %08x", i, cmd[i], cmd[i + 1U], cmd[i + 2U], cmd[i + 3U],
287 : cmd[i + 4U], cmd[i + 5U], cmd[i + 6U], cmd[i + 7U]);
288 : }
289 : // 打印后64个uint32_t数据
290 0 : for (size_t i = cnt - partNum; i < cnt; i += everyNum) {
291 0 : HCCL_RUN_INFO(
292 : "%zu: %08x %08x %08x %08x %08x %08x %08x %08x", i, cmd[i], cmd[i + 1U], cmd[i + 2U], cmd[i + 3U],
293 : cmd[i + 4U], cmd[i + 5U], cmd[i + 6U], cmd[i + 7U]);
294 : }
295 : }
296 : }
297 :
298 591 : void AicpuKfcUtils::PrintApiBufferByMsgPos(const HcclMsg& msg, uint32_t msgPos)
299 : {
300 : u64 dataSize;
301 591 : if (msg.addMsg.v0Msg.version != HcclTilingVersion::DEPRECATED_TILING_VERSION) {
302 10 : dataSize = msg.dataCnt * DataUnitSize(static_cast<HcclDataType>(msg.addMsg.v1Msg.hcclDataType));
303 : } else {
304 581 : dataSize = msg.dataCnt * DataUnitSize(static_cast<HcclDataType>(msg.addMsg.v0Msg.hcclDataType));
305 : }
306 591 : AicpuKfcUtils::PrintApiBuffer(
307 591 : reinterpret_cast<const void*>(msg.sendBuffer), dataSize, "sendBuffer after comm " + std::to_string(msgPos));
308 591 : AicpuKfcUtils::PrintApiBuffer(
309 591 : reinterpret_cast<const void*>(msg.recvBuffer), dataSize, "recvBuffer after comm " + std::to_string(msgPos));
310 591 : }
311 :
312 4 : uint64_t AicpuKfcUtils::GenXor(HcclMsgExt* msg, u32 rankSize)
313 : {
314 4 : if (UNLIKELY(rankSize > HCCL_MAX_RANK_NUM_V2)) {
315 0 : return 0UL;
316 : }
317 4 : uint64_t xorVal = 0U;
318 10 : for (u32 i = 0U; i < rankSize; ++i) {
319 6 : xorVal ^= msg->sendCounts[i];
320 6 : xorVal ^= msg->sendOffset[i];
321 6 : xorVal ^= msg->recvCounts[i];
322 6 : xorVal ^= msg->recvOffset[i];
323 : }
324 4 : xorVal ^= msg->valid;
325 4 : return xorVal;
326 : }
327 :
328 72 : void AicpuKfcUtils::PrintBuffer(const void* const buffer, uint32_t totalSize, const std::string& desc)
329 : {
330 72 : if (buffer == nullptr) {
331 0 : HCCL_DEBUG("buffer is nullptr");
332 0 : return;
333 : }
334 : #ifndef RUN_TEST
335 72 : constexpr uint32_t maxPrintNum = 128U;
336 :
337 72 : uint32_t cnt = totalSize / sizeof(uint32_t);
338 72 : cnt = std::min(cnt, maxPrintNum);
339 72 : const uint32_t* const cmd = reinterpret_cast<const uint32_t*>(buffer);
340 :
341 1215 : for (size_t i = 0UL; i < cnt; i += 8U) {
342 1143 : HCCL_DEBUG(
343 : "%p %zu %s: %08x %08x %08x %08x %08x %08x %08x %08x", buffer, i, desc.c_str(), cmd[i], cmd[i + 1U],
344 : cmd[i + 2U], cmd[i + 3U], cmd[i + 4U], cmd[i + 5U], cmd[i + 6U], cmd[i + 7U]);
345 : }
346 :
347 72 : if (cnt > maxPrintNum) {
348 0 : for (size_t i = cnt - maxPrintNum; i < cnt - 8U; i += 8U) { // 8 is byte size
349 0 : HCCL_DEBUG(
350 : "%p %zu %s: %08x %08x %08x %08x %08x %08x %08x %08x", buffer, i, desc.c_str(), cmd[i], cmd[i + 1U],
351 : cmd[i + 2U], cmd[i + 3U], cmd[i + 4U], cmd[i + 5U], cmd[i + 6U], cmd[i + 7U]);
352 : }
353 : }
354 : #endif
355 : }
356 :
357 91 : uint32_t AicpuKfcUtils::GenXor(HcclMsg* msg)
358 : {
359 91 : if (msg == nullptr) {
360 0 : return UINT32_MAX;
361 : }
362 91 : DataBlock* block = reinterpret_cast<DataBlock*>(msg);
363 91 : uint32_t xorVal = 0;
364 1456 : for (uint32_t i = 0; i < sizeof(DataBlock) / sizeof(u32) - 1U; i++) {
365 1365 : xorVal ^= block->data[i];
366 : }
367 91 : return xorVal;
368 : }
369 :
370 24 : void AicpuKfcUtils::PrintBuffer(AicpuComContext* ctx, const AivAicpuOpParam& msgAddr)
371 : {
372 24 : if (ctx == nullptr || ctx->logLevel > HCCL_LOG_DEBUG) {
373 0 : return;
374 : }
375 : #ifdef __aarch64__
376 : __asm__ __volatile__("dsb ld" : : : "memory");
377 : #endif
378 : #ifdef __amd64__
379 24 : __asm__ __volatile__("" : : : "memory");
380 : #endif
381 :
382 48 : PrintBuffer(reinterpret_cast<void*>(msgAddr.sendBuffer), msgAddr.count * ctx->unitSize, "after copy, send buffer");
383 72 : PrintBuffer(
384 24 : reinterpret_cast<void*>(ctx->rankInfo[ctx->rankId].window), msgAddr.count * ctx->unitSize,
385 : "after copy, window");
386 48 : PrintBuffer(reinterpret_cast<void*>(msgAddr.recvBuffer), msgAddr.count * ctx->unitSize, "after copy, recv buffer");
387 : }
388 :
389 34 : int AicpuKfcUtils::GetSendCnt(AicpuComContext* ctx)
390 : {
391 34 : if (ctx == nullptr) {
392 0 : return 0;
393 : }
394 34 : return static_cast<int>((reinterpret_cast<AivAicpuOpParam*>(ctx->workSpaceAddr + ctx->notifyOff))->sendCnt);
395 : }
396 :
397 34 : int AicpuKfcUtils::GetRecvCnt(AicpuComContext* ctx)
398 : {
399 34 : if (ctx == nullptr) {
400 0 : return 0;
401 : }
402 : return static_cast<int>(
403 : (reinterpret_cast<AivAicpuOpParam*>(
404 34 : ctx->workSpaceAddr + ctx->notifyOff + ctx->notifyBeginCnt * sizeof(uint8_t) * AC_SQE_SIZE))
405 34 : ->rcvCnt);
406 : }
407 :
408 3061 : bool AicpuKfcUtils::IsDebugModeEquals(const AicpuComContext& ctx, const uint8_t Mode) { return ctx.debugMode == Mode; }
409 :
410 3019 : bool AicpuKfcUtils::NeedRecordTimeTaken(const AicpuComContext& ctx)
411 : {
412 3019 : return IsDebugModeEquals(ctx, MC2_DEBUG_TIME_TAKEN) || dfx::ProfilingManager::GetProfL1State();
413 : }
414 :
415 37 : void AicpuKfcUtils::PrintApiStats(HcclMsgArea* hcclMsgArea, const s32 logLevel)
416 : {
417 37 : const auto& apiStats = hcclMsgArea->apiStats;
418 37 : std::stringstream ssCommitStats;
419 814 : for (u32 i = 0; i < sizeof(apiStats.commitStats) / sizeof(apiStats.commitStats[0]); ++i) {
420 777 : ssCommitStats << apiStats.commitStats[i].cnt << ",";
421 : }
422 37 : HCCL_LOG_BY_LEVEL(logLevel, "apiCommitStats: %s", ssCommitStats.str().c_str());
423 :
424 74 : std::stringstream ssWaitStats;
425 814 : for (u32 i = 0; i < sizeof(apiStats.waitStats) / sizeof(apiStats.waitStats[0]); ++i) {
426 777 : ssWaitStats << apiStats.waitStats[i].cnt << ",";
427 : }
428 37 : HCCL_LOG_BY_LEVEL(logLevel, "apiWaitStats: %s", ssWaitStats.str().c_str());
429 :
430 74 : std::stringstream ssMsgStats;
431 185 : for (u32 i = 0; i < sizeof(apiStats.msgStats) / sizeof(apiStats.msgStats[0]); ++i) {
432 148 : ssMsgStats << apiStats.msgStats[i].cnt << ",";
433 : }
434 37 : HCCL_LOG_BY_LEVEL(logLevel, "apiMsgStats: %s", ssMsgStats.str().c_str());
435 :
436 74 : std::stringstream ssSnapshots;
437 37 : const u64 cnt = apiStats.snapshots[0].cnt;
438 37 : const u64 start = (cnt > HCCL_API_SNAPSHOTS_CNT ? cnt - HCCL_API_SNAPSHOTS_CNT : 0UL);
439 37 : for (u64 i = start; i < cnt; ++i) {
440 0 : ssSnapshots << apiStats.snapshots[i % HCCL_API_SNAPSHOTS_CNT + 1UL].cnt << ",";
441 : }
442 37 : HCCL_LOG_BY_LEVEL(logLevel, "apiSnapshots(%llu-%llu): %s", start + 1UL, cnt, ssSnapshots.str().c_str());
443 37 : }
444 :
445 37 : void AicpuKfcUtils::PrintAllHcclMsgArea(HcclMsgArea* hcclMsgArea, u32 rankSize, bool errorFlag)
446 : {
447 37 : const s32 logLevel = (errorFlag ? DLOG_ERROR : HCCL_DLOG_DEFAULT);
448 37 : HCCL_LOG_BY_LEVEL(logLevel, "********* msgArea %p start print **********", hcclMsgArea);
449 37 : if (hcclMsgArea == nullptr) {
450 0 : return;
451 : }
452 37 : const SingleQueueMsg& msg = hcclMsgArea->commMsg.singleMsg;
453 2405 : for (uint32_t i = 0; i < HCCL_MSG_CNT; ++i) {
454 2368 : HcclCMDType type = static_cast<HcclCMDType>(msg.sendMsgs[i].commType.msgType);
455 2368 : if (type == HcclCMDType::HCCL_CMD_INVALID || type >= HcclCMDType::HCCL_CMD_MAX) {
456 2302 : continue;
457 : }
458 66 : HCCL_LOG_BY_LEVEL(logLevel, "SendMsg[%d]: %s", i, GetMsgSimpleStr(msg.sendMsgs[i]).c_str());
459 : }
460 : // recvMsgList暂不支持,不处理了
461 2405 : for (uint32_t i = 0; i < HCCL_MSG_CNT; ++i) {
462 2368 : if (static_cast<HcclCMDType>(msg.sendMsgs[i].commType.msgType) != HcclCMDType::HCCL_CMD_ALLTOALLV) {
463 2367 : continue;
464 : }
465 1 : HCCL_LOG_BY_LEVEL(logLevel, "MsgExt[%d]: %s", i, GetMsgSimpleStr(rankSize, msg.paramExtMsgList[i]).c_str());
466 : }
467 :
468 74 : std::stringstream ssCommitCnt;
469 2405 : for (uint32_t i = 0; i < HCCL_MSG_CNT; ++i) {
470 2368 : ssCommitCnt << msg.commitTurnCnt[i].cnt << ",";
471 : }
472 37 : HCCL_LOG_BY_LEVEL(logLevel, "commitTurnCnt: %s", ssCommitCnt.str().c_str());
473 :
474 74 : std::stringstream ssFinishCnt;
475 2405 : for (uint32_t i = 0; i < HCCL_MSG_CNT; ++i) {
476 2368 : ssFinishCnt << msg.finishedTurnCnt[i].cnt << ",";
477 : }
478 37 : HCCL_LOG_BY_LEVEL(logLevel, "finishedTurnCnt: %s", ssFinishCnt.str().c_str());
479 :
480 37 : PrintApiStats(hcclMsgArea, logLevel);
481 :
482 37 : HCCL_LOG_BY_LEVEL(logLevel, "********* msgArea %p end print **********", hcclMsgArea);
483 : }
484 :
485 5 : void AicpuKfcUtils::PrintAllHcclMsgAreaForMulti(HcclMsgArea* hcclMsgArea, bool errorFlag)
486 : {
487 5 : const s32 logLevel = (errorFlag ? DLOG_ERROR : HCCL_DLOG_DEFAULT);
488 5 : HCCL_LOG_BY_LEVEL(logLevel, "********* msgArea %p start print **********", hcclMsgArea);
489 5 : if (hcclMsgArea == nullptr) {
490 2 : return;
491 : }
492 3 : const MultiQueueMsg& msg = hcclMsgArea->commMsg.multiMsg;
493 147 : for (u32 i = 0U; i < MAX_QUE_NUM; ++i) {
494 9360 : for (u32 j = 0; j < HCCL_MSG_CNT; ++j) {
495 9216 : HcclCMDType type = static_cast<HcclCMDType>(msg.sendMsgs[i][j].commType.msgType);
496 9216 : if (type == HcclCMDType::HCCL_CMD_INVALID || type >= HcclCMDType::HCCL_CMD_MAX) {
497 9208 : continue;
498 : }
499 8 : HCCL_LOG_BY_LEVEL(logLevel, "SendMsg[%u/%u]: %s", i, j, GetMsgSimpleStr(msg.sendMsgs[i][j]).c_str());
500 : }
501 : }
502 3 : HCCL_LOG_BY_LEVEL(logLevel, "********* msgArea %p end print **********", hcclMsgArea);
503 : }
504 :
505 17 : HcclResult AicpuKfcUtils::ThreadBarrier(u64 timeout)
506 : {
507 17 : const u32 threadNum = HcclAicpuUtils::GetBlockNum();
508 17 : if (threadNum <= 1U) {
509 17 : return HCCL_SUCCESS;
510 : }
511 : static std::atomic<u32> threadCount{0U};
512 : static std::atomic<u32> round{0U};
513 :
514 0 : const u32 curRound = round.load(std::memory_order_acquire);
515 0 : if (threadCount.fetch_add(1U, std::memory_order_acq_rel) + 1U == threadNum) {
516 : threadCount.store(0U, std::memory_order_relaxed);
517 : round.fetch_add(1U, std::memory_order_release);
518 : } else {
519 0 : const u64 ts = GetCurCpuTimestamp();
520 0 : while (round.load(std::memory_order_acquire) == curRound) {
521 : #ifdef __aarch64__
522 : __asm__ __volatile__("nop");
523 : #endif
524 0 : CHK_PRT_RET(
525 : timeout > 0UL && GetCurCpuTimestamp() - ts > timeout,
526 : HCCL_ERROR(
527 : "[%s]Timeout during thread barrier, thread number %u/%u, round %u.", __func__,
528 : threadCount.load(std::memory_order_acquire), threadNum, curRound),
529 : HCCL_E_AGAIN);
530 : }
531 : }
532 0 : return HCCL_SUCCESS;
533 : }
534 :
535 30098919 : HcclResult AicpuKfcUtils::TraceProfSubmit()
536 : {
537 30098919 : if (dfx::ProfilingManager::GetProfL1State()) {
538 2 : CHK_PRT_RET(
539 : dfx::AicpuProfilingManager::ReportTaskInfo() != HCCL_SUCCESS, HCCL_ERROR("prof task info failed"),
540 : HCCL_E_INTERNAL);
541 : }
542 30098919 : return HCCL_SUCCESS;
543 : }
544 :
545 35 : void AicpuKfcUtils::PrintHcclCommParamDesc(const CommKfcParamDesc& desc)
546 : {
547 35 : HCCL_INFO("CommKfcParamDesc.version %lu", desc.version);
548 35 : HCCL_INFO("CommKfcParamDesc.itemNum %lu", desc.itemNum);
549 35 : HCCL_INFO("CommKfcParamDesc.hasFfts %lu", desc.hasFfts);
550 35 : HCCL_INFO("CommKfcParamDesc.tilingOff %lu", desc.tilingOff);
551 35 : HCCL_INFO("CommKfcParamDesc.isDyn %lu", desc.isDyn);
552 35 : }
553 :
554 34980972 : HcclResult AicpuKfcUtils::ReadMsgFromMemory(HcclMsg* src, HcclMsg& dst)
555 : {
556 : #ifdef __aarch64__
557 : __asm__ __volatile__("dsb ld" : : : "memory");
558 : #endif
559 : #ifdef __amd64__
560 34980972 : __asm__ __volatile__("" : : : "memory");
561 : #endif
562 34980972 : if (src->addMsg.v0Msg.valid != HCCL_MSG_VALID_MASK) {
563 34980947 : return HCCL_E_AGAIN;
564 : }
565 25 : (void)memcpy_s(&dst, sizeof(dst), src, sizeof(HcclMsg));
566 25 : const u32 xorVal = GenXor(&dst);
567 25 : if (UNLIKELY(xorVal != src->addMsg.v0Msg.xorCheck)) {
568 : static u32 cnt = 0;
569 0 : if (cnt++ % MC2_API_XORCHECK_PRINT_NUM == 0) {
570 0 : PrintMsg("Rcv src msg", *src, true);
571 0 : PrintMsg("Rcv dst msg", dst, true);
572 0 : HCCL_RUN_INFO("Data is modified, modified_xor:%u, origin_xor:%u.", xorVal, src->addMsg.v0Msg.xorCheck);
573 : }
574 0 : return HCCL_E_AGAIN;
575 : }
576 :
577 25 : src->addMsg.v0Msg.valid = ~HCCL_MSG_VALID_MASK;
578 : #ifdef __aarch64__
579 : __asm__ __volatile__("dsb st" : : : "memory");
580 : #endif
581 25 : PrintMsg("Read message", dst);
582 25 : return HCCL_SUCCESS;
583 : }
584 :
585 3 : HcclResult AicpuKfcUtils::ReadMsgFromMemory(HcclMsgExt* src, u32 rankSize, HcclMsgExt& dst)
586 : {
587 : #ifdef __aarch64__
588 : __asm__ __volatile__("dsb ld" : : : "memory");
589 : #endif
590 : #ifdef __amd64__
591 3 : __asm__ __volatile__("" : : : "memory");
592 : #endif
593 3 : if (src->valid != HCCL_MSG_VALID_MASK) {
594 0 : return HCCL_E_AGAIN;
595 : }
596 :
597 3 : CHK_PRT_RET(rankSize > HCCL_MAX_RANK_NUM_V2, HCCL_ERROR("Invalid rank size %u.", rankSize), HCCL_E_PARA);
598 2 : (void)memcpy_s(&dst, sizeof(HcclMsgExt), src, sizeof(HcclMsgExt));
599 2 : const u64 xorVal = GenXor(&dst, rankSize);
600 2 : if (UNLIKELY(xorVal != src->xorCheck)) {
601 : static u32 cnt = 0;
602 0 : if (cnt++ % MC2_API_XORCHECK_PRINT_NUM == 0) {
603 0 : HCCL_RUN_INFO("Rcv src ext msg %s", GetMsgSimpleStr(rankSize, *src).c_str());
604 0 : HCCL_RUN_INFO("Rcv dst ext msg %s", GetMsgSimpleStr(rankSize, dst).c_str());
605 0 : HCCL_RUN_INFO("Extended data is modified, modified_xor:%llu, origin_xor:%llu.", xorVal, src->xorCheck);
606 : }
607 0 : return HCCL_E_AGAIN;
608 : }
609 2 : src->valid = ~HCCL_MSG_VALID_MASK;
610 : #ifdef __aarch64__
611 : __asm__ __volatile__("dsb st" : : : "memory");
612 : #endif
613 2 : HCCL_INFO("Read extended message %s", GetMsgSimpleStr(rankSize, dst).c_str());
614 2 : return HCCL_SUCCESS;
615 : }
|