• 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 #include "memory_level_manager.h"
16 #include "memmgr_log.h"
17 #include "memmgr_ptr_util.h"
18 #include "app_mem_info.h"
19 #include "app_mgr_client.h"
20 #include "kernel_interface.h"
21 #include "reclaim_priority_manager.h"
22 #include "memmgr_config_manager.h"
23 
24 namespace OHOS {
25 namespace Memory {
26 namespace {
27     const std::string TAG = "MemoryLevelManager";
28 }
29 
30 IMPLEMENT_SINGLE_INSTANCE(MemoryLevelManager);
31 
MemoryLevelManager()32 MemoryLevelManager::MemoryLevelManager()
33 {
34     initialized_ = GetEventHandler();
35     if (initialized_) {
36         HILOGI("init succeeded");
37     } else {
38         HILOGE("init failed");
39     }
40 }
41 
GetEventHandler()42 bool MemoryLevelManager::GetEventHandler()
43 {
44     if (!handler_) {
45         MAKE_POINTER(handler_, shared, AppExecFwk::EventHandler, "failed to create event handler", return false,
46             AppExecFwk::EventRunner::Create());
47     }
48     return true;
49 }
50 
CalcSystemMemoryLevel(SystemMemoryLevel & level)51 bool MemoryLevelManager::CalcSystemMemoryLevel(SystemMemoryLevel &level)
52 {
53     int currentBuffer = KernelInterface::GetInstance().GetCurrentBuffer();
54     std::shared_ptr<SystemMemoryLevelConfig> config =
55         std::make_shared<SystemMemoryLevelConfig>(MemmgrConfigManager::GetInstance().GetSystemMemoryLevelConfig());
56     if (config == nullptr) {
57         HILOGE("The SystemMemoryLevelConfig is NULL.");
58         return false;
59     }
60 
61     if (currentBuffer <= config->GetCritical()) {
62         level = SystemMemoryLevel::MEMORY_LEVEL_CRITICAL;
63     } else if (currentBuffer <= config->GetLow()) {
64         level = SystemMemoryLevel::MEMORY_LEVEL_LOW;
65     } else if (currentBuffer <= config->GetModerate()) {
66         level = SystemMemoryLevel::MEMORY_LEVEL_MODERATE;
67     } else {
68         return false;
69     }
70 
71     HILOGI("critical:%{public}d low:%{public}d moderate:%{public}d in config, curBuf:%{public}dKB, level:%{public}d.",
72            config->GetCritical(), config->GetLow(), config->GetModerate(), currentBuffer, static_cast<int>(level));
73     return true;
74 }
75 
CalcReclaimAppList(std::vector<std::shared_ptr<AppEntity>> & appList)76 bool MemoryLevelManager::CalcReclaimAppList(std::vector<std::shared_ptr<AppEntity>> &appList)
77 {
78     ReclaimPriorityManager::BunldeCopySet bundleSet;
79     ReclaimPriorityManager::GetInstance().GetBundlePrioSet(bundleSet);
80     for (auto bundleInfo : bundleSet) {
81         std::shared_ptr<AppEntity> app;
82         MAKE_POINTER(app, shared, AppEntity, "make shared failed", return false, bundleInfo.uid_, bundleInfo.name_);
83         appList.push_back(app);
84         HILOGI("The app will be reclaimed, uid:%{public}d, name:%{public}s.", app->uid_, app->name_.c_str());
85     }
86     return true;
87 }
88 
PsiHandlerInner()89 void MemoryLevelManager::PsiHandlerInner()
90 {
91     HILOGD("[%{public}ld] called", ++calledCount_);
92 
93     /* Calculate the system memory level */
94     SystemMemoryLevel level;
95     if (!CalcSystemMemoryLevel(level)) {
96         return;
97     }
98 
99     DECLARE_UNIQUE_POINTER(AppExecFwk::AppMgrClient, appMgrClient_);
100     MAKE_POINTER(appMgrClient_, unique, AppExecFwk::AppMgrClient, "make unique failed", return,
101         /* no param */);
102     switch (level) {
103         case SystemMemoryLevel::MEMORY_LEVEL_MODERATE:
104             appMgrClient_->NotifyMemoryLevel(AppExecFwk::MemoryLevel::MEMORY_LEVEL_MODERATE);
105             break;
106         case SystemMemoryLevel::MEMORY_LEVEL_LOW:
107             appMgrClient_->NotifyMemoryLevel(AppExecFwk::MemoryLevel::MEMORY_LEVEL_LOW);
108             break;
109         case SystemMemoryLevel::MEMORY_LEVEL_CRITICAL:
110             appMgrClient_->NotifyMemoryLevel(AppExecFwk::MemoryLevel::MEMORY_LEVEL_CRITICAL);
111             break;
112         default:
113             break;
114     }
115 }
116 
PsiHandler()117 void MemoryLevelManager::PsiHandler()
118 {
119     if (!initialized_) {
120         HILOGE("is not initialized, return!");
121         return;
122     }
123     std::function<void()> func = std::bind(&MemoryLevelManager::PsiHandlerInner, this);
124     handler_->PostImmediateTask(func);
125 }
126 } // namespace Memory
127 } // namespace OHOS
128