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