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 QUEUE_SCHEDULE_BQS_QUEUE_SCHEDULE_H
12 : #define QUEUE_SCHEDULE_BQS_QUEUE_SCHEDULE_H
13 :
14 : #include <vector>
15 : #include <thread>
16 : #include <atomic>
17 : #include <set>
18 : #include <string>
19 : #include <condition_variable>
20 : #include "driver/ascend_hal.h"
21 : #include "common/bqs_status.h"
22 : #include "common/bqs_msg.h"
23 : #include "bind_relation.h"
24 : namespace bqs {
25 : class QueueSchedule {
26 : public:
27 : /**
28 : * QueueSchedule construct.
29 : * @param deviceId device chip id.
30 : * @param enqueGroupId enqueue group id.
31 : * @param f2nfGroupId full to not full group id.
32 : */
33 108 : explicit QueueSchedule(const InitQsParams ¶ms) : deviceId_(params.deviceId),
34 108 : enqueGroupId_(params.enqueGroupId),
35 108 : f2nfGroupId_(params.f2nfGroupId),
36 108 : running_(false),
37 108 : hasAICPU_(true),
38 108 : isZeroSizeAicpuNum_(false),
39 108 : reschedInterval_(params.reschedInterval),
40 108 : runMode_(params.runMode),
41 108 : qsInitGroupName_(params.qsInitGrpName),
42 108 : initQsParams_(params),
43 216 : abnormalInterval_(0U)
44 108 : {}
45 :
46 : ~QueueSchedule();
47 :
48 : QueueSchedule(const QueueSchedule &) = delete;
49 :
50 : QueueSchedule &operator=(const QueueSchedule &) = delete;
51 :
52 : QueueSchedule(QueueSchedule &&) = delete;
53 :
54 : QueueSchedule &operator=(QueueSchedule &&) = delete;
55 :
56 : public:
57 : /**
58 : * Init and start working.
59 : * @return BQS_STATUS_OK:success, other:failed
60 : */
61 : BqsStatus StartQueueSchedule();
62 :
63 : /* *
64 : * stop working.
65 : */
66 : void StopQueueSchedule();
67 :
68 : /**
69 : * destroy source.
70 : */
71 : void Destroy() const;
72 :
73 : /**
74 : * enqueue event working thread function
75 : * @param threadIndex: thread index
76 : * @param bindCpuIndex: bind cpu index
77 : * @param groupId: wait event group
78 : */
79 : void EnqueueThreadTask(const uint32_t deviceId, const uint32_t threadIndex, const uint32_t bindCpuIndex,
80 : const uint32_t groupId, const uint32_t index);
81 :
82 : /**
83 : * full to not full working thread function
84 : * @param threadIndex: thread index
85 : * @param bindCpuIndex: bind cpu index
86 : * @param groupId: wait event group
87 : */
88 : void F2NFThreadTask(const uint32_t threadIndex, const uint32_t bindCpuIndex, const uint32_t groupId);
89 :
90 : /**
91 : * daemon thread run function
92 : */
93 : void DaemonThreadTask(const uint32_t index);
94 :
95 : /**
96 : * wait for stop.
97 : */
98 : void WaitForStop();
99 :
100 : void ReportAbnormal() const;
101 : private:
102 : /**
103 : * handle enqueue event.
104 : * @param threadIndex thread index
105 : * @param event event
106 : */
107 : void ProcessEnqueueEvent(const uint32_t threadIndex, const event_info &event, const uint32_t index = 0U,
108 : const bool procF2NF = false);
109 :
110 : /**
111 : * process enqueue event loop.
112 : * @param threadIndex thread index
113 : * @param groupId groupId
114 : */
115 : void LoopProcessEnqueueEvent(const uint32_t threadIndex, const uint32_t deviceId, const uint32_t groupId,
116 : const uint32_t index);
117 :
118 : /**
119 : * start enqueue thread.
120 : * @param threadNum thread total num
121 : * @param aicpuBeginIndex begin index
122 : */
123 : BqsStatus StartThreadGroup(const uint32_t threadNum, const uint32_t deviceId, const uint32_t enqueGroupId,
124 : const uint32_t index);
125 :
126 : /**
127 : * start enqueue thread.
128 : * @param threadIndex thread id
129 : * @param bindCpuIndex cpu index
130 : */
131 : void BindAicpu(const uint32_t threadIndex, const uint32_t bindCpuIndex);
132 :
133 : /**
134 : * handle enqueue event in daemon thread
135 : */
136 : void DaemonEnqueueEvent(const uint32_t index);
137 :
138 : /**
139 : * schedule data buff for all queue.
140 : * @param dataEnqueue true means data enqueue, false means relation or f2nf enqueue
141 : */
142 : void ScheduleDataBuffAll(const bool dataEnqueue, const uint32_t index = 0U) const;
143 :
144 : /**
145 : * process full entity
146 : * @param entity entity
147 : */
148 : dgw::FsmStatus ProcessDstEntity(const EntityInfo &entity, const uint32_t index) const;
149 :
150 : /**
151 : * init drv event scheduler.
152 : * @return BQS_STATUS_OK: success, other: error
153 : */
154 : BqsStatus InitDrvSchedModule(const uint32_t deviceId, const uint32_t enqueGroupId,
155 : const uint32_t f2nfGroupId) const;
156 :
157 : /**
158 : * process event
159 : * @param event esched event info
160 : */
161 : BqsStatus ProcessEvent(const uint32_t threadIndex, event_info &event, const uint32_t index);
162 :
163 : void CheckIfRecover(uint32_t &errCount, const char_t * const identity, const uint32_t threadIndex,
164 : const uint32_t groupId) const;
165 :
166 : void DynamicSchedule(const uint32_t index) const;
167 :
168 : void ProcessFullToNotFullEvent(const uint32_t index);
169 :
170 : BqsStatus InitExtraSchedule(const std::set<uint32_t> &resDevids, uint32_t threadNum);
171 :
172 : /**
173 : * device chip id.
174 : */
175 : uint32_t deviceId_;
176 :
177 : /**
178 : * enqueue event group id.
179 : */
180 : uint32_t enqueGroupId_;
181 :
182 : /**
183 : * full to not full event group id.
184 : */
185 : uint32_t f2nfGroupId_;
186 :
187 : /**
188 : * thread run flag.
189 : */
190 : volatile bool running_;
191 :
192 : /**
193 : * daemon thread sleep for a while.
194 : */
195 : std::condition_variable daemonWait_;
196 :
197 : /**
198 : * run on aicpu flag.
199 : */
200 : bool hasAICPU_;
201 :
202 : /**
203 : * run on aicpu flag.
204 : */
205 : bool isZeroSizeAicpuNum_;
206 :
207 : /**
208 : * working threads.
209 : */
210 : std::vector<std::thread> workThreads_;
211 :
212 : /**
213 : * enqueue event working mutual exclusion
214 : */
215 : std::atomic_flag queueEventAtomicFlag_ = ATOMIC_FLAG_INIT;
216 :
217 : std::atomic_flag queueEventAtomicFlagExtra_ = ATOMIC_FLAG_INIT;
218 :
219 : /**
220 : * daemon threads wait mtx.
221 : */
222 : std::mutex daemonWaitMtx_;
223 :
224 : /**
225 : * daemon threads reschedule interval
226 : */
227 : uint32_t reschedInterval_;
228 :
229 : /**
230 : * qs run mode
231 : */
232 : bqs::QueueSchedulerRunMode runMode_;
233 :
234 : /**
235 : * group name send by tsd used for qs to attach when start
236 : */
237 : std::string qsInitGroupName_;
238 :
239 : /**
240 : * queue schedule initialization parameters
241 : */
242 : InitQsParams initQsParams_;
243 :
244 : /**
245 : * f2nf event working mutual exclusion
246 : */
247 : std::atomic_flag f2nfEventAtomicFlag_ = ATOMIC_FLAG_INIT;
248 :
249 : bool aicpuFeatureDisableRecvRequestEvent_ {false};
250 :
251 : bool aicpuFeatureSetPidPriority_ {false};
252 :
253 : uint32_t abnormalInterval_;
254 : };
255 : } // namespace bqs
256 : #endif // QUEUE_SCHEDULE_BQS_QUEUE_SCHEDULE_H
|