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_EVENTHANDLER_ADAPTER_QUEUE_H
16 #define FFRT_EVENTHANDLER_ADAPTER_QUEUE_H
17 
18 #include <vector>
19 #include "tm/queue_task.h"
20 #include "eventhandler_interactive_queue.h"
21 
22 namespace ffrt {
23 struct HistoryTask {
24     int32_t senderKernelThreadId_{0};
25     std::string taskName_{0};
26     uint64_t sendTime_{0};
27     uint64_t handleTime_{0};
28     uint64_t beginTime_{0};
29     uint64_t triggerTime_{0};
30     uint64_t completeTime_{0};
31 
HistoryTaskHistoryTask32     HistoryTask()
33     {
34         beginTime_ = std::numeric_limits<uint64_t>::max();
35     }
36 
HistoryTaskHistoryTask37     HistoryTask(uint64_t beginTime, QueueTask* task)
38     {
39         beginTime_ = beginTime;
40         senderKernelThreadId_ = task->fromTid;
41         sendTime_ = task->GetUptime() - task->GetDelay();
42         taskName_ = task->label;
43         handleTime_ = task->GetUptime();
44     }
45 };
46 
47 class EventHandlerAdapterQueue : public EventHandlerInteractiveQueue {
48 public:
49     explicit EventHandlerAdapterQueue();
50     ~EventHandlerAdapterQueue() override;
51 
52     int Push(QueueTask* task) override;
53     QueueTask* Pull() override;
54 
GetActiveStatus()55     bool GetActiveStatus() override
56     {
57         std::unique_lock lock(mutex_);
58         return isActiveState_.load();
59     }
60 
GetQueueType()61     int GetQueueType() const override
62     {
63         return ffrt_queue_eventhandler_adapter;
64     }
65 
Remove(const QueueTask * task)66     int Remove(const QueueTask* task) override
67     {
68         return BaseQueue::Remove(task);
69     }
70 
Stop()71     void Stop() override
72     {
73         return BaseQueue::Stop();
74     }
75 
76     bool IsIdle();
77 
78     int Dump(const char* tag, char* buf, uint32_t len, bool historyInfo = true);
79     int DumpSize(ffrt_inner_queue_priority_t priority);
80 
81     void SetCurrentRunningTask(QueueTask* task);
82     void PushHistoryTask(QueueTask* task, uint64_t triggerTime, uint64_t completeTime);
83 
84 private:
85     HistoryTask currentRunningTask_;
86     std::vector<HistoryTask> historyTasks_;
87     std::atomic_uint8_t historyTaskIndex_ {0};
88     std::vector<int> pulledTaskCount_;
89 };
90 
91 std::unique_ptr<BaseQueue> CreateEventHandlerAdapterQueue(const ffrt_queue_attr_t* attr);
92 } // namespace ffrt
93 
94 #endif // FFRT_EVENTHANDLER_ADAPTER_QUEUE_H
95