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_CONFIG_H
17 #define SOC_PERF_SERVICES_CORE_INCLUDE_SOCPERF_CONFIG_H
18 
19 #include "libxml/tree.h"
20 #include "socperf_common.h"
21 #include <string>
22 #include <vector>
23 
24 namespace OHOS {
25 namespace SOCPERF {
26 using ReportDataFunc = int (*)(const std::vector<int32_t>& resId, const std::vector<int64_t>& value,
27     const std::vector<int64_t>& endTime, const std::string& msgStr);
28 class SocPerfConfig {
29 public:
30     bool Init();
31     bool IsGovResId(int32_t resId) const;
32     bool IsValidResId(int32_t resId) const;
33     static SocPerfConfig& GetInstance();
34 
35 public:
36     ReportDataFunc reportFunc_ = nullptr;
37     std::mutex resourceNodeMutex_;
38     std::unordered_map<int32_t, std::shared_ptr<ResourceNode>> resourceNodeInfo_;
39     std::mutex perfActionsMutex_;
40     std::unordered_map<int32_t, std::shared_ptr<Actions>> perfActionsInfo_;
41     bool isTraceDug = false;
42 
43 private:
44     SocPerfConfig();
45     ~SocPerfConfig();
46     std::string GetRealConfigPath(const std::string& configFile);
47     std::vector<std::string> GetAllRealConfigPath(const std::string& configFile);
48     bool LoadAllConfigXmlFile(const std::string& configFile);
49     bool LoadConfigXmlFile(const std::string& realConfigFile);
50     void InitPerfFunc(const char* perfSoPath, const char* perfSoFunc);
51     bool ParseBoostXmlFile(const xmlNode* rootNode, const std::string& realConfigFile, xmlDoc* file);
52     bool ParseResourceXmlFile(const xmlNode* rootNode, const std::string& realConfigFile, xmlDoc* file);
53     bool LoadResource(xmlNode* rootNode, const std::string& configFile);
54     bool TraversalFreqResource(xmlNode* grandson, const std::string& configFile);
55     bool LoadFreqResourceContent(int32_t persistMode, xmlNode* greatGrandson, const std::string& configFile,
56         std::shared_ptr<ResNode> resNode);
57     int32_t GetXmlIntProp(const xmlNode* xmlNode, const char* propName) const;
58     bool LoadGovResource(xmlNode* rootNode, const std::string& configFile);
59     bool TraversalGovResource(int32_t persistMode, xmlNode* greatGrandson, const std::string& configFile,
60         std::shared_ptr<GovResNode> govResNode);
61     void LoadInfo(xmlNode* child, const std::string& configFile);
62     bool LoadCmd(const xmlNode* rootNode, const std::string& configFile);
63     bool TraversalBoostResource(xmlNode* grandson, const std::string& configFile, std::shared_ptr<Actions> actions);
64     bool ParseDuration(xmlNode* greatGrandson, const std::string& configFile, std::shared_ptr<Action> action) const;
65     bool ParseResValue(xmlNode* greatGrandson, const std::string& configFile, std::shared_ptr<Action> action);
66     bool CheckResourceTag(const char* id, const char* name, const char* pair, const char* mode,
67         const char* persistMode, const std::string& configFile) const;
68     bool CheckResourcePersistMode(const char* persistMode, const std::string& configFile) const;
69     bool CheckResourceTag(int32_t persistMode, const char* def, const char* path, const std::string& configFile) const;
70     bool LoadResourceAvailable(std::shared_ptr<ResNode> resNode, const char* node);
71     bool CheckPairResIdValid() const;
72     bool CheckDefValid() const;
73     bool CheckGovResourceTag(const char* id, const char* name, const char* persistMode,
74         const std::string& configFile) const;
75     void ParseModeCmd(const char* mode, const std::string& configFile, std::shared_ptr<Actions> actions);
76     bool LoadGovResourceAvailable(std::shared_ptr<GovResNode> govResNode, const char* level, const char* node);
77     bool CheckCmdTag(const char* id, const char* name, const std::string& configFile) const;
78     bool CheckActionResIdAndValueValid(const std::string& configFile);
79     bool TraversalActions(std::shared_ptr<Action> action, int32_t actionId);
80     bool CheckTrace(const char* trace);
81     void IsTeaceDug();
82 };
83 } // namespace SOCPERF
84 } // namespace OHOS
85 #endif // SOC_PERF_SERVICES_CORE_INCLUDE_SOCPERF_H
86