• 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 "app_state_observer.h"
17 #include <sstream>
18 #include "kernel_interface.h"
19 #include "mem_mgr_event_center.h"
20 #include "memmgr_log.h"
21 #include "reclaim_priority_manager.h"
22 
23 #ifdef USE_PURGEABLE_MEMORY
24 #include "purgeable_mem_manager.h"
25 #endif
26 
27 namespace OHOS {
28 namespace Memory {
29 namespace {
30 const std::string TAG = "AppStateObserver";
31 const std::string SLASH = "/";
32 constexpr int RSS_THRESHOLD_RATIO = 80;
33 }
34 
OnForegroundApplicationChanged(const AppExecFwk::AppStateData & appStateData)35 void AppStateObserver::OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData)
36 {
37     // no pid here !
38     HILOGI("uid=%{public}d, bundleName=%{public}s, state=%{public}d, ",
39         appStateData.uid, appStateData.bundleName.c_str(), appStateData.state);
40 #ifdef USE_PURGEABLE_MEMORY
41     PurgeableMemManager::GetInstance().ChangeAppState(appStateData.pid, appStateData.uid, appStateData.state);
42 #endif
43 }
44 
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)45 void AppStateObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData)
46 {
47     auto stateReasonPair = stateReasonMap_.find(abilityStateData.abilityState);
48     auto stateReasonStrPair = stateReasonStrMap_.find(abilityStateData.abilityState);
49     if (stateReasonPair != stateReasonMap_.end() && stateReasonStrPair != stateReasonStrMap_.end()) {
50         AppStateUpdateReason reason = stateReasonPair->second;
51         std::string reasonStr = stateReasonStrPair->second;
52         ReclaimHandleRequest request;
53         request.pid = abilityStateData.pid;
54         request.uid = abilityStateData.uid;
55         request.bundleName = abilityStateData.bundleName;
56         request.reason = reason;
57         ReclaimPriorityManager::GetInstance().UpdateReclaimPriority(request);
58         HILOGI("called, uid=%{public}d, pid=%{public}d, bundleName=%{public}s %{public}s",
59             abilityStateData.uid, abilityStateData.pid, abilityStateData.bundleName.c_str(), reasonStr.c_str());
60     } else {
61         HILOGI("called, uid=%{public}d, pid=%{public}d, bundleName=%{public}s %{public}s",
62             abilityStateData.uid, abilityStateData.pid, abilityStateData.bundleName.c_str(), "Skiped!");
63     }
64 }
65 
OnExtensionStateChanged(const AppExecFwk::AbilityStateData & extensionStateData)66 void AppStateObserver::OnExtensionStateChanged(const AppExecFwk::AbilityStateData &extensionStateData)
67 {
68 }
69 
OnProcessCreated(const AppExecFwk::ProcessData & processData)70 void AppStateObserver::OnProcessCreated(const AppExecFwk::ProcessData &processData)
71 {
72     int32_t renderUid = processData.renderUid;
73     int uid = -1;
74     std::stringstream ss;
75 
76     ss << KernelInterface::ROOT_PROC_PATH << SLASH << processData.pid << KernelInterface::RSS_THRESHOLD_PATH;
77     std::string path = ss.str();
78     int32_t rssThresoldMax = KernelInterface::GetInstance().GetTotalBuffer() * RSS_THRESHOLD_RATIO / 100;
79     std::string content = std::to_string(rssThresoldMax);
80     KernelInterface::GetInstance().EchoToPath(path.c_str(), content.c_str());
81     if (renderUid != -1) {
82         uid = renderUid;
83     } else {
84         uid = processData.uid;
85     }
86     HILOGI("uid=%{public}d, pid=%{public}d, bundle=%{public}s", uid, processData.pid, processData.bundleName.c_str());
87     ReclaimPriorityManager::GetInstance().UpdateReclaimPriority(
88         SingleRequest(processData.pid, uid, processData.bundleName, AppStateUpdateReason::CREATE_PROCESS));
89 }
90 
OnProcessDied(const AppExecFwk::ProcessData & processData)91 void AppStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData)
92 {
93     int32_t renderUid = processData.renderUid;
94     int uid = -1;
95 
96     if (renderUid != -1) {
97         uid = renderUid;
98     } else {
99         uid = processData.uid;
100     }
101     HILOGD("uid=%{public}d, pid=%{public}d, bundle=%{public}s", uid, processData.pid, processData.bundleName.c_str());
102     ReclaimPriorityManager::GetInstance().UpdateReclaimPriority(
103         SingleRequest(processData.pid, uid, processData.bundleName, AppStateUpdateReason::PROCESS_TERMINATED));
104     MemMgrEventCenter::GetInstance().OnProcessDied(processData.pid);
105 }
106 } // namespace Memory
107 } // namespace OHOS
108