1 /*
2  * Copyright (c) 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 #ifndef FFRT_QUEUE_MONITOR_H
16 #define FFRT_QUEUE_MONITOR_H
17 
18 #include <vector>
19 #include <shared_mutex>
20 #include "sched/execute_ctx.h"
21 #include "queue_handler.h"
22 
23 namespace ffrt {
24 class QueueMonitor {
25 public:
26     static QueueMonitor &GetInstance();
27     void RegisterQueueId(uint32_t queueId, QueueHandler* queueStruct);
28     void ResetQueueInfo(uint32_t queueId);
29     void ResetQueueStruct(uint32_t queueId);
30     void UpdateQueueInfo(uint32_t queueId, const uint64_t &taskId);
31     uint64_t QueryQueueStatus(uint32_t queueId);
32     bool HasQueueActive();
33 
34 private:
35     QueueMonitor();
36     ~QueueMonitor();
37     QueueMonitor(const QueueMonitor &) = delete;
38     QueueMonitor(QueueMonitor &&) = delete;
39     QueueMonitor &operator=(const QueueMonitor &) = delete;
40     QueueMonitor &operator=(QueueMonitor &&) = delete;
41 
42     void SendDelayedWorker(TimePoint delay);
43     void CheckQueuesStatus();
44     void ResetTaskTimestampAfterWarning(uint32_t queueId, const uint64_t &taskId);
45 
46     WaitUntilEntry* we_ = nullptr;
47     uint64_t timeoutUs_ = 0;
48     std::shared_mutex mutex_;
49     std::vector<std::pair<uint64_t, TimePoint>> queuesRunningInfo_;
50     std::vector<QueueHandler*> queuesStructInfo_;
51     std::atomic_bool exit_ { true };
52     std::vector<uint64_t> lastReportedTask_;
53 };
54 } // namespace ffrt
55 
56 #endif // FFRT_QUEUE_MONITOR_H
57