• 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_service.h"
17 #include "memmgr_log.h"
18 #include "system_ability_definition.h"
19 #include "memmgr_config_manager.h"
20 #include "mem_mgr_event_center.h"
21 #include "nandlife_controller.h"
22 #include "reclaim_priority_manager.h"
23 #include "reclaim_strategy_manager.h"
24 #include "multi_account_manager.h"
25 
26 #include <unistd.h>
27 
28 namespace OHOS {
29 namespace Memory {
30 namespace {
31 const std::string TAG = "MemMgrService";
32 }
33 
34 IMPLEMENT_SINGLE_INSTANCE(MemMgrService);
35 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&MemMgrService::GetInstance());
36 
MemMgrService()37 MemMgrService::MemMgrService() : SystemAbility(MEMORY_MANAGER_SA_ID, true)
38 {
39 }
40 
Init()41 bool MemMgrService::Init()
42 {
43     MemmgrConfigManager::GetInstance().Init();
44 
45     // init reclaim priority manager
46     if (!ReclaimPriorityManager::GetInstance().Init()) {
47         HILOGE("ReclaimPriorityManager init failed");
48         return false;
49     }
50 
51     // init multiple account manager
52     MultiAccountManager::GetInstance().Init();
53 
54     // init reclaim strategy manager
55     if (!ReclaimStrategyManager::GetInstance().Init()) {
56         HILOGE("ReclaimStrategyManager init failed");
57         return false;
58     }
59 
60     // init event center, then managers above can work by event trigger
61     if (!MemMgrEventCenter::GetInstance().Init()) {
62         HILOGE("MemMgrEventCenter init failed");
63         return false;
64     }
65 
66     // init nandlife controller
67     NandLifeController::GetInstance().Init();
68     HILOGI("init successed");
69     return true;
70 }
71 
OnStart()72 void MemMgrService::OnStart()
73 {
74     HILOGI("called");
75     if (!Init()) {
76         HILOGE("init failed");
77         return;
78     }
79     if (!Publish(this)) {
80         HILOGE("publish SA failed");
81         return;
82     }
83     HILOGI("publish SA successed");
84 
85     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
86     AddSystemAbilityListener(COMMON_EVENT_SERVICE_ABILITY_ID);
87     AddSystemAbilityListener(BACKGROUND_TASK_MANAGER_SERVICE_ID);
88     AddSystemAbilityListener(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN);
89     AddSystemAbilityListener(SUBSYS_APPLICATIONS_SYS_ABILITY_ID_BEGIN);
90     AddSystemAbilityListener(APP_MGR_SERVICE_ID);
91     AddSystemAbilityListener(ABILITY_MGR_SERVICE_ID);
92 }
93 
OnStop()94 void MemMgrService::OnStop()
95 {
96     HILOGI("called");
97 }
98 
99 // implements of innerkits list below
100 
GetBundlePriorityList(BundlePriorityList & bundlePrioList)101 int32_t MemMgrService::GetBundlePriorityList(BundlePriorityList &bundlePrioList)
102 {
103     HILOGI("called");
104     ReclaimPriorityManager::BunldeCopySet bundleSet;
105     ReclaimPriorityManager::GetInstance().GetBundlePrioSet(bundleSet);
106     for (auto bundlePriorityInfo : bundleSet) {
107         Memory::BundlePriority *bi = new Memory::BundlePriority(bundlePriorityInfo.uid_,
108             bundlePriorityInfo.name_, bundlePriorityInfo.priority_, bundlePriorityInfo.accountId_);
109         bundlePrioList.AddBundleInfo(*bi);
110     }
111     bundlePrioList.SetCount(bundlePrioList.Size());
112     return 0;
113 }
114 
NotifyDistDevStatus(int32_t pid,int32_t uid,const std::string & name,bool connected)115 int32_t MemMgrService::NotifyDistDevStatus(int32_t pid, int32_t uid, const std::string &name, bool connected)
116 {
117     HILOGI("called, pid=%{public}d, uid=%{public}d, name=%{public}s, connected=%{public}d", pid, uid, name.c_str(),
118         connected);
119     ReclaimPriorityManager::GetInstance().UpdateReclaimPriority(pid, uid, name,
120         connected ? AppStateUpdateReason::DIST_DEVICE_CONNECTED : AppStateUpdateReason::DIST_DEVICE_DISCONNECTED);
121     return 0;
122 }
123 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)124 void MemMgrService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
125 {
126     HILOGI("systemAbilityId: %{public}d add", systemAbilityId);
127     MemMgrEventCenter::GetInstance().RetryRegisterEventObserver(systemAbilityId);
128 }
129 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)130 void MemMgrService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
131 {
132     HILOGI("systemAbilityId: %{public}d remove", systemAbilityId);
133     MemMgrEventCenter::GetInstance().RemoveEventObserver(systemAbilityId);
134 }
135 
ShowHelpInfo(int fd)136 void ShowHelpInfo(int fd) {
137     dprintf(fd, "Usage:\n");
138     dprintf(fd, "-h                          |help for memmgrservice dumper\n");
139     dprintf(fd, "-a                          |dump all info\n");
140     dprintf(fd, "-e                          |dump event observer\n");
141     dprintf(fd, "-r                          |dump reclaim info and adj\n");
142     dprintf(fd, "-c                          |dump config\n");
143 }
144 
Dump(int fd,const std::vector<std::u16string> & args)145 int MemMgrService::Dump(int fd, const std::vector<std::u16string> &args)
146 {
147     HILOGI("called");
148     std::vector<std::string> params;
149     for (auto& arg : args) {
150         params.emplace_back(Str16ToStr8(arg));
151     }
152 
153     if (params.empty() || (params.size() == 1 && params[0] == "-h")) {
154         ShowHelpInfo(fd);
155     } else if (params.size() == 1 && params[0] == "-a") {
156         MemMgrEventCenter::GetInstance().Dump(fd);
157         ReclaimPriorityManager::GetInstance().Dump(fd);
158     } else if (params.size() == 1 && params[0] == "-e") {
159         MemMgrEventCenter::GetInstance().Dump(fd);
160     } else if (params.size() == 1 && params[0] == "-r") {
161         ReclaimPriorityManager::GetInstance().Dump(fd);
162     } else if (params.size() == 1 && params[0] == "-c") {
163         MemmgrConfigManager::GetInstance().Dump(fd);
164     }
165     return 0;
166 }
167 } // namespace Memory
168 } // namespace OHOS
169