1 /*
2 * Copyright (c) 2022-2024 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 <mutex>
22 #include <string>
23 #include <vector>
24 #include <unordered_map>
25 #include <unordered_set>
26
27 #include "ffrt.h"
28 #include "socperf_log.h"
29 #include "socperf_action_type.h"
30
31 namespace OHOS {
32 namespace SOCPERF {
33 enum EventType {
34 EVENT_INVALID = -1,
35 EVENT_OFF,
36 EVENT_ON
37 };
38
39 enum InterActionStatus {
40 BOOST_STATUS = 0,
41 BOOST_END_STATUS,
42 WEAK_INTERACTION_STATUS
43 };
44
45 const std::string SOCPERF_RESOURCE_CONFIG_XML = "etc/soc_perf/socperf_resource_config.xml";
46 const std::string SOCPERF_BOOST_CONFIG_XML = "etc/soc_perf/socperf_boost_config.xml";
47 const std::string SOCPERF_BOOST_CONFIG_XML_EXT = "etc/soc_perf/socperf_boost_config_ext.xml";
48 const std::string CAMERA_AWARE_CONFIG_XML = "etc/camera/cas/camera_aware_config.xml";
49 const int64_t MAX_INT_VALUE = 0x7FFFFFFFFFFFFFFF;
50 const int64_t MIN_INT_VALUE = 0x8000000000000000;
51 const int32_t MAX_INT32_VALUE = 0x7FFFFFFF;
52 const int32_t INVALID_VALUE = INT_MIN;
53 const int32_t RESET_VALUE = -1;
54 const int32_t MIN_RESOURCE_ID = 1000;
55 const int32_t MAX_RESOURCE_ID = 5999;
56 const int32_t RES_ID_ADDITION = 10000;
57 const int32_t RES_ID_AND_VALUE_PAIR = 2;
58 const int32_t RES_ID_NUMS_PER_TYPE = 1000;
59 const int32_t RES_ID_NUMS_PER_TYPE_EXT = 10000;
60 const int32_t WRITE_NODE = 0;
61 const int32_t REPORT_TO_PERFSO = 1;
62 const int32_t PERF_OPEN_TRACE = 1;
63 const int32_t INVALID_THERMAL_CMD_ID = -1;
64 const int32_t INVALID_DURATION = -1;
65 const int32_t INVALID_THERMAL_LVL = -1;
66 const int32_t DEFAULT_THERMAL_LVL = 0;
67 const int32_t RES_MODE_AND_ID_PAIR = 2;
68 const int32_t MAX_RES_MODE_LEN = 64;
69 const int32_t MAX_FREQUE_NODE = 1;
70 const int32_t NODE_DEFAULT_VALUE = -1;
71 const int32_t TYPE_TRACE_DEBUG = 3;
72 const std::string DEFAULT_CONFIG_MODE = "default";
73
74 class ResourceNode {
75 public:
76 int32_t id;
77 std::string name;
78 int64_t def;
79 std::unordered_set<int64_t> available;
80 int32_t persistMode;
81 bool isGov;
82 bool isMaxValue;
83 bool trace = false;
84 public:
ResourceNode(int32_t id,const std::string & name,int32_t persistMode,bool isGov,bool isMaxValue)85 ResourceNode(int32_t id, const std::string& name, int32_t persistMode, bool isGov, bool isMaxValue) : id(id),
86 name(name), def(INVALID_VALUE), persistMode(persistMode), isGov(isGov), isMaxValue(isMaxValue) {}
~ResourceNode()87 virtual ~ResourceNode() {};
PrintString()88 virtual void PrintString() {};
89 };
90
91 class ResNode : public ResourceNode {
92 public:
93 std::string path;
94 int32_t pair;
95
96 public:
ResNode(int32_t resId,const std::string & resName,int32_t resMode,int32_t resPair,int32_t resPersistMode)97 ResNode(int32_t resId, const std::string& resName, int32_t resMode, int32_t resPair, int32_t resPersistMode)
98 : ResourceNode(resId, resName, resPersistMode, false, resMode == MAX_FREQUE_NODE)
99 {
100 pair = resPair;
101 }
~ResNode()102 ~ResNode() {}
103 };
104
105 class GovResNode : public ResourceNode {
106 public:
107 std::vector<std::string> paths;
108 std::mutex levelToStrMutex_;
109 std::unordered_map<int64_t, std::vector<std::string>> levelToStr;
110
111 public:
GovResNode(int32_t govResId,const std::string & govResName,int32_t govPersistMode)112 GovResNode(int32_t govResId, const std::string& govResName, int32_t govPersistMode)
113 : ResourceNode(govResId, govResName, govPersistMode, true, false) {}
~GovResNode()114 ~GovResNode() {}
115 };
116
117 class SceneItem {
118 public:
119 std::string name;
120 int32_t req;
121
122 public:
SceneItem(const std::string & name,int32_t req)123 SceneItem(const std::string& name, int32_t req) : name(name), req(req) {}
~SceneItem()124 ~SceneItem() {}
125 };
126
127 class SceneResNode {
128 public:
129 std::string name;
130 int32_t persistMode;
131 std::vector<std::shared_ptr<SceneItem>> items;
132
133 public:
SceneResNode(const std::string & name,int32_t persistMode)134 SceneResNode(const std::string& name, int32_t persistMode) : name(name), persistMode(persistMode) {}
~SceneResNode()135 ~SceneResNode() {}
136 };
137
138 class ModeMap {
139 public:
140 std::string mode;
141 int32_t cmdId;
142
143 public:
ModeMap(const std::string & mode,int32_t cmdId)144 ModeMap(const std::string& mode, int32_t cmdId) : mode(mode), cmdId(cmdId) {}
~ModeMap()145 ~ModeMap() {}
146 };
147
148 class Action {
149 public:
150 int32_t thermalCmdId_ = INVALID_THERMAL_CMD_ID;
151 int32_t thermalLvl_ = INVALID_THERMAL_LVL;
152 int32_t duration = INVALID_DURATION;
153 std::vector<int64_t> variable;
154
155 public:
Action()156 Action() {}
~Action()157 ~Action() {}
158 };
159
160 class Actions {
161 public:
162 int32_t id;
163 std::string name;
164 std::list<std::shared_ptr<Action>> actionList;
165 std::vector<std::shared_ptr<ModeMap>> modeMap;
166 bool isLongTimePerf = false;
167 bool interaction = true;
168
169 public:
Actions(int32_t cmdId,const std::string & cmdName)170 Actions(int32_t cmdId, const std::string& cmdName)
171 {
172 id = cmdId;
173 name = cmdName;
174 }
~Actions()175 ~Actions() {}
176 };
177
178 class ResAction {
179 public:
180 int64_t value;
181 int32_t duration;
182 int32_t type;
183 int32_t onOff;
184 int32_t cmdId;
185 int64_t endTime;
186 bool interaction = true;
187
188 public:
ResAction(int64_t resActionValue,int32_t resActionDuration,int32_t resActionType,int32_t resActionOnOff,int32_t resActionCmdId,int64_t resActionEndTime)189 ResAction(int64_t resActionValue, int32_t resActionDuration, int32_t resActionType,
190 int32_t resActionOnOff, int32_t resActionCmdId, int64_t resActionEndTime)
191 {
192 value = resActionValue;
193 duration = resActionDuration;
194 type = resActionType;
195 onOff = resActionOnOff;
196 cmdId = resActionCmdId;
197 endTime = resActionEndTime;
198 if (type != ACTION_TYPE_PERF) {
199 interaction = false;
200 }
201 }
~ResAction()202 ~ResAction() {}
203
TotalSame(std::shared_ptr<ResAction> resAction)204 bool TotalSame(std::shared_ptr<ResAction> resAction)
205 {
206 if (value == resAction->value
207 && duration == resAction->duration
208 && type == resAction->type
209 && onOff == resAction->onOff
210 && cmdId == resAction->cmdId) {
211 return true;
212 }
213 return false;
214 }
215
PartSame(std::shared_ptr<ResAction> resAction)216 bool PartSame(std::shared_ptr<ResAction> resAction)
217 {
218 if (value == resAction->value
219 && duration == resAction->duration
220 && type == resAction->type
221 && cmdId == resAction->cmdId) {
222 return true;
223 }
224 return false;
225 }
226 };
227
228 class ResActionItem {
229 public:
ResActionItem(int32_t id)230 ResActionItem(int32_t id)
231 {
232 resId = id;
233 }
234
235 ~ResActionItem() = default;
236
237 int32_t resId;
238 std::shared_ptr<ResAction> resAction = nullptr;
239 std::shared_ptr<ResActionItem> next = nullptr;
240 };
241
242 class ResStatus {
243 public:
244 std::vector<std::list<std::shared_ptr<ResAction>>> resActionList;
245 std::vector<int64_t> candidatesValue;
246 std::vector<int64_t> candidatesEndTime;
247 int64_t candidate;
248 int64_t currentValue;
249 int64_t previousValue;
250 int64_t currentEndTime;
251 int64_t previousEndTime;
252
253 public:
ResStatus()254 explicit ResStatus()
255 {
256 resActionList = std::vector<std::list<std::shared_ptr<ResAction>>>(ACTION_TYPE_MAX);
257 candidatesValue = std::vector<int64_t>(ACTION_TYPE_MAX);
258 candidatesEndTime = std::vector<int64_t>(ACTION_TYPE_MAX);
259 candidatesValue[ACTION_TYPE_PERF] = INVALID_VALUE;
260 candidatesValue[ACTION_TYPE_POWER] = INVALID_VALUE;
261 candidatesValue[ACTION_TYPE_THERMAL] = INVALID_VALUE;
262 candidatesValue[ACTION_TYPE_PERFLVL] = INVALID_VALUE;
263 candidatesEndTime[ACTION_TYPE_PERF] = MAX_INT_VALUE;
264 candidatesEndTime[ACTION_TYPE_POWER] = MAX_INT_VALUE;
265 candidatesEndTime[ACTION_TYPE_THERMAL] = MAX_INT_VALUE;
266 candidatesEndTime[ACTION_TYPE_PERFLVL] = MAX_INT_VALUE;
267 candidate = NODE_DEFAULT_VALUE;
268 currentValue = NODE_DEFAULT_VALUE;
269 previousValue = NODE_DEFAULT_VALUE;
270 currentEndTime = MAX_INT_VALUE;
271 previousEndTime = MAX_INT_VALUE;
272 }
~ResStatus()273 ~ResStatus() {}
274 };
275
276 class InterAction {
277 public:
278 int32_t cmdId;
279 int32_t actionType;
280 int64_t delayTime;
281 int32_t status;
282 ffrt::task_handle timerTask;
283
284 public:
InterAction(int32_t cmdid,int32_t actiontype,int64_t delaytime)285 InterAction(int32_t cmdid, int32_t actiontype, int64_t delaytime)
286 {
287 cmdId = cmdid;
288 actionType = actiontype;
289 delayTime = delaytime;
290 status = 0;
291 timerTask = nullptr;
292 }
~InterAction()293 ~InterAction() {}
294 };
295
Max(int64_t num1,int64_t num2)296 static inline int64_t Max(int64_t num1, int64_t num2)
297 {
298 if (num1 >= num2) {
299 return num1;
300 }
301 return num2;
302 }
303
Max(int64_t num1,int64_t num2,int64_t num3)304 static inline int64_t Max(int64_t num1, int64_t num2, int64_t num3)
305 {
306 return Max(Max(num1, num2), num3);
307 }
308
Min(int64_t num1,int64_t num2)309 static inline int64_t Min(int64_t num1, int64_t num2)
310 {
311 if (num1 <= num2) {
312 return num1;
313 }
314 return num2;
315 }
316
Min(int64_t num1,int64_t num2,int64_t num3)317 static inline int64_t Min(int64_t num1, int64_t num2, int64_t num3)
318 {
319 return Min(Min(num1, num2), num3);
320 }
321
IsNumber(const std::string & str)322 static inline bool IsNumber(const std::string& str)
323 {
324 for (int32_t i = 0; i < (int32_t)str.size(); i++) {
325 if (i == 0 && str.at(i) == '-') {
326 continue;
327 }
328 if (str.at(i) < '0' || str.at(i) > '9') {
329 return false;
330 }
331 }
332 return true;
333 }
334
IsValidRangeResId(int32_t id)335 static inline bool IsValidRangeResId(int32_t id)
336 {
337 if (id < MIN_RESOURCE_ID || id > MAX_RESOURCE_ID) {
338 return false;
339 }
340 return true;
341 }
342
IsValidPersistMode(int32_t persistMode)343 static inline bool IsValidPersistMode(int32_t persistMode)
344 {
345 if (persistMode != WRITE_NODE && persistMode != REPORT_TO_PERFSO) {
346 return false;
347 }
348 return true;
349 }
350
SplitEx(const std::string & str,const std::string & pattern)351 static std::vector<std::string> SplitEx(const std::string& str, const std::string& pattern)
352 {
353 int32_t position;
354 std::vector<std::string> result;
355 std::string tempStr = str;
356 tempStr += pattern;
357 int32_t length = (int32_t)tempStr.size();
358 for (int32_t i = 0; i < length; i++) {
359 position = (int32_t)tempStr.find(pattern, i);
360 if (position < length) {
361 std::string tmp = tempStr.substr(i, position - i);
362 result.push_back(tmp);
363 i = position + (int32_t)pattern.size() - 1;
364 }
365 }
366 return result;
367 }
368
Split(const std::string & str,const std::string & pattern)369 static inline std::vector<std::string> Split(const std::string& str, const std::string& pattern)
370 {
371 return SplitEx(str, pattern);
372 }
373
374 } // namespace SOCPERF
375 } // namespace OHOS
376
377 #endif // SOC_PERF_SERVICES_CORE_INCLUDE_COMMON_H
378