1ba5c3796Sopenharmony_ci/*
2ba5c3796Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3ba5c3796Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4ba5c3796Sopenharmony_ci * you may not use this file except in compliance with the License.
5ba5c3796Sopenharmony_ci * You may obtain a copy of the License at
6ba5c3796Sopenharmony_ci *
7ba5c3796Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8ba5c3796Sopenharmony_ci *
9ba5c3796Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10ba5c3796Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11ba5c3796Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12ba5c3796Sopenharmony_ci * See the License for the specific language governing permissions and
13ba5c3796Sopenharmony_ci * limitations under the License.
14ba5c3796Sopenharmony_ci */
15ba5c3796Sopenharmony_ci
16ba5c3796Sopenharmony_ci#ifndef OHOS_MEMORY_MEMMGR_RECLAIM_STRATEGY_MEMCG_H
17ba5c3796Sopenharmony_ci#define OHOS_MEMORY_MEMMGR_RECLAIM_STRATEGY_MEMCG_H
18ba5c3796Sopenharmony_ci
19ba5c3796Sopenharmony_ci#include <string>
20ba5c3796Sopenharmony_ci
21ba5c3796Sopenharmony_cinamespace OHOS {
22ba5c3796Sopenharmony_cinamespace Memory {
23ba5c3796Sopenharmony_ciclass SwapInfo final {
24ba5c3796Sopenharmony_cipublic:
25ba5c3796Sopenharmony_ci    unsigned int swapOutCount_;
26ba5c3796Sopenharmony_ci    unsigned int swapOutSize_;
27ba5c3796Sopenharmony_ci    unsigned int swapInCount_;
28ba5c3796Sopenharmony_ci    unsigned int swapInSize_;
29ba5c3796Sopenharmony_ci    unsigned int pageInCount_;
30ba5c3796Sopenharmony_ci    unsigned int swapSizeCurr_;
31ba5c3796Sopenharmony_ci    unsigned int swapSizeMax_;
32ba5c3796Sopenharmony_ci
33ba5c3796Sopenharmony_ci    SwapInfo();
34ba5c3796Sopenharmony_ci    SwapInfo(unsigned int swapOutCount, unsigned int swapOutSize, unsigned int swapInCount,
35ba5c3796Sopenharmony_ci        unsigned int swapInSize, unsigned int pageInCount, unsigned int swapSizeCurr, unsigned int swapSizeMax);
36ba5c3796Sopenharmony_ci    std::string ToString() const;
37ba5c3796Sopenharmony_ci
38ba5c3796Sopenharmony_ci    SwapInfo(const SwapInfo&) = delete;
39ba5c3796Sopenharmony_ci    SwapInfo& operator=(const SwapInfo&) = delete;
40ba5c3796Sopenharmony_ci    SwapInfo(SwapInfo&&) = delete;
41ba5c3796Sopenharmony_ci    SwapInfo& operator=(SwapInfo&&) = delete;
42ba5c3796Sopenharmony_ci}; // end class SwapInfo
43ba5c3796Sopenharmony_ci
44ba5c3796Sopenharmony_ciclass MemInfo final {
45ba5c3796Sopenharmony_cipublic:
46ba5c3796Sopenharmony_ci    unsigned int anonKiB_;
47ba5c3796Sopenharmony_ci    unsigned int zramKiB_;
48ba5c3796Sopenharmony_ci    unsigned int eswapKiB_;
49ba5c3796Sopenharmony_ci
50ba5c3796Sopenharmony_ci    MemInfo();
51ba5c3796Sopenharmony_ci    MemInfo(unsigned int anonKiB, unsigned int zramKiB, unsigned int eswapKiB);
52ba5c3796Sopenharmony_ci    std::string ToString() const;
53ba5c3796Sopenharmony_ci
54ba5c3796Sopenharmony_ci    MemInfo(const MemInfo&) = delete;
55ba5c3796Sopenharmony_ci    MemInfo& operator=(const MemInfo&) = delete;
56ba5c3796Sopenharmony_ci    MemInfo(MemInfo&&) = delete;
57ba5c3796Sopenharmony_ci    MemInfo& operator=(MemInfo&&) = delete;
58ba5c3796Sopenharmony_ci}; // end class MemInfo
59ba5c3796Sopenharmony_ci
60ba5c3796Sopenharmony_ciclass ReclaimRatios final {
61ba5c3796Sopenharmony_cipublic:
62ba5c3796Sopenharmony_ci    unsigned int mem2zramRatio_;
63ba5c3796Sopenharmony_ci    unsigned int zram2ufsRatio_;
64ba5c3796Sopenharmony_ci    unsigned int refaultThreshold_;
65ba5c3796Sopenharmony_ci
66ba5c3796Sopenharmony_ci    ReclaimRatios();
67ba5c3796Sopenharmony_ci    ReclaimRatios(unsigned int mem2zramRatio, unsigned int zram2ufsRatio, unsigned int refaultThreshold);
68ba5c3796Sopenharmony_ci    void SetRatiosByValue(unsigned int mem2zramRatio, unsigned int zram2ufsRatio, unsigned int refaultThreshold);
69ba5c3796Sopenharmony_ci    void SetRatios(const ReclaimRatios& ratios);
70ba5c3796Sopenharmony_ci    std::string NumsToString() const; // only nums, not easy for reading
71ba5c3796Sopenharmony_ci    std::string ToString() const; // easy for reading
72ba5c3796Sopenharmony_ci
73ba5c3796Sopenharmony_ci    ReclaimRatios(const ReclaimRatios&) = delete;
74ba5c3796Sopenharmony_ci    ReclaimRatios& operator=(const ReclaimRatios&) = delete;
75ba5c3796Sopenharmony_ci    ReclaimRatios(ReclaimRatios&&) = delete;
76ba5c3796Sopenharmony_ci    ReclaimRatios& operator=(ReclaimRatios&&) = delete;
77ba5c3796Sopenharmony_ci}; // end class ReclaimRatios
78ba5c3796Sopenharmony_ci
79ba5c3796Sopenharmony_ciclass Memcg {
80ba5c3796Sopenharmony_cipublic:
81ba5c3796Sopenharmony_ci    int score_;
82ba5c3796Sopenharmony_ci    SwapInfo* swapInfo_;
83ba5c3796Sopenharmony_ci    MemInfo* memInfo_;
84ba5c3796Sopenharmony_ci    ReclaimRatios* reclaimRatios_;
85ba5c3796Sopenharmony_ci
86ba5c3796Sopenharmony_ci    Memcg();
87ba5c3796Sopenharmony_ci    virtual ~Memcg();
88ba5c3796Sopenharmony_ci    Memcg(const Memcg&) = delete;
89ba5c3796Sopenharmony_ci    Memcg& operator=(const Memcg&) = delete;
90ba5c3796Sopenharmony_ci    Memcg(Memcg&&) = delete;
91ba5c3796Sopenharmony_ci    Memcg& operator=(Memcg&&) = delete;
92ba5c3796Sopenharmony_ci
93ba5c3796Sopenharmony_ci    bool UpdateMemInfoFromKernel();
94ba5c3796Sopenharmony_ci
95ba5c3796Sopenharmony_ci    void SetScore(int score);
96ba5c3796Sopenharmony_ci    void SetReclaimRatios(unsigned int mem2zramRatio, unsigned int zram2ufsRatio, unsigned int refaultThreshold);
97ba5c3796Sopenharmony_ci    bool SetReclaimRatios(const ReclaimRatios& ratios);
98ba5c3796Sopenharmony_ci    bool SetScoreAndReclaimRatiosToKernel();
99ba5c3796Sopenharmony_ci    bool SwapIn(); // 100% load to mem
100ba5c3796Sopenharmony_ci    virtual std::string GetMemcgPath_();
101ba5c3796Sopenharmony_ciprotected:
102ba5c3796Sopenharmony_ci    bool WriteToFile_(const std::string& path, const std::string& content, bool truncated = true);
103ba5c3796Sopenharmony_ci    bool ReadScoreAndReclaimRatiosFromKernel_(int& score, unsigned int& mem2zramRatio, unsigned int& zram2ufsRatio,
104ba5c3796Sopenharmony_ci                                              unsigned int& refaultThreshold);
105ba5c3796Sopenharmony_ci}; // end class Memcg
106ba5c3796Sopenharmony_ci
107ba5c3796Sopenharmony_ciclass UserMemcg final : public Memcg {
108ba5c3796Sopenharmony_cipublic:
109ba5c3796Sopenharmony_ci    unsigned int userId_;
110ba5c3796Sopenharmony_ci
111ba5c3796Sopenharmony_ci    explicit UserMemcg(unsigned int userId);
112ba5c3796Sopenharmony_ci    ~UserMemcg();
113ba5c3796Sopenharmony_ci    UserMemcg() = delete;
114ba5c3796Sopenharmony_ci    UserMemcg(const UserMemcg&) = delete;
115ba5c3796Sopenharmony_ci    UserMemcg& operator=(const UserMemcg&) = delete;
116ba5c3796Sopenharmony_ci    UserMemcg(UserMemcg&&) = delete;
117ba5c3796Sopenharmony_ci    UserMemcg& operator=(UserMemcg&&) = delete;
118ba5c3796Sopenharmony_ci
119ba5c3796Sopenharmony_ci    bool CreateMemcgDir();
120ba5c3796Sopenharmony_ci    bool RemoveMemcgDir();
121ba5c3796Sopenharmony_ci    bool AddProc(unsigned int pid);
122ba5c3796Sopenharmony_ci    std::string GetMemcgPath_() final;
123ba5c3796Sopenharmony_ci}; // end class UserMemcg
124ba5c3796Sopenharmony_ci} // namespace Memory
125ba5c3796Sopenharmony_ci} // namespace OHOS
126ba5c3796Sopenharmony_ci#endif // OHOS_MEMORY_MEMMGR_RECLAIM_STRATEGY_MEMCG_H
127