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