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 "reclaim_priority_config.h"
16 #include "memmgr_ptr_util.h"
17 #include "kernel_interface.h"
18 #include "reclaim_priority_manager.h"
19 #include "memmgr_config_manager.h"
20 #include "memmgr_log.h"
21 #include "xml_helper.h"
22
23 namespace OHOS {
24 namespace Memory {
25 namespace {
26 const std::string TAG = "ReclaimPriorityConfig";
27 }
28
ParseConfig(const xmlNodePtr & rootNodePtr)29 void ReclaimPriorityConfig::ParseConfig(const xmlNodePtr &rootNodePtr)
30 {
31 if (!XmlHelper::CheckNode(rootNodePtr) || !XmlHelper::HasChild(rootNodePtr)) {
32 HILOGD("Node exsist:%{public}d,has child node:%{public}d",
33 XmlHelper::CheckNode(rootNodePtr), XmlHelper::HasChild(rootNodePtr));
34 return;
35 }
36 for (xmlNodePtr currNode = rootNodePtr->xmlChildrenNode; currNode != nullptr; currNode = currNode->next) {
37 if (!XmlHelper::CheckNode(currNode)) {
38 return;
39 }
40 std::string name = std::string(reinterpret_cast<const char *>(currNode->name));
41 if (name.compare("killalbeSystemApps") == 0) {
42 ParseReclaimPriorityKillableSystemAppsConfig(currNode);
43 continue;
44 }
45 if (name.compare("importantBgApps") == 0) {
46 ParseReclaimPriorityImportantBgAppsConfig(currNode);
47 continue;
48 }
49 HILOGW("unknown node :<%{public}s>", name.c_str());
50 return;
51 }
52 return;
53 }
54
ParseReclaimPriorityKillableSystemAppsConfig(const xmlNodePtr & rootNodePtr)55 void ReclaimPriorityConfig::ParseReclaimPriorityKillableSystemAppsConfig(const xmlNodePtr &rootNodePtr)
56 {
57 if (!XmlHelper::CheckNode(rootNodePtr) || !XmlHelper::HasChild(rootNodePtr)) {
58 HILOGD("Node exsist:%{public}d,has child node:%{public}d",
59 XmlHelper::CheckNode(rootNodePtr), XmlHelper::HasChild(rootNodePtr));
60 return;
61 }
62 for (xmlNodePtr currNode = rootNodePtr->xmlChildrenNode; currNode != nullptr; currNode = currNode->next) {
63 if (!XmlHelper::CheckNode(currNode)) {
64 return;
65 }
66 std::string name = std::string(reinterpret_cast<const char *>(currNode->name));
67 if (name.compare("killableSysApp") == 0) {
68 auto contentPtr = xmlNodeGetContent(currNode);
69 if (contentPtr == nullptr) {
70 continue;
71 }
72 std::string value = std::string(reinterpret_cast<char *>(contentPtr));
73 HILOGW("read a killable app: %{public}s", value.c_str());
74 if (value.size() == 0) {
75 HILOGE("read a empty killable app: %{public}s, ignore it!", value.c_str());
76 continue;
77 }
78 killalbeSystemApps_.insert(value);
79 xmlFree(contentPtr);
80 continue;
81 }
82 HILOGW("unknown node :<%{public}s>", name.c_str());
83 return;
84 }
85 return;
86 }
87
ParseReclaimPriorityImportantBgAppsConfig(const xmlNodePtr & rootNodePtr)88 void ReclaimPriorityConfig::ParseReclaimPriorityImportantBgAppsConfig(const xmlNodePtr &rootNodePtr)
89 {
90 HILOGI("called");
91 if (!XmlHelper::CheckNode(rootNodePtr) || !XmlHelper::HasChild(rootNodePtr)) {
92 HILOGD("Node exsist:%{public}d,has child node:%{public}d",
93 XmlHelper::CheckNode(rootNodePtr), XmlHelper::HasChild(rootNodePtr));
94 return;
95 }
96 for (xmlNodePtr currNode = rootNodePtr->xmlChildrenNode; currNode != nullptr; currNode = currNode->next) {
97 if (!XmlHelper::CheckNode(currNode)) {
98 return;
99 }
100 std::string name = std::string(reinterpret_cast<const char *>(currNode->name));
101 if (name.compare("importantBgApp") == 0) {
102 std::map<std::string, std::string> param;
103 std::string procName;
104 int minPriority;
105
106 XmlHelper::GetModuleParam(currNode, param);
107 XmlHelper::SetStringParam(param, "procName", procName, "");
108 XmlHelper::SetIntParam(param, "minPriority", minPriority, RECLAIM_PRIORITY_MAX + 1);
109
110 if (procName.size() == 0 || minPriority < RECLAIM_PRIORITY_MIN || minPriority > RECLAIM_PRIORITY_MAX + 1) {
111 HILOGE("read fail, ignore it!");
112 continue;
113 }
114 importantBgApps_.insert(std::make_pair(procName, minPriority));
115 continue;
116 }
117 HILOGW("unknown node :<%{public}s>", name.c_str());
118 }
119 return;
120 }
121
GetkillalbeSystemApps()122 std::set<std::string> ReclaimPriorityConfig::GetkillalbeSystemApps()
123 {
124 return killalbeSystemApps_;
125 }
126
GetImportantBgApps()127 std::map<std::string, int> ReclaimPriorityConfig::GetImportantBgApps()
128 {
129 return importantBgApps_;
130 }
131
Dump(int fd)132 void ReclaimPriorityConfig::Dump(int fd)
133 {
134 dprintf(fd, "ImportantBgApps: \n");
135 for (auto it = importantBgApps_.begin(); it != importantBgApps_.end(); it++) {
136 dprintf(fd, " procName:%s ----> prio:%d \n", it->first.c_str(), it->second);
137 }
138 }
139 } // namespace Memory
140 } // namespace OHOS
141