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_NANDLIFE_CONTROLLER_H
17 #define OHOS_MEMORY_MEMMGR_NANDLIFE_CONTROLLER_H
18 
19 #include "single_instance.h"
20 #include "event_handler.h"
21 #include "memmgr_config_manager.h"
22 
23 namespace OHOS {
24 namespace Memory {
25 class NandLifeController {
26     DECLARE_SINGLE_INSTANCE_BASE(NandLifeController);
27 
28 public:
29     bool Init();
30 private:
31     std::shared_ptr<AppExecFwk::EventHandler> handler_;
32     NandLifeConfig config_;
33 
34     unsigned long long DAILY_SWAP_OUT_QUOTA_KB;
35     unsigned long long TOTAL_SWAP_OUT_QUOTA_KB;
36 
37     unsigned long long lastSwapOutKB_ = 0;
38     unsigned long long nowSwapOutKB_ = 0;
39 
40     unsigned long long minsToday_ = 0;
41     unsigned long long swapOutKBToday_ = 0;
42 
43     unsigned long long minsSinceBirth_ = 0;
44     unsigned long long swapOutKBSinceBirth_ = 0;
45 
46     unsigned long long iter_ = 0;
47 
48     NandLifeController();
49     bool GetEventHandler();
50     bool IsNandLifeParamExist();
51     void PrintNandLifeParam();
52     bool LoadNandLifeParam();
53     bool IsSwapOutClosedPermently();
54     bool GetAndValidateNandLifeConfig();
55     void SetTimer();
56     void CheckSwapOut();
57     bool GetSwapOutKBSinceKernelBoot(unsigned long long &ret);
58     bool CheckReachedDailyLimit();
59     bool CheckReachedTotalLimit();
60     bool SetParameterRetry(const std::string &paramName, const std::string &paramValue, int retryTimes);
61     bool UpdateNandLifeParam();
62 
63     void OpenSwapOutTemporarily(const std::string &reason);
64     void CloseSwapOutTemporarily(const std::string &reason);
65     void OpenSwapOutPermanently();
66     void CloseSwapOutPermanently();
67 };
68 } // namespace Memory
69 } // namespace OHOS
70 #endif // OHOS_MEMORY_MEMMGR_NANDLIFE_CONTROLLER_H
71