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