• 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 #ifndef OHOS_MEMORY_MEMMGR_MEMMGR_CONFIG_MANAGER_H
17 #define OHOS_MEMORY_MEMMGR_MEMMGR_CONFIG_MANAGER_H
18 
19 #include <stdexcept>
20 #include <map>
21 #include <string>
22 #include <set>
23 #include "event_handler.h"
24 #include "libxml/parser.h"
25 #include "libxml/xpath.h"
26 #include "kernel_interface.h"
27 #include "single_instance.h"
28 #include "reclaim_strategy_constants.h"
29 #include "reclaim_priority_constants.h"
30 
31 namespace OHOS {
32 namespace Memory {
33 class AvailBufferSize {
34 public:
35     int availBuffer;
36     int minAvailBuffer;
37     int highAvailBuffer;
38     int swapReserve;
39     AvailBufferSize(int availBuffer, int minAvailBuffer, int highAvailBuffer, int swapReserve);
40 };
41 
42 class ReclaimRatiosConfig {
43 public:
44     int minScore;
45     int maxScore;
46     int mem2zramRatio;
47     int zran2ufsRation;
48     int refaultThreshold;
49     ReclaimRatiosConfig(int minScore, int maxScore, int mem2zramRatio, int zran2ufsRation, int refaultThreshold);
50 };
51 
52 struct ReclaimRatiosConfigPtrCmp {
operatorReclaimRatiosConfigPtrCmp53     bool operator()(const ReclaimRatiosConfig *p1, const ReclaimRatiosConfig *p2)
54     {
55         if (p1->minScore <= p2->minScore) {
56             return true;
57         } else {
58             return false;
59         }
60     };
61 };
62 
63 class MemmgrConfigManager {
64     DECLARE_SINGLE_INSTANCE_BASE(MemmgrConfigManager);
65 public:
66     bool Init();
67     bool ReadParamFromXml();
68     bool WriteReclaimRatiosConfigToKernel();
69     using ReclaimRatiosConfigSet = std::set<ReclaimRatiosConfig *, ReclaimRatiosConfigPtrCmp>;
70     bool GetXmlLoaded();
71     AvailBufferSize *GetAvailBufferSize();
72     const ReclaimRatiosConfigSet GetReclaimRatiosConfigSet();
73 
74 private:
75     bool ParseXmlRootNode(const xmlNodePtr &rootNodePtr);
76     bool ParseKillConfig(const xmlNodePtr &rootNodePtr);
77     bool ParseReclaimConfig(const xmlNodePtr &rootNodePtr);
78     bool GetModuleParam(const xmlNodePtr &currNodePtr, std::map<std::string, std::string> &param);
79     void SetIntParam(std::map<std::string, std::string> &param, std::string key, int* dst);
80     bool SetReclaimParam(const xmlNodePtr &currNodePtr, std::map<std::string, std::string> &param);
81     bool SetAvailBufferConfig(std::map<std::string, std::string> &param);
82     bool SetZswapdParamConfig (std::map<std::string, std::string> &param);
83     bool CheckNode(const xmlNodePtr &nodePtr);
84     bool HasChild(const xmlNodePtr &rootNodePtr);
85     bool CheckPathExist(const char *path);
86     void ClearExistConfig();
87     bool XmlLoaded = false;
88     AvailBufferSize *availBufferSize =
89         new AvailBufferSize(AVAIL_BUFFER, MIN_AVAIL_BUFFER, HIGH_AVAIL_BUFFER, SWAP_RESERVE);
90     ReclaimRatiosConfigSet reclaimRatiosConfigSet {
91         new ReclaimRatiosConfig(RECLAIM_PRIORITY_MIN, RECLAIM_PRIORITY_MAX, MEMCG_MEM_2_ZRAM_RATIO,
92                                 MEMCG_ZRAM_2_UFS_RATIO, MEMCG_REFAULT_THRESHOLD)};
93     void AddReclaimRatiosConfigToSet(ReclaimRatiosConfig *reclaimRatiosConfig);
94     void ClearReclaimRatiosConfigSet();
95     MemmgrConfigManager();
96     ~MemmgrConfigManager();
97 };
98 } // namespace Memory
99 } // namespace OHOS
100 #endif // OHOS_MEMORY_MEMMGR_RECLAIM_CONFIG_MANAGER_H
101