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 : #ifndef AICPUSD_DUMP_TASK_H
11 : #define AICPUSD_DUMP_TASK_H
12 :
13 : #include <map>
14 : #include <memory>
15 : #include <mutex>
16 : #include <set>
17 : #include <string>
18 : #include <vector>
19 : #include "aicpusd_status.h"
20 : #include "dump_data.pb.h"
21 : #include "op_mapping_info.pb.h"
22 : #include "dump/adump_device_pub.h"
23 : #include "aicpusd_common.h"
24 : #include "aicpusd_sqe_adapter.h"
25 :
26 : namespace AicpuSchedule {
27 : constexpr uint32_t INVALID_VAL = 65535U;
28 : constexpr uint8_t STARS_DATADUMP_LOAD_INFO = 8;
29 :
30 : struct MappingInfoOptionalParam {
31 31 : MappingInfoOptionalParam()
32 31 : : hasModelName(false),
33 31 : hasModelId(false),
34 31 : modelId(0U),
35 31 : hasStepId(false),
36 31 : stepIdAddr(nullptr),
37 31 : hasIterationsPerLoop(false),
38 31 : iterationsPerLoopAddr(nullptr),
39 31 : hasLoopCond(false),
40 31 : loopCondAddr(nullptr)
41 31 : {}
42 :
43 : bool hasModelName;
44 : std::string modelName;
45 : bool hasModelId;
46 : uint32_t modelId;
47 : bool hasStepId;
48 : uint64_t* stepIdAddr;
49 : bool hasIterationsPerLoop;
50 : uint64_t* iterationsPerLoopAddr;
51 : bool hasLoopCond;
52 : uint64_t* loopCondAddr;
53 : };
54 :
55 : struct IntervalStep {
56 : uint64_t start;
57 : uint64_t end;
58 : };
59 :
60 : struct DumpStep {
61 : std::set<uint64_t> singleStep;
62 : std::vector<IntervalStep> intervalStep;
63 : std::string DebugString();
64 : };
65 :
66 : struct TaskInfo {
67 22 : TaskInfo() : streamId_(0U), taskId_(0U){};
68 :
69 11 : TaskInfo(const uint32_t streamId, const uint32_t taskId) : streamId_(streamId), taskId_(taskId){};
70 :
71 : uint32_t streamId_;
72 : uint32_t taskId_;
73 : friend bool operator<(const TaskInfo& item1, const TaskInfo& item2);
74 : };
75 :
76 22 : inline bool operator<(const TaskInfo& item1, const TaskInfo& item2)
77 : {
78 22 : if (item1.streamId_ == item2.streamId_) {
79 22 : return item1.taskId_ < item2.taskId_;
80 : }
81 0 : return item1.streamId_ < item2.streamId_;
82 : }
83 :
84 : class OpDumpTask {
85 : public:
86 : explicit OpDumpTask(const int32_t hostPid, const uint32_t deviceId);
87 22 : ~OpDumpTask() = default;
88 :
89 : /**
90 : * Preprocess op mapping info.
91 : * @param task task info from op mapping info
92 : * @param basePath base dump path
93 : * @param param optional param
94 : * @param dumpStep step need dump
95 : * @param skipAddressConversion indicates whether to skip address conversion based on addrtype
96 : * @return whather preprocess success
97 : */
98 : StatusCode PreProcessOpMappingInfo(
99 : const aicpu::dump::Task& task, const std::string& basePath, const MappingInfoOptionalParam& param,
100 : const DumpStep& dumpStep, const bool skipAddressConversion = false);
101 :
102 : /**
103 : * Deal with dump info event.
104 : * @return whather dump success
105 : */
106 : StatusCode DumpOpInfo(
107 : const uint32_t streamId = INVALID_VAL, const uint32_t taskId = INVALID_VAL,
108 : const std::string& dumpDebugInfo = "");
109 :
110 : /**
111 : * Get model id of this task.
112 : * @param modelId model id
113 : * @return whather get model id success
114 : */
115 : bool GetModelId(uint32_t& modelId) const;
116 :
117 : /**
118 : * Check this task is end graph task or not.
119 : * @return whather is end graph task
120 : */
121 : bool IsEndGraph() const;
122 :
123 : /**
124 : * Update dump number.
125 : * @return void
126 : */
127 : void UpdateDumpNum();
128 :
129 : /**
130 : * Get op name.
131 : * @return op name
132 : */
133 : std::string GetOpName() const;
134 :
135 : /**
136 : * Clear baseDumpData_.
137 : * @return void
138 : */
139 : void ClearBaseDumpData();
140 :
141 : private:
142 : /**
143 : * Get dump number
144 : * @param dumpNum task dump number
145 : * @return whather get dump param success
146 : */
147 : StatusCode GetDumpNumber(uint64_t& dumpNum);
148 :
149 : /**
150 : * This step need dump or not
151 : * @param step task dump number
152 : * @return whather need dump
153 : */
154 : bool NeedDump(const uint64_t step);
155 :
156 : StatusCode PreProcessOutput(const aicpu::dump::Task& task, ::toolkit::dumpdata::DumpData& dumpData);
157 :
158 : StatusCode PreProcessInput(const aicpu::dump::Task& task, ::toolkit::dumpdata::DumpData& dumpData);
159 :
160 : StatusCode PreProcessOpBuffer(const aicpu::dump::Task& task, ::toolkit::dumpdata::DumpData& dumpData);
161 : StatusCode PreProcessWorkspace(const aicpu::dump::Task& task, ::toolkit::dumpdata::DumpData& dumpData);
162 : StatusCode ProcessInputDump(
163 : const ::toolkit::dumpdata::DumpData& dumpData, const std::string& path, const IDE_SESSION ideSession);
164 :
165 : StatusCode ProcessOutputDump(
166 : const ::toolkit::dumpdata::DumpData& dumpData, const std::string& path, const IDE_SESSION ideSession);
167 :
168 : StatusCode ProcessOpBufferDump(
169 : const ::toolkit::dumpdata::DumpData& dumpData, const std::string& path, const IDE_SESSION ideSession);
170 :
171 : StatusCode ProcessOpWorkspaceDump(
172 : const ::toolkit::dumpdata::DumpData& dumpData, const std::string& path, const IDE_SESSION ideSession);
173 :
174 : StatusCode Dump(
175 : const std::string& path, char_t* const data, const uint64_t len, const IDE_SESSION ideSession,
176 : const bool isLastSlice) const;
177 :
178 : std::string DumpPath(
179 : const uint64_t nowTime, const uint64_t dumpNumber, const uint32_t streamId, const uint32_t taskId,
180 : const bool debugFlag = false);
181 :
182 : StatusCode DoDump(
183 : const std::string& dumpFilePath, const std::string& dumpDebugFilePath = "",
184 : const std::string& dumpDebugInfo = "");
185 :
186 : private:
187 : std::mutex dumpMtx_;
188 : ::toolkit::dumpdata::DumpData baseDumpData_;
189 : std::string baseDumpPath_;
190 : std::string opName_;
191 : std::string opType_;
192 : MappingInfoOptionalParam optionalParam_;
193 : uint64_t taskDumpNum_;
194 : TaskInfo taskInfo_;
195 : DumpStep dumpStep_;
196 : bool endGraph_;
197 : std::vector<uint64_t> inputsBaseAddr_;
198 : std::vector<uint64_t> outputsBaseAddr_;
199 : std::vector<uint64_t> opBufferAddr_;
200 : std::vector<uint64_t> opWorkspaceAddr_;
201 : uint64_t inputTotalSize_;
202 : uint64_t outputTotalSize_;
203 : uint64_t opBufferTotalSize_;
204 : uint64_t opWorkspaceTotalSize_;
205 : std::unique_ptr<char_t[]> buff_;
206 : uint64_t buffSize_;
207 : uint64_t offset_;
208 : bool skipAddressConversion_;
209 : int32_t hostPid_;
210 : uint32_t deviceId_;
211 : }; // class OpDumpTask
212 :
213 : class OpDumpTaskManager {
214 : public:
215 : static OpDumpTaskManager& GetInstance();
216 1 : OpDumpTaskManager() = default;
217 1 : ~OpDumpTaskManager() = default;
218 :
219 : /**
220 : * Load op mapping info
221 : * @param infoAddr info address pointer
222 : * @param len info length
223 : * @return whather load success
224 : */
225 : int32_t LoadOpMappingInfo(const char_t* const infoAddr, const uint32_t len);
226 :
227 : /**
228 : * Deal with dump info event for know shape.
229 : * @param streamId stream id
230 : * @param taskId task id
231 : * @return whather dump success
232 : */
233 : int32_t DumpOpInfo(
234 : const uint32_t streamId, const uint32_t taskId, const uint32_t streamId1 = INVALID_VAL,
235 : const uint32_t taskId1 = INVALID_VAL, const std::string& dumpDebugInfo = "");
236 :
237 : /**
238 : * Deal with dump info event for unknow shape.
239 : * @param opMappingInfoAddr op mapping info addr
240 : * @param opMappingInfoLen op mapping info length
241 : * @return whather dump success
242 : */
243 : int32_t DumpOpInfoForUnknowShape(const uint64_t opMappingInfoAddr, const uint64_t opMappingInfoLen) const;
244 :
245 : /**
246 : * clear all resource od data dump for ctrl cpu and minirc
247 : * @return void
248 : */
249 : void ClearResource();
250 :
251 : int32_t DoDump(const aicpu::dump::OpMappingInfo& opMappingInfo) const;
252 :
253 : private:
254 : OpDumpTaskManager(const OpDumpTaskManager&) = delete;
255 : OpDumpTaskManager& operator=(const OpDumpTaskManager&) = delete;
256 : OpDumpTaskManager(OpDumpTaskManager&&) = delete;
257 : OpDumpTaskManager& operator=(OpDumpTaskManager&&) = delete;
258 :
259 : /**
260 : * Get optional param from op mapping info proto
261 : * @param opMappingInfo op mapping info
262 : * @param optionalParam optional param
263 : * @return void
264 : */
265 : void GetOptionalParam(
266 : const aicpu::dump::OpMappingInfo& opMappingInfo, MappingInfoOptionalParam& optionalParam) const;
267 :
268 : /**
269 : * Update all task dump number of according model id
270 : * @param modelId model id
271 : * @return void
272 : */
273 : void UpdateDumpNumByModelId(const uint32_t modelId);
274 :
275 : /**
276 : * Porcess end graph task if it exist in opDumptasks
277 : * @param opDumptasks tasks
278 : * @return void
279 : */
280 : void ProcessEndGraph(const std::vector<std::shared_ptr<OpDumpTask>>& opDumptasks);
281 :
282 : /**
283 : * Parse dump step from string, like 0|1-20
284 : * @param str dump step string
285 : * @param dumpStep dump step of parse result
286 : * @return whather parse success
287 : */
288 : bool GetDumpStepFromString(const std::string& str, DumpStep& dumpStep) const;
289 :
290 : /**
291 : * Parse dump step from step string
292 : * @param step step string
293 : * @param tmpDumpStep dump step of parse result
294 : * @return whather parse success
295 : */
296 : bool MatchAndInsert(const std::string& step, DumpStep& tmpDumpStep) const;
297 :
298 : /**
299 : * load mapping info
300 : * @param opMappingInfo op mapping info proto
301 : * @return whather load success
302 : */
303 : int32_t Load(const aicpu::dump::OpMappingInfo& opMappingInfo);
304 :
305 : /**
306 : * unload mapping info
307 : * @param opMappingInfo op mapping info proto
308 : * @return whather unload success
309 : */
310 : int32_t Unload(const aicpu::dump::OpMappingInfo& opMappingInfo);
311 :
312 : /**
313 : * clear baseDumpData
314 : * @param TaskInfo taskInfo
315 : * @return void
316 : */
317 : void UnloadClearTaskInfo(const TaskInfo& taskInfo);
318 :
319 : private:
320 : std::multimap<TaskInfo, std::shared_ptr<OpDumpTask>> dumpTaskMap_;
321 : std::mutex dumpTaskMapMtx_;
322 : std::map<uint32_t, std::set<TaskInfo>> modelIdToTask_;
323 : };
324 : } // namespace AicpuSchedule
325 :
326 : #endif
|