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_INTERFACE_HPP
17 #define FFRT_CPU_MANAGER_INTERFACE_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 };
36 
37 enum class SleepType {
38     SLEEP_UNTIL_WAKEUP = 0,
39     SLEEP_UNTIL_INTERRUPT,
40     SLEEP_BREAK,
41 };
42 
43 struct CpuWorkerOps {
44     std::function<void (WorkerThread*)> WorkerLooper;
45     std::function<CPUEUTask* (WorkerThread*)> PickUpTask;
46     std::function<void (const WorkerThread*)> NotifyTaskPicked;
47     std::function<WorkerAction (const WorkerThread*)> WaitForNewAction;
48     std::function<void (WorkerThread*)> WorkerRetired;
49     std::function<void (WorkerThread*)> WorkerPrepare;
50     std::function<PollerRet (const WorkerThread*, int timeout)> TryPoll;
51     std::function<unsigned int (WorkerThread*)> StealTaskBatch;
52     std::function<CPUEUTask* (WorkerThread*)> PickUpTaskBatch;
53 #ifdef FFRT_WORKERS_DYNAMIC_SCALING
54     std::function<bool (WorkerThread*)> IsExceedRunningThreshold;
55     std::function<bool (void)> IsBlockAwareInit;
56 #endif
57 };
58 
59 struct CpuMonitorOps {
60     std::function<bool (const QoS& qos)> IncWorker;
61     std::function<void (const QoS& qos)> WakeupWorkers;
62     std::function<int (const QoS& qos)> GetTaskCount;
63     std::function<int (const QoS& qos)> GetWorkerCount;
64     std::function<void (const QoS& qos, void*, TaskNotifyType)> HandleTaskNotity;
65 };
66 
67 class CPUMonitor;
68 class CPUManagerStrategy {
69 public:
70     static WorkerThread* CreateCPUWorker(const QoS& qos, void* manager);
71     static CPUMonitor* CreateCPUMonitor(void* manager);
72 };
73 }
74 #endif
75