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 : #ifndef TASK_PROFILING_PUB_H
12 : #define TASK_PROFILING_PUB_H
13 :
14 : #include <chrono>
15 : #include <memory>
16 : #include <mutex>
17 : #include <string>
18 : #include <map>
19 : #include <queue>
20 : #include <hccl/hccl_types.h>
21 :
22 : #include "hccl/base.h"
23 : #include "profiler_base_pub.h"
24 : #include "adapter_prof.h"
25 : #include "prof_common.h"
26 :
27 : namespace hccl {
28 : constexpr u64 SHIFT_BITS_PLANE_ID = 28;
29 : constexpr u64 SHIFT_BITS_RANK_SIZE = 16;
30 : constexpr u64 SHIFT_BITS_RANK = 0;
31 :
32 : constexpr u64 MASK_PLANE_ID = 0xF;
33 : constexpr u64 MASK_RANK_SIZE = 0xFFF;
34 : constexpr u64 MASK_RANK = 0xFFFF;
35 : constexpr u64 INVALID_U64_PROF = 0xFFFFFFFFFFFFFFFF;
36 : constexpr u64 OP_CALLED_COUNT_LIMIT = 1000000;
37 :
38 : #define PARSE_PLANE_ID(id) (((static_cast<u64>(id)) >> SHIFT_BITS_PLANE_ID) & MASK_PLANE_ID)
39 : #define PARSE_RANK(id) (((static_cast<u64>(id)) >> SHIFT_BITS_RANK) & MASK_RANK)
40 : #define PARSE_RANK_SIZE(id) (((static_cast<u64>(id)) >> SHIFT_BITS_RANK_SIZE) & MASK_RANK_SIZE)
41 :
42 : constexpr u64 ENGINE_MAX_TAG_LEN = 31;
43 : /* *
44 : * @name ProfReporterData
45 : * @brief struct of data to report
46 : */
47 : struct ProfReporterData {
48 : char tag[ENGINE_MAX_TAG_LEN + 1]; // the sub-type of the module, data with different tag will be written
49 : u32 deviceId; // the index of device
50 : size_t dataLen; // the length of send data
51 : u8 *data; // the data content
52 : };
53 :
54 : struct TaskData {
55 : u32 streamID;
56 : u32 taskID;
57 :
58 : TaskType taskType;
59 :
60 : TaskParaDMA DMA; // taskType = SDMA/RDMA使用, 包括rtRDMASend写notify
61 : TaskParaReduce Reduce; // taskType = inline/CCE Reduce使用
62 : TaskParaNotify Notify; // taskType = Noitfy Record/Wait使用
63 : TaskParaAiv Aiv; // taskType = Aiv 使用
64 :
65 : TaskData() : streamID(-1), taskID(-1), taskType(TaskType::TASK_SDMA)
66 : {
67 : }
68 32 : TaskData(u32 &streamID, u32 &taskID, TaskType &taskType, const TaskParaDMA ¶)
69 32 : : streamID(streamID),
70 32 : taskID(taskID),
71 32 : taskType(taskType),
72 32 : DMA(para)
73 : {
74 32 : }
75 9 : TaskData(u32 &streamID, u32 &taskID, TaskType &taskType, const TaskParaReduce ¶)
76 9 : : streamID(streamID),
77 9 : taskID(taskID),
78 9 : taskType(taskType),
79 9 : Reduce(para)
80 : {
81 9 : }
82 0 : TaskData(u32 &streamID, u32 &taskID, TaskType &taskType, const TaskParaNotify ¶)
83 0 : : streamID(streamID),
84 0 : taskID(taskID),
85 0 : taskType(taskType),
86 0 : Notify(para)
87 : {
88 0 : }
89 : TaskData(u32 &streamID, u32 &taskID, TaskType &taskType, const TaskParaAiv ¶)
90 : : streamID(streamID),
91 : taskID(taskID),
92 : taskType(taskType),
93 : Aiv(para)
94 : {
95 : }
96 : };
97 :
98 : struct HCCLReportData {
99 : std::string fileTag;
100 : uint64_t ts;
101 : uint32_t type;
102 : MsprofHcclInfo profInfo;
103 : std::string tag;
104 : std::string groupName;
105 : };
106 :
107 :
108 : enum class ProfTaskType {
109 : TASK_HCCL_INFO = 0,
110 : TASK_SDMA,
111 : TASK_RDMA,
112 : TASK_REDUCE_INLINE,
113 : TASK_REDUCE_TBE,
114 : TASK_NOTIFY_RECORD,
115 : TASK_NOTIFY_WAIT,
116 : TASK_STAGEX_STEPX,
117 : TASK_FLAG,
118 : TASK_END,
119 : TASK_MULTI_THREAD,
120 : TASK_LAUNCH_FFTS_TASK,
121 : TASK_AIV,
122 : TASK_DPU_HCCL_INFO, // 用于区分DPU侧上报的HCCL信息和Host侧上报的HCCL信息
123 :
124 : TASK_ISET_LOOKUP_RESPONSE,
125 : TASK_WAIT_SOME,
126 : TASK_GET_LOOKUP_REQUEST,
127 : TASK_COLL_RECV_LOOKUP_REQUEST,
128 : TASK_COLL_RECV_UPDATE_REQUEST,
129 : TASK_ISEND_UPDATE_RESPONSE,
130 : TASK_ISEND_LOOKUP_RESPONSE,
131 :
132 : // update
133 : TASK_UPDATE_IMRECV,
134 : TASK_UPDATE_GLOBAL_REDUCE,
135 :
136 : // new
137 : TASK_LOOKUP_RESPONSE_MEMCPY,
138 : TASK_LOOKUP_RESPONSE_ISEND,
139 : TASK_SHARE_MEMORY_ISEND_RECORD,
140 :
141 : TASK_ABORT_SELF,
142 : TASK_SERVICE_CANCEL,
143 : TASK_DESTROY_RESOURCE,
144 : TASK_EVENT_WAIT,
145 :
146 : // npu lookup
147 : TASK_KEY_DROP_DUPLICATES,
148 : TASK_SEND_KEYS,
149 : TASK_SEND_KEYS_RECORD,
150 : TASK_EVENT_WAIT_RECV_DONE,
151 : TASK_RESET_UNIQUE_HANDLE,
152 : TASK_EVENT_WAIT_SEND_DONE,
153 : TASK_RECV_VALUES,
154 : TASK_RECOVER_VALUE_AICORE,
155 : TASK_GATHER_FINISH,
156 :
157 : // npu update
158 : TASK_REMOTE_UPDATE_KEY_REDUCE,
159 : TASK_VALUE_CLEAR_AICORE,
160 : TASK_VALUE_REDUCE_SUM_AICORE,
161 : TASK_REMOTE_UPDATE_SEND_REQUEST,
162 : TASK_UPDATE_RESET_UNIQUE_HANDLE,
163 : TASK_NOTIFY_REMOTE_IMRECV_DONE_SIGNAL_KEY,
164 : TASK_NOTIFY_REMOTE_IMRECV_DONE_SIGNAL_VALUE,
165 : TASK_REMOTE_UPDATE_RECV_RESPONSE,
166 :
167 : TASK_BUILD_CS_TRANSPORT,
168 : TASK_UPDATE_ALG_GLOBAL_REDUCE,
169 : TASK_INTER_PROCESSOR_SYNC,
170 : TASK_INTER_RANK_RECORD,
171 : TASK_INVALID
172 : };
173 :
174 : const std::map<ProfTaskType, std::string> PROF_TASK_OP_NAME = {
175 : {ProfTaskType::TASK_HCCL_INFO, "hccl_info"},
176 : {ProfTaskType::TASK_DPU_HCCL_INFO, "dpu_hccl_info"},
177 : {ProfTaskType::TASK_SDMA, "Memcpy"},
178 : {ProfTaskType::TASK_RDMA, "RDMASend"},
179 : {ProfTaskType::TASK_REDUCE_INLINE, "Reduce_Inline"},
180 : {ProfTaskType::TASK_REDUCE_TBE, "Reduce_TBE"},
181 : {ProfTaskType::TASK_NOTIFY_RECORD, "Notify_Record"},
182 : {ProfTaskType::TASK_NOTIFY_WAIT, "Notify_Wait"},
183 : {ProfTaskType::TASK_STAGEX_STEPX, "StageX_StepX"},
184 : {ProfTaskType::TASK_FLAG, "Flag"},
185 : {ProfTaskType::TASK_END, "End"},
186 : {ProfTaskType::TASK_MULTI_THREAD, "Multi_Thread"},
187 : {ProfTaskType::TASK_LAUNCH_FFTS_TASK, "Launch_Ffts"},
188 : {ProfTaskType::TASK_AIV, "AivKernel"},
189 :
190 : {ProfTaskType::TASK_WAIT_SOME, "Wait_Some"},
191 : {ProfTaskType::TASK_COLL_RECV_LOOKUP_REQUEST, "Coll_Recv_Lookup_Request"},
192 : {ProfTaskType::TASK_COLL_RECV_UPDATE_REQUEST, "Coll_Recv_Update_Request"},
193 : {ProfTaskType::TASK_ISEND_UPDATE_RESPONSE, "Isend_Update_Response"},
194 : {ProfTaskType::TASK_ISEND_LOOKUP_RESPONSE, "Isend_Lookup_Response"},
195 :
196 : // RemoteUpdate 使用
197 : {ProfTaskType::TASK_UPDATE_IMRECV, "Update_Imrecv"},
198 : {ProfTaskType::TASK_UPDATE_GLOBAL_REDUCE, "Update_Global_Reduce"},
199 :
200 : // RemoteLookup 使用
201 : {ProfTaskType::TASK_LOOKUP_RESPONSE_MEMCPY, "Lookup_Response_Memcpy"},
202 : {ProfTaskType::TASK_LOOKUP_RESPONSE_ISEND, "Lookup_Response_Isend"},
203 :
204 : // SHM 使用
205 : {ProfTaskType::TASK_SHARE_MEMORY_ISEND_RECORD, "Share_Memory_Isend_Record"},
206 :
207 : {ProfTaskType::TASK_ABORT_SELF, "Abort_Self"},
208 : {ProfTaskType::TASK_SERVICE_CANCEL, "Service_Cancel"},
209 : {ProfTaskType::TASK_DESTROY_RESOURCE, "Destroy_Resource"},
210 : {ProfTaskType::TASK_EVENT_WAIT, "Event_Wait"},
211 :
212 : {ProfTaskType::TASK_KEY_DROP_DUPLICATES, "Key_Drop_Duplicates"},
213 : {ProfTaskType::TASK_SEND_KEYS, "Send_Keys"},
214 : {ProfTaskType::TASK_SEND_KEYS_RECORD, "Send_Keys_Record"},
215 : {ProfTaskType::TASK_EVENT_WAIT_RECV_DONE, "Event_Wait_Recv_Done"},
216 : {ProfTaskType::TASK_RESET_UNIQUE_HANDLE, "Reset_Unique_Handle"},
217 : {ProfTaskType::TASK_EVENT_WAIT_SEND_DONE, "Event_Wait_Send_Done"},
218 : {ProfTaskType::TASK_RECV_VALUES, "Recv_Values"},
219 : {ProfTaskType::TASK_RECOVER_VALUE_AICORE, "Recover_value_Aicore"},
220 : {ProfTaskType::TASK_GATHER_FINISH, "Gather_Finish"},
221 :
222 : {ProfTaskType::TASK_REMOTE_UPDATE_KEY_REDUCE, "Remote_Update_Key_Reduce"},
223 : {ProfTaskType::TASK_VALUE_CLEAR_AICORE, "Value_Clear_Aicore"},
224 : {ProfTaskType::TASK_VALUE_REDUCE_SUM_AICORE, "Value_Reduce_Sum_Aicore"},
225 : {ProfTaskType::TASK_REMOTE_UPDATE_SEND_REQUEST, "Remote_Update_Send_Request"},
226 : {ProfTaskType::TASK_UPDATE_RESET_UNIQUE_HANDLE, "Update_Reset_Unique_Handle"},
227 : {ProfTaskType::TASK_NOTIFY_REMOTE_IMRECV_DONE_SIGNAL_KEY, "Notify_Remote_Imrecv_Done_Signal_Key"},
228 : {ProfTaskType::TASK_NOTIFY_REMOTE_IMRECV_DONE_SIGNAL_VALUE, "Notify_Remote_Imrecv_Done_Signal_Value"},
229 : {ProfTaskType::TASK_REMOTE_UPDATE_RECV_RESPONSE, "Remote_Update_Recv_Response"},
230 :
231 : {ProfTaskType::TASK_BUILD_CS_TRANSPORT, "Build_Cs_Transport"},
232 : {ProfTaskType::TASK_UPDATE_ALG_GLOBAL_REDUCE, "Update_Glg_Global_Reduce"},
233 : {ProfTaskType::TASK_INTER_PROCESSOR_SYNC, "Inter_Processor_Sync"},
234 : {ProfTaskType::TASK_INTER_RANK_RECORD , "Inter_Rank_Record"},
235 : {ProfTaskType::TASK_INVALID, "unknown"}
236 : };
237 :
238 1584 : inline std::string GetProfTaskOpName(ProfTaskType type)
239 : {
240 1584 : CHK_PRT_RET(PROF_TASK_OP_NAME.empty(), HCCL_ERROR("PROF_OP_NAME has not inited."), "invalid");
241 1584 : auto it = PROF_TASK_OP_NAME.find(type);
242 1584 : if (it != PROF_TASK_OP_NAME.end()) {
243 1580 : return it->second;
244 : }
245 8 : return "unknown";
246 : }
247 :
248 : const std::map<HcclCMDType, std::string> PROF_OP_NAME = {{HcclCMDType::HCCL_CMD_INVALID, "hcom_invalid_"},
249 : {HcclCMDType::HCCL_CMD_BROADCAST, "hcom_broadcast_"}, {HcclCMDType::HCCL_CMD_ALLREDUCE, "hcom_allReduce_"},
250 : {HcclCMDType::HCCL_CMD_REDUCE, "hcom_reduce_"}, {HcclCMDType::HCCL_CMD_SEND, "hcom_send_"},
251 : {HcclCMDType::HCCL_CMD_RECEIVE, "hcom_receive_"}, {HcclCMDType::HCCL_CMD_ALLGATHER, "hcom_allGather_"},
252 : {HcclCMDType::HCCL_CMD_REDUCE_SCATTER, "hcom_reduceScatter_"}, {HcclCMDType::HCCL_CMD_SCATTER, "hcom_scatter_"},
253 : {HcclCMDType::HCCL_CMD_ALLTOALL, "hcom_alltoall_"}, {HcclCMDType::HCCL_CMD_ALLTOALLV, "hcom_alltoallv_"},
254 : {HcclCMDType::HCCL_CMD_ALLGATHER_V, "hcom_allGatherv_"}, {HcclCMDType::HCCL_CMD_REDUCE_SCATTER_V, "hcom_reduceScatterv_"},
255 : {HcclCMDType::HCCL_CMD_ALLTOALLVC, "hcom_alltoallvc_"},
256 : {HcclCMDType::HCCL_CMD_BATCH_SEND_RECV, "hcom_batchSendRecv_"},
257 : {HcclCMDType::HCCL_CMD_BATCH_PUT, "hccl_batchPut_"}, {HcclCMDType::HCCL_CMD_BATCH_GET, "hccl_batchGet_"}};
258 :
259 13 : inline std::string GetProfOpName(HcclCMDType cmdType)
260 : {
261 13 : CHK_PRT_RET(PROF_OP_NAME.empty(), HCCL_ERROR("PROF_OP_NAME has not inited."), "hcom_ivalid_");
262 13 : auto it = PROF_OP_NAME.find(cmdType);
263 13 : if (it != PROF_OP_NAME.end()) {
264 13 : return it->second;
265 : }
266 0 : return PROF_OP_NAME.begin()->second;
267 : }
268 :
269 : class TaskProfiling : public ProfilerBase {
270 : public:
271 : /* * 当前Profling只有注册接口, 生命期需要贯穿整个进程, 故选择静态成员变量
272 : 多线程操作相同reporter_对象需要加锁 */
273 : static std::mutex mutex_;
274 :
275 : public:
276 : explicit TaskProfiling(u32 deviceLogicId_, u32 localRank_, u32 rankSize_, bool profilingOn = true);
277 : ~TaskProfiling() override;
278 :
279 : public:
280 : HcclResult Run(const std::string &opName, const std::string &tag) const;
281 : HcclResult Run(const StepData &stepData) override;
282 : HcclResult Flush() override;
283 : HcclResult Save(u32 &streamID, u32 &taskID, TaskType &taskType, const TaskParaDMA ¶DMA) override;
284 : HcclResult Save(u32 &streamID, u32 &taskID, TaskType &taskType, const TaskParaReduce ¶Reduce) override;
285 : HcclResult Save(u32 &streamID, u32 &taskID, TaskType &taskType, const TaskParaNotify ¶Notify) override;
286 : HcclResult Save(u32 streamID, u32 taskID, const TaskParaAiv ¶Aiv) override;
287 : HcclResult Save(u32 &streamID, u32 &taskID, const void *descBuf = nullptr, size_t descBufLen = 0) override;
288 : HcclResult SaveToLog(const TaskParaHost ¶Host) override;
289 : HcclResult Save(u32 captureStreamID, u32 streamID, u32 taskID, TaskType &taskType, const TaskParaDMA ¶) override;
290 : HcclResult Save(u32 captureStreamID, u32 streamID, u32 taskID, TaskType &taskType, const TaskParaReduce ¶) override;
291 : HcclResult Save(u32 captureStreamID, u32 streamID, u32 taskID, TaskType &taskType, const TaskParaNotify ¶) override;
292 : HcclResult Save(u32 captureStreamID, u32 streamID, u32 taskID, const void *descBuf = nullptr, size_t descBufLen = 0) override;
293 : HcclResult Save(u32 captureStreamID, u32 streamID, u32 taskID, const TaskParaAiv ¶Aiv) override;
294 :
295 : static void DumpReportDataInfo(uint32_t type, const MsprofHcclInfo &profInfo);
296 :
297 : static HcclResult ReportMsprofData(HCCLReportData &hcclReportData);
298 : protected:
299 : private:
300 : HcclResult Run(const TaskData &taskData, bool isCapture = false);
301 : u64 TimestampNanosecond() const;
302 :
303 : HcclResult Report(struct ProfReporterData &data);
304 :
305 : ProfTaskType GetProfTaskType(TaskType taskType) const;
306 : double GetTaskTime(TaskType taskType, const TaskData &taskData) const;
307 : void GetTaskData(TaskType taskType, const TaskData &taskData, struct MsprofHcclInfo &taskInfo);
308 : void GetSdmaTaskData(TaskType taskType, const TaskData &taskData, struct MsprofHcclInfo &taskInfo) const;
309 : void GetRdmaTaskData(TaskType taskType, const TaskData &taskData, struct MsprofHcclInfo &taskInfo) const;
310 : void GetReduceTaskData(TaskType taskType, const TaskData &taskData, struct MsprofHcclInfo &taskInfo) const;
311 : void GetNotifyTaskData(TaskType taskType, const TaskData &taskData, struct MsprofHcclInfo &taskInfo) const;
312 : uint32_t GetTransportType(TaskType taskType) const;
313 : uint32_t GetTaskRole(TaskType taskType) const;
314 :
315 : private:
316 : const u32 localRank_;
317 : const u32 rankSize_;
318 : bool profilingOn_; // 当前无条件启动profiling
319 : };
320 : } // namespace hccl
321 :
322 : #endif /* TASK_PROFILING_PUB_H */
|