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 <fstream>
17 #include <string>
18
19 #include "xml_helper.h"
20 #include "memmgr_config_manager.h"
21
22 namespace OHOS {
23 namespace Memory {
24 namespace {
25 const std::string TAG = "MemmgrConfigManager";
26 const std::string XML_PATH = "/etc/memmgr/";
27 const std::string MEMCG_PATH = KernelInterface::MEMCG_BASE_PATH;
28 } // namespace
29 IMPLEMENT_SINGLE_INSTANCE(MemmgrConfigManager);
30
Init()31 bool MemmgrConfigManager::Init()
32 {
33 killConfig_.ClearKillLevelsMap();
34 ReadParamFromXml();
35 WriteReclaimRatiosConfigToKernel();
36 return this->xmlLoaded_;
37 }
38
MemmgrConfigManager()39 MemmgrConfigManager::MemmgrConfigManager()
40 {
41 InitDefaultConfig();
42 }
43
~MemmgrConfigManager()44 MemmgrConfigManager::~MemmgrConfigManager()
45 {
46 ClearReclaimConfigSet();
47 killConfig_.ClearKillLevelsMap();
48 }
49
InitDefaultConfig()50 void MemmgrConfigManager::InitDefaultConfig()
51 {
52 availBufferConfig_.SetDefaultConfig(AVAIL_BUFFER, MIN_AVAIL_BUFFER, HIGH_AVAIL_BUFFER, SWAP_RESERVE);
53 reclaimConfig_.SetDefaultConfig(RECLAIM_PRIORITY_MIN, RECLAIM_PRIORITY_MAX,
54 MEMCG_MEM_2_ZRAM_RATIO, MEMCG_ZRAM_2_UFS_RATIO, MEMCG_REFAULT_THRESHOLD);
55 }
56
GetXmlLoaded()57 bool MemmgrConfigManager::GetXmlLoaded()
58 {
59 return xmlLoaded_;
60 }
61
ClearExistConfig()62 void MemmgrConfigManager::ClearExistConfig()
63 {
64 ClearReclaimConfigSet();
65 }
66
ReadParamFromXml()67 bool MemmgrConfigManager::ReadParamFromXml()
68 {
69 std::string path = KernelInterface::GetInstance().JoinPath(XML_PATH, "memmgr_config.xml");
70 char absPatch[PATH_MAX] = {0};
71 HILOGI(":%{public}s", path.c_str());
72 if (path.length() > PATH_MAX || realpath(path.c_str(), absPatch) == NULL) {
73 return false;
74 }
75 if (!XmlHelper::CheckPathExist(path.c_str())) {
76 HILOGE("bad profile path! path:%{public}s", path.c_str());
77 return false;
78 }
79 std::unique_ptr<xmlDoc, decltype(&xmlFreeDoc)> docPtr(
80 xmlReadFile(path.c_str(), nullptr, XML_PARSE_NOBLANKS), xmlFreeDoc);
81 if (docPtr == nullptr) {
82 HILOGE("xmlReadFile error!");
83 return false;
84 }
85 ClearExistConfig();
86 xmlNodePtr rootNodePtr = xmlDocGetRootElement(docPtr.get());
87 this->xmlLoaded_ = ParseXmlRootNode(rootNodePtr);
88 return this->xmlLoaded_;
89 }
90
ParseXmlRootNode(const xmlNodePtr & rootNodePtr)91 bool MemmgrConfigManager::ParseXmlRootNode(const xmlNodePtr &rootNodePtr)
92 {
93 for (xmlNodePtr currNode = rootNodePtr->xmlChildrenNode; currNode != nullptr; currNode = currNode->next) {
94 std::string name = std::string(reinterpret_cast<const char *>(currNode->name));
95 if (name.compare("availBufferConfig") == 0) {
96 availBufferConfig_.ParseConfig(currNode);
97 continue;
98 }
99
100 if (name.compare("reclaimConfig") == 0) {
101 reclaimConfig_.ParseConfig(currNode);
102 continue;
103 }
104
105 if (name.compare("killConfig") == 0) {
106 killConfig_.ParseConfig(currNode);
107 continue;
108 }
109 if (name.compare("systemMemoryLevelConfig") == 0) {
110 systemMemoryLevelConfig_.ParseConfig(currNode);
111 continue;
112 }
113 if (name.compare("reclaimPriorityConfig") == 0) {
114 reclaimPriorityConfig_.ParseConfig(currNode);
115 continue;
116 }
117 if (name.compare("nandLifeConfig") == 0) {
118 nandLifeConfig_.ParseConfig(currNode);
119 continue;
120 }
121 if (name.compare("switchConfig") == 0) {
122 switchConfig_.ParseConfig(currNode);
123 continue;
124 }
125 HILOGW("unknown node :<%{public}s>", name.c_str());
126 return false;
127 }
128 return true;
129 }
130
ClearReclaimConfigSet()131 void MemmgrConfigManager::ClearReclaimConfigSet()
132 {
133 reclaimConfig_.ClearReclaimConfigSet();
134 }
135
GetNandLifeConfig()136 const NandLifeConfig &MemmgrConfigManager::GetNandLifeConfig()
137 {
138 return nandLifeConfig_;
139 }
140
GetSwitchConfig()141 const SwitchConfig &MemmgrConfigManager::GetSwitchConfig()
142 {
143 return switchConfig_;
144 }
145
GetReclaimPriorityConfig()146 const ReclaimPriorityConfig& MemmgrConfigManager::GetReclaimPriorityConfig()
147 {
148 return reclaimPriorityConfig_;
149 }
150
GetKillLevelsMap()151 const KillConfig::KillLevelsMap& MemmgrConfigManager::GetKillLevelsMap()
152 {
153 return killConfig_.GetKillLevelsMap();
154 }
155
WriteReclaimRatiosConfigToKernel()156 bool MemmgrConfigManager::WriteReclaimRatiosConfigToKernel()
157 {
158 std::string path = KernelInterface::GetInstance().JoinPath(MEMCG_PATH, "memory.zswapd_memcgs_param");
159 std::string content;
160
161 unsigned int paramNum = reclaimConfig_.GetReclaimConfigSet().size();
162 content = std::to_string(paramNum);
163 for (auto i = reclaimConfig_.GetReclaimConfigSet().begin(); i != reclaimConfig_.GetReclaimConfigSet().end(); ++i) {
164 content += " " + std::to_string((*i)->GetMinScore());
165 content += " " + std::to_string((*i)->GetMaxScore());
166 content += " " + std::to_string((*i)->GetMem2zramRatio());
167 content += " " + std::to_string((*i)->GetZram2ufsRatio());
168 content += " " + std::to_string((*i)->GetRefaultThreshold());
169 }
170
171 HILOGI("Write to kernel: <%{public}s>", content.c_str());
172 return KernelInterface::GetInstance().WriteToFile(path, content);
173 }
174
GetAvailBufferConfig()175 AvailBufferConfig MemmgrConfigManager::GetAvailBufferConfig()
176 {
177 return availBufferConfig_;
178 }
179
GetReclaimConfigSet()180 const ReclaimConfig::ReclaimConfigSet& MemmgrConfigManager::GetReclaimConfigSet()
181 {
182 return reclaimConfig_.GetReclaimConfigSet();
183 }
184
GetKillConfig()185 const KillConfig& MemmgrConfigManager::GetKillConfig()
186 {
187 return killConfig_;
188 }
189
GetSystemMemoryLevelConfig()190 SystemMemoryLevelConfig MemmgrConfigManager::GetSystemMemoryLevelConfig()
191 {
192 return systemMemoryLevelConfig_;
193 }
194
Dump(int fd)195 void MemmgrConfigManager::Dump(int fd)
196 {
197 availBufferConfig_.Dump(fd);
198 killConfig_.Dump(fd);
199 reclaimConfig_.Dump(fd);
200 nandLifeConfig_.Dump(fd);
201 systemMemoryLevelConfig_.Dump(fd);
202 switchConfig_.Dump(fd);
203 }
204 } // namespace Memory
205 } // namespace OHOS
206