• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <numeric>
20 #include "tm/queue_task.h"
21 #include "queue/base_queue.h"
22 #include "eventhandler_interactive_queue.h"
23 
24 namespace ffrt {
25 struct HistoryTask {
26     int32_t senderKernelThreadId_{0};
27     std::string taskName_{0};
28     uint64_t sendTime_{0};
29     uint64_t handleTime_{0};
30     uint64_t beginTime_{0};
31     uint64_t triggerTime_{0};
32     uint64_t completeTime_{0};
33 
HistoryTaskHistoryTask34     HistoryTask()
35     {
36         beginTime_ = std::numeric_limits<uint64_t>::max();
37     }
38 
HistoryTaskHistoryTask39     HistoryTask(uint64_t beginTime, QueueTask* task)
40     {
41         beginTime_ = beginTime;
42         senderKernelThreadId_ = task->fromTid;
43         sendTime_ = task->GetUptime() - task->GetDelay();
44         taskName_ = task->label;
45         handleTime_ = task->GetUptime();
46     }
47 };
48 
49 class EventHandlerAdapterQueue : public EventHandlerInteractiveQueue {
50 public:
51     explicit EventHandlerAdapterQueue();
52     ~EventHandlerAdapterQueue() override;
53 
54     int Push(QueueTask* task) override;
55     QueueTask* Pull() override;
56 
GetActiveStatus()57     bool GetActiveStatus() override
58     {
59         std::lock_guard lock(mutex_);
60         return isActiveState_.load();
61     }
62 
GetQueueType()63     int GetQueueType() const override
64     {
65         return ffrt_queue_eventhandler_adapter;
66     }
67 
GetMapSize()68     uint64_t GetMapSize() override
69     {
70         std::lock_guard lock(mutex_);
71         return std::accumulate(std::begin(whenMapVec_), std::end(whenMapVec_), 0u,
72             [] (uint64_t size, const std::multimap<uint64_t, QueueTask*>& whenMap) { return size + whenMap.size(); });
73     }
74 
75     void Stop() override;
76     bool HasTask(const char* name) override;
77     int Remove() override;
78     int Remove(const char* name) override;
79     int Remove(const QueueTask* task) override;
80     uint64_t GetDueTaskCount() override;
81 
82     bool IsIdle();
83     int Dump(const char* tag, char* buf, uint32_t len, bool historyInfo = true);
84     int DumpSize(ffrt_inner_queue_priority_t priority);
85 
86     void SetCurrentRunningTask(QueueTask* task);
87     void PushHistoryTask(QueueTask* task, uint64_t triggerTime, uint64_t completeTime);
88 
89 private:
90     HistoryTask currentRunningTask_;
91     std::vector<HistoryTask> historyTasks_;
92     std::atomic_uint8_t historyTaskIndex_ {0};
93     std::vector<int> pulledTaskCount_;
94     std::multimap<uint64_t, QueueTask*> whenMapVec_[5];
95 };
96 
97 std::unique_ptr<BaseQueue> CreateEventHandlerAdapterQueue(const ffrt_queue_attr_t* attr);
98 } // namespace ffrt
99 
100 #endif // FFRT_EVENTHANDLER_ADAPTER_QUEUE_H
101