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