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 #include "system_memory_level_config.h"
16 
17 #include "memmgr_log.h"
18 #include "memory_level_constants.h"
19 #include "xml_helper.h"
20 
21 namespace OHOS {
22 namespace Memory {
23 namespace {
24     const std::string TAG = "SystemMemoryLevelConfig";
25 }
26 
SetPurgeable(unsigned int purgeable)27 void SystemMemoryLevelConfig::SetPurgeable(unsigned int purgeable)
28 {
29     purgeable_ = purgeable;
30 }
31 
GetPurgeable(void)32 unsigned int SystemMemoryLevelConfig::GetPurgeable(void)
33 {
34     return purgeable_;
35 }
36 
SetModerate(unsigned int moderate)37 void SystemMemoryLevelConfig::SetModerate(unsigned int moderate)
38 {
39     moderate_ = moderate;
40 }
41 
GetModerate(void)42 unsigned int SystemMemoryLevelConfig::GetModerate(void)
43 {
44     return moderate_;
45 }
46 
SetLow(unsigned int low)47 void SystemMemoryLevelConfig::SetLow(unsigned int low)
48 {
49     low_ = low;
50 }
51 
GetLow(void)52 unsigned int SystemMemoryLevelConfig::GetLow(void)
53 {
54     return low_;
55 }
56 
SetCritical(unsigned int critical)57 void SystemMemoryLevelConfig::SetCritical(unsigned int critical)
58 {
59     critical_ = critical;
60 }
61 
GetCritical(void)62 unsigned int SystemMemoryLevelConfig::GetCritical(void)
63 {
64     return critical_;
65 }
66 
ParseConfig(const xmlNodePtr & rootNodePtr)67 void SystemMemoryLevelConfig::ParseConfig(const xmlNodePtr &rootNodePtr)
68 {
69     if (!XmlHelper::CheckNode(rootNodePtr) || !XmlHelper::HasChild(rootNodePtr)) {
70         HILOGD("Node exsist:%{public}d,has child node:%{public}d",
71                XmlHelper::CheckNode(rootNodePtr), XmlHelper::HasChild(rootNodePtr));
72         return;
73     }
74 
75     std::map<std::string, std::string> param;
76     if (!XmlHelper::GetModuleParam(rootNodePtr, param)) {
77         HILOGW("Get moudle param failed.");
78         return;
79     }
80 
81     unsigned int purgeable;
82     unsigned int moderate;
83     unsigned int low;
84     unsigned int critical;
85     XmlHelper::SetUnsignedIntParam(param, "purgeable", purgeable, MEMORY_LEVEL_PURGEABLE_DEFAULT);
86     XmlHelper::SetUnsignedIntParam(param, "moderate", moderate, MEMORY_LEVEL_MODERATE_DEFAULT);
87     XmlHelper::SetUnsignedIntParam(param, "low", low, MEMORY_LEVEL_LOW_DEFAULT);
88     XmlHelper::SetUnsignedIntParam(param, "critical", critical, MEMORY_LEVEL_CRITICAL_DEFAULT);
89 
90     /* change MB to KB */
91     purgeable *= KB_PER_MB;
92     moderate *= KB_PER_MB;
93     low *= KB_PER_MB;
94     critical *= KB_PER_MB;
95 
96     SetPurgeable(purgeable);
97     SetModerate(moderate);
98     SetLow(low);
99     SetCritical(critical);
100 
101     HILOGI("purgeable=%{public}u, moderate=%{public}u, low=%{public}u, critical=%{public}u.", purgeable, moderate, low,
102            critical);
103 }
104 
Dump(int fd)105 void SystemMemoryLevelConfig::Dump(int fd)
106 {
107     dprintf(fd, "SystemMemoryLevelConfig:   \n");
108     dprintf(fd, "                     purgeable: %u\n", purgeable_);
109     dprintf(fd, "                      moderate: %u\n", moderate_);
110     dprintf(fd, "                           low: %u\n", low_);
111     dprintf(fd, "                      critical: %u\n", critical_);
112 }
113 } // namespace Memory
114 } // namespace OHOS
115