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 COMMON_PROFILING_PROFILING_MANAGER_H
12 : #define COMMON_PROFILING_PROFILING_MANAGER_H
13 :
14 : #include "rt_external.h"
15 : #include "adapter_prof.h"
16 : #include "task_profiling_pub.h"
17 : #include "profiler_base_pub.h"
18 : #include "externalinput_pub.h"
19 :
20 : #include "profiling_manager_pub.h"
21 : #include "aprof_pub.h"
22 : #include "dispatcher.h"
23 :
24 : #include <string>
25 : #include <cstdio>
26 : #include <atomic>
27 :
28 : namespace hccl {
29 : using Prof_Status = uint32_t;
30 : const Prof_Status SUCCESS = 0x0;
31 : const Prof_Status FAILED = 0xFFFFFFFF;
32 :
33 : struct EsLoopUpPara {
34 : s32 tag;
35 : void *srcAddr;
36 : void *dstAddr;
37 : u32 dataSize;
38 : };
39 :
40 : struct EsUpdatePara {
41 : s32 tag;
42 : char groupName[GROUP_NAME_MAX_LEN] = {0};
43 : void *srcAddr;
44 : void *dstAddr;
45 : u32 dataSize;
46 : };
47 :
48 : class ProfilingManager {
49 : public:
50 : static constexpr u32 aging = 1;
51 :
52 : ProfilingManager();
53 : virtual ~ProfilingManager();
54 : static ProfilingManager &Instance();
55 : HcclResult CallMsprofRegFftsLaunch() const;
56 : HcclResult CallMsprofRegHcclOpApi() const;
57 : HcclResult CallMsprofRegHostApi() const;
58 : HcclResult RegEsTaskType(ProfTaskType taskType) const;
59 : HcclResult CallMsprofRegEsTaskTypeApi() const;
60 : HcclResult CallMsprofRegTaskTypeApi() const;
61 : HcclResult CallMsprofReportHostApi(HcclCMDType cmdType, uint64_t beginTime, u64 count, HcclDataType dataType,
62 : AlgType algType, uint64_t groupName, u32 numBlocks=0) const;
63 : HcclResult CallMsprofReportHostHcclOpApi(uint64_t beginTime, uint64_t endTime, uint64_t itemId,
64 : uint32_t threadId) const;
65 : HcclResult CallMsprofReportTaskApi(bool isMainStrem, uint64_t beginTime, ProfTaskType taskType) const;
66 : HcclResult ReportTaskApi(bool isMainStrem, uint64_t beginTime, ProfTaskType taskType, uint32_t agingFlag) const;
67 : HcclResult CallEsMsprofReportTaskApi(bool isMainStrem, uint64_t beginTime, ProfTaskType taskType) const;
68 : HcclResult CallMsprofReportAdditionInfo(uint32_t type, uint64_t timeStamp, const void *data, int len) const;
69 : HcclResult CallMsprofReportMultiThreadInfo(const std::vector<uint32_t> &tidInfo) const;
70 : HcclResult CallMsprofReportContextIdInfo(u32 ctxIdMax) const;
71 : HcclResult CallMsprofReportHostAclApi(uint32_t type, uint64_t beginTime, uint64_t endTime,
72 : uint64_t itemId, uint32_t threadId) const;
73 : HcclResult CallMsprofReportHostNodeApi(uint64_t beginTime, uint64_t endTime,
74 : uint64_t itemId, uint32_t threadId) const;
75 : HcclResult CallMsprofReportHostNodeBasicInfo(uint64_t timeStamp, uint64_t itemId, uint32_t threadId, u32 numBlocks = 0) const;
76 : HcclResult CallMsprofReportHostHcclOpInfo(uint64_t timeStamp, uint32_t threadId, u64 count, HcclDataType dataType,
77 : std::string &algTypeStr, uint64_t groupName) const;
78 : HcclResult CallMsprofReportAdditionInfoForEsLookup(EsLoopUpPara ¶, ProfTaskType type);
79 : HcclResult CallMsprofReportAdditionInfoForEsUpdate(const EsUpdatePara ¶, ProfTaskType type);
80 : HcclResult ReportAdditionInfo(
81 : uint32_t type, uint64_t timeStamp, const void *data, int len, uint32_t agingFlag) const;
82 : HcclResult CallMsprofReportMc2CommInfo(uint64_t timeStamp, const void *data, int len);
83 : HcclResult CallMsprofReportEsAdditionInfo(uint32_t type, uint64_t timeStamp, const void *data, int len) const;
84 :
85 : Prof_Status CallMsprofReport(ReporterData &reporterData) const;
86 : Prof_Status GetHashKey(MsprofHashData &data) const;
87 : Prof_Status PluginInit() const;
88 : Prof_Status PluginUnInit() const;
89 : HcclResult ReportStoragedTaskApi();
90 : HcclResult ReportStoragedAdditionInfo();
91 : HcclResult ReportStoragedCompactInfo();
92 : HcclResult ClearStoragedProfilingInfo();
93 : HcclResult ReportStoragedFftsInfo();
94 : HcclResult ReportStoragedOpApi();
95 : HcclResult CallMsprofReportNodeInfo(uint64_t beginTime, uint64_t endTime,
96 : const std::string profName, uint32_t threadId);
97 1 : void SetMsprofReporterCallback(MsprofReporterCallback func)
98 : {
99 1 : reporterCallback_ = func;
100 1 : HCCL_INFO("[Check][Param]SetMsprofReporterCallback.");
101 1 : }
102 :
103 2 : void StartFftsLaunchSubscribe()
104 : {
105 2 : isFftsLaunchSubscribe_ = true;
106 2 : CallMsprofRegFftsLaunch();
107 2 : HCCL_RUN_INFO("StartFftsLaunchSubscribe:[%d]", isFftsLaunchSubscribe_);
108 2 : }
109 :
110 2 : void StartHostHcclOpSubscribe()
111 : {
112 2 : isHostHcclOpSubscribe_ = true;
113 2 : HCCL_RUN_INFO("StartHostHcclOpSubscribe:[%d]", isHostHcclOpSubscribe_);
114 2 : }
115 :
116 0 : void StartHostApiSubscribe()
117 : {
118 0 : isHostApiSubscribe_ = true;
119 0 : CallMsprofRegHostApi();
120 0 : ReportStoragedCompactInfo();
121 0 : HCCL_RUN_INFO("SetHostApiSubscribe:[%d]", isHostApiSubscribe_);
122 0 : }
123 :
124 2 : void StartTaskApiSubscribe()
125 : {
126 2 : isTaskApiSubscribe_ = true;
127 2 : CallMsprofRegTaskTypeApi();
128 2 : HCCL_RUN_INFO("SetTaskApiSubscribe:[%d]", isTaskApiSubscribe_);
129 2 : }
130 :
131 2 : void StartAdditionInfoSubscribe()
132 : {
133 2 : isAdditionInfoSubscribe_ = true;
134 2 : ReportStoragedAdditionInfo();
135 2 : HCCL_RUN_INFO("StartAdditionInfoSubscribe:[%d]", isAdditionInfoSubscribe_);
136 2 : }
137 :
138 2 : void StartSubscribe(uint64_t profconfig)
139 : {
140 : // profconfig同步到platform
141 2 : SetProfConfig(profconfig);
142 : // HostApi粒度的打点控制
143 2 : if ((profconfig & PROF_ACL_API_MASK) != 0) {
144 0 : StartHostApiSubscribe();
145 : }
146 :
147 : // aicpu模式下 开启L0就上报task打点; 其他场景开启L1才上报
148 2 : if ((GetExternalInputHcclAicpuUnfold() && (profconfig & PROF_TASK_TIME_MASK) != 0) ||
149 2 : ((profconfig & PROF_TASK_TIME_L1_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
150 2 : StartTaskApiSubscribe();
151 : }
152 :
153 : // 集合通信算子粒度的打点 只有L0打开的时候才上报 L1打开的时候不上报; AICPU也不上报算子粒度的打点
154 2 : if (((profconfig & PROF_TASK_TIME_MASK) != 0) && ((profconfig & PROF_TASK_TIME_L1_MASK) == 0)) {
155 2 : StartHostHcclOpSubscribe();
156 : }
157 :
158 2 : if (GetExternalInputHcclEnableFfts()) {
159 : // FFTS打开的时候 L0和L1都上报FFTSLauch和contextID
160 2 : if (((profconfig & PROF_TASK_TIME_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
161 2 : StartFftsLaunchSubscribe();
162 : }
163 : }
164 : // L1打开时, 上报task粒度的打点和子task的详细信息
165 2 : if (((profconfig & PROF_TASK_TIME_L1_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
166 2 : StartAdditionInfoSubscribe();
167 : } else {
168 0 : HCCL_RUN_INFO("[Profiling][CommandHandle] profSwitch is[%llu]", profconfig);
169 : }
170 2 : }
171 :
172 2 : void EsStartTaskApiSubscribe()
173 : {
174 2 : isTaskApiSubscribe_ = true;
175 2 : CallMsprofRegEsTaskTypeApi();
176 2 : HCCL_INFO("EsStartTaskApiSubscribe:[%d]", isTaskApiSubscribe_);
177 2 : }
178 :
179 0 : void EsStartAdditionInfoSubscribe()
180 : {
181 0 : isAdditionInfoSubscribe_ = true;
182 0 : HCCL_INFO("[Check][Param]EsStartAdditionInfoSubscribe.");
183 0 : }
184 :
185 2 : void EsStartSubscribe(uint64_t profconfig)
186 : {
187 2 : HCCL_INFO("[Profiling][CommandHandle] EsStartSubscribe profSwitch is[%llu]", profconfig);
188 : // profconfig同步到platform
189 2 : SetProfConfig(profconfig);
190 2 : if (((profconfig & PROF_TASK_TIME_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
191 2 : EsStartTaskApiSubscribe();
192 : }
193 :
194 2 : if (((profconfig & PROF_TASK_TIME_L1_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
195 0 : EsStartAdditionInfoSubscribe();
196 : }
197 :
198 2 : HCCL_INFO("EsStartSubscribe");
199 2 : }
200 :
201 3 : void StopSubscribe(uint64_t profconfig)
202 : {
203 : // profconfig同步到platform
204 3 : SetProfConfig(profconfig);
205 3 : CallMsprofRegHcclOpApi();
206 3 : ReportStoragedOpApi();
207 3 : ReportStoragedTaskApi();
208 3 : isHostApiSubscribe_ = false;
209 3 : isHostHcclOpSubscribe_ = false;
210 3 : isTaskApiSubscribe_ = false;
211 3 : isAdditionInfoSubscribe_ = false;
212 3 : isFftsLaunchSubscribe_ = false;
213 3 : HCCL_RUN_INFO("[ProfilingManage]StopSubscribe.");
214 3 : }
215 :
216 2 : void EsStopSubscribe(uint64_t profconfig)
217 : {
218 : // profconfig同步到platform
219 2 : SetProfConfig(profconfig);
220 2 : isTaskApiSubscribe_ = false;
221 2 : isAdditionInfoSubscribe_ = false;
222 2 : HCCL_INFO("[Check][Param]EsStopSubscribe.");
223 2 : }
224 :
225 0 : bool GetFftsLaunchApiState() const
226 : {
227 0 : return isFftsLaunchSubscribe_;
228 : }
229 75 : bool GetAdditionInfoState() const
230 : {
231 75 : return isAdditionInfoSubscribe_;
232 : }
233 26 : bool GetTaskApiState()
234 : {
235 26 : return isTaskApiSubscribe_;
236 : }
237 551 : bool GetAllState()
238 : {
239 1102 : return !isHostApiSubscribe_ &&
240 551 : !isTaskApiSubscribe_ &&
241 551 : !isAdditionInfoSubscribe_ &&
242 1653 : !isHostHcclOpSubscribe_ &&
243 1102 : !isFftsLaunchSubscribe_;
244 : }
245 : void SetFftsDispatcherMode();
246 : void ReSetFftsDispatcherMode();
247 : static void SetThreadCaptureStatus(s32 threadID, bool isCapture);
248 : static bool GetThreadCaptureStatus();
249 : static void DeleteThreadCaptureStatus(s32 threadID);
250 : private:
251 : MsprofReporterCallback reporterCallback_;
252 : bool isHostApiSubscribe_ = false;
253 : bool isTaskApiSubscribe_ = false;
254 : bool isAdditionInfoSubscribe_ = false;
255 : bool isHostHcclOpSubscribe_ = false;
256 : bool isFftsLaunchSubscribe_ = false;
257 : static std::queue<MsprofApi> storageTaskApi_;
258 : static std::array<std::queue<MsprofAdditionalInfo>, MAX_MODULE_DEVICE_NUM> storageAdditionInfo_;
259 : static std::array<std::mutex, MAX_MODULE_DEVICE_NUM> reportAddInfoMutex_;
260 : static std::array<std::queue<MsprofCompactInfo>, MAX_MODULE_DEVICE_NUM> storageCompactInfo_;
261 : static std::array<std::mutex, MAX_MODULE_DEVICE_NUM> reportCompactInfoMutex_;
262 : static std::mutex reportDataQueueMutex_;
263 : static std::array<std::queue<MsprofAdditionalInfo>, MAX_MODULE_DEVICE_NUM> storageAdditionInfoFftsCapture_;
264 : static std::array<std::mutex, MAX_MODULE_DEVICE_NUM> reportAddInfoFftsCaptureMutex_;
265 : std::atomic<bool> isFftsDispatcher_{false};
266 : static std::unordered_map<s32, bool> captureStatusThreadIDMap_;
267 : static std::mutex captureStatusMapMutex_;
268 : static std::queue<MsprofApi> storageOpApi_;
269 : static std::mutex reportDataOpQueueMutex_;
270 : };
271 : } // namespace hccl
272 : #endif // COMMON_PROFILING_PROFILING_MANAGER_H
|