• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "qos.h"
17 #include <cstdlib>
18 #include <unistd.h>
19 #include "qos_common.h"
20 #include "rme_log_domain.h"
21 
22 constexpr int ERROR_NUM = -1;
23 
24 namespace OHOS {
25 namespace QOS {
26 using namespace QosCommon;
27 DEFINE_RMELOG_INTELLISENSE("qos_manager");
28 
29 static struct QosPolicyDatas g_defaultPolicy = {
30     .policyType = QOS_POLICY_DEFAULT,
31     .policyFlag = QOS_ALL_FLAG,
32     .policys = {
33         {0, 0, 0, 1024, 0},
34         {0, 0, 0, 1024, 0},
35         {0, 0, 0, 1024, 0},
36         {0, 0, 0, 1024, 0},
37         {0, 0, 0, 1024, 0},
38         {0, 0, 0, 1024, 0},
39         {0, 0, 0, 1024, 0},
40     }
41 };
42 
43 static struct QosPolicyDatas g_systemServerPolicy = {
44     .policyType = QOS_POLICY_SYSTEM_SERVER,
45     .policyFlag = QOS_ALL_FLAG,
46     .policys = {
47         {0, 0, 0, 1024, 0},
48         {10, 0, 0, 1024, 0},
49         {5, 0, 0, 1024, 0},
50         {0, 0, 0, 1024, 0},
51         {0, 0, 0, 1024, 0},
52         {0, 0, 0, 1024, 0},
53         {0, 0, 0, 1024, 0},
54     }
55 };
56 
57 static struct QosPolicyDatas g_foregroundPolicy = {
58     .policyType = QOS_POLICY_FRONT,
59     .policyFlag = QOS_ALL_FLAG,
60     .policys = {
61         {0, 0, 0, 1024, 0},
62         {10, 0, 0, 1024, 0},
63         {5, 0, 0, 1024, 0},
64         {0, 0, 0, 1024, 0},
65         {0, 0, 0, 1024, 0},
66         {0, 0, 0, 1024, 0},
67         {0, 0, 0, 1024, 0},
68     }
69 };
70 
71 static struct QosPolicyDatas g_backgroundPolicy = {
72     .policyType = QOS_POLICY_BACK,
73     .policyFlag = QOS_ALL_FLAG & ~QOS_RT_FLAG,
74     .policys = {
75         {0, 0, 0, 1024, 0},
76         {15, 0, 0, 1024, 0},
77         {10, 0, 0, 1024, 0},
78         {5, 0, 0, 1024, 0},
79         {0, 0, 0, 1024, 0},
80         {0, 0, 0, 1024, 0},
81         {0, 0, 0, 1024, 0},
82     }
83 };
84 
GetInstance()85 QosController& QosController::GetInstance()
86 {
87     static QosController instance;
88     if (!instance.policyStatus_) {
89         int ret = instance.SetPolicy();
90         if (ret == 0) {
91             instance.policyStatus_ = true;
92             RME_LOGI("set qos policy success");
93         }
94     }
95     return instance;
96 }
97 
SetPolicy()98 int QosController::SetPolicy()
99 {
100     int ret;
101 
102     ret = QosPolicy(&g_defaultPolicy);
103     if (ret) {
104         RME_LOGE("set g_defaultPolicy failed");
105         return ret;
106     }
107 
108     ret = QosPolicy(&g_foregroundPolicy);
109     if (ret) {
110         RME_LOGE("set g_foregroundPolicy failed");
111         return ret;
112     }
113 
114     ret = QosPolicy(&g_backgroundPolicy);
115     if (ret) {
116         RME_LOGE("set g_backgroundPolicy failed");
117         return ret;
118     }
119 
120     ret = QosPolicy(&g_systemServerPolicy);
121     if (ret) {
122         RME_LOGE("set g_systemServerPolicy failed");
123     }
124     return ret;
125 }
126 
SetThreadQosForOtherThread(enum QosLevel level,int tid)127 int QosController::SetThreadQosForOtherThread(enum QosLevel level, int tid)
128 {
129     int qos = static_cast<int>(level);
130     if (level < QosLevel::qos_background || level > QosLevel::qos_user_interactive) {
131         RME_LOGE("invalid qos level %{public}d", qos);
132         return ERROR_NUM;
133     }
134     int ret = QosApplyForThread(qos, tid);
135     if (ret == 0) {
136         RME_LOGD("qoslevel %{public}d apply for tid %{public}d success", qos, tid);
137     } else {
138         RME_LOGE("qoslevel %{public}d apply for tid %{public}d failure", qos, tid);
139     }
140 
141     return ret;
142 }
143 
ResetThreadQosForOtherThread(int tid)144 int QosController::ResetThreadQosForOtherThread(int tid)
145 {
146     int ret = QosLeaveForThread(tid);
147     if (ret == 0) {
148         RME_LOGD("qoslevel reset for tid %{public}d success", tid);
149     } else {
150         RME_LOGE("qoslevel reset for tid %{public}d failure", tid);
151     }
152 
153     return ret;
154 }
155 
SetThreadQos(enum QosLevel level)156 int SetThreadQos(enum QosLevel level)
157 {
158     int tid = gettid();
159     return QosController::GetInstance().SetThreadQosForOtherThread(level, tid);
160 }
161 
SetQosForOtherThread(enum QosLevel level,int tid)162 int SetQosForOtherThread(enum QosLevel level, int tid)
163 {
164     return QosController::GetInstance().SetThreadQosForOtherThread(level, tid);
165 }
166 
ResetThreadQos()167 int ResetThreadQos()
168 {
169     int tid = gettid();
170     return QosController::GetInstance().ResetThreadQosForOtherThread(tid);
171 }
172 
ResetQosForOtherThread(int tid)173 int ResetQosForOtherThread(int tid)
174 {
175     return QosController::GetInstance().ResetThreadQosForOtherThread(tid);
176 }
177 
178 } // namespace QOS
179 } // namespace OHOS
180