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