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_MANAGER_H
12 : #define QUEUE_MANAGER_H
13 :
14 : #include <cstdint>
15 : #include <atomic>
16 : #include <condition_variable>
17 : #include "bqs_status.h"
18 : #include "bqs_log.h"
19 : #include "bind_relation.h"
20 : #include "bqs_util.h"
21 :
22 : namespace bqs {
23 : class QueueManager {
24 : public:
25 : static QueueManager& GetInstance();
26 :
27 : ~QueueManager();
28 :
29 : QueueManager(const QueueManager&) = delete;
30 :
31 : QueueManager(QueueManager&&) = delete;
32 :
33 : QueueManager& operator=(const QueueManager&) = delete;
34 :
35 : QueueManager& operator=(QueueManager&&) = delete;
36 :
37 : /**
38 : * init/create/subscribe buff queue
39 : * @return BQS_STATUS_OK:success other:failed
40 : */
41 : BqsStatus InitQueueManager(
42 : const uint32_t deviceId, const uint32_t groupId, const bool hasAICPU, const std::string& groupName);
43 : void InitExtra(const uint32_t deviceIdExtra, const uint32_t groupIdExtra);
44 :
45 : BqsStatus InitQueueExtra();
46 :
47 : /**
48 : * init/create/subscribe buff queue
49 : * @return BQS_STATUS_OK:success other:failed
50 : */
51 : BqsStatus InitQueue();
52 :
53 : /**
54 : * destroy buff queue
55 : * @return NA
56 : */
57 : void Destroy();
58 :
59 : /**
60 : * Enqueue a data to implies that the client sent a message
61 : * @return BQS_STATUS_OK:success other:failed
62 : */
63 : BqsStatus EnqueueRelationEvent();
64 :
65 : BqsStatus EnqueueRelationEventExtra();
66 :
67 : BqsStatus EnqueueRelationEventToQ(const uint32_t deviceId, const uint32_t relationEventQ) const;
68 :
69 : /**
70 : * handle the bind or unbind msg that the client sent
71 : * @return true:has handle relation msg, false:not handle
72 : */
73 : bool HandleRelationEvent(const uint32_t index = 0U) const;
74 :
75 : /**
76 : * enqueue the queue id of full to not full queue
77 : * @return BQS_STATUS_OK:success other:failed
78 : */
79 : BqsStatus EnqueueFullToNotFullEvent(const uint32_t index);
80 :
81 : /**
82 : * handle the event of full to not full
83 : * @return true:has handle f2nf msg, false:not handle
84 : */
85 : bool HandleFullToNotFullEvent(const uint32_t index);
86 :
87 : /**
88 : * enqueue the queue id of asyn mem buff queue
89 : * @return BQS_STATUS_OK:success other:failed
90 : */
91 : BqsStatus EnqueueAsynMemBuffEvent();
92 :
93 : /**
94 : * handle the event of asyn mem buff event
95 : * @return true:has handle asyn mem buff msg, false:not handle
96 : */
97 : bool HandleAsynMemBuffEvent(const uint32_t index);
98 :
99 : /**
100 : * enable async mem dequeu flag
101 : */
102 1 : void enableAsyncMemDequeueFlag() { isTriggeredByAsyncMemDequeue_ = true; }
103 :
104 : /**
105 : * enable async mem enqueue flag
106 : */
107 2 : void enableAsyncMemEnqueueFlag() { isTriggeredByAsyncMemEnqueue_ = true; }
108 :
109 : /**
110 : * work thread init success will notify queue manager
111 : * @return NA
112 : */
113 : void NotifyInitSuccess(const uint32_t index);
114 :
115 : /**
116 : * log queue status for error occur
117 : * @return NA
118 : */
119 : void LogErrorQueueStatus(const uint32_t queueId) const;
120 :
121 : /**
122 : * log relation queue status for error occur
123 : * @return NA
124 : */
125 : void LogErrorRelationQueueStatus() const;
126 :
127 : /**
128 : * create and subscribe buff queue
129 : * @param name queue name
130 : * @param depth queue depth
131 : * @param queueId queue id
132 : * @return BQS_STATUS_OK: success, other: failed
133 : */
134 : BqsStatus CreateAndSubscribeQueue(const char_t* const name, const uint32_t depth, uint32_t& queueId) const;
135 :
136 : BqsStatus CreateAndSubscribeQueueExtra(const char_t* const name, const uint32_t depth, uint32_t& queueId) const;
137 :
138 : /**
139 : * destroy queue
140 : * @param queueId queue id
141 : * @return BQS_STATUS_OK: success, other: failed
142 : */
143 : BqsStatus DestroyQueue(const uint32_t queueId) const;
144 :
145 : BqsStatus DestroyQueue(const uint32_t queueId, uint32_t deviceId) const;
146 :
147 : /**
148 : * @brief Create a Queue object
149 : * @param name queue name
150 : * @param depth queue depth
151 : * @param queueId queue id
152 : * @return BQS_STATUS_OK: success, other: failed
153 : */
154 : BqsStatus CreateQueue(const char_t* const name, const uint32_t depth, uint32_t& queueId) const;
155 :
156 : BqsStatus CreateQueue(const char_t* const name, const uint32_t depth, uint32_t& queueId, uint32_t deviceId) const;
157 :
158 : /**
159 : * @brief get device id
160 : * @return device id
161 : */
162 7 : inline uint32_t GetDeviceId() const { return deviceId_; }
163 :
164 2 : inline uint32_t GetExtraDeviceId() const { return deviceIdExtra_; }
165 :
166 : private:
167 : /**
168 : * @brief Construct a new Queue Manager object
169 : */
170 : QueueManager();
171 : BqsStatus UnsubscribeQueue(const uint32_t queueId, const QUEUE_EVENT_TYPE eventType) const;
172 : void Clear();
173 : void ClearQueue(const uint32_t queueId, const QUEUE_EVENT_TYPE eventType) const;
174 : void MakeUpF2NFMbuf(const uint32_t index);
175 : void MakeUpMbuf(Mbuf** mbufPtr) const;
176 :
177 : private:
178 : uint32_t deviceId_;
179 : uint32_t groupId_;
180 : std::string grpName_;
181 : uint32_t relationEventQId_;
182 : uint32_t fullToNotFullEventQId_;
183 : uint32_t asyncMemDequeueBuffQId_;
184 : uint32_t asyncMemEnqueueBuffQId_;
185 :
186 : std::condition_variable cv_; // condition var to wait queue_schedule init success
187 : std::mutex mutex_;
188 : bool initialized_; // true means queue_schedule has init success
189 : bool stopped_; // true means queue manager has been stopped
190 : std::atomic<bool> f2nfQueueEmptyFlag_;
191 : Mbuf* mbufForF2nf_;
192 : SpinLock f2nfLock_;
193 : bool relationEventQInitialized_;
194 : bool fullToNotFullEventQInitialized_;
195 : uint32_t deviceIdExtra_;
196 : uint32_t groupIdExtra_;
197 : uint32_t relationEventQIdExtra_;
198 : bool relationEventQInitializedExtra_;
199 : uint32_t fullToNotFullEventQIdExtra_;
200 : bool fullToNotFullEventQInitializedExtra_;
201 : Mbuf* mbufForF2nfExtra_;
202 : SpinLock f2nfLockExtra_;
203 : std::atomic<bool> f2nfQueueEmptyFlagExtra_;
204 : bool isTriggeredByAsyncMemDequeue_;
205 : bool isTriggeredByAsyncMemEnqueue_;
206 : bool ayncMemBuffEventQInitialized_;
207 : bool initiallizedExtra_;
208 : };
209 : } // namespace bqs
210 : #endif // QUEUE_MANAGER_H
|