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_RECLAIM_STRATEGY_MEMCG_H 17 #define OHOS_MEMORY_MEMMGR_RECLAIM_STRATEGY_MEMCG_H 18 19 #include <string> 20 21 namespace OHOS { 22 namespace Memory { 23 class SwapInfo final { 24 public: 25 unsigned int swapOutCount_; 26 unsigned int swapOutSize_; 27 unsigned int swapInCount_; 28 unsigned int swapInSize_; 29 unsigned int pageInCount_; 30 unsigned int swapSizeCurr_; 31 unsigned int swapSizeMax_; 32 33 SwapInfo(); 34 SwapInfo(unsigned int swapOutCount, unsigned int swapOutSize, unsigned int swapInCount, 35 unsigned int swapInSize, unsigned int pageInCount, unsigned int swapSizeCurr, unsigned int swapSizeMax); 36 std::string ToString() const; 37 38 SwapInfo(const SwapInfo&) = delete; 39 SwapInfo& operator=(const SwapInfo&) = delete; 40 SwapInfo(SwapInfo&&) = delete; 41 SwapInfo& operator=(SwapInfo&&) = delete; 42 }; // end class SwapInfo 43 44 class MemInfo final { 45 public: 46 unsigned int anonKiB_; 47 unsigned int zramKiB_; 48 unsigned int eswapKiB_; 49 50 MemInfo(); 51 MemInfo(unsigned int anonKiB, unsigned int zramKiB, unsigned int eswapKiB); 52 std::string ToString() const; 53 54 MemInfo(const MemInfo&) = delete; 55 MemInfo& operator=(const MemInfo&) = delete; 56 MemInfo(MemInfo&&) = delete; 57 MemInfo& operator=(MemInfo&&) = delete; 58 }; // end class MemInfo 59 60 class ReclaimRatios final { 61 public: 62 unsigned int mem2zramRatio_; 63 unsigned int zram2ufsRatio_; 64 unsigned int refaultThreshold_; 65 66 ReclaimRatios(); 67 ReclaimRatios(unsigned int mem2zramRatio, unsigned int zram2ufsRatio, unsigned int refaultThreshold); 68 void SetRatiosByValue(unsigned int mem2zramRatio, unsigned int zram2ufsRatio, unsigned int refaultThreshold); 69 void SetRatios(const ReclaimRatios& ratios); 70 std::string NumsToString() const; // only nums, not easy for reading 71 std::string ToString() const; // easy for reading 72 73 ReclaimRatios(const ReclaimRatios&) = delete; 74 ReclaimRatios& operator=(const ReclaimRatios&) = delete; 75 ReclaimRatios(ReclaimRatios&&) = delete; 76 ReclaimRatios& operator=(ReclaimRatios&&) = delete; 77 }; // end class ReclaimRatios 78 79 class Memcg { 80 public: 81 int score_; 82 SwapInfo* swapInfo_; 83 MemInfo* memInfo_; 84 ReclaimRatios* reclaimRatios_; 85 86 Memcg(); 87 virtual ~Memcg(); 88 Memcg(const Memcg&) = delete; 89 Memcg& operator=(const Memcg&) = delete; 90 Memcg(Memcg&&) = delete; 91 Memcg& operator=(Memcg&&) = delete; 92 93 bool UpdateSwapInfoFromKernel(); 94 bool UpdateMemInfoFromKernel(); 95 96 void SetScore(int score); 97 void SetReclaimRatios(unsigned int mem2zramRatio, unsigned int zram2ufsRatio, unsigned int refaultThreshold); 98 bool SetReclaimRatios(const ReclaimRatios& ratios); 99 bool SetScoreAndReclaimRatiosToKernel(); 100 bool SwapIn(); // 100% load to mem 101 protected: 102 virtual std::string GetMemcgPath_(); 103 bool WriteToFile_(const std::string& path, const std::string& content, bool truncated = true); 104 bool ReadScoreAndReclaimRatiosFromKernel_(int& score, unsigned int& mem2zramRatio, unsigned int& zram2ufsRatio, 105 unsigned int& refaultThreshold); 106 }; // end class Memcg 107 108 class UserMemcg final : public Memcg { 109 public: 110 unsigned int userId_; 111 112 explicit UserMemcg(unsigned int userId); 113 ~UserMemcg(); 114 UserMemcg() = delete; 115 UserMemcg(const UserMemcg&) = delete; 116 UserMemcg& operator=(const UserMemcg&) = delete; 117 UserMemcg(UserMemcg&&) = delete; 118 UserMemcg& operator=(UserMemcg&&) = delete; 119 120 bool CreateMemcgDir(); 121 bool RemoveMemcgDir(); 122 bool AddProc(unsigned int pid); 123 protected: 124 std::string GetMemcgPath_() final; 125 }; // end class UserMemcg 126 } // namespace Memory 127 } // namespace OHOS 128 #endif // OHOS_MEMORY_MEMMGR_RECLAIM_STRATEGY_MEMCG_H 129