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 CPU_MONITOR_H 17 #define CPU_MONITOR_H 18 19 #include <atomic> 20 #include <vector> 21 #include <functional> 22 #include <mutex> 23 #include "qos.h" 24 #include "cpp/mutex.h" 25 #include "eu/cpu_manager_interface.h" 26 27 namespace ffrt { 28 struct WorkerCtrl { 29 size_t hardLimit = 0; 30 size_t maxConcurrency = 0; 31 size_t workerManagerID = 0; 32 int executionNum = 0; 33 int sleepingWorkerNum = 0; 34 #ifdef FFRT_IO_TASK_SCHEDULER 35 bool pollWaitFlag = false; 36 #endif 37 int deepSleepingWorkerNum = 0; 38 std::mutex lock; 39 }; 40 41 class CPUMonitor { 42 public: 43 CPUMonitor(CpuMonitorOps&& ops); 44 CPUMonitor(const CPUMonitor&) = delete; 45 CPUMonitor& operator=(const CPUMonitor&) = delete; 46 virtual ~CPUMonitor(); 47 uint32_t GetMonitorTid() const; 48 void HandleBlocked(const QoS& qos); 49 void DecExeNumRef(const QoS& qos); 50 void IncSleepingRef(const QoS& qos); 51 void DecSleepingRef(const QoS& qos); 52 virtual SleepType IntoSleep(const QoS& qos) = 0; 53 void WakeupCount(const QoS& qos); 54 void IntoDeepSleep(const QoS& qos); 55 void OutOfDeepSleep(const QoS& qos); 56 #ifdef FFRT_IO_TASK_SCHEDULER 57 void IntoPollWait(const QoS& qos); 58 void OutOfPollWait(const QoS& qos); 59 #endif 60 void TimeoutCount(const QoS& qos); 61 void RegWorker(const QoS& qos); 62 void UnRegWorker(); 63 virtual void Notify(const QoS& qos, TaskNotifyType notifyType) = 0; 64 int SetWorkerMaxNum(const QoS& qos, int num); 65 bool IsExceedDeepSleepThreshold(); 66 #ifdef FFRT_IO_TASK_SCHEDULER 67 int WakedWorkerNum(const QoS& qos); 68 #endif 69 70 uint32_t monitorTid = 0; 71 protected: 72 WorkerCtrl ctrlQueue[QoS::Max()]; 73 void Poke(const QoS& qos, TaskNotifyType notifyType); GetOps()74 CpuMonitorOps& GetOps() 75 { 76 return ops; 77 } 78 private: 79 size_t CountBlockedNum(const QoS& qos); 80 void SetupMonitor(); 81 void StartMonitor(); 82 83 std::thread* monitorThread; 84 CpuMonitorOps ops; 85 }; 86 } 87 #endif /* CPU_MONITOR_H */ 88