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