• 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 
16 #ifndef FFRT_CPU_MANAGER_STRATEGY_HPP
17 #define FFRT_CPU_MANAGER_STRATEGY_HPP
18 
19 #include "eu/worker_thread.h"
20 #include "qos.h"
21 #include "sync/poller.h"
22 #include "tm/cpu_task.h"
23 
24 namespace ffrt {
25 enum class WorkerAction {
26     RETRY = 0,
27     RETIRE,
28     MAX,
29 };
30 
31 enum class TaskNotifyType {
32     TASK_PICKED = 0,
33     TASK_ADDED,
34     TASK_LOCAL,
35     TASK_ESCAPED,
36 };
37 
38 struct CpuWorkerOps {
39     std::function<void (const WorkerThread*)> NotifyTaskPicked;
40     std::function<WorkerAction (const WorkerThread*)> WaitForNewAction;
41     std::function<void (WorkerThread*)> WorkerRetired;
42     std::function<void (WorkerThread*)> WorkerPrepare;
43     std::function<PollerRet (const WorkerThread*, int timeout)> TryPoll;
44     std::function<unsigned int (WorkerThread*)> StealTaskBatch;
45     std::function<TaskBase* (WorkerThread*)> PickUpTaskBatch;
46     std::function<int (const QoS&)> GetTaskCount; // Obtain the number of tasks corresponding to the QoS
47 #ifdef FFRT_WORKERS_DYNAMIC_SCALING
48     std::function<bool (WorkerThread*)> IsExceedRunningThreshold;
49     std::function<bool (void)> IsBlockAwareInit;
50 #endif
51 };
52 
53 struct CpuMonitorOps {
54     std::function<bool (const QoS& qos)> IncWorker;
55     std::function<void (const QoS& qos)> WakeupWorkers;
56     std::function<int (const QoS& qos)> GetTaskCount;
57     std::function<int (const QoS& qos)> GetWorkerCount;
58     std::function<void (const QoS& qos, void*, TaskNotifyType)> HandleTaskNotity;
59 };
60 
61 class CPUMonitor;
62 class CPUManagerStrategy {
63 public:
64     static WorkerThread* CreateCPUWorker(const QoS& qos, void* manager);
65     static CPUMonitor* CreateCPUMonitor(void* manager);
SetSchedMode(const QoS qos,const sched_mode_type mode)66     static inline void SetSchedMode(const QoS qos, const sched_mode_type mode)
67     {
68         schedMode[qos()] = mode;
69     }
70 
GetSchedMode(const QoS qos)71     static inline const sched_mode_type& GetSchedMode(const QoS qos)
72     {
73         return schedMode[qos];
74     }
75 private:
76     static std::array<sched_mode_type, QoS::MaxNum()> schedMode;
77 };
78 }
79 #endif
80