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