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