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