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 #ifndef OHOS_MEMORY_MEMMGR_PURGEABLE_MEM_MANAGER_H
17 #define OHOS_MEMORY_MEMMGR_PURGEABLE_MEM_MANAGER_H
18 
19 #include <map>
20 #include <unordered_map>
21 
22 #include "app_state_subscriber.h"
23 #include "event_handler.h"
24 #include "kernel_interface.h"
25 #include "memory_level_constants.h"
26 #include "purgeable_mem_constants.h"
27 #include "purgeable_mem_utils.h"
28 #include "remote_death_recipient.h"
29 #include "single_instance.h"
30 
31 namespace OHOS {
32 namespace Memory {
33 struct PurgeableMemoryInfo {
34     PurgeableMemoryType type;
35     int reclaimableKB;
36     std::vector<PurgeableAshmInfo> ashmInfoToReclaim;
37 };
38 
39 struct DumpReclaimInfo {
40     PurgeableMemoryType reclaimType;
41     bool ifReclaimTypeAll;
42     int memcgUserId;
43     int reclaimHeapSizeKB;
44     unsigned int ashmId;
45     unsigned int ashmTime;
46     int subscriberPid;
47 };
48 
49 class PurgeableMemManager {
50     DECLARE_SINGLE_INSTANCE_BASE(PurgeableMemManager);
51 
52 public:
53     void NotifyMemoryLevel(const SystemMemoryInfo &info);
54     void RegisterActiveApps(int32_t pid, int32_t uid);
55     void DeregisterActiveApps(int32_t pid, int32_t uid);
56     void ChangeAppState(int32_t pid, int32_t uid, int32_t state);
57     void AddSubscriber(const sptr<IAppStateSubscriber> &subscriber);
58     void RemoveSubscriber(const sptr<IAppStateSubscriber> &subscriber);
59     void OnRemoteSubscriberDied(const wptr<IRemoteObject> &object);
60     void DumpSubscribers(const int fd);
61     bool ForceReclaimByDump(const DumpReclaimInfo &dumpInfo);
62     bool IsPurgeWhiteApp(const std::string &curAppName);
63 
64 private:
65     PurgeableMemManager();
66     ~PurgeableMemManager() = default;
67     void NotifyMemoryLevelInner(const SystemMemoryInfo &info);
68     void TriggerByManualDump(const SystemMemoryInfo &info);
69     void TriggerByPsi(const SystemMemoryInfo &info);
70     int PurgeByTypeAndTarget(const PurgeableMemoryType &type, const int reclaimTargetKB);
71     bool GetPurgeableInfo(PurgeableMemoryInfo &info);
72     bool GetMemcgPathByUserId(const int userId, std::string &memcgPath);
73     bool PurgeTypeAll(const PurgeableMemoryType &type);
74     bool PurgeHeap(const int userId, const int size);
75     bool PurgeAshm(const unsigned int ashmId, const unsigned int time);
76     void PurgHeapMemcgOneByOne(const int reclaimTargetKB, int &reclaimResultKB);
77     bool PurgHeapOneMemcg(const std::vector<int> &memcgPids, const std::string &memcgPath, const int reclaimTargetKB,
78                           int &reclaimResultKB);
79     void PurgAshmIdOneByOne(std::vector<PurgeableAshmInfo> &ashmInfoToReclaim, const int reclaimTargetKB,
80                             int &reclaimResultKB);
81     bool AshmReclaimPriorityCompare(const PurgeableAshmInfo &left, const PurgeableAshmInfo &right);
82     std::string PurgMemType2String(const PurgeableMemoryType &type);
83     void RegisterActiveAppsInner(int32_t pid, int32_t uid);
84     void DeregisterActiveAppsInner(int32_t pid, int32_t uid);
85     void AddSubscriberInner(const sptr<IAppStateSubscriber> &subscriber);
86     void RemoveSubscriberInner(const sptr<IAppStateSubscriber> &subscriber);
87     void OnRemoteSubscriberDiedInner(const wptr<IRemoteObject> &object);
88     void ChangeAppStateInner(int32_t pid, int32_t uid, int32_t state);
89     void TrimAllSubscribers(const SystemMemoryLevel &level);
90     void ReclaimSubscriberProc(const int32_t pid);
91     void ReclaimSubscriberAll();
92     bool GetEventHandler();
93     bool CheckCallingToken();
94     std::shared_ptr<AppExecFwk::EventHandler> handler_;
95     bool initialized_ = false;
96     std::map<int32_t, std::pair<int32_t, int32_t>> appList_;
97     std::list<sptr<IAppStateSubscriber>> appStateSubscribers_ {};
98     std::map<sptr<IRemoteObject>, sptr<RemoteDeathRecipient>> subscriberRecipients_ {};
99     std::mutex mutexAppList_;
100     std::mutex mutexSubscribers_;
101     time_t lastTriggerTime_ = 0; // last trigger time by psi or kswapd.
102 };
103 } // namespace Memory
104 } // namespace OHOS
105 #endif // OHOS_MEMORY_MEMMGR_PURGEABLE_MEM_MANAGER_H
106