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 CORE_AICPUSD_EVENT_MANAGER_PROC_H
12 : #define CORE_AICPUSD_EVENT_MANAGER_PROC_H
13 :
14 : #include <atomic>
15 : #include <cstdint>
16 : #include <functional>
17 : #include <mutex>
18 : #include <thread>
19 : #include <bitset>
20 : #include "aicpu_event_struct.h"
21 : #include "aicpusd_status.h"
22 : #include "aicpusd_common.h"
23 : #include "aicpusd_info.h"
24 : #include "ascend_hal.h"
25 : #include "event_custom_api_struct.h"
26 : #include "aicpusd_sqe_adapter.h"
27 : #include "ts_api.h"
28 :
29 : namespace AicpuSchedule {
30 : constexpr uint32_t ENABLE_MODE_OFFSET = 48U;
31 : constexpr uint32_t DATADUMP_ENABLE_MODE_OFFSET = 49U;
32 : constexpr uint32_t DEBUGDUMP_ENABLE_MODE_OFFSET = 50U;
33 : constexpr uint32_t MAX_AICPU_THREAD_NUM = 32U;
34 : // timeout interval
35 : constexpr int32_t AICPU_TIMEOUT_INTERVAL = 3000;
36 :
37 : class AicpuEventManager {
38 : public:
39 : __attribute__((visibility("default"))) static AicpuEventManager& GetInstance();
40 :
41 : /**
42 : * @ingroup AicpuEventManager
43 : * @brief it use to init.
44 : * @param [in] noThreadFlag : have thread flag.
45 : * @param [in] runningFlag : running flag.
46 : * @param [in] grpId : group id.
47 : * @param [in] aicpuSchedMode : aicpu sched mode.
48 : * @return AICPU_SCHEDULE_OK: success, other: error code
49 : */
50 : __attribute__((visibility("default"))) void InitEventMgr(
51 : const bool noThreadFlag, const bool runningFlag, const uint32_t grpId);
52 :
53 : __attribute__((visibility("default"))) void InitEventFunc(const AicpuSchedMode aicpuSchedMode);
54 : /**
55 : * @ingroup AicpuEventManager
56 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
57 : * @brief it is used in multi-thread.
58 : */
59 : void LoopProcess(const uint32_t threadIndex);
60 :
61 : /**
62 : * @ingroup AicpuEventManager
63 : * @brief it use to set the exit flag of loop which wait event.
64 : */
65 : void CheckAndSetExitFlag(void) const;
66 : bool ModelLoopTimeOut(const int32_t retVal, uint32_t& waitCounter, const uint32_t maxCounter);
67 : /**
68 : * @ingroup AicpuEventManager
69 : * @brief it is used in call model, which is used in different thread need different while loop variable.
70 : */
71 : void CallModeLoopProcess();
72 :
73 : /**
74 : * @ingroup AicpuEventManager
75 : * @brief it is used to parse the struct and get some parameter.
76 : * @param [in] drvEventInfo : the event information from mailbox.
77 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
78 : * @return AICPU_SCHEDULE_OK: success, other: error code
79 : */
80 : int32_t ProcessHWTSKernelEvent(const event_info& drvEventInfo, const uint32_t threadIndex) const;
81 :
82 : /**
83 : * @ingroup AicpuEventManager
84 : * @brief it use to process control task from ts
85 : * @param [in] drvEventInfo : the event information from ts.
86 : * @return AICPU_SCHEDULE_OK: success, other: error code
87 : */
88 : int32_t ProcessHWTSControlEvent(const event_info& drvEventInfo);
89 :
90 : /**
91 : * @ingroup AicpuEventManager
92 : * @brief it use to execute the model from call interface.
93 : * @param [in] modelId : the id of model.
94 : * @return AICPU_SCHEDULE_OK: success, other: error code
95 : */
96 : int32_t ExecuteProcess(const uint32_t modelId);
97 :
98 : /**
99 : * @ingroup AicpuEventManager
100 : * @brief it use to execute the model from call interface.
101 : * @param [in] drvEventInfo : event info.
102 : * @param [out] drvEventAck : event ack.
103 : * @return 0: success, other: error code
104 : */
105 : int32_t ExecuteProcessSyc(const event_info* const drvEventInfo, event_ack* const drvEventAck);
106 :
107 : /**
108 : * @ingroup AicpuEventManager
109 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
110 : * @param [in] deviceId : device id
111 : * @brief it is used to wait event for once.
112 : */
113 : int32_t DoOnce(const uint32_t threadIndex, const uint32_t deviceId, const int32_t timeout = AICPU_TIMEOUT_INTERVAL);
114 :
115 : /**
116 : * @ingroup AicpuEventManager
117 : * @brief it use to set running flag
118 : * @param [in] runningFlag : running flag.
119 : */
120 : void SetRunningFlag(const bool runningFlag);
121 :
122 : /**
123 : * @ingroup AicpuEventManager
124 : * @brief print aic error report info
125 : * @param [in] reportInfo : info from aicpu sd adapter.
126 : */
127 : void PrintAicErrReportInfo(const AicpuSqeAdapter::AicErrReportInfo& reportInfo);
128 : /**
129 : * @ingroup AicpuEventManager
130 : * @brief it use to get running flag
131 : * @return true: running, false: stopped
132 : */
133 1 : bool GetRunningFlag() { return runningFlag_; }
134 :
135 : private:
136 : AicpuEventManager();
137 :
138 3 : virtual ~AicpuEventManager()
139 3 : {
140 3 : if (cancelLastword_ != nullptr) {
141 2 : cancelLastword_();
142 : }
143 3 : };
144 :
145 : AicpuEventManager(const AicpuEventManager&) = delete;
146 :
147 : AicpuEventManager& operator=(const AicpuEventManager&) = delete;
148 :
149 : int32_t DoOnceEsched(const uint32_t threadIndex, const uint32_t deviceId, const int32_t timeout);
150 : int32_t DoOnceMsq(const uint32_t threadIndex, const uint32_t deviceId, const int32_t timeout);
151 :
152 : /**
153 : * @ingroup AicpuEventManager
154 : * @brief it use to execute the model from ts.
155 : * @param [in] modelId : the id of model.
156 : * @return AICPU_SCHEDULE_OK: success, other: error code
157 : */
158 : int32_t TsControlExecuteProcess(const uint32_t modelId) const;
159 :
160 : /**
161 : * @ingroup AicpuEventManager
162 : * @brief it use to process task abort.
163 : * @param [in] info : the information of task.
164 : * @return AICPU_SCHEDULE_OK: success, other: error code
165 : */
166 : int32_t TsControlTaskAbort(const AicpuSqeAdapter::AicpuModelOperateInfo& info) const;
167 :
168 : /**
169 : * @ingroup AicpuEventManager
170 : * @brief it use to process distributing event.
171 : * @param [in] info : the information of task.
172 : * @return AICPU_SCHEDULE_OK: success, other: error code
173 : */
174 : int32_t EventDistribution(const AicpuSqeAdapter::AicpuModelOperateInfo& info) const;
175 :
176 : /**
177 : * @ingroup AicpuEventManager
178 : * @brief it use to process TS_AICPU_MODEL_DESTROY task.
179 : * @param [in] info : the information of task.
180 : * @return AICPU_SCHEDULE_OK: success, other: error code
181 : */
182 : int32_t ProcessTsAicpuModelDestroy(const AicpuSqeAdapter::AicpuModelOperateInfo& info) const;
183 :
184 : /**
185 : * @ingroup AicpuEventManager
186 : * @brief it use to process control task from ts
187 : * @param [in] ctrlMsg : the struct of control task.
188 : * @param [in] subEvent : it is used to store subeventid which is in receive struct.
189 : * @return AICPU_SCHEDULE_OK: success, other: error code
190 : */
191 : int32_t ProcessModelEvent(AicpuSqeAdapter& aicpuSqeAdapter, const uint32_t subEvent);
192 :
193 : /**
194 : * @ingroup AicpuEventManager
195 : * @brief it is used to parse the struct and get some parameter.
196 : * @param [in] drvEventInfo : the event information from mailbox.
197 : * @param [in] mailboxId : mailbox id
198 : * @param [in] info : the info is used to execute task.
199 : * @param [in] serialNo : the serial no. in mailbox, which is need to use in response package.
200 : * @param [out] streamId : streamId is used to cache task.
201 : * @param [out] taskId : task id.
202 : * @return AICPU_SCHEDULE_OK: success, other: error code
203 : */
204 : int32_t HWTSKernelEventMessageParse(
205 : const event_info& drvEventInfo, uint32_t& mailboxId, aicpu::HwtsTsKernel& info, uint64_t& serialNo,
206 : uint32_t& streamId, uint64_t& taskId, uint16_t& dataDumpEnableMode) const;
207 :
208 : /**
209 : * @ingroup AicpuEventManager
210 : * @brief it is used to process dvpp event.
211 : * @param [in] drvEventInfo : the event information from mailbox.
212 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
213 : * @return AICPU_SCHEDULE_OK: success, other: error code
214 : */
215 : int32_t ProcessDvppEvent(const event_info& drvEventInfo, const uint32_t threadIndex) const;
216 :
217 : /**
218 : * @ingroup AicpuEventManager
219 : * @brief it is used to process host mpi dvpp event.
220 : * @param [in] drvEventInfo : the event information from mailbox.
221 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
222 : * @return AICPU_SCHEDULE_OK: success, other: error code
223 : */
224 : int32_t ProcessMpiDvppEvent(const event_info& drvEventInfo, const uint32_t threadIndex) const;
225 :
226 : /**
227 : * @ingroup AicpuEventManager
228 : * @brief it is used to process retr event.
229 : * @param [in] drvEventInfo : the event information from mailbox.
230 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
231 : * @return AICPU_SCHEDULE_OK: success, other: error code
232 : */
233 : int32_t ProcessRetrEvent(const event_info& drvEventInfo, const uint32_t threadIndex) const;
234 :
235 : /**
236 : * @ingroup AicpuEventManager
237 : * @brief it is used to process cdq event.
238 : * @param [in] drvEventInfo : the event information from mailbox.
239 : * @return AICPU_SCHEDULE_OK: success, other: error code
240 : */
241 : int32_t ProcessCdqEvent(const event_info& drvEventInfo) const;
242 :
243 : /**
244 : * @ingroup AicpuEventManager
245 : * @brief it use to process all event.
246 : * @param [in] drvEventInfo : the event information from ts.
247 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
248 : * @return AICPU_SCHEDULE_OK: success, other: error code
249 : */
250 : int32_t ProcessEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
251 :
252 : /**
253 : * @ingroup AicpuEventManager
254 : * @brief it use to process default event.
255 : * @param [in] drvEventInfo : the event information
256 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
257 : * @return AICPU_SCHEDULE_OK: success, other: error code
258 : */
259 : int32_t ProcDefaultEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
260 :
261 : /**
262 : * @ingroup AicpuEventManager
263 : * @brief it is used to process EVENT_TS_HWTS_KERNEL event
264 : * @param [in] drvEventInfo : the event information.
265 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
266 : * @return AICPU_SCHEDULE_OK: success, other: error code
267 : */
268 : int32_t ProcTsHWTSEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
269 :
270 : /**
271 : * @ingroup AicpuEventManager
272 : * @brief it is used to execute HWTS_KERNEL event Task
273 : * @return AICPU_SCHEDULE_OK: success, other: error code
274 : */
275 : int32_t ExecuteHWTSEventTask(
276 : const uint32_t& threadIndex, const uint32_t& streamId, const uint64_t taskId, const uint64_t& serialNo,
277 : aicpu::HwtsTsKernel& aicpufwKernelInfo, const event_info& drvEventInfo, const uint32_t& mailboxId,
278 : hwts_response_t& hwtsResp, uint16_t& dataDumpEnableMode) const;
279 : /**
280 : * @ingroup AicpuEventManager
281 : * @brief it is used to execute HWTS_KERNEL event Task, kernelType is KERNEL_TYPE_AICPU_KFC
282 : * @return AICPU_SCHEDULE_OK: success, other: error code
283 : */
284 : int32_t ExecuteHWTSKFCEventTask(
285 : const event_info& drvEventInfo, const aicpu::HwtsTsKernel& aicpufwKernelInfo, const uint32_t threadIndex,
286 : const uint32_t streamId, const uint64_t taskId) const;
287 :
288 : /**
289 : * @ingroup AicpuEventManager
290 : * @brief it is used to process EVENT_DVPP_MSG event
291 : * @param [in] drvEventInfo : the event information.
292 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
293 : * @return AICPU_SCHEDULE_OK: success, other: error code
294 : */
295 : int32_t ProcDvppMsgEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
296 :
297 : /**
298 : * @ingroup AicpuEventManager
299 : * @brief it is used to process EVENT_DVPP_MPI_MSG event
300 : * @param [in] drvEventInfo : the event information.
301 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
302 : * @return AICPU_SCHEDULE_OK: success, other: error code
303 : */
304 : int32_t ProcDvppMpiMsgEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
305 :
306 : /**
307 : * @ingroup AicpuEventManager
308 : * @brief it is used to process EVENT_FR_MSG event
309 : * @param [in] drvEventInfo : the event information.
310 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
311 : * @return AICPU_SCHEDULE_OK: success, other: error code
312 : */
313 : int32_t ProcFrMsgEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
314 :
315 : /**
316 : * @ingroup AicpuEventManager
317 : * @brief it is used to process EVENT_SPLIT_KERNEL event
318 : * @param [in] drvEventInfo : the event information.
319 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
320 : * @return AICPU_SCHEDULE_OK: success, other: error code
321 : */
322 : int32_t ProcSplitKernelEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
323 :
324 : /**
325 : * @ingroup AicpuEventManager
326 : * @brief it is used to process EVENT_RANDOM_KERNEL event
327 : * @param [in] drvEventInfo : the event information.
328 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
329 : * @return AICPU_SCHEDULE_OK: success, other: error code
330 : */
331 : int32_t ProcRandomKernelEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
332 :
333 : /**
334 : * @ingroup AicpuEventManager
335 : * @brief it is used to process EVENT_TS_CTRL_MSG event
336 : * @param [in] drvEventInfo : the event information.
337 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
338 : * @return AICPU_SCHEDULE_OK: success, other: error code
339 : */
340 : int32_t ProcTsCtrlEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
341 :
342 : /**
343 : * @ingroup AicpuEventManager
344 : * @brief it is used to process EVENT_QUEUE_EMPTY_TO_NOT_EMPTY event
345 : * @param [in] drvEventInfo : the event information.
346 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
347 : * @return AICPU_SCHEDULE_OK: success, other: error code
348 : */
349 : int32_t ProcQueueEmptyToNotEmptyEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
350 :
351 : /**
352 : * @ingroup AicpuEventManager
353 : * @brief it is used to process EVENT_QUEUE_EMPTY_TO_NOT_EMPTY event
354 : * @param [in] drvEventInfo : the event information.
355 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
356 : * @return AICPU_SCHEDULE_OK: success, other: error code
357 : */
358 : int32_t ProcQueueFullToNotFullEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
359 :
360 : /**
361 : * @ingroup AicpuEventManager
362 : * @brief it is used to process EVENT_AICPU_MSG event
363 : * @param [in] drvEventInfo : the event information.
364 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
365 : * @return AICPU_SCHEDULE_OK: success, other: error code
366 : */
367 : int32_t ProcAicpuMsgEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
368 :
369 : /**
370 : * @ingroup AicpuEventManager
371 : * @brief it is used to process EVENT_QUEUE_ENQUEUE event
372 : * @param [in] drvEventInfo : the event information.
373 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
374 : * @return AICPU_SCHEDULE_OK: success, other: error code
375 : */
376 : int32_t ProcQueueEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
377 :
378 : /**
379 : * @ingroup AicpuEventManager
380 : * @brief it is used to process EVENT_TDT_ENQUEUE, EVENT_ACPU_MSG_TYPE1 event
381 : * @param [in] drvEventInfo : the event information.
382 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
383 : * @return AICPU_SCHEDULE_OK: success, other: error code
384 : */
385 : int32_t ProcOnPreprocessEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
386 :
387 : /**
388 : * @ingroup AicpuEventManager
389 : * @brief it is used to process EVENT_CDQ_MSG event
390 : * @param [in] drvEventInfo : the event information.
391 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
392 : * @return AICPU_SCHEDULE_OK: success, other: error code
393 : */
394 : int32_t ProcCDQEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
395 :
396 : /**
397 : * @ingroup AicpuEventManager
398 : * @brief it is used to process EVENT_QS_MSG event
399 : * @param [in] drvEventInfo : the event information.
400 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
401 : * @return AICPU_SCHEDULE_OK: success, other: error code
402 : */
403 : int32_t ProcQsEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
404 :
405 : /**
406 : * @ingroup AicpuEventManager
407 : * @brief it is used to process EVENT_DRV_MSG event
408 : * @param [in] drvEventInfo : the event information.
409 : * @param [in] threadIndex : thread index assign by aicpu index, but when running time they are different.
410 : * @return AICPU_SCHEDULE_OK: success, other: error code
411 : */
412 : int32_t ProcDrvEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
413 :
414 : /**
415 : * @ingroup AicpuEventManager
416 : * @brief it use to process control task from ts version 0
417 : * @param [in] drvEventInfo : the event information from ts.
418 : * @return AICPU_SCHEDULE_OK: success, other: error code
419 : */
420 : int32_t ProcessHWTSControlEventV0(const event_info& drvEventInfo);
421 :
422 : /**
423 : * @ingroup AicpuEventManager
424 : * @brief it use to process control task from ts version 1
425 : * @param [in] drvEventInfo : the event information from ts.
426 : * @return AICPU_SCHEDULE_OK: success, other: error code
427 : */
428 : int32_t ProcessHWTSControlEventV1(const event_info& drvEventInfo);
429 :
430 : /**
431 : * @ingroup AicpuEventManager
432 : * @brief it use to init control event version function map
433 : */
434 : void InitControlVersionFunc();
435 :
436 : /**
437 : * @ingroup AicpuEventManager
438 : * @brief it use to get stream id and task id form event info
439 : */
440 : void GetStreamIdAndTaskIdFromEvent(
441 : const hwts_ts_kernel& tsKernel, uint32_t& streamId, uint64_t& taskId, uint32_t subevent_id) const;
442 :
443 : void InitLastwordCallback();
444 :
445 : int32_t ProcProxyEvent(const event_info& drvEventInfo, const uint32_t threadIndex);
446 : int32_t FillEvent(const AicpuTopicMailbox& mb, event_info& eventInfo) const;
447 : int32_t FillEventCommInfo(const AicpuTopicMailbox& mb, event_info& eventInfo) const;
448 : int32_t FillEventPrivHwtsInfo(const AicpuTopicMailbox& mb, event_info& eventInfo) const;
449 : int32_t FillEventPrivCommInfo(const AicpuTopicMailbox& mb, event_info& eventInfo) const;
450 :
451 : int32_t SendResponse(const uint32_t deviceId, const uint32_t subEventId, hwts_response_t& resp) const;
452 : int32_t ResponseMsq(const uint32_t deviceId, const uint32_t subEventId, hwts_response_t& resp) const;
453 : int32_t ResponseEsched(const uint32_t deviceId, const uint32_t subEventId, hwts_response_t& resp) const;
454 :
455 : // it is used to mark which is in call mode or multi-thread mode.
456 : std::atomic<bool> noThreadFlag_;
457 :
458 : // it is used to mark the loop.
459 : std::atomic<bool> runningFlag_;
460 :
461 : // the id is used to group in process.
462 : uint32_t groupId_;
463 :
464 : using AicpuEventGetFunc = int32_t (AicpuEventManager::*)(const uint32_t, const uint32_t, const int32_t);
465 : AicpuEventGetFunc eventGetFunc_;
466 :
467 : using AicpuEventRspFunc = int32_t (AicpuEventManager::*)(const uint32_t, const uint32_t, hwts_response_t&) const;
468 : AicpuEventRspFunc eventRspFunc_;
469 :
470 : using AicpuControlEventVersionFunc = int32_t (AicpuEventManager::*)(const event_info&);
471 : std::map<uint16_t, AicpuControlEventVersionFunc> controlEventVersionFuncMap_;
472 :
473 : // event process function type
474 : using AicpuEventProc = int32_t (AicpuEventManager::*)(const event_info&, const uint32_t);
475 :
476 : AicpuEventProc aicpuEventProcFunc_[EVENT_MAX_NUM] = {nullptr};
477 :
478 : uint64_t recvEventStat_[MAX_AICPU_THREAD_NUM][EVENT_MAX_NUM];
479 : uint64_t procEventStat_[MAX_AICPU_THREAD_NUM][EVENT_MAX_NUM];
480 : std::function<void()> cancelLastword_ = nullptr;
481 : };
482 : } // namespace AicpuSchedule
483 : #endif
|