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_RECALIM_STRATEGY_MANAGER_H
17 #define OHOS_MEMORY_MEMMGR_RECALIM_STRATEGY_MANAGER_H
18 
19 #include "single_instance.h"
20 #include "event_handler.h"
21 #include "memcg_mgr.h"
22 #include "reclaim_param.h"
23 
24 namespace OHOS {
25 namespace Memory {
26 class ReclaimStrategyManager {
27     DECLARE_SINGLE_INSTANCE_BASE(ReclaimStrategyManager);
28 
29 public:
30     bool Init();
31     void NotifyAppStateChanged(std::shared_ptr<ReclaimParam> reclaimPara);
32     void NotifyAccountDied(int accountId);
33     void NotifyAccountPriorityChanged(int accountId, int priority);
34 
Initailized()35     inline bool Initailized()
36     {
37         return initialized_;
38     };
39     //this method is only used for class PurgeableMemManager
40     std::shared_ptr<AppExecFwk::EventHandler> GetEventHandler() const;
41 private:
42     bool initialized_ = false;
43     std::shared_ptr<AppExecFwk::EventHandler> handler_;
44 
45     ReclaimStrategyManager();
46     bool CreateEventHandler();
47     void InitProcessBeforeMemmgr();
48 
49     // handle app and os user event
50     bool HandleAppStateChanged_(std::shared_ptr<ReclaimParam> reclaimPara);
51     bool HandleProcessCreate_(std::shared_ptr<ReclaimParam> reclaimPara);
52     bool HandleAccountDied_(int accountId);
53     bool HandleAccountPriorityChanged_(int accountId, int priority);
54 
55     // get param for config_mgr
56     bool GetReclaimRatiosByScore_(int score, ReclaimRatios& ratios);
57     void GetValidScore_(int& priority);
58 };
59 } // namespace Memory
60 } // namespace OHOS
61 #endif // OHOS_MEMORY_MEMMGR_RECALIM_STRATEGY_MANAGER_H
62