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 GLOBAL_CONFIG_H 17 #define GLOBAL_CONFIG_H 18 19 #ifdef USE_OHOS_QOS 20 #include "qos.h" 21 #else 22 #include "staging_qos/sched/qos.h" 23 #endif 24 #include "types.h" 25 26 namespace ffrt { 27 constexpr int DEFAULT_MINCONCURRENCY = 4; 28 constexpr int INTERACTIVE_MAXCONCURRENCY = USE_COROUTINE ? 8 : 40000; 29 constexpr int DEFAULT_MAXCONCURRENCY = USE_COROUTINE ? 8 : 80000; 30 constexpr int DEFAULT_HARDLIMIT = USE_COROUTINE ? 16 : 128; 31 constexpr int QOS_WORKER_MAXNUM = (8 * 16); 32 33 class GlobalConfig { 34 public: 35 GlobalConfig(const GlobalConfig&) = delete; 36 37 GlobalConfig& operator=(const GlobalConfig&) = delete; 38 ~GlobalConfig()39 ~GlobalConfig() {} 40 Instance()41 static inline GlobalConfig& Instance() 42 { 43 static GlobalConfig cfg; 44 return cfg; 45 } 46 setCpuWorkerNum(const QoS & qos,int num)47 void setCpuWorkerNum(const QoS& qos, int num) 48 { 49 if ((num <= 0) || (num > DEFAULT_MAXCONCURRENCY)) { 50 num = DEFAULT_MAXCONCURRENCY; 51 } 52 this->cpu_worker_num[qos()] = static_cast<size_t>(num); 53 } 54 getCpuWorkerNum(const QoS & qos)55 size_t getCpuWorkerNum(const QoS& qos) 56 { 57 return this->cpu_worker_num[qos()]; 58 } 59 60 private: GlobalConfig()61 GlobalConfig() 62 { 63 for (auto qos = QoS::Min(); qos < QoS::Max(); ++qos) { 64 if (qos == static_cast<int>(qos_user_interactive)) { 65 this->cpu_worker_num[qos] = INTERACTIVE_MAXCONCURRENCY; 66 } else { 67 this->cpu_worker_num[qos] = DEFAULT_MAXCONCURRENCY; 68 } 69 } 70 } 71 72 size_t cpu_worker_num[QoS::MaxNum()]; 73 }; 74 } 75 76 #endif /* GLOBAL_CONFIG_H */ 77