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 std::function<void()> timerFunc_; 34 35 unsigned long long DAILY_SWAP_OUT_QUOTA_KB; 36 unsigned long long TOTAL_SWAP_OUT_QUOTA_KB; 37 38 unsigned long long lastSwapOutKB_ = 0; 39 unsigned long long nowSwapOutKB_ = 0; 40 41 unsigned long long minsToday_ = 0; 42 unsigned long long swapOutKBToday_ = 0; 43 44 unsigned long long minsSinceBirth_ = 0; 45 unsigned long long swapOutKBSinceBirth_ = 0; 46 47 unsigned long long iter_ = 0; 48 49 NandLifeController(); 50 bool GetEventHandler(); 51 bool IsNandLifeParamExist(); 52 void PrintNandLifeParam(); 53 bool LoadNandLifeParam(); 54 bool IsSwapOutClosedPermently(); 55 bool GetAndValidateNandLifeConfig(); 56 void SetTimer(); 57 void CheckSwapOut(); 58 bool GetSwapOutKBSinceKernelBoot(unsigned long long &ret); 59 bool CheckReachedDailyLimit(); 60 bool CheckReachedTotalLimit(); 61 bool SetParameterRetry(const std::string ¶mName, const std::string ¶mValue, int retryTimes); 62 bool UpdateNandLifeParam(); 63 64 void OpenSwapOutTemporarily(const std::string &reason); 65 void CloseSwapOutTemporarily(const std::string &reason); 66 void OpenSwapOutPermanently(); 67 void CloseSwapOutPermanently(); 68 }; 69 } // namespace Memory 70 } // namespace OHOS 71 #endif // OHOS_MEMORY_MEMMGR_NANDLIFE_CONTROLLER_H 72