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 16 #ifndef FFRT_SCHEDULER_HPP 17 #define FFRT_SCHEDULER_HPP 18 #include <list> 19 #include <vector> 20 #include <string> 21 #include <map> 22 #include <mutex> 23 #include <array> 24 #include "core/entity.h" 25 #include "eu/execute_unit.h" 26 #include "sync/sync.h" 27 #include "sched/task_scheduler.h" 28 #include "tm/cpu_task.h" 29 #include "util/cb_func.h" 30 #include "dfx/bbox/bbox.h" 31 32 namespace ffrt { 33 class Scheduler { 34 public: 35 Scheduler(const Scheduler&) = delete; 36 Scheduler& operator=(const Scheduler&) = delete; ~Scheduler()37 virtual ~Scheduler() 38 { 39 tearDown = true; 40 for (int i = 0; i < QoS::Max(); i++) { 41 SchedulerFactory::Recycle(taskSchedulers[i]); 42 } 43 } 44 45 // 获取调度器的单例 46 static Scheduler* Instance(); 47 GetScheduler(const QoS & qos)48 inline TaskScheduler& GetScheduler(const QoS& qos) 49 { 50 return *taskSchedulers[static_cast<unsigned short>(qos)]; 51 } 52 PushTask(const QoS & qos,TaskBase * task)53 void PushTask(const QoS& qos, TaskBase* task) 54 { 55 if (!tearDown && task) { 56 taskSchedulers[qos]->PushTask(task); 57 } 58 } 59 PopTask(const QoS & qos)60 TaskBase* PopTask(const QoS& qos) 61 { 62 if (tearDown) { 63 return nullptr; 64 } 65 return taskSchedulers[qos]->PopTask(); 66 } 67 GetTotalTaskCnt(const QoS & qos)68 inline uint64_t GetTotalTaskCnt(const QoS& qos) 69 { 70 return taskSchedulers[static_cast<unsigned short>(qos)]->GetTotalTaskCnt(); 71 } 72 GetGlobalTaskCnt(const QoS & qos)73 inline uint64_t GetGlobalTaskCnt(const QoS& qos) 74 { 75 return taskSchedulers[static_cast<unsigned short>(qos)]->GetGlobalTaskCnt(); 76 } 77 GetTaskSchedMode(const QoS & qos)78 inline const TaskSchedMode& GetTaskSchedMode(const QoS& qos) 79 { 80 return taskSchedulers[static_cast<unsigned short>(qos)]->GetTaskSchedMode(); 81 } 82 83 bool CancelUVWork(ffrt_executor_task_t* uvWork, int qos); 84 GetWorkerLocalQueue(const QoS & qos,pid_t pid)85 inline SpmcQueue* GetWorkerLocalQueue(const QoS& qos, pid_t pid) 86 { 87 return taskSchedulers[static_cast<unsigned short>(qos)]->GetWorkerLocalQueue(pid); 88 } 89 90 bool CheckUVTaskConcurrency(ffrt_executor_task_t* task, const QoS& qos); 91 ffrt_executor_task_t* PickWaitingUVTask(const QoS& qos); 92 93 std::atomic_bool tearDown { false }; 94 95 private: 96 std::array<TaskScheduler*, QoS::MaxNum()> taskSchedulers; Scheduler()97 Scheduler() 98 { 99 for (int i = 0; i < QoS::Max(); i++) { 100 taskSchedulers[i] = SchedulerFactory::Alloc(); 101 QoS qos = QoS(i); 102 GetScheduler(i).SetQos(qos); 103 } 104 } 105 }; 106 } // namespace ffrt 107 #endif 108