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