1 /*
2  * Copyright (c) 2023 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_COMMON_INCLUDE_PURGEABLE_MEM_UTILS_H
17 #define OHOS_MEMORY_MEMMGR_COMMON_INCLUDE_PURGEABLE_MEM_UTILS_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "single_instance.h"
23 
24 namespace OHOS {
25 namespace Memory {
26 struct PurgeableAshmInfo {
27     int sizeKB;
28     int minPriority;
29     std::string idWithTime;
30     std::string curAppName;
31 };
32 
33 using PurgeableAshmInfoMap = std::unordered_map<std::string, PurgeableAshmInfo>;
34 
35 class PurgeableMemUtils {
36     DECLARE_SINGLE_INSTANCE(PurgeableMemUtils);
37 
38 public:
39     bool GetPurgeableHeapInfo(int &reclaimableKB);
40     bool GetProcPurgeableHeapInfo(const int pid, int &reclaimableKB);
41     bool PurgeHeapAll();
42     bool PurgeHeapMemcg(const std::string &memcgPath, const int sizeKB);
43     bool GetPurgeableAshmInfo(int &reclaimableKB, std::vector<PurgeableAshmInfo> &ashmInfoToReclaim);
44     bool PurgeAshmAll();
45     bool PurgeAshmByIdWithTime(const std::string &idWithTime);
46     PurgeableAshmInfoMap GetashmIdToInfoMap(const std::vector<std::string> &strLines) const;
47 
48     static const std::string PATH_PURGE_HEAP;
49     static const std::string PATH_PURGEABLE_ASHMEM;
50     static const std::string FILE_PURGE_MEMCG_HEAP;
51     static const std::string ACTIVE_PURGEABLE_HEAP;
52     static const std::string INACTIVE_PURGEABLE_HEAP;
53     static const std::string PINED_PURGEABLE_HEAP;
54     static const std::string PROC_PURGEABLE_HEAP;
55     static const std::string PROC_PINED_PURGEABLE_HEAP;
56     static const unsigned int ASHM_PARAM_SIZE_ONE_LINE;
57     static const unsigned int ASHM_ID_INDEX;
58     static const unsigned int ASHM_ADJ_INDEX;
59     static const unsigned int ASHM_REF_COUNT_INDEX;
60     static const unsigned int ASHM_PURGED_INDEX;
61     static const unsigned int ASHM_SIZE_INDEX;
62     static const unsigned int ASHM_TIME_INDEX;
63     static const unsigned int HEAPINFO_SIZE_ONE_LINE;
64     static const unsigned int ASHM_PROCESS_NAME_INDEX;
65 };
66 } // namespace Memory
67 } // namespace OHOS
68 #endif // OHOS_MEMORY_MEMMGR_COMMON_INCLUDE_PURGEABLE_MEM_UTILS_H
69