1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_BASE_H
17 #define BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_BASE_H
18 
19 #include <array>
20 #include <list>
21 #include <map>
22 #include <mutex>
23 
24 #include "event_queue.h"
25 
26 #define LOCAL_API __attribute__((visibility ("hidden")))
27 namespace OHOS {
28 namespace AppExecFwk {
29 class IoWaiter;
30 class EventHandler;
31 class DeamonIoWaiter;
32 
33 struct CurrentRunningEvent {
34     InnerEvent::TimePoint beginTime_;
35     std::weak_ptr<EventHandler> owner_;
36     uint64_t senderKernelThreadId_{0};
37     InnerEvent::TimePoint sendTime_;
38     InnerEvent::TimePoint handleTime_;
39     InnerEvent::TimePoint triggerTime_;
40     int64_t param_{0};
41     bool hasTask_{false};
42     std::string taskName_;
43     std::string callerInfo_;
44     InnerEvent::EventId innerEventId_ = 0u;
45     CurrentRunningEvent();
46     CurrentRunningEvent(InnerEvent::TimePoint time, InnerEvent::Pointer &event);
47 };
48 
49 class EventQueueBase : public EventQueue {
50 public:
51 
52     EventQueueBase();
53     explicit EventQueueBase(const std::shared_ptr<IoWaiter> &ioWaiter);
54     ~EventQueueBase();
55     DISALLOW_COPY_AND_MOVE(EventQueueBase);
56 
57     /**
58      * Insert an event into event queue with different priority.
59      * The events will be sorted by handle time.
60      *
61      * @param event Event instance which should be added into event queue.
62      * @param Priority Priority of the event
63      * @param insertType The type of insertint event to queue
64      *
65      * @see #Priority
66      */
67     void Insert(InnerEvent::Pointer &event, Priority priority = Priority::LOW,
68         EventInsertType insertType = EventInsertType::AT_END) override;
69 
70     /**
71      * Remove events if its owner is invalid.
72      */
73     void RemoveOrphan() override;
74 
75     /**
76      * Remove all events.
77      */
78     LOCAL_API void RemoveAll() override;
79 
80     /**
81      * Remove events with specified requirements.
82      *
83      * @param owner Owner of the event which is point to an instance of 'EventHandler'.
84      */
85     void Remove(const std::shared_ptr<EventHandler> &owner) override;
86 
87     /**
88      * Remove events with specified requirements.
89      *
90      * @param owner Owner of the event which is point to an instance of 'EventHandler'.
91      * @param innerEventId Remove events by event id.
92      */
93     void Remove(const std::shared_ptr<EventHandler> &owner, uint32_t innerEventId) override;
94 
95     /**
96      * Remove events with specified requirements.
97      *
98      * @param owner Owner of the event which is point to an instance of 'EventHandler'.
99      * @param innerEventId Remove events by event id.
100      * @param param Remove events by value of param.
101      */
102     void Remove(const std::shared_ptr<EventHandler> &owner, uint32_t innerEventId, int64_t param) override;
103 
104     /**
105      * Remove events with specified requirements.
106      *
107      * @param owner Owner of the event which is point to an instance of 'EventHandler'.
108      * @param name Remove events by name of the task.
109      */
110     bool Remove(const std::shared_ptr<EventHandler> &owner, const std::string &name) override;
111 
112     /**
113      * Get event from event queue one by one.
114      * Before calling this method, developers should call {@link #Prepare} first.
115      * If none should be handled right now, the thread will be blocked in this method.
116      * Call {@link #Finish} to exit from blocking.
117      *
118      * @return Returns nullptr if event queue is not prepared yet, or {@link #Finish} is called.
119      * Otherwise returns event instance.
120      */
121     InnerEvent::Pointer GetEvent() override;
122 
123     /**
124      * Get expired event from event queue one by one.
125      * Before calling this method, developers should call {@link #Prepare} first.
126      *
127      * @param nextExpiredTime Output the expired time for the next event.
128      * @return Returns nullptr if none in event queue is expired.
129      * Otherwise returns event instance.
130      */
131     InnerEvent::Pointer GetExpiredEvent(InnerEvent::TimePoint &nextExpiredTime) override;
132 
133     /**
134      * Prints out the internal information about an object in the specified format,
135      * helping you diagnose internal errors of the object.
136      *
137      * @param dumper The Dumper object you have implemented to process the output internal information.
138      */
139     void Dump(Dumper &dumper) override;
140 
141     /**
142      * Print out the internal information about an object in the specified format,
143      * helping you diagnose internal errors of the object.
144      *
145      * @param queueInfo queue Info.
146      */
147     void DumpQueueInfo(std::string& queueInfo) override;
148 
149     /**
150      * Checks whether the current EventHandler is idle.
151      *
152      * @return Returns true if all events have been processed; returns false otherwise.
153      */
154     bool IsIdle() override;
155 
156     /**
157      * Check whether this event queue is empty.
158      *
159      * @return If queue is empty return true otherwise return false.
160      */
161     bool IsQueueEmpty() override;
162 
163     /**
164      * Check whether an event with the given ID can be found among the events that have been sent but not processed.
165      *
166      * @param owner Owner of the event which is point to an instance of 'EventHandler'.
167      * @param innerEventId The id of the event.
168      */
169     bool HasInnerEvent(const std::shared_ptr<EventHandler> &owner, uint32_t innerEventId) override;
170 
171     /**
172      * Check whether an event carrying the given param can be found among the events that have been sent but not
173      * processed.
174      *
175      * @param owner The owner of the event which is point to an instance of 'EventHandler'.
176      * @param param The basic parameter of the event.
177      */
178     bool HasInnerEvent(const std::shared_ptr<EventHandler> &owner, int64_t param) override;
179 
180     LOCAL_API void PushHistoryQueueBeforeDistribute(const InnerEvent::Pointer &event) override;
181 
182     LOCAL_API void PushHistoryQueueAfterDistribute() override;
183 
184     LOCAL_API bool HasPreferEvent(int basePrio) override;
185 
186     LOCAL_API std::string DumpCurrentQueueSize() override;
187 
188     LOCAL_API PendingTaskInfo QueryPendingTaskInfo(int32_t fileDescriptor) override;
189     /**
190      * execute callback
191      *
192      * @param nextExpiredTime next task execute.
193      */
194     void TryExecuteObserverCallback(InnerEvent::TimePoint &nextExpiredTime, EventRunnerStage stage);
195     int64_t ExecuteObserverCallback(ObserverTrace obsTrace, EventRunnerStage stage, StageInfo &Info);
196     std::string GetObserverTypeName(Observer observerType);
197 
198     /**
199      * clear current observer.
200      */
201     void ClearObserver();
202 
203     /**
204      * Cancel And Wait.
205      */
206     LOCAL_API void CancelAndWait() override;
207 
208 private:
209     using RemoveFilter = std::function<bool(const InnerEvent::Pointer &)>;
210     using HasFilter = std::function<bool(const InnerEvent::Pointer &)>;
211 
212     struct HistoryEvent {
213         uint64_t senderKernelThreadId{0};
214         std::string taskName;
215         InnerEvent::EventId innerEventId = 0u;
216         bool hasTask{false};
217         InnerEvent::TimePoint sendTime;
218         InnerEvent::TimePoint handleTime;
219         InnerEvent::TimePoint triggerTime;
220         InnerEvent::TimePoint completeTime;
221         int32_t priority = -1;
222     };
223 
224     /*
225      * To avoid starvation of lower priority event queue, give a chance to process lower priority events,
226      * after continuous processing several higher priority events.
227      */
228     static const uint32_t DEFAULT_MAX_HANDLED_EVENT_COUNT = 5;
229 
230     // Sub event queues for IMMEDIATE, HIGH and LOW priority. So use value of IDLE as size.
231     static const uint32_t SUB_EVENT_QUEUE_NUM = static_cast<uint32_t>(Priority::IDLE);
232 
233     struct SubEventQueue {
234         std::list<InnerEvent::Pointer> queue;
235         uint32_t handledEventsCount{0};
236         uint32_t maxHandledEventsCount{DEFAULT_MAX_HANDLED_EVENT_COUNT};
237     };
238 
239     LOCAL_API void Remove(const RemoveFilter &filter);
240     LOCAL_API void RemoveOrphan(const RemoveFilter &filter);
241     LOCAL_API bool HasInnerEvent(const HasFilter &filter);
242     LOCAL_API InnerEvent::Pointer PickEventLocked(const InnerEvent::TimePoint &now,
243         InnerEvent::TimePoint &nextWakeUpTime);
244     LOCAL_API InnerEvent::Pointer GetExpiredEventLocked(InnerEvent::TimePoint &nextExpiredTime);
245     LOCAL_API std::string HistoryQueueDump(const HistoryEvent &historyEvent);
246     LOCAL_API std::string DumpCurrentRunning();
247     LOCAL_API void DumpCurrentRunningEventId(const InnerEvent::EventId &innerEventId, std::string &content);
248     LOCAL_API void DumpCurentQueueInfo(Dumper &dumper, uint32_t dumpMaxSize);
249 
250     // Sub event queues for different priority.
251     std::array<SubEventQueue, SUB_EVENT_QUEUE_NUM> subEventQueues_;
252 
253     // Event queue for IDLE events.
254     std::list<InnerEvent::Pointer> idleEvents_;
255 
256     // Next wake up time when block in 'GetEvent'.
257     InnerEvent::TimePoint wakeUpTime_ { InnerEvent::TimePoint::max() };
258 
259     // Mark if in idle mode, and record the start time of idle.
260     InnerEvent::TimePoint idleTimeStamp_ { InnerEvent::Clock::now() };
261 
262     bool isIdle_ {true};
263 
264     // current running event info
265     CurrentRunningEvent currentRunningEvent_;
266 
267     static const uint8_t HISTORY_EVENT_NUM_POWER = 32; // 必须是2的幂次,使用位运算取余
268     std::vector<HistoryEvent> historyEvents_;
269     uint8_t historyEventIndex_ = 0;
270 };
271 }  // namespace AppExecFwk
272 }  // namespace OHOS
273 
274 #endif  // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_BASE_H
275