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 "mem_mgr_client.h"
17 #include "memmgr_log.h"
18
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22
23 extern "C" {
notify_process_status(int32_t pid,int32_t type,int32_t status,int saId)24 int32_t notify_process_status(int32_t pid, int32_t type, int32_t status, int saId)
25 {
26 return OHOS::Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, type, status, saId);
27 }
28
set_critical(int32_t pid,bool critical,int32_t saId)29 int32_t set_critical(int32_t pid, bool critical, int32_t saId)
30 {
31 return OHOS::Memory::MemMgrClient::GetInstance().SetCritical(pid, critical, saId);
32 }
33 }
34
35 namespace OHOS {
36 namespace Memory {
37 namespace {
38 const std::string TAG = "MemMgrClient";
39 }
40
41 IMPLEMENT_SINGLE_INSTANCE(MemMgrClient);
42
GetBundlePriorityList(BundlePriorityList & bundlePrioList)43 int32_t MemMgrClient::GetBundlePriorityList(BundlePriorityList &bundlePrioList)
44 {
45 HILOGE("called");
46 auto dps = GetMemMgrService();
47 if (dps == nullptr) {
48 HILOGE("MemMgrService is null");
49 return -1;
50 }
51 return dps->GetBundlePriorityList(bundlePrioList);
52 }
53
NotifyDistDevStatus(int32_t pid,int32_t uid,const std::string & name,bool connected)54 int32_t MemMgrClient::NotifyDistDevStatus(int32_t pid, int32_t uid, const std::string &name, bool connected)
55 {
56 HILOGI("called, pid=%{public}d, uid=%{public}d, name=%{public}s, connected=%{public}d", pid, uid, name.c_str(),
57 connected);
58 auto dps = GetMemMgrService();
59 if (dps == nullptr) {
60 HILOGE("MemMgrService is null");
61 return -1;
62 }
63 return dps->NotifyDistDevStatus(pid, uid, name, connected);
64 }
65
GetKillLevelOfLmkd(int32_t & killLevel)66 int32_t MemMgrClient::GetKillLevelOfLmkd(int32_t &killLevel)
67 {
68 HILOGE("called");
69 auto dps = GetMemMgrService();
70 if (dps == nullptr) {
71 HILOGE("MemMgrService is null");
72 return -1;
73 }
74 return dps->GetKillLevelOfLmkd(killLevel);
75 }
76
77 #ifdef USE_PURGEABLE_MEMORY
RegisterActiveApps(int32_t pid,int32_t uid)78 int32_t MemMgrClient::RegisterActiveApps(int32_t pid, int32_t uid)
79 {
80 HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
81 auto dps = GetMemMgrService();
82 if (dps == nullptr) {
83 HILOGE("MemMgrService is null");
84 return -1;
85 }
86 return dps->RegisterActiveApps(pid, uid);
87 }
88
DeregisterActiveApps(int32_t pid,int32_t uid)89 int32_t MemMgrClient::DeregisterActiveApps(int32_t pid, int32_t uid)
90 {
91 HILOGI("called, pid=%{public}d, uid=%{public}d", pid, uid);
92 auto dps = GetMemMgrService();
93 if (dps == nullptr) {
94 HILOGE("MemMgrService is null");
95 return -1;
96 }
97 return dps->DeregisterActiveApps(pid, uid);
98 }
99
SubscribeAppState(const AppStateSubscriber & subscriber)100 int32_t MemMgrClient::SubscribeAppState(const AppStateSubscriber &subscriber)
101 {
102 HILOGI("called");
103 auto dps = GetMemMgrService();
104 if (dps == nullptr) {
105 HILOGE("MemMgrService is null");
106 return -1;
107 }
108 sptr<AppStateSubscriber::AppStateSubscriberImpl> subscriberSptr = subscriber.GetImpl();
109 if (subscriberSptr == nullptr) {
110 HILOGE("subscriberSptr is null");
111 return -1;
112 }
113 return dps->SubscribeAppState(subscriberSptr);
114 }
115
UnsubscribeAppState(const AppStateSubscriber & subscriber)116 int32_t MemMgrClient::UnsubscribeAppState(const AppStateSubscriber &subscriber)
117 {
118 HILOGI("called");
119 auto dps = GetMemMgrService();
120 if (dps == nullptr) {
121 HILOGE("MemMgrService is null");
122 return -1;
123 }
124 sptr<AppStateSubscriber::AppStateSubscriberImpl> subscriberSptr = subscriber.GetImpl();
125 if (subscriberSptr == nullptr) {
126 HILOGE("subscriberSptr is null");
127 return -1;
128 }
129 return dps->UnsubscribeAppState(subscriberSptr);
130 }
131
GetAvailableMemory(int32_t & memSize)132 int32_t MemMgrClient::GetAvailableMemory(int32_t &memSize)
133 {
134 HILOGI("called");
135 auto dps = GetMemMgrService();
136 if (dps == nullptr) {
137 HILOGE("MemMgrService is null");
138 return -1;
139 }
140 return dps->GetAvailableMemory(memSize);
141 }
142
GetTotalMemory(int32_t & memSize)143 int32_t MemMgrClient::GetTotalMemory(int32_t &memSize)
144 {
145 HILOGI("called");
146 auto dps = GetMemMgrService();
147 if (dps == nullptr) {
148 HILOGE("MemMgrService is null");
149 return -1;
150 }
151 return dps->GetTotalMemory(memSize);
152 }
153
GetAvailableMemory()154 int32_t MemMgrClient::GetAvailableMemory()
155 {
156 int32_t memSize = 0;
157 if (GetAvailableMemory(memSize) != 0) {
158 return -1;
159 }
160 return memSize;
161 }
162
GetTotalMemory()163 int32_t MemMgrClient::GetTotalMemory()
164 {
165 int32_t memSize = 0;
166 if (GetTotalMemory(memSize) != 0) {
167 return -1;
168 }
169 return memSize;
170 }
171
172 #else
RegisterActiveApps(int32_t pid,int32_t uid)173 int32_t MemMgrClient::RegisterActiveApps(int32_t pid, int32_t uid)
174 {
175 return -1;
176 }
177
DeregisterActiveApps(int32_t pid,int32_t uid)178 int32_t MemMgrClient::DeregisterActiveApps(int32_t pid, int32_t uid)
179 {
180 return -1;
181 }
182
SubscribeAppState(const AppStateSubscriber & subscriber)183 int32_t MemMgrClient::SubscribeAppState(const AppStateSubscriber &subscriber)
184 {
185 return -1;
186 }
187
UnsubscribeAppState(const AppStateSubscriber & subscriber)188 int32_t MemMgrClient::UnsubscribeAppState(const AppStateSubscriber &subscriber)
189 {
190 return -1;
191 }
192
GetAvailableMemory(int32_t & memSize)193 int32_t MemMgrClient::GetAvailableMemory(int32_t &memSize)
194 {
195 return -1;
196 }
197
GetTotalMemory(int32_t & memSize)198 int32_t MemMgrClient::GetTotalMemory(int32_t &memSize)
199 {
200 return -1;
201 }
202
GetAvailableMemory()203 int32_t MemMgrClient::GetAvailableMemory()
204 {
205 return -1;
206 }
207
GetTotalMemory()208 int32_t MemMgrClient::GetTotalMemory()
209 {
210 return -1;
211 }
212 #endif // USE_PURGEABLE_MEMORY
213
OnWindowVisibilityChanged(const std::vector<sptr<MemMgrWindowInfo>> & MemMgrWindowInfo)214 int32_t MemMgrClient::OnWindowVisibilityChanged(const std::vector<sptr<MemMgrWindowInfo>> &MemMgrWindowInfo)
215 {
216 HILOGD("called");
217 auto dps = GetMemMgrService();
218 if (dps == nullptr) {
219 HILOGE("MemMgrService is null");
220 return -1;
221 }
222 return dps->OnWindowVisibilityChanged(MemMgrWindowInfo);
223 }
224
GetReclaimPriorityByPid(int32_t pid,int32_t & priority)225 int32_t MemMgrClient::GetReclaimPriorityByPid(int32_t pid, int32_t &priority)
226 {
227 HILOGD("called");
228 auto dps = GetMemMgrService();
229 if (dps == nullptr) {
230 HILOGE("MemMgrService is null");
231 return -1;
232 }
233 return dps->GetReclaimPriorityByPid(pid, priority);
234 }
235
NotifyProcessStateChangedSync(const MemMgrProcessStateInfo & processStateInfo)236 int32_t MemMgrClient::NotifyProcessStateChangedSync(const MemMgrProcessStateInfo &processStateInfo)
237 {
238 HILOGD("called");
239 auto dps = GetMemMgrService();
240 if (dps == nullptr) {
241 HILOGE("MemMgrService is null");
242 return -1;
243 }
244 return dps->NotifyProcessStateChangedSync(processStateInfo);
245 }
246
NotifyProcessStateChangedAsync(const MemMgrProcessStateInfo & processStateInfo)247 int32_t MemMgrClient::NotifyProcessStateChangedAsync(const MemMgrProcessStateInfo &processStateInfo)
248 {
249 HILOGD("called");
250 auto dps = GetMemMgrService();
251 if (dps == nullptr) {
252 HILOGE("MemMgrService is null");
253 return -1;
254 }
255 return dps->NotifyProcessStateChangedAsync(processStateInfo);
256 }
257
NotifyProcessStatus(int32_t pid,int32_t type,int32_t status,int saId)258 int32_t MemMgrClient::NotifyProcessStatus(int32_t pid, int32_t type, int32_t status, int saId)
259 {
260 HILOGI("called");
261 auto dps = GetMemMgrService();
262 if (dps == nullptr) {
263 HILOGE("MemMgrService is null");
264 return -1;
265 }
266 return dps->NotifyProcessStatus(pid, type, status, saId);
267 }
268
SetCritical(int32_t pid,bool critical,int32_t saId)269 int32_t MemMgrClient::SetCritical(int32_t pid, bool critical, int32_t saId)
270 {
271 HILOGI("called");
272 auto dps = GetMemMgrService();
273 if (dps == nullptr) {
274 HILOGE("MemMgrService is null");
275 return -1;
276 }
277 return dps->SetCritical(pid, critical, saId);
278 }
279
GetMemMgrService()280 sptr<IMemMgr> MemMgrClient::GetMemMgrService()
281 {
282 HILOGI("called");
283 std::lock_guard<std::mutex> lock(mutex_);
284
285 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
286 if (samgrProxy == nullptr) {
287 HILOGE("get samgr failed");
288 return nullptr;
289 }
290 auto object = samgrProxy->GetSystemAbility(MEMORY_MANAGER_SA_ID);
291 if (object == nullptr) {
292 HILOGE("get service failed");
293 return nullptr;
294 }
295 HILOGI("get service succeed");
296 dpProxy_ = iface_cast<IMemMgr>(object);
297 return dpProxy_;
298 }
299 } // namespace Memory
300 } // namespace OHOS
301