• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 SOC_PERF_COMMON_H
17 #define SOC_PERF_COMMON_H
18 
19 #include <string>
20 #include <vector>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include "hilog/log.h"
24 
25 namespace OHOS {
26 namespace SOCPERF {
27 #define LOG_TAG_SOC_PERF "socperf"
28 #define LOG_TAG_DOMAIN_ID_SOC_PERF 0xD001700
29 
30 static constexpr OHOS::HiviewDFX::HiLogLabel SOC_PERF_LOG_LABEL = {
31     LOG_CORE,
32     LOG_TAG_DOMAIN_ID_SOC_PERF,
33     LOG_TAG_SOC_PERF
34 };
35 
36 #define SOC_PERF_LOGF(...) (void)OHOS::HiviewDFX::HiLog::Fatal(SOC_PERF_LOG_LABEL, __VA_ARGS__)
37 #define SOC_PERF_LOGE(...) (void)OHOS::HiviewDFX::HiLog::Error(SOC_PERF_LOG_LABEL, __VA_ARGS__)
38 #define SOC_PERF_LOGW(...) (void)OHOS::HiviewDFX::HiLog::Warn(SOC_PERF_LOG_LABEL, __VA_ARGS__)
39 #define SOC_PERF_LOGI(...) (void)OHOS::HiviewDFX::HiLog::Info(SOC_PERF_LOG_LABEL, __VA_ARGS__)
40 #define SOC_PERF_LOGD(...) (void)OHOS::HiviewDFX::HiLog::Debug(SOC_PERF_LOG_LABEL, __VA_ARGS__)
41 
42 enum EventType {
43     EVENT_INVALID = -1,
44     EVENT_OFF,
45     EVENT_ON
46 };
47 
48 enum ActionType {
49     ACTION_TYPE_PERF,
50     ACTION_TYPE_POWER,
51     ACTION_TYPE_THERMAL,
52     ACTION_TYPE_MAX
53 };
54 
55 namespace {
56     const int INNER_EVENT_ID_INIT_RES_NODE_INFO       = 0;
57     const int INNER_EVENT_ID_INIT_GOV_RES_NODE_INFO   = 1;
58     const int INNER_EVENT_ID_DO_FREQ_ACTION           = 2;
59     const int INNER_EVENT_ID_DO_FREQ_ACTION_DELAYED   = 3;
60     const int INNER_EVENT_ID_POWER_LIMIT_BOOST_FREQ   = 4;
61     const int INNER_EVENT_ID_THERMAL_LIMIT_BOOST_FREQ = 5;
62 }
63 
64 namespace {
65     const std::string SOCPERF_RESOURCE_CONFIG_XML = "/system/etc/socperf/socperf_resource_config.xml";
66     const std::string SOCPERF_BOOST_CONFIG_XML    = "/system/etc/socperf/socperf_boost_config.xml";
67     const std::string SOCPERF_POWER_CONFIG_XML    = "/system/etc/socperf/socperf_power_config.xml";
68     const std::string SOCPERF_THERMAL_CONFIG_XML  = "/system/etc/socperf/socperf_thermal_config.xml";
69     const int MAX_INT_VALUE                       = 0x7FFFFFFF;
70     const int MIN_INT_VALUE                       = 0x80000000;
71     const int INVALID_VALUE                       = -1;
72     const int MAX_HANDLER_THREADS                 = 5;
73     const int MIN_RESOURCE_ID                     = 1000;
74     const int MAX_RESOURCE_ID                     = 5999;
75     const int RES_ID_AND_VALUE_PAIR               = 2;
76     const int RES_ID_NUMS_PER_TYPE                = 1000;
77 }
78 
79 class ResNode {
80 public:
81     int id;
82     std::string name;
83     int def;
84     std::string path;
85     int mode;
86     int pair;
87     std::unordered_set<int> available;
88 
89 public:
ResNode(int resId,std::string resName,int resMode,int resPair)90     ResNode(int resId, std::string resName, int resMode, int resPair)
91     {
92         id = resId;
93         name = resName;
94         mode = resMode;
95         pair = resPair;
96         def = INVALID_VALUE;
97     }
~ResNode()98     ~ResNode() {}
99 
PrintString()100     void PrintString()
101     {
102         SOC_PERF_LOGI("resNode-> id: [%{public}d], name: [%{public}s]", id, name.c_str());
103         SOC_PERF_LOGI("          path: [%{public}s]", path.c_str());
104         SOC_PERF_LOGI("          def: [%{public}d], mode: [%{public}d], pair: [%{public}d]", def, mode, pair);
105         std::string str;
106         str.append("available(").append(std::to_string((int)available.size())).append("): ");
107         str.append("[");
108         for (auto validValue : available) {
109             str.append(std::to_string(validValue)).append(",");
110         }
111         if (!available.empty()) {
112             str.pop_back();
113         }
114         str.append("]");
115         SOC_PERF_LOGI("          %{public}s", str.c_str());
116     }
117 };
118 
119 class GovResNode {
120 public:
121     int id;
122     std::string name;
123     int def;
124     std::vector<std::string> paths;
125     std::unordered_set<int> available;
126     std::unordered_map<int, std::vector<std::string>> levelToStr;
127 
128 public:
GovResNode(int govResId,std::string govResName)129     GovResNode(int govResId, std::string govResName)
130     {
131         id = govResId;
132         name = govResName;
133         def = INVALID_VALUE;
134     }
~GovResNode()135     ~GovResNode() {}
136 
PrintString()137     void PrintString()
138     {
139         SOC_PERF_LOGI("govResNode-> id: [%{public}d], name: [%{public}s]", id, name.c_str());
140         SOC_PERF_LOGI("             def: [%{public}d]", def);
141         for (auto path : paths) {
142             SOC_PERF_LOGI("             path: [%{public}s]", path.c_str());
143         }
144         std::string str;
145         str.append("available(").append(std::to_string((int)available.size())).append("): ");
146         str.append("[");
147         for (auto validValue : available) {
148             str.append(std::to_string(validValue)).append(",");
149         }
150         if (!available.empty()) {
151             str.pop_back();
152         }
153         str.append("]");
154         SOC_PERF_LOGI("             %{public}s", str.c_str());
155         for (auto iter = levelToStr.begin(); iter != levelToStr.end(); ++iter) {
156             std::string str2;
157             int level = iter->first;
158             std::vector<std::string> result = iter->second;
159             for (int i = 0; i < (int)result.size(); i++) {
160                 str2.append(result[i]).append(",");
161             }
162             if (!result.empty()) {
163                 str2.pop_back();
164             }
165             SOC_PERF_LOGI("             %{public}d: [%{public}s]", level, str2.c_str());
166         }
167     }
168 };
169 
170 class Action {
171 public:
172     int id;
173     std::string name;
174     int duration;
175     std::vector<int> variable;
176 
177 public:
Action(int cmdId,std::string cmdName)178     Action(int cmdId, std::string cmdName)
179     {
180         id = cmdId;
181         name = cmdName;
182         duration = INVALID_VALUE;
183     }
~Action()184     ~Action() {}
185 
PrintString()186     void PrintString()
187     {
188         SOC_PERF_LOGI("action-> id: [%{public}d], name: [%{public}s]", id, name.c_str());
189         SOC_PERF_LOGI("         duration: [%{public}d]", duration);
190         std::string str;
191         str.append("variable(").append(std::to_string((int)variable.size())).append("): [");
192         for (int i = 0; i < (int)variable.size(); i++) {
193             str.append(std::to_string(variable[i])).append(",");
194         }
195         if (!variable.empty()) {
196             str.pop_back();
197         }
198         str.append("]");
199         SOC_PERF_LOGI("         %{public}s", str.c_str());
200     }
201 };
202 
203 class ResAction {
204 public:
205     int value;
206     int duration;
207     int type;
208     int onOff;
209 
210 public:
ResAction(int resActionValue,int resActionDuration,int resActionType,int resActionOnOff)211     ResAction(int resActionValue, int resActionDuration, int resActionType, int resActionOnOff)
212     {
213         value = resActionValue;
214         duration = resActionDuration;
215         type = resActionType;
216         onOff = resActionOnOff;
217     }
~ResAction()218     ~ResAction() {}
219 
TotalSame(std::shared_ptr<ResAction> resAction)220     bool TotalSame(std::shared_ptr<ResAction> resAction)
221     {
222         if (value == resAction->value
223             && duration == resAction->duration
224             && type == resAction->type
225             && onOff == resAction->onOff) {
226             return true;
227         }
228         return false;
229     }
230 
PartSame(std::shared_ptr<ResAction> resAction)231     bool PartSame(std::shared_ptr<ResAction> resAction)
232     {
233         if (value == resAction->value
234             && duration == resAction->duration
235             && type == resAction->type) {
236             return true;
237         }
238         return false;
239     }
240 };
241 
242 class ResStatus {
243 public:
244     std::vector<std::list<std::shared_ptr<ResAction>>> resActionList;
245     std::vector<int> candidates;
246     int candidate;
247     int current;
248 
249 public:
ResStatus(int val)250     ResStatus(int val)
251     {
252         resActionList = std::vector<std::list<std::shared_ptr<ResAction>>>(ACTION_TYPE_MAX);
253         candidates = std::vector<int>(ACTION_TYPE_MAX);
254         candidates[ACTION_TYPE_PERF] = INVALID_VALUE;
255         candidates[ACTION_TYPE_POWER] = INVALID_VALUE;
256         candidates[ACTION_TYPE_THERMAL] = INVALID_VALUE;
257         candidate = val;
258         current = val;
259     }
~ResStatus()260     ~ResStatus() {}
261 
ToString()262     std::string ToString()
263     {
264         std::string str;
265         str.append("perf:[");
266         for (auto iter = resActionList[ACTION_TYPE_PERF].begin();
267             iter != resActionList[ACTION_TYPE_PERF].end(); ++iter) {
268             str.append(std::to_string((*iter)->value)).append(",");
269         }
270         if (!resActionList[ACTION_TYPE_PERF].empty()) {
271             str.pop_back();
272         }
273         str.append("]power:[");
274         for (auto iter = resActionList[ACTION_TYPE_POWER].begin();
275             iter != resActionList[ACTION_TYPE_POWER].end(); ++iter) {
276             str.append(std::to_string((*iter)->value)).append(",");
277         }
278         if (!resActionList[ACTION_TYPE_POWER].empty()) {
279             str.pop_back();
280         }
281         str.append("]thermal:[");
282         for (auto iter = resActionList[ACTION_TYPE_THERMAL].begin();
283             iter != resActionList[ACTION_TYPE_THERMAL].end(); ++iter) {
284             str.append(std::to_string((*iter)->value)).append(",");
285         }
286         if (!resActionList[ACTION_TYPE_THERMAL].empty()) {
287             str.pop_back();
288         }
289         str.append("]candidates[");
290         for (auto iter = candidates.begin(); iter != candidates.end(); ++iter) {
291             str.append(std::to_string(*iter)).append(",");
292         }
293         str.pop_back();
294         str.append("]candidate[").append(std::to_string(candidate));
295         str.append("]current[").append(std::to_string(current)).append("]");
296         return str;
297     }
298 };
299 
Max(int num1,int num2)300 static inline int Max(int num1, int num2)
301 {
302     if (num1 >= num2) {
303         return num1;
304     }
305     return num2;
306 }
307 
Max(int num1,int num2,int num3)308 static inline int Max(int num1, int num2, int num3)
309 {
310     return Max(Max(num1, num2), num3);
311 }
312 
Min(int num1,int num2)313 static inline int Min(int num1, int num2)
314 {
315     if (num1 <= num2) {
316         return num1;
317     }
318     return num2;
319 }
320 
Min(int num1,int num2,int num3)321 static inline int Min(int num1, int num2, int num3)
322 {
323     return Min(Min(num1, num2), num3);
324 }
325 
IsNumber(std::string str)326 static inline bool IsNumber(std::string str)
327 {
328     for (int i = 0; i < (int)str.size(); i++) {
329         if (i == 0 && str.at(i) == '-') {
330             continue;
331         }
332         if (str.at(i) < '0' || str.at(i) > '9') {
333             return false;
334         }
335     }
336     return true;
337 }
338 
IsValidResId(int id)339 static inline bool IsValidResId(int id)
340 {
341     if (id < MIN_RESOURCE_ID || id > MAX_RESOURCE_ID) {
342         return false;
343     }
344     return true;
345 }
346 
Split(std::string str,std::string pattern)347 static inline std::vector<std::string> Split(std::string str, std::string pattern)
348 {
349     int position;
350     std::vector<std::string> result;
351     str += pattern;
352     int length = (int)str.size();
353     for (int i = 0; i < length; i++) {
354         position = (int)str.find(pattern, i);
355         if (position < length) {
356             std::string tmp = str.substr(i, position - i);
357             result.push_back(tmp);
358             i = position + (int)pattern.size() - 1;
359         }
360     }
361     return result;
362 }
363 } // namespace SOCPERF
364 } // namespace OHOS
365 
366 #endif // SOC_PERF_COMMON_H
367