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