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_SUBSCRIBE_MANAGER_H
12 : #define QUEUE_SCHEDULE_SUBSCRIBE_MANAGER_H
13 :
14 : #include <cstdint>
15 : #include <unordered_map>
16 : #include <map>
17 : #include <set>
18 : #include <unordered_set>
19 : #include "common/bqs_log.h"
20 : #include "common/bqs_status.h"
21 : #include "driver/ascend_hal_external.h"
22 : #include "driver/ascend_hal.h"
23 :
24 : namespace bqs {
25 : /**
26 : * attention:not thread safe. can't call concurrently.
27 : */
28 :
29 : class SubscribeManager {
30 : public:
31 : static SubscribeManager &GetInstance();
32 :
33 57 : ~SubscribeManager() = default;
34 :
35 : SubscribeManager(const SubscribeManager &) = delete;
36 :
37 : SubscribeManager &operator=(const SubscribeManager &) = delete;
38 :
39 : SubscribeManager(SubscribeManager &&) = delete;
40 :
41 : SubscribeManager &operator=(SubscribeManager &&) = delete;
42 :
43 : void InitSubscribeManager(const uint32_t deviceId, const uint32_t enqueGroupId,
44 : const uint32_t f2nfGroupId, const uint32_t dstDeviceId);
45 :
46 : /**
47 : * Subscribe queue group event.
48 : * @param queueId queue id
49 : * @return BQS_STATUS_OK:success, other:failed
50 : */
51 : BqsStatus Subscribe(uint32_t queueId);
52 :
53 : /**
54 : * update subscribe queue group event.
55 : * @param queueId queue id
56 : * @return BQS_STATUS_OK:success, other:failed
57 : */
58 : BqsStatus UpdateSubscribe(const uint32_t queueId);
59 :
60 : /**
61 : * Unsubscribe queue group event.
62 : * @param queueId queue id
63 : * @return BQS_STATUS_OK:success, other:failed
64 : */
65 : BqsStatus Unsubscribe(const uint32_t queueId);
66 :
67 : /**
68 : * pause subscribe queue group event.
69 : * attention: must ensure dst queue will trigger full to not full event.
70 : * @param queueId queue id.
71 : * @param fullId full queue id or full tag id
72 : * @param idleLog idle status is true : log
73 : * @return BQS_STATUS_OK:success, other:failed
74 : */
75 : BqsStatus PauseSubscribe(const uint32_t queueId, const uint32_t fullId, const bool idleLog);
76 :
77 : /**
78 : * resume subscribe queue group event.
79 : * attention: must ensure dst queue will trigger full to not full event.
80 : * @param queueId queue id.
81 : * @param notFullId not full queue id or tag id.
82 : * @return BQS_STATUS_OK:success, other:failed
83 : */
84 : BqsStatus ResumeSubscribe(const uint32_t queueId, const uint32_t notFullId);
85 :
86 : /**
87 : * subscribe queue Full to not full event.
88 : * @param queueId queue id
89 : * @return BQS_STATUS_OK:success, other:failed
90 : */
91 : BqsStatus SubscribeFullToNotFull(uint32_t queueId);
92 :
93 : /**
94 : * update subscribe queue Full to not full event.
95 : * @param queueId queue id
96 : * @return BQS_STATUS_OK:success, other:failed
97 : */
98 : BqsStatus UpdateSubscribeFullToNotFull(const uint32_t queueId) const;
99 :
100 : /**
101 : * Unsubscribe queue Full to not full event.
102 : * @param queueId queue id
103 : * @return BQS_STATUS_OK:success, other:failed
104 : */
105 : BqsStatus UnsubscribeFullToNotFull(const uint32_t queueId);
106 :
107 57 : SubscribeManager() = default;
108 :
109 : private:
110 :
111 : /**
112 : * Resubscribe queue group event.
113 : * @param queueId queue id
114 : * @return BQS_STATUS_OK:success, other:failed
115 : */
116 : BqsStatus Resubscribe(const uint32_t queueId) const;
117 :
118 : /**
119 : * Resubscribe queue Full to not full event.
120 : * @param queueId queue id
121 : * @return BQS_STATUS_OK:success, other:failed
122 : */
123 : BqsStatus ResubscribeF2NF(const uint32_t queueId) const;
124 :
125 : drvError_t DefalutSubscribe(const uint32_t queueId, const QUEUE_EVENT_TYPE eventType) const;
126 : drvError_t EnhancedSubscribe(const uint32_t queueId, const QUEUE_EVENT_TYPE eventType) const;
127 : drvError_t DefalutUnSubscribe(const uint32_t queueId, const QUEUE_EVENT_TYPE eventType) const;
128 : drvError_t EnhancedUnSubscribe(const uint32_t queueId, const QUEUE_EVENT_TYPE eventType) const;
129 :
130 : private:
131 : // device chip id.
132 : uint32_t deviceId_ = 0U;
133 :
134 : // enqueue event group id.
135 : uint32_t enqueGroupId_ = 0U;
136 :
137 : // full to not full event group id.
138 : uint32_t f2nfGroupId_ = 1U;
139 :
140 : // queue subscribe(group event) status, true:subscribed, false:pause subscribed
141 : std::map<uint32_t, bool> subscribeQueuesMaps_;
142 :
143 : // full to not full event subscribe queues.
144 : std::set<uint32_t> fullToNotFullQueuesSets_;
145 : uint32_t dstDeviceId_{0U};
146 : bool extendDriverInterface_{false};
147 : };
148 :
149 : class Subscribers {
150 : public:
151 : static Subscribers &GetInstance();
152 : SubscribeManager *GetSubscribeManager(const uint32_t resId, const uint32_t deviceId);
153 : // init subscribers which subscribe queue on deviceId to dstDeviceId for each deviceId
154 : void InitSubscribeManagers(const std::set<uint32_t> &deviceIds, const uint32_t dstDeviceId);
155 :
156 : private:
157 : // resId - deviceId
158 : std::unordered_map<uint32_t, std::unordered_map<uint32_t, SubscribeManager>> subscribeManagers_;
159 : };
160 : } // namespace bqs
161 :
162 : #endif // QUEUE_SCHEDULE_SUBSCRIBE_MANAGER_H
|