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 "sched/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 std::mutex lock; 35 }; 36 37 class CPUMonitor { 38 public: 39 CPUMonitor(CpuMonitorOps&& ops); 40 CPUMonitor(const CPUMonitor&) = delete; 41 CPUMonitor& operator=(const CPUMonitor&) = delete; 42 ~CPUMonitor(); 43 uint32_t GetMonitorTid() const; 44 void HandleBlocked(const QoS& qos); 45 void DecExeNumRef(const QoS& qos); 46 void IncSleepingRef(const QoS& qos); 47 void DecSleepingRef(const QoS& qos); 48 void IntoSleep(const QoS& qos); 49 void WakeupCount(const QoS& qos); 50 void TimeoutCount(const QoS& qos); 51 void RegWorker(const QoS& qos); 52 void UnRegWorker(); 53 void Notify(const QoS& qos, TaskNotifyType notifyType); 54 55 uint32_t monitorTid = 0; 56 57 private: 58 size_t CountBlockedNum(const QoS& qos); 59 void SetupMonitor(); 60 void StartMonitor(); 61 void Poke(const QoS& qos); 62 63 std::thread* monitorThread; 64 CpuMonitorOps ops; 65 WorkerCtrl ctrlQueue[QoS::Max()]; 66 }; 67 } 68 #endif /* CPU_MONITOR_H */ 69