• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "socperf.h"
17 
18 namespace OHOS {
19 namespace SOCPERF {
SocPerf()20 SocPerf::SocPerf()
21 {
22 }
23 
~SocPerf()24 SocPerf::~SocPerf()
25 {
26 }
27 
Init()28 bool SocPerf::Init()
29 {
30     if (!LoadConfigXmlFile(SOCPERF_RESOURCE_CONFIG_XML)) {
31         SOC_PERF_LOGE("Failed to load %{public}s", SOCPERF_RESOURCE_CONFIG_XML.c_str());
32         return false;
33     }
34 
35     if (!LoadConfigXmlFile(SOCPERF_BOOST_CONFIG_XML)) {
36         SOC_PERF_LOGE("Failed to load %{public}s", SOCPERF_BOOST_CONFIG_XML.c_str());
37         return false;
38     }
39 
40     if (!LoadConfigXmlFile(SOCPERF_POWER_CONFIG_XML)) {
41         SOC_PERF_LOGE("Failed to load %{public}s", SOCPERF_POWER_CONFIG_XML.c_str());
42         return false;
43     }
44 
45     if (!LoadConfigXmlFile(SOCPERF_THERMAL_CONFIG_XML)) {
46         SOC_PERF_LOGE("Failed to load %{public}s", SOCPERF_THERMAL_CONFIG_XML.c_str());
47         return false;
48     }
49 
50     PrintCachedInfo();
51 
52     if (!CreateHandlers()) {
53         SOC_PERF_LOGE("Failed to create handler threads");
54         return false;
55     }
56 
57     InitHandlerThreads();
58 
59     resNodeInfo.clear();
60     govResNodeInfo.clear();
61     resStrToIdInfo.clear();
62 
63     enabled = true;
64     return true;
65 }
66 
PerfRequest(int cmdId,const std::string & msg)67 void SocPerf::PerfRequest(int cmdId, const std::string& msg)
68 {
69     if (!enabled) {
70         SOC_PERF_LOGE("SocPerf disabled!");
71         return;
72     }
73     if (perfActionInfo.find(cmdId) == perfActionInfo.end()
74         || perfActionInfo[cmdId]->duration == 0) {
75         SOC_PERF_LOGE("Invalid PerfRequest cmdId[%{public}d]", cmdId);
76         return;
77     }
78     SOC_PERF_LOGI("PerfRequest cmdId[%{public}d]msg[%{public}s]", cmdId, msg.c_str());
79     DoFreqAction(perfActionInfo[cmdId], EVENT_INVALID, ACTION_TYPE_PERF);
80 }
81 
PerfRequestEx(int cmdId,bool onOffTag,const std::string & msg)82 void SocPerf::PerfRequestEx(int cmdId, bool onOffTag, const std::string& msg)
83 {
84     if (!enabled) {
85         SOC_PERF_LOGE("SocPerf disabled!");
86         return;
87     }
88     if (perfActionInfo.find(cmdId) == perfActionInfo.end()) {
89         SOC_PERF_LOGE("Invalid PerfRequestEx cmdId[%{public}d]", cmdId);
90         return;
91     }
92     SOC_PERF_LOGI("PerfRequestEx cmdId[%{public}d]onOffTag[%{public}d]msg[%{public}s]",
93         cmdId, onOffTag, msg.c_str());
94     DoFreqAction(perfActionInfo[cmdId], onOffTag ? EVENT_ON : EVENT_OFF, ACTION_TYPE_PERF);
95 }
96 
PowerRequest(int cmdId,const std::string & msg)97 void SocPerf::PowerRequest(int cmdId, const std::string& msg)
98 {
99     if (!enabled) {
100         SOC_PERF_LOGE("SocPerf disabled!");
101         return;
102     }
103     if (powerActionInfo.find(cmdId) == powerActionInfo.end()
104         || powerActionInfo[cmdId]->duration == 0) {
105         SOC_PERF_LOGE("Invalid PowerRequest cmdId[%{public}d]", cmdId);
106         return;
107     }
108     SOC_PERF_LOGI("PowerRequest cmdId[%{public}d]msg[%{public}s]", cmdId, msg.c_str());
109     DoFreqAction(powerActionInfo[cmdId], EVENT_INVALID, ACTION_TYPE_POWER);
110 }
111 
PowerRequestEx(int cmdId,bool onOffTag,const std::string & msg)112 void SocPerf::PowerRequestEx(int cmdId, bool onOffTag, const std::string& msg)
113 {
114     if (!enabled) {
115         SOC_PERF_LOGE("SocPerf disabled!");
116         return;
117     }
118     if (powerActionInfo.find(cmdId) == powerActionInfo.end()) {
119         SOC_PERF_LOGE("Invalid PowerRequestEx cmdId[%{public}d]", cmdId);
120         return;
121     }
122     SOC_PERF_LOGI("PowerRequestEx cmdId[%{public}d]onOffTag[%{public}d]msg[%{public}s]",
123         cmdId, onOffTag, msg.c_str());
124     DoFreqAction(powerActionInfo[cmdId], onOffTag ? EVENT_ON : EVENT_OFF, ACTION_TYPE_POWER);
125 }
126 
PowerLimitBoost(bool onOffTag,const std::string & msg)127 void SocPerf::PowerLimitBoost(bool onOffTag, const std::string& msg)
128 {
129     if (!enabled) {
130         SOC_PERF_LOGE("SocPerf disabled!");
131         return;
132     }
133     SOC_PERF_LOGI("PowerLimitBoost onOffTag[%{public}d]msg[%{public}s]", onOffTag, msg.c_str());
134     for (auto handler : handlers) {
135         auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_POWER_LIMIT_BOOST_FREQ, onOffTag ? 1 : 0);
136         handler->SendEvent(event);
137     }
138 }
139 
ThermalRequest(int cmdId,const std::string & msg)140 void SocPerf::ThermalRequest(int cmdId, const std::string& msg)
141 {
142     if (!enabled) {
143         SOC_PERF_LOGE("SocPerf disabled!");
144         return;
145     }
146     if (thermalActionInfo.find(cmdId) == thermalActionInfo.end()
147         || thermalActionInfo[cmdId]->duration == 0) {
148         SOC_PERF_LOGE("Invalid ThermalRequest cmdId[%{public}d]", cmdId);
149         return;
150     }
151     SOC_PERF_LOGI("ThermalRequest cmdId[%{public}d]msg[%{public}s]", cmdId, msg.c_str());
152     DoFreqAction(thermalActionInfo[cmdId], EVENT_INVALID, ACTION_TYPE_THERMAL);
153 }
154 
ThermalRequestEx(int cmdId,bool onOffTag,const std::string & msg)155 void SocPerf::ThermalRequestEx(int cmdId, bool onOffTag, const std::string& msg)
156 {
157     if (!enabled) {
158         SOC_PERF_LOGE("SocPerf disabled!");
159         return;
160     }
161     if (thermalActionInfo.find(cmdId) == thermalActionInfo.end()) {
162         SOC_PERF_LOGE("Invalid ThermalRequestEx cmdId[%{public}d]", cmdId);
163         return;
164     }
165     SOC_PERF_LOGI("ThermalRequestEx cmdId[%{public}d]onOffTag[%{public}d]msg[%{public}s]",
166         cmdId, onOffTag, msg.c_str());
167     DoFreqAction(thermalActionInfo[cmdId], onOffTag ? EVENT_ON : EVENT_OFF, ACTION_TYPE_THERMAL);
168 }
169 
ThermalLimitBoost(bool onOffTag,const std::string & msg)170 void SocPerf::ThermalLimitBoost(bool onOffTag, const std::string& msg)
171 {
172     if (!enabled) {
173         SOC_PERF_LOGE("SocPerf disabled!");
174         return;
175     }
176     SOC_PERF_LOGI("ThermalLimitBoost onOffTag[%{public}d]msg[%{public}s]", onOffTag, msg.c_str());
177     for (auto handler : handlers) {
178         auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_THERMAL_LIMIT_BOOST_FREQ, onOffTag ? 1 : 0);
179         handler->SendEvent(event);
180     }
181 }
182 
DoFreqAction(std::shared_ptr<Action> action,int onOff,int actionType)183 void SocPerf::DoFreqAction(std::shared_ptr<Action> action, int onOff, int actionType)
184 {
185     for (int i = 0; i < (int)action->variable.size(); i += RES_ID_AND_VALUE_PAIR) {
186         auto resAction = std::make_shared<ResAction>(action->variable[i + 1], action->duration, actionType, onOff);
187         auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_DO_FREQ_ACTION, resAction, action->variable[i]);
188         handlers[action->variable[i] / RES_ID_NUMS_PER_TYPE - 1]->SendEvent(event);
189     }
190 }
191 
LoadConfigXmlFile(std::string configFile)192 bool SocPerf::LoadConfigXmlFile(std::string configFile)
193 {
194     xmlKeepBlanksDefault(0);
195     xmlDoc* file = xmlReadFile(configFile.c_str(), nullptr, 0);
196     if (!file) {
197         SOC_PERF_LOGE("Failed to open xml file");
198         return false;
199     }
200     xmlNode* rootNode = xmlDocGetRootElement(file);
201     if (!rootNode) {
202         SOC_PERF_LOGE("Failed to get xml file's RootNode");
203         xmlFreeDoc(file);
204         return false;
205     }
206     if (!xmlStrcmp(rootNode->name, reinterpret_cast<const xmlChar*>("Configs"))) {
207         if (configFile == SOCPERF_RESOURCE_CONFIG_XML) {
208             xmlNode* child = rootNode->children;
209             for (; child; child = child->next) {
210                 if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>("Resource"))) {
211                     if (!LoadResource(child, configFile)) {
212                         xmlFreeDoc(file);
213                         return false;
214                     }
215                 } else if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>("GovResource"))) {
216                     if (!LoadGovResource(child, configFile)) {
217                         xmlFreeDoc(file);
218                         return false;
219                     }
220                 }
221             }
222         } else {
223             if (!LoadCmd(rootNode, configFile)) {
224                 xmlFreeDoc(file);
225                 return false;
226             }
227         }
228     } else {
229         SOC_PERF_LOGE("Wrong format for xml file");
230         xmlFreeDoc(file);
231         return false;
232     }
233     xmlFreeDoc(file);
234     SOC_PERF_LOGI("Success to Load %{public}s", configFile.c_str());
235     return true;
236 }
237 
CreateHandlers()238 bool SocPerf::CreateHandlers()
239 {
240     handlers = std::vector<std::shared_ptr<SocPerfHandler>>(MAX_HANDLER_THREADS);
241     std::string threadName = "socperf_handler";
242     for (int i = 0; i < (int)handlers.size(); i++) {
243         auto runner = AppExecFwk::EventRunner::Create(threadName);
244         if (!runner) {
245             SOC_PERF_LOGE("Failed to Create EventRunner");
246             return false;
247         }
248         handlers[i] = std::make_shared<SocPerfHandler>(runner);
249         SOC_PERF_LOGI("Success to Create SocPerfHandler");
250     }
251     SOC_PERF_LOGI("Success to Create All Handler threads");
252     return true;
253 }
254 
InitHandlerThreads()255 void SocPerf::InitHandlerThreads()
256 {
257     for (auto iter = resNodeInfo.begin(); iter != resNodeInfo.end(); ++iter) {
258         std::shared_ptr<ResNode> resNode = iter->second;
259         auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_INIT_RES_NODE_INFO, resNode);
260         handlers[resNode->id / RES_ID_NUMS_PER_TYPE - 1]->SendEvent(event);
261     }
262     for (auto iter = govResNodeInfo.begin(); iter != govResNodeInfo.end(); ++iter) {
263         std::shared_ptr<GovResNode> govResNode = iter->second;
264         auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_INIT_GOV_RES_NODE_INFO, govResNode);
265         handlers[govResNode->id / RES_ID_NUMS_PER_TYPE - 1]->SendEvent(event);
266     }
267 }
268 
LoadResource(xmlNode * child,std::string configFile)269 bool SocPerf::LoadResource(xmlNode* child, std::string configFile)
270 {
271     xmlNode* grandson = child->children;
272     for (; grandson; grandson = grandson->next) {
273         if (!xmlStrcmp(grandson->name, reinterpret_cast<const xmlChar*>("res"))) {
274             char* id = reinterpret_cast<char*>(xmlGetProp(grandson, reinterpret_cast<const xmlChar*>("id")));
275             char* name = reinterpret_cast<char*>(xmlGetProp(grandson, reinterpret_cast<const xmlChar*>("name")));
276             char* pair = reinterpret_cast<char*>(xmlGetProp(grandson, reinterpret_cast<const xmlChar*>("pair")));
277             char* mode = reinterpret_cast<char*>(xmlGetProp(grandson, reinterpret_cast<const xmlChar*>("mode")));
278             if (!CheckResourceTag(id, name, pair, mode, configFile)) {
279                 return false;
280             }
281             xmlNode* greatGrandson = grandson->children;
282             std::shared_ptr<ResNode> resNode = std::make_shared<ResNode>(
283                 atoi(id), name, mode ? atoi(mode) : 0, pair ? atoi(pair) : INVALID_VALUE);
284             char *def = nullptr;
285             char *path  = nullptr;
286             char *node  = nullptr;
287             for (; greatGrandson; greatGrandson = greatGrandson->next) {
288                 if (!xmlStrcmp(greatGrandson->name, reinterpret_cast<const xmlChar*>("default"))) {
289                     def = reinterpret_cast<char*>(xmlNodeGetContent(greatGrandson));
290                 } else if (!xmlStrcmp(greatGrandson->name, reinterpret_cast<const xmlChar*>("path"))) {
291                     path = reinterpret_cast<char*>(xmlNodeGetContent(greatGrandson));
292                 } else if (!xmlStrcmp(greatGrandson->name, reinterpret_cast<const xmlChar*>("node"))) {
293                     node = reinterpret_cast<char*>(xmlNodeGetContent(greatGrandson));
294                 }
295             }
296             if (!CheckResourceTag(def, path, configFile)) {
297                 return false;
298             }
299             resNode->def = atoi(def);
300             resNode->path = path;
301             if (node && !LoadResourceAvailable(resNode, node)) {
302                 SOC_PERF_LOGE("Invalid resource node for %{public}s", configFile.c_str());
303                 return false;
304             }
305 
306             resStrToIdInfo.insert(std::pair<std::string, int>(resNode->name, resNode->id));
307             resNodeInfo.insert(std::pair<int, std::shared_ptr<ResNode>>(resNode->id, resNode));
308         }
309     }
310 
311     if (!CheckPairResIdValid() || !CheckResDefValid()) {
312         return false;
313     }
314 
315     return true;
316 }
317 
LoadGovResource(xmlNode * child,std::string configFile)318 bool SocPerf::LoadGovResource(xmlNode* child, std::string configFile)
319 {
320     xmlNode* grandson = child->children;
321     for (; grandson; grandson = grandson->next) {
322         if (!xmlStrcmp(grandson->name, reinterpret_cast<const xmlChar*>("res"))) {
323             char* id = reinterpret_cast<char*>(xmlGetProp(grandson, reinterpret_cast<const xmlChar*>("id")));
324             char* name = reinterpret_cast<char*>(xmlGetProp(grandson, reinterpret_cast<const xmlChar*>("name")));
325             if (!CheckGovResourceTag(id, name, configFile)) {
326                 return false;
327             }
328             xmlNode* greatGrandson = grandson->children;
329             std::shared_ptr<GovResNode> govResNode = std::make_shared<GovResNode>(atoi(id), name);
330             for (; greatGrandson; greatGrandson = greatGrandson->next) {
331                 if (!xmlStrcmp(greatGrandson->name, reinterpret_cast<const xmlChar*>("default"))) {
332                     char* def = reinterpret_cast<char*>(xmlNodeGetContent(greatGrandson));
333                     if (!def || !IsNumber(def)) {
334                         SOC_PERF_LOGE("Invalid governor resource default for %{public}s", configFile.c_str());
335                         return false;
336                     }
337                     govResNode->def = atoi(def);
338                 } else if (!xmlStrcmp(greatGrandson->name, reinterpret_cast<const xmlChar*>("path"))) {
339                     char* path = reinterpret_cast<char*>(xmlNodeGetContent(greatGrandson));
340                     if (!path) {
341                         SOC_PERF_LOGE("Invalid governor resource path for %{public}s", configFile.c_str());
342                         return false;
343                     }
344                     govResNode->paths.push_back(path);
345                 } else if (!xmlStrcmp(greatGrandson->name, reinterpret_cast<const xmlChar*>("node"))) {
346                     char* level = reinterpret_cast<char*>(
347                         xmlGetProp(greatGrandson, reinterpret_cast<const xmlChar*>("level")));
348                     char* node = reinterpret_cast<char*>(xmlNodeGetContent(greatGrandson));
349                     if (!level || !IsNumber(level) || !node
350                         || !LoadGovResourceAvailable(govResNode, level, node)) {
351                         SOC_PERF_LOGE("Invalid governor resource node for %{public}s", configFile.c_str());
352                         return false;
353                     }
354                 }
355             }
356 
357             resStrToIdInfo.insert(std::pair<std::string, int>(govResNode->name, govResNode->id));
358             govResNodeInfo.insert(std::pair<int, std::shared_ptr<GovResNode>>(govResNode->id, govResNode));
359         }
360     }
361 
362     if (!CheckGovResDefValid()) {
363         return false;
364     }
365 
366     return true;
367 }
368 
LoadCmd(xmlNode * rootNode,std::string configFile)369 bool SocPerf::LoadCmd(xmlNode* rootNode, std::string configFile)
370 {
371     xmlNode* child = rootNode->children;
372     for (; child; child = child->next) {
373         if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>("cmd"))) {
374             char* id = reinterpret_cast<char*>(xmlGetProp(child, reinterpret_cast<const xmlChar*>("id")));
375             char* name = reinterpret_cast<char*>(xmlGetProp(child, reinterpret_cast<const xmlChar*>("name")));
376             if (!CheckCmdTag(id, name, configFile)) {
377                 return false;
378             }
379             xmlNode* grandson = child->children;
380             std::shared_ptr<Action> action = std::make_shared<Action>(atoi(id), name);
381             for (; grandson; grandson = grandson->next) {
382                 if (!xmlStrcmp(grandson->name, reinterpret_cast<const xmlChar*>("duration"))) {
383                     char* duration = reinterpret_cast<char*>(xmlNodeGetContent(grandson));
384                     if (!duration || !IsNumber(duration)) {
385                         SOC_PERF_LOGE("Invalid cmd duration for %{public}s", configFile.c_str());
386                         return false;
387                     }
388                     action->duration = atoi(duration);
389                 } else {
390                     char* resStr = reinterpret_cast<char*>(const_cast<xmlChar*>(grandson->name));
391                     char* resValue = reinterpret_cast<char*>(xmlNodeGetContent(grandson));
392                     if (!resStr || resStrToIdInfo.find(resStr) == resStrToIdInfo.end()
393                         || !resValue || !IsNumber(resValue)) {
394                         SOC_PERF_LOGE("Invalid cmd resource(%{public}s) for %{public}s, cannot find resId",
395                             resStr, configFile.c_str());
396                         return false;
397                     }
398                     action->variable.push_back(resStrToIdInfo[resStr]);
399                     action->variable.push_back(atoi(resValue));
400                 }
401             }
402 
403             if (configFile == SOCPERF_BOOST_CONFIG_XML) {
404                 perfActionInfo.insert(std::pair<int, std::shared_ptr<Action>>(action->id, action));
405             } else if (configFile == SOCPERF_POWER_CONFIG_XML) {
406                 powerActionInfo.insert(std::pair<int, std::shared_ptr<Action>>(action->id, action));
407             } else if (configFile == SOCPERF_THERMAL_CONFIG_XML) {
408                 thermalActionInfo.insert(std::pair<int, std::shared_ptr<Action>>(action->id, action));
409             }
410         }
411     }
412 
413     if (!CheckActionResIdAndValueValid(configFile)) {
414         return false;
415     }
416 
417     return true;
418 }
419 
CheckResourceTag(char * id,char * name,char * pair,char * mode,std::string configFile)420 bool SocPerf::CheckResourceTag(char* id, char* name, char* pair, char* mode, std::string configFile)
421 {
422     if (!id || !IsNumber(id) || !IsValidResId(atoi(id))) {
423         SOC_PERF_LOGE("Invalid resource id for %{public}s", configFile.c_str());
424         return false;
425     }
426     if (!name) {
427         SOC_PERF_LOGE("Invalid resource name for %{public}s", configFile.c_str());
428         return false;
429     }
430     if (pair && (!IsNumber(pair) || !IsValidResId(atoi(pair)))) {
431         SOC_PERF_LOGE("Invalid resource pair for %{public}s", configFile.c_str());
432         return false;
433     }
434     if (mode && !IsNumber(mode)) {
435         SOC_PERF_LOGE("Invalid resource mode for %{public}s", configFile.c_str());
436         return false;
437     }
438     return true;
439 }
440 
CheckResourceTag(char * def,char * path,std::string configFile)441 bool SocPerf::CheckResourceTag(char* def, char* path, std::string configFile)
442 {
443     if (!def || !IsNumber(def)) {
444         SOC_PERF_LOGE("Invalid resource default for %{public}s", configFile.c_str());
445         return false;
446     }
447     if (!path) {
448         SOC_PERF_LOGE("Invalid resource path for %{public}s", configFile.c_str());
449         return false;
450     }
451     return true;
452 }
453 
LoadResourceAvailable(std::shared_ptr<ResNode> resNode,char * node)454 bool SocPerf::LoadResourceAvailable(std::shared_ptr<ResNode> resNode, char* node)
455 {
456     std::string nodeStr = node;
457     std::vector<std::string> result = Split(nodeStr, " ");
458     for (auto str : result) {
459         if (IsNumber(str)) {
460             resNode->available.insert(stoi(str));
461         } else {
462             return false;
463         }
464     }
465     return true;
466 }
467 
CheckPairResIdValid()468 bool SocPerf::CheckPairResIdValid()
469 {
470     for (auto iter = resNodeInfo.begin(); iter != resNodeInfo.end(); ++iter) {
471         int resId = iter->first;
472         std::shared_ptr<ResNode> resNode = iter->second;
473         int pairResId = resNode->pair;
474         if (resNodeInfo.find(pairResId) == resNodeInfo.end()) {
475             SOC_PERF_LOGE("resId[%{public}d]'s pairResId[%{public}d] is not valid", resId, pairResId);
476             return false;
477         }
478     }
479     return true;
480 }
481 
CheckResDefValid()482 bool SocPerf::CheckResDefValid()
483 {
484     for (auto iter = resNodeInfo.begin(); iter != resNodeInfo.end(); ++iter) {
485         int resId = iter->first;
486         std::shared_ptr<ResNode> resNode = iter->second;
487         int def = resNode->def;
488         if (!resNode->available.empty() && resNode->available.find(def) == resNode->available.end()) {
489             SOC_PERF_LOGE("resId[%{public}d]'s def[%{public}d] is not valid", resId, def);
490             return false;
491         }
492     }
493     return true;
494 }
495 
CheckGovResourceTag(char * id,char * name,std::string configFile)496 bool SocPerf::CheckGovResourceTag(char* id, char* name, std::string configFile)
497 {
498     if (!id || !IsNumber(id) || !IsValidResId(atoi(id))) {
499         SOC_PERF_LOGE("Invalid governor resource id for %{public}s", configFile.c_str());
500         return false;
501     }
502     if (!name) {
503         SOC_PERF_LOGE("Invalid governor resource name for %{public}s", configFile.c_str());
504         return false;
505     }
506     return true;
507 }
508 
LoadGovResourceAvailable(std::shared_ptr<GovResNode> govResNode,char * level,char * node)509 bool SocPerf::LoadGovResourceAvailable(std::shared_ptr<GovResNode> govResNode, char* level, char* node)
510 {
511     govResNode->available.insert(atoi(level));
512     std::string nodeStr = node;
513     std::vector<std::string> result = Split(nodeStr, "|");
514     if (result.size() != govResNode->paths.size()) {
515         SOC_PERF_LOGE("Invalid governor resource node matches paths");
516         return false;
517     }
518     govResNode->levelToStr.insert(std::pair<int, std::vector<std::string>>(atoi(level), result));
519     return true;
520 }
521 
CheckGovResDefValid()522 bool SocPerf::CheckGovResDefValid()
523 {
524     for (auto iter = govResNodeInfo.begin(); iter != govResNodeInfo.end(); ++iter) {
525         int govResId = iter->first;
526         std::shared_ptr<GovResNode> govResNode = iter->second;
527         int def = govResNode->def;
528         if (govResNode->available.find(def) == govResNode->available.end()) {
529             SOC_PERF_LOGE("govResId[%{public}d]'s def[%{public}d] is not valid", govResId, def);
530             return false;
531         }
532     }
533     return true;
534 }
535 
CheckCmdTag(char * id,char * name,std::string configFile)536 bool SocPerf::CheckCmdTag(char* id, char* name, std::string configFile)
537 {
538     if (!id || !IsNumber(id)) {
539         SOC_PERF_LOGE("Invalid cmd id for %{public}s", configFile.c_str());
540         return false;
541     }
542     if (!name) {
543         SOC_PERF_LOGE("Invalid cmd name for %{public}s", configFile.c_str());
544         return false;
545     }
546     return true;
547 }
548 
CheckActionResIdAndValueValid(std::string configFile)549 bool SocPerf::CheckActionResIdAndValueValid(std::string configFile)
550 {
551     std::unordered_map<int, std::shared_ptr<Action>> actionInfo;
552     if (configFile == SOCPERF_BOOST_CONFIG_XML) {
553         actionInfo = perfActionInfo;
554     } else if (configFile == SOCPERF_POWER_CONFIG_XML) {
555         actionInfo = powerActionInfo;
556     } else if (configFile == SOCPERF_THERMAL_CONFIG_XML) {
557         actionInfo = thermalActionInfo;
558     }
559     for (auto iter = actionInfo.begin(); iter != actionInfo.end(); ++iter) {
560         int actionId = iter->first;
561         std::shared_ptr<Action> action = iter->second;
562         for (int i = 0; i < (int)action->variable.size(); i += RES_ID_AND_VALUE_PAIR) {
563             int resId = action->variable[i];
564             int resValue = action->variable[i + 1];
565             if (resNodeInfo.find(resId) != resNodeInfo.end()) {
566                 if (!resNodeInfo[resId]->available.empty()
567                     && resNodeInfo[resId]->available.find(resValue) == resNodeInfo[resId]->available.end()) {
568                     SOC_PERF_LOGE("action[%{public}d]'s resValue[%{public}d] is not valid", actionId, resValue);
569                     return false;
570                 }
571             } else if (govResNodeInfo.find(resId) != govResNodeInfo.end()) {
572                 if (govResNodeInfo[resId]->available.find(resValue) == govResNodeInfo[resId]->available.end()) {
573                     SOC_PERF_LOGE("action[%{public}d]'s resValue[%{public}d] is not valid", actionId, resValue);
574                     return false;
575                 }
576             } else {
577                 SOC_PERF_LOGE("action[%{public}d]'s resId[%{public}d] is not valid", actionId, resId);
578                 return false;
579             }
580         }
581     }
582     return true;
583 }
584 
PrintCachedInfo()585 void SocPerf::PrintCachedInfo()
586 {
587     SOC_PERF_LOGI("------------------------------------");
588     SOC_PERF_LOGI("resNodeInfo(%{public}d)", (int)resNodeInfo.size());
589     for (auto iter = resNodeInfo.begin(); iter != resNodeInfo.end(); ++iter) {
590         std::shared_ptr<ResNode> resNode = iter->second;
591         resNode->PrintString();
592     }
593     SOC_PERF_LOGI("------------------------------------");
594     SOC_PERF_LOGI("govResNodeInfo(%{public}d)", (int)govResNodeInfo.size());
595     for (auto iter = govResNodeInfo.begin(); iter != govResNodeInfo.end(); ++iter) {
596         std::shared_ptr<GovResNode> govResNode = iter->second;
597         govResNode->PrintString();
598     }
599     SOC_PERF_LOGI("------------------------------------");
600     SOC_PERF_LOGI("perfActionInfo(%{public}d)", (int)perfActionInfo.size());
601     for (auto iter = perfActionInfo.begin(); iter != perfActionInfo.end(); ++iter) {
602         std::shared_ptr<Action> action = iter->second;
603         action->PrintString();
604     }
605     SOC_PERF_LOGI("------------------------------------");
606     SOC_PERF_LOGI("powerActionInfo(%{public}d)", (int)powerActionInfo.size());
607     for (auto iter = powerActionInfo.begin(); iter != powerActionInfo.end(); ++iter) {
608         std::shared_ptr<Action> action = iter->second;
609         action->PrintString();
610     }
611     SOC_PERF_LOGI("------------------------------------");
612     SOC_PERF_LOGI("thermalActionInfo(%{public}d)", (int)thermalActionInfo.size());
613     for (auto iter = thermalActionInfo.begin(); iter != thermalActionInfo.end(); ++iter) {
614         std::shared_ptr<Action> action = iter->second;
615         action->PrintString();
616     }
617     SOC_PERF_LOGI("------------------------------------");
618 }
619 } // namespace SOCPERF
620 } // namespace OHOS