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